Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub struct Opt {
#[clap(long, display_order = 10)]
/// Output logs in alternative format (same as kaspad)
pub altlogs: bool,
#[clap(long = "user-agent-suffix", display_order = 11)]
/// Custom user agent suffix (max 20 characters)
pub user_agent_suffix: Option<String>,
}

fn parse_devfund_percent(s: &str) -> Result<u16, &'static str> {
Expand Down Expand Up @@ -82,6 +85,15 @@ impl Opt {
}
log::info!("Kaspad address: {}", self.kaspad_address);

if let Some(suffix) = &self.user_agent_suffix {
if suffix.contains('/') {
return Err("--user-agent-suffix cannot contain '/' characters".into());
}
if suffix.chars().count() > 20 {
return Err("--user-agent-suffix must be at most 20 characters".into());
}
}

Ok(())
}

Expand Down
26 changes: 23 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,39 @@ pub struct KaspadHandler {
devfund_address: Option<String>,
devfund_percent: u16,
block_template_ctr: u64,
extra_data: String,
}

impl KaspadHandler {
pub async fn connect<D>(address: D, miner_address: String, mine_when_not_synced: bool) -> Result<Self, Error>
pub async fn connect<D>(
address: D,
miner_address: String,
mine_when_not_synced: bool,
user_agent_suffix: Option<String>,
) -> Result<Self, Error>
where
D: TryInto<tonic::transport::Endpoint>,
D::Error: Into<Error>,
{
let mut client = RpcClient::connect(address).await?;
let (send_channel, recv) = mpsc::channel(3);

let extra_data = match user_agent_suffix {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we perhaps add

log::info!("Using user agent: {}", extra_data);

to show it once at startup?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea & done

Some(suffix) => {
let extra_data = format!("{}/{}", EXTRA_DATA, suffix);
info!("Using user agent: {}", extra_data);
extra_data
}
None => {
info!("Using user agent: {}, specify --user-agent-suffix to customize", EXTRA_DATA);
EXTRA_DATA.to_string()
}
};

send_channel.send(GetInfoRequestMessage {}.into()).await?;
send_channel
.send(
GetBlockTemplateRequestMessage { pay_address: miner_address.clone(), extra_data: EXTRA_DATA.into() }
GetBlockTemplateRequestMessage { pay_address: miner_address.clone(), extra_data: extra_data.clone() }
.into(),
)
.await?;
Expand All @@ -50,6 +69,7 @@ impl KaspadHandler {
devfund_address: None,
devfund_percent: 0,
block_template_ctr: 0,
extra_data,
})
}

Expand All @@ -70,7 +90,7 @@ impl KaspadHandler {
_ => self.miner_address.clone(),
};
self.block_template_ctr += 1;
self.client_send(GetBlockTemplateRequestMessage { pay_address, extra_data: EXTRA_DATA.into() }).await
self.client_send(GetBlockTemplateRequestMessage { pay_address, extra_data: self.extra_data.clone() }).await
}

pub async fn listen(&mut self, miner: &mut MinerManager, shutdown: ShutdownHandler) -> Result<(), Error> {
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ async fn main() -> Result<(), Error> {
let _shutdown_when_dropped = shutdown.arm();

while !shutdown.is_shutdown() {
let mut client =
KaspadHandler::connect(opt.kaspad_address.clone(), opt.mining_address.clone(), opt.mine_when_not_synced)
.await?;
let mut client = KaspadHandler::connect(
opt.kaspad_address.clone(),
opt.mining_address.clone(),
opt.mine_when_not_synced,
opt.user_agent_suffix.clone(),
)
.await?;
if let Some(devfund_address) = &opt.devfund_address {
client.add_devfund(devfund_address.clone(), opt.devfund_percent);
info!(
Expand Down