diff --git a/bridge/l1/watcher.go b/bridge/l1/watcher.go index 4f7d314ec..02d81e93b 100644 --- a/bridge/l1/watcher.go +++ b/bridge/l1/watcher.go @@ -93,6 +93,8 @@ func (r *Watcher) Stop() { r.stop <- true } +const contractEventsBlocksFetchLimit = int64(10) + // FetchContractEvent pull latest event logs from given contract address and save in DB func (r *Watcher) fetchContractEvent(blockHeight uint64) error { fromBlock := int64(r.processedMsgHeight) + 1 @@ -102,6 +104,10 @@ func (r *Watcher) fetchContractEvent(blockHeight uint64) error { return nil } + if toBlock > fromBlock+contractEventsBlocksFetchLimit { + toBlock = fromBlock + contractEventsBlocksFetchLimit - 1 + } + // warning: uint int conversion... query := ethereum.FilterQuery{ FromBlock: big.NewInt(fromBlock), // inclusive diff --git a/bridge/l2/watcher.go b/bridge/l2/watcher.go index ea72eed0d..9b3c1dbcf 100644 --- a/bridge/l2/watcher.go +++ b/bridge/l2/watcher.go @@ -116,6 +116,8 @@ func (w *WatcherClient) Stop() { w.stopCh <- struct{}{} } +const blockResultsFetchLimit = uint64(10) + // try fetch missing blocks if inconsistent func (w *WatcherClient) tryFetchRunningMissingBlocks(ctx context.Context, backTrackFrom uint64) error { // Get newest block in DB. must have blocks at that time. @@ -130,6 +132,11 @@ func (w *WatcherClient) tryFetchRunningMissingBlocks(ctx context.Context, backTr backTrackTo = uint64(heightInDB) } + // note that backTrackFrom >= backTrackTo because we are doing backtracking + if backTrackFrom > backTrackTo+blockResultsFetchLimit { + backTrackFrom = backTrackTo + blockResultsFetchLimit + } + // start backtracking heights := []uint64{} toSkipped := []*types.BlockResult{} @@ -178,6 +185,8 @@ func (w *WatcherClient) tryFetchRunningMissingBlocks(ctx context.Context, backTr return nil } +const contractEventsBlocksFetchLimit = int64(10) + // FetchContractEvent pull latest event logs from given contract address and save in DB func (w *WatcherClient) fetchContractEvent(blockHeight uint64) error { fromBlock := int64(w.processedMsgHeight) + 1 @@ -187,6 +196,10 @@ func (w *WatcherClient) fetchContractEvent(blockHeight uint64) error { return nil } + if toBlock > fromBlock+contractEventsBlocksFetchLimit { + toBlock = fromBlock + contractEventsBlocksFetchLimit - 1 + } + // warning: uint int conversion... query := ethereum.FilterQuery{ FromBlock: big.NewInt(fromBlock), // inclusive