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/staking): Migrate UnbondingDelegationByValIndex to use collections #17291

Merged
merged 63 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
f551008
change UnbondingDelegationByValIndexKey to use collections prefix
likhita-809 Aug 3, 2023
ecf2d25
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 4, 2023
49bb6d9
move UnbondingDelegationByValIndex
likhita-809 Aug 4, 2023
74aa65d
fix lint
likhita-809 Aug 7, 2023
2a94074
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 7, 2023
54e3f91
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 7, 2023
0a02c45
remove unused GetUBDKeyFromValIndexKey
likhita-809 Aug 7, 2023
ec03801
fix integration tests
likhita-809 Aug 7, 2023
6a511b7
fix tests
likhita-809 Aug 7, 2023
b1ae859
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 7, 2023
efd620c
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 8, 2023
6932ae8
add changelog
likhita-809 Aug 8, 2023
b255839
nit
likhita-809 Aug 8, 2023
1496889
wip: use keyset
likhita-809 Aug 8, 2023
5005687
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 8, 2023
4dcf9ef
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 9, 2023
1bd8833
Revert "wip: use keyset"
likhita-809 Aug 9, 2023
f5ccac8
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 9, 2023
2aa4e7c
bump collections & remove ErrInvaliIterator
likhita-809 Aug 9, 2023
de9ff14
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 9, 2023
0bb8455
run go mod tidy
likhita-809 Aug 9, 2023
348229f
fix tests
likhita-809 Aug 9, 2023
81b81cb
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 10, 2023
512a428
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 10, 2023
9c6c248
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 10, 2023
b067556
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 10, 2023
71c9730
increase consensus version
likhita-809 Aug 10, 2023
76a5189
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 11, 2023
400aa1a
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 16, 2023
bfac9f9
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 16, 2023
cc7b5ea
revert cons version change
likhita-809 Aug 16, 2023
cfdd38c
revert go mod changes
likhita-809 Aug 16, 2023
3087457
address review comments
likhita-809 Aug 18, 2023
d1e26bc
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 18, 2023
ac2eefb
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 18, 2023
2bb19fe
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 18, 2023
6cd410c
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 21, 2023
9e8ca80
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 21, 2023
e65f4d7
add diff test
likhita-809 Aug 21, 2023
c5f2950
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 21, 2023
a1b2ee6
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 22, 2023
eec358e
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 23, 2023
01ab986
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 23, 2023
af7c7a0
address review comment
likhita-809 Aug 23, 2023
f78356c
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 23, 2023
a7143e3
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 23, 2023
3e51a94
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 24, 2023
053127a
address review comments
likhita-809 Aug 24, 2023
a6c9044
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 24, 2023
3da55cf
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 28, 2023
b15183a
address review comments
likhita-809 Aug 28, 2023
b1a5200
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 28, 2023
a0cc241
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 28, 2023
012f9f4
remove duplicate keeper test
likhita-809 Aug 28, 2023
caee945
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 28, 2023
2ce8b8a
Merge branch 'main' into likhita/ubdValIndex-coll
likhita-809 Aug 28, 2023
f1be944
Merge branch 'main' into likhita/ubdValIndex-coll
likhita-809 Aug 29, 2023
e4cc6c5
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 29, 2023
9504954
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 29, 2023
d3c738f
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 29, 2023
a6ca994
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 29, 2023
41e3b1d
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Aug 30, 2023
6d3e6f0
Merge branch 'main' into likhita/ubdValIndex-coll
facundomedica Aug 30, 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
39 changes: 21 additions & 18 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,26 +215,29 @@ func (k Keeper) GetUnbondingDelegation(ctx context.Context, delAddr sdk.AccAddre
// particular validator.
func (k Keeper) GetUnbondingDelegationsFromValidator(ctx context.Context, valAddr sdk.ValAddress) (ubds []types.UnbondingDelegation, err error) {
store := k.storeService.OpenKVStore(ctx)
prefix := types.GetUBDsByValIndexKey(valAddr)
iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix))
if err != nil {
rng := collections.NewPrefixUntilPairRange[sdk.ValAddress, sdk.AccAddress](valAddr)
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
err = k.UnbondingDelegationByValIndex.Walk(
ctx,
rng,
func(key collections.Pair[sdk.ValAddress, sdk.AccAddress], value []byte) (stop bool, err error) {
valAddr := key.K1()
delAddr := key.K2()
ubdkey := types.GetUBDKey(delAddr, valAddr)
ubdValue, err := store.Get(ubdkey)
if err != nil {
return true, err
}
unbondingDelegation, err := types.UnmarshalUBD(k.cdc, ubdValue)
if err != nil {
return true, err
}
ubds = append(ubds, unbondingDelegation)
return false, nil
},
)
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) {
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
return ubds, err
}
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
key := types.GetUBDKeyFromValIndexKey(iterator.Key())
value, err := store.Get(key)
if err != nil {
return ubds, err
}
ubd, err := types.UnmarshalUBD(k.cdc, value)
if err != nil {
return ubds, err
}
ubds = append(ubds, ubd)
}

return ubds, nil
}

Expand Down
32 changes: 19 additions & 13 deletions x/staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,25 @@ func (k Querier) ValidatorUnbondingDelegations(ctx context.Context, req *types.Q
}

