Skip to content

Commit 058c7f0

Browse files
authored
Merge pull request #1905 from mintlayer/fix/generate_transaction_graph_fee
Fix `generate_transaction_graph` test function
2 parents 27d5946 + 47fd98b commit 058c7f0

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

Cargo.lock

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mempool/src/pool/tx_pool/tests/utils.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,32 @@ pub fn generate_transaction_graph(
269269
100_000_000_000_000_u128,
270270
)];
271271

272-
std::iter::repeat_with(move || {
272+
std::iter::from_fn(move || {
273273
let n_inputs = rng.gen_range(1..=std::cmp::min(3, utxos.len()));
274274
let n_outputs = rng.gen_range(1..=3);
275275

276+
let estimated_fee = get_relay_fee_from_tx_size(estimate_tx_size(n_inputs, n_outputs));
277+
276278
let mut builder = TransactionBuilder::new();
277279
let mut total = 0u128;
278280
let mut amts = Vec::new();
279281

280-
for _ in 0..n_inputs {
282+
// the number is chosen to avoid generating empty range below
283+
let min_valid_total_amount = 2;
284+
285+
let mut input_count = 0;
286+
while input_count < n_inputs || total < estimated_fee.into_atoms() + min_valid_total_amount
287+
{
281288
let (outpt, amt) = utxos.swap_remove(rng.gen_range(0..utxos.len()));
282289
total += amt;
283290
builder = builder.add_input(outpt, empty_witness(rng));
291+
input_count += 1;
284292
}
285293

286294
for _ in 0..n_outputs {
295+
if total < min_valid_total_amount {
296+
break;
297+
}
287298
let amt = rng.gen_range((total / 2)..(95 * total / 100));
288299
total -= amt;
289300
builder = builder.add_output(TxOutput::Transfer(
@@ -305,7 +316,10 @@ pub fn generate_transaction_graph(
305316
let origin = RemoteTxOrigin::new(p2p_types::PeerId::from_u64(1)).into();
306317
let options = crate::TxOptions::default_for(origin);
307318
let entry = TxEntry::new(tx, time, origin, options);
308-
TxEntryWithFee::new(entry, Fee::new(Amount::from_atoms(total)))
319+
Some(TxEntryWithFee::new(
320+
entry,
321+
Fee::new(Amount::from_atoms(total)),
322+
))
309323
})
310324
}
311325

0 commit comments

Comments
 (0)