@@ -2,14 +2,19 @@ use std::sync::{Arc, Once, RwLock};
22use std:: { env, net} ;
33
44use stderrlog:: StdErrLog ;
5+ use tempfile:: TempDir ;
56
6- use bitcoind:: {
7- bitcoincore_rpc:: { self , RpcApi } ,
8- BitcoinD ,
9- } ;
7+ use serde_json:: { json, Value } ;
8+
9+ #[ cfg( not( feature = "liquid" ) ) ]
10+ use bitcoind:: { self as noded, BitcoinD as NodeD } ;
11+ #[ cfg( feature = "liquid" ) ]
12+ use elementsd:: { self as noded, ElementsD as NodeD } ;
13+
14+ use noded:: bitcoincore_rpc:: { self , RpcApi } ;
1015
1116use electrs:: {
12- chain:: { self , Network } ,
17+ chain:: { self , Address , BlockHash , Network , Txid } ,
1318 config:: { self , Config } ,
1419 daemon:: Daemon ,
1520 electrum:: RPC as ElectrumRPC ,
@@ -18,28 +23,49 @@ use electrs::{
1823 rest,
1924 signal:: Waiter ,
2025} ;
21- use tempfile:: TempDir ;
2226
2327pub fn init_tester ( ) -> Result < TestRunner > {
2428 let log = init_log ( ) ;
2529
26- let mut bitcoind_conf = bitcoind:: Conf :: default ( ) ;
27- bitcoind_conf. view_stdout = true ;
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 ( ) ) ;
2853
29- let bitcoind =
30- BitcoinD :: with_conf ( bitcoind:: downloaded_exe_path ( ) . unwrap ( ) , & bitcoind_conf) . unwrap ( ) ;
54+ log:: info!( "node params: {:?}" , params) ;
3155
32- init_node ( & bitcoind . client ) . chain_err ( || "failed initializing node " ) ?;
56+ generate ( node_client , 101 ) . chain_err ( || "failed initializing blocks " ) ?;
3357
34- log:: info!( "bitcoind: {:?}" , bitcoind. params) ;
58+ // Needed to claim the initialfreecoins are our own
59+ // See https://github.com/ElementsProject/elements/issues/956
60+ #[ cfg( feature = "liquid" ) ]
61+ node_client. call :: < Value > ( "rescanblockchain" , & [ ] ) ?;
3562
3663 #[ cfg( not( feature = "liquid" ) ) ]
3764 let network_type = Network :: Regtest ;
3865 #[ cfg( feature = "liquid" ) ]
3966 let network_type = Network :: LiquidRegtest ;
4067
41- let daemon_subdir = bitcoind
42- . params
68+ let daemon_subdir = params
4369 . datadir
4470 . join ( config:: get_network_subdir ( network_type) . unwrap ( ) ) ;
4571
@@ -51,7 +77,7 @@ pub fn init_tester() -> Result<TestRunner> {
5177 db_path : electrsdb. path ( ) . to_path_buf ( ) ,
5278 daemon_dir : daemon_subdir. clone ( ) ,
5379 blocks_dir : daemon_subdir. join ( "blocks" ) ,
54- daemon_rpc_addr : bitcoind . params . rpc_socket . into ( ) ,
80+ daemon_rpc_addr : params. rpc_socket . into ( ) ,
5581 cookie : None ,
5682 electrum_rpc_addr : rand_available_addr ( ) ,
5783 http_addr : rand_available_addr ( ) ,
@@ -134,7 +160,7 @@ pub fn init_tester() -> Result<TestRunner> {
134160
135161 Ok ( TestRunner {
136162 config,
137- bitcoind ,
163+ node ,
138164 _electrsdb : electrsdb,
139165 indexer,
140166 query,
@@ -146,7 +172,8 @@ pub fn init_tester() -> Result<TestRunner> {
146172
147173pub struct TestRunner {
148174 config : Arc < Config > ,
149- bitcoind : BitcoinD ,
175+ /// bitcoind::BitcoinD or an elementsd::ElementsD in liquid mode
176+ node : NodeD ,
150177 _electrsdb : TempDir , // rm'd when dropped
151178 indexer : Indexer ,
152179 query : Arc < Query > ,
@@ -156,8 +183,11 @@ pub struct TestRunner {
156183}
157184
158185impl TestRunner {
159- pub fn bitcoind ( & self ) -> & bitcoincore_rpc:: Client {
160- & self . bitcoind . client
186+ pub fn node_client ( & self ) -> & bitcoincore_rpc:: Client {
187+ #[ cfg( not( feature = "liquid" ) ) ]
188+ return & self . node . client ;
189+ #[ cfg( feature = "liquid" ) ]
190+ return & self . node . client ( ) ;
161191 }
162192
163193 pub fn sync ( & mut self ) -> Result < ( ) > {
@@ -169,18 +199,17 @@ impl TestRunner {
169199 Ok ( ( ) )
170200 }
171201
172- pub fn mine ( & mut self ) -> Result < chain:: BlockHash > {
173- let addr = self . bitcoind . client . get_new_address ( None , None ) ?;
174- let mut generated = self . bitcoind . client . generate_to_address ( 1 , & addr) ?;
202+ pub fn mine ( & mut self ) -> Result < BlockHash > {
203+ let mut generated = generate ( self . node_client ( ) , 1 ) ?;
175204 self . sync ( ) ?;
176205 Ok ( generated. remove ( 0 ) )
177206 }
178207
179- pub fn send ( & mut self , addr : & chain :: Address , amount : bitcoin:: Amount ) -> Result < chain :: Txid > {
180- let txid = self
181- . bitcoind
182- . client
183- . send_to_address ( addr , amount , None , None , None , None , None , None ) ?;
208+ pub fn send ( & mut self , addr : & Address , amount : bitcoin:: Amount ) -> Result < Txid > {
209+ let txid = self . node_client ( ) . call (
210+ "sendtoaddress" ,
211+ & [ addr . to_string ( ) . into ( ) , json ! ( amount . as_btc ( ) ) ] ,
212+ ) ?;
184213 self . sync ( ) ?;
185214 Ok ( txid)
186215 }
@@ -206,10 +235,15 @@ pub fn init_electrum_tester() -> Result<(ElectrumRPC, net::SocketAddr, TestRunne
206235 Ok ( ( electrum_server, tester. config . electrum_rpc_addr , tester) )
207236}
208237
209- fn init_node ( client : & bitcoincore_rpc:: Client ) -> bitcoincore_rpc:: Result < ( ) > {
210- let addr = client. get_new_address ( None , None ) ?;
211- client. generate_to_address ( 101 , & addr) ?;
212- Ok ( ( ) )
238+ fn generate (
239+ client : & bitcoincore_rpc:: Client ,
240+ num_blocks : u32 ,
241+ ) -> bitcoincore_rpc:: Result < Vec < BlockHash > > {
242+ let addr = client. call :: < Address > ( "getnewaddress" , & [ ] ) ?;
243+ client. call (
244+ "generatetoaddress" ,
245+ & [ num_blocks. into ( ) , addr. to_string ( ) . into ( ) ] ,
246+ )
213247}
214248
215249fn init_log ( ) -> StdErrLog {
@@ -256,6 +290,10 @@ error_chain::error_chain! {
256290 description( "ureq error" )
257291 display( "ureq error: {:?}" , e)
258292 }
293+ Json ( e: serde_json:: Error ) {
294+ description( "JSON error" )
295+ display( "JSON error: {:?}" , e)
296+ }
259297 }
260298}
261299
@@ -284,3 +322,8 @@ impl From<ureq::Error> for Error {
284322 Error :: from ( ErrorKind :: Ureq ( e) )
285323 }
286324}
325+ impl From < serde_json:: Error > for Error {
326+ fn from ( e : serde_json:: Error ) -> Self {
327+ Error :: from ( ErrorKind :: Json ( e) )
328+ }
329+ }
0 commit comments