Skip to content

Commit f773496

Browse files
committed
elementsd-tests: consolidate setup code
1 parent 7bbf981 commit f773496

3 files changed

Lines changed: 164 additions & 164 deletions

File tree

elementsd-tests/src/lib.rs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,163 @@
11

2+
#[cfg(test)]
23
mod pset;
4+
#[cfg(test)]
35
mod taproot;
46

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+
let mut bitcoind = None;
141+
if validate_pegin {
142+
let bitcoind_exe = bitcoind::exe_path().unwrap();
143+
let bitcoind_conf = bitcoind::Conf::default();
144+
bitcoind = Some(bitcoind::BitcoinD::with_conf(&bitcoind_exe, &bitcoind_conf).unwrap());
145+
}
146+
147+
let conf = elementsd::Conf::new(bitcoind.as_ref());
148+
149+
let elementsd = ElementsD::with_conf(elementsd::exe_path().unwrap(), &conf).unwrap();
150+
151+
let create = elementsd.call("createwallet", &["wallet".into()]);
152+
assert_eq!(create.get("name").unwrap(), "wallet");
153+
154+
let rescan = elementsd.call("rescanblockchain", &[]);
155+
assert_eq!(rescan.get("stop_height").unwrap(), 0);
156+
157+
let balances = elementsd.call("getbalances", &[]);
158+
let mine = balances.get("mine").unwrap();
159+
let trusted = mine.get("trusted").unwrap();
160+
assert_eq!(trusted.get("bitcoin").unwrap(), 21.0);
161+
162+
(elementsd, bitcoind)
163+
}

elementsd-tests/src/pset.rs

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
extern crate elements;
44

55
extern crate bitcoin;
6-
#[cfg(feature = "integration")]
76
extern crate elementsd;
87
extern crate rand;
98

9+
use crate::setup;
10+
1011
use bitcoin::hashes::hex::ToHex;
1112
use bitcoin::{Address, Amount};
1213
use elements::bitcoin::hashes::hex::FromHex;
@@ -22,21 +23,6 @@ use elementsd::{bitcoind, ElementsD};
2223
use rand::distributions::{Distribution, Uniform};
2324
use std::str::FromStr;
2425

