Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: deprecate Voteinfo in favour of Cometinfo on Context #17670

Merged
merged 19 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
130 changes: 4 additions & 126 deletions baseapp/info.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -137,7 +15,7 @@
var _ comet.BlockInfo = (*prepareProposalInfo)(nil)

func (r prepareProposalInfo) GetEvidence() comet.EvidenceList {
return evidenceWrapper{r.Misbehavior}
return sdk.EvidenceWrapper{r.Misbehavior}

Check failure on line 18 in baseapp/info.go

View workflow job for this annotation

GitHub Actions / Analyze

composites: github.com/cosmos/cosmos-sdk/types.EvidenceWrapper struct literal uses unkeyed fields (govet)
}

func (r prepareProposalInfo) GetValidatorsHash() []byte {
Expand Down Expand Up @@ -193,5 +71,5 @@
}

func (e extendedVoteInfoWrapper) Validator() comet.Validator {
return validatorWrapper{e.ExtendedVoteInfo.Validator}
return sdk.ValidatorWrapper{e.ExtendedVoteInfo.Validator}

Check failure on line 74 in baseapp/info.go

View workflow job for this annotation

GitHub Actions / Analyze

composites: github.com/cosmos/cosmos-sdk/types.ValidatorWrapper struct literal uses unkeyed fields (govet)
}
5 changes: 3 additions & 2 deletions tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
152 changes: 136 additions & 16 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +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
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
voteInfo []abci.VoteInfo // Deprecated: use Cometinfo.GetLastCommit().Votes() instead, will be removed in 0.51
gasMeter storetypes.GasMeter
blockGasMeter storetypes.GasMeter
checkTx bool
Expand All @@ -69,13 +66,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, will be removed after 0.51
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
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 }
Expand Down Expand Up @@ -216,6 +215,7 @@ func (c Context) WithLogger(logger log.Logger) Context {
}

// WithVoteInfos returns a Context with an updated consensus VoteInfo.
// Deprecated: use WithCometinfo() instead, will be removed after 0.51
func (c Context) WithVoteInfos(voteInfo []abci.VoteInfo) Context {
c.voteInfo = voteInfo
return c
Expand Down Expand Up @@ -393,3 +393,123 @@ func UnwrapSDKContext(ctx context.Context) Context {
}
return ctx.Value(SdkContextKey).(Context)
}

// CometInfo defines the properties provided by comet to the application
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
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
}
7 changes: 4 additions & 3 deletions x/distribution/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

// 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
// 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 {
Fixed Show fixed Hide fixed
return err
}
}
Expand Down
Loading
Loading