Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
eth-block initial support and tests (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis authored and daviddias committed Dec 8, 2016
1 parent 4cb3be7 commit 4380d46
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 7 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -67,4 +69,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"wanderer <mjbecze@gmail.com>"
]
}
}
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
2 changes: 1 addition & 1 deletion test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
230 changes: 230 additions & 0 deletions test/test-ipld-eth-block.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})
}

0 comments on commit 4380d46

Please sign in to comment.