From 2723c96f1e534d91d4a74ee2bfa32e45f8a586dc Mon Sep 17 00:00:00 2001 From: Peter Todd Date: Sat, 2 Jan 2021 00:15:08 -0500 Subject: [PATCH] Fix randomized min-timestamp interval to be stable 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. --- otsserver/stamper.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/otsserver/stamper.py b/otsserver/stamper.py index 3393836..da05ec6 100644 --- a/otsserver/stamper.py +++ b/otsserver/stamper.py @@ -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) @@ -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)