From 00f48065a88d87105ac1655ef2314e16bf02c1eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=E1=BA=A3i=20Nam?= Date: Fri, 23 Aug 2024 10:28:07 +0700 Subject: [PATCH] chore: ignore block that too old --- .gitignore | 1 + pkg/listener/handler.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/.gitignore b/.gitignore index 66fd13c..238d5cf 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ +.env diff --git a/pkg/listener/handler.go b/pkg/listener/handler.go index 217bd46..12f9e9f 100644 --- a/pkg/listener/handler.go +++ b/pkg/listener/handler.go @@ -2,6 +2,7 @@ package listener import ( "context" + "math/big" "github.com/KyberNetwork/evmlistener/pkg/block" "github.com/KyberNetwork/evmlistener/pkg/errors" @@ -208,6 +209,19 @@ func (h *Handler) handleNewBlock(ctx context.Context, b types.Block) error { log.Infow("Handling new block") + blockHead, err := h.blockKeeper.Head() + if err == nil { + blockDiff := new(big.Int).Sub(blockHead.Number, b.Number).Int64() + if blockDiff > int64(h.blockKeeper.Cap()) { + log.Warnw("Ignore block that too old", + "blockNumber", b.Number, + "blockHeadNumber", blockHead.Number, + "blockDiff", blockDiff) + + return nil + } + } + isReorg, err := h.blockKeeper.IsReorg(b) if err != nil { log.Errorw("Fail to check for re-organization", "error", err)