Skip to content

Commit

Permalink
fabric batch operations tests
Browse files Browse the repository at this point in the history
Signed-off-by: Fedor Partanskiy <[email protected]>
  • Loading branch information
pfi79 committed Sep 2, 2024
1 parent 951e95a commit b522057
Show file tree
Hide file tree
Showing 48 changed files with 852 additions and 264 deletions.
106 changes: 105 additions & 1 deletion core/chaincode/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ func (h *Handler) handleMessageReadyState(msg *pb.ChaincodeMessage) error {
go h.HandleTransaction(msg, h.HandlePutStateMetadata)
case pb.ChaincodeMessage_PURGE_PRIVATE_DATA:
go h.HandleTransaction(msg, h.HandlePurgePrivateData)
case pb.ChaincodeMessage_PUT_STATE_BATCH:
go h.HandleTransaction(msg, h.HandlePutStateBatch)
default:
return fmt.Errorf("[%s] Fabric side handler cannot handle message (%s) while in ready state", msg.Txid, msg.Type)
}
Expand Down Expand Up @@ -441,7 +443,19 @@ func (h *Handler) ProcessStream(stream ccintf.ChaincodeStream) error {
// sendReady sends READY to chaincode serially (just like REGISTER)
func (h *Handler) sendReady() error {
chaincodeLogger.Debugf("sending READY for chaincode %s", h.chaincodeID)
ccMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_READY}

chaincodeAdditionalParams := &pb.ChaincodeAdditionalParams{
UsePutStateBatch: true,
MaxSizePutStateBatch: 1000,
}
payloadBytes, err := proto.Marshal(chaincodeAdditionalParams)
if err != nil {
return errors.WithStack(err)
}
ccMsg := &pb.ChaincodeMessage{
Type: pb.ChaincodeMessage_READY,
Payload: payloadBytes,
}

// if error in sending tear down the h
if err := h.serialSend(ccMsg); err != nil {
Expand Down Expand Up @@ -1153,6 +1167,96 @@ func (h *Handler) HandlePurgePrivateData(msg *pb.ChaincodeMessage, txContext *Tr
return &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_RESPONSE, Txid: msg.Txid, ChannelId: msg.ChannelId}, nil
}

func (h *Handler) HandlePutStateBatch(msg *pb.ChaincodeMessage, txContext *TransactionContext) (*pb.ChaincodeMessage, error) {
batch := &pb.PutStateBatch{}
err := proto.Unmarshal(msg.Payload, batch)
if err != nil {
return nil, errors.Wrap(err, "unmarshal failed")
}

namespaceID := txContext.NamespaceID
for _, kv := range batch.GetKvs() {
collection := kv.GetCollection()

switch kv.Type {
case pb.StateKV_PUT_STATE:
if isCollectionSet(collection) {
if txContext.IsInitTransaction {
return nil, errors.New("private data APIs are not allowed in chaincode Init()")
}
if err = errorIfCreatorHasNoWritePermission(namespaceID, collection, txContext); err != nil {
return nil, err
}
err = txContext.TXSimulator.SetPrivateData(namespaceID, collection, kv.GetKey(), kv.GetValue())
} else {
err = txContext.TXSimulator.SetState(namespaceID, kv.GetKey(), kv.GetValue())
}

case pb.StateKV_DEL_STATE:
if isCollectionSet(collection) {
if txContext.IsInitTransaction {
return nil, errors.New("private data APIs are not allowed in chaincode Init()")
}
if err = errorIfCreatorHasNoWritePermission(namespaceID, collection, txContext); err != nil {
return nil, err
}
err = txContext.TXSimulator.DeletePrivateData(namespaceID, collection, kv.GetKey())
} else {
err = txContext.TXSimulator.DeleteState(namespaceID, kv.GetKey())
}

case pb.StateKV_PURGE_PRIVATE_DATA:
err = h.checkPurgePrivateDataCap(msg)
if err != nil {
return nil, err
}

if !isCollectionSet(collection) {
return nil, errors.New("only applicable for private data")
}
if txContext.IsInitTransaction {
return nil, errors.New("private data APIs are not allowed in chaincode Init()")
}
if err = errorIfCreatorHasNoWritePermission(namespaceID, collection, txContext); err != nil {
return nil, err
}
if err = txContext.TXSimulator.PurgePrivateData(namespaceID, collection, kv.GetKey()); err != nil {
return nil, errors.WithStack(err)
}

case pb.StateKV_PUT_STATE_METADATA:
err = h.checkMetadataCap(msg)
if err != nil {
return nil, err
}

metadata := make(map[string][]byte)
metadata[kv.GetMetadata().GetMetakey()] = kv.GetMetadata().GetValue()

if isCollectionSet(collection) {
if txContext.IsInitTransaction {
return nil, errors.New("private data APIs are not allowed in chaincode Init()")
}
if err = errorIfCreatorHasNoWritePermission(namespaceID, collection, txContext); err != nil {
return nil, err
}
err = txContext.TXSimulator.SetPrivateDataMetadata(namespaceID, collection, kv.GetKey(), metadata)
} else {
err = txContext.TXSimulator.SetStateMetadata(namespaceID, kv.GetKey(), metadata)
}

default:
return nil, errors.New("unknown operation type")
}

if err != nil {
return nil, errors.WithStack(err)
}
}

return &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_RESPONSE, Txid: msg.Txid, ChannelId: msg.ChannelId}, nil
}

