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