Skip to content

Commit 46e4af3

Browse files
committed
Stop using memoize for average block time; atxm instance will now keep a cached value of the determined busy interval that uses average block time.
1 parent 09ca18a commit 46e4af3

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

atxm/machine.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ def __init__(
130130
self._task.clock = self.__CLOCK
131131
self._task.interval = self._IDLE_INTERVAL
132132

133+
# busy interval
134+
self._busy_interval = None
135+
133136
super().__init__()
134137

135138
self.add_listener(_Machine.LogObserver())
@@ -173,12 +176,15 @@ def _enter_idle_mode(self):
173176
@_transition_to_busy.before
174177
def _enter_busy_mode(self):
175178
"""About to enter busy work mode (speed up interval)"""
176-
average_block_time = _get_average_blocktime(
177-
w3=self.w3, sample_size=self._BLOCK_SAMPLE_SIZE
178-
)
179-
self._task.interval = max(
180-
round(average_block_time * self._BLOCK_INTERVAL), self._MIN_INTERVAL
181-
)
179+
if self._busy_interval is None:
180+
average_block_time = _get_average_blocktime(
181+
w3=self.w3, sample_size=self._BLOCK_SAMPLE_SIZE
182+
)
183+
self._busy_interval = max(
184+
round(average_block_time * self._BLOCK_INTERVAL), self._MIN_INTERVAL
185+
)
186+
187+
self._task.interval = self._busy_interval
182188
self.log.info(f"[working] cycle interval is now {self._task.interval} seconds")
183189

184190
@_BUSY.enter

atxm/utils.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import contextlib
22
from typing import Callable, Optional
33

4-
from cytoolz import memoize
54
from eth_utils import ValidationError
65
from twisted.internet import reactor
76
from web3 import Web3
@@ -18,7 +17,6 @@
1817
from atxm.tx import AsyncTx, PendingTx, TxHash
1918

2019

21-
@memoize
2220
def _get_average_blocktime(w3: Web3, sample_size: int) -> float:
2321
"""Returns the average block time in seconds."""
2422
latest_block = w3.eth.get_block("latest")

0 commit comments

Comments
 (0)