Skip to content

fix: blocks may not be in the blockstore yet #215

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"promisify-es6": "^1.0.3",
"rimraf": "^3.0.0",
"safe-buffer": "^5.1.2",
"sinon": "^9.0.0",
"stats-lite": "^2.2.0",
"uuid": "^3.3.2"
},
Expand Down
38 changes: 35 additions & 3 deletions src/decision-engine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Ledger = require('./ledger')
const { logger, groupBy, pullAllWith, uniqWith } = require('../utils')

const MAX_MESSAGE_SIZE = 512 * 1024
const MAX_TASK_ATTEMPTS = 6

class DecisionEngine {
constructor (peerId, blockstore, network, stats) {
Expand Down Expand Up @@ -80,8 +81,41 @@ class DecisionEngine {
const cids = entries.map((e) => e.cid)
const uniqCids = uniqWith((a, b) => a.equals(b), cids)
const groupedTasks = groupBy(task => task.target.toB58String(), tasks)
const unresolvedCids = []

const blocks = await Promise.all(uniqCids.map(cid => this.blockstore.get(cid)))
const blocks = (await Promise.all(
uniqCids.map(async cid => {
try {
const block = await this.blockstore.get(cid)

return block
} catch (err) {
this._log.error(`Could not load block for cid ${cid}`, err)
unresolvedCids.push(cid)
}
}))
).filter(Boolean)

if (blocks.length !== uniqCids.length) {
// put any tasks with unresolved CIDs back into the task queue
unresolvedCids.forEach(cid => {
this._tasks = this._tasks.concat(
tasks
.filter(task => task.entry.cid.equals(cid))
.map(task => {
task.attempt = (task.attempt || 0) + 1

if (task.attempt < MAX_TASK_ATTEMPTS) {
return task
}
})
.filter(Boolean)
)
})

// run queue again later
this._outbox()
}

await Promise.all(Object.values(groupedTasks).map(async (tasks) => {
// all tasks in the group have the same target
Expand All @@ -99,8 +133,6 @@ class DecisionEngine {
this.messageSent(peer, block)
}
}))

this._tasks = []
}

wantlistForPeer (peerId) {
Expand Down
56 changes: 56 additions & 0 deletions test/decision-engine/decision-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const range = require('lodash.range')
const difference = require('lodash.difference')
const flatten = require('lodash.flatten')
const Block = require('ipfs-block')
const makeBlock = require('../utils/make-block')
const CID = require('cids')
const multihashing = require('multihashing-async')
const Buffer = require('safe-buffer').Buffer
const sinon = require('sinon')

const Message = require('../../src/types/message')
const DecisionEngine = require('../../src/decision-engine')
Expand Down Expand Up @@ -197,4 +199,58 @@ describe('Engine', () => {
.catch(reject)
})
})

it('handles the blockstore not having all the blocks', async () => {
const block = await makeBlock()

const blockstore = {
get: sinon.stub().withArgs(block.cid).throws(new Error('Not found'))
}

const engine = new DecisionEngine(await PeerId.create({ bits: 512 }), blockstore)
engine._running = true

engine._tasks.push({
entry: {
cid: block.cid
},
target: await PeerId.create({ bits: 512 })
})

await engine._processTasks()

expect(engine).to.have.nested.deep.property('_tasks[0].entry.cid', block.cid)
expect(engine).to.have.nested.deep.property('_tasks[0].attempt', 1)
})

it('does not try to resolve blocks forever', async () => {
const block = await makeBlock()

const blockstore = {
get: sinon.stub().withArgs(block.cid).throws(new Error('Not found'))
}

const engine = new DecisionEngine(await PeerId.create({ bits: 512 }), blockstore)
engine._running = true

engine._tasks.push({
entry: {
cid: block.cid
},
target: await PeerId.create({ bits: 512 })
})

await engine._processTasks()
await engine._processTasks()
await engine._processTasks()
await engine._processTasks()
await engine._processTasks()

expect(engine).to.have.nested.deep.property('_tasks[0].entry.cid', block.cid)
expect(engine).to.have.nested.deep.property('_tasks[0].attempt', 5)

await engine._processTasks()

expect(engine).to.have.property('_tasks').that.has.lengthOf(0)
})
})