Skip to content

Commit db181cc

Browse files
committed
test: add integration test for DnsPeer
Add dns_peer_sync test that builds a node with a DnsPeer pointing at the local bitcoind and verifies the node resolves the hostname, connects, and syncs the chain successfully.
1 parent 5f7e263 commit db181cc

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

tests/core.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bip157::{
88
chain::{checkpoints::HeaderCheckpoint, BlockHeaderChanges, ChainState},
99
client::Client,
1010
node::Node,
11-
Address, BlockHash, Event, Info, ServiceFlags, Transaction, TrustedPeer, Warning,
11+
Address, BlockHash, DnsPeer, Event, Info, ServiceFlags, Transaction, TrustedPeer, Warning,
1212
};
1313
use bitcoin::{
1414
absolute,
@@ -633,6 +633,39 @@ async fn tx_can_broadcast() {
633633
.unwrap();
634634
}
635635

636+
#[tokio::test]
637+
async fn dns_peer_sync() {
638+
let (bitcoind, socket_addr) = start_bitcoind(true).unwrap();
639+
let rpc = &bitcoind.client;
640+
let tempdir = tempfile::TempDir::new().unwrap().path().to_owned();
641+
let miner = rpc.new_address().unwrap();
642+
mine_blocks(rpc, &miner, 10, 2).await;
643+
let best = best_hash(rpc);
644+
// Use a DNS peer with a hostname instead of a resolved TrustedPeer.
645+
// The hostname is resolved via tokio::net::lookup_host on each connection attempt.
646+
let dns_peer = DnsPeer::new(socket_addr.ip().to_string(), socket_addr.port());
647+
let builder = bip157::builder::Builder::new(bitcoin::Network::Regtest)
648+
.chain_state(ChainState::Checkpoint(HeaderCheckpoint::from_genesis(
649+
bitcoin::Network::Regtest,
650+
)))
651+
.add_dns_peer(dns_peer)
652+
.data_dir(tempdir);
653+
let (node, client) = builder.build();
654+
tokio::task::spawn(async move { node.run().await });
655+
let Client {
656+
requester,
657+
info_rx,
658+
warn_rx,
659+
event_rx: mut channel,
660+
} = client;
661+
tokio::task::spawn(async move { print_logs(info_rx, warn_rx).await });
662+
sync_assert(&best, &mut channel).await;
663+
let cp = requester.chain_tip().await.unwrap();
664+
assert_eq!(cp.hash, best);
665+
requester.shutdown().unwrap();
666+
rpc.stop().unwrap();
667+
}
668+
636669
#[tokio::test]
637670
async fn dns_works() {
638671
let hostname = bip157::lookup_host("seed.bitcoin.sipa.be").await;

0 commit comments

Comments
 (0)