Skip to content

Commit 1e65848

Browse files
committed
Move newaddress() and init_tester() into TestRunner
1 parent 8768b1b commit 1e65848

File tree

2 files changed

+172
-171
lines changed

2 files changed

+172
-171
lines changed

tests/common.rs

Lines changed: 169 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -24,153 +24,6 @@ use electrs::{
2424
signal::Waiter,
2525
};
2626

27-
pub fn init_tester() -> Result<TestRunner> {
28-
let log = init_log();
29-
30-
// Setup the bitcoind/elementsd config
31-
let mut node_conf = noded::Conf::default();
32-
{
33-
#[cfg(not(feature = "liquid"))]
34-
let node_conf = &mut node_conf;
35-
#[cfg(feature = "liquid")]
36-
let node_conf = &mut node_conf.0;
37-
38-
#[cfg(feature = "liquid")]
39-
{
40-
node_conf.args.push("-anyonecanspendaremine=1");
41-
}
42-
43-
node_conf.view_stdout = true;
44-
}
45-
46-
// Setup node
47-
let node = NodeD::with_conf(noded::downloaded_exe_path().unwrap(), &node_conf).unwrap();
48-
49-
#[cfg(not(feature = "liquid"))]
50-
let (node_client, params) = (&node.client, &node.params);
51-
#[cfg(feature = "liquid")]
52-
let (node_client, params) = (node.client(), &node.params());
53-
54-
log::info!("node params: {:?}", params);
55-
56-
generate(node_client, 101).chain_err(|| "failed initializing blocks")?;
57-
58-
// Needed to claim the initialfreecoins as our own
59-
// See https://github.com/ElementsProject/elements/issues/956
60-
#[cfg(feature = "liquid")]
61-
node_client.call::<Value>("rescanblockchain", &[])?;
62-
63-
#[cfg(not(feature = "liquid"))]
64-
let network_type = Network::Regtest;
65-
#[cfg(feature = "liquid")]
66-
let network_type = Network::LiquidRegtest;
67-
68-
let daemon_subdir = params
69-
.datadir
70-
.join(config::get_network_subdir(network_type).unwrap());
71-
72-
let electrsdb = tempfile::tempdir().unwrap();
73-
74-
let config = Arc::new(Config {
75-
log,
76-
network_type,
77-
db_path: electrsdb.path().to_path_buf(),
78-
daemon_dir: daemon_subdir.clone(),
79-
blocks_dir: daemon_subdir.join("blocks"),
80-
daemon_rpc_addr: params.rpc_socket.into(),
81-
cookie: None,
82-
electrum_rpc_addr: rand_available_addr(),
83-
http_addr: rand_available_addr(),
84-
http_socket_file: None, // XXX test with socket file or tcp?
85-
monitoring_addr: rand_available_addr(),
86-
jsonrpc_import: false,
87-
light_mode: false,
88-
address_search: true,
89-
index_unspendables: false,
90-
cors: None,
91-
precache_scripts: None,
92-
utxos_limit: 100,
93-
electrum_txs_limit: 100,
94-
electrum_banner: "".into(),
95-
96-
#[cfg(feature = "liquid")]
97-
asset_db_path: None, // XXX
98-
#[cfg(feature = "liquid")]
99-
parent_network: chain::BNetwork::Regtest,
100-
//#[cfg(feature = "electrum-discovery")]
101-
//electrum_public_hosts: Option<crate::electrum::ServerHosts>,
102-
//#[cfg(feature = "electrum-discovery")]
103-
//electrum_announce: bool,
104-
//#[cfg(feature = "electrum-discovery")]
105-
//tor_proxy: Option<std::net::SocketAddr>,
106-
});
107-
108-
let signal = Waiter::start();
109-
let metrics = Metrics::new(rand_available_addr());
110-
metrics.start();
111-
112-
let daemon = Arc::new(Daemon::new(
113-
&config.daemon_dir,
114-
&config.blocks_dir,
115-
config.daemon_rpc_addr,
116-
config.cookie_getter(),
117-
config.network_type,
118-
signal.clone(),
119-
&metrics,
120-
)?);
121-
122-
let store = Arc::new(Store::open(&config.db_path.join("newindex"), &config));
123-
124-
let fetch_from = if !env::var("JSONRPC_IMPORT").is_ok() && !cfg!(feature = "liquid") {
125-
// run the initial indexing from the blk files then switch to using the jsonrpc,
126-
// similarly to how electrs is typically used.
127-
FetchFrom::BlkFiles
128-
} else {
129-
// when JSONRPC_IMPORT is set, use the jsonrpc for the initial indexing too.
130-
// this runs faster on small regtest chains and can be useful for quicker local development iteration.
131-
// this is also used on liquid regtest, which currently fails to parse the BlkFiles due to the magic bytes
132-
FetchFrom::Bitcoind
133-
};
134-
135-
let mut indexer = Indexer::open(Arc::clone(&store), fetch_from, &config, &metrics);
136-
indexer.update(&daemon)?;
137-
indexer.fetch_from(FetchFrom::Bitcoind);
138-
139-
let chain = Arc::new(ChainQuery::new(
140-
Arc::clone(&store),
141-
Arc::clone(&daemon),
142-
&config,
143-
&metrics,
144-
));
145-
146-
let mempool = Arc::new(RwLock::new(Mempool::new(
147-
Arc::clone(&chain),
148-
&metrics,
149-
Arc::clone(&config),
150-
)));
151-
mempool.write().unwrap().update(&daemon)?;
152-
153-
let query = Arc::new(Query::new(
154-
Arc::clone(&chain),
155-
Arc::clone(&mempool),
156-
Arc::clone(&daemon),
157-
Arc::clone(&config),
158-
#[cfg(feature = "liquid")]
159-
None, // TODO
160-
));
161-
162-
Ok(TestRunner {
163-
config,
164-
node,
165-
_electrsdb: electrsdb,
166-
indexer,
167-
query,
168-
daemon,
169-
mempool,
170-
metrics,
171-
})
172-
}
173-
17427
pub struct TestRunner {
17528
config: Arc<Config>,
17629
/// bitcoind::BitcoinD or an elementsd::ElementsD in liquid mode
@@ -184,6 +37,153 @@ pub struct TestRunner {
18437
}
18538

18639
impl TestRunner {
40+
pub fn new() -> Result<TestRunner> {
41+
let log = init_log();
42+
43+
// Setup the bitcoind/elementsd config
44+
let mut node_conf = noded::Conf::default();
45+
{
46+
#[cfg(not(feature = "liquid"))]
47+
let node_conf = &mut node_conf;
48+
#[cfg(feature = "liquid")]
49+
let node_conf = &mut node_conf.0;
50+
51+
#[cfg(feature = "liquid")]
52+
{
53+
node_conf.args.push("-anyonecanspendaremine=1");
54+
}
55+
56+
node_conf.view_stdout = true;
57+
}
58+
59+
// Setup node
60+
let node = NodeD::with_conf(noded::downloaded_exe_path().unwrap(), &node_conf).unwrap();
61+
62+
#[cfg(not(feature = "liquid"))]
63+
let (node_client, params) = (&node.client, &node.params);
64+
#[cfg(feature = "liquid")]
65+
let (node_client, params) = (node.client(), &node.params());
66+
67+
log::info!("node params: {:?}", params);
68+
69+
generate(node_client, 101).chain_err(|| "failed initializing blocks")?;
70+
71+
// Needed to claim the initialfreecoins as our own
72+
// See https://github.com/ElementsProject/elements/issues/956
73+
#[cfg(feature = "liquid")]
74+
node_client.call::<Value>("rescanblockchain", &[])?;
75+
76+
#[cfg(not(feature = "liquid"))]
77+
let network_type = Network::Regtest;
78+
#[cfg(feature = "liquid")]
79+
let network_type = Network::LiquidRegtest;
80+
81+
let daemon_subdir = params
82+
.datadir
83+
.join(config::get_network_subdir(network_type).unwrap());
84+
85+
let electrsdb = tempfile::tempdir().unwrap();
86+
87+
let config = Arc::new(Config {
88+
log,
89+
network_type,
90+
db_path: electrsdb.path().to_path_buf(),
91+
daemon_dir: daemon_subdir.clone(),
92+
blocks_dir: daemon_subdir.join("blocks"),
93+
daemon_rpc_addr: params.rpc_socket.into(),
94+
cookie: None,
95+
electrum_rpc_addr: rand_available_addr(),
96+
http_addr: rand_available_addr(),
97+
http_socket_file: None, // XXX test with socket file or tcp?
98+
monitoring_addr: rand_available_addr(),
99+
jsonrpc_import: false,
100+
light_mode: false,
101+
address_search: true,
102+
index_unspendables: false,
103+
cors: None,
104+
precache_scripts: None,
105+
utxos_limit: 100,
106+
electrum_txs_limit: 100,
107+
electrum_banner: "".into(),
108+
109+
#[cfg(feature = "liquid")]
110+
asset_db_path: None, // XXX
111+
#[cfg(feature = "liquid")]
112+
parent_network: chain::BNetwork::Regtest,
113+
//#[cfg(feature = "electrum-discovery")]
114+
//electrum_public_hosts: Option<crate::electrum::ServerHosts>,
115+
//#[cfg(feature = "electrum-discovery")]
116+
//electrum_announce: bool,
117+
//#[cfg(feature = "electrum-discovery")]
118+
//tor_proxy: Option<std::net::SocketAddr>,
119+
});
120+
121+
let signal = Waiter::start();
122+
let metrics = Metrics::new(rand_available_addr());
123+
metrics.start();
124+
125+
let daemon = Arc::new(Daemon::new(
126+
&config.daemon_dir,
127+
&config.blocks_dir,
128+
config.daemon_rpc_addr,
129+
config.cookie_getter(),
130+
config.network_type,
131+
signal.clone(),
132+
&metrics,
133+
)?);
134+
135+
let store = Arc::new(Store::open(&config.db_path.join("newindex"), &config));
136+
137+
let fetch_from = if !env::var("JSONRPC_IMPORT").is_ok() && !cfg!(feature = "liquid") {
138+
// run the initial indexing from the blk files then switch to using the jsonrpc,
139+
// similarly to how electrs is typically used.
140+
FetchFrom::BlkFiles
141+
} else {
142+
// when JSONRPC_IMPORT is set, use the jsonrpc for the initial indexing too.
143+
// this runs faster on small regtest chains and can be useful for quicker local development iteration.
144+
// this is also used on liquid regtest, which currently fails to parse the BlkFiles due to the magic bytes
145+
FetchFrom::Bitcoind
146+
};
147+
148+
let mut indexer = Indexer::open(Arc::clone(&store), fetch_from, &config, &metrics);
149+
indexer.update(&daemon)?;
150+
indexer.fetch_from(FetchFrom::Bitcoind);
151+
152+
let chain = Arc::new(ChainQuery::new(
153+
Arc::clone(&store),
154+
Arc::clone(&daemon),
155+
&config,
156+
&metrics,
157+
));
158+
159+
let mempool = Arc::new(RwLock::new(Mempool::new(
160+
Arc::clone(&chain),
161+
&metrics,
162+
Arc::clone(&config),
163+
)));
164+
mempool.write().unwrap().update(&daemon)?;
165+
166+
let query = Arc::new(Query::new(
167+
Arc::clone(&chain),
168+
Arc::clone(&mempool),
169+
Arc::clone(&daemon),
170+
Arc::clone(&config),
171+
#[cfg(feature = "liquid")]
172+
None, // TODO
173+
));
174+
175+
Ok(TestRunner {
176+
config,
177+
node,
178+
_electrsdb: electrsdb,
179+
indexer,
180+
query,
181+
daemon,
182+
mempool,
183+
metrics,
184+
})
185+
}
186+
187187
pub fn node_client(&self) -> &bitcoincore_rpc::Client {
188188
#[cfg(not(feature = "liquid"))]
189189
return &self.node.client;
@@ -240,16 +240,36 @@ impl TestRunner {
240240
self.sync()?;
241241
Ok(txid)
242242
}
243+
244+
pub fn newaddress(&self) -> Result<Address> {
245+
#[cfg(not(feature = "liquid"))]
246+
return Ok(self.node_client().get_new_address(None, None)?);
247+
248+
// Return the unconfidential address on Liquid, so that the Bitcoin tests using
249+
// newaddress() can work on Liquid too. The confidential address can be obtained
250+
// by calling ct_newaddress()
251+
#[cfg(feature = "liquid")]
252+
return Ok(self.ct_newaddress()?.1);
253+
}
254+
255+
#[cfg(feature = "liquid")]
256+
pub fn ct_newaddress(&self) -> Result<(Address, Address)> {
257+
let client = self.node_client();
258+
let c_addr = client.call::<Address>("getnewaddress", &[])?;
259+
let mut info = client.call::<Value>("getaddressinfo", &[c_addr.to_string().into()])?;
260+
let uc_addr = serde_json::from_value(info["unconfidential"].take())?;
261+
Ok((c_addr, uc_addr))
262+
}
243263
}
244264

245265
pub fn init_rest_tester() -> Result<(rest::Handle, net::SocketAddr, TestRunner)> {
246-
let tester = init_tester()?;
266+
let tester = TestRunner::new()?;
247267
let rest_server = rest::start(Arc::clone(&tester.config), Arc::clone(&tester.query));
248268
log::info!("REST server running on {}", tester.config.http_addr);
249269
Ok((rest_server, tester.config.http_addr, tester))
250270
}
251271
pub fn init_electrum_tester() -> Result<(ElectrumRPC, net::SocketAddr, TestRunner)> {
252-
let tester = init_tester()?;
272+
let tester = TestRunner::new()?;
253273
let electrum_server = ElectrumRPC::start(
254274
Arc::clone(&tester.config),
255275
Arc::clone(&tester.query),

tests/rest.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn test_rest() -> Result<()> {
2626
};
2727

2828
// Send transaction and confirm it
29-
let addr1 = newaddress(tester.node_client())?;
29+
let addr1 = tester.newaddress()?;
3030
let txid1_confirmed = tester.send(&addr1, "1.19123 BTC".parse().unwrap())?;
3131
tester.mine()?;
3232

@@ -174,7 +174,7 @@ fn test_rest() -> Result<()> {
174174
{
175175
// Test confidential transactions
176176
{
177-
let (c_addr, uc_addr) = elements_newaddress(tester.node_client())?;
177+
let (c_addr, uc_addr) = tester.ct_newaddress()?;
178178
let txid = tester.send(&c_addr, "3.5 BTC".parse().unwrap())?;
179179
tester.mine()?;
180180

@@ -259,7 +259,7 @@ fn test_rest() -> Result<()> {
259259
let assetid = issuance["asset"].as_str().expect("asset id");
260260
tester.mine()?;
261261

262-
let (c_addr, uc_addr) = elements_newaddress(tester.node_client())?;
262+
let (c_addr, uc_addr) = tester.ct_newaddress()?;
263263

264264
// With blinding off
265265
let txid = tester.send_asset(
@@ -318,22 +318,3 @@ fn test_rest() -> Result<()> {
318318
rest_handle.stop();
319319
Ok(())
320320
}
321-
322-
fn newaddress(client: &bitcoincore_rpc::Client) -> Result<Address> {
323-
#[cfg(not(feature = "liquid"))]
324-
return Ok(client.get_new_address(None, None)?);
325-
326-
// Return the unconfidential address on Liquid, so that the same tests work
327-
// on both Bitcoin and Liquid mode. The Liquid-specific functionality, including
328-
// confidentially, is tested separately.
329-
#[cfg(feature = "liquid")]
330-
return Ok(elements_newaddress(client)?.1);
331-
}
332-
333-
#[cfg(feature = "liquid")]
334-
fn elements_newaddress(client: &bitcoincore_rpc::Client) -> Result<(Address, Address)> {
335-
let c_addr = client.call::<Address>("getnewaddress", &[])?;
336-
let mut info = client.call::<Value>("getaddressinfo", &[c_addr.to_string().into()])?;
337-
let uc_addr = serde_json::from_value(info["unconfidential"].take())?;
338-
Ok((c_addr, uc_addr))
339-
}

0 commit comments

Comments
 (0)