Skip to content

Commit a4d656f

Browse files
iqdecayhal3e
andauthored
fix: generate salt inside macro call to avoid contract id collision (#1177)
- Fix an issue where two contracts would have the same id because the salt was not generated properly because the macro didn't always recompile. Co-authored-by: hal3e <git@hal3e.io>
1 parent 51a4cc4 commit a4d656f

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

packages/fuels-macros/src/setup_program_test/code_gen.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::{
66
use fuels_code_gen::{utils::ident, Abigen, AbigenTarget, ProgramType};
77
use proc_macro2::{Ident, TokenStream};
88
use quote::quote;
9-
use rand::{rngs::StdRng, Rng, SeedableRng};
109
use syn::LitStr;
1110

1211
use crate::setup_program_test::parsing::{
@@ -109,9 +108,6 @@ fn contract_deploying_code(
109108
commands
110109
.iter()
111110
.map(|command| {
112-
// Generate random salt for contract deployment
113-
let mut rng = StdRng::from_entropy();
114-
let salt: [u8; 32] = rng.gen();
115111

116112
let contract_instance_name = ident(&command.name);
117113
let contract_struct_name = ident(&command.contract.value());
@@ -123,8 +119,14 @@ fn contract_deploying_code(
123119
let bin_path = project.bin_path();
124120

125121
quote! {
122+
// Generate random salt for contract deployment.
123+
// These lines must be inside the `quote!` macro, otherwise the salt remains
124+
// identical between macro compilation, causing contract id collision.
125+
let mut rng = <::rand::rngs::StdRng as ::rand::SeedableRng>::from_entropy();
126+
let salt: [u8; 32] = <::rand::rngs::StdRng as ::rand::Rng>::gen(&mut rng);
127+
126128
let #contract_instance_name = {
127-
let load_config = LoadConfiguration::default().with_salt([#(#salt),*]);
129+
let load_config = LoadConfiguration::default().with_salt(salt);
128130

129131
let loaded_contract = Contract::load_from(#bin_path, load_config).expect("Failed to load the contract");
130132

packages/fuels/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ fuels-programs = { workspace = true, optional = true }
2424
fuels-test-helpers = { workspace = true, optional = true }
2525

2626
[dev-dependencies]
27-
fuels-code-gen = { workspace = true }
2827
chrono = { workspace = true }
2928
fuel-core = { workspace = true, default-features = false }
3029
fuel-core-types = { workspace = true }
30+
fuels-code-gen = { workspace = true }
3131
fuels-test-helpers = { workspace = true }
3232
hex = { workspace = true, default-features = false }
33+
rand = { workspace = true }
3334
sha2 = { workspace = true }
3435
tempfile = { workspace = true }
3536
tokio = { workspace = true }

0 commit comments

Comments
 (0)