25-
trait Call {
26-
fn call(&self, cmd: &str, args: &[Value]) -> Value;
27-
fn decode_psbt(&self, psbt: &str) -> Option<Value>;
28-
fn get_new_address(&self) -> String;
29-
fn get_pegin_address(&self) -> (String, String);
30-
fn wallet_create_funded_psbt(&self, address: &str) -> String;
31-
fn expected_next(&self, psbt: &str) -> String;
32-
fn wallet_process_psbt(&self, psbt: &str) -> String;
33-
fn finalize_psbt(&self, psbt: &str) -> String;
34-
fn test_mempool_accept(&self, hex: &str) -> bool;
35-
fn get_first_prevout(&self) -> OutPoint;
36-
fn generate(&self, blocks: u32);
37-
fn get_balances(&self) -> Value;
38-
}
39-
4026
#[test]
4127
fn tx_unblinded() {
4228
let (elementsd, _bitcoind) = setup(false);
@@ -194,27 +180,11 @@ impl Call for ElementsD {
194180
.to_string()
195181
}
196182

197-
fn get_pegin_address(&self) -> (String, String) {
198-
let value = self.call("getpeginaddress", &[]);
199-
let mainchain_address = value.get("mainchain_address").unwrap();
200-
let mainchain_address = mainchain_address.as_str().unwrap().to_string();
201-
let claim_script = value.get("claim_script").unwrap();
202-
let claim_script = claim_script.as_str().unwrap().to_string();
203-
(mainchain_address, claim_script)
204-
}
205-
206183
fn generate(&self, blocks: u32) {
207184
let address = self.get_new_address();
208185
let _value = self.call("generatetoaddress", &[blocks.into(), address.into()]);
209186
}
210187

211-
fn wallet_create_funded_psbt(&self, address: &str) -> String {
212-
let value = self.call(
213-
"walletcreatefundedpsbt",
214-
&[json!([]), json!([{address.to_string(): "1"}])],
215-
);
216-
value.get("psbt").unwrap().as_str().unwrap().to_string()
217-
}
218188

219189
fn expected_next(&self, base64: &str) -> String {
220190
let value = self.call("analyzepsbt", &[base64.into()]);
@@ -231,14 +201,6 @@ impl Call for ElementsD {
231201
value.get("hex").unwrap().as_str().unwrap().to_string()
232202
}
233203

234-
fn get_first_prevout(&self) -> OutPoint {
235-
let value = self.call("listunspent", &[]);
236-
let first = value.get(0).unwrap();
237-
let txid = first.get("txid").unwrap().as_str().unwrap();
238-
let vout = first.get("vout").unwrap().as_u64().unwrap();
239-
240-
OutPoint::new(Txid::from_hex(txid).unwrap(), vout as u32)
241-
}
242204
fn get_balances(&self) -> Value {
243205
self.call("getbalances", &[])
244206
}
@@ -255,28 +217,3 @@ fn psbt_from_base64(base64: &str) -> PartiallySignedTransaction {
255217
deserialize(&bytes).unwrap()
256218
}
257219

258-
fn setup(validate_pegin: bool) -> (ElementsD, Option<BitcoinD>) {
259-
let mut bitcoind = None;
260-
if validate_pegin {
261-
let bitcoind_exe = bitcoind::exe_path().unwrap();
262-
let bitcoind_conf = bitcoind::Conf::default();
263-
bitcoind = Some(bitcoind::BitcoinD::with_conf(&bitcoind_exe, &bitcoind_conf).unwrap());
264-
}
265-
266-
let conf = elementsd::Conf::new(bitcoind.as_ref());
267-
268-
let elementsd = ElementsD::with_conf(elementsd::exe_path().unwrap(), &conf).unwrap();
269-
270-
let create = elementsd.call("createwallet", &["wallet".into()]);
271-
assert_eq!(create.get("name").unwrap(), "wallet");
272-
273-
let rescan = elementsd.call("rescanblockchain", &[]);
274-
assert_eq!(rescan.get("stop_height").unwrap(), 0);
275-
276-
let balances = elementsd.call("getbalances", &[]);
277-
let mine = balances.get("mine").unwrap();
278-
let trusted = mine.get("trusted").unwrap();
279-
assert_eq!(trusted.get("bitcoin").unwrap(), 21.0);
280-
281-
(elementsd, bitcoind)
282-
}

elementsd-tests/src/taproot.rs

Lines changed: 3 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ extern crate elements;
33
extern crate elementsd;
44
extern crate rand;
55

6+
use crate::{Call, setup};
7+
68
use elements::bitcoin::{Amount, XOnlyPublicKey, KeyPair};
79
use elements::bitcoin::hashes::hex::FromHex;
810
use elements::confidential::{AssetBlindingFactor, ValueBlindingFactor};
@@ -18,28 +20,13 @@ use elements::{
1820
Sequence, TxInWitness, TxOut, Txid,
1921
};
2022
use elements::{AddressParams, Transaction, TxIn, TxOutSecrets};
21-
use elementsd::bitcoincore_rpc::jsonrpc::serde_json::{json, Value};
22-
use elementsd::bitcoincore_rpc::RpcApi;
23-
use elementsd::bitcoind::BitcoinD;
24-
use elementsd::{bitcoind, ElementsD};
23+
use elementsd::ElementsD;
2524
use rand::{rngs, thread_rng};
2625
use secp256k1_zkp::Secp256k1;
2726
use std::str::FromStr;
2827

2928
static PARAMS: AddressParams = AddressParams::ELEMENTS;
3029

31-
trait Call {
32-
fn call(&self, cmd: &str, args: &[Value]) -> Value;
33-
fn decode_psbt(&self, psbt: &str) -> Option<Value>;
34-
fn get_new_address(&self) -> String;
35-
fn send_to_address(&self, addr: &str, amt: &str) -> String;
36-
fn get_transaction(&self, txid: &str) -> String;
37-
fn get_block_hash(&self, id: u32) -> String;
38-
fn test_mempool_accept(&self, hex: &str) -> bool;
39-
fn send_raw_transaction(&self, hex: &str) -> String;
40-
fn generate(&self, blocks: u32);
41-
}
42-
4330
fn gen_keypair(
4431
secp: &secp256k1_zkp::Secp256k1<secp256k1_zkp::All>,
4532
rng: &mut rngs::ThreadRng,
@@ -321,86 +308,3 @@ fn taproot_tests() {
321308
}
322309
}
323310

324-
impl Call for ElementsD {
325-
fn call(&self, cmd: &str, args: &[Value]) -> Value {
326-
self.client().call::<Value>(cmd, args).unwrap()
327-
}
328-
329-
fn decode_psbt(&self, psbt: &str) -> Option<Value> {
330-
self.client()
331-
.call::<Value>("decodepsbt", &[psbt.into()])
332-
.ok()
333-
}
334-
335-
fn get_new_address(&self) -> String {
336-
self.call("getnewaddress", &[])
337-
.as_str()
338-
.unwrap()
339-
.to_string()
340-
}
341-
342-
fn get_transaction(&self, txid: &str) -> String {
343-
self.call("gettransaction", &[txid.into()])["hex"]
344-
.as_str()
345-
.unwrap()
346-
.to_string()
347-
}
348-
349-
fn send_to_address(&self, addr: &str, amt: &str) -> String {
350-
self.call("sendtoaddress", &[addr.into(), amt.into()])
351-
.as_str()
352-
.unwrap()
353-
.to_string()
354-
}
355-
356-
fn send_raw_transaction(&self, tx: &str) -> String {
357-
self.call("sendrawtransaction", &[tx.into()])
358-
.as_str()
359-
.unwrap()
360-
.to_string()
361-
}
362-
363-
fn get_block_hash(&self, id: u32) -> String {
364-
self.call("getblockhash", &[id.into()])
365-
.as_str()
366-
.unwrap()
367-
.to_string()
368-
}
369-
370-
fn generate(&self, blocks: u32) {
371-
let address = self.get_new_address();
372-
let _value = self.call("generatetoaddress", &[blocks.into(), address.into()]);
373-
}
374-
375-
fn test_mempool_accept(&self, hex: &str) -> bool {
376-
let result = self.call("testmempoolaccept", &[json!([hex])]);
377-
let allowed = result.get(0).unwrap().get("allowed");
378-
allowed.unwrap().as_bool().unwrap()
379-
}
380-
}
381-
382-
fn setup(validate_pegin: bool) -> (ElementsD, Option<BitcoinD>) {
383-
let mut bitcoind = None;
384-
if validate_pegin {
385-
let bitcoind_exe = bitcoind::exe_path().unwrap();
386-
let bitcoind_conf = bitcoind::Conf::default();
387-
bitcoind = Some(bitcoind::BitcoinD::with_conf(&bitcoind_exe, &bitcoind_conf).unwrap());
388-
}
389-
390-
let conf = elementsd::Conf::new(bitcoind.as_ref());
391-
392-
let elementsd = ElementsD::with_conf(elementsd::exe_path().unwrap(), &conf).unwrap();
393-
394-
let create = elementsd.call("createwallet", &["wallet".into()]);
395-
assert_eq!(create.get("name").unwrap(), "wallet");
396-
397-
let rescan = elementsd.call("rescanblockchain", &[]);
398-
assert_eq!(rescan.get("stop_height").unwrap(), 0);
399-
400-
let balances = elementsd.call("getbalances", &[]);
401-
let mine = balances.get("mine").unwrap();
402-
let trusted = mine.get("trusted").unwrap();
403-
assert_eq!(trusted.get("bitcoin").unwrap(), 21.0);
404-
405-
(elementsd, bitcoind)
406-
}

0 commit comments

Comments
 (0)