diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-10-21 09:56:21 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-10-21 09:56:28 +0800 |
| commit | f754675aac96ac04599ec79d609e2c5b08be4561 (patch) | |
| tree | a92a2e4b910593d3593744dd99bae0ba28a44c37 /src | |
| parent | b71fae628d97ada84c533ce746d4d7980a9f6d73 (diff) | |
feat: ui
Diffstat (limited to 'src')
| -rw-r--r-- | src/blocks.rs | 13 | ||||
| -rw-r--r-- | src/client.rs | 8 | ||||
| -rw-r--r-- | src/main.rs | 28 | ||||
| -rw-r--r-- | src/network.rs | 21 | ||||
| -rw-r--r-- | src/transaction.rs | 10 | ||||
| -rw-r--r-- | src/ui.rs | 52 |
6 files changed, 130 insertions, 2 deletions
diff --git a/src/blocks.rs b/src/blocks.rs index 288e754..84f6c92 100644 --- a/src/blocks.rs +++ b/src/blocks.rs @@ -84,6 +84,19 @@ impl BlockChain { } } + pub fn new_block(&self, trans: Transaction) -> Block { + let mut hasher = DefaultHasher::new(); + Block::new( + self.blocks + .last() + .map(|b| { + b.hash(&mut hasher); + hasher.finish() + }), + trans, + ) + } + pub fn store(&mut self, block: Block) { // TODO: verify blocks self.blocks diff --git a/src/client.rs b/src/client.rs index 5edba62..457a70d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -6,6 +6,14 @@ pub struct Client { id: u32, } +impl Client { + pub fn new(id: u32) -> Self { + Self { + id, + } + } +} + impl Default for Client { fn default() -> Self { Self { diff --git a/src/main.rs b/src/main.rs index 0c25725..2abe8d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,12 @@ -use std::{sync::Arc, time::Duration}; +use std::{io, sync::Arc}; use bitcoin::{blocks::BlockChain, network::Network}; use tokio::sync::Mutex; +use crate::ui::Ui; + +pub mod ui; + #[tokio::main] async fn main() { let bc = Arc::new(Mutex::new(BlockChain::default())); @@ -16,5 +20,25 @@ async fn main() { .await .broadcast(bitcoin::network::Request::Ibd(None)) .await; - tokio::time::sleep(Duration::from_secs(100)).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(), + ); + loop { + let mut cmd = String::new(); + io::stdin() + .read_line(&mut cmd) + .unwrap(); + + ui.cmd(&cmd) + .await; + } } diff --git a/src/network.rs b/src/network.rs index fd0362c..08d59ef 100644 --- a/src/network.rs +++ b/src/network.rs @@ -23,15 +23,35 @@ pub enum Request { Ibd(Option<BlockChain>), } +#[derive(Default)] +pub struct NetworkConfig { + auto_mine: bool, +} + pub struct Network { this: Weak<Mutex<Self>>, me: Client, + listener: Arc<TcpListener>, peers: Vec<Arc<Mutex<Peer>>>, blockchain: Arc<Mutex<BlockChain>>, + + config: NetworkConfig, } impl Network { + pub fn get_config(&mut self) -> &mut NetworkConfig { + &mut self.config + } + + pub fn get_me(&self) -> &Client { + &self.me + } + + pub fn get_blockchain(&self) -> &Arc<Mutex<BlockChain>> { + &self.blockchain + } + pub async fn new(blockchain: Arc<Mutex<BlockChain>>) -> Arc<Mutex<Self>> { let listener = TcpListener::bind("0.0.0.0:6767") .await @@ -42,6 +62,7 @@ impl Network { listener: Arc::new(listener), peers: Vec::default(), blockchain, + config: NetworkConfig::default(), })); this.lock() .await diff --git a/src/transaction.rs b/src/transaction.rs index 0b77a10..dcaff49 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -8,3 +8,13 @@ pub struct Transaction { to: Client, amount: u32, } + +impl Transaction { + pub fn new(from: Client, to: Client, amount: u32) -> Self { + Self { + from, + to, + amount, + } + } +} diff --git a/src/ui.rs b/src/ui.rs new file mode 100644 index 0000000..c820422 --- /dev/null +++ b/src/ui.rs @@ -0,0 +1,52 @@ +use std::sync::{Arc, Weak}; + +use bitcoin::{ + blocks::{Block, BlockChain}, + client::Client, + network::{Network, NetworkConfig, Request}, + transaction::Transaction, +}; +use tokio::sync::Mutex; + +pub struct Ui<'a> { + network_config: &'a mut NetworkConfig, + blockchain: Arc<Mutex<BlockChain>>, + me: &'a Client, +} + +impl<'a> Ui<'a> { + pub fn new(network_config: &'a mut NetworkConfig, blockchain: Arc<Mutex<BlockChain>>, me: &'a Client) -> Self { + Self { + network_config, + blockchain, + me, + } + } + + // TODO: typed command and result propagation + pub async fn cmd(&mut self, cmd: &str) -> Option<Request> { + let cmd: Vec<&str> = cmd + .split_whitespace() + .collect(); + if cmd[0] == "trans" { + let trans = Transaction::new( + *self.me, + Client::new( + cmd[1] + .parse() + .unwrap(), + ), + cmd[2] + .parse() + .unwrap(), + ); + return Some(Request::Block( + self.blockchain + .lock() + .await + .new_block(trans), + )); + } + None + } +} |
