From c4eb4dbf814ed3ecb06eb91123763e6ca7b71ed7 Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Mon, 1 Apr 2024 20:43:50 -0700 Subject: [PATCH] refactor(connector-besu): tx poll per second not at full CPU speed 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 --- .../src/main/typescript/plugin-ledger-connector-besu.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts index a562b15a44..34ee9db244 100644 --- a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts @@ -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"; @@ -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; }