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 delegation key to collections #17162

Merged
merged 27 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
781d284
WIP: migrate delegation key to collections
atheeshp Jul 26, 2023
c2eb2d8
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/delegatio…
atheeshp Jul 27, 2023
31031ef
fix lint
atheeshp Jul 27, 2023
3e21600
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/delegatio…
atheeshp Jul 28, 2023
539cf24
delegations to collections
atheeshp Jul 28, 2023
ef0f057
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/delegatio…
atheeshp Jul 28, 2023
3629a50
fix tests
atheeshp Jul 28, 2023
400ea4a
Merge branch 'main' into ap/delegation-key-collections
atheeshp Jul 28, 2023
a03fd54
fix tests
atheeshp Aug 1, 2023
fc331f8
Merge branch 'ap/delegation-key-collections' of github.com:cosmos/cos…
atheeshp Aug 1, 2023
c1e1839
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/delegatio…
atheeshp Aug 1, 2023
c3c066c
Merge branch 'main' into ap/delegation-key-collections
atheeshp Aug 1, 2023
f0b14c8
Merge branch 'main' into ap/delegation-key-collections
atheeshp Aug 2, 2023
58a2a03
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/delegatio…
atheeshp Aug 4, 2023
48d30b6
Merge branch 'ap/delegation-key-collections' of github.com:cosmos/cos…
atheeshp Aug 4, 2023
13e2253
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/delegatio…
atheeshp Aug 5, 2023
2a21cc3
review changes
atheeshp Aug 5, 2023
a340498
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/delegatio…
atheeshp Aug 7, 2023
346d5af
nit
atheeshp Aug 7, 2023
f4a68d1
Merge branch 'main' into ap/delegation-key-collections
atheeshp Aug 7, 2023
8d1e3ce
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/delegatio…
atheeshp Aug 8, 2023
3a40d3b
Merge branch 'ap/delegation-key-collections' of github.com:cosmos/cos…
atheeshp Aug 8, 2023
0471806
review changes
atheeshp Aug 8, 2023
3028663
fix tests
atheeshp Aug 8, 2023
73ed6f7
fix err
atheeshp Aug 8, 2023
489a338
fix tests
atheeshp Aug 8, 2023
7a8d938
Merge branch 'main' into ap/delegation-key-collections
atheeshp Aug 9, 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
2 changes: 1 addition & 1 deletion tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) {
ValidatorAddress: f.valAddr.String(),
},
expErr: true,
expErrMsg: "no delegation for (address, validator) tuple",
expErrMsg: "not found",
},
{
name: "validator with no delegations",
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"gotest.tools/v3/assert"

"cosmossdk.io/collections"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/comet"
"cosmossdk.io/log"
Expand Down Expand Up @@ -243,7 +244,7 @@ func TestHandleDoubleSign(t *testing.T) {

// require we be able to unbond now
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
del, _ := f.stakingKeeper.GetDelegation(ctx, sdk.AccAddress(operatorAddr), operatorAddr)
del, _ := f.stakingKeeper.Delegations.Get(ctx, collections.Join(sdk.AccAddress(operatorAddr), operatorAddr))
validator, _ := f.stakingKeeper.GetValidator(ctx, operatorAddr)
totalBond := validator.TokensFromShares(del.GetShares()).TruncateInt()
tstaking.Ctx = ctx
Expand Down
9 changes: 5 additions & 4 deletions tests/integration/staking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"gotest.tools/v3/assert"

"cosmossdk.io/collections"
"cosmossdk.io/math"

simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand Down Expand Up @@ -223,7 +224,7 @@ func TestGRPCQueryDelegatorValidator(t *testing.T) {
}
},
false,
"no delegation for (address, validator) tuple",
"not found",
},
{
"empty delegator address",
Expand Down Expand Up @@ -289,7 +290,7 @@ func TestGRPCQueryDelegation(t *testing.T) {
addrVal := vals[0].OperatorAddress
valAddr, err := sdk.ValAddressFromBech32(addrVal)
assert.NilError(t, err)
delegation, found := f.stakingKeeper.GetDelegation(ctx, addrAcc, valAddr)
delegation, found := f.stakingKeeper.Delegations.Get(ctx, collections.Join(addrAcc, valAddr))
assert.Assert(t, found)
var req *types.QueryDelegationRequest

Expand Down Expand Up @@ -358,7 +359,7 @@ func TestGRPCQueryDelegatorDelegations(t *testing.T) {
addrVal1 := vals[0].OperatorAddress
valAddr, err := sdk.ValAddressFromBech32(addrVal1)
assert.NilError(t, err)
delegation, found := f.stakingKeeper.GetDelegation(ctx, addrAcc, valAddr)
delegation, found := f.stakingKeeper.Delegations.Get(ctx, collections.Join(addrAcc, valAddr))
assert.Assert(t, found)
var req *types.QueryDelegatorDelegationsRequest

Expand Down Expand Up @@ -438,7 +439,7 @@ func TestGRPCQueryValidatorDelegations(t *testing.T) {
addrVal2 := valAddrs[4]
valAddr, err := sdk.ValAddressFromBech32(addrVal1)
assert.NilError(t, err)
delegation, found := f.stakingKeeper.GetDelegation(ctx, addrAcc, valAddr)
delegation, found := f.stakingKeeper.Delegations.Get(ctx, collections.Join(addrAcc, valAddr))
assert.Assert(t, found)

var req *types.QueryValidatorDelegationsRequest
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/staking/keeper/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"

"cosmossdk.io/collections"
"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec/address"
Expand Down Expand Up @@ -167,7 +168,7 @@ func TestSlashRedelegation(t *testing.T) {
assert.DeepEqual(t, math.NewInt(10), rd.Entries[0].InitialBalance)

// shares decreased
del, found = f.stakingKeeper.GetDelegation(f.sdkCtx, addrDels[0], addrVals[1])
del, found = f.stakingKeeper.Delegations.Get(f.sdkCtx, collections.Join(addrDels[0], addrVals[1]))
assert.Assert(t, found)
assert.Equal(t, int64(5), del.Shares.RoundInt64())

Expand Down
11 changes: 5 additions & 6 deletions tests/integration/staking/keeper/validator_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,16 @@ func updateValidatorDelegationsLegacy(f *fixture, existingValAddr, newValAddr sd
}

func updateValidatorDelegations(f *fixture, existingValAddr, newValAddr sdk.ValAddress) {
storeKey := f.keys[types.StoreKey]
cdc, k := f.cdc, f.stakingKeeper

store := f.sdkCtx.KVStore(storeKey)
k := f.stakingKeeper

rng := collections.NewPrefixedPairRange[sdk.ValAddress, sdk.AccAddress](existingValAddr)
err := k.DelegationsByValidator.Walk(f.sdkCtx, rng, func(key collections.Pair[sdk.ValAddress, sdk.AccAddress], _ []byte) (stop bool, err error) {
valAddr, delAddr := key.K1(), key.K2()

bz := store.Get(types.GetDelegationKey(delAddr, valAddr))
delegation := types.MustUnmarshalDelegation(cdc, bz)
delegation, err := k.Delegations.Get(f.sdkCtx, collections.Join(delAddr, valAddr))
if err != nil {
return true, err
}

// remove old operator addr from delegation
if err := k.RemoveDelegation(f.sdkCtx, delegation); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions x/staking/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"

"cosmossdk.io/collections"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/math"
Expand Down Expand Up @@ -115,7 +116,7 @@ func TestStakingMsgs(t *testing.T) {

ctxCheck = app.BaseApp.NewContext(true)
require.True(t, sdk.Coins{genCoin.Sub(bondCoin)}.Equal(bankKeeper.GetAllBalances(ctxCheck, addr2)))
_, err = stakingKeeper.GetDelegation(ctxCheck, addr2, sdk.ValAddress(addr1))
_, err = stakingKeeper.Delegations.Get(ctxCheck, collections.Join(addr2, sdk.ValAddress(addr1)))
require.NoError(t, err)

// begin unbonding
Expand All @@ -126,8 +127,8 @@ func TestStakingMsgs(t *testing.T) {

// delegation should exist anymore
ctxCheck = app.BaseApp.NewContext(true)
_, err = stakingKeeper.GetDelegation(ctxCheck, addr2, sdk.ValAddress(addr1))
require.ErrorIs(t, err, types.ErrNoDelegation)
_, err = stakingKeeper.Delegations.Get(ctxCheck, collections.Join(addr2, sdk.ValAddress(addr1)))
require.ErrorIs(t, err, collections.ErrNotFound)

// balance should be the same because bonding not yet complete
require.True(t, sdk.Coins{genCoin.Sub(bondCoin)}.Equal(bankKeeper.GetAllBalances(ctxCheck, addr2)))
Expand Down
30 changes: 13 additions & 17 deletions x/staking/keeper/alias_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package keeper

import (
"context"
"errors"

"cosmossdk.io/collections"
storetypes "cosmossdk.io/store/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -115,7 +117,7 @@ func (k Keeper) GetValidatorSet() types.ValidatorSet {

// Delegation gets the delegation interface for a particular set of delegator and validator addresses
func (k Keeper) Delegation(ctx context.Context, addrDel sdk.AccAddress, addrVal sdk.ValAddress) (types.DelegationI, error) {
bond, err := k.GetDelegation(ctx, addrDel, addrVal)
bond, err := k.Delegations.Get(ctx, collections.Join(addrDel, addrVal))
if err != nil {
return nil, err
}
Expand All @@ -127,25 +129,19 @@ func (k Keeper) Delegation(ctx context.Context, addrDel sdk.AccAddress, addrVal
func (k Keeper) IterateDelegations(ctx context.Context, delAddr sdk.AccAddress,
fn func(index int64, del types.DelegationI) (stop bool),
) error {
store := k.storeService.OpenKVStore(ctx)
delegatorPrefixKey := types.GetDelegationsKey(delAddr)
iterator, err := store.Iterator(delegatorPrefixKey, storetypes.PrefixEndBytes(delegatorPrefixKey))
if err != nil {
return err
}
defer iterator.Close()

for i := int64(0); iterator.Valid(); iterator.Next() {
del, err := types.UnmarshalDelegation(k.cdc, iterator.Value())
if err != nil {
return err
}

stop := fn(i, del)
var i int64
rng := collections.NewPrefixedPairRange[sdk.AccAddress, sdk.ValAddress](delAddr)
err := k.Delegations.Walk(ctx, rng, func(key collections.Pair[sdk.AccAddress, sdk.ValAddress], del types.Delegation) (stop bool, err error) {
stop = fn(i, del)
if stop {
break
return true, err
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
}
i++

return false, nil
})
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) {
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
return err
}

return nil
Expand Down
Loading