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): migrate AddrPubkeyRelation to collections #17044

Merged
merged 22 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d2b54d2
migrate AddrPubkeyRelation to collections
likhita-809 Jul 18, 2023
8727127
fix lint
likhita-809 Jul 19, 2023
0e280bc
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Jul 19, 2023
0fbe189
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Jul 19, 2023
bffec87
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Jul 19, 2023
5b03842
delete pubkey keeper methods
likhita-809 Jul 19, 2023
e299158
add changelog
likhita-809 Jul 19, 2023
ed0ea74
more changes
likhita-809 Jul 19, 2023
a97cfc5
fix tests and add changes accordingly
likhita-809 Jul 19, 2023
fa86f1f
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Jul 19, 2023
fdbc914
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Jul 20, 2023
35680bb
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 7, 2023
cf9ca31
more changes
likhita-809 Aug 7, 2023
fdff427
cleanup
likhita-809 Aug 7, 2023
0f40cb0
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 8, 2023
f14cf39
remove AddPubKey from keeper
likhita-809 Aug 8, 2023
b7f34d0
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 9, 2023
6bc1056
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 9, 2023
8901084
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 9, 2023
9645181
remove unnecessary decoder test case
likhita-809 Aug 10, 2023
9864c8e
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 10, 2023
96b3afe
fix lint
likhita-809 Aug 10, 2023
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
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)...)
}
3 changes: 2 additions & 1 deletion x/slashing/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/kv"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/slashing"
Expand Down Expand Up @@ -36,7 +37,7 @@ func TestDecodeStore(t *testing.T) {
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: append(types.AddrPubkeyRelationKeyPrefix, address.MustLengthPrefix(delAddr1)...), Value: bz},
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
{Key: []byte{0x99}, Value: []byte{0x99}}, // This test should panic
},
}
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)...)
}