Skip to content

Commit

Permalink
sia: recursively verify merkle dag blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Nov 30, 2023
1 parent 13f92eb commit 43b3fdf
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions sia/sia.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"

chunker "github.com/ipfs/boxo/chunker"
"github.com/ipfs/boxo/ipld/merkledag"
"github.com/ipfs/boxo/ipld/unixfs/importer/balanced"
ihelpers "github.com/ipfs/boxo/ipld/unixfs/importer/helpers"
blocks "github.com/ipfs/go-block-format"
Expand Down Expand Up @@ -237,13 +238,29 @@ func (n *Node) CalculateBlocks(ctx context.Context, r io.Reader, opts UnixFSOpti
func (n *Node) VerifyCID(ctx context.Context, c cid.Cid) error {
rbs := NewBlockStore(n.store, n.renterd, n.log.Named("verify"))

block, err := rbs.Get(ctx, c)
if err != nil {
return fmt.Errorf("failed to get block: %w", err)
} else if block.Cid().Hash().String() != c.Hash().String() {
return fmt.Errorf("unexpected root cid: %s", block.Cid().String())
var recursiveVerifyCid func(ctx context.Context, c cid.Cid) error
recursiveVerifyCid = func(ctx context.Context, c cid.Cid) error {
block, err := rbs.Get(ctx, c)
if err != nil {
return fmt.Errorf("failed to get block: %w", err)
} else if block.Cid().Hash().String() != c.Hash().String() {
return fmt.Errorf("unexpected multihash: %s != %s", block.Cid(), c)
}

switch node := block.(type) {
case *merkledag.ProtoNode:
links := node.Links()
for _, link := range links {
if err := recursiveVerifyCid(ctx, link.Cid); err != nil {
return err
}
}
}

return nil
}
return nil

return recursiveVerifyCid(ctx, c)
}

// New creates a new Sia IPFS store
Expand Down

0 comments on commit 43b3fdf

Please sign in to comment.