Skip to content

Commit

Permalink
Use CreateActionID to give simulated actions unique actionID (#1644)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbuchwald authored Oct 10, 2024
1 parent 0a2d991 commit 725a589
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 3 additions & 3 deletions api/jsonrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
)
Expand Down
18 changes: 13 additions & 5 deletions api/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
Endpoint = "/coreapi"
)

var errNoActionsToExecute = errors.New("no actions to execute")

var _ api.HandlerFactory[api.VM] = (*JSONRPCServerFactory)(nil)

var (
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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))
}
Expand All @@ -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)
Expand Down

0 comments on commit 725a589

Please sign in to comment.