Skip to content

Instantly share code, notes, and snippets.

@harding
Created October 14, 2015 23:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harding/4ef43532fe1131c48f2f to your computer and use it in GitHub Desktop.
Save harding/4ef43532fe1131c48f2f to your computer and use it in GitHub Desktop.
Code to scan for BIP100 coinbase strings
## (python3) Objective: count the number of blocks voting for BIP100 or
## using the BVnnnnnnn convention
# Requires: sudo pip3 install json-rpc
## WARNING: read and understand the following link before writing
## production JSON-parsing code for Bitcoin:
## https://en.bitcoin.it/wiki/Proper_Money_Handling_%28JSON-RPC%29
import requests
import json
from codecs import decode
import re
## Bitcoin Core by default listens on localhost:8332
url = "http://localhost:8332"
## Change this to your Bitcoin Core credentials. These are listed in the
## file .bitcoin/bitcoin.conf as the rpcuser and rpcpassword settings.
## WARNING: Be extremely careful with this information on any system
## that has a wallet or other secure/private information.
auth=('test', 'foobar')
## Prepare a couple counters for the test
bip100_blocks = 0
non_bip100_blocks = 0
## Prepare a regular expression to search binary data for the strings
## associated with BIP100
bip100 = re.compile(b'BIP100|BV[0-9]{7}')
## A simple function to get data from Bitcoin Core over JSON-RPC
def request(payload):
return requests.post(
url,
data=json.dumps(payload),
auth=auth
).json()
## Get the header hash of the most recent block
block = request({
"method": "getbestblockhash"
})['result']
## Loop over the last 100 blocks and process their coinbase
## transactions. This code is not guaranteed to work with Bitcoin Core's
## default settings more than 100 blocks in the past
for i in range(0,100):
## Get all the data from the block
block_data = request({
"method": "getblock",
"params": [ block ]
})
## The header hash of the previous block, which we'll use in the next loop
block = block_data['result']['previousblockhash']
## The txid of the coinbase tarnsaction
coinbase_txid = block_data['result']['tx'][0]
## Each coinbase transaction has a single input; this gets it.
## (This special input is called the coinbase field)
coinbase_field = request({
"method": "getrawtransaction",
"params": [ coinbase_txid, 1 ]
})['result']['vin'][0]['coinbase']
## Convert the coinbase field to binary for searching
coinbase_field_binary = decode(coinbase_field, 'hex')
## Increment counters based on matching
if bip100.search(coinbase_field_binary):
bip100_blocks += 1
else:
non_bip100_blocks += 1
## Show our results
print("BIP100 blocks: ", bip100_blocks)
print("Non-BIP100 blocks: ", non_bip100_blocks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment