Skip to content
38 changes: 25 additions & 13 deletions crates/floresta-node/src/florestad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,13 +694,24 @@ impl Florestad {
} else {
match data.unwrap_err() {
FlorestadError::TomlParsing(e) => {
warn!("Could not parse config file, ignoring it");
debug!("{e}");
warn!(
"Could not parse config file {}, ignoring it: {e}",
path.display()
);
ConfigFile::default()
}
// No file at the default path is the common case, not a problem
FlorestadError::Io(e)
if config_file.is_none() && e.kind() == std::io::ErrorKind::NotFound =>
{
debug!("No config file at {}, using defaults", path.display());
ConfigFile::default()
}
FlorestadError::Io(e) => {
warn!("Could not read config file, ignoring it");
debug!("{e}");
warn!(
"Could not read config file {}, ignoring it: {e}",
path.display()
);
ConfigFile::default()
}
// Shouldn't be any other error
Expand Down Expand Up @@ -739,13 +750,14 @@ impl Florestad {
fn setup_wallet(&self) -> Result<AddressCache<KvDatabase>, FlorestadError> {
let wallet =
AddressCache::<KvDatabase>::new_default(&self.config.datadir, ConsensusMerkle)?;
let config_file = self.get_config_file();

wallet
.setup()
.map_err(FlorestadError::CouldNotInitializeWallet)?;

// Add the configured descriptors and addresses to the wallet
for descriptor in self.get_descriptors() {
for descriptor in self.get_descriptors(&config_file) {
match wallet.push_descriptor(&descriptor) {
Ok(_) => info!("Added descriptor to wallet: {descriptor}"),
Err(WatchOnlyError::DuplicateDescriptor { descriptor: _ }) => {
Expand All @@ -757,7 +769,7 @@ impl Florestad {
}
}

for xpub in self.get_xpubs() {
for xpub in self.get_xpubs(&config_file) {
match wallet.push_xpub(&xpub, self.config.network) {
Ok(()) => info!("Added xpubs to wallet: {xpub}"),
Err(WatchOnlyError::DuplicateDescriptor { descriptor: _ }) => warn!(
Expand All @@ -767,7 +779,7 @@ impl Florestad {
}
}

for address in self.get_addresses()? {
for address in self.get_addresses(&config_file)? {
wallet.cache_address(address)?;
}

Expand All @@ -776,31 +788,31 @@ impl Florestad {
}

/// Get the wallet descriptors from the config file
fn get_descriptors(&self) -> Vec<String> {
fn get_descriptors(&self, config_file: &ConfigFile) -> Vec<String> {
self.config
.wallet_descriptor
.iter()
.flatten()
.chain(self.get_config_file().wallet.descriptors.iter().flatten())
.chain(config_file.wallet.descriptors.iter().flatten())
.cloned()
.collect()
}

/// Get the wallet xpubs from the config file and the environment
fn get_xpubs(&self) -> Vec<String> {
fn get_xpubs(&self, config_file: &ConfigFile) -> Vec<String> {
self.config
.wallet_xpub
.iter()
.flatten()
.chain(self.get_config_file().wallet.xpubs.iter().flatten())
.chain(config_file.wallet.xpubs.iter().flatten())
.chain(Self::get_key_from_env().iter())
.cloned()
.collect()
}

/// Get the wallet addresses from the config file
fn get_addresses(&self) -> Result<Vec<ScriptBuf>, FlorestadError> {
self.get_config_file()
fn get_addresses(&self, config_file: &ConfigFile) -> Result<Vec<ScriptBuf>, FlorestadError> {
config_file
.wallet
.addresses
.as_deref()
Expand Down
45 changes: 27 additions & 18 deletions crates/floresta-wire/src/p2p_wire/address_man.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use rand::seq::IteratorRandom;
use serde::Deserialize;
use serde::Serialize;
use tracing::debug;
use tracing::error;
use tracing::info;
use tracing::warn;

Expand Down Expand Up @@ -649,26 +648,29 @@ impl AddressMan {
Some(proxy) => {
debug!("Performing DNS lookup for host: {host}, using SOCKS5 proxy: {proxy}");
// SOCKS5 proxy lookup (proxied DNS-over-HTTPS).
dns_proxy::lookup_host_via_proxy(host, proxy).unwrap_or_else(|e| {
error!("DNS lookup via SOCKS5 proxy failed: {e}");
Vec::new()
})
dns_proxy::lookup_host_via_proxy(host, proxy).map_err(|e| e.to_string())
}
None => {
debug!("Performing DNS lookup for host: {host}, using the system resolver");
// System lookup (usually unencrypted, resolver sees both query and our IP).
dns_lookup::lookup_host(host).unwrap_or_else(|e| {
error!("DNS lookup failed: {e}");
Vec::new()
})
dns_lookup::lookup_host(host).map_err(|e| e.to_string())
}
};

if ips.is_empty() {
warn!("No peer addresses read from DNS host: {host}");
} else {
info!("Fetched {} peer addresses from DNS host: {host}", ips.len());
}
let ips = match ips {
Ok(ips) if ips.is_empty() => {
warn!("No peer addresses read from DNS host: {host}");
ips
}
Ok(ips) => {
info!("Fetched {} peer addresses from DNS host: {host}", ips.len());
ips
}
Err(e) => {
warn!("DNS lookup failed for host {host}: {e}");
Vec::new()
}
};

let mut addresses = Vec::new();
for ip in ips {
Expand Down Expand Up @@ -883,11 +885,18 @@ impl AddressMan {
.collect::<Vec<_>>())
});

if anchors.is_err() {
warn!("Failed to init Utreexo peers: anchors.json does not exist yet, or is invalid");
match anchors {
Ok(anchors) => anchors,
// No utreexo peer was saved yet, e.g. on a fresh start
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
debug!("No anchors.json yet, no Utreexo peers to reconnect to");
Vec::new()
}
Err(e) => {
warn!("Failed to init Utreexo peers from anchors.json: {e}");
Vec::new()
}
}

anchors.unwrap_or_default()
}

/// This function moves addresses between buckets, like if the ban time of a peer expired,
Expand Down
6 changes: 5 additions & 1 deletion crates/floresta-wire/src/p2p_wire/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use tokio::sync::mpsc::UnboundedReceiver;
use tokio::sync::mpsc::UnboundedSender;
use tokio::sync::mpsc::unbounded_channel;
use tokio::sync::oneshot;
use tracing::debug;
use tracing::info;

use super::UtreexoNodeConfig;
Expand Down Expand Up @@ -481,7 +482,10 @@ where
info!("Shutting down node...");
try_and_warn!(self.save_utreexo_peers());
for peer in self.peer_ids.iter() {
try_and_log!(self.send_to_peer(*peer, NodeRequest::Shutdown));
// The peer may already be gone, which is fine since we're shutting down anyway
if let Err(e) = self.send_to_peer(*peer, NodeRequest::Shutdown) {
debug!("Could not send shutdown to peer {peer}: {e:?}");
}
}
try_and_log!(self.save_peers());
try_and_log!(self.chain.flush());
Expand Down
2 changes: 1 addition & 1 deletion crates/floresta-wire/src/p2p_wire/node/peer_man.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ where
.map(|peer| peer.address.clone())
.collect();
if utreexo_peers.is_empty() {
warn!("No connected Utreexo peers to save to disk");
debug!("No connected Utreexo peers to save to disk");
return Ok(());
}
info!("Saving utreexo peers to disk...");
Expand Down
11 changes: 9 additions & 2 deletions crates/floresta-wire/src/p2p_wire/node/sync_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ where
self.last_block_request += 1;
}

try_and_log!(self.request_blocks(blocks));
match self.request_blocks(blocks) {
// Transient, e.g. right after startup or the assume-utreexo jump
Err(WireError::NoPeersAvailable) => debug!("No peer to request blocks from yet"),
result => try_and_log!(result),
}
}

fn ask_for_missed_blocks(&mut self) -> Result<(), WireError> {
Expand Down Expand Up @@ -252,7 +256,10 @@ where
}

// Ask for missed blocks or proofs if they are no longer inflight or pending
try_and_log!(self.ask_for_missed_blocks());
match self.ask_for_missed_blocks() {
Err(WireError::NoPeersAvailable) => debug!("No peer to request missed blocks from yet"),
result => try_and_log!(result),
}
try_and_log!(self.ask_for_missed_proofs());

self.get_blocks_to_download();
Expand Down
12 changes: 10 additions & 2 deletions crates/floresta-wire/src/p2p_wire/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,11 @@ impl<T: AsyncWrite + Unpin + Send + Sync> Peer<T> {
self.handle_version(version).await?;
}
_ => {
warn!("unexpected message: {:?} from peer {}", message, self.id);
debug!(
"unexpected message: {} from peer {}",
message.command(),
self.id
);
return Err(PeerError::UnexpectedMessage);
}
},
Expand Down Expand Up @@ -720,7 +724,11 @@ impl<T: AsyncWrite + Unpin + Send + Sync> Peer<T> {
}
bitcoin::p2p::message::NetworkMessage::WtxidRelay => {}
_ => {
warn!("unexpected message: {:?} from peer {}", message, self.id);
debug!(
"unexpected message: {} from peer {}",
message.command(),
self.id
);
return Err(PeerError::UnexpectedMessage);
}
},
Expand Down
Loading