Skip to content

Commit 3b5c1ef

Browse files
committed
fix: simplify Cid generation cache & usage
1 parent 2a1cd35 commit 3b5c1ef

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

node.go

+4-18
Original file line numberDiff line numberDiff line change
@@ -406,19 +406,12 @@ func (n *ProtoNode) MarshalJSON() ([]byte, error) {
406406
// Cid returns the node's Cid, calculated according to its prefix
407407
// and raw data contents.
408408
func (n *ProtoNode) Cid() cid.Cid {
409-
if n.encoded != nil && n.cached.Defined() {
410-
return n.cached
411-
}
412-
413-
c, err := n.CidBuilder().Sum(n.RawData())
414-
if err != nil {
415-
// programmer error
416-
err = fmt.Errorf("invalid CID of length %d: %x: %v", len(n.RawData()), n.RawData(), err)
409+
// re-encode if necessary and we'll get a new cached CID
410+
if _, err := n.EncodeProtobuf(false); err != nil {
417411
panic(err)
418412
}
419413

420-
n.cached = c
421-
return c
414+
return n.cached
422415
}
423416

424417
// String prints the node's Cid.
@@ -428,14 +421,7 @@ func (n *ProtoNode) String() string {
428421

429422
// Multihash hashes the encoded data of this node.
430423
func (n *ProtoNode) Multihash() mh.Multihash {
431-
// NOTE: EncodeProtobuf generates the hash and puts it in n.cached.
432-
_, err := n.EncodeProtobuf(false)
433-
if err != nil {
434-
// Note: no possibility exists for an error to be returned through here
435-
panic(err)
436-
}
437-
438-
return n.cached.Hash()
424+
return n.Cid().Hash()
439425
}
440426

441427
// Links returns a copy of the node's links.

0 commit comments

Comments
 (0)