Remove println from verifier.rs
authorArik Sosman <git@arik.io>
Thu, 3 Aug 2023 01:34:01 +0000 (18:34 -0700)
committerArik Sosman <git@arik.io>
Thu, 3 Aug 2023 01:34:01 +0000 (18:34 -0700)
src/downloader.rs
src/verifier.rs

index 34a5c66b3ba22d95ce2d648ebe1ff5f086813cb9..8d5cdb0005e7d8e3ea74f5036ee6c20220a9729e 100644 (file)
@@ -42,7 +42,7 @@ pub(crate) struct GossipRouter<L: Deref + Clone + Send + Sync + 'static> where L
 impl<L: Deref + Clone + Send + Sync> GossipRouter<L> where L::Target: Logger {
        pub(crate) fn new(network_graph: Arc<NetworkGraph<L>>, sender: mpsc::Sender<GossipMessage>, logger: L) -> Self {
                let outbound_gossiper = Arc::new(P2PGossipSync::new(Arc::clone(&network_graph), None, logger.clone()));
-               let verifier = Arc::new(ChainVerifier::new(Arc::clone(&network_graph), Arc::clone(&outbound_gossiper)));
+               let verifier = Arc::new(ChainVerifier::new(Arc::clone(&network_graph), Arc::clone(&outbound_gossiper), logger.clone()));
                Self {
                        native_router: P2PGossipSync::new(network_graph, Some(Arc::clone(&verifier)), logger.clone()),
                        outbound_gossiper,
index 872a5010c8955b17a1f6114ab9c63ea57f8ad079..6813ff7d3dd766da51d0ae23779e27f2ea8c76de 100644 (file)
@@ -6,6 +6,7 @@ use std::sync::Mutex;
 use bitcoin::{BlockHash, TxOut};
 use bitcoin::blockdata::block::Block;
 use bitcoin::hashes::Hash;
+use lightning::log_error;
 use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
 use lightning::routing::utxo::{UtxoFuture, UtxoLookup, UtxoResult, UtxoLookupError};
 use lightning::util::logger::Logger;
@@ -21,41 +22,49 @@ pub(crate) struct ChainVerifier<L: Deref + Clone + Send + Sync + 'static> where
        graph: Arc<NetworkGraph<L>>,
        outbound_gossiper: Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Arc<Self>, L>>,
        peer_handler: Mutex<Option<GossipPeerManager<L>>>,
+       logger: L
 }
 
 struct RestBinaryResponse(Vec<u8>);
 
 impl<L: Deref + Clone + Send + Sync + 'static> ChainVerifier<L> where L::Target: Logger {
-       pub(crate) fn new(graph: Arc<NetworkGraph<L>>, outbound_gossiper: Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Arc<Self>, L>>) -> Self {
+       pub(crate) fn new(graph: Arc<NetworkGraph<L>>, outbound_gossiper: Arc<P2PGossipSync<Arc<NetworkGraph<L>>, Arc<Self>, L>>, logger: L) -> Self {
                ChainVerifier {
                        rest_client: Arc::new(RestClient::new(config::bitcoin_rest_endpoint()).unwrap()),
                        outbound_gossiper,
                        graph,
                        peer_handler: Mutex::new(None),
+                       logger
                }
        }
        pub(crate) fn set_ph(&self, peer_handler: GossipPeerManager<L>) {
                *self.peer_handler.lock().unwrap() = Some(peer_handler);
        }
 
-       async fn retrieve_utxo(client: Arc<RestClient>, short_channel_id: u64) -> Result<TxOut, UtxoLookupError> {
+       async fn retrieve_utxo(client: Arc<RestClient>, short_channel_id: u64, logger: L) -> Result<TxOut, UtxoLookupError> {
                let block_height = (short_channel_id >> 5 * 8) as u32; // block height is most significant three bytes
                let transaction_index = ((short_channel_id >> 2 * 8) & 0xffffff) as u32;
                let output_index = (short_channel_id & 0xffff) as u16;
 
-               let mut block = Self::retrieve_block(client, block_height).await?;
-               if transaction_index as usize >= block.txdata.len() { return Err(UtxoLookupError::UnknownTx); }
+               let mut block = Self::retrieve_block(client, block_height, logger.clone()).await?;
+               if transaction_index as usize >= block.txdata.len() {
+                       log_error!(logger, "Could't find transaction {} in block {}", transaction_index, block_height);
+                       return Err(UtxoLookupError::UnknownTx);
+               }
                let mut transaction = block.txdata.swap_remove(transaction_index as usize);
-               if output_index as usize >= transaction.output.len() { return Err(UtxoLookupError::UnknownTx); }
+               if output_index as usize >= transaction.output.len() {
+                       log_error!(logger, "Could't find output {} in transaction {}", output_index, transaction.txid());
+                       return Err(UtxoLookupError::UnknownTx);
+               }
                Ok(transaction.output.swap_remove(output_index as usize))
        }
 
-       async fn retrieve_block(client: Arc<RestClient>, block_height: u32) -> Result<Block, UtxoLookupError> {
+       async fn retrieve_block(client: Arc<RestClient>, block_height: u32, logger: L) -> Result<Block, UtxoLookupError> {
                let uri = format!("blockhashbyheight/{}.bin", block_height);
                let block_hash_result =
                        client.request_resource::<BinaryResponse, RestBinaryResponse>(&uri).await;
                let block_hash: Vec<u8> = block_hash_result.map_err(|error| {
-                       eprintln!("Could't find block hash at height {}: {}", block_height, error.to_string());
+                       log_error!(logger, "Could't find block hash at height {}: {}", block_height, error.to_string());
                        UtxoLookupError::UnknownChain
                })?.0;
                let block_hash = BlockHash::from_slice(&block_hash).unwrap();
@@ -67,7 +76,7 @@ impl<L: Deref + Clone + Send + Sync + 'static> ChainVerifier<L> where L::Target:
                        },
                        Ok(_) => unreachable!(),
                        Err(error) => {
-                               eprintln!("Couldn't retrieve block {}: {:?} ({})", block_height, error, block_hash);
+                               log_error!(logger, "Couldn't retrieve block {}: {:?} ({})", block_height, error, block_hash);
                                Err(UtxoLookupError::UnknownChain)
                        }
                }
@@ -82,8 +91,9 @@ impl<L: Deref + Clone + Send + Sync + 'static> UtxoLookup for ChainVerifier<L> w
                let client_ref = Arc::clone(&self.rest_client);
                let gossip_ref = Arc::clone(&self.outbound_gossiper);
                let pm_ref = self.peer_handler.lock().unwrap().clone();
+               let logger_ref = self.logger.clone();
                tokio::spawn(async move {
-                       let res = Self::retrieve_utxo(client_ref, short_channel_id).await;
+                       let res = Self::retrieve_utxo(client_ref, short_channel_id, logger_ref).await;
                        fut.resolve(&*graph_ref, &*gossip_ref, res);
                        if let Some(pm) = pm_ref { pm.process_events(); }
                });