@@ -25,8 +25,6 @@ use std::path::PathBuf;
25
25
use std:: time;
26
26
use std:: time:: Duration ;
27
27
28
- use tini:: Ini ;
29
-
30
28
use burnchains:: bitcoin:: blocks:: BitcoinHeaderIPC ;
31
29
use burnchains:: bitcoin:: messages:: BitcoinMessageHandler ;
32
30
use burnchains:: bitcoin:: spv:: * ;
@@ -137,165 +135,6 @@ impl BitcoinIndexerConfig {
137
135
magic_bytes : BLOCKSTACK_MAGIC_MAINNET . clone ( ) ,
138
136
}
139
137
}
140
-
141
- pub fn to_file ( & self , path : & String ) -> Result < ( ) , btc_error > {
142
- let username = self . username . clone ( ) . unwrap_or ( "" . to_string ( ) ) ;
143
- let password = self . password . clone ( ) . unwrap_or ( "" . to_string ( ) ) ;
144
-
145
- let conf = Ini :: new ( )
146
- . section ( "bitcoin" )
147
- . item ( "server" , self . peer_host . as_str ( ) )
148
- . item ( "p2p_port" , format ! ( "{}" , self . peer_port) . as_str ( ) )
149
- . item ( "rpc_port" , format ! ( "{}" , self . rpc_port) . as_str ( ) )
150
- . item ( "rpc_ssl" , format ! ( "{}" , self . rpc_ssl) . as_str ( ) )
151
- . item ( "username" , username. as_str ( ) )
152
- . item ( "password" , password. as_str ( ) )
153
- . item ( "timeout" , format ! ( "{}" , self . timeout) . as_str ( ) )
154
- . item ( "spv_path" , self . spv_headers_path . as_str ( ) )
155
- . item ( "first_block" , format ! ( "{}" , self . first_block) . as_str ( ) )
156
- . section ( "blockstack" )
157
- . item (
158
- "network_id" ,
159
- format ! (
160
- "{}{}" ,
161
- self . magic_bytes. as_bytes( ) [ 0 ] as char ,
162
- self . magic_bytes. as_bytes( ) [ 1 ] as char
163
- )
164
- . as_str ( ) ,
165
- ) ;
166
-
167
- conf. to_file ( & path) . map_err ( |e| btc_error:: Io ( e) )
168
- }
169
-
170
- pub fn from_file ( path : & String ) -> Result < BitcoinIndexerConfig , btc_error > {
171
- let conf_path = PathBuf :: from ( path) ;
172
- if !conf_path. is_file ( ) {
173
- return Err ( btc_error:: ConfigError (
174
- "Failed to load BitcoinIndexerConfig file: No such file or directory" . to_string ( ) ,
175
- ) ) ;
176
- }
177
-
178
- let default_config = BitcoinIndexerConfig :: default ( 0 ) ;
179
-
180
- match Ini :: from_file ( path) {
181
- Ok ( ini_file) => {
182
- // [bitcoin]
183
- let peer_host = ini_file
184
- . get ( "bitcoin" , "server" )
185
- . unwrap_or ( default_config. peer_host ) ;
186
-
187
- let peer_port = ini_file
188
- . get ( "bitcoin" , "p2p_port" )
189
- . unwrap_or ( format ! ( "{}" , default_config. peer_port) )
190
- . trim ( )
191
- . parse ( )
192
- . map_err ( |_e| {
193
- btc_error:: ConfigError ( "Invalid bitcoin:p2p_port value" . to_string ( ) )
194
- } ) ?;
195
-
196
- if peer_port <= 1024 || peer_port == 65535 {
197
- return Err ( btc_error:: ConfigError ( "Invalid p2p_port" . to_string ( ) ) ) ;
198
- }
199
-
200
- let rpc_port = ini_file
201
- . get ( "bitcoin" , "rpc_port" )
202
- . unwrap_or ( format ! ( "{}" , default_config. rpc_port) )
203
- . trim ( )
204
- . parse ( )
205
- . map_err ( |_e| {
206
- btc_error:: ConfigError ( "Invalid bitcoin:port value" . to_string ( ) )
207
- } ) ?;
208
-
209
- if rpc_port <= 1024 || rpc_port == 65535 {
210
- return Err ( btc_error:: ConfigError ( "Invalid rpc_port" . to_string ( ) ) ) ;
211
- }
212
-
213
- let username: Option < String > =
214
- ini_file. get ( "bitcoin" , "username" ) . and_then ( |s| Some ( s) ) ;
215
- let password: Option < String > =
216
- ini_file. get ( "bitcoin" , "password" ) . and_then ( |s| Some ( s) ) ;
217
-
218
- let timeout = ini_file
219
- . get ( "bitcoin" , "timeout" )
220
- . unwrap_or ( format ! ( "{}" , default_config. timeout) )
221
- . trim ( )
222
- . parse ( )
223
- . map_err ( |_e| {
224
- btc_error:: ConfigError ( "Invalid bitcoin:timeout value" . to_string ( ) )
225
- } ) ?;
226
-
227
- let spv_headers_path_cfg = ini_file
228
- . get ( "bitcoin" , "spv_path" )
229
- . unwrap_or ( default_config. spv_headers_path ) ;
230
-
231
- let spv_headers_path =
232
- if path:: is_separator ( spv_headers_path_cfg. chars ( ) . next ( ) . ok_or (
233
- btc_error:: ConfigError ( "Invalid bitcoin:spv_path value" . to_string ( ) ) ,
234
- ) ?) {
235
- // absolute
236
- spv_headers_path_cfg
237
- } else {
238
- // relative to config file
239
- let mut p = PathBuf :: from ( path) ;
240
- p. pop ( ) ;
241
- let s = p. join ( & spv_headers_path_cfg) ;
242
- s. to_str ( ) . unwrap ( ) . to_string ( )
243
- } ;
244
-
245
- let first_block = ini_file
246
- . get ( "bitcoin" , "first_block" )
247
- . unwrap_or ( format ! ( "{}" , 0 ) )
248
- . trim ( )
249
- . parse ( )
250
- . map_err ( |_e| {
251
- btc_error:: ConfigError ( "Invalid bitcoin:first_block value" . to_string ( ) )
252
- } ) ?;
253
-
254
- let rpc_ssl_str = ini_file
255
- . get ( "bitcoin" , "ssl" )
256
- . unwrap_or ( format ! ( "{}" , default_config. rpc_ssl) ) ;
257
-
258
- let rpc_ssl = rpc_ssl_str == "1" || rpc_ssl_str == "true" ;
259
-
260
- // [blockstack]
261
- let blockstack_magic_str =
262
- ini_file. get ( "blockstack" , "network_id" ) . unwrap_or ( format ! (
263
- "{}{}" ,
264
- BLOCKSTACK_MAGIC_MAINNET . as_bytes( ) [ 0 ] as char ,
265
- BLOCKSTACK_MAGIC_MAINNET . as_bytes( ) [ 1 ] as char
266
- ) ) ;
267
-
268
- if blockstack_magic_str. len ( ) != 2 {
269
- return Err ( btc_error:: ConfigError (
270
- "Invalid blockstack:network_id value: must be two bytes" . to_string ( ) ,
271
- ) ) ;
272
- }
273
-
274
- let blockstack_magic = MagicBytes ( [
275
- blockstack_magic_str. as_bytes ( ) [ 0 ] as u8 ,
276
- blockstack_magic_str. as_bytes ( ) [ 1 ] as u8 ,
277
- ] ) ;
278
-
279
- let cfg = BitcoinIndexerConfig {
280
- peer_host : peer_host,
281
- peer_port : peer_port,
282
- rpc_port : rpc_port,
283
- rpc_ssl : rpc_ssl,
284
- username : username,
285
- password : password,
286
- timeout : timeout,
287
- spv_headers_path : spv_headers_path,
288
- first_block : first_block,
289
- magic_bytes : blockstack_magic,
290
- } ;
291
-
292
- Ok ( cfg)
293
- }
294
- Err ( _) => Err ( btc_error:: ConfigError (
295
- "Failed to parse BitcoinConfigIndexer config file" . to_string ( ) ,
296
- ) ) ,
297
- }
298
- }
299
138
}
300
139
301
140
impl BitcoinIndexerRuntime {
@@ -323,18 +162,6 @@ impl BitcoinIndexer {
323
162
}
324
163
}
325
164
326
- pub fn from_file (
327
- network_id : BitcoinNetworkType ,
328
- config_file : & String ,
329
- ) -> Result < BitcoinIndexer , btc_error > {
330
- let config = BitcoinIndexerConfig :: from_file ( config_file) ?;
331
- let runtime = BitcoinIndexerRuntime :: new ( network_id) ;
332
- Ok ( BitcoinIndexer {
333
- config : config,
334
- runtime : runtime,
335
- } )
336
- }
337
-
338
165
pub fn dup ( & self ) -> BitcoinIndexer {
339
166
BitcoinIndexer {
340
167
config : self . config . clone ( ) ,
@@ -775,57 +602,6 @@ impl Drop for BitcoinIndexer {
775
602
impl BurnchainIndexer for BitcoinIndexer {
776
603
type P = BitcoinBlockParser ;
777
604
778
- /// Instantiate the Bitcoin indexer, and connect to the peer network.
779
- /// Instead, load our configuration state and sanity-check it.
780
- ///
781
- /// Pass a directory (working_dir) that contains a "bitcoin.ini" file.
782
- fn init (
783
- working_dir : & String ,
784
- network_name : & String ,
785
- first_block_height : u64 ,
786
- ) -> Result < BitcoinIndexer , burnchain_error > {
787
- let conf_path_str =
788
- Burnchain :: get_chainstate_config_path ( working_dir, & "bitcoin" . to_string ( ) ) ;
789
-
790
- let network_id_opt = match network_name. as_ref ( ) {
791
- BITCOIN_MAINNET_NAME => Some ( BitcoinNetworkType :: Mainnet ) ,
792
- BITCOIN_TESTNET_NAME => Some ( BitcoinNetworkType :: Testnet ) ,
793
- BITCOIN_REGTEST_NAME => Some ( BitcoinNetworkType :: Regtest ) ,
794
- _ => None ,
795
- } ;
796
-
797
- if network_id_opt. is_none ( ) {
798
- return Err ( burnchain_error:: Bitcoin ( btc_error:: ConfigError ( format ! (
799
- "Unrecognized network name '{}'" ,
800
- network_name
801
- ) ) ) ) ;
802
- }
803
- let bitcoin_network_id = network_id_opt. unwrap ( ) ;
804
-
805
- if !PathBuf :: from ( & conf_path_str) . exists ( ) {
806
- let default_config = BitcoinIndexerConfig :: default ( first_block_height) ;
807
- default_config
808
- . to_file ( & conf_path_str)
809
- . map_err ( burnchain_error:: Bitcoin ) ?;
810
- }
811
-
812
- let mut indexer = BitcoinIndexer :: from_file ( bitcoin_network_id, & conf_path_str)
813
- . map_err ( burnchain_error:: Bitcoin ) ?;
814
-
815
- SpvClient :: new (
816
- & indexer. config . spv_headers_path ,
817
- 0 ,
818
- None ,
819
- indexer. runtime . network_id ,
820
- true ,
821
- false ,
822
- )
823
- . map_err ( burnchain_error:: Bitcoin ) ?;
824
-
825
- indexer. connect ( ) ?;
826
- Ok ( indexer)
827
- }
828
-
829
605
/// Connect to the Bitcoin peer network.
830
606
/// Use the peer host and peer port given in the config file,
831
607
/// and loaded in on setup. Don't call this before init().
0 commit comments