// Handles requests that modify ledger state
func (h *Handler) HandleInvokeChaincode(msg *pb.ChaincodeMessage, txContext *TransactionContext) (*pb.ChaincodeMessage, error) {
chaincodeLogger.Debugf("[%s] C-call-C", shorttxid(msg.Txid))
Expand Down
12 changes: 10 additions & 2 deletions core/chaincode/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2783,8 +2783,16 @@ var _ = Describe("Handler", func() {
Type: pb.ChaincodeMessage_REGISTERED,
}))

Expect(readyMessage).To(ProtoEqual(&pb.ChaincodeMessage{
Type: pb.ChaincodeMessage_READY,
chaincodeAdditionalParams := &pb.ChaincodeAdditionalParams{
UsePutStateBatch: true,
MaxSizePutStateBatch: 1000,
}
payloadBytes, err := proto.Marshal(chaincodeAdditionalParams)
Expect(err).NotTo(HaveOccurred())

Expect(readyMessage).To(Equal(&pb.ChaincodeMessage{
Type: pb.ChaincodeMessage_READY,
Payload: payloadBytes,
}))
})

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,7 @@ require (

replace (
github.com/IBM/idemix => github.com/pfi79/idemix v0.0.0-20240827084656-d5b1e15270f4
github.com/hyperledger/fabric-chaincode-go/v2 => github.com/scientificideas/fabric-chaincode-go/v2 v2.0.0-20240820011636-d200b51745bf
github.com/hyperledger/fabric-config => github.com/pfi79/fabric-config v0.0.0-20240822061539-a86c531d2c81
github.com/hyperledger/fabric-protos-go-apiv2 => github.com/scientificideas/fabric-protos-go-apiv2 v0.0.0-20240819205904-84edd4adb1f8
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,8 @@ github.com/hyperledger-labs/SmartBFT v0.0.0-20240616160543-3f61a410b8c1 h1:0a9x5
github.com/hyperledger-labs/SmartBFT v0.0.0-20240616160543-3f61a410b8c1/go.mod h1:bY4EAvtsXf132ySoNJncMUOyBcGEQL3MXVtavRNFia4=
github.com/hyperledger/fabric-amcl v0.0.0-20230602173724-9e02669dceb2 h1:B1Nt8hKb//KvgGRprk0h1t4lCnwhE9/ryb1WqfZbV+M=
github.com/hyperledger/fabric-amcl v0.0.0-20230602173724-9e02669dceb2/go.mod h1:X+DIyUsaTmalOpmpQfIvFZjKHQedrURQ5t4YqquX7lE=
github.com/hyperledger/fabric-chaincode-go/v2 v2.0.0-20240802023949-a356b32676fd h1:oKqsj1ZcFHsiTkgQnOXnMlb3auklCSz8A+xg6T4Hul4=
github.com/hyperledger/fabric-chaincode-go/v2 v2.0.0-20240802023949-a356b32676fd/go.mod h1:c0qeSZK5N/SehpFRaA3CPlLlpfySyNq/DeJV1/tmqG0=
github.com/hyperledger/fabric-lib-go v1.1.3-0.20240523144151-25edd1eaf5f5 h1:RPWTL5wxAb+xDOrsCU3QYZP65305F8v3PaOyzdbPVMU=
github.com/hyperledger/fabric-lib-go v1.1.3-0.20240523144151-25edd1eaf5f5/go.mod h1:SHNCq8AB0VpHAmvJEtdbzabv6NNV1F48JdmDihasBjc=
github.com/hyperledger/fabric-protos-go-apiv2 v0.3.3 h1:Xpd6fzG/KjAOHJsq7EQXY2l+qi/y8muxBaY7R6QWABk=
github.com/hyperledger/fabric-protos-go-apiv2 v0.3.3/go.mod h1:2pq0ui6ZWA0cC8J+eCErgnMDCS1kPOEYVY+06ZAK0qE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down Expand Up @@ -487,6 +483,10 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk=
github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0=
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/scientificideas/fabric-chaincode-go/v2 v2.0.0-20240820011636-d200b51745bf h1:4dcRXDTBlfinzmhTEUuYTnkzDXTjSUggly8XPC8rJQU=
github.com/scientificideas/fabric-chaincode-go/v2 v2.0.0-20240820011636-d200b51745bf/go.mod h1:qaTeuuCq82fzRhNOso621JuWjOVqV+ymGNB5UpBxxEI=
github.com/scientificideas/fabric-protos-go-apiv2 v0.0.0-20240819205904-84edd4adb1f8 h1:7hFAzj8zZyoNGo1zWIB5rYpY1GFkxwxKblcFsW8zQWY=
github.com/scientificideas/fabric-protos-go-apiv2 v0.0.0-20240819205904-84edd4adb1f8/go.mod h1:1x9TFdg5b2M9vema7s4EdQZ1qUV4peAtF2jQj8FjLjc=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down
Loading

0 comments on commit b522057

Please sign in to comment.