|
| 1 | +use bitcoin::Amount; |
| 2 | +pub use corepc_node; |
| 3 | +use corepc_node::AddressType; |
| 4 | +use tracing::Level; |
| 5 | + |
| 6 | +use crate::BoxError; |
| 7 | + |
| 8 | +pub fn init_bitcoind() -> Result<corepc_node::Node, BoxError> { |
| 9 | + let bitcoind_exe = corepc_node::exe_path()?; |
| 10 | + let mut conf = corepc_node::Conf::default(); |
| 11 | + conf.view_stdout = tracing::enabled!(target: "corepc", Level::TRACE); |
| 12 | + // conf.args.push("-txindex"); |
| 13 | + let bitcoind = corepc_node::Node::with_conf(bitcoind_exe, &conf)?; |
| 14 | + Ok(bitcoind) |
| 15 | +} |
| 16 | + |
| 17 | +pub fn init_bitcoind_sender_receiver( |
| 18 | + sender_address_type: Option<AddressType>, |
| 19 | + receiver_address_type: Option<AddressType>, |
| 20 | +) -> Result<(corepc_node::Node, corepc_node::Client, corepc_node::Client), BoxError> { |
| 21 | + let bitcoind = init_bitcoind()?; |
| 22 | + let mut wallets = create_and_fund_wallets( |
| 23 | + &bitcoind, |
| 24 | + vec![("receiver", receiver_address_type), ("sender", sender_address_type)], |
| 25 | + )?; |
| 26 | + let receiver = wallets.pop().expect("receiver to exist"); |
| 27 | + let sender = wallets.pop().expect("sender to exist"); |
| 28 | + |
| 29 | + Ok((bitcoind, receiver, sender)) |
| 30 | +} |
| 31 | + |
| 32 | +fn create_and_fund_wallets<W: AsRef<str>>( |
| 33 | + bitcoind: &corepc_node::Node, |
| 34 | + wallets: Vec<(W, Option<AddressType>)>, |
| 35 | +) -> Result<Vec<corepc_node::Client>, BoxError> { |
| 36 | + let mut funded_wallets = vec![]; |
| 37 | + let funding_wallet = bitcoind.create_wallet("funding_wallet")?; |
| 38 | + let funding_address = funding_wallet.new_address()?; |
| 39 | + // 100 blocks would work here, we add a extra block to cover fees between transfers |
| 40 | + bitcoind.client.generate_to_address(101 + wallets.len(), &funding_address)?; |
| 41 | + for (wallet_name, address_type) in wallets { |
| 42 | + let wallet = bitcoind.create_wallet(wallet_name)?; |
| 43 | + let address = wallet.get_new_address(None, address_type)?.into_model()?.0.assume_checked(); |
| 44 | + funding_wallet.send_to_address(&address, Amount::from_btc(50.0)?)?; |
| 45 | + funded_wallets.push(wallet); |
| 46 | + } |
| 47 | + // Mine the block which funds the different wallets |
| 48 | + bitcoind.client.generate_to_address(1, &funding_address)?; |
| 49 | + |
| 50 | + for wallet in funded_wallets.iter() { |
| 51 | + let balances = wallet.get_balances()?.into_model()?; |
| 52 | + assert_eq!( |
| 53 | + balances.mine.trusted, |
| 54 | + Amount::from_btc(50.0)?, |
| 55 | + "wallet doesn't have expected amount of bitcoin" |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + Ok(funded_wallets) |
| 60 | +} |
| 61 | + |
| 62 | +pub fn init_bitcoind_multi_sender_single_reciever( |
| 63 | + number_of_senders: usize, |
| 64 | +) -> Result<(corepc_node::Node, Vec<corepc_node::Client>, corepc_node::Client), BoxError> { |
| 65 | + let bitcoind = init_bitcoind()?; |
| 66 | + let wallets_to_create = |
| 67 | + (0..number_of_senders + 1).map(|i| (format!("sender_{i}"), None)).collect::<Vec<_>>(); |
| 68 | + let mut wallets = create_and_fund_wallets(&bitcoind, wallets_to_create)?; |
| 69 | + let receiver = wallets.pop().expect("receiver to exist"); |
| 70 | + let senders = wallets; |
| 71 | + |
| 72 | + Ok((bitcoind, senders, receiver)) |
| 73 | +} |
0 commit comments