Unverified Commit 0244e50b authored by Kevin Atkinson's avatar Kevin Atkinson Committed by GitHub

Merge pull request #6 from ipfs/kevina/builder

Update to use new Builder interface for creating CIDs.
parents 4d2a12ab 70cb3dd8
...@@ -93,10 +93,7 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) { ...@@ -93,10 +93,7 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
} }
if n.cached == nil { if n.cached == nil {
if n.Prefix.Codec == 0 { // unset c, err := n.CidBuilder().Sum(n.encoded)
n.Prefix = v0CidPrefix
}
c, err := n.Prefix.Sum(n.encoded)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -134,7 +131,7 @@ func DecodeProtobufBlock(b blocks.Block) (ipld.Node, error) { ...@@ -134,7 +131,7 @@ func DecodeProtobufBlock(b blocks.Block) (ipld.Node, error) {
} }
decnd.cached = c decnd.cached = c
decnd.Prefix = c.Prefix() decnd.SetCidBuilder(c.Prefix())
return decnd, nil return decnd, nil
} }
......
...@@ -27,8 +27,8 @@ type ProtoNode struct { ...@@ -27,8 +27,8 @@ type ProtoNode struct {
cached *cid.Cid cached *cid.Cid
// Prefix specifies cid version and hashing function // builder specifies cid version and hashing function
Prefix cid.Prefix builder cid.Builder
} }
var v0CidPrefix = cid.Prefix{ var v0CidPrefix = cid.Prefix{
...@@ -63,14 +63,21 @@ func PrefixForCidVersion(version int) (cid.Prefix, error) { ...@@ -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 // CidBuilder returns the CID Builder for this ProtoNode, it is never nil
// it resets it the default value func (n *ProtoNode) CidBuilder() cid.Builder {
func (n *ProtoNode) SetPrefix(prefix *cid.Prefix) { if n.builder == nil {
if prefix == nil { n.builder = v0CidPrefix
n.Prefix = 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 { } else {
n.Prefix = *prefix n.builder = builder.WithCodec(cid.DagProtobuf)
n.Prefix.Codec = cid.DagProtobuf
n.encoded = nil n.encoded = nil
n.cached = nil n.cached = nil
} }
...@@ -191,7 +198,7 @@ func (n *ProtoNode) Copy() ipld.Node { ...@@ -191,7 +198,7 @@ func (n *ProtoNode) Copy() ipld.Node {
copy(nnode.links, n.links) copy(nnode.links, n.links)
} }
nnode.Prefix = n.Prefix nnode.builder = n.builder
return nnode return nnode
} }
...@@ -301,11 +308,7 @@ func (n *ProtoNode) Cid() *cid.Cid { ...@@ -301,11 +308,7 @@ func (n *ProtoNode) Cid() *cid.Cid {
return n.cached return n.cached
} }
if n.Prefix.Codec == 0 { c, err := n.builder.Sum(n.RawData())
n.SetPrefix(nil)
}
c, err := n.Prefix.Sum(n.RawData())
if err != nil { if err != nil {
// programmer error // programmer error
err = fmt.Errorf("invalid CID of length %d: %x: %v", len(n.RawData()), n.RawData(), err) err = fmt.Errorf("invalid CID of length %d: %x: %v", len(n.RawData()), n.RawData(), err)
......
...@@ -34,14 +34,10 @@ func DecodeRawBlock(block blocks.Block) (ipld.Node, error) { ...@@ -34,14 +34,10 @@ func DecodeRawBlock(block blocks.Block) (ipld.Node, error) {
var _ ipld.DecodeBlockFunc = DecodeRawBlock var _ ipld.DecodeBlockFunc = DecodeRawBlock
// NewRawNodeWPrefix creates a RawNode with the hash function // NewRawNodeWPrefix creates a RawNode using the provided cid builder
// specified in prefix. func NewRawNodeWPrefix(data []byte, builder cid.Builder) (*RawNode, error) {
func NewRawNodeWPrefix(data []byte, prefix cid.Prefix) (*RawNode, error) { builder = builder.WithCodec(cid.Raw)
prefix.Codec = cid.Raw c, err := builder.Sum(data)
if prefix.Version == 0 {
prefix.Version = 1
}
c, err := prefix.Sum(data)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment