Skip to content

Commit

Permalink
test(precompiles): add missing test in staking (#3017)
Browse files Browse the repository at this point in the history
* test(precompiles): add missing test in staking

* remove redundant require
  • Loading branch information
Francisco de Borja Aranda Castillejo authored Oct 17, 2024
1 parent 250b90e commit 920779b
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions precompiles/staking/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/zeta-chain/ethermint/x/evm/statedb"
"github.com/zeta-chain/node/cmd/zetacored/config"
"github.com/zeta-chain/node/precompiles/prototype"
ptypes "github.com/zeta-chain/node/precompiles/types"
"github.com/zeta-chain/node/testutil/keeper"

"github.com/zeta-chain/node/testutil/sample"
Expand Down Expand Up @@ -238,6 +239,33 @@ func Test_InvalidABI(t *testing.T) {
}

func Test_Stake(t *testing.T) {
t.Run("should fail in read only mode", func(t *testing.T) {
// ARRANGE
ctx, contract, abi, sdkKeepers, mockEVM, mockVMContract := setup(t)
methodID := abi.Methods[StakeMethodName]
r := rand.New(rand.NewSource(42))
validator := sample.Validator(t, r)

staker := sample.Bech32AccAddress()
stakerEthAddr := common.BytesToAddress(staker.Bytes())
coins := sample.Coins()
err := sdkKeepers.BankKeeper.MintCoins(ctx, fungibletypes.ModuleName, sample.Coins())
require.NoError(t, err)
err = sdkKeepers.BankKeeper.SendCoinsFromModuleToAccount(ctx, fungibletypes.ModuleName, staker, coins)
require.NoError(t, err)

stakerAddr := common.BytesToAddress(staker.Bytes())
mockVMContract.CallerAddress = stakerAddr
args := []interface{}{stakerEthAddr, validator.OperatorAddress, coins.AmountOf(config.BaseDenom).BigInt()}
mockVMContract.Input = packInputArgs(t, methodID, args...)

// ACT
_, err = contract.Run(mockEVM, mockVMContract, true)

// ASSERT
require.ErrorIs(t, err, ptypes.ErrWriteMethod{Method: StakeMethodName})
})

t.Run("should fail if validator doesn't exist", func(t *testing.T) {
// ARRANGE
ctx, contract, abi, sdkKeepers, mockEVM, mockVMContract := setup(t)
Expand Down Expand Up @@ -483,6 +511,34 @@ func Test_Stake(t *testing.T) {
}

func Test_Unstake(t *testing.T) {
t.Run("should fail in read only method", func(t *testing.T) {
// ARRANGE
ctx, contract, abi, sdkKeepers, mockEVM, mockVMContract := setup(t)
methodID := abi.Methods[UnstakeMethodName]
r := rand.New(rand.NewSource(42))
validator := sample.Validator(t, r)

staker := sample.Bech32AccAddress()
stakerEthAddr := common.BytesToAddress(staker.Bytes())
coins := sample.Coins()
err := sdkKeepers.BankKeeper.MintCoins(ctx, fungibletypes.ModuleName, sample.Coins())
require.NoError(t, err)
err = sdkKeepers.BankKeeper.SendCoinsFromModuleToAccount(ctx, fungibletypes.ModuleName, staker, coins)
require.NoError(t, err)

stakerAddr := common.BytesToAddress(staker.Bytes())
mockVMContract.CallerAddress = stakerAddr

args := []interface{}{stakerEthAddr, validator.OperatorAddress, coins.AmountOf(config.BaseDenom).BigInt()}
mockVMContract.Input = packInputArgs(t, methodID, args...)

// ACT
_, err = contract.Run(mockEVM, mockVMContract, true)

// ASSERT
require.ErrorIs(t, err, ptypes.ErrWriteMethod{Method: UnstakeMethodName})
})

t.Run("should fail if validator doesn't exist", func(t *testing.T) {
// ARRANGE
ctx, contract, abi, sdkKeepers, mockEVM, mockVMContract := setup(t)
Expand Down Expand Up @@ -721,6 +777,53 @@ func Test_Unstake(t *testing.T) {
}

func Test_MoveStake(t *testing.T) {
t.Run("should fail in read only method", func(t *testing.T) {
// ARRANGE
ctx, contract, abi, sdkKeepers, mockEVM, mockVMContract := setup(t)
methodID := abi.Methods[MoveStakeMethodName]
r := rand.New(rand.NewSource(42))
validatorSrc := sample.Validator(t, r)
sdkKeepers.StakingKeeper.SetValidator(ctx, validatorSrc)
validatorDest := sample.Validator(t, r)

staker := sample.Bech32AccAddress()
stakerEthAddr := common.BytesToAddress(staker.Bytes())
coins := sample.Coins()
err := sdkKeepers.BankKeeper.MintCoins(ctx, fungibletypes.ModuleName, sample.Coins())
require.NoError(t, err)
err = sdkKeepers.BankKeeper.SendCoinsFromModuleToAccount(ctx, fungibletypes.ModuleName, staker, coins)
require.NoError(t, err)

stakerAddr := common.BytesToAddress(staker.Bytes())
mockVMContract.CallerAddress = stakerAddr

argsStake := []interface{}{
stakerEthAddr,
validatorSrc.OperatorAddress,
coins.AmountOf(config.BaseDenom).BigInt(),
}

// stake to validator src
stakeMethodID := abi.Methods[StakeMethodName]
mockVMContract.Input = packInputArgs(t, stakeMethodID, argsStake...)
_, err = contract.Run(mockEVM, mockVMContract, false)
require.NoError(t, err)

argsMoveStake := []interface{}{
stakerEthAddr,
validatorSrc.OperatorAddress,
validatorDest.OperatorAddress,
coins.AmountOf(config.BaseDenom).BigInt(),
}
mockVMContract.Input = packInputArgs(t, methodID, argsMoveStake...)

// ACT
_, err = contract.Run(mockEVM, mockVMContract, true)

// ASSERT
require.ErrorIs(t, err, ptypes.ErrWriteMethod{Method: MoveStakeMethodName})
})

t.Run("should fail if validator dest doesn't exist", func(t *testing.T) {
// ARRANGE
ctx, contract, abi, sdkKeepers, mockEVM, mockVMContract := setup(t)
Expand Down

0 comments on commit 920779b

Please sign in to comment.