diff --git a/README.md b/README.md index c57701b..0cce2cf 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# 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. +# python-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 (blk*.dat files). ## Features - Detects outputs types diff --git a/blockchain_parser/block.py b/blockchain_parser/block.py index 2ad3263..af215b1 100644 --- a/blockchain_parser/block.py +++ b/blockchain_parser/block.py @@ -40,16 +40,13 @@ class Block(object): def __init__(self, raw_hex, height = None): self.hex = raw_hex - self._hash = None - self._transactions = None - self._header = None - self._n_transactions = None + self._hash = format_hash(double_sha256(self.hex[:80])) + self._header = BlockHeader(self.hex[:80]) + self._n_transactions = decode_varint(self.hex[80:])[0] + self._transactions = list(get_block_transactions(self.hex)) self.size = len(raw_hex) self.height = height - def __repr__(self): - return "Block(%s)" % self.hash - @classmethod def from_hex(cls, raw_hex): """Builds a block object from its bytes representation""" @@ -58,8 +55,6 @@ def from_hex(cls, raw_hex): @property def hash(self): """Returns the block's hash (double sha256 of its 80 bytes header""" - if self._hash is None: - self._hash = format_hash(double_sha256(self.hex[:80])) return self._hash @property @@ -68,23 +63,15 @@ def n_transactions(self): it is faster to use this than to use len(block.transactions) as there's no need to parse all transactions to get this information """ - if self._n_transactions is None: - self._n_transactions = decode_varint(self.hex[80:])[0] - return self._n_transactions @property def transactions(self): """Returns a list of the block's transactions represented as Transaction objects""" - if self._transactions is None: - self._transactions = list(get_block_transactions(self.hex)) - return self._transactions @property def header(self): """Returns a BlockHeader object corresponding to this block""" - if self._header is None: - self._header = BlockHeader.from_hex(self.hex[:80]) return self._header diff --git a/blockchain_parser/block_header.py b/blockchain_parser/block_header.py index 9e615e8..29f8110 100644 --- a/blockchain_parser/block_header.py +++ b/blockchain_parser/block_header.py @@ -9,82 +9,61 @@ # modified, propagated, or distributed except according to the terms contained # in the LICENSE file. +import struct from datetime import datetime from bitcoin.core import CBlockHeader -from .utils import decode_uint32, format_hash +from .utils import format_hash class BlockHeader(object): """Represents a block header""" def __init__(self, raw_hex): - self._version = None - self._previous_block_hash = None - self._merkle_root = None - self._timestamp = None - self._bits = None - self._nonce = None - self._difficulty = None - - self.hex = raw_hex[:80] - - def __repr__(self): - return "BlockHeader(previous_block_hash=%s)" % self.previous_block_hash - - @classmethod - def from_hex(cls, raw_hex): - """Builds a BlockHeader object from its bytes representation""" - return cls(raw_hex) - + self._hex = raw_hex[:80] + self._version,\ + self._previous_block_hash,\ + self._merkle_root,\ + self._timestamp,\ + self._bits,\ + self._nonce = struct.unpack("