From 1be7c1a20357bbb8ced3478f8b7dde9d694864d9 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Mon, 18 Sep 2023 20:58:16 +1000 Subject: [PATCH] fix: traverser should skip over identity CIDs --- ipldutil/traverser.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ipldutil/traverser.go b/ipldutil/traverser.go index 1aa95150..34cfd335 100644 --- a/ipldutil/traverser.go +++ b/ipldutil/traverser.go @@ -1,6 +1,7 @@ package ipldutil import ( + "bytes" "context" "errors" "io" @@ -12,7 +13,9 @@ import ( "github.com/ipld/go-ipld-prime/node/basicnode" "github.com/ipld/go-ipld-prime/traversal" "github.com/ipld/go-ipld-prime/traversal/selector" + "github.com/multiformats/go-multihash" + "github.com/ipfs/go-cid" "github.com/ipfs/go-graphsync/panics" ) @@ -165,7 +168,23 @@ func (t *traverser) NBlocksTraversed() int { return t.blocksCount } +func asIdentity(c cid.Cid) (digest []byte, ok bool, err error) { + dmh, err := multihash.Decode(c.Hash()) + if err != nil { + return nil, false, err + } + ok = dmh.Code == multihash.IDENTITY + digest = dmh.Digest + return digest, ok, nil +} + func (t *traverser) loader(lnkCtx ipld.LinkContext, lnk ipld.Link) (io.Reader, error) { + if digest, ok, err := asIdentity(lnk.(cidlink.Link).Cid); ok { + return io.NopCloser(bytes.NewReader(digest)), nil + } else if err != nil { + return nil, err + } + // A StorageReadOpener call came in; update the state and release the lock. // We can't simply unlock the mutex inside the <-t.responses case, // as then we'd deadlock with the other side trying to send.