From 8b58df66fc9efbf8d1c305ccb34f4f0700c63b97 Mon Sep 17 00:00:00 2001 From: Primoz Z Date: Wed, 31 Oct 2018 00:28:16 +0100 Subject: [PATCH 1/6] wip --- example.js | 15 +++++++++++++++ gethDbPath/000005.ldb | Bin 0 -> 566 bytes gethDbPath/CURRENT | 1 + gethDbPath/LOCK | 0 gethDbPath/LOG | 5 +++++ gethDbPath/LOG.old | 1 + gethDbPath/MANIFEST-000004 | Bin 0 -> 111 bytes index.js | 2 +- package.json | 5 +++-- test/index.js | 6 +++--- 10 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 example.js create mode 100644 gethDbPath/000005.ldb create mode 100644 gethDbPath/CURRENT create mode 100644 gethDbPath/LOCK create mode 100644 gethDbPath/LOG create mode 100644 gethDbPath/LOG.old create mode 100644 gethDbPath/MANIFEST-000004 diff --git a/example.js b/example.js new file mode 100644 index 0000000..0f30d7b --- /dev/null +++ b/example.js @@ -0,0 +1,15 @@ +const levelup = require('levelup') +const leveldown = require('leveldown') +const Blockchain = require('./index.js') +const utils = require('ethereumjs-util') + +const gethDbPath = '/Users/primoz/Library/Ethereum/geth/chaindata' // Add your own path here +const db = levelup(leveldown('gethDbPath')) + + +new Blockchain({ db: db }).iterator('i', (block, reorg, cb) => { + const blockNumber = utils.bufferToInt(block.header.number) + const blockHash = block.hash().toString('hex') + console.log(`BLOCK ${blockNumber}: ${blockHash}`) + cb() +}, (err) => console.log(err || 'Done.')) \ No newline at end of file diff --git a/gethDbPath/000005.ldb b/gethDbPath/000005.ldb new file mode 100644 index 0000000000000000000000000000000000000000..3264430dbeb4a1caa0c8bddd2930ebd88f49861a GIT binary patch literal 566 zcmaFMzJNiK!{f@+bcY{h>wX*%*pcOM^=rE8LWv6#vP_=Mdeg(Z@pS)UMh*rB20bPQ z13?9!#NraCoc!c$Ms7~YIu`~WE_MaxX<7^#EPM(csfj76MT}h23>kR1*%g?8!aNLy z%t@SD3=B;h8CV$EIJG1W9XP-sq>#bME5N{z$H>mf&2Xj4l#z#1V>(Nn3Q!Fzrw`9h zCXof4TxpC99Sda7_`bMzJhp0e>(=ybXRb(H7IKxI(p4h-VWHDshbxoar!aWgGJJ4e zaQ(-0|E+5(Zm!{+J?koC_uE6~xQcFFY^oCHyk!u<&+4)u?1j9z^qJ6Q|C?Q(O*-%+ zFWTdQ^vs;K2N;v|w?0nZqPQTeN|AwOC!<#d!#_qS+Q!Jh(#pd4VMarfa7RPK0>Rxc zZu^;3`}It&k!db?vh}U;+Re8!Pv5Sxc=(iCy6IQbE1;7ZeHDPA<)q2UR>H^{&A`B$ ztiZ^_4-A@OMovZr4F-nj{G_bZhNCPx4O literal 0 HcmV?d00001 diff --git a/index.js b/index.js index 6fa9f90..2e32335 100644 --- a/index.js +++ b/index.js @@ -55,7 +55,7 @@ function Blockchain (opts) { self.db = opts.db || opts.blockDb // defaults - self.db = self.db ? self.db : levelup('', { db: memdown }) + self.db = self.db ? self.db : levelup(memdown()) self.validate = (opts.validate === undefined ? true : opts.validate) self.ethash = self.validate ? new Ethash(self.db) : null self._heads = {} diff --git a/package.json b/package.json index 0313809..d720f17 100644 --- a/package.json +++ b/package.json @@ -30,9 +30,10 @@ "ethereumjs-common": "~0.6.0", "ethereumjs-util": "~6.0.0", "flow-stoplight": "^1.0.0", - "levelup": "^1.3.2", + "leveldown": "^4.0.1", + "levelup": "^3.1.1", "lru-cache": "^4.1.3", - "memdown": "^1.1.0", + "memdown": "^3.0.0", "safe-buffer": "^5.1.2", "semaphore": "^1.1.0" }, diff --git a/test/index.js b/test/index.js index a87d930..c8fa008 100644 --- a/test/index.js +++ b/test/index.js @@ -45,7 +45,7 @@ test('blockchain test', function (t) { }) }, function alternateConstructors (done) { - var db = levelup('', { db: memdown }) + var db = levelup(memdown()) var blockchain = new Blockchain(db) t.equals(db, blockchain.db, 'support constructor with db parameter') blockchain = new Blockchain({detailsDb: db, blockDb: db}) @@ -375,7 +375,7 @@ test('blockchain test', function (t) { }) }, function saveHeads (done) { - var db = levelup('', { db: memdown }) + var db = levelup(memdown()) var blockchain = new Blockchain({db: db, validate: false}) var header = new Block.Header() header.number = ethUtil.toBuffer(1) @@ -523,7 +523,7 @@ function isConsecutive (blocks) { function createTestDB (cb) { var genesis = new Block() genesis.setGenesisParams() - var db = levelup('', { db: memdown }) + var db = levelup(memdown()) db.batch([{ type: 'put', key: Buffer.from('6800000000000000006e', 'hex'), From 4794e921873a70015f76982a6be8ef5d546b30f6 Mon Sep 17 00:00:00 2001 From: Primoz Z Date: Wed, 31 Oct 2018 09:26:08 +0100 Subject: [PATCH 2/6] wip --- .vscode/launch.json | 17 +++++++++++++++++ example.js | 3 ++- index.js | 9 ++++----- package.json | 5 ++--- test/index.js | 9 ++++----- 5 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..bccc583 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${workspaceFolder}/node_modules/tape/bin/tape", + "args": [ + "./test/index.js" + ], + } + ] +} \ No newline at end of file diff --git a/example.js b/example.js index 0f30d7b..e6ba84b 100644 --- a/example.js +++ b/example.js @@ -1,10 +1,11 @@ const levelup = require('levelup') const leveldown = require('leveldown') +const encode = require('encoding-down') const Blockchain = require('./index.js') const utils = require('ethereumjs-util') const gethDbPath = '/Users/primoz/Library/Ethereum/geth/chaindata' // Add your own path here -const db = levelup(leveldown('gethDbPath')) +const db = levelup(encode(leveldown(gethDbPath))) new Blockchain({ db: db }).iterator('i', (block, reorg, cb) => { diff --git a/index.js b/index.js index 2e32335..2d5ba7c 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,7 @@ const async = require('async') const Stoplight = require('flow-stoplight') const semaphore = require('semaphore') -const levelup = require('levelup') -const memdown = require('memdown') +const level = require('level-mem') const Block = require('ethereumjs-block') const Common = require('ethereumjs-common') const ethUtil = require('ethereumjs-util') @@ -55,7 +54,7 @@ function Blockchain (opts) { self.db = opts.db || opts.blockDb // defaults - self.db = self.db ? self.db : levelup(memdown()) + self.db = self.db ? self.db : level() self.validate = (opts.validate === undefined ? true : opts.validate) self.ethash = self.validate ? new Ethash(self.db) : null self._heads = {} @@ -938,7 +937,7 @@ Blockchain.prototype._iterator = function (name, func, cb) { } else { blockNumber = false // No more blocks, return - if (err instanceof levelup.errors.NotFoundError) { + if (err instanceof level.errors.NotFoundError) { return cb2() } } @@ -1001,7 +1000,7 @@ Blockchain.prototype._numberToHash = function (number, cb) { const self = this if (number.ltn(0)) { - return cb(new levelup.errors.NotFoundError()) + return cb(new level.errors.NotFoundError()) } var key = numberToHashKey(number) var hash = self._cache.numberToHash.get(key) diff --git a/package.json b/package.json index d720f17..b71076d 100644 --- a/package.json +++ b/package.json @@ -25,15 +25,14 @@ "homepage": "https://github.com/ethereumjs/ethereumjs-blockchain#readme", "dependencies": { "async": "^2.6.1", + "encoding-down": "^5.0.4", "ethashjs": "~0.0.7", "ethereumjs-block": "~2.1.0", "ethereumjs-common": "~0.6.0", "ethereumjs-util": "~6.0.0", "flow-stoplight": "^1.0.0", - "leveldown": "^4.0.1", - "levelup": "^3.1.1", + "level-mem": "^3.0.1", "lru-cache": "^4.1.3", - "memdown": "^3.0.0", "safe-buffer": "^5.1.2", "semaphore": "^1.1.0" }, diff --git a/test/index.js b/test/index.js index c8fa008..ebba9d2 100644 --- a/test/index.js +++ b/test/index.js @@ -6,8 +6,7 @@ const Block = require('ethereumjs-block') const Common = require('ethereumjs-common') const async = require('async') const ethUtil = require('ethereumjs-util') -const levelup = require('levelup') -const memdown = require('memdown') +const level = require('level-mem') const testData = require('./testdata.json') const BN = require('bn.js') const rlp = ethUtil.rlp @@ -45,7 +44,7 @@ test('blockchain test', function (t) { }) }, function alternateConstructors (done) { - var db = levelup(memdown()) + var db = level() var blockchain = new Blockchain(db) t.equals(db, blockchain.db, 'support constructor with db parameter') blockchain = new Blockchain({detailsDb: db, blockDb: db}) @@ -375,7 +374,7 @@ test('blockchain test', function (t) { }) }, function saveHeads (done) { - var db = levelup(memdown()) + var db = level() var blockchain = new Blockchain({db: db, validate: false}) var header = new Block.Header() header.number = ethUtil.toBuffer(1) @@ -523,7 +522,7 @@ function isConsecutive (blocks) { function createTestDB (cb) { var genesis = new Block() genesis.setGenesisParams() - var db = levelup(memdown()) + var db = level() db.batch([{ type: 'put', key: Buffer.from('6800000000000000006e', 'hex'), From e81611679ea7da43c62681618663eaeea98939b0 Mon Sep 17 00:00:00 2001 From: Primoz Z Date: Wed, 31 Oct 2018 09:38:58 +0100 Subject: [PATCH 3/6] remove example --- example.js | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 example.js diff --git a/example.js b/example.js deleted file mode 100644 index e6ba84b..0000000 --- a/example.js +++ /dev/null @@ -1,16 +0,0 @@ -const levelup = require('levelup') -const leveldown = require('leveldown') -const encode = require('encoding-down') -const Blockchain = require('./index.js') -const utils = require('ethereumjs-util') - -const gethDbPath = '/Users/primoz/Library/Ethereum/geth/chaindata' // Add your own path here -const db = levelup(encode(leveldown(gethDbPath))) - - -new Blockchain({ db: db }).iterator('i', (block, reorg, cb) => { - const blockNumber = utils.bufferToInt(block.header.number) - const blockHash = block.hash().toString('hex') - console.log(`BLOCK ${blockNumber}: ${blockHash}`) - cb() -}, (err) => console.log(err || 'Done.')) \ No newline at end of file From 876207dd655f86855af0669860bac0134acb292a Mon Sep 17 00:00:00 2001 From: Primoz Z Date: Wed, 31 Oct 2018 09:43:04 +0100 Subject: [PATCH 4/6] clean --- .vscode/launch.json | 17 ----------------- gethDbPath/000005.ldb | Bin 566 -> 0 bytes gethDbPath/CURRENT | 1 - gethDbPath/LOCK | 0 gethDbPath/LOG | 5 ----- gethDbPath/LOG.old | 1 - gethDbPath/MANIFEST-000004 | Bin 111 -> 0 bytes 7 files changed, 24 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 gethDbPath/000005.ldb delete mode 100644 gethDbPath/CURRENT delete mode 100644 gethDbPath/LOCK delete mode 100644 gethDbPath/LOG delete mode 100644 gethDbPath/LOG.old delete mode 100644 gethDbPath/MANIFEST-000004 diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index bccc583..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "node", - "request": "launch", - "name": "Launch Program", - "program": "${workspaceFolder}/node_modules/tape/bin/tape", - "args": [ - "./test/index.js" - ], - } - ] -} \ No newline at end of file diff --git a/gethDbPath/000005.ldb b/gethDbPath/000005.ldb deleted file mode 100644 index 3264430dbeb4a1caa0c8bddd2930ebd88f49861a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 566 zcmaFMzJNiK!{f@+bcY{h>wX*%*pcOM^=rE8LWv6#vP_=Mdeg(Z@pS)UMh*rB20bPQ z13?9!#NraCoc!c$Ms7~YIu`~WE_MaxX<7^#EPM(csfj76MT}h23>kR1*%g?8!aNLy z%t@SD3=B;h8CV$EIJG1W9XP-sq>#bME5N{z$H>mf&2Xj4l#z#1V>(Nn3Q!Fzrw`9h zCXof4TxpC99Sda7_`bMzJhp0e>(=ybXRb(H7IKxI(p4h-VWHDshbxoar!aWgGJJ4e zaQ(-0|E+5(Zm!{+J?koC_uE6~xQcFFY^oCHyk!u<&+4)u?1j9z^qJ6Q|C?Q(O*-%+ zFWTdQ^vs;K2N;v|w?0nZqPQTeN|AwOC!<#d!#_qS+Q!Jh(#pd4VMarfa7RPK0>Rxc zZu^;3`}It&k!db?vh}U;+Re8!Pv5Sxc=(iCy6IQbE1;7ZeHDPA<)q2UR>H^{&A`B$ ztiZ^_4-A@OMovZr4F-nj{G_bZhNCPx4O From 087e693289f295f39448bdadaa55e371a088be7a Mon Sep 17 00:00:00 2001 From: Primoz Z Date: Thu, 1 Nov 2018 19:29:47 +0100 Subject: [PATCH 5/6] remove encoding-down from package.json --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index b71076d..4d0dc35 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "homepage": "https://github.com/ethereumjs/ethereumjs-blockchain#readme", "dependencies": { "async": "^2.6.1", - "encoding-down": "^5.0.4", "ethashjs": "~0.0.7", "ethereumjs-block": "~2.1.0", "ethereumjs-common": "~0.6.0", From f6761ad5edf1eac084284a2144d044935b1e565e Mon Sep 17 00:00:00 2001 From: Primoz Z Date: Fri, 2 Nov 2018 15:26:25 +0100 Subject: [PATCH 6/6] update readme.md example --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7e63ce3..bb7c198 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ A module to store and interact with blocks. # EXAMPLE -The following is an example to iterate through an existing Geth DB (needs ``leveldown`` to be +The following is an example to iterate through an existing Geth DB (needs ```levelup``` and ``leveldown`` to be installed separately): ```javascript @@ -21,7 +21,7 @@ const Blockchain = require('ethereumjs-blockchain') const utils = require('ethereumjs-util') const gethDbPath = './chaindata' // Add your own path here -const db = levelup(gethDbPath, { db: leveldown }) +const db = levelup(leveldown(gethDbPath)) new Blockchain({db: db}).iterator('i', (block, reorg, cb) => { const blockNumber = utils.bufferToInt(block.header.number)