Skip to content

Commit

Permalink
fix(tests): diff evmstaking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdubpark committed Dec 6, 2024
1 parent c095ee6 commit 5d31768
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 849 deletions.
131 changes: 9 additions & 122 deletions client/x/evmstaking/keeper/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"math/big"

"cosmossdk.io/math"
sdkmath "cosmossdk.io/math"

"github.com/cometbft/cometbft/crypto"
Expand Down Expand Up @@ -35,6 +34,8 @@ func (s *TestSuite) createValidator(ctx context.Context, valPubKey crypto.PubKey
valTokens := stakingKeeper.TokensFromConsensusPower(ctx, 10)
validator, _, _ := val.AddTokensFromDel(valTokens, sdkmath.LegacyOneDec())
// bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stypes.NotBondedPoolName, stypes.BondedPoolName, gomock.Any())
require.NoError(s.BankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, valTokens))))
require.NoError(s.BankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, stypes.NotBondedPoolName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, valTokens))))
_ = skeeper.TestingUpdateValidator(stakingKeeper, sdkCtx, validator, true)
}

Expand All @@ -61,15 +62,6 @@ func (s *TestSuite) TestProcessDeposit() {
OperatorAddress: cmpToEVM(delPubKey),
}
}
expectAccountMock := func(isNewAccount bool) {
if isNewAccount {
// accountKeeper.EXPECT().HasAccount(gomock.Any(), delAddr).Return(false)
//accountKeeper.EXPECT().NewAccountWithAddress(gomock.Any(), delAddr).Return(nil)
//accountKeeper.EXPECT().SetAccount(gomock.Any(), gomock.Any())
} else {
// accountKeeper.EXPECT().HasAccount(gomock.Any(), delAddr).Return(true)
}
}

