Skip to content

Commit

Permalink
Move chaincode argument helpers to util
Browse files Browse the repository at this point in the history
Change-Id: Iff28dc9f8b828a47978846741054599c02d3a39c
Signed-off-by: Gabor Hosszu <gabor@digitalasset.com>
  • Loading branch information
gaborh-da committed Aug 23, 2016
1 parent 8ea25a9 commit 78c4b68
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 70 deletions.
4 changes: 2 additions & 2 deletions bddtests/syschaincode/noop/chaincode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
53 changes: 26 additions & 27 deletions core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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}}
}
Expand Down Expand Up @@ -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}}

Expand Down Expand Up @@ -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}}

Expand All @@ -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
Expand Down Expand Up @@ -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}}

Expand All @@ -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}}

Expand All @@ -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
Expand Down Expand Up @@ -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}}

Expand All @@ -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}}

Expand All @@ -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
Expand Down Expand Up @@ -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}

Expand All @@ -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}

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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}}

Expand All @@ -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}}

Expand All @@ -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
Expand Down Expand Up @@ -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}}

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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}}

Expand Down
4 changes: 2 additions & 2 deletions core/chaincode/platforms/car/test/car_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions core/chaincode/platforms/java/test/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
16 changes: 0 additions & 16 deletions core/chaincode/shim/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions core/container/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions core/ledger/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions core/system_chaincode/systemchaincode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 78c4b68

Please sign in to comment.