-
Notifications
You must be signed in to change notification settings - Fork 886
PLT-980: Populate validator-indexed delegation store at the v6.7 upgrade #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
11
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.
Open
Changes from 4 commits
Commits
Show all changes
11 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 09f745d
Populate delegation-by-validator index at v6.7 upgrade.
amir-deris d761729
Merge branch 'main' into amir/plt-980-validator-delegations-query-sec…
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
Some comments aren't visible on the classic Files Changed page.
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,59 @@ | ||
| 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" | ||
|
|
||
| func delegationByValIndexActive(ctx sdk.Context) bool { | ||
| 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 | ||
| } | ||
|
|
||
| // 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) BackfillDelegationByValIndexResult { | ||
| start := time.Now() | ||
| result := BackfillDelegationByValIndexResult{DryRun: dryRun} | ||
|
|
||
| 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++ | ||
| continue | ||
| } | ||
|
|
||
| if !dryRun { | ||
| store.Set(indexKey, []byte{}) | ||
| } | ||
| result.IndexWritten++ | ||
| } | ||
|
|
||
| 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,98 @@ | ||
| 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) | ||
| ctx = ctx.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]) | ||
|
|
||
| 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 TestDelegationByValIndexPreUpgradeNoDualWrite(t *testing.T) { | ||
| _, app, ctx := createTestInput(t) | ||
| ctx = ctx.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 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) | ||
| 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) | ||
| 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) | ||
| 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
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,85 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| tmproto "github.com/sei-protocol/sei-chain/sei-tendermint/proto/tendermint/types" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/sei-protocol/sei-chain/app" | ||
| stakingtool "github.com/sei-protocol/sei-chain/tools/staking" | ||
| ) | ||
|
|
||
| // BackfillDelegationIndexCmd backfills validator-indexed delegation keys for benchmarking. | ||
| func BackfillDelegationIndexCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "backfill-delegation-index", | ||
| Short: "Backfill validator-indexed delegation keys (dev/benchmark)", | ||
| Long: `Iterate all delegations and write validator-indexed secondary index keys. | ||
|
|
||
| By default this runs in dry-run mode and does not modify state. Pass --write to | ||
| persist index keys. When using --write, stop the node and operate on a copy of | ||
| the data directory. --write cannot be combined with --height.`, | ||
| RunE: runBackfillDelegationIndex, | ||
| } | ||
|
|
||
| stakingtool.BindServerFlags(cmd, app.DefaultNodeHome) | ||
| cmd.Flags().Int64("height", 0, "Load state at this height (0 = latest committed)") | ||
| cmd.Flags().Bool("write", false, "Write index keys and commit state (requires stopped node)") | ||
| return cmd | ||
| } | ||
|
|
||
| func runBackfillDelegationIndex(cmd *cobra.Command, _ []string) error { | ||
| height, err := cmd.Flags().GetInt64("height") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| write, err := cmd.Flags().GetBool("write") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| dryRun := !write | ||
|
|
||
| if write && height > 0 { | ||
| return fmt.Errorf("--write cannot be used with --height") | ||
| } | ||
|
|
||
| if write { | ||
| fmt.Fprintln(os.Stderr, "WARNING: --write mutates application state. Stop the node and use a data copy.") | ||
| } | ||
|
|
||
| seiApp, err := stakingtool.LoadApp(cmd, height) | ||
|
amir-deris marked this conversation as resolved.
Outdated
|
||
| if err != nil { | ||
| return err | ||
| } | ||
| defer seiApp.Close() | ||
|
|
||
| blockHeight := seiApp.LastBlockHeight() | ||
| if blockHeight == 0 { | ||
| blockHeight = 1 | ||
| } | ||
|
|
||
| ctx := seiApp.NewUncachedContext(false, tmproto.Header{Height: blockHeight}) | ||
| result := seiApp.StakingKeeper.BackfillDelegationByValIndex(ctx, dryRun) | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| fmt.Printf("dry_run=%t height=%d\n", result.DryRun, blockHeight) | ||
| fmt.Printf("total_delegations=%d index_written=%d already_indexed=%d elapsed=%s\n", | ||
| result.TotalDelegations, | ||
| result.IndexWritten, | ||
| result.AlreadyIndexed, | ||
| result.Elapsed, | ||
| ) | ||
| if result.TotalDelegations > 0 && result.Elapsed > 0 { | ||
| perSec := float64(result.TotalDelegations) / result.Elapsed.Seconds() | ||
| fmt.Printf("delegations_per_second=%.2f\n", perSec) | ||
| } | ||
|
|
||
| if !write { | ||
| return nil | ||
| } | ||
|
|
||
| commitID := seiApp.CommitMultiStore().Commit(true) | ||
|
amir-deris marked this conversation as resolved.
Outdated
|
||
| fmt.Printf("committed_version=%d\n", commitID.Version) | ||
| return nil | ||
| } | ||
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,15 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // StakingCmd returns offline staking maintenance commands. | ||
| func StakingCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "staking", | ||
| Short: "Offline staking maintenance tools", | ||
| } | ||
| cmd.AddCommand(BackfillDelegationIndexCmd()) | ||
| return cmd | ||
| } |
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.