Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the Warning type #278

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/chain/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<H: HeaderStore> Chain<H> {
let header_opt = db.header_at(height).await;
if header_opt.is_err() {
self.dialog
.send_warning(Warning::FailedPersistance {
.send_warning(Warning::FailedPersistence {
warning: format!(
"Unexpected error fetching a header from the header store at height {height}"
),
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<H: HeaderStore> Chain<H> {
.write(self.header_chain.headers())
.await
{
self.dialog.send_warning(Warning::FailedPersistance {
self.dialog.send_warning(Warning::FailedPersistence {
warning: format!("Could not save headers to disk: {e}"),
});
}
Expand All @@ -263,7 +263,7 @@ impl<H: HeaderStore> Chain<H> {
.write_over(self.header_chain.headers(), height)
.await
{
self.dialog.send_warning(Warning::FailedPersistance {
self.dialog.send_warning(Warning::FailedPersistence {
warning: format!("Could not save headers to disk: {e}"),
});
}
Expand All @@ -280,7 +280,7 @@ impl<H: HeaderStore> Chain<H> {
.map_err(HeaderPersistenceError::Database)?;
if let Some(first) = loaded_headers.values().next() {
if first.prev_blockhash.ne(&self.tip()) {
self.dialog.send_warning(Warning::UnlinkableAnchor);
self.dialog.send_warning(Warning::InvalidStartHeight);
// The header chain did not align, so just start from the anchor
return Err(HeaderPersistenceError::CannotLocateHistory);
} else if loaded_headers
Expand Down Expand Up @@ -886,7 +886,7 @@ impl<H: HeaderStore> Chain<H> {
let mut db = self.db.lock().await;
let range_opt = db.load(range).await;
if range_opt.is_err() {
self.dialog.send_warning(Warning::FailedPersistance {
self.dialog.send_warning(Warning::FailedPersistence {
warning: "Unexpected error fetching a range of headers from the header store"
.to_string(),
});
Expand Down
35 changes: 25 additions & 10 deletions src/core/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,12 @@ impl BlockRequest {
#[derive(Debug, Clone)]
pub enum Warning {
/// The node is looking for connections to peers.
NotEnoughConnections,
NeedConnections {
/// The number of live connections.
connected: usize,
/// The configured requirement.
required: usize,
},
/// A connection to a peer timed out.
PeerTimedOut,
/// The node was unable to connect to a peer in the database.
Expand All @@ -236,13 +241,16 @@ pub enum Warning {
UnsolicitedMessage,
/// The provided anchor is deeper than the database history.
/// Recoverable by deleting the headers from the database or starting from a higher point in the chain.
UnlinkableAnchor,
InvalidStartHeight,
/// The headers in the database do not link together. Recoverable by deleting the database.
CorruptedHeaders,
/// A transaction got rejected, likely for being an insufficient fee or non-standard transaction.
TransactionRejected(RejectPayload),
TransactionRejected {
/// The transaction ID and reject reason, if it exists.
payload: RejectPayload,
},
/// A database failed to persist some data.
FailedPersistance {
FailedPersistence {
/// Additional context for the persistance failure.
warning: String,
},
Expand All @@ -262,10 +270,17 @@ pub enum Warning {
impl core::fmt::Display for Warning {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Warning::NotEnoughConnections => {
write!(f, "Looking for connections to peers.")
Warning::NeedConnections {
connected,
required,
} => {
write!(
f,
"Looking for connections to peers. Connected: {}, Required: {}",
connected, required
)
}
Warning::UnlinkableAnchor => write!(
Warning::InvalidStartHeight => write!(
f,
"The provided anchor is deeper than the database history."
),
Expand All @@ -281,10 +296,10 @@ impl core::fmt::Display for Warning {
"The node has been running for a long duration without receiving new blocks."
)
}
Warning::TransactionRejected(r) => {
write!(f, "A transaction got rejected: TXID {}", r.txid)
Warning::TransactionRejected { payload } => {
write!(f, "A transaction got rejected: TXID {}", payload.txid)
}
Warning::FailedPersistance { warning } => {
Warning::FailedPersistence { warning } => {
write!(f, "A database failed to persist some data: {}", warning)
}
Warning::EvaluatingFork => write!(f, "Peer sent us a potential fork."),
Expand Down
16 changes: 11 additions & 5 deletions src/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
}
PeerMessage::Reject(payload) => {
self.dialog
.send_warning(Warning::TransactionRejected(payload));
.send_warning(Warning::TransactionRejected { payload });
}
PeerMessage::FeeFilter(feerate) => {
let mut peer_map = self.peer_map.lock().await;
Expand Down Expand Up @@ -361,9 +361,14 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
async fn dispatch(&self) -> Result<(), NodeError<H::Error, P::Error>> {
let mut peer_map = self.peer_map.lock().await;
peer_map.clean().await;
let live = peer_map.live();
let required = self.next_required_peers().await;
// Find more peers when lower than the desired threshold.
if peer_map.live() < self.next_required_peers().await {
self.dialog.send_warning(Warning::NotEnoughConnections);
if live < required {
self.dialog.send_warning(Warning::NeedConnections {
connected: live,
required,
});
let address = peer_map.next_peer().await?;
if peer_map.dispatch(address).await.is_err() {
self.dialog.send_warning(Warning::CouldNotConnect);
Expand Down Expand Up @@ -416,8 +421,9 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
if did_broadcast {
self.dialog.send_info(Log::TxSent(txid)).await;
} else {
self.dialog
.send_warning(Warning::TransactionRejected(RejectPayload::from_txid(txid)));
self.dialog.send_warning(Warning::TransactionRejected {
payload: RejectPayload::from_txid(txid),
});
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/peer_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl<P: PeerStore> PeerMap<P> {
))
.await
{
self.dialog.send_warning(Warning::FailedPersistance {
self.dialog.send_warning(Warning::FailedPersistence {
warning: format!(
"Encountered an error adding {:?}:{} flags: {} ... {e}",
peer.addr, peer.port, peer.services
Expand All @@ -357,7 +357,7 @@ impl<P: PeerStore> PeerMap<P> {
))
.await
{
self.dialog.send_warning(Warning::FailedPersistance {
self.dialog.send_warning(Warning::FailedPersistence {
warning: format!(
"Encountered an error adding {:?}:{} flags: {} ... {e}",
peer.address, peer.port, peer.service_flags
Expand All @@ -380,7 +380,7 @@ impl<P: PeerStore> PeerMap<P> {
))
.await
{
self.dialog.send_warning(Warning::FailedPersistance {
self.dialog.send_warning(Warning::FailedPersistence {
warning: format!(
"Encountered an error adding {:?}:{} flags: {} ... {e}",
peer.address, peer.port, peer.service_flags
Expand Down
Loading