Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(precompiles): add missing test in staking #3017

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading