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

feat: add header and comet info service #15850

Merged
merged 38 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
63cc148
experimentation of block info service
tac0turtle Apr 15, 2023
0ed3898
remove nil check
tac0turtle Apr 15, 2023
1d97881
move blockinfo to types
tac0turtle Apr 15, 2023
ef59eb7
define our own types
tac0turtle Apr 16, 2023
0144793
fix build
tac0turtle Apr 16, 2023
947de98
fix build
tac0turtle Apr 16, 2023
d9437f2
address comments
tac0turtle Apr 16, 2023
5f772ce
fix build & lint
tac0turtle Apr 17, 2023
e1f53cc
Merge branch 'main' into marko/block_info
tac0turtle Apr 17, 2023
3f136c0
fix build & lint
tac0turtle Apr 17, 2023
cf9b6d7
minor change
tac0turtle Apr 17, 2023
a5d72fd
fix test
tac0turtle Apr 17, 2023
7ed99df
fix tests
tac0turtle Apr 17, 2023
ec6f5d5
add getblockinfo
tac0turtle Apr 17, 2023
56c340e
implement changes
tac0turtle Apr 17, 2023
5b1cd47
address feedback
tac0turtle Apr 18, 2023
0206cb2
Merge branch 'main' into marko/block_info
tac0turtle Apr 18, 2023
a6d45bf
adjust adr
tac0turtle Apr 18, 2023
6995674
make it structs
tac0turtle Apr 18, 2023
06fcbe5
update docs
tac0turtle Apr 18, 2023
22ae764
add docs
tac0turtle Apr 18, 2023
4b85274
fixxie
tac0turtle Apr 18, 2023
46fee3c
WIP on comet block info (#15889)
aaronc Apr 24, 2023
2cfb91d
fix
tac0turtle Apr 25, 2023
9c509b2
minor changes
tac0turtle Apr 25, 2023
99903f9
adjust changes
tac0turtle Apr 25, 2023
d3b8000
implement voteinfos interface
tac0turtle Apr 26, 2023
f6aafd0
rename comet.Service to comet.InfoService
tac0turtle Apr 28, 2023
a6f68c0
address cosmetic changes
tac0turtle Apr 30, 2023
938b449
fix evidence integration tests
tac0turtle Apr 30, 2023
5d313a1
Merge branch 'main' into marko/block_info
tac0turtle Apr 30, 2023
02e3dde
fix build
tac0turtle May 1, 2023
640086d
Merge branch 'main' into marko/block_info
tac0turtle May 2, 2023
95864c7
fix tests
tac0turtle May 2, 2023
238de6d
Merge branch 'main' into marko/block_info
tac0turtle May 3, 2023
b9f01ea
migrate evidence to use len and get
tac0turtle May 3, 2023
3a692ec
fix lint and test in evidence module
tac0turtle May 3, 2023
abf9ac7
fix integration test and go mod tidy all modules
tac0turtle May 3, 2023
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
3 changes: 3 additions & 0 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
WithHeaderHash(req.Hash)
}

// Set blockInfo for module use
app.BlockInfo = sdk.NewBlockInfo(req)

