From 981c294d1d87c6e7d8ec71402a7765ec406c4235 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 6 Jul 2023 11:00:32 +0000 Subject: [PATCH] refactor(types)!: cherry-pick sdk collections key changes from #16440 (#16854) Co-authored-by: testinginprod <98415576+testinginprod@users.noreply.github.com> Co-authored-by: Julien Robert --- types/collections.go | 20 ++++++++++---------- types/collections_test.go | 2 +- x/bank/keeper/view.go | 2 +- x/gov/keeper/keeper.go | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/types/collections.go b/types/collections.go index 9fc7e1f59c25..41b9e18606c9 100644 --- a/types/collections.go +++ b/types/collections.go @@ -94,26 +94,26 @@ func (a genericAddressKey[T]) SizeNonTerminal(key T) int { return collections.BytesKey.SizeNonTerminal(key) } -// Deprecated: genericAddressIndexKey is a special key codec used to retain state backwards compatibility +// Deprecated: lengthPrefixedAddressKey is a special key codec used to retain state backwards compatibility // when a generic address key (be: AccAddress, ValAddress, ConsAddress), is used as an index key. -// More docs can be found in the AddressKeyAsIndexKey function. -type genericAddressIndexKey[T addressUnion] struct { +// More docs can be found in the LengthPrefixedAddressKey function. +type lengthPrefixedAddressKey[T addressUnion] struct { collcodec.KeyCodec[T] } -func (g genericAddressIndexKey[T]) Encode(buffer []byte, key T) (int, error) { +func (g lengthPrefixedAddressKey[T]) Encode(buffer []byte, key T) (int, error) { return g.EncodeNonTerminal(buffer, key) } -func (g genericAddressIndexKey[T]) Decode(buffer []byte) (int, T, error) { +func (g lengthPrefixedAddressKey[T]) Decode(buffer []byte) (int, T, error) { return g.DecodeNonTerminal(buffer) } -func (g genericAddressIndexKey[T]) Size(key T) int { return g.SizeNonTerminal(key) } +func (g lengthPrefixedAddressKey[T]) Size(key T) int { return g.SizeNonTerminal(key) } -func (g genericAddressIndexKey[T]) KeyType() string { return "index_key/" + g.KeyCodec.KeyType() } +func (g lengthPrefixedAddressKey[T]) KeyType() string { return "index_key/" + g.KeyCodec.KeyType() } -// Deprecated: AddressKeyAsIndexKey implements an SDK backwards compatible indexing key encoder +// Deprecated: LengthPrefixedAddressKey implements an SDK backwards compatible indexing key encoder // for addresses. // The status quo in the SDK is that address keys are length prefixed even when they're the // last part of a composite key. This should never be used unless to retain state compatibility. @@ -122,8 +122,8 @@ func (g genericAddressIndexKey[T]) KeyType() string { return "index_key/" + g.Ke // byte to the string, then when you know when the string part finishes, it's logical that the // part which remains is the address key. In the SDK instead we prepend to the address key its // length too. -func AddressKeyAsIndexKey[T addressUnion](keyCodec collcodec.KeyCodec[T]) collcodec.KeyCodec[T] { - return genericAddressIndexKey[T]{ +func LengthPrefixedAddressKey[T addressUnion](keyCodec collcodec.KeyCodec[T]) collcodec.KeyCodec[T] { + return lengthPrefixedAddressKey[T]{ keyCodec, } } diff --git a/types/collections_test.go b/types/collections_test.go index 222566c27eb0..e6a15608f6b7 100644 --- a/types/collections_test.go +++ b/types/collections_test.go @@ -21,7 +21,7 @@ func TestCollectionsCorrectness(t *testing.T) { }) t.Run("AddressIndexingKey", func(t *testing.T) { - colltest.TestKeyCodec(t, AddressKeyAsIndexKey(AccAddressKey), AccAddress{0x2, 0x5, 0x8}) + colltest.TestKeyCodec(t, LengthPrefixedAddressKey(AccAddressKey), AccAddress{0x2, 0x5, 0x8}) }) t.Run("Time", func(t *testing.T) { diff --git a/x/bank/keeper/view.go b/x/bank/keeper/view.go index 8d7fae395954..747b85281dde 100644 --- a/x/bank/keeper/view.go +++ b/x/bank/keeper/view.go @@ -42,7 +42,7 @@ func newBalancesIndexes(sb *collections.SchemaBuilder) BalancesIndexes { return BalancesIndexes{ Denom: indexes.NewReversePair[math.Int]( sb, types.DenomAddressPrefix, "address_by_denom_index", - collections.PairKeyCodec(sdk.AddressKeyAsIndexKey(sdk.AccAddressKey), collections.StringKey), // nolint:staticcheck // Note: refer to the AddressKeyAsIndexKey docs to understand why we do this. + collections.PairKeyCodec(sdk.LengthPrefixedAddressKey(sdk.AccAddressKey), collections.StringKey), // nolint:staticcheck // Note: refer to the LengthPrefixedAddressKey docs to understand why we do this. ), } } diff --git a/x/gov/keeper/keeper.go b/x/gov/keeper/keeper.go index 45e97dc301dd..3396997124ce 100644 --- a/x/gov/keeper/keeper.go +++ b/x/gov/keeper/keeper.go @@ -103,8 +103,8 @@ func NewKeeper( authority: authority, Constitution: collections.NewItem(sb, types.ConstitutionKey, "constitution", collections.StringValue), Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[v1.Params](cdc)), - Deposits: collections.NewMap(sb, types.DepositsKeyPrefix, "deposits", collections.PairKeyCodec(collections.Uint64Key, sdk.AddressKeyAsIndexKey(sdk.AccAddressKey)), codec.CollValue[v1.Deposit](cdc)), // nolint: staticcheck // sdk.AddressKeyAsIndexKey is needed to retain state compatibility - Votes: collections.NewMap(sb, types.VotesKeyPrefix, "votes", collections.PairKeyCodec(collections.Uint64Key, sdk.AddressKeyAsIndexKey(sdk.AccAddressKey)), codec.CollValue[v1.Vote](cdc)), // nolint: staticcheck // sdk.AddressKeyAsIndexKey is needed to retain state compatibility + Deposits: collections.NewMap(sb, types.DepositsKeyPrefix, "deposits", collections.PairKeyCodec(collections.Uint64Key, sdk.LengthPrefixedAddressKey(sdk.AccAddressKey)), codec.CollValue[v1.Deposit](cdc)), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility + Votes: collections.NewMap(sb, types.VotesKeyPrefix, "votes", collections.PairKeyCodec(collections.Uint64Key, sdk.LengthPrefixedAddressKey(sdk.AccAddressKey)), codec.CollValue[v1.Vote](cdc)), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility ProposalID: collections.NewSequence(sb, types.ProposalIDKey, "proposal_id"), Proposals: collections.NewMap(sb, types.ProposalsKeyPrefix, "proposals", collections.Uint64Key, codec.CollValue[v1.Proposal](cdc)), ActiveProposalsQueue: collections.NewMap(sb, types.ActiveProposalQueuePrefix, "active_proposals_queue", collections.PairKeyCodec(sdk.TimeKey, collections.Uint64Key), collections.Uint64Value), // sdk.TimeKey is needed to retain state compatibility