From 725a589cf3e9cdb037ae6c9b0fd27e2fe506380e Mon Sep 17 00:00:00 2001 From: aaronbuchwald Date: Thu, 10 Oct 2024 10:12:59 -0400 Subject: [PATCH] Use CreateActionID to give simulated actions unique actionID (#1644) --- api/jsonrpc/client.go | 6 +++--- api/jsonrpc/server.go | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/api/jsonrpc/client.go b/api/jsonrpc/client.go index c433b1c87b..d189525700 100644 --- a/api/jsonrpc/client.go +++ b/api/jsonrpc/client.go @@ -194,8 +194,8 @@ func (cli *JSONRPCClient) GetABI(ctx context.Context) (abi.ABI, error) { return resp.ABI, err } -func (cli *JSONRPCClient) Execute(ctx context.Context, actor codec.Address, actions []chain.Action) ([][]byte, error) { - actionsMarshaled := make([][]byte, 0) +func (cli *JSONRPCClient) ExecuteActions(ctx context.Context, actor codec.Address, actions []chain.Action) ([][]byte, error) { + actionsMarshaled := make([][]byte, 0, len(actions)) for _, action := range actions { actionBytes, err := chain.MarshalTyped(action) if err != nil { @@ -212,7 +212,7 @@ func (cli *JSONRPCClient) Execute(ctx context.Context, actor codec.Address, acti resp := new(ExecuteActionReply) err := cli.requester.SendRequest( ctx, - "execute", + "executeActions", args, resp, ) diff --git a/api/jsonrpc/server.go b/api/jsonrpc/server.go index 8ba47847fe..364b82185f 100644 --- a/api/jsonrpc/server.go +++ b/api/jsonrpc/server.go @@ -26,6 +26,8 @@ const ( Endpoint = "/coreapi" ) +var errNoActionsToExecute = errors.New("no actions to execute") + var _ api.HandlerFactory[api.VM] = (*JSONRPCServerFactory)(nil) var ( @@ -171,7 +173,7 @@ type ExecuteActionReply struct { Error string `json:"error"` } -func (j *JSONRPCServer) Execute( +func (j *JSONRPCServer) ExecuteActions( req *http.Request, args *ExecuteActionArgs, reply *ExecuteActionReply, @@ -180,7 +182,13 @@ func (j *JSONRPCServer) Execute( defer span.End() actionCodec := j.vm.ActionCodec() - actions := make([]chain.Action, 0) + if len(args.Actions) == 0 { + return errNoActionsToExecute + } + if maxActionsPerTx := int(j.vm.Rules(time.Now().Unix()).GetMaxActionsPerTx()); len(args.Actions) > maxActionsPerTx { + return fmt.Errorf("exceeded max actions per simulation: %d", maxActionsPerTx) + } + actions := make([]chain.Action, 0, len(args.Actions)) for _, action := range args.Actions { action, err := actionCodec.Unmarshal(codec.NewReader(action, len(action))) if err != nil { @@ -194,12 +202,12 @@ func (j *JSONRPCServer) Execute( storage := make(map[string][]byte) ts := tstate.New(1) - for _, action := range actions { + for actionIndex, action := range actions { // Get expected state keys stateKeysWithPermissions := action.StateKeys(args.Actor) // flatten the map to a slice of keys - storageKeysToRead := make([][]byte, 0) + storageKeysToRead := make([][]byte, 0, len(stateKeysWithPermissions)) for key := range stateKeysWithPermissions { storageKeysToRead = append(storageKeysToRead, []byte(key)) } @@ -225,7 +233,7 @@ func (j *JSONRPCServer) Execute( tsv, now, args.Actor, - ids.Empty, + chain.CreateActionID(ids.Empty, uint8(actionIndex)), ) if err != nil { reply.Error = fmt.Sprintf("failed to execute action: %s", err)