if app.beginBlocker != nil {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
var err error
res, err = app.beginBlocker(app.deliverState.ctx, req)
Expand Down
3 changes: 3 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"
"strings"

"cosmossdk.io/core/blockinfo"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
"cosmossdk.io/store"
Expand Down Expand Up @@ -145,6 +146,8 @@ type BaseApp struct {
streamingManager storetypes.StreamingManager

chainID string

BlockInfo blockinfo.Service
}

// NewBaseApp returns a reference to an initialized BaseApp. It accepts a
Expand Down
50 changes: 50 additions & 0 deletions core/blockinfo/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package blockinfo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this blockinfo guaranteed to be consistent across consensus engines and between comet and roll kit? Is this guaranteed to be stable across all versions of comet? If not, we should put it in a different package, maybe even outside of core


import "time"

type Service interface {
GetHeight() int64 // GetHeight returns the height of the block
Misbehavior() []Misbehavior // Misbehavior returns the misbehavior of the block
GetHeaderHash() []byte // GetHeaderHash returns the hash of the block header
// GetValidatorsHash returns the hash of the validators
// For Comet, it is the hash of the next validator set
GetValidatorsHash() []byte
GetProposerAddress() []byte // GetProposerAddress returns the address of the block proposer
GetDecidedLastCommit() CommitInfo // GetDecidedLastCommit returns the last commit info
}

// MisbehaviorType is the type of misbehavior for a validator
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would love feedback on defining our own types like this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me.

type MisbehaviorType int32

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

// Validator is the validator information of ABCI
type Validator struct {
Address []byte
Power int64
}

// Misbehavior is the misbehavior information of ABCI
type Misbehavior 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
SignedLastBlock bool
}
2 changes: 1 addition & 1 deletion runtime/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type BlockInfoService interface {
Misbehavior() []abci.Misbehavior // Misbehavior returns the misbehavior of the block
GetHeaderHash() []byte // GetHeaderHash returns the hash of the block header
// GetValidatorsHash returns the hash of the validators
// For Comet, it is the hash of the next validators
// For Comet, it is the hash of the next validator set
GetValidatorsHash() []byte
GetProposerAddress() []byte // GetProposerAddress returns the address of the block proposer
GetDecidedLastCommit() abci.CommitInfo // GetDecidedLastCommit returns the last commit info
Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func NewSimApp(

// create evidence keeper with router
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec, keys[evidencetypes.StoreKey], app.StakingKeeper, app.SlashingKeeper, app.AccountKeeper.GetAddressCodec(),
appCodec, keys[evidencetypes.StoreKey], app.StakingKeeper, app.SlashingKeeper, app.AccountKeeper.GetAddressCodec(), app.BlockInfo,
)
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper
Expand Down
99 changes: 99 additions & 0 deletions types/block_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package types
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to add new stuff to types. We should figure out the proper package location.


import (
"cosmossdk.io/core/blockinfo"
abci "github.com/cometbft/cometbft/abci/types"
)

// ======================================================
// BlockInfoService
// ======================================================
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved

var _ blockinfo.Service = (*BlockInfo)(nil)

type BlockInfo struct {
Height int64
Evidence []blockinfo.Misbehavior
HeaderHash []byte
ValidatorsHash []byte
ProposerAddress []byte
DecidedLastCommit blockinfo.CommitInfo
}

func NewBlockInfo(bg abci.RequestBeginBlock) *BlockInfo {
return &BlockInfo{
Height: bg.Header.Height,
Evidence: FromABCIEvidence(bg.ByzantineValidators),
HeaderHash: bg.Hash,
ValidatorsHash: bg.Header.NextValidatorsHash,
ProposerAddress: bg.Header.ProposerAddress,
DecidedLastCommit: FromABCICommitInfo(bg.LastCommitInfo),
}
}

func (bis *BlockInfo) GetHeight() int64 {
return bis.Height
}

func (bis *BlockInfo) Misbehavior() []blockinfo.Misbehavior {
return bis.Evidence
}

func (bis *BlockInfo) GetHeaderHash() []byte {
return bis.HeaderHash
}

func (bis *BlockInfo) GetValidatorsHash() []byte {
return bis.ValidatorsHash
}

func (bis *BlockInfo) GetProposerAddress() []byte {
return bis.ProposerAddress
}

func (bis *BlockInfo) GetDecidedLastCommit() blockinfo.CommitInfo {
return bis.DecidedLastCommit
}

// ======================================================
// Utils
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved

// FromABCIEvidence converts a CometBFT concrete Evidence type to
// SDK Evidence.
func FromABCIEvidence(e []abci.Misbehavior) []blockinfo.Misbehavior {

var misbehavior []blockinfo.Misbehavior
for _, ev := range e {
misbehavior = append(misbehavior, blockinfo.Misbehavior{
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
Type: blockinfo.MisbehaviorType(ev.Type),
Height: ev.Height,
Validator: blockinfo.Validator{
Address: ev.Validator.Address,
Power: ev.Validator.Power,
},
Time: ev.Time,
TotalVotingPower: ev.TotalVotingPower,
},
)
}

return misbehavior
}

func FromABCICommitInfo(ci abci.CommitInfo) blockinfo.CommitInfo {
var votes []*blockinfo.VoteInfo
for _, v := range ci.Votes {
votes = append(votes, &blockinfo.VoteInfo{
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
Validator: blockinfo.Validator{
Address: v.Validator.Address,
Power: v.Validator.Power,
},
SignedLastBlock: v.SignedLastBlock,
})
}

return blockinfo.CommitInfo{
Round: ci.Round,
Votes: votes,
}
}
2 changes: 2 additions & 0 deletions x/evidence/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,5 @@ require (
// Fix upstream GHSA-h395-qcrw-5vmq vulnerability.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
replace github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.8.1

replace cosmossdk.io/core => ../../core
12 changes: 7 additions & 5 deletions x/evidence/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package keeper

import (
"context"
"fmt"
"time"

"cosmossdk.io/core/blockinfo"
"cosmossdk.io/x/evidence/types"
abci "github.com/cometbft/cometbft/abci/types"

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

// BeginBlocker iterates through and handles any newly discovered evidence of
// misbehavior submitted by CometBFT. Currently, only equivocation is handled.
func (k Keeper) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) {
func (k Keeper) BeginBlocker(goctx context.Context) {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

for _, tmEvidence := range req.ByzantineValidators {
ctx := sdk.UnwrapSDKContext(goctx)
for _, tmEvidence := range k.blockInfo.Misbehavior() {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
switch tmEvidence.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 abci.MisbehaviorType_DUPLICATE_VOTE, abci.MisbehaviorType_LIGHT_CLIENT_ATTACK:
case blockinfo.LightClientAttack, blockinfo.DuplicateVote:
evidence := types.FromABCIEvidence(tmEvidence)
k.handleEquivocationEvidence(ctx, evidence)

default:
k.Logger(ctx).Error(fmt.Sprintf("ignored unknown evidence type: %s", tmEvidence.Type))
k.Logger(ctx).Error(fmt.Sprintf("ignored unknown evidence type: %x", tmEvidence.Type))
}
}
}
6 changes: 5 additions & 1 deletion x/evidence/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"cosmossdk.io/x/evidence/exported"
"cosmossdk.io/x/evidence/types"

"cosmossdk.io/core/blockinfo"
"cosmossdk.io/errors"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
Expand All @@ -28,19 +29,22 @@ type Keeper struct {
stakingKeeper types.StakingKeeper
slashingKeeper types.SlashingKeeper
addressCodec address.Codec

blockInfo blockinfo.Service
}

// NewKeeper creates a new Keeper object.
func NewKeeper(
cdc codec.BinaryCodec, storeKey storetypes.StoreKey, stakingKeeper types.StakingKeeper,
slashingKeeper types.SlashingKeeper, ac address.Codec,
slashingKeeper types.SlashingKeeper, ac address.Codec, bi blockinfo.Service,
) *Keeper {
return &Keeper{
cdc: cdc,
storeKey: storeKey,
stakingKeeper: stakingKeeper,
slashingKeeper: slashingKeeper,
addressCodec: ac,
blockInfo: bi,
}
}

Expand Down
17 changes: 11 additions & 6 deletions x/evidence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
modulev1 "cosmossdk.io/api/cosmos/evidence/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/blockinfo"
"cosmossdk.io/depinject"

store "cosmossdk.io/store/types"
Expand All @@ -31,7 +32,6 @@ import (
)

var (
_ module.BeginBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
Expand Down Expand Up @@ -125,8 +125,9 @@ func NewAppModule(keeper keeper.Keeper) AppModule {
}

var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasServices = AppModule{}
_ appmodule.AppModule = AppModule{}
_ appmodule.HasServices = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
)

// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
Expand Down Expand Up @@ -169,8 +170,10 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) ConsensusVersion() uint64 { return 1 }

// BeginBlock executes all ABCI BeginBlock logic respective to the evidence module.
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
am.keeper.BeginBlocker(ctx, req)
func (am AppModule) BeginBlock(ctx context.Context) error {
am.keeper.BeginBlocker(ctx)

return nil
}

// AppModuleSimulation functions
Expand Down Expand Up @@ -209,6 +212,8 @@ type ModuleInputs struct {
StakingKeeper types.StakingKeeper
SlashingKeeper types.SlashingKeeper
AddressCodec address.Codec

BlockInfoService blockinfo.Service
}

type ModuleOutputs struct {
Expand All @@ -219,7 +224,7 @@ type ModuleOutputs struct {
}

func ProvideModule(in ModuleInputs) ModuleOutputs {
k := keeper.NewKeeper(in.Cdc, in.Key, in.StakingKeeper, in.SlashingKeeper, in.AddressCodec)
k := keeper.NewKeeper(in.Cdc, in.Key, in.StakingKeeper, in.SlashingKeeper, in.AddressCodec, in.BlockInfoService)
m := NewAppModule(*k)

return ModuleOutputs{EvidenceKeeper: *k, Module: m}
Expand Down
4 changes: 2 additions & 2 deletions x/evidence/types/evidence.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"time"

"cosmossdk.io/core/blockinfo"
"cosmossdk.io/x/evidence/exported"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/tmhash"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -74,7 +74,7 @@ func (e Equivocation) GetTotalPower() int64 { return 0 }

// FromABCIEvidence converts a CometBFT concrete Evidence type to
// SDK Evidence using Equivocation as the concrete type.
func FromABCIEvidence(e abci.Misbehavior) *Equivocation {
func FromABCIEvidence(e blockinfo.Misbehavior) *Equivocation {
bech32PrefixConsAddr := sdk.GetConfig().GetBech32ConsensusAddrPrefix()
consAddr, err := sdk.Bech32ifyAddressBytes(bech32PrefixConsAddr, e.Validator.Address)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions x/evidence/types/evidence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"
"time"

"cosmossdk.io/core/blockinfo"
"cosmossdk.io/x/evidence/types"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -72,9 +72,9 @@ func TestEquivocationValidateBasic(t *testing.T) {

func TestEvidenceAddressConversion(t *testing.T) {
sdk.GetConfig().SetBech32PrefixForConsensusNode("testcnclcons", "testcnclconspub")
tmEvidence := abci.Misbehavior{
Type: abci.MisbehaviorType_DUPLICATE_VOTE,
Validator: abci.Validator{
tmEvidence := blockinfo.Misbehavior{
Type: blockinfo.DuplicateVote,
Validator: blockinfo.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,
},
Expand Down