Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/ethwalinfo/ethwalinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"cmp"
"fmt"
"os"
"strings"

"github.com/0xsequence/ethwal"
"github.com/0xsequence/ethwal/storage"
Expand Down Expand Up @@ -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("/")
Comment on lines +53 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be an absolute path? Or can we allow relative paths ./ too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relative paths are handled by default. There was the issue where absolute path do not work.

}

dataset := ethwal.Dataset{
Expand Down Expand Up @@ -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{
Expand Down
23 changes: 19 additions & 4 deletions writer_with_verify_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}