-
Notifications
You must be signed in to change notification settings - Fork 193
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
priestc
wants to merge
3
commits into
alecalve:master
Choose a base branch
from
priestc:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,23 @@ | ||
# bitcoin-blockchain-parser [](https://travis-ci.org/alecalve/python-bitcoin-blockchain-parser) [](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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,6 @@ | |
from .block import Block | ||
|
||
|
||
# Constant separating blocks in the .blk files | ||
BITCOIN_CONSTANT = b"\xf9\xbe\xb4\xd9" | ||
|
||
|
||
def get_files(path): | ||
""" | ||
Given the path to the .bitcoin directory, returns the sorted list of .blk | ||
|
@@ -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"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would write |
||
""" | ||
Given the name of a .blk file, for every block contained in the file, | ||
yields its raw hexadecimal value | ||
|
@@ -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 | ||
|
@@ -63,13 +59,14 @@ class Blockchain(object): | |
maintained by bitcoind. | ||
""" | ||
|
||
def __init__(self, path): | ||
def __init__(self, path, pchMessageStart=b"\xf9\xbe\xb4\xd9"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would write |
||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.