@@ -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
0 commit comments