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 11, 2024
1 parent 404bd96 commit aad0f0f
Showing 1 changed file with 19 additions and 1 deletion.
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.ErrUnexpected{
Got: "method not allowed in read-only mode: " + method.Name,
}
}

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.ErrUnexpected{
Got: "method not allowed in read-only mode: " + method.Name,
}
}

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.ErrUnexpected{
Got: "method not allowed in read-only mode: " + method.Name,
}
}

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

0 comments on commit aad0f0f

Please sign in to comment.