Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Use offline blockstore for pin operations #2196

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
30 changes: 20 additions & 10 deletions src/core/components/dag.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const flattenDeep = require('just-flatten-it')
const errCode = require('err-code')
const multicodec = require('multicodec')

module.exports = function dag (self) {
return {
module.exports = function dag (self, ipld) {
const dag = {
put: promisify((dagNode, options, callback) => {
if (typeof options === 'function') {
callback = options
Expand Down Expand Up @@ -53,7 +53,7 @@ module.exports = function dag (self) {
}
}

self._ipld.put(dagNode, options.format, {
ipld.put(dagNode, options.format, {
hashAlg: options.hashAlg,
cidVersion: options.version
}).then(
Expand Down Expand Up @@ -115,22 +115,30 @@ module.exports = function dag (self) {
self._preload(cid)
}

const niceError = (err) => {
if (err.code === 'ERR_NOT_FOUND') {
err.name = 'ERR_NOT_FOUND'
err.message = `Could not resolve node with CID ${cid} - ${err.message}`
}
return err
}

if (path === undefined || path === '/') {
self._ipld.get(cid).then(
ipld.get(cid).then(
(value) => {
callback(null, {
value,
remainderPath: ''
})
},
(error) => callback(error)
(error) => callback(niceError(error))
)
} else {
const result = self._ipld.resolve(cid, path)
const result = ipld.resolve(cid, path)
const promisedValue = options.localResolve ? result.first() : result.last()
promisedValue.then(
(value) => callback(null, value),
(error) => callback(error)
(error) => callback(niceError(error))
)
}
}),
Expand Down Expand Up @@ -177,7 +185,7 @@ module.exports = function dag (self) {
}

pull(
iterToPull(self._ipld.tree(cid, path, options)),
iterToPull(ipld.tree(cid, path, options)),
pull.collect(callback)
)
}),
Expand All @@ -201,11 +209,11 @@ module.exports = function dag (self) {
return setImmediate(() => callback(errCode(err, 'ERR_INVALID_CID')))
}

self.dag.get(cid, '', options, (err, res) => {
dag.get(cid, '', options, (err, res) => {
if (err) { return callback(err) }

mapAsync(res.value.Links, (link, cb) => {
self.dag._getRecursive(link.Hash, options, cb)
dag._getRecursive(link.Hash, options, cb)
}, (err, nodes) => {
// console.log('nodes:', nodes)
if (err) return callback(err)
Expand All @@ -214,4 +222,6 @@ module.exports = function dag (self) {
})
})
}

return dag
}
15 changes: 14 additions & 1 deletion src/core/components/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,30 @@ const waterfall = require('async/waterfall')
const setImmediate = require('async/setImmediate')
const errCode = require('err-code')
const multibase = require('multibase')
const Ipld = require('ipld')
const BlockService = require('ipfs-block-service')

const { resolvePath } = require('../utils')
const ipldOptions = require('../runtime/ipld-nodejs')
const components = require('../components')
const PinManager = require('./pin/pin-manager')
const PinTypes = PinManager.PinTypes

function toB58String (hash) {
return new CID(hash).toBaseEncodedString()
}

// The pinner uses BlockService to store pin sets internally. We don't want
// to expose these internal blocks to the network, so we create an offline
// BlockService (one that doesn't talk to Bitswap)
function createOfflineDag (self) {
const offlineBlockService = new BlockService(self._repo)
const offlineIpld = new Ipld(ipldOptions(offlineBlockService, self._options.ipld, self.log))
return components.dag(self, offlineIpld)
}

module.exports = (self) => {
const dag = self.dag
const dag = createOfflineDag(self)
const pinManager = new PinManager(self._repo, dag, self._options.repoOwner, self.log)

const pin = {
Expand Down
2 changes: 1 addition & 1 deletion src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class IPFS extends EventEmitter {
this.config = components.config(this)
this.block = components.block(this)
this.object = components.object(this)
this.dag = components.dag(this)
this.dag = components.dag(this, this._ipld)
this.files = components.filesMFS(this)
this.libp2p = null // assigned on start
this.swarm = components.swarm(this)
Expand Down
16 changes: 14 additions & 2 deletions test/core/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const expect = chai.expect
chai.use(dirtyChai)

const fs = require('fs')
const sinon = require('sinon')

const IPFS = require('../../src/core')
const createTempRepo = require('../utils/create-repo-nodejs')
Expand Down Expand Up @@ -190,9 +191,10 @@ describe('pin', function () {
})

it('can\'t pin item not in datastore', function () {
this.timeout(5 * 1000)
const falseHash = `${pins.root.slice(0, -2)}ss`
return expectTimeout(pin.add(falseHash), 4000)
return pin.add(falseHash)
.then(() => expect.fail('Expected ERR_NOT_FOUND error'))
.catch(err => expect(err.code).to.equal('ERR_NOT_FOUND'))
})

// TODO block rm breaks subsequent tests
Expand Down Expand Up @@ -317,6 +319,11 @@ describe('pin', function () {
beforeEach(function () {
return pin.add(pins.root)
})
before(() => {
sinon.spy(ipfs._bitswap, 'put')
sinon.spy(ipfs._bitswap, 'putMany')
})
after(() => sinon.restore())

it('flushes', function () {
return pin.ls()
Expand All @@ -330,6 +337,11 @@ describe('pin', function () {
.then(() => pin.ls())
.then(ls => expect(ls.length).to.eql(1))
})

it('does not put to bitswap', function () {
expect(ipfs._bitswap.put.called).to.equal(false)
expect(ipfs._bitswap.putMany.called).to.equal(false)
})
})

describe('locking', function () {
Expand Down