Skip to content

Commit

Permalink
explicitly check for failed tx when staking
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Oct 17, 2024
1 parent 01934cc commit 80dfd08
Show file tree
Hide file tree
Showing 6 changed files with 1,094 additions and 900 deletions.
3 changes: 2 additions & 1 deletion cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
precompiledContractTests = []string{
e2etests.TestPrecompilesPrototypeName,
e2etests.TestPrecompilesPrototypeThroughContractName,
// e2etests.TestPrecompilesStakingName,
e2etests.TestPrecompilesStakingName,
// Disabled until further notice, check https://github.com/zeta-chain/node/issues/3005.
// e2etests.TestPrecompilesStakingThroughContractName,
e2etests.TestPrecompilesBankName,
e2etests.TestPrecompilesBankFailName,
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/e2etests.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ var AllE2ETests = []runner.E2ETest{
TestPrecompilesStakingName,
"test stateful precompiled contracts staking",
[]runner.ArgDefinition{},
TestPrecompilesStaking,
TestPrecompilesStakingIsDisabled,
),
runner.NewE2ETest(
TestPrecompilesStakingThroughContractName,
Expand Down
52 changes: 52 additions & 0 deletions e2e/e2etests/test_precompiles_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,58 @@ import (
"github.com/zeta-chain/node/precompiles/staking"
)

func TestPrecompilesStakingIsDisabled(r *runner.E2ERunner, args []string) {
require.Len(r, args, 0, "No arguments expected")

stakingContract, err := staking.NewIStaking(staking.ContractAddress, r.ZEVMClient)
require.NoError(r, err, "Failed to create staking contract caller")

previousGasLimit := r.ZEVMAuth.GasLimit
r.ZEVMAuth.GasLimit = 10000000
defer func() {
r.ZEVMAuth.GasLimit = previousGasLimit
}()

validators, err := stakingContract.GetAllValidators(&bind.CallOpts{})
require.NoError(r, err)
require.GreaterOrEqual(r, len(validators), 2)

CleanValidatorDelegations(r, stakingContract, validators)

// shares are 0 for both validators at the start
sharesBeforeVal1, err := stakingContract.GetShares(&bind.CallOpts{}, r.ZEVMAuth.From, validators[0].OperatorAddress)
require.NoError(r, err)
require.Equal(r, int64(0), sharesBeforeVal1.Int64())

sharesBeforeVal2, err := stakingContract.GetShares(&bind.CallOpts{}, r.ZEVMAuth.From, validators[1].OperatorAddress)
require.NoError(r, err)
require.Equal(r, int64(0), sharesBeforeVal2.Int64())

// stake 3 to validator1
tx, err := stakingContract.Stake(r.ZEVMAuth, r.ZEVMAuth.From, validators[0].OperatorAddress, big.NewInt(3))
require.NoError(r, err)
receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequiredTxFailed(r, receipt)

// unstake 1 from validator1
tx, err = stakingContract.Unstake(r.ZEVMAuth, r.ZEVMAuth.From, validators[0].OperatorAddress, big.NewInt(1))
require.NoError(r, err)
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequiredTxFailed(r, receipt)

// move 1 stake from validator1 to validator2
tx, err = stakingContract.MoveStake(
r.ZEVMAuth,
r.ZEVMAuth.From,
validators[0].OperatorAddress,
validators[1].OperatorAddress,
big.NewInt(1),
)
require.NoError(r, err)
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequiredTxFailed(r, receipt)
}

func TestPrecompilesStaking(r *runner.E2ERunner, args []string) {
require.Len(r, args, 0, "No arguments expected")

Expand Down
113 changes: 64 additions & 49 deletions 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 @@ -414,54 +414,69 @@ func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, _ bool) ([]byte, erro
return nil, err
}
return res, nil
// case StakeMethodName:
// if readOnly {
// return nil, ptypes.ErrWriteMethod{
// Method: 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)
// return err
// })
// if execErr != nil {
// return nil, err
// }
// return res, nil
// case UnstakeMethodName:
// if readOnly {
// return nil, ptypes.ErrWriteMethod{
// Method: 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)
// return err
// })
// if execErr != nil {
// return nil, err
// }
// return res, nil
// case MoveStakeMethodName:
// if readOnly {
// return nil, ptypes.ErrWriteMethod{
// Method: 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)
// return err
// })
// if execErr != nil {
// return nil, err
// }
// return res, nil
case StakeMethodName:
// Disabled until further notice, check https://github.com/zeta-chain/node/issues/3005.
return nil, ptypes.ErrDisabledMethod{
Method: method.Name,
}

if readOnly {

Check failure on line 423 in precompiles/staking/staking.go

View workflow job for this annotation

GitHub Actions / lint

unreachable: unreachable code (govet)
return nil, ptypes.ErrWriteMethod{
Method: 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)
return err
})
if execErr != nil {
return nil, err
}
return res, nil
case UnstakeMethodName:
// Disabled until further notice, check https://github.com/zeta-chain/node/issues/3005.
return nil, ptypes.ErrDisabledMethod{
Method: method.Name,
}

if readOnly {

Check failure on line 444 in precompiles/staking/staking.go

View workflow job for this annotation

GitHub Actions / lint

unreachable: unreachable code (govet)
return nil, ptypes.ErrWriteMethod{
Method: 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)
return err
})
if execErr != nil {
return nil, err
}
return res, nil
case MoveStakeMethodName:
// Disabled until further notice, check https://github.com/zeta-chain/node/issues/3005.
return nil, ptypes.ErrDisabledMethod{
Method: method.Name,
}

if readOnly {

Check failure on line 465 in precompiles/staking/staking.go

View workflow job for this annotation

GitHub Actions / lint

unreachable: unreachable code (govet)
return nil, ptypes.ErrWriteMethod{
Method: 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)
return err
})
if execErr != nil {
return nil, err
}
return res, nil

default:
return nil, ptypes.ErrInvalidMethod{
Expand Down
Loading

0 comments on commit 80dfd08

Please sign in to comment.