Skip to content

Commit

Permalink
chore: fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Apr 9, 2024
1 parent 81903dd commit 194383d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 17 deletions.
12 changes: 7 additions & 5 deletions packages/blockstore-core/src/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class IdentityBlockstore extends BaseBlockstore {
}

put (key: CID, block: Uint8Array): Await<CID> {
if (key.code === IDENTITY_CODEC) {
if (key.multihash.code === IDENTITY_CODEC) {
return key
}

Expand All @@ -29,7 +29,7 @@ export class IdentityBlockstore extends BaseBlockstore {
}

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

Expand All @@ -41,7 +41,7 @@ export class IdentityBlockstore extends BaseBlockstore {
}

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

Expand All @@ -62,9 +62,11 @@ export class IdentityBlockstore extends BaseBlockstore {
}
}

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

return []
}
}
79 changes: 67 additions & 12 deletions packages/blockstore-core/test/identity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
import all from 'it-all'
import drain from 'it-drain'
import { CID } from 'multiformats/cid'
import * as raw from 'multiformats/codecs/raw'
Expand Down Expand Up @@ -28,18 +29,6 @@ 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 Expand Up @@ -71,4 +60,70 @@ describe('identity', () => {

expect(blockstore.has(cid)).to.be.true()
})

it('puts CIDs to child', async () => {
const block = Uint8Array.from([0, 1, 2, 3, 4])
const multihash = await sha256.digest(block)
const cid = CID.createV1(raw.code, multihash)

blockstore = new IdentityBlockstore(child)

await blockstore.put(cid, block)
expect(child.has(cid)).to.be.true()
expect(child.get(cid)).to.equalBytes(block)
})

it('gets 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('has 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()
})

it('deletes 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()

await blockstore.delete(cid)

expect(blockstore.has(cid)).to.be.false()
})

it('gets all pairs 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()

const result = await all(blockstore.getAll())

expect(result).to.have.lengthOf(1)
expect(result[0].cid.toString()).to.equal(cid.toString())
})
})

0 comments on commit 194383d

Please sign in to comment.