1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use serde::{Deserialize, Serialize};
use crate::TestData;
#[derive(Serialize, Deserialize)]
pub struct Tx {
txid: String,
height: i32,
}
const SCRIPT_STATUS: &str = include_str!("../test_data/electrum_script_status.json");
pub fn script_status() -> Vec<TestData<Vec<Tx>, String>> {
serde_json::from_str(SCRIPT_STATUS).unwrap()
}
#[cfg(test)]
mod tests {
use electrsd::{
bitcoind::{self, bitcoincore_rpc::RpcApi, BitcoinD, P2P},
electrum_client::{
bitcoin::{hashes::hex::ToHex, Address, Amount},
ElectrumApi,
},
ElectrsD,
};
use crate::TestData;
use super::Tx;
#[ignore]
#[test]
fn create_test_vectors() {
let exe = bitcoind::downloaded_exe_path().unwrap();
let mut conf = bitcoind::Conf::default();
conf.p2p = P2P::Yes;
let bitcoind = BitcoinD::with_conf(&exe, &conf).unwrap();
let exe = electrsd::downloaded_exe_path().unwrap();
let electrsd = ElectrsD::new(&exe, &bitcoind).unwrap();
let mut result = vec![];
let address = bitcoind.client.get_new_address(None, None).unwrap();
let address_accumulate = bitcoind.client.get_new_address(None, None).unwrap();
for addr in [&address, &address_accumulate] {
electrsd
.client
.script_subscribe(&addr.script_pubkey())
.unwrap();
}
let _ = bitcoind.client.generate_to_address(1, &address).unwrap();
let _ = bitcoind
.client
.generate_to_address(100, &address_accumulate)
.unwrap();
electrsd.wait_height(102);
let (script_history, status) = ask_history_and_status(&electrsd, &address);
let test_data1 = make_test_data(script_history, status);
result.push(test_data1);
let txid = send_sat(&bitcoind, &address);
electrsd.wait_tx(&txid);
let (script_history, status) = ask_history_and_status(&electrsd, &address);
let test_data2 = make_test_data(script_history, status);
result.push(test_data2);
let _blocks = bitcoind
.client
.generate_to_address(1, &address_accumulate)
.unwrap();
electrsd.wait_height(103);
let (script_history, status) = ask_history_and_status(&electrsd, &address);
let test_data3 = make_test_data(script_history, status);
result.push(test_data3);
println!("{}", serde_json::to_string_pretty(&result).unwrap());
}
fn send_sat(
bitcoind: &BitcoinD,
address: &Address,
) -> electrsd::electrum_client::bitcoin::Txid {
bitcoind
.client
.send_to_address(
&address,
Amount::from_sat(10000),
None,
None,
None,
None,
None,
None,
)
.unwrap()
}
fn make_test_data(
script_history: Vec<electrsd::electrum_client::GetHistoryRes>,
status: Option<electrsd::electrum_client::Hex32Bytes>,
) -> TestData<Vec<Tx>, String> {
TestData {
input: script_history
.iter()
.map(|h| Tx {
txid: h.tx_hash.to_string(),
height: h.height,
})
.collect::<Vec<_>>(),
expected: status.unwrap().to_hex(),
}
}
fn ask_history_and_status(
electrsd: &ElectrsD,
address: &Address,
) -> (
Vec<electrsd::electrum_client::GetHistoryRes>,
Option<electrsd::electrum_client::Hex32Bytes>,
) {
let script_history = electrsd
.client
.script_get_history(&address.script_pubkey())
.unwrap();
dbg!(&script_history);
let status = electrsd
.client
.script_pop(&address.script_pubkey())
.unwrap();
(script_history, status)
}
}