Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cancel-stuck-transactions #363

Merged
merged 10 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/dry-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ const { ignoredErrors } = await evaluate({
ieContractWithSigner,
logger: console,
recordTelemetry,
createPgClient
createPgClient,
stuckTransactionsCanceller: { addPending: () => {} }
})

console.log('Duration: %sms', Date.now() - started)
Expand Down
3 changes: 2 additions & 1 deletion bin/fetch-recent-miner-measurements.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ async function processRound (roundIndex, measurementCids, resultCounts) {
fetchRoundDetails,
recordTelemetry,
logger: { log: debug, error: debug },
ieContractWithSigner
ieContractWithSigner,
stuckTransactionsCanceller: { addPending: () => {} }
})

for (const m of round.measurements) {
Expand Down
19 changes: 11 additions & 8 deletions bin/spark-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { fetchMeasurements } from '../lib/preprocess.js'
import { migrateWithPgConfig } from '../lib/migrate.js'
import pg from 'pg'
import { createMeridianContract } from '../lib/ie-contract.js'
import { startCancelStuckTxs } from '../lib/cancel-stuck-txs.js'
import { createStuckTransactionsCanceller, startCancelStuckTransactions } from '../lib/cancel-stuck-transactions.js'

const {
SENTRY_ENVIRONMENT = 'development',
Expand Down Expand Up @@ -43,6 +43,13 @@ const createPgClient = async () => {
return pgClient
}

const pgClient = await createPgClient()
const stuckTransactionsCanceller = createStuckTransactionsCanceller({
pgClient,
// Bypass NonceManager as we need to cancel transactions with the same nonce
signer: wallet
})

await Promise.all([
startEvaluate({
ieContract,
Expand All @@ -51,12 +58,8 @@ await Promise.all([
fetchRoundDetails,
recordTelemetry,
createPgClient,
logger: console
logger: console,
stuckTransactionsCanceller
}),
startCancelStuckTxs({
walletDelegatedAddress,
address: wallet.address,
// Bypass NonceManager as we need to cancel transactions with the same nonce
signer: wallet
})
startCancelStuckTransactions(stuckTransactionsCanceller)
])
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const startEvaluate = async ({
fetchRoundDetails,
recordTelemetry,
createPgClient,
logger
logger,
stuckTransactionsCanceller
}) => {
assert(typeof createPgClient === 'function', 'createPgClient must be a function')

Expand Down Expand Up @@ -115,7 +116,8 @@ export const startEvaluate = async ({
fetchRoundDetails,
recordTelemetry,
createPgClient,
logger
logger,
stuckTransactionsCanceller
}).catch(err => {
console.error('CANNOT EVALUATE ROUND %s:', evaluatedRoundIndex, err)
Sentry.captureException(err, {
Expand Down
68 changes: 68 additions & 0 deletions lib/cancel-stuck-transactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as Sentry from '@sentry/node'
import { StuckTransactionsCanceller } from 'cancel-stuck-transactions'
import ms from 'ms'
import timers from 'node:timers/promises'

const ROUND_LENGTH_MS = ms('20 minutes')
const CHECK_STUCK_TXS_DELAY = ms('1 minute')

export const createStuckTransactionsCanceller = ({ pgClient, signer }) => {
return new StuckTransactionsCanceller({
store: {
async set ({ hash, timestamp, from, maxPriorityFeePerGas, nonce }) {
await pgClient.query(`
INSERT INTO transactions_pending (hash, timestamp, from_address, max_priority_fee_per_gas, nonce)
VALUES ($1, $2, $3, $4, $5)
`,
[hash, timestamp, from, maxPriorityFeePerGas, nonce]
)
},
async list () {
const { rows } = await pgClient.query(`
SELECT
hash,
timestamp,
from_address as "from",
max_priority_fee_per_gas as "maxPriorityFeePerGas",
nonce
FROM transactions_pending
`)
return rows
},
async remove (hash) {
await pgClient.query(
'DELETE FROM transactions_pending WHERE hash = $1',
[hash]
)
}
},
log (str) {
console.log(str)
},
sendTransaction (tx) {
return signer.sendTransaction(tx)
}
})
}

export const startCancelStuckTransactions = async stuckTransactionsCanceller => {
while (true) {
try {
const res = await stuckTransactionsCanceller.cancelOlderThan(
2 * ROUND_LENGTH_MS
)
if (res !== undefined) {
for (const { status, reason } of res) {
if (status === 'rejected') {
console.error('Failed to cancel transaction:', reason)
Sentry.captureException(reason)
}
}
}
} catch (err) {
console.error(err)
Sentry.captureException(err)
}
await timers.setTimeout(CHECK_STUCK_TXS_DELAY)
}
}
184 changes: 0 additions & 184 deletions lib/cancel-stuck-txs.js

This file was deleted.

9 changes: 8 additions & 1 deletion lib/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const createSetScoresBuckets = participants => {
* @param {import('./typings.js').RecordTelemetryFn} args.recordTelemetry
* @param {import('./typings.js').CreatePgClient} [args.createPgClient]
* @param {Pick<Console, 'log' | 'error'>} args.logger
* @param {import('cancel-stuck-transactions').StuckTransactionsCanceller} args.stuckTransactionsCanceller
*/
export const evaluate = async ({
round,
Expand All @@ -64,7 +65,8 @@ export const evaluate = async ({
fetchRoundDetails,
recordTelemetry,
createPgClient,
logger
logger,
stuckTransactionsCanceller
}) => {
requiredCommitteeSize ??= REQUIRED_COMMITTEE_SIZE

Expand Down Expand Up @@ -161,6 +163,11 @@ export const evaluate = async ({
Number(bucketIndex) + 1,
buckets.length
)
await stuckTransactionsCanceller.addPending(tx)
;(async () => {
await tx.wait()
stuckTransactionsCanceller.removeConfirmed(tx)
})().catch(console.error)
} catch (err) {
logger.error('CANNOT SUBMIT SCORES FOR ROUND %s (CALL %s/%s): %s',
roundIndex,
Expand Down
7 changes: 7 additions & 0 deletions migrations/017.do.cancel-stuck-transactions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE transactions_pending (
hash TEXT NOT NULL PRIMARY KEY,
timestamp TIMESTAMPTZ NOT NULL,
from_address TEXT NOT NULL,
max_priority_fee_per_gas BIGINT NOT NULL,
nonce INT NOT NULL
);
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading