Skip to content

Commit 78a78db

Browse files
committed
Updating the height for blocks
1 parent dbf8e58 commit 78a78db

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ for block in blockchain.get_unordered_blocks():
1919
for tx in block.transactions:
2020
for no, output in enumerate(tx.outputs):
2121
print("tx=%s outputno=%d type=%s value=%s" % (tx.hash, no, output.type, output.value))
22+
23+
# To get the blocks ordered by height, you need to provide the path of the
24+
# `index` directory (LevelDB index) being maintained by bitcoind. It contains
25+
# .ldb files and is present inside the `blocks` directory
26+
for block in blockchain.get_ordered_blocks(sys.argv[1] + '/index', end=1000):
27+
print("height=%d block=%s" % (block.height, block.hash))
2228
```
2329

2430
More examples are available in the examples directory.

blockchain_parser/block.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ class Block(object):
3838
Represents a Bitcoin block, contains its header and its transactions.
3939
"""
4040

41-
def __init__(self, raw_hex):
41+
def __init__(self, raw_hex, height = None):
4242
self.hex = raw_hex
4343
self._hash = None
4444
self._transactions = None
4545
self._header = None
4646
self._n_transactions = None
4747
self.size = len(raw_hex)
48-
self.height = None
48+
self.height = height
4949

5050
def __repr__(self):
5151
return "Block(%s)" % self.hash

blockchain_parser/blockchain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ def get_ordered_blocks(self, index, start=0, end=None):
114114

115115
for blkIdx in blockIndexes[start:end]:
116116
blkFile = os.path.join(self.path, "blk%05d.dat" % blkIdx.nFile)
117-
yield Block(get_block(blkFile, blkIdx.dataPos))
117+
yield Block(get_block(blkFile, blkIdx.dataPos), blkIdx.height)

0 commit comments

Comments
 (0)