research/diffadjust/blkdiff.py

151 lines
5.0 KiB
Python
Raw Normal View History

2015-01-13 02:13:56 +00:00
import math, random
2014-12-04 17:09:55 +00:00
hashpower = [float(x) for x in open('hashpower.csv').readlines()]
2015-01-13 02:32:26 +00:00
# Target block time
TARGET = 12
# Should be 86400, but can reduce for a quicker sim
SECONDS_IN_DAY = 86400
# Look at the 1/x day exponential moving average
EMA_FACTOR = 0.01
# Damping factor for simple difficulty adjustment
SIMPLE_ADJUST_DAMPING_FACTOR = 20
# Maximum per-block diff adjustment (as fraction of current diff)
SIMPLE_ADJUST_MAX = 0.5
# Damping factor for quadratic difficulty adjustment
QUADRATIC_ADJUST_DAMPING_FACTOR = 3
# Maximum per-block diff adjustment (as fraction of current diff)
QUADRATIC_ADJUST_MAX = 0.5
# Threshold for bounded adjustor
BOUNDED_ADJUST_THRESHOLD = 1.3
# Bounded adjustment factor
BOUNDED_ADJUST_FACTOR = 0.01
# How many blocks back to look
BLKS_BACK = 10
2015-01-13 05:59:53 +00:00
# Naive difficulty adjustment factor
NAIVE_ADJUST_FACTOR = 1/1024.
2015-01-13 02:32:26 +00:00
# Produces a value according to the exponential distribution; used
# to determine the time until the next block given an average block
# time of t
2014-12-04 17:09:55 +00:00
def expdiff(t):
return -math.log(random.random()) * t
2015-01-13 02:32:26 +00:00
# abs_sqr(3) = 9, abs_sqr(-7) = -49, etc
2015-01-13 02:13:56 +00:00
def abs_sqr(x):
return -(x**2) if x < 0 else x**2
2015-01-13 02:32:26 +00:00
# Given an array of the most recent timestamps, and the most recent
# difficulties, compute the next difficulty
2015-01-13 02:13:56 +00:00
def simple_adjust(timestamps, diffs):
2015-01-13 02:32:26 +00:00
if len(timestamps) < BLKS_BACK + 2:
2015-01-13 02:13:56 +00:00
return diffs[-1]
# Total interval between previous block and block a bit further back
2015-01-13 02:32:26 +00:00
delta = timestamps[-2] - timestamps[-2-BLKS_BACK] + 0.0
2015-01-13 02:13:56 +00:00
# Expected interval
2015-01-13 02:32:26 +00:00
expected = TARGET * BLKS_BACK
# Compute adjustment factor
fac = 1 - (delta / expected - 1) / SIMPLE_ADJUST_DAMPING_FACTOR
fac = max(min(fac, 1 + SIMPLE_ADJUST_MAX), 1 - SIMPLE_ADJUST_MAX)
2015-01-13 02:13:56 +00:00
return diffs[-1] * fac
2015-01-13 02:32:26 +00:00
# Alternative adjustment algorithm
2015-01-13 02:13:56 +00:00
def quadratic_adjust(timestamps, diffs):
2015-01-13 02:32:26 +00:00
if len(timestamps) < BLKS_BACK + 2:
2015-01-13 02:13:56 +00:00
return diffs[-1]
# Total interval between previous block and block a bit further back
2015-01-13 02:32:26 +00:00
delta = timestamps[-2] - timestamps[-2-BLKS_BACK] + 0.0
2015-01-13 02:13:56 +00:00
# Expected interval
2015-01-13 02:32:26 +00:00
expected = TARGET * BLKS_BACK
# Compute adjustment factor
fac = 1 - abs_sqr(delta / expected - 1) / QUADRATIC_ADJUST_DAMPING_FACTOR
fac = max(min(fac, 1 + QUADRATIC_ADJUST_MAX), 1 - QUADRATIC_ADJUST_MAX)
2015-01-13 02:13:56 +00:00
return diffs[-1] * fac
2015-01-13 02:32:26 +00:00
# Alternative adjustment algorithm
2015-01-13 02:13:56 +00:00
def bounded_adjust(timestamps, diffs):
2015-01-13 02:32:26 +00:00
if len(timestamps) < BLKS_BACK + 2:
2014-12-04 17:09:55 +00:00
return diffs[-1]
2015-01-13 02:13:56 +00:00
# Total interval between previous block and block a bit further back
2015-01-13 02:32:26 +00:00
delta = timestamps[-2] - timestamps[-2-BLKS_BACK] + 0.0
2015-01-13 02:13:56 +00:00
# Expected interval
2015-01-13 02:32:26 +00:00
expected = TARGET * BLKS_BACK
if delta / expected > BOUNDED_ADJUST_THRESHOLD:
fac = (1 - BOUNDED_ADJUST_FACTOR)
elif delta / expected < 1 / BOUNDED_ADJUST_THRESHOLD:
fac = (1 + BOUNDED_ADJUST_FACTOR) ** (delta / expected)
2014-12-04 17:09:55 +00:00
else:
fac = 1
return diffs[-1] * fac
2015-01-13 05:59:53 +00:00
# Old Ethereum algorithm
def old_adjust(timestamps, diffs):
if len(timestamps) < 2:
return diffs[-1]
delta = timestamps[-1] - timestamps[-2]
expected = TARGET * 0.693
if delta > expected:
fac = 1 - NAIVE_ADJUST_FACTOR
else:
fac = 1 + NAIVE_ADJUST_FACTOR
return diffs[-1] * fac
2014-12-04 17:09:55 +00:00
def test(source, adjust):
2015-01-13 02:32:26 +00:00
# Variables to keep track of for stats purposes
ema = maxema = minema = TARGET
2015-01-13 02:13:56 +00:00
lthalf, gtdouble, lttq, gtft = 0, 0, 0, 0
2015-01-13 02:32:26 +00:00
count = 0
# Block times
2015-01-13 02:13:56 +00:00
times = [0]
2015-01-13 02:32:26 +00:00
# Block difficulty values
2015-01-13 02:13:56 +00:00
diffs = [source[0]]
2015-01-13 02:32:26 +00:00
# Next time to print status update
2015-01-13 02:13:56 +00:00
nextprint = 10**6
2015-01-13 02:32:26 +00:00
# Main loop
while times[-1] < len(source) * SECONDS_IN_DAY:
# Print status update every 10**6 seconds
2015-01-13 02:13:56 +00:00
if times[-1] > nextprint:
2015-01-13 02:32:26 +00:00
print '%d out of %d processed, ema %f' % \
(times[-1], len(source) * SECONDS_IN_DAY, ema)
2015-01-13 02:13:56 +00:00
nextprint += 10**6
# Grab hashpower from data source
2015-01-13 02:32:26 +00:00
hashpower = source[int(times[-1] // SECONDS_IN_DAY)]
2015-01-13 02:13:56 +00:00
# Calculate new difficulty
diffs.append(adjust(times, diffs))
# Calculate next block time
times.append(times[-1] + expdiff(diffs[-1] / hashpower))
# Calculate min and max ema
2015-01-13 02:32:26 +00:00
ema = ema * (1 - EMA_FACTOR) + (times[-1] - times[-2]) * EMA_FACTOR
2015-01-13 02:13:56 +00:00
minema = min(minema, ema)
maxema = max(maxema, ema)
count += 1
2015-01-13 02:32:26 +00:00
# Keep track of number of blocks we are below 75/50% or above
# 133/200% of target
if ema < TARGET * 0.75:
2015-01-13 02:13:56 +00:00
lttq += 1
2015-01-13 02:32:26 +00:00
if ema < TARGET * 0.5:
2015-01-13 02:13:56 +00:00
lthalf += 1
2015-01-13 02:32:26 +00:00
elif ema > TARGET * 1.33333:
2015-01-13 02:13:56 +00:00
gtft += 1
2015-01-13 02:32:26 +00:00
if ema > TARGET * 2:
2015-01-13 02:13:56 +00:00
gtdouble += 1
# Pop items to save memory
if len(times) > 2000:
times.pop(0)
diffs.pop(0)
print 'min', minema, 'max', maxema, 'avg', times[-1] / count, \
'ema < half', lthalf * 1.0 / count, \
'ema > double', gtdouble * 1.0 / count, \
'ema < 3/4', lttq * 1.0 / count, \
'ema > 4/3', gtft * 1.0 / count
2015-01-13 02:32:26 +00:00
# Example usage
# blkdiff.test(blkdiff.hashpower, blkdiff.simple_adjust)