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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use flex_error::{define_error, TraceError};
use std::time::Duration;
#[cfg(feature = "rpc-client")]
use tendermint_rpc::Client;
use crate::verifier::types::{Height, LightBlock};
use tendermint_rpc as rpc;
#[cfg(feature = "tokio")]
type TimeoutError = flex_error::DisplayOnly<tokio::time::error::Elapsed>;
#[cfg(not(feature = "tokio"))]
type TimeoutError = flex_error::NoSource;
pub enum AtHeight {
At(Height),
Highest,
}
impl From<Height> for AtHeight {
fn from(height: Height) -> Self {
if height.value() == 0 {
Self::Highest
} else {
Self::At(height)
}
}
}
define_error! {
#[derive(Debug)]
IoError {
Rpc
[ rpc::Error ]
| _ | { "rpc error" },
InvalidHeight
| _ | {
"invalid height: given height must be greater than 0"
},
InvalidValidatorSet
[ tendermint::Error ]
| _ | { "fetched validator set is invalid" },
Timeout
{ duration: Duration }
[ TimeoutError ]
| e | {
format_args!("task timed out after {} ms",
e.duration.as_millis())
},
Runtime
[ TraceError<std::io::Error> ]
| _ | { "failed to initialize runtime" },
}
}
impl IoErrorDetail {
pub fn is_timeout(&self) -> Option<Duration> {
match self {
Self::Timeout(e) => Some(e.duration),
_ => None,
}
}
}
pub trait Io: Send + Sync {
fn fetch_light_block(&self, height: AtHeight) -> Result<LightBlock, IoError>;
}
impl<F: Send + Sync> Io for F
where
F: Fn(AtHeight) -> Result<LightBlock, IoError>,
{
fn fetch_light_block(&self, height: AtHeight) -> Result<LightBlock, IoError> {
self(height)
}
}
#[cfg(feature = "rpc-client")]
pub use self::prod::ProdIo;
#[cfg(feature = "rpc-client")]
mod prod {
use super::*;
use std::time::Duration;
use crate::utils::block_on;
use crate::verifier::types::PeerId;
use tendermint::account::Id as TMAccountId;
use tendermint::block::signed_header::SignedHeader as TMSignedHeader;
use tendermint::validator::Set as TMValidatorSet;
use tendermint_rpc::Paging;
#[derive(Clone, Debug)]
pub struct ProdIo {
peer_id: PeerId,
rpc_client: rpc::HttpClient,
timeout: Option<Duration>,
}
impl Io for ProdIo {
fn fetch_light_block(&self, height: AtHeight) -> Result<LightBlock, IoError> {
let signed_header = self.fetch_signed_header(height)?;
let height = signed_header.header.height;
let proposer_address = signed_header.header.proposer_address;
let validator_set = self.fetch_validator_set(height.into(), Some(proposer_address))?;
let next_validator_set = self.fetch_validator_set(height.increment().into(), None)?;
let light_block = LightBlock::new(
signed_header,
validator_set,
next_validator_set,
self.peer_id,
);
Ok(light_block)
}
}
impl ProdIo {
pub fn new(
peer_id: PeerId,
rpc_client: rpc::HttpClient,
timeout: Option<Duration>,
) -> Self {
Self {
peer_id,
rpc_client,
timeout,
}
}
fn fetch_signed_header(&self, height: AtHeight) -> Result<TMSignedHeader, IoError> {
let client = self.rpc_client.clone();
let res = block_on(self.timeout, async move {
match height {
AtHeight::Highest => client.latest_commit().await,
AtHeight::At(height) => client.commit(height).await,
}
})?;
match res {
Ok(response) => Ok(response.signed_header),
Err(err) => Err(IoError::rpc(err)),
}
}
fn fetch_validator_set(
&self,
height: AtHeight,
proposer_address: Option<TMAccountId>,
) -> Result<TMValidatorSet, IoError> {
let height = match height {
AtHeight::Highest => {
return Err(IoError::invalid_height());
}
AtHeight::At(height) => height,
};
let client = self.rpc_client.clone();
let response = block_on(self.timeout, async move {
client.validators(height, Paging::All).await
})?
.map_err(IoError::rpc)?;
let validator_set = match proposer_address {
Some(proposer_address) => {
TMValidatorSet::with_proposer(response.validators, proposer_address)
.map_err(IoError::invalid_validator_set)?
}
None => TMValidatorSet::without_proposer(response.validators),
};
Ok(validator_set)
}
}
}