Skip to content
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

Prevent double writes of samples to IPFS #271

Merged
merged 3 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 8 additions & 4 deletions p2p/ipld/plugin/nodes/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func prependNode(newNode node.Node, nodes []node.Node) []node.Node {
type NmtNodeAdder struct {
batch *format.Batch
ctx context.Context
leaves *cid.Set
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
}

// NewNmtNodeAdder returns a new NmtNodeAdder with the provided context and
Expand All @@ -215,6 +216,7 @@ func NewNmtNodeAdder(ctx context.Context, batch *format.Batch) *NmtNodeAdder {
return &NmtNodeAdder{
batch: batch,
ctx: ctx,
leaves: cid.NewSet(),
}
}

Expand All @@ -223,10 +225,12 @@ func (n *NmtNodeAdder) Visit(hash []byte, children ...[]byte) {
cid := mustCidFromNamespacedSha256(hash)
switch len(children) {
case 1:
n.batch.Add(n.ctx, nmtLeafNode{
cid: cid,
Data: children[0],
})
if n.leaves.Visit(cid) {
liamsi marked this conversation as resolved.
Show resolved Hide resolved
n.batch.Add(n.ctx, nmtLeafNode{
cid: cid,
Data: children[0],
})
}
case 2:
n.batch.Add(n.ctx, nmtNode{
cid: cid,
Expand Down
14 changes: 5 additions & 9 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,11 @@ func (b *Block) PutBlock(ctx context.Context, nodeAdder format.NodeAdder) error
// add namespaces to erasured shares and flatten the eds
leaves := flattenNamespacedEDS(namespacedShares, eds)

// create nmt adder wrapping batch adder
batchAdder := nodes.NewNmtNodeAdder(ctx, format.NewBatch(ctx, nodeAdder))

// iterate through each set of col and row leaves
for _, leafSet := range leaves {
// create a batch per each leafSet
batchAdder := nodes.NewNmtNodeAdder(ctx, format.NewBatch(ctx, nodeAdder))
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
tree := nmt.New(sha256.New(), nmt.NodeVisitor(batchAdder.Visit))
for _, share := range leafSet {
err = tree.Push(share)
Expand All @@ -332,15 +333,10 @@ func (b *Block) PutBlock(ctx context.Context, nodeAdder format.NodeAdder) error

// compute the root in order to collect the ipld.Nodes
tree.Root()

// commit the batch to ipfs
err = batchAdder.Batch().Commit()
if err != nil {
return err
}
}

return nil
// commit the batch to ipfs
return batchAdder.Batch().Commit()
}

// flattenNamespacedEDS returns a flattend extendedDataSquare with namespaces
Expand Down