Skip to content

Instantly share code, notes, and snippets.

@Sjors
Forked from jnewbery/print_coinbase_height.py
Last active December 20, 2023 10:37
Show Gist options
  • Save Sjors/018f764b28efdf29fda60e47f19615a5 to your computer and use it in GitHub Desktop.
Save Sjors/018f764b28efdf29fda60e47f19615a5 to your computer and use it in GitHub Desktop.
block_height OP_RETURN
#!/usr/bin/env python3
"""Search for blocks where the BIP34 indicated coinbase height is > block height.
Original by @jnewbery in 2018, mofied to look for OP_RETURN outputs instead.
# https://gist.githubusercontent.com/jnewbery/df0a98f3d2fea52e487001bf2b9ef1fd
Uses Bitcoin Core's test framework.
Put this file in test/functional to run.
"""
import time
from test_framework.test_framework import BitcoinTestFramework
from test_framework.authproxy import AuthServiceProxy
from test_framework.script import (
CScript,
)
a = AuthServiceProxy("http://<user>:<password>@<ip>:<port>")
START_BLOCK = 1
# BIP34 activated at height 227931, block 246816 is known to have an OP_RETURN
NUM_BLOCKS = 247000
print(','.join(['block_height', 'OP_RETURN']) + '\n')
heights = []
block_hash = a.getblockhash(START_BLOCK)
for block_height in range(START_BLOCK, START_BLOCK + NUM_BLOCKS):
if not block_height % 10000:
print("... processing block at height {}".format(block_height))
block = a.getblock(block_hash, 2)
tx = block['tx'][0]
# Look for any OP_RETURN in a coinbase output
for output in block['tx'][0]['vout']:
script = output['scriptPubKey']['hex']
if script[0:2] == "6a":
# TODO: if anything is found, check SegWit
# magic 0x6a24aa21a9ed in bytes 0-5
print("{} {}\n".format(block_height, script))
block_hash = block["nextblockhash"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment