-
Notifications
You must be signed in to change notification settings - Fork 250
refactor: store pending block separately #3073
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
Merged
+199
−40
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c90d21b
Store pending block separately
alpe 522cdfd
Support migration
alpe 8c3afce
Merge branch 'main' into alex/pending_block_fix
alpe ca22251
Review feedback
alpe 838a1f8
Review feedback
alpe c1eb1de
Merge branch 'main' into alex/pending_block_fix
alpe eb7d790
Linter
alpe 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
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,147 @@ | ||
| package executing | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/sha256" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/evstack/ev-node/pkg/store" | ||
| "github.com/evstack/ev-node/types" | ||
| ds "github.com/ipfs/go-datastore" | ||
| ) | ||
|
|
||
| const ( | ||
| headerKey = "pending_header" | ||
| dataKey = "pending_data" | ||
| ) | ||
|
|
||
| // getPendingBlock retrieves the pending block from metadata if it exists | ||
| func (e *Executor) getPendingBlock(ctx context.Context) (*types.SignedHeader, *types.Data, error) { | ||
| headerBytes, err := e.store.GetMetadata(ctx, headerKey) | ||
| if err != nil { | ||
| if errors.Is(err, ds.ErrNotFound) { | ||
| return nil, nil, nil | ||
| } | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| dataBytes, err := e.store.GetMetadata(ctx, dataKey) | ||
| if err != nil { | ||
| if errors.Is(err, ds.ErrNotFound) { | ||
| return nil, nil, fmt.Errorf("pending header exists but data is missing: corrupt state") | ||
| } | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| header := new(types.SignedHeader) | ||
| if err := header.UnmarshalBinary(headerBytes); err != nil { | ||
| return nil, nil, fmt.Errorf("unmarshal pending header: %w", err) | ||
| } | ||
|
|
||
| data := new(types.Data) | ||
| if err := data.UnmarshalBinary(dataBytes); err != nil { | ||
| return nil, nil, fmt.Errorf("unmarshal pending data: %w", err) | ||
| } | ||
| return header, data, nil | ||
| } | ||
|
|
||
| // savePendingBlock saves a block to metadata as pending | ||
| func (e *Executor) savePendingBlock(ctx context.Context, header *types.SignedHeader, data *types.Data) error { | ||
| headerBytes, err := header.MarshalBinary() | ||
| if err != nil { | ||
| return fmt.Errorf("marshal header: %w", err) | ||
| } | ||
|
|
||
| dataBytes, err := data.MarshalBinary() | ||
| if err != nil { | ||
| return fmt.Errorf("marshal data: %w", err) | ||
| } | ||
|
|
||
| batch, err := e.store.NewBatch(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("create batch for early save: %w", err) | ||
| } | ||
|
|
||
| if err := batch.Put(ds.NewKey(store.GetMetaKey(headerKey)), headerBytes); err != nil { | ||
| return fmt.Errorf("save pending header: %w", err) | ||
| } | ||
|
|
||
| if err := batch.Put(ds.NewKey(store.GetMetaKey(dataKey)), dataBytes); err != nil { | ||
| return fmt.Errorf("save pending data: %w", err) | ||
| } | ||
|
|
||
| if err := batch.Commit(); err != nil { | ||
| return fmt.Errorf("commit pending block: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // deletePendingBlock removes pending block metadata | ||
| func (e *Executor) deletePendingBlock(batch store.Batch) error { | ||
| if err := batch.Delete(ds.NewKey(store.GetMetaKey(headerKey))); err != nil { | ||
| return fmt.Errorf("delete pending header: %w", err) | ||
| } | ||
|
|
||
| if err := batch.Delete(ds.NewKey(store.GetMetaKey(dataKey))); err != nil { | ||
| return fmt.Errorf("delete pending data: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // migrateLegacyPendingBlock detects old-style pending blocks that were stored | ||
| // at height N+1 via SaveBlockData with an empty signature (pre-upgrade format) | ||
| // and migrates them to the new metadata-key format (m/pending_header, m/pending_data). | ||
| // | ||
| // This prevents double-signing when a node is upgraded: without migration the | ||
| // new code would not find the pending block and would create+sign a new one at | ||
| // the same height. | ||
| func (e *Executor) migrateLegacyPendingBlock(ctx context.Context) error { | ||
| candidateHeight := e.getLastState().LastBlockHeight + 1 | ||
| pendingHeader, pendingData, err := e.store.GetBlockData(ctx, candidateHeight) | ||
| if err != nil { | ||
| if !errors.Is(err, ds.ErrNotFound) { | ||
| return fmt.Errorf("get block data: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
| if len(pendingHeader.Signature) != 0 { | ||
| return errors.New("pending block with signatures found") | ||
| } | ||
| // Migrate: write header+data to the new metadata keys. | ||
| if err := e.savePendingBlock(ctx, pendingHeader, pendingData); err != nil { | ||
| return fmt.Errorf("save migrated pending block: %w", err) | ||
| } | ||
|
|
||
| // Clean up old-style keys. | ||
| batch, err := e.store.NewBatch(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("create cleanup batch: %w", err) | ||
| } | ||
|
|
||
| headerBytes, err := pendingHeader.MarshalBinary() | ||
| if err != nil { | ||
| return fmt.Errorf("marshal header for hash: %w", err) | ||
| } | ||
| headerHash := sha256.Sum256(headerBytes) | ||
|
|
||
| for _, key := range []string{ | ||
| store.GetHeaderKey(candidateHeight), | ||
| store.GetDataKey(candidateHeight), | ||
| store.GetSignatureKey(candidateHeight), | ||
| store.GetIndexKey(headerHash[:]), | ||
| } { | ||
| if err := batch.Delete(ds.NewKey(key)); err != nil && !errors.Is(err, ds.ErrNotFound) { | ||
| return fmt.Errorf("delete legacy key %s: %w", key, err) | ||
| } | ||
| } | ||
|
|
||
| if err := batch.Commit(); err != nil { | ||
| return fmt.Errorf("commit cleanup batch: %w", err) | ||
| } | ||
|
|
||
| e.logger.Info(). | ||
| Uint64("height", candidateHeight). | ||
| Msg("migrated legacy pending block to metadata format") | ||
| 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
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.
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.
Can we link #2795 as a todo.