diff --git a/cc/addmultisig_request.go b/cc/addmultisig_request.go index 72efd41..a61b5d1 100644 --- a/cc/addmultisig_request.go +++ b/cc/addmultisig_request.go @@ -106,7 +106,7 @@ func (r *AddMultisigRequest) parseArguments( } } - r.Message = message(operation, args[:len(args)-len(r.PublicKeys)]...) + r.Message = msgAddMultisigRequest(operation, args[:len(args)-len(r.PublicKeys)]...) r.SignedTx = append( []string{operation}, args..., @@ -161,7 +161,7 @@ func (r *AddMultisigRequest) parseKeysAndSignatures( return nil } -func message(op string, args ...string) string { +func msgAddMultisigRequest(op string, args ...string) string { const ( messageSeparator = "" ) diff --git a/cc/chaincode.go b/cc/chaincode.go index 203a28e..453aa49 100644 --- a/cc/chaincode.go +++ b/cc/chaincode.go @@ -20,6 +20,7 @@ type ( ACL struct { adminSKI []byte config *proto.ACLConfig + methods map[string]ccFunc } ccFunc func(stub shim.ChaincodeStubInterface, args []string) peer.Response ) @@ -81,6 +82,7 @@ func (c *ACL) Invoke(stub shim.ChaincodeStubInterface) peer.Response { } } } + c.methods = methods ccInvoke, ok := methods[fn] if !ok { diff --git a/cc/get_accounts_info.go b/cc/get_accounts_info.go index 39d63d8..5bd8129 100644 --- a/cc/get_accounts_info.go +++ b/cc/get_accounts_info.go @@ -4,24 +4,11 @@ import ( "encoding/json" "fmt" + "github.com/anoideaopen/acl/cc/querystub" "github.com/hyperledger/fabric-chaincode-go/shim" "github.com/hyperledger/fabric-protos-go/peer" ) -var getAccountInfoMethodHandlersMap map[string]func(shim.ChaincodeStubInterface, []string) peer.Response - -func (c *ACL) getAccountInfoHandlers() map[string]func(shim.ChaincodeStubInterface, []string) peer.Response { - if getAccountInfoMethodHandlersMap != nil { - return getAccountInfoMethodHandlersMap - } - getAccountInfoMethodHandlersMap = map[string]func(shim.ChaincodeStubInterface, []string) peer.Response{ - "getAccountInfo": c.GetAccountInfo, - "checkAddress": c.CheckAddress, - "checkKeys": c.CheckKeys, - } - return getAccountInfoMethodHandlersMap -} - func (c *ACL) GetAccountsInfo(stub shim.ChaincodeStubInterface, _ []string) peer.Response { responses := make([]peer.Response, 0) for _, bytes := range stub.GetArgs()[1:] { @@ -47,11 +34,14 @@ func (c *ACL) handleGetAccountsInfoItem(stub shim.ChaincodeStubInterface, b []by return shim.Error(fmt.Sprintf("not enough arguments '%s'", string(b))) } - method := args[0] + fn := args[0] methodArgs := args[1:] - handler, ok := c.getAccountInfoHandlers()[method] + ccInvoke, ok := c.methods[fn] if !ok { - return shim.Error(fmt.Sprintf("failed get accounts info: unknown method '%s'", method)) + return shim.Error(fmt.Sprintf("failed get accounts info: unknown method '%s' in tx %s", fn, stub.GetTxID())) } - return handler(stub, methodArgs) + + stub = querystub.NewQueryStub(stub, args...) + + return ccInvoke(stub, methodArgs) } diff --git a/cc/querystub/query_stub.go b/cc/querystub/query_stub.go new file mode 100644 index 0000000..a87d64d --- /dev/null +++ b/cc/querystub/query_stub.go @@ -0,0 +1,87 @@ +package querystub + +import ( + "github.com/hyperledger/fabric-chaincode-go/shim" +) + +type queryStub struct { + shim.ChaincodeStubInterface + args [][]byte +} + +func NewQueryStub(stub shim.ChaincodeStubInterface, args ...string) shim.ChaincodeStubInterface { + qs := &queryStub{ + ChaincodeStubInterface: stub, + args: make([][]byte, 0, len(args)), + } + for _, arg := range args { + qs.args = append(qs.args, []byte(arg)) + } + + return qs +} + +func (qs *queryStub) PutState(_ string, _ []byte) error { + return nil +} + +func (qs *queryStub) DelState(_ string) error { + return nil +} + +func (qs *queryStub) SetStateValidationParameter(_ string, _ []byte) error { + return nil +} + +func (qs *queryStub) PutPrivateData(_ string, _ string, _ []byte) error { + return nil +} + +func (qs *queryStub) DelPrivateData(_, _ string) error { + return nil +} + +func (qs *queryStub) PurgePrivateData(_, _ string) error { + return nil +} + +func (qs *queryStub) SetPrivateDataValidationParameter(_, _ string, _ []byte) error { + return nil +} + +func (qs *queryStub) SetEvent(_ string, _ []byte) error { + return nil +} + +func (qs *queryStub) GetArgs() [][]byte { + return qs.args +} + +func (qs *queryStub) GetStringArgs() []string { + args := qs.GetArgs() + strargs := make([]string, 0, len(args)) + for _, barg := range args { + strargs = append(strargs, string(barg)) + } + return strargs +} + +func (qs *queryStub) GetFunctionAndParameters() (function string, params []string) { + allargs := qs.GetStringArgs() + function = "" + params = []string{} + if len(allargs) >= 1 { + function = allargs[0] + params = allargs[1:] + } + return +} + +func (qs *queryStub) GetArgsSlice() ([]byte, error) { + args := qs.GetArgs() + res := []byte{} + for _, barg := range args { + res = append(res, barg...) + } + return res, nil +} diff --git a/tests/unit/get_accounts_info_test.go b/tests/unit/get_accounts_info_test.go index 1e61c15..82bd6f2 100644 --- a/tests/unit/get_accounts_info_test.go +++ b/tests/unit/get_accounts_info_test.go @@ -64,7 +64,7 @@ func TestGetAccountsInfoWrongMethodName(t *testing.T) { require.NoError(t, err) require.Len(t, responses, 1) require.Equal(t, int32(500), responses[0].GetStatus()) - require.Equal(t, "failed get accounts info: unknown method 'tesst'", responses[0].GetMessage()) + require.Equal(t, "failed get accounts info: unknown method 'tesst' in tx 0", responses[0].GetMessage()) } func TestGetAccountsInfoOkAndErrResp(t *testing.T) { @@ -100,7 +100,7 @@ func TestGetAccountsInfoOkAndErrResp(t *testing.T) { require.Len(t, responses, 2) require.Equal(t, int32(500), responses[0].GetStatus()) - require.Equal(t, "failed get accounts info: unknown method 'tesst'", responses[0].GetMessage()) + require.Equal(t, "failed get accounts info: unknown method 'tesst' in tx 0", responses[0].GetMessage()) expectedResponse := &seriesGetAccountInfo{ testAddress: common.TestAddr, respStatus: int32(shim.OK),