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

Add fn add_fee_input() and make fn add_random_fee_input() truly random #829

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 17 additions & 1 deletion fuel-tx/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ use fuel_types::{
Salt,
Word,
};
#[cfg(feature = "rand")]
use rand::{
rngs::StdRng,
Rng,
};

pub trait BuildableAloc
where
Expand Down Expand Up @@ -408,7 +413,18 @@ impl<Tx: Buildable> TransactionBuilder<Tx> {
}

#[cfg(feature = "rand")]
pub fn add_random_fee_input(&mut self) -> &mut Self {
pub fn add_random_fee_input(&mut self, rng: &mut StdRng) -> &mut Self {
self.add_unsigned_coin_input(
SecretKey::random(rng),
rng.gen(),
u32::MAX as u64,
*self.params.base_asset_id(),
Default::default(),
)
}

#[cfg(feature = "rand")]
pub fn add_fee_input(&mut self) -> &mut Self {
Copy link
Contributor

@acerone85 acerone85 Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be simply

self.add_rnadom_fee_input(rand::rngs::StdRng::seed_from_u64(2322u64))

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would work. Since it's not critical, I'll consider updating this part with next PR to fuel-vm.

use rand::{
Rng,
SeedableRng,
Expand Down
34 changes: 17 additions & 17 deletions fuel-tx/src/tests/valid_cases/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ fn gas_limit() {

TransactionBuilder::script(generate_bytes(rng), generate_bytes(rng))
.maturity(maturity)
.add_random_fee_input()
.add_fee_input()
.finalize()
.check(block_height, &test_params())
.expect("Failed to validate transaction");

TransactionBuilder::create(vec![0xfau8].into(), rng.gen(), vec![])
.maturity(maturity)
.add_random_fee_input()
.add_fee_input()
.add_contract_created()
.finalize()
.check(block_height, &test_params())
Expand Down Expand Up @@ -81,14 +81,14 @@ fn maturity() {

TransactionBuilder::script(generate_bytes(rng), generate_bytes(rng))
.maturity(block_height)
.add_random_fee_input()
.add_fee_input()
.finalize()
.check(block_height, &test_params())
.expect("Failed to validate script");

TransactionBuilder::create(rng.gen(), rng.gen(), vec![])
.maturity(block_height)
.add_random_fee_input()
.add_fee_input()
.add_contract_created()
.finalize()
.check(block_height, &test_params())
Expand Down Expand Up @@ -131,7 +131,7 @@ fn script__check__not_set_witness_limit_success() {

// When
let result = TransactionBuilder::script(generate_bytes(rng), generate_bytes(rng))
.add_random_fee_input()
.add_fee_input()
.finalize()
.check(block_height, &test_params());

Expand All @@ -148,7 +148,7 @@ fn create_not_set_witness_limit_success() {

// When
let result = TransactionBuilder::create(bytecode.clone().into(), rng.gen(), vec![])
.add_random_fee_input()
.add_fee_input()
.add_contract_created()
.finalize()
.check(block_height, &test_params());
Expand All @@ -168,7 +168,7 @@ fn script__check__set_witness_limit_for_empty_witness_success() {

// When
let result = TransactionBuilder::script(generate_bytes(rng), generate_bytes(rng))
.add_random_fee_input()
.add_fee_input()
.witness_limit(limit as u64)
.finalize()
.check(block_height, &test_params());
Expand All @@ -189,7 +189,7 @@ fn script_set_witness_limit_less_than_witness_data_size_fails() {

// When
let err = TransactionBuilder::script(generate_bytes(rng), generate_bytes(rng))
.add_random_fee_input()
.add_fee_input()
.witness_limit(limit as u64)
.finalize()
.check(block_height, &test_params())
Expand All @@ -210,7 +210,7 @@ fn create_set_witness_limit_for_empty_witness_success() {

// When
let result = TransactionBuilder::create(bytecode.clone().into(), rng.gen(), vec![])
.add_random_fee_input()
.add_fee_input()
.add_contract_created()
.witness_limit(limit as u64)
.finalize()
Expand All @@ -231,7 +231,7 @@ fn create_set_witness_limit_less_than_witness_data_size_fails() {

// When
let err = TransactionBuilder::create(bytecode.clone().into(), rng.gen(), vec![])
.add_random_fee_input()
.add_fee_input()
.witness_limit(limit as u64 - 1)
.finalize()
.check(block_height, &test_params())
Expand All @@ -249,7 +249,7 @@ fn script_not_set_max_fee_limit_success() {

// When
let result = TransactionBuilder::script(generate_bytes(rng), generate_bytes(rng))
.add_random_fee_input()
.add_fee_input()
.finalize()
.check(block_height, &test_params());

Expand All @@ -264,7 +264,7 @@ fn script__check__no_max_fee_fails() {

// Given
let mut tx = TransactionBuilder::script(generate_bytes(rng), generate_bytes(rng))
.add_random_fee_input()
.add_fee_input()
.finalize();
tx.policies_mut().set(PolicyType::MaxFee, None);

Expand All @@ -285,7 +285,7 @@ fn create_not_set_max_fee_limit_success() {

// When
let result = TransactionBuilder::create(rng.gen(), rng.gen(), vec![])
.add_random_fee_input()
.add_fee_input()
.add_contract_created()
.finalize()
.check(block_height, &test_params());
Expand All @@ -301,7 +301,7 @@ fn create__check__no_max_fee_fails() {

// Given
let mut tx = TransactionBuilder::create(rng.gen(), rng.gen(), vec![])
.add_random_fee_input()
.add_fee_input()
.finalize();
tx.policies_mut().set(PolicyType::MaxFee, None);

Expand Down Expand Up @@ -646,7 +646,7 @@ fn create__check__happy_path() {

TransactionBuilder::create(generate_bytes(rng).into(), rng.gen(), vec![])
.maturity(maturity)
.add_random_fee_input()
.add_fee_input()
.add_contract_created()
.finalize()
.check(block_height, &test_params())
Expand Down Expand Up @@ -692,7 +692,7 @@ fn create__check__must_contain_contract_created_output() {

let err = TransactionBuilder::create(generate_bytes(rng).into(), rng.gen(), vec![])
.maturity(maturity)
.add_random_fee_input()
.add_fee_input()
.finalize()
.check(block_height, &test_params())
.expect_err("Expected erroneous transaction");
Expand Down Expand Up @@ -742,7 +742,7 @@ fn create__check__cannot_have_variable_output() {

let err = TransactionBuilder::create(generate_bytes(rng).into(), rng.gen(), vec![])
.maturity(maturity)
.add_random_fee_input()
.add_fee_input()
.add_contract_created()
.add_output(Output::variable(rng.gen(), rng.gen(), rng.gen()))
.finalize()
Expand Down
2 changes: 1 addition & 1 deletion fuel-tx/src/tests/valid_cases/transaction/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn check__set_witness_limit_less_than_witness_data_size_fails() {
#[test]
fn check__no_max_fee_fails() {
let block_height = 1000.into();
let mut tx = valid_blob_transaction().add_random_fee_input().finalize();
let mut tx = valid_blob_transaction().add_fee_input().finalize();

// Given
tx.policies_mut().set(PolicyType::MaxFee, None);
Expand Down
6 changes: 2 additions & 4 deletions fuel-tx/src/tests/valid_cases/transaction/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn script_set_witness_limit_less_than_witness_data_size_fails() {
let failing_limit = limit - 1;
let tx = valid_upgrade_transaction()
.witness_limit(failing_limit as u64)
.add_random_fee_input()
.add_fee_input()
.finalize_as_transaction();

// When
Expand All @@ -143,9 +143,7 @@ fn script_set_witness_limit_less_than_witness_data_size_fails() {
#[test]
fn check__no_max_fee_fails() {
let block_height = 1000.into();
let mut tx = valid_upgrade_transaction()
.add_random_fee_input()
.finalize();
let mut tx = valid_upgrade_transaction().add_fee_input().finalize();

// Given
tx.policies_mut().set(PolicyType::MaxFee, None);
Expand Down
2 changes: 1 addition & 1 deletion fuel-tx/src/tests/valid_cases/transaction/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ fn check__set_witness_limit_less_than_witness_data_size_fails() {
#[test]
fn check__no_max_fee_fails() {
let block_height = 1000.into();
let mut tx = valid_upload_transaction().add_random_fee_input().finalize();
let mut tx = valid_upload_transaction().add_fee_input().finalize();

// Given
tx.policies_mut().set(PolicyType::MaxFee, None);
Expand Down
2 changes: 1 addition & 1 deletion fuel-tx/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Transaction {

crate::TransactionBuilder::script(vec![], vec![])
.max_fee_limit(0)
.add_random_fee_input()
.add_fee_input()
.finalize()
.into()
}
Expand Down
4 changes: 2 additions & 2 deletions fuel-tx/src/transaction/types/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ mod tests {
Salt::zeroed(),
storage_slots,
)
.add_random_fee_input()
.add_fee_input()
.finalize();
tx.body.storage_slots.reverse();

Expand All @@ -441,7 +441,7 @@ mod tests {
Salt::zeroed(),
storage_slots,
)
.add_random_fee_input()
.add_fee_input()
.finalize()
.check(0.into(), &ConsensusParameters::standard())
.expect_err("Expected erroneous transaction");
Expand Down
6 changes: 3 additions & 3 deletions fuel-vm/benches/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn execution(c: &mut Criterion) {
vec![],
)
.max_fee_limit(0)
.add_random_fee_input()
.add_fee_input()
.finalize();
let script = script
.into_checked_basic(Default::default(), &Default::default())
Expand Down Expand Up @@ -92,7 +92,7 @@ fn execution(c: &mut Criterion) {
vec![],
)
.max_fee_limit(0)
.add_random_fee_input()
.add_fee_input()
.finalize();
let script = script
.into_checked_basic(Default::default(), &Default::default())
Expand Down Expand Up @@ -137,7 +137,7 @@ fn execution(c: &mut Criterion) {
vec![],
)
.max_fee_limit(0)
.add_random_fee_input()
.add_fee_input()
.finalize();
let script = script
.into_checked_basic(Default::default(), &Default::default())
Expand Down
6 changes: 3 additions & 3 deletions fuel-vm/examples/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn example_file_read() {
let tx = TransactionBuilder::script(script, script_data)
.script_gas_limit(1_000_000)
.maturity(Default::default())
.add_random_fee_input()
.add_fee_input()
.finalize()
.into_checked(Default::default(), &consensus_params)
.expect("failed to generate a checked tx");
Expand Down Expand Up @@ -170,7 +170,7 @@ fn example_counter() {
let tx = TransactionBuilder::script(script, script_data)
.script_gas_limit(1_000_000)
.maturity(Default::default())
.add_random_fee_input()
.add_fee_input()
.finalize()
.into_checked(Default::default(), &consensus_params)
.expect("failed to generate a checked tx");
Expand Down Expand Up @@ -233,7 +233,7 @@ fn example_shared_counter() {
let tx = TransactionBuilder::script(script, script_data)
.script_gas_limit(1_000_000)
.maturity(Default::default())
.add_random_fee_input()
.add_fee_input()
.finalize()
.into_checked(Default::default(), &consensus_params)
.expect("failed to generate a checked tx");
Expand Down
2 changes: 1 addition & 1 deletion fuel-vm/examples/single_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn main() {
let tx = TransactionBuilder::script(script, script_data)
.script_gas_limit(1_000_000)
.maturity(Default::default())
.add_random_fee_input()
.add_fee_input()
.finalize()
.into_checked(Default::default(), &consensus_params)
.expect("failed to generate a checked tx")
Expand Down
6 changes: 3 additions & 3 deletions fuel-vm/src/interpreter/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mod tests {

let tx = TransactionBuilder::script(script, vec![])
.script_gas_limit(gas_limit)
.add_random_fee_input()
.add_fee_input()
.finalize()
.into_checked(height, &consensus_params)
.expect("failed to generate checked tx")
Expand Down Expand Up @@ -173,7 +173,7 @@ mod tests {

let tx = TransactionBuilder::script(script, vec![])
.script_gas_limit(gas_limit)
.add_random_fee_input()
.add_fee_input()
.finalize()
.into_checked(height, &consensus_params)
.expect("failed to generate checked tx")
Expand Down Expand Up @@ -226,7 +226,7 @@ mod tests {

let tx = TransactionBuilder::script(script, vec![])
.script_gas_limit(gas_limit)
.add_random_fee_input()
.add_fee_input()
.finalize()
.into_checked(height, &consensus_params)
.expect("failed to generate checked tx")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn cant_write_to_reserved_registers(raw_random_instruction: u32) -> TestResult {
let script = op::ret(0x10).to_bytes().to_vec();
let block_height = Default::default();
let tx = TransactionBuilder::script(script, vec![])
.add_random_fee_input()
.add_fee_input()
.finalize();

let tx = tx
Expand Down
8 changes: 4 additions & 4 deletions fuel-vm/src/interpreter/executors/main/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn valid_script_tx() -> Checked<Script> {

TransactionBuilder::script(vec![], vec![])
.max_fee_limit(arb_max_fee)
.add_random_fee_input()
.add_fee_input()
.finalize_checked_basic(Default::default())
}

Expand Down Expand Up @@ -151,7 +151,7 @@ fn valid_create_tx() -> Checked<Create> {

TransactionBuilder::create(witness, salt, vec![])
.max_fee_limit(arb_max_fee)
.add_random_fee_input()
.add_fee_input()
.add_contract_created()
.finalize_checked_basic(Default::default())
}
Expand Down Expand Up @@ -193,7 +193,7 @@ fn valid_upgrade_tx() -> Checked<Upgrade> {
Default::default(),
0,
))
.add_random_fee_input()
.add_fee_input()
.finalize_checked_basic(Default::default())
}

Expand Down Expand Up @@ -234,7 +234,7 @@ fn valid_upload_tx() -> Checked<Upload> {
})
.add_witness(subsection.subsection.into())
.max_fee_limit(arb_max_fee)
.add_random_fee_input()
.add_fee_input()
.finalize_checked_basic(Default::default())
}

Expand Down
2 changes: 1 addition & 1 deletion fuel-vm/src/interpreter/internal/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn variable_output_updates_in_memory() {

let tx = TransactionBuilder::script(vec![], vec![])
.script_gas_limit(gas_limit)
.add_random_fee_input()
.add_fee_input()
.add_output(variable_output)
.finalize()
.into_checked(height, &consensus_params)
Expand Down
Loading
Loading