Skip to content

Instantly share code, notes, and snippets.

@LarryRuane
Created December 31, 2023 20:52
Show Gist options
  • Save LarryRuane/8347b5fc44c846f1de1a7955202ebdcc to your computer and use it in GitHub Desktop.
Save LarryRuane/8347b5fc44c846f1de1a7955202ebdcc to your computer and use it in GitHub Desktop.
print heights of blocks with out-of-order timestamps
#!/usr/bin/env python3
# When two cosecutive blocks have out of order timestamps (the higher height's time
# is less than the lower height's time), print the height of the lower-height block.
#
# prerequisite;
# $ pip3 install python-bitcoinrpc
# or see https://github.com/jgarzik/python-bitcoinrpc
from bitcoinrpc.authproxy import AuthServiceProxy
api = AuthServiceProxy("http://lmr:lmr@127.0.0.1:8332")
info = api.getblockchaininfo()
tip_height = info['blocks']
prev_time = 0
for height in range(tip_height, 0, -1):
blockhash = api.getblockhash(height)
b = api.getblock(blockhash, 1)
block_time = b['time']
if prev_time > 0 and block_time > prev_time:
print(b['height'])
prev_time = block_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment