diff --git a/src/dexorder/base/fork.py b/src/dexorder/base/fork.py index 875a6c9..a66c770 100644 --- a/src/dexorder/base/fork.py +++ b/src/dexorder/base/fork.py @@ -58,15 +58,16 @@ class DisjointFork: def __init__(self, block: Block, root: Block): self.height = block.height self.hash = block.hash - self.parent = root + self.parent = root.hash self.disjoint = True + self.root = root def __contains__(self, item): if item.height > self.height: return False # item is in the future - if item.height < self.parent.height: + if item.height < self.root.height: return True # item is ancient - return item.hash in (self.hash, self.parent.hash) + return item.hash in (self.hash, self.parent) def __str__(self): return f'{self.height}_[{self.hash.hex()}->{self.parent.hash.hex()}]' diff --git a/src/dexorder/blocktime.py b/src/dexorder/blocktime.py index 263667e..cff9806 100644 --- a/src/dexorder/blocktime.py +++ b/src/dexorder/blocktime.py @@ -12,7 +12,7 @@ log = logging.getLogger(__name__) @alru_cache(maxsize=1024) async def get_block_timestamp(blockhash) -> int: try: - return current_blockstate.get().by_hash[blockhash] + return current_blockstate.get().by_hash[blockhash].timestamp except (LookupError, KeyError): pass response = await current_w3.get().provider.make_request('eth_getBlockByHash', [blockhash, False]) diff --git a/src/dexorder/contract/dexorder.py b/src/dexorder/contract/dexorder.py index 43acd6a..7833c81 100644 --- a/src/dexorder/contract/dexorder.py +++ b/src/dexorder/contract/dexorder.py @@ -39,7 +39,7 @@ def get_by_chain(d): return d[chain_id] except KeyError: _load_chain(chain_id) - return d[chain_id] + return d.get(chain_id) def get_factory_contract() -> ContractProxy: return get_by_chain(_factory) diff --git a/src/dexorder/database/model/block.py b/src/dexorder/database/model/block.py index 40a7c71..3cf7d00 100644 --- a/src/dexorder/database/model/block.py +++ b/src/dexorder/database/model/block.py @@ -27,7 +27,7 @@ class Block(Base): return raw if type(raw) is int else hexint(raw) def __str__(self): - return f'{self.height}_{self.hash.hex()[:5]}' + return f'{self.height}_{self.hash.hex()[2:7]}' current_block = ContextVar[Block]('Block.cur') # block for the current thread diff --git a/src/dexorder/ohlc.py b/src/dexorder/ohlc.py index 0039635..d2f9fde 100644 --- a/src/dexorder/ohlc.py +++ b/src/dexorder/ohlc.py @@ -281,19 +281,20 @@ class OHLCRepository: def dir(self): if self._dir is None: self._dir = config.ohlc_dir - if self._dir is None: - raise ValueError('OHLCRepository needs an ohlc_dir configured') return self._dir @property def quotes(self) -> dict[str,tuple[int,str]]: if self._quotes is None: - try: - with open(os.path.join(self.dir, quotes_path()), 'r') as f: - self._quotes = json.load(f) - except FileNotFoundError: + if self._dir is None: self._quotes = {} + else: + try: + with open(os.path.join(self.dir, quotes_path()), 'r') as f: + self._quotes = json.load(f) + except FileNotFoundError: + self._quotes = {} return self._quotes diff --git a/src/dexorder/progressor.py b/src/dexorder/progressor.py index 61cada6..7b0d356 100644 --- a/src/dexorder/progressor.py +++ b/src/dexorder/progressor.py @@ -95,7 +95,7 @@ class BlockProgressor(metaclass=ABCMeta): try: parsed = event.process_log(log_event) if event is not None else log_event except (LogTopicError, MismatchedABI) as e: - log.warning(f'logevent parse error {e}\n{log_event}') + # log.debug(f'logevent parse error {e}\n{log_event}') # this happens for Swap events from non-Uniswap pools parsed = NARG # need a placeholder parsed_events.append(parsed) # todo try/except for known retryable errors diff --git a/src/dexorder/runner.py b/src/dexorder/runner.py index ca5de55..04759b6 100644 --- a/src/dexorder/runner.py +++ b/src/dexorder/runner.py @@ -9,7 +9,7 @@ from websockets.exceptions import ConnectionClosedError from dexorder import Blockchain, db, current_pub, async_yield, current_w3, config, NARG from dexorder.base.chain import current_chain, current_clock, BlockClock -from dexorder.base.fork import current_fork, Fork +from dexorder.base.fork import current_fork, Fork, DisjointFork from dexorder.blockchain.connection import create_w3_ws, create_w3 from dexorder.blockstate import BlockState, current_blockstate from dexorder.blockstate.diff import DiffEntryItem @@ -266,7 +266,7 @@ class BlockStateRunner(BlockProgressor): # initialize self.state = BlockState(block) current_blockstate.set(self.state) - fork = Fork([block.hash], height=block.height) + fork: Fork = Fork([block.hash], height=block.height) log.info('Created new empty root state') else: fork = self.state.add_block(block) @@ -321,15 +321,17 @@ class BlockStateRunner(BlockProgressor): promotion_height = latest_block.get().height - confirm_offset new_root_fork = None if fork.disjoint: + fork: DisjointFork # individually check the fork's head and ancestor if fork.height <= promotion_height: new_root_fork = fork else: state = current_blockstate.get() - parent_block = state.by_hash[fork.parent] + parent_block = fork.root if parent_block.height <= promotion_height: new_root_fork = state.fork(parent_block) else: + fork: Fork # non-disjoint, contiguous fork if fork.height <= promotion_height: new_root_fork = fork