Skip to content

Commit

Permalink
Merge pull request #101 from kaleido-io/lifecycle-args
Browse files Browse the repository at this point in the history
Use structs specific to the lifecycle calls to unmarshal the args
  • Loading branch information
nguyer authored Sep 15, 2023
2 parents 7fd8d03 + d359694 commit 2346ff4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
27 changes: 24 additions & 3 deletions internal/fabric/utils/blockdecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/msp"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric-protos-go/peer/lifecycle"
"github.com/hyperledger/firefly-fabconnect/internal/events/api"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -333,9 +334,29 @@ func (block *RawBlock) decodeActionPayloadChaincodeProposalPayload(chaincodeProp
chaincodeInput := &ChaincodeSpecInput{}
chaincodeSpec.Input = chaincodeInput

chaincodeInputArgs := make([]string, len(ccSpec.ChaincodeSpec.Input.Args))
for j, arg := range ccSpec.ChaincodeSpec.Input.Args {
chaincodeInputArgs[j] = string(arg)
chaincodeInputArgs := make([]interface{}, len(ccSpec.ChaincodeSpec.Input.Args))
if chaincodeSpec.ChaincodeId.Name == "_lifecycle" {
funcName := string(ccSpec.ChaincodeSpec.Input.Args[0])
chaincodeInputArgs[0] = funcName
argsBytes := ccSpec.ChaincodeSpec.Input.Args[1]

if funcName == "ApproveChaincodeDefinitionForMyOrg" {
approveArgs := &lifecycle.ApproveChaincodeDefinitionForMyOrgArgs{}
if err := proto.Unmarshal(argsBytes, approveArgs); err != nil {
return errors.Wrap(err, "error decoding args for ApproveChaincodeDefinitionForMyOrg")
}
chaincodeInputArgs[1] = approveArgs
} else if funcName == "CommitChaincodeDefinition" {
commitArgs := &lifecycle.CommitChaincodeDefinitionArgs{}
if err := proto.Unmarshal(argsBytes, commitArgs); err != nil {
return errors.Wrap(err, "error decoding args for CommitChaincodeDefinition")
}
chaincodeInputArgs[1] = commitArgs
}
} else {
for j, arg := range ccSpec.ChaincodeSpec.Input.Args {
chaincodeInputArgs[j] = string(arg)
}
}
chaincodeInput.Args = chaincodeInputArgs
chaincodeInput.IsInit = ccSpec.ChaincodeSpec.Input.IsInit
Expand Down
4 changes: 4 additions & 0 deletions internal/fabric/utils/blockdecoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/golang/protobuf/proto" //nolint
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/peer/lifecycle"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -83,6 +84,9 @@ func TestDecodeEndorserBlockLifecycleTxs(t *testing.T) {
assert.Equal("_lifecycle", cpp.Input.ChaincodeSpec.ChaincodeId.Name)
assert.Equal("UNDEFINED", cpp.Input.ChaincodeSpec.Type)
assert.Equal("ApproveChaincodeDefinitionForMyOrg", cpp.Input.ChaincodeSpec.Input.Args[0])
assert.Equal(int64(1), cpp.Input.ChaincodeSpec.Input.Args[1].(*lifecycle.ApproveChaincodeDefinitionForMyOrgArgs).Sequence)
assert.Equal("asset_transfer", cpp.Input.ChaincodeSpec.Input.Args[1].(*lifecycle.ApproveChaincodeDefinitionForMyOrgArgs).Name)
assert.Equal("1.1.0.u0ypz4p14q", cpp.Input.ChaincodeSpec.Input.Args[1].(*lifecycle.ApproveChaincodeDefinitionForMyOrgArgs).Version)
}

func TestDecodeConfigBlock(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/fabric/utils/rawblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ type ChaincodeSpec struct {
}

type ChaincodeSpecInput struct {
Args []string `json:"args"`
IsInit bool `json:"is_init"`
Args []interface{} `json:"args"`
IsInit bool `json:"is_init"`
}

0 comments on commit 2346ff4

Please sign in to comment.