This commit is contained in:
Tim
2024-03-14 00:47:16 -04:00
parent dfd1c7137e
commit b1df816d3f
7 changed files with 20 additions and 16 deletions

View File

@@ -58,15 +58,16 @@ class DisjointFork:
def __init__(self, block: Block, root: Block): def __init__(self, block: Block, root: Block):
self.height = block.height self.height = block.height
self.hash = block.hash self.hash = block.hash
self.parent = root self.parent = root.hash
self.disjoint = True self.disjoint = True
self.root = root
def __contains__(self, item): def __contains__(self, item):
if item.height > self.height: if item.height > self.height:
return False # item is in the future 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 True # item is ancient
return item.hash in (self.hash, self.parent.hash) return item.hash in (self.hash, self.parent)
def __str__(self): def __str__(self):
return f'{self.height}_[{self.hash.hex()}->{self.parent.hash.hex()}]' return f'{self.height}_[{self.hash.hex()}->{self.parent.hash.hex()}]'

View File

@@ -12,7 +12,7 @@ log = logging.getLogger(__name__)
@alru_cache(maxsize=1024) @alru_cache(maxsize=1024)
async def get_block_timestamp(blockhash) -> int: async def get_block_timestamp(blockhash) -> int:
try: try:
return current_blockstate.get().by_hash[blockhash] return current_blockstate.get().by_hash[blockhash].timestamp
except (LookupError, KeyError): except (LookupError, KeyError):
pass pass
response = await current_w3.get().provider.make_request('eth_getBlockByHash', [blockhash, False]) response = await current_w3.get().provider.make_request('eth_getBlockByHash', [blockhash, False])

View File

@@ -39,7 +39,7 @@ def get_by_chain(d):
return d[chain_id] return d[chain_id]
except KeyError: except KeyError:
_load_chain(chain_id) _load_chain(chain_id)
return d[chain_id] return d.get(chain_id)
def get_factory_contract() -> ContractProxy: def get_factory_contract() -> ContractProxy:
return get_by_chain(_factory) return get_by_chain(_factory)

View File

@@ -27,7 +27,7 @@ class Block(Base):
return raw if type(raw) is int else hexint(raw) return raw if type(raw) is int else hexint(raw)
def __str__(self): 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 current_block = ContextVar[Block]('Block.cur') # block for the current thread

View File

@@ -281,19 +281,20 @@ class OHLCRepository:
def dir(self): def dir(self):
if self._dir is None: if self._dir is None:
self._dir = config.ohlc_dir self._dir = config.ohlc_dir
if self._dir is None:
raise ValueError('OHLCRepository needs an ohlc_dir configured')
return self._dir return self._dir
@property @property
def quotes(self) -> dict[str,tuple[int,str]]: def quotes(self) -> dict[str,tuple[int,str]]:
if self._quotes is None: if self._quotes is None:
try: if self._dir is None:
with open(os.path.join(self.dir, quotes_path()), 'r') as f:
self._quotes = json.load(f)
except FileNotFoundError:
self._quotes = {} 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 return self._quotes

View File

@@ -95,7 +95,7 @@ class BlockProgressor(metaclass=ABCMeta):
try: try:
parsed = event.process_log(log_event) if event is not None else log_event parsed = event.process_log(log_event) if event is not None else log_event
except (LogTopicError, MismatchedABI) as e: 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 = NARG # need a placeholder
parsed_events.append(parsed) parsed_events.append(parsed)
# todo try/except for known retryable errors # todo try/except for known retryable errors

View File

@@ -9,7 +9,7 @@ from websockets.exceptions import ConnectionClosedError
from dexorder import Blockchain, db, current_pub, async_yield, current_w3, config, NARG 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.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.blockchain.connection import create_w3_ws, create_w3
from dexorder.blockstate import BlockState, current_blockstate from dexorder.blockstate import BlockState, current_blockstate
from dexorder.blockstate.diff import DiffEntryItem from dexorder.blockstate.diff import DiffEntryItem
@@ -266,7 +266,7 @@ class BlockStateRunner(BlockProgressor):
# initialize # initialize
self.state = BlockState(block) self.state = BlockState(block)
current_blockstate.set(self.state) 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') log.info('Created new empty root state')
else: else:
fork = self.state.add_block(block) fork = self.state.add_block(block)
@@ -321,15 +321,17 @@ class BlockStateRunner(BlockProgressor):
promotion_height = latest_block.get().height - confirm_offset promotion_height = latest_block.get().height - confirm_offset
new_root_fork = None new_root_fork = None
if fork.disjoint: if fork.disjoint:
fork: DisjointFork
# individually check the fork's head and ancestor # individually check the fork's head and ancestor
if fork.height <= promotion_height: if fork.height <= promotion_height:
new_root_fork = fork new_root_fork = fork
else: else:
state = current_blockstate.get() state = current_blockstate.get()
parent_block = state.by_hash[fork.parent] parent_block = fork.root
if parent_block.height <= promotion_height: if parent_block.height <= promotion_height:
new_root_fork = state.fork(parent_block) new_root_fork = state.fork(parent_block)
else: else:
fork: Fork
# non-disjoint, contiguous fork # non-disjoint, contiguous fork
if fork.height <= promotion_height: if fork.height <= promotion_height:
new_root_fork = fork new_root_fork = fork