refactor!: deprecate blocktime on context (#17738)
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
// InitGenesis initializes new authz genesis
|
||||
func (k Keeper) InitGenesis(ctx sdk.Context, data *authz.GenesisState) {
|
||||
now := ctx.BlockTime()
|
||||
now := ctx.HeaderInfo().Time
|
||||
for _, entry := range data.Authorization {
|
||||
// ignore expired authorizations
|
||||
if entry.Expiration != nil && entry.Expiration.Before(now) {
|
||||
|
||||
@@ -73,7 +73,7 @@ func (suite *GenesisTestSuite) SetupTest() {
|
||||
func (suite *GenesisTestSuite) TestImportExportGenesis() {
|
||||
coins := sdk.NewCoins(sdk.NewCoin("foo", sdkmath.NewInt(1_000)))
|
||||
|
||||
now := suite.ctx.BlockTime()
|
||||
now := suite.ctx.HeaderInfo().Time
|
||||
expires := now.Add(time.Hour)
|
||||
grant := &bank.SendAuthorization{SpendLimit: coins}
|
||||
err := suite.keeper.SaveGrant(suite.ctx, granteeAddr, granterAddr, grant, &expires)
|
||||
|
||||
@@ -277,7 +277,7 @@ func (suite *TestSuite) TestGRPCQueryGranteeGrants() {
|
||||
}
|
||||
|
||||
func (suite *TestSuite) createSendAuthorization(grantee, granter sdk.AccAddress) authz.Authorization {
|
||||
exp := suite.ctx.BlockHeader().Time.Add(time.Hour)
|
||||
exp := suite.ctx.HeaderInfo().Time.Add(time.Hour)
|
||||
newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100))
|
||||
authorization := &banktypes.SendAuthorization{SpendLimit: newCoins}
|
||||
err := suite.authzKeeper.SaveGrant(suite.ctx, grantee, granter, authorization, &exp)
|
||||
@@ -286,7 +286,7 @@ func (suite *TestSuite) createSendAuthorization(grantee, granter sdk.AccAddress)
|
||||
}
|
||||
|
||||
func (suite *TestSuite) createSendAuthorizationWithAllowList(grantee, granter sdk.AccAddress) authz.Authorization {
|
||||
exp := suite.ctx.BlockHeader().Time.Add(time.Hour)
|
||||
exp := suite.ctx.HeaderInfo().Time.Add(time.Hour)
|
||||
newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100))
|
||||
authorization := &banktypes.SendAuthorization{SpendLimit: newCoins, AllowList: []string{suite.addrs[5].String()}}
|
||||
err := suite.authzKeeper.SaveGrant(suite.ctx, grantee, granter, authorization, &exp)
|
||||
|
||||
@@ -95,7 +95,7 @@ func (k Keeper) update(ctx context.Context, grantee, granter sdk.AccAddress, upd
|
||||
func (k Keeper) DispatchActions(ctx context.Context, grantee sdk.AccAddress, msgs []sdk.Msg) ([][]byte, error) {
|
||||
results := make([][]byte, len(msgs))
|
||||
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
||||
now := sdkCtx.BlockTime()
|
||||
now := sdkCtx.HeaderInfo().Time
|
||||
|
||||
for i, msg := range msgs {
|
||||
signers, _, err := k.cdc.GetMsgV1Signers(msg)
|
||||
@@ -189,7 +189,7 @@ func (k Keeper) SaveGrant(ctx context.Context, grantee, granter sdk.AccAddress,
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
skey := grantStoreKey(grantee, granter, msgType)
|
||||
|
||||
grant, err := authz.NewGrant(sdkCtx.BlockTime(), authorization, expiration)
|
||||
grant, err := authz.NewGrant(sdkCtx.HeaderInfo().Time, authorization, expiration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -292,7 +292,7 @@ func (k Keeper) GetAuthorizations(ctx context.Context, grantee, granter sdk.AccA
|
||||
func (k Keeper) GetAuthorization(ctx context.Context, grantee, granter sdk.AccAddress, msgType string) (authz.Authorization, *time.Time) {
|
||||
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
||||
grant, found := k.getGrant(ctx, grantStoreKey(grantee, granter, msgType))
|
||||
if !found || (grant.Expiration != nil && grant.Expiration.Before(sdkCtx.BlockHeader().Time)) {
|
||||
if !found || (grant.Expiration != nil && grant.Expiration.Before(sdkCtx.HeaderInfo().Time)) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -411,7 +411,7 @@ func (k Keeper) DequeueAndDeleteExpiredGrants(ctx context.Context) error {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
||||
|
||||
iterator, err := store.Iterator(GrantQueuePrefix, storetypes.InclusiveEndBytes(GrantQueueTimePrefix(sdkCtx.BlockTime())))
|
||||
iterator, err := store.Iterator(GrantQueuePrefix, storetypes.InclusiveEndBytes(GrantQueueTimePrefix(sdkCtx.HeaderInfo().Time)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,11 +4,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
||||
cmttime "github.com/cometbft/cometbft/types/time"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"cosmossdk.io/core/header"
|
||||
"cosmossdk.io/log"
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
@@ -51,7 +50,7 @@ func (s *TestSuite) SetupTest() {
|
||||
key := storetypes.NewKVStoreKey(authzkeeper.StoreKey)
|
||||
storeService := runtime.NewKVStoreService(key)
|
||||
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
|
||||
s.ctx = testCtx.Ctx.WithBlockHeader(cmtproto.Header{Time: cmttime.Now()})
|
||||
s.ctx = testCtx.Ctx.WithHeaderInfo(header.Info{Time: time.Now().Round(0).UTC()})
|
||||
s.encCfg = moduletestutil.MakeTestEncodingConfig(authzmodule.AppModuleBasic{})
|
||||
|
||||
s.baseApp = baseapp.NewBaseApp(
|
||||
@@ -87,7 +86,7 @@ func (s *TestSuite) SetupTest() {
|
||||
|
||||
func (s *TestSuite) TestKeeper() {
|
||||
ctx, addrs := s.ctx, s.addrs
|
||||
now := ctx.BlockTime()
|
||||
now := ctx.HeaderInfo().Time
|
||||
require := s.Require()
|
||||
|
||||
granterAddr := addrs[0]
|
||||
@@ -145,7 +144,7 @@ func (s *TestSuite) TestKeeperIter() {
|
||||
granterAddr := addrs[0]
|
||||
granteeAddr := addrs[1]
|
||||
granter2Addr := addrs[2]
|
||||
e := ctx.BlockTime().AddDate(1, 0, 0)
|
||||
e := ctx.HeaderInfo().Time.AddDate(1, 0, 0)
|
||||
sendAuthz := banktypes.NewSendAuthorization(coins100, nil)
|
||||
|
||||
err := s.authzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, sendAuthz, &e)
|
||||
@@ -164,7 +163,7 @@ func (s *TestSuite) TestKeeperIter() {
|
||||
func (s *TestSuite) TestDispatchAction() {
|
||||
addrs := s.addrs
|
||||
require := s.Require()
|
||||
now := s.ctx.BlockTime()
|
||||
now := s.ctx.HeaderInfo().Time
|
||||
|
||||
granterAddr := addrs[0]
|
||||
granteeAddr := addrs[1]
|
||||
@@ -213,7 +212,7 @@ func (s *TestSuite) TestDispatchAction() {
|
||||
e := now.AddDate(0, 0, 1)
|
||||
err := s.authzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, a, &e)
|
||||
require.NoError(err)
|
||||
return s.ctx.WithBlockTime(s.ctx.BlockTime().AddDate(0, 0, 2))
|
||||
return s.ctx.WithHeaderInfo(header.Info{Time: s.ctx.HeaderInfo().Time.AddDate(0, 0, 2)})
|
||||
},
|
||||
func() {},
|
||||
},
|
||||
@@ -314,7 +313,7 @@ func (s *TestSuite) TestDispatchedEvents() {
|
||||
granterAddr := addrs[0]
|
||||
granteeAddr := addrs[1]
|
||||
recipientAddr := addrs[2]
|
||||
expiration := s.ctx.BlockTime().Add(1 * time.Second) // must be in the future
|
||||
expiration := s.ctx.HeaderInfo().Time.Add(1 * time.Second) // must be in the future
|
||||
|
||||
msgs := authz.NewMsgExec(granteeAddr, []sdk.Msg{
|
||||
&banktypes.MsgSend{
|
||||
@@ -363,7 +362,7 @@ func (s *TestSuite) TestDequeueAllGrantsQueue() {
|
||||
granter := addrs[0]
|
||||
grantee := addrs[1]
|
||||
grantee1 := addrs[2]
|
||||
exp := s.ctx.BlockTime().AddDate(0, 0, 1)
|
||||
exp := s.ctx.HeaderInfo().Time.AddDate(0, 0, 1)
|
||||
a := banktypes.SendAuthorization{SpendLimit: coins100}
|
||||
|
||||
// create few authorizations
|
||||
@@ -381,7 +380,7 @@ func (s *TestSuite) TestDequeueAllGrantsQueue() {
|
||||
err = s.authzKeeper.SaveGrant(s.ctx, granter, grantee, &a, &exp2)
|
||||
require.NoError(err)
|
||||
|
||||
newCtx := s.ctx.WithBlockTime(exp.AddDate(1, 0, 0))
|
||||
newCtx := s.ctx.WithHeaderInfo(header.Info{Time: exp.AddDate(1, 0, 0)})
|
||||
err = s.authzKeeper.DequeueAndDeleteExpiredGrants(newCtx)
|
||||
require.NoError(err)
|
||||
|
||||
@@ -413,7 +412,7 @@ func (s *TestSuite) TestGetAuthorization() {
|
||||
genAuthSend := authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgSend{}))
|
||||
sendAuth := banktypes.NewSendAuthorization(coins10, nil)
|
||||
|
||||
start := s.ctx.BlockHeader().Time
|
||||
start := s.ctx.HeaderInfo().Time
|
||||
expired := start.Add(time.Duration(1) * time.Second)
|
||||
notExpired := start.Add(time.Duration(5) * time.Hour)
|
||||
|
||||
@@ -421,7 +420,7 @@ func (s *TestSuite) TestGetAuthorization() {
|
||||
s.Require().NoError(s.authzKeeper.SaveGrant(s.ctx, addr1, addr3, genAuthSend, &expired), "creating grant 1->3")
|
||||
s.Require().NoError(s.authzKeeper.SaveGrant(s.ctx, addr1, addr4, sendAuth, ¬Expired), "creating grant 1->4")
|
||||
// Without access to private keeper methods, I don't know how to save a grant with an invalid authorization.
|
||||
newCtx := s.ctx.WithBlockTime(start.Add(time.Duration(1) * time.Minute))
|
||||
newCtx := s.ctx.WithHeaderInfo(header.Info{Time: start.Add(time.Duration(1) * time.Minute)})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -490,7 +489,7 @@ func (s *TestSuite) TestGetAuthorizations() {
|
||||
genAuthMulti := authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgMultiSend{}))
|
||||
genAuthSend := authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgSend{}))
|
||||
|
||||
start := s.ctx.BlockHeader().Time
|
||||
start := s.ctx.HeaderInfo().Time
|
||||
expired := start.Add(time.Duration(1) * time.Second)
|
||||
|
||||
s.Require().NoError(s.authzKeeper.SaveGrant(s.ctx, addr1, addr2, genAuthMulti, &expired), "creating multi send grant 1->2")
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
|
||||
"cosmossdk.io/core/header"
|
||||
sdkmath "cosmossdk.io/math"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec/address"
|
||||
@@ -23,9 +24,9 @@ func (suite *TestSuite) createAccounts(accs int) []sdk.AccAddress {
|
||||
}
|
||||
|
||||
func (suite *TestSuite) TestGrant() {
|
||||
ctx := suite.ctx.WithBlockTime(time.Now())
|
||||
ctx := suite.ctx.WithHeaderInfo(header.Info{Time: time.Now()})
|
||||
addrs := suite.createAccounts(2)
|
||||
curBlockTime := ctx.BlockTime()
|
||||
curBlockTime := ctx.HeaderInfo().Time
|
||||
|
||||
suite.accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ func addExpiredGrantsIndex(ctx sdk.Context, store storetypes.KVStore, cdc codec.
|
||||
defer grantsIter.Close()
|
||||
|
||||
queueItems := make(map[string][]string)
|
||||
now := ctx.BlockTime()
|
||||
now := ctx.HeaderInfo().Time
|
||||
for ; grantsIter.Valid(); grantsIter.Next() {
|
||||
var grant authz.Grant
|
||||
bz := grantsIter.Value()
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/core/header"
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
@@ -36,7 +37,7 @@ func TestMigration(t *testing.T) {
|
||||
sendMsgType := banktypes.SendAuthorization{}.MsgTypeURL()
|
||||
genericMsgType := sdk.MsgTypeURL(&govtypes.MsgVote{})
|
||||
coins100 := sdk.NewCoins(sdk.NewInt64Coin("atom", 100))
|
||||
blockTime := ctx.BlockTime()
|
||||
blockTime := ctx.HeaderInfo().Time
|
||||
oneDay := blockTime.AddDate(0, 0, 1)
|
||||
oneYear := blockTime.AddDate(1, 0, 0)
|
||||
sendAuthz := banktypes.NewSendAuthorization(coins100, nil)
|
||||
@@ -110,7 +111,7 @@ func TestMigration(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(1 * time.Hour))
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.Add(1 * time.Hour)})
|
||||
require.NoError(t, v2.MigrateStore(ctx, storeService, cdc))
|
||||
|
||||
bz, err := store.Get(v2.GrantStoreKey(grantee1, granter2, genericMsgType))
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/core/header"
|
||||
"cosmossdk.io/log"
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
@@ -49,7 +50,7 @@ func TestExpiredGrantsQueue(t *testing.T) {
|
||||
grantee2 := addrs[2]
|
||||
grantee3 := addrs[3]
|
||||
grantee4 := addrs[4]
|
||||
expiration := ctx.BlockTime().AddDate(0, 1, 0)
|
||||
expiration := ctx.HeaderInfo().Time.AddDate(0, 1, 0)
|
||||
expiration2 := expiration.AddDate(1, 0, 0)
|
||||
smallCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 10))
|
||||
sendAuthz := banktypes.NewSendAuthorization(smallCoins, nil)
|
||||
@@ -94,12 +95,12 @@ func TestExpiredGrantsQueue(t *testing.T) {
|
||||
checkGrants(ctx, 4)
|
||||
|
||||
// expiration is exclusive!
|
||||
ctx = ctx.WithBlockTime(expiration)
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: expiration})
|
||||
checkGrants(ctx, 4)
|
||||
|
||||
ctx = ctx.WithBlockTime(expiration.AddDate(0, 0, 1))
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: expiration.AddDate(0, 0, 1)})
|
||||
checkGrants(ctx, 2)
|
||||
|
||||
ctx = ctx.WithBlockTime(expiration2.AddDate(0, 0, 1))
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: expiration2.AddDate(0, 0, 1)})
|
||||
checkGrants(ctx, 1)
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ func SimulateMsgGrant(
|
||||
|
||||
var expiration *time.Time
|
||||
t1 := simtypes.RandTimestamp(r)
|
||||
if !t1.Before(ctx.BlockTime()) {
|
||||
if !t1.Before(ctx.HeaderInfo().Time) {
|
||||
expiration = &t1
|
||||
}
|
||||
randomAuthz := generateRandomAuthorization(r, spendLimit)
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/cosmos/gogoproto/proto"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"cosmossdk.io/core/header"
|
||||
"cosmossdk.io/depinject"
|
||||
"cosmossdk.io/log"
|
||||
|
||||
@@ -120,7 +121,7 @@ func (suite *SimTestSuite) TestSimulateGrant() {
|
||||
r := rand.New(s)
|
||||
accounts := suite.getTestingAccounts(r, 2)
|
||||
blockTime := time.Now().UTC()
|
||||
ctx := suite.ctx.WithBlockTime(blockTime)
|
||||
ctx := suite.ctx.WithHeaderInfo(header.Info{Time: blockTime})
|
||||
|
||||
_, err := suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{
|
||||
Height: suite.app.LastBlockHeight() + 1,
|
||||
@@ -195,7 +196,7 @@ func (suite *SimTestSuite) TestSimulateExec() {
|
||||
granter := accounts[0]
|
||||
grantee := accounts[1]
|
||||
a := banktypes.NewSendAuthorization(initCoins, nil)
|
||||
expire := suite.ctx.BlockTime().Add(1 * time.Hour)
|
||||
expire := suite.ctx.HeaderInfo().Time.Add(1 * time.Hour)
|
||||
|
||||
err = suite.authzKeeper.SaveGrant(suite.ctx, grantee.Address, granter.Address, a, &expire)
|
||||
suite.Require().NoError(err)
|
||||
|
||||
Reference in New Issue
Block a user