Skip to content

Commit

Permalink
client: update sync event
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Jun 21, 2024
1 parent 711dad1 commit b03c4ca
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 23 deletions.
6 changes: 3 additions & 3 deletions example/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ async fn main() {
NodeMessage::TxBroadcastFailure => {
tracing::error!("The transaction could not be broadcast.")
}
NodeMessage::Synced(tip) => {
tracing::info!("Synced chain up to block {}", tip.height,);
tracing::info!("Chain tip: {}", tip.hash.to_string(),);
NodeMessage::Synced(update) => {
tracing::info!("Synced chain up to block {}", update.tip().height,);
tracing::info!("Chain tip: {}", update.tip().hash.to_string(),);
break;
}
}
Expand Down
12 changes: 6 additions & 6 deletions example/rescan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ async fn main() {
match message {
NodeMessage::Dialog(d) => tracing::info!("{}", d),
NodeMessage::Warning(e) => tracing::warn!("{}", e),
NodeMessage::Synced(tip) => {
tracing::info!("Synced chain up to block {}", tip.height,);
tracing::info!("Chain tip: {}", tip.hash.to_string(),);
NodeMessage::Synced(update) => {
tracing::info!("Synced chain up to block {}", update.tip().height);
tracing::info!("Chain tip: {}", update.tip().hash);
break;
}
_ => (),
Expand All @@ -85,9 +85,9 @@ async fn main() {
match message {
NodeMessage::Dialog(d) => tracing::info!("{}", d),
NodeMessage::Warning(e) => tracing::warn!("{}", e),
NodeMessage::Synced(tip) => {
tracing::info!("Synced chain up to block {}", tip.height,);
tracing::info!("Chain tip: {}", tip.hash.to_string(),);
NodeMessage::Synced(update) => {
tracing::info!("Synced chain up to block {}", update.tip.height);
tracing::info!("Chain tip: {}", update.tip.hash);
break;
}
_ => (),
Expand Down
12 changes: 9 additions & 3 deletions example/signet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ async fn main() {
NodeMessage::TxBroadcastFailure => {
tracing::error!("The transaction could not be broadcast.")
}
NodeMessage::Synced(tip) => {
tracing::info!("Synced chain up to block {}", tip.height,);
tracing::info!("Chain tip: {}", tip.hash.to_string(),);
NodeMessage::Synced(update) => {
tracing::info!("Synced chain up to block {}", update.tip.height);
tracing::info!("Chain tip: {}", update.tip.hash);
let recent = update.recent_history;
tracing::info!("Recent history:");
for (height, hash) in recent {
tracing::info!("Synced chain up to block {}", height);
tracing::info!("Chain tip: {}", hash.block_hash());
}
break;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/chain/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ impl Chain {
self.checkpoints.is_exhausted()
}

// The last ten heights and headers in the chain
pub(crate) fn last_ten(&self) -> BTreeMap<u32, Header> {
self.header_chain.last_ten()
}

// Set the best known height to our peer
pub(crate) async fn set_best_known_height(&mut self, height: u32) {
self.dialog
Expand Down
11 changes: 11 additions & 0 deletions src/chain/header_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ impl HeaderChain {
locators
}

// The last ten heights and headers in chronological order
pub(crate) fn last_ten(&self) -> BTreeMap<u32, Header> {
self.headers
.iter()
.rev()
.take(10)
.rev()
.map(|(height, header)| (*height, header.clone()))
.collect()
}

// Extend the current chain, potentially rewriting history. Higher order functions should decide what we extend
pub(crate) fn extend(&mut self, batch: &[Header]) -> Vec<DisconnectedHeader> {
let mut reorged = Vec::new();
Expand Down
39 changes: 36 additions & 3 deletions src/node/messages.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;
use std::collections::{BTreeMap, HashSet};

use bitcoin::ScriptBuf;
use bitcoin::{block::Header, ScriptBuf};

use crate::{
chain::checkpoints::HeaderCheckpoint, DisconnectedHeader, IndexedBlock, IndexedTransaction,
Expand All @@ -19,13 +19,46 @@ pub enum NodeMessage {
/// A relevant [`crate::Block`] based on the user provided scripts
Block(IndexedBlock),
/// The node is fully synced, having scanned the requested range
Synced(HeaderCheckpoint),
Synced(SyncUpdate),
/// Blocks were reorganized out of the chain
BlocksDisconnected(Vec<DisconnectedHeader>),
/// A problem occured sending a transaction.
TxBroadcastFailure,
}

/// The node has synced to a new tip of the chain.
#[derive(Debug, Clone)]
pub struct SyncUpdate {
/// Last known tip of the blockchain
pub tip: HeaderCheckpoint,
/// Ten recent headers ending with the tip
pub recent_history: BTreeMap<u32, Header>,
}

impl SyncUpdate {
pub(crate) fn new(tip: HeaderCheckpoint, recent_history: BTreeMap<u32, Header>) -> Self {
Self {
tip,
recent_history,
}
}

/// Get the tip of the blockchain after this sync.
pub fn tip(&self) -> HeaderCheckpoint {
self.tip.clone()
}

/// Get the ten most recent blocks in chronological order after this sync.
/// For nodes that do not save any block header history, it is recommmended to use
/// a block with significant depth, say 10 blocks deep, as the anchor for the
/// next sync. This is so the node may gracefully handle block reorganizations,
/// so long as they occur within 10 blocks of depth. This occurs at more than
/// a 99% probability.
pub fn recent_history(&self) -> &BTreeMap<u32, Header> {
&self.recent_history
}
}

/// Commands to issue a node.
#[derive(Debug, Clone)]
pub enum ClientMessage {
Expand Down
14 changes: 6 additions & 8 deletions src/node/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use super::{
config::NodeConfig,
dialog::Dialog,
error::NodeError,
messages::{ClientMessage, NodeMessage},
messages::{ClientMessage, NodeMessage, SyncUpdate},
};

type Whitelist = Option<Vec<(IpAddr, u16)>>;
Expand Down Expand Up @@ -357,13 +357,11 @@ impl Node {
let header_chain = self.chain.lock().await;
if header_chain.block_queue_empty() {
*state = NodeState::TransactionsSynced;
let _ = self
.dialog
.send_data(NodeMessage::Synced(HeaderCheckpoint::new(
header_chain.height(),
header_chain.tip(),
)))
.await;
let update = SyncUpdate::new(
HeaderCheckpoint::new(header_chain.height(), header_chain.tip()),
header_chain.last_ten(),
);
let _ = self.dialog.send_data(NodeMessage::Synced(update)).await;
}
}
NodeState::TransactionsSynced => (),
Expand Down

0 comments on commit b03c4ca

Please sign in to comment.