use bitcoin::hashes::hex::ToHex;
use bitcoin::secp256k1::{PublicKey, SecretKey};
-use futures::executor;
use lightning;
use lightning::ln::peer_handler::{
ErroringMessageHandler, IgnoringMessageHandler, MessageHandler, PeerManager,
let mut connected_peer_count = 0;
for current_peer in peers {
- let initial_connection_succeeded = monitor_peer_connection(current_peer, Arc::clone(&arc_peer_handler));
+ let initial_connection_succeeded = connect_peer(current_peer, Arc::clone(&arc_peer_handler)).await;
if initial_connection_succeeded {
connected_peer_count += 1;
}
});
}
-fn monitor_peer_connection(current_peer: (PublicKey, SocketAddr), peer_manager: GossipPeerManager) -> bool {
- let peer_manager_clone = Arc::clone(&peer_manager);
- eprintln!("Connecting to peer {}@{}…", current_peer.0.to_hex(), current_peer.1.to_string());
- let connection = executor::block_on(async move {
- lightning_net_tokio::connect_outbound(
- peer_manager_clone,
- current_peer.0,
- current_peer.1,
- ).await
- });
- let mut initial_connection_succeeded = false;
+async fn connect_peer(current_peer: (PublicKey, SocketAddr), peer_manager: GossipPeerManager) -> bool {
+ eprintln!("Connecting to peer {}@{}...", current_peer.0.to_hex(), current_peer.1.to_string());
+ let connection = lightning_net_tokio::connect_outbound(
+ Arc::clone(&peer_manager),
+ current_peer.0,
+ current_peer.1,
+ ).await;
if let Some(disconnection_future) = connection {
eprintln!("Connected to peer {}@{}!", current_peer.0.to_hex(), current_peer.1.to_string());
- initial_connection_succeeded = true;
- let peer_manager_clone = Arc::clone(&peer_manager);
tokio::spawn(async move {
disconnection_future.await;
- eprintln!("Disconnected from peer {}@{}", current_peer.0.to_hex(), current_peer.1.to_string());
- monitor_peer_connection(current_peer.clone(), peer_manager_clone);
+ loop {
+ eprintln!("Reconnecting to peer {}@{}...", current_peer.0.to_hex(), current_peer.1.to_string());
+ if let Some(disconnection_future) = lightning_net_tokio::connect_outbound(
+ Arc::clone(&peer_manager),
+ current_peer.0,
+ current_peer.1,
+ ).await {
+ disconnection_future.await;
+ } else {
+ tokio::time::sleep(Duration::from_secs(10)).await;
+ }
+ }
});
+ true
} else {
- eprintln!("Failed to connect to peer {}@{}", current_peer.0.to_hex(), current_peer.1.to_string())
- };
- initial_connection_succeeded
+ eprintln!("Failed to connect to peer {}@{}", current_peer.0.to_hex(), current_peer.1.to_string());
+ false
+ }
}
use bitcoin::{BlockHash, TxOut};
use bitcoin::blockdata::block::Block;
use bitcoin::hashes::Hash;
-use futures::executor;
use lightning::chain;
use lightning::chain::AccessError;
use lightning_block_sync::BlockSource;
fn retrieve_block(&self, block_height: u32) -> Result<Block, AccessError> {
let rest_client = self.rest_client.clone();
- executor::block_on(async move {
+ tokio::task::block_in_place(move || { tokio::runtime::Handle::current().block_on(async move {
let block_hash_result = rest_client.request_resource::<BinaryResponse, RestBinaryResponse>(&format!("blockhashbyheight/{}.bin", block_height)).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());
AccessError::UnknownChain
})?;
Ok(block)
- })
+ }) })
}
}