Skip to content

Commit

Permalink
refactor(x/slashing): migrate AddrPubkeyRelation to collections (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
likhita-809 authored Aug 10, 2023
1 parent 40cf50a commit 8035b5b
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 47 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* (x/slashing) [17044](https://github.com/cosmos/cosmos-sdk/pull/17044) Use collections for `AddrPubkeyRelation`:
* remove from `types`: `AddrPubkeyRelationKey`
* remove from `Keeper`: `AddPubkey`
* (x/staking) [#17260](https://github.com/cosmos/cosmos-sdk/pull/17260) Use collections for `DelegationKey`:
* remove from `types`: `GetDelegationKey`, `GetDelegationsKey`
* (x/staking) [#17288](https://github.com/cosmos/cosmos-sdk/pull/17288) Use collections for `UnbondingIndex`:
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestHandleDoubleSign(t *testing.T) {
assert.NilError(t, err)
assert.DeepEqual(t, selfDelegation, val.GetBondedTokens())

assert.NilError(t, f.slashingKeeper.AddPubkey(f.sdkCtx, valpubkey))
assert.NilError(t, f.slashingKeeper.AddrPubkeyRelation.Set(f.sdkCtx, valpubkey.Address(), valpubkey))

info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(valpubkey.Address()), f.sdkCtx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.sdkCtx, sdk.ConsAddress(valpubkey.Address()), info)
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func TestHandleNewValidator(t *testing.T) {
assert.NilError(t, err)
f.ctx = f.ctx.WithBlockHeight(signedBlocksWindow + 1)

assert.NilError(t, f.slashingKeeper.AddPubkey(f.ctx, pks[0]))
assert.NilError(t, f.slashingKeeper.AddrPubkeyRelation.Set(f.ctx, pks[0].Address(), 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.ValidatorSigningInfo.Set(f.ctx, sdk.ConsAddress(valpubkey.Address()), info))
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestHandleAlreadyJailed(t *testing.T) {
power := int64(100)
tstaking := stakingtestutil.NewHelper(t, f.ctx, f.stakingKeeper)

err := f.slashingKeeper.AddPubkey(f.ctx, pks[0])
err := f.slashingKeeper.AddrPubkeyRelation.Set(f.ctx, pks[0].Address(), pks[0])
assert.NilError(t, err)

info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(val.Address()), f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
Expand Down Expand Up @@ -362,7 +362,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
tstaking := stakingtestutil.NewHelper(t, f.ctx, f.stakingKeeper)
valAddr := sdk.ValAddress(addr)

assert.NilError(t, f.slashingKeeper.AddPubkey(f.ctx, pks[0]))
assert.NilError(t, f.slashingKeeper.AddrPubkeyRelation.Set(f.ctx, pks[0].Address(), pks[0]))

info := slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
assert.NilError(t, f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, consAddr, info))
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (keeper Keeper) InitGenesis(ctx sdk.Context, stakingKeeper types.StakingKee
panic(err)
}

err = keeper.AddPubkey(ctx, consPk)
err = keeper.AddrPubkeyRelation.Set(ctx, consPk.Address(), consPk)
if err != nil {
panic(err)
}
Expand Down
5 changes: 2 additions & 3 deletions x/slashing/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ func (h Hooks) AfterValidatorBonded(ctx context.Context, consAddr sdk.ConsAddres

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

// AfterValidatorCreated adds the address-pubkey relation when a validator is created.
func (h Hooks) AfterValidatorCreated(ctx context.Context, valAddr sdk.ValAddress) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
validator, err := h.k.sk.Validator(ctx, valAddr)
if err != nil {
return err
Expand All @@ -62,7 +61,7 @@ func (h Hooks) AfterValidatorCreated(ctx context.Context, valAddr sdk.ValAddress
return err
}

return h.k.AddPubkey(sdkCtx, consPk)
return h.k.AddrPubkeyRelation.Set(ctx, consPk.Address(), consPk)
}

func (h Hooks) AfterValidatorBeginUnbonding(_ context.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
Expand Down
35 changes: 9 additions & 26 deletions x/slashing/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Keeper struct {
Schema collections.Schema
Params collections.Item[types.Params]
ValidatorSigningInfo collections.Map[sdk.ConsAddress, types.ValidatorSigningInfo]
AddrPubkeyRelation collections.Map[[]byte, cryptotypes.PubKey]
}

// NewKeeper creates a slashing keeper
Expand All @@ -48,6 +49,13 @@ func NewKeeper(cdc codec.BinaryCodec, legacyAmino *codec.LegacyAmino, storeServi
sdk.LengthPrefixedAddressKey(sdk.ConsAddressKey), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
codec.CollValue[types.ValidatorSigningInfo](cdc),
),
AddrPubkeyRelation: collections.NewMap(
sb,
types.AddrPubkeyRelationKeyPrefix,
"addr_pubkey_relation",
sdk.LengthPrefixedBytesKey, // sdk.LengthPrefixedBytesKey is needed to retain state compatibility
codec.CollInterfaceValue[cryptotypes.PubKey](cdc),
),
}

schema, err := sb.Build()
Expand All @@ -69,29 +77,9 @@ func (k Keeper) Logger(ctx context.Context) log.Logger {
return sdkCtx.Logger().With("module", "x/"+types.ModuleName)
}

// AddPubkey sets a address-pubkey relation
func (k Keeper) AddPubkey(ctx context.Context, pubkey cryptotypes.PubKey) error {
bz, err := k.cdc.MarshalInterface(pubkey)
if err != nil {
return err
}
store := k.storeService.OpenKVStore(ctx)
key := types.AddrPubkeyRelationKey(pubkey.Address())
return store.Set(key, bz)
}

// GetPubkey returns the pubkey from the adddress-pubkey relation
func (k Keeper) GetPubkey(ctx context.Context, a cryptotypes.Address) (cryptotypes.PubKey, error) {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.AddrPubkeyRelationKey(a))
if err != nil {
return nil, err
}
if bz == nil {
return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(a))
}
var pk cryptotypes.PubKey
return pk, k.cdc.UnmarshalInterface(bz, &pk)
return k.AddrPubkeyRelation.Get(ctx, a)
}

// Slash attempts to slash a validator. The slash is delegated to the staking
Expand Down Expand Up @@ -145,8 +133,3 @@ func (k Keeper) Jail(ctx context.Context, consAddr sdk.ConsAddress) error {
)
return nil
}

func (k Keeper) deleteAddrPubkeyRelation(ctx context.Context, addr cryptotypes.Address) error {
store := k.storeService.OpenKVStore(ctx)
return store.Delete(types.AddrPubkeyRelationKey(addr))
}
2 changes: 1 addition & 1 deletion x/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *KeeperTestSuite) TestPubkey() {
require := s.Require()

_, pubKey, addr := testdata.KeyTestPubAddr()
require.NoError(keeper.AddPubkey(ctx, pubKey))
require.NoError(keeper.AddrPubkeyRelation.Set(ctx, pubKey.Address(), pubKey))

expectedPubKey, err := keeper.GetPubkey(ctx, addr.Bytes())
require.NoError(err)
Expand Down
7 changes: 6 additions & 1 deletion x/slashing/migrations/v2/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
v1 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v1"
v2 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v2"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
Expand Down Expand Up @@ -46,7 +47,7 @@ func TestStoreMigration(t *testing.T) {
{
"AddrPubkeyRelationKey",
v1.AddrPubkeyRelationKey(consAddr),
types.AddrPubkeyRelationKey(consAddr),
addrPubkeyRelationKey(consAddr),
},
}

Expand Down Expand Up @@ -75,3 +76,7 @@ func TestStoreMigration(t *testing.T) {
})
}
}

func addrPubkeyRelationKey(addr []byte) []byte {
return append(types.AddrPubkeyRelationKeyPrefix, address.MustLengthPrefix(addr)...)
}
5 changes: 0 additions & 5 deletions x/slashing/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

var (
delPk1 = ed25519.GenPrivKey().PubKey()
delAddr1 = sdk.AccAddress(delPk1.Address())
consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes())
)

