Skip to content

feat: add identity blockstore #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/blockstore-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ const store = new TieredBlockstore([
])
```

## Example - IdentityBlockstore

An identity blockstore is one that deals exclusively in Identity CIDs - this is a special CID with the codec [0x00](https://github.com/multiformats/multicodec/blob/d06fc6194710e8909bac64273c43f16b56ca4c34/table.csv#L2) where the multihash digest is the data that makes up the block.

```TypeScript
import { IdentityBlockstore } from 'blockstore-core/identity'
import { CID } from 'multiformats/cid'

const blockstore = new IdentityBlockstore()

blockstore.has(CID.parse('QmFoo')) // false

blockstore.has(CID.parse('bafkqac3imvwgy3zao5xxe3de')) // true
```

# Install

```console
Expand Down
4 changes: 4 additions & 0 deletions packages/blockstore-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
"types": "./dist/src/errors.d.ts",
"import": "./dist/src/errors.js"
},
"./identity": {
"types": "./dist/src/identity.d.ts",
"import": "./dist/src/identity.js"
},
"./memory": {
"types": "./dist/src/memory.d.ts",
"import": "./dist/src/memory.js"
Expand Down
4 changes: 2 additions & 2 deletions packages/blockstore-core/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class BaseBlockstore implements Blockstore {
}
}

async delete (key: CID, options?: AbortOptions): Promise<void> {
await Promise.reject(new Error('.delete is not implemented'))
delete (key: CID, options?: AbortOptions): Await<void> {
return Promise.reject(new Error('.delete is not implemented'))
}

async * deleteMany (source: AwaitIterable<CID>, options?: AbortOptions): AwaitIterable<CID> {
Expand Down
34 changes: 34 additions & 0 deletions packages/blockstore-core/src/identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 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
}

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

throw Errors.notFoundError()
}

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

delete (): void {

}

* getAll (): AwaitIterable<Pair> {

}
}
15 changes: 15 additions & 0 deletions packages/blockstore-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@
* // ...etc
* ])
* ```
*
* @example IdentityBlockstore
*
* An identity blockstore is one that deals exclusively in Identity CIDs - this is a special CID with the codec [0x00](https://github.com/multiformats/multicodec/blob/d06fc6194710e8909bac64273c43f16b56ca4c34/table.csv#L2) where the multihash digest is the data that makes up the block.
*
* ```TypeScript
* import { IdentityBlockstore } from 'blockstore-core/identity'
* import { CID } from 'multiformats/cid'
*
* const blockstore = new IdentityBlockstore()
*
* blockstore.has(CID.parse('QmFoo')) // false
*
* blockstore.has(CID.parse('bafkqac3imvwgy3zao5xxe3de')) // true
* ```
*/

import * as ErrorsImport from './errors.js'
Expand Down
59 changes: 59 additions & 0 deletions packages/blockstore-core/test/identity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
import drain from 'it-drain'
import { CID } from 'multiformats/cid'
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 type { Blockstore } from 'interface-blockstore'

describe('identity', () => {
let blockstore: Blockstore

beforeEach(() => {
blockstore = new IdentityBlockstore()
})

it('has an identity CID', () => {
const block = Uint8Array.from([0, 1, 2, 3, 4])
const multihash = identity.digest(block)
const cid = CID.createV1(identity.code, multihash)

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)
const cid = CID.createV1(raw.code, multihash)

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

await blockstore.put(cid, block)

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

it('cannot delete an identity CID', async () => {
const block = Uint8Array.from([0, 1, 2, 3, 4])
const multihash = identity.digest(block)
const cid = CID.createV1(identity.code, multihash)

await blockstore.delete(cid)

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

it('cannot delete many identity CIDs', async () => {
const block = Uint8Array.from([0, 1, 2, 3, 4])
const multihash = identity.digest(block)
const cid = CID.createV1(identity.code, multihash)

await drain(blockstore.deleteMany([cid]))

expect(blockstore.has(cid)).to.be.true()
})
})
1 change: 1 addition & 0 deletions packages/blockstore-core/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"./src/base.ts",
"./src/black-hole.ts",
"./src/errors.ts",
"./src/identity.ts",
"./src/memory.ts",
"./src/tiered.ts"
]
Expand Down