From ff4fe8a1d4986a5d577ac443f22dd06c7cb694ee Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Fri, 10 Aug 2018 16:07:02 -0400 Subject: [PATCH 1/2] Update to use new Builder interface for creating CIDs. --- coding.go | 7 ++----- node.go | 27 +++++++++++++++------------ raw.go | 7 ++----- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/coding.go b/coding.go index c880316..2f24ca1 100644 --- a/coding.go +++ b/coding.go @@ -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.Prefix().Sum(n.encoded) if err != nil { return nil, err } @@ -134,7 +131,7 @@ func DecodeProtobufBlock(b blocks.Block) (ipld.Node, error) { } decnd.cached = c - decnd.Prefix = c.Prefix() + decnd.SetPrefix(c.Prefix()) return decnd, nil } diff --git a/node.go b/node.go index 876d404..1f87612 100644 --- a/node.go +++ b/node.go @@ -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{ @@ -63,14 +63,21 @@ func PrefixForCidVersion(version int) (cid.Prefix, error) { } } +// Prefix returns the CID Prefix for this ProtoNode, it is never nil +func (n *ProtoNode) Prefix() cid.Builder { + if n.builder == nil { + n.builder = v0CidPrefix + } + return n.builder +} + // 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) { +func (n *ProtoNode) SetPrefix(prefix cid.Builder) { if prefix == nil { - n.Prefix = v0CidPrefix + n.builder = v0CidPrefix } else { - n.Prefix = *prefix - n.Prefix.Codec = cid.DagProtobuf + n.builder = prefix.WithCodec(cid.DagProtobuf) n.encoded = nil n.cached = nil } @@ -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 } @@ -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) diff --git a/raw.go b/raw.go index 4d93b72..ebfaba7 100644 --- a/raw.go +++ b/raw.go @@ -36,11 +36,8 @@ 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 - } +func NewRawNodeWPrefix(data []byte, prefix cid.Builder) (*RawNode, error) { + prefix = prefix.WithCodec(cid.Raw) c, err := prefix.Sum(data) if err != nil { return nil, err From 70cb3dd8eaba22c01cb7629c77eae412cc51e311 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Fri, 10 Aug 2018 22:15:45 -0400 Subject: [PATCH 2/2] Rename Prefix() and SetPrefix() to CidPrefix() and SetCidPrefix(). --- coding.go | 4 ++-- node.go | 14 +++++++------- raw.go | 9 ++++----- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/coding.go b/coding.go index 2f24ca1..efb5dc2 100644 --- a/coding.go +++ b/coding.go @@ -93,7 +93,7 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) { } if n.cached == nil { - c, err := n.Prefix().Sum(n.encoded) + c, err := n.CidBuilder().Sum(n.encoded) if err != nil { return nil, err } @@ -131,7 +131,7 @@ func DecodeProtobufBlock(b blocks.Block) (ipld.Node, error) { } decnd.cached = c - decnd.SetPrefix(c.Prefix()) + decnd.SetCidBuilder(c.Prefix()) return decnd, nil } diff --git a/node.go b/node.go index 1f87612..7cca8c2 100644 --- a/node.go +++ b/node.go @@ -63,21 +63,21 @@ func PrefixForCidVersion(version int) (cid.Prefix, error) { } } -// Prefix returns the CID Prefix for this ProtoNode, it is never nil -func (n *ProtoNode) Prefix() cid.Builder { +// 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 } -// 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.Builder) { - if prefix == nil { +// 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.builder = prefix.WithCodec(cid.DagProtobuf) + n.builder = builder.WithCodec(cid.DagProtobuf) n.encoded = nil n.cached = nil } diff --git a/raw.go b/raw.go index ebfaba7..d0e456a 100644 --- a/raw.go +++ b/raw.go @@ -34,11 +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.Builder) (*RawNode, error) { - prefix = prefix.WithCodec(cid.Raw) - 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) + c, err := builder.Sum(data) if err != nil { return nil, err }