From 0ef2cd3f234ce2603da0dd49d3f3ba88333794a1 Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Fri, 17 Dec 2021 13:35:05 +0200 Subject: [PATCH 1/2] add grpc call for search for commitment changed to bytes refactor variable name fix UI feedback --- .../tari_app_grpc/proto/base_node.proto | 7 +++ .../tari_base_node/src/command_handler.rs | 2 +- .../src/grpc/base_node_grpc_server.rs | 59 ++++++++++++++++++- .../comms_interface/inbound_handlers.rs | 19 +++--- 4 files changed, 76 insertions(+), 11 deletions(-) diff --git a/applications/tari_app_grpc/proto/base_node.proto b/applications/tari_app_grpc/proto/base_node.proto index c1e3a2c5a7..264759f256 100644 --- a/applications/tari_app_grpc/proto/base_node.proto +++ b/applications/tari_app_grpc/proto/base_node.proto @@ -68,6 +68,8 @@ service BaseNode { rpc GetTipInfo(Empty) returns (TipInfoResponse); // Search for blocks containing the specified kernels rpc SearchKernels(SearchKernelsRequest) returns (stream HistoricalBlock); + // Search for blocks containing the specified commitments + rpc SearchUtxos(SearchUtxosRequest) returns (stream HistoricalBlock); // Fetch any utxos that exist in the main chain rpc FetchMatchingUtxos(FetchMatchingUtxosRequest) returns (stream FetchMatchingUtxosResponse); // get all peers from the base node @@ -289,6 +291,11 @@ message SearchKernelsRequest{ repeated Signature signatures = 1; } +// This is the request type for the Search Utxo rpc +message SearchUtxosRequest{ + repeated bytes commitments = 1; +} + message FetchMatchingUtxosRequest { repeated bytes hashes = 1; } diff --git a/applications/tari_base_node/src/command_handler.rs b/applications/tari_base_node/src/command_handler.rs index ce5d9f35e8..6d83a4d368 100644 --- a/applications/tari_base_node/src/command_handler.rs +++ b/applications/tari_base_node/src/command_handler.rs @@ -336,7 +336,7 @@ impl CommandHandler { Ok(mut data) => match data.pop() { Some(v) => println!("{}", v.block()), _ => println!( - "Pruned node: utxo found, but block not found for utxo commitment {}", + "Block not found for utxo commitment {}", commitment.to_hex() ), }, diff --git a/applications/tari_base_node/src/grpc/base_node_grpc_server.rs b/applications/tari_base_node/src/grpc/base_node_grpc_server.rs index d9a237e069..417a191a77 100644 --- a/applications/tari_base_node/src/grpc/base_node_grpc_server.rs +++ b/applications/tari_base_node/src/grpc/base_node_grpc_server.rs @@ -32,7 +32,7 @@ use tari_app_grpc::{ tari_rpc::{CalcType, Sorting}, }; use tari_app_utilities::consts; -use tari_common_types::types::Signature; +use tari_common_types::types::{Commitment, Signature}; use tari_comms::{Bytes, CommsNode}; use tari_core::{ base_node::{ @@ -122,6 +122,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer { type GetTokensInCirculationStream = mpsc::Receiver>; type ListHeadersStream = mpsc::Receiver>; type SearchKernelsStream = mpsc::Receiver>; + type SearchUtxosStream = mpsc::Receiver>; async fn get_network_difficulty( &self, @@ -794,6 +795,62 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer { Ok(Response::new(rx)) } + async fn search_utxos( + &self, + request: Request, + ) -> Result, Status> { + debug!(target: LOG_TARGET, "Incoming GRPC request for SearchUtxos"); + let request = request.into_inner(); + + let converted: Result, _> = request + .commitments + .into_iter() + .map(|s| Commitment::from_bytes(&s)) + .collect(); + let outputs = converted.map_err(|_| Status::internal("Failed to convert one or more arguments."))?; + + let mut handler = self.node_service.clone(); + + let (mut tx, rx) = mpsc::channel(GET_BLOCKS_PAGE_SIZE); + task::spawn(async move { + let blocks = match handler.fetch_blocks_with_utxos(outputs).await { + Err(err) => { + warn!( + target: LOG_TARGET, + "Error communicating with local base node: {:?}", err, + ); + return; + }, + Ok(data) => data, + }; + for block in blocks { + match tx + .send( + block + .try_into() + .map_err(|err| Status::internal(format!("Could not provide block:{}", err))), + ) + .await + { + Ok(_) => (), + Err(err) => { + warn!(target: LOG_TARGET, "Error sending header via GRPC: {}", err); + match tx.send(Err(Status::unknown("Error sending data"))).await { + Ok(_) => (), + Err(send_err) => { + warn!(target: LOG_TARGET, "Error sending error to GRPC client: {}", send_err) + }, + } + return; + }, + } + } + }); + + debug!(target: LOG_TARGET, "Sending SearchUtxos response stream to client"); + Ok(Response::new(rx)) + } + #[allow(clippy::useless_conversion)] async fn fetch_matching_utxos( &self, diff --git a/base_layer/core/src/base_node/comms_interface/inbound_handlers.rs b/base_layer/core/src/base_node/comms_interface/inbound_handlers.rs index 9372ec8c5c..5e3894e57b 100644 --- a/base_layer/core/src/base_node/comms_interface/inbound_handlers.rs +++ b/base_layer/core/src/base_node/comms_interface/inbound_handlers.rs @@ -292,24 +292,25 @@ where T: BlockchainBackend + 'static } Ok(NodeCommsResponse::HistoricalBlocks(blocks)) }, - NodeCommsRequest::FetchBlocksWithUtxos(hashes) => { - let mut blocks = Vec::with_capacity(hashes.len()); - for hash in hashes { - let hash_hex = hash.to_hex(); + NodeCommsRequest::FetchBlocksWithUtxos(commitments) => { + let mut blocks = Vec::with_capacity(commitments.len()); + for commitment in commitments { + let commitment_hex = commitment.to_hex(); debug!( target: LOG_TARGET, - "A peer has requested a block with hash {}", hash_hex, + "A peer has requested a block with commitment {}", commitment_hex, ); - match self.blockchain_db.fetch_block_with_utxo(hash).await { + match self.blockchain_db.fetch_block_with_utxo(commitment).await { Ok(Some(block)) => blocks.push(block), Ok(None) => warn!( target: LOG_TARGET, - "Could not provide requested block {} to peer because not stored", hash_hex, + "Could not provide requested block with commitment {} to peer because not stored", + commitment_hex, ), Err(e) => warn!( target: LOG_TARGET, - "Could not provide requested block {} to peer because: {}", - hash_hex, + "Could not provide requested block with commitment {} to peer because: {}", + commitment_hex, e.to_string() ), } From c940e18f1230ae41473c2b237fc2866de470ff0e Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Fri, 17 Dec 2021 16:09:52 +0200 Subject: [PATCH 2/2] cargo fmt --- applications/tari_base_node/src/command_handler.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/applications/tari_base_node/src/command_handler.rs b/applications/tari_base_node/src/command_handler.rs index 6d83a4d368..c67cee2298 100644 --- a/applications/tari_base_node/src/command_handler.rs +++ b/applications/tari_base_node/src/command_handler.rs @@ -335,10 +335,7 @@ impl CommandHandler { }, Ok(mut data) => match data.pop() { Some(v) => println!("{}", v.block()), - _ => println!( - "Block not found for utxo commitment {}", - commitment.to_hex() - ), + _ => println!("Block not found for utxo commitment {}", commitment.to_hex()), }, }; });