-
Notifications
You must be signed in to change notification settings - Fork 260
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
Changes from all commits
c90d21b
522cdfd
8c3afce
ca22251
838a1f8
c1eb1de
eb7d790
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+29
to
+35
Contributor
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. The current logic correctly handles the case where pending data is not found after a pending header has been found, but it does so silently. If a pending header exists without corresponding data, it indicates an inconsistent state that should be logged as a warning. This would help in debugging potential issues where pending blocks are not saved correctly.
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
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.