Skip to content

Commit

Permalink
introduce core/abci package
Browse files Browse the repository at this point in the history
- alias core/comet to core/abci
- refactor usages of core/comet throughout SDK
  • Loading branch information
kocubinski committed May 2, 2024
1 parent 13fd302 commit fb6f2a7
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 79 deletions.
4 changes: 2 additions & 2 deletions baseapp/abci_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
protoio "github.com/cosmos/gogoproto/io"
"github.com/cosmos/gogoproto/proto"

"cosmossdk.io/core/comet"
coreabci "cosmossdk.io/core/abci"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/mempool"
Expand Down Expand Up @@ -150,7 +150,7 @@ func ValidateVoteExtensions(
// it checks that the ExtendedCommit + LastCommit (for the same height), are consistent with each other + that
// they are ordered correctly (by voting power) in accordance with
// [comet](https://github.com/cometbft/cometbft/blob/4ce0277b35f31985bbf2c25d3806a184a4510010/types/validator_set.go#L784).
func validateExtendedCommitAgainstLastCommit(ec abci.ExtendedCommitInfo, lc comet.CommitInfo) error {
func validateExtendedCommitAgainstLastCommit(ec abci.ExtendedCommitInfo, lc coreabci.CommitInfo) error {
// check that the rounds are the same
if ec.Round != lc.Round {
return fmt.Errorf("extended commit round %d does not match last commit round %d", ec.Round, lc.Round)
Expand Down
9 changes: 5 additions & 4 deletions baseapp/abci_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

coreabci "cosmossdk.io/core/abci"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/header"
"cosmossdk.io/log"
Expand Down Expand Up @@ -755,14 +756,14 @@ func extendedCommitToLastCommit(ec abci.ExtendedCommitInfo) (abci.ExtendedCommit
sort.Sort(extendedVoteInfos(ec.Votes))

// convert the extended commit info to last commit info
lastCommit := comet.CommitInfo{
lastCommit := coreabci.CommitInfo{
Round: ec.Round,
Votes: make([]comet.VoteInfo, len(ec.Votes)),
Votes: make([]coreabci.VoteInfo, len(ec.Votes)),
}

for i, vote := range ec.Votes {
lastCommit.Votes[i] = comet.VoteInfo{
Validator: comet.Validator{
lastCommit.Votes[i] = coreabci.VoteInfo{
Validator: coreabci.Validator{
Address: vote.Validator.Address,
Power: vote.Validator.Power,
},
Expand Down
7 changes: 7 additions & 0 deletions core/abci/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Package abci defines the ABCIInfo Service interface and BlockInfo types which applications
should use in order to get access to the current block's evidence, validators hash, proposer address.
This information is specific to ABCI
*/
package abci
70 changes: 70 additions & 0 deletions core/abci/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package abci

import (
"context"
"time"
)

// Service is an interface that can be used to get information specific to Comet
type Service interface {
ABCIInfo(context.Context) Info
}

// Info is the information comet provides apps in ABCI
type Info struct {
Evidence []Evidence // Evidence misbehavior of the block
// ValidatorsHash returns the hash of the validators
// For Comet, it is the hash of the next validator set
ValidatorsHash []byte
ProposerAddress []byte // ProposerAddress is the address of the block proposer
LastCommit CommitInfo // DecidedLastCommit returns the last commit info
}

// MisbehaviorType is the type of misbehavior for a validator
type MisbehaviorType int32

const (
Unknown MisbehaviorType = 0
DuplicateVote MisbehaviorType = 1
LightClientAttack MisbehaviorType = 2
)

// Evidence is the misbehavior information of ABCI
type Evidence struct {
Type MisbehaviorType
Validator Validator
Height int64
Time time.Time
TotalVotingPower int64
}

// CommitInfo is the commit information of ABCI
type CommitInfo struct {
Round int32
Votes []VoteInfo
}

// VoteInfo is the vote information of ABCI
type VoteInfo struct {
Validator Validator
BlockIDFlag BlockIDFlag
}

// BlockIDFlag indicates which BlockID the signature is for
type BlockIDFlag int32

const (
BlockIDFlagUnknown BlockIDFlag = 0
// BlockIDFlagAbsent - no vote was received from a validator.
BlockIDFlagAbsent BlockIDFlag = 1
// BlockIDFlagCommit - voted for the Commit.BlockID.
BlockIDFlagCommit BlockIDFlag = 2
// BlockIDFlagNil - voted for nil.
BlockIDFlagNil BlockIDFlag = 3
)

// Validator is the validator information of ABCI
type Validator struct {
Address []byte
Power int64
}
4 changes: 2 additions & 2 deletions core/appmodule/v2/environment.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package appmodule

import (
"cosmossdk.io/core/abci"
"cosmossdk.io/core/branch"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/event"
"cosmossdk.io/core/gas"
"cosmossdk.io/core/header"
Expand All @@ -16,7 +16,7 @@ import (
type Environment struct {
Logger log.Logger

CometInfoService comet.Service
ABCIInfoService abci.Service
BranchService branch.Service
EventService event.Service
GasService gas.Service
Expand Down
53 changes: 16 additions & 37 deletions core/comet/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package comet

import (
"context"
"time"

"cosmossdk.io/core/abci"
)

// Service is an interface that can be used to get information specific to Comet
Expand All @@ -11,60 +12,38 @@ type Service interface {
}

// Info is the information comet provides apps in ABCI
type Info struct {
Evidence []Evidence // Evidence misbehavior of the block
// ValidatorsHash returns the hash of the validators
// For Comet, it is the hash of the next validator set
ValidatorsHash []byte
ProposerAddress []byte // ProposerAddress is the address of the block proposer
LastCommit CommitInfo // DecidedLastCommit returns the last commit info
}
type Info abci.Info

// MisbehaviorType is the type of misbehavior for a validator
type MisbehaviorType int32
type MisbehaviorType abci.MisbehaviorType

const (
Unknown MisbehaviorType = 0
DuplicateVote MisbehaviorType = 1
LightClientAttack MisbehaviorType = 2
Unknown = abci.Unknown
DuplicateVote = abci.DuplicateVote
LightClientAttack = abci.LightClientAttack
)

// Evidence is the misbehavior information of ABCI
type Evidence struct {
Type MisbehaviorType
Validator Validator
Height int64
Time time.Time
TotalVotingPower int64
}
type Evidence abci.Evidence

// CommitInfo is the commit information of ABCI
type CommitInfo struct {
Round int32
Votes []VoteInfo
}
type CommitInfo abci.CommitInfo

// VoteInfo is the vote information of ABCI
type VoteInfo struct {
Validator Validator
BlockIDFlag BlockIDFlag
}
type VoteInfo abci.VoteInfo

// BlockIDFlag indicates which BlockID the signature is for
type BlockIDFlag int32
type BlockIDFlag abci.BlockIDFlag

const (
BlockIDFlagUnknown BlockIDFlag = 0
BlockIDFlagUnknown = abci.BlockIDFlagUnknown
// BlockIDFlagAbsent - no vote was received from a validator.
BlockIDFlagAbsent BlockIDFlag = 1
BlockIDFlagAbsent = abci.BlockIDFlagAbsent
// BlockIDFlagCommit - voted for the Commit.BlockID.
BlockIDFlagCommit BlockIDFlag = 2
BlockIDFlagCommit = abci.BlockIDFlagCommit
// BlockIDFlagNil - voted for nil.
BlockIDFlagNil BlockIDFlag = 3
BlockIDFlagNil = abci.BlockIDFlagNil
)

// Validator is the validator information of ABCI
type Validator struct {
Address []byte
Power int64
}
type Validator abci.Validator
19 changes: 13 additions & 6 deletions runtime/comet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ package runtime
import (
"context"

corecomet "cosmossdk.io/core/comet"
"cosmossdk.io/core/abci"

Check failure on line 6 in runtime/comet.go

View workflow job for this annotation

GitHub Actions / golangci-lint

ST1019: package "cosmossdk.io/core/abci" is being imported more than once (stylecheck)
corecomet "cosmossdk.io/core/abci"

Check failure on line 7 in runtime/comet.go

View workflow job for this annotation

GitHub Actions / golangci-lint

ST1019(related information): other import of "cosmossdk.io/core/abci" (stylecheck)

sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ corecomet.Service = &ContextAwareCometInfoService{}
var _ corecomet.Service = &ContextAwareABCIInfoService{}

// ContextAwareCometInfoService provides CometInfo which is embedded as a value in a Context.
// ContextAwareABCIInfoService provides CometInfo which is embedded as a value in a Context.
// This the legacy (server v1, baseapp) way of accessing CometInfo at the module level.
type ContextAwareCometInfoService struct{}
type ContextAwareABCIInfoService struct{}

func (c ContextAwareCometInfoService) CometInfo(ctx context.Context) corecomet.Info {
return sdk.UnwrapSDKContext(ctx).CometInfo()
func (c ContextAwareABCIInfoService) ABCIInfo(ctx context.Context) abci.Info {
ci := sdk.UnwrapSDKContext(ctx).CometInfo()
return abci.Info{
Evidence: ci.Evidence,
ValidatorsHash: ci.ValidatorsHash,
ProposerAddress: ci.ProposerAddress,
LastCommit: ci.LastCommit,
}
}
2 changes: 1 addition & 1 deletion runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewEnvironment(
) appmodule.Environment {
env := appmodule.Environment{
Logger: logger,
CometInfoService: ContextAwareCometInfoService{},
ABCIInfoService: ContextAwareABCIInfoService{},
EventService: EventService{},
HeaderService: HeaderService{},
BranchService: BranchService{},
Expand Down
33 changes: 17 additions & 16 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

coreabci "cosmossdk.io/core/abci"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/header"
"cosmossdk.io/log"
Expand Down Expand Up @@ -394,15 +395,15 @@ func UnwrapSDKContext(ctx context.Context) Context {
}

// ToSDKEvidence takes comet evidence and returns sdk evidence
func ToSDKEvidence(ev []abci.Misbehavior) []comet.Evidence {
evidence := make([]comet.Evidence, len(ev))
func ToSDKEvidence(ev []abci.Misbehavior) []coreabci.Evidence {
evidence := make([]coreabci.Evidence, len(ev))
for i, e := range ev {
evidence[i] = comet.Evidence{
Type: comet.MisbehaviorType(e.Type),
evidence[i] = coreabci.Evidence{
Type: coreabci.MisbehaviorType(e.Type),
Height: e.Height,
Time: e.Time,
TotalVotingPower: e.TotalVotingPower,
Validator: comet.Validator{
Validator: coreabci.Validator{
Address: e.Validator.Address,
Power: e.Validator.Power,
},
Expand All @@ -412,37 +413,37 @@ func ToSDKEvidence(ev []abci.Misbehavior) []comet.Evidence {
}

// ToSDKCommitInfo takes comet commit info and returns sdk commit info
func ToSDKCommitInfo(commit abci.CommitInfo) comet.CommitInfo {
ci := comet.CommitInfo{
func ToSDKCommitInfo(commit abci.CommitInfo) coreabci.CommitInfo {
ci := coreabci.CommitInfo{
Round: commit.Round,
}

for _, v := range commit.Votes {
ci.Votes = append(ci.Votes, comet.VoteInfo{
Validator: comet.Validator{
ci.Votes = append(ci.Votes, coreabci.VoteInfo{
Validator: coreabci.Validator{
Address: v.Validator.Address,
Power: v.Validator.Power,
},
BlockIDFlag: comet.BlockIDFlag(v.BlockIdFlag),
BlockIDFlag: coreabci.BlockIDFlag(v.BlockIdFlag),
})
}
return ci
}

// ToSDKExtendedCommitInfo takes comet extended commit info and returns sdk commit info
func ToSDKExtendedCommitInfo(commit abci.ExtendedCommitInfo) comet.CommitInfo {
ci := comet.CommitInfo{
func ToSDKExtendedCommitInfo(commit abci.ExtendedCommitInfo) coreabci.CommitInfo {
ci := coreabci.CommitInfo{
Round: commit.Round,
Votes: make([]comet.VoteInfo, len(commit.Votes)),
Votes: make([]coreabci.VoteInfo, len(commit.Votes)),
}

for i, v := range commit.Votes {
ci.Votes[i] = comet.VoteInfo{
Validator: comet.Validator{
ci.Votes[i] = coreabci.VoteInfo{
Validator: coreabci.Validator{
Address: v.Validator.Address,
Power: v.Validator.Power,
},
BlockIDFlag: comet.BlockIDFlag(v.BlockIdFlag),
BlockIDFlag: coreabci.BlockIDFlag(v.BlockIdFlag),
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/evidence/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import (
func (k Keeper) BeginBlocker(ctx context.Context) error {
defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker)

bi := k.CometInfoService.CometInfo(ctx)
bi := k.ABCIInfoService.ABCIInfo(ctx)

evidences := bi.Evidence
for _, evidence := range evidences {
switch evidence.Type {
// It's still ongoing discussion how should we treat and slash attacks with
// premeditation. So for now we agree to treat them in the same way.
case comet.LightClientAttack, comet.DuplicateVote:
evidence := types.FromABCIEvidence(evidence, k.stakingKeeper.ConsensusAddressCodec())
evidence := types.FromABCIEvidence(comet.Evidence(evidence), k.stakingKeeper.ConsensusAddressCodec())

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
err := k.handleEquivocationEvidence(ctx, evidence)
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions x/evidence/types/evidence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/stretchr/testify/require"

"cosmossdk.io/core/abci"
"cosmossdk.io/core/comet"
"cosmossdk.io/x/evidence/types"

Expand Down Expand Up @@ -79,8 +80,8 @@ func TestEquivocationValidateBasic(t *testing.T) {

func TestEvidenceAddressConversion(t *testing.T) {
sdk.GetConfig().SetBech32PrefixForConsensusNode("testcnclcons", "testcnclconspub")
tmEvidence := NewCometMisbehavior(1, 100, time.Now(), comet.DuplicateVote,
comet.Validator{Address: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Power: 100})
tmEvidence := newCometMisbehavior(1, 100, time.Now(), comet.DuplicateVote,
abci.Validator{Address: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Power: 100})

evidence := types.FromABCIEvidence(tmEvidence, address.NewBech32Codec("testcnclcons"))
consAddr := evidence.GetConsensusAddress(address.NewBech32Codec("testcnclcons"))
Expand All @@ -89,7 +90,7 @@ func TestEvidenceAddressConversion(t *testing.T) {
sdk.GetConfig().SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub)
}

func NewCometMisbehavior(height, tvp int64, t time.Time, tpe comet.MisbehaviorType, val comet.Validator) comet.Evidence {
func newCometMisbehavior(height, tvp int64, t time.Time, tpe abci.MisbehaviorType, val abci.Validator) comet.Evidence {
return comet.Evidence{
Height: height,
Time: t,
Expand Down
Loading

0 comments on commit fb6f2a7

Please sign in to comment.