Skip to content

Commit

Permalink
Stop using memoize for average block time; atxm instance will now kee…
Browse files Browse the repository at this point in the history
…p a cached value of the determined busy interval that uses average block time.
  • Loading branch information
derekpierre committed Jul 29, 2024
1 parent 09ca18a commit 46e4af3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 12 additions & 6 deletions atxm/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ def __init__(
self._task.clock = self.__CLOCK
self._task.interval = self._IDLE_INTERVAL

# busy interval
self._busy_interval = None

super().__init__()

self.add_listener(_Machine.LogObserver())
Expand Down Expand Up @@ -173,12 +176,15 @@ def _enter_idle_mode(self):
@_transition_to_busy.before
def _enter_busy_mode(self):
"""About to enter busy work mode (speed up interval)"""
average_block_time = _get_average_blocktime(
w3=self.w3, sample_size=self._BLOCK_SAMPLE_SIZE
)
self._task.interval = max(
round(average_block_time * self._BLOCK_INTERVAL), self._MIN_INTERVAL
)
if self._busy_interval is None:
average_block_time = _get_average_blocktime(
w3=self.w3, sample_size=self._BLOCK_SAMPLE_SIZE
)
self._busy_interval = max(
round(average_block_time * self._BLOCK_INTERVAL), self._MIN_INTERVAL
)

self._task.interval = self._busy_interval
self.log.info(f"[working] cycle interval is now {self._task.interval} seconds")

@_BUSY.enter
Expand Down
2 changes: 0 additions & 2 deletions atxm/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import contextlib
from typing import Callable, Optional

from cytoolz import memoize
from eth_utils import ValidationError
from twisted.internet import reactor
from web3 import Web3
Expand All @@ -18,7 +17,6 @@
from atxm.tx import AsyncTx, PendingTx, TxHash


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

0 comments on commit 46e4af3

Please sign in to comment.