Skip to content

Commit

Permalink
Merge pull request #1799 from statechannels/check-reorg
Browse files Browse the repository at this point in the history
Check block is still in chain before processing event
  • Loading branch information
bitwiseguy authored Oct 3, 2023
2 parents adb3d13 + 7281fb4 commit 0d8c24e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion node/engine/chainservice/eth_chainservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,22 @@ func (ecs *EthChainService) updateEventTracker(errorChan chan<- error, blockNumb
eventsToDispatch := []ethTypes.Log{}
for ecs.eventTracker.events.Len() > 0 && ecs.eventTracker.latestBlockNum >= (ecs.eventTracker.events)[0].BlockNumber+REQUIRED_BLOCK_CONFIRMATIONS {
chainEvent := ecs.eventTracker.Pop()
eventsToDispatch = append(eventsToDispatch, chainEvent)
ecs.logger.Debug("event popped from queue", "updated-queue-length", ecs.eventTracker.events.Len())

// Ensure event & associated tx is still in the chain before adding to eventsToDispatch
oldBlock, err := ecs.chain.BlockByNumber(context.Background(), new(big.Int).SetUint64(chainEvent.BlockNumber))
if err != nil {
ecs.logger.Error("failed to fetch block: %v", err)
errorChan <- fmt.Errorf("failed to fetch block: %v", err)
return
}

if oldBlock.Hash() != chainEvent.BlockHash {
ecs.logger.Warn("dropping event because its block is no longer in the chain (possible re-org)", "blockNumber", chainEvent.BlockNumber, "blockHash", chainEvent.BlockHash)
continue
}

eventsToDispatch = append(eventsToDispatch, chainEvent)
}
ecs.eventTracker.mu.Unlock()

Expand Down

0 comments on commit 0d8c24e

Please sign in to comment.