forked from romanz/electrs
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathcommon.rs
More file actions
398 lines (349 loc) · 12.7 KB
/
common.rs
File metadata and controls
398 lines (349 loc) · 12.7 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
use std::str::FromStr;
use std::sync::{Arc, Once, RwLock};
use std::{env, net};
use log::LevelFilter;
use stderrlog::StdErrLog;
use tempfile::TempDir;
use serde_json::json;
#[cfg(feature = "liquid")]
use serde_json::Value;
#[cfg(not(feature = "liquid"))]
use bitcoind::{self as noded, BitcoinD as NodeD};
#[cfg(feature = "liquid")]
use elementsd::{self as noded, ElementsD as NodeD};
use noded::bitcoincore_rpc::{self, RpcApi};
use electrs::{
chain::{Address, BlockHash, Network, Txid},
config::Config,
daemon::Daemon,
electrum::RPC as ElectrumRPC,
metrics::Metrics,
new_index::{ChainQuery, FetchFrom, Indexer, Mempool, Query, Store},
rest,
signal::Waiter,
};
pub struct TestRunner {
config: Arc<Config>,
/// bitcoind::BitcoinD or an elementsd::ElementsD in liquid mode
node: NodeD,
_electrsdb: TempDir, // rm'd when dropped
indexer: Indexer,
query: Arc<Query>,
daemon: Arc<Daemon>,
mempool: Arc<RwLock<Mempool>>,
metrics: Metrics,
}
impl TestRunner {
pub fn new() -> Result<TestRunner> {
let log = init_log();
// Setup the bitcoind/elementsd config
let mut node_conf = noded::Conf::default();
{
#[cfg(not(feature = "liquid"))]
let node_conf = &mut node_conf;
#[cfg(feature = "liquid")]
let node_conf = &mut node_conf.0;
#[cfg(feature = "liquid")]
node_conf.args.push("-anyonecanspendaremine=1");
node_conf.view_stdout = std::env::var_os("RUST_LOG").is_some();
}
// Setup node
let node = NodeD::with_conf(noded::exe_path().unwrap(), &node_conf).unwrap();
#[cfg(not(feature = "liquid"))]
let (node_client, params) = (&node.client, &node.params);
#[cfg(feature = "liquid")]
let (node_client, params) = (node.client(), &node.params());
log::info!("node params: {:?}", params);
generate(node_client, 101).chain_err(|| "failed initializing blocks")?;
// Needed to claim the initialfreecoins as our own
// See https://github.com/ElementsProject/elements/issues/956
#[cfg(feature = "liquid")]
node_client.call::<Value>("rescanblockchain", &[])?;
#[cfg(not(feature = "liquid"))]
let network_type = Network::Regtest;
#[cfg(feature = "liquid")]
let network_type = Network::LiquidRegtest;
let mut daemon_subdir = params.cookie_file.clone();
// drop `.cookie` filename, leaving just the network subdirectory
daemon_subdir.pop();
let electrsdb = tempfile::tempdir().unwrap();
let config = Arc::new(Config {
log,
network_type,
db_path: electrsdb.path().to_path_buf(),
daemon_dir: daemon_subdir.clone(),
blocks_dir: daemon_subdir.join("blocks"),
daemon_rpc_addr: params.rpc_socket.into(),
cookie: None,
electrum_rpc_addr: rand_available_addr(),
http_addr: rand_available_addr(),
http_socket_file: None, // XXX test with socket file or tcp?
monitoring_addr: rand_available_addr(),
jsonrpc_import: false,
light_mode: false,
address_search: true,
index_unspendables: false,
cors: None,
precache_scripts: None,
utxos_limit: 100,
electrum_txs_limit: 100,
electrum_banner: "".into(),
electrum_rpc_logging: None,
#[cfg(feature = "liquid")]
asset_db_path: None, // XXX
#[cfg(feature = "liquid")]
parent_network: bitcoin::Network::Regtest,
tx_cache_size: 100,
initial_sync_compaction: false,
//#[cfg(feature = "electrum-discovery")]
//electrum_public_hosts: Option<crate::electrum::ServerHosts>,
//#[cfg(feature = "electrum-discovery")]
//electrum_announce: bool,
//#[cfg(feature = "electrum-discovery")]
//tor_proxy: Option<std::net::SocketAddr>,
});
let signal = Waiter::start();
let metrics = Metrics::new(rand_available_addr());
metrics.start();
let daemon = Arc::new(Daemon::new(
&config.daemon_dir,
&config.blocks_dir,
config.daemon_rpc_addr,
config.cookie_getter(),
config.network_type,
signal.clone(),
&metrics,
)?);
let store = Arc::new(Store::open(&config.db_path.join("newindex"), &config));
let fetch_from = if !env::var("JSONRPC_IMPORT").is_ok() && !cfg!(feature = "liquid") {
// run the initial indexing from the blk files then switch to using the jsonrpc,
// similarly to how electrs is typically used.
FetchFrom::BlkFiles
} else {
// when JSONRPC_IMPORT is set, use the jsonrpc for the initial indexing too.
// this runs faster on small regtest chains and can be useful for quicker local development iteration.
// this is also used on liquid regtest, which currently fails to parse the BlkFiles due to the magic bytes
FetchFrom::Bitcoind
};
let mut indexer = Indexer::open(Arc::clone(&store), fetch_from, &config, &metrics);
indexer.update(&daemon)?;
indexer.fetch_from(FetchFrom::Bitcoind);
let chain = Arc::new(ChainQuery::new(
Arc::clone(&store),
Arc::clone(&daemon),
&config,
&metrics,
));
let mempool = Arc::new(RwLock::new(Mempool::new(
Arc::clone(&chain),
&metrics,
Arc::clone(&config),
)));
Mempool::update(&mempool, &daemon)?;
let query = Arc::new(Query::new(
Arc::clone(&chain),
Arc::clone(&mempool),
Arc::clone(&daemon),
Arc::clone(&config),
#[cfg(feature = "liquid")]
None, // TODO
));
Ok(TestRunner {
config,
node,
_electrsdb: electrsdb,
indexer,
query,
daemon,
mempool,
metrics,
})
}
pub fn node_client(&self) -> &bitcoincore_rpc::Client {
#[cfg(not(feature = "liquid"))]
return &self.node.client;
#[cfg(feature = "liquid")]
return &self.node.client();
}
pub fn sync(&mut self) -> Result<()> {
self.indexer.update(&self.daemon)?;
Mempool::update(&self.mempool, &self.daemon)?;
// force an update for the mempool stats, which are normally cached
self.mempool.write().unwrap().update_backlog_stats();
Ok(())
}
pub fn mine(&mut self) -> Result<BlockHash> {
let mut generated = generate(self.node_client(), 1)?;
self.sync()?;
Ok(generated.remove(0))
}
pub fn send(&mut self, addr: &Address, amount: bitcoin::Amount) -> Result<Txid> {
// Must use raw call() because send_to_address() expects a bitcoin::Address and not an elements::Address
let txid = self.node_client().call(
"sendtoaddress",
&[addr.to_string().into(), json!(amount.to_btc())],
)?;
self.sync()?;
Ok(txid)
}
#[cfg(feature = "liquid")]
pub fn send_asset(
&mut self,
addr: &Address,
amount: bitcoin::Amount,
assetid: elements::AssetId,
) -> Result<Txid> {
let txid = self.node_client().call(
"sendtoaddress",
&[
addr.to_string().into(),
json!(amount.to_btc()),
Value::Null,
Value::Null,
Value::Null,
Value::Null,
Value::Null,
Value::Null,
Value::Null,
json!(assetid),
],
)?;
self.sync()?;
Ok(txid)
}
/// Generate and return a new address.
/// Returns the unconfidential address in Liquid mode, to make it interchangeable with Bitcoin addresses in tests.
pub fn newaddress(&self) -> Result<Address> {
#[cfg(not(feature = "liquid"))]
return Ok(raw_new_address(self.node_client())?);
#[cfg(feature = "liquid")]
return Ok(self.ct_newaddress()?.1);
}
/// Generate a new address, returning both the confidential and non-confidential versions
#[cfg(feature = "liquid")]
pub fn ct_newaddress(&self) -> Result<(Address, Address)> {
let client = self.node_client();
let c_addr = raw_new_address(client)?;
let mut info = client.call::<Value>("getaddressinfo", &[c_addr.to_string().into()])?;
let uc_addr = serde_json::from_value(info["unconfidential"].take())?;
Ok((c_addr, uc_addr))
}
}
pub fn init_rest_tester() -> Result<(rest::Handle, net::SocketAddr, TestRunner)> {
let tester = TestRunner::new()?;
let rest_server = rest::start(Arc::clone(&tester.config), Arc::clone(&tester.query));
log::info!("REST server running on {}", tester.config.http_addr);
Ok((rest_server, tester.config.http_addr, tester))
}
pub fn init_electrum_tester() -> Result<(ElectrumRPC, net::SocketAddr, TestRunner)> {
let tester = TestRunner::new()?;
let electrum_server = ElectrumRPC::start(
Arc::clone(&tester.config),
Arc::clone(&tester.query),
&tester.metrics,
);
log::info!(
"Electrum server running on {}",
tester.config.electrum_rpc_addr
);
Ok((electrum_server, tester.config.electrum_rpc_addr, tester))
}
#[cfg(not(feature = "liquid"))]
fn raw_new_address(
client: &bitcoincore_rpc::Client,
) -> bitcoincore_rpc::Result<Address<bitcoin::address::NetworkChecked>> {
Ok(client.get_new_address(None, None)?.assume_checked())
}
// Returns the confidential address
#[cfg(feature = "liquid")]
fn raw_new_address(client: &bitcoincore_rpc::Client) -> bitcoincore_rpc::Result<Address> {
// Must use raw call() because get_new_address() returns a bitcoin::Address and not an elements::Address
Ok(client.call::<Address>("getnewaddress", &[])?)
}
fn generate(
client: &bitcoincore_rpc::Client,
num_blocks: u32,
) -> bitcoincore_rpc::Result<Vec<BlockHash>> {
let addr = raw_new_address(client)?;
client.call(
"generatetoaddress",
&[num_blocks.into(), addr.to_string().into()],
)
}
fn init_log() -> StdErrLog {
static ONCE: Once = Once::new();
let mut log = stderrlog::new();
match std::env::var("RUST_LOG") {
Ok(e) => log.verbosity(LevelFilter::from_str(&e).unwrap_or(LevelFilter::Off)),
Err(_) => log.verbosity(0),
};
// log.timestamp(stderrlog::Timestamp::Millisecond );
ONCE.call_once(|| log.init().expect("logging initialization failed"));
log
}
fn rand_available_addr() -> net::SocketAddr {
// note this has a potential but unlikely race condition, if the port is grabbed before the caller binds it
let socket = net::UdpSocket::bind("127.0.0.1:0").unwrap();
socket.local_addr().unwrap()
}
error_chain::error_chain! {
types {
Error, ErrorKind, ResultExt, Result;
}
errors {
Electrs(e: electrs::errors::Error) {
description("Electrs error")
display("Electrs error: {:?}", e)
}
BitcoindRpc(e: bitcoind::bitcoincore_rpc::Error) {
description("Bitcoind RPC error")
display("Bitcoind RPC error: {:?}", e)
}
ElectrumD(e: electrumd::Error) {
description("Electrum wallet RPC error")
display("Electrum wallet RPC error: {:?}", e)
}
Io(e: std::io::Error) {
description("IO error")
display("IO error: {:?}", e)
}
Ureq(e: ureq::Error) {
description("ureq error")
display("ureq error: {:?}", e)
}
Json(e: serde_json::Error) {
description("JSON error")
display("JSON error: {:?}", e)
}
}
}
impl From<electrs::errors::Error> for Error {
fn from(e: electrs::errors::Error) -> Self {
Error::from(ErrorKind::Electrs(e))
}
}
impl From<bitcoind::bitcoincore_rpc::Error> for Error {
fn from(e: bitcoind::bitcoincore_rpc::Error) -> Self {
Error::from(ErrorKind::BitcoindRpc(e))
}
}
impl From<electrumd::Error> for Error {
fn from(e: electrumd::Error) -> Self {
Error::from(ErrorKind::ElectrumD(e))
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::from(ErrorKind::Io(e))
}
}
impl From<ureq::Error> for Error {
fn from(e: ureq::Error) -> Self {
Error::from(ErrorKind::Ureq(e))
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::from(ErrorKind::Json(e))
}
}