Skip to content

Commit

Permalink
fix: try removing just min gas price calc
Browse files Browse the repository at this point in the history
  • Loading branch information
philbow61 committed Jan 28, 2025
1 parent 44a0a19 commit ea31fc7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 75 deletions.
28 changes: 2 additions & 26 deletions src/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ import {
} from '../utils'
import { sendTransaction, sendTransactionWithRetries } from './transaction_manager'

// Fallback gas amounts -- in the event gas estimation fails due to this race
// condition: https://github.com/celo-org/celo-blockchain/issues/1419
// We fall back to a hardcoded gas amount intended to be a little higher than
// normal to be extra safe:

// 400k -- gas estimations (including contractkit's inflation factor of 1.3)
// are typically ~330k and gas used is typically ~190k
const FALLBACK_REPORT_GAS = 400000
// 450k -- gas estimations (including contractkit's inflation factor of 1.3)
// are typically ~350k and gas used is typically ~200k
const FALLBACK_EXPIRY_GAS = 450000

export interface BaseReporterConfig {
/**
* A base instance of the logger that can be extended for a particular context
Expand Down Expand Up @@ -236,21 +224,15 @@ export abstract class BaseReporter {
sortedOracles.report(this.config.reportTarget, price.toFixed(), this.config.oracleAccount),
'report'
)
const gasPrice = await this.doAsyncReportAction(
() => this.calculateGasPrice(),
'calculateGasPrice'
)

const receipt = await sendTransactionWithRetries(
this.logger,
tx,
gasPrice,
{
...this.config,
logger: this.logger,
},
this.doAsyncReportAction.bind(this),
FALLBACK_REPORT_GAS
this.doAsyncReportAction.bind(this)
)

if (this.config.metricCollector) {
Expand Down Expand Up @@ -337,18 +319,12 @@ export abstract class BaseReporter {
() => sortedOracles.removeExpiredReports(this.config.reportTarget),
'removeExpiredReports'
)
const gasPrice = await this.doAsyncReportAction(
() => this.calculateGasPrice(),
'calculateGasPrice'
)

return sendTransaction(
this.logger,
tx,
gasPrice,
this.config.oracleAccount,
this.doAsyncExpiryAction.bind(this),
FALLBACK_EXPIRY_GAS
this.doAsyncExpiryAction.bind(this)
)
}
}
Expand Down
40 changes: 6 additions & 34 deletions src/reporters/transaction_manager/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,29 @@ import { TransactionReceipt } from 'web3-core'
* by gas estimation failing but the subsequent eth_call done by contractkit not
* indicating a revert, fallbackGas is used.
* @param tx the transaction to send
* @param gasPrice the gas price for the transaction
* @param from the from address for the transaction
* @param metricAction a function that wraps the sending of the tx, intended to record any metrics
* @param fallbackGas the fallback gas to use in the event gas estimation incorrectly fails
*/
export default async function send(
logger: Logger,
tx: CeloTransactionObject<void>,
gasPrice: number,
from: string,
metricAction: <T>(fn: () => Promise<T>, action: string) => Promise<T>,
fallbackGas: number
metricAction: <T>(fn: () => Promise<T>, action: string) => Promise<T>
) {
const txResult = await metricAction(async () => {
try {
// First, attempt to send transaction without a gas amount to have
// contractkit estimate gas
return await tx.send({
from,
gasPrice,
})
} catch (err: any) {
// If anything fails, the error is caught here.
// We seek the case where gas estimation has failed but the subsequent
// eth_call made by contractkit to get the revert reason has not given
// a revert reason. In this situation, the following string will be
// included in the error string: 'Gas estimation failed: Could not decode transaction failure reason'
if (
err.message.includes(
'Gas estimation failed: Could not decode transaction failure reason'
) &&
fallbackGas !== undefined
) {
logger.info(
{
tx,
gasPrice,
from,
fallbackGas,
err,
},
'Gas estimation failed but eth_call did not, using fallback gas'
)
// Retry with the fallbackGas to avoid gas estimation
return tx.send({
from,
gasPrice,
gas: fallbackGas,
})
}
// If there was a legitimate error, we still throw
logger.info({
tx,
from,
err,
})
throw err
}
}, 'send')
Expand Down
17 changes: 2 additions & 15 deletions src/reporters/transaction_manager/send_with_retries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,17 @@ import send from './send'
export default async function sendWithRetries(
logger: Logger,
tx: CeloTransactionObject<void>,
initialGasPrice: number,
config: TransactionManagerConfig,
metricAction: <T>(fn: () => Promise<T>, action: string) => Promise<T>,
fallbackGas: number
metricAction: <T>(fn: () => Promise<T>, action: string) => Promise<T>
): Promise<TransactionReceipt> {
let attempt = 0
let lastCaughtError = null

do {
const calculatedGasPrice = config.transactionRetryGasPriceMultiplier
.times(attempt)
.times(initialGasPrice)
.plus(initialGasPrice)
.toNumber()
try {
return await send(
logger,
tx,
calculatedGasPrice,
config.oracleAccount,
metricAction,
fallbackGas
metricAction
)
} catch (err: any) {
lastCaughtError = err
Expand All @@ -42,8 +31,6 @@ export default async function sendWithRetries(
swallowError: true,
})
}
attempt++
} while (attempt <= config.transactionRetryLimit)

throw lastCaughtError
}

0 comments on commit ea31fc7

Please sign in to comment.