Skip to content

Commit

Permalink
refactor(connector-besu): tx poll per second not at full CPU speed
Browse files Browse the repository at this point in the history
1. Prior to this change the polling function that waits for transactions
to be confirmed was running in  while loop without any delay, meaning that
the code that fetches the latest block is executing thousands of times
each second (or however fast the CPU in the machine/network connection are).
2. Now there is a second delay between each execution of the loop so that
we are not hammering the node of the ledger we are connected to.
3. This also has the added benefit of the test cases using this method
using much less CPU power.

Signed-off-by: Peter Somogyvari <[email protected]>
  • Loading branch information
petermetz committed Apr 3, 2024
1 parent f71c48e commit c4eb4db
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Server } from "http";
import { Server as SecureServer } from "https";
import { setTimeout } from "timers/promises";

import type { Server as SocketIoServer } from "socket.io";
import type { Socket as SocketIoSocket } from "socket.io";
Expand Down Expand Up @@ -788,8 +789,14 @@ export class PluginLedgerConnectorBesu
const startedAt = new Date();

do {
const now = Date.now();
const elapsedTime = now - startedAt.getTime();
timedOut = now >= startedAt.getTime() + timeoutMs;
this.log.debug("%s tries=%n elapsedMs=%n", fnTag, tries, elapsedTime);
if (tries > 0) {
await setTimeout(1000);
}
tries++;
timedOut = Date.now() >= startedAt.getTime() + timeoutMs;
if (timedOut) {
break;
}
Expand Down

0 comments on commit c4eb4db

Please sign in to comment.