[Blockchain Watcher] (EVM) Fix improve evm process blocks (#1521)

* Validate latestBlockHeight block

* Improve comments

* Improve validation

* Improve latestBlockHeight validation

* Resolve evm test

* Improve evm process

* Resolve test

* Update diff value

* Improve variables name

---------

Co-authored-by: julian merlo <julianmerlo@julians-MacBook-Pro-2.local>
This commit is contained in:
Julian 2024-06-27 17:54:47 -03:00 committed by GitHub
parent 51f6e8ebb0
commit 7e4a687904
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import { GetEvmLogs } from "./GetEvmLogs";
import { Filters } from "./types";
import winston from "winston";
const MAX_DIFF_BLOCK_HEIGHT = 5_000;
const ID = "watch-evm-logs";
/**
@ -111,9 +112,13 @@ export class PollEvm extends RunPollingJob {
}
let toBlock = BigInt(fromBlock) + BigInt(this.cfg.getBlockBatchSize());
// Limit toBlock to obtained block height and restrict toBlock update because the latestBlockHeight may be outdated
if (toBlock > fromBlock && toBlock > latestBlockHeight && fromBlock < latestBlockHeight) {
toBlock = latestBlockHeight;
// Limit toBlock to obtained block height
if (toBlock > fromBlock && toBlock > latestBlockHeight) {
// Restrict toBlock update because the latestBlockHeight may be outdated
const diffBlockHeight = toBlock - latestBlockHeight;
if (diffBlockHeight <= MAX_DIFF_BLOCK_HEIGHT) {
toBlock = latestBlockHeight;
}
}
// Limit toBlock to configured toBlock

View File

@ -33,7 +33,6 @@ describe("PollEvm", () => {
it("should be able to read logs from latest block when no fromBlock is configured", async () => {
const currentHeight = 10n;
const blocksAhead = 1n;
const blockBatchSize = 100n;
givenEvmBlockRepository(currentHeight, blocksAhead);
givenMetadataRepository();
givenStatsRepository();
@ -54,7 +53,7 @@ describe("PollEvm", () => {
addresses: cfg.filters[0].addresses,
topics: cfg.filters[0].topics,
fromBlock: currentHeight + blocksAhead,
toBlock: currentHeight + blocksAhead + blockBatchSize,
toBlock: currentHeight + blocksAhead,
})
);
});