Skip to content

Commit a53b36a

Browse files
authored
Proto updates to use NewBlockTemplate (#29)
* Proto updates to use NewBlockTemplate * Handle NotifyNewBlockTemplateResponse * Register for new block template notification * Remove unused arms
1 parent d1dd351 commit a53b36a

5 files changed

Lines changed: 54 additions & 10 deletions

File tree

proto/messages.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ message KaspadMessage {
121121
NotifyVirtualDaaScoreChangedRequestMessage notifyVirtualDaaScoreChangedRequest = 1074;
122122
NotifyVirtualDaaScoreChangedResponseMessage notifyVirtualDaaScoreChangedResponse = 1075;
123123
VirtualDaaScoreChangedNotificationMessage virtualDaaScoreChangedNotification = 1076;
124+
NotifyNewBlockTemplateRequestMessage notifyNewBlockTemplateRequest = 1081;
125+
NotifyNewBlockTemplateResponseMessage notifyNewBlockTemplateResponse = 1082;
126+
NewBlockTemplateNotificationMessage newBlockTemplateNotification = 1083;
124127
}
125128
}
126129

proto/rpc.proto

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ message RpcBlockVerboseData{
5252
bool isHeaderOnly = 15;
5353
uint64 blueScore = 16;
5454
repeated string childrenHashes = 17;
55+
repeated string mergeSetBluesHashes = 18;
56+
repeated string mergeSetRedsHashes = 19;
57+
bool isChainBlock = 20;
5558
}
5659

