-
Notifications
You must be signed in to change notification settings - Fork 105
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
feat: blockBrokers can control block validation #285
Merged
SgtPooki
merged 3 commits into
fix/block-broker-testing
from
fix/block-broker-testing-invert-blockValidation-control
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { BitswapBlockBroker, BitswapBlockBrokerFactory } from './bitswap-block-broker.js' | ||
export { TrustedGatewayBlockBroker } from './trustless-gateway-block-broker.js' | ||
export { TrustlessGatewayBlockBroker } from './trustless-gateway-block-broker.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'aegir/chai' | ||
import { MemoryBlockstore } from 'blockstore-core' | ||
import delay from 'delay' | ||
import all from 'it-all' | ||
import * as raw from 'multiformats/codecs/raw' | ||
import Sinon from 'sinon' | ||
import { type StubbedInstance, stubInterface } from 'sinon-ts' | ||
import { defaultHashers } from '../../src/utils/default-hashers.js' | ||
import { NetworkedStorage } from '../../src/utils/networked-storage.js' | ||
import { createBlock } from '../fixtures/create-block.js' | ||
import type { BitswapBlockBroker, TrustlessGatewayBlockBroker } from '../../src/block-brokers/index.js' | ||
import type { Blockstore } from 'interface-blockstore' | ||
import type { CID } from 'multiformats/cid' | ||
|
||
describe('block-provider', () => { | ||
let storage: NetworkedStorage | ||
let blockstore: Blockstore | ||
let bitswapBlockBroker: StubbedInstance<BitswapBlockBroker> | ||
let blocks: Array<{ cid: CID, block: Uint8Array }> | ||
let gatewayBlockBroker: StubbedInstance<TrustlessGatewayBlockBroker> | ||
|
||
beforeEach(async () => { | ||
blocks = [] | ||
|
||
for (let i = 0; i < 10; i++) { | ||
blocks.push(await createBlock(raw.code, Uint8Array.from([0, 1, 2, i]))) | ||
} | ||
|
||
blockstore = new MemoryBlockstore() | ||
bitswapBlockBroker = stubInterface<BitswapBlockBroker>() | ||
gatewayBlockBroker = stubInterface<TrustlessGatewayBlockBroker>() | ||
storage = new NetworkedStorage(blockstore, { | ||
blockBrokers: [ | ||
bitswapBlockBroker, | ||
gatewayBlockBroker | ||
], | ||
hashers: defaultHashers() | ||
}) | ||
}) | ||
|
||
it('gets a block from the gatewayBlockBroker when it is not in the blockstore', async () => { | ||
const { cid, block } = blocks[0] | ||
|
||
gatewayBlockBroker.retrieve.withArgs(cid, Sinon.match.any).resolves(block) | ||
|
||
expect(await blockstore.has(cid)).to.be.false() | ||
|
||
const returned = await storage.get(cid) | ||
|
||
expect(await blockstore.has(cid)).to.be.true() | ||
expect(returned).to.equalBytes(block) | ||
expect(gatewayBlockBroker.retrieve.calledWith(cid)).to.be.true() | ||
}) | ||
|
||
it('gets many blocks from gatewayBlockBroker when they are not in the blockstore', async () => { | ||
const count = 5 | ||
|
||
for (let i = 0; i < count; i++) { | ||
const { cid, block } = blocks[i] | ||
gatewayBlockBroker.retrieve.withArgs(cid, Sinon.match.any).resolves(block) | ||
|
||
expect(await blockstore.has(cid)).to.be.false() | ||
} | ||
|
||
const retrieved = await all(storage.getMany(async function * () { | ||
for (let i = 0; i < count; i++) { | ||
yield blocks[i].cid | ||
await delay(10) | ||
} | ||
}())) | ||
|
||
expect(retrieved).to.deep.equal(new Array(count).fill(0).map((_, i) => blocks[i])) | ||
|
||
for (let i = 0; i < count; i++) { | ||
const { cid } = blocks[i] | ||
expect(gatewayBlockBroker.retrieve.calledWith(cid)).to.be.true() | ||
expect(await blockstore.has(cid)).to.be.true() | ||
} | ||
}) | ||
|
||
it('gets some blocks from gatewayBlockBroker when they are not in the blockstore', async () => { | ||
const count = 5 | ||
|
||
// blocks 0,1,3,4 are in the blockstore | ||
await blockstore.put(blocks[0].cid, blocks[0].block) | ||
await blockstore.put(blocks[1].cid, blocks[1].block) | ||
await blockstore.put(blocks[3].cid, blocks[3].block) | ||
await blockstore.put(blocks[4].cid, blocks[4].block) | ||
|
||
// block #2 comes from gatewayBlockBroker but slowly | ||
gatewayBlockBroker.retrieve.withArgs(blocks[2].cid).callsFake(async () => { | ||
await delay(100) | ||
return blocks[2].block | ||
}) | ||
|
||
const retrieved = await all(storage.getMany(async function * () { | ||
for (let i = 0; i < count; i++) { | ||
yield blocks[i].cid | ||
await delay(10) | ||
} | ||
}())) | ||
|
||
expect(retrieved).to.deep.equal(new Array(count).fill(0).map((_, i) => blocks[i])) | ||
|
||
for (let i = 0; i < count; i++) { | ||
expect(await blockstore.has(blocks[i].cid)).to.be.true() | ||
} | ||
}) | ||
|
||
it('handles incorrect bytes from a gateway', async () => { | ||
const { cid } = blocks[0] | ||
const block = blocks[1].block | ||
storage = new NetworkedStorage(blockstore, { | ||
blockBrokers: [ | ||
gatewayBlockBroker | ||
], | ||
hashers: defaultHashers() | ||
}) | ||
|
||
gatewayBlockBroker.retrieve.withArgs(cid, Sinon.match.any).resolves(block) | ||
|
||
expect(await blockstore.has(cid)).to.be.false() | ||
|
||
try { | ||
await storage.get(cid) | ||
throw new Error('should have thrown') | ||
} catch (err) { | ||
const error = err as AggregateError & { errors: Error & { code: string } } | ||
expect(error).to.be.an('error') | ||
expect(error.errors).to.be.an('array').with.lengthOf(1) | ||
expect(error.errors[0]).to.be.an('error').with.property('code', 'ERR_HASH_MISMATCH') | ||
} | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have a callback method where we tell
blockBroker
s that they gave us an invalid block, and then wait for them to handle it however they see fit, but that would require us to do another try block, and ends up in some weird "back-and-forth" control scenario when blockProviders have complex retry flows.We don't want to get in the business of handling those retry flows, so exposing a function they can call to determine whether blocks are valid or not prior to giving control back to us is the most simple & flexible method I can think of right now.
We can adjust if we see problems with this, but I think this is fairly flexible, and won't end up with duplicate validations (except where js-ipfs-bitswap is doing so internally)