refactor: Use CoreAPI when possible (#15496)

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Marko <marbar3778@yahoo.com>
This commit is contained in:
Facundo Medica
2023-03-23 22:40:09 +00:00
committed by GitHub
co-authored by Aleksandr Bezobchuk Marko
parent b44caab53a
commit 05dcf860eb
22 changed files with 178 additions and 163 deletions
+2 -4
View File
@@ -3,8 +3,6 @@ package slashing
import (
"time"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
@@ -13,13 +11,13 @@ import (
// BeginBlocker check for infraction evidence or downtime of validators
// on every begin block
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
// Iterate over all the validators which *should* have signed this block
// store whether or not they have actually signed it and slash/unbond any
// which have missed too many blocks in a row (downtime slashing)
for _, voteInfo := range req.LastCommitInfo.GetVotes() {
for _, voteInfo := range ctx.VoteInfos() {
k.HandleValidatorSignature(ctx, voteInfo.Validator.Address, voteInfo.Validator.Power, voteInfo.SignedLastBlock)
}
}
+17 -30
View File
@@ -58,17 +58,12 @@ func TestBeginBlocker(t *testing.T) {
Power: power,
}
// mark the validator as having signed
req := abci.RequestBeginBlock{
LastCommitInfo: abci.CommitInfo{
Votes: []abci.VoteInfo{{
Validator: val,
SignedLastBlock: true,
}},
},
}
ctx = ctx.WithVoteInfos([]abci.VoteInfo{{
Validator: val,
SignedLastBlock: true,
}})
slashing.BeginBlocker(ctx, req, slashingKeeper)
slashing.BeginBlocker(ctx, slashingKeeper)
info, found := slashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
require.True(t, found)
@@ -81,32 +76,24 @@ func TestBeginBlocker(t *testing.T) {
// for 1000 blocks, mark the validator as having signed
for ; height < slashingKeeper.SignedBlocksWindow(ctx); height++ {
ctx = ctx.WithBlockHeight(height)
req = abci.RequestBeginBlock{
LastCommitInfo: abci.CommitInfo{
Votes: []abci.VoteInfo{{
Validator: val,
SignedLastBlock: true,
}},
},
}
ctx = ctx.WithBlockHeight(height).
WithVoteInfos([]abci.VoteInfo{{
Validator: val,
SignedLastBlock: true,
}})
slashing.BeginBlocker(ctx, req, slashingKeeper)
slashing.BeginBlocker(ctx, slashingKeeper)
}
// for 500 blocks, mark the validator as having not signed
for ; height < ((slashingKeeper.SignedBlocksWindow(ctx) * 2) - slashingKeeper.MinSignedPerWindow(ctx) + 1); height++ {
ctx = ctx.WithBlockHeight(height)
req = abci.RequestBeginBlock{
LastCommitInfo: abci.CommitInfo{
Votes: []abci.VoteInfo{{
Validator: val,
SignedLastBlock: false,
}},
},
}
ctx = ctx.WithBlockHeight(height).
WithVoteInfos([]abci.VoteInfo{{
Validator: val,
SignedLastBlock: false,
}})
slashing.BeginBlocker(ctx, req, slashingKeeper)
slashing.BeginBlocker(ctx, slashingKeeper)
}
// end block
+8 -4
View File
@@ -35,7 +35,6 @@ import (
const ConsensusVersion = 3
var (
_ module.BeginBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
@@ -120,7 +119,10 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper,
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@@ -168,8 +170,10 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
// BeginBlock returns the begin blocker for the slashing module.
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
BeginBlocker(ctx, req, am.keeper)
func (am AppModule) BeginBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
BeginBlocker(c, am.keeper)
return nil
}
// AppModuleSimulation functions