From c0613df808b77378c909347e7d6137ffd35c1467 Mon Sep 17 00:00:00 2001 From: Marcin Gorzynski Date: Thu, 2 Oct 2025 10:28:33 +0200 Subject: [PATCH] skip: hash validation for block 0 and 1 --- cmd/ethwalinfo/ethwalinfo.go | 5 +++++ writer_with_verify_hash.go | 23 +++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cmd/ethwalinfo/ethwalinfo.go b/cmd/ethwalinfo/ethwalinfo.go index 8bb7dd6..3b78777 100644 --- a/cmd/ethwalinfo/ethwalinfo.go +++ b/cmd/ethwalinfo/ethwalinfo.go @@ -4,6 +4,7 @@ import ( "cmp" "fmt" "os" + "strings" "github.com/0xsequence/ethwal" "github.com/0xsequence/ethwal/storage" @@ -49,6 +50,8 @@ func main() { var fs storage.FS = local.NewLocalFS("./") if bucket := c.String(GoogleCloudBucket.Name); bucket != "" { fs = gcloud.NewGCloudFS(bucket, nil) + } else if strings.HasPrefix(c.String(DatasetPathFlag.Name), "/") { + fs = local.NewLocalFS("/") } dataset := ethwal.Dataset{ @@ -91,6 +94,8 @@ func main() { var fs storage.FS = local.NewLocalFS("./") if bucket := c.String(GoogleCloudBucket.Name); bucket != "" { fs = gcloud.NewGCloudFS(bucket, nil) + } else if strings.HasPrefix(c.String(DatasetPathFlag.Name), "/") { + fs = local.NewLocalFS("/") } dataset := ethwal.Dataset{ diff --git a/writer_with_verify_hash.go b/writer_with_verify_hash.go index 4f93493..76a9bcc 100644 --- a/writer_with_verify_hash.go +++ b/writer_with_verify_hash.go @@ -45,26 +45,41 @@ func NewWriterWithVerifyHash[T any](writer Writer[T], blockHashGetter BlockHashG } func (w *writerWithVerifyHash[T]) Write(ctx context.Context, b Block[T]) error { - var err error - if w.prevHash == (common.Hash{}) && b.Number > 1 { - w.prevHash, err = w.blockHashGetter(ctx, b.Number-1) + // Skip validation if block is first block + if b.Number == 1 { + if err := w.Writer.Write(ctx, b); err != nil { + return fmt.Errorf("failed to write block: %w", err) + } + + w.prevHash = b.Hash + return nil + } + + // Get previous hash if not already set + if w.prevHash == (common.Hash{}) { + prevHash, err := w.blockHashGetter(ctx, b.Number-1) if err != nil { return fmt.Errorf("failed to get block hash: %w", err) } + + w.prevHash = prevHash } + // Validate parent hash if b.Parent != w.prevHash { w.prevHash = common.Hash{} return fmt.Errorf("parent hash mismatch, expected %s, got %s", w.prevHash.String(), b.Parent.String()) } - err = w.Writer.Write(ctx, b) + // Write block + err := w.Writer.Write(ctx, b) if err != nil { w.prevHash = common.Hash{} return fmt.Errorf("failed to write block: %w", err) } + // Update prev hash w.prevHash = b.Hash return nil }