Skip to content

fix: implement .has method from the blockstore interface #520

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 1 commit into from
Jan 27, 2023
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
"varint-decoder": "^1.0.0"
},
"devDependencies": {
"@chainsafe/libp2p-noise": "^10.0.1",
"@chainsafe/libp2p-noise": "^11.0.0",
"@libp2p/kad-dht": "^7.0.0",
"@libp2p/mplex": "^7.0.0",
"@libp2p/peer-id": "^2.0.0",
Expand All @@ -208,7 +208,7 @@
"iso-random-stream": "^2.0.0",
"it-all": "^2.0.0",
"it-drain": "^2.0.0",
"libp2p": "next",
"libp2p": "^0.42.0",
"lodash.difference": "^4.5.0",
"lodash.flatten": "^4.4.0",
"lodash.range": "^3.2.0",
Expand Down
8 changes: 8 additions & 0 deletions src/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,12 @@ export class Bitswap extends BaseBlockstore {
unwrap () {
return this.blockstore
}

/**
* @param {CID} cid
* @returns {Promise<boolean>}
*/
has (cid) {
return this.blockstore.has(cid)
}
}
24 changes: 24 additions & 0 deletions test/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ describe('start/stop', () => {
})
})

describe('blockstore', () => {
it('should support .has', async () => {
const [block] = await makeBlocks(1)
const libp2p = {
handle: () => {},
unhandle: () => {},
register: () => {},
unregister: () => {},
getConnections: () => []
}
// @ts-ignore not a full libp2p
const bitswap = new Bitswap(libp2p, new MemoryBlockstore())
await bitswap.start()

await expect(bitswap.has(block.cid)).to.eventually.be.false()

await bitswap.put(block.cid, block.data)

await expect(bitswap.has(block.cid)).to.eventually.be.true()

await bitswap.stop()
})
})

describe('bitswap without DHT', function () {
this.timeout(20 * 1000)

Expand Down