# Setup Bitcoin Local Rust Test Environment ## Metadata **Status**:: #x **Zettel**:: #zettel/fleeting **Created**:: [[2024-03-12]] ## Synopsis Add the dependency [bitcoind](https://github.com/rust-bitcoin/bitcoind) to start the bitcoind process. Enable feature `22_1` to let it download the executable binary automatically. ```toml bitcoind = { version = "0.34.1", features = ["22_1"] } ``` The versions newer than 22 have a bad performance in macOS. See [this issue](https://github.com/bitcoin/bitcoin/pull/25985). Example to start the process and use the RPC: ```rust use bitcoind::bitcoincore_rpc::RpcApi as _; let bitcoind = bitcoind::BitcoinD::from_downloaded().unwrap(); let cl = &bitcoind.client; assert_eq!(0, cl.get_blockchain_info().unwrap().blocks) ``` This will start a local testnet chain in [Regtest Mode](https://developer.bitcoin.org/examples/testing.html#regtest-mode). Generate 101 blocks to make the 50 bitcoins reward for block #1 available. In Regtest, the first 150 blocks get 50 bitcoins reward, but they must wait for 100 blocks to become mature. ```rust fn mine(cl: &Client, n: usize) { let blocks = cl .generate_to_address( n as u64, &cl.get_new_address(None, None).unwrap().assume_checked(), ) .unwrap(); assert_eq!(blocks.len(), n); } mine(cl, 101); assert_eq!( cl.get_balance(Some(1), None).unwrap(), Amount::from_int_btc(50) ); ``` ## References - [rust-miniscript/bitcoind-tests/tests/test_desc.rs](https://github.com/rust-bitcoin/rust-miniscript/blob/master/bitcoind-tests/tests/test_desc.rs) - [ckb-pcn-cross-chain-poc/examples/schnorr-signature.rs](https://github.com/doitian/ckb-pcn-cross-chain-poc/blob/main/examples/schnorr-signature.rs)