Skip to content

Commit

Permalink
feat: use fallback predicate estimation in set_max_fee_policy (#1435)
Browse files Browse the repository at this point in the history
  • Loading branch information
hal3e authored Jun 18, 2024
1 parent 184a0c9 commit a893bb7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
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
53 changes: 36 additions & 17 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,33 +324,34 @@ 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!(
Other,
"error calculating `TransactionFee` in `TransactionBuilder`"
))?;

let max_fee = tx_fee.max_fee();
let new_max_fee = max_fee + max_fee / 10; // 10% buffer
tx.policies_mut().set(PolicyType::MaxFee, Some(new_max_fee));
tx.policies_mut()
.set(PolicyType::MaxFee, Some(tx_fee.max_fee()));

Ok(())
}
Expand Down Expand Up @@ -521,6 +521,7 @@ impl ScriptTransactionBuilder {
&mut tx,
&dry_runner,
self.gas_price_estimation_block_horizon,
self.is_using_predicates(),
)
.await?;
}
Expand Down Expand Up @@ -751,6 +752,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 +767,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 +856,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 +875,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 +972,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 +985,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

0 comments on commit a893bb7

Please sign in to comment.