diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a06a112ad63..3b2c31a4a966 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/simapp/export.go b/simapp/export.go index f4ac64f9f56a..6f611a130e7a 100644 --- a/simapp/export.go +++ b/simapp/export.go @@ -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) } } diff --git a/tests/integration/evidence/keeper/infraction_test.go b/tests/integration/evidence/keeper/infraction_test.go index e3b1494cf44c..f716cf4f0101 100644 --- a/tests/integration/evidence/keeper/infraction_test.go +++ b/tests/integration/evidence/keeper/infraction_test.go @@ -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) diff --git a/tests/integration/slashing/keeper/keeper_test.go b/tests/integration/slashing/keeper/keeper_test.go index 383a035df8cd..fae0ff2f58f6 100644 --- a/tests/integration/slashing/keeper/keeper_test.go +++ b/tests/integration/slashing/keeper/keeper_test.go @@ -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) diff --git a/x/slashing/abci_test.go b/x/slashing/abci_test.go index d5ebe2a9900a..e33508678e6f 100644 --- a/x/slashing/abci_test.go +++ b/x/slashing/abci_test.go @@ -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) diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index 01f82303ab18..0ad0ac89550a 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -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 diff --git a/x/slashing/keeper/genesis.go b/x/slashing/keeper/genesis.go index 446ff0a89b15..60ea32eb71e9 100644 --- a/x/slashing/keeper/genesis.go +++ b/x/slashing/keeper/genesis.go @@ -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) diff --git a/x/slashing/keeper/genesis_test.go b/x/slashing/keeper/genesis_test.go index cde4bef704e4..43eba6a8df4a 100644 --- a/x/slashing/keeper/genesis_test.go +++ b/x/slashing/keeper/genesis_test.go @@ -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) } diff --git a/x/slashing/keeper/grpc_query.go b/x/slashing/keeper/grpc_query.go index 3b59097cc87f..cac02378a1f0 100644 --- a/x/slashing/keeper/grpc_query.go +++ b/x/slashing/keeper/grpc_query.go @@ -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) } diff --git a/x/slashing/keeper/grpc_query_test.go b/x/slashing/keeper/grpc_query_test.go index a9d80da510ad..cb370d2ea976 100644 --- a/x/slashing/keeper/grpc_query_test.go +++ b/x/slashing/keeper/grpc_query_test.go @@ -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 diff --git a/x/slashing/keeper/hooks.go b/x/slashing/keeper/hooks.go index f90ba4da96d6..8dadfb6488da 100644 --- a/x/slashing/keeper/hooks.go +++ b/x/slashing/keeper/hooks.go @@ -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, diff --git a/x/slashing/keeper/hooks_test.go b/x/slashing/keeper/hooks_test.go index f1aa3bcddeeb..0456bcb9c170 100644 --- a/x/slashing/keeper/hooks_test.go +++ b/x/slashing/keeper/hooks_test.go @@ -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) } diff --git a/x/slashing/keeper/infractions.go b/x/slashing/keeper/infractions.go index 0efef7dd427d..d207e7b65f93 100644 --- a/x/slashing/keeper/infractions.go +++ b/x/slashing/keeper/infractions.go @@ -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) } diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index 31e82a73c804..1e622f56eb10 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -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() diff --git a/x/slashing/keeper/msg_server_test.go b/x/slashing/keeper/msg_server_test.go index 271763e254bf..b059d21d0da6 100644 --- a/x/slashing/keeper/msg_server_test.go +++ b/x/slashing/keeper/msg_server_test.go @@ -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)) diff --git a/x/slashing/keeper/signing_info.go b/x/slashing/keeper/signing_info.go index f4e1fe94f3a4..be19a08d9873 100644 --- a/x/slashing/keeper/signing_info.go +++ b/x/slashing/keeper/signing_info.go @@ -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 } diff --git a/x/slashing/keeper/signing_info_test.go b/x/slashing/keeper/signing_info_test.go index c2e3a69f97fe..fa696b9b33ad 100644 --- a/x/slashing/keeper/signing_info_test.go +++ b/x/slashing/keeper/signing_info_test.go @@ -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) } diff --git a/x/slashing/keeper/unjail.go b/x/slashing/keeper/unjail.go index b3ab0706e243..7680da4b21b9 100644 --- a/x/slashing/keeper/unjail.go +++ b/x/slashing/keeper/unjail.go @@ -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 { diff --git a/x/slashing/simulation/operations.go b/x/slashing/simulation/operations.go index 839edc172fa1..e807f114ebad 100644 --- a/x/slashing/simulation/operations.go +++ b/x/slashing/simulation/operations.go @@ -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 } diff --git a/x/slashing/simulation/operations_test.go b/x/slashing/simulation/operations_test.go index 1963b6dd805c..69ba5ef6f55f 100644 --- a/x/slashing/simulation/operations_test.go +++ b/x/slashing/simulation/operations_test.go @@ -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)) diff --git a/x/slashing/types/keys.go b/x/slashing/types/keys.go index ffd1c73650bc..7786bb4949e4 100644 --- a/x/slashing/types/keys.go +++ b/x/slashing/types/keys.go @@ -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 {