refactor!: deprecate blocktime on context (#17738)

This commit is contained in:
Marko
2023-09-18 13:55:21 +00:00
committed by GitHub
parent 8df065b611
commit 6615ff4f76
89 changed files with 379 additions and 369 deletions
+5 -5
View File
@@ -385,7 +385,7 @@ func (k Keeper) InsertUBDQueue(ctx context.Context, ubd types.UnbondingDelegatio
// DequeueAllMatureUBDQueue returns a concatenated list of all the timeslices inclusively previous to
// currTime, and deletes the timeslices from the queue.
func (k Keeper) DequeueAllMatureUBDQueue(ctx context.Context, currTime time.Time) (matureUnbonds []types.DVPair, err error) {
// get an iterator for all timeslices from time 0 until the current Blockheader time
// get an iterator for all timeslices from time 0 until the current HeaderInfo time
iter, err := k.UnbondingQueue.Iterate(ctx, (&collections.Range[time.Time]{}).EndInclusive(currTime))
if err != nil {
return matureUnbonds, err
@@ -894,7 +894,7 @@ func (k Keeper) getBeginInfo(
switch {
case errors.Is(err, types.ErrNoValidatorFound) || validator.IsBonded():
// the longest wait - just unbonding period from now
completionTime = sdkCtx.BlockHeader().Time.Add(unbondingTime)
completionTime = sdkCtx.HeaderInfo().Time.Add(unbondingTime)
height = sdkCtx.BlockHeight()
return completionTime, height, false, nil
@@ -951,7 +951,7 @@ func (k Keeper) Undelegate(
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
completionTime := sdkCtx.BlockHeader().Time.Add(unbondingTime)
completionTime := sdkCtx.HeaderInfo().Time.Add(unbondingTime)
ubd, err := k.SetUnbondingDelegationEntry(ctx, delAddr, valAddr, sdkCtx.BlockHeight(), completionTime, returnAmount)
if err != nil {
return time.Time{}, math.Int{}, err
@@ -981,7 +981,7 @@ func (k Keeper) CompleteUnbonding(ctx context.Context, delAddr sdk.AccAddress, v
balances := sdk.NewCoins()
sdkCtx := sdk.UnwrapSDKContext(ctx)
ctxTime := sdkCtx.BlockHeader().Time
ctxTime := sdkCtx.HeaderInfo().Time
delegatorAddress, err := k.authKeeper.AddressCodec().StringToBytes(ubd.DelegatorAddress)
if err != nil {
@@ -1126,7 +1126,7 @@ func (k Keeper) CompleteRedelegation(
balances := sdk.NewCoins()
sdkCtx := sdk.UnwrapSDKContext(ctx)
ctxTime := sdkCtx.BlockHeader().Time
ctxTime := sdkCtx.HeaderInfo().Time
// loop through all the entries and complete mature redelegation entries
for i := 0; i < len(red.Entries); i++ {
+17 -16
View File
@@ -6,6 +6,7 @@ import (
"github.com/golang/mock/gomock"
"cosmossdk.io/collections"
coreheader "cosmossdk.io/core/header"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec/address"
@@ -478,12 +479,12 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondingValidator() {
delegation := stakingtypes.NewDelegation(addrDels[1].String(), addrVals[0].String(), issuedShares)
require.NoError(keeper.SetDelegation(ctx, delegation))
header := ctx.BlockHeader()
header := ctx.HeaderInfo()
blockHeight := int64(10)
header.Height = blockHeight
blockTime := time.Unix(333, 0)
header.Time = blockTime
ctx = ctx.WithBlockHeader(header)
ctx = ctx.WithHeaderInfo(header)
// unbond the all self-delegation to put validator in unbonding state
val0AccAddr := sdk.AccAddress(addrVals[0])
@@ -506,7 +507,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondingValidator() {
blockHeight2 := int64(20)
blockTime2 := time.Unix(444, 0).UTC()
ctx = ctx.WithBlockHeight(blockHeight2)
ctx = ctx.WithBlockTime(blockTime2)
ctx = ctx.WithHeaderInfo(coreheader.Info{Height: blockHeight2, Time: blockTime2})
// unbond some of the other delegation's shares
undelegateAmount := math.LegacyNewDec(6)
@@ -555,7 +556,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondedValidator() {
require.NoError(keeper.SetDelegation(ctx, delegation))
ctx = ctx.WithBlockHeight(10)
ctx = ctx.WithBlockTime(time.Unix(333, 0))
ctx = ctx.WithHeaderInfo(coreheader.Info{Height: 10, Time: time.Unix(333, 0)})
// unbond the all self-delegation to put validator in unbonding state
s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any())
@@ -572,10 +573,10 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondedValidator() {
require.Equal(ctx.BlockHeight(), validator.UnbondingHeight)
params, err := keeper.GetParams(ctx)
require.NoError(err)
require.True(ctx.BlockHeader().Time.Add(params.UnbondingTime).Equal(validator.UnbondingTime))
require.True(ctx.HeaderInfo().Time.Add(params.UnbondingTime).Equal(validator.UnbondingTime))
// unbond the validator
ctx = ctx.WithBlockTime(validator.UnbondingTime)
ctx = ctx.WithHeaderInfo(coreheader.Info{Time: validator.UnbondingTime})
err = keeper.UnbondAllMatureValidators(ctx)
require.NoError(err)
@@ -636,7 +637,7 @@ func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() {
require.NoError(keeper.SetDelegation(ctx, delegation))
ctx = ctx.WithBlockHeight(10)
ctx = ctx.WithBlockTime(time.Unix(333, 0))
ctx = ctx.WithHeaderInfo(coreheader.Info{Height: 10, Time: time.Unix(333, 0)})
// unbond the all self-delegation to put validator in unbonding state
s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any())
@@ -659,7 +660,7 @@ func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() {
require.Equal(validator.Status, stakingtypes.Unbonding)
// unbond the validator
ctx = ctx.WithBlockTime(validator.UnbondingTime)
ctx = ctx.WithHeaderInfo(coreheader.Info{Time: validator.UnbondingTime})
err = keeper.UnbondAllMatureValidators(ctx)
require.NoError(err)
@@ -842,7 +843,7 @@ func (s *KeeperTestSuite) TestRedelegationMaxEntries() {
require.Error(err)
// mature redelegations
ctx = ctx.WithBlockTime(completionTime)
ctx = ctx.WithHeaderInfo(coreheader.Info{Time: completionTime})
_, err = keeper.CompleteRedelegation(ctx, val0AccAddr, addrVals[0], addrVals[1])
require.NoError(err)
@@ -937,12 +938,12 @@ func (s *KeeperTestSuite) TestRedelegateFromUnbondingValidator() {
s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.NotBondedPoolName, stakingtypes.BondedPoolName, gomock.Any())
_ = stakingkeeper.TestingUpdateValidator(keeper, ctx, validator2, true)
header := ctx.BlockHeader()
header := ctx.HeaderInfo()
blockHeight := int64(10)
header.Height = blockHeight
blockTime := time.Unix(333, 0)
header.Time = blockTime
ctx = ctx.WithBlockHeader(header)
ctx = ctx.WithHeaderInfo(header)
// unbond the all self-delegation to put validator in unbonding state
s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any())
@@ -962,12 +963,12 @@ func (s *KeeperTestSuite) TestRedelegateFromUnbondingValidator() {
require.True(blockTime.Add(params.UnbondingTime).Equal(validator.UnbondingTime))
// change the context
header = ctx.BlockHeader()
header = ctx.HeaderInfo()
blockHeight2 := int64(20)
header.Height = blockHeight2
blockTime2 := time.Unix(444, 0)
header.Time = blockTime2
ctx = ctx.WithBlockHeader(header)
ctx = ctx.WithHeaderInfo(header)
// unbond some of the other delegation's shares
redelegateTokens := keeper.TokensFromConsensusPower(ctx, 6)
@@ -1020,7 +1021,7 @@ func (s *KeeperTestSuite) TestRedelegateFromUnbondedValidator() {
require.Equal(stakingtypes.Bonded, validator2.Status)
ctx = ctx.WithBlockHeight(10)
ctx = ctx.WithBlockTime(time.Unix(333, 0))
ctx = ctx.WithHeaderInfo(coreheader.Info{Height: 10, Time: time.Unix(333, 0)})
// unbond the all self-delegation to put validator in unbonding state
s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any())
@@ -1034,10 +1035,10 @@ func (s *KeeperTestSuite) TestRedelegateFromUnbondedValidator() {
validator, err = keeper.GetValidator(ctx, addrVals[0])
require.NoError(err)
require.Equal(ctx.BlockHeight(), validator.UnbondingHeight)
require.Equal(ctx.HeaderInfo().Height, validator.UnbondingHeight)
params, err := keeper.GetParams(ctx)
require.NoError(err)
require.True(ctx.BlockHeader().Time.Add(params.UnbondingTime).Equal(validator.UnbondingTime))
require.True(ctx.HeaderInfo().Time.Add(params.UnbondingTime).Equal(validator.UnbondingTime))
// unbond the validator
_, err = keeper.UnbondingToUnbonded(ctx, validator)
+2 -1
View File
@@ -4,6 +4,7 @@ import (
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"cosmossdk.io/collections"
coreheader "cosmossdk.io/core/header"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/x/staking/testutil"
@@ -108,7 +109,7 @@ func (s *KeeperTestSuite) TestTrackHistoricalInfo() {
ChainID: "HelloChain",
Height: 10,
}
ctx = ctx.WithBlockHeader(header)
ctx = ctx.WithBlockHeader(header).WithHeaderInfo(coreheader.Info{ChainID: header.ChainID, Height: header.Height})
require.NoError(keeper.TrackHistoricalInfo(ctx))
+2 -3
View File
@@ -4,13 +4,12 @@ import (
"testing"
"time"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
cmttime "github.com/cometbft/cometbft/types/time"
gogotypes "github.com/cosmos/gogoproto/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
"cosmossdk.io/collections"
"cosmossdk.io/core/header"
"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
@@ -56,7 +55,7 @@ func (s *KeeperTestSuite) SetupTest() {
storeService := runtime.NewKVStoreService(key)
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
s.key = key
ctx := testCtx.Ctx.WithBlockHeader(cmtproto.Header{Time: cmttime.Now()})
ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Time: time.Now()})
encCfg := moduletestutil.MakeTestEncodingConfig()
s.cdc = encCfg.Codec
+2 -2
View File
@@ -106,7 +106,7 @@ func (k msgServer) CreateValidator(ctx context.Context, msg *types.MsgCreateVali
commission := types.NewCommissionWithTime(
msg.Commission.Rate, msg.Commission.MaxRate,
msg.Commission.MaxChangeRate, sdkCtx.BlockHeader().Time,
msg.Commission.MaxChangeRate, sdkCtx.HeaderInfo().Time,
)
validator, err = validator.SetInitialCommission(commission)
@@ -541,7 +541,7 @@ func (k msgServer) CancelUnbondingDelegation(ctx context.Context, msg *types.Msg
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
if unbondEntry.CompletionTime.Before(sdkCtx.BlockTime()) {
if unbondEntry.CompletionTime.Before(sdkCtx.HeaderInfo().Time) {
return nil, sdkerrors.ErrInvalidRequest.Wrap("unbonding delegation is already processed")
}
+3 -3
View File
@@ -7,6 +7,7 @@ import (
"github.com/golang/mock/gomock"
"cosmossdk.io/collections"
"cosmossdk.io/core/header"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec/address"
@@ -247,8 +248,7 @@ func (s *KeeperTestSuite) TestMsgEditValidator() {
s.execExpectCalls()
// create new context with updated block time
newCtx := ctx.WithBlockTime(ctx.BlockTime().AddDate(0, 0, 1))
newCtx := ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.AddDate(0, 0, 1)})
pk := ed25519.GenPrivKey().PubKey()
require.NotNil(pk)
@@ -852,7 +852,7 @@ func (s *KeeperTestSuite) TestMsgCancelUnbondingDelegation() {
require.NoError(err)
require.Equal(del, resDel)
ubd := stakingtypes.NewUnbondingDelegation(Addr, ValAddr, 10, ctx.BlockTime().Add(time.Minute*10), shares.RoundInt(), 0, keeper.ValidatorAddressCodec(), ak.AddressCodec())
ubd := stakingtypes.NewUnbondingDelegation(Addr, ValAddr, 10, ctx.HeaderInfo().Time.Add(time.Minute*10), shares.RoundInt(), 0, keeper.ValidatorAddressCodec(), ak.AddressCodec())
require.NoError(keeper.SetUnbondingDelegation(ctx, ubd))
resUnbond, err := keeper.GetUnbondingDelegation(ctx, Addr, ValAddr)
require.NoError(err)
+2 -2
View File
@@ -226,7 +226,7 @@ func (k Keeper) SlashUnbondingDelegation(ctx context.Context, unbondingDelegatio
infractionHeight int64, slashFactor math.LegacyDec,
) (totalSlashAmount math.Int, err error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
now := sdkCtx.BlockHeader().Time
now := sdkCtx.HeaderInfo().Time
totalSlashAmount = math.ZeroInt()
burnedAmount := math.ZeroInt()
@@ -283,7 +283,7 @@ func (k Keeper) SlashRedelegation(ctx context.Context, srcValidator types.Valida
infractionHeight int64, slashFactor math.LegacyDec,
) (totalSlashAmount math.Int, err error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
now := sdkCtx.BlockHeader().Time
now := sdkCtx.HeaderInfo().Time
totalSlashAmount = math.ZeroInt()
bondedBurnedAmount, notBondedBurnedAmount := math.ZeroInt(), math.ZeroInt()
+2 -2
View File
@@ -284,7 +284,7 @@ func (k Keeper) unbondingDelegationEntryCanComplete(ctx context.Context, id uint
sdkCtx := sdk.UnwrapSDKContext(ctx)
// Check if entry is matured.
if !ubd.Entries[i].OnHold() && ubd.Entries[i].IsMature(sdkCtx.BlockHeader().Time) {
if !ubd.Entries[i].OnHold() && ubd.Entries[i].IsMature(sdkCtx.HeaderInfo().Time) {
// If matured, complete it.
delegatorAddress, err := k.authKeeper.AddressCodec().StringToBytes(ubd.DelegatorAddress)
if err != nil {
@@ -346,7 +346,7 @@ func (k Keeper) redelegationEntryCanComplete(ctx context.Context, id uint64) err
red.Entries[i].UnbondingOnHoldRefCount--
sdkCtx := sdk.UnwrapSDKContext(ctx)
if !red.Entries[i].OnHold() && red.Entries[i].IsMature(sdkCtx.BlockHeader().Time) {
if !red.Entries[i].OnHold() && red.Entries[i].IsMature(sdkCtx.HeaderInfo().Time) {
// If matured, complete it.
// Remove entry
red.RemoveEntry(int64(i))
+4 -4
View File
@@ -41,7 +41,7 @@ func (k Keeper) BlockValidatorUpdates(ctx context.Context) ([]abci.ValidatorUpda
sdkCtx := sdk.UnwrapSDKContext(ctx)
// Remove all mature unbonding delegations from the ubd queue.
matureUnbonds, err := k.DequeueAllMatureUBDQueue(ctx, sdkCtx.BlockHeader().Time)
matureUnbonds, err := k.DequeueAllMatureUBDQueue(ctx, sdkCtx.HeaderInfo().Time)
if err != nil {
return nil, err
}
@@ -72,7 +72,7 @@ func (k Keeper) BlockValidatorUpdates(ctx context.Context) ([]abci.ValidatorUpda
}
// Remove all mature redelegations from the red queue.
matureRedelegations, err := k.DequeueAllMatureRedelegationQueue(ctx, sdkCtx.BlockHeader().Time)
matureRedelegations, err := k.DequeueAllMatureRedelegationQueue(ctx, sdkCtx.HeaderInfo().Time)
if err != nil {
return nil, err
}
@@ -400,8 +400,8 @@ func (k Keeper) BeginUnbondingValidator(ctx context.Context, validator types.Val
sdkCtx := sdk.UnwrapSDKContext(ctx)
// set the unbonding completion time and completion height appropriately
validator.UnbondingTime = sdkCtx.BlockHeader().Time.Add(params.UnbondingTime)
validator.UnbondingHeight = sdkCtx.BlockHeader().Height
validator.UnbondingTime = sdkCtx.HeaderInfo().Time.Add(params.UnbondingTime)
validator.UnbondingHeight = sdkCtx.HeaderInfo().Height
validator.UnbondingIds = append(validator.UnbondingIds, id)
+2 -2
View File
@@ -185,7 +185,7 @@ func (k Keeper) UpdateValidatorCommission(ctx context.Context,
) (types.Commission, error) {
commission := validator.Commission
sdkCtx := sdk.UnwrapSDKContext(ctx)
blockTime := sdkCtx.BlockHeader().Time
blockTime := sdkCtx.HeaderInfo().Time
if err := commission.ValidateNewRate(newRate, blockTime); err != nil {
return commission, err
@@ -476,7 +476,7 @@ func (k Keeper) DeleteValidatorQueue(ctx context.Context, val types.Validator) e
// have finished their unbonding period.
func (k Keeper) UnbondAllMatureValidators(ctx context.Context) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
blockTime := sdkCtx.BlockTime()
blockTime := sdkCtx.HeaderInfo().Time
blockHeight := uint64(sdkCtx.BlockHeight())
rng := new(collections.Range[collections.Triple[uint64, time.Time, uint64]]).
+4 -3
View File
@@ -7,6 +7,7 @@ import (
"github.com/golang/mock/gomock"
"cosmossdk.io/collections"
"cosmossdk.io/core/header"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -342,7 +343,7 @@ func (s *KeeperTestSuite) TestUpdateValidatorCommission() {
require.Equal(tc.newRate, val.Commission.Rate,
"expected new validator commission rate for test case #%d with rate: %s", i, tc.newRate,
)
require.Equal(ctx.BlockHeader().Time, val.Commission.UpdateTime,
require.Equal(ctx.HeaderInfo().Time, val.Commission.UpdateTime,
"expected new validator commission update time for test case #%d with rate: %s", i, tc.newRate,
)
}
@@ -415,12 +416,12 @@ func (s *KeeperTestSuite) TestUnbondingValidator() {
require.Equal(valAddr.String(), resVals[0])
// check unbonding mature validators
ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime)
ctx = ctx.WithBlockHeight(endHeight).WithHeaderInfo(header.Info{Time: endTime})
err = keeper.UnbondAllMatureValidators(ctx)
require.EqualError(err, "validator in the unbonding queue was not found: validator does not exist")
require.NoError(keeper.SetValidator(ctx, validator))
ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime)
ctx = ctx.WithBlockHeight(endHeight).WithHeaderInfo(header.Info{Time: endTime})
err = keeper.UnbondAllMatureValidators(ctx)
require.EqualError(err, "unexpected validator in unbonding queue; status was not unbonding")
+2 -2
View File
@@ -221,7 +221,7 @@ func SimulateMsgEditValidator(
address := val.GetOperator()
newCommissionRate := simtypes.RandomDecAmount(r, val.Commission.MaxRate)
if err := val.Commission.ValidateNewRate(newCommissionRate, ctx.BlockHeader().Time); err != nil {
if err := val.Commission.ValidateNewRate(newCommissionRate, ctx.HeaderInfo().Time); err != nil {
// skip as the commission is invalid
return simtypes.NoOpMsg(types.ModuleName, msgType, "invalid commission rate"), nil, nil
}
@@ -517,7 +517,7 @@ func SimulateMsgCancelUnbondingDelegate(
}
}
if unbondingDelegationEntry.CompletionTime.Before(ctx.BlockTime()) {
if unbondingDelegationEntry.CompletionTime.Before(ctx.HeaderInfo().Time) {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unbonding delegation is already processed"), nil, nil
}
+3 -2
View File
@@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"
"cosmossdk.io/core/header"
"cosmossdk.io/math"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
@@ -121,7 +122,7 @@ func (sh *Helper) CheckValidator(addr sdk.ValAddress, status stakingtypes.BondSt
// TurnBlock calls EndBlocker and updates the block time
func (sh *Helper) TurnBlock(newTime time.Time) sdk.Context {
sh.Ctx = sh.Ctx.WithBlockTime(newTime)
sh.Ctx = sh.Ctx.WithHeaderInfo(header.Info{Time: newTime})
_, err := sh.k.EndBlocker(sh.Ctx)
require.NoError(sh.t, err)
return sh.Ctx
@@ -130,7 +131,7 @@ func (sh *Helper) TurnBlock(newTime time.Time) sdk.Context {
// TurnBlockTimeDiff calls EndBlocker and updates the block time by adding the
// duration to the current block time
func (sh *Helper) TurnBlockTimeDiff(diff time.Duration) sdk.Context {
sh.Ctx = sh.Ctx.WithBlockTime(sh.Ctx.BlockHeader().Time.Add(diff))
sh.Ctx = sh.Ctx.WithHeaderInfo(header.Info{Time: sh.Ctx.HeaderInfo().Time.Add(diff)})
_, err := sh.k.EndBlocker(sh.Ctx)
require.NoError(sh.t, err)
return sh.Ctx