Skip to content

Commit

Permalink
Proto updates to use NewBlockTemplate (#29)
Browse files Browse the repository at this point in the history
* Proto updates to use NewBlockTemplate

* Handle NotifyNewBlockTemplateResponse

* Register for new block template notification

* Remove unused arms
  • Loading branch information
coderofstuff authored Dec 11, 2023
1 parent d1dd351 commit a53b36a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
3 changes: 3 additions & 0 deletions proto/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ message KaspadMessage {
NotifyVirtualDaaScoreChangedRequestMessage notifyVirtualDaaScoreChangedRequest = 1074;
NotifyVirtualDaaScoreChangedResponseMessage notifyVirtualDaaScoreChangedResponse = 1075;
VirtualDaaScoreChangedNotificationMessage virtualDaaScoreChangedNotification = 1076;
NotifyNewBlockTemplateRequestMessage notifyNewBlockTemplateRequest = 1081;
NotifyNewBlockTemplateResponseMessage notifyNewBlockTemplateResponse = 1082;
NewBlockTemplateNotificationMessage newBlockTemplateNotification = 1083;
}
}

Expand Down
23 changes: 23 additions & 0 deletions proto/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ message RpcBlockVerboseData{
bool isHeaderOnly = 15;
uint64 blueScore = 16;
repeated string childrenHashes = 17;
repeated string mergeSetBluesHashes = 18;
repeated string mergeSetRedsHashes = 19;
bool isChainBlock = 20;
}

