From 78c4b680cbe8ac612f9c0342dd2c91fe1fa2ac5f Mon Sep 17 00:00:00 2001 From: Gabor Hosszu Date: Tue, 23 Aug 2016 13:31:03 +0200 Subject: [PATCH] Move chaincode argument helpers to util Change-Id: Iff28dc9f8b828a47978846741054599c02d3a39c Signed-off-by: Gabor Hosszu --- bddtests/syschaincode/noop/chaincode_test.go | 4 +- core/chaincode/exectransaction_test.go | 53 +++++++++---------- core/chaincode/platforms/car/test/car_test.go | 4 +- .../platforms/java/test/java_test.go | 4 +- core/chaincode/shim/chaincode.go | 16 ------ core/container/vm_test.go | 6 +-- core/ledger/blockchain_test.go | 3 +- core/system_chaincode/systemchaincode_test.go | 5 +- core/util/utils.go | 16 ++++++ .../chaincode_example04.go | 3 +- .../chaincode_example04_test.go | 7 ++- .../chaincode_example05.go | 9 ++-- examples/chaincode/go/passthru/passthru.go | 5 +- peer/chaincode/common.go | 4 +- 14 files changed, 69 insertions(+), 70 deletions(-) diff --git a/bddtests/syschaincode/noop/chaincode_test.go b/bddtests/syschaincode/noop/chaincode_test.go index 5b4e80a69d8..8a000cc5450 100644 --- a/bddtests/syschaincode/noop/chaincode_test.go +++ b/bddtests/syschaincode/noop/chaincode_test.go @@ -21,7 +21,7 @@ import ( "testing" "github.com/golang/protobuf/proto" - "github.com/hyperledger/fabric/core/chaincode/shim" + "github.com/hyperledger/fabric/core/util" "github.com/hyperledger/fabric/protos" ) @@ -116,7 +116,7 @@ func (ml mockLedger) GetTransactionByID(txID string) (*protos.Transaction, error if txID == "noSuchTX" { return nil, fmt.Errorf("Some error") } - newCCIS := &protos.ChaincodeInvocationSpec{ChaincodeSpec: &protos.ChaincodeSpec{CtorMsg: &protos.ChaincodeInput{Args: shim.ToChaincodeArgs("execute", something)}}} + newCCIS := &protos.ChaincodeInvocationSpec{ChaincodeSpec: &protos.ChaincodeSpec{CtorMsg: &protos.ChaincodeInput{Args: util.ToChaincodeArgs("execute", something)}}} pl, _ := proto.Marshal(newCCIS) return &protos.Transaction{Payload: pl}, nil } diff --git a/core/chaincode/exectransaction_test.go b/core/chaincode/exectransaction_test.go index 2829e4de985..b8a60a8a94f 100644 --- a/core/chaincode/exectransaction_test.go +++ b/core/chaincode/exectransaction_test.go @@ -28,7 +28,6 @@ import ( "path/filepath" - "github.com/hyperledger/fabric/core/chaincode/shim" "github.com/hyperledger/fabric/core/container" "github.com/hyperledger/fabric/core/container/ccintf" "github.com/hyperledger/fabric/core/crypto" @@ -339,7 +338,7 @@ func executeDeployTransaction(t *testing.T, url string) { var ctxt = context.Background() f := "init" - args := shim.ToChaincodeArgs(f, "a", "100", "b", "200") + args := util.ToChaincodeArgs(f, "a", "100", "b", "200") spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: &pb.ChaincodeID{Path: url}, CtorMsg: &pb.ChaincodeInput{Args: args}} _, err = deploy(ctxt, spec) chaincodeID := spec.ChaincodeID.Name @@ -426,7 +425,7 @@ func checkFinalState(uuid string, chaincodeID string) error { func invokeExample02Transaction(ctxt context.Context, cID *pb.ChaincodeID, args []string, destroyImage bool) error { f := "init" - argsDeploy := shim.ToChaincodeArgs(f, "a", "100", "b", "200") + argsDeploy := util.ToChaincodeArgs(f, "a", "100", "b", "200") spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: argsDeploy}} _, err := deploy(ctxt, spec) chaincodeID := spec.ChaincodeID.Name @@ -449,7 +448,7 @@ func invokeExample02Transaction(ctxt context.Context, cID *pb.ChaincodeID, args f = "invoke" invokeArgs := append([]string{f}, args...) - spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: shim.ToChaincodeArgs(invokeArgs...)}} + spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(invokeArgs...)}} _, uuid, _, err := invoke(ctxt, spec, pb.Transaction_CHAINCODE_INVOKE) if err != nil { return fmt.Errorf("Error invoking <%s>: %s", chaincodeID, err) @@ -462,7 +461,7 @@ func invokeExample02Transaction(ctxt context.Context, cID *pb.ChaincodeID, args // Test for delete state f = "delete" - delArgs := shim.ToChaincodeArgs(f, "a") + delArgs := util.ToChaincodeArgs(f, "a") spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: delArgs}} _, uuid, _, err = invoke(ctxt, spec, pb.Transaction_CHAINCODE_INVOKE) if err != nil { @@ -542,12 +541,12 @@ func exec(ctxt context.Context, chaincodeID string, numTrans int, numQueries int var spec *pb.ChaincodeSpec if typ == pb.Transaction_CHAINCODE_INVOKE { f := "invoke" - args := shim.ToChaincodeArgs(f, "a", "b", "10") + args := util.ToChaincodeArgs(f, "a", "b", "10") spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: &pb.ChaincodeID{Name: chaincodeID}, CtorMsg: &pb.ChaincodeInput{Args: args}} } else { f := "query" - args := shim.ToChaincodeArgs(f, "a") + args := util.ToChaincodeArgs(f, "a") spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: &pb.ChaincodeID{Name: chaincodeID}, CtorMsg: &pb.ChaincodeInput{Args: args}} } @@ -617,7 +616,7 @@ func TestExecuteQuery(t *testing.T) { cID := &pb.ChaincodeID{Path: url} f := "init" - args := shim.ToChaincodeArgs(f, "a", "100", "b", "200") + args := util.ToChaincodeArgs(f, "a", "100", "b", "200") spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: args}} @@ -763,7 +762,7 @@ func TestExecuteInvalidQuery(t *testing.T) { cID := &pb.ChaincodeID{Path: url} f := "init" - args := shim.ToChaincodeArgs(f, "a", "100") + args := util.ToChaincodeArgs(f, "a", "100") spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: args}} @@ -780,7 +779,7 @@ func TestExecuteInvalidQuery(t *testing.T) { time.Sleep(time.Second) f = "query" - args = shim.ToChaincodeArgs(f, "b", "200") + args = util.ToChaincodeArgs(f, "b", "200") spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: args}} // This query should fail as it attempts to put state @@ -837,7 +836,7 @@ func TestChaincodeInvokeChaincode(t *testing.T) { cID1 := &pb.ChaincodeID{Path: url1} f := "init" - args := shim.ToChaincodeArgs(f, "a", "100", "b", "200") + args := util.ToChaincodeArgs(f, "a", "100", "b", "200") spec1 := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID1, CtorMsg: &pb.ChaincodeInput{Args: args}} @@ -860,7 +859,7 @@ func TestChaincodeInvokeChaincode(t *testing.T) { cID2 := &pb.ChaincodeID{Path: url2} f = "init" - args = shim.ToChaincodeArgs(f, "e", "0") + args = util.ToChaincodeArgs(f, "e", "0") spec2 := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID2, CtorMsg: &pb.ChaincodeInput{Args: args}} @@ -879,7 +878,7 @@ func TestChaincodeInvokeChaincode(t *testing.T) { // Invoke second chaincode, which will inturn invoke the first chaincode f = "invoke" - args = shim.ToChaincodeArgs(f, "e", "1") + args = util.ToChaincodeArgs(f, "e", "1") spec2 = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID2, CtorMsg: &pb.ChaincodeInput{Args: args}} // Invoke chaincode @@ -953,7 +952,7 @@ func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) { cID1 := &pb.ChaincodeID{Path: url1} f := "init" - args := shim.ToChaincodeArgs(f, "a", "100", "b", "200") + args := util.ToChaincodeArgs(f, "a", "100", "b", "200") spec1 := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID1, CtorMsg: &pb.ChaincodeInput{Args: args}} @@ -974,7 +973,7 @@ func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) { cID2 := &pb.ChaincodeID{Path: url2} f = "init" - args = shim.ToChaincodeArgs(f) + args = util.ToChaincodeArgs(f) spec2 := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID2, CtorMsg: &pb.ChaincodeInput{Args: args}} @@ -993,7 +992,7 @@ func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) { // Invoke second chaincode, which will inturn invoke the first chaincode but pass bad params f = chaincodeID1 - args = shim.ToChaincodeArgs(f, "invoke", "a") + args = util.ToChaincodeArgs(f, "invoke", "a") spec2 = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID2, CtorMsg: &pb.ChaincodeInput{Args: args}} // Invoke chaincode @@ -1030,7 +1029,7 @@ func chaincodeQueryChaincode(user string) error { cID1 := &pb.ChaincodeID{Path: url1} f := "init" - args := shim.ToChaincodeArgs(f, "a", "100", "b", "200") + args := util.ToChaincodeArgs(f, "a", "100", "b", "200") spec1 := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID1, CtorMsg: &pb.ChaincodeInput{Args: args}, SecureContext: user} @@ -1048,7 +1047,7 @@ func chaincodeQueryChaincode(user string) error { cID2 := &pb.ChaincodeID{Path: url2} f = "init" - args = shim.ToChaincodeArgs(f, "sum", "0") + args = util.ToChaincodeArgs(f, "sum", "0") spec2 := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID2, CtorMsg: &pb.ChaincodeInput{Args: args}, SecureContext: user} @@ -1064,7 +1063,7 @@ func chaincodeQueryChaincode(user string) error { // Invoke second chaincode, which will inturn query the first chaincode f = "invoke" - args = shim.ToChaincodeArgs(f, chaincodeID1, "sum") + args = util.ToChaincodeArgs(f, chaincodeID1, "sum") spec2 = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID2, CtorMsg: &pb.ChaincodeInput{Args: args}, SecureContext: user} // Invoke chaincode @@ -1087,7 +1086,7 @@ func chaincodeQueryChaincode(user string) error { // Query second chaincode, which will inturn query the first chaincode f = "query" - args = shim.ToChaincodeArgs(f, chaincodeID1, "sum") + args = util.ToChaincodeArgs(f, chaincodeID1, "sum") spec2 = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID2, CtorMsg: &pb.ChaincodeInput{Args: args}, SecureContext: user} // Invoke chaincode @@ -1176,7 +1175,7 @@ func TestChaincodeQueryChaincodeErrorCase(t *testing.T) { cID1 := &pb.ChaincodeID{Path: url1} f := "init" - args := shim.ToChaincodeArgs(f, "a", "100", "b", "200") + args := util.ToChaincodeArgs(f, "a", "100", "b", "200") spec1 := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID1, CtorMsg: &pb.ChaincodeInput{Args: args}} @@ -1197,7 +1196,7 @@ func TestChaincodeQueryChaincodeErrorCase(t *testing.T) { cID2 := &pb.ChaincodeID{Path: url2} f = "init" - args = shim.ToChaincodeArgs(f) + args = util.ToChaincodeArgs(f) spec2 := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID2, CtorMsg: &pb.ChaincodeInput{Args: args}} @@ -1216,7 +1215,7 @@ func TestChaincodeQueryChaincodeErrorCase(t *testing.T) { // Invoke second chaincode, which will inturn invoke the first chaincode but pass bad params f = chaincodeID1 - args = shim.ToChaincodeArgs(f, "query", "c") + args = util.ToChaincodeArgs(f, "query", "c") spec2 = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID2, CtorMsg: &pb.ChaincodeInput{Args: args}} // Invoke chaincode @@ -1340,7 +1339,7 @@ func TestRangeQuery(t *testing.T) { cID := &pb.ChaincodeID{Path: url} f := "init" - args := shim.ToChaincodeArgs(f) + args := util.ToChaincodeArgs(f) spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: args}} @@ -1356,7 +1355,7 @@ func TestRangeQuery(t *testing.T) { // Invoke second chaincode, which will inturn invoke the first chaincode f = "keys" - args = shim.ToChaincodeArgs(f) + args = util.ToChaincodeArgs(f) spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: args}} _, _, _, err = invoke(ctxt, spec, pb.Transaction_CHAINCODE_QUERY) @@ -1411,7 +1410,7 @@ func TestGetEvent(t *testing.T) { cID := &pb.ChaincodeID{Path: url} f := "init" - spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: shim.ToChaincodeArgs(f)}} + spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(f)}} _, err = deploy(ctxt, spec) chaincodeID := spec.ChaincodeID.Name @@ -1425,7 +1424,7 @@ func TestGetEvent(t *testing.T) { time.Sleep(time.Second) - args := shim.ToChaincodeArgs("", "i", "am", "satoshi") + args := util.ToChaincodeArgs("", "i", "am", "satoshi") spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: args}} diff --git a/core/chaincode/platforms/car/test/car_test.go b/core/chaincode/platforms/car/test/car_test.go index eac94a6802d..f480acd8f4c 100644 --- a/core/chaincode/platforms/car/test/car_test.go +++ b/core/chaincode/platforms/car/test/car_test.go @@ -20,9 +20,9 @@ import ( "os" "testing" - "github.com/hyperledger/fabric/core/chaincode/shim" "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/core/container" + "github.com/hyperledger/fabric/core/util" pb "github.com/hyperledger/fabric/protos" ) @@ -49,7 +49,7 @@ func TestCar_BuildImage(t *testing.T) { } chaincodePath := cwd + "/org.hyperledger.chaincode.example02-0.1-SNAPSHOT.car" - spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_CAR, ChaincodeID: &pb.ChaincodeID{Path: chaincodePath}, CtorMsg: &pb.ChaincodeInput{Args: shim.ToChaincodeArgs("f")}} + spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_CAR, ChaincodeID: &pb.ChaincodeID{Path: chaincodePath}, CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("f")}} if _, err := vm.BuildChaincodeContainer(spec); err != nil { t.Fail() t.Log(err) diff --git a/core/chaincode/platforms/java/test/java_test.go b/core/chaincode/platforms/java/test/java_test.go index be3cdfc5734..6af11b14baa 100644 --- a/core/chaincode/platforms/java/test/java_test.go +++ b/core/chaincode/platforms/java/test/java_test.go @@ -20,9 +20,9 @@ import ( "os" "testing" - "github.com/hyperledger/fabric/core/chaincode/shim" "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/core/container" + "github.com/hyperledger/fabric/core/util" pb "github.com/hyperledger/fabric/protos" ) @@ -42,7 +42,7 @@ func TestJava_BuildImage(t *testing.T) { chaincodePath := "../../../shim/java" //TODO find a better way to launch example java chaincode - spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_JAVA, ChaincodeID: &pb.ChaincodeID{Path: chaincodePath}, CtorMsg: &pb.ChaincodeInput{Args: shim.ToChaincodeArgs("f")}} + spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_JAVA, ChaincodeID: &pb.ChaincodeID{Path: chaincodePath}, CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("f")}} if _, err := vm.BuildChaincodeContainer(spec); err != nil { t.Fail() t.Log(err) diff --git a/core/chaincode/shim/chaincode.go b/core/chaincode/shim/chaincode.go index 1c0fe5cd5d4..4b3b7ab5bdf 100644 --- a/core/chaincode/shim/chaincode.go +++ b/core/chaincode/shim/chaincode.go @@ -1017,19 +1017,3 @@ func (c *ChaincodeLogger) Errorf(format string, args ...interface{}) { func (c *ChaincodeLogger) Criticalf(format string, args ...interface{}) { c.logger.Criticalf(format, args...) } - -func ToChaincodeArgs(args ...string) [][]byte { - bargs := make([][]byte, len(args)) - for i, arg := range args { - bargs[i] = []byte(arg) - } - return bargs -} - -func ArrayToChaincodeArgs(args []string) [][]byte { - bargs := make([][]byte, len(args)) - for i, arg := range args { - bargs[i] = []byte(arg) - } - return bargs -} diff --git a/core/container/vm_test.go b/core/container/vm_test.go index a01aaac1570..af1d3ed1e2b 100644 --- a/core/container/vm_test.go +++ b/core/container/vm_test.go @@ -24,8 +24,8 @@ import ( "os" "testing" - "github.com/hyperledger/fabric/core/chaincode/shim" cutil "github.com/hyperledger/fabric/core/container/util" + "github.com/hyperledger/fabric/core/util" pb "github.com/hyperledger/fabric/protos" "golang.org/x/net/context" ) @@ -74,7 +74,7 @@ func TestVM_BuildImage_ChaincodeLocal(t *testing.T) { } // Build the spec chaincodePath := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example01" - spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG, ChaincodeID: &pb.ChaincodeID{Path: chaincodePath}, CtorMsg: &pb.ChaincodeInput{Args: shim.ToChaincodeArgs("f")}} + spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG, ChaincodeID: &pb.ChaincodeID{Path: chaincodePath}, CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("f")}} if _, err := vm.BuildChaincodeContainer(spec); err != nil { t.Fail() t.Log(err) @@ -91,7 +91,7 @@ func TestVM_BuildImage_ChaincodeRemote(t *testing.T) { } // Build the spec chaincodePath := "https://github.com/prjayach/chaincode_examples/chaincode_example02" - spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG, ChaincodeID: &pb.ChaincodeID{Path: chaincodePath}, CtorMsg: &pb.ChaincodeInput{Args: shim.ToChaincodeArgs("f")}} + spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG, ChaincodeID: &pb.ChaincodeID{Path: chaincodePath}, CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("f")}} if _, err := vm.BuildChaincodeContainer(spec); err != nil { t.Fail() t.Log(err) diff --git a/core/ledger/blockchain_test.go b/core/ledger/blockchain_test.go index 8d114fabd85..f916b74933c 100644 --- a/core/ledger/blockchain_test.go +++ b/core/ledger/blockchain_test.go @@ -20,7 +20,6 @@ import ( "testing" "time" - "github.com/hyperledger/fabric/core/chaincode/shim" "github.com/hyperledger/fabric/core/ledger/testutil" "github.com/hyperledger/fabric/core/util" "github.com/hyperledger/fabric/protos" @@ -59,7 +58,7 @@ func TestBlockChain_SingleBlock(t *testing.T) { // Create the Chaincode specification chaincodeSpec := &protos.ChaincodeSpec{Type: protos.ChaincodeSpec_GOLANG, ChaincodeID: &protos.ChaincodeID{Path: "Contracts"}, - CtorMsg: &protos.ChaincodeInput{Args: shim.ToChaincodeArgs("Initialize", "param1")}} + CtorMsg: &protos.ChaincodeInput{Args: util.ToChaincodeArgs("Initialize", "param1")}} chaincodeDeploymentSepc := &protos.ChaincodeDeploymentSpec{ChaincodeSpec: chaincodeSpec} uuid := testutil.GenerateID(t) newChaincodeTx, err := protos.NewChaincodeDeployTransaction(chaincodeDeploymentSepc, uuid) diff --git a/core/system_chaincode/systemchaincode_test.go b/core/system_chaincode/systemchaincode_test.go index b47d060c1be..88e29da354d 100644 --- a/core/system_chaincode/systemchaincode_test.go +++ b/core/system_chaincode/systemchaincode_test.go @@ -24,7 +24,6 @@ import ( "time" "github.com/hyperledger/fabric/core/chaincode" - "github.com/hyperledger/fabric/core/chaincode/shim" "github.com/hyperledger/fabric/core/db" "github.com/hyperledger/fabric/core/ledger" "github.com/hyperledger/fabric/core/system_chaincode/api" @@ -122,7 +121,7 @@ func TestExecuteDeploySysChaincode(t *testing.T) { url := "github.com/hyperledger/fabric/core/system_chaincode/sample_syscc" f := "putval" - args := shim.ToChaincodeArgs(f, "greeting", "hey there") + args := util.ToChaincodeArgs(f, "greeting", "hey there") spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: &pb.ChaincodeID{Name: "sample_syscc", Path: url}, CtorMsg: &pb.ChaincodeInput{Args: args}} _, _, _, err = invoke(ctxt, spec, pb.Transaction_CHAINCODE_INVOKE) @@ -134,7 +133,7 @@ func TestExecuteDeploySysChaincode(t *testing.T) { } f = "getval" - args = shim.ToChaincodeArgs(f, "greeting") + args = util.ToChaincodeArgs(f, "greeting") spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: &pb.ChaincodeID{Name: "sample_syscc", Path: url}, CtorMsg: &pb.ChaincodeInput{Args: args}} _, _, _, err = invoke(ctxt, spec, pb.Transaction_CHAINCODE_QUERY) if err != nil { diff --git a/core/util/utils.go b/core/util/utils.go index af4caad1026..9156b9836f0 100644 --- a/core/util/utils.go +++ b/core/util/utils.go @@ -125,3 +125,19 @@ all: } return } + +func ToChaincodeArgs(args ...string) [][]byte { + bargs := make([][]byte, len(args)) + for i, arg := range args { + bargs[i] = []byte(arg) + } + return bargs +} + +func ArrayToChaincodeArgs(args []string) [][]byte { + bargs := make([][]byte, len(args)) + for i, arg := range args { + bargs[i] = []byte(arg) + } + return bargs +} diff --git a/examples/chaincode/go/chaincode_example04/chaincode_example04.go b/examples/chaincode/go/chaincode_example04/chaincode_example04.go index 3b7f2b3db26..7afe414801d 100644 --- a/examples/chaincode/go/chaincode_example04/chaincode_example04.go +++ b/examples/chaincode/go/chaincode_example04/chaincode_example04.go @@ -22,6 +22,7 @@ import ( "strconv" "github.com/hyperledger/fabric/core/chaincode/shim" + "github.com/hyperledger/fabric/core/util" ) // This chaincode is a test for chaincode invoking another chaincode - invokes chaincode_example02 @@ -88,7 +89,7 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function stri chainCodeToCall := t.GetChaincodeToCall() f := "invoke" - invokeArgs := shim.ToChaincodeArgs(f, "a", "b", "10") + invokeArgs := util.ToChaincodeArgs(f, "a", "b", "10") response, err := stub.InvokeChaincode(chainCodeToCall, invokeArgs) if err != nil { errStr := fmt.Sprintf("Failed to invoke chaincode. Got error: %s", err.Error()) diff --git a/examples/chaincode/go/chaincode_example04/chaincode_example04_test.go b/examples/chaincode/go/chaincode_example04/chaincode_example04_test.go index 4c3c852cd6d..b2658b2047c 100644 --- a/examples/chaincode/go/chaincode_example04/chaincode_example04_test.go +++ b/examples/chaincode/go/chaincode_example04/chaincode_example04_test.go @@ -21,7 +21,6 @@ import ( "github.com/hyperledger/fabric/core/chaincode/shim" ex02 "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02" - main "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example04" ) // this is the response to any successful Invoke() on chaincode_example04 @@ -72,7 +71,7 @@ func checkInvoke(t *testing.T, stub *shim.MockStub, args []string) { } func TestExample04_Init(t *testing.T) { - scc := new(main.SimpleChaincode) + scc := new(SimpleChaincode) stub := shim.NewMockStub("ex04", scc) // Init A=123 B=234 @@ -82,7 +81,7 @@ func TestExample04_Init(t *testing.T) { } func TestExample04_Query(t *testing.T) { - scc := new(main.SimpleChaincode) + scc := new(SimpleChaincode) stub := shim.NewMockStub("ex04", scc) // Init A=345 B=456 @@ -93,7 +92,7 @@ func TestExample04_Query(t *testing.T) { } func TestExample04_Invoke(t *testing.T) { - scc := new(main.SimpleChaincode) + scc := new(SimpleChaincode) stub := shim.NewMockStub("ex04", scc) ccEx2 := new(ex02.SimpleChaincode) diff --git a/examples/chaincode/go/chaincode_example05/chaincode_example05.go b/examples/chaincode/go/chaincode_example05/chaincode_example05.go index 26ac2bad6f0..0023df9d14d 100644 --- a/examples/chaincode/go/chaincode_example05/chaincode_example05.go +++ b/examples/chaincode/go/chaincode_example05/chaincode_example05.go @@ -22,6 +22,7 @@ import ( "strconv" "github.com/hyperledger/fabric/core/chaincode/shim" + "github.com/hyperledger/fabric/core/util" ) // This chaincode is a test for chaincode querying another chaincode - invokes chaincode_example02 and computes the sum of a and b and stores it as state @@ -73,7 +74,7 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function stri // Query chaincode_example02 f := "query" - queryArgs := shim.ToChaincodeArgs(f, "a") + queryArgs := util.ToChaincodeArgs(f, "a") response, err := stub.QueryChaincode(chaincodeURL, queryArgs) if err != nil { errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error()) @@ -87,7 +88,7 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function stri return nil, errors.New(errStr) } - queryArgs = shim.ToChaincodeArgs(f, "b") + queryArgs = util.ToChaincodeArgs(f, "b") response, err = stub.QueryChaincode(chaincodeURL, queryArgs) if err != nil { errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error()) @@ -133,7 +134,7 @@ func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function strin // Query chaincode_example02 f := "query" - queryArgs := shim.ToChaincodeArgs(f, "a") + queryArgs := util.ToChaincodeArgs(f, "a") response, err := stub.QueryChaincode(chaincodeURL, queryArgs) if err != nil { errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error()) @@ -147,7 +148,7 @@ func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function strin return nil, errors.New(errStr) } - queryArgs = shim.ToChaincodeArgs(f, "b") + queryArgs = util.ToChaincodeArgs(f, "b") response, err = stub.QueryChaincode(chaincodeURL, queryArgs) if err != nil { errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error()) diff --git a/examples/chaincode/go/passthru/passthru.go b/examples/chaincode/go/passthru/passthru.go index 93f33d15bc5..d7e440a250b 100644 --- a/examples/chaincode/go/passthru/passthru.go +++ b/examples/chaincode/go/passthru/passthru.go @@ -22,6 +22,7 @@ import ( "strings" "github.com/hyperledger/fabric/core/chaincode/shim" + "github.com/hyperledger/fabric/core/util" ) // PassthruChaincode passes thru invoke and query to another chaincode where @@ -48,9 +49,9 @@ func (p *PassthruChaincode) iq(invoke bool, stub shim.ChaincodeStubInterface, fu chaincodeID := function if invoke { - return stub.InvokeChaincode(chaincodeID, shim.ToChaincodeArgs(args...)) + return stub.InvokeChaincode(chaincodeID, util.ToChaincodeArgs(args...)) } - return stub.QueryChaincode(chaincodeID, shim.ToChaincodeArgs(args...)) + return stub.QueryChaincode(chaincodeID, util.ToChaincodeArgs(args...)) } // Invoke passes through the invoke call diff --git a/peer/chaincode/common.go b/peer/chaincode/common.go index a5aa09de057..4ea50465afe 100755 --- a/peer/chaincode/common.go +++ b/peer/chaincode/common.go @@ -25,7 +25,7 @@ import ( "strings" "github.com/hyperledger/fabric/core" - "github.com/hyperledger/fabric/core/chaincode/shim" + u "github.com/hyperledger/fabric/core/util" "github.com/hyperledger/fabric/peer/common" "github.com/hyperledger/fabric/peer/util" pb "github.com/hyperledger/fabric/protos" @@ -49,7 +49,7 @@ func getChaincodeSpecification(cmd *cobra.Command) (*pb.ChaincodeSpec, error) { if err := json.Unmarshal([]byte(chaincodeCtorJSON), &inputc); err != nil { return spec, fmt.Errorf("Chaincode argument error: %s", err) } - input := &pb.ChaincodeInput{Args: shim.ToChaincodeArgs(inputc.Args...)} + input := &pb.ChaincodeInput{Args: u.ToChaincodeArgs(inputc.Args...)} var attributes []string if err := json.Unmarshal([]byte(chaincodeAttributesJSON), &attributes); err != nil {