From 2d2185bc4804732a55cbc84d81c5deb12692a9a7 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Wed, 2 Nov 2016 15:27:12 +0100 Subject: [PATCH] fix: async .key --- src/cli/commands/block/put.js | 7 ++++--- src/core/components/block.js | 26 ++++++++++++++++++-------- src/http-api/resources/block.js | 14 ++++++++++---- test/core/both/test-bitswap.js | 11 ++++++++--- test/http-api/ipfs-api/test-block.js | 13 ++++++++----- 5 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/cli/commands/block/put.js b/src/cli/commands/block/put.js index 377285481e..861888418d 100644 --- a/src/cli/commands/block/put.js +++ b/src/cli/commands/block/put.js @@ -18,13 +18,14 @@ function addBlock (buf) { waterfall([ (cb) => Block.create(buf, cb), - (block, cb) => ipfs.block.put(block, cb) - ], (err, block) => { + (block, cb) => ipfs.block.put(block, cb), + (block, cb) => block.key(cb) + ], (err, key) => { if (err) { throw err } - console.log(mh.toB58String(block.key())) + console.log(mh.toB58String(key)) }) }) } diff --git a/src/core/components/block.js b/src/core/components/block.js index 1f69ae968a..c86db76fea 100644 --- a/src/core/components/block.js +++ b/src/core/components/block.js @@ -3,6 +3,7 @@ const Block = require('ipfs-block') const multihash = require('multihashes') const CID = require('cids') +const waterfall = require('async/waterfall') module.exports = function block (self) { return { @@ -17,21 +18,30 @@ module.exports = function block (self) { if (Buffer.isBuffer(block)) { block = new Block(block) - return } if (typeof cid === 'function') { // legacy (without CID) callback = cid - cid = new CID(block.key('sha2-256')) + cid === undefined } - self._blockService.put({ - block: block, - cid: cid - }, (err) => { - callback(err, block) - }) + waterfall([ + (cb) => { + if (cid) { + return cb(null, cid) + } + + block.key('sha2-256', (err, key) => { + if (err) { + return cb(err) + } + + cb(null, new CID(key)) + }) + }, + (cid, cb) => self._blockService.put({block: block, cid: cid}) + ], callback) }, rm: (cid, callback) => { cid = cleanCid(cid) diff --git a/src/http-api/resources/block.js b/src/http-api/resources/block.js index c95fca1990..47b6194f37 100644 --- a/src/http-api/resources/block.js +++ b/src/http-api/resources/block.js @@ -85,8 +85,14 @@ exports.put = { waterfall([ (cb) => Block.create(data, cb), - (block, cb) => ipfs.block.put(block, cb) - ], (err, block) => { + (block, cb) => ipfs.block.put(block, cb), + (block, cb) => block.key('sha2-256', (err, key) => { + if (err) { + return cb(err) + } + cb(null, [key, block]) + }) + ], (err, res) => { if (err) { log.error(err) return reply({ @@ -96,8 +102,8 @@ exports.put = { } return reply({ - Key: mh.toB58String(block.key('sha2-256')), - Size: block.data.length + Key: mh.toB58String(res[0]), + Size: res[1].data.length }) }) } diff --git a/test/core/both/test-bitswap.js b/test/core/both/test-bitswap.js index ab59382086..199807f880 100644 --- a/test/core/both/test-bitswap.js +++ b/test/core/both/test-bitswap.js @@ -7,6 +7,7 @@ const _ = require('lodash') const series = require('async/series') const waterfall = require('async/waterfall') const parallel = require('async/parallel') +const map = require('async/map') const leftPad = require('left-pad') const Block = require('ipfs-block') const mh = require('multihashes') @@ -126,7 +127,8 @@ describe.skip('bitswap', () => { cb() }, (cb) => remoteNode.block.put(block, cb), - (cb) => inProcNode.block.get(block.key('sha2-256'), cb), + (cb) => block.key('sha2-256', cb), + (key, cb) => inProcNode.block.get(key, cb), (b, cb) => { expect(b.data).to.be.eql(block.data) cb() @@ -145,8 +147,11 @@ describe.skip('bitswap', () => { (cb) => parallel(_.range(6).map((i) => makeBlock), (err, _blocks) => { expect(err).to.not.exist blocks = _blocks - keys = blocks.map((b) => b.key('sha2-256')) - cb() + map(blocks, (b, cb) => b.key('sha2-256', cb), (err, res) => { + expect(err).to.not.exist + keys = res + cb() + }) }), (cb) => addNode(8, (err, _ipfs) => { remoteNodes.push(_ipfs) diff --git a/test/http-api/ipfs-api/test-block.js b/test/http-api/ipfs-api/test-block.js index 490d2472a3..e2e8a7933d 100644 --- a/test/http-api/ipfs-api/test-block.js +++ b/test/http-api/ipfs-api/test-block.js @@ -3,6 +3,7 @@ const expect = require('chai').expect const multihash = require('multihashes') +const waterfall = require('async/waterfall') module.exports = (ctl) => { describe('.block', () => { @@ -14,11 +15,13 @@ module.exports = (ctl) => { size: 12 } - ctl.block.put(data, (err, block) => { - expect(err).not.to.exist - expect(block.key()).to.deep.equal(multihash.fromB58String(expectedResult.key)) - done() - }) + waterfall([ + (cb) => ctl.block.put(data, cb), + (block, cb) => block.key(cb), + (key, cb) => { + expect(key).to.deep.equal(multihash.fromB58String(expectedResult.key)) + } + ], done) }) })