<p>Plyvel has Snappy compression <a href="https://plyvel.readthedocs.io/en/latest/api.html#DB" rel="nofollow noreferrer">enabled by default</a>. Have you tried disabling it when opening the database?</p>

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

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

CC BY-SA 3.0
2017-07-12T08:51:46.587Z
That's seems to be the problem. No data corruption after opening it using compression=None. Thanks!
sr_gi
CC BY-SA 3.0
2017-07-11T15:57:37.367Z
2017-07-11T15:57:37.367Z
cpsola
<p>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. </p>

<p>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.</p>

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

<p><strong>Example</strong></p>

<pre><code># Open the LevelDB
db = plyvel.DB(".bitcoin/chainstate")

for key, o_value in db:
 # do stuff

# Close the LevelDB
db.close()
</code></pre>

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

<p><strong>chainstate LOG</strong> after running the code, retrieving the obfuscation key, and closing the database:</p>

<pre><code>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
</code></pre>

<p><strong>Example code</strong>:</p>

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

<p><strong>debug.log</strong> after opening bitcoind (just the part referring to the LevelDB):</p>

<pre><code>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
</code></pre>

CC BY-SA 3.0
2017-03-21T17:48:02.983Z
Are you opening the database while bitcoind is running?
Pieter Wuille
CC BY-SA 3.0
2017-03-21T18:32:25.260Z
No, bitcoind is not running when I'm opening the database.
sr_gi
CC BY-SA 3.0
2017-03-21T19:25:24.697Z
Then I have no clue. Can you share your debug.log or your chainstate/LOG file?
Pieter Wuille
CC BY-SA 3.0
2017-03-22T10:10:11.193Z
Updated with the logs
sr_gi
<bitcoind><python><leveldb>
Chainstate LevelDB corruption after reading from the database
sr_gi
# Chainstate LevelDB corruption after reading from the database

(Asked by: username_0 on 2017-03-21)

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

```

**Tags:** bitcoind, python, leveldb

> **Comments:**
> **username_1**: Are you opening the database while bitcoind is running?  
> **username_0**: No, bitcoind is not running when I'm opening the database.  
> **username_1**: Then I have no clue. Can you share your debug.log or your chainstate/LOG file?  
> **username_0**: Updated with the logs  

---

## 1 answers

### Answer by username_2 on 2017-07-11. Score: 3 | Accepted answer

Plyvel has Snappy compression [enabled by default](https://plyvel.readthedocs.io/en/latest/api.html#DB). 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.

> **Comments:**
> **username_0**: That's seems to be the problem. No data corruption after opening it using compression=None. Thanks!  

---

