Skip to content

Commit

Permalink
test(*): more test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdubpark committed Nov 18, 2024
1 parent 6f4d42f commit 3cf4f97
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 603 deletions.
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ linters-settings:
nolintlint:
require-explanation: true
require-specific: true
paralleltest:
# Ignore missing calls to `t.Parallel()` and only report incorrect uses of it.
# Default: false
ignore-missing: true
# Ignore missing calls to `t.Parallel()` in subtests. Top-level tests are
# still required to have `t.Parallel`, but subtests are allowed to skip it.
# Default: false
ignore-missing-subtests: true
revive:
enable-all-rules: true
severity: warning
Expand Down
1 change: 0 additions & 1 deletion client/x/evmengine/keeper/abci_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ func TestKeeper_PrepareProposal(t *testing.T) {
})
}

//nolint:paralleltest // no parallel test for now
func TestKeeper_PostFinalize(t *testing.T) {
payloadID := eengine.PayloadID{0x1}
payloadFailedToSet := func(k *Keeper) {
Expand Down
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
102 changes: 48 additions & 54 deletions client/x/evmstaking/keeper/deposit_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package keeper_test

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

"cosmossdk.io/math"
sdkmath "cosmossdk.io/math"
Expand All @@ -19,17 +17,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 +34,13 @@ 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())
_ = 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 @@ -68,11 +63,11 @@ func (s *TestSuite) TestProcessDeposit() {
}
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())
// 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)
// accountKeeper.EXPECT().HasAccount(gomock.Any(), delAddr).Return(true)
}
}

