Skip to main content

Bitcoin blockchain parser

Project description

bitcoin-blockchain-parser Build Status Coverage Status

This Python 3 library provides a parser for the raw data stored by bitcoind.

Features

  • Detects outputs types
  • Detects addresses in outputs
  • Interprets scripts
  • Supports SegWit
  • Supports ordered block parsing

Installing

Whether installing using Pip or from source, plyvel requires leveldb development libraries for LevelDB >1.2.X.

On Linux, install libleveldb-dev

sudo apt-get install libleveldb-dev

Using pip

pip install blockchain-parser

Using source

Requirements : python-bitcoinlib, plyvel, coverage for tests

Install dependencies contained in requirements.txt:

pip install -r requirements.txt

Then, just run

python setup.py install

Developing

First, setup a virtualenv and install dependencies:

virtualenv -p python3 .venv
source .venv/bin/activate
pip install -r requirements.txt

Run the test suite by lauching

./tests.sh

Examples

Below are two basic examples for parsing the blockchain. More examples are available in the examples directory.

Unordered Blocks

This blockchain parser parses raw blocks saved in Bitcoin Core's .blk file format. Bitcoin Core does not guarantee that these blocks are saved in order. If your application does not require that blocks are parsed in order, the Blockchain.get_unordered_blocks(...) method can be used:

import os
from blockchain_parser.blockchain import Blockchain

# Instantiate the Blockchain by giving the path to the directory
# containing the .blk files created by bitcoind
blockchain = Blockchain(os.path.expanduser('~/.bitcoin/blocks'))
for block in blockchain.get_unordered_blocks():
    for tx in block.transactions:
        for no, output in enumerate(tx.outputs):
            print("tx=%s outputno=%d type=%s value=%s" % (tx.hash, no, output.type, output.value))

Ordered Blocks

If maintaining block order is necessary for your application, you should use the Blockchain.get_ordered_blocks(...) method. This method uses Bitcoin Core's LevelDB index to locate ordered block data in it's .blk files.

import os
from blockchain_parser.blockchain import Blockchain

# To get the blocks ordered by height, you need to provide the path of the
# `index` directory (LevelDB index) being maintained by bitcoind. It contains
# .ldb files and is present inside the `blocks` directory.
blockchain = Blockchain(os.path.expanduser('~/.bitcoin/blocks'))
for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), end=1000):
    print("height=%d block=%s" % (block.height, block.hash))

Blocks can be iterated in reverse by specifying a start parameter that is greater than the end parameter.

for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), start=510000, end=0):
    print("height=%d block=%s" % (block.height, block.hash))

Building the LevelDB index can take a while which can make iterative development and debugging challenging. For this reason, Blockchain.get_ordered_blocks(...) supports caching the LevelDB index database using pickle. To use a cache simply pass cache=filename to the ordered blocks method. If the cached file does not exist it will be created for faster parsing the next time the method is run. If the cached file already exists it will be used instead of re-parsing the LevelDB database.

for block in blockchain.get_ordered_blocks(os.path.expanduser('~/.bitcoin/blocks/index'), cache='index-cache.pickle'):
    print("height=%d block=%s" % (block.height, block.hash))

NOTE: You must manually/programmatically delete the cache file in order to rebuild the cache. Don't forget to do this each time you would like to re-parse the blockchain with a higher block height than the first time you saved the cache file as the new blocks will not be included in the cache.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

blockchain-parser-0.1.6.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

blockchain_parser-0.1.6-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

Details for the file blockchain-parser-0.1.6.tar.gz.

File metadata

  • Download URL: blockchain-parser-0.1.6.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for blockchain-parser-0.1.6.tar.gz
Algorithm Hash digest
SHA256 f3ace730b5e95bf93c7adb5f0d840037b7f24f054cfa96c1f843b91a6b363b85
MD5 f8e0255f625ca975b215c0a211d490e6
BLAKE2b-256 1865c3d08b2b74bad9d4c0983acd116270b24857749bf72cdbc518d719eb5118

See more details on using hashes here.

File details

Details for the file blockchain_parser-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for blockchain_parser-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 fd7c9dcc2e93d4b85a1454fb77e8091f204dc74958497ef51a14807685dfd58e
MD5 71042752809687174bb56391e86b163f
BLAKE2b-256 5d78c7a2104e8845a2ebd0b93b690a472a7a232ffd7a4c7d72fd77bd146c5eb6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page