summaryrefslogtreecommitdiff
path: root/src/ui.rs
blob: c820422159482e72e816196caf6456c7f1031331 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
    }
}