summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs62
-rw-r--r--src/ui.rs21
2 files changed, 46 insertions, 37 deletions
diff --git a/src/main.rs b/src/main.rs
index 2abe8d7..9526d51 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,34 +11,50 @@ pub mod ui;
async fn main() {
let bc = Arc::new(Mutex::new(BlockChain::default()));
let network = Network::new(bc).await;
- network
- .lock()
- .await
- .start();
- network
- .lock()
- .await
- .broadcast(bitcoin::network::Request::Ibd(None))
- .await;
- let n = network
- .lock()
- .await;
- let mut binding = network
- .lock()
- .await;
- let mut ui = Ui::new(
- binding.get_config(),
- n.get_blockchain()
- .clone(),
- n.get_me(),
- );
+
+ {
+ let net = network
+ .lock()
+ .await;
+ net.start();
+ }
+
+ {
+ let mut net = network
+ .lock()
+ .await;
+ net.broadcast(bitcoin::network::Request::Ibd(None))
+ .await;
+ }
+
+ let (blockchain, me) = {
+ let net = network
+ .lock()
+ .await;
+ (
+ net.get_blockchain()
+ .clone(),
+ *net.get_me(),
+ )
+ };
+
+ let mut ui = Ui::new(blockchain, me);
+
loop {
let mut cmd = String::new();
io::stdin()
.read_line(&mut cmd)
.unwrap();
- ui.cmd(&cmd)
- .await;
+ if let Some(req) = ui
+ .cmd(&cmd)
+ .await
+ {
+ let mut net = network
+ .lock()
+ .await;
+ net.broadcast(req)
+ .await;
+ }
}
}
diff --git a/src/ui.rs b/src/ui.rs
index c820422..b2c57c0 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -1,23 +1,16 @@
-use std::sync::{Arc, Weak};
+use std::sync::Arc;
-use bitcoin::{
- blocks::{Block, BlockChain},
- client::Client,
- network::{Network, NetworkConfig, Request},
- transaction::Transaction,
-};
+use bitcoin::{blocks::BlockChain, client::Client, network::Request, transaction::Transaction};
use tokio::sync::Mutex;
-pub struct Ui<'a> {
- network_config: &'a mut NetworkConfig,
+pub struct Ui {
blockchain: Arc<Mutex<BlockChain>>,
- me: &'a Client,
+ me: Client,
}
-impl<'a> Ui<'a> {
- pub fn new(network_config: &'a mut NetworkConfig, blockchain: Arc<Mutex<BlockChain>>, me: &'a Client) -> Self {
+impl Ui {
+ pub fn new(blockchain: Arc<Mutex<BlockChain>>, me: Client) -> Self {
Self {
- network_config,
blockchain,
me,
}
@@ -30,7 +23,7 @@ impl<'a> Ui<'a> {
.collect();
if cmd[0] == "trans" {
let trans = Transaction::new(
- *self.me,
+ self.me,
Client::new(
cmd[1]
.parse()