Expand Down Expand Up @@ -119,16 +114,16 @@ func (s *TestSuite) TestProcessDeposit() {
},
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: 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(""))
// 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",
Expand All @@ -137,7 +132,7 @@ func (s *TestSuite) TestProcessDeposit() {
name: "fail: mint coins to new delegator",
settingMock: func() {
expectAccountMock(true)
bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(errors.New(""))
// 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",
Expand All @@ -146,8 +141,8 @@ func (s *TestSuite) TestProcessDeposit() {
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(""))
// 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",
Expand All @@ -156,8 +151,8 @@ func (s *TestSuite) TestProcessDeposit() {
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(""))
// 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",
Expand All @@ -166,9 +161,9 @@ func (s *TestSuite) TestProcessDeposit() {
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"))
// 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",
Expand All @@ -177,9 +172,9 @@ func (s *TestSuite) TestProcessDeposit() {
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"))
// 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",
Expand All @@ -188,48 +183,48 @@ func (s *TestSuite) TestProcessDeposit() {
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)
// 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)
},
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{},
},
},
//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{},
// },
// },
},
},
{
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)
// 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)
},
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{},
},
},
//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{},
// },
// },
},
},
}
Expand All @@ -248,7 +243,7 @@ func (s *TestSuite) TestProcessDeposit() {
// check delegation
delegation, err := stakingKeeper.GetDelegation(cachedCtx, delAddr, valAddr)
require.NoError(err)
delegation.PeriodDelegations[stypes.FlexibleDelegationID].EndTime = time.Time{}
// delegation.PeriodDelegations[stypes.FlexibleDelegationID].EndTime = time.Time{}
require.Equal(tc.expectedResult, delegation)
}
})
Expand Down Expand Up @@ -291,4 +286,3 @@ func (s *TestSuite) TestParseDepositLog() {
})
}
}
*/
15 changes: 11 additions & 4 deletions client/x/evmstaking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,12 +810,19 @@ func (s *TestSuite) setupValidatorAndDelegation(ctx context.Context, valPubKey,
require.Equal(valTokens, issuedShares.RoundInt())

// bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stypes.NotBondedPoolName, stypes.BondedPoolName, gomock.Any()).Times(2)
_ = skeeper.TestingUpdateValidator(stakingKeeper, sdkCtx, validator, true)
// _ = skeeper.TestingUpdateValidator(stakingKeeper, sdkCtx, validator, true)

// Create/Get period delegation
periodDelegation, err := stakingKeeper.GetOrCreatePeriodDelegation(ctx, delAddr, valAddr, stypes.FlexiblePeriodDelegationID, stypes.DefaultFlexiblePeriodType, time.Now())
require.NoError(err)

// Create and set delegation
delAmt := stakingKeeper.TokensFromConsensusPower(ctx, 100).ToLegacyDec()
delegation := stypes.NewDelegation(delAddr.String(), valAddr.String(), delAmt, sdkmath.LegacyZeroDec())
require.NoError(stakingKeeper.SetDelegation(ctx, delegation))
_, _, err = stakingKeeper.Delegate(
ctx, delAddr, delAmt.RoundInt(), stypes.Unbonded, val, false,
periodDelegation.PeriodDelegationId, periodDelegation.PeriodType, periodDelegation.EndTime,
)
require.NoError(err)

validator.DelegatorShares = validator.DelegatorShares.Add(delAmt)
validator.DelegatorRewardsShares = validator.DelegatorRewardsShares.Add(delAmt)
Expand All @@ -831,7 +838,7 @@ func (s *TestSuite) setupValidatorAndDelegation(ctx context.Context, valPubKey,
// require.NoError(evmstakingKeeper.DelegatorOperatorAddress.Set(ctx, delAddr.String(), delEvmAddr.String()))

// Ensure delegation is set correctly
delegation, err = stakingKeeper.GetDelegation(ctx, delAddr, valAddr)
delegation, err := stakingKeeper.GetDelegation(ctx, delAddr, valAddr)
require.NoError(err)
require.Equal(delAmt, delegation.GetShares())
}
Expand Down
10 changes: 5 additions & 5 deletions client/x/evmstaking/keeper/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ func (s *TestSuite) TestProcessCreateValidator() {
{
name: "fail: nil validator pubkey",
valUncmpPubKey: nil,
expectedError: "validator pubkey to cosmos",
expectedError: "compress validator pubkey: invalid uncompressed public key length or format",
},
{
name: "fail: invalid validator pubkey",
valUncmpPubKey: uncmpPubKey0[1:],
expectedError: "validator pubkey to cosmos",
expectedError: "compress validator pubkey: invalid uncompressed public key length or format",
},
{
name: "fail: corrupted validator pubkey",
valUncmpPubKey: corruptedPubKey,
expectedError: "validator pubkey to evm address",
expectedError: "validator pubkey to evm address: invalid public key",
},
{
name: "fail: mint coins",
Expand Down Expand Up @@ -144,7 +144,7 @@ func (s *TestSuite) TestProcessCreateValidator() {
// accountKeeper.EXPECT().SetAccount(gomock.Any(), gomock.Any())
// bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil)
// bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), types.ModuleName, valDelAddr, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().DelegateCoinsFromAccountToModule(gomock.Any(), valDelAddr, gomock.Any(), gomock.Any()).Return(nil)
// bankKeeper.EXPECT().DelegateCoinsFromAccountToModule(gomock.Any(), valDelAddr, gomock.Any(), gomock.Any()).Return(nil)
},
postCheck: checkDelegatorMapAndValidator,
},
Expand Down Expand Up @@ -196,7 +196,7 @@ func (s *TestSuite) TestProcessCreateValidator() {
// accountKeeper.EXPECT().SetAccount(gomock.Any(), gomock.Any())
// bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil)
// bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), types.ModuleName, valDelAddr, gomock.Any()).Return(nil)
//bankKeeper.EXPECT().DelegateCoinsFromAccountToModule(gomock.Any(), valDelAddr, gomock.Any(), gomock.Any()).Return(nil)
// bankKeeper.EXPECT().DelegateCoinsFromAccountToModule(gomock.Any(), valDelAddr, gomock.Any(), gomock.Any()).Return(nil)
},
postCheck: checkDelegatorMapAndValTokens,
},
Expand Down
Loading

0 comments on commit 3cf4f97

Please sign in to comment.