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(x/evmstaking): reenable tests #324

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 0 additions & 4 deletions client/x/evmengine/keeper/upgrades_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ var (
)

func TestKeeper_ProcessSoftwareUpgrade(t *testing.T) {
t.Parallel()
keeper, ctx, ctrl, uk := setupTestEnvironment(t)
t.Cleanup(ctrl.Finish)

Expand Down Expand Up @@ -88,7 +87,6 @@ func TestKeeper_ProcessSoftwareUpgrade(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tc.setupMock()
err := keeper.ProcessSoftwareUpgrade(ctx, tc.ev())
if tc.expectedErr != "" {
Expand All @@ -102,7 +100,6 @@ func TestKeeper_ProcessSoftwareUpgrade(t *testing.T) {
}

func TestKeeper_ProcessUpgradeEvents(t *testing.T) {
t.Parallel()
keeper, ctx, ctrl, uk := setupTestEnvironment(t)
t.Cleanup(ctrl.Finish)

Expand Down Expand Up @@ -241,7 +238,6 @@ func TestKeeper_ProcessUpgradeEvents(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if tc.setupMock != nil {
tc.setupMock()
}
Expand Down
143 changes: 12 additions & 131 deletions client/x/evmstaking/keeper/deposit_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package keeper_test

/*
import (
"context"
"math/big"
"time"

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

"github.com/cometbft/cometbft/crypto"
Expand All @@ -19,17 +16,14 @@ import (

"github.com/piplabs/story/client/x/evmstaking/types"
"github.com/piplabs/story/contracts/bindings"
"github.com/piplabs/story/lib/errors"
"github.com/piplabs/story/lib/k1util"

"go.uber.org/mock/gomock"
)

// createValidator creates a validator.
func (s *TestSuite) createValidator(ctx context.Context, valPubKey crypto.PubKey, valAddr sdk.ValAddress) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
require := s.Require()
bankKeeper, stakingKeeper := s.BankKeeper, s.StakingKeeper
stakingKeeper := s.StakingKeeper

// Convert public key to cosmos format
valCosmosPubKey, err := k1util.PubKeyToCosmos(valPubKey)
Expand All @@ -39,13 +33,15 @@ func (s *TestSuite) createValidator(ctx context.Context, valPubKey crypto.PubKey
val := testutil.NewValidator(s.T(), valAddr, valCosmosPubKey)
valTokens := stakingKeeper.TokensFromConsensusPower(ctx, 10)
validator, _, _ := val.AddTokensFromDel(valTokens, sdkmath.LegacyOneDec())
bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stypes.NotBondedPoolName, stypes.BondedPoolName, gomock.Any())
// 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)
}

func (s *TestSuite) TestProcessDeposit() {
require := s.Require()
ctx, keeper, accountKeeper, bankKeeper, stakingKeeper := s.Ctx, s.EVMStakingKeeper, s.AccountKeeper, s.BankKeeper, s.StakingKeeper
ctx, keeper, stakingKeeper := s.Ctx, s.EVMStakingKeeper, s.StakingKeeper

pubKeys, accAddrs, valAddrs := createAddresses(2)
// delegator
Expand All @@ -66,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 @@ -107,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 @@ -248,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 Expand Up @@ -291,4 +173,3 @@ func (s *TestSuite) TestParseDepositLog() {
})
}
}
*/
7 changes: 1 addition & 6 deletions client/x/evmstaking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (k Keeper) ValidatorAddressCodec() addresscodec.Codec {
return k.validatorAddressCodec
}

//nolint:gocyclo // TODO
//nolint:gocyclo // a lot of switch cases, could be refactored
func (k Keeper) ProcessStakingEvents(ctx context.Context, height uint64, logs []*evmenginetypes.EVMEvent) error {
gwei, exp := big.NewInt(10), big.NewInt(9)
gwei.Exp(gwei, exp, nil)
Expand All @@ -123,11 +123,6 @@ func (k Keeper) ProcessStakingEvents(ctx context.Context, height uint64, logs []
return err
}

// TODO: handle when each event processing fails.

// Convert the amount from wei to gwei (Eth2 spec withdrawal is specified in gwei) by dividing by 10^9.
// TODO: consider rounding and decimal precision when dividing bigint.

switch ethlog.Topics[0] {
case types.UpdateValidatorCommission.ID:
ev, err := k.ipTokenStakingContract.ParseUpdateValidatorCommssion(ethlog)
Expand Down
Loading
Loading