refactor(x/slashing): migrate ValidatorSigningInfo to collections (#17023)
This commit is contained in:
parent
0c4f246d86
commit
a61e11ebab
@ -60,6 +60,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
||||
|
||||
### API Breaking Changes
|
||||
|
||||
* (x/slashing) [17023](https://github.com/cosmos/cosmos-sdk/pull/17023) Use collections for `ValidatorSigningInfo`:
|
||||
* remove `Keeper`: `SetValidatorSigningInfo`, `GetValidatorSigningInfo`, `IterateValidatorSigningInfos`
|
||||
* (x/staking) [#17026](https://github.com/cosmos/cosmos-sdk/pull/17026) Use collections for `LastTotalPower`:
|
||||
* remove `Keeper`: `SetLastTotalPower`, `GetLastTotalPower`
|
||||
* (staking) [#16959](https://github.com/cosmos/cosmos-sdk/pull/16959) Add validator and consensus address codec as staking keeper arguments.
|
||||
|
||||
@ -2,11 +2,13 @@ package simapp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
||||
|
||||
"cosmossdk.io/collections"
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
servertypes "github.com/cosmos/cosmos-sdk/server/types"
|
||||
@ -230,18 +232,15 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []
|
||||
/* Handle slashing state. */
|
||||
|
||||
// reset start height on signing infos
|
||||
err = app.SlashingKeeper.IterateValidatorSigningInfos(
|
||||
ctx,
|
||||
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
|
||||
info.StartHeight = 0
|
||||
err := app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return false
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
err = app.SlashingKeeper.ValidatorSigningInfo.Walk(ctx, nil, func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool, err error) {
|
||||
info.StartHeight = 0
|
||||
err = app.SlashingKeeper.ValidatorSigningInfo.Set(ctx, addr, info)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
return false, nil
|
||||
})
|
||||
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -195,7 +195,7 @@ func TestHandleDoubleSign(t *testing.T) {
|
||||
assert.NilError(t, f.slashingKeeper.AddPubkey(f.sdkCtx, valpubkey))
|
||||
|
||||
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(valpubkey.Address()), f.sdkCtx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
|
||||
err = f.slashingKeeper.SetValidatorSigningInfo(f.sdkCtx, sdk.ConsAddress(valpubkey.Address()), info)
|
||||
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.sdkCtx, sdk.ConsAddress(valpubkey.Address()), info)
|
||||
assert.NilError(t, err)
|
||||
// handle a signature to set signing info
|
||||
err = f.slashingKeeper.HandleValidatorSignature(ctx, valpubkey.Address(), selfDelegation.Int64(), comet.BlockIDFlagCommit)
|
||||
|
||||
@ -124,9 +124,9 @@ func initFixture(tb testing.TB) *fixture {
|
||||
info1 := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[0]), int64(4), int64(3), time.Unix(2, 0), false, int64(10))
|
||||
info2 := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[1]), int64(5), int64(4), time.Unix(2, 0), false, int64(10))
|
||||
|
||||
err = slashingKeeper.SetValidatorSigningInfo(sdkCtx, sdk.ConsAddress(addrDels[0]), info1)
|
||||
err = slashingKeeper.ValidatorSigningInfo.Set(sdkCtx, sdk.ConsAddress(addrDels[0]), info1)
|
||||
assert.NilError(tb, err)
|
||||
err = slashingKeeper.SetValidatorSigningInfo(sdkCtx, sdk.ConsAddress(addrDels[1]), info2)
|
||||
err = slashingKeeper.ValidatorSigningInfo.Set(sdkCtx, sdk.ConsAddress(addrDels[1]), info2)
|
||||
assert.NilError(tb, err)
|
||||
return &fixture{
|
||||
app: integrationApp,
|
||||
@ -236,7 +236,7 @@ func TestHandleNewValidator(t *testing.T) {
|
||||
assert.NilError(t, f.slashingKeeper.AddPubkey(f.ctx, pks[0]))
|
||||
|
||||
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(valpubkey.Address()), f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
|
||||
assert.NilError(t, f.slashingKeeper.SetValidatorSigningInfo(f.ctx, sdk.ConsAddress(valpubkey.Address()), info))
|
||||
assert.NilError(t, f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, sdk.ConsAddress(valpubkey.Address()), info))
|
||||
|
||||
// Validator created
|
||||
amt := tstaking.CreateValidatorWithValPower(addr, valpubkey, 100, true)
|
||||
@ -261,7 +261,7 @@ func TestHandleNewValidator(t *testing.T) {
|
||||
f.ctx = f.ctx.WithBlockHeight(signedBlocksWindow + 2)
|
||||
assert.NilError(t, f.slashingKeeper.HandleValidatorSignature(f.ctx, valpubkey.Address(), 100, comet.BlockIDFlagAbsent))
|
||||
|
||||
info, found := f.slashingKeeper.GetValidatorSigningInfo(f.ctx, sdk.ConsAddress(valpubkey.Address()))
|
||||
info, found := f.slashingKeeper.ValidatorSigningInfo.Get(f.ctx, sdk.ConsAddress(valpubkey.Address()))
|
||||
assert.Assert(t, found)
|
||||
assert.Equal(t, signedBlocksWindow+1, info.StartHeight)
|
||||
assert.Equal(t, int64(2), info.IndexOffset)
|
||||
@ -291,7 +291,7 @@ func TestHandleAlreadyJailed(t *testing.T) {
|
||||
assert.NilError(t, err)
|
||||
|
||||
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(val.Address()), f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
|
||||
assert.NilError(t, f.slashingKeeper.SetValidatorSigningInfo(f.ctx, sdk.ConsAddress(val.Address()), info))
|
||||
assert.NilError(t, f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, sdk.ConsAddress(val.Address()), info))
|
||||
|
||||
amt := tstaking.CreateValidatorWithValPower(addr, val, power, true)
|
||||
|
||||
@ -365,7 +365,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
|
||||
assert.NilError(t, f.slashingKeeper.AddPubkey(f.ctx, pks[0]))
|
||||
|
||||
info := slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
|
||||
assert.NilError(t, f.slashingKeeper.SetValidatorSigningInfo(f.ctx, consAddr, info))
|
||||
assert.NilError(t, f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, consAddr, info))
|
||||
|
||||
tstaking.CreateValidatorWithValPower(valAddr, val, power, true)
|
||||
validatorUpdates, err := f.stakingKeeper.EndBlocker(f.ctx)
|
||||
@ -426,11 +426,11 @@ func TestValidatorDippingInAndOut(t *testing.T) {
|
||||
tstaking.CheckValidator(valAddr, stakingtypes.Unbonding, true)
|
||||
|
||||
info = slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
|
||||
err = f.slashingKeeper.SetValidatorSigningInfo(f.ctx, consAddr, info)
|
||||
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, consAddr, info)
|
||||
assert.NilError(t, err)
|
||||
|
||||
// check all the signing information
|
||||
signInfo, found := f.slashingKeeper.GetValidatorSigningInfo(f.ctx, consAddr)
|
||||
signInfo, found := f.slashingKeeper.ValidatorSigningInfo.Get(f.ctx, consAddr)
|
||||
assert.Assert(t, found)
|
||||
assert.Equal(t, int64(700), signInfo.StartHeight)
|
||||
assert.Equal(t, int64(0), signInfo.MissedBlocksCounter)
|
||||
@ -441,7 +441,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
|
||||
f.ctx = f.ctx.WithBlockHeight(height)
|
||||
|
||||
info = slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
|
||||
err = f.slashingKeeper.SetValidatorSigningInfo(f.ctx, consAddr, info)
|
||||
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, consAddr, info)
|
||||
assert.NilError(t, err)
|
||||
|
||||
// validator rejoins and starts signing again
|
||||
@ -456,7 +456,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
|
||||
tstaking.CheckValidator(valAddr, stakingtypes.Bonded, false)
|
||||
|
||||
// check start height is correctly set
|
||||
signInfo, found = f.slashingKeeper.GetValidatorSigningInfo(f.ctx, consAddr)
|
||||
signInfo, found = f.slashingKeeper.ValidatorSigningInfo.Get(f.ctx, consAddr)
|
||||
assert.Assert(t, found)
|
||||
assert.Equal(t, height, signInfo.StartHeight)
|
||||
|
||||
|
||||
@ -76,7 +76,7 @@ func TestBeginBlocker(t *testing.T) {
|
||||
err = slashing.BeginBlocker(ctx, slashingKeeper)
|
||||
require.NoError(t, err)
|
||||
|
||||
info, err := slashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
|
||||
info, err := slashingKeeper.ValidatorSigningInfo.Get(ctx, sdk.ConsAddress(pk.Address()))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, ctx.BlockHeight(), info.StartHeight)
|
||||
require.Equal(t, int64(1), info.IndexOffset)
|
||||
|
||||
@ -103,7 +103,7 @@ func TestSlashingMsgs(t *testing.T) {
|
||||
unjailMsg := &types.MsgUnjail{ValidatorAddr: sdk.ValAddress(addr1).String()}
|
||||
|
||||
ctxCheck = app.BaseApp.NewContext(true)
|
||||
_, err = slashingKeeper.GetValidatorSigningInfo(ctxCheck, sdk.ConsAddress(valAddr))
|
||||
_, err = slashingKeeper.ValidatorSigningInfo.Get(ctxCheck, sdk.ConsAddress(valAddr))
|
||||
require.NoError(t, err)
|
||||
|
||||
// unjail should fail with unknown validator
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"cosmossdk.io/collections"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
@ -32,7 +36,7 @@ func (keeper Keeper) InitGenesis(ctx sdk.Context, stakingKeeper types.StakingKee
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = keeper.SetValidatorSigningInfo(ctx, address, info.ValidatorSigningInfo)
|
||||
err = keeper.ValidatorSigningInfo.Set(ctx, address, info.ValidatorSigningInfo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -66,7 +70,7 @@ func (keeper Keeper) ExportGenesis(ctx sdk.Context) (data *types.GenesisState) {
|
||||
}
|
||||
signingInfos := make([]types.SigningInfo, 0)
|
||||
missedBlocks := make([]types.ValidatorMissedBlocks, 0)
|
||||
err = keeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) {
|
||||
err = keeper.ValidatorSigningInfo.Walk(ctx, nil, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool, err error) {
|
||||
bechAddr := address.String()
|
||||
signingInfos = append(signingInfos, types.SigningInfo{
|
||||
Address: bechAddr,
|
||||
@ -83,9 +87,9 @@ func (keeper Keeper) ExportGenesis(ctx sdk.Context) (data *types.GenesisState) {
|
||||
MissedBlocks: localMissedBlocks,
|
||||
})
|
||||
|
||||
return false
|
||||
return false, nil
|
||||
})
|
||||
if err != nil {
|
||||
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) {
|
||||
panic(err)
|
||||
}
|
||||
return types.NewGenesisState(params, signingInfos, missedBlocks)
|
||||
|
||||
@ -15,16 +15,16 @@ func (s *KeeperTestSuite) TestExportAndInitGenesis() {
|
||||
require := s.Require()
|
||||
err := keeper.Params.Set(ctx, testutil.TestParams())
|
||||
s.Require().NoError(err)
|
||||
consAddr1 := sdk.ConsAddress(sdk.AccAddress([]byte("addr1_______________")))
|
||||
consAddr2 := sdk.ConsAddress(sdk.AccAddress([]byte("addr2_______________")))
|
||||
consAddr1 := sdk.ConsAddress(([]byte("addr1_______________")))
|
||||
consAddr2 := sdk.ConsAddress(([]byte("addr2_______________")))
|
||||
|
||||
info1 := types.NewValidatorSigningInfo(consAddr1, int64(4), int64(3),
|
||||
time.Now().UTC().Add(100000000000), false, int64(10))
|
||||
info2 := types.NewValidatorSigningInfo(consAddr2, int64(5), int64(4),
|
||||
time.Now().UTC().Add(10000000000), false, int64(10))
|
||||
|
||||
s.Require().NoError(keeper.SetValidatorSigningInfo(ctx, consAddr1, info1))
|
||||
s.Require().NoError(keeper.SetValidatorSigningInfo(ctx, consAddr2, info2))
|
||||
s.Require().NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr1, info1))
|
||||
s.Require().NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr2, info2))
|
||||
genesisState := keeper.ExportGenesis(ctx)
|
||||
|
||||
require.Equal(genesisState.Params, testutil.TestParams())
|
||||
@ -40,7 +40,7 @@ func (s *KeeperTestSuite) TestExportAndInitGenesis() {
|
||||
ok := keeper.IsTombstoned(ctx, consAddr1)
|
||||
require.True(ok)
|
||||
|
||||
newInfo1, _ := keeper.GetValidatorSigningInfo(ctx, consAddr1)
|
||||
newInfo1, _ := keeper.ValidatorSigningInfo.Get(ctx, consAddr1)
|
||||
require.NotEqual(info1, newInfo1)
|
||||
|
||||
// Initialize genesis with genesis state before tombstone
|
||||
@ -51,8 +51,8 @@ func (s *KeeperTestSuite) TestExportAndInitGenesis() {
|
||||
ok = keeper.IsTombstoned(ctx, consAddr1)
|
||||
require.False(ok)
|
||||
|
||||
newInfo1, _ = keeper.GetValidatorSigningInfo(ctx, consAddr1)
|
||||
newInfo2, _ := keeper.GetValidatorSigningInfo(ctx, consAddr2)
|
||||
newInfo1, _ = keeper.ValidatorSigningInfo.Get(ctx, consAddr1)
|
||||
newInfo2, _ := keeper.ValidatorSigningInfo.Get(ctx, consAddr2)
|
||||
require.Equal(info1, newInfo1)
|
||||
require.Equal(info2, newInfo2)
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ func (k Keeper) SigningInfo(ctx context.Context, req *types.QuerySigningInfoRequ
|
||||
return nil, err
|
||||
}
|
||||
|
||||
signingInfo, err := k.GetValidatorSigningInfo(ctx, consAddr)
|
||||
signingInfo, err := k.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.NotFound, "SigningInfo not found for validator %s", req.ConsAddress)
|
||||
}
|
||||
|
||||
@ -38,8 +38,8 @@ func (s *KeeperTestSuite) TestGRPCSigningInfo() {
|
||||
int64(0),
|
||||
)
|
||||
|
||||
require.NoError(keeper.SetValidatorSigningInfo(ctx, consAddr, signingInfo))
|
||||
info, err := keeper.GetValidatorSigningInfo(ctx, consAddr)
|
||||
require.NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr, signingInfo))
|
||||
info, err := keeper.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
require.NoError(err)
|
||||
|
||||
infoResp, err = queryClient.SigningInfo(gocontext.Background(),
|
||||
@ -64,14 +64,14 @@ func (s *KeeperTestSuite) TestGRPCSigningInfos() {
|
||||
int64(0),
|
||||
)
|
||||
|
||||
require.NoError(keeper.SetValidatorSigningInfo(ctx, consAddr1, signingInfo))
|
||||
require.NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr1, signingInfo))
|
||||
signingInfo.Address = string(consAddr2)
|
||||
require.NoError(keeper.SetValidatorSigningInfo(ctx, consAddr2, signingInfo))
|
||||
require.NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr2, signingInfo))
|
||||
var signingInfos []slashingtypes.ValidatorSigningInfo
|
||||
|
||||
err := keeper.IterateValidatorSigningInfos(ctx, func(consAddr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
|
||||
err := keeper.ValidatorSigningInfo.Walk(ctx, nil, func(consAddr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool, err error) {
|
||||
signingInfos = append(signingInfos, info)
|
||||
return false
|
||||
return false, nil
|
||||
})
|
||||
require.NoError(err)
|
||||
// verify all values are returned without pagination
|
||||
|
||||
@ -27,7 +27,7 @@ func (k Keeper) Hooks() Hooks {
|
||||
// AfterValidatorBonded updates the signing info start height or create a new signing info
|
||||
func (h Hooks) AfterValidatorBonded(ctx context.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
|
||||
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
||||
signingInfo, err := h.k.GetValidatorSigningInfo(ctx, consAddr)
|
||||
signingInfo, err := h.k.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
if err == nil {
|
||||
signingInfo.StartHeight = sdkCtx.BlockHeight()
|
||||
} else {
|
||||
@ -41,7 +41,7 @@ func (h Hooks) AfterValidatorBonded(ctx context.Context, consAddr sdk.ConsAddres
|
||||
)
|
||||
}
|
||||
|
||||
return h.k.SetValidatorSigningInfo(ctx, consAddr, signingInfo)
|
||||
return h.k.ValidatorSigningInfo.Set(ctx, consAddr, signingInfo)
|
||||
}
|
||||
|
||||
// AfterValidatorRemoved deletes the address-pubkey relation when a validator is removed,
|
||||
|
||||
@ -13,7 +13,7 @@ func (s *KeeperTestSuite) TestAfterValidatorBonded() {
|
||||
valAddr := sdk.ValAddress(consAddr.Bytes())
|
||||
err := keeper.Hooks().AfterValidatorBonded(ctx, consAddr, valAddr)
|
||||
require.NoError(err)
|
||||
_, err = keeper.GetValidatorSigningInfo(ctx, consAddr)
|
||||
_, err = keeper.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
require.NoError(err)
|
||||
}
|
||||
|
||||
|
||||
@ -34,7 +34,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
|
||||
}
|
||||
|
||||
// fetch signing info
|
||||
signInfo, err := k.GetValidatorSigningInfo(ctx, consAddr)
|
||||
signInfo, err := k.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -182,5 +182,5 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
|
||||
}
|
||||
|
||||
// Set the updated signing info
|
||||
return k.SetValidatorSigningInfo(ctx, consAddr, signInfo)
|
||||
return k.ValidatorSigningInfo.Set(ctx, consAddr, signInfo)
|
||||
}
|
||||
|
||||
@ -25,9 +25,10 @@ type Keeper struct {
|
||||
|
||||
// the address capable of executing a MsgUpdateParams message. Typically, this
|
||||
// should be the x/gov module account.
|
||||
authority string
|
||||
Schema collections.Schema
|
||||
Params collections.Item[types.Params]
|
||||
authority string
|
||||
Schema collections.Schema
|
||||
Params collections.Item[types.Params]
|
||||
ValidatorSigningInfo collections.Map[sdk.ConsAddress, types.ValidatorSigningInfo]
|
||||
}
|
||||
|
||||
// NewKeeper creates a slashing keeper
|
||||
@ -40,6 +41,13 @@ func NewKeeper(cdc codec.BinaryCodec, legacyAmino *codec.LegacyAmino, storeServi
|
||||
sk: sk,
|
||||
authority: authority,
|
||||
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
|
||||
ValidatorSigningInfo: collections.NewMap(
|
||||
sb,
|
||||
types.ValidatorSigningInfoKeyPrefix,
|
||||
"validator_signing_info",
|
||||
sdk.ConsAddressKey,
|
||||
codec.CollValue[types.ValidatorSigningInfo](cdc),
|
||||
),
|
||||
}
|
||||
|
||||
schema, err := sb.Build()
|
||||
|
||||
@ -211,7 +211,7 @@ func (s *KeeperTestSuite) TestUnjail() {
|
||||
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addr), int64(4), int64(3),
|
||||
time.Unix(2, 0), false, int64(10))
|
||||
|
||||
s.Require().NoError(s.slashingKeeper.SetValidatorSigningInfo(s.ctx, sdk.ConsAddress(addr), info))
|
||||
s.Require().NoError(s.slashingKeeper.ValidatorSigningInfo.Set(s.ctx, sdk.ConsAddress(addr), info))
|
||||
s.stakingKeeper.EXPECT().Validator(s.ctx, valAddr).Return(val, nil)
|
||||
del := types.NewDelegation(addr, valAddr, sdkmath.LegacyNewDec(100))
|
||||
|
||||
@ -240,7 +240,7 @@ func (s *KeeperTestSuite) TestUnjail() {
|
||||
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addr), int64(4), int64(3),
|
||||
time.Unix(2, 0), true, int64(10))
|
||||
|
||||
s.Require().NoError(s.slashingKeeper.SetValidatorSigningInfo(s.ctx, sdk.ConsAddress(addr), info))
|
||||
s.Require().NoError(s.slashingKeeper.ValidatorSigningInfo.Set(s.ctx, sdk.ConsAddress(addr), info))
|
||||
s.stakingKeeper.EXPECT().Validator(s.ctx, valAddr).Return(val, nil)
|
||||
del := types.NewDelegation(addr, valAddr, sdkmath.LegacyNewDec(100))
|
||||
|
||||
@ -269,7 +269,7 @@ func (s *KeeperTestSuite) TestUnjail() {
|
||||
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addr), int64(4), int64(3),
|
||||
s.ctx.BlockTime().AddDate(0, 0, 1), false, int64(10))
|
||||
|
||||
s.Require().NoError(s.slashingKeeper.SetValidatorSigningInfo(s.ctx, sdk.ConsAddress(addr), info))
|
||||
s.Require().NoError(s.slashingKeeper.ValidatorSigningInfo.Set(s.ctx, sdk.ConsAddress(addr), info))
|
||||
s.stakingKeeper.EXPECT().Validator(s.ctx, valAddr).Return(val, nil)
|
||||
del := types.NewDelegation(addr, valAddr, sdkmath.LegacyNewDec(10000))
|
||||
|
||||
@ -298,7 +298,7 @@ func (s *KeeperTestSuite) TestUnjail() {
|
||||
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addr), int64(4), int64(3),
|
||||
time.Unix(2, 0), false, int64(10))
|
||||
|
||||
s.Require().NoError(s.slashingKeeper.SetValidatorSigningInfo(s.ctx, sdk.ConsAddress(addr), info))
|
||||
s.Require().NoError(s.slashingKeeper.ValidatorSigningInfo.Set(s.ctx, sdk.ConsAddress(addr), info))
|
||||
s.stakingKeeper.EXPECT().Validator(s.ctx, valAddr).Return(val, nil)
|
||||
del := types.NewDelegation(addr, valAddr, sdkmath.LegacyNewDec(100))
|
||||
|
||||
|
||||
@ -13,84 +13,29 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
||||
)
|
||||
|
||||
// GetValidatorSigningInfo retruns the ValidatorSigningInfo for a specific validator
|
||||
// ConsAddress. If not found it returns ErrNoSigningInfoFound, but other errors
|
||||
// may be returned if there is an error reading from the store.
|
||||
func (k Keeper) GetValidatorSigningInfo(ctx context.Context, address sdk.ConsAddress) (types.ValidatorSigningInfo, error) {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
var info types.ValidatorSigningInfo
|
||||
bz, err := store.Get(types.ValidatorSigningInfoKey(address))
|
||||
if err != nil {
|
||||
return info, err
|
||||
}
|
||||
|
||||
if bz == nil {
|
||||
return info, types.ErrNoSigningInfoFound
|
||||
}
|
||||
|
||||
err = k.cdc.Unmarshal(bz, &info)
|
||||
return info, err
|
||||
}
|
||||
|
||||
// HasValidatorSigningInfo returns if a given validator has signing information
|
||||
// persisted.
|
||||
func (k Keeper) HasValidatorSigningInfo(ctx context.Context, consAddr sdk.ConsAddress) bool {
|
||||
_, err := k.GetValidatorSigningInfo(ctx, consAddr)
|
||||
_, err := k.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// SetValidatorSigningInfo sets the validator signing info to a consensus address key
|
||||
func (k Keeper) SetValidatorSigningInfo(ctx context.Context, address sdk.ConsAddress, info types.ValidatorSigningInfo) error {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
bz, err := k.cdc.Marshal(&info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return store.Set(types.ValidatorSigningInfoKey(address), bz)
|
||||
}
|
||||
|
||||
// IterateValidatorSigningInfos iterates over the stored ValidatorSigningInfo
|
||||
func (k Keeper) IterateValidatorSigningInfos(ctx context.Context,
|
||||
handler func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool),
|
||||
) error {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
iter, err := store.Iterator(types.ValidatorSigningInfoKeyPrefix, storetypes.PrefixEndBytes(types.ValidatorSigningInfoKeyPrefix))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer iter.Close()
|
||||
for ; iter.Valid(); iter.Next() {
|
||||
address := types.ValidatorSigningInfoAddress(iter.Key())
|
||||
var info types.ValidatorSigningInfo
|
||||
err = k.cdc.Unmarshal(iter.Value(), &info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if handler(address, info) {
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// JailUntil attempts to set a validator's JailedUntil attribute in its signing
|
||||
// info. It will panic if the signing info does not exist for the validator.
|
||||
func (k Keeper) JailUntil(ctx context.Context, consAddr sdk.ConsAddress, jailTime time.Time) error {
|
||||
signInfo, err := k.GetValidatorSigningInfo(ctx, consAddr)
|
||||
signInfo, err := k.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "cannot jail validator that does not have any signing information")
|
||||
}
|
||||
|
||||
signInfo.JailedUntil = jailTime
|
||||
return k.SetValidatorSigningInfo(ctx, consAddr, signInfo)
|
||||
return k.ValidatorSigningInfo.Set(ctx, consAddr, signInfo)
|
||||
}
|
||||
|
||||
// Tombstone attempts to tombstone a validator. It will panic if signing info for
|
||||
// the given validator does not exist.
|
||||
func (k Keeper) Tombstone(ctx context.Context, consAddr sdk.ConsAddress) error {
|
||||
signInfo, err := k.GetValidatorSigningInfo(ctx, consAddr)
|
||||
signInfo, err := k.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
if err != nil {
|
||||
return types.ErrNoSigningInfoFound.Wrap("cannot tombstone validator that does not have any signing information")
|
||||
}
|
||||
@ -100,12 +45,12 @@ func (k Keeper) Tombstone(ctx context.Context, consAddr sdk.ConsAddress) error {
|
||||
}
|
||||
|
||||
signInfo.Tombstoned = true
|
||||
return k.SetValidatorSigningInfo(ctx, consAddr, signInfo)
|
||||
return k.ValidatorSigningInfo.Set(ctx, consAddr, signInfo)
|
||||
}
|
||||
|
||||
// IsTombstoned returns if a given validator by consensus address is tombstoned.
|
||||
func (k Keeper) IsTombstoned(ctx context.Context, consAddr sdk.ConsAddress) bool {
|
||||
signInfo, err := k.GetValidatorSigningInfo(ctx, consAddr)
|
||||
signInfo, err := k.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
@ -22,9 +22,9 @@ func (s *KeeperTestSuite) TestValidatorSigningInfo() {
|
||||
)
|
||||
|
||||
// set the validator signing information
|
||||
require.NoError(keeper.SetValidatorSigningInfo(ctx, consAddr, signingInfo))
|
||||
require.NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr, signingInfo))
|
||||
require.True(keeper.HasValidatorSigningInfo(ctx, consAddr))
|
||||
info, err := keeper.GetValidatorSigningInfo(ctx, consAddr)
|
||||
info, err := keeper.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
require.NoError(err)
|
||||
require.Equal(info.StartHeight, ctx.BlockHeight())
|
||||
require.Equal(info.IndexOffset, int64(3))
|
||||
@ -33,9 +33,9 @@ func (s *KeeperTestSuite) TestValidatorSigningInfo() {
|
||||
|
||||
var signingInfos []slashingtypes.ValidatorSigningInfo
|
||||
|
||||
err = keeper.IterateValidatorSigningInfos(ctx, func(consAddr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
|
||||
err = keeper.ValidatorSigningInfo.Walk(ctx, nil, func(consAddr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool, err error) {
|
||||
signingInfos = append(signingInfos, info)
|
||||
return false
|
||||
return false, nil
|
||||
})
|
||||
require.NoError(err)
|
||||
require.Equal(signingInfos[0].Address, signingInfo.Address)
|
||||
@ -48,7 +48,7 @@ func (s *KeeperTestSuite) TestValidatorSigningInfo() {
|
||||
// test JailUntil
|
||||
jailTime := time.Now().Add(time.Hour).UTC()
|
||||
require.NoError(keeper.JailUntil(ctx, consAddr, jailTime))
|
||||
sInfo, _ := keeper.GetValidatorSigningInfo(ctx, consAddr)
|
||||
sInfo, _ := keeper.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
require.Equal(sInfo.JailedUntil, jailTime)
|
||||
}
|
||||
|
||||
|
||||
@ -55,7 +55,7 @@ func (k Keeper) Unjail(ctx context.Context, validatorAddr sdk.ValAddress) error
|
||||
// that the validator was never bonded and must've been jailed due to falling
|
||||
// below their minimum self-delegation. The validator can unjail at any point
|
||||
// assuming they've now bonded above their minimum self-delegation.
|
||||
info, err := k.GetValidatorSigningInfo(ctx, consAddr)
|
||||
info, err := k.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
if err == nil {
|
||||
// cannot be unjailed if tombstoned
|
||||
if info.Tombstoned {
|
||||
|
||||
@ -87,7 +87,7 @@ func SimulateMsgUnjail(
|
||||
if err != nil {
|
||||
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to get validator consensus key"), nil, err
|
||||
}
|
||||
info, err := k.GetValidatorSigningInfo(ctx, consAddr)
|
||||
info, err := k.ValidatorSigningInfo.Get(ctx, consAddr)
|
||||
if err != nil {
|
||||
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to find validator signing info"), nil, err // skip
|
||||
}
|
||||
|
||||
@ -167,7 +167,7 @@ func (suite *SimTestSuite) TestSimulateMsgUnjail() {
|
||||
suite.Require().NoError(err)
|
||||
info := types.NewValidatorSigningInfo(val0ConsAddress, int64(4), int64(3),
|
||||
time.Unix(2, 0), false, int64(10))
|
||||
err = suite.slashingKeeper.SetValidatorSigningInfo(ctx, val0ConsAddress, info)
|
||||
err = suite.slashingKeeper.ValidatorSigningInfo.Set(ctx, val0ConsAddress, info)
|
||||
suite.Require().NoError(err)
|
||||
// put validator0 in jail
|
||||
suite.Require().NoError(suite.stakingKeeper.Jail(ctx, val0ConsAddress))
|
||||
|
||||
@ -7,7 +7,6 @@ import (
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/address"
|
||||
"github.com/cosmos/cosmos-sdk/types/kv"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -51,7 +50,7 @@ const (
|
||||
|
||||
var (
|
||||
ParamsKey = collections.NewPrefix(0) // Prefix for params key
|
||||
ValidatorSigningInfoKeyPrefix = []byte{0x01} // Prefix for signing info
|
||||
ValidatorSigningInfoKeyPrefix = collections.NewPrefix(1) // Prefix for signing info
|
||||
ValidatorMissedBlockBitmapKeyPrefix = []byte{0x02} // Prefix for missed block bitmap
|
||||
AddrPubkeyRelationKeyPrefix = []byte{0x03} // Prefix for address-pubkey relation
|
||||
)
|
||||
@ -61,15 +60,6 @@ func ValidatorSigningInfoKey(v sdk.ConsAddress) []byte {
|
||||
return append(ValidatorSigningInfoKeyPrefix, address.MustLengthPrefix(v.Bytes())...)
|
||||
}
|
||||
|
||||
// ValidatorSigningInfoAddress - extract the address from a validator signing info key
|
||||
func ValidatorSigningInfoAddress(key []byte) (v sdk.ConsAddress) {
|
||||
// Remove prefix and address length.
|
||||
kv.AssertKeyAtLeastLength(key, 3)
|
||||
addr := key[2:]
|
||||
|
||||
return sdk.ConsAddress(addr)
|
||||
}
|
||||
|
||||
// ValidatorMissedBlockBitmapPrefixKey returns the key prefix for a validator's
|
||||
// missed block bitmap.
|
||||
func ValidatorMissedBlockBitmapPrefixKey(v sdk.ConsAddress) []byte {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user