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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
use std::{collections::HashMap, net::SocketAddr, sync::Arc, time::Duration};
use chrono::{TimeDelta, Utc};
use futures_util::future;
use serde_json::Value;
use snops_common::state::{EnvId, LatestBlockInfo, NetworkId, NodeKey};
use tokio::{sync::mpsc, time::timeout};
use super::{snarkos_request, AgentClient, GlobalState};
use crate::{
env::{
cache::{ABlockHash, ATransactionId, MAX_BLOCK_RANGE},
EnvNodeState, EnvPeer,
},
schema::nodes::ExternalNode,
};
type ExtPeerPair = (NodeKey, SocketAddr);
struct BlockRequestPeers {
height: u32,
peers: Vec<ExtPeerPair>,
}
type PendingBlockRequests = HashMap<(EnvId, NetworkId), HashMap<ABlockHash, BlockRequestPeers>>;
/// Hit all the external peers to update their latest block infos.
///
/// If an external peer has a new block info and transaction list, update the
/// cache with the new data.
pub async fn block_info_task(state: Arc<GlobalState>) {
loop {
// Get applicable external peers. This is unfiltered as all block info can be
// expected to be out of date before the next time this loop is run.
let external_rest_peers = get_all_external_peers(&state);
// channel to measure the success of peer requests
let (req_ok_tx, mut req_ok_rx) = mpsc::unbounded_channel();
// fetch the latest block hashes for EVERY external peer across EVERY
// environment
let peers_with_block_hashes = future::join_all(external_rest_peers.into_iter().map(
|((env, network), peers)| {
let req_ok_tx = req_ok_tx.clone();
async move {
let peers = future::join_all(peers.into_iter().map(|(key, addr)| {
let req_ok_tx = req_ok_tx.clone();
async move {
let res = timeout(
// short timeout for block hash requests as not much is being
// serialized on snarkOS side
Duration::from_secs(1),
get_block_hash_for_peer(network, addr),
)
.await
.ok()
.and_then(|hash| hash.map(|h| (key.clone(), addr, h)));
// mark down a successful request
let _ = req_ok_tx.send((env, key, res.is_some()));
res
}
}))
.await;
((env, network), peers)
}
},
))
.await;
let now = Utc::now();
// map of block hashes and environments to peers that can provide them
// TODO: fetch this from an AOT peer instead if possible
let mut blocks_pending_request: PendingBlockRequests = HashMap::new();
// Go through each env and peer info
for ((env, network), peers_and_hashes) in peers_with_block_hashes {
// If there is no cache we skip
let Some(mut cache) = state.env_network_cache.get_mut(&env) else {
continue;
};
// Go through each peer for an env if they were responsive with the block hash
// request (flatten)
for (key, addr, (hash, height)) in peers_and_hashes.into_iter().flatten() {
// update the peer's block info if it is different than the peer's current info
cache.update_peer_info_for_hash(&key, &hash);
// prevent re-requesting the list of transactions for a block that
// is already cached
if cache.block_to_transaction.contains_key(&hash) {
continue;
}
// prevent making a request on a peer that is probably syncing (way out of date
// height)
if cache.latest.as_ref().is_some_and(|i|
// peer's height outside the max block range
i.height.saturating_sub(MAX_BLOCK_RANGE) >= height
// and the block range is recent
&& (now - i.update_time) < TimeDelta::seconds(60))
{
continue;
}
use std::collections::hash_map::Entry::*;
// update the list of blocks that need to be requested
match blocks_pending_request
.entry((env, network))
.or_default()
.entry(hash)
{
// append this peer to the list of peers that can provide
// use the min height because of a slim chance that the latest block changed
// in the time between the height and hash requests.
Occupied(e) => {
let e = e.into_mut();
e.height = e.height.min(height);
e.peers.push((key, addr));
}
// insert this height and peer into the list of peers that can provide
Vacant(e) => {
e.insert(BlockRequestPeers {
height,
peers: vec![(key, addr)],
});
}
}
}
}
// fetch the missing block info from agents if possible (fallback on external
// peers), then update the cache with the peer data
let block_request_tasks = future::join_all(blocks_pending_request.into_iter().map(
|((env, network), requests)| {
// highest height of all requests
let max_height = requests
.values()
.map(|BlockRequestPeers { height, .. }| *height)
.max()
.unwrap();
// list of agents that could fulfil this request (rather than making slow rest &
// deserialize requests)
let agents = Arc::new(online_agents_above_height(&state, env, max_height));
let req_ok_tx = req_ok_tx.clone();
async move {
(
env,
future::join_all(requests.into_iter().map(
|(hash, BlockRequestPeers { peers, .. })| {
let req_ok_tx = req_ok_tx.clone();
let agents = agents.clone();
// peer keys to update (or request)
let keys =
peers.iter().map(|(key, _)| key.clone()).collect::<Vec<_>>();
async move {
// attempt to use agents to get the block
if let Some(res) =
get_block_from_agents(&agents, Arc::clone(&hash)).await
{
return Some((res, keys));
}
// if agents failed, fallback on external peers
let mut failures = 0u8;
for (key, addr) in peers {
if let Some(res) =
get_block_info_for_peer(network, addr).await
{
let _ = req_ok_tx.send((env, key, true));
return Some((res, keys));
}
let _ = req_ok_tx.send((env, key, false));
failures += 1;
if failures >= MAX_BLOCK_REQUEST_FAILURES {
break;
}
}
None
}
},
))
.await,
)
}
},
))
.await;
// update the cache with the request results
while let Ok((env, key, success)) = req_ok_rx.try_recv() {
let Some(mut cache) = state.env_network_cache.get_mut(&env) else {
continue;
};
cache.update_peer_req(&key, success);
}
// update the chache with the block info and transaction ids
// from the block requests
for (env, responses) in block_request_tasks {
let Some(mut cache) = state.env_network_cache.get_mut(&env) else {
continue;
};
// update the cache with the block info and transaction ids
// then update each peer's info
for ((info, txs), keys) in responses.into_iter().flatten() {
cache.add_block(info.clone(), txs);
for key in keys {
cache.update_latest_info(&info);
cache.update_peer_info(key, info.clone());
}
}
}
// wait 10 seconds between checks, including the time it took to process
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
}
}
/// Get all online agents above a certain height in an environment
pub fn online_agents_above_height(
state: &GlobalState,
env: EnvId,
height: u32,
) -> Vec<AgentClient> {
let Some(env) = state.get_env(env) else {
return Vec::new();
};
env.node_peers
.iter()
.filter_map(|(_, peer)| {
// ensure peer is internal
let EnvPeer::Internal(agent_id) = peer else {
return None;
};
let agent = state.pool.get(agent_id)?;
// ensure peer height is above or equal the requested height
if agent.status.block_info.as_ref()?.height < height {
return None;
}
// ensure the agent is online
agent.client_owned()
})
.collect()
}
/// Obtain a peer's latest block hash and height
/// We do not assume the hash and height are related, and they are used for
/// separate purposes.
async fn get_block_hash_for_peer(network: NetworkId, addr: SocketAddr) -> Option<(Arc<str>, u32)> {
// make a request to the external peer for the latest block hash
let hash_res = snarkos_request::get_on_addr::<Value>(network, "/block/hash/latest", addr)
.await
.ok()?;
let height_res = snarkos_request::get_on_addr::<Value>(network, "/block/hash/latest", addr)
.await
.ok()?;
Some((hash_res.as_str()?.into(), height_res.as_u64()? as u32))
}
const MAX_BLOCK_REQUEST_FAILURES: u8 = 3;
/// Obtain a block from a list of agents, permits up to 3 failures
async fn get_block_from_agents(
agents: &Vec<AgentClient>,
hash: ABlockHash,
) -> Option<(LatestBlockInfo, Vec<ATransactionId>)> {
let mut failures = 0u8;
for agent in agents {
if let Ok(Some(block)) = agent.get_snarkos_block_lite(hash.to_string()).await {
return Some(block.split());
}
failures += 1;
if failures >= MAX_BLOCK_REQUEST_FAILURES {
break;
}
}
None
}
/// Obtain a peer's block info and transaction ids
async fn get_block_info_for_peer(
network: NetworkId,
addr: SocketAddr,
) -> Option<(LatestBlockInfo, Vec<ATransactionId>)> {
// make a request to the external peer for the latest block
// TODO: make this a RawValue to prevent unnecessarily parsing the response
let Ok(block_raw) = snarkos_request::get_on_addr::<Value>(network, "/block/latest", addr).await
else {
tracing::trace!("failed to get latest block for peer: {addr:?}");
return None;
};
let Some(block_hash) = block_raw.get("block_hash").and_then(|s| s.as_str()) else {
tracing::trace!("block request for peer is missing block hash: {addr:?}");
return None;
};
let Some(previous_hash) = block_raw.get("previous_hash").and_then(|s| s.as_str()) else {
tracing::trace!("block request for peer is missing previous hash: {addr:?}");
return None;
};
let Some(header) = block_raw.get("header").and_then(|h| h.get("metadata")) else {
tracing::trace!("block request for peer is missing header metadata: {addr:?}");
return None;
};
let Some(height) = header
.get("height")
.and_then(|h| h.as_u64().map(|h| h as u32))
else {
tracing::trace!("block request for peer is missing block height: {addr:?}");
return None;
};
let Some(block_timestamp) = header.get("timestamp").and_then(|t| t.as_i64()) else {
tracing::trace!("block request for peer is missing block timestamp: {addr:?}");
return None;
};
let Some(txs_raw) = block_raw.get("transactions").and_then(|t| t.as_array()) else {
tracing::trace!("block request for peer is missing transactions: {addr:?}");
return None;
};
// fetch the state root (because it's missing from the block)
let route = format!("/stateRoot/{height}");
let Ok(state_root) = snarkos_request::get_on_addr::<String>(network, &route, addr).await else {
tracing::trace!("failed to get state root for peer: {addr:?}");
return None;
};
// assemble transaction ids from valid json value
let mut txs = Vec::with_capacity(txs_raw.len());
for tx in txs_raw {
let Some(tx_id) = tx
.get("transaction")
.and_then(|tx| tx.get("id").and_then(|id| id.as_str()))
else {
tracing::trace!("transaction is missing tx_id: {tx:?}");
continue;
};
txs.push(Arc::from(tx_id));
}
Some((
LatestBlockInfo {
height,
state_root,
block_hash: block_hash.to_owned(),
block_timestamp,
previous_hash: previous_hash.to_owned(),
update_time: Utc::now(),
},
txs,
))
}
// Compute a list of all external peers that have rest addresses
fn get_all_external_peers(state: &GlobalState) -> Vec<((EnvId, NetworkId), Vec<ExtPeerPair>)> {
state
.envs
.iter()
.map(|e| {
let Some(cache) = state.env_network_cache.get(e.key()) else {
return ((*e.key(), e.network), Vec::new());
};
(
// environment meta required for requests and cache updates
(*e.key(), e.network),
// iterate the environment's nodes
e.node_states
.iter()
.filter_map(|n| {
// skip unresponsive peers
if cache.is_peer_penalized(n.key()) {
return None;
}
match n.value() {
// filter by external with rest addresses
EnvNodeState::External(ExternalNode {
rest: Some(addr), ..
}) => Some((n.key().clone(), *addr)),
_ => None,
}
})
.collect::<Vec<_>>(),
)
})
// collect here to avoid holding a dashmap read lock for too long
.collect::<Vec<_>>()
}