5760
message RpcTransaction {
@@ -129,6 +132,7 @@ message GetCurrentNetworkResponseMessage{
129132
// See: GetBlockTemplateRequestMessage
130133
message SubmitBlockRequestMessage{
131134
RpcBlock block = 2;
135+
bool allowNonDAABlocks = 3;
132136
}
133137

134138
message SubmitBlockResponseMessage{
@@ -148,6 +152,7 @@ message SubmitBlockResponseMessage{
148152
message GetBlockTemplateRequestMessage{
149153
// Which kaspa address should the coinbase block reward transaction pay into
150154
string payAddress = 1;
155+
string extraData = 2;
151156
}
152157

153158
message GetBlockTemplateResponseMessage{
@@ -620,4 +625,22 @@ message EstimateNetworkHashesPerSecondResponseMessage{
620625
RPCError error = 1000;
621626
}
622627

628+
// NotifyNewBlockTemplateRequestMessage registers this connection for
629+
// NewBlockTemplate notifications.
630+
//
631+
// See: NewBlockTemplateNotificationMessage
632+
message NotifyNewBlockTemplateRequestMessage {
633+
}
634+
635+
message NotifyNewBlockTemplateResponseMessage {
636+
RPCError error = 1000;
637+
}
638+
639+
// NewBlockTemplateNotificationMessage is sent whenever a new updated block template is
640+
// available for miners.
641+
//
642+
// See NotifyNewBlockTemplateRequestMessage
643+
message NewBlockTemplateNotificationMessage {
644+
}
645+
623646

src/client.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use tokio::sync::mpsc::{self, error::SendError, Sender};
1111
use tokio_stream::wrappers::ReceiverStream;
1212
use tonic::{transport::Channel as TonicChannel, Streaming};
1313

14+
static EXTRA_DATA: &str = concat!(env!("CARGO_PKG_VERSION"));
15+
1416
#[allow(dead_code)]
1517
pub struct KaspadHandler {
1618
client: RpcClient<TonicChannel>,
@@ -32,7 +34,12 @@ impl KaspadHandler {
3234
let mut client = RpcClient::connect(address).await?;
3335
let (send_channel, recv) = mpsc::channel(3);
3436
send_channel.send(GetInfoRequestMessage {}.into()).await?;
35-
send_channel.send(GetBlockTemplateRequestMessage { pay_address: miner_address.clone() }.into()).await?;
37+
send_channel
38+
.send(
39+
GetBlockTemplateRequestMessage { pay_address: miner_address.clone(), extra_data: EXTRA_DATA.into() }
40+
.into(),
41+
)
42+
.await?;
3643
let stream = client.message_stream(ReceiverStream::new(recv)).await?.into_inner();
3744
Ok(Self {
3845
client,
@@ -63,7 +70,7 @@ impl KaspadHandler {
6370
_ => self.miner_address.clone(),
6471
};
6572
self.block_template_ctr += 1;
66-
self.client_send(GetBlockTemplateRequestMessage { pay_address }).await
73+
self.client_send(GetBlockTemplateRequestMessage { pay_address, extra_data: EXTRA_DATA.into() }).await
6774
}
6875

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

8289
async fn handle_message(&mut self, msg: Payload, miner: &mut MinerManager) -> Result<(), Error> {
8390
match msg {
84-
Payload::BlockAddedNotification(_) => self.client_get_block_template().await?,
91+
Payload::NewBlockTemplateNotification(_) => self.client_get_block_template().await?,
8592
Payload::GetBlockTemplateResponse(template) => match (template.block, template.is_synced, template.error) {
8693
(Some(b), true, None) => miner.process_block(Some(b))?,
8794
(Some(b), false, None) if self.mine_when_not_synced => miner.process_block(Some(b))?,
@@ -100,9 +107,9 @@ impl KaspadHandler {
100107
info!("Get block response: {:?}", msg);
101108
}
102109
Payload::GetInfoResponse(info) => info!("Kaspad version: {}", info.server_version),
103-
Payload::NotifyBlockAddedResponse(res) => match res.error {
104-
None => info!("Registered for block notifications"),
105-
Some(e) => error!("Failed registering for block notifications: {:?}", e),
110+
Payload::NotifyNewBlockTemplateResponse(res) => match res.error {
111+
None => info!("Registered for new template notifications"),
112+
Some(e) => error!("Failed registering for new template notifications: {:?}", e),
106113
},
107114
msg => info!("Got unknown msg: {:?}", msg),
108115
}

src/kaspad_messages.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
pow::{self, HeaderHasher},
33
proto::{
44
kaspad_message::Payload, GetBlockTemplateRequestMessage, GetInfoRequestMessage, KaspadMessage,
5-
NotifyBlockAddedRequestMessage, RpcBlock, SubmitBlockRequestMessage,
5+
NotifyBlockAddedRequestMessage, RpcBlock, SubmitBlockRequestMessage, NotifyNewBlockTemplateRequestMessage,
66
},
77
Hash,
88
};
@@ -21,7 +21,12 @@ impl KaspadMessage {
2121
#[must_use]
2222
#[inline(always)]
2323
pub fn submit_block(block: RpcBlock) -> Self {
24-
KaspadMessage { payload: Some(Payload::SubmitBlockRequest(SubmitBlockRequestMessage { block: Some(block) })) }
24+
KaspadMessage {
25+
payload: Some(Payload::SubmitBlockRequest(SubmitBlockRequestMessage {
26+
block: Some(block),
27+
allow_non_daa_blocks: false,
28+
})),
29+
}
2530
}
2631
}
2732

@@ -45,6 +50,12 @@ impl From<GetBlockTemplateRequestMessage> for KaspadMessage {
4550
}
4651
}
4752

53+
impl From<NotifyNewBlockTemplateRequestMessage> for KaspadMessage {
54+
fn from(a: NotifyNewBlockTemplateRequestMessage) -> Self {
55+
KaspadMessage { payload: Some(Payload::NotifyNewBlockTemplateRequest(a)) }
56+
}
57+
}
58+
4859
impl RpcBlock {
4960
#[must_use]
5061
#[inline(always)]

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::{
1414
};
1515

1616
use crate::{
17-
cli::Opt, client::KaspadHandler, miner::MinerManager, proto::NotifyBlockAddedRequestMessage, target::Uint256,
17+
cli::Opt, client::KaspadHandler, miner::MinerManager, proto::NotifyNewBlockTemplateRequestMessage, target::Uint256,
1818
};
1919

2020
mod cli;
@@ -89,7 +89,7 @@ async fn main() -> Result<(), Error> {
8989
devfund_address
9090
);
9191
}
92-
client.client_send(NotifyBlockAddedRequestMessage {}).await?;
92+
client.client_send(NotifyNewBlockTemplateRequestMessage {}).await?;
9393
client.client_get_block_template().await?;
9494

9595
let mut miner_manager =

0 commit comments

Comments
 (0)