Skip to content

Commit 3d84dd0

Browse files
authored
fix: identity blockstore should wrap child (#303)
Allow passing a child blockstore to an identity blockstore.
1 parent c536e25 commit 3d84dd0

File tree

3 files changed

+120
-11
lines changed

3 files changed

+120
-11
lines changed

packages/blockstore-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"devDependencies": {
111111
"aegir": "^42.2.3",
112112
"interface-blockstore-tests": "^6.0.0",
113+
"it-all": "^3.0.4",
113114
"uint8arrays": "^5.0.2"
114115
}
115116
}
Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,72 @@
11
import { BaseBlockstore } from './base.js'
2-
import * as Errors from './errors.js'
3-
import type { Pair } from 'interface-blockstore'
4-
import type { Await, AwaitIterable } from 'interface-store'
2+
import { Errors } from './index.js'
3+
import type { Blockstore, Pair } from 'interface-blockstore'
4+
import type { AbortOptions, Await, AwaitIterable } from 'interface-store'
55
import type { CID } from 'multiformats/cid'
66

77
// https://github.com/multiformats/multicodec/blob/d06fc6194710e8909bac64273c43f16b56ca4c34/table.csv#L2
88
const IDENTITY_CODEC = 0x00
99

1010
export class IdentityBlockstore extends BaseBlockstore {
11-
put (key: CID): CID {
12-
return key
11+
private readonly child?: Blockstore
12+
13+
constructor (child?: Blockstore) {
14+
super()
15+
16+
this.child = child
17+
}
18+
19+
put (key: CID, block: Uint8Array): Await<CID> {
20+
if (key.multihash.code === IDENTITY_CODEC) {
21+
return key
22+
}
23+
24+
if (this.child == null) {
25+
return key
26+
}
27+
28+
return this.child.put(key, block)
1329
}
1430

1531
get (key: CID): Await<Uint8Array> {
16-
if (key.code === IDENTITY_CODEC) {
32+
if (key.multihash.code === IDENTITY_CODEC) {
1733
return key.multihash.digest
1834
}
1935

20-
throw Errors.notFoundError()
36+
if (this.child == null) {
37+
throw Errors.notFoundError()
38+
}
39+
40+
return this.child.get(key)
2141
}
2242

23-
has (key: CID): boolean {
24-
return key.code === IDENTITY_CODEC
43+
has (key: CID): Await<boolean> {
44+
if (key.multihash.code === IDENTITY_CODEC) {
45+
return true
46+
}
47+
48+
if (this.child == null) {
49+
return false
50+
}
51+
52+
return this.child.has(key)
2553
}
2654

27-
delete (): void {
55+
delete (key: CID): Await<void> {
56+
if (key.code === IDENTITY_CODEC) {
57+
return
58+
}
2859

60+
if (this.child != null) {
61+
return this.child.delete(key)
62+
}
2963
}
3064

31-
* getAll (): AwaitIterable<Pair> {
65+
getAll (options?: AbortOptions): AwaitIterable<Pair> {
66+
if (this.child != null) {
67+
return this.child.getAll(options)
68+
}
3269

70+
return []
3371
}
3472
}

packages/blockstore-core/test/identity.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
/* eslint-env mocha */
22

33
import { expect } from 'aegir/chai'
4+
import all from 'it-all'
45
import drain from 'it-drain'
56
import { CID } from 'multiformats/cid'
67
import * as raw from 'multiformats/codecs/raw'
78
import { identity } from 'multiformats/hashes/identity'
89
import { sha256 } from 'multiformats/hashes/sha2'
910
import { IdentityBlockstore } from '../src/identity.js'
11+
import { MemoryBlockstore } from '../src/memory.js'
1012
import type { Blockstore } from 'interface-blockstore'
1113

1214
describe('identity', () => {
1315
let blockstore: Blockstore
16+
let child: Blockstore
1417

1518
beforeEach(() => {
1619
blockstore = new IdentityBlockstore()
20+
child = new MemoryBlockstore()
1721
})
1822

1923
it('has an identity CID', () => {
@@ -56,4 +60,70 @@ describe('identity', () => {
5660

5761
expect(blockstore.has(cid)).to.be.true()
5862
})
63+
64+
it('puts CIDs to child', async () => {
65+
const block = Uint8Array.from([0, 1, 2, 3, 4])
66+
const multihash = await sha256.digest(block)
67+
const cid = CID.createV1(raw.code, multihash)
68+
69+
blockstore = new IdentityBlockstore(child)
70+
71+
await blockstore.put(cid, block)
72+
expect(child.has(cid)).to.be.true()
73+
expect(child.get(cid)).to.equalBytes(block)
74+
})
75+
76+
it('gets CIDs from child', async () => {
77+
const block = Uint8Array.from([0, 1, 2, 3, 4])
78+
const multihash = await sha256.digest(block)
79+
const cid = CID.createV1(raw.code, multihash)
80+
81+
await child.put(cid, block)
82+
83+
blockstore = new IdentityBlockstore(child)
84+
expect(blockstore.has(cid)).to.be.true()
85+
expect(blockstore.get(cid)).to.equalBytes(block)
86+
})
87+
88+
it('has CIDs from child', async () => {
89+
const block = Uint8Array.from([0, 1, 2, 3, 4])
90+
const multihash = await sha256.digest(block)
91+
const cid = CID.createV1(raw.code, multihash)
92+
93+
await child.put(cid, block)
94+
95+
blockstore = new IdentityBlockstore(child)
96+
expect(blockstore.has(cid)).to.be.true()
97+
})
98+
99+
it('deletes CIDs from child', async () => {
100+
const block = Uint8Array.from([0, 1, 2, 3, 4])
101+
const multihash = await sha256.digest(block)
102+
const cid = CID.createV1(raw.code, multihash)
103+
104+
await child.put(cid, block)
105+
106+
blockstore = new IdentityBlockstore(child)
107+
expect(blockstore.has(cid)).to.be.true()
108+
109+
await blockstore.delete(cid)
110+
111+
expect(blockstore.has(cid)).to.be.false()
112+
})
113+
114+
it('gets all pairs from child', async () => {
115+
const block = Uint8Array.from([0, 1, 2, 3, 4])
116+
const multihash = await sha256.digest(block)
117+
const cid = CID.createV1(raw.code, multihash)
118+
119+
await child.put(cid, block)
120+
121+
blockstore = new IdentityBlockstore(child)
122+
expect(blockstore.has(cid)).to.be.true()
123+
124+
const result = await all(blockstore.getAll())
125+
126+
expect(result).to.have.lengthOf(1)
127+
expect(result[0].cid.toString()).to.equal(cid.toString())
128+
})
59129
})

0 commit comments

Comments
 (0)