summaryrefslogtreecommitdiff
path: root/src/ui.rs
blob: f11eaaaa4c777a612b0a5111ac8428b957575030 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::sync::{Arc, Weak};

use bitcoin::{
    client::Client,
    network::{Network, Request},
    transaction::Transaction,
};
use tokio::sync::Mutex;

pub struct Ui {
    network: Weak<Mutex<Network>>,
    me: Client,
}

impl Ui {
    pub fn new(network: Weak<Mutex<Network>>, me: Client) -> Self {
        Self {
            network,
            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(),
            );
            let net = self
                .network
                .upgrade()
                .unwrap();
            let block = Network::new_block(net, trans).await;
            return Some(Request::Block(Arc::new(block)));
        }

        if cmd[0] == "bc" {
            let net = self
                .network
                .upgrade()
                .unwrap();
            let bc = {
                net.lock()
                    .await
                    .get_blockchain()
                    .clone()
            };
            let bc = bc
                .lock()
                .await;
            if let Some(cmd) = cmd.get(1) {
                if *cmd == "verify" {
                    bc.verify(
                        &bc.last()
                            .unwrap()
                            .0
                            .clone(),
                    );
                }
            } else {
                println!("{:#?}", bc);
            }
        }

        if cmd[0] == "peer" {
            if cmd[1] == "add" {
                let net = self
                    .network
                    .upgrade()
                    .unwrap();
                Network::try_peer(net, cmd[2]).await;
            }

            if cmd[1] == "list" {
                let net = self
                    .network
                    .upgrade()
                    .unwrap();
                println!(
                    "{:#?}",
                    net.lock()
                        .await
                        .peers
                );
            }
        }

        if cmd[0] == "fetch" {
            let net = self
                .network
                .upgrade()
                .unwrap();
            Network::get_idb(net);
        }

        None
    }
}