Skip to content

Made pchMessageStart variable so it works with altcoins #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 16 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,23 @@
# bitcoin-blockchain-parser [![Build Status](https://travis-ci.org/alecalve/python-bitcoin-blockchain-parser.svg?branch=master)](https://travis-ci.org/alecalve/python-bitcoin-blockchain-parser) [![Coverage Status](https://coveralls.io/repos/alecalve/python-bitcoin-blockchain-parser/badge.svg?branch=master&service=github)](https://coveralls.io/github/alecalve/python-bitcoin-blockchain-parser?branch=master)
This Python 3 library provides a parser for the raw data stored by bitcoind.
This is a fork of the `bitcoin-blockchain-parser` project found here:
https://github.com/alecalve/python-bitcoin-blockchain-parser

## Features
- Detects outputs types
- Detects addresses in outputs
- Interprets scripts
Differences include: Modifications to make parsing work with altcoins such as
Dash, Dogecoin, Litecoin and Feathercoin.

## Examples
To use with litecoin:

```python
import sys
from blockchain_parser.blockchain import Blockchain
>>> ltc=Blockchain('/media/chris/3033-6537/litecoin/blocks', b"\xfb\xc0\xb6\xdb")
>>> next(ltc.get_unordered_blocks())
Block(12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2)

# Instanciate the Blockchain by giving the path to the directory
# containing the .blk files created by bitcoind
blockchain = Blockchain(sys.argv[1])
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))
```

More examples are available in the examples directory.

## Installing

Requirements : python-bitcoinlib, coverage for tests

To install, just run
```
python setup.py install
```

## Tests

Run the test suite by lauching
```
./tests.sh
```
To use with bitcoin:

>>> btc=Blockchain('/home/chris/.bitcoin/blocks/')
>>> next(btc.get_unordered_blocks())
Block(000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f)

and dash:

>>> dash=Blockchain('/media/chris/3033-6537/dash/blocks', b"\xbf\x0c\x6b\xbd")
>>> next(dash.get_unordered_blocks())
Block(089fc444b06edd0f70d9fda85f9a3b2e22e549b354a1bdb210ce7804c69eb0a4)
13 changes: 5 additions & 8 deletions blockchain_parser/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
from .block import Block


# Constant separating blocks in the .blk files
BITCOIN_CONSTANT = b"\xf9\xbe\xb4\xd9"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave this constat here.



def get_files(path):
"""
Given the path to the .bitcoin directory, returns the sorted list of .blk
Expand All @@ -31,7 +27,7 @@ def get_files(path):
return sorted(files)


def get_blocks(blockfile):
def get_blocks(blockfile, pchMessageStart=b"\xf9\xbe\xb4\xd9"):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write pchMessageStart=BITCOIN_CONSTANT here.

"""
Given the name of a .blk file, for every block contained in the file,
yields its raw hexadecimal value
Expand All @@ -47,7 +43,7 @@ def get_blocks(blockfile):
offset = 0
block_count = 0
while offset < (length - 4):
if raw_data[offset:offset+4] == BITCOIN_CONSTANT:
if raw_data[offset:offset+4] == pchMessageStart:
offset += 4
size = struct.unpack("<I", raw_data[offset:offset+4])[0]
offset += 4 + size
Expand All @@ -63,13 +59,14 @@ class Blockchain(object):
maintained by bitcoind.
"""

def __init__(self, path):
def __init__(self, path, pchMessageStart=b"\xf9\xbe\xb4\xd9"):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would write pchMessageStart=BITCOIN_CONSTANT here.

self.path = path
self.pchMessageStart = pchMessageStart

def get_unordered_blocks(self):
"""Yields the blocks contained in the .blk files as is,
without ordering them according to height.
"""
for blk_file in get_files(self.path):
for raw_block in get_blocks(blk_file):
for raw_block in get_blocks(blk_file, pchMessageStart=self.pchMessageStart):
yield Block(raw_block)