From 62f0f903986cd86eb854a1b8b3a4c62de2fe3023 Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 25 Jul 2024 17:22:02 -0400 Subject: [PATCH] get block from db bugfixes --- src/dexorder/bin/mirror.py | 4 ++-- src/dexorder/blocks.py | 2 +- src/dexorder/blockstate/db_state.py | 11 ++++++----- src/dexorder/database/__init__.py | 1 + src/dexorder/runner.py | 2 ++ 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/dexorder/bin/mirror.py b/src/dexorder/bin/mirror.py index 07d00ab..26751a0 100644 --- a/src/dexorder/bin/mirror.py +++ b/src/dexorder/bin/mirror.py @@ -202,8 +202,8 @@ async def main(): await tx.wait() last_prices[pool] = price log.debug(f'Mirrored {pool} {price}') - except Exception: - log.debug(f'Could not update {pool}') + except Exception as x: + log.debug(f'Could not update {pool}: {x}') continue try: pool = next(pool_iter) diff --git a/src/dexorder/blocks.py b/src/dexorder/blocks.py index ed46775..0deb470 100644 --- a/src/dexorder/blocks.py +++ b/src/dexorder/blocks.py @@ -51,7 +51,7 @@ async def _fetch(fetch: FetchLock, chain_id: int, block_id: Union[int,bytes]) -> blocks = [b for b in blocks if b.confirmed] found = blocks[0] if len(blocks) == 1 else None else: - found = db.session.get(DbBlock, (chain, block_id)) # by-hash is the primary key + found = db.session.get(DbBlock, dict(chain=chain, hash=block_id)) # by-hash is the primary key if found: return Block(chain_id, found.data) diff --git a/src/dexorder/blockstate/db_state.py b/src/dexorder/blockstate/db_state.py index bd74df7..0773dac 100644 --- a/src/dexorder/blockstate/db_state.py +++ b/src/dexorder/blockstate/db_state.py @@ -9,7 +9,7 @@ from .. import db, DELETE from ..base.chain import current_chain from ..blocks import get_block from ..database.model import SeriesSet, SeriesDict -from ..util import hexstr +from ..util import hexbytes from ..util.shutdown import fatal log = logging.getLogger(__name__) @@ -72,17 +72,18 @@ class DbState(SeriesCollection): async def load(self) -> Optional[BlockState]: chain_id = current_chain.get().id try: - height, hash = db.kv[f'root_block|{chain_id}'] + height, hash_str = db.kv[f'root_block|{chain_id}'] except (KeyError, ValueError): return None # log.debug(f'getting state for hash {hash}') - root_block = await get_block(hash) + blockhash = hexbytes(hash_str) + root_block = await get_block(blockhash) if root_block is None: log.debug(f'couldn\'t find root block by hash. trying number {height}.') root_block = await get_block(height) if root_block is None: - fatal(f'Could not get root block {height} {hash} from RPC') - assert hexstr(root_block.hash) == hash + fatal(f'Could not get root block {height} {hash_str} from RPC') + assert root_block.hash == blockhash assert root_block.height == height state = BlockState() root_fork = state.init_root_block(root_block) diff --git a/src/dexorder/database/__init__.py b/src/dexorder/database/__init__.py index 3b6000c..a15215a 100644 --- a/src/dexorder/database/__init__.py +++ b/src/dexorder/database/__init__.py @@ -83,6 +83,7 @@ class Db: kwargs.setdefault('expire_on_commit', False) s = Session(engine, **kwargs) _session.set(s) + log.debug('created new DB session') return s @staticmethod diff --git a/src/dexorder/runner.py b/src/dexorder/runner.py index 702814f..0017569 100644 --- a/src/dexorder/runner.py +++ b/src/dexorder/runner.py @@ -190,6 +190,8 @@ class BlockStateRunner(BlockProgressor): # do not query into the reorgable area. only query finalized data. height = min( start + chain.batch_size, block.height - chain.confirms) end_block = await get_block(height) + if end_block is None: + raise ValueError(f'Could not get block with height {block.height}') branch = Branch(height, start, path=[end_block.hash]) # no parent return self.state.add_branch(branch)