Skip to content

Commit a96f653

Browse files
committed
Add verbose option to getblockheader RPC call
There's no other way to get certain metadata about blocks like the block height.
1 parent d5773b4 commit a96f653

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

bitcoin/rpc.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import urlparse
4444

4545
import bitcoin
46-
from bitcoin.core import COIN, lx, b2lx, CBlock, CBlockHeader, CTransaction, COutPoint, CTxOut
46+
from bitcoin.core import COIN, x, lx, b2lx, CBlock, CBlockHeader, CTransaction, COutPoint, CTxOut
4747
from bitcoin.core.script import CScript
4848
from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret
4949

@@ -318,9 +318,13 @@ def getbestblockhash(self):
318318
"""Return hash of best (tip) block in longest block chain."""
319319
return lx(self._call('getbestblockhash'))
320320

321-
def getblockheader(self, block_hash):
321+
def getblockheader(self, block_hash, verbose=False):
322322
"""Get block header <block_hash>
323323
324+
verbose - If true a dict is returned with the values returned by
325+
getblockheader that are not in the block header itself
326+
(height, nextblockhash, etc.)
327+
324328
Raises IndexError if block_hash is not valid.
325329
"""
326330
try:
@@ -329,11 +333,22 @@ def getblockheader(self, block_hash):
329333
raise TypeError('%s.getblockheader(): block_hash must be bytes; got %r instance' %
330334
(self.__class__.__name__, block_hash.__class__))
331335
try:
332-
r = self._call('getblockheader', block_hash, False)
336+
r = self._call('getblockheader', block_hash, verbose)
333337
except JSONRPCError as ex:
334338
raise IndexError('%s.getblockheader(): %s (%d)' %
335339
(self.__class__.__name__, ex.error['message'], ex.error['code']))
336-
return CBlockHeader.deserialize(unhexlify(r))
340+
341+
if verbose:
342+
nextblockhash = None
343+
if 'nextblockhash' in r:
344+
nextblockhash = lx(r['nextblockhash'])
345+
return {'confirmations':r['confirmations'],
346+
'height':r['height'],
347+
'mediantime':r['mediantime'],
348+
'nextblockhash':nextblockhash,
349+
'chainwork':x(r['chainwork'])}
350+
else:
351+
return CBlockHeader.deserialize(unhexlify(r))
337352

338353

339354
def getblock(self, block_hash):

release-notes.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
python-bitcoinlib release notes
22
===============================
33

4+
v0.6.1
5+
======
6+
7+
New features:
8+
9+
* getblockheader RPC call now supports the verbose option; there's no other way
10+
to get the block height, among other things, from the RPC interface.
11+
12+
413
v0.6.0
514
======
615

0 commit comments

Comments
 (0)