store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
srcValPrefix := types.GetUBDsByValIndexKey(valAddr)
ubdStore := prefix.NewStore(store, srcValPrefix)
pageRes, err := query.Paginate(ubdStore, req.Pagination, func(key, value []byte) error {
storeKey := types.GetUBDKeyFromValIndexKey(append(srcValPrefix, key...))
storeValue := store.Get(storeKey)

ubd, err := types.UnmarshalUBD(k.cdc, storeValue)
if err != nil {
return err
}
ubds = append(ubds, ubd)
return nil
})
_, pageRes, err := query.CollectionPaginate(
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
ctx,
k.UnbondingDelegationByValIndex,
req.Pagination,
func(key collections.Pair[sdk.ValAddress, sdk.AccAddress], value []byte) (types.UnbondingDelegation, error) {
valAddr := key.K1()
delAddr := key.K2()
ubdKey := types.GetUBDKey(delAddr, valAddr)
storeValue := store.Get(ubdKey)

ubd, err := types.UnmarshalUBD(k.cdc, storeValue)
if err != nil {
return types.UnbondingDelegation{}, err
}
ubds = append(ubds, ubd)
return ubd, nil
},
query.WithCollectionPaginationPairPrefix[sdk.ValAddress, sdk.AccAddress](valAddr),
)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down
23 changes: 15 additions & 8 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ type Keeper struct {
validatorAddressCodec addresscodec.Codec
consensusAddressCodec addresscodec.Codec

Schema collections.Schema
HistoricalInfo collections.Map[uint64, types.HistoricalInfo]
LastTotalPower collections.Item[math.Int]
ValidatorUpdates collections.Item[types.ValidatorUpdates]
DelegationsByValidator collections.Map[collections.Pair[sdk.ValAddress, sdk.AccAddress], []byte]
UnbondingID collections.Sequence
ValidatorByConsensusAddress collections.Map[sdk.ConsAddress, sdk.ValAddress]
UnbondingType collections.Map[uint64, uint64]
Schema collections.Schema
HistoricalInfo collections.Map[uint64, types.HistoricalInfo]
LastTotalPower collections.Item[math.Int]
ValidatorUpdates collections.Item[types.ValidatorUpdates]
DelegationsByValidator collections.Map[collections.Pair[sdk.ValAddress, sdk.AccAddress], []byte]
UnbondingID collections.Sequence
ValidatorByConsensusAddress collections.Map[sdk.ConsAddress, sdk.ValAddress]
UnbondingType collections.Map[uint64, uint64]
UnbondingDelegationByValIndex collections.Map[collections.Pair[sdk.ValAddress, sdk.AccAddress], []byte]
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
}

// NewKeeper creates a new staking Keeper instance
Expand Down Expand Up @@ -98,6 +99,12 @@ func NewKeeper(
collcodec.KeyToValueCodec(sdk.ValAddressKey),
),
UnbondingType: collections.NewMap(sb, types.UnbondingTypeKey, "unbonding_type", collections.Uint64Key, collections.Uint64Value),
UnbondingDelegationByValIndex: collections.NewMap(
sb, types.UnbondingDelegationByValIndexKey,
"unbonding_delegation_by_val_index",
collections.PairKeyCodec(sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), sdk.AccAddressKey), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
collections.BytesValue,
),
}

schema, err := sb.Build()
Expand Down
19 changes: 7 additions & 12 deletions x/staking/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ var (
ValidatorsByConsAddrKey = collections.NewPrefix(34) // prefix for each key to a validator index, by pubkey
ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power

DelegationKey = []byte{0x31} // key for a delegation
UnbondingDelegationKey = []byte{0x32} // key for an unbonding-delegation
UnbondingDelegationByValIndexKey = []byte{0x33} // prefix for each key for an unbonding-delegation, by validator operator
RedelegationKey = []byte{0x34} // key for a redelegation
RedelegationByValSrcIndexKey = []byte{0x35} // prefix for each key for an redelegation, by source validator operator
RedelegationByValDstIndexKey = []byte{0x36} // prefix for each key for an redelegation, by destination validator operator
DelegationKey = []byte{0x31} // key for a delegation
UnbondingDelegationKey = []byte{0x32} // key for an unbonding-delegation
UnbondingDelegationByValIndexKey = collections.NewPrefix(51) // prefix for each key for an unbonding-delegation, by validator operator
RedelegationKey = []byte{0x34} // key for a redelegation
RedelegationByValSrcIndexKey = []byte{0x35} // prefix for each key for an redelegation, by source validator operator
RedelegationByValDstIndexKey = []byte{0x36} // prefix for each key for an redelegation, by destination validator operator

UnbondingIDKey = collections.NewPrefix(55) // key for the counter for the incrementing id for UnbondingOperations
UnbondingIndexKey = []byte{0x38} // prefix for an index for looking up unbonding operations by their IDs
Expand Down Expand Up @@ -216,7 +216,7 @@ func GetUBDKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte {
// GetUBDByValIndexKey creates the index-key for an unbonding delegation, stored by validator-index
// VALUE: none (key rearrangement used)
func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte {
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
return append(GetUBDsByValIndexKey(valAddr), address.MustLengthPrefix(delAddr)...)
return append(append(UnbondingDelegationByValIndexKey, address.MustLengthPrefix(valAddr)...), address.MustLengthPrefix(delAddr)...)
}

// GetUBDKeyFromValIndexKey rearranges the ValIndexKey to get the UBDKey
Expand All @@ -238,11 +238,6 @@ func GetUBDsKey(delAddr sdk.AccAddress) []byte {
return append(UnbondingDelegationKey, address.MustLengthPrefix(delAddr)...)
}

// GetUBDsByValIndexKey creates the prefix keyspace for the indexes of unbonding delegations for a validator
func GetUBDsByValIndexKey(valAddr sdk.ValAddress) []byte {
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
return append(UnbondingDelegationByValIndexKey, address.MustLengthPrefix(valAddr)...)
}

// GetUnbondingDelegationTimeKey creates the prefix for all unbonding delegations from a delegator
func GetUnbondingDelegationTimeKey(timestamp time.Time) []byte {
bz := sdk.FormatTimeBytes(timestamp)
Expand Down