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

feat: use fallback predicate estimation in set_max_fee_policy #1435

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion packages/fuels-accounts/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ impl Provider {

pub async fn estimate_transaction_cost<T: Transaction>(
&self,
tx: T,
mut tx: T,
tolerance: Option<f64>,
block_horizon: Option<u32>,
) -> Result<TransactionCost> {
Expand All @@ -614,6 +614,10 @@ impl Provider {
.get_gas_used_with_tolerance(tx.clone(), tolerance)
.await?;

if tx.is_using_predicates() {
tx.estimate_predicates(self, None).await?;
}

let transaction_fee = tx
.clone()
.fee_checked_from_tx(&self.consensus_parameters, gas_price)
Expand Down
48 changes: 34 additions & 14 deletions packages/fuels-core/src/types/transaction_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use fuel_tx::{
};
pub use fuel_tx::{UpgradePurpose, UploadSubsection};
use fuel_types::{bytes::padded_len_usize, Bytes32, Salt};
use fuel_vm::{checked_transaction::EstimatePredicates, interpreter::MemoryInstance};
use itertools::Itertools;
use script_dry_runner::ScriptDryRunner;

Expand Down Expand Up @@ -325,23 +324,25 @@ macro_rules! impl_tx_trait {
Ok(padded_len as u64)
}

async fn set_max_fee_policy<T: PoliciesField + Chargeable + EstimatePredicates>(
async fn set_max_fee_policy<T: Clone + PoliciesField + Chargeable + Into<$tx_ty>>(
tx: &mut T,
provider: impl DryRunner,
block_horizon: u32,
is_using_predicates: bool,
) -> Result<()> {
let mut wrapper_tx: $tx_ty = tx.clone().into();

if is_using_predicates {
wrapper_tx.estimate_predicates(&provider, None).await?;
}

let gas_price = provider.estimate_gas_price(block_horizon).await?;
let consensus_parameters = provider.consensus_parameters();

tx.estimate_predicates(
&provider.consensus_parameters().into(),
MemoryInstance::new(),
)?;

let tx_fee = TransactionFee::checked_from_tx(
&consensus_parameters.gas_costs(),
consensus_parameters.fee_params(),
tx,
&wrapper_tx.tx,
gas_price,
)
.ok_or(error_transaction!(
Expand Down Expand Up @@ -521,6 +522,7 @@ impl ScriptTransactionBuilder {
&mut tx,
&dry_runner,
self.gas_price_estimation_block_horizon,
self.is_using_predicates(),
)
.await?;
}
Expand Down Expand Up @@ -751,6 +753,7 @@ impl CreateTransactionBuilder {
let chain_id = provider.consensus_parameters().chain_id();
let num_witnesses = self.num_witnesses()?;
let policies = self.generate_fuel_policies()?;
let is_using_predicates = self.is_using_predicates();

let mut tx = FuelTransaction::create(
self.bytecode_witness_index,
Expand All @@ -765,8 +768,13 @@ impl CreateTransactionBuilder {
if let Some(max_fee) = self.tx_policies.max_fee() {
tx.policies_mut().set(PolicyType::MaxFee, Some(max_fee));
} else {
Self::set_max_fee_policy(&mut tx, &provider, self.gas_price_estimation_block_horizon)
.await?;
Self::set_max_fee_policy(
&mut tx,
&provider,
self.gas_price_estimation_block_horizon,
is_using_predicates,
)
.await?;
}

let missing_witnesses =
Expand Down Expand Up @@ -849,6 +857,7 @@ impl UploadTransactionBuilder {
let chain_id = provider.consensus_parameters().chain_id();
let num_witnesses = self.num_witnesses()?;
let policies = self.generate_fuel_policies()?;
let is_using_predicates = self.is_using_predicates();

let mut tx = FuelTransaction::upload(
UploadBody {
Expand All @@ -867,8 +876,13 @@ impl UploadTransactionBuilder {
if let Some(max_fee) = self.tx_policies.max_fee() {
tx.policies_mut().set(PolicyType::MaxFee, Some(max_fee));
} else {
Self::set_max_fee_policy(&mut tx, &provider, self.gas_price_estimation_block_horizon)
.await?;
Self::set_max_fee_policy(
&mut tx,
&provider,
self.gas_price_estimation_block_horizon,
is_using_predicates,
)
.await?;
}

let missing_witnesses =
Expand Down Expand Up @@ -959,6 +973,7 @@ impl UpgradeTransactionBuilder {
let chain_id = provider.consensus_parameters().chain_id();
let num_witnesses = self.num_witnesses()?;
let policies = self.generate_fuel_policies()?;
let is_using_predicates = self.is_using_predicates();

let mut tx = FuelTransaction::upgrade(
self.purpose,
Expand All @@ -971,8 +986,13 @@ impl UpgradeTransactionBuilder {
if let Some(max_fee) = self.tx_policies.max_fee() {
tx.policies_mut().set(PolicyType::MaxFee, Some(max_fee));
} else {
Self::set_max_fee_policy(&mut tx, &provider, self.gas_price_estimation_block_horizon)
.await?;
Self::set_max_fee_policy(
&mut tx,
&provider,
self.gas_price_estimation_block_horizon,
is_using_predicates,
)
.await?;
}

let missing_witnesses =
Expand Down
Loading