message RpcTransaction {
Expand Down Expand Up @@ -129,6 +132,7 @@ message GetCurrentNetworkResponseMessage{
// See: GetBlockTemplateRequestMessage
message SubmitBlockRequestMessage{
RpcBlock block = 2;
bool allowNonDAABlocks = 3;
}

message SubmitBlockResponseMessage{
Expand All @@ -148,6 +152,7 @@ message SubmitBlockResponseMessage{
message GetBlockTemplateRequestMessage{
// Which kaspa address should the coinbase block reward transaction pay into
string payAddress = 1;
string extraData = 2;
}

message GetBlockTemplateResponseMessage{
Expand Down Expand Up @@ -620,4 +625,22 @@ message EstimateNetworkHashesPerSecondResponseMessage{
RPCError error = 1000;
}

// NotifyNewBlockTemplateRequestMessage registers this connection for
// NewBlockTemplate notifications.
//
// See: NewBlockTemplateNotificationMessage
message NotifyNewBlockTemplateRequestMessage {
}

message NotifyNewBlockTemplateResponseMessage {
RPCError error = 1000;
}

// NewBlockTemplateNotificationMessage is sent whenever a new updated block template is
// available for miners.
//
// See NotifyNewBlockTemplateRequestMessage
message NewBlockTemplateNotificationMessage {
}


19 changes: 13 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use tokio::sync::mpsc::{self, error::SendError, Sender};
use tokio_stream::wrappers::ReceiverStream;
use tonic::{transport::Channel as TonicChannel, Streaming};

static EXTRA_DATA: &str = concat!(env!("CARGO_PKG_VERSION"));

#[allow(dead_code)]
pub struct KaspadHandler {
client: RpcClient<TonicChannel>,
Expand All @@ -32,7 +34,12 @@ impl KaspadHandler {
let mut client = RpcClient::connect(address).await?;
let (send_channel, recv) = mpsc::channel(3);
send_channel.send(GetInfoRequestMessage {}.into()).await?;
send_channel.send(GetBlockTemplateRequestMessage { pay_address: miner_address.clone() }.into()).await?;
send_channel
.send(
GetBlockTemplateRequestMessage { pay_address: miner_address.clone(), extra_data: EXTRA_DATA.into() }
.into(),
)
.await?;
let stream = client.message_stream(ReceiverStream::new(recv)).await?.into_inner();
Ok(Self {
client,
Expand Down Expand Up @@ -63,7 +70,7 @@ impl KaspadHandler {
_ => self.miner_address.clone(),
};
self.block_template_ctr += 1;
self.client_send(GetBlockTemplateRequestMessage { pay_address }).await
self.client_send(GetBlockTemplateRequestMessage { pay_address, extra_data: EXTRA_DATA.into() }).await
}

pub async fn listen(&mut self, miner: &mut MinerManager, shutdown: ShutdownHandler) -> Result<(), Error> {
Expand All @@ -81,7 +88,7 @@ impl KaspadHandler {

async fn handle_message(&mut self, msg: Payload, miner: &mut MinerManager) -> Result<(), Error> {
match msg {
Payload::BlockAddedNotification(_) => self.client_get_block_template().await?,
Payload::NewBlockTemplateNotification(_) => self.client_get_block_template().await?,
Payload::GetBlockTemplateResponse(template) => match (template.block, template.is_synced, template.error) {
(Some(b), true, None) => miner.process_block(Some(b))?,
(Some(b), false, None) if self.mine_when_not_synced => miner.process_block(Some(b))?,
Expand All @@ -100,9 +107,9 @@ impl KaspadHandler {
info!("Get block response: {:?}", msg);
}
Payload::GetInfoResponse(info) => info!("Kaspad version: {}", info.server_version),
Payload::NotifyBlockAddedResponse(res) => match res.error {
None => info!("Registered for block notifications"),
Some(e) => error!("Failed registering for block notifications: {:?}", e),
Payload::NotifyNewBlockTemplateResponse(res) => match res.error {
None => info!("Registered for new template notifications"),
Some(e) => error!("Failed registering for new template notifications: {:?}", e),
},
msg => info!("Got unknown msg: {:?}", msg),
}
Expand Down
15 changes: 13 additions & 2 deletions src/kaspad_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
pow::{self, HeaderHasher},
proto::{
kaspad_message::Payload, GetBlockTemplateRequestMessage, GetInfoRequestMessage, KaspadMessage,
NotifyBlockAddedRequestMessage, RpcBlock, SubmitBlockRequestMessage,
NotifyBlockAddedRequestMessage, RpcBlock, SubmitBlockRequestMessage, NotifyNewBlockTemplateRequestMessage,
},
Hash,
};
Expand All @@ -21,7 +21,12 @@ impl KaspadMessage {
#[must_use]
#[inline(always)]
pub fn submit_block(block: RpcBlock) -> Self {
KaspadMessage { payload: Some(Payload::SubmitBlockRequest(SubmitBlockRequestMessage { block: Some(block) })) }
KaspadMessage {
payload: Some(Payload::SubmitBlockRequest(SubmitBlockRequestMessage {
block: Some(block),
allow_non_daa_blocks: false,
})),
}
}
}

Expand All @@ -45,6 +50,12 @@ impl From<GetBlockTemplateRequestMessage> for KaspadMessage {
}
}

impl From<NotifyNewBlockTemplateRequestMessage> for KaspadMessage {
fn from(a: NotifyNewBlockTemplateRequestMessage) -> Self {
KaspadMessage { payload: Some(Payload::NotifyNewBlockTemplateRequest(a)) }
}
}

impl RpcBlock {
#[must_use]
#[inline(always)]
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{
};

use crate::{
cli::Opt, client::KaspadHandler, miner::MinerManager, proto::NotifyBlockAddedRequestMessage, target::Uint256,
cli::Opt, client::KaspadHandler, miner::MinerManager, proto::NotifyNewBlockTemplateRequestMessage, target::Uint256,
};

mod cli;
Expand Down Expand Up @@ -89,7 +89,7 @@ async fn main() -> Result<(), Error> {
devfund_address
);
}
client.client_send(NotifyBlockAddedRequestMessage {}).await?;
client.client_send(NotifyNewBlockTemplateRequestMessage {}).await?;
client.client_get_block_template().await?;

let mut miner_manager =
Expand Down

0 comments on commit a53b36a

Please sign in to comment.