-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblock_downloader.rs
173 lines (161 loc) · 6.72 KB
/
block_downloader.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
use crate::{
config::Config,
types::{ManagerMessage, ManagerMessageKind, ShutdownSignal},
};
use near_jsonrpc_client::{methods, JsonRpcClient};
use near_primitives::{
hash::CryptoHash,
types::{BlockId, BlockReference, Finality},
views::BlockView,
};
use std::time::Duration;
use tokio::{
sync::mpsc::{self, Receiver, Sender},
task::JoinHandle,
};
/// An "actor" which represents a background task to poll the Near RPC
/// at regular intervals for new blocks.
pub struct BlockDownloader {
id: String,
client: JsonRpcClient,
polling_frequency: Duration,
manager_channel: Sender<ManagerMessage>,
shutdown_channel: Receiver<ShutdownSignal>,
last_seen_block: BlockView,
retry_count: usize,
max_retry_count: usize,
}
impl BlockDownloader {
pub async fn new(
config: &Config,
manager_channel: Sender<ManagerMessage>,
id_no: usize,
) -> anyhow::Result<(Self, Sender<ShutdownSignal>)> {
let id = format!("BlockDownloader_{id_no}");
let max_retry_count = config.max_download_retry.into();
let polling_frequency = Duration::from_millis(config.polling_frequency_ms);
let client = JsonRpcClient::new_client().connect(&config.near_rpc_url);
let last_seen_block = get_latest_block(&client).await?;
let (shutdown_sender, shutdown_channel) = mpsc::channel(5);
let this = Self {
id,
client,
last_seen_block,
manager_channel,
shutdown_channel,
polling_frequency,
retry_count: 0,
max_retry_count,
};
Ok((this, shutdown_sender))
}
pub fn start(mut self) -> JoinHandle<anyhow::Result<()>> {
tokio::task::spawn(async move {
loop {
let maybe_shutdown =
tokio::time::timeout(self.polling_frequency, self.shutdown_channel.recv())
.await;
match maybe_shutdown {
Ok(Some(ShutdownSignal)) => {
tracing::info!("BlockDownloader received ShutdownSignal");
break;
}
Ok(None) => {
tracing::warn!("BlockDownloader shutdown channel closed.");
break;
}
Err(_) => {
// Err(_) means we hit the polling frequency before receiving a shutdown message.
// So let's see if there is a new block to download.
tracing::debug!("BlockDownloader beginning polling cycle");
let maybe_latest_block = get_latest_block(&self.client).await;
let maybe_blocks = match maybe_latest_block {
Ok(block) => {
// If the block has not updated then we wait for
// the next polling cycle.
if block.header.hash == self.last_seen_block.header.hash {
continue;
}
download_block_chain(
&self.client,
block,
self.last_seen_block.header.hash,
)
.await
}
Err(e) => Err(e),
};
match maybe_blocks {
Ok(blocks) => {
self.retry_count = 0;
for block in blocks {
let message = ManagerMessageKind::NewBlock {
block: Box::new(self.last_seen_block),
next_block_hash: block.header.hash,
};
self.last_seen_block = block;
if let Err(e) = self.send_manager_message(message).await {
tracing::error!(
"BlockDownloader failed to communicate with Manager."
);
return Err(e);
}
}
}
Err(e) => {
tracing::warn!("BlockDownloader failed to fetch blocks: {:?}", e);
self.retry_count += 1;
if self.retry_count >= self.max_retry_count {
self.send_manager_message(ManagerMessageKind::Shutdown(
ShutdownSignal,
))
.await
.ok();
return Err(anyhow::anyhow!("Failed to fetch blocks"));
}
}
}
}
}
}
Ok(())
})
}
async fn send_manager_message(&self, kind: ManagerMessageKind) -> anyhow::Result<()> {
let message = ManagerMessage {
worker_id: self.id.clone(),
kind,
};
self.manager_channel.send(message).await?;
Ok(())
}
}
/// Downloads blocks, following parent hashes until the `target_parent` is reached.
async fn download_block_chain(
client: &JsonRpcClient,
current_block: BlockView,
target_parent: CryptoHash,
) -> anyhow::Result<Vec<BlockView>> {
let mut blocks = vec![current_block];
while blocks.last().unwrap().header.prev_hash != target_parent {
let hash = blocks.last().unwrap().header.prev_hash;
let block_request = methods::block::RpcBlockRequest {
block_reference: BlockReference::BlockId(BlockId::Hash(hash)),
};
tracing::debug!("JsonRpcClient call to download block {:?}", hash);
let block = client.call(block_request).await?;
blocks.push(block);
}
// Reverse the order of blocks so they are ordered oldest to newest
// instead of the other way around.
blocks.reverse();
Ok(blocks)
}
async fn get_latest_block(client: &JsonRpcClient) -> anyhow::Result<BlockView> {
let block_request = methods::block::RpcBlockRequest {
block_reference: BlockReference::Finality(Finality::DoomSlug),
};
tracing::debug!("JsonRpcClient call to download latest block");
let block = client.call(block_request).await?;
Ok(block)
}