-
Notifications
You must be signed in to change notification settings - Fork 886
PLT-980: Add validator-indexed delegation store (phase 1) #3979
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
Open
amir-deris
wants to merge
9
commits into
main
Choose a base branch
from
amir/plt-980-validator-delegations-query-second-index
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+678
−3
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6bdcd89
Add validator-indexed delegation store and backfill tooling (PLT-980).
amir-deris 911c3f3
Merge branch 'main' into amir/plt-980-validator-delegations-query-sec…
amir-deris 544ba7c
Fix backfill CLI --write commit path under SeiDB.
amir-deris 24c0ccc
Gate delegation index dual-write on v6.7 upgrade.
amir-deris 576c63e
Fix staking tools config loading and LoadApp wiring.
amir-deris 4d5d317
Order delegation index prefix and add backfill CLI tests.
amir-deris e3a0902
Fix delegation index upgrade gate for live execution.
amir-deris 7a0eea0
Fixing lint issue
amir-deris 9b66144
Improve backfill-delegation-index benchmark UX with guidance and prog…
amir-deris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package keeper | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "golang.org/x/mod/semver" | ||
|
|
||
| sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types" | ||
| "github.com/sei-protocol/sei-chain/sei-cosmos/x/staking/types" | ||
| ) | ||
|
|
||
| // DelegationByValIndexUpgrade is the upgrade at which SetDelegation and | ||
| // RemoveDelegation begin maintaining the validator-indexed delegation store. | ||
| const DelegationByValIndexUpgrade = "v6.7" | ||
|
|
||
| // delegationByValIndexActive reports whether SetDelegation and RemoveDelegation | ||
| // should maintain the validator-indexed delegation store. | ||
| // | ||
| // Live execution always does: the current binary only ever executes at/after | ||
| // the upgrade that ships this behavior, so a non-tracing block is never a | ||
| // pre-upgrade block. The only place pre-upgrade behavior must be reproduced is | ||
| // when re-tracing a historical block, where the era is signaled through | ||
| // ClosestUpgradeName (see app.RPCContextProvider). | ||
| func delegationByValIndexActive(ctx sdk.Context) bool { | ||
| if !ctx.IsTracing() { | ||
| return true | ||
| } | ||
| return semver.Compare(ctx.ClosestUpgradeName(), DelegationByValIndexUpgrade) >= 0 | ||
| } | ||
|
|
||
| // BackfillDelegationByValIndexResult reports the outcome of a delegation index backfill. | ||
| type BackfillDelegationByValIndexResult struct { | ||
| TotalDelegations int | ||
| IndexWritten int | ||
| AlreadyIndexed int | ||
| DryRun bool | ||
| Elapsed time.Duration | ||
| } | ||
|
|
||
| // BackfillDelegationByValIndexProgress is a progress snapshot during backfill. | ||
| type BackfillDelegationByValIndexProgress struct { | ||
| TotalDelegations int | ||
| IndexWritten int | ||
| AlreadyIndexed int | ||
| Elapsed time.Duration | ||
| } | ||
|
|
||
| // BackfillProgress reports incremental backfill progress. Nil disables callbacks. | ||
| type BackfillProgress func(BackfillDelegationByValIndexProgress) | ||
|
|
||
| const ( | ||
| backfillProgressDelegationInterval = 100_000 | ||
| backfillProgressMinInterval = 10 * time.Second | ||
| ) | ||
|
|
||
| // BackfillDelegationByValIndex writes validator-indexed delegation keys for existing | ||
| // delegations. When dryRun is true, delegations are counted but no store writes occur. | ||
| func (k Keeper) BackfillDelegationByValIndex(ctx sdk.Context, dryRun bool, progress BackfillProgress) BackfillDelegationByValIndexResult { | ||
| start := time.Now() | ||
| result := BackfillDelegationByValIndexResult{DryRun: dryRun} | ||
| lastProgressReport := start | ||
|
|
||
| reportProgress := func() { | ||
| if progress == nil { | ||
| return | ||
| } | ||
| progress(BackfillDelegationByValIndexProgress{ | ||
| TotalDelegations: result.TotalDelegations, | ||
| IndexWritten: result.IndexWritten, | ||
| AlreadyIndexed: result.AlreadyIndexed, | ||
| Elapsed: time.Since(start), | ||
| }) | ||
| lastProgressReport = time.Now() | ||
| } | ||
|
|
||
| store := ctx.KVStore(k.storeKey) | ||
| iterator := sdk.KVStorePrefixIterator(store, types.DelegationKey) | ||
| defer func() { _ = iterator.Close() }() | ||
|
|
||
| for ; iterator.Valid(); iterator.Next() { | ||
| delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value()) | ||
| result.TotalDelegations++ | ||
|
|
||
| delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress) | ||
| valAddr := delegation.GetValidatorAddr() | ||
| indexKey := types.GetDelegationByValIndexKey(delegatorAddress, valAddr) | ||
| if store.Has(indexKey) { | ||
| result.AlreadyIndexed++ | ||
| } else { | ||
| if !dryRun { | ||
| store.Set(indexKey, []byte{}) | ||
| } | ||
| result.IndexWritten++ | ||
| } | ||
|
|
||
| if progress == nil { | ||
| continue | ||
| } | ||
| now := time.Now() | ||
| if result.TotalDelegations == 1 || | ||
| result.TotalDelegations%backfillProgressDelegationInterval == 0 || | ||
| now.Sub(lastProgressReport) >= backfillProgressMinInterval { | ||
| reportProgress() | ||
| } | ||
| } | ||
|
|
||
| result.Elapsed = time.Since(start) | ||
| return result | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package keeper_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types" | ||
| "github.com/sei-protocol/sei-chain/sei-cosmos/x/staking/types" | ||
| ) | ||
|
|
||
| func TestDelegationByValIndexDualWrite(t *testing.T) { | ||
| _, app, ctx := createTestInput(t) | ||
|
|
||
| addrDels, valAddrs := generateAddresses(app, ctx, 1) | ||
| delegation := types.NewDelegation(addrDels[0], valAddrs[0], sdk.NewDec(1)) | ||
|
|
||
| store := ctx.KVStore(app.StakingKeeper.GetStoreKey()) | ||
| indexKey := types.GetDelegationByValIndexKey(addrDels[0], valAddrs[0]) | ||
|
|
||
| require.False(t, store.Has(indexKey)) | ||
|
|
||
| app.StakingKeeper.SetDelegation(ctx, delegation) | ||
| require.True(t, store.Has(indexKey)) | ||
| require.NotNil(t, store.Get(types.GetDelegationKey(addrDels[0], valAddrs[0]))) | ||
|
|
||
| app.StakingKeeper.RemoveDelegation(ctx, delegation) | ||
| require.False(t, store.Has(indexKey)) | ||
| require.Nil(t, store.Get(types.GetDelegationKey(addrDels[0], valAddrs[0]))) | ||
| } | ||
|
|
||
| func TestDelegationByValIndexTracingPreUpgradeNoDualWrite(t *testing.T) { | ||
| _, app, ctx := createTestInput(t) | ||
| ctx = ctx.WithIsTracing(true).WithClosestUpgradeName("v6.6") | ||
|
|
||
| addrDels, valAddrs := generateAddresses(app, ctx, 1) | ||
| delegation := types.NewDelegation(addrDels[0], valAddrs[0], sdk.NewDec(1)) | ||
|
|
||
| store := ctx.KVStore(app.StakingKeeper.GetStoreKey()) | ||
| indexKey := types.GetDelegationByValIndexKey(addrDels[0], valAddrs[0]) | ||
|
|
||
| app.StakingKeeper.SetDelegation(ctx, delegation) | ||
| require.False(t, store.Has(indexKey)) | ||
| require.NotNil(t, store.Get(types.GetDelegationKey(addrDels[0], valAddrs[0]))) | ||
|
|
||
| app.StakingKeeper.RemoveDelegation(ctx, delegation) | ||
| require.False(t, store.Has(indexKey)) | ||
| require.Nil(t, store.Get(types.GetDelegationKey(addrDels[0], valAddrs[0]))) | ||
| } | ||
|
|
||
| func TestDelegationByValIndexTracingPostUpgradeDualWrite(t *testing.T) { | ||
| _, app, ctx := createTestInput(t) | ||
| ctx = ctx.WithIsTracing(true).WithClosestUpgradeName("v6.7") | ||
|
|
||
| addrDels, valAddrs := generateAddresses(app, ctx, 1) | ||
| delegation := types.NewDelegation(addrDels[0], valAddrs[0], sdk.NewDec(1)) | ||
|
|
||
| store := ctx.KVStore(app.StakingKeeper.GetStoreKey()) | ||
| indexKey := types.GetDelegationByValIndexKey(addrDels[0], valAddrs[0]) | ||
|
|
||
| app.StakingKeeper.SetDelegation(ctx, delegation) | ||
| require.True(t, store.Has(indexKey)) | ||
| } | ||
|
|
||
| func TestBackfillDelegationByValIndex(t *testing.T) { | ||
| _, app, ctx := createTestInput(t) | ||
|
|
||
| addrDels, valAddrs := generateAddresses(app, ctx, 2) | ||
| delegations := []types.Delegation{ | ||
| types.NewDelegation(addrDels[0], valAddrs[0], sdk.NewDec(1)), | ||
| types.NewDelegation(addrDels[0], valAddrs[1], sdk.NewDec(2)), | ||
| types.NewDelegation(addrDels[1], valAddrs[0], sdk.NewDec(3)), | ||
| } | ||
|
|
||
| store := ctx.KVStore(app.StakingKeeper.GetStoreKey()) | ||
| for _, delegation := range delegations { | ||
| delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress) | ||
| valAddr := delegation.GetValidatorAddr() | ||
| store.Set( | ||
| types.GetDelegationKey(delegatorAddress, valAddr), | ||
| types.MustMarshalDelegation(app.AppCodec(), delegation), | ||
| ) | ||
| } | ||
|
|
||
| dryRunResult := app.StakingKeeper.BackfillDelegationByValIndex(ctx, true, nil) | ||
| require.Equal(t, 3, dryRunResult.TotalDelegations) | ||
| require.Equal(t, 3, dryRunResult.IndexWritten) | ||
| require.Equal(t, 0, dryRunResult.AlreadyIndexed) | ||
| require.True(t, dryRunResult.DryRun) | ||
| for _, delegation := range delegations { | ||
| delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress) | ||
| valAddr := delegation.GetValidatorAddr() | ||
| require.False(t, store.Has(types.GetDelegationByValIndexKey(delegatorAddress, valAddr))) | ||
| } | ||
|
|
||
| writeResult := app.StakingKeeper.BackfillDelegationByValIndex(ctx, false, nil) | ||
| require.Equal(t, 3, writeResult.TotalDelegations) | ||
| require.Equal(t, 3, writeResult.IndexWritten) | ||
| require.Equal(t, 0, writeResult.AlreadyIndexed) | ||
| require.False(t, writeResult.DryRun) | ||
| for _, delegation := range delegations { | ||
| delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress) | ||
| valAddr := delegation.GetValidatorAddr() | ||
| require.True(t, store.Has(types.GetDelegationByValIndexKey(delegatorAddress, valAddr))) | ||
| } | ||
|
|
||
| repeatResult := app.StakingKeeper.BackfillDelegationByValIndex(ctx, false, nil) | ||
| require.Equal(t, 3, repeatResult.TotalDelegations) | ||
| require.Equal(t, 0, repeatResult.IndexWritten) | ||
| require.Equal(t, 3, repeatResult.AlreadyIndexed) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.