Skip to content

Commit

Permalink
Add back actionID to StateKeys() (#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoVillar authored Oct 24, 2024
1 parent cf6d48a commit 82dc621
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (j *JSONRPCServer) ExecuteActions(

for actionIndex, action := range actions {
// Get expected state keys
stateKeysWithPermissions := action.StateKeys(args.Actor)
stateKeysWithPermissions := action.StateKeys(args.Actor, chain.CreateActionID(ids.Empty, uint8(actionIndex)))

// flatten the map to a slice of keys
storageKeysToRead := make([][]byte, 0, len(stateKeysWithPermissions))
Expand Down
12 changes: 9 additions & 3 deletions chain/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,22 @@ type Action interface {
// All keys specified must be suffixed with the number of chunks that could ever be read from that
// key (formatted as a big-endian uint16). This is used to automatically calculate storage usage.
//
// If any key is removed and then re-created, this will count as a creation instead of a modification.
StateKeys(actor codec.Address) state.Keys
// If any key is removed and then re-created, this will count as a creation
// instead of a modification.
//
// [actionID] is a unique, but nonrandom identifier for each [Action].
StateKeys(actor codec.Address, actionID ids.ID) state.Keys

// Execute actually runs the [Action]. Any state changes that the [Action] performs should
// be done here.
//
// If any keys are touched during [Execute] that are not specified in [StateKeys], the transaction
// will revert and the max fee will be charged.
//
// If [Execute] returns an error, execution will halt and any state changes will revert.
// If [Execute] returns an error, execution will halt and any state changes
// will revert.
//
// [actionID] is a unique, but nonrandom identifier for each [Action].
Execute(
ctx context.Context,
r Rules,
Expand Down
8 changes: 4 additions & 4 deletions chain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ func (t *Transaction) StateKeys(bh BalanceHandler) (state.Keys, error) {
stateKeys := make(state.Keys)

// Verify the formatting of state keys passed by the controller
for _, action := range t.Actions {
for k, v := range action.StateKeys(t.Auth.Actor()) {
for i, action := range t.Actions {
for k, v := range action.StateKeys(t.Auth.Actor(), CreateActionID(t.ID(), uint8(i))) {
if !stateKeys.Add(k, v) {
return nil, ErrInvalidKeyValue
}
Expand Down Expand Up @@ -517,14 +517,14 @@ func EstimateUnits(r Rules, actions Actions, authFactory AuthFactory) (fees.Dime

// Calculate over action/auth
bandwidth += consts.Uint8Len
for _, action := range actions {
for i, action := range actions {
actionSize, err := GetSize(action)
if err != nil {
return fees.Dimensions{}, err
}

actor := authFactory.Address()
stateKeys := action.StateKeys(actor)
stateKeys := action.StateKeys(actor, CreateActionID(ids.Empty, uint8(i)))
actionStateKeysMaxChunks, ok := stateKeys.ChunkSizes()
if !ok {
return fees.Dimensions{}, ErrInvalidKeyValue
Expand Down
2 changes: 1 addition & 1 deletion chain/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (*abstractMockAction) Execute(_ context.Context, _ chain.Rules, _ state.Mut
panic("unimplemented")
}

func (*abstractMockAction) StateKeys(_ codec.Address) state.Keys {
func (*abstractMockAction) StateKeys(_ codec.Address, _ ids.ID) state.Keys {
panic("unimplemented")
}

Expand Down
2 changes: 1 addition & 1 deletion examples/morpheusvm/actions/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (*Transfer) GetTypeID() uint8 {
return mconsts.TransferID
}

func (t *Transfer) StateKeys(actor codec.Address) state.Keys {
func (t *Transfer) StateKeys(actor codec.Address, _ ids.ID) state.Keys {
return state.Keys{
string(storage.BalanceKey(actor)): state.Read | state.Write,
string(storage.BalanceKey(t.To)): state.All,
Expand Down
2 changes: 1 addition & 1 deletion examples/vmwithcontracts/actions/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (*Call) GetTypeID() uint8 {
return mconsts.CallContractID
}

func (t *Call) StateKeys(_ codec.Address) state.Keys {
func (t *Call) StateKeys(_ codec.Address, _ ids.ID) state.Keys {
result := state.Keys{}
for _, stateKeyPermission := range t.SpecifiedStateKeys {
result.Add(stateKeyPermission.Key, stateKeyPermission.Permission)
Expand Down
2 changes: 1 addition & 1 deletion examples/vmwithcontracts/actions/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (*Deploy) GetTypeID() uint8 {
return mconsts.DeployID
}

func (d *Deploy) StateKeys(_ codec.Address) state.Keys {
func (d *Deploy) StateKeys(_ codec.Address, _ ids.ID) state.Keys {
if d.address == codec.EmptyAddress {
d.address = storage.GetAddressForDeploy(0, d.CreationInfo)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/vmwithcontracts/actions/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (*Publish) GetTypeID() uint8 {
return mconsts.PublishID
}

func (t *Publish) StateKeys(_ codec.Address) state.Keys {
func (t *Publish) StateKeys(_ codec.Address, _ ids.ID) state.Keys {
if t.id == nil {
hashedID := sha256.Sum256(t.ContractBytes)
t.id, _ = keys.Encode(storage.ContractsKey(hashedID[:]), len(t.ContractBytes))
Expand Down
2 changes: 1 addition & 1 deletion examples/vmwithcontracts/actions/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (*Transfer) GetTypeID() uint8 {
return mconsts.TransferID
}

func (t *Transfer) StateKeys(actor codec.Address) state.Keys {
func (t *Transfer) StateKeys(actor codec.Address, _ ids.ID) state.Keys {
return state.Keys{
string(storage.BalanceKey(actor)): state.Read | state.Write,
string(storage.BalanceKey(t.To)): state.All,
Expand Down

0 comments on commit 82dc621

Please sign in to comment.