diff --git a/package.json b/package.json index 348c20f..7c7306f 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,8 @@ "multicodec": "~0.5.0", "pull-defer": "~0.2.3", "pull-stream": "^3.6.9", - "pull-traverse": "^1.0.3" + "pull-traverse": "^1.0.3", + "typical": "^3.0.0" }, "contributors": [ "Alan Shaw ", diff --git a/src/index.js b/src/index.js index 3b14b70..690c015 100644 --- a/src/index.js +++ b/src/index.js @@ -12,6 +12,7 @@ const ipldDagCbor = require('ipld-dag-cbor') const ipldDagPb = require('ipld-dag-pb') const ipldRaw = require('ipld-raw') const multicodec = require('multicodec') +const typical = require('typical') const { fancyIterator } = require('./util') function noop () {} @@ -170,40 +171,82 @@ class IPLDResolver { }) } - put (node, options, callback) { - if (typeof options === 'function') { - callback = options - return setImmediate(() => callback( - new Error('IPLDResolver.put requires options') - )) + /** + * Stores the given IPLD Nodes of a recognized IPLD Format. + * + * @param {Iterable.} nodes - Deserialized IPLD nodes that should be inserted. + * @param {number} format - The multicodec of the format that IPLD Node should be encoded in. + * @param {Object} [userOptions] - Options are applied to any of the `nodes` and is an object with the following properties. + * @param {number} [userOtions.hashAlg=hash algorithm of the given multicodec] - The hashing algorithm that is used to calculate the CID. + * @param {number} [userOptions.cidVersion=1]`- The CID version to use. + * @param {boolean} [userOptions.onlyHash=false] - If true the serialized form of the IPLD Node will not be passed to the underlying block store. + * @returns {Iterable.>} - Returns an async iterator with the CIDs of the serialized IPLD Nodes. + */ + put (nodes, format, userOptions) { + if (!typical.isIterable(nodes) || typical.isString(nodes) || + Buffer.isBuffer(nodes)) { + throw new Error('`nodes` must be an iterable') + } + if (format === undefined) { + throw new Error('`put` requires a format') + } + if (typeof format !== 'number') { + throw new Error('`format` parameter must be number (multicodec)') } - callback = callback || noop - if (options.cid && CID.isCID(options.cid)) { - if (options.onlyHash) { - return setImmediate(() => callback(null, options.cid)) - } + let options + let formatImpl - return this._put(options.cid, node, callback) - } + const next = () => { + // End iteration if there are no more nodes to put + if (nodes.length === 0) { + return Promise.resolve({ done: true }) + } - // TODO vmx 2018-12-07: Make this async/await once `put()` returns a - // Promise - this._getFormat(options.format).then((format) => { - format.util.cid(node, options, (err, cid) => { - if (err) { - return callback(err) + return new Promise(async (resolve, reject) => { + // Lazy load the options not when the iterator is initialized, but + // when we hit the first iteration. This way the constructor can be + // a synchronous function. + if (options === undefined) { + try { + formatImpl = await this._getFormat(format) + } catch (err) { + return reject(err) + } + const defaultOptions = { + hashAlg: formatImpl.defaultHashAlg, + cidVersion: 1, + onlyHash: false + } + options = mergeOptions(defaultOptions, userOptions) } - if (options.onlyHash) { - return callback(null, cid) + const node = nodes.shift() + const cidOptions = { + version: options.cidVersion, + hashAlg: options.hashAlg, + onlyHash: options.onlyHash } + formatImpl.util.cid(node, cidOptions, (err, cid) => { + if (err) { + return reject(err) + } - this._put(cid, node, callback) + if (options.onlyHash) { + return resolve({ done: false, value: cid }) + } + + this._put(cid, node, (err, cid) => { + if (err) { + return reject(err) + } + return resolve({ done: false, value: cid }) + }) + }) }) - }).catch((err) => { - callback(err) - }) + } + + return fancyIterator(next) } treeStream (cid, path, options) { diff --git a/test/basics.js b/test/basics.js index 1ac432d..a88fc46 100644 --- a/test/basics.js +++ b/test/basics.js @@ -10,6 +10,7 @@ chai.use(chaiAsProised) const BlockService = require('ipfs-block-service') const CID = require('cids') const multihash = require('multihashes') +const multicodec = require('multicodec') const pull = require('pull-stream') const inMemory = require('ipld-in-memory') @@ -62,25 +63,19 @@ module.exports = (repo) => { // }) // } - it('put - errors on unknown resolver', (done) => { + it('put - errors on unknown resolver', async () => { const bs = new BlockService(repo) const r = new IPLDResolver({ blockService: bs }) // choosing a format that is not supported - r.put(null, { format: 'blake2b-8' }, (err, result) => { - expect(err).to.exist() - expect(err.message).to.eql('No resolver found for codec "blake2b-8"') - done() - }) + const result = r.put([null], multicodec.BLAKE2B_8) + await expect(result.next()).to.be.rejectedWith( + 'No resolver found for codec "blake2b-8"') }) - it('put - errors if no options', (done) => { + it('put - errors if no format is provided', () => { const bs = new BlockService(repo) const r = new IPLDResolver({ blockService: bs }) - r.put(null, (err, result) => { - expect(err).to.exist() - expect(err.message).to.eql('IPLDResolver.put requires options') - done() - }) + expect(() => r.put([null])).to.be.throw('`put` requires a format') }) it('_put - errors on unknown resolver', (done) => { diff --git a/test/format-support.js b/test/format-support.js index 60785ec..dc362d3 100644 --- a/test/format-support.js +++ b/test/format-support.js @@ -17,17 +17,14 @@ module.exports = (repo) => { describe('IPLD format support', () => { let data, cid - before((done) => { + before(async () => { const bs = new BlockService(repo) const resolver = new IPLDResolver({ blockService: bs }) data = { now: Date.now() } - dagCBOR.util.cid(data, (err, c) => { - expect(err).to.not.exist() - cid = c - resolver.put(data, { cid }, done) - }) + const result = resolver.put([data], multicodec.DAG_CBOR) + cid = await result.last() }) describe('Dynamic format loading', () => { diff --git a/test/ipld-all.js b/test/ipld-all.js index 18b8443..7b5bd0b 100644 --- a/test/ipld-all.js +++ b/test/ipld-all.js @@ -17,6 +17,7 @@ const each = require('async/each') const waterfall = require('async/waterfall') const CID = require('cids') const inMemory = require('ipld-in-memory') +const multicodec = require('multicodec') const IPLDResolver = require('../src') @@ -52,10 +53,15 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => { cidCbor = cid each([ - { node: nodePb, cid: cidPb }, - { node: nodeCbor, cid: cidCbor } + { node: nodePb, format: multicodec.DAG_PB, cidVersion: 0 }, + { node: nodeCbor, format: multicodec.DAG_CBOR, cidVersion: 1 } ], (nac, cb) => { - resolver.put(nac.node, { cid: nac.cid }, cb) + resolver.put([nac.node], nac.format, { + cidVersion: nac.cidVersion + }).first().then( + () => cb(null), + (error) => cb(error) + ) }, cb) } ], done) @@ -76,12 +82,17 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => { it('does not store nodes when onlyHash is passed', (done) => { waterfall([ (cb) => dagPB.DAGNode.create(Buffer.from('Some data here'), cb), - (node, cb) => resolver.put(node, { - onlyHash: true, - version: 1, - hashAlg: 'sha2-256', - format: 'dag-pb' - }, cb), + (node, cb) => { + const result = resolver.put([node], multicodec.DAG_PB, { + onlyHash: true, + cidVersion: 1, + hashAlg: multicodec.SHA2_256 + }) + result.first().then( + (cid) => cb(null, cid), + (error) => cb(error) + ) + }, (cid, cb) => resolver.bs._repo.blocks.has(cid, cb) ], (error, result) => { if (error) { @@ -93,30 +104,6 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => { }) }) - it('does not store nodes when onlyHash is passed and a CID is passed', (done) => { - const cid = new CID('QmTmxQfEHbQzntsXPTU4ae2ZgBGwseBmS12AkZnKCkuf2G') - - waterfall([ - (cb) => dagPB.DAGNode.create(Buffer.from('Some data here'), cb), - (node, cb) => resolver.put(node, { - onlyHash: true, - cid - }, cb), - (cid2, cb) => { - expect(cid2).to.equal(cid) - - resolver.bs._repo.blocks.has(cid2, cb) - } - ], (error, result) => { - if (error) { - return done(error) - } - - expect(result).to.be.false() - done() - }) - }) - describe('getMany', () => { it('should return nodes correctly', (done) => { resolver.getMany([cidCbor, cidPb], (err, result) => { diff --git a/test/ipld-bitcoin.js b/test/ipld-bitcoin.js index a97e7a7..bf7f6a9 100644 --- a/test/ipld-bitcoin.js +++ b/test/ipld-bitcoin.js @@ -11,7 +11,7 @@ const BitcoinBlock = require('bitcoinjs-lib').Block const multihash = require('multihashes') const series = require('async/series') const each = require('async/each') -const pull = require('pull-stream') +const multicodec = require('multicodec') const IPLDResolver = require('../src') @@ -83,16 +83,12 @@ module.exports = (repo) => { } ], store) - function store () { - pull( - pull.values([ - { node: node1, cid: cid1 }, - { node: node2, cid: cid2 }, - { node: node3, cid: cid3 } - ]), - pull.asyncMap((nac, cb) => resolver.put(nac.node, { cid: nac.cid }, cb)), - pull.onEnd(done) - ) + async function store () { + const nodes = [node1, node2, node3] + const result = resolver.put(nodes, multicodec.BITCOIN_BLOCK) + ;[cid1, cid2, cid3] = await result.all() + + done() } }) @@ -121,34 +117,26 @@ module.exports = (repo) => { }) describe('public api', () => { - it('resolver.put', (done) => { - resolver.put(node1, { cid: cid1 }, done) - }) - - it('resolver.put with format', (done) => { - resolver.put(node1, { format: 'bitcoin-block' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('bitcoin-block') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('dbl-sha2-256') - done() - }) + it('resolver.put with format', async () => { + const result = resolver.put([node1], multicodec.BITCOIN_BLOCK) + const cid = await result.first() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('bitcoin-block') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('dbl-sha2-256') }) - it('resolver.put with format + hashAlg', (done) => { - resolver.put(node1, { format: 'bitcoin-block', hashAlg: 'sha3-512' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('bitcoin-block') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('sha3-512') - done() + it('resolver.put with format + hashAlg', async () => { + const result = resolver.put([node1], multicodec.BITCOIN_BLOCK, { + hashAlg: multicodec.SHA3_512 }) + const cid = await result.first() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('bitcoin-block') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha3-512') }) // TODO vmx 2018-11-30: Implement getting the whole object properly diff --git a/test/ipld-dag-cbor.js b/test/ipld-dag-cbor.js index afba4d8..4d99743 100644 --- a/test/ipld-dag-cbor.js +++ b/test/ipld-dag-cbor.js @@ -12,6 +12,7 @@ const dagCBOR = require('ipld-dag-cbor') const series = require('async/series') const each = require('async/each') const pull = require('pull-stream') +const multicodec = require('multicodec') const multihash = require('multihashes') const IPLDResolver = require('../src') @@ -69,16 +70,12 @@ module.exports = (repo) => { } ], store) - function store () { - pull( - pull.values([ - { node: node1, cid: cid1 }, - { node: node2, cid: cid2 }, - { node: node3, cid: cid3 } - ]), - pull.asyncMap((nac, cb) => resolver.put(nac.node, { cid: nac.cid }, cb)), - pull.onEnd(done) - ) + async function store () { + const nodes = [node1, node2, node3] + const result = resolver.put(nodes, multicodec.DAG_CBOR) + ;[cid1, cid2, cid3] = await result.all() + + done() } }) @@ -107,34 +104,27 @@ module.exports = (repo) => { }) describe('public api', () => { - it('resolver.put with CID', (done) => { - resolver.put(node1, { cid: cid1 }, done) - }) - - it('resolver.put with format', (done) => { - resolver.put(node1, { format: 'dag-cbor' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('dag-cbor') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('sha2-256') - done() - }) + it('resolver.put with format', async () => { + const result = resolver.put([node1], multicodec.DAG_CBOR) + const cid = await result.first() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('dag-cbor') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha2-256') }) - it('resolver.put with format + hashAlg', (done) => { - resolver.put(node1, { format: 'dag-cbor', hashAlg: 'sha3-512' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('dag-cbor') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('sha3-512') - done() + it('resolver.put with format + hashAlg', async () => { + const result = resolver.put([node1], multicodec.DAG_CBOR, { + hashAlg: multicodec.SHA3_512 }) + const cid = await result.first() + expect(cid).to.exist() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('dag-cbor') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha3-512') }) // TODO vmx 2018-11-30: Implement getting the whole object properly diff --git a/test/ipld-dag-pb.js b/test/ipld-dag-pb.js index efffcfe..bb2eeaa 100644 --- a/test/ipld-dag-pb.js +++ b/test/ipld-dag-pb.js @@ -9,8 +9,8 @@ const BlockService = require('ipfs-block-service') const dagPB = require('ipld-dag-pb') const series = require('async/series') const each = require('async/each') -const pull = require('pull-stream') const multihash = require('multihashes') +const multicodec = require('multicodec') const IPLDResolver = require('../src') module.exports = (repo) => { @@ -95,44 +95,14 @@ module.exports = (repo) => { }) }) } - ], cids) + ], store) - function cids () { - series([ - (cb) => { - dagPB.util.cid(node1, (err, cid) => { - expect(err).to.not.exist() - cid1 = cid - cb() - }) - }, - (cb) => { - dagPB.util.cid(node2, (err, cid) => { - expect(err).to.not.exist() - cid2 = cid - cb() - }) - }, - (cb) => { - dagPB.util.cid(node3, (err, cid) => { - expect(err).to.not.exist() - cid3 = cid - cb() - }) - } - ], store) - } + async function store () { + const nodes = [node1, node2, node3] + const result = resolver.put(nodes, multicodec.DAG_PB) + ;[cid1, cid2, cid3] = await result.all() - function store () { - pull( - pull.values([ - { node: node1, cid: cid1 }, - { node: node2, cid: cid2 }, - { node: node3, cid: cid3 } - ]), - pull.asyncMap((nac, cb) => resolver.put(nac.node, { cid: nac.cid }, cb)), - pull.onEnd(done) - ) + done() } }) @@ -160,34 +130,26 @@ module.exports = (repo) => { }) describe('public api', () => { - it('resolver.put with CID', (done) => { - resolver.put(node1, { cid: cid1 }, done) - }) - - it('resolver.put with format', (done) => { - resolver.put(node1, { format: 'dag-pb' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(0) - expect(cid.codec).to.equal('dag-pb') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('sha2-256') - done() - }) + it('resolver.put with format', async () => { + const result = resolver.put([node1], multicodec.DAG_PB) + const cid = await result.first() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('dag-pb') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha2-256') }) - it('resolver.put with format + hashAlg', (done) => { - resolver.put(node1, { format: 'dag-pb', hashAlg: 'sha3-512' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('dag-pb') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('sha3-512') - done() + it('resolver.put with format + hashAlg', async () => { + const result = resolver.put([node1], multicodec.DAG_PB, { + hashAlg: multicodec.SHA3_512 }) + const cid = await result.first() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('dag-pb') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha3-512') }) // TODO vmx 2018-11-29: Change this test to use the new `get()` @@ -215,7 +177,7 @@ module.exports = (repo) => { const node1 = await result.first() expect(node1.remainderPath).to.eql('Data') - expect(node1.value).to.eql(cid1) + expect(node1.value).to.eql(cid1.toV0()) const node2 = await result.first() expect(node2.remainderPath).to.eql('') @@ -227,11 +189,11 @@ module.exports = (repo) => { const node1 = await result.first() expect(node1.remainderPath).to.eql('Links/0/Hash/Data') - expect(node1.value).to.eql(cid2) + expect(node1.value).to.eql(cid2.toV0()) const node2 = await result.first() expect(node2.remainderPath).to.eql('Data') - expect(node2.value).to.eql(cid1) + expect(node2.value).to.eql(cid1.toV0()) const node3 = await result.first() expect(node3.remainderPath).to.eql('') diff --git a/test/ipld-eth-block.js b/test/ipld-eth-block.js index 1231380..72f25fa 100644 --- a/test/ipld-eth-block.js +++ b/test/ipld-eth-block.js @@ -9,9 +9,8 @@ const BlockService = require('ipfs-block-service') const ipldEthBlock = require('ipld-ethereum').ethBlock const EthBlockHeader = require('ethereumjs-block/header') const multihash = require('multihashes') -const series = require('async/series') const each = require('async/each') -const pull = require('pull-stream') +const multicodec = require('multicodec') const IPLDResolver = require('../src') @@ -26,62 +25,28 @@ module.exports = (repo) => { let cid2 let cid3 - before((done) => { + before(async () => { const bs = new BlockService(repo) resolver = new IPLDResolver({ blockService: bs, formats: [ipldEthBlock] }) - series([ - (cb) => { - node1 = new EthBlockHeader({ - number: 1 - }) - - ipldEthBlock.util.cid(node1, (err, cid) => { - expect(err).to.not.exist() - cid1 = cid - cb() - }) - }, - (cb) => { - node2 = new EthBlockHeader({ - number: 2, - parentHash: node1.hash() - }) - - ipldEthBlock.util.cid(node2, (err, cid) => { - expect(err).to.not.exist() - cid2 = cid - cb() - }) - }, - (cb) => { - node3 = new EthBlockHeader({ - number: 3, - parentHash: node2.hash() - }) - - ipldEthBlock.util.cid(node3, (err, cid) => { - expect(err).to.not.exist() - cid3 = cid - cb() - }) - } - ], store) - - function store () { - pull( - pull.values([ - { node: node1, cid: cid1 }, - { node: node2, cid: cid2 }, - { node: node3, cid: cid3 } - ]), - pull.asyncMap((nac, cb) => resolver.put(nac.node, { cid: nac.cid }, cb)), - pull.onEnd(done) - ) - } + node1 = new EthBlockHeader({ + number: 1 + }) + node2 = new EthBlockHeader({ + number: 2, + parentHash: node1.hash() + }) + node3 = new EthBlockHeader({ + number: 3, + parentHash: node2.hash() + }) + + const nodes = [node1, node2, node3] + const result = resolver.put(nodes, multicodec.ETH_BLOCK) + ;[cid1, cid2, cid3] = await result.all() }) describe('internals', () => { @@ -111,34 +76,26 @@ module.exports = (repo) => { }) describe('public api', () => { - it('resolver.put', (done) => { - resolver.put(node1, { cid: cid1 }, done) - }) - - it('resolver.put with format', (done) => { - resolver.put(node1, { format: 'eth-block' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('eth-block') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('keccak-256') - done() - }) + it('resolver.put with format', async () => { + const result = resolver.put([node1], multicodec.ETH_BLOCK) + const cid = await result.first() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('eth-block') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('keccak-256') }) - it('resolver.put with format + hashAlg', (done) => { - resolver.put(node1, { format: 'eth-block', hashAlg: 'keccak-512' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('eth-block') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('keccak-512') - done() + it('resolver.put with format + hashAlg', async () => { + const result = resolver.put([node1], multicodec.ETH_BLOCK, { + hashAlg: multicodec.KECCAK_512 }) + const cid = await result.first() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('eth-block') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('keccak-512') }) // TODO vmx 2018-11-30: Implement getting the whole object properly diff --git a/test/ipld-eth.js b/test/ipld-eth.js index 0e1579e..ed6a200 100644 --- a/test/ipld-eth.js +++ b/test/ipld-eth.js @@ -13,8 +13,7 @@ const loadFixture = require('aegir/fixtures') const async = require('async') const EthBlockHeader = require('ethereumjs-block/header') const EthTrieNode = require('merkle-patricia-tree/trieNode') -const multihashes = require('multihashes') -const CID = require('cids') +const multicodec = require('multicodec') const IPLDResolver = require('../src') @@ -34,8 +33,7 @@ module.exports = (repo) => { async.waterfall([ readFilesFixture, - generateCids, - putInStore + generateCids ], done) function readFilesFixture (cb) { @@ -54,48 +52,44 @@ module.exports = (repo) => { function generateCids (fileData, cb) { ethObjs = { - child: generateForType('child', 'eth-block', fileData.child), - block: generateForType('block', 'eth-block', fileData.block), - stateRoot: generateForType('stateRoot', 'eth-state-trie', fileData.stateRoot), - state0: generateForType('state0', 'eth-state-trie', fileData.state0), - state00: generateForType('state00', 'eth-state-trie', fileData.state00), - state000: generateForType('state000', 'eth-state-trie', fileData.state000), - state0000: generateForType('state0000', 'eth-state-trie', fileData.state0000), - state00001: generateForType('state00001', 'eth-state-trie', fileData.state00001), - state000017: generateForType('state000017', 'eth-state-trie', fileData.state000017) + child: generateForType('child', multicodec.ETH_BLOCK, fileData.child), + block: generateForType('block', multicodec.ETH_BLOCK, fileData.block), + stateRoot: generateForType('stateRoot', multicodec.ETH_STATE_TRIE, fileData.stateRoot), + state0: generateForType('state0', multicodec.ETH_STATE_TRIE, fileData.state0), + state00: generateForType('state00', multicodec.ETH_STATE_TRIE, fileData.state00), + state000: generateForType('state000', multicodec.ETH_STATE_TRIE, fileData.state000), + state0000: generateForType('state0000', multicodec.ETH_STATE_TRIE, fileData.state0000), + state00001: generateForType('state00001', multicodec.ETH_STATE_TRIE, fileData.state00001), + state000017: generateForType('state000017', multicodec.ETH_STATE_TRIE, fileData.state000017) } cb() } - function generateForType (label, type, rawData) { + async function generateForType (label, type, rawData) { let node switch (type) { - case 'eth-block': node = new EthBlockHeader(rawData); break - case 'eth-state-trie': node = new EthTrieNode(rlp.decode(rawData)); break + case multicodec.ETH_BLOCK: node = new EthBlockHeader(rawData); break + case multicodec.ETH_STATE_TRIE: node = new EthTrieNode(rlp.decode(rawData)); break default: throw new Error('Unknown type!') } - const multihash = multihashes.encode(node.hash(), 'keccak-256') - const cid = new CID(1, type, multihash) + const result = resolver.put([node], type) + const cid = await result.first() + return { raw: rawData, node: node, cid: cid } } - - function putInStore (cb) { - async.each(ethObjs, (nodeData, next) => { - resolver.put(nodeData.node, { cid: nodeData.cid }, next) - }, cb) - } }) describe('resolver.resolve', () => { it('block-to-block', async () => { - const result = resolver.resolve(ethObjs.child.cid, 'parent') + const child = await ethObjs.child + const result = resolver.resolve(child.cid, 'parent') const node1 = await result.first() expect(node1.remainderPath).to.eql('') @@ -106,7 +100,8 @@ module.exports = (repo) => { }) it('block-to-account resolve', async () => { - const result = resolver.resolve(ethObjs.child.cid, + const child = await ethObjs.child + const result = resolver.resolve(child.cid, 'parent/state/0/0/0/0/1/7/2/7/8/a/1/e/6/e/9/6/3/5/e/1/a/3/f/1/1/e/b/0/2/2/d/a/1/f/5/7/e/a/0/0/4/d/8/5/2/d/9/d/1/9/4/2/d/4/3/6/0/8/5/4/0/4/7/1/nonce') const node = await result.last() expect(node.value.toString('hex'), '03') diff --git a/test/ipld-git.js b/test/ipld-git.js index 529986f..1111432 100644 --- a/test/ipld-git.js +++ b/test/ipld-git.js @@ -10,7 +10,7 @@ const ipldGit = require('ipld-git') const multihash = require('multihashes') const series = require('async/series') const each = require('async/each') -const pull = require('pull-stream') +const multicodec = require('multicodec') const IPLDResolver = require('../src') @@ -134,18 +134,12 @@ module.exports = (repo) => { } ], store) - function store () { - pull( - pull.values([ - { node: blobNode, cid: blobCid }, - { node: treeNode, cid: treeCid }, - { node: commitNode, cid: commitCid }, - { node: commit2Node, cid: commit2Cid }, - { node: tagNode, cid: tagCid } - ]), - pull.asyncMap((nac, cb) => resolver.put(nac.node, { cid: nac.cid }, cb)), - pull.onEnd(done) - ) + async function store () { + const nodes = [blobNode, treeNode, commitNode, commit2Node, tagNode] + const result = resolver.put(nodes, multicodec.GIT_RAW) + ;[blobCid, treeCid, commitCid, commit2Cid, tagCid] = await result.all() + + done() } }) @@ -176,34 +170,26 @@ module.exports = (repo) => { }) describe('public api', () => { - it('resolver.put', (done) => { - resolver.put(blobNode, { cid: blobCid }, done) - }) - - it('resolver.put with format', (done) => { - resolver.put(blobNode, { format: 'git-raw' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('git-raw') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('sha1') - done() - }) + it('resolver.put with format', async () => { + const result = resolver.put([blobNode], multicodec.GIT_RAW) + const cid = await result.first() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('git-raw') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha1') }) - it('resolver.put with format + hashAlg', (done) => { - resolver.put(blobNode, { format: 'git-raw', hashAlg: 'sha3-512' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('git-raw') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('sha3-512') - done() + it('resolver.put with format + hashAlg', async () => { + const result = resolver.put([blobNode], multicodec.GIT_RAW, { + hashAlg: multicodec.SHA3_512 }) + const cid = await result.first() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('git-raw') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha3-512') }) // TODO vmx 2018-11-30: Implement getting the whole object properly diff --git a/test/ipld-zcash.js b/test/ipld-zcash.js index 0980e5d..19b4c39 100644 --- a/test/ipld-zcash.js +++ b/test/ipld-zcash.js @@ -11,7 +11,7 @@ const ZcashBlockHeader = require('zcash-bitcore-lib').BlockHeader const multihash = require('multihashes') const series = require('async/series') const each = require('async/each') -const pull = require('pull-stream') +const multicodec = require('multicodec') const IPLDResolver = require('../src') @@ -88,16 +88,12 @@ module.exports = (repo) => { } ], store) - function store () { - pull( - pull.values([ - { node: node1, cid: cid1 }, - { node: node2, cid: cid2 }, - { node: node3, cid: cid3 } - ]), - pull.asyncMap((nac, cb) => resolver.put(nac.node, { cid: nac.cid }, cb)), - pull.onEnd(done) - ) + async function store () { + const nodes = [node1, node2, node3] + const result = resolver.put(nodes, multicodec.ZCASH_BLOCK) + ;[cid1, cid2, cid3] = await result.all() + + done() } }) @@ -126,34 +122,26 @@ module.exports = (repo) => { }) describe('public api', () => { - it('resolver.put', (done) => { - resolver.put(node1, { cid: cid1 }, done) - }) - - it('resolver.put with format', (done) => { - resolver.put(node1, { format: 'zcash-block' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('zcash-block') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('dbl-sha2-256') - done() - }) + it('resolver.put with format', async () => { + const result = resolver.put([node1], multicodec.ZCASH_BLOCK) + const cid = await result.first() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('zcash-block') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('dbl-sha2-256') }) - it('resolver.put with format + hashAlg', (done) => { - resolver.put(node1, { format: 'zcash-block', hashAlg: 'sha3-512' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('zcash-block') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('sha3-512') - done() + it('resolver.put with format + hashAlg', async () => { + const result = resolver.put([node1], multicodec.ZCASH_BLOCK, { + hashAlg: multicodec.SHA3_512 }) + const cid = await result.first() + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('zcash-block') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha3-512') }) // // TODO vmx 2018-11-30: Implement getting the whole object properly