Skip to content

Commit

Permalink
refactor(staking): do not execute write functions on staticcall
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Oct 12, 2024
1 parent 404bd96 commit 715d729
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
4 changes: 2 additions & 2 deletions precompiles/bank/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) ([]byt
// Deposit and Withdraw methods are both not allowed in read-only mode.
case DepositMethodName, WithdrawMethodName:
if readOnly {
return nil, ptypes.ErrUnexpected{
Got: "method not allowed in read-only mode: " + method.Name,
return nil, ptypes.ErrWriteMethod{
Method: method.Name,
}
}

Expand Down
8 changes: 4 additions & 4 deletions precompiles/bank/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func Test_Methods(t *testing.T) {
success, err := ts.bankContract.Run(ts.mockEVM, ts.mockVMContract, true)
require.ErrorIs(
t,
ptypes.ErrUnexpected{
Got: "method not allowed in read-only mode: deposit",
ptypes.ErrWriteMethod{
Method: "deposit",
},
err)
require.Empty(t, success)
Expand All @@ -73,8 +73,8 @@ func Test_Methods(t *testing.T) {
success, err := ts.bankContract.Run(ts.mockEVM, ts.mockVMContract, true)
require.ErrorIs(
t,
ptypes.ErrUnexpected{
Got: "method not allowed in read-only mode: withdraw",
ptypes.ErrWriteMethod{
Method: "withdraw",
},
err)
require.Empty(t, success)
Expand Down
20 changes: 19 additions & 1 deletion precompiles/staking/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (c *Contract) MoveStake(

// Run is the entrypoint of the precompiled contract, it switches over the input method,
// and execute them accordingly.
func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, _ bool) ([]byte, error) {
func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) ([]byte, error) {
method, err := ABI.MethodById(contract.Input[:4])
if err != nil {
return nil, err
Expand Down Expand Up @@ -415,6 +415,12 @@ func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, _ bool) ([]byte, erro
}
return res, nil
case StakeMethodName:
if readOnly {
return nil, ptypes.ErrWriteMethod{
Method: method.Name,

Check warning on line 420 in precompiles/staking/staking.go

View check run for this annotation

Codecov / codecov/patch

precompiles/staking/staking.go#L419-L420

Added lines #L419 - L420 were not covered by tests
}
}

var res []byte
execErr := stateDB.ExecuteNativeAction(contract.Address(), nil, func(ctx sdk.Context) error {
res, err = c.Stake(ctx, evm, contract, method, args)
Expand All @@ -425,6 +431,12 @@ func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, _ bool) ([]byte, erro
}
return res, nil
case UnstakeMethodName:
if readOnly {
return nil, ptypes.ErrWriteMethod{
Method: method.Name,

Check warning on line 436 in precompiles/staking/staking.go

View check run for this annotation

Codecov / codecov/patch

precompiles/staking/staking.go#L435-L436

Added lines #L435 - L436 were not covered by tests
}
}

var res []byte
execErr := stateDB.ExecuteNativeAction(contract.Address(), nil, func(ctx sdk.Context) error {
res, err = c.Unstake(ctx, evm, contract, method, args)
Expand All @@ -435,6 +447,12 @@ func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, _ bool) ([]byte, erro
}
return res, nil
case MoveStakeMethodName:
if readOnly {
return nil, ptypes.ErrWriteMethod{
Method: method.Name,

Check warning on line 452 in precompiles/staking/staking.go

View check run for this annotation

Codecov / codecov/patch

precompiles/staking/staking.go#L451-L452

Added lines #L451 - L452 were not covered by tests
}
}

var res []byte
execErr := stateDB.ExecuteNativeAction(contract.Address(), nil, func(ctx sdk.Context) error {
res, err = c.MoveStake(ctx, evm, contract, method, args)
Expand Down
8 changes: 8 additions & 0 deletions precompiles/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ func (e ErrInvalidMethod) Error() string {
return fmt.Sprintf("invalid method: %s", e.Method)
}

type ErrWriteMethod struct {
Method string
}

func (e ErrWriteMethod) Error() string {
return fmt.Sprintf("method not allowed in read-only mode: %s", e.Method)
}

type ErrUnexpected struct {
When string
Got string
Expand Down
12 changes: 12 additions & 0 deletions precompiles/types/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,15 @@ func Test_ErrInvalidToken(t *testing.T) {
}
require.ErrorIs(t, ErrInvalidToken{"foo", "bar"}, e)
}

func Test_ErrWriteMethod(t *testing.T) {
e := ErrWriteMethod{
Method: "foo",
}
got := e.Error()
expect := "method not allowed in read-only mode: foo"
if got != expect {
t.Errorf("Expected %v, got %v", expect, got)
}
require.ErrorIs(t, ErrWriteMethod{"foo"}, e)
}

0 comments on commit 715d729

Please sign in to comment.