1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
//! Client events.
use std::fmt;
use std::io;
use std::sync::Arc;
use nakamoto_common::bitcoin::network::constants::ServiceFlags;
use nakamoto_common::bitcoin::{Transaction, Txid};
use nakamoto_common::block::{BlockHash, BlockHeader, Height};
use nakamoto_net::DisconnectReason;
use nakamoto_p2p::protocol;
use nakamoto_p2p::protocol::fees::FeeEstimate;
use nakamoto_p2p::protocol::{Link, PeerId};
use crate::spv::TxStatus;
/// Event emitted by the client.
#[derive(Debug, Clone)]
pub enum Event {
/// Ready to process peer events and start receiving commands.
/// Note that this isn't necessarily the first event emitted.
Ready {
/// The tip of the block header chain.
tip: Height,
/// The tip of the filter header chain.
filter_tip: Height,
},
/// Peer connected. This is fired when the physical TCP/IP connection
/// is established. Use [`Event::PeerNegotiated`] to know when the P2P handshake
/// has completed.
PeerConnected {
/// Peer address.
addr: PeerId,
/// Connection link.
link: Link,
},
/// Peer disconnected after successful connection.
PeerDisconnected {
/// Peer address.
addr: PeerId,
/// Reason for disconnection.
reason: DisconnectReason<protocol::DisconnectReason>,
},
/// Connection was never established and timed out or failed.
PeerConnectionFailed {
/// Peer address.
addr: PeerId,
/// Connection error.
error: Arc<io::Error>,
},
/// Peer handshake completed. The peer connection is fully functional from this point.
PeerNegotiated {
/// Peer address.
addr: PeerId,
/// Connection link.
link: Link,
/// Peer services.
services: ServiceFlags,
/// Peer height.
height: Height,
/// Peer user agent.
user_agent: String,
/// Negotiated protocol version.
version: u32,
},
/// The best known height amongst connected peers has been updated.
/// Note that there is no guarantee that this height really exists;
/// peers don't have to follow the protocol and could send a bogus
/// height.
PeerHeightUpdated {
/// Best block height known.
height: Height,
},
/// A block was added to the main chain.
BlockConnected {
/// Block header.
header: BlockHeader,
/// Block hash.
hash: BlockHash,
/// Height of the block.
height: Height,
},
/// One of the blocks of the main chain was reverted, due to a re-org.
/// These events will fire from the latest block starting from the tip, to the earliest.
/// Mark all transactions belonging to this block as *unconfirmed*.
BlockDisconnected {
/// Header of the block.
header: BlockHeader,
/// Block hash.
hash: BlockHash,
/// Height of the block when it was part of the main chain.
height: Height,
},
/// A block has matched one of the filters and is ready to be processed.
/// This event usually precedes [`Event::TxStatusChanged`] events.
BlockMatched {
/// Hash of the matching block.
hash: BlockHash,
/// Block header.
header: BlockHeader,
/// Block height.
height: Height,
/// Transactions in this block.
transactions: Vec<Transaction>,
},
/// Transaction fee rate estimated for a block.
FeeEstimated {
/// Block hash of the estimate.
block: BlockHash,
/// Block height of the estimate.
height: Height,
/// Fee estimate.
fees: FeeEstimate,
},
/// A filter was processed. If it matched any of the scripts in the watchlist,
/// the corresponding block was scheduled for download, and a [`Event::BlockMatched`]
/// event will eventually be fired.
FilterProcessed {
/// Corresponding block hash.
block: BlockHash,
/// Filter height (same as block).
height: Height,
/// Whether or not this filter matched any of the watched scripts.
matched: bool,
/// Whether or not this filter is valid.
valid: bool,
},
/// The status of a transaction has changed.
TxStatusChanged {
/// The Transaction ID.
txid: Txid,
/// The new transaction status.
status: TxStatus,
},
/// Compact filters have been synced and processed up to this point and matching blocks have
/// been fetched.
///
/// If filters have been processed up to the last block in the client's header chain, `height`
/// and `tip` will be equal.
Synced {
/// Height up to which we are synced.
height: Height,
/// Tip of our block header chain.
tip: Height,
},
}
impl fmt::Display for Event {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Ready { .. } => {
write!(fmt, "ready to process events and commands")
}
Self::BlockConnected { hash, height, .. } => {
write!(fmt, "block {} connected at height {}", hash, height)
}
Self::BlockDisconnected { hash, height, .. } => {
write!(fmt, "block {} disconnected at height {}", hash, height)
}
Self::BlockMatched { hash, height, .. } => {
write!(
fmt,
"block {} ready to be processed at height {}",
hash, height
)
}
Self::FeeEstimated { fees, height, .. } => {
write!(
fmt,
"transaction median fee rate for block #{} is {} sat/vB",
height, fees.median,
)
}
Self::FilterProcessed {
height, matched, ..
} => {
write!(
fmt,
"filter processed at height {} (match = {})",
height, matched
)
}
Self::TxStatusChanged { txid, status } => {
write!(fmt, "transaction {} status changed: {}", txid, status)
}
Self::Synced { height, .. } => write!(fmt, "filters synced up to height {}", height),
Self::PeerConnected { addr, link } => {
write!(fmt, "peer {} connected ({:?})", &addr, link)
}
Self::PeerConnectionFailed { addr, error } => {
write!(
fmt,
"peer connection attempt to {} failed with {}",
&addr, error
)
}
Self::PeerHeightUpdated { height } => {
write!(fmt, "peer height updated to {}", height)
}
Self::PeerDisconnected { addr, reason } => {
write!(fmt, "disconnected from {} ({})", &addr, reason)
}
Self::PeerNegotiated {
addr,
height,
services,
..
} => write!(
fmt,
"peer {} negotiated with services {} and height {}..",
addr, services, height
),
}
}
}