2

Read the article about how to get info from local chain, but example not work. My code stoped when i create connection to LevelDB of my local chain in var db = levelup(...):

var levelup = require('levelup');
var db = levelup('/Users/User/WebstormProjects/untitled/bin/chaindata');

//the genesis state root
var root = 'e5be92145a301820111f91866566e3e99ee344d155569e4556a39bc71238f3bc';

//Note: we are doing everything using binary encoding.
db.get(

    new Buffer(root, 'hex'),
        {
            encoding: 'binary'
        },

    function (err, value)
        {
            console.log(value);
        }
    );

"chaindata" is a copy of my local eth chain on Mac. Folder contains such files:

  • 000002.ldb
  • 000003.log
  • CURRENT
  • LOCK
  • LOG
  • LOG.old
  • MANIFEST-000004

So, when i try to open db get error: InitializationError: Must provide db.

1

Looking at the LevelDB code, that error appears to be returned if the passed in DB is either empty or invalid. You'll need to check your copy is intact and that there's actually something in it (by letting Geth run for a while).

Using the following code on Geth chaindata for the public chain...

var levelup = require('levelup');
var db = levelup('chaindata');
var rlp = require('rlp')

// The genesis state root.
var root = 'd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544';

// Note: we're doing everything using binary encoding.
db.get(new Buffer(root, 'hex'), {
    encoding: 'binary'
}, function (err, value) {
    console.log(rlp.decode(value));
});

... I get the following expected (though uninteresting) raw output:

[ <Buffer 90 dc af 88 c4 0c 7b bc 95 a9 12 cb dd e6 7c 17 57 67 b3 11 73 df 9e e4 b0 d7 33 bf dd 51 1c 43>,
  <Buffer ba be 36 9f 6b 12 09 2f 49 18 1a e0 4c a1 73 fb 68 d1 a5 45 6f 18 d2 0f a3 2c ba 73 95 40 52 bd>,
  <Buffer 47 3e cf 8a 7e 36 a8 29 e7 50 39 a3 b0 55 e5 1b 83 32 cb f0 33 24 ab 4a f2 06 6b bd 6f bf 00 21>,
  <Buffer bb da 34 75 3d 7a a6 c3 8e 60 3f 36 02 44 e8 f5 96 11 92 1d 9e 1f 12 83 72 fe c0 d5 86 d4 f9 e0>,
  <Buffer 4e 44 ca ec ff 45 c9 89 1f 74 f6 a2 15 67 35 88 6e ed f6 f1 a7 33 62 8e bc 80 2e c7 9d 84 46 48>,
  <Buffer a5 f3 f2 f7 54 21 48 c9 73 97 7c 8a 1e 15 4c 43 00 fe c9 2f 75 5f 78 46 f1 b7 34 d3 ab 1d 90 e7>,
  <Buffer e8 23 85 0f 50 bf 72 ba ae 9d 17 33 a3 6a 44 4a b6 5d 0a 6f aa ba 40 4f 05 83 ce 0c a4 da d9 2d>,
  <Buffer f7 a0 0c be 7d 4b 30 b1 1f ae a3 ae 61 b7 f1 f2 b3 15 b6 1d 9f 6b d6 8b fe 58 7a d0 ee ce b7 21>,
  <Buffer 71 17 ef 9f c9 32 f1 a8 8e 90 8e ae ad 85 65 c1 9b 56 45 dc 9e 5b 1b 6e 84 1c 5e db df d7 16 81>,
  <Buffer 69 eb 2d e2 83 f3 2c 11 f8 59 d7 bc f9 3d a2 39 90 d3 e6 62 93 5e d4 d6 b3 9c e3 67 3e c8 44 72>,
  <Buffer 20 3d 26 45 63 12 bb c4 da 5c d2 93 b7 5b 84 0f c5 04 5e 49 3d 6f 90 4d 18 08 23 ec 22 bf ed 8e>,
  <Buffer 92 87 b5 c2 1f 22 54 af 4e 64 fc a7 6a cc 5c d8 73 99 c7 f1 ed e8 18 db 43 26 c9 8c e2 dc 22 08>,
  <Buffer 6f c2 d7 54 e3 04 c4 8c e6 a5 17 75 3c 62 b1 a9 c1 d5 92 5b 89 70 74 86 d7 fc 08 91 9e 0a 94 ec>,
  <Buffer 7b 1c 54 f1 5e 29 9b d5 8b df ef 97 41 53 8c 78 28 b5 d7 d1 1a 48 9f 9c 20 d0 52 b3 47 1d f4 75>,
  <Buffer 51 f9 dd 37 39 a9 27 c8 9e 35 75 80 a4 c9 7b 40 23 4a a0 1e d3 d5 e0 39 0d c9 82 a7 97 58 80 a0>,
  <Buffer 89 d6 13 f2 61 59 af 43 61 6f d9 45 5b b4 61 f4 86 9b fe de 26 f2 13 08 35 ed 06 7a 8b 96 7b fb>,
  <Buffer > ]

This is the raw version of the state Patricia-Merkle Trie. To navigate and make sense of it you'll need to bring in the relevant packages. (e.g. var Trie = require('merkle-patricia-tree');, etc... )

  • Where you got your 'chaindata'? Even if i try to connect to public chain at /Users/User/Library/Ethereum/geth/chaindata had such problem. Maybe it works on linux or win, not mac? – Gudsaf Nov 25 '17 at 21:05
  • That's just my chaindata folder under ~.ethereum/geth/ on my machine (I'm not connected to an external node). I'm running the script from the same directory. I think it should work on Mac. Can you try downloading more of the chaindata? I'm not too sure what else to suggest I'm afraid. – Richard Horrocks Nov 25 '17 at 21:15
  • Do that right now. Hm can you sad what files you have in ~.ethereum/geth/chaindata? Maybe our lists are different and problem in that. – Gudsaf Nov 25 '17 at 21:19
  • get solution, write after – Gudsaf Nov 25 '17 at 21:33
  • 1
    Aha, it must be that I was already running the old version of the levelup package, which is why it worked for me :-) – Richard Horrocks Nov 26 '17 at 12:07
1

Problem was, that i had to use this:

var level = require('level')
var db = level('/Users/User/WebstormProjects/untitled/bin/chaindata')

instead

var levelup = require('levelup');
var db = levelup('/Users/User/WebstormProjects/untitled/bin/chaindata');

Also can read about it in: github issues

  • +1 from me. Didn't think to check if it was a LevelDB versioning issue. – Richard Horrocks Nov 26 '17 at 12:09
  • (You can accept your own answer, although I think you might have to wait 48 hours before the system will let you.) – Richard Horrocks Nov 26 '17 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.