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
122 changes: 29 additions & 93 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ rust-version = "1.84.0"
[dependencies]
# bitcoinkernel = { path = "../rust-bitcoinkernel", version = "0.0.15" }
bitcoinkernel = "0.2"
addrman = { package = "bitcoin-address-book", git = "https://github.com/rustaceanrob/bitcoin-address-book", rev = "4417e4c8f5d04245947c758b73fb0dc9e7e0bd15" }
bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin", default-features = false, rev = "16cc257c3695dea0e7301a5fa9cab44b8ed60598" }
p2p = { package = "bitcoin-p2p", git = "https://github.com/2140-dev/bitcoin-p2p.git", rev = "322c4abb2a947f3f2d85991a0cb450249c68c1ea" }
addrman = { package = "bitcoin-address-book", version = "0.1.1" }
bitcoin = "0.32.8"
p2p = { package = "bitcoin-p2p", git = "https://github.com/2140-dev/bitcoin-p2p.git", rev = "5dc03375636a10487c7e50659416876c28f58a3b" }
## Log and configuration
configure_me = "0.4.0"
clap = { version = "4", features = ["derive"] }
Expand Down
25 changes: 11 additions & 14 deletions src/bin/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ use std::{
time::{Duration, Instant},
};

use bitcoin::{BlockHash, Network, TestnetVersion};
use bitcoin::p2p::{
address::{AddrV2, AddrV2Message},
ServiceFlags,
};
use bitcoin::{hashes::Hash, BlockHash, Network};
use bitcoinkernel::{
core::BlockHashExt, prelude::BlockValidationStateExt, ChainType, ChainstateManagerBuilder,
Context, ContextBuilder, Log, Logger, SynchronizationState, ValidationMode,
};
use kernel_node::{
daemonize::Daemonize,
kernel_util::NetworkExt,
peer::{BitcoinPeer, NodeState, TipState},
};
use kernel_node::{
Expand All @@ -25,10 +30,7 @@ use kernel_node::{
server_capnp::server,
};
use log::{debug, error, info, warn};
use p2p::{
dns::{BITCOIN_SEEDS, SIGNET_SEEDS, TESTNET3_SEEDS, TESTNET4_SEEDS},
p2p_message_types::{address::AddrV2, message::AddrV2Payload, NetworkExt, ServiceFlags},
};
use p2p::dns::{BITCOIN_SEEDS, SIGNET_SEEDS, TESTNET3_SEEDS, TESTNET4_SEEDS};
use tokio::net::UnixListener;
use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};

Expand Down Expand Up @@ -146,14 +148,9 @@ fn resolve_seeds(network: Network) -> Vec<IpAddr> {
let seeds: Vec<String> = match network {
Network::Bitcoin => BITCOIN_SEEDS.into_iter().map(format_hostname).collect(),
Network::Signet => SIGNET_SEEDS.into_iter().map(format_hostname).collect(),
Network::Testnet(TestnetVersion::V3) => {
TESTNET3_SEEDS.into_iter().map(format_hostname).collect()
}
Network::Testnet(TestnetVersion::V4) => {
TESTNET4_SEEDS.into_iter().map(format_hostname).collect()
}
Network::Testnet => TESTNET3_SEEDS.into_iter().map(format_hostname).collect(),
Network::Testnet4 => TESTNET4_SEEDS.into_iter().map(format_hostname).collect(),
Network::Regtest => Vec::new(),
_ => panic!("unknown network."),
};
let mut results = Vec::new();
for host in seeds {
Expand All @@ -173,7 +170,7 @@ fn run(
connect: Option<SocketAddr>,
mut node_state: NodeState,
shutdown_rx: mpsc::Receiver<()>,
addr_rx: mpsc::Receiver<AddrV2Payload>,
addr_rx: mpsc::Receiver<Vec<AddrV2Message>>,
block_rx: mpsc::Receiver<bitcoinkernel::Block>,
) -> std::io::Result<()> {
let mut table = addrman::Table::<TABLE_WIDTH, TABLE_SLOT, MAX_BUCKETS>::new();
Expand Down Expand Up @@ -284,7 +281,7 @@ fn run(
match addr_rx.recv() {
Ok(payload) => {
let mut addr_lock = addrman.lock().unwrap();
for address in payload.0 {
for address in payload {
let record = addrman::Record::new(
address.addr,
address.port,
Expand Down
29 changes: 23 additions & 6 deletions src/kernel_util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bitcoin::{block::Checked, consensus::encode, Network, TestnetVersion};
use bitcoin::{consensus::encode, hashes::Hash, Network};
use bitcoinkernel::{core::BlockHashExt, BlockTreeEntry, ChainType};
use std::{fs, path::PathBuf};

Expand All @@ -11,10 +11,9 @@ impl ChainExt for Network {
match self {
Network::Bitcoin => ChainType::Mainnet,
Network::Signet => ChainType::Signet,
Network::Testnet(TestnetVersion::V3) => ChainType::Testnet,
Network::Testnet(TestnetVersion::V4) => ChainType::Testnet4,
Network::Testnet => ChainType::Testnet,
Network::Testnet4 => ChainType::Testnet4,
Network::Regtest => ChainType::Regtest,
_ => unimplemented!("unsupported network"),
}
}
}
Expand Down Expand Up @@ -46,13 +45,31 @@ impl<S: AsRef<str>> DirnameExt for S {
}
}

pub fn bitcoin_block_to_kernel_block(block: &bitcoin::Block<Checked>) -> bitcoinkernel::Block {
pub trait NetworkExt {
// The P2P port for a given [`Network`].
fn default_p2p_port(self) -> u16;
}

impl NetworkExt for Network {
// The P2P port for a given [`Network`].
fn default_p2p_port(self) -> u16 {
match &self {
Self::Bitcoin => 8333,
Self::Signet => 38333,
Self::Testnet => 18333,
Self::Testnet4 => 48333,
Self::Regtest => 18444,
}
}
}

pub fn bitcoin_block_to_kernel_block(block: &bitcoin::Block) -> bitcoinkernel::Block {
let ser_block = encode::serialize(block);
bitcoinkernel::Block::try_from(ser_block.as_slice()).unwrap()
}

pub fn bitcoin_header_to_kernel_header(
header: &bitcoin::BlockHeader,
header: &bitcoin::block::Header,
) -> bitcoinkernel::BlockHeader {
let ser_header = encode::serialize(header);
bitcoinkernel::BlockHeader::new(ser_header.as_slice()).unwrap()
Expand Down
Loading
Loading