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

ipld: adding IPLD package to provide helpers for dealing with IPLD-based data storage #110

Merged
merged 5 commits into from
Oct 7, 2021
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
13 changes: 11 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ module github.com/celestiaorg/celestia-node

go 1.16

replace github.com/ipfs/go-verifcid => github.com/celestiaorg/go-verifcid v0.0.1-lazypatch
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately, this is still required as we rely on Blockservice which relies on this check. We can remove this once we migrate to newer IPLD stack.


require (
github.com/BurntSushi/toml v0.4.1
github.com/celestiaorg/celestia-core v0.0.2-0.20210924001615-488ac31b4b3c
github.com/celestiaorg/nmt v0.7.0
github.com/celestiaorg/rsmt2d v0.3.0
github.com/ipfs/go-bitswap v0.4.0
github.com/ipfs/go-bitswap v0.3.4
Copy link
Member

Choose a reason for hiding this comment

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

Well, this downgrade was not supposed to happen 🤔

Copy link
Member

Choose a reason for hiding this comment

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

That's my bad. Rene updated to the latest versions of ipld etc and I told her to use the same versions of that stack that worked in core (and bitswap v0.3.4 was used there IIRC). Thanks for opening #137.

github.com/ipfs/go-block-format v0.0.3
github.com/ipfs/go-blockservice v0.1.7
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-datastore v0.4.6
github.com/ipfs/go-ds-badger2 v0.1.1
github.com/ipfs/go-ipfs-blockstore v0.1.6
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
github.com/ipfs/go-ipld-cbor v0.0.5 // indirect
github.com/ipfs/go-ipld-format v0.2.0
github.com/ipfs/go-log/v2 v2.3.0
github.com/ipfs/go-merkledag v0.4.0
github.com/ipfs/go-merkledag v0.3.2
github.com/ipfs/go-peertaskqueue v0.4.0 // indirect
github.com/ipld/go-ipld-prime v0.11.0 // indirect
Copy link
Member

Choose a reason for hiding this comment

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

Well, we should double-check why this dependency is here. That's basically an import of the newer IPLD stack that we are not using.

github.com/libp2p/go-libp2p v0.15.0
github.com/libp2p/go-libp2p-connmgr v0.2.4
github.com/libp2p/go-libp2p-core v0.9.0
Expand All @@ -25,6 +33,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/multiformats/go-base32 v0.0.4
github.com/multiformats/go-multiaddr v0.4.0
github.com/multiformats/go-multihash v0.0.15
github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942
go.uber.org/fx v1.14.2
go.uber.org/zap v1.19.0
Expand Down
43 changes: 30 additions & 13 deletions go.sum

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions ipld/nmt_adder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ipld

import (
"context"

"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"

"github.com/celestiaorg/celestia-node/ipld/plugin"
)

// NmtNodeAdder adds ipld.Nodes to the underlying ipld.Batch if it is inserted
// into an nmt tree
type NmtNodeAdder struct {
ctx context.Context
batch *ipld.Batch
leaves *cid.Set
err error
}

// NewNmtNodeAdder returns a new NmtNodeAdder with the provided context and
// batch. Note that the context provided should have a timeout
// It is not thread-safe.
func NewNmtNodeAdder(ctx context.Context, batch *ipld.Batch) *NmtNodeAdder {
return &NmtNodeAdder{
batch: batch,
ctx: ctx,
leaves: cid.NewSet(),
}
}

// Visit is a NodeVisitor that can be used during the creation of a new NMT to
// create and add ipld.Nodes to the Batch while computing the root of the NMT.
func (n *NmtNodeAdder) Visit(hash []byte, children ...[]byte) {
if n.err != nil {
return // protect from further visits if there is an error
}

id := plugin.MustCidFromNamespacedSha256(hash)
switch len(children) {
case 1:
if n.leaves.Visit(id) {
n.err = n.batch.Add(n.ctx, plugin.NewNMTLeafNode(id, children[0]))
}
case 2:
n.err = n.batch.Add(n.ctx, plugin.NewNMTNode(id, children[0], children[1]))
default:
panic("expected a binary tree")
}
}

// Batch return the ipld.Batch originally provided to the NmtNodeAdder
func (n *NmtNodeAdder) Batch() *ipld.Batch {
return n.batch
}

// Commit checks for errors happened during Visit and if absent commits data to inner Batch.
func (n *NmtNodeAdder) Commit() error {
if n.err != nil {
return n.err
}

return n.batch.Commit()
}
Loading