Skip to content
This repository was archived by the owner on Mar 18, 2026. It is now read-only.

Commit e8b0142

Browse files
Merge pull request #444 from aibtcdev/add-more-checking
add-more-checking
2 parents ba58431 + c238390 commit e8b0142

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

app/services/integrations/webhooks/chainhook/handler.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,32 @@ async def handle(self, parsed_data: ChainHookData) -> Dict[str, Any]:
107107
await self.block_state_handler.handle_block(apply)
108108

109109
# Check if BlockStateHandler successfully processed *this* block.
110-
# This implies the block was newer than the DB state AND the DB update succeeded.
111-
# We check if the handler's internal state now matches this block's height.
110+
# This happens when: 1) Block is newer than DB state, 2) DB update succeeded
111+
# We validate by checking if the handler's state matches this block's height
112+
current_db_state = self.block_state_handler.latest_chain_state
113+
block_height = apply.block_identifier.index
114+
112115
block_processed_by_state_handler = (
113-
self.block_state_handler.latest_chain_state is not None
114-
and self.block_state_handler.latest_chain_state.block_height
115-
== apply.block_identifier.index
116+
current_db_state is not None
117+
and current_db_state.block_height == block_height
116118
)
117119

120+
# Additional validation: ensure block is not older than database state
121+
if (
122+
current_db_state
123+
and block_height <= current_db_state.block_height
124+
and not block_processed_by_state_handler
125+
):
126+
self.logger.info(
127+
f"Block {block_height} is older than or equal to current DB state "
128+
f"({current_db_state.block_height}). Skipping processing to prevent duplicate work."
129+
)
130+
continue # Skip to the next block
131+
118132
if not block_processed_by_state_handler:
119133
self.logger.warning(
120-
f"Block {apply.block_identifier.index} was not processed by BlockStateHandler "
121-
f"(likely older than current DB state or failed update). Skipping other handlers for this block."
134+
f"Block {block_height} was not processed by BlockStateHandler "
135+
f"(failed update or other error). Skipping other handlers for this block."
122136
)
123137
continue # Skip to the next block in the webhook payload
124138

app/services/integrations/webhooks/chainhook/handlers/block_state_handler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,14 @@ async def handle_block(self, block: Apply) -> None:
9595
)
9696

9797
if current_state:
98+
# Ensure we have the current state loaded in memory
99+
self._latest_chain_state = current_state
100+
98101
# Only update if new block is higher
99102
if block_height <= current_state.block_height:
100103
self.logger.debug(
101104
f"Skipping block update - current: {current_state.block_height}, "
102-
f"new: {block_height}"
105+
f"new: {block_height} (block is older or equal)"
103106
)
104107
return
105108

0 commit comments

Comments
 (0)