Skip to content

Commit

Permalink
fix: identity blockstore should wrap child
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Apr 9, 2024
1 parent c536e25 commit 81903dd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
58 changes: 47 additions & 11 deletions packages/blockstore-core/src/identity.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,70 @@
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<CID> {
if (key.code === IDENTITY_CODEC) {
return key
}

Check warning on line 22 in packages/blockstore-core/src/identity.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-core/src/identity.ts#L21-L22

Added lines #L21 - L22 were not covered by tests

if (this.child == null) {
return key
}

return this.child.put(key, block)

Check warning on line 28 in packages/blockstore-core/src/identity.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-core/src/identity.ts#L27-L28

Added lines #L27 - L28 were not covered by tests
}

get (key: CID): Await<Uint8Array> {
if (key.code === IDENTITY_CODEC) {
return key.multihash.digest
}

throw Errors.notFoundError()
}
if (this.child == null) {
throw Errors.notFoundError()

Check warning on line 37 in packages/blockstore-core/src/identity.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-core/src/identity.ts#L37

Added line #L37 was not covered by tests
}

has (key: CID): boolean {
return key.code === IDENTITY_CODEC
return this.child.get(key)
}

delete (): void {
has (key: CID): Await<boolean> {
if (key.code === IDENTITY_CODEC) {
return true
}

if (this.child == null) {
return false
}

return this.child.has(key)
}

* getAll (): AwaitIterable<Pair> {
delete (key: CID): Await<void> {
if (key.code === IDENTITY_CODEC) {
return
}

if (this.child != null) {
return this.child.delete(key)
}

Check warning on line 62 in packages/blockstore-core/src/identity.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-core/src/identity.ts#L59-L62

Added lines #L59 - L62 were not covered by tests
}

* getAll (options?: AbortOptions): AwaitIterable<Pair> {
if (this.child != null) {
yield * this.child.getAll(options)
}

Check warning on line 68 in packages/blockstore-core/src/identity.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-core/src/identity.ts#L66-L68

Added lines #L66 - L68 were not covered by tests
}
}
15 changes: 15 additions & 0 deletions packages/blockstore-core/test/identity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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)
Expand Down

0 comments on commit 81903dd

Please sign in to comment.