Skip to content

Commit

Permalink
Merge pull request #68 from nyonson/harden-regtest-script
Browse files Browse the repository at this point in the history
Harden regtest script
  • Loading branch information
rustaceanrob authored Sep 30, 2024
2 parents cd584df + 86db8ba commit b8275f1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 130 deletions.
98 changes: 0 additions & 98 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ bitcoin = { version = "0.32.0", default-features = false }

[dev-dependencies]
hex = { package = "hex-conservative", version = "0.2.0" }
bitcoincore-rpc = "0.19.0"

[lib]
name = "bip324"
Expand Down
54 changes: 48 additions & 6 deletions protocol/tests/regtest.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
bitcoind --chain=regtest --txindex --blockfilterindex --peerblockfilters --rpcport=18443 --rpcuser=test --rpcpassword=b324 --rest=1 --server=1 --listen=1 --v2transport=1 &
sleep 1
cargo test regtest_handshake -- --nocapture
sleep 1
## In case of failure this will stop core anyway.
bitcoin-cli --chain=regtest --rpcuser=test --rpcpassword=b324 stop
#!/usr/bin/env bash
#
# Test a handshake with a running bitcoin daemon.

# Exit immediately if a command exits with a non-zero status.
set -e

BITCOIND_PID=""

cleanup() {
echo "Cleaning up..."
if [ -n "$BITCOIND_PID" ]; then
bitcoin-cli --chain=regtest --rpcuser=test --rpcpassword=b324 stop
# Wait for the bitcoind process to stop.
wait $BITCOIND_PID
fi
}

# Ensure the bitcoind process is cleaned up if this script is killed for any reason.
trap cleanup EXIT

start_bitcoind() {
bitcoind --chain=regtest --txindex --blockfilterindex --peerblockfilters \
--rpcport=18443 --rpcuser=test --rpcpassword=b324 --rest=1 \
--server=1 --listen=1 --v2transport=1 &
BITCOIND_PID=$!

echo "Waiting for bitcoind to start..."
until bitcoin-cli --chain=regtest --rpcuser=test --rpcpassword=b324 getblockchaininfo &>/dev/null
do
sleep 1
done
echo "bitcoind started."
}

run_tests() {
cargo test regtest_handshake -- --ignored --nocapture
TEST_EXIT_CODE=$?
return $TEST_EXIT_CODE
}

main() {
start_bitcoind
run_tests
return $?
}

main
36 changes: 11 additions & 25 deletions protocol/tests/round_trips.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

const RPC_USER: &str = "test";
const RPC_PASSWORD: &str = "b324";
const HOST: &str = "http://localhost:18443";
const PORT: u16 = 18444;

#[test]
Expand Down Expand Up @@ -70,28 +67,20 @@ fn hello_world_happy_path() {

#[test]
#[cfg(feature = "std")]
#[ignore = "CI"]
#[ignore = "Requires a running bitcoin daemon."]
fn regtest_handshake() {
use std::{
io::{Read, Write},
net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream},
time::{Duration, SystemTime, UNIX_EPOCH},
time::{SystemTime, UNIX_EPOCH},
};

use bip324::{
serde::{deserialize, serialize, NetworkMessage},
Handshake, PacketType,
};
use bitcoincore_rpc::{
bitcoin::p2p::{message_network::VersionMessage, Address, ServiceFlags},
RpcApi,
};
use bitcoin::p2p::{message_network::VersionMessage, Address, ServiceFlags};

let rpc = bitcoincore_rpc::Client::new(
HOST,
bitcoincore_rpc::Auth::UserPass(RPC_USER.into(), RPC_PASSWORD.into()),
)
.unwrap();
let mut stream = TcpStream::connect(format!("127.0.0.1:{PORT}")).unwrap();
let mut public_key = [0u8; 64];
let mut handshake = Handshake::new(
Expand All @@ -101,14 +90,14 @@ fn regtest_handshake() {
&mut public_key,
)
.unwrap();
dbg!("Writing public key to the remote node");
println!("Writing public key to the remote node");
stream.write_all(&public_key).unwrap();
stream.flush().unwrap();
let mut remote_public_key = [0u8; 64];
dbg!("Reading the remote node public key");
println!("Reading the remote node public key");
stream.read_exact(&mut remote_public_key).unwrap();
let mut local_garbage_terminator_message = [0u8; 36];
dbg!("Sending our garbage terminator");
println!("Sending our garbage terminator");
handshake
.complete_materials(
remote_public_key,
Expand All @@ -119,14 +108,14 @@ fn regtest_handshake() {
stream.write_all(&local_garbage_terminator_message).unwrap();
stream.flush().unwrap();
let mut max_response = [0; 4096];
dbg!("Reading the response buffer");
println!("Reading the response buffer");
let size = stream.read(&mut max_response).unwrap();
let response = &mut max_response[..size];
dbg!("Authenticating the handshake");
println!("Authenticating the handshake");
handshake
.authenticate_garbage_and_version_with_alloc(response)
.unwrap();
dbg!("Finalizing the handshake");
println!("Finalizing the handshake");
let packet_handler = handshake.finalize().unwrap();
let (mut decrypter, mut encrypter) = packet_handler.into_split();
let now = SystemTime::now()
Expand All @@ -150,9 +139,9 @@ fn regtest_handshake() {
let packet = encrypter
.encrypt_packet_with_alloc(&message, None, PacketType::Genuine)
.unwrap();
dbg!("Serializing and writing version message");
println!("Serializing and writing version message");
stream.write_all(&packet).unwrap();
dbg!("Reading the response length buffer");
println!("Reading the response length buffer");
let mut response_len = [0; 3];
stream.read_exact(&mut response_len).unwrap();
let message_len = decrypter.decypt_len(response_len);
Expand All @@ -162,8 +151,5 @@ fn regtest_handshake() {
.decrypt_payload_with_alloc(&response_message, None)
.unwrap();
let message = deserialize(msg.contents()).unwrap();
dbg!("{}", message.cmd());
assert_eq!(message.cmd(), "version");
rpc.stop().unwrap();
std::thread::sleep(Duration::from_secs(1));
}

0 comments on commit b8275f1

Please sign in to comment.