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

fix(example): new HeaderCheckpoint constructor #171

Merged
merged 1 commit into from
Oct 22, 2024
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
29 changes: 15 additions & 14 deletions example/rescan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,38 @@
//! the filters for inclusions of these scripts and download the relevant
//! blocks.

use bitcoin::BlockHash;
use kyoto::core::messages::NodeMessage;
use kyoto::{chain::checkpoints::HeaderCheckpoint, core::builder::NodeBuilder};
use kyoto::{Address, Network};
use std::collections::HashSet;
use std::str::FromStr;

const NETWORK: Network = Network::Signet;
const RECOVERY_HEIGHT: u32 = 170_000;

#[tokio::main]
async fn main() {
// Add third-party logging
let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber).unwrap();
// Add Bitcoin scripts to scan the blockchain for
let address = bitcoin::Address::from_str("tb1q9pvjqz5u5sdgpatg3wn0ce438u5cyv85lly0pc")
let address = Address::from_str("tb1q9pvjqz5u5sdgpatg3wn0ce438u5cyv85lly0pc")
.unwrap()
.require_network(bitcoin::Network::Signet)
.require_network(NETWORK)
.unwrap()
.into();
let mut addresses = HashSet::new();
addresses.insert(address);
// Create a new node builder
let builder = NodeBuilder::new(bitcoin::Network::Signet);
let builder = NodeBuilder::new(NETWORK);
// Add node preferences and build the node/client
let (node, client) = builder
// The Bitcoin scripts to monitor
.add_scripts(addresses)
// Only scan blocks strictly after an anchor checkpoint
.anchor_checkpoint(HeaderCheckpoint::new(
170_000,
BlockHash::from_str("00000041c812a89f084f633e4cf47e819a2f6b1c0a15162355a930410522c99d")
.unwrap(),
.anchor_checkpoint(HeaderCheckpoint::closest_checkpoint_below_height(
RECOVERY_HEIGHT,
NETWORK,
))
// The number of connections we would like to maintain
.num_required_peers(1)
Expand All @@ -58,12 +60,11 @@ async fn main() {
}
}
// Add new scripts to the node.
let new_script = bitcoin::Address::from_str(
"tb1par6ufhp0t448t908kyyvkp3a48r42qcjmg0z9p6a0zuakc44nn2seh63jr",
)
.unwrap()
.require_network(bitcoin::Network::Signet)
.unwrap();
let new_script =
Address::from_str("tb1par6ufhp0t448t908kyyvkp3a48r42qcjmg0z9p6a0zuakc44nn2seh63jr")
.unwrap()
.require_network(NETWORK)
.unwrap();
sender.add_script(new_script).await.unwrap();
// // Tell the node to look for these new scripts
sender.rescan().await.unwrap();
Expand Down
16 changes: 8 additions & 8 deletions example/signet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
//! New peers are stored in a local database, and the node connects to multiple peers
//! to improve the anonymity set when broadcasting transactions to the Bitcoin network.

use bitcoin::BlockHash;
use kyoto::chain::checkpoints::SIGNET_HEADER_CP;
use kyoto::core::messages::NodeMessage;
use kyoto::{chain::checkpoints::HeaderCheckpoint, core::builder::NodeBuilder};
use kyoto::{AddrV2, ServiceFlags, TrustedPeer};
use kyoto::{AddrV2, Address, Network, ServiceFlags, TrustedPeer};
use std::collections::HashSet;
use std::{
net::{IpAddr, Ipv4Addr},
str::FromStr,
};

const NETWORK: Network = Network::Signet;
const RECOVERY_HEIGHT: u32 = 170_000;

#[tokio::main]
async fn main() {
// Add third-party logging
let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber).unwrap();
// Use a predefined checkpoint
let (height, hash) = SIGNET_HEADER_CP.iter().rev().nth(3).unwrap();
let checkpoint = HeaderCheckpoint::new(*height, BlockHash::from_str(hash).unwrap());
let checkpoint = HeaderCheckpoint::closest_checkpoint_below_height(RECOVERY_HEIGHT, NETWORK);
// Add Bitcoin scripts to scan the blockchain for
let address = bitcoin::Address::from_str("tb1q9pvjqz5u5sdgpatg3wn0ce438u5cyv85lly0pc")
let address = Address::from_str("tb1q9pvjqz5u5sdgpatg3wn0ce438u5cyv85lly0pc")
.unwrap()
.require_network(bitcoin::Network::Signet)
.require_network(NETWORK)
.unwrap()
.into();
let mut addresses = HashSet::new();
Expand All @@ -37,7 +37,7 @@ async fn main() {
ServiceFlags::P2P_V2,
);
// Create a new node builder
let builder = NodeBuilder::new(bitcoin::Network::Signet);
let builder = NodeBuilder::new(NETWORK);
// Add node preferences and build the node/client
let (node, client) = builder
// Add the peers
Expand Down
Loading