From 6a706230f7061f0a257b9be1685938d8db6a14ab Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 12 Feb 2024 10:58:29 +0000 Subject: [PATCH] fix: support reading identity cids Use a tiered blockstore that combines an identity blockstore with the configured blockstore to support reading identity CIDs. --- packages/car/package.json | 2 +- packages/dag-cbor/package.json | 2 +- packages/dag-json/package.json | 2 +- packages/helia/package.json | 2 +- packages/http/package.json | 2 +- packages/json/package.json | 2 +- packages/mfs/package.json | 2 +- packages/strings/package.json | 2 +- packages/unixfs/package.json | 2 +- packages/utils/package.json | 2 +- packages/utils/src/utils/networked-storage.ts | 7 ++++++- packages/utils/test/utils/networked-storage.spec.ts | 13 ++++++++++++- 12 files changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/car/package.json b/packages/car/package.json index 2babf4c1..dfa5ce43 100644 --- a/packages/car/package.json +++ b/packages/car/package.json @@ -154,7 +154,7 @@ "@helia/unixfs": "^3.0.0", "@ipld/dag-pb": "^4.0.8", "aegir": "^42.2.2", - "blockstore-core": "^4.3.10", + "blockstore-core": "^4.4.0", "ipfs-unixfs-importer": "^15.2.4", "it-to-buffer": "^4.0.5" } diff --git a/packages/dag-cbor/package.json b/packages/dag-cbor/package.json index 4d96e17c..68527fce 100644 --- a/packages/dag-cbor/package.json +++ b/packages/dag-cbor/package.json @@ -149,6 +149,6 @@ }, "devDependencies": { "aegir": "^42.2.2", - "blockstore-core": "^4.3.10" + "blockstore-core": "^4.4.0" } } diff --git a/packages/dag-json/package.json b/packages/dag-json/package.json index 539941a5..a44ec2ea 100644 --- a/packages/dag-json/package.json +++ b/packages/dag-json/package.json @@ -148,6 +148,6 @@ }, "devDependencies": { "aegir": "^42.2.2", - "blockstore-core": "^4.3.10" + "blockstore-core": "^4.4.0" } } diff --git a/packages/helia/package.json b/packages/helia/package.json index 0ee69e24..5c191dde 100644 --- a/packages/helia/package.json +++ b/packages/helia/package.json @@ -78,7 +78,7 @@ "@libp2p/webrtc": "^4.0.17", "@libp2p/websockets": "^8.0.13", "@libp2p/webtransport": "^4.0.17", - "blockstore-core": "^4.3.10", + "blockstore-core": "^4.4.0", "datastore-core": "^9.2.7", "interface-blockstore": "^5.2.9", "interface-datastore": "^8.2.10", diff --git a/packages/http/package.json b/packages/http/package.json index b6e9e80e..9be64aeb 100644 --- a/packages/http/package.json +++ b/packages/http/package.json @@ -57,7 +57,7 @@ "@helia/interface": "^4.0.0", "@helia/routers": "^1.0.0", "@helia/utils": "^0.0.1", - "blockstore-core": "^4.3.10", + "blockstore-core": "^4.4.0", "datastore-core": "^9.2.7" }, "devDependencies": { diff --git a/packages/json/package.json b/packages/json/package.json index 8bd49b15..153524fd 100644 --- a/packages/json/package.json +++ b/packages/json/package.json @@ -147,6 +147,6 @@ }, "devDependencies": { "aegir": "^42.2.2", - "blockstore-core": "^4.3.10" + "blockstore-core": "^4.4.0" } } diff --git a/packages/mfs/package.json b/packages/mfs/package.json index 1904090f..a132778b 100644 --- a/packages/mfs/package.json +++ b/packages/mfs/package.json @@ -152,7 +152,7 @@ "devDependencies": { "@ipld/dag-pb": "^4.0.8", "aegir": "^42.2.2", - "blockstore-core": "^4.3.10", + "blockstore-core": "^4.4.0", "datastore-core": "^9.2.7", "delay": "^6.0.0", "it-all": "^3.0.4", diff --git a/packages/strings/package.json b/packages/strings/package.json index e9b36ec3..7a4b326c 100644 --- a/packages/strings/package.json +++ b/packages/strings/package.json @@ -150,6 +150,6 @@ }, "devDependencies": { "aegir": "^42.2.2", - "blockstore-core": "^4.3.10" + "blockstore-core": "^4.4.0" } } diff --git a/packages/unixfs/package.json b/packages/unixfs/package.json index 7fc86403..a2099de7 100644 --- a/packages/unixfs/package.json +++ b/packages/unixfs/package.json @@ -180,7 +180,7 @@ }, "devDependencies": { "aegir": "^42.2.2", - "blockstore-core": "^4.3.10", + "blockstore-core": "^4.4.0", "delay": "^6.0.0", "iso-url": "^1.2.1", "it-all": "^3.0.4", diff --git a/packages/utils/package.json b/packages/utils/package.json index 937c4469..110bf2f7 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -62,6 +62,7 @@ "@libp2p/peer-collections": "^5.1.5", "@libp2p/utils": "^5.2.3", "any-signal": "^4.1.1", + "blockstore-core": "^4.4.0", "cborg": "^4.0.8", "interface-blockstore": "^5.2.9", "interface-datastore": "^8.2.10", @@ -78,7 +79,6 @@ "devDependencies": { "@types/sinon": "^17.0.3", "aegir": "^42.2.2", - "blockstore-core": "^4.3.10", "datastore-core": "^9.2.7", "delay": "^6.0.0", "it-all": "^3.0.4", diff --git a/packages/utils/src/utils/networked-storage.ts b/packages/utils/src/utils/networked-storage.ts index bf98500f..5e8f5964 100644 --- a/packages/utils/src/utils/networked-storage.ts +++ b/packages/utils/src/utils/networked-storage.ts @@ -1,5 +1,7 @@ import { CodeError, start, stop } from '@libp2p/interface' import { anySignal } from 'any-signal' +import { IdentityBlockstore } from 'blockstore-core/identity' +import { TieredBlockstore } from 'blockstore-core/tiered' import filter from 'it-filter' import forEach from 'it-foreach' import { CustomProgressEvent, type ProgressOptions } from 'progress-events' @@ -47,7 +49,10 @@ export class NetworkedStorage implements Blocks, Startable { */ constructor (components: NetworkedStorageComponents) { this.log = components.logger.forComponent('helia:networked-storage') - this.child = components.blockstore + this.child = new TieredBlockstore([ + new IdentityBlockstore(), + components.blockstore + ]) this.blockRetrievers = (components.blockBrokers ?? []).filter(isBlockRetriever) this.blockAnnouncers = (components.blockBrokers ?? []).filter(isBlockAnnouncer) this.hashers = components.hashers ?? {} diff --git a/packages/utils/test/utils/networked-storage.spec.ts b/packages/utils/test/utils/networked-storage.spec.ts index 46a7d4ad..7d0fd4b2 100644 --- a/packages/utils/test/utils/networked-storage.spec.ts +++ b/packages/utils/test/utils/networked-storage.spec.ts @@ -6,15 +6,18 @@ import { MemoryBlockstore } from 'blockstore-core' import delay from 'delay' import all from 'it-all' import drain from 'it-drain' +import { CID } from 'multiformats/cid' import * as raw from 'multiformats/codecs/raw' +import { identity } from 'multiformats/hashes/identity' import Sinon from 'sinon' import { type StubbedInstance, stubInterface } from 'sinon-ts' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { defaultHashers } from '../../src/utils/default-hashers.js' import { NetworkedStorage } from '../../src/utils/networked-storage.js' import { createBlock } from '../fixtures/create-block.js' import type { BlockAnnouncer, BlockRetriever } from '@helia/interface/blocks' import type { Blockstore } from 'interface-blockstore' -import type { CID } from 'multiformats/cid' describe('networked-storage', () => { let storage: NetworkedStorage @@ -185,4 +188,12 @@ describe('networked-storage', () => { expect(await blockstore.has(blocks[i].cid)).to.be.true() } }) + + it('supports identity CIDs', async () => { + const data = uint8ArrayFromString('hello world') + const cid = CID.createV1(identity.code, identity.digest(data)) + + const block = await storage.get(cid) + expect(uint8ArrayToString(block)).to.equal('hello world') + }) })