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
use std::net;
use crossbeam_channel as chan;
use thiserror::Error;
use nakamoto_common::block::tree::ImportResult;
use nakamoto_common::block::{self, Block, BlockHash, BlockHeader, Height, Transaction};
use nakamoto_p2p::protocol::Link;
#[derive(Error, Debug)]
pub enum Error {
#[error("command channel disconnected")]
Disconnected,
#[error("the operation timed out")]
Timeout,
#[error(transparent)]
Io(#[from] std::io::Error),
}
impl From<chan::RecvError> for Error {
fn from(_: chan::RecvError) -> Self {
Self::Disconnected
}
}
impl<T> From<chan::SendError<T>> for Error {
fn from(_: chan::SendError<T>) -> Self {
Self::Disconnected
}
}
pub trait Handle {
type Event;
type Message;
fn get_tip(&self) -> Result<BlockHeader, Error>;
fn get_block(&self, hash: &BlockHash) -> Result<Block, Error>;
fn connect(&self, addr: net::SocketAddr) -> Result<Link, Error>;
fn submit_transaction(&self, tx: Transaction) -> Result<(), Error>;
fn import_headers(
&self,
headers: Vec<BlockHeader>,
) -> Result<Result<ImportResult, block::tree::Error>, Error>;
fn receive(&self, from: net::SocketAddr, msg: Self::Message) -> Result<(), Error>;
fn wait<F: Fn(Self::Event) -> Option<T>, T>(&self, f: F) -> Result<T, Error>;
fn wait_for_peers(&self, count: usize) -> Result<(), Error>;
fn wait_for_ready(&self) -> Result<(), Error>;
fn wait_for_height(&self, h: Height) -> Result<BlockHash, Error>;
fn shutdown(self) -> Result<(), Error>;
}