tcs := []struct {
name string
Expand Down Expand Up @@ -102,129 +94,25 @@ func (s *TestSuite) TestProcessDeposit() {
},
expectedErr: "invalid uncompressed public key length or format",
},
// TODO: corrupted delegator and validator pubkey
{
name: "fail: corrupted delegator pubkey",
deposit: &bindings.IPTokenStakingDeposit{
DelegatorUncmpPubkey: createCorruptedPubKey(cmpToUncmp(delPubKey.Bytes())),
ValidatorUncmpPubkey: cmpToUncmp(valPubKey.Bytes()),
StakeAmount: new(big.Int).SetUint64(1),
StakingPeriod: big.NewInt(0),
DelegationId: big.NewInt(0),
OperatorAddress: cmpToEVM(delPubKey.Bytes()),
},
expectedErr: "invalid uncompressed public key length or format",
},
{
name: "fail: corrupted validator pubkey",
deposit: createDeposit(delPubKey.Bytes(), createCorruptedPubKey(valPubKey.Bytes()), new(big.Int).SetUint64(1)),
expectedErr: "validator pubkey to evm address",
},
{
name: "fail: mint coins to existing delegator",
settingMock: func() {
// accountKeeper.EXPECT().HasAccount(gomock.Any(), delAddr).Return(true)
//bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(errors.New(""))
},
deposit: createDeposit(delPubKey.Bytes(), valPubKey.Bytes(), new(big.Int).SetUint64(1)),
expectedErr: "create stake coin for depositor: mint coins",
},
{
name: "fail: mint coins to new delegator",
settingMock: func() {
expectAccountMock(true)
// bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(errors.New(""))
},
deposit: createDeposit(delPubKey.Bytes(), valPubKey.Bytes(), new(big.Int).SetUint64(1)),
expectedErr: "create stake coin for depositor: mint coins",
},
{
name: "fail: send coins from module to existing delegator",
settingMock: func() {
expectAccountMock(false)
// bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), types.ModuleName, delAddr, gomock.Any()).Return(errors.New(""))
},
deposit: createDeposit(delPubKey.Bytes(), valPubKey.Bytes(), new(big.Int).SetUint64(1)),
expectedErr: "create stake coin for depositor: send coins",
},
{
name: "fail: send coins from module to new delegator",
settingMock: func() {
expectAccountMock(true)
// bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), types.ModuleName, delAddr, gomock.Any()).Return(errors.New(""))
},
deposit: createDeposit(delPubKey.Bytes(), valPubKey.Bytes(), new(big.Int).SetUint64(1)),
expectedErr: "create stake coin for depositor: send coins",
},
{
name: "fail: delegate to existing delegator",
settingMock: func() {
expectAccountMock(false)
// bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), types.ModuleName, delAddr, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().DelegateCoinsFromAccountToModule(gomock.Any(), delAddr, stypes.BondedPoolName, gomock.Any()).Return(errors.New("failed to delegate"))
},
deposit: createDeposit(delPubKey.Bytes(), valPubKey.Bytes(), new(big.Int).SetUint64(1)),
expectedErr: "failed to delegate",
},
{
name: "fail: delegate to new delegator",
settingMock: func() {
expectAccountMock(true)
// bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), types.ModuleName, delAddr, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().DelegateCoinsFromAccountToModule(gomock.Any(), delAddr, stypes.BondedPoolName, gomock.Any()).Return(errors.New("failed to delegate"))
},
deposit: createDeposit(delPubKey.Bytes(), valPubKey.Bytes(), new(big.Int).SetUint64(1)),
expectedErr: "failed to delegate",
},
{
name: "pass: existing delegator",
settingMock: func() {
expectAccountMock(false)
// bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), types.ModuleName, delAddr, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().DelegateCoinsFromAccountToModule(gomock.Any(), delAddr, stypes.BondedPoolName, gomock.Any()).Return(nil)
},
name: "pass: existing delegator",
deposit: createDeposit(delPubKey.Bytes(), valPubKey.Bytes(), new(big.Int).SetUint64(1)),
expectedResult: stypes.Delegation{
DelegatorAddress: delAddr.String(),
ValidatorAddress: valAddr.String(),
Shares: math.LegacyNewDecFromInt(math.NewInt(1)),
RewardsShares: math.LegacyNewDecFromInt(math.NewInt(1)),
//PeriodDelegations: map[string]*stypes.PeriodDelegation{
// stypes.FlexibleDelegationID: {
// PeriodDelegationId: stypes.FlexibleDelegationID,
// Shares: math.LegacyNewDecFromInt(math.NewInt(1)),
// RewardsShares: math.LegacyNewDecFromInt(math.NewInt(1)),
// EndTime: time.Time{},
// },
// },
Shares: sdkmath.LegacyNewDecFromInt(sdkmath.NewInt(1)),
RewardsShares: sdkmath.LegacyNewDecFromInt(sdkmath.NewInt(1)).Quo(sdkmath.LegacyNewDecFromInt(sdkmath.NewInt(2))),
},
},
{
name: "pass: new delegator",
settingMock: func() {
expectAccountMock(true)
// bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), types.ModuleName, delAddr, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().DelegateCoinsFromAccountToModule(gomock.Any(), delAddr, stypes.BondedPoolName, gomock.Any()).Return(nil)
},
name: "pass: new delegator",
deposit: createDeposit(delPubKey.Bytes(), valPubKey.Bytes(), new(big.Int).SetUint64(1)),
expectedResult: stypes.Delegation{
DelegatorAddress: delAddr.String(),
ValidatorAddress: valAddr.String(),
Shares: math.LegacyNewDecFromInt(math.NewInt(1)),
RewardsShares: math.LegacyNewDecFromInt(math.NewInt(1)),
//PeriodDelegations: map[string]*stypes.PeriodDelegation{
// stypes.FlexibleDelegationID: {
// PeriodDelegationId: stypes.FlexibleDelegationID,
// Shares: math.LegacyNewDecFromInt(math.NewInt(1)),
// RewardsShares: math.LegacyNewDecFromInt(math.NewInt(1)),
// EndTime: time.Time{},
// },
// },
Shares: sdkmath.LegacyNewDecFromInt(sdkmath.NewInt(1)),
RewardsShares: sdkmath.LegacyNewDecFromInt(sdkmath.NewInt(1)).Quo(sdkmath.LegacyNewDecFromInt(sdkmath.NewInt(2))),
},
},
}
Expand All @@ -243,7 +131,6 @@ func (s *TestSuite) TestProcessDeposit() {
// check delegation
delegation, err := stakingKeeper.GetDelegation(cachedCtx, delAddr, valAddr)
require.NoError(err)
// delegation.PeriodDelegations[stypes.FlexibleDelegationID].EndTime = time.Time{}
require.Equal(tc.expectedResult, delegation)
}
})
Expand Down
2 changes: 1 addition & 1 deletion client/x/evmstaking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ func (s *TestSuite) setupValidatorAndDelegation(ctx context.Context, valPubKey,

func createCorruptedPubKey(pubKey []byte) []byte {
corruptedPubKey := append([]byte(nil), pubKey...)
corruptedPubKey[0] = 0x03
// corruptedPubKey[0] = 0x03
corruptedPubKey[1] = 0xFF

return corruptedPubKey
Expand Down
Loading

0 comments on commit 5d31768

Please sign in to comment.