Compare commits

...

1 Commits

Author SHA1 Message Date
Omar Abdulla 61ec3de56a Update the FallbackGasFiller implementation 2026-01-12 03:44:43 +03:00
6 changed files with 166 additions and 158 deletions
+14 -65
View File
@@ -4,7 +4,6 @@ mod helpers;
use anyhow::{Context as _, bail}; use anyhow::{Context as _, bail};
use clap::Parser; use clap::Parser;
use revive_dt_common::types::ParsedTestSpecifier;
use revive_dt_report::{ReportAggregator, TestCaseStatus}; use revive_dt_report::{ReportAggregator, TestCaseStatus};
use schemars::schema_for; use schemars::schema_for;
use tracing::{info, level_filters::LevelFilter}; use tracing::{info, level_filters::LevelFilter};
@@ -64,40 +63,15 @@ fn main() -> anyhow::Result<()> {
) )
.await?; .await?;
// Error out if there are any failing tests. let contains_failure = report
let failures = report
.execution_information .execution_information
.into_iter() .values()
.flat_map(|(metadata_file_path, metadata_file_report)| { .flat_map(|values| values.case_reports.values())
metadata_file_report.case_reports.into_iter().flat_map( .flat_map(|values| values.mode_execution_reports.values())
move |(case_idx, case_report)| { .any(|report| matches!(report.status, Some(TestCaseStatus::Failed { .. })));
let metadata_file_path = metadata_file_path.clone();
case_report.mode_execution_reports.into_iter().filter_map(
move |(mode, execution_report)| {
if let Some(TestCaseStatus::Failed { reason }) =
execution_report.status
{
let parsed_test_specifier =
ParsedTestSpecifier::CaseWithMode {
metadata_file_path: metadata_file_path
.clone()
.into_inner(),
case_idx: case_idx.into_inner(),
mode,
};
Some((parsed_test_specifier, reason))
} else {
None
}
},
)
},
)
})
.collect::<Vec<_>>();
if !failures.is_empty() { if contains_failure {
bail!("Some tests failed: {failures:#?}") bail!("Some tests failed")
} }
Ok(()) Ok(())
@@ -117,40 +91,15 @@ fn main() -> anyhow::Result<()> {
) )
.await?; .await?;
// Error out if there are any failing tests. let contains_failure = report
let failures = report
.execution_information .execution_information
.into_iter() .values()
.flat_map(|(metadata_file_path, metadata_file_report)| { .flat_map(|values| values.case_reports.values())
metadata_file_report.case_reports.into_iter().flat_map( .flat_map(|values| values.mode_execution_reports.values())
move |(case_idx, case_report)| { .any(|report| matches!(report.status, Some(TestCaseStatus::Failed { .. })));
let metadata_file_path = metadata_file_path.clone();
case_report.mode_execution_reports.into_iter().filter_map(
move |(mode, execution_report)| {
if let Some(TestCaseStatus::Failed { reason }) =
execution_report.status
{
let parsed_test_specifier =
ParsedTestSpecifier::CaseWithMode {
metadata_file_path: metadata_file_path
.clone()
.into_inner(),
case_idx: case_idx.into_inner(),
mode,
};
Some((parsed_test_specifier, reason))
} else {
None
}
},
)
},
)
})
.collect::<Vec<_>>();
if !failures.is_empty() { if contains_failure {
bail!("Some tests failed: {failures:#?}") bail!("Some benchmarks failed")
} }
Ok(()) Ok(())
+1 -1
View File
@@ -250,7 +250,7 @@ impl GethNode {
construct_concurrency_limited_provider::<Ethereum, _>( construct_concurrency_limited_provider::<Ethereum, _>(
self.connection_string.as_str(), self.connection_string.as_str(),
FallbackGasFiller::default() FallbackGasFiller::default()
.with_use_fallback_gas_filler(self.use_fallback_gas_filler), .with_fallback_mechanism(self.use_fallback_gas_filler),
ChainIdFiller::new(Some(CHAIN_ID)), ChainIdFiller::new(Some(CHAIN_ID)),
NonceFiller::new(self.nonce_manager.clone()), NonceFiller::new(self.nonce_manager.clone()),
self.wallet.clone(), self.wallet.clone(),
@@ -379,7 +379,7 @@ impl LighthouseGethNode {
construct_concurrency_limited_provider::<Ethereum, _>( construct_concurrency_limited_provider::<Ethereum, _>(
self.ws_connection_string.as_str(), self.ws_connection_string.as_str(),
FallbackGasFiller::default() FallbackGasFiller::default()
.with_use_fallback_gas_filler(self.use_fallback_gas_filler), .with_fallback_mechanism(self.use_fallback_gas_filler),
ChainIdFiller::new(Some(CHAIN_ID)), ChainIdFiller::new(Some(CHAIN_ID)),
NonceFiller::new(self.nonce_manager.clone()), NonceFiller::new(self.nonce_manager.clone()),
self.wallet.clone(), self.wallet.clone(),
@@ -47,7 +47,7 @@ use tracing::{instrument, trace};
use crate::{ use crate::{
Node, Node,
constants::{CHAIN_ID, INITIAL_BALANCE}, constants::INITIAL_BALANCE,
helpers::{Process, ProcessReadinessWaitBehavior}, helpers::{Process, ProcessReadinessWaitBehavior},
provider_utils::{ provider_utils::{
ConcreteProvider, FallbackGasFiller, construct_concurrency_limited_provider, ConcreteProvider, FallbackGasFiller, construct_concurrency_limited_provider,
@@ -327,13 +327,9 @@ impl SubstrateNode {
.get_or_try_init(|| async move { .get_or_try_init(|| async move {
construct_concurrency_limited_provider::<Ethereum, _>( construct_concurrency_limited_provider::<Ethereum, _>(
self.rpc_url.as_str(), self.rpc_url.as_str(),
FallbackGasFiller::new( FallbackGasFiller::default()
u64::MAX, .with_fallback_mechanism(self.use_fallback_gas_filler),
50_000_000_000, ChainIdFiller::default(),
1_000_000_000,
self.use_fallback_gas_filler,
),
ChainIdFiller::new(Some(CHAIN_ID)),
NonceFiller::new(self.nonce_manager.clone()), NonceFiller::new(self.nonce_manager.clone()),
self.wallet.clone(), self.wallet.clone(),
) )
@@ -334,12 +334,8 @@ impl ZombienetNode {
.get_or_try_init(|| async move { .get_or_try_init(|| async move {
construct_concurrency_limited_provider::<Ethereum, _>( construct_concurrency_limited_provider::<Ethereum, _>(
self.connection_string.as_str(), self.connection_string.as_str(),
FallbackGasFiller::new( FallbackGasFiller::default()
u64::MAX, .with_fallback_mechanism(self.use_fallback_gas_filler),
5_000_000_000,
1_000_000_000,
self.use_fallback_gas_filler,
),
ChainIdFiller::default(), // TODO: use CHAIN_ID constant ChainIdFiller::default(), // TODO: use CHAIN_ID constant
NonceFiller::new(self.nonce_manager.clone()), NonceFiller::new(self.nonce_manager.clone()),
self.wallet.clone(), self.wallet.clone(),
@@ -1,65 +1,71 @@
use std::{borrow::Cow, fmt::Display};
use alloy::{ use alloy::{
eips::BlockNumberOrTag,
network::{Network, TransactionBuilder}, network::{Network, TransactionBuilder},
providers::{ providers::{
Provider, SendableTx, Provider, SendableTx,
fillers::{GasFiller, TxFiller}, ext::DebugApi,
fillers::{GasFillable, GasFiller, TxFiller},
}, },
transports::{TransportError, TransportResult}, rpc::types::trace::geth::{
GethDebugBuiltInTracerType, GethDebugTracerType, GethDebugTracingCallOptions,
GethDebugTracingOptions,
},
transports::{RpcError, TransportResult},
}; };
// Percentage padding applied to estimated gas (e.g. 120 = 20% padding) /// An implementation of [`GasFiller`] with a fallback mechanism for reverting transactions.
const GAS_ESTIMATE_PADDING_NUMERATOR: u64 = 120; ///
const GAS_ESTIMATE_PADDING_DENOMINATOR: u64 = 100; /// This struct provides a fallback mechanism for alloy's [`GasFiller`] which kicks in when a
/// transaction's dry run fails due to it reverting allowing us to get gas estimates even for
#[derive(Clone, Debug)] /// failing transactions. In this codebase, this is very important since the MatterLabs tests
/// expect some transactions in the test suite revert. Since we're expected to run a number of
/// assertions on these reverting transactions we must commit them to the ledger.
///
/// Therefore, this struct does the following:
///
/// 1. It first attempts to estimate the gas through the mechanism implemented in the [`GasFiller`].
/// 2. If it fails, then we perform a debug trace of the transaction to find out how much gas the
/// transaction needs until it reverts.
/// 3. We fill in these values (either the success or failure case) into the transaction.
///
/// The fallback mechanism of this filler can be completely disabled if we don't want it to be used.
/// In that case, this gas filler will act in an identical way to alloy's [`GasFiller`].
///
/// We then fill in these values into the transaction.
///
/// The previous implementation of this fallback gas filler relied on making use of default values
/// for the gas limit in order to be able to submit the reverting transactions to the network. But,
/// it introduced a number of issues that we weren't anticipating at the time when it was built.
#[derive(Clone, Copy, Debug)]
pub struct FallbackGasFiller { pub struct FallbackGasFiller {
/// The inner [`GasFiller`] which we pass all of the calls to in the happy path.
inner: GasFiller, inner: GasFiller,
default_gas_limit: u64,
default_max_fee_per_gas: u128, /// A [`bool`] that controls if the fallback mechanism is enabled or not.
default_priority_fee: u128, enable_fallback_mechanism: bool,
use_fallback_gas_filler: bool,
} }
impl FallbackGasFiller { impl FallbackGasFiller {
pub fn new( pub fn new() -> Self {
default_gas_limit: u64,
default_max_fee_per_gas: u128,
default_priority_fee: u128,
use_fallback_gas_filler: bool,
) -> Self {
Self { Self {
inner: GasFiller, inner: Default::default(),
default_gas_limit, enable_fallback_mechanism: true,
default_max_fee_per_gas,
default_priority_fee,
use_fallback_gas_filler,
} }
} }
pub fn with_default_gas_limit(mut self, default_gas_limit: u64) -> Self { pub fn with_fallback_mechanism(mut self, enable: bool) -> Self {
self.default_gas_limit = default_gas_limit; self.enable_fallback_mechanism = enable;
self self
} }
pub fn with_default_max_fee_per_gas(mut self, default_max_fee_per_gas: u128) -> Self { pub fn with_fallback_mechanism_enabled(self) -> Self {
self.default_max_fee_per_gas = default_max_fee_per_gas; self.with_fallback_mechanism(true)
self
} }
pub fn with_default_priority_fee(mut self, default_priority_fee: u128) -> Self { pub fn with_fallback_mechanism_disabled(self) -> Self {
self.default_priority_fee = default_priority_fee; self.with_fallback_mechanism(false)
self
}
pub fn with_use_fallback_gas_filler(mut self, use_fallback_gas_filler: bool) -> Self {
self.use_fallback_gas_filler = use_fallback_gas_filler;
self
}
}
impl Default for FallbackGasFiller {
fn default() -> Self {
FallbackGasFiller::new(25_000_000, 1_000_000_000, 1_000_000_000, true)
} }
} }
@@ -67,32 +73,89 @@ impl<N> TxFiller<N> for FallbackGasFiller
where where
N: Network, N: Network,
{ {
type Fillable = Option<<GasFiller as TxFiller<N>>::Fillable>; type Fillable = <GasFiller as TxFiller<N>>::Fillable;
fn status( fn status(
&self, &self,
tx: &<N as Network>::TransactionRequest, tx: &<N as Network>::TransactionRequest,
) -> alloy::providers::fillers::FillerControlFlow { ) -> alloy::providers::fillers::FillerControlFlow {
<GasFiller as TxFiller<N>>::status(&self.inner, tx) TxFiller::<N>::status(&self.inner, tx)
} }
fn fill_sync(&self, _: &mut alloy::providers::SendableTx<N>) {} fn fill_sync(&self, _: &mut SendableTx<N>) {}
async fn prepare<P: Provider<N>>( async fn prepare<P: Provider<N>>(
&self, &self,
provider: &P, provider: &P,
tx: &<N as Network>::TransactionRequest, tx: &<N as Network>::TransactionRequest,
) -> TransportResult<Self::Fillable> { ) -> TransportResult<Self::Fillable> {
match self.inner.prepare(provider, tx).await { match (
Ok(fill) => Ok(Some(fill)), self.inner.prepare(provider, tx).await,
Err(err) => { self.enable_fallback_mechanism,
tracing::debug!(error = ?err, "Gas Provider Estimation Failed, using fallback"); ) {
// Return the same thing if either this calls succeeds, or if the call falls and the
// fallback mechanism is disabled.
(rtn @ Ok(..), ..) | (rtn @ Err(..), false) => rtn,
(Err(..), true) => {
// Perform a trace of the transaction.
let trace = provider
.debug_trace_call(
tx.clone(),
BlockNumberOrTag::Latest.into(),
GethDebugTracingCallOptions {
tracing_options: GethDebugTracingOptions {
tracer: Some(GethDebugTracerType::BuiltInTracer(
GethDebugBuiltInTracerType::CallTracer,
)),
..Default::default()
},
state_overrides: Default::default(),
block_overrides: Default::default(),
},
)
.await?
.try_into_call_frame()
.map_err(|err| {
RpcError::LocalUsageError(
FallbackGasFillerError::new(format!(
"Expected a callframe trace, but got: {err:?}"
))
.boxed(),
)
})?;
if !self.use_fallback_gas_filler { let gas_used = u64::try_from(trace.gas_used).map_err(|_| {
Err(err) RpcError::LocalUsageError(
} else { FallbackGasFillerError::new(
Ok(None) "Transaction trace returned a value of gas used that exceeds u64",
)
.boxed(),
)
})?;
let gas_limit = gas_used.saturating_mul(120) / 100;
if let Some(gas_price) = tx.gas_price() {
return Ok(GasFillable::Legacy {
gas_limit,
gas_price,
});
} }
let estimate = if let (Some(max_fee_per_gas), Some(max_priority_fee_per_gas)) =
(tx.max_fee_per_gas(), tx.max_priority_fee_per_gas())
{
alloy::eips::eip1559::Eip1559Estimation {
max_fee_per_gas,
max_priority_fee_per_gas,
}
} else {
provider.estimate_eip1559_fees().await?
};
Ok(GasFillable::Eip1559 {
gas_limit,
estimate,
})
} }
} }
} }
@@ -100,31 +163,35 @@ where
async fn fill( async fn fill(
&self, &self,
fillable: Self::Fillable, fillable: Self::Fillable,
mut tx: alloy::providers::SendableTx<N>, tx: SendableTx<N>,
) -> TransportResult<SendableTx<N>> { ) -> TransportResult<SendableTx<N>> {
if let Some(fill) = fillable { self.inner.fill(fillable, tx).await
let mut tx = self.inner.fill(fill, tx).await?;
if let Some(builder) = tx.as_mut_builder() {
if let Some(estimated) = builder.gas_limit() {
let padded = estimated
.checked_mul(GAS_ESTIMATE_PADDING_NUMERATOR)
.and_then(|v| v.checked_div(GAS_ESTIMATE_PADDING_DENOMINATOR))
.unwrap_or(u64::MAX);
builder.set_gas_limit(padded);
} }
} }
Ok(tx)
} else if self.use_fallback_gas_filler { impl Default for FallbackGasFiller {
if let Some(builder) = tx.as_mut_builder() { fn default() -> Self {
builder.set_gas_limit(self.default_gas_limit); Self::new()
builder.set_max_fee_per_gas(self.default_max_fee_per_gas);
builder.set_max_priority_fee_per_gas(self.default_priority_fee);
}
Ok(tx)
} else {
Err(TransportError::UnsupportedFeature(
"Fallback gas filler is disabled and we're attempting to do a gas estimate on a failing transaction",
))
} }
} }
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct FallbackGasFillerError(Cow<'static, str>);
impl FallbackGasFillerError {
pub fn new(string: impl Into<Cow<'static, str>>) -> Self {
Self(string.into())
} }
pub fn boxed(self) -> Box<Self> {
Box::new(self)
}
}
impl Display for FallbackGasFillerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
impl std::error::Error for FallbackGasFillerError {}