Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.6.0

## Changed

- `HeaderCheckpoint` renamed to `HashCheckpoint`

## 0.5.0

## Added
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bip157"
version = "0.5.0"
version = "0.6.0"
authors = ["Rob <rob@2140.dev>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -13,7 +13,7 @@ rust-version = "1.84.0"

[dependencies]
addrman = { package = "bitcoin-address-book", version = "0.1.1" }
bitcoin = { version = "0.32.8", default-features = false, features = [
bitcoin = { version = "=0.32.8", default-features = false, features = [
"rand-std",
] }
bip324 = { version = "0.7.0", default-features = false, features = [
Expand Down
6 changes: 2 additions & 4 deletions examples/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use bip157::builder::Builder;
use bip157::chain::{BlockHeaderChanges, ChainState};
use bip157::{Client, Event, HeaderCheckpoint, Network, ScriptBuf};
use bip157::{Client, Event, HashCheckpoint, Network, ScriptBuf};
use std::collections::HashSet;
use tokio::time::Instant;

Expand All @@ -28,9 +28,7 @@ async fn main() {
// The number of connections we would like to maintain
.required_peers(2)
// Only scan for taproot scripts
.chain_state(ChainState::Checkpoint(
HeaderCheckpoint::taproot_activation(),
))
.chain_state(ChainState::Checkpoint(HashCheckpoint::taproot_activation()))
// Add some initial peers
// .add_peers(seeds.into_iter().map(From::from))
// Connections over Tor are supported by Socks5 proxy
Expand Down
4 changes: 2 additions & 2 deletions examples/signet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use bip157::chain::{BlockHeaderChanges, ChainState};
use bip157::messages::Event;
use bip157::{builder::Builder, chain::checkpoints::HeaderCheckpoint, Client};
use bip157::{builder::Builder, chain::checkpoints::HashCheckpoint, Client};
use bip157::{Address, BlockHash, Network};
use std::collections::HashSet;
use std::str::FromStr;
Expand All @@ -20,7 +20,7 @@ async fn main() {
tracing::subscriber::set_global_default(subscriber).unwrap();
// Use a predefined checkpoint
let checkpoint =
HeaderCheckpoint::new(RECOVERY_HEIGHT, BlockHash::from_str(RECOVERY_HASH).unwrap());
HashCheckpoint::new(RECOVERY_HEIGHT, BlockHash::from_str(RECOVERY_HASH).unwrap());
// Add Bitcoin scripts to scan the blockchain for
let address = Address::from_str(ADDR)
.unwrap()
Expand Down
8 changes: 4 additions & 4 deletions src/chain/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,14 @@ mod tests {
use crate::chain::ChainState;
use crate::FilterType;
use crate::{
chain::checkpoints::HeaderCheckpoint,
chain::checkpoints::HashCheckpoint,
messages::{Event, Info, Warning},
Dialog,
};

use super::{CFHeaderChanges, Chain};

fn new_regtest(anchor: HeaderCheckpoint, peers: u8) -> Chain {
fn new_regtest(anchor: HashCheckpoint, peers: u8) -> Chain {
let (info_tx, _) = tokio::sync::mpsc::channel::<Info>(1);
let (warn_tx, _) = tokio::sync::mpsc::unbounded_channel::<Warning>();
let (event_tx, _) = tokio::sync::mpsc::unbounded_channel::<Event>();
Expand All @@ -450,8 +450,8 @@ mod tests {
)
}

fn base_block() -> HeaderCheckpoint {
HeaderCheckpoint::new(
fn base_block() -> HashCheckpoint {
HashCheckpoint::new(
2496,
BlockHash::from_str("4b4f478800538b3301b681358f84d870da0f9c4cde63ebd85fa0f273dfb07c6a")
.unwrap(),
Expand Down
28 changes: 14 additions & 14 deletions src/chain/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ type Height = u32;

/// A known block hash in the chain of most work.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HeaderCheckpoint {
pub struct HashCheckpoint {
/// The index of the block hash.
pub height: Height,
/// The Bitcoin block hash expected at this height
pub hash: BlockHash,
}

impl HeaderCheckpoint {
impl HashCheckpoint {
/// Create a new checkpoint from a known checkpoint of significant work.
pub fn new(height: Height, hash: BlockHash) -> Self {
HeaderCheckpoint { height, hash }
HashCheckpoint { height, hash }
}

/// Build a checkpoint from the genesis block for a given network.
pub fn from_genesis(params: impl AsRef<Params>) -> Self {
let genesis_block = genesis_block(params);
let hash = genesis_block.block_hash();
HeaderCheckpoint { height: 0, hash }
HashCheckpoint { height: 0, hash }
}

/// One block before the activation of the taproot softfork.
Expand All @@ -32,7 +32,7 @@ impl HeaderCheckpoint {
.parse::<BlockHash>()
.unwrap();
let height = 709_631;
HeaderCheckpoint { height, hash }
HashCheckpoint { height, hash }
}

/// One block before the activation of the segwit softfork.
Expand All @@ -41,42 +41,42 @@ impl HeaderCheckpoint {
.parse::<BlockHash>()
.unwrap();
let height = 481_823;
HeaderCheckpoint { height, hash }
HashCheckpoint { height, hash }
}
}

impl std::cmp::PartialOrd for HeaderCheckpoint {
impl std::cmp::PartialOrd for HashCheckpoint {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl std::cmp::Ord for HeaderCheckpoint {
impl std::cmp::Ord for HashCheckpoint {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.height.cmp(&other.height)
}
}

impl From<(u32, BlockHash)> for HeaderCheckpoint {
impl From<(u32, BlockHash)> for HashCheckpoint {
fn from(value: (u32, BlockHash)) -> Self {
HeaderCheckpoint::new(value.0, value.1)
HashCheckpoint::new(value.0, value.1)
}
}

impl TryFrom<(u32, String)> for HeaderCheckpoint {
impl TryFrom<(u32, String)> for HashCheckpoint {
type Error = <BlockHash as FromStr>::Err;

fn try_from(value: (u32, String)) -> Result<Self, Self::Error> {
let hash = BlockHash::from_str(&value.1)?;
Ok(HeaderCheckpoint::new(value.0, hash))
Ok(HashCheckpoint::new(value.0, hash))
}
}

impl TryFrom<(u32, &str)> for HeaderCheckpoint {
impl TryFrom<(u32, &str)> for HashCheckpoint {
type Error = <BlockHash as FromStr>::Err;

fn try_from(value: (u32, &str)) -> Result<Self, Self::Error> {
let hash = BlockHash::from_str(value.1)?;
Ok(HeaderCheckpoint::new(value.0, hash))
Ok(HashCheckpoint::new(value.0, hash))
}
}
6 changes: 3 additions & 3 deletions src/chain/graph.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{BTreeMap, HashMap};

use crate::HeaderCheckpoint;
use crate::HashCheckpoint;

use bitcoin::{
block::Header, constants::genesis_block, BlockHash, CompactTarget, FilterHash, Network, Work,
Expand Down Expand Up @@ -54,8 +54,8 @@ impl Tip {
}
}

impl From<HeaderCheckpoint> for Tip {
fn from(value: HeaderCheckpoint) -> Self {
impl From<HashCheckpoint> for Tip {
fn from(value: HashCheckpoint) -> Self {
Tip::from_checkpoint(value.height, value.hash)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use bitcoin::{
};

use crate::network::PeerId;
use crate::HeaderCheckpoint;
use crate::HashCheckpoint;

const MAX_PREV_STOP_HASHES: usize = 3;

Expand Down Expand Up @@ -96,7 +96,7 @@ pub enum ChainState {
/// A single checkpoint to start the sync _strictly after_.
///
/// Note that no reorganizations can be reported.
Checkpoint(HeaderCheckpoint),
Checkpoint(HashCheckpoint),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
6 changes: 3 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tokio::sync::oneshot;
use crate::chain::block_subsidy;
use crate::chain::IndexedHeader;
use crate::messages::ClientRequest;
use crate::{Event, HeaderCheckpoint, Info, Package, TrustedPeer, Warning};
use crate::{Event, HashCheckpoint, Info, Package, TrustedPeer, Warning};

use super::{error::ClientError, messages::ClientMessage};
use super::{error::FetchBlockError, IndexedBlock};
Expand Down Expand Up @@ -223,8 +223,8 @@ impl Requester {
/// # Errors
///
/// If the node has stopped running.
pub async fn chain_tip(&self) -> Result<HeaderCheckpoint, ClientError> {
let (tx, rx) = tokio::sync::oneshot::channel::<HeaderCheckpoint>();
pub async fn chain_tip(&self) -> Result<HashCheckpoint, ClientError> {
let (tx, rx) = tokio::sync::oneshot::channel::<HashCheckpoint>();
let request = ClientRequest::new((), tx);
self.ntx
.send(ClientMessage::BestBlock(request))
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use std::path::PathBuf;

// Re-exports
#[doc(inline)]
pub use chain::checkpoints::HeaderCheckpoint;
pub use chain::checkpoints::HashCheckpoint;

#[doc(inline)]
pub use tokio::sync::mpsc::Receiver;
Expand Down
10 changes: 5 additions & 5 deletions src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bitcoin::p2p::ServiceFlags;
use bitcoin::{block::Header, p2p::message_network::RejectReason, BlockHash, FeeRate, Wtxid};

use crate::chain::{BlockHeaderChanges, IndexedHeader};
use crate::{chain::checkpoints::HeaderCheckpoint, IndexedBlock, TrustedPeer};
use crate::{chain::checkpoints::HashCheckpoint, IndexedBlock, TrustedPeer};
use crate::{IndexedFilter, Package};

use super::error::FetchBlockError;
Expand Down Expand Up @@ -53,21 +53,21 @@ pub enum Event {
#[derive(Debug, Clone)]
pub struct SyncUpdate {
/// Last known tip of the blockchain
pub tip: HeaderCheckpoint,
pub tip: HashCheckpoint,
/// 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 {
pub(crate) fn new(tip: HashCheckpoint, recent_history: BTreeMap<u32, Header>) -> Self {
Self {
tip,
recent_history,
}
}

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

Expand Down Expand Up @@ -144,7 +144,7 @@ pub(crate) enum ClientMessage {
/// Explicitly request a block from the node.
GetBlock(ClientRequest<BlockHash, Result<IndexedBlock, FetchBlockError>>),
/// Get the chain tip.
BestBlock(ClientRequest<(), HeaderCheckpoint>),
BestBlock(ClientRequest<(), HashCheckpoint>),
/// Add another known peer to connect to.
AddPeer(TrustedPeer),
/// Request the broadcast minimum fee rate.
Expand Down
2 changes: 1 addition & 1 deletion src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) mod reader;
pub(crate) mod socks;

pub const PROTOCOL_VERSION: u32 = 70016;
pub const KYOTO_VERSION: &str = "0.5.0";
pub const KYOTO_VERSION: &str = "0.6.0";
pub const RUST_BITCOIN_VERSION: &str = "0.32.8";

const THIRTY_MINS: Duration = Duration::from_secs(60 * 30);
Expand Down
2 changes: 1 addition & 1 deletion src/network/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<R: AsyncBufReadExt + Send + Sync + Unpin> Reader<R> {
}
NetworkMessage::SendAddrV2 => None,
#[allow(unused)]
NetworkMessage::Unknown { command, payload } => Some(ReaderMessage::Disconnect),
NetworkMessage::Unknown { command, payload } => None,
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
chain::{
block_queue::{BlockQueue, ProcessBlockResponse},
chain::Chain,
checkpoints::HeaderCheckpoint,
checkpoints::HashCheckpoint,
CFHeaderChanges, ChainState, FilterCheck, HeaderSyncEffect, IndexedHeader,
},
error::FetchBlockError,
Expand Down Expand Up @@ -98,7 +98,7 @@ impl Node {
);
// Build the chain
let chain_state = chain_state.unwrap_or(ChainState::Checkpoint(
HeaderCheckpoint::from_genesis(network),
HashCheckpoint::from_genesis(network),
));
let chain = Chain::new(
network,
Expand Down Expand Up @@ -228,7 +228,7 @@ impl Node {
let block_tree = &self.chain.header_chain;
let hash = block_tree.tip_hash();
let height = block_tree.height();
let checkpoint = HeaderCheckpoint::new(height, hash);
let checkpoint = HashCheckpoint::new(height, hash);
let send_result = oneshot.send(checkpoint);
if send_result.is_err() {
self.dialog.send_warning(Warning::ChannelDropped);
Expand Down Expand Up @@ -338,7 +338,7 @@ impl Node {
if self.chain.is_filters_synced() {
self.state = NodeState::FiltersSynced;
let update = SyncUpdate::new(
HeaderCheckpoint::new(
HashCheckpoint::new(
self.chain.header_chain.height(),
self.chain.header_chain.tip_hash(),
),
Expand Down
Loading
Loading