Expand All @@ -29,14 +28,11 @@ func TestDecodeStore(t *testing.T) {

info := types.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0)
missed := []byte{1} // we want to display the bytes for simulation diffs
bz, err := cdc.MarshalInterface(delPk1)
require.NoError(t, err)

kvPairs := kv.Pairs{
Pairs: []kv.Pair{
{Key: types.ValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshal(&info)},
{Key: types.ValidatorMissedBlockBitmapKey(consAddr1, 6), Value: missed},
{Key: types.AddrPubkeyRelationKey(delAddr1), Value: bz},
{Key: []byte{0x99}, Value: []byte{0x99}}, // This test should panic
},
}
Expand All @@ -48,7 +44,6 @@ func TestDecodeStore(t *testing.T) {
}{
{"ValidatorSigningInfo", fmt.Sprintf("%v\n%v", info, info), false},
{"ValidatorMissedBlockBitArray", fmt.Sprintf("missedA: %v\nmissedB: %v\n", missed, missed), false},
{"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", delPk1, delPk1), false},
{"other", "", true},
}
for i, tt := range tests {
Expand Down
7 changes: 1 addition & 6 deletions x/slashing/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
ParamsKey = collections.NewPrefix(0) // Prefix for params key
ValidatorSigningInfoKeyPrefix = collections.NewPrefix(1) // Prefix for signing info
ValidatorMissedBlockBitmapKeyPrefix = []byte{0x02} // Prefix for missed block bitmap
AddrPubkeyRelationKeyPrefix = []byte{0x03} // Prefix for address-pubkey relation
AddrPubkeyRelationKeyPrefix = collections.NewPrefix(3) // Prefix for address-pubkey relation
)

// ValidatorSigningInfoKey - stored by *Consensus* address (not operator address)
Expand All @@ -74,8 +74,3 @@ func ValidatorMissedBlockBitmapKey(v sdk.ConsAddress, chunkIndex int64) []byte {

return append(ValidatorMissedBlockBitmapPrefixKey(v), bz...)
}

// AddrPubkeyRelationKey gets pubkey relation key used to get the pubkey from the address
func AddrPubkeyRelationKey(addr []byte) []byte {
return append(AddrPubkeyRelationKeyPrefix, address.MustLengthPrefix(addr)...)
}

0 comments on commit 8035b5b

Please sign in to comment.