Skip to content
Draft
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
30 changes: 18 additions & 12 deletions example/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use bip157::builder::Builder;
use bip157::chain::{BlockHeaderChanges, ChainState};
use bip157::{lookup_host, Client, Event, HeaderCheckpoint, Network, ScriptBuf};
use std::collections::HashSet;
use bip157::{Client, Event, FilterType, HeaderCheckpoint, Network};
use std::net::{IpAddr, Ipv4Addr};
use tokio::time::Instant;

const NETWORK: Network = Network::Bitcoin;
Expand All @@ -15,23 +15,23 @@ async fn main() {
let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber).unwrap();
let now = Instant::now();
// Add Bitcoin scripts to scan the blockchain for
let address = ScriptBuf::new_op_return(b"Kyoto light client");
let mut addresses = HashSet::new();
addresses.insert(address);
let seeds = lookup_host("seed.bitcoin.sipa.be").await;
// 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(2)
.required_peers(1)
// Only scan for taproot scripts
.chain_state(ChainState::Checkpoint(
HeaderCheckpoint::taproot_activation(),
))
.chain_state(ChainState::Checkpoint(HeaderCheckpoint {
height: 927516,
hash: "00000000000000000001849ec8caa64852aa2299002629269ee90424c765c617"
.parse()
.unwrap(),
}))
.filter_type(FilterType::Taproot)
// Add some initial peers
.add_peers(seeds.into_iter().map(From::from))
.add_peer(IpAddr::V4(Ipv4Addr::new(65, 109, 109, 117)))
// Create the node and client
.build();
// Run the node on a separate task
Expand All @@ -45,6 +45,7 @@ async fn main() {
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! {
Expand All @@ -60,8 +61,13 @@ async fn main() {
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);
},
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ pub enum FilterType {
#[default]
/// A golomb coded compact sketch based on siphash. Contains all spendable script types.
Basic,
/// GCS filter only containing P2TR outputs.
Taproot,
}

impl From<FilterType> for u8 {
fn from(value: FilterType) -> Self {
match value {
FilterType::Basic => 0x00,
FilterType::Taproot => 0x01,
}
}
}
Expand Down