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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::net::IpAddr;
use std::sync::Mutex;
use std::time::Instant;

use bitcoin::Block;
use bitcoin::BlockHash;
use bitcoin::Transaction;
use bitcoin::Txid;
use floresta_chain::UtreexoBlock;
use serde::Deserialize;
use serde::Serialize;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UserRequest {
    Block(BlockHash),
    UtreexoBlock(BlockHash),
    MempoolTransaction(Txid),
    GetPeerInfo,
    Connect((IpAddr, u16)),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerInfo {
    pub address: String,
    pub services: String,
    pub user_agent: String,
    pub initial_height: u32,
}

#[derive(Debug, Clone)]
pub enum NodeResponse {
    Block(Block),
    UtreexoBlock(UtreexoBlock),
    MempoolTransaction(Transaction),
    GetPeerInfo(Vec<PeerInfo>),
    Connect(bool),
}

pub trait NodeMethods {
    fn get_block(&self, block: BlockHash) -> Result<Option<Block>, oneshot::RecvError>;
    fn get_utreexo_block(
        &self,
        block: BlockHash,
    ) -> Result<Option<UtreexoBlock>, oneshot::RecvError>;
    fn get_mempool_transaction(
        &self,
        txid: Txid,
    ) -> Result<Option<Transaction>, oneshot::RecvError>;
    fn get_peer_info(&self) -> Result<Vec<PeerInfo>, oneshot::RecvError>;
    fn connect(&self, addr: IpAddr, port: u16) -> Result<bool, oneshot::RecvError>;
}
#[derive(Debug)]
pub struct NodeInterface {
    pub(super) requests: Mutex<Vec<RequestData>>,
}
#[derive(Debug)]
pub struct RequestData {
    pub time: Instant,
    pub resolve: oneshot::Sender<Option<NodeResponse>>,
    pub req: UserRequest,
}
impl NodeInterface {
    pub fn send_answer(&self, request: UserRequest, answer: Option<NodeResponse>) {
        let mut requests = self.requests.lock().unwrap();
        let req = requests.iter().position(|x| x.req == request);
        if let Some(req) = req {
            let req = requests.remove(req);
            req.resolve.send(answer).unwrap();
        }
    }
}
impl NodeMethods for NodeInterface {
    fn connect(&self, addr: IpAddr, port: u16) -> Result<bool, oneshot::RecvError> {
        let (tx, rx) = oneshot::channel();
        self.requests.lock().unwrap().push(RequestData {
            time: Instant::now(),
            resolve: tx,
            req: UserRequest::Connect((addr, port)),
        });
        let connected = rx.recv()?;
        Ok(match connected {
            Some(NodeResponse::Connect(connected)) => connected,
            _ => unreachable!(),
        })
    }
    fn get_block(&self, block: BlockHash) -> Result<Option<Block>, oneshot::RecvError> {
        let (tx, rx) = oneshot::channel();
        self.requests.lock().unwrap().push(RequestData {
            time: Instant::now(),
            resolve: tx,
            req: UserRequest::Block(block),
        });
        let blk = rx.recv()?;
        Ok(match blk {
            Some(NodeResponse::Block(blk)) => Some(blk),
            None => None,
            _ => unreachable!(),
        })
    }

    fn get_utreexo_block(
        &self,
        block: BlockHash,
    ) -> Result<Option<UtreexoBlock>, oneshot::RecvError> {
        let (tx, rx) = oneshot::channel();
        self.requests.lock().unwrap().push(RequestData {
            time: Instant::now(),
            resolve: tx,
            req: UserRequest::UtreexoBlock(block),
        });
        let blk = rx.recv()?;
        Ok(match blk {
            Some(NodeResponse::UtreexoBlock(blk)) => Some(blk),
            None => None,
            _ => unreachable!(),
        })
    }

    fn get_mempool_transaction(
        &self,
        txid: Txid,
    ) -> Result<Option<Transaction>, oneshot::RecvError> {
        let (tx, rx) = oneshot::channel();
        self.requests.lock().unwrap().push(RequestData {
            time: Instant::now(),
            resolve: tx,
            req: UserRequest::MempoolTransaction(txid),
        });
        let tx = rx.recv()?;
        Ok(match tx {
            Some(NodeResponse::MempoolTransaction(tx)) => Some(tx),
            None => None,
            _ => unreachable!(),
        })
    }

    fn get_peer_info(&self) -> Result<Vec<PeerInfo>, oneshot::RecvError> {
        let (tx, rx) = oneshot::channel();
        self.requests.lock().unwrap().push(RequestData {
            time: Instant::now(),
            resolve: tx,
            req: UserRequest::GetPeerInfo,
        });
        let peer_info = rx.recv()?;
        Ok(match peer_info {
            Some(NodeResponse::GetPeerInfo(peer_info)) => peer_info,
            None => Vec::new(),
            _ => unreachable!(),
        })
    }
}