feat: add header and comet info service (#15850)

Co-authored-by: Aaron Craelius <aaron@regen.network>
This commit is contained in:
Marko
2023-05-03 13:32:25 +00:00
committed by GitHub
co-authored by Aaron Craelius
parent 1d03f419f7
commit 1705615ef0
27 changed files with 659 additions and 107 deletions
+2 -2
View File
@@ -4,6 +4,7 @@ go 1.20
require (
cosmossdk.io/api v0.4.1
cosmossdk.io/core v0.6.2-0.20230323161322-ccd8d40119e4
cosmossdk.io/depinject v1.0.0-alpha.3
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/log v1.1.0
@@ -38,7 +39,6 @@ require (
cloud.google.com/go/storage v1.30.0 // indirect
cosmossdk.io/client/v2 v2.0.0-20230309163709-87da587416ba // indirect
cosmossdk.io/collections v0.1.0 // indirect
cosmossdk.io/core v0.6.2-0.20230323161322-ccd8d40119e4 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
@@ -193,7 +193,6 @@ require (
replace (
// TODO tag all extracted modules after SDK refactor
cosmossdk.io/api => ../api
cosmossdk.io/core => ../core
cosmossdk.io/store => ../store
cosmossdk.io/x/evidence => ../x/evidence
cosmossdk.io/x/feegrant => ../x/feegrant
@@ -206,6 +205,7 @@ replace cosmossdk.io/x/tx => ../x/tx
// Below are the long-lived replace for tests.
replace (
cosmossdk.io/core => ../core
// We always want to test against the latest version of the simapp.
cosmossdk.io/simapp => ../simapp
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
@@ -171,7 +171,7 @@ func TestGRPCValidatorCommission(t *testing.T) {
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
tstaking.CreateValidator(f.valAddr, valConsPk0, sdk.NewInt(initialStake), true)
commission := sdk.DecCoins{{Denom: "token1", Amount: math.LegacyNewDec(4)}, {Denom: "token2", Amount: math.LegacyNewDec(2)}}
commission := sdk.DecCoins{sdk.DecCoin{Denom: "token1", Amount: math.LegacyNewDec(4)}, {Denom: "token2", Amount: math.LegacyNewDec(2)}}
f.distrKeeper.SetValidatorAccumulatedCommission(f.sdkCtx, f.valAddr, types.ValidatorAccumulatedCommission{Commission: commission})
testCases := []struct {
@@ -521,7 +521,7 @@ func TestGRPCDelegationRewards(t *testing.T) {
f.distrKeeper.SetValidatorOutstandingRewards(f.sdkCtx, f.valAddr, types.ValidatorOutstandingRewards{Rewards: decCoins})
expRes := &types.QueryDelegationRewardsResponse{
Rewards: sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initialStake / 10)}},
Rewards: sdk.DecCoins{sdk.DecCoin{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initialStake / 10)}},
}
// test command delegation rewards grpc
@@ -6,6 +6,7 @@ import (
"testing"
"time"
"cosmossdk.io/core/comet"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/x/evidence/exported"
@@ -117,16 +118,19 @@ func TestHandleDoubleSign(t *testing.T) {
// double sign less than max age
oldTokens := f.stakingKeeper.Validator(ctx, operatorAddr).GetTokens()
evidence := abci.RequestBeginBlock{
nci := NewCometInfo(abci.RequestBeginBlock{
ByzantineValidators: []abci.Misbehavior{{
Validator: abci.Validator{Address: val.Address(), Power: power},
Type: abci.MisbehaviorType_DUPLICATE_VOTE,
Time: time.Unix(0, 0),
Height: 0,
}},
}
})
f.evidenceKeeper.BeginBlocker(ctx, evidence)
ctx = ctx.WithCometInfo(nci)
f.evidenceKeeper.BeginBlocker(ctx)
// should be jailed and tombstoned
assert.Assert(t, f.stakingKeeper.Validator(ctx, operatorAddr).IsJailed())
@@ -137,7 +141,7 @@ func TestHandleDoubleSign(t *testing.T) {
assert.Assert(t, newTokens.LT(oldTokens))
// submit duplicate evidence
f.evidenceKeeper.BeginBlocker(ctx, evidence)
f.evidenceKeeper.BeginBlocker(ctx)
// tokens should be the same (capped slash)
assert.Assert(t, f.stakingKeeper.Validator(ctx, operatorAddr).GetTokens().Equal(newTokens))
@@ -184,22 +188,23 @@ func TestHandleDoubleSign_TooOld(t *testing.T) {
)
assert.DeepEqual(t, amt, f.stakingKeeper.Validator(ctx, operatorAddr).GetBondedTokens())
evidence := abci.RequestBeginBlock{
nci := NewCometInfo(abci.RequestBeginBlock{
ByzantineValidators: []abci.Misbehavior{{
Validator: abci.Validator{Address: val.Address(), Power: power},
Type: abci.MisbehaviorType_DUPLICATE_VOTE,
Time: ctx.BlockTime(),
Height: 0,
}},
}
})
cp := f.app.BaseApp.GetConsensusParams(ctx)
ctx = ctx.WithCometInfo(nci)
ctx = ctx.WithConsensusParams(cp)
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(cp.Evidence.MaxAgeDuration + 1))
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + cp.Evidence.MaxAgeNumBlocks + 1)
f.evidenceKeeper.BeginBlocker(ctx, evidence)
f.evidenceKeeper.BeginBlocker(ctx)
assert.Assert(t, f.stakingKeeper.Validator(ctx, operatorAddr).IsJailed() == false)
assert.Assert(t, f.slashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(val.Address())) == false)
@@ -244,3 +249,80 @@ func testEquivocationHandler(_ interface{}) types.Handler {
return nil
}
}
type CometService struct {
Evidence []abci.Misbehavior
}
func NewCometInfo(bg abci.RequestBeginBlock) comet.BlockInfo {
return CometService{
Evidence: bg.ByzantineValidators,
}
}
func (r CometService) GetEvidence() comet.EvidenceList {
return evidenceWrapper{evidence: r.Evidence}
}
func (CometService) GetValidatorsHash() []byte {
return []byte{}
}
func (CometService) GetProposerAddress() []byte {
return []byte{}
}
func (CometService) GetLastCommit() comet.CommitInfo {
return nil
}
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]}
}
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
}
// 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
}