@@ -269,21 +269,32 @@ pub fn generate_transaction_graph(
269
269
100_000_000_000_000_u128 ,
270
270
) ] ;
271
271
272
- std:: iter:: repeat_with ( move || {
272
+ std:: iter:: from_fn ( move || {
273
273
let n_inputs = rng. gen_range ( 1 ..=std:: cmp:: min ( 3 , utxos. len ( ) ) ) ;
274
274
let n_outputs = rng. gen_range ( 1 ..=3 ) ;
275
275
276
+ let estimated_fee = get_relay_fee_from_tx_size ( estimate_tx_size ( n_inputs, n_outputs) ) ;
277
+
276
278
let mut builder = TransactionBuilder :: new ( ) ;
277
279
let mut total = 0u128 ;
278
280
let mut amts = Vec :: new ( ) ;
279
281
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
+ {
281
288
let ( outpt, amt) = utxos. swap_remove ( rng. gen_range ( 0 ..utxos. len ( ) ) ) ;
282
289
total += amt;
283
290
builder = builder. add_input ( outpt, empty_witness ( rng) ) ;
291
+ input_count += 1 ;
284
292
}
285
293
286
294
for _ in 0 ..n_outputs {
295
+ if total < min_valid_total_amount {
296
+ break ;
297
+ }
287
298
let amt = rng. gen_range ( ( total / 2 ) ..( 95 * total / 100 ) ) ;
288
299
total -= amt;
289
300
builder = builder. add_output ( TxOutput :: Transfer (
@@ -305,7 +316,10 @@ pub fn generate_transaction_graph(
305
316
let origin = RemoteTxOrigin :: new ( p2p_types:: PeerId :: from_u64 ( 1 ) ) . into ( ) ;
306
317
let options = crate :: TxOptions :: default_for ( origin) ;
307
318
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
+ ) )
309
323
} )
310
324
}
311
325
0 commit comments