From 91c87360388fcbeebf9788df8c8d0b7aeb5ccd08 Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Thu, 16 May 2024 12:00:24 +0200 Subject: [PATCH 1/5] fix add `on-contract-event` --- lib/ie-contract.js | 2 +- lib/round-tracker.js | 25 ++++++++++++++++++++----- package-lock.json | 9 +++++++++ package.json | 1 + test/round-tracker.test.js | 5 +++-- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/lib/ie-contract.js b/lib/ie-contract.js index 93fe87c7..8be1f7c3 100644 --- a/lib/ie-contract.js +++ b/lib/ie-contract.js @@ -3,7 +3,7 @@ import { IE_CONTRACT_ABI, IE_CONTRACT_ADDRESS, RPC_URL, GLIF_TOKEN } from '../sp const fetchRequest = new ethers.FetchRequest(RPC_URL) fetchRequest.setHeader('Authorization', `Bearer ${GLIF_TOKEN}`) -const provider = new ethers.JsonRpcProvider(fetchRequest) +export const provider = new ethers.JsonRpcProvider(fetchRequest) // Uncomment for troubleshooting // provider.on('debug', d => console.log('[ethers:debug] %s\npayload: %o', d.action, d.payload)) diff --git a/lib/round-tracker.js b/lib/round-tracker.js index 6b5b9726..06224eef 100644 --- a/lib/round-tracker.js +++ b/lib/round-tracker.js @@ -1,6 +1,8 @@ import assert from 'node:assert' import * as Sentry from '@sentry/node' -import { createMeridianContract } from './ie-contract.js' +import { createMeridianContract, provider } from './ie-contract.js' +import { onContractEvent } from 'on-contract-event' +import { RPC_URL, GLIF_TOKEN } from '../spark-publish/ie-contract-config.js' // The number of tasks per round is proportionate to the SPARK round length - longer rounds require // more tasks per round. @@ -16,6 +18,7 @@ export const MAX_TASKS_PER_NODE = 15 /** * @param {import('pg').Pool} pgPool + * @param {AbortSignal} signal * @returns { * sparkRoundNumber: bigint; * meridianContractAddress: string; @@ -23,7 +26,7 @@ export const MAX_TASKS_PER_NODE = 15 * roundStartEpoch: bigint; * } */ -export async function startRoundTracker (pgPool) { +export async function startRoundTracker (pgPool, signal) { const contract = await createMeridianContract() const updateSparkRound = async (/** @type {bigint} */ newRoundIndex) => { @@ -63,12 +66,24 @@ export async function startRoundTracker (pgPool) { Sentry.captureException(err) }) } - contract.on('RoundStart', onRoundStart) - const close = () => contract.removeListener('RoundStart', onRoundStart) + const it = onContractEvent({ + contract, + provider, + rpcUrl: RPC_URL, + rpcHeaders: { + Authorization: `Bearer ${GLIF_TOKEN}` + }, + signal + }) + ;(async () => { + for await (const event of it) { + onRoundStart(event.args[0]) + } + })() try { const currentRound = await updateSparkRound(await contract.currentRoundIndex()) - return { ...currentRound, close } + return currentRound } catch (err) { close() throw err diff --git a/package-lock.json b/package-lock.json index 2b4d074d..1e16ba06 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "http-assert": "^1.5.0", "http-responders": "^2.0.2", "multiformats": "^13.1.0", + "on-contract-event": "^1.0.2", "pg": "^8.11.5", "postgrator": "^7.2.0", "raw-body": "^2.5.2" @@ -4077,6 +4078,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/on-contract-event": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/on-contract-event/-/on-contract-event-1.0.2.tgz", + "integrity": "sha512-Kuyqa82Jk4NYhs/KjUUM5ptOCCTg1fiPrkseeoDM5OKAGZE2XStYGu/EzVDVtFQ1ZqmXvHo4n/Gyrc2rzHAfXA==", + "dependencies": { + "ethers": "^6.10.0" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", diff --git a/package.json b/package.json index 99d24056..b8a2a21d 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "http-assert": "^1.5.0", "http-responders": "^2.0.2", "multiformats": "^13.1.0", + "on-contract-event": "^1.0.2", "pg": "^8.11.5", "postgrator": "^7.2.0", "raw-body": "^2.5.2" diff --git a/test/round-tracker.test.js b/test/round-tracker.test.js index 7838070d..d3fb4808 100644 --- a/test/round-tracker.test.js +++ b/test/round-tracker.test.js @@ -221,8 +221,9 @@ describe('Round Tracker', () => { describe('startRoundTracker', () => { it('detects the current round', async function () { this.timeout(TIMEOUT_WHEN_QUERYING_CHAIN) - const { sparkRoundNumber, close } = await startRoundTracker(pgPool) - close() + const controller = new AbortController() + const { sparkRoundNumber } = await startRoundTracker(pgPool, controller.signal) + signal.abort() assert.strictEqual(typeof sparkRoundNumber, 'bigint') }) }) From 6bc64ebe04dc971dcc7ab08f9152d76a495ebed3 Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Thu, 16 May 2024 12:01:13 +0200 Subject: [PATCH 2/5] catch abort errors --- lib/round-tracker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/round-tracker.js b/lib/round-tracker.js index 06224eef..dabd3311 100644 --- a/lib/round-tracker.js +++ b/lib/round-tracker.js @@ -79,7 +79,7 @@ export async function startRoundTracker (pgPool, signal) { for await (const event of it) { onRoundStart(event.args[0]) } - })() + })().catch(console.error) try { const currentRound = await updateSparkRound(await contract.currentRoundIndex()) From d4d341a1ae5b2f6ce83dff94d1e5247e85b17f83 Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Thu, 16 May 2024 12:12:56 +0200 Subject: [PATCH 3/5] fix issues discovered in linting --- lib/round-tracker.js | 8 ++++++-- test/round-tracker.test.js | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/round-tracker.js b/lib/round-tracker.js index dabd3311..5feab7e8 100644 --- a/lib/round-tracker.js +++ b/lib/round-tracker.js @@ -28,6 +28,7 @@ export const MAX_TASKS_PER_NODE = 15 */ export async function startRoundTracker (pgPool, signal) { const contract = await createMeridianContract() + const controller = new AbortController() const updateSparkRound = async (/** @type {bigint} */ newRoundIndex) => { const meridianRoundIndex = BigInt(newRoundIndex) @@ -73,7 +74,10 @@ export async function startRoundTracker (pgPool, signal) { rpcHeaders: { Authorization: `Bearer ${GLIF_TOKEN}` }, - signal + signal: AbortSignal.any([ + signal, + controller.signal + ]) }) ;(async () => { for await (const event of it) { @@ -85,7 +89,7 @@ export async function startRoundTracker (pgPool, signal) { const currentRound = await updateSparkRound(await contract.currentRoundIndex()) return currentRound } catch (err) { - close() + controller.abort() throw err } } diff --git a/test/round-tracker.test.js b/test/round-tracker.test.js index d3fb4808..fc50bbb4 100644 --- a/test/round-tracker.test.js +++ b/test/round-tracker.test.js @@ -223,7 +223,7 @@ describe('Round Tracker', () => { this.timeout(TIMEOUT_WHEN_QUERYING_CHAIN) const controller = new AbortController() const { sparkRoundNumber } = await startRoundTracker(pgPool, controller.signal) - signal.abort() + controller.signal.abort() assert.strictEqual(typeof sparkRoundNumber, 'bigint') }) }) From 8802a353141d5a60d9afe77042e3c218225dbad8 Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Thu, 16 May 2024 13:32:08 +0200 Subject: [PATCH 4/5] update `on-contract-event --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1e16ba06..bcb3bf85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,7 @@ "http-assert": "^1.5.0", "http-responders": "^2.0.2", "multiformats": "^13.1.0", - "on-contract-event": "^1.0.2", + "on-contract-event": "^1.1.0", "pg": "^8.11.5", "postgrator": "^7.2.0", "raw-body": "^2.5.2" @@ -4079,9 +4079,9 @@ } }, "node_modules/on-contract-event": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/on-contract-event/-/on-contract-event-1.0.2.tgz", - "integrity": "sha512-Kuyqa82Jk4NYhs/KjUUM5ptOCCTg1fiPrkseeoDM5OKAGZE2XStYGu/EzVDVtFQ1ZqmXvHo4n/Gyrc2rzHAfXA==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/on-contract-event/-/on-contract-event-1.1.0.tgz", + "integrity": "sha512-03oqQiOoeffiDkL6yL/HABgFwM1pPRlgMzQsAti/Y5xBaSpcdKwcNl3dJtCrEjfNIEr+1z3wNlLAgRges94yUg==", "dependencies": { "ethers": "^6.10.0" } diff --git a/package.json b/package.json index b8a2a21d..006711a4 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "http-assert": "^1.5.0", "http-responders": "^2.0.2", "multiformats": "^13.1.0", - "on-contract-event": "^1.0.2", + "on-contract-event": "^1.1.0", "pg": "^8.11.5", "postgrator": "^7.2.0", "raw-body": "^2.5.2" From 1860de991f8764185043cc3c5a95a6ee9db7438f Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Thu, 16 May 2024 13:41:21 +0200 Subject: [PATCH 5/5] clean up --- lib/round-tracker.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/round-tracker.js b/lib/round-tracker.js index 3f681194..707d741a 100644 --- a/lib/round-tracker.js +++ b/lib/round-tracker.js @@ -29,7 +29,6 @@ export const MAX_TASKS_PER_NODE = 15 */ export async function startRoundTracker ({ pgPool, signal }) { const contract = await createMeridianContract() - const controller = new AbortController() const updateSparkRound = async (/** @type {bigint} */ newRoundIndex) => { const meridianRoundIndex = BigInt(newRoundIndex)