Created
July 10, 2018 09:41
-
-
Save EggPool/03a30906a9c707bd63f70ddceb7ebd5a to your computer and use it in GitHub Desktop.
diff get
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def difficulty(c): | |
execute(c, "SELECT block_height, timestamp FROM transactions WHERE reward != 0 ORDER BY block_height DESC LIMIT 2") | |
block_height, timestamp_last = c.fetchone() | |
timestamp_before_last = c.fetchone()[1] | |
execute_param(c, "SELECT timestamp FROM transactions WHERE block_height > ? AND reward != 0 ORDER BY timestamp ASC LIMIT 2", (block_height - 1441,)) | |
timestamp_1441 = c.fetchone()[0] | |
block_time_prev = (timestamp_before_last - timestamp_1441) / 1440 | |
timestamp_1440 = c.fetchone()[0] | |
block_time = (timestamp_last - timestamp_1440) / 1440 | |
execute(c, "SELECT difficulty FROM misc ORDER BY block_height DESC LIMIT 1") | |
diff_block_previous = c.fetchone()[0] | |
time_to_generate = timestamp_last - timestamp_before_last | |
hashrate = pow(2, diff_block_previous / 2.0) / (block_time * math.ceil(28 - diff_block_previous / 16.0)) | |
# Calculate new difficulty for desired blocktime of 60 seconds | |
target = 60.0 | |
difficulty_new = (2 / math.log(2)) * math.log(hashrate * target * math.ceil(28 - diff_block_previous / 16.0)) | |
# Feedback controller | |
Kd = 10 | |
difficulty_new = difficulty_new - Kd * (block_time - block_time_prev) | |
diff_adjustment = (difficulty_new - diff_block_previous) / 720 # reduce by factor of 720 | |
if diff_adjustment > 1: | |
diff_adjustment = 1 | |
difficulty_new_adjusted = diff_block_previous + diff_adjustment | |
difficulty = difficulty_new_adjusted | |
# Tail Removal Code | |
diff_drop_time = 180 | |
if time.time() > timestamp_last + diff_drop_time: | |
time_difference = time.time() - timestamp_last | |
diff_dropped = difficulty - time_difference / diff_drop_time | |
else: | |
diff_dropped = difficulty | |
# End Tail Removal Code | |
if difficulty < 50: | |
difficulty = 50 | |
if diff_dropped < 50: | |
diff_dropped = 50 | |
return (difficulty, diff_dropped, time_to_generate, diff_block_previous, block_time, | |
hashrate, diff_adjustment, block_height) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment