@@ -166,83 +166,24 @@ pub struct GlobalStateLock {
166
166
}
167
167
168
168
impl GlobalStateLock {
169
- pub ( crate ) fn new_internal (
170
- wallet_state : WalletState ,
171
- chain : BlockchainState ,
172
- net : NetworkingState ,
173
- cli : cli_args:: Args ,
174
- mempool : Mempool ,
169
+ pub ( crate ) fn from_global_state (
170
+ global_state : GlobalState ,
175
171
rpc_server_to_main_tx : tokio:: sync:: mpsc:: Sender < RPCServerToMain > ,
176
172
) -> Self {
177
- let global_state = GlobalState :: new ( wallet_state , chain , net , cli. clone ( ) , mempool ) ;
173
+ let cli = global_state . cli . clone ( ) ;
178
174
let global_state_lock = sync_tokio:: AtomicRw :: from ( (
179
175
global_state,
180
176
Some ( "GlobalState" ) ,
181
177
Some ( crate :: LOG_TOKIO_LOCK_EVENT_CB ) ,
182
178
) ) ;
179
+
183
180
Self {
184
181
global_state_lock,
185
182
cli,
186
183
rpc_server_to_main_tx,
187
184
}
188
185
}
189
186
190
- /// the key to the watery kingdom.
191
- pub async fn new (
192
- data_directory : DataDirectory ,
193
- genesis : Block ,
194
- cli_args : cli_args:: Args ,
195
- rpc_server_to_main_tx : tokio:: sync:: mpsc:: Sender < RPCServerToMain > ,
196
- ) -> Result < Self > {
197
- // Get wallet object, create various wallet secret files
198
- let wallet_dir = data_directory. wallet_directory_path ( ) ;
199
- DataDirectory :: create_dir_if_not_exists ( & wallet_dir) . await ?;
200
- let wallet_file_context =
201
- WalletFileContext :: read_from_file_or_create ( & data_directory. wallet_directory_path ( ) ) ?;
202
- debug ! ( "Now getting wallet state. This may take a while if the database needs pruning." ) ;
203
- let wallet_state = WalletState :: try_new_from_context (
204
- & data_directory,
205
- wallet_file_context,
206
- & cli_args,
207
- & genesis,
208
- )
209
- . await ?;
210
- debug ! ( "Got wallet state." ) ;
211
-
212
- let archival_state = ArchivalState :: new ( data_directory. clone ( ) , genesis) . await ;
213
- debug ! ( "Got archival state" ) ;
214
-
215
- // Get latest block. Use hardcoded genesis block if nothing is in database.
216
- let latest_block: Block = archival_state. get_tip ( ) . await ;
217
-
218
- let peer_map: HashMap < SocketAddr , PeerInfo > = HashMap :: new ( ) ;
219
- let peer_databases = NetworkingState :: initialize_peer_databases ( & data_directory) . await ?;
220
- debug ! ( "Got peer databases" ) ;
221
-
222
- let networking_state = NetworkingState :: new ( peer_map, peer_databases) ;
223
-
224
- let light_state: LightState = LightState :: from ( latest_block) ;
225
- let blockchain_archival_state = BlockchainArchivalState {
226
- light_state,
227
- archival_state,
228
- } ;
229
- let blockchain_state = BlockchainState :: Archival ( Box :: new ( blockchain_archival_state) ) ;
230
- let mempool = Mempool :: new (
231
- cli_args. max_mempool_size ,
232
- cli_args. max_mempool_num_tx ,
233
- blockchain_state. light_state ( ) . hash ( ) ,
234
- ) ;
235
-
236
- Ok ( GlobalStateLock :: new_internal (
237
- wallet_state,
238
- blockchain_state,
239
- networking_state,
240
- cli_args,
241
- mempool,
242
- rpc_server_to_main_tx. clone ( ) ,
243
- ) )
244
- }
245
-
246
187
// check if mining
247
188
pub async fn mining ( & self ) -> bool {
248
189
self . lock ( |s| match s. mining_state . mining_status {
@@ -676,6 +617,49 @@ impl Drop for GlobalState {
676
617
}
677
618
678
619
impl GlobalState {
620
+ pub ( crate ) async fn try_new (
621
+ data_directory : DataDirectory ,
622
+ genesis : Block ,
623
+ cli : cli_args:: Args ,
624
+ ) -> Result < Self > {
625
+ // Get wallet object, create various wallet secret files
626
+ let wallet_dir = data_directory. wallet_directory_path ( ) ;
627
+ DataDirectory :: create_dir_if_not_exists ( & wallet_dir) . await ?;
628
+ let wallet_file_context =
629
+ WalletFileContext :: read_from_file_or_create ( & data_directory. wallet_directory_path ( ) ) ?;
630
+ debug ! ( "Now getting wallet state. This may take a while if the database needs pruning." ) ;
631
+ let wallet_state =
632
+ WalletState :: try_new_from_context ( & data_directory, wallet_file_context, & cli, & genesis)
633
+ . await ?;
634
+ debug ! ( "Got wallet state." ) ;
635
+
636
+ let archival_state = ArchivalState :: new ( data_directory. clone ( ) , genesis) . await ;
637
+ debug ! ( "Got archival state" ) ;
638
+
639
+ // Get latest block. Use hardcoded genesis block if nothing is in database.
640
+ let latest_block: Block = archival_state. get_tip ( ) . await ;
641
+
642
+ let peer_map: HashMap < SocketAddr , PeerInfo > = HashMap :: new ( ) ;
643
+ let peer_databases = NetworkingState :: initialize_peer_databases ( & data_directory) . await ?;
644
+ debug ! ( "Got peer databases" ) ;
645
+
646
+ let net = NetworkingState :: new ( peer_map, peer_databases) ;
647
+
648
+ let light_state: LightState = LightState :: from ( latest_block) ;
649
+ let chain = BlockchainArchivalState {
650
+ light_state,
651
+ archival_state,
652
+ } ;
653
+ let chain = BlockchainState :: Archival ( Box :: new ( chain) ) ;
654
+ let mempool = Mempool :: new (
655
+ cli. max_mempool_size ,
656
+ cli. max_mempool_num_tx ,
657
+ chain. light_state ( ) . hash ( ) ,
658
+ ) ;
659
+
660
+ Ok ( Self :: new ( wallet_state, chain, net, cli, mempool) )
661
+ }
662
+
679
663
pub fn new (
680
664
wallet_state : WalletState ,
681
665
chain : BlockchainState ,
0 commit comments