-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
205 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package staking | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
"github.com/stretchr/testify/require" | ||
precompiletypes "github.com/zeta-chain/node/precompiles/types" | ||
"github.com/zeta-chain/node/testutil/sample" | ||
) | ||
|
||
func Test_GetValidators(t *testing.T) { | ||
t.Run("should return an empty list for a non staker address", func(t *testing.T) { | ||
/* ARRANGE */ | ||
s := newTestSuite(t) | ||
|
||
// Create validator. | ||
validator := sample.Validator(t, rand.New(rand.NewSource(42))) | ||
s.sdkKeepers.StakingKeeper.SetValidator(s.ctx, validator) | ||
|
||
// Create staker. | ||
stakerEVMAddr := sample.EthAddress() | ||
|
||
/* ACT */ | ||
// Call getRewards. | ||
getValidatorsMethod := s.stkContractABI.Methods[GetValidatorsMethodName] | ||
|
||
s.mockVMContract.Input = packInputArgs( | ||
t, | ||
getValidatorsMethod, | ||
[]interface{}{stakerEVMAddr}..., | ||
) | ||
|
||
bytes, err := s.stkContract.Run(s.mockEVM, s.mockVMContract, false) | ||
require.NoError(t, err) | ||
|
||
res, err := getValidatorsMethod.Outputs.Unpack(bytes) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, res) | ||
|
||
list, ok := res[0].([]string) | ||
require.True(t, ok) | ||
require.Len(t, list, 0) | ||
}) | ||
|
||
t.Run("should return staker's validator list", func(t *testing.T) { | ||
/* ARRANGE */ | ||
s := newTestSuite(t) | ||
|
||
// Create validator. | ||
validator := sample.Validator(t, rand.New(rand.NewSource(42))) | ||
s.sdkKeepers.StakingKeeper.SetValidator(s.ctx, validator) | ||
|
||
// Create staker. | ||
stakerEVMAddr := sample.EthAddress() | ||
stakerCosmosAddr, err := precompiletypes.GetCosmosAddress(s.sdkKeepers.BankKeeper, stakerEVMAddr) | ||
require.NoError(t, err) | ||
|
||
// Become a staker. | ||
stakeThroughCosmosAPI( | ||
t, | ||
s.ctx, | ||
s.sdkKeepers.BankKeeper, | ||
s.sdkKeepers.StakingKeeper, | ||
validator, | ||
stakerCosmosAddr, | ||
math.NewInt(100), | ||
) | ||
|
||
/* ACT */ | ||
// Call getRewards. | ||
getValidatorsMethod := s.stkContractABI.Methods[GetValidatorsMethodName] | ||
|
||
s.mockVMContract.Input = packInputArgs( | ||
t, | ||
getValidatorsMethod, | ||
[]interface{}{stakerEVMAddr}..., | ||
) | ||
|
||
bytes, err := s.stkContract.Run(s.mockEVM, s.mockVMContract, false) | ||
require.NoError(t, err) | ||
|
||
res, err := getValidatorsMethod.Outputs.Unpack(bytes) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, res) | ||
|
||
list, ok := res[0].([]string) | ||
require.True(t, ok) | ||
require.Len(t, list, 1) | ||
require.Equal(t, validator.GetOperator().String(), list[0]) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters