Skip to content

Instantly share code, notes, and snippets.

@bitcoinfees
Last active December 28, 2015 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitcoinfees/0f2203bdced119b98c30 to your computer and use it in GitHub Desktop.
Save bitcoinfees/0f2203bdced119b98c30 to your computer and use it in GitHub Desktop.
"""
Simulation to estimate the rate of occurrences of the event
'>= 48 blocks in 4 hours'.
"""
from random import expovariate
BLOCKRATE = 1. / 600 # Once per 10 min, the equilibrium block rate
NUMBLOCKS = 144*365*1000 # 1000 years of simulation
# Event constants
BLKTHRESH = 48 # 48 blocks
INTERVALTHRESH = 4*3600 # 4 hours
blocktimes = [None]*BLKTHRESH # The blocktimes of the most recent 48 blocks
t = 0
numexceeded = 0
for n in range(NUMBLOCKS):
curr = n % BLKTHRESH
blocktimes[curr] = t
tbegin = blocktimes[(curr+1) % BLKTHRESH]
if tbegin is not None:
interval = t - tbegin
if interval <= INTERVALTHRESH:
# Last BLKTHRESH blocks took <= INTERVALTHRESH.
# Equivalent to there being >= BLKTHRESH blocks in the past
# INTERVALTHRESH.
numexceeded += 1
# Reset blocktimes; we want independent events
blocktimes = [None]*BLKTHRESH
t += expovariate(BLOCKRATE)
if not n % (1051200):
print("{}% complete".format(n*100./NUMBLOCKS))
print("The event '>= {} blocks in {} hours' happened {} times in {} years.".
format(BLKTHRESH, INTERVALTHRESH/3600, numexceeded, NUMBLOCKS/144/365))
print("This is roughly once every {} years.".
format(NUMBLOCKS/144/365/float(numexceeded)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment