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(x/slashing)!: use kvStoreService, context.Context and return errors #16246

Merged
merged 20 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
12 changes: 9 additions & 3 deletions x/slashing/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slashing

import (
"context"
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
Expand All @@ -11,13 +12,18 @@ import (

// BeginBlocker check for infraction evidence or downtime of validators
// on every begin block
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
func BeginBlocker(ctx context.Context, k keeper.Keeper) error {
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 ctx.VoteInfos() {
k.HandleValidatorSignature(ctx, voteInfo.Validator.Address, voteInfo.Validator.Power, voteInfo.SignedLastBlock)
sdkCtx := sdk.UnwrapSDKContext(ctx)
for _, voteInfo := range sdkCtx.VoteInfos() {
err := k.HandleValidatorSignature(ctx, voteInfo.Validator.Address, voteInfo.Validator.Power, voteInfo.SignedLastBlock)
Fixed Show fixed Hide fixed
if err != nil {
return err
}
}
return nil
}
24 changes: 16 additions & 8 deletions x/slashing/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,41 +67,49 @@ func TestBeginBlocker(t *testing.T) {
SignedLastBlock: true,
}})

slashing.BeginBlocker(ctx, slashingKeeper)
err = slashing.BeginBlocker(ctx, slashingKeeper)
require.NoError(t, err)

info, found := slashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
require.True(t, found)
info, err := slashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
require.NoError(t, err)
require.Equal(t, ctx.BlockHeight(), info.StartHeight)
require.Equal(t, int64(1), info.IndexOffset)
require.Equal(t, time.Unix(0, 0).UTC(), info.JailedUntil)
require.Equal(t, int64(0), info.MissedBlocksCounter)

height := int64(0)

signedBlocksWindow, err := slashingKeeper.SignedBlocksWindow(ctx)
require.NoError(t, err)
// for 1000 blocks, mark the validator as having signed
for ; height < slashingKeeper.SignedBlocksWindow(ctx); height++ {
for ; height < signedBlocksWindow; height++ {
ctx = ctx.WithBlockHeight(height).
WithVoteInfos([]abci.VoteInfo{{
Validator: val,
SignedLastBlock: true,
}})

slashing.BeginBlocker(ctx, slashingKeeper)
err = slashing.BeginBlocker(ctx, slashingKeeper)
require.NoError(t, err)
}

minSignedPerWindow, err := slashingKeeper.MinSignedPerWindow(ctx)
require.NoError(t, err)
// for 500 blocks, mark the validator as having not signed
for ; height < ((slashingKeeper.SignedBlocksWindow(ctx) * 2) - slashingKeeper.MinSignedPerWindow(ctx) + 1); height++ {
for ; height < ((signedBlocksWindow * 2) - minSignedPerWindow + 1); height++ {
ctx = ctx.WithBlockHeight(height).
WithVoteInfos([]abci.VoteInfo{{
Validator: val,
SignedLastBlock: false,
}})

slashing.BeginBlocker(ctx, slashingKeeper)
err = slashing.BeginBlocker(ctx, slashingKeeper)
require.NoError(t, err)
}

// end block
stakingKeeper.EndBlocker(ctx)
_, err = stakingKeeper.EndBlocker(ctx)
require.NoError(t, err)

// validator should be jailed
validator, found := stakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func TestSlashingMsgs(t *testing.T) {
unjailMsg := &types.MsgUnjail{ValidatorAddr: sdk.ValAddress(addr1).String()}

ctxCheck = app.BaseApp.NewContext(true, cmtproto.Header{})
_, found = slashingKeeper.GetValidatorSigningInfo(ctxCheck, sdk.ConsAddress(valAddr))
require.True(t, found)
_, err = slashingKeeper.GetValidatorSigningInfo(ctxCheck, sdk.ConsAddress(valAddr))
require.NoError(t, err)

// unjail should fail with unknown validator
header = cmtproto.Header{Height: app.LastBlockHeight() + 1}
Expand Down
10 changes: 8 additions & 2 deletions x/slashing/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ func (keeper Keeper) InitGenesis(ctx sdk.Context, stakingKeeper types.StakingKee
// to a genesis file, which can be imported again
// with InitGenesis
func (keeper Keeper) ExportGenesis(ctx sdk.Context) (data *types.GenesisState) {
params := keeper.GetParams(ctx)
params, err := keeper.GetParams(ctx)
if err != nil {
panic(err)
}
signingInfos := make([]types.SigningInfo, 0)
missedBlocks := make([]types.ValidatorMissedBlocks, 0)
keeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) {
Expand All @@ -61,7 +64,10 @@ func (keeper Keeper) ExportGenesis(ctx sdk.Context) (data *types.GenesisState) {
ValidatorSigningInfo: info,
})

localMissedBlocks := keeper.GetValidatorMissedBlocks(ctx, address)
localMissedBlocks, err := keeper.GetValidatorMissedBlocks(ctx, address)
if err != nil {
panic(err)
}

missedBlocks = append(missedBlocks, types.ValidatorMissedBlocks{
Address: bechAddr,
Expand Down
6 changes: 4 additions & 2 deletions x/slashing/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ func (s *KeeperTestSuite) TestExportAndInitGenesis() {
require.Equal(genesisState.SigningInfos[0].ValidatorSigningInfo, info1)

// Tombstone validators after genesis shouldn't effect genesis state
keeper.Tombstone(ctx, consAddr1)
keeper.Tombstone(ctx, consAddr2)
err := keeper.Tombstone(ctx, consAddr1)
require.NoError(err)
err = keeper.Tombstone(ctx, consAddr2)
require.NoError(err)

ok := keeper.IsTombstoned(ctx, consAddr1)
require.True(ok)
Expand Down
22 changes: 10 additions & 12 deletions x/slashing/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"cosmossdk.io/store/prefix"

"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
Expand All @@ -24,19 +25,18 @@ func NewQuerier(keeper Keeper) Querier {
}

// Params returns parameters of x/slashing module
func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
func (k Keeper) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)
params := k.GetParams(ctx)
params, err := k.GetParams(ctx)

return &types.QueryParamsResponse{Params: params}, nil
return &types.QueryParamsResponse{Params: params}, err
}

// SigningInfo returns signing-info of a specific validator.
func (k Keeper) SigningInfo(c context.Context, req *types.QuerySigningInfoRequest) (*types.QuerySigningInfoResponse, error) {
func (k Keeper) SigningInfo(ctx context.Context, req *types.QuerySigningInfoRequest) (*types.QuerySigningInfoResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
Expand All @@ -50,26 +50,24 @@ func (k Keeper) SigningInfo(c context.Context, req *types.QuerySigningInfoReques
return nil, err
}

ctx := sdk.UnwrapSDKContext(c)
signingInfo, found := k.GetValidatorSigningInfo(ctx, consAddr)
if !found {
signingInfo, err := k.GetValidatorSigningInfo(ctx, consAddr)
if err != nil {
return nil, status.Errorf(codes.NotFound, "SigningInfo not found for validator %s", req.ConsAddress)
}

return &types.QuerySigningInfoResponse{ValSigningInfo: signingInfo}, nil
}

// SigningInfos returns signing-infos of all validators.
func (k Keeper) SigningInfos(c context.Context, req *types.QuerySigningInfosRequest) (*types.QuerySigningInfosResponse, error) {
func (k Keeper) SigningInfos(ctx context.Context, req *types.QuerySigningInfosRequest) (*types.QuerySigningInfosResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(k.storeKey)
store := k.storeService.OpenKVStore(ctx)
var signInfos []types.ValidatorSigningInfo

sigInfoStore := prefix.NewStore(store, types.ValidatorSigningInfoKeyPrefix)
sigInfoStore := prefix.NewStore(runtime.KVStoreAdapter(store), types.ValidatorSigningInfoKeyPrefix)
pageRes, err := query.Paginate(sigInfoStore, req.Pagination, func(key, value []byte) error {
var info types.ValidatorSigningInfo
err := k.cdc.Unmarshal(value, &info)
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func (s *KeeperTestSuite) TestGRPCSigningInfo() {
)

keeper.SetValidatorSigningInfo(ctx, consAddr, signingInfo)
info, found := keeper.GetValidatorSigningInfo(ctx, consAddr)
require.True(found)
info, err := keeper.GetValidatorSigningInfo(ctx, consAddr)
require.NoError(err)

infoResp, err = queryClient.SigningInfo(gocontext.Background(),
&slashingtypes.QuerySigningInfoRequest{ConsAddress: consAddr.String()})
Expand Down
11 changes: 4 additions & 7 deletions x/slashing/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (k Keeper) Hooks() Hooks {

// AfterValidatorBonded updates the signing info start height or create a new signing info
func (h Hooks) AfterValidatorBonded(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
signingInfo, found := h.k.GetValidatorSigningInfo(ctx, consAddr)
if found {
signingInfo, err := h.k.GetValidatorSigningInfo(ctx, consAddr)
if err == nil {
signingInfo.StartHeight = ctx.BlockHeight()
} else {
signingInfo = types.NewValidatorSigningInfo(
Expand All @@ -38,15 +38,12 @@ func (h Hooks) AfterValidatorBonded(ctx sdk.Context, consAddr sdk.ConsAddress, v
)
}

h.k.SetValidatorSigningInfo(ctx, consAddr, signingInfo)

return nil
return h.k.SetValidatorSigningInfo(ctx, consAddr, signingInfo)
}

// AfterValidatorRemoved deletes the address-pubkey relation when a validator is removed,
func (h Hooks) AfterValidatorRemoved(ctx sdk.Context, consAddr sdk.ConsAddress, _ sdk.ValAddress) error {
h.k.deleteAddrPubkeyRelation(ctx, crypto.Address(consAddr))
return nil
return h.k.deleteAddrPubkeyRelation(ctx, crypto.Address(consAddr))
}

// AfterValidatorCreated adds the address-pubkey relation when a validator is created.
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func (s *KeeperTestSuite) TestAfterValidatorBonded() {
valAddr := sdk.ValAddress(consAddr.Bytes())
keeper.Hooks().AfterValidatorBonded(ctx, consAddr, valAddr)

_, ok := keeper.GetValidatorSigningInfo(ctx, consAddr)
require.True(ok)
_, err := keeper.GetValidatorSigningInfo(ctx, consAddr)
require.NoError(err)
}

func (s *KeeperTestSuite) TestAfterValidatorCreatedOrRemoved() {
Expand Down
Loading