-
Notifications
You must be signed in to change notification settings - Fork 0
fix: writer_with_verify_hash tries to get hash from block that hasn't… #23
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
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 |
|---|---|---|
|
|
@@ -2,11 +2,14 @@ package ethwal | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/0xsequence/ethkit/go-ethereum/common" | ||
| ) | ||
|
|
||
| var ErrParentHashMismatch = errors.New("parent hash mismatch") | ||
|
|
||
| type BlockHashGetter func(ctx context.Context, blockNum uint64) (common.Hash, error) | ||
|
|
||
| func BlockHashGetterFromReader[T any](options Options) BlockHashGetter { | ||
|
|
@@ -45,6 +48,11 @@ func NewWriterWithVerifyHash[T any](writer Writer[T], blockHashGetter BlockHashG | |
| } | ||
|
|
||
| func (w *writerWithVerifyHash[T]) Write(ctx context.Context, b Block[T]) error { | ||
| // Skip if block is already written | ||
| if b.Number <= w.Writer.BlockNum() { | ||
| return nil | ||
| } | ||
|
|
||
| // Skip validation if block is first block | ||
| if b.Number == 1 { | ||
| if err := w.Writer.Write(ctx, b); err != nil { | ||
|
|
@@ -67,15 +75,12 @@ func (w *writerWithVerifyHash[T]) Write(ctx context.Context, b Block[T]) error { | |
|
|
||
| // Validate parent hash | ||
| if b.Parent != w.prevHash { | ||
| w.prevHash = common.Hash{} | ||
|
Contributor
Author
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. This will not work as previous block may still be in memory and pending being written to s3. So it's better to keep prevHash and try to match next time someones writes. |
||
| return fmt.Errorf("parent hash mismatch, expected %s, got %s", | ||
| w.prevHash.String(), b.Parent.String()) | ||
| return fmt.Errorf("%w, expected %s, got %s", ErrParentHashMismatch, b.Parent.String(), w.prevHash.String()) | ||
|
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. Wrapping like this probably won't work, since You can use |
||
| } | ||
|
|
||
| // Write block | ||
| err := w.Writer.Write(ctx, b) | ||
| if err != nil { | ||
| w.prevHash = common.Hash{} | ||
| return fmt.Errorf("failed to write block: %w", err) | ||
| } | ||
|
|
||
|
|
||
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.
This is to keep behaviour in line with other Writer implementations.