Skip to content

Instantly share code, notes, and snippets.

@ksaynice
Created January 26, 2019 09:13
Show Gist options
  • Save ksaynice/11ea00d2872e6e110d83090335a4bd8b to your computer and use it in GitHub Desktop.
Save ksaynice/11ea00d2872e6e110d83090335a4bd8b to your computer and use it in GitHub Desktop.
eth_iceage avg block time simulator
import random
import datetime
import sys
def calc_bomb(i):
subtract = 52 if i >= CNSTNTNPL_FORK_BLKNUM else 32
period = i // 100000
if (period > 0):
# Subtract 2, this is the original formula
# Subtract another 30, this is the Byzantium delay
# Subtract another 50, this is the Constantinople delay
return 2**(period - subtract)
return 0
def calc_difficulty(i, cur_time, prev_diff, prev_time):
new_diff = prev_diff + prev_diff // 2048 * max(1 - (cur_time - prev_time) // 9, -99)
new_diff += calc_bomb(i)
return new_diff
first_block = 7_127_339
times = [1548476384]
#diffs = [3_033_556_249_786_083]
diffs = [2_800_541_000_000_000]
hashpower = diffs[0] / 14
CNSTNTNPL_FORK_BLKNUM = 7_280_000
for i in range(first_block, 10_000_000):
blocktime = random.expovariate(hashpower / diffs[-1])
new_time = times[-1] + blocktime
new_diff = calc_difficulty(i, new_time, diffs[-1], times[-1])
times.append(new_time)
diffs.append(new_diff)
predicted_avg_blocktime = diffs[-1] / hashpower
if i % 10000 == 0:
print('Block %d, time %r blocktime %.2f diffratio %.4f' % (
i,
datetime.datetime.utcfromtimestamp(times[-1]).isoformat().replace('T',' '),
predicted_avg_blocktime,
calc_bomb(i) / diffs[-1],
))
if predicted_avg_blocktime > 60:
sys.exit('Predicted block time exceeded 60s')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment