Skip to content

Commit

Permalink
Ignore inconsistent RPC response on initial send (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Tyurin authored Jan 17, 2023
1 parent 883ad79 commit 359914f
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 145 deletions.
3 changes: 0 additions & 3 deletions zp-relayer/utils/redisFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import { redis } from '@/services/redisClient'
import { web3 } from '@/services/web3'
import config from '@/config'
import { getNonce } from './web3'
import { pool } from '@/pool'

export enum RelayerKeys {
TRANSFER_NUM = 'relayer:transferNum',
NONCE = `relayer:nonce`,
}

export const readNonce = readFieldBuilder(RelayerKeys.NONCE, () => getNonce(web3, config.relayerAddress))
export const readTransferNum = readFieldBuilder(RelayerKeys.TRANSFER_NUM, () => pool.getContractIndex())

function readFieldBuilder(key: RelayerKeys, forceUpdateFunc?: Function) {
return async (forceUpdate?: boolean) => {
Expand Down
15 changes: 6 additions & 9 deletions zp-relayer/workers/poolTxWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Job, Worker } from 'bullmq'
import { web3, web3Redundant } from '@/services/web3'
import { logger } from '@/services/appLogger'
import { poolTxQueue, BatchTx, PoolTxResult, TxPayload } from '@/queue/poolTxQueue'
import { TX_QUEUE_NAME, OUTPLUSONE } from '@/utils/constants'
import { readNonce, updateField, RelayerKeys, updateNonce } from '@/utils/redisFields'
import { TX_QUEUE_NAME } from '@/utils/constants'
import { readNonce, updateNonce } from '@/utils/redisFields'
import { buildPrefixedMemo, truncateMemoTxPrefix, waitForFunds, withErrorLog, withMutex } from '@/utils/helpers'
import { signTransaction, sendTransaction } from '@/tx/signAndSend'
import { Pool, pool } from '@/pool'
Expand Down Expand Up @@ -77,11 +77,11 @@ export async function createPoolTxWorker<T extends EstimationType>(
},
config.relayerPrivateKey
)
jobLogger.info('Sending tx', { txHash })
try {
await sendTransaction(web3Redundant, rawTransaction)
} catch (e) {
const err = e as Error
if (isInsufficientBalanceError(err)) {
if (isInsufficientBalanceError(e as Error)) {
const minimumBalance = toBN(gas).mul(toBN(getMaxRequiredGasPrice(gasPriceWithExtra)))
jobLogger.error('Insufficient balance, waiting for funds', { minimumBalance: minimumBalance.toString(10) })
await Promise.all([poolTxQueue.pause(), sentTxQueue.pause()])
Expand All @@ -92,16 +92,13 @@ export async function createPoolTxWorker<T extends EstimationType>(
minimumBalance,
config.insufficientBalanceCheckTimeout
)
throw e
}
throw e
jobLogger.error('Tx send failed; it will be re-sent later', { txHash, error: (e as Error).message })
}

await updateNonce(++nonce)

jobLogger.info('Sent tx', { txHash })

await updateField(RelayerKeys.TRANSFER_NUM, commitIndex * OUTPLUSONE)

const nullifier = getTxProofField(txProof, 'nullifier')
const outCommit = getTxProofField(txProof, 'out_commit')

Expand Down
Loading

0 comments on commit 359914f

Please sign in to comment.