From 4380d46edc1ab90a8b1938159dd4ab30d3ba8c63 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 8 Dec 2016 00:19:05 -0500 Subject: [PATCH] eth-block initial support and tests (#69) --- package.json | 10 +- src/index.js | 4 +- test/browser.js | 2 +- test/node.js | 2 +- test/test-ipld-eth-block.js | 230 ++++++++++++++++++++++++++++++++++++ 5 files changed, 241 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 770b514..734bd5f 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "aegir": "^9.1.2", "buffer-loader": "0.0.1", "chai": "^3.5.0", + "ethereumjs-block": "^1.3.1", "fs-pull-blob-store": "^0.4.1", "idb-pull-blob-store": "^0.5.1", "lodash": "^4.17.2", @@ -45,13 +46,14 @@ }, "dependencies": { "async": "^2.1.4", - "cids": "^0.2.0", + "cids": "^0.3.4", "interface-pull-blob-store": "^0.6.0", "ipfs-block": "^0.5.1", + "ipfs-block-service": "^0.7.0", + "ipfs-repo": "^0.11.1", "ipld-dag-cbor": "^0.8.2", "ipld-dag-pb": "^0.9.2", - "ipfs-repo": "^0.11.1", - "ipfs-block-service": "^0.7.0", + "ipld-eth-block": "^2.0.3", "is-ipfs": "^0.2.1", "lodash.flatten": "^4.4.0", "lodash.includes": "^4.3.0", @@ -67,4 +69,4 @@ "greenkeeperio-bot ", "wanderer " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index e04ec24..ddec076 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,7 @@ const BlockService = require('ipfs-block-service') const dagPB = require('ipld-dag-pb') const dagCBOR = require('ipld-dag-cbor') +const ipldEthBlock = require('ipld-eth-block') module.exports = class IPLDResolver { constructor (blockService) { @@ -42,9 +43,10 @@ module.exports = class IPLDResolver { } } - // Support by default dag-pb and dag-cbor + // Support by default dag-pb, dag-cbor, and eth-block this.support.add(dagPB.resolver.multicodec, dagPB.resolver, dagPB.util) this.support.add(dagCBOR.resolver.multicodec, dagCBOR.resolver, dagCBOR.util) + this.support.add(ipldEthBlock.resolver.multicodec, ipldEthBlock.resolver, ipldEthBlock.util) } resolve (cid, path, callback) { diff --git a/test/browser.js b/test/browser.js index 6cdcf94..29ede35 100644 --- a/test/browser.js +++ b/test/browser.js @@ -51,6 +51,6 @@ describe('Browser', () => { require('./test-ipld-dag-pb')(repo) require('./test-ipld-dag-cbor')(repo) - // require('./test-ipld-eth-block')(repo) + require('./test-ipld-eth-block')(repo) require('./test-ipld-all-together-now') }) diff --git a/test/node.js b/test/node.js index 3bb567e..fd17b76 100644 --- a/test/node.js +++ b/test/node.js @@ -23,6 +23,6 @@ describe('Node.js', () => { require('./test-ipld-dag-pb')(repo) require('./test-ipld-dag-cbor')(repo) - // require('./test-ipld-eth-block')(repo) + require('./test-ipld-eth-block')(repo) require('./test-ipld-all-together-now') }) diff --git a/test/test-ipld-eth-block.js b/test/test-ipld-eth-block.js index 98e1f9c..724f8ba 100644 --- a/test/test-ipld-eth-block.js +++ b/test/test-ipld-eth-block.js @@ -1,2 +1,232 @@ /* eslint-env mocha */ 'use strict' + +const expect = require('chai').expect +const BlockService = require('ipfs-block-service') +const ipldEthBlock = require('ipld-eth-block') +const EthBlockHeader = require('ethereumjs-block/header') +const series = require('async/series') +const pull = require('pull-stream') + +const IPLDResolver = require('../src') + +module.exports = (repo) => { + describe('IPLD Resolver with eth-block (IPLD Ethereum Block)', () => { + const bs = new BlockService(repo) + const resolver = new IPLDResolver(bs) + + let node1 + let node2 + let node3 + let cid1 + let cid2 + let cid3 + + before((done) => { + node1 = new EthBlockHeader({ number: 1 }) + node2 = new EthBlockHeader({ number: 2 }) + node3 = new EthBlockHeader({ number: 3 }) + + series([ + (cb) => { + ipldEthBlock.util.cid(node1, (err, cid) => { + expect(err).to.not.exist + cid1 = cid + cb() + }) + }, + (cb) => { + ipldEthBlock.util.cid(node2, (err, cid) => { + expect(err).to.not.exist + cid2 = cid + cb() + }) + }, + (cb) => { + ipldEthBlock.util.cid(node3, (err, cid) => { + expect(err).to.not.exist + cid3 = cid + cb() + }) + } + ], done) + }) + + it('resolver.put', (done) => { + resolver.put({ + node: node1, + cid: cid1 + }, done) + }) + + it('resolver.putStream', (done) => { + pull( + pull.values([ + { node: node1, cid: cid1 }, + { node: node2, cid: cid2 }, + { node: node3, cid: cid3 } + ]), + resolver.putStream(done) + ) + }) + + it('resolver.get', (done) => { + resolver.put({ + node: node1, + cid: cid1 + }, (err) => { + expect(err).to.not.exist + resolver.get(cid1, (err, node) => { + expect(err).to.not.exist + expect(node1.number.toString('hex')).to.eql('01') + expect(node1.raw).to.eql(node.raw) + expect(node1.hash()).to.eql(node.hash()) + done() + }) + }) + }) + + it('resolver.getStream', (done) => { + resolver.put({ + node: node1, + cid: cid1 + }, (err) => { + expect(err).to.not.exist + pull( + resolver.getStream(cid1), + pull.collect((err, nodes) => { + expect(err).to.not.exist + expect(node1.raw).to.eql(nodes[0].raw) + expect(node1.hash()).to.eql(nodes[0].hash()) + done() + }) + ) + }) + }) + + it('resolver.remove', (done) => { + resolver.put({ + node: node1, + cid: cid1 + }, (err) => { + expect(err).to.not.exist + resolver.get(cid1, (err, node) => { + expect(err).to.not.exist + expect(node1.raw).to.eql(node.raw) + expect(node1.hash()).to.eql(node.hash()) + remove() + }) + }) + + function remove () { + resolver.remove(cid1, (err) => { + expect(err).to.not.exist + resolver.get(cid1, (err) => { + expect(err).to.exist + done() + }) + }) + } + }) + }) + + describe('IPLD Path Resolver for eth-block', () => { + let resolver + + let node1 + let node2 + let node3 + let cid1 + let cid2 + let cid3 + + before((done) => { + resolver = new IPLDResolver() + + 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 } + ]), + resolver.putStream(done) + ) + } + }) + + it('root path (same as get)', (done) => { + resolver.resolve(cid1, '/', (err, result) => { + expect(err).to.not.exist + + ipldEthBlock.util.cid(result, (err, cid) => { + expect(err).to.not.exist + expect(cid).to.eql(cid1) + done() + }) + }) + }) + + it('value within 1st node scope', (done) => { + resolver.resolve(cid1, 'number', (err, result) => { + expect(err).to.not.exist + expect(result.toString('hex')).to.eql('01') + done() + }) + }) + + it('value within nested scope (1 level)', (done) => { + resolver.resolve(cid2, 'parent/number', (err, result) => { + expect(err).to.not.exist + expect(result.toString('hex')).to.eql('01') + done() + }) + }) + + it('value within nested scope (2 levels)', (done) => { + resolver.resolve(cid3, 'parent/parent/number', (err, result) => { + expect(err).to.not.exist + expect(result.toString('hex')).to.eql('01') + done() + }) + }) + }) +}