Skip to content

Commit

Permalink
change GetAccountsInfo
Browse files Browse the repository at this point in the history
Signed-off-by: Fedor Partanskiy <fredprtnsk@gmail.com>
  • Loading branch information
pfi79 committed Jul 1, 2024
1 parent 756d676 commit 8cd3fd6
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 22 deletions.
4 changes: 2 additions & 2 deletions cc/addmultisig_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...,
Expand Down Expand Up @@ -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 = ""
)
Expand Down
2 changes: 2 additions & 0 deletions cc/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -81,6 +82,7 @@ func (c *ACL) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
}
}
}
c.methods = methods

ccInvoke, ok := methods[fn]
if !ok {
Expand Down
26 changes: 8 additions & 18 deletions cc/get_accounts_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:] {
Expand All @@ -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)
}
87 changes: 87 additions & 0 deletions cc/querystub/query_stub.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions tests/unit/get_accounts_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 8cd3fd6

Please sign in to comment.