Skip to content

Commit

Permalink
Fix randomized min-timestamp interval to be stable
Browse files Browse the repository at this point in the history
Previously the randomization was applied every block, which meant that the
"Waiting Xs before next tx" log message was inaccurate. Now the randomization
is applied once, after a timestamp is confirmed, by picking a time in the
future to start timestamping again.
  • Loading branch information
petertodd committed Jan 2, 2021
1 parent 1b19143 commit 2723c96
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions otsserver/stamper.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,14 @@ def __do_bitcoin(self):
# Erase all unconfirmed txs, as they all conflict with each other
self.unconfirmed_txs.clear()

# And finally, we can reset the last time a timestamp
# transaction was mined to right now.
self.last_timestamp_tx = time.time()
# Finally, schedule a new timestamp transaction.
#
# To help desync calendars, this time is randomized.
self.next_timestamp_tx = time.time() + (self.min_tx_interval * random.uniform(1, 2))

break

time_to_next_tx = int(self.last_timestamp_tx + self.min_tx_interval * random.uniform(1, 2) - time.time())
time_to_next_tx = self.next_timestamp_tx - time.time()
if time_to_next_tx > 0:
# Minimum interval between transactions hasn't been reached, so do nothing
logging.debug("Waiting %ds before next tx" % time_to_next_tx)
Expand Down Expand Up @@ -559,7 +560,7 @@ def __init__(self, calendar, exit_event, relay_feerate, min_confirmations, min_t
self.pending_commitments = OrderedSet()
self.txs_waiting_for_confirmation = {}

self.last_timestamp_tx = 0
self.next_timestamp_tx = time.time()
self.journal_cursor = None

self.thread = threading.Thread(target=self.__loop)
Expand Down

0 comments on commit 2723c96

Please sign in to comment.