From a307f828c98ae7515e65b2d2ea7d58ffcac8f693 Mon Sep 17 00:00:00 2001 From: Deniod Date: Sat, 13 Jan 2024 22:41:02 +0100 Subject: [PATCH] less debug, handle error if channel is closed --- lib/src/grpcconnector.rs | 70 +++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/lib/src/grpcconnector.rs b/lib/src/grpcconnector.rs index 63db47a..f7d9894 100644 --- a/lib/src/grpcconnector.rs +++ b/lib/src/grpcconnector.rs @@ -122,21 +122,20 @@ where F : Fn(&[u8], u64) { while let Ok(Some(block)) = rx.recv() { use prost::Message; let mut encoded_buf = vec![]; - - match block.encode(&mut encoded_buf) { - Ok(_) => c(&encoded_buf, block.height), - Err(e) => { - eprintln!("Error encoding block: {:?}", e); - break; - } + + if let Err(e) = block.encode(&mut encoded_buf) { + eprintln!("Error encoding block: {:?}", e); + break; } + + c(&encoded_buf, block.height); } - + if let Err(e) = ftx.send(Ok(())) { eprintln!("Error sending completion signal: {:?}", e); } }); - + let mut response = client.get_block_range(request).await?.into_inner(); while let Some(block) = response.message().await? { @@ -178,25 +177,44 @@ pub fn fetch_blocks(uri: &http::Uri, start_heig } } - // get_address_txids GRPC call -async fn get_address_txids(uri: &http::Uri, address: String, - start_height: u64, end_height: u64, no_cert: bool, c: F) -> Result<(), Box> - where F : Fn(&[u8], u64) { +async fn get_address_txids( + uri: &http::Uri, + address: String, + start_height: u64, + end_height: u64, + no_cert: bool, + c: F +) -> Result<(), Box> +where F : Fn(&[u8], u64) { + + let mut client = match get_client(uri, no_cert).await { + Ok(client) => client, + Err(e) => { + eprintln!("Error creating client: {:?}", e); + return Err(e.into()); + } + }; - let mut client = get_client(uri, no_cert).await?; let start = Some(BlockId{ height: start_height, hash: vec!()}); - let end = Some(BlockId{ height: end_height, hash: vec!()}); + let end = Some(BlockId{ height: end_height, hash: vec!()}); - let request = Request::new(TransparentAddressBlockFilter{ address, range: Some(BlockRange{start, end}) }); + let request = Request::new(TransparentAddressBlockFilter{ address, range: Some(BlockRange{ start, end }) }); + + let maybe_response = match client.get_address_txids(request).await { + Ok(response) => response, + Err(e) => { + eprintln!("Error getting address txids: {:?}", e); + return Err(e.into()); + } + }; - let maybe_response = client.get_address_txids(request).await?; let mut response = maybe_response.into_inner(); while let Some(tx) = response.message().await? { c(&tx.data, tx.height); } - + Ok(()) } @@ -234,10 +252,16 @@ where Ok(()) } -pub fn fetch_transparent_txids(uri: &http::Uri, address: String, - start_height: u64, end_height: u64, no_cert: bool, c: F) -> Result<(), String> - where F : Fn(&[u8], u64) { - +pub fn fetch_transparent_txids( + uri: &http::Uri, + address: String, + start_height: u64, + end_height: u64, + no_cert: bool, + c: F +) -> Result<(), String> +where F : Fn(&[u8], u64) { + let mut rt = match tokio::runtime::Runtime::new() { Ok(r) => r, Err(e) => { @@ -252,7 +276,7 @@ pub fn fetch_transparent_txids(uri: &http::Uri, Err(e) => { let e = format!("Error with get_address_txids runtime {:?}", e); error!("{}", e); - Err(e) + return Err(e) } } }