forked from ethereum/trin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportal_rpc.rs
313 lines (281 loc) · 11.2 KB
/
portal_rpc.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use anyhow::{anyhow, bail};
use async_trait::async_trait;
use ethportal_api::{
consensus::light_client::bootstrap::LightClientBootstrap,
light_client::{
bootstrap::LightClientBootstrapDeneb, finality_update::LightClientFinalityUpdateDeneb,
optimistic_update::LightClientOptimisticUpdateDeneb, update::LightClientUpdateDeneb,
},
types::{
consensus::light_client::{
finality_update::LightClientFinalityUpdate,
optimistic_update::LightClientOptimisticUpdate, update::LightClientUpdate,
},
content_key::beacon::{LightClientFinalityUpdateKey, LightClientOptimisticUpdateKey},
},
BeaconContentKey, BeaconContentValue, ContentValue, LightClientBootstrapKey,
LightClientUpdatesByRangeKey,
};
use futures::channel::oneshot;
use tokio::sync::mpsc::UnboundedSender;
use tracing::warn;
use trin_portalnet::overlay::command::OverlayCommand;
use crate::consensus::rpc::ConsensusRpc;
pub const BEACON_GENESIS_TIME: u64 = 1606824023;
#[derive(Clone, Debug)]
pub struct PortalRpc {
overlay_tx: UnboundedSender<OverlayCommand<BeaconContentKey>>,
}
impl PortalRpc {
pub fn with_portal(overlay_tx: UnboundedSender<OverlayCommand<BeaconContentKey>>) -> Self {
Self { overlay_tx }
}
}
#[async_trait]
impl ConsensusRpc for PortalRpc {
fn new(_path: &str) -> Self {
unreachable!("PortalRpc does not use path.")
}
async fn get_bootstrap(
&self,
block_root: &'_ [u8],
) -> anyhow::Result<LightClientBootstrapDeneb> {
let bootstrap_key = BeaconContentKey::LightClientBootstrap(LightClientBootstrapKey {
block_hash: <[u8; 32]>::try_from(block_root)?,
});
let (tx, rx) = oneshot::channel();
let overlay_command = OverlayCommand::FindContentQuery {
target: bootstrap_key.clone(),
callback: tx,
config: Default::default(),
};
if let Err(err) = self.overlay_tx.send(overlay_command) {
warn!(
protocol = "beacon",
error = %err,
"Error submitting FindContent query to service"
);
return Err(err.into());
}
match rx.await {
Ok(result) => {
let bootstrap = match result {
Ok(result) => BeaconContentValue::decode(&bootstrap_key, &result.0)?,
Err(err) => {
bail!("LightClientBootstrap content not found on the network: {err}")
}
};
if let BeaconContentValue::LightClientBootstrap(bootstrap) = bootstrap {
if let LightClientBootstrap::Deneb(bootstrap_deneb) = bootstrap.bootstrap {
Ok(bootstrap_deneb)
} else {
bail!("Error converting LightClientBootstrap to LightClientBootstrapDeneb")
}
} else {
bail!("Error converting BeaconContentValue to LightClientBootstrap");
}
}
Err(err) => {
warn!(
protocol = %"beacon",
error = %err,
"Error receiving FindContent query response"
);
return Err(err.into());
}
}
}
async fn get_updates(
&self,
period: u64,
count: u8,
) -> anyhow::Result<Vec<LightClientUpdateDeneb>> {
let updates_key =
BeaconContentKey::LightClientUpdatesByRange(LightClientUpdatesByRangeKey {
start_period: period,
count: count as u64,
});
let (tx, rx) = oneshot::channel();
let overlay_command = OverlayCommand::FindContentQuery {
target: updates_key.clone(),
callback: tx,
config: Default::default(),
};
if let Err(err) = self.overlay_tx.send(overlay_command) {
warn!(
protocol = "beacon",
error = %err,
"Error submitting FindContent query to service"
);
return Err(err.into());
}
match rx.await {
Ok(result) => {
let content_value = match result {
Ok(result) => BeaconContentValue::decode(&updates_key, &result.0)?,
Err(err) => {
return Err(anyhow!("LightClientUpdatesByRange period={period}, count={count} not found on the network: {err}"));
}
};
if let BeaconContentValue::LightClientUpdatesByRange(updates) = content_value {
let deneb_updates: Vec<LightClientUpdate> = updates
.as_ref()
.iter()
.map(|update| update.update.clone())
.collect();
let mut result: Vec<LightClientUpdateDeneb> = Vec::new();
for update in deneb_updates {
// Check if updates is LightLCientUpdateDeneb
if let LightClientUpdate::Deneb(update_deneb) = update {
result.push(update_deneb);
} else {
return Err(anyhow!(
"Error converting LightClientUpdate to LightClientUpdateDeneb"
));
}
}
Ok(result)
} else {
return Err(anyhow!(
"Error converting BeaconContentValue to LightClientUpdatesByRange"
));
}
}
Err(err) => {
warn!(
protocol = %"beacon",
error = %err,
"Error receiving FindContent query response"
);
return Err(err.into());
}
}
}
async fn get_finality_update(&self) -> anyhow::Result<LightClientFinalityUpdateDeneb> {
// Get the finality update for the most recent finalized epoch. We use 0 as the finalized
// slot because the finalized slot is not known at this point and the protocol is
// designed to return the most recent which is > 0
let finality_update_key =
BeaconContentKey::LightClientFinalityUpdate(LightClientFinalityUpdateKey {
finalized_slot: 0,
});
let (tx, rx) = oneshot::channel();
let overlay_command = OverlayCommand::FindContentQuery {
target: finality_update_key.clone(),
callback: tx,
config: Default::default(),
};
if let Err(err) = self.overlay_tx.send(overlay_command) {
warn!(
protocol = "beacon",
error = %err,
"Error submitting FindContent query to service"
);
return Err(err.into());
}
match rx.await {
Ok(result) => {
let finality_update = match result {
Ok(result) => BeaconContentValue::decode(&finality_update_key, &result.0)?,
Err(err) => {
return Err(anyhow!("LightClientFinalityUpdate content with finalized slot 0 not found on the network: {err}"));
}
};
if let BeaconContentValue::LightClientFinalityUpdate(finality_update) =
finality_update
{
if let LightClientFinalityUpdate::Deneb(finality_update_deneb) =
finality_update.update
{
Ok(finality_update_deneb)
} else {
return Err(anyhow!(
"Error converting LightClientFinalityUpdate to LightClientFinalityUpdateDeneb"
));
}
} else {
return Err(anyhow!(
"Error converting BeaconContentValue to LightClientFinalityUpdate"
));
}
}
Err(err) => {
warn!(
protocol = %"beacon",
error = %err,
"Error receiving FindContent query response"
);
return Err(err.into());
}
}
}
async fn get_optimistic_update(&self) -> anyhow::Result<LightClientOptimisticUpdateDeneb> {
let expected_current_slot = expected_current_slot();
let optimistic_update_key =
BeaconContentKey::LightClientOptimisticUpdate(LightClientOptimisticUpdateKey {
signature_slot: expected_current_slot,
});
let (tx, rx) = oneshot::channel();
let overlay_command = OverlayCommand::FindContentQuery {
target: optimistic_update_key.clone(),
callback: tx,
config: Default::default(),
};
if let Err(err) = self.overlay_tx.send(overlay_command) {
warn!(
protocol = "beacon",
error = %err,
"Error submitting FindContent query to service"
);
return Err(err.into());
}
match rx.await {
Ok(result) => {
let optimistic_update = match result {
Ok(result) => BeaconContentValue::decode(&optimistic_update_key, &result.0)?,
Err(err) => {
return Err(anyhow!("LightClientOptimisticUpdate content with signature slot {expected_current_slot} not found on the network: {err}"));
}
};
if let BeaconContentValue::LightClientOptimisticUpdate(optimistic_update) =
optimistic_update
{
if let LightClientOptimisticUpdate::Deneb(optimistic_update_deneb) =
optimistic_update.update
{
Ok(optimistic_update_deneb)
} else {
return Err(anyhow!(
"Error converting LightClientOptimisticUpdate to LightClientOptimisticUpdateDeneb"
));
}
} else {
return Err(anyhow!(
"Error converting BeaconContentValue to LightClientOptimisticUpdate"
));
}
}
Err(err) => {
warn!(
protocol = %"beacon",
error = %err,
"Error receiving FindContent query response"
);
return Err(err.into());
}
}
}
async fn chain_id(&self) -> anyhow::Result<u64> {
Ok(1)
}
fn name(&self) -> String {
"portal".to_string()
}
}
pub fn expected_current_slot() -> u64 {
let now = SystemTime::now();
let now = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
let since_genesis = now - Duration::from_secs(BEACON_GENESIS_TIME);
since_genesis.as_secs() / 12
}