I am facing a stupid problem. I am trying to read bitcoin/blocks/index leveldb using plyvel. I've set up a regtest and i know there is a block with the hash :


I am trying to query it using plyvel.

Note that I already read the topics :

How does Bitcoin read from/write to LevelDB

What are the keys used in the blockchain levelDB (ie what are the key:value pairs)?


Then, I know I have to swap the endianness so it becomes :

MYVAR = "4b67af01baa540cb587d3be4c9a3d8fc07f29ec2de92f3e4962fe2de9981ce27"

Thus, I should query something like :

b + (byte) MYVAR 

However I do not get how. I guess I missunderstand something in encoding. I tried :



Then, I opened the bitcoin/blocks/index leveldb using a gui explorer and I noticed the key for this block is :


which is \x62 corresponding to the "b" + the hash with \x (escaping caracter ? I know here is my misunderstanding on hex encoding)

and obviously


perfectly works

I also tried these methods :


No one of them works and I really don't get why :


Thank you in advance


The leveldb databases are serialized for optimization and do not work well with attempting to manually parse the data. You can more easily get details on a specific blockhash using the built-in JSON-RPC API and use the getblock call.

  • Thank you for your reply, and I will do so. However, what I am trying to do is to reference orphans blocks. I'm not sure I would be able to see these orphans blocks through the API calls.. I mean if I know block O is an orphan block, I will be able to getblock O. But if I am looking for orphan blocks, not knowing their hash, I will be probably face some problems Iterating over all the keys of the leveldb would allow me to distinguish orphan block and "main" blocks.
    – Elerir
    Dec 4 '18 at 10:40
  • I know i shouldn't answer to my own question but I found away to do so : I'm getting all the keys using db.iterator() And then using binascii.hexlify / unhexlify I've solved my problems. My workspace was a bit messy and that's why I didn't even realize that simple trick works (I'm sure I had tried)
    – Elerir
    Dec 9 '18 at 11:16

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.