I've been working with the LevelDB for a few weeks, coding some tools to access the database and parse the data to be human readable.

However, every single time I read from the chainstate the data gets corrupted (ask me to rebuild it when running bitcoind). I decided to copy the DB as a workaround after it happened the first time, but it's quite annoying to do so every single time I want to update the data.

I'm using plyvel and Python 2.8, and I close the DB every time I finish with the reading.

Example

# Open the LevelDB
db = plyvel.DB(".bitcoin/chainstate")

for key, o_value in db:
 # do stuff

# Close the LevelDB
db.close()

Does anyone knows what could be the reason? Does bitcoind prevent other software to access the DB by marking the last access somehow?

chainstate LOG after running the code, retrieving the obfuscation key, and closing the database:

2017/03/22-11:03:45.746702 7f3c18a39700 Recovering log #394423
2017/03/22-11:03:45.854583 7f3c18a39700 Delete type=0 #394423
2017/03/22-11:03:45.854711 7f3c18a39700 Delete type=3 #394422

Example code:

db = plyvel.DB(".bitcoin/chainstate")
o_key = db.get((a2b_hex("0e00") + "obfuscate_key"))
db.close()

debug.log after opening bitcoind (just the part referring to the LevelDB):

2017-03-22 10:03:45 Cache configuration:
2017-03-22 10:03:45 * Using 2.0MiB for block index database
2017-03-22 10:03:45 * Using 8.0MiB for chain state database
2017-03-22 10:03:45 * Using 290.0MiB for in-memory UTXO set
2017-03-22 10:03:45 init message: Loading block index...
2017-03-22 10:03:45 Opening LevelDB in /home/sdelgado/.bitcoin/blocks/index
2017-03-22 10:03:45 Opened LevelDB successfully
2017-03-22 10:03:45 Using obfuscation key for /home/sdelgado/.bitcoin/blocks/index: 0000000000000000
2017-03-22 10:03:45 Opening LevelDB in /home/sdelgado/.bitcoin/chainstate
2017-03-22 10:03:45 Opened LevelDB successfully
2017-03-22 10:03:45 Using obfuscation key for /home/sdelgado/.bitcoin/chainstate: 27c78118b7316105
2017-03-22 10:03:48 LoadBlockIndexDB: last block file = 810
2017-03-22 10:03:48 LoadBlockIndexDB: last block file info: CBlockFileInfo(blocks=53, size=51783543, heights=457770...458279, time=2017-03-18...2017-03-21)
2017-03-22 10:03:48 Checking all blk files are present...
2017-03-22 10:03:49 LoadBlockIndexDB: transaction index disabled
2017-03-22 10:03:49 LevelDB read failure: Corruption: corrupted compressed block contents
2017-03-22 10:03:49 Corruption: corrupted compressed block contents
2017-03-22 10:03:51 Aborted block database rebuild. Exiting.
2017-03-22 10:03:51 scheduler thread interrupt
2017-03-22 10:03:51 Shutdown: In progress...
2017-03-22 10:03:51 StopNode()
2017-03-22 10:03:51 Shutdown: done
  • Are you opening the database while bitcoind is running? – Pieter Wuille Mar 21 '17 at 17:48
  • No, bitcoind is not running when I'm opening the database. – sr-gi Mar 21 '17 at 18:32
  • Then I have no clue. Can you share your debug.log or your chainstate/LOG file? – Pieter Wuille Mar 21 '17 at 19:25
  • Updated with the logs – sr-gi Mar 22 '17 at 10:10
up vote 2 down vote accepted

Plyvel has Snappy compression enabled by default. Have you tried disabling it when opening the database?

# Open the LevelDB
db = plyvel.DB(".bitcoin/chainstate", compression=None)

With this code my bitcoind does not seem to complain after having opened the database with python.

  • That's seems to be the problem. No data corruption after opening it using compression=None. Thanks! – sr-gi Jul 12 '17 at 8:51

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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