From e3280dd3db98cbe7cc3a80a10e4d69d87d014798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=E1=BA=A3i=20Nam?= Date: Fri, 23 Aug 2024 11:02:09 +0700 Subject: [PATCH] chore: ignore block that too old (#119) * chore: ignore block that too old * chore: move to more meaningful block --- .gitignore | 1 + pkg/listener/handler.go | 15 +++++++++++++++ 2 files changed, 16 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..0fc1a56 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" @@ -276,5 +277,19 @@ func (h *Handler) Handle(ctx context.Context, b types.Block) error { return nil } + 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 + } + } + return h.handleNewBlock(ctx, b) }