Skip to content

Commit

Permalink
fix(evmstaking): fix tests with implementation of epoched staking
Browse files Browse the repository at this point in the history
  • Loading branch information
Narangde committed Sep 18, 2024
1 parent dff4399 commit 4e25327
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 13 deletions.
6 changes: 1 addition & 5 deletions client/x/evmstaking/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ func (s *TestSuite) TestEndBlock() {
pastHeader.Time = pastHeader.Time.Add(-ubdTime).Add(-time.Minute)
s.setupUnbonding(sdkCtx.WithBlockHeader(pastHeader), delAddr, valAddr1, "10")

// Mock staking.EndBlocker
s.BankKeeper.EXPECT().UndelegateCoinsFromModuleToAccount(gomock.Any(), stypes.NotBondedPoolName, delAddr, gomock.Any()).Return(nil)
// Mock evmstaking.EndBlocker
s.BankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), delAddr, types.ModuleName, gomock.Any()).Return(errors.New("failed to send coins to module"))

Expand All @@ -268,8 +266,6 @@ func (s *TestSuite) TestEndBlock() {
pastHeader.Time = pastHeader.Time.Add(-ubdTime).Add(-time.Minute)
s.setupUnbonding(sdkCtx.WithBlockHeader(pastHeader), delAddr, valAddr1, "10")

// Mock staking.EndBlocker
s.BankKeeper.EXPECT().UndelegateCoinsFromModuleToAccount(gomock.Any(), stypes.NotBondedPoolName, delAddr, gomock.Any()).Return(nil)
// Mock evmstaking.EndBlocker
s.BankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), delAddr, types.ModuleName, gomock.Any()).Return(nil)
s.BankKeeper.EXPECT().BurnCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(errors.New("failed to burn coins"))
Expand Down Expand Up @@ -370,7 +366,7 @@ func (s *TestSuite) setupMatureUnbondingDelegation(ctx sdk.Context, delAddr sdk.
s.setupUnbonding(pastCtx, delAddr, valAddr, amt)

// Mock staking.EndBlocker
s.BankKeeper.EXPECT().UndelegateCoinsFromModuleToAccount(gomock.Any(), stypes.NotBondedPoolName, delAddr, gomock.Any()).Return(nil)
s.BankKeeper.EXPECT().UndelegateCoinsFromModuleToAccount(gomock.Any(), stypes.NotBondedPoolName, delAddr, gomock.Any()).Return(nil).AnyTimes()
// Mock evmstaking.EndBlocker
s.BankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), delAddr, types.ModuleName, gomock.Any()).Return(nil)
s.BankKeeper.EXPECT().BurnCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil)
Expand Down
11 changes: 10 additions & 1 deletion client/x/evmstaking/keeper/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,16 @@ func (s *TestSuite) TestProcessDeposit() {
tc.settingMock()
}
cachedCtx, _ := ctx.CacheContext()
err := keeper.HandleDepositEvent(cachedCtx, tc.deposit)
var err error
err = keeper.HandleDepositEvent(cachedCtx, tc.deposit)
if !keeper.MessageQueue.IsEmpty(cachedCtx) {
var queuedMsgs []*types.QueuedMessage
queuedMsgs, err = keeper.DequeueAllMsgs(cachedCtx)

for _, msg := range queuedMsgs {
err = keeper.ProcessMsg(cachedCtx, msg)
}
}
if tc.expectedErr != "" {
require.ErrorContains(err, tc.expectedErr)
} else {
Expand Down
32 changes: 29 additions & 3 deletions client/x/evmstaking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/suite"

epochstypes "github.com/piplabs/story/client/x/epochs/types"
evmenginetypes "github.com/piplabs/story/client/x/evmengine/types"
"github.com/piplabs/story/client/x/evmstaking/keeper"
"github.com/piplabs/story/client/x/evmstaking/module"
Expand All @@ -46,7 +47,11 @@ import (
)

var (
PKs = simtestutil.CreateTestPubKeys(3)
PKs = simtestutil.CreateTestPubKeys(3)
TestEVMStakingParams = types.NewParams(types.DefaultMaxWithdrawalPerBlock,
types.DefaultMaxSweepPerBlock,
types.DefaultMinPartialWithdrawalAmount,
"second")
)

type TestSuite struct {
Expand Down Expand Up @@ -111,6 +116,16 @@ func (s *TestSuite) SetupTest() {
slashingKeeper := estestutil.NewMockSlashingKeeper(ctrl)
s.SlashingKeeper = slashingKeeper
epochsKeeper := estestutil.NewMockEpochsKeeper(ctrl)
secondEpochInfo := epochstypes.EpochInfo{
Identifier: "second",
StartTime: time.Time{},
Duration: time.Second,
CurrentEpoch: 1, // to update validator set
CurrentEpochStartHeight: s.Ctx.BlockHeight(),
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: true,
}
epochsKeeper.EXPECT().GetEpochInfo(gomock.Any(), gomock.Any()).Return(secondEpochInfo, nil).AnyTimes()

// staking keeper
stakingKeeper := skeeper.NewKeeper(
Expand Down Expand Up @@ -141,7 +156,8 @@ func (s *TestSuite) SetupTest() {
ethCl,
address.NewBech32Codec("storyvaloper"),
)
s.Require().NoError(evmstakingKeeper.SetParams(s.Ctx, types.DefaultParams()))
s.Require().NoError(evmstakingKeeper.SetParams(s.Ctx, TestEVMStakingParams))
s.Require().NoError(evmstakingKeeper.SetEpochNumber(s.Ctx, 0))
s.EVMStakingKeeper = evmstakingKeeper
s.msgServer = keeper.NewMsgServerImpl(evmstakingKeeper)
}
Expand Down Expand Up @@ -617,12 +633,22 @@ func (s *TestSuite) TestProcessStakingEvents() {
if tc.setup != nil {
tc.setup(cachedCtx)
}
evmLogs, err := tc.evmEvents()
var err error
var evmLogs []*evmenginetypes.EVMEvent
evmLogs, err = tc.evmEvents()
require.NoError(err)
if tc.stateCheck != nil {
tc.stateCheck(cachedCtx)
}
err = evmstakingKeeper.ProcessStakingEvents(cachedCtx, 1, evmLogs)
if !evmstakingKeeper.MessageQueue.IsEmpty(cachedCtx) {
var queuedMsgs []*types.QueuedMessage
queuedMsgs, err = evmstakingKeeper.DequeueAllMsgs(cachedCtx)

for _, msg := range queuedMsgs {
err = evmstakingKeeper.ProcessMsg(cachedCtx, msg)
}
}
if tc.expectedError != "" {
require.Error(err)
require.Contains(err.Error(), tc.expectedError)
Expand Down
2 changes: 1 addition & 1 deletion client/x/evmstaking/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func (s *TestSuite) TestGetParams() {
ctx, keeper := s.Ctx, s.EVMStakingKeeper
params, err := keeper.GetParams(ctx)
require.NoError(err)
require.Equal(types.DefaultParams(), params)
require.Equal(TestEVMStakingParams, params)
}

func (s *TestSuite) TestSetParams() {
Expand Down
11 changes: 10 additions & 1 deletion client/x/evmstaking/keeper/redelegate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,16 @@ func (s *TestSuite) TestRedelegation() {
s.Run(tc.name, func() {
cachedCtx, _ := ctx.CacheContext()
input := tc.input()
err := keeper.HandleRedelegateEvent(cachedCtx, &input)
var err error
err = keeper.HandleRedelegateEvent(cachedCtx, &input)
if !keeper.MessageQueue.IsEmpty(cachedCtx) {
var queuedMsgs []*types.QueuedMessage
queuedMsgs, err = keeper.DequeueAllMsgs(cachedCtx)

for _, msg := range queuedMsgs {
err = keeper.ProcessMsg(cachedCtx, msg)
}
}
if tc.expectedError != "" {
require.ErrorContains(err, tc.expectedError)
} else {
Expand Down
11 changes: 10 additions & 1 deletion client/x/evmstaking/keeper/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ func (s *TestSuite) TestProcessCreateValidator() {
if moniker == "" {
moniker = "testing"
}
err := keeper.HandleCreateValidatorEvent(cachedCtx, &bindings.IPTokenStakingCreateValidator{
var err error
err = keeper.HandleCreateValidatorEvent(cachedCtx, &bindings.IPTokenStakingCreateValidator{
ValidatorUncmpPubkey: nil,
ValidatorCmpPubkey: tc.valPubKeyBytes,
Moniker: moniker,
Expand All @@ -221,6 +222,14 @@ func (s *TestSuite) TestProcessCreateValidator() {
MaxCommissionChangeRate: 500, // 5%
Raw: gethtypes.Log{},
})
if !keeper.MessageQueue.IsEmpty(cachedCtx) {
var queuedMsgs []*types.QueuedMessage
queuedMsgs, err = keeper.DequeueAllMsgs(cachedCtx)

for _, msg := range queuedMsgs {
err = keeper.ProcessMsg(cachedCtx, msg)
}
}
if tc.expectedError != "" {
require.Error(err, tc.expectedError)
} else {
Expand Down
11 changes: 10 additions & 1 deletion client/x/evmstaking/keeper/withdraw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,19 @@ func (s *TestSuite) TestProcessWithdraw() {
}
cachedCtx, _ := ctx.CacheContext()
// check undelegation does not exist
_, err := s.StakingKeeper.GetUnbondingDelegation(cachedCtx, delAddr1, valAddr)
var err error
_, err = s.StakingKeeper.GetUnbondingDelegation(cachedCtx, delAddr1, valAddr)
require.ErrorContains(err, "no unbonding delegation found")

err = keeper.HandleWithdrawEvent(cachedCtx, tc.withdraw)
if !keeper.MessageQueue.IsEmpty(cachedCtx) {
var queuedMsgs []*types.QueuedMessage
queuedMsgs, err = keeper.DequeueAllMsgs(cachedCtx)

for _, msg := range queuedMsgs {
err = keeper.ProcessMsg(cachedCtx, msg)
}
}
if tc.expectedErr != "" {
require.ErrorContains(err, tc.expectedErr)
} else {
Expand Down

0 comments on commit 4e25327

Please sign in to comment.