From b5c7ee330e8fe4ecce515fe30d416703861f2c3e Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Thu, 29 Sep 2022 22:00:44 +1000 Subject: [PATCH] fix: simplify Cid generation cache & usage --- node.go | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/node.go b/node.go index 85f5b4f..b2f93de 100644 --- a/node.go +++ b/node.go @@ -393,19 +393,12 @@ func (n *ProtoNode) MarshalJSON() ([]byte, error) { // Cid returns the node's Cid, calculated according to its prefix // and raw data contents. func (n *ProtoNode) Cid() cid.Cid { - if n.encoded != nil && n.cached.Defined() { - return n.cached - } - - c, err := n.CidBuilder().Sum(n.RawData()) - if err != nil { - // programmer error - err = fmt.Errorf("invalid CID of length %d: %x: %v", len(n.RawData()), n.RawData(), err) + // re-encode if necessary and we'll get a new cached CID + if _, err := n.EncodeProtobuf(false); err != nil { panic(err) } - n.cached = c - return c + return n.cached } // String prints the node's Cid. @@ -415,14 +408,7 @@ func (n *ProtoNode) String() string { // Multihash hashes the encoded data of this node. func (n *ProtoNode) Multihash() mh.Multihash { - // NOTE: EncodeProtobuf generates the hash and puts it in n.cached. - _, err := n.EncodeProtobuf(false) - if err != nil { - // Note: no possibility exists for an error to be returned through here - panic(err) - } - - return n.cached.Hash() + return n.Cid().Hash() } // Links returns a copy of the node's links.