Skip to content
This repository has been archived by the owner on Jan 26, 2018. It is now read-only.

Upgrade to new block interface #4

Merged
merged 10 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ Small note: If editing the README, please conform to the [standard-readme](https

## License

[MIT](LICENSE) © 2016 Protocol Labs Inc.
[MIT](LICENSE) © 2017 Protocol Labs Inc.
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
},
"author": "",
"author": "kumavis",
"dependencies": {
"aegir": "^9.3.0",
"async": "^2.1.4",
"ipld-eth-account-snapshot": "^1.0.0",
"ipld-eth-trie": "^2.0.0"
"async": "^2.1.5",
"ethereumjs-block": "^1.5.0",
"multihashing-async": "~0.4.4",
"ipfs-block": "^0.6.0",
"ipld-eth-trie": "^2.0.0",
"ipld-eth-tx": "^1.0.0"
},
"devDependencies": {
"aegir": "^11.0.0",
"chai": "^3.5.0",
"ethereumjs-account": "^2.0.4",
"ipfs-block": "^0.5.3",
"dirty-chai": "^1.2.2",
"ethereumjs-tx": "^1.2.5",
"merkle-patricia-tree": "^2.1.2"
},
"directories": {
Expand Down
108 changes: 68 additions & 40 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict'
/* eslint max-nested-callbacks: ["error", 5] */

const async = require('async')
const each = require('async/each')
const waterfall = require('async/waterfall')
const util = require('ipld-eth-trie/src/util.js')
const resolver = require('ipld-eth-trie/src/resolver.js')
const isExternalLink = require('ipld-eth-trie/src/common').isExternalLink
const IpldEthAccountSnapshotResolver = require('ipld-eth-account-snapshot').resolver
const IpldEthTxResolver = require('ipld-eth-tx').resolver
const IpfsBlock = require('ipfs-block')
const CID = require('cids')
const multihashing = require('multihashing-async')

const trieIpldFormat = 'eth-state-trie'

Expand All @@ -23,54 +26,79 @@ exports.resolver = {
}

function resolve (block, path, callback) {
resolver.resolve(trieIpldFormat, block, path, (err, result) => {
if (err) return callback(err)
if (isExternalLink(result.value) || result.remainderPath.length === 0) {
return callback(null, result)
waterfall([
(cb) => resolver.resolve(trieIpldFormat, block, path, cb),
(result, cb) => {
if (isExternalLink(result.value) || result.remainderPath.length === 0) {
return callback(null, result)
}

// continue to resolve on node
toIpfsBlock(result.value, (err, block) => {
if (err) {
return cb(err)
}
IpldEthTxResolver.resolve(block, result.remainderPath, cb)
})
}
// continue to resolve on node
let block = new IpfsBlock(result.value)
IpldEthAccountSnapshotResolver.resolve(block, result.remainderPath, callback)
})
], callback)
}

function tree (block, options, callback) {
exports.util.deserialize(block.data, (err, trieNode) => {
if (err) return callback(err)
if (err) {
return callback(err)
}

// leaf node
if (trieNode.type === 'leaf') {
let block = new IpfsBlock(trieNode.getValue())
IpldEthAccountSnapshotResolver.tree(block, options, (err, paths) => {
if (err) return callback(err)
callback(null, paths)
})
return
return waterfall([
(cb) => toIpfsBlock(trieNode.getValue(), cb),
(block, cb) => IpldEthTxResolver.tree(block, options, cb)
], callback)
}

// non-leaf node
resolver.treeFromObject(trieIpldFormat, trieNode, options, (err, result) => {
if (err) return callback(err)
let paths = []
async.each(result, (child, next) => {
if (Buffer.isBuffer(child.value)) {
waterfall([
(cb) => resolver.treeFromObject(trieIpldFormat, trieNode, options, cb),
(result, cb) => {
let paths = []
each(result, (child, next) => {
if (!Buffer.isBuffer(child.value)) {
// node is non-leaf - add as is
paths.push(child)
return next()
}

// node is leaf - continue to tree
let key = child.key
let block = new IpfsBlock(child.value)
IpldEthAccountSnapshotResolver.tree(block, options, (err, subpaths) => {
if (err) return next(err)
subpaths.forEach((path) => {
path.path = key + '/' + path.path
})
paths = paths.concat(subpaths)
})
} else {
// node is non-leaf - add as is
paths.push(child)
next()
}
}, (err) => {
if (err) return callback(err)
callback(null, paths)
})
})
waterfall([
(cb) => toIpfsBlock(child.value, cb),
(block, cb) => IpldEthTxResolver.tree(block, options, cb),
(subpaths, cb) => {
paths = paths.concat(subpaths.map((p) => {
p.path = key + '/' + p.path
}))
cb()
}
], next)
}, (err) => {
if (err) {
return cb(err)
}
cb(null, paths)
})
}
], callback)
})
}

function toIpfsBlock (value, callback) {
multihashing(value, 'keccak-256', (err, hash) => {
if (err) {
return callback(err)
}
const cid = new CID(1, trieIpldFormat, hash)
callback(null, new IpfsBlock(value, cid))
})
}
Loading