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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::net;
use std::ops::{RangeBounds, RangeInclusive};
use crossbeam_channel as chan;
use thiserror::Error;
use nakamoto_common::bitcoin::network::constants::ServiceFlags;
use nakamoto_common::bitcoin::network::Address;
use nakamoto_common::bitcoin::Script;
use nakamoto_common::bitcoin::network::message::NetworkMessage;
use nakamoto_common::block::filter::BlockFilter;
use nakamoto_common::block::tree::{BlockReader, ImportResult};
use nakamoto_common::block::{self, Block, BlockHash, BlockHeader, Height, Transaction};
use nakamoto_common::nonempty::NonEmpty;
use nakamoto_p2p::protocol::Link;
use nakamoto_p2p::protocol::{self, Command, CommandError, GetFiltersError, Peer};
use crate::client::Event;
#[derive(Error, Debug)]
pub enum Error {
#[error("command channel disconnected")]
Disconnected,
#[error("command failed: {0}")]
Command(#[from] CommandError),
#[error("failed to get filters: {0}")]
GetFilters(#[from] GetFiltersError),
#[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 From<chan::RecvTimeoutError> for Error {
fn from(err: chan::RecvTimeoutError) -> Self {
match err {
chan::RecvTimeoutError::Timeout => Self::Timeout,
chan::RecvTimeoutError::Disconnected => Self::Disconnected,
}
}
}
impl<T> From<chan::SendError<T>> for Error {
fn from(_: chan::SendError<T>) -> Self {
Self::Disconnected
}
}
pub trait Handle: Sized + Send + Sync + Clone {
fn get_tip(&self) -> Result<(Height, BlockHeader), Error>;
fn get_block(&self, hash: &BlockHash) -> Result<(), Error>;
fn get_filters(&self, range: RangeInclusive<Height>) -> Result<(), Error>;
fn query_tree(
&self,
query: impl Fn(&dyn BlockReader) + Send + Sync + 'static,
) -> Result<(), Error>;
fn find_branch(&self, to: &BlockHash)
-> Result<Option<(Height, NonEmpty<BlockHeader>)>, Error>;
fn blocks(&self) -> chan::Receiver<(Block, Height)>;
fn filters(&self) -> chan::Receiver<(BlockFilter, BlockHash, Height)>;
fn subscribe(&self) -> chan::Receiver<Event>;
fn command(&self, cmd: Command) -> Result<(), Error>;
fn rescan(
&self,
range: impl RangeBounds<Height>,
watch: impl Iterator<Item = Script>,
) -> Result<(), Error> {
let from = range.start_bound().cloned();
let to = range.end_bound().cloned();
self.command(Command::Rescan {
from,
to,
watch: watch.collect(),
})?;
Ok(())
}
fn watch(&self, watch: impl Iterator<Item = Script>) -> Result<(), Error> {
self.command(Command::Watch {
watch: watch.collect(),
})?;
Ok(())
}
fn broadcast(
&self,
msg: NetworkMessage,
predicate: fn(Peer) -> bool,
) -> Result<Vec<net::SocketAddr>, Error>;
fn query(&self, msg: NetworkMessage) -> Result<Option<net::SocketAddr>, Error>;
fn connect(&self, addr: net::SocketAddr) -> Result<Link, Error>;
fn disconnect(&self, addr: net::SocketAddr) -> Result<(), Error>;
fn submit_transaction(&self, tx: Transaction) -> Result<NonEmpty<net::SocketAddr>, Error>;
fn import_headers(
&self,
headers: Vec<BlockHeader>,
) -> Result<Result<ImportResult, block::tree::Error>, Error>;
fn import_addresses(&self, addrs: Vec<Address>) -> Result<(), Error>;
fn wait<F: FnMut(protocol::Event) -> Option<T>, T>(&self, f: F) -> Result<T, Error>;
fn wait_for_peers(
&self,
count: usize,
required_services: impl Into<ServiceFlags>,
) -> Result<Vec<(net::SocketAddr, Height, ServiceFlags)>, Error>;
fn wait_for_height(&self, h: Height) -> Result<BlockHash, Error>;
fn events(&self) -> chan::Receiver<protocol::Event>;
fn shutdown(self) -> Result<(), Error>;
}