From 747d95739b9cc546d845f4cecaa8dc7226c91543 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 3 Apr 2024 02:57:40 -0400 Subject: [PATCH] bugfixes --- src/dexorder/blockstate/branch.py | 2 +- src/dexorder/blockstate/state.py | 4 ++-- src/dexorder/event_handler.py | 1 + src/dexorder/pools.py | 2 +- src/dexorder/runner.py | 21 ++++++++++++--------- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/dexorder/blockstate/branch.py b/src/dexorder/blockstate/branch.py index 415c19f..050dd67 100644 --- a/src/dexorder/blockstate/branch.py +++ b/src/dexorder/blockstate/branch.py @@ -60,7 +60,7 @@ class Branch: def __str__(self): # noinspection PyTypeChecker return (f"Branch#{str(self.id)[2:7]}[{self.height}" + - (':' if self.contiguous else '_') + + ('_' if self.path else '') + ('<-'.join(hexstr(b)[2:7] for b in self.path) if self.path else f'{self.start}') + (f':{self.start}' if self.disjoint else '') + f'<-({hexstr(self.parent)[2:7]})]') diff --git a/src/dexorder/blockstate/state.py b/src/dexorder/blockstate/state.py index 55ca214..7fb9909 100644 --- a/src/dexorder/blockstate/state.py +++ b/src/dexorder/blockstate/state.py @@ -176,7 +176,7 @@ class BlockState: difflist = self.diffs_by_series.get(diff.series,{}).get(diff.key) if difflist is not None: difflist.remove(diff.entry) - state_log.info(('promoting' if remove_series_diffs else 'removed')+f' branch {branch}') + state_log.info(('removed' if remove_series_diffs else 'promoting')+f' branch {branch}') def get(self, fork: Fork, series, key, default=NARG): @@ -325,7 +325,7 @@ class BlockState: pass self.root_branch = fork.branch - state_log.info(f'promoted {self.root_branch.height} '+(hexstr(self.root_branch.path[0])[:7]+' ' if self.root_branch.path else '')+' '.join(str(b) for b in reversed(promotion_branches))) + # state_log.info(f'promoted {self.root_branch.height} '+(hexstr(self.root_branch.path[0])[:7]+' ' if self.root_branch.path else '')+' '.join(str(b) for b in reversed(promotion_branches))) return diffs # old code that would remove a series entirely upon promotion of the branch that deleted it diff --git a/src/dexorder/event_handler.py b/src/dexorder/event_handler.py index 46df375..1169c4a 100644 --- a/src/dexorder/event_handler.py +++ b/src/dexorder/event_handler.py @@ -149,6 +149,7 @@ async def handle_uniswap_swaps(swaps: list[EventData]): async def handle_uniswap_swap(swap: EventData): + # todo gather prices first then apply only the value at the end of the block data = await get_uniswap_data(swap) if data is None: return diff --git a/src/dexorder/pools.py b/src/dexorder/pools.py index e070ee5..91714e6 100644 --- a/src/dexorder/pools.py +++ b/src/dexorder/pools.py @@ -51,7 +51,7 @@ async def load_pool(address: str) -> PoolDict: except ValueError as v: try: code = v.args[0].get('code') - except: + except Exception: raise v else: if code == -32000: diff --git a/src/dexorder/runner.py b/src/dexorder/runner.py index 04329af..a9ea51b 100644 --- a/src/dexorder/runner.py +++ b/src/dexorder/runner.py @@ -1,6 +1,6 @@ import asyncio import logging -from asyncio import Event +from asyncio import Event, CancelledError from datetime import timedelta from typing import Any, Iterable, Callable, Optional @@ -93,7 +93,7 @@ class BlockStateRunner(BlockProgressor): await async_yield() except (ConnectionClosedError, TimeoutError, asyncio.TimeoutError) as e: log.debug(f'runner timeout {e}') - except: + except Exception: log.exception(f'Unhandled exception during run_polling()') finally: # noinspection PyBroadException @@ -134,7 +134,7 @@ class BlockStateRunner(BlockProgressor): log.debug(f'runner timeout {e}') except (ConnectionClosedError, TimeoutError) as e: log.debug(f'runner timeout {e}') - except: + except Exception: log.exception(f'Unhandled exception during run_polling()') finally: # noinspection PyBroadException @@ -182,7 +182,7 @@ class BlockStateRunner(BlockProgressor): if block.height - self.state.height >= chain.confirms * 2: # create a disjoint backfilling branch - start = self.state.root_branch.height + 1 + start = self.state.height + 1 # do not query more than the chain's batch size # do not query into the reorgable area. only query finalized data. height = min( start + chain.batch_size, block.height - chain.confirms) @@ -226,8 +226,11 @@ class BlockStateRunner(BlockProgressor): try: if fork is not None: await self.process(fork) - except: - log.exception('Reverting branch due to exception') + except BaseException as e: + if isinstance(e, Exception): + log.exception(f'Reverting {fork.branch} due to exception') + else: + log.info(f'Reverting {fork.branch} due to {e}') self.state.remove_branch(fork.branch) except Exception: log.exception('Unhandled exception in runner worker') @@ -307,7 +310,7 @@ class BlockStateRunner(BlockProgressor): # todo try/except for known retryable errors # noinspection PyCallingNonCallable await maywait(callback(promotion_fork, diff_items)) - except: # legitimately catch EVERYTHING because we re-raise + except Exception: # legitimately catch EVERYTHING because we re-raise log.debug('rolling back session') if session is not None: session.rollback() @@ -328,7 +331,7 @@ class BlockStateRunner(BlockProgressor): # todo separate out the transaction manager completely from runner try: await create_and_send_transactions() - except: + except BaseException: db.session.rollback() raise else: @@ -357,7 +360,7 @@ class BlockStateRunner(BlockProgressor): for callback in self.postprocess_cbs: # noinspection PyCallingNonCallable await maywait(callback()) - except: + except BaseException: session.rollback() raise else: