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

Update to use new Builder interface for creating CIDs. #6

Merged
merged 2 commits into from
Aug 11, 2018
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
7 changes: 2 additions & 5 deletions coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
}

if n.cached == nil {
if n.Prefix.Codec == 0 { // unset
n.Prefix = v0CidPrefix
}
c, err := n.Prefix.Sum(n.encoded)
c, err := n.CidBuilder().Sum(n.encoded)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -134,7 +131,7 @@ func DecodeProtobufBlock(b blocks.Block) (ipld.Node, error) {
}

decnd.cached = c
decnd.Prefix = c.Prefix()
decnd.SetCidBuilder(c.Prefix())
return decnd, nil
}

Expand Down
33 changes: 18 additions & 15 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type ProtoNode struct {

cached *cid.Cid

// Prefix specifies cid version and hashing function
Prefix cid.Prefix
// builder specifies cid version and hashing function
builder cid.Builder
}

var v0CidPrefix = cid.Prefix{
Expand Down Expand Up @@ -63,14 +63,21 @@ func PrefixForCidVersion(version int) (cid.Prefix, error) {
}
}

// SetPrefix sets the CID prefix if it is non nil, if prefix is nil then
// it resets it the default value
func (n *ProtoNode) SetPrefix(prefix *cid.Prefix) {
if prefix == nil {
n.Prefix = v0CidPrefix
// CidBuilder returns the CID Builder for this ProtoNode, it is never nil
func (n *ProtoNode) CidBuilder() cid.Builder {
if n.builder == nil {
n.builder = v0CidPrefix
}
return n.builder
}

// SetCidBuilder sets the CID builder if it is non nil, if nil then it
// is reset to the default value
func (n *ProtoNode) SetCidBuilder(builder cid.Builder) {
if builder == nil {
n.builder = v0CidPrefix
} else {
n.Prefix = *prefix
n.Prefix.Codec = cid.DagProtobuf
n.builder = builder.WithCodec(cid.DagProtobuf)
n.encoded = nil
n.cached = nil
}
Expand Down Expand Up @@ -191,7 +198,7 @@ func (n *ProtoNode) Copy() ipld.Node {
copy(nnode.links, n.links)
}

nnode.Prefix = n.Prefix
nnode.builder = n.builder

return nnode
}
Expand Down Expand Up @@ -301,11 +308,7 @@ func (n *ProtoNode) Cid() *cid.Cid {
return n.cached
}

if n.Prefix.Codec == 0 {
n.SetPrefix(nil)
}

c, err := n.Prefix.Sum(n.RawData())
c, err := n.builder.Sum(n.RawData())
if err != nil {
// programmer error
err = fmt.Errorf("invalid CID of length %d: %x: %v", len(n.RawData()), n.RawData(), err)
Expand Down
12 changes: 4 additions & 8 deletions raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ func DecodeRawBlock(block blocks.Block) (ipld.Node, error) {

var _ ipld.DecodeBlockFunc = DecodeRawBlock

// NewRawNodeWPrefix creates a RawNode with the hash function
// specified in prefix.
func NewRawNodeWPrefix(data []byte, prefix cid.Prefix) (*RawNode, error) {
prefix.Codec = cid.Raw
if prefix.Version == 0 {
prefix.Version = 1
}
c, err := prefix.Sum(data)
// NewRawNodeWPrefix creates a RawNode using the provided cid builder
func NewRawNodeWPrefix(data []byte, builder cid.Builder) (*RawNode, error) {
builder = builder.WithCodec(cid.Raw)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: leaving this one alone for now. I personally think we should change NewRawNode to always take a builder rather than creating a separate function, it can take nil if the user wishes to use the default.

c, err := builder.Sum(data)
if err != nil {
return nil, err
}
Expand Down