From fcd47f525270237f21335a061ec67aca1042de52 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Sun, 10 Sep 2023 18:51:43 +0200 Subject: [PATCH 01/15] depreacte voteinfo infavour of cometinfo --- types/context.go | 23 +++++++++++++---------- x/distribution/abci.go | 5 +++-- x/slashing/abci.go | 5 +++-- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/types/context.go b/types/context.go index 2a0ac17340c2..799a8bd115b4 100644 --- a/types/context.go +++ b/types/context.go @@ -45,9 +45,10 @@ type Context struct { // Deprecated: Use HeaderService for hash headerHash []byte // Deprecated: Use HeaderService for chainID and CometService for the rest - chainID string - txBytes []byte - logger log.Logger + chainID string + txBytes []byte + logger log.Logger + // Depreacted: use Cometinfo.GetLastCommit().Votes() instead voteInfo []abci.VoteInfo gasMeter storetypes.GasMeter blockGasMeter storetypes.GasMeter @@ -69,13 +70,15 @@ type Context struct { type Request = Context // Read-only accessors -func (c Context) Context() context.Context { return c.baseCtx } -func (c Context) MultiStore() storetypes.MultiStore { return c.ms } -func (c Context) BlockHeight() int64 { return c.header.Height } -func (c Context) BlockTime() time.Time { return c.header.Time } -func (c Context) ChainID() string { return c.chainID } -func (c Context) TxBytes() []byte { return c.txBytes } -func (c Context) Logger() log.Logger { return c.logger } +func (c Context) Context() context.Context { return c.baseCtx } +func (c Context) MultiStore() storetypes.MultiStore { return c.ms } +func (c Context) BlockHeight() int64 { return c.header.Height } +func (c Context) BlockTime() time.Time { return c.header.Time } +func (c Context) ChainID() string { return c.chainID } +func (c Context) TxBytes() []byte { return c.txBytes } +func (c Context) Logger() log.Logger { return c.logger } + +// Deprecated: use Cometinfo.GetLastCommit().Votes() instead func (c Context) VoteInfos() []abci.VoteInfo { return c.voteInfo } func (c Context) GasMeter() storetypes.GasMeter { return c.gasMeter } func (c Context) BlockGasMeter() storetypes.GasMeter { return c.blockGasMeter } diff --git a/x/distribution/abci.go b/x/distribution/abci.go index 573881cfe0c2..98cdb70fc9ba 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -16,8 +16,9 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) error { // determine the total power signing the block var previousTotalPower int64 - for _, voteInfo := range ctx.VoteInfos() { - previousTotalPower += voteInfo.Validator.Power + for i := 0; i < ctx.CometInfo().GetLastCommit().Votes().Len(); i++ { + vote := ctx.CometInfo().GetLastCommit().Votes().Get(i) + previousTotalPower += vote.Validator().Power() } // TODO this is Tendermint-dependent diff --git a/x/slashing/abci.go b/x/slashing/abci.go index af987c394a3b..c69d116dc5f2 100644 --- a/x/slashing/abci.go +++ b/x/slashing/abci.go @@ -21,8 +21,9 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error { // store whether or not they have actually signed it and slash/unbond any // which have missed too many blocks in a row (downtime slashing) sdkCtx := sdk.UnwrapSDKContext(ctx) - for _, voteInfo := range sdkCtx.VoteInfos() { - err := k.HandleValidatorSignature(ctx, voteInfo.Validator.Address, voteInfo.Validator.Power, comet.BlockIDFlag(voteInfo.BlockIdFlag)) + for i := 0; i < sdkCtx.CometInfo().GetLastCommit().Votes().Len(); i++ { + vote := sdkCtx.CometInfo().GetLastCommit().Votes().Get(i) + err := k.HandleValidatorSignature(ctx, vote.Validator().Address(), vote.Validator().Power(), comet.BlockIDFlag(vote.GetBlockIDFlag())) if err != nil { return err } From 2b04a9f81a8a5defa0941d64de9aa25929ff0c85 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Sun, 10 Sep 2023 18:53:14 +0200 Subject: [PATCH 02/15] add when voteinfo will be removed --- types/context.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/context.go b/types/context.go index 799a8bd115b4..ee60a231fa8f 100644 --- a/types/context.go +++ b/types/context.go @@ -48,7 +48,7 @@ type Context struct { chainID string txBytes []byte logger log.Logger - // Depreacted: use Cometinfo.GetLastCommit().Votes() instead + // Depreacted: use Cometinfo.GetLastCommit().Votes() instead, will be removed in 0.51 voteInfo []abci.VoteInfo gasMeter storetypes.GasMeter blockGasMeter storetypes.GasMeter @@ -78,7 +78,7 @@ func (c Context) ChainID() string { return c.chainID } func (c Context) TxBytes() []byte { return c.txBytes } func (c Context) Logger() log.Logger { return c.logger } -// Deprecated: use Cometinfo.GetLastCommit().Votes() instead +// Deprecated: use Cometinfo.GetLastCommit().Votes() instead, will be removed after 0.51 func (c Context) VoteInfos() []abci.VoteInfo { return c.voteInfo } func (c Context) GasMeter() storetypes.GasMeter { return c.gasMeter } func (c Context) BlockGasMeter() storetypes.GasMeter { return c.blockGasMeter } From 2b9e3fba073eefadc4f352ce1cc785771824f65d Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Sun, 10 Sep 2023 18:53:40 +0200 Subject: [PATCH 03/15] fix lint --- types/context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/context.go b/types/context.go index ee60a231fa8f..65dd47f2fbe9 100644 --- a/types/context.go +++ b/types/context.go @@ -48,7 +48,7 @@ type Context struct { chainID string txBytes []byte logger log.Logger - // Depreacted: use Cometinfo.GetLastCommit().Votes() instead, will be removed in 0.51 + // Deprecated: use Cometinfo.GetLastCommit().Votes() instead, will be removed in 0.51 voteInfo []abci.VoteInfo gasMeter storetypes.GasMeter blockGasMeter storetypes.GasMeter From f111077b96c4938a79ff21667051faaccc58264c Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 11 Sep 2023 09:10:17 +0200 Subject: [PATCH 04/15] move depreacted comments to right of field --- types/context.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/types/context.go b/types/context.go index 65dd47f2fbe9..1e29bacaf3a5 100644 --- a/types/context.go +++ b/types/context.go @@ -38,18 +38,14 @@ but please do not over-use it. We try to keep all data structured and standard additions here would be better just to add to the Context struct */ type Context struct { - baseCtx context.Context - ms storetypes.MultiStore - // Deprecated: Use HeaderService for height, time, and chainID and CometService for the rest - header cmtproto.Header - // Deprecated: Use HeaderService for hash - headerHash []byte - // Deprecated: Use HeaderService for chainID and CometService for the rest - chainID string - txBytes []byte - logger log.Logger - // Deprecated: use Cometinfo.GetLastCommit().Votes() instead, will be removed in 0.51 - voteInfo []abci.VoteInfo + baseCtx context.Context + ms storetypes.MultiStore + header cmtproto.Header // Deprecated: Use HeaderService for height, time, and chainID and CometService for the rest + headerHash []byte // Deprecated: Use HeaderService for hash + chainID string // Deprecated: Use HeaderService for chainID and CometService for the rest + txBytes []byte + logger log.Logger + voteInfo []abci.VoteInfo // Deprecated: use Cometinfo.GetLastCommit().Votes() instead, will be removed in 0.51 gasMeter storetypes.GasMeter blockGasMeter storetypes.GasMeter checkTx bool From ddbb50c89680f712c3831cda32d162b2300e83bc Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 11 Sep 2023 12:22:20 +0200 Subject: [PATCH 05/15] change some more things --- .../distribution/keeper/msg_server_test.go | 5 +- types/context.go | 121 ++++++++++++++++++ x/distribution/abci.go | 2 +- x/distribution/keeper/allocation.go | 12 +- x/distribution/keeper/allocation_test.go | 10 +- x/slashing/abci.go | 4 +- x/slashing/abci_test.go | 25 ++-- 7 files changed, 151 insertions(+), 28 deletions(-) diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index b0f579d5bc48..4b24e52eb3b2 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -322,8 +322,9 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { assert.Assert(t, prevProposerConsAddr.Empty() == false) assert.DeepEqual(t, prevProposerConsAddr, valConsAddr) var previousTotalPower int64 - for _, voteInfo := range f.sdkCtx.VoteInfos() { - previousTotalPower += voteInfo.Validator.Power + for i := 0; i < f.sdkCtx.CometInfo().GetLastCommit().Votes().Len(); i++ { + vote := f.sdkCtx.CometInfo().GetLastCommit().Votes().Get(i) + previousTotalPower += vote.Validator().Power() } assert.Equal(t, previousTotalPower, int64(100)) }) diff --git a/types/context.go b/types/context.go index 1e29bacaf3a5..d65e2ac02c23 100644 --- a/types/context.go +++ b/types/context.go @@ -215,6 +215,7 @@ func (c Context) WithLogger(logger log.Logger) Context { } // WithVoteInfos returns a Context with an updated consensus VoteInfo. +// Deprecated: use Cometinfo.GetLastCommit().Votes() instead, will be removed after 0.51 func (c Context) WithVoteInfos(voteInfo []abci.VoteInfo) Context { c.voteInfo = voteInfo return c @@ -392,3 +393,123 @@ func UnwrapSDKContext(ctx context.Context) Context { } return ctx.Value(SdkContextKey).(Context) } + +// CometInfo defines the properties provided by comet to the application +type CometInfo struct { + Misbehavior []abci.Misbehavior + ValidatorsHash []byte + ProposerAddress []byte + LastCommit abci.CommitInfo +} + +func (r CometInfo) GetEvidence() comet.EvidenceList { + return evidenceWrapper{evidence: r.Misbehavior} +} + +func (r CometInfo) GetValidatorsHash() []byte { + return r.ValidatorsHash +} + +func (r CometInfo) GetProposerAddress() []byte { + return r.ProposerAddress +} + +func (r CometInfo) GetLastCommit() comet.CommitInfo { + return commitInfoWrapper{r.LastCommit} +} + +type evidenceWrapper struct { + evidence []abci.Misbehavior +} + +func (e evidenceWrapper) Len() int { + return len(e.evidence) +} + +func (e evidenceWrapper) Get(i int) comet.Evidence { + return misbehaviorWrapper{e.evidence[i]} +} + +// commitInfoWrapper is a wrapper around abci.CommitInfo that implements CommitInfo interface +type commitInfoWrapper struct { + abci.CommitInfo +} + +var _ comet.CommitInfo = (*commitInfoWrapper)(nil) + +func (c commitInfoWrapper) Round() int32 { + return c.CommitInfo.Round +} + +func (c commitInfoWrapper) Votes() comet.VoteInfos { + return AbciVoteInfoWrapper{c.CommitInfo.Votes} +} + +// AbciVoteInfoWrapper is a wrapper around abci.VoteInfo that implements VoteInfos interface +type AbciVoteInfoWrapper struct { + Votes []abci.VoteInfo +} + +var _ comet.VoteInfos = (*AbciVoteInfoWrapper)(nil) + +func (e AbciVoteInfoWrapper) Len() int { + return len(e.Votes) +} + +func (e AbciVoteInfoWrapper) Get(i int) comet.VoteInfo { + return VoteInfoWrapper{e.Votes[i]} +} + +// VoteInfoWrapper is a wrapper around abci.VoteInfo that implements VoteInfo interface +type VoteInfoWrapper struct { + abci.VoteInfo +} + +var _ comet.VoteInfo = (*VoteInfoWrapper)(nil) + +func (v VoteInfoWrapper) GetBlockIDFlag() comet.BlockIDFlag { + return comet.BlockIDFlag(v.VoteInfo.BlockIdFlag) +} + +func (v VoteInfoWrapper) Validator() comet.Validator { + return ValidatorWrapper{v.VoteInfo.Validator} +} + +// ValidatorWrapper is a wrapper around abci.Validator that implements Validator interface +type ValidatorWrapper struct { + abci.Validator +} + +var _ comet.Validator = (*ValidatorWrapper)(nil) + +func (v ValidatorWrapper) Address() []byte { + return v.Validator.Address +} + +func (v ValidatorWrapper) Power() int64 { + return v.Validator.Power +} + +type misbehaviorWrapper struct { + abci.Misbehavior +} + +func (m misbehaviorWrapper) Type() comet.MisbehaviorType { + return comet.MisbehaviorType(m.Misbehavior.Type) +} + +func (m misbehaviorWrapper) Height() int64 { + return m.Misbehavior.Height +} + +func (m misbehaviorWrapper) Validator() comet.Validator { + return ValidatorWrapper{m.Misbehavior.Validator} +} + +func (m misbehaviorWrapper) Time() time.Time { + return m.Misbehavior.Time +} + +func (m misbehaviorWrapper) TotalVotingPower() int64 { + return m.Misbehavior.TotalVotingPower +} diff --git a/x/distribution/abci.go b/x/distribution/abci.go index 98cdb70fc9ba..a48581beda1d 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -24,7 +24,7 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) error { // TODO this is Tendermint-dependent // ref https://github.com/cosmos/cosmos-sdk/issues/3095 if ctx.BlockHeight() > 1 { - if err := k.AllocateTokens(ctx, previousTotalPower, ctx.VoteInfos()); err != nil { + if err := k.AllocateTokens(ctx, previousTotalPower, ctx.CometInfo().GetLastCommit().Votes()); err != nil { return err } } diff --git a/x/distribution/keeper/allocation.go b/x/distribution/keeper/allocation.go index 2851e21bc2bf..7deab9c03605 100644 --- a/x/distribution/keeper/allocation.go +++ b/x/distribution/keeper/allocation.go @@ -4,9 +4,8 @@ import ( "context" "errors" - abci "github.com/cometbft/cometbft/abci/types" - "cosmossdk.io/collections" + "cosmossdk.io/core/comet" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -16,7 +15,7 @@ import ( // AllocateTokens performs reward and fee distribution to all validators based // on the F1 fee distribution specification. -func (k Keeper) AllocateTokens(ctx context.Context, totalPreviousPower int64, bondedVotes []abci.VoteInfo) error { +func (k Keeper) AllocateTokens(ctx context.Context, totalPreviousPower int64, bondedVotes comet.VoteInfos) error { // fetch and clear the collected fees for distribution, since this is // called in BeginBlock, collected fees will be from the previous block // (and distributed to the previous proposer) @@ -57,8 +56,9 @@ func (k Keeper) AllocateTokens(ctx context.Context, totalPreviousPower int64, bo // TODO: Consider parallelizing later // // Ref: https://github.com/cosmos/cosmos-sdk/pull/3099#discussion_r246276376 - for _, vote := range bondedVotes { - validator, err := k.stakingKeeper.ValidatorByConsAddr(ctx, vote.Validator.Address) + for i := 0; i < bondedVotes.Len(); i++ { + vote := bondedVotes.Get(i) + validator, err := k.stakingKeeper.ValidatorByConsAddr(ctx, vote.Validator().Address()) if err != nil { return err } @@ -66,7 +66,7 @@ func (k Keeper) AllocateTokens(ctx context.Context, totalPreviousPower int64, bo // TODO: Consider micro-slashing for missing votes. // // Ref: https://github.com/cosmos/cosmos-sdk/issues/2525#issuecomment-430838701 - powerFraction := math.LegacyNewDec(vote.Validator.Power).QuoTruncate(math.LegacyNewDec(totalPreviousPower)) + powerFraction := math.LegacyNewDec(vote.Validator().Power()).QuoTruncate(math.LegacyNewDec(totalPreviousPower)) reward := feeMultiplier.MulDecTruncate(powerFraction) err = k.AllocateTokensToValidator(ctx, validator, reward) diff --git a/x/distribution/keeper/allocation_test.go b/x/distribution/keeper/allocation_test.go index a3feca31835a..b21603b88b63 100644 --- a/x/distribution/keeper/allocation_test.go +++ b/x/distribution/keeper/allocation_test.go @@ -165,14 +165,15 @@ func TestAllocateTokensToManyValidators(t *testing.T) { bankKeeper.EXPECT().GetAllBalances(gomock.Any(), feeCollectorAcc.GetAddress()).Return(fees) bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), "fee_collector", disttypes.ModuleName, fees) - votes := []abci.VoteInfo{ + votes := sdk.AbciVoteInfoWrapper{Votes: []abci.VoteInfo{ { Validator: abciValA, }, { Validator: abciValB, }, - } + }} + require.NoError(t, distrKeeper.AllocateTokens(ctx, 200, votes)) // 98 outstanding rewards (100 less 2 to community pool) @@ -303,7 +304,7 @@ func TestAllocateTokensTruncation(t *testing.T) { bankKeeper.EXPECT().GetAllBalances(gomock.Any(), feeCollectorAcc.GetAddress()).Return(fees) bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), "fee_collector", disttypes.ModuleName, fees) - votes := []abci.VoteInfo{ + votes := sdk.AbciVoteInfoWrapper{Votes: []abci.VoteInfo{ { Validator: abciValA, }, @@ -313,7 +314,8 @@ func TestAllocateTokensTruncation(t *testing.T) { { Validator: abciValC, }, - } + }} + require.NoError(t, distrKeeper.AllocateTokens(ctx, 31, votes)) val0OutstandingRewards, err := distrKeeper.ValidatorOutstandingRewards.Get(ctx, valAddr0) diff --git a/x/slashing/abci.go b/x/slashing/abci.go index c69d116dc5f2..0f0fcadb0a77 100644 --- a/x/slashing/abci.go +++ b/x/slashing/abci.go @@ -4,8 +4,6 @@ import ( "context" "time" - "cosmossdk.io/core/comet" - "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/keeper" @@ -23,7 +21,7 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error { sdkCtx := sdk.UnwrapSDKContext(ctx) for i := 0; i < sdkCtx.CometInfo().GetLastCommit().Votes().Len(); i++ { vote := sdkCtx.CometInfo().GetLastCommit().Votes().Get(i) - err := k.HandleValidatorSignature(ctx, vote.Validator().Address(), vote.Validator().Power(), comet.BlockIDFlag(vote.GetBlockIDFlag())) + err := k.HandleValidatorSignature(ctx, vote.Validator().Address(), vote.Validator().Power(), vote.GetBlockIDFlag()) if err != nil { return err } diff --git a/x/slashing/abci_test.go b/x/slashing/abci_test.go index c5d9ed64ced4..a5284bd0e57c 100644 --- a/x/slashing/abci_test.go +++ b/x/slashing/abci_test.go @@ -70,10 +70,14 @@ func TestBeginBlocker(t *testing.T) { Power: power, } - ctx = ctx.WithVoteInfos([]abci.VoteInfo{{ - Validator: abciVal, - BlockIdFlag: cmtproto.BlockIDFlagCommit, - }}) + comet := sdk.CometInfo{ + LastCommit: abci.CommitInfo{Votes: []abci.VoteInfo{{ + Validator: abciVal, + BlockIdFlag: cmtproto.BlockIDFlagCommit, + }}}, + } + + ctx = ctx.WithCometInfo(comet) err = slashing.BeginBlocker(ctx, slashingKeeper) require.NoError(t, err) @@ -91,11 +95,7 @@ func TestBeginBlocker(t *testing.T) { require.NoError(t, err) // for 100 blocks, mark the validator as having signed for ; height < signedBlocksWindow; height++ { - ctx = ctx.WithBlockHeight(height). - WithVoteInfos([]abci.VoteInfo{{ - Validator: abciVal, - BlockIdFlag: cmtproto.BlockIDFlagCommit, - }}) + ctx = ctx.WithBlockHeight(height) err = slashing.BeginBlocker(ctx, slashingKeeper) require.NoError(t, err) @@ -105,11 +105,12 @@ func TestBeginBlocker(t *testing.T) { require.NoError(t, err) // for 50 blocks, mark the validator as having not signed for ; height < ((signedBlocksWindow * 2) - minSignedPerWindow + 1); height++ { - ctx = ctx.WithBlockHeight(height). - WithVoteInfos([]abci.VoteInfo{{ + ctx = ctx.WithBlockHeight(height).WithCometInfo(sdk.CometInfo{ + LastCommit: abci.CommitInfo{Votes: []abci.VoteInfo{{ Validator: abciVal, BlockIdFlag: cmtproto.BlockIDFlagAbsent, - }}) + }}}, + }) err = slashing.BeginBlocker(ctx, slashingKeeper) require.NoError(t, err) From 9f44509da22773ad55852dd8503c0f53791b48ff Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 11 Sep 2023 12:24:13 +0200 Subject: [PATCH 06/15] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76f13b7b3aad..e0efe3cedbb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -140,6 +140,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (types) [#17426](https://github.com/cosmos/cosmos-sdk/pull/17426) `NewContext` does not take a `cmtproto.Header{}` any longer. * `WithChainID` / `WithBlockHeight` / `WithBlockHeader` must be used to set values on the context * (x/bank) [#17569](https://github.com/cosmos/cosmos-sdk/pull/17569) `BurnCoins` takes an address instead of a module name +* (x/distribution) [#17670](https://github.com/cosmos/cosmos-sdk/pull/17670) `AllocateTokens` takes `comet.VoteInfos` instead of `[]abci.VoteInfo` ### CLI Breaking Changes From f9344a930bd1732d0c9d942349e78fa02d7cb5de Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 11 Sep 2023 13:15:51 +0200 Subject: [PATCH 07/15] deduplicate --- baseapp/abci.go | 4 +- baseapp/info.go | 130 ++--------------------------------------------- types/context.go | 14 ++--- 3 files changed, 13 insertions(+), 135 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index cae600d95b5f..e2900eee1fe4 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -514,7 +514,7 @@ func (app *BaseApp) ProcessProposal(req *abci.RequestProcessProposal) (resp *abc WithBlockTime(req.Time). WithHeaderHash(req.Hash). WithProposer(req.ProposerAddress). - WithCometInfo(cometInfo{ProposerAddress: req.ProposerAddress, ValidatorsHash: req.NextValidatorsHash, Misbehavior: req.Misbehavior, LastCommit: req.ProposedLastCommit}). + WithCometInfo(sdk.CometInfo{ProposerAddress: req.ProposerAddress, ValidatorsHash: req.NextValidatorsHash, Misbehavior: req.Misbehavior, LastCommit: req.ProposedLastCommit}). WithExecMode(sdk.ExecModeProcessProposal). WithHeaderInfo(coreheader.Info{ ChainID: app.chainID, @@ -713,7 +713,7 @@ func (app *BaseApp) FinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.Respons WithConsensusParams(app.GetConsensusParams(app.finalizeBlockState.ctx)). WithVoteInfos(req.DecidedLastCommit.Votes). WithExecMode(sdk.ExecModeFinalize). - WithCometInfo(cometInfo{ + WithCometInfo(sdk.CometInfo{ Misbehavior: req.Misbehavior, ValidatorsHash: req.NextValidatorsHash, ProposerAddress: req.ProposerAddress, diff --git a/baseapp/info.go b/baseapp/info.go index 8c8a4451206e..66ca07ef2b18 100644 --- a/baseapp/info.go +++ b/baseapp/info.go @@ -1,134 +1,12 @@ package baseapp import ( - "time" - abci "github.com/cometbft/cometbft/abci/types" "cosmossdk.io/core/comet" -) - -var _ comet.BlockInfo = (*cometInfo)(nil) - -// CometInfo defines the properties provided by comet to the application -type cometInfo struct { - Misbehavior []abci.Misbehavior - ValidatorsHash []byte - ProposerAddress []byte - LastCommit abci.CommitInfo -} - -func (r cometInfo) GetEvidence() comet.EvidenceList { - return evidenceWrapper{evidence: r.Misbehavior} -} - -func (r cometInfo) GetValidatorsHash() []byte { - return r.ValidatorsHash -} - -func (r cometInfo) GetProposerAddress() []byte { - return r.ProposerAddress -} - -func (r cometInfo) GetLastCommit() comet.CommitInfo { - return commitInfoWrapper{r.LastCommit} -} - -type evidenceWrapper struct { - evidence []abci.Misbehavior -} - -func (e evidenceWrapper) Len() int { - return len(e.evidence) -} - -func (e evidenceWrapper) Get(i int) comet.Evidence { - return misbehaviorWrapper{e.evidence[i]} -} - -// commitInfoWrapper is a wrapper around abci.CommitInfo that implements CommitInfo interface -type commitInfoWrapper struct { - abci.CommitInfo -} - -var _ comet.CommitInfo = (*commitInfoWrapper)(nil) - -func (c commitInfoWrapper) Round() int32 { - return c.CommitInfo.Round -} - -func (c commitInfoWrapper) Votes() comet.VoteInfos { - return abciVoteInfoWrapper{c.CommitInfo.Votes} -} - -// abciVoteInfoWrapper is a wrapper around abci.VoteInfo that implements VoteInfos interface -type abciVoteInfoWrapper struct { - votes []abci.VoteInfo -} - -var _ comet.VoteInfos = (*abciVoteInfoWrapper)(nil) - -func (e abciVoteInfoWrapper) Len() int { - return len(e.votes) -} - -func (e abciVoteInfoWrapper) Get(i int) comet.VoteInfo { - return voteInfoWrapper{e.votes[i]} -} - -// voteInfoWrapper is a wrapper around abci.VoteInfo that implements VoteInfo interface -type voteInfoWrapper struct { - abci.VoteInfo -} - -var _ comet.VoteInfo = (*voteInfoWrapper)(nil) - -func (v voteInfoWrapper) GetBlockIDFlag() comet.BlockIDFlag { - return comet.BlockIDFlag(v.VoteInfo.BlockIdFlag) -} - -func (v voteInfoWrapper) Validator() comet.Validator { - return validatorWrapper{v.VoteInfo.Validator} -} - -// validatorWrapper is a wrapper around abci.Validator that implements Validator interface -type validatorWrapper struct { - abci.Validator -} - -var _ comet.Validator = (*validatorWrapper)(nil) - -func (v validatorWrapper) Address() []byte { - return v.Validator.Address -} - -func (v validatorWrapper) Power() int64 { - return v.Validator.Power -} - -type misbehaviorWrapper struct { - abci.Misbehavior -} - -func (m misbehaviorWrapper) Type() comet.MisbehaviorType { - return comet.MisbehaviorType(m.Misbehavior.Type) -} -func (m misbehaviorWrapper) Height() int64 { - return m.Misbehavior.Height -} - -func (m misbehaviorWrapper) Validator() comet.Validator { - return validatorWrapper{m.Misbehavior.Validator} -} - -func (m misbehaviorWrapper) Time() time.Time { - return m.Misbehavior.Time -} - -func (m misbehaviorWrapper) TotalVotingPower() int64 { - return m.Misbehavior.TotalVotingPower -} + sdk "github.com/cosmos/cosmos-sdk/types" +) type prepareProposalInfo struct { *abci.RequestPrepareProposal @@ -137,7 +15,7 @@ type prepareProposalInfo struct { var _ comet.BlockInfo = (*prepareProposalInfo)(nil) func (r prepareProposalInfo) GetEvidence() comet.EvidenceList { - return evidenceWrapper{r.Misbehavior} + return sdk.EvidenceWrapper{r.Misbehavior} } func (r prepareProposalInfo) GetValidatorsHash() []byte { @@ -193,5 +71,5 @@ func (e extendedVoteInfoWrapper) GetBlockIDFlag() comet.BlockIDFlag { } func (e extendedVoteInfoWrapper) Validator() comet.Validator { - return validatorWrapper{e.ExtendedVoteInfo.Validator} + return sdk.ValidatorWrapper{e.ExtendedVoteInfo.Validator} } diff --git a/types/context.go b/types/context.go index d65e2ac02c23..db50b33f0ec7 100644 --- a/types/context.go +++ b/types/context.go @@ -403,7 +403,7 @@ type CometInfo struct { } func (r CometInfo) GetEvidence() comet.EvidenceList { - return evidenceWrapper{evidence: r.Misbehavior} + return EvidenceWrapper{Evidence: r.Misbehavior} } func (r CometInfo) GetValidatorsHash() []byte { @@ -418,16 +418,16 @@ func (r CometInfo) GetLastCommit() comet.CommitInfo { return commitInfoWrapper{r.LastCommit} } -type evidenceWrapper struct { - evidence []abci.Misbehavior +type EvidenceWrapper struct { + Evidence []abci.Misbehavior } -func (e evidenceWrapper) Len() int { - return len(e.evidence) +func (e EvidenceWrapper) Len() int { + return len(e.Evidence) } -func (e evidenceWrapper) Get(i int) comet.Evidence { - return misbehaviorWrapper{e.evidence[i]} +func (e EvidenceWrapper) Get(i int) comet.Evidence { + return misbehaviorWrapper{e.Evidence[i]} } // commitInfoWrapper is a wrapper around abci.CommitInfo that implements CommitInfo interface From 196af2a1cca6e6675ebff4f5386e291524e5bf74 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 11 Sep 2023 13:16:38 +0200 Subject: [PATCH 08/15] fix godoc --- types/context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/context.go b/types/context.go index db50b33f0ec7..97d27716b461 100644 --- a/types/context.go +++ b/types/context.go @@ -215,7 +215,7 @@ func (c Context) WithLogger(logger log.Logger) Context { } // WithVoteInfos returns a Context with an updated consensus VoteInfo. -// Deprecated: use Cometinfo.GetLastCommit().Votes() instead, will be removed after 0.51 +// Deprecated: use WithCometinfo() instead, will be removed after 0.51 func (c Context) WithVoteInfos(voteInfo []abci.VoteInfo) Context { c.voteInfo = voteInfo return c From dddc7697b0c28eddf3bb2fc93246ea71f5029b70 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 11 Sep 2023 15:59:23 +0200 Subject: [PATCH 09/15] fix lint --- baseapp/info.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/baseapp/info.go b/baseapp/info.go index 66ca07ef2b18..93842bc2fb7d 100644 --- a/baseapp/info.go +++ b/baseapp/info.go @@ -15,7 +15,7 @@ type prepareProposalInfo struct { var _ comet.BlockInfo = (*prepareProposalInfo)(nil) func (r prepareProposalInfo) GetEvidence() comet.EvidenceList { - return sdk.EvidenceWrapper{r.Misbehavior} + return sdk.EvidenceWrapper{Evidence: r.Misbehavior} } func (r prepareProposalInfo) GetValidatorsHash() []byte { @@ -71,5 +71,5 @@ func (e extendedVoteInfoWrapper) GetBlockIDFlag() comet.BlockIDFlag { } func (e extendedVoteInfoWrapper) Validator() comet.Validator { - return sdk.ValidatorWrapper{e.ExtendedVoteInfo.Validator} + return sdk.ValidatorWrapper{Validator: e.ExtendedVoteInfo.Validator} } From 412812e6dda06d286a2734cf743919de8923ee9f Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 11 Sep 2023 16:16:28 +0200 Subject: [PATCH 10/15] lint and fix integration tests --- .../distribution/keeper/msg_server_test.go | 16 ++++++++++------ .../internal/implementation/protoaccount.go | 9 ++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index 4b24e52eb3b2..135e703d48f8 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -116,13 +116,17 @@ func initFixture(tb testing.TB) *fixture { valConsAddr := sdk.ConsAddress(valConsPk0.Address()) // set proposer and vote infos - ctx := newCtx.WithProposer(valConsAddr).WithVoteInfos([]cmtabcitypes.VoteInfo{ - { - Validator: cmtabcitypes.Validator{ - Address: valAddr, - Power: 100, + ctx := newCtx.WithProposer(valConsAddr).WithCometInfo(sdk.CometInfo{ + LastCommit: cmtabcitypes.CommitInfo{ + Votes: []cmtabcitypes.VoteInfo{ + { + Validator: cmtabcitypes.Validator{ + Address: valAddr, + Power: 100, + }, + BlockIdFlag: types.BlockIDFlagCommit, + }, }, - BlockIdFlag: types.BlockIDFlagCommit, }, }) diff --git a/x/accounts/internal/implementation/protoaccount.go b/x/accounts/internal/implementation/protoaccount.go index 02e2c2f15edc..1a507642a2c0 100644 --- a/x/accounts/internal/implementation/protoaccount.go +++ b/x/accounts/internal/implementation/protoaccount.go @@ -17,7 +17,8 @@ type ProtoMsg[T any] interface { // RegisterInitHandler registers an initialisation handler for a smart account that uses protobuf. func RegisterInitHandler[ Req any, ProtoReq ProtoMsg[Req], Resp any, ProtoResp ProtoMsg[Resp], -](router *InitBuilder, handler func(ctx context.Context, req ProtoReq) (ProtoResp, error)) { +](router *InitBuilder, handler func(ctx context.Context, req ProtoReq) (ProtoResp, error), +) { reqName := ProtoReq(new(Req)).ProtoReflect().Descriptor().FullName() respName := ProtoResp(new(Resp)).ProtoReflect().Descriptor().FullName() @@ -47,7 +48,8 @@ func RegisterInitHandler[ // RegisterExecuteHandler registers an execution handler for a smart account that uses protobuf. func RegisterExecuteHandler[ Req any, ProtoReq ProtoMsg[Req], Resp any, ProtoResp ProtoMsg[Resp], -](router *ExecuteBuilder, handler func(ctx context.Context, req ProtoReq) (ProtoResp, error)) { +](router *ExecuteBuilder, handler func(ctx context.Context, req ProtoReq) (ProtoResp, error), +) { reqName := ProtoReq(new(Req)).ProtoReflect().Descriptor().FullName() // check if not registered already if _, ok := router.handlers[string(reqName)]; ok { @@ -67,6 +69,7 @@ func RegisterExecuteHandler[ // RegisterQueryHandler registers a query handler for a smart account that uses protobuf. func RegisterQueryHandler[ Req any, ProtoReq ProtoMsg[Req], Resp any, ProtoResp ProtoMsg[Resp], -](router *QueryBuilder, handler func(ctx context.Context, req ProtoReq) (ProtoResp, error)) { +](router *QueryBuilder, handler func(ctx context.Context, req ProtoReq) (ProtoResp, error), +) { RegisterExecuteHandler(router.er, handler) } From 1a17e2d98743cced917c6eaefdcaf16ff9559c96 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 11 Sep 2023 16:33:57 +0200 Subject: [PATCH 11/15] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0efe3cedbb5..68925ebda8e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [#17470](https://github.com/cosmos/cosmos-sdk/pull/17470) Avoid open 0.0.0.0 to public by default and add `listen-ip-address` argument for `testnet init-files` cmd. * (types/module) [#17554](https://github.com/cosmos/cosmos-sdk/pull/17554) Introduce `HasABCIGenesis` which is implemented by a module only when a validatorset update needs to be returned * (baseapp) [#17667](https://github.com/cosmos/cosmos-sdk/pull/17667) Close databases opened by cosmos-sdk during BaseApp shutdown +* (types) [#17670](https://github.com/cosmos/cosmos-sdk/pull/17670) Use `ctx.CometInfo` in place of `ctx.VoteInfos` ### Bug Fixes From 2ac8ea104d2ac36b9763596d345fc2a8d29c21f2 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 11 Sep 2023 19:21:28 +0200 Subject: [PATCH 12/15] attempt to fix --- testutil/integration/router.go | 6 +++++- x/slashing/abci.go | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/testutil/integration/router.go b/testutil/integration/router.go index 719575bcd65b..b9dd4160fc34 100644 --- a/testutil/integration/router.go +++ b/testutil/integration/router.go @@ -140,13 +140,17 @@ func (app *App) RunMsg(msg sdk.Msg, option ...Option) (*codectypes.Any, error) { }() } + app.ctx = app.ctx.WithCometInfo(sdk.CometInfo{}) + if cfg.AutomaticFinalizeBlock { height := app.LastBlockHeight() + 1 - if _, err := app.FinalizeBlock(&cmtabcitypes.RequestFinalizeBlock{Height: height}); err != nil { + if _, err := app.FinalizeBlock(&cmtabcitypes.RequestFinalizeBlock{Height: height, DecidedLastCommit: cmtabcitypes.CommitInfo{Votes: []cmtabcitypes.VoteInfo{{}}}}); err != nil { return nil, fmt.Errorf("failed to run finalize block: %w", err) } } + fmt.Println(app.ctx.CometInfo(), "runmsg") + app.logger.Info("Running msg", "msg", msg.String()) handler := app.MsgServiceRouter().Handler(msg) diff --git a/x/slashing/abci.go b/x/slashing/abci.go index 0f0fcadb0a77..4106a168910f 100644 --- a/x/slashing/abci.go +++ b/x/slashing/abci.go @@ -19,6 +19,10 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error { // store whether or not they have actually signed it and slash/unbond any // which have missed too many blocks in a row (downtime slashing) sdkCtx := sdk.UnwrapSDKContext(ctx) + // in testing cometInfo is nil + if sdkCtx.CometInfo() == nil { + return nil + } for i := 0; i < sdkCtx.CometInfo().GetLastCommit().Votes().Len(); i++ { vote := sdkCtx.CometInfo().GetLastCommit().Votes().Get(i) err := k.HandleValidatorSignature(ctx, vote.Validator().Address(), vote.Validator().Power(), vote.GetBlockIDFlag()) From 1df4c466b04d9dd696391e39fcca5dbcfffe0720 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 11 Sep 2023 19:29:45 +0200 Subject: [PATCH 13/15] remove print --- testutil/integration/router.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/testutil/integration/router.go b/testutil/integration/router.go index b9dd4160fc34..389469b00384 100644 --- a/testutil/integration/router.go +++ b/testutil/integration/router.go @@ -140,8 +140,6 @@ func (app *App) RunMsg(msg sdk.Msg, option ...Option) (*codectypes.Any, error) { }() } - app.ctx = app.ctx.WithCometInfo(sdk.CometInfo{}) - if cfg.AutomaticFinalizeBlock { height := app.LastBlockHeight() + 1 if _, err := app.FinalizeBlock(&cmtabcitypes.RequestFinalizeBlock{Height: height, DecidedLastCommit: cmtabcitypes.CommitInfo{Votes: []cmtabcitypes.VoteInfo{{}}}}); err != nil { @@ -149,8 +147,6 @@ func (app *App) RunMsg(msg sdk.Msg, option ...Option) (*codectypes.Any, error) { } } - fmt.Println(app.ctx.CometInfo(), "runmsg") - app.logger.Info("Running msg", "msg", msg.String()) handler := app.MsgServiceRouter().Handler(msg) From 0fe01dd117d06d203985a68368187b2f2aaf5334 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Wed, 13 Sep 2023 23:26:29 +0200 Subject: [PATCH 14/15] fix integration tests --- .../distribution/keeper/msg_server_test.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index 135e703d48f8..6eb37d0c73d9 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -4,13 +4,12 @@ import ( "fmt" "testing" - cmtabcitypes "github.com/cometbft/cometbft/abci/types" - "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/stretchr/testify/require" "gotest.tools/v3/assert" "cosmossdk.io/collections" "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/comet" "cosmossdk.io/log" "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" @@ -116,15 +115,15 @@ func initFixture(tb testing.TB) *fixture { valConsAddr := sdk.ConsAddress(valConsPk0.Address()) // set proposer and vote infos - ctx := newCtx.WithProposer(valConsAddr).WithCometInfo(sdk.CometInfo{ - LastCommit: cmtabcitypes.CommitInfo{ - Votes: []cmtabcitypes.VoteInfo{ + ctx := newCtx.WithProposer(valConsAddr).WithCometInfo(comet.Info{ + LastCommit: comet.CommitInfo{ + Votes: []comet.VoteInfo{ { - Validator: cmtabcitypes.Validator{ + Validator: comet.Validator{ Address: valAddr, Power: 100, }, - BlockIdFlag: types.BlockIDFlagCommit, + BlockIDFlag: comet.BlockIDFlagCommit, }, }, }, @@ -326,9 +325,8 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { assert.Assert(t, prevProposerConsAddr.Empty() == false) assert.DeepEqual(t, prevProposerConsAddr, valConsAddr) var previousTotalPower int64 - for i := 0; i < f.sdkCtx.CometInfo().GetLastCommit().Votes().Len(); i++ { - vote := f.sdkCtx.CometInfo().GetLastCommit().Votes().Get(i) - previousTotalPower += vote.Validator().Power() + for _, vote := range f.sdkCtx.CometInfo().LastCommit.Votes { + previousTotalPower += vote.Validator.Power } assert.Equal(t, previousTotalPower, int64(100)) }) From 10146f03930c0aa76e5d2ced8598ce47a4349c81 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Wed, 13 Sep 2023 23:44:49 +0200 Subject: [PATCH 15/15] fix distr tests --- x/distribution/keeper/allocation_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/x/distribution/keeper/allocation_test.go b/x/distribution/keeper/allocation_test.go index b21603b88b63..b7ddc3d38fec 100644 --- a/x/distribution/keeper/allocation_test.go +++ b/x/distribution/keeper/allocation_test.go @@ -4,12 +4,12 @@ import ( "testing" "time" - abci "github.com/cometbft/cometbft/abci/types" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" "cosmossdk.io/collections" + "cosmossdk.io/core/comet" "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" @@ -128,11 +128,11 @@ func TestAllocateTokensToManyValidators(t *testing.T) { val1.Commission = stakingtypes.NewCommission(math.LegacyNewDec(0), math.LegacyNewDec(0), math.LegacyNewDec(0)) stakingKeeper.EXPECT().ValidatorByConsAddr(gomock.Any(), sdk.GetConsAddress(valConsPk1)).Return(val1, nil).AnyTimes() - abciValA := abci.Validator{ + abciValA := comet.Validator{ Address: valConsPk0.Address(), Power: 100, } - abciValB := abci.Validator{ + abciValB := comet.Validator{ Address: valConsPk1.Address(), Power: 100, } @@ -165,14 +165,14 @@ func TestAllocateTokensToManyValidators(t *testing.T) { bankKeeper.EXPECT().GetAllBalances(gomock.Any(), feeCollectorAcc.GetAddress()).Return(fees) bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), "fee_collector", disttypes.ModuleName, fees) - votes := sdk.AbciVoteInfoWrapper{Votes: []abci.VoteInfo{ + votes := []comet.VoteInfo{ { Validator: abciValA, }, { Validator: abciValB, }, - }} + } require.NoError(t, distrKeeper.AllocateTokens(ctx, 200, votes)) @@ -263,15 +263,15 @@ func TestAllocateTokensTruncation(t *testing.T) { val2.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDec(0)) stakingKeeper.EXPECT().ValidatorByConsAddr(gomock.Any(), sdk.GetConsAddress(valConsPk2)).Return(val2, nil).AnyTimes() - abciValA := abci.Validator{ + abciValA := comet.Validator{ Address: valConsPk0.Address(), Power: 11, } - abciValB := abci.Validator{ + abciValB := comet.Validator{ Address: valConsPk1.Address(), Power: 10, } - abciValC := abci.Validator{ + abciValC := comet.Validator{ Address: valConsPk2.Address(), Power: 10, } @@ -304,7 +304,7 @@ func TestAllocateTokensTruncation(t *testing.T) { bankKeeper.EXPECT().GetAllBalances(gomock.Any(), feeCollectorAcc.GetAddress()).Return(fees) bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), "fee_collector", disttypes.ModuleName, fees) - votes := sdk.AbciVoteInfoWrapper{Votes: []abci.VoteInfo{ + votes := []comet.VoteInfo{ { Validator: abciValA, }, @@ -314,7 +314,7 @@ func TestAllocateTokensTruncation(t *testing.T) { { Validator: abciValC, }, - }} + } require.NoError(t, distrKeeper.AllocateTokens(ctx, 31, votes))