Skip to content

Commit b96fbf1

Browse files
committed
Fix rothschild sending less than 100 txs
Due to integer divide, when tps is < 100, we end up creating no tx
1 parent 309a31a commit b96fbf1

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

rothschild/src/main.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,22 +208,40 @@ async fn main() {
208208
});
209209
let tx_sender = submit_tx_pool.sender();
210210

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 };
211214
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+
}));
213220
ticker.set_missed_tick_behavior(MissedTickBehavior::Delay);
214221

215222
let mut maximize_inputs = false;
216223
let mut last_refresh = unix_now();
217224
// This allows us to keep track of the UTXOs we already tried to use for this period
218225
// until the UTXOs are refreshed. At that point, this will be reset as well.
219226
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+
221230
loop {
222231
ticker.tick().await;
223232
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+
224242
let now = unix_now();
225243
let has_funds = maybe_send_tx(
226-
target_tps,
244+
txs_to_send,
227245
&tx_sender,
228246
kaspa_addr.clone(),
229247
&mut utxos,
@@ -336,7 +354,7 @@ fn is_utxo_spendable(entry: &UtxoEntry, virtual_daa_score: u64, coinbase_maturit
336354
}
337355

338356
async fn maybe_send_tx(
339-
tps: u64,
357+
txs_to_send: u64,
340358
tx_sender: &async_channel::Sender<ClientPoolArg>,
341359
kaspa_addr: Address,
342360
utxos: &mut Vec<(TransactionOutpoint, UtxoEntry)>,
@@ -350,7 +368,7 @@ async fn maybe_send_tx(
350368

351369
let mut has_fund = false;
352370

353-
let selected_utxos_groups = (0..tps * MILLIS_PER_TICK / 1000)
371+
let selected_utxos_groups = (0..txs_to_send)
354372
.map(|_| {
355373
let (selected_utxos, selected_amount) =
356374
select_utxos(utxos, DEFAULT_SEND_AMOUNT, num_outs, maximize_inputs, next_available_utxo_index);

0 commit comments

Comments
 (0)