fix(bridge): fix fetching too many blocks (#63)

This commit is contained in:
HAOYUatHZ
2022-11-07 17:51:37 +08:00
committed by GitHub
parent 60f30e5b33
commit 0e312f88bb
2 changed files with 19 additions and 0 deletions

View File

@@ -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

View File

@@ -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