refactor(x/slashing): migrate AddrPubkeyRelation to collections (#17044)

This commit is contained in:
Likhita Polavarapu
2023-08-10 08:08:56 +00:00
committed by GitHub
parent 40cf50a30a
commit 8035b5b524
10 changed files with 27 additions and 47 deletions
+1 -1
View File
@@ -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)
}
+2 -3
View File
@@ -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
@@ -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 {
+9 -26
View File
@@ -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
@@ -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()
@@ -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
@@ -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))
}
+1 -1
View File
@@ -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)