From 81903dddc9d159e21f87c138576c336059d90d8f Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 9 Apr 2024 18:59:36 +0100 Subject: [PATCH] fix: identity blockstore should wrap child --- packages/blockstore-core/src/identity.ts | 58 +++++++++++++++---- .../blockstore-core/test/identity.spec.ts | 15 +++++ 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/packages/blockstore-core/src/identity.ts b/packages/blockstore-core/src/identity.ts index 059902df..da4e8776 100644 --- a/packages/blockstore-core/src/identity.ts +++ b/packages/blockstore-core/src/identity.ts @@ -1,15 +1,31 @@ import { BaseBlockstore } from './base.js' -import * as Errors from './errors.js' -import type { Pair } from 'interface-blockstore' -import type { Await, AwaitIterable } from 'interface-store' +import { Errors } from './index.js' +import type { Blockstore, Pair } from 'interface-blockstore' +import type { AbortOptions, Await, AwaitIterable } from 'interface-store' import type { CID } from 'multiformats/cid' // https://github.com/multiformats/multicodec/blob/d06fc6194710e8909bac64273c43f16b56ca4c34/table.csv#L2 const IDENTITY_CODEC = 0x00 export class IdentityBlockstore extends BaseBlockstore { - put (key: CID): CID { - return key + private readonly child?: Blockstore + + constructor (child?: Blockstore) { + super() + + this.child = child + } + + put (key: CID, block: Uint8Array): Await { + if (key.code === IDENTITY_CODEC) { + return key + } + + if (this.child == null) { + return key + } + + return this.child.put(key, block) } get (key: CID): Await { @@ -17,18 +33,38 @@ export class IdentityBlockstore extends BaseBlockstore { return key.multihash.digest } - throw Errors.notFoundError() - } + if (this.child == null) { + throw Errors.notFoundError() + } - has (key: CID): boolean { - return key.code === IDENTITY_CODEC + return this.child.get(key) } - delete (): void { + has (key: CID): Await { + if (key.code === IDENTITY_CODEC) { + return true + } + if (this.child == null) { + return false + } + + return this.child.has(key) } - * getAll (): AwaitIterable { + delete (key: CID): Await { + if (key.code === IDENTITY_CODEC) { + return + } + + if (this.child != null) { + return this.child.delete(key) + } + } + * getAll (options?: AbortOptions): AwaitIterable { + if (this.child != null) { + yield * this.child.getAll(options) + } } } diff --git a/packages/blockstore-core/test/identity.spec.ts b/packages/blockstore-core/test/identity.spec.ts index e0bfcdfc..2913dd99 100644 --- a/packages/blockstore-core/test/identity.spec.ts +++ b/packages/blockstore-core/test/identity.spec.ts @@ -7,13 +7,16 @@ import * as raw from 'multiformats/codecs/raw' import { identity } from 'multiformats/hashes/identity' import { sha256 } from 'multiformats/hashes/sha2' import { IdentityBlockstore } from '../src/identity.js' +import { MemoryBlockstore } from '../src/memory.js' import type { Blockstore } from 'interface-blockstore' describe('identity', () => { let blockstore: Blockstore + let child: Blockstore beforeEach(() => { blockstore = new IdentityBlockstore() + child = new MemoryBlockstore() }) it('has an identity CID', () => { @@ -25,6 +28,18 @@ describe('identity', () => { expect(blockstore.get(cid)).to.equalBytes(block) }) + it('retrieves CIDs from child', async () => { + const block = Uint8Array.from([0, 1, 2, 3, 4]) + const multihash = await sha256.digest(block) + const cid = CID.createV1(raw.code, multihash) + + await child.put(cid, block) + + blockstore = new IdentityBlockstore(child) + expect(blockstore.has(cid)).to.be.true() + expect(blockstore.get(cid)).to.equalBytes(block) + }) + it('does not have a non-identity CID', async () => { const block = Uint8Array.from([0, 1, 2, 3, 4]) const multihash = await sha256.digest(block)