Skip to content

Commit 7fd6d51

Browse files
committed
refactor: Tidy up AssembleTx::new logic
1 parent 208f60b commit 7fd6d51

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

crates/fuel-core/src/schema/tx/assemble_tx.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,12 @@ where
162162
return Err(anyhow::anyhow!("The fee address index is out of bounds"));
163163
}
164164

165-
let mut duplicate_checker =
166-
HashSet::with_capacity(arguments.required_balances.len());
167-
for required_balance in &arguments.required_balances {
168-
let asset_id = required_balance.asset_id;
169-
let owner = required_balance.account.owner();
170-
if !duplicate_checker.insert((asset_id, owner)) {
171-
return Err(anyhow::anyhow!(
172-
"The same asset and account pair is used multiple times in required balances"
173-
));
174-
}
165+
if has_duplicates(&arguments.required_balances, |balance| {
166+
(balance.asset_id, balance.account.owner())
167+
}) {
168+
return Err(anyhow::anyhow!(
169+
"required balances contain duplicate (asset, account) pair"
170+
));
175171
}
176172

177173
let mut signature_witness_indexes = HashMap::<Address, u16>::new();
@@ -282,7 +278,7 @@ where
282278
let original_max_fee = tx.max_fee_limit();
283279
let original_witness_limit = tx.witness_limit();
284280

285-
let _self = Self {
281+
Ok(Self {
286282
tx,
287283
arguments,
288284
signature_witness_indexes,
@@ -294,9 +290,7 @@ where
294290
original_max_fee,
295291
original_witness_limit,
296292
fee_payer_account,
297-
};
298-
299-
Ok(_self)
293+
})
300294
}
301295

302296
pub async fn assemble(mut self) -> anyhow::Result<Tx> {
@@ -820,6 +814,22 @@ where
820814
}
821815
}
822816

817+
fn has_duplicates<T, F, K>(items: &[T], extractor: F) -> bool
818+
where
819+
F: Fn(&T) -> K,
820+
K: std::hash::Hash + std::cmp::Eq,
821+
{
822+
let mut duplicates = HashSet::with_capacity(items.len());
823+
for item in items {
824+
let key = extractor(item);
825+
if !duplicates.insert(key) {
826+
return true
827+
}
828+
}
829+
830+
false
831+
}
832+
823833
fn set_max_fee<Tx>(
824834
tx: &mut Tx,
825835
consensus_parameters: &ConsensusParameters,

0 commit comments

Comments
 (0)