Skip to content

Commit

Permalink
fix: attempt better handling of reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev committed Nov 2, 2023
1 parent e522d8e commit b678c47
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,8 @@ def poll_blocks(

last_yielded_height = None

reorg_height = None

while True:
if (
stop_block is not None
Expand All @@ -1420,14 +1422,17 @@ def poll_blocks(
latest_block = self.web3.eth.block_number
if last_yielded_height is None:
last_yielded_height = latest_block - required_confirmations
elif latest_block - required_confirmations <= last_yielded_height:
elif latest_block - required_confirmations <= last_yielded_height and reorg_height is None:
print("continuing")
time.sleep(wait_time)
continue
else:
for block_num in range(
last_yielded_height + 1, latest_block - required_confirmations + 1
):
confirmed_block = self.web3.eth.get_block(block_num)
if reorg_height and confirmed_block and confirmed_block.number == reorg_height:
reorg_height = None
if not confirmed_block:
start_time = time.time()
while confirmed_block is None:
Expand All @@ -1445,6 +1450,8 @@ def poll_blocks(
f"{num_blocks_behind} Block reorganization detected. "
+ "Try adjusting the required network confirmations"
)
reorg_height = confirmed_block.number
last_yielded_height = confirmed_block.number
else:
logger.warning(
f"{num_blocks_behind} Block reorganization detected. "
Expand Down

0 comments on commit b678c47

Please sign in to comment.