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

Commit

Permalink
fix: use binary blobs directly
Browse files Browse the repository at this point in the history
IPLD shouldn't need to know about IPFS. Hence work directly with
the binary data instead of using an IPFS block.

This is part of ipld/interface-ipld-format#21

BREAKING CHANGE: Everyone calling the functions of `resolve` need to
pass in the binary data instead of an IPFS block.

So if your input is an IPFS block, the code changes from

    resolver.resolve(block, path, (err, result) => {…}

to

    resolver.resolve(block.data, path, (err, result) => {…}
  • Loading branch information
vmx committed Feb 12, 2018
1 parent 5d823e6 commit e69f539
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 82 deletions.
17 changes: 6 additions & 11 deletions eth-account-snapshot/test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
const expect = require('chai').expect
const dagEthAccount = require('../index')
const resolver = dagEthAccount.resolver
const IpfsBlock = require('ipfs-block')
const Account = require('ethereumjs-account')
const toIpfsBlock = require('../../util/toIpfsBlock')
const emptyCodeHash = require('../../util/emptyCodeHash')

describe('IPLD format resolver (local)', () => {
let testIpfsBlock
let testBlob
let testData = {
nonce: new Buffer('02', 'hex'),
balance: new Buffer('04a817c800', 'hex'),
Expand All @@ -22,11 +20,8 @@ describe('IPLD format resolver (local)', () => {
const testAccount = new Account(testData)
dagEthAccount.util.serialize(testAccount, (err, result) => {
if (err) return done(err)
toIpfsBlock(resolver.multicodec, result, (err, ipfsBlock) => {
if (err) return done(err)
testIpfsBlock = ipfsBlock
done()
})
testBlob = result
done()
})
})

Expand All @@ -36,14 +31,14 @@ describe('IPLD format resolver (local)', () => {

describe('resolver.resolve', () => {
it('path within scope', () => {
resolver.resolve(testIpfsBlock, 'nonce', (err, result) => {
resolver.resolve(testBlob, 'nonce', (err, result) => {
expect(err).to.not.exist
expect(result.value.toString('hex')).to.equal(testData.nonce.toString('hex'))
})
})

it('resolves empty code', () => {
resolver.resolve(testIpfsBlock, 'code', (err, result) => {
resolver.resolve(testBlob, 'code', (err, result) => {
expect(err).to.not.exist
expect(result.remainderPath).to.equal('')
expect(Buffer.isBuffer(result.value)).to.equal(true)
Expand All @@ -54,7 +49,7 @@ describe('IPLD format resolver (local)', () => {

describe('resolver.tree', () => {
it('basic sanity test', () => {
resolver.tree(testIpfsBlock, (err, paths) => {
resolver.tree(testBlob, (err, paths) => {
expect(err).to.not.exist
expect(Array.isArray(paths)).to.eql(true)
})
Expand Down
20 changes: 8 additions & 12 deletions eth-block-list/test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@ const resolver = dagEthBlockList.resolver
const block97Data = require('./data/block97.json')
const ommerData0 = require('./data/ommer0.json')
const ommerData1 = require('./data/ommer1.json')
const toIpfsBlock = require('../../util/toIpfsBlock')

describe('IPLD format resolver (local)', () => {
let testIpfsBlock
let testBlob
let ethBlock = EthBlockFromRpc(block97Data, [ommerData0, ommerData1])

before((done) => {
dagEthBlockList.util.serialize(ethBlock.uncleHeaders, (err, result) => {
if (err) return done(err)
toIpfsBlock(resolver.multicodec, result, (err, ipfsBlock) => {
if (err) return done(err)
testIpfsBlock = ipfsBlock
done()
})
testBlob = result
done()
})
})

Expand All @@ -34,7 +30,7 @@ describe('IPLD format resolver (local)', () => {

describe('resolver.resolve', () => {
it('uncle #0', (done) => {
resolver.resolve(testIpfsBlock, '0', (err, result) => {
resolver.resolve(testBlob, '0', (err, result) => {
expect(err).to.not.exist()
expect(result.value.hash().toString('hex')).to.equal('acfa207ce9d5139b85ecfdc197f8d283fc241f95f176f008f44aab35ef1f901f')
expect(result.remainderPath).to.equal('')
Expand All @@ -43,7 +39,7 @@ describe('IPLD format resolver (local)', () => {
})

it('uncle #1', (done) => {
resolver.resolve(testIpfsBlock, '1', (err, result) => {
resolver.resolve(testBlob, '1', (err, result) => {
expect(err).to.not.exist()
expect(result.value.hash().toString('hex')).to.equal('fe426f2eb0adc88f05ea737da1ebb79e03bca546563ad74bda7bffeb37ad4d6a')
expect(result.remainderPath).to.equal('')
Expand All @@ -52,7 +48,7 @@ describe('IPLD format resolver (local)', () => {
})

it('uncle count', (done) => {
resolver.resolve(testIpfsBlock, 'count', (err, result) => {
resolver.resolve(testBlob, 'count', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.equal(2)
expect(result.remainderPath).to.equal('')
Expand All @@ -61,7 +57,7 @@ describe('IPLD format resolver (local)', () => {
})

it('resolve block data off uncle #0', (done) => {
resolver.resolve(testIpfsBlock, '0/timestamp', (err, result) => {
resolver.resolve(testBlob, '0/timestamp', (err, result) => {
expect(err).to.not.exist()
expect(result.remainderPath.length).to.equal(0)
expect(result.value.toString('hex')).to.equal('55ba43df')
Expand All @@ -73,7 +69,7 @@ describe('IPLD format resolver (local)', () => {

describe('resolver.tree', () => {
it('returns all uncles', (done) => {
resolver.tree(testIpfsBlock, (err, paths) => {
resolver.tree(testBlob, (err, paths) => {
expect(err).to.not.exist()
expect(typeof paths).to.eql('object')
expect(Array.isArray(paths)).to.eql(true)
Expand Down
9 changes: 4 additions & 5 deletions eth-block/test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const CID = require('cids')
const IpfsBlock = require('ipfs-block')
const EthBlockHeader = require('ethereumjs-block/header')
const multihashing = require('multihashing-async')
const waterfall = require('async/waterfall')
Expand All @@ -16,7 +15,7 @@ const isExternalLink = require('../../util/isExternalLink')
const resolver = ipldEthBlock.resolver

describe('IPLD format resolver (local)', () => {
let testIpfsBlock
let testBlob
let testData = {
// 12345678901234567890123456789012
parentHash: new Buffer('0100000000000000000000000000000000000000000000000000000000000000', 'hex'),
Expand Down Expand Up @@ -45,7 +44,7 @@ describe('IPLD format resolver (local)', () => {
if (err) {
return cb(err)
}
testIpfsBlock = new IpfsBlock(serialized, new CID(hash))
testBlob = serialized
cb()
})
], done)
Expand All @@ -70,15 +69,15 @@ describe('IPLD format resolver (local)', () => {

describe('resolver.resolve', () => {
it('path within scope', () => {
resolver.resolve(testIpfsBlock, 'number', (err, result) => {
resolver.resolve(testBlob, 'number', (err, result) => {
expect(err).not.to.exist()
expect(result.value.toString('hex')).to.equal(testData.number.toString('hex'))
})
})
})

it('resolver.tree', () => {
resolver.tree(testIpfsBlock, (err, paths) => {
resolver.tree(testBlob, (err, paths) => {
expect(err).not.to.exist()
expect(Array.isArray(paths)).to.eql(true)
expect(paths.length).to.eql(20)
Expand Down
30 changes: 21 additions & 9 deletions eth-state-trie/test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const multihashing = require('multihashing-async')
const CID = require('cids')
const cidFromHash = require('eth-hash-to-cid')
const ipldEthStateTrie = require('../index')
const toIpfsBlock = require('../../util/toIpfsBlock')
const isExternalLink = require('../../util/isExternalLink')
const resolver = ipldEthStateTrie.resolver

Expand Down Expand Up @@ -53,10 +52,7 @@ describe('IPLD format resolver (local)', () => {
(cb) => dumpTrieNonInlineNodes(trie, trieNodes, cb),
// (cb) => logTrie(trie, cb),
(cb) => async.mapValues(trieNodes, (node, key, cb) => {
async.waterfall([
(cb) => ipldEthStateTrie.util.serialize(node, cb),
(data, cb) => toIpfsBlock(resolver.multicodec, data, cb),
], cb)
ipldEthStateTrie.util.serialize(node, cb)
}, cb)
], (err, result) => {
if (err) {
Expand All @@ -82,6 +78,16 @@ describe('IPLD format resolver (local)', () => {
})
}

function cid (data, cb) {
multihashing(data, 'keccak-256', (err, hash) => {
if (err) {
return cb(err)
}
const cid = new CID(1, resolver.multicodec, hash)
cb(null, cid)
})
}

it('multicodec is eth-state-trie', () => {
expect(resolver.multicodec).to.equal('eth-state-trie')
})
Expand All @@ -94,8 +100,11 @@ describe('IPLD format resolver (local)', () => {
let trieNode = result.value
expect(result.remainderPath).to.eql('c/0/a/0/0/codeHash')
expect(isExternalLink(trieNode)).to.eql(true)
expect(trieNode['/']).to.eql(dagNodes['0/0/0'].cid.toBaseEncodedString())
done()
cid(dagNodes['0/0/0'], (err, cid) => {
expect(err).to.not.exist()
expect(trieNode['/']).to.eql(cid.toBaseEncodedString())
done()
})
})
})

Expand All @@ -106,8 +115,11 @@ describe('IPLD format resolver (local)', () => {
let trieNode = result.value
expect(result.remainderPath).to.eql('0/a/0/0/codeHash')
expect(isExternalLink(trieNode)).to.eql(true)
expect(trieNode['/']).to.eql(dagNodes['0/0/0/c'].cid.toBaseEncodedString())
done()
cid(dagNodes['0/0/0/c'], (err, cid) => {
expect(err).to.not.exist()
expect(trieNode['/']).to.eql(cid.toBaseEncodedString())
done()
})
})
})

Expand Down
7 changes: 1 addition & 6 deletions eth-storage-trie/test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

const expect = require('chai').expect
const async = require('async')
const IpfsBlock = require('ipfs-block')
const Trie = require('merkle-patricia-tree')
const TrieNode = require('merkle-patricia-tree/trieNode')
const ipldEthStateTrie = require('../index')
const toIpfsBlock = require('../../util/toIpfsBlock')
const isExternalLink = require('../../util/isExternalLink')
const resolver = ipldEthStateTrie.resolver

Expand All @@ -21,10 +19,7 @@ describe('IPLD format resolver (local)', () => {
async.waterfall([
(cb) => populateTrie(trie, cb),
(cb) => dumpTrieNonInlineNodes(trie, trieNodes, cb),
(cb) => async.map(trieNodes, ipldEthStateTrie.util.serialize, cb),
(nodes, cb) => async.map(nodes, (node, cb) => {
toIpfsBlock(resolver.multicodec, node, cb)
}, cb)
(cb) => async.map(trieNodes, ipldEthStateTrie.util.serialize, cb)
], (err, result) => {
if (err) return done(err)
dagNodes = result
Expand Down
7 changes: 1 addition & 6 deletions eth-tx-trie/test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

const expect = require('chai').expect
const async = require('async')
const IpfsBlock = require('ipfs-block')
const EthBlock = require('ethereumjs-block')
const EthTx = require('ethereumjs-tx')
const Trie = require('merkle-patricia-tree')
const ipldEthStateTrie = require('../index')
const toIpfsBlock = require('../../util/toIpfsBlock')
const isExternalLink = require('../../util/isExternalLink')
const resolver = ipldEthStateTrie.resolver

Expand All @@ -23,10 +21,7 @@ describe('IPLD format resolver (local)', () => {
async.waterfall([
(cb) => populateTrie(cb),
(cb) => dumpTrieDbNodes(trie, trieNodes, cb),
(cb) => async.map(trieNodes, ipldEthStateTrie.util.serialize, cb),
(nodes, cb) => async.map(nodes, (node, cb) => {
toIpfsBlock(resolver.multicodec, node, cb)
}, cb)
(cb) => async.map(trieNodes, ipldEthStateTrie.util.serialize, cb)
], (err, result) => {
if (err) return done(err)
dagNodes = result
Expand Down
15 changes: 5 additions & 10 deletions eth-tx/test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
'use strict'

const expect = require('chai').expect
const IpfsBlock = require('ipfs-block')
const Transaction = require('ethereumjs-tx')
const toIpfsBlock = require('../../util/toIpfsBlock')
const dagEthBlock = require('../index')
const resolver = dagEthBlock.resolver

describe('IPLD format resolver (local)', () => {
let testIpfsBlock
let testIpfsBlob
let testData = {
nonce: new Buffer('01', 'hex'),
gasPrice: new Buffer('04a817c800', 'hex'),
Expand All @@ -26,11 +24,8 @@ describe('IPLD format resolver (local)', () => {
const testTx = new Transaction(testData)
dagEthBlock.util.serialize(testTx, (err, result) => {
if (err) return done(err)
toIpfsBlock(resolver.multicodec, result, (err, ipfsBlock) => {
if (err) return done(err)
testIpfsBlock = ipfsBlock
done()
})
testIpfsBlob = result
done()
})
})

Expand All @@ -40,7 +35,7 @@ describe('IPLD format resolver (local)', () => {

describe('resolver.resolve', () => {
it('path within scope', () => {
resolver.resolve(testIpfsBlock, 'nonce', (err, result) => {
resolver.resolve(testIpfsBlob, 'nonce', (err, result) => {
expect(err).to.not.exist()
expect(result.value.toString('hex')).to.equal(testData.nonce.toString('hex'))
// expect(result.value).to.equal(testData.nonce.toString('hex'))
Expand All @@ -50,7 +45,7 @@ describe('IPLD format resolver (local)', () => {

describe('resolver.resolve', () => {
it('resolver.tree', () => {
resolver.tree(testIpfsBlock, (err, paths) => {
resolver.tree(testIpfsBlob, (err, paths) => {
expect(err).to.not.exist()
expect(typeof paths).to.eql('object')
// expect(Array.isArray(paths)).to.eql(true)
Expand Down
14 changes: 7 additions & 7 deletions util/createResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function createResolver (multicodec, EthObjClass, mapFromEthObject) {
* are option (i.e. nestness)
*/

function tree (ipfsBlock, options, callback) {
function tree (binaryBlob, options, callback) {
// parse arguments
if (typeof options === 'function') {
callback = options
Expand All @@ -38,7 +38,7 @@ function createResolver (multicodec, EthObjClass, mapFromEthObject) {
}

waterfall([
(cb) => util.deserialize(ipfsBlock.data, cb),
(cb) => util.deserialize(binaryBlob, cb),
(ethObj, cb) => treeFromEthObject(ethObj, options, cb)
], callback)
}
Expand All @@ -51,13 +51,13 @@ function createResolver (multicodec, EthObjClass, mapFromEthObject) {
}

/*
* resolve: receives a path and a ipfsBlock and returns the value on path,
* throw if not possible. `ipfsBlock` is an IPFS Block instance (contains data + key)
* resolve: receives a path and a binary blob and returns the value on path,
* throw if not possible. `binaryBlob`` is an Ethereum binary block.
*/

function resolve (ipfsBlock, path, callback) {
function resolve (binaryBlob, path, callback) {
waterfall([
(cb) => util.deserialize(ipfsBlock.data, cb),
(cb) => util.deserialize(binaryBlob, cb),
(ethObj, cb) => resolveFromEthObject(ethObj, path, cb)
], callback)
}
Expand Down Expand Up @@ -99,4 +99,4 @@ function createResolver (multicodec, EthObjClass, mapFromEthObject) {
return callback(null, result)
})
}
}
}
1 change: 0 additions & 1 deletion util/createTrieResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const cidFromHash = require('eth-hash-to-cid')
// const createBaseTrieResolver = require('./createBaseTrieResolver.js')
const createResolver = require('./createResolver')
const isExternalLink = require('./isExternalLink')
const toIpfsBlock = require('./toIpfsBlock')
const createUtil = require('./createUtil')
const createIsLink = require('./createIsLink')
const cidFromEthObj = require('./cidFromEthObj')
Expand Down
15 changes: 0 additions & 15 deletions util/toIpfsBlock.js

This file was deleted.

0 comments on commit e69f539

Please sign in to comment.