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

Commit

Permalink
Add ethereum resolvers (#78)
Browse files Browse the repository at this point in the history
* feat: add eth resolvers
  • Loading branch information
kumavis authored and daviddias committed Feb 11, 2017
1 parent 0e555ab commit 9d1f98e
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 4 deletions.
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
"aegir": "^10.0.0",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"eth-hash-to-cid": "^0.1.0",
"ethereumjs-block": "^1.5.0",
"fs-pull-blob-store": "~0.4.1",
"idb-pull-blob-store": "~0.5.1",
"lodash": "^4.17.4",
"ncp": "^2.0.0",
"pre-commit": "^1.2.2",
"rimraf": "^2.5.4"
"rimraf": "^2.5.4",
"rlp": "^2.0.0"
},
"dependencies": {
"async": "^2.1.4",
Expand All @@ -52,7 +54,11 @@
"ipfs-repo": "~0.11.2",
"ipld-dag-cbor": "~0.9.1",
"ipld-dag-pb": "~0.9.5",
"ipld-eth-block": "~2.2.1",
"ipld-eth-block": "^2.2.1",
"ipld-eth-block-list": "^1.0.3",
"ipld-eth-state-trie": "^1.0.2",
"ipld-eth-storage-trie": "^1.0.0",
"ipld-eth-tx-trie": "^1.0.0",
"is-ipfs": "~0.3.0",
"lodash.flatten": "^4.4.0",
"lodash.includes": "^4.3.0",
Expand All @@ -69,4 +75,4 @@
"kumavis <kumavis@users.noreply.github.com>",
"wanderer <mjbecze@gmail.com>"
]
}
}
22 changes: 21 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const pullDeferSource = require('pull-defer').source
const dagPB = require('ipld-dag-pb')
const dagCBOR = require('ipld-dag-cbor')
const ipldEthBlock = require('ipld-eth-block')
const ipldEthBlockList = require('ipld-eth-block-list')
const ipldEthTxTrie = require('ipld-eth-tx-trie')
const ipldEthStateTrie = require('ipld-eth-state-trie')
const ipldEthStorageTrie = require('ipld-eth-storage-trie')

