Skip to content

Commit

Permalink
Allow difficulty to be changed sooner
Browse files Browse the repository at this point in the history
  • Loading branch information
william-gr committed Mar 23, 2024
1 parent ec326da commit 30e8ff9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
12 changes: 11 additions & 1 deletion pool/difficulty_adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ def get_new_difficulty(

# If we recently updated difficulty, don't update again
if any(difficulty != current_difficulty for timestamp, difficulty in recent_partials):
return current_difficulty

before_last_time = last_time = recent_partials[0][0]
last_diff = recent_partials[0][1]
for partial in recent_partials:
if partial[1] != last_diff:
before_last_time = partial[0]
break

# Do not allow difficulty changes in less than 2 hours
if last_time - before_last_time < 2 * 3600:
return current_difficulty

# Lower the difficulty if we are really slow since our last partial
last_timestamp = recent_partials[0][0]
Expand Down
19 changes: 17 additions & 2 deletions tests/test_difficulty.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ def test_recently_updated(self):

assert get_new_difficulty(partials, num_partials, time_target, 50, 'MEDIUM', current_time, 1) == 50

def test_recently_updated_2h(self):
num_partials = 300
time_target = 24 * 3600
partials = []
current_time = uint64(time.time())
for i in range(num_partials):
if i < 50:
diff = 30
else:
diff = 20
partials.append((current_time - i * 200, diff))

assert get_new_difficulty(partials, num_partials, time_target, 20, 'MEDIUM', current_time, 1) == 28

def test_really_slow(self):
num_partials = 300
time_target = 24 * 3600
Expand Down Expand Up @@ -58,9 +72,10 @@ def test_not_enough_partials_yet(self):
partials = []
current_time = uint64(time.time())
for i in range(num_partials):
partials.append((uint64(current_time - (i) * 200), 20))
partials.append((uint64(current_time - i * 200), 20))

partials[-1] = (partials[-1][0], 15)
# Difficulty changed less than 2 hours ago, so do not change again
partials[20] = (partials[20][0], 15)

# Doesn't change diff
assert get_new_difficulty(partials, num_partials, time_target, 20, 'MEDIUM', current_time, 1) == 20
Expand Down

0 comments on commit 30e8ff9

Please sign in to comment.