Skip to content

Commit

Permalink
Add sink to get_block_dag_info RPC call (kaspanet#306)
Browse files Browse the repository at this point in the history
* Add sink to get_block_dag_info RPC call

* Convert selected tip to sink in the rest of the RPC API
  • Loading branch information
someone235 authored Oct 31, 2023
1 parent c9d5958 commit 2388d11
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cli/src/modules/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ impl Rpc {
let result = rpc.get_peer_addresses_call(GetPeerAddressesRequest {}).await?;
self.println(&ctx, result);
}
RpcApiOps::GetSelectedTipHash => {
let result = rpc.get_selected_tip_hash_call(GetSelectedTipHashRequest {}).await?;
RpcApiOps::GetSink => {
let result = rpc.get_sink_call(GetSinkRequest {}).await?;
self.println(&ctx, result);
}
// RpcApiOps::GetMempoolEntry => {
Expand Down
2 changes: 1 addition & 1 deletion rpc/core/src/api/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum RpcApiOps {
/// Returns a list of all the addresses (IP, port) this Kaspad knows and a list of all addresses that are currently banned by this Kaspad
GetPeerAddresses,
/// Returns the hash of the current selected tip block of the DAG
GetSelectedTipHash,
GetSink,
/// Get information about an entry in the node's mempool
GetMempoolEntry,
/// Get a snapshot of the node's mempool
Expand Down
6 changes: 3 additions & 3 deletions rpc/core/src/api/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ pub trait RpcApi: Sync + Send + AnySync {
async fn get_peer_addresses_call(&self, request: GetPeerAddressesRequest) -> RpcResult<GetPeerAddressesResponse>;

/// requests the hash of the current virtual's selected parent.
async fn get_selected_tip_hash(&self) -> RpcResult<GetSelectedTipHashResponse> {
self.get_selected_tip_hash_call(GetSelectedTipHashRequest {}).await
async fn get_sink(&self) -> RpcResult<GetSinkResponse> {
self.get_sink_call(GetSinkRequest {}).await
}
async fn get_selected_tip_hash_call(&self, request: GetSelectedTipHashRequest) -> RpcResult<GetSelectedTipHashResponse>;
async fn get_sink_call(&self, request: GetSinkRequest) -> RpcResult<GetSinkResponse>;

/// Requests information about a specific transaction in the mempool.
async fn get_mempool_entry(
Expand Down
13 changes: 8 additions & 5 deletions rpc/core/src/model/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,17 @@ impl GetPeerAddressesResponse {

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize, BorshSchema)]
#[serde(rename_all = "camelCase")]
pub struct GetSelectedTipHashRequest {}
pub struct GetSinkRequest {}

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize, BorshSchema)]
#[serde(rename_all = "camelCase")]
pub struct GetSelectedTipHashResponse {
pub selected_tip_hash: RpcHash,
pub struct GetSinkResponse {
pub sink: RpcHash,
}

impl GetSelectedTipHashResponse {
impl GetSinkResponse {
pub fn new(selected_tip_hash: RpcHash) -> Self {
Self { selected_tip_hash }
Self { sink: selected_tip_hash }
}
}

Expand Down Expand Up @@ -398,6 +398,7 @@ pub struct GetBlockDagInfoResponse {
pub virtual_parent_hashes: Vec<RpcHash>,
pub pruning_point_hash: RpcHash,
pub virtual_daa_score: u64,
pub sink: RpcHash,
}

impl GetBlockDagInfoResponse {
Expand All @@ -411,6 +412,7 @@ impl GetBlockDagInfoResponse {
virtual_parent_hashes: Vec<RpcHash>,
pruning_point_hash: RpcHash,
virtual_daa_score: u64,
sink: RpcHash,
) -> Self {
Self {
network,
Expand All @@ -422,6 +424,7 @@ impl GetBlockDagInfoResponse {
virtual_parent_hashes,
pruning_point_hash,
virtual_daa_score,
sink,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rpc/grpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl RpcApi for GrpcClient {
route!(get_info_call, GetInfo);
route!(get_current_network_call, GetCurrentNetwork);
route!(get_peer_addresses_call, GetPeerAddresses);
route!(get_selected_tip_hash_call, GetSelectedTipHash);
route!(get_sink_call, GetSink);
route!(get_mempool_entry_call, GetMempoolEntry);
route!(get_mempool_entries_call, GetMempoolEntries);
route!(get_connected_peer_info_call, GetConnectedPeerInfo);
Expand Down
4 changes: 2 additions & 2 deletions rpc/grpc/core/proto/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ message KaspadRequest {
NotifyBlockAddedRequestMessage notifyBlockAddedRequest = 1007;
// BlockAddedNotificationMessage blockAddedNotification = 1009;
GetPeerAddressesRequestMessage getPeerAddressesRequest = 1010;
GetSelectedTipHashRequestMessage getSelectedTipHashRequest = 1012;
GetSinkRequestMessage GetSinkRequest = 1012;
GetMempoolEntryRequestMessage getMempoolEntryRequest = 1014;
GetConnectedPeerInfoRequestMessage getConnectedPeerInfoRequest = 1016;
AddPeerRequestMessage addPeerRequest = 1018;
Expand Down Expand Up @@ -68,7 +68,7 @@ message KaspadResponse {
NotifyBlockAddedResponseMessage notifyBlockAddedResponse = 1008;
BlockAddedNotificationMessage blockAddedNotification = 1009;
GetPeerAddressesResponseMessage getPeerAddressesResponse = 1011;
GetSelectedTipHashResponseMessage getSelectedTipHashResponse = 1013;
GetSinkResponseMessage GetSinkResponse = 1013;
GetMempoolEntryResponseMessage getMempoolEntryResponse = 1015;
GetConnectedPeerInfoResponseMessage getConnectedPeerInfoResponse = 1017;
AddPeerResponseMessage addPeerResponse = 1019;
Expand Down
9 changes: 5 additions & 4 deletions rpc/grpc/core/proto/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ message GetPeerAddressesKnownAddressMessage {
string Addr = 1;
}

// GetSelectedTipHashRequestMessage requests the hash of the current virtual's
// GetSinkRequestMessage requests the hash of the current virtual's
// selected parent.
message GetSelectedTipHashRequestMessage{
message GetSinkRequestMessage{
}

message GetSelectedTipHashResponseMessage{
string selectedTipHash = 1;
message GetSinkResponseMessage{
string sink = 1;
RPCError error = 1000;
}

Expand Down Expand Up @@ -425,6 +425,7 @@ message GetBlockDagInfoResponseMessage{
repeated string virtualParentHashes = 7;
string pruningPointHash = 8;
uint64 virtualDaaScore = 9;
string sink = 10;
RPCError error = 1000;
}

Expand Down
4 changes: 2 additions & 2 deletions rpc/grpc/core/src/convert/kaspad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod kaspad_request_convert {

impl_into_kaspad_request!(GetCurrentNetwork);
impl_into_kaspad_request!(GetPeerAddresses);
impl_into_kaspad_request!(GetSelectedTipHash);
impl_into_kaspad_request!(GetSink);
impl_into_kaspad_request!(GetMempoolEntry);
impl_into_kaspad_request!(GetMempoolEntries);
impl_into_kaspad_request!(GetConnectedPeerInfo);
Expand Down Expand Up @@ -158,7 +158,7 @@ pub mod kaspad_response_convert {
impl_into_kaspad_response!(GetCurrentNetwork);

impl_into_kaspad_response!(GetPeerAddresses);
impl_into_kaspad_response!(GetSelectedTipHash);
impl_into_kaspad_response!(GetSink);
impl_into_kaspad_response!(GetMempoolEntry);
impl_into_kaspad_response!(GetMempoolEntries);
impl_into_kaspad_response!(GetConnectedPeerInfo);
Expand Down
14 changes: 8 additions & 6 deletions rpc/grpc/core/src/convert/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ from!(item: RpcResult<&kaspa_rpc_core::GetPeerAddressesResponse>, protowire::Get
}
});

from!(&kaspa_rpc_core::GetSelectedTipHashRequest, protowire::GetSelectedTipHashRequestMessage);
from!(item: RpcResult<&kaspa_rpc_core::GetSelectedTipHashResponse>, protowire::GetSelectedTipHashResponseMessage, {
Self { selected_tip_hash: item.selected_tip_hash.to_string(), error: None }
from!(&kaspa_rpc_core::GetSinkRequest, protowire::GetSinkRequestMessage);
from!(item: RpcResult<&kaspa_rpc_core::GetSinkResponse>, protowire::GetSinkResponseMessage, {
Self { sink: item.sink.to_string(), error: None }
});

from!(item: &kaspa_rpc_core::GetMempoolEntryRequest, protowire::GetMempoolEntryRequestMessage, {
Expand Down Expand Up @@ -267,6 +267,7 @@ from!(item: RpcResult<&kaspa_rpc_core::GetBlockDagInfoResponse>, protowire::GetB
virtual_parent_hashes: item.virtual_parent_hashes.iter().map(|x| x.to_string()).collect(),
pruning_point_hash: item.pruning_point_hash.to_string(),
virtual_daa_score: item.virtual_daa_score,
sink: item.sink.to_string(),
error: None,
}
});
Expand Down Expand Up @@ -501,9 +502,9 @@ try_from!(item: &protowire::GetPeerAddressesResponseMessage, RpcResult<kaspa_rpc
}
});

try_from!(&protowire::GetSelectedTipHashRequestMessage, kaspa_rpc_core::GetSelectedTipHashRequest);
try_from!(item: &protowire::GetSelectedTipHashResponseMessage, RpcResult<kaspa_rpc_core::GetSelectedTipHashResponse>, {
Self { selected_tip_hash: RpcHash::from_str(&item.selected_tip_hash)? }
try_from!(&protowire::GetSinkRequestMessage, kaspa_rpc_core::GetSinkRequest);
try_from!(item: &protowire::GetSinkResponseMessage, RpcResult<kaspa_rpc_core::GetSinkResponse>, {
Self { sink: RpcHash::from_str(&item.sink)? }
});

try_from!(item: &protowire::GetMempoolEntryRequestMessage, kaspa_rpc_core::GetMempoolEntryRequest, {
Expand Down Expand Up @@ -607,6 +608,7 @@ try_from!(item: &protowire::GetBlockDagInfoResponseMessage, RpcResult<kaspa_rpc_
virtual_parent_hashes: item.virtual_parent_hashes.iter().map(|x| RpcHash::from_str(x)).collect::<Result<Vec<_>, _>>()?,
pruning_point_hash: RpcHash::from_str(&item.pruning_point_hash)?,
virtual_daa_score: item.virtual_daa_score,
sink: item.sink.parse()?,
}
});

Expand Down
2 changes: 1 addition & 1 deletion rpc/grpc/core/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub enum KaspadPayloadOps {
GetInfo,
Shutdown,
GetPeerAddresses,
GetSelectedTipHash,
GetSink,
GetMempoolEntry,
GetMempoolEntries,
GetConnectedPeerInfo,
Expand Down
2 changes: 1 addition & 1 deletion rpc/grpc/server/src/request_handler/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Factory {
GetInfo,
Shutdown,
GetPeerAddresses,
GetSelectedTipHash,
GetSink,
GetMempoolEntry,
GetMempoolEntries,
GetConnectedPeerInfo,
Expand Down
2 changes: 1 addition & 1 deletion rpc/grpc/server/src/tests/rpc_core_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl RpcApi for RpcCoreMock {
Err(RpcError::NotImplemented)
}

async fn get_selected_tip_hash_call(&self, _request: GetSelectedTipHashRequest) -> RpcResult<GetSelectedTipHashResponse> {
async fn get_sink_call(&self, _request: GetSinkRequest) -> RpcResult<GetSinkResponse> {
Err(RpcError::NotImplemented)
}

Expand Down
5 changes: 3 additions & 2 deletions rpc/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ impl RpcApi for RpcCoreService {
Err(RpcError::NotImplemented)
}

async fn get_selected_tip_hash_call(&self, _: GetSelectedTipHashRequest) -> RpcResult<GetSelectedTipHashResponse> {
Ok(GetSelectedTipHashResponse::new(self.consensus_manager.consensus().unguarded_session().async_get_sink().await))
async fn get_sink_call(&self, _: GetSinkRequest) -> RpcResult<GetSinkResponse> {
Ok(GetSinkResponse::new(self.consensus_manager.consensus().unguarded_session().async_get_sink().await))
}

async fn get_sink_blue_score_call(&self, _: GetSinkBlueScoreRequest) -> RpcResult<GetSinkBlueScoreResponse> {
Expand Down Expand Up @@ -543,6 +543,7 @@ impl RpcApi for RpcCoreService {
session.async_get_virtual_parents().await.iter().copied().collect::<Vec<_>>(),
session.async_pruning_point().await,
session.async_get_virtual_daa_score().await,
session.async_get_sink().await,
))
}

Expand Down
2 changes: 1 addition & 1 deletion rpc/wrpc/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ impl RpcApi for KaspaRpcClient {
GetMempoolEntry,
GetPeerAddresses,
GetMetrics,
GetSelectedTipHash,
GetSink,
GetSyncStatus,
GetSubnetwork,
GetUtxosByAddresses,
Expand Down
2 changes: 1 addition & 1 deletion rpc/wrpc/client/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ build_wrpc_wasm_bindgen_interface!(
GetInfo,
GetPeerAddresses,
GetMetrics,
GetSelectedTipHash,
GetSink,
GetSinkBlueScore,
Ping,
Shutdown,
Expand Down
2 changes: 1 addition & 1 deletion rpc/wrpc/server/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Router {
GetMempoolEntry,
GetPeerAddresses,
GetMetrics,
GetSelectedTipHash,
GetSink,
GetSubnetwork,
GetSyncStatus,
GetUtxosByAddresses,
Expand Down
6 changes: 5 additions & 1 deletion testing/integration/src/daemon_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@ async fn daemon_mining_test() {
assert_eq!(rpc_client2.get_connected_peer_info().await.unwrap().peer_info.len(), 1);

// Mine 10 blocks to daemon #1
let mut last_block_hash = None;
for _ in 0..10 {
let template = rpc_client1
.get_block_template(Address::new(kaspad1.network.into(), kaspa_addresses::Version::PubKey, &[0; 32]), vec![])
.await
.unwrap();
last_block_hash = Some(template.block.header.hash);
rpc_client1.submit_block(template.block, false).await.unwrap();
}

tokio::time::sleep(Duration::from_secs(1)).await;
// Expect the blocks to be relayed to daemon #2
assert_eq!(rpc_client2.get_block_dag_info().await.unwrap().block_count, 10);
let dag_info = rpc_client2.get_block_dag_info().await.unwrap();
assert_eq!(dag_info.block_count, 10);
assert_eq!(dag_info.sink, last_block_hash.unwrap());

// Check that acceptance data contains the expected coinbase tx ids
let vc = rpc_client2.get_virtual_chain_from_block(kaspa_consensus::params::SIMNET_GENESIS.hash, true).await.unwrap();
Expand Down

0 comments on commit 2388d11

Please sign in to comment.