-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbitcoin.rs
More file actions
92 lines (89 loc) · 3.79 KB
/
bitcoin.rs
File metadata and controls
92 lines (89 loc) · 3.79 KB
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
//! Sync a simple script with the Bitcoin network. This example is intended to demonstrate the
//! expected sync time on your machine and in your region.
use bip157::builder::Builder;
use bip157::chain::{BlockHeaderChanges, ChainState};
use bip157::{Client, Event, FilterType, HeaderCheckpoint, Network};
use std::net::{IpAddr, Ipv4Addr};
use tokio::time::Instant;
const NETWORK: Network = Network::Bitcoin;
#[tokio::main]
async fn main() {
// Add third-party logging
let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber).unwrap();
let now = Instant::now();
// let seeds = lookup_host("seed.bitcoin.sipa.be").await;
// Create a new node builder
let builder = Builder::new(NETWORK);
// Add node preferences and build the node/client
let (node, client) = builder
// The number of connections we would like to maintain
.required_peers(1)
// Only scan for taproot scripts
.chain_state(ChainState::Checkpoint(HeaderCheckpoint {
height: 927516,
hash: "00000000000000000001849ec8caa64852aa2299002629269ee90424c765c617"
.parse()
.unwrap(),
}))
.filter_type(FilterType::Taproot)
// Add some initial peers
.add_peer(IpAddr::V4(Ipv4Addr::new(65, 109, 109, 117)))
// Create the node and client
.build();
// Run the node on a separate task
tokio::task::spawn(async move { node.run().await });
// Split the client into components that send messages and listen to messages.
// With this construction, different parts of the program can take ownership of
// specific tasks.
let Client {
requester,
mut info_rx,
mut warn_rx,
mut event_rx,
} = client;
let mut total_bytes = 0;
// Continually listen for events until the node is synced to its peers.
loop {
tokio::select! {
event = event_rx.recv() => {
if let Some(event) = event {
match event {
Event::FiltersSynced(update) => {
tracing::info!("Chain tip: {}",update.tip().hash);
// Request information from the node
let fee = requester.broadcast_min_feerate().await.unwrap();
tracing::info!("Minimum transaction broadcast fee rate: {:#}", fee);
let sync_time = now.elapsed().as_secs_f32();
tracing::info!("Total sync time: {sync_time} seconds");
let avg_fee_rate = requester.average_fee_rate(update.tip().hash).await.unwrap();
tracing::info!("Last block average fee rate: {:#}", avg_fee_rate);
tracing::info!("Total bytes downloaded {}", total_bytes);
break;
},
Event::IndexedFilter(filter) => {
let byte_vec = filter.into_contents();
total_bytes += byte_vec.len();
},
Event::ChainUpdate(BlockHeaderChanges::Connected(header)) => {
tracing::info!("New best tip {}", header.height);
},
_ => (),
}
}
}
info = info_rx.recv() => {
if let Some(info) = info {
tracing::info!("{info}");
}
}
warn = warn_rx.recv() => {
if let Some(warn) = warn {
tracing::warn!("{warn}");
}
}
}
}
let _ = requester.shutdown();
tracing::info!("Shutting down");
}