Skip to content

Commit 4a9dfd9

Browse files
committed
chore: introduced bitcoin module structure, #6250
1 parent 3429977 commit 4a9dfd9

17 files changed

+187
-130
lines changed

stacks-node/src/tests/bitcoin_regtest.rs renamed to stacks-node/src/burnchains/bitcoin/core_controller.rs

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
//! including utilities to quickly start and stop instances for testing or
2020
//! development purposes.
2121
22-
use std::env;
2322
use std::io::{BufRead, BufReader};
2423
use std::process::{Child, Command, Stdio};
2524

@@ -189,119 +188,3 @@ impl Drop for BitcoinCoreController {
189188
self.kill_bitcoind().unwrap();
190189
}
191190
}
192-
193-
#[cfg(test)]
194-
mod tests {
195-
use std::path::Path;
196-
197-
use super::*;
198-
use crate::{BitcoinRegtestController, BurnchainController};
199-
mod utils {
200-
use std::net::TcpListener;
201-
202-
use stacks::util::get_epoch_time_nanos;
203-
204-
use super::*;
205-
206-
pub fn create_config() -> Config {
207-
let mut config = Config::default();
208-
config.burnchain.magic_bytes = "T3".as_bytes().into();
209-
config.burnchain.username = Some(String::from("user"));
210-
config.burnchain.password = Some(String::from("12345"));
211-
// overriding default "0.0.0.0" because doesn't play nicely on Windows.
212-
config.burnchain.peer_host = String::from("127.0.0.1");
213-
// avoiding peer port biding to reduce the number of ports to bind to.
214-
config.burnchain.peer_port = BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
215-
216-
//Ask the OS for a free port. Not guaranteed to stay free,
217-
//after TcpListner is dropped, but good enough for testing
218-
//and starting bitcoind right after config is created
219-
let tmp_listener =
220-
TcpListener::bind("127.0.0.1:0").expect("Failed to bind to get a free port");
221-
let port = tmp_listener.local_addr().unwrap().port();
222-
223-
config.burnchain.rpc_port = port;
224-
225-
let now = get_epoch_time_nanos();
226-
let dir = format!("/tmp/regtest-ctrl-{port}-{now}");
227-
config.node.working_dir = dir;
228-
229-
config
230-
}
231-
}
232-
233-
#[test]
234-
#[ignore]
235-
fn test_bitcoind_start_and_stop() {
236-
if env::var("BITCOIND_TEST") != Ok("1".into()) {
237-
return;
238-
}
239-
240-
let config = utils::create_config();
241-
let data_path_str = config.get_burnchain_path_str();
242-
let data_path = Path::new(data_path_str.as_str());
243-
244-
let mut bitcoind = BitcoinCoreController::from_stx_config(&config);
245-
246-
bitcoind.start_bitcoind().expect("should start!");
247-
assert!(bitcoind.is_running(), "should be running after start!");
248-
assert!(data_path.exists(), "data path should exists after start!");
249-
250-
bitcoind.stop_bitcoind().expect("should stop!");
251-
assert!(!bitcoind.is_running(), "should not be running after stop!");
252-
assert!(data_path.exists(), "data path should exists after stop!");
253-
}
254-
255-
#[test]
256-
#[ignore]
257-
fn test_bitcoind_start_and_kill() {
258-
if env::var("BITCOIND_TEST") != Ok("1".into()) {
259-
return;
260-
}
261-
262-
let config = utils::create_config();
263-
let data_path_str = config.get_burnchain_path_str();
264-
let data_path = Path::new(data_path_str.as_str());
265-
266-
let mut bitcoind = BitcoinCoreController::from_stx_config(&config);
267-
268-
bitcoind.start_bitcoind().expect("should start!");
269-
assert!(bitcoind.is_running(), "should be running after start!");
270-
assert!(data_path.exists(), "data path should exists after start!");
271-
272-
bitcoind.kill_bitcoind().expect("should kill!");
273-
assert!(!bitcoind.is_running(), "should not be running after kill!");
274-
assert!(data_path.exists(), "data path should exists after kill!");
275-
}
276-
277-
#[test]
278-
#[ignore]
279-
fn test_bitcoind_restart_with_bootstrapped_chain_data() {
280-
if env::var("BITCOIND_TEST") != Ok("1".into()) {
281-
return;
282-
}
283-
284-
let config = utils::create_config();
285-
286-
let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
287-
btcd_controller
288-
.start_bitcoind()
289-
.expect("Failed starting bitcoind");
290-
291-
let btc_controller = BitcoinRegtestController::new(config, None);
292-
btc_controller.bootstrap_chain(201);
293-
info!("Chain bootstrapped...");
294-
295-
btcd_controller
296-
.stop_bitcoind()
297-
.expect("Failed to stop bitcoind");
298-
299-
btcd_controller
300-
.start_bitcoind()
301-
.expect("Failed to restart bitcoind");
302-
303-
btcd_controller
304-
.stop_bitcoind()
305-
.expect("Failed to re-stop bitcoind");
306-
}
307-
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (C) 2025 Stacks Open Internet Foundation
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
//! Bitcoin Module
17+
//!
18+
//! Entry point for all bitcoin related modules
19+
20+
#[cfg(test)]
21+
pub mod core_controller;

stacks-node/src/burnchains/bitcoin_regtest_controller.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,7 +2823,7 @@ mod tests {
28232823
use stacks_common::util::secp256k1::Secp256k1PrivateKey;
28242824

28252825
use super::*;
2826-
use crate::tests::bitcoin_regtest::BitcoinCoreController;
2826+
use crate::burnchains::bitcoin::core_controller::BitcoinCoreController;
28272827
use crate::Keychain;
28282828

28292829
mod utils {
@@ -2834,7 +2834,7 @@ mod tests {
28342834
use stacks::util::vrf::{VRFPrivateKey, VRFPublicKey};
28352835

28362836
use super::*;
2837-
use crate::tests::bitcoin_regtest::BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
2837+
use crate::burnchains::bitcoin::core_controller::BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
28382838
use crate::util::get_epoch_time_nanos;
28392839

28402840
pub fn create_config() -> Config {

stacks-node/src/burnchains/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod bitcoin;
12
pub mod bitcoin_regtest_controller;
23
pub mod mocknet_controller;
34
pub mod rpc;
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright (C) 2025 Stacks Open Internet Foundation
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
//! Integration tests for [`BitcoinCoreController`]
17+
18+
use std::env;
19+
use std::path::Path;
20+
21+
use crate::burnchains::bitcoin::core_controller::BitcoinCoreController;
22+
use crate::{BitcoinRegtestController, BurnchainController};
23+
24+
mod utils {
25+
use std::net::TcpListener;
26+
27+
use stacks::config::Config;
28+
use stacks::util::get_epoch_time_nanos;
29+
30+
use crate::burnchains::bitcoin::core_controller::BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
31+
32+
pub fn create_config() -> Config {
33+
let mut config = Config::default();
34+
config.burnchain.magic_bytes = "T3".as_bytes().into();
35+
config.burnchain.username = Some(String::from("user"));
36+
config.burnchain.password = Some(String::from("12345"));
37+
// overriding default "0.0.0.0" because doesn't play nicely on Windows.
38+
config.burnchain.peer_host = String::from("127.0.0.1");
39+
// avoiding peer port biding to reduce the number of ports to bind to.
40+
config.burnchain.peer_port = BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
41+
42+
//Ask the OS for a free port. Not guaranteed to stay free,
43+
//after TcpListner is dropped, but good enough for testing
44+
//and starting bitcoind right after config is created
45+
let tmp_listener =
46+
TcpListener::bind("127.0.0.1:0").expect("Failed to bind to get a free port");
47+
let port = tmp_listener.local_addr().unwrap().port();
48+
49+
config.burnchain.rpc_port = port;
50+
51+
let now = get_epoch_time_nanos();
52+
let dir = format!("/tmp/regtest-ctrl-{port}-{now}");
53+
config.node.working_dir = dir;
54+
55+
config
56+
}
57+
}
58+
59+
#[test]
60+
#[ignore]
61+
fn test_bitcoind_start_and_stop() {
62+
if env::var("BITCOIND_TEST") != Ok("1".into()) {
63+
return;
64+
}
65+
66+
let config = utils::create_config();
67+
let data_path_str = config.get_burnchain_path_str();
68+
let data_path = Path::new(data_path_str.as_str());
69+
70+
let mut bitcoind = BitcoinCoreController::from_stx_config(&config);
71+
72+
bitcoind.start_bitcoind().expect("should start!");
73+
assert!(bitcoind.is_running(), "should be running after start!");
74+
assert!(data_path.exists(), "data path should exists after start!");
75+
76+
bitcoind.stop_bitcoind().expect("should stop!");
77+
assert!(!bitcoind.is_running(), "should not be running after stop!");
78+
assert!(data_path.exists(), "data path should exists after stop!");
79+
}
80+
81+
#[test]
82+
#[ignore]
83+
fn test_bitcoind_start_and_kill() {
84+
if env::var("BITCOIND_TEST") != Ok("1".into()) {
85+
return;
86+
}
87+
88+
let config = utils::create_config();
89+
let data_path_str = config.get_burnchain_path_str();
90+
let data_path = Path::new(data_path_str.as_str());
91+
92+
let mut bitcoind = BitcoinCoreController::from_stx_config(&config);
93+
94+
bitcoind.start_bitcoind().expect("should start!");
95+
assert!(bitcoind.is_running(), "should be running after start!");
96+
assert!(data_path.exists(), "data path should exists after start!");
97+
98+
bitcoind.kill_bitcoind().expect("should kill!");
99+
assert!(!bitcoind.is_running(), "should not be running after kill!");
100+
assert!(data_path.exists(), "data path should exists after kill!");
101+
}
102+
103+
#[test]
104+
#[ignore]
105+
fn test_bitcoind_restart_with_bootstrapped_chain_data() {
106+
if env::var("BITCOIND_TEST") != Ok("1".into()) {
107+
return;
108+
}
109+
110+
let config = utils::create_config();
111+
112+
let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
113+
btcd_controller
114+
.start_bitcoind()
115+
.expect("Failed starting bitcoind");
116+
117+
let btc_controller = BitcoinRegtestController::new(config, None);
118+
btc_controller.bootstrap_chain(201);
119+
info!("Chain bootstrapped...");
120+
121+
btcd_controller
122+
.stop_bitcoind()
123+
.expect("Failed to stop bitcoind");
124+
125+
btcd_controller
126+
.start_bitcoind()
127+
.expect("Failed to restart bitcoind");
128+
129+
btcd_controller
130+
.stop_bitcoind()
131+
.expect("Failed to re-stop bitcoind");
132+
}

stacks-node/src/tests/bitcoin/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (C) 2025 Stacks Open Internet Foundation
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
//! Bitcoin Integration Test Module
17+
//!
18+
//! Entry point for all bitcoin related test modules
19+
20+
mod core_controller_integrations;

stacks-node/src/tests/bitcoin_rpc_integrations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ use stacks::burnchains::bitcoin::BitcoinNetworkType;
2222
use stacks::core::BITCOIN_REGTEST_FIRST_BLOCK_HASH;
2323
use stacks::types::chainstate::BurnchainHeaderHash;
2424

25+
use crate::burnchains::bitcoin::core_controller::BitcoinCoreController;
2526
use crate::burnchains::rpc::bitcoin_rpc_client::test_utils::AddressType;
2627
use crate::burnchains::rpc::bitcoin_rpc_client::{
2728
BitcoinRpcClient, BitcoinRpcClientError, ImportDescriptorsRequest, Timestamp,
2829
};
2930
use crate::burnchains::rpc::rpc_transport::RpcError;
30-
use crate::tests::bitcoin_regtest::BitcoinCoreController;
3131

3232
mod utils {
3333
use std::net::TcpListener;

stacks-node/src/tests/epoch_205.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use stacks_common::codec::StacksMessageCodec;
2424
use stacks_common::types::chainstate::{BlockHeaderHash, BurnchainHeaderHash, VRFSeed};
2525
use stacks_common::util::hash::hex_bytes;
2626

27-
use crate::tests::bitcoin_regtest::BitcoinCoreController;
27+
use crate::burnchains::bitcoin::core_controller::BitcoinCoreController;
2828
use crate::tests::neon_integrations::*;
2929
use crate::tests::{run_until_burnchain_height, select_transactions_where};
3030
use crate::{neon, BitcoinRegtestController, BurnchainController, Keychain};

stacks-node/src/tests/epoch_21.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ use stacks_common::util::hash::{Hash160, Sha256Sum};
3636
use stacks_common::util::secp256k1::{Secp256k1PrivateKey, Secp256k1PublicKey};
3737
use stacks_common::util::sleep_ms;
3838

39+
use crate::burnchains::bitcoin::core_controller::BitcoinCoreController;
3940
use crate::burnchains::bitcoin_regtest_controller::UTXO;
4041
use crate::neon::RunLoopCounter;
4142
use crate::operations::BurnchainOpSigner;
4243
use crate::stacks_common::address::AddressHashMode;
4344
use crate::stacks_common::types::Address;
4445
use crate::stacks_common::util::hash::{bytes_to_hex, hex_bytes};
45-
use crate::tests::bitcoin_regtest::BitcoinCoreController;
4646
use crate::tests::neon_integrations::*;
4747
use crate::tests::*;
4848
use crate::{neon, BitcoinRegtestController, BurnchainController, Keychain};

stacks-node/src/tests/epoch_22.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use stacks_common::util::secp256k1::Secp256k1PublicKey;
1919
use stacks_common::util::sleep_ms;
2020

2121
use super::neon_integrations::get_account;
22+
use crate::burnchains::bitcoin::core_controller::BitcoinCoreController;
2223
use crate::neon_node::StacksNode;
2324
use crate::stacks_common::types::Address;
2425
use crate::stacks_common::util::hash::bytes_to_hex;
25-
use crate::tests::bitcoin_regtest::BitcoinCoreController;
2626
use crate::tests::epoch_21::wait_pox_stragglers;
2727
use crate::tests::neon_integrations::*;
2828
use crate::tests::*;

0 commit comments

Comments
 (0)