bugfixes
This commit is contained in:
@@ -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()}]'
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user