-
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
base: main
Are you sure you want to change the base?
Changes from all commits
6bdcd89
911c3f3
544ba7c
24c0ccc
576c63e
4d5d317
e3a0902
7a0eea0
9b66144
09f745d
d761729
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package keeper | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types" | ||
| "github.com/sei-protocol/sei-chain/sei-cosmos/x/staking/types" | ||
| ) | ||
|
|
||
| // MigrateDelegationByValIndexResult reports the outcome of populating the | ||
| // validator-indexed delegation store. | ||
| type MigrateDelegationByValIndexResult struct { | ||
| TotalDelegations int | ||
| IndexWritten int | ||
| AlreadyReady bool | ||
| Elapsed time.Duration | ||
| } | ||
|
|
||
| // DelegationByValIndexReady reports whether the validator-indexed delegation store | ||
| // is populated at the version this context reads. | ||
| // | ||
| // The marker is versioned state written by MigrateDelegationByValIndex, so a context | ||
| // reading a height before that migration observes it absent. That makes the answer | ||
| // correct for historical queries and re-traced blocks without the caller supplying | ||
| // an upgrade name or height. | ||
| func (k Keeper) DelegationByValIndexReady(ctx sdk.Context) bool { | ||
| return ctx.KVStore(k.storeKey).Has(types.DelegationByValIndexReadyKey) | ||
| } | ||
|
|
||
| // MigrateDelegationByValIndex writes a validator-indexed key for every existing | ||
| // delegation and then marks the index ready. It is a no-op once the marker is set. | ||
| func (k Keeper) MigrateDelegationByValIndex(ctx sdk.Context) (MigrateDelegationByValIndexResult, error) { | ||
| start := time.Now() | ||
| store := ctx.KVStore(k.storeKey) | ||
|
|
||
| if store.Has(types.DelegationByValIndexReadyKey) { | ||
| return MigrateDelegationByValIndexResult{AlreadyReady: true, Elapsed: time.Since(start)}, nil | ||
| } | ||
|
|
||
| result := MigrateDelegationByValIndexResult{} | ||
| iterator := sdk.KVStorePrefixIterator(store, types.DelegationKey) | ||
| defer func() { _ = iterator.Close() }() | ||
|
|
||
| for ; iterator.Valid(); iterator.Next() { | ||
| delegation, err := types.UnmarshalDelegation(k.cdc, iterator.Value()) | ||
| if err != nil { | ||
| return result, fmt.Errorf("unmarshal delegation at key %X: %w", iterator.Key(), err) | ||
| } | ||
| delAddr, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress) | ||
| if err != nil { | ||
| return result, fmt.Errorf("parse delegator address %q: %w", delegation.DelegatorAddress, err) | ||
| } | ||
|
|
||
| result.TotalDelegations++ | ||
| indexKey := types.GetDelegationByValIndexKey(delAddr, delegation.GetValidatorAddr()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] The delegator address is parsed with |
||
| if store.Has(indexKey) { | ||
| continue | ||
| } | ||
| store.Set(indexKey, []byte{}) | ||
| result.IndexWritten++ | ||
| } | ||
|
|
||
| store.Set(types.DelegationByValIndexReadyKey, []byte{}) | ||
| result.Elapsed = time.Since(start) | ||
| return result, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Genesis never activates the index
Medium Severity
SetDelegationonly dual-writes afterDelegationByValIndexReady, and that marker is set solely by thev6.7upgrade handler.InitGenesisnever sets the marker, andExportGenesisdoes not persist the marker or0x37keys, so new chains and genesis imports never activate the index andvalidatorDelegationskeeps taking the capped full scan.Additional Locations (2)
sei-cosmos/x/staking/keeper/delegation_index.go#L63-L64app/upgrades.go#L101-L104Reviewed by Cursor Bugbot for commit 09f745d. Configure here.