Skip to content

Commit

Permalink
rothschild: add option to choose and randomize fee
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Perchanov <[email protected]>
  • Loading branch information
Dmitry Perchanov committed Sep 20, 2024
1 parent 66535be commit 52ad92d
Showing 1 changed file with 67 additions and 9 deletions.
76 changes: 67 additions & 9 deletions rothschild/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use kaspa_rpc_core::{api::rpc::RpcApi, notify::mode::NotificationMode, RpcUtxoEn
use kaspa_txscript::pay_to_address_script;
use parking_lot::Mutex;
use rayon::prelude::*;
use secp256k1::{rand::thread_rng, rand::random, Keypair};
use secp256k1::{rand::{thread_rng, Rng}, Keypair};
use tokio::time::{interval, MissedTickBehavior};

const DEFAULT_SEND_AMOUNT: u64 = 10 * SOMPI_PER_KASPA;
Expand All @@ -41,6 +41,8 @@ pub struct Args {
pub threads: u8,
pub unleashed: bool,
pub addr: Option<String>,
pub priority_fee: u64,
pub randomize_fee: bool,
}

impl Args {
Expand All @@ -53,6 +55,8 @@ impl Args {
threads: m.get_one::<u8>("threads").cloned().unwrap(),
unleashed: m.get_one::<bool>("unleashed").cloned().unwrap_or(false),
addr: m.get_one::<String>("addr").cloned(),
priority_fee: m.get_one::<u64>("priority-fee").cloned().unwrap_or(0),
randomize_fee: m.get_one::<bool>("randomize-fee").cloned().unwrap_or(false),
}
}
}
Expand Down Expand Up @@ -88,6 +92,24 @@ pub fn cli() -> Command {
)
.arg(Arg::new("unleashed").long("unleashed").action(ArgAction::SetTrue).hide(true).help("Allow higher TPS"))
.arg(Arg::new("addr").long("to-addr").short('a').value_name("addr").help("address to send to"))
.arg(
Arg::new("priority-fee")
.long("priority-fee")
.short('f')
.value_name("priority-fee")
.default_value("0")
.value_parser(clap::value_parser!(u64))
.help("Transaction priority fee"),
)
.arg(
Arg::new("randomize-fee")
.long("randomize-fee")
.short('r')
.value_name("randomize-fee")
.action(ArgAction::SetTrue)
.default_value("false")
.help("Randomize transaction priority fee")
)
}

async fn new_rpc_client(subscription_context: &SubscriptionContext, address: &str) -> GrpcClient {
Expand Down Expand Up @@ -153,15 +175,33 @@ async fn main() {

let kaspa_addr = Address::new(ADDRESS_PREFIX, ADDRESS_VERSION, &schnorr_key.x_only_public_key().0.serialize());

let kaspa_to_addr = if let Some(addr_str) = args.addr {
Address::try_from(addr_str).unwrap()
} else {
kaspa_addr.clone()
};
let kaspa_to_addr = if let Some(ref addr_str) = args.addr {
Address::try_from(addr_str.clone()).unwrap()
} else {
kaspa_addr.clone()
};

rayon::ThreadPoolBuilder::new().num_threads(args.threads as usize).build_global().unwrap();

info!("Using Rothschild with private key {} and address {}\n\tspend to {}", schnorr_key.display_secret(), String::from(&kaspa_addr), String::from(&kaspa_to_addr));
let mut log_message = format!(
"Using Rothschild with:\n\
\tprivate key: {}\n\
\tfrom address: {}",
schnorr_key.display_secret(),
String::from(&kaspa_addr)
);
if args.addr.is_some() {
log_message.push_str(&format!("\n\tto address: {}", String::from(&kaspa_to_addr)));
}
if args.priority_fee != 0 {
log_message.push_str(&format!(
"\n\tpriority fee: {} SOMPS {}",
args.priority_fee,
if args.randomize_fee { "[randomize]" } else { "" }
));
}
info!("{}", log_message);

let info = rpc_client.get_block_dag_info().await.unwrap();
let coinbase_maturity = match info.network.suffix {
Some(11) => TESTNET11_PARAMS.coinbase_maturity,
Expand Down Expand Up @@ -265,6 +305,8 @@ async fn main() {
stats.clone(),
maximize_inputs,
&mut next_available_utxo_index,
args.priority_fee,
args.randomize_fee,
)
.await;
if !has_funds {
Expand Down Expand Up @@ -378,6 +420,8 @@ async fn maybe_send_tx(
stats: Arc<Mutex<Stats>>,
maximize_inputs: bool,
next_available_utxo_index: &mut usize,
priority_fee: u64,
randomize_fee: bool,
) -> bool {
let num_outs = if maximize_inputs { 1 } else { 2 };

Expand All @@ -386,7 +430,14 @@ async fn maybe_send_tx(
let selected_utxos_groups = (0..txs_to_send)
.map(|_| {
let (selected_utxos, selected_amount) =
select_utxos(utxos, DEFAULT_SEND_AMOUNT, num_outs, maximize_inputs, next_available_utxo_index);
select_utxos(utxos,
DEFAULT_SEND_AMOUNT,
num_outs,
maximize_inputs,
next_available_utxo_index,
priority_fee,
randomize_fee
);
if selected_amount == 0 {
return None;
}
Expand Down Expand Up @@ -482,18 +533,25 @@ fn select_utxos(
num_outs: u64,
maximize_utxos: bool,
next_available_utxo_index: &mut usize,
priority_fee: u64,
randomize_fee: bool,
) -> (Vec<(TransactionOutpoint, UtxoEntry)>, u64) {
const MAX_UTXOS: usize = 84;
let mut selected_amount: u64 = 0;
let mut selected = Vec::new();
let mut rng = thread_rng();

while next_available_utxo_index < &mut utxos.len() {
let (outpoint, entry) = utxos[*next_available_utxo_index].clone();
selected_amount += entry.amount;
selected.push((outpoint, entry));

let fee = required_fee(selected.len(), num_outs);
let priority_fee = (random::<f32>() * SOMPI_PER_KASPA as f32) as u64;
let priority_fee = if randomize_fee {
rng.gen_range(0..priority_fee)
} else {
priority_fee
};

*next_available_utxo_index += 1;

Expand Down

0 comments on commit 52ad92d

Please sign in to comment.