module.exports = class IPLDResolver {
constructor (blockService) {
Expand Down Expand Up @@ -45,7 +49,7 @@ module.exports = class IPLDResolver {
}
}

// Support by default dag-pb, dag-cbor, and eth-block
// Support by default dag-pb, dag-cbor, and eth-*
this.support.add(dagPB.resolver.multicodec,
dagPB.resolver,
dagPB.util)
Expand All @@ -57,6 +61,22 @@ module.exports = class IPLDResolver {
this.support.add(ipldEthBlock.resolver.multicodec,
ipldEthBlock.resolver,
ipldEthBlock.util)

this.support.add(ipldEthBlockList.resolver.multicodec,
ipldEthBlockList.resolver,
ipldEthBlockList.util)

this.support.add(ipldEthTxTrie.resolver.multicodec,
ipldEthTxTrie.resolver,
ipldEthTxTrie.util)

this.support.add(ipldEthStateTrie.resolver.multicodec,
ipldEthStateTrie.resolver,
ipldEthStateTrie.util)

this.support.add(ipldEthStorageTrie.resolver.multicodec,
ipldEthStorageTrie.resolver,
ipldEthStorageTrie.util)
}

get (cid, path, options, callback) {
Expand Down
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ describe('Browser', () => {
require('./ipld-dag-pb')(repo)
require('./ipld-dag-cbor')(repo)
require('./ipld-eth-block')(repo)
require('./ipld-eth-star')(repo)
require('./ipld-all')
})
Binary file added test/fixtures/block_302515
Binary file not shown.
Binary file added test/fixtures/block_302516
Binary file not shown.
Binary file added test/fixtures/block_302517
Binary file not shown.
Binary file added test/fixtures/state_000017_302516
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/state_00001_302516
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�Q��������p����'�(�Eׅ=X�5�msi���(�0oE�]߀����k�^<|a�P����ް��u��.iK�j�-�����
Binary file added test/fixtures/state_0000_302516
Binary file not shown.
Binary file added test/fixtures/state_000_302516
Binary file not shown.
Binary file added test/fixtures/state_00_302516
Binary file not shown.
Binary file added test/fixtures/state_0_302516
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/state_r_302516
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��'��^�9pe4����\�f��ʳ7�56�������|���̾����0ۃ�8��oTȖ}ʊf�C�Ϡc�?�B�ɣ-B!^N�1nǺ�n�nR���[��~۠YQ��f��̞Pa���#f�՝g����R��|�����X���72a���Q2ڱ����$��ť��a�,����`�F1�?Qm��3#{fM �(�wg����ޜw�G�2�?���?xN@��#�(�*(5����h1�������N\�#��U�ಚ�r��h�UqT9|��>�"��Iw����4���n3�.��R�I���I�~&V� ��$���Y��3Oʦr��^�hޕ��\i/kq�u1��U�uZ�*H9���7�^4��٪��zϿش�����Y����r���`;O'��µ��N�Y1͗w��'�2��ߠ2J?Z�V������1yF1�r66#�u��7/.����ktI2.N��8U�e����k y���|G����V�w�;Tk��g���I;���+�k��'Hiq��
Expand Down
104 changes: 104 additions & 0 deletions test/ipld-eth-star.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const rlp = require('rlp')
const BlockService = require('ipfs-block-service')
const loadFixture = require('aegir/fixtures')
const async = require('async')
const cidForHash = require('eth-hash-to-cid')
const EthBlockHeader = require('ethereumjs-block/header')
const EthTrieNode = require('merkle-patricia-tree/trieNode')

const IPLDResolver = require('../src')

module.exports = (repo) => {
describe('IPLD Resolver with eth-* (resolving across various Eth objects)', () => {
let resolver

let ethObjs

before((done) => {
const bs = new BlockService(repo)
resolver = new IPLDResolver(bs)

async.waterfall([
readFilesFixture,
generateCids,
putInStore
], done)

function readFilesFixture (cb) {
async.parallel({
child: (cb) => cb(null, loadFixture(__dirname, 'fixtures/block_302517')),
block: (cb) => cb(null, loadFixture(__dirname, 'fixtures/block_302516')),
stateRoot: (cb) => cb(null, loadFixture(__dirname, 'fixtures/state_r_302516')),
state0: (cb) => cb(null, loadFixture(__dirname, 'fixtures/state_0_302516')),
state00: (cb) => cb(null, loadFixture(__dirname, 'fixtures/state_00_302516')),
state000: (cb) => cb(null, loadFixture(__dirname, 'fixtures/state_000_302516')),
state0000: (cb) => cb(null, loadFixture(__dirname, 'fixtures/state_0000_302516')),
state00001: (cb) => cb(null, loadFixture(__dirname, 'fixtures/state_00001_302516')),
state000017: (cb) => cb(null, loadFixture(__dirname, 'fixtures/state_000017_302516'))
}, cb)
}

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)
}

cb()
}

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
default: throw new Error('Unknown type!')
}

return {
raw: rawData,
node: node,
cid: cidForHash(type, node.hash())
}
}

function putInStore (cb) {
async.each(ethObjs, (nodeData, next) => {
resolver.put(nodeData.node, { cid: nodeData.cid }, next)
}, cb)
}
})

describe('resolver.get', () => {
it('block-to-block', (done) => {
resolver.get(ethObjs.child.cid, '/parent', (err, result) => {
expect(err).to.not.exist
expect(result.path).to.equal('')
expect(result.value.number.toString('hex')).to.equal('302516')
done()
})
})

it('block-to-account resolve', (done) => {
resolver.get(ethObjs.child.cid, '/parent/state/0/0/0/0/1/7/nonce', (err, result) => {
expect(err).to.not.exist
expect(result.value.toString('hex'), '03')
expect(result.path).to.equal('')
done()
})
})
})
})
}
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ describe('Node.js', () => {
require('./ipld-dag-pb')(repo)
require('./ipld-dag-cbor')(repo)
require('./ipld-eth-block')(repo)
require('./ipld-eth-star')(repo)
require('./ipld-all')
})

0 comments on commit 9d1f98e

Please sign in to comment.