summaryrefslogtreecommitdiff
path: root/src/cli.rs
blob: f171e5ed7d72c6b4394c4ef9cb1ee1a68bb3355c (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::daemon::init;
use std::{
    io,
    sync::{Arc, Weak},
};

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

pub mod daemon;

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()
            };
            println!(
                "{:#?}",
                bc.lock()
                    .await
            );
        }

        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
    }
}

#[tokio::main]
async fn main() {
    let network = init().await;

    let mut ui = Ui::new(
        Arc::downgrade(&network),
        *network
            .lock()
            .await
            .get_me(),
    );

    loop {
        let mut cmd = String::new();
        io::stdin()
            .read_line(&mut cmd)
            .unwrap();

        if let Some(req) = ui
            .cmd(&cmd)
            .await
        {
            Network::broadcast(network.clone(), req).await;
        }
    }
}