Skip to content

Commit c576b96

Browse files
committed
refactor(GlobalState): Change constructors
1 parent 7535b1b commit c576b96

File tree

3 files changed

+58
-80
lines changed

3 files changed

+58
-80
lines changed

src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use models::blockchain::block::Block;
6161
use models::blockchain::shared::Hash;
6262
use models::peer::handshake_data::HandshakeData;
6363
use models::state::wallet::wallet_file::WalletFileContext;
64+
use models::state::GlobalState;
6465
use prelude::tasm_lib;
6566
use prelude::triton_vm;
6667
use prelude::twenty_first;
@@ -129,13 +130,10 @@ pub async fn initialize(cli_args: cli_args::Args) -> Result<MainLoopHandler> {
129130
let (rpc_server_to_main_tx, rpc_server_to_main_rx) =
130131
mpsc::channel::<RPCServerToMain>(RPC_CHANNEL_CAPACITY);
131132
let genesis = Block::genesis(cli_args.network);
132-
let mut global_state_lock = GlobalStateLock::new(
133-
data_directory.clone(),
134-
genesis,
135-
cli_args.clone(),
136-
rpc_server_to_main_tx.clone(),
137-
)
138-
.await?;
133+
let global_state =
134+
GlobalState::try_new(data_directory.clone(), genesis, cli_args.clone()).await?;
135+
let mut global_state_lock =
136+
GlobalStateLock::from_global_state(global_state, rpc_server_to_main_tx.clone());
139137

140138
// Construct the broadcast channel to communicate from the main task to peer tasks
141139
let (main_to_peer_broadcast_tx, _main_to_peer_broadcast_rx) =

src/models/state/mod.rs

Lines changed: 47 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -166,83 +166,24 @@ pub struct GlobalStateLock {
166166
}
167167

168168
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,
175171
rpc_server_to_main_tx: tokio::sync::mpsc::Sender<RPCServerToMain>,
176172
) -> Self {
177-
let global_state = GlobalState::new(wallet_state, chain, net, cli.clone(), mempool);
173+
let cli = global_state.cli.clone();
178174
let global_state_lock = sync_tokio::AtomicRw::from((
179175
global_state,
180176
Some("GlobalState"),
181177
Some(crate::LOG_TOKIO_LOCK_EVENT_CB),
182178
));
179+
183180
Self {
184181
global_state_lock,
185182
cli,
186183
rpc_server_to_main_tx,
187184
}
188185
}
189186

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-
246187
// check if mining
247188
pub async fn mining(&self) -> bool {
248189
self.lock(|s| match s.mining_state.mining_status {
@@ -676,6 +617,49 @@ impl Drop for GlobalState {
676617
}
677618

678619
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+
679663
pub fn new(
680664
wallet_state: WalletState,
681665
chain: BlockchainState,

src/tests/shared.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ use crate::models::state::wallet::expected_utxo::UtxoNotifier;
103103
use crate::models::state::wallet::transaction_output::TxOutputList;
104104
use crate::models::state::wallet::wallet_entropy::WalletEntropy;
105105
use crate::models::state::wallet::wallet_state::WalletState;
106+
use crate::models::state::GlobalState;
106107
use crate::models::state::GlobalStateLock;
107108
use crate::prelude::twenty_first;
108109
use crate::triton_vm_job_queue::TritonVmJobQueue;
@@ -203,14 +204,14 @@ pub(crate) async fn mock_genesis_global_state_with_block(
203204
std::net::SocketAddr::from_str(&format!("123.123.123.{}:8080", i)).unwrap();
204205
peer_map.insert(peer_address, get_dummy_peer_outgoing(peer_address));
205206
}
206-
let networking_state = NetworkingState::new(peer_map, peer_db);
207+
let net = NetworkingState::new(peer_map, peer_db);
207208

208209
// Sanity check
209210
assert_eq!(archival_state.genesis_block().hash(), genesis_block.hash());
210211
assert_eq!(archival_state.get_tip().await.hash(), genesis_block.hash());
211212

212213
let light_state: LightState = LightState::from(genesis_block.to_owned());
213-
let blockchain_state = BlockchainState::Archival(Box::new(BlockchainArchivalState {
214+
let chain = BlockchainState::Archival(Box::new(BlockchainArchivalState {
214215
light_state,
215216
archival_state,
216217
}));
@@ -230,14 +231,9 @@ pub(crate) async fn mock_genesis_global_state_with_block(
230231
}
231232
});
232233

233-
GlobalStateLock::new_internal(
234-
wallet_state,
235-
blockchain_state,
236-
networking_state,
237-
cli.clone(),
238-
mempool,
239-
rpc_to_main_tx,
240-
)
234+
let global_state = GlobalState::new(wallet_state, chain, net, cli, mempool);
235+
236+
GlobalStateLock::from_global_state(global_state, rpc_to_main_tx)
241237
}
242238

243239
/// Get a global state object for unit test purposes. This global state is

0 commit comments

Comments
 (0)