Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
hal3e committed Feb 3, 2025
2 parents 173b5e9 + bf12588 commit 3a2ec67
Show file tree
Hide file tree
Showing 23 changed files with 191 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ env:
FUEL_CORE_PATCH_BRANCH: ""
FUEL_CORE_PATCH_REVISION: ""
RUST_VERSION: 1.81.0
FORC_VERSION: 0.66.5
FORC_VERSION: 0.66.6
FORC_PATCH_BRANCH: ""
FORC_PATCH_REVISION: ""
NEXTEST_HIDE_PROGRESS_BAR: "true"
Expand Down
2 changes: 1 addition & 1 deletion e2e/sway/contracts/lib_contract/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ license = "Apache-2.0"
name = "lib_contract"

[dependencies]
lib_contract = { path = "../lib_contract_abi/", package = "lib_contract_abi" }
lib_contract_abi = { path = "../lib_contract_abi/", package = "lib_contract_abi" }
2 changes: 1 addition & 1 deletion e2e/sway/contracts/lib_contract/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
contract;

use lib_contract::LibContract;
use lib_contract_abi::LibContract;

impl LibContract for Contract {
fn increment(value: u64) -> u64 {
Expand Down
2 changes: 1 addition & 1 deletion e2e/sway/logs/contract_logs/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ license = "Apache-2.0"
name = "contract_logs"

[dependencies]
contract_logs = { path = "../contract_logs_abi/", package = "contract_logs_abi" }
contract_logs_abi = { path = "../contract_logs_abi/", package = "contract_logs_abi" }
2 changes: 1 addition & 1 deletion e2e/sway/logs/contract_logs/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
contract;

use std::{logging::log, string::String};
use contract_logs::ContractLogs;
use contract_logs_abi::ContractLogs;

#[allow(dead_code)]
struct TestStruct {
Expand Down
10 changes: 1 addition & 9 deletions e2e/sway/predicates/swap/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
predicate;

use std::{
constants::ZERO_B256,
outputs::{
Output,
output_amount,
output_asset_id,
output_asset_to,
},
};
use std::{constants::ZERO_B256, outputs::{Output, output_amount, output_asset_id, output_asset_to}};

fn main() -> bool {
let receiver = Address::from(0x09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db);
Expand Down
7 changes: 4 additions & 3 deletions e2e/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use fuel_tx::{
use fuels::{
core::codec::{calldata, encode_fn_selector, DecoderConfig, EncoderConfig},
prelude::*,
programs::DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
tx::ContractParameters,
types::{errors::transaction::Reason, input::Input, Bits256, Identity},
};
Expand Down Expand Up @@ -291,7 +292,7 @@ async fn test_contract_call_fee_estimation() -> Result<()> {
let gas_limit = 800;
let tolerance = Some(0.2);
let block_horizon = Some(1);
let expected_gas_used = 8462;
let expected_gas_used = 8463;
let expected_metered_bytes_size = 824;

let estimated_transaction_cost = contract_instance
Expand Down Expand Up @@ -2114,8 +2115,8 @@ async fn max_fee_estimation_respects_tolerance() -> Result<()> {
.unwrap();

assert_eq!(
builder.max_fee_estimation_tolerance, 0.05,
"Expected pre-set tolerance of 0.05"
builder.max_fee_estimation_tolerance, DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
"Expected pre-set tolerance"
);

builder
Expand Down
4 changes: 2 additions & 2 deletions e2e/tests/debug_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ async fn can_debug_multi_call_tx() -> Result<()> {

assert_eq!(
decoder.decode_fn_args(&fn_selector, call_description.encoded_args.as_slice())?,
vec!["AllStruct { some_struct: SomeStruct { field: 2, field_2: true } }", "MemoryAddress { contract_id: std::contract_id::ContractId { bits: Bits256([77, 127, 224, 17, 182, 42, 211, 241, 46, 156, 74, 204, 31, 156, 188, 77, 183, 63, 55, 80, 119, 142, 192, 75, 130, 205, 208, 253, 25, 104, 22, 171]) }, function_selector: 123, function_data: 456 }"]
vec![ "AllStruct { some_struct: SomeStruct { field: 2, field_2: true } }".to_string(), format!("MemoryAddress {{ contract_id: std::contract_id::ContractId {{ bits: Bits256({:?}) }}, function_selector: 123, function_data: 456 }}", contract_id.as_slice())]
);
}

Expand Down Expand Up @@ -304,7 +304,7 @@ async fn can_debug_multi_call_tx() -> Result<()> {

assert_eq!(
decoder.decode_fn_args(&call_description.decode_fn_selector().unwrap(), call_description.encoded_args.as_slice())?,
vec!["AllStruct { some_struct: SomeStruct { field: 2, field_2: true } }", "MemoryAddress { contract_id: std::contract_id::ContractId { bits: Bits256([77, 127, 224, 17, 182, 42, 211, 241, 46, 156, 74, 204, 31, 156, 188, 77, 183, 63, 55, 80, 119, 142, 192, 75, 130, 205, 208, 253, 25, 104, 22, 171]) }, function_selector: 123, function_data: 456 }"]
vec![ "AllStruct { some_struct: SomeStruct { field: 2, field_2: true } }".to_string(), format!("MemoryAddress {{ contract_id: std::contract_id::ContractId {{ bits: Bits256({:?}) }}, function_selector: 123, function_data: 456 }}", contract_id.as_slice())]
);
}

Expand Down
4 changes: 2 additions & 2 deletions e2e/tests/predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,9 @@ async fn predicate_adjust_fee_persists_message_w_data() -> Result<()> {
let mut tb = ScriptTransactionBuilder::prepare_transfer(
vec![message_input.clone()],
vec![],
TxPolicies::default().with_tip(1),
TxPolicies::default(),
);
predicate.adjust_for_fee(&mut tb, 1000).await?;
predicate.adjust_for_fee(&mut tb, 0).await?;

let tx = tb.build(&provider).await?;

Expand Down
2 changes: 1 addition & 1 deletion e2e/tests/providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ async fn test_gas_forwarded_defaults_to_tx_limit() -> Result<()> {
);

// The gas used by the script to call a contract and forward remaining gas limit.
let gas_used_by_script = 254;
let gas_used_by_script = 247;
let gas_limit = 225_883;
let response = contract_instance
.methods()
Expand Down
69 changes: 68 additions & 1 deletion e2e/tests/scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use std::time::Duration;

use fuel_tx::Output;
use fuels::{
client::{PageDirection, PaginationRequest},
core::{
codec::{DecoderConfig, EncoderConfig},
traits::Tokenizable,
Configurables,
},
prelude::*,
programs::executable::Executable,
programs::{executable::Executable, DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE},
types::{Bits256, Identity},
};

Expand Down Expand Up @@ -489,6 +490,72 @@ async fn can_be_run_in_blobs_high_level() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn high_level_blob_upload_sets_max_fee_tolerance() -> Result<()> {
let node_config = NodeConfig {
starting_gas_price: 1000000000,
..Default::default()
};
let mut wallet = WalletUnlocked::new_random(None);
let coins = setup_single_asset_coins(wallet.address(), AssetId::zeroed(), 1, u64::MAX);
let provider = setup_test_provider(coins, vec![], Some(node_config), None).await?;
wallet.set_provider(provider.clone());

setup_program_test!(
Abigen(Script(
project = "e2e/sway/scripts/script_blobs",
name = "MyScript"
)),
LoadScript(name = "my_script", script = "MyScript", wallet = "wallet")
);

let loader = Executable::from_bytes(std::fs::read(
"sway/scripts/script_blobs/out/release/script_blobs.bin",
)?)
.convert_to_loader()?;

let zero_tolerance_fee = {
let mut tb = BlobTransactionBuilder::default()
.with_blob(loader.blob())
.with_max_fee_estimation_tolerance(0.);

wallet.adjust_for_fee(&mut tb, 0).await?;

wallet.add_witnesses(&mut tb)?;
let tx = tb.build(&provider).await?;
tx.max_fee().unwrap()
};

let mut my_script = my_script;
my_script.convert_into_loader().await?;

let max_fee_of_sent_blob_tx = provider
.get_transactions(PaginationRequest {
cursor: None,
results: 100,
direction: PageDirection::Forward,
})
.await?
.results
.into_iter()
.find_map(|tx| {
if let TransactionType::Blob(blob_transaction) = tx.transaction {
blob_transaction.max_fee()
} else {
None
}
})
.unwrap();

assert_eq!(
max_fee_of_sent_blob_tx,
(zero_tolerance_fee as f32 * (1.0 + DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE)).ceil() as u64,
"the blob upload tx should have had the max fee increased by the default estimation tolerance"
);

Ok(())
}

#[tokio::test]
async fn no_data_section_blob_run() -> Result<()> {
setup_program_test!(
Expand Down
71 changes: 70 additions & 1 deletion e2e/tests/wallets.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use fuels::{prelude::*, types::output::Output};
use fuels::{
prelude::*,
types::{coin_type::CoinType, input::Input, output::Output},
};

#[tokio::test]
async fn test_wallet_balance_api_multi_asset() -> Result<()> {
Expand Down Expand Up @@ -105,6 +108,72 @@ async fn adjust_fee_empty_transaction() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn adjust_for_fee_with_message_data_input() -> Result<()> {
let mut wallet = WalletUnlocked::new_random(None);
let mut receiver = WalletUnlocked::new_random(None);

let messages = setup_single_message(
&Bech32Address::default(),
wallet.address(),
100,
0.into(),
vec![1, 2, 3], // has data
);
let asset_id = AssetId::zeroed();
let coins = setup_single_asset_coins(wallet.address(), asset_id, 1, 50);
let provider = setup_test_provider(coins, vec![messages], None, None).await?;
wallet.set_provider(provider.clone());
receiver.set_provider(provider.clone());

let amount_to_send = 14;
let message = wallet.get_messages().await?.pop().unwrap();
let input = Input::resource_signed(CoinType::Message(message));
let outputs = wallet.get_asset_outputs_for_amount(receiver.address(), asset_id, amount_to_send);

{
// message with data as only input - without adjust for fee
let mut tb = ScriptTransactionBuilder::prepare_transfer(
vec![input.clone()],
outputs.clone(),
TxPolicies::default(),
);
tb.add_signer(wallet.clone())?;

let tx = tb.build(wallet.try_provider()?).await?;
let err = provider
.send_transaction_and_await_commit(tx)
.await
.unwrap_err();

assert!(err.to_string().contains("Validity(NoSpendableInput)"));
}
{
// message with data as only input - with adjust for fee
let mut tb = ScriptTransactionBuilder::prepare_transfer(
vec![input.clone()],
outputs.clone(),
TxPolicies::default(),
);

tb.add_signer(wallet.clone())?;
wallet.adjust_for_fee(&mut tb, 0).await.unwrap();

let tx = tb.build(wallet.try_provider()?).await?;

assert_eq!(receiver.get_asset_balance(&asset_id).await?, 0);

provider
.send_transaction_and_await_commit(tx)
.await
.unwrap();

assert_eq!(receiver.get_asset_balance(&asset_id).await?, amount_to_send);
}

Ok(())
}

#[tokio::test]
async fn adjust_fee_resources_to_transfer_with_base_asset() -> Result<()> {
let wallet = launch_provider_and_get_wallet().await?;
Expand Down
4 changes: 2 additions & 2 deletions examples/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ mod tests {
.await?;
// ANCHOR_END: contract_call_cost_estimation

let expected_gas = 8921;
let expected_gas = 9068;

assert_eq!(transaction_cost.gas_used, expected_gas);

Expand Down Expand Up @@ -634,7 +634,7 @@ mod tests {
.await?;
// ANCHOR_END: multi_call_cost_estimation

let expected_gas = 10997;
let expected_gas = 11231;

assert_eq!(transaction_cost.gas_used, expected_gas);

Expand Down
9 changes: 7 additions & 2 deletions packages/fuels-accounts/src/accounts_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ pub fn available_base_assets_and_amount(
resource.id()
}
CoinType::Message(message) => {
sum += message.amount;
resource.id()
if message.data.is_empty() {
sum += message.amount;

resource.id()
} else {
None
}
}
_ => None,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-accounts/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ impl Provider {
async fn gas_used_with_tolerance(&self, tx_status: &TxStatus, tolerance: f64) -> Result<u64> {
let gas_used = tx_status.total_gas();

Ok((gas_used as f64 * (1.0 + tolerance)) as u64)
Ok((gas_used as f64 * (1.0 + tolerance)).ceil() as u64)
}

// Increase estimated total fee by the provided tolerance
Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-core/src/types/dry_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl DryRun {
pub fn gas_with_tolerance(&self, tolerance: f32) -> u64 {
let gas_used = self.script_gas as f64;
let adjusted_gas = gas_used * (1.0 + f64::from(tolerance));
adjusted_gas as u64
adjusted_gas.ceil() as u64
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-core/src/types/transaction_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ pub(crate) fn estimate_max_fee_w_tolerance<T: Chargeable>(

let max_fee_w_tolerance = tx_fee.max_fee() as f64 * (1.0 + f64::from(tolerance));

Ok(max_fee_w_tolerance as u64)
Ok(max_fee_w_tolerance.ceil() as u64)
}

impl Debug for dyn Signer + Send + Sync {
Expand Down
13 changes: 8 additions & 5 deletions packages/fuels-programs/src/calls/traits/transaction_tuner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ use fuels_core::types::{
},
};

use crate::calls::{
utils::{build_tx_from_contract_calls, sealed, transaction_builder_from_contract_calls},
ContractCall, ScriptCall,
use crate::{
calls::{
utils::{build_tx_from_contract_calls, sealed, transaction_builder_from_contract_calls},
ContractCall, ScriptCall,
},
DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
};

#[async_trait::async_trait]
Expand Down Expand Up @@ -79,8 +82,8 @@ impl TransactionTuner for ScriptCall {
.with_script_data(self.compute_script_data()?)
.with_inputs(inputs)
.with_outputs(outputs)
.with_gas_estimation_tolerance(0.05)
.with_max_fee_estimation_tolerance(0.05))
.with_gas_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE)
.with_max_fee_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE))
}

async fn build_tx<T: Account>(
Expand Down
5 changes: 3 additions & 2 deletions packages/fuels-programs/src/calls/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use itertools::{chain, Itertools};
use crate::{
assembly::contract_call::{CallOpcodeParamsOffset, ContractCallInstructions},
calls::ContractCall,
DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE,
};

pub(crate) mod sealed {
Expand Down Expand Up @@ -73,8 +74,8 @@ pub(crate) async fn transaction_builder_from_contract_calls(
.with_script_data(script_data.clone())
.with_inputs(inputs)
.with_outputs(outputs)
.with_gas_estimation_tolerance(0.05)
.with_max_fee_estimation_tolerance(0.05))
.with_gas_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE)
.with_max_fee_estimation_tolerance(DEFAULT_MAX_FEE_ESTIMATION_TOLERANCE))
}

/// Creates a [`ScriptTransaction`] from contract calls. The internal [Transaction] is
Expand Down
Loading

0 comments on commit 3a2ec67

Please sign in to comment.