|
| 1 | + |
| 2 | +#[cfg(test)] |
| 3 | +mod pset; |
| 4 | +#[cfg(test)] |
| 5 | +mod taproot; |
| 6 | + |
| 7 | +use elementsd::bitcoincore_rpc::RpcApi; |
| 8 | +use elementsd::bitcoincore_rpc::jsonrpc::serde_json::{json, Value}; |
| 9 | +#[cfg(test)] |
| 10 | +use elementsd::bitcoind::{self, BitcoinD}; |
| 11 | +use elementsd::ElementsD; |
| 12 | +use std::str::FromStr; |
| 13 | + |
| 14 | +trait Call { |
| 15 | + fn call(&self, cmd: &str, args: &[Value]) -> Value; |
| 16 | + fn decode_psbt(&self, psbt: &str) -> Option<Value>; |
| 17 | + fn get_new_address(&self) -> String; |
| 18 | + fn get_pegin_address(&self) -> (String, String); |
| 19 | + fn wallet_create_funded_psbt(&self, address: &str) -> String; |
| 20 | + fn expected_next(&self, psbt: &str) -> String; |
| 21 | + fn wallet_process_psbt(&self, psbt: &str) -> String; |
| 22 | + fn finalize_psbt(&self, psbt: &str) -> String; |
| 23 | + fn test_mempool_accept(&self, hex: &str) -> bool; |
| 24 | + fn get_first_prevout(&self) -> elements::OutPoint; |
| 25 | + fn generate(&self, blocks: u32); |
| 26 | + fn get_balances(&self) -> Value; |
| 27 | + |
| 28 | + fn send_to_address(&self, addr: &str, amt: &str) -> String; |
| 29 | + fn get_transaction(&self, txid: &str) -> String; |
| 30 | + fn get_block_hash(&self, id: u32) -> String; |
| 31 | + fn send_raw_transaction(&self, hex: &str) -> String; |
| 32 | +} |
| 33 | + |
| 34 | +impl Call for ElementsD { |
| 35 | + fn call(&self, cmd: &str, args: &[Value]) -> Value { |
| 36 | + self.client().call::<Value>(cmd, args).unwrap() |
| 37 | + } |
| 38 | + |
| 39 | + fn decode_psbt(&self, psbt: &str) -> Option<Value> { |
| 40 | + self.client() |
| 41 | + .call::<Value>("decodepsbt", &[psbt.into()]) |
| 42 | + .ok() |
| 43 | + } |
| 44 | + |
| 45 | + fn get_new_address(&self) -> String { |
| 46 | + self.call("getnewaddress", &[]) |
| 47 | + .as_str() |
| 48 | + .unwrap() |
| 49 | + .to_string() |
| 50 | + } |
| 51 | + |
| 52 | + fn get_pegin_address(&self) -> (String, String) { |
| 53 | + let value = self.call("getpeginaddress", &[]); |
| 54 | + let mainchain_address = value.get("mainchain_address").unwrap(); |
| 55 | + let mainchain_address = mainchain_address.as_str().unwrap().to_string(); |
| 56 | + let claim_script = value.get("claim_script").unwrap(); |
| 57 | + let claim_script = claim_script.as_str().unwrap().to_string(); |
| 58 | + (mainchain_address, claim_script) |
| 59 | + } |
| 60 | + |
| 61 | + fn wallet_create_funded_psbt(&self, address: &str) -> String { |
| 62 | + let value = self.call( |
| 63 | + "walletcreatefundedpsbt", |
| 64 | + &[json!([]), json!([{address.to_string(): "1"}])], |
| 65 | + ); |
| 66 | + value.get("psbt").unwrap().as_str().unwrap().to_string() |
| 67 | + } |
| 68 | + |
| 69 | + fn expected_next(&self, base64: &str) -> String { |
| 70 | + let value = self.call("analyzepsbt", &[base64.into()]); |
| 71 | + value.get("next").unwrap().as_str().unwrap().to_string() |
| 72 | + } |
| 73 | + |
| 74 | + fn wallet_process_psbt(&self, base64: &str) -> String { |
| 75 | + let value = self.call("walletprocesspsbt", &[base64.into()]); |
| 76 | + value.get("psbt").unwrap().as_str().unwrap().to_string() |
| 77 | + } |
| 78 | + |
| 79 | + fn finalize_psbt(&self, base64: &str) -> String { |
| 80 | + let value = self.call("finalizepsbt", &[base64.into()]); |
| 81 | + value.get("hex").unwrap().as_str().unwrap().to_string() |
| 82 | + } |
| 83 | + |
| 84 | + fn test_mempool_accept(&self, hex: &str) -> bool { |
| 85 | + let result = self.call("testmempoolaccept", &[json!([hex])]); |
| 86 | + let allowed = result.get(0).unwrap().get("allowed"); |
| 87 | + allowed.unwrap().as_bool().unwrap() |
| 88 | + } |
| 89 | + |
| 90 | + fn get_first_prevout(&self) -> elements::OutPoint { |
| 91 | + let value = self.call("listunspent", &[]); |
| 92 | + let first = value.get(0).unwrap(); |
| 93 | + let txid = first.get("txid").unwrap().as_str().unwrap(); |
| 94 | + let vout = first.get("vout").unwrap().as_u64().unwrap(); |
| 95 | + |
| 96 | + elements::OutPoint::new(elements::Txid::from_str(txid).unwrap(), vout as u32) |
| 97 | + } |
| 98 | + |
| 99 | + fn generate(&self, blocks: u32) { |
| 100 | + let address = self.get_new_address(); |
| 101 | + let _value = self.call("generatetoaddress", &[blocks.into(), address.into()]); |
| 102 | + } |
| 103 | + |
| 104 | + fn get_balances(&self) -> Value { |
| 105 | + self.call("getbalances", &[]) |
| 106 | + } |
| 107 | + |
| 108 | + fn get_transaction(&self, txid: &str) -> String { |
| 109 | + self.call("gettransaction", &[txid.into()])["hex"] |
| 110 | + .as_str() |
| 111 | + .unwrap() |
| 112 | + .to_string() |
| 113 | + } |
| 114 | + |
| 115 | + fn send_to_address(&self, addr: &str, amt: &str) -> String { |
| 116 | + self.call("sendtoaddress", &[addr.into(), amt.into()]) |
| 117 | + .as_str() |
| 118 | + .unwrap() |
| 119 | + .to_string() |
| 120 | + } |
| 121 | + |
| 122 | + fn send_raw_transaction(&self, tx: &str) -> String { |
| 123 | + self.call("sendrawtransaction", &[tx.into()]) |
| 124 | + .as_str() |
| 125 | + .unwrap() |
| 126 | + .to_string() |
| 127 | + } |
| 128 | + |
| 129 | + fn get_block_hash(&self, id: u32) -> String { |
| 130 | + self.call("getblockhash", &[id.into()]) |
| 131 | + .as_str() |
| 132 | + .unwrap() |
| 133 | + .to_string() |
| 134 | + } |
| 135 | + |
| 136 | +} |
| 137 | + |
| 138 | +#[cfg(test)] |
| 139 | +fn setup(validate_pegin: bool) -> (ElementsD, Option<BitcoinD>) { |
| 140 | + // Create env var BITCOIND_EXE_PATH to point to the ../bitcoind/bin/bitcoind binary |
| 141 | + let key = "BITCOIND_EXE"; |
| 142 | + if std::env::var(key).is_err() { |
| 143 | + let mut root_path = std::env::current_dir().unwrap(); |
| 144 | + while std::fs::metadata(root_path.join("LICENSE")).is_err() { |
| 145 | + if !root_path.pop() { |
| 146 | + panic!("Could not find LICENSE file; do not know where repo root is."); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + let bitcoind_path = root_path |
| 151 | + .join("elementsd-tests") |
| 152 | + .join("bin") |
| 153 | + .join("bitcoind"); |
| 154 | + std::env::set_var(key, bitcoind_path); |
| 155 | + } |
| 156 | + |
| 157 | + // Create env var BITCOIND_EXE_PATH to point to the ../bitcoind/bin/bitcoind binary |
| 158 | + let key = "ELEMENTSD_EXE"; |
| 159 | + if std::env::var(key).is_err() { |
| 160 | + let mut root_path = std::env::current_dir().unwrap(); |
| 161 | + while std::fs::metadata(root_path.join("LICENSE")).is_err() { |
| 162 | + if !root_path.pop() { |
| 163 | + panic!("Could not find LICENSE file; do not know where repo root is."); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + let bitcoind_path = root_path |
| 168 | + .join("elementsd-tests") |
| 169 | + .join("bin") |
| 170 | + .join("elementsd"); |
| 171 | + std::env::set_var(key, bitcoind_path); |
| 172 | + } |
| 173 | + |
| 174 | + let mut bitcoind = None; |
| 175 | + if validate_pegin { |
| 176 | + let bitcoind_exe = bitcoind::exe_path().unwrap(); |
| 177 | + let bitcoind_conf = bitcoind::Conf::default(); |
| 178 | + bitcoind = Some(bitcoind::BitcoinD::with_conf(&bitcoind_exe, &bitcoind_conf).unwrap()); |
| 179 | + } |
| 180 | + |
| 181 | + let conf = elementsd::Conf::new(bitcoind.as_ref()); |
| 182 | + |
| 183 | + let elementsd = ElementsD::with_conf(elementsd::exe_path().unwrap(), &conf).unwrap(); |
| 184 | + |
| 185 | + let create = elementsd.call("createwallet", &["wallet".into()]); |
| 186 | + assert_eq!(create.get("name").unwrap(), "wallet"); |
| 187 | + |
| 188 | + let rescan = elementsd.call("rescanblockchain", &[]); |
| 189 | + assert_eq!(rescan.get("stop_height").unwrap(), 0); |
| 190 | + |
| 191 | + let balances = elementsd.call("getbalances", &[]); |
| 192 | + let mine = balances.get("mine").unwrap(); |
| 193 | + let trusted = mine.get("trusted").unwrap(); |
| 194 | + assert_eq!(trusted.get("bitcoin").unwrap(), 21.0); |
| 195 | + |
| 196 | + (elementsd, bitcoind) |
| 197 | +} |
0 commit comments