I've been trying to read the transactions and block data directly from the LevelDB files (.ldb
) stored within Geth's chaindata/
directory. To do so, I first tried to extract the keys stored on LevelDB using the following Python Script:
import leveldb
def get_keys():
geth_ldb_path = "/path_to_geth/geth/chaindata"
db = leveldb.LevelDB(geth_ldb_path)
print("Keys: ")
for k in db.RangeIter(include_value=False):
print(k)
It gave me an output where the keys seem to be encoded as bytes and here's an example of one of the items in the output list:
bytearray(b'B\x02X\x00\x00\x00\x00\x00\x00\x00o\xf8\xdc\x87\xbeFC%$\xfeI\xc4e\xe4\x8a%6\xe6\xd8\x91\x8d\xd9\xc2\xba"\xc2#\x14\xcc\xda\xad1\xf8')
Then, from a few other posts such as this one, I found out that those bytes are the output of an RLP (Recursive Length Prefix) encoding. So, I tried to decode one of the keys (the same one as in the example above) using the function provided by the rlp
library of Python as follows:
import rlp
example_key = bytearray(b'B\x02X\x00\x00\x00\x00\x00\x00\x00o\xf8\xdc\x87\xbeFC%$\xfeI\xc4e\xe4\x8a%6\xe6\xd8\x91\x8d\xd9\xc2\xba"\xc2#\x14\xcc\xda\xad1\xf8')
print(rlp.decode(example_key))
However, running the above code gave me the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/some_path/LevelDBTest/python_implementation/venv/lib/python3.9/site-packages/rlp/codec.py", line 265, in decode
item, per_item_rlp = decode_raw(rlp, strict, recursive_cache)
File "/some_path/LevelDBTest/python_implementation/venv/lib/python3.9/site-packages/rlp/codec.py", line 58, in decode_raw
raise DecodingError(msg, item)
rlp.exceptions.DecodingError: RLP string ends with 42 superfluous bytes
Am I right in using RLP to decode the values of the keys stored in the LevelDB files of Geth? If not, what should I be using for decoding those key values?