@@ -208,22 +208,40 @@ async fn main() {
208
208
} ) ;
209
209
let tx_sender = submit_tx_pool. sender ( ) ;
210
210
211
+ let target_tps = args. tps . min ( if args. unleashed { u64:: MAX } else { 100 } ) ;
212
+ let should_tick_per_second = target_tps * MILLIS_PER_TICK / 1000 == 0 ;
213
+ let avg_txs_per_tick = if should_tick_per_second { target_tps } else { target_tps * MILLIS_PER_TICK / 1000 } ;
211
214
let mut utxos = refresh_utxos ( & rpc_client, kaspa_addr. clone ( ) , & mut pending, coinbase_maturity) . await ;
212
- let mut ticker = interval ( Duration :: from_millis ( MILLIS_PER_TICK ) ) ;
215
+ let mut ticker = interval ( Duration :: from_millis ( if should_tick_per_second {
216
+ 1000
217
+ } else {
218
+ MILLIS_PER_TICK
219
+ } ) ) ;
213
220
ticker. set_missed_tick_behavior ( MissedTickBehavior :: Delay ) ;
214
221
215
222
let mut maximize_inputs = false ;
216
223
let mut last_refresh = unix_now ( ) ;
217
224
// This allows us to keep track of the UTXOs we already tried to use for this period
218
225
// until the UTXOs are refreshed. At that point, this will be reset as well.
219
226
let mut next_available_utxo_index = 0 ;
220
- let target_tps = args. tps . min ( if args. unleashed { u64:: MAX } else { 100 } ) ;
227
+ // Tracker so we can try to send as close as possible to the target TPS
228
+ let mut remaining_txs_in_interval = target_tps;
229
+
221
230
loop {
222
231
ticker. tick ( ) . await ;
223
232
maximize_inputs = should_maximize_inputs ( maximize_inputs, & utxos, & pending) ;
233
+ let txs_to_send = if remaining_txs_in_interval > avg_txs_per_tick * 2 {
234
+ remaining_txs_in_interval = remaining_txs_in_interval - avg_txs_per_tick;
235
+ avg_txs_per_tick
236
+ } else {
237
+ let count = remaining_txs_in_interval;
238
+ remaining_txs_in_interval = target_tps;
239
+ count
240
+ } ;
241
+
224
242
let now = unix_now ( ) ;
225
243
let has_funds = maybe_send_tx (
226
- target_tps ,
244
+ txs_to_send ,
227
245
& tx_sender,
228
246
kaspa_addr. clone ( ) ,
229
247
& mut utxos,
@@ -336,7 +354,7 @@ fn is_utxo_spendable(entry: &UtxoEntry, virtual_daa_score: u64, coinbase_maturit
336
354
}
337
355
338
356
async fn maybe_send_tx (
339
- tps : u64 ,
357
+ txs_to_send : u64 ,
340
358
tx_sender : & async_channel:: Sender < ClientPoolArg > ,
341
359
kaspa_addr : Address ,
342
360
utxos : & mut Vec < ( TransactionOutpoint , UtxoEntry ) > ,
@@ -350,7 +368,7 @@ async fn maybe_send_tx(
350
368
351
369
let mut has_fund = false ;
352
370
353
- let selected_utxos_groups = ( 0 ..tps * MILLIS_PER_TICK / 1000 )
371
+ let selected_utxos_groups = ( 0 ..txs_to_send )
354
372
. map ( |_| {
355
373
let ( selected_utxos, selected_amount) =
356
374
select_utxos ( utxos, DEFAULT_SEND_AMOUNT , num_outs, maximize_inputs, next_available_utxo_index) ;
0 commit comments