2

I've created a simple script to read ethereum data from the leveldb using C++. Basically this testing program would print all keys in leveldb to the console.

#include <cassert>
#include <iostream>
#include "leveldb/db.h"
using namespace std;
// g++ leveldb_test.cpp -o test -lleveldb
int main(int argc, const char *argv[]) {
    leveldb::DB* db;
    leveldb::Options options;
    options.create_if_missing = true;
    leveldb::Status status = leveldb::DB::Open(options, "/home/user/.ethereum/rinkeby/geth/chaindata", &db);

    if (!status.ok()){
        cerr << status.ToString() << endl;
        delete db;
        return 0;
    }
    leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
    for (it->SeekToFirst(); it->Valid(); it->Next()) {
        cout << it->key().ToString() << endl; 
    }
    delete it;
    delete db;
    return 1;
}

but the results are encoded like this.

console output image

How can I output a readable output? Is there a way to decode the ethereum leveldb data? (may be a c++ library?)

2
  • The information is stored as Hex, but you output it as a string. The information as stored as a key value pair, so take one key and enter it into the database and get a value. The information should look something like this: 0000ae1265f93afe581a53d84b99da10a86eb39ae2ff4b5a1acbafc59d828811 f8719f2089a29adfdb0047fc4ff5daba3c7c3cd2e720fd57b2a618a3b0ab5aa2107cb84ff84d8089206aeac7a903980000a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
    – Fortune
    Commented Jul 4, 2018 at 19:05
  • Also if you want a c++ library just use Google's LevelDB, they made it in C++ for C++. github.com/google/leveldb
    – Fortune
    Commented Jul 4, 2018 at 19:07

2 Answers 2

0

Not sure if it really helps you, i have tried reading statetrie following https://medium.com/cybermiles/diving-into-ethereums-world-state-c893102030ed. My output is something like this,

key:9bd9f586100bd06ef90756fd3aaad9e2ab641443d22cb185d364d627964c1855
[ <Buffer 09>,
  <Buffer 03 2e db 3c fb 1d 5c 00>,
  <Buffer 56 e8 1f 17 1b cc 55 a6 ff 83 45 e6 92 c0 f8 6e 5b 48 e0 1b 99 6c ad c0 01 62 2f b5 e3 63 b4 21>,
  <Buffer c5 d2 46 01 86 f7 23 3c 92 7e 7d b2 dc c7 03 c0 e5 00 b6 53 ca 82 27 3b 7b fa d8 04 5d 85 a4 70> ]
2
  • that's a node.js implementation :-(
    – Devortz
    Commented May 16, 2018 at 6:42
  • This is already a little ahead of the question. You took the key and it seems decoded it through RLP to show it's hex values. I think the issue is they are not even getting hex values to appear for them to manipulate.
    – Fortune
    Commented Jul 4, 2018 at 19:10
0

Ethereum nodes usually store binary data in LevelDB, but method ToString that use used is designed to work with text data. So, you need to implement your own method to convert binary data into human readable string. In Ethereum world common way to display binary data is to convert it into hexadecimal format like this: 0x0123456789ABCDEF. You may use the following function:

std::string toHexString (const char *data, size_t len) {
  std::string result;
  for (size_t i = 0; i < len; i++) {
    result.push_back ("0123456789ABCDEF" [data [i] >> 4 & 0x0F]);
    result.push_back ("0123456789ABCDEF" [data [i] & 0x0F]);
  }
  return result;
}

like this:

for (it->SeekToFirst(); it->Valid(); it->Next()) {
    cout << "0x" << toHexString (it->key().data(), it->key().size()) << endl; 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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