-
Notifications
You must be signed in to change notification settings - Fork 216
/
io.rs
179 lines (153 loc) · 4.96 KB
/
io.rs
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
use std::collections::HashMap;
use std::time::Duration;
use contracts::{contract_trait, post, pre};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tendermint::{
block::signed_header::SignedHeader as TMSignedHeader, validator::Set as TMValidatorSet,
};
use tendermint_rpc as rpc;
use crate::{
bail,
types::{Height, LightBlock, PeerId},
};
pub enum AtHeight {
At(Height),
Highest,
}
impl From<Height> for AtHeight {
fn from(height: Height) -> Self {
if height == 0 {
Self::Highest
} else {
Self::At(height)
}
}
}
#[derive(Clone, Debug, Error, PartialEq, Serialize, Deserialize)]
pub enum IoError {
/// Wrapper for a `tendermint::rpc::Error`.
#[error(transparent)]
IoError(#[from] rpc::Error),
/// Given height is invalid
#[error("invalid height: {0}")]
InvalidHeight(String),
/// The request timed out.
#[error("request to peer {0} timed out")]
Timeout(PeerId),
}
/// Interface for fetching light blocks from a full node, typically via the RPC client.
#[contract_trait]
pub trait Io: Send {
/// Fetch a light block at the given height from the peer with the given peer ID.
///
/// ## Postcondition
/// - The provider of the returned light block matches the given peer [LCV-IO-POST-PROVIDER]
#[post(ret.as_ref().map(|lb| lb.provider == peer).unwrap_or(true))]
fn fetch_light_block(&self, peer: PeerId, height: AtHeight) -> Result<LightBlock, IoError>;
}
#[contract_trait]
impl<F: Send> Io for F
where
F: Fn(PeerId, AtHeight) -> Result<LightBlock, IoError>,
{
fn fetch_light_block(&self, peer: PeerId, height: AtHeight) -> Result<LightBlock, IoError> {
self(peer, height)
}
}
/// Production implementation of the Io component, which fetches
/// light blocks from full nodes via RPC.
#[derive(Clone, Debug)]
pub struct ProdIo {
peer_map: HashMap<PeerId, tendermint::net::Address>,
timeout: Option<Duration>,
}
#[contract_trait]
impl Io for ProdIo {
fn fetch_light_block(&self, peer: PeerId, height: AtHeight) -> Result<LightBlock, IoError> {
let signed_header = self.fetch_signed_header(peer, height)?;
let height: Height = signed_header.header.height.into();
let validator_set = self.fetch_validator_set(peer, height.into())?;
let next_validator_set = self.fetch_validator_set(peer, (height + 1).into())?;
let light_block = LightBlock::new(signed_header, validator_set, next_validator_set, peer);
Ok(light_block)
}
}
impl ProdIo {
/// Constructs a new ProdIo component.
///
/// A peer map which maps peer IDS to their network address must be supplied.
pub fn new(
peer_map: HashMap<PeerId, tendermint::net::Address>,
timeout: Option<Duration>,
) -> Self {
Self { peer_map, timeout }
}
#[pre(self.peer_map.contains_key(&peer))]
fn fetch_signed_header(
&self,
peer: PeerId,
height: AtHeight,
) -> Result<TMSignedHeader, IoError> {
let rpc_client = self.rpc_client_for(peer);
let res = block_on(
async {
match height {
AtHeight::Highest => rpc_client.latest_commit().await,
AtHeight::At(height) => rpc_client.commit(height).await,
}
},
peer,
self.timeout,
)?;
match res {
Ok(response) => Ok(response.signed_header),
Err(err) => Err(IoError::IoError(err)),
}
}
#[pre(self.peer_map.contains_key(&peer))]
fn fetch_validator_set(
&self,
peer: PeerId,
height: AtHeight,
) -> Result<TMValidatorSet, IoError> {
let height = match height {
AtHeight::Highest => bail!(IoError::InvalidHeight(
"given height must be greater than 0".to_string()
)),
AtHeight::At(height) => height,
};
let res = block_on(
self.rpc_client_for(peer).validators(height),
peer,
self.timeout,
)?;
match res {
Ok(response) => Ok(TMValidatorSet::new(response.validators)),
Err(err) => Err(IoError::IoError(err)),
}
}
// FIXME: Cannot enable precondition because of "autoref lifetime" issue
// #[pre(self.peer_map.contains_key(&peer))]
fn rpc_client_for(&self, peer: PeerId) -> rpc::Client {
let peer_addr = self.peer_map.get(&peer).unwrap().to_owned();
rpc::Client::new(peer_addr)
}
}
fn block_on<F: std::future::Future>(
f: F,
peer: PeerId,
timeout: Option<Duration>,
) -> Result<F::Output, IoError> {
let mut rt = tokio::runtime::Builder::new()
.basic_scheduler()
.enable_all()
.build()
.unwrap();
if let Some(timeout) = timeout {
rt.block_on(async { tokio::time::timeout(timeout, f).await })
.map_err(|_| IoError::Timeout(peer))
} else {
Ok(rt.block_on(f))
}
}