Skip to content

Commit fa41faa

Browse files
authored
fix: fetch only header instead of full block (#64)
1 parent dbb8dca commit fa41faa

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

script/src/tendermint.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ impl Default for TendermintRPCClient {
2929
pub const DEFAULT_TENDERMINT_RPC_TIMEOUT_SECS: u64 = 20;
3030

3131
/// The default concurrency for Tendermint RPC requests.
32-
pub const DEFAULT_TENDERMINT_RPC_CONCURRENCY: usize = 5;
32+
pub const DEFAULT_TENDERMINT_RPC_CONCURRENCY: usize = 20;
3333

3434
/// The default sleep duration for Tendermint RPC requests in milliseconds.
35-
pub const DEFAULT_TENDERMINT_RPC_SLEEP_MS: Duration = Duration::from_millis(1250);
35+
pub const DEFAULT_TENDERMINT_RPC_SLEEP_MS: Duration = Duration::from_millis(500);
3636

3737
/// The maximum number of failures allowed when retrying a Tendermint RPC request.
3838
pub const DEFAULT_FAILURES_ALLOWED: u32 = 20;
@@ -128,6 +128,19 @@ impl TendermintRPCClient {
128128
.context("Failed to parse block by height response")
129129
}
130130

131+
/// Fetches the header by its height.
132+
pub async fn fetch_header_by_height(&self, height: u64) -> anyhow::Result<HeaderResponse> {
133+
let url = format!("{}/header?height={}", self.url, height);
134+
self.client
135+
.get(url)
136+
.send()
137+
.await
138+
.context("Failed to fetch header by height")?
139+
.json::<HeaderResponse>()
140+
.await
141+
.context("Failed to parse header by height response")
142+
}
143+
131144
/// Fetches the latest commit from the Tendermint node.
132145
pub async fn fetch_latest_commit(&self) -> anyhow::Result<CommitResponse> {
133146
pub async fn inner(client: &TendermintRPCClient) -> anyhow::Result<CommitResponse> {

script/src/types.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::Deserialize;
22
use tendermint::{
3-
block::{self, signed_header::SignedHeader},
3+
block::{self, signed_header::SignedHeader, Header},
44
validator::Info,
55
Block,
66
};
@@ -32,6 +32,16 @@ pub struct BlockWrapper {
3232
pub block: Block,
3333
}
3434

35+
#[derive(Debug, Deserialize)]
36+
pub struct HeaderResponse {
37+
pub result: HeaderWrapper,
38+
}
39+
40+
#[derive(Debug, Deserialize)]
41+
pub struct HeaderWrapper {
42+
pub header: Header,
43+
}
44+
3545
#[derive(Debug, Deserialize)]
3646
pub struct CommitResponse {
3747
pub result: SignedHeaderWrapper,

script/src/util.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ pub async fn get_block(client: &TendermintRPCClient, height: u64) -> anyhow::Res
133133
Ok(block.result.block)
134134
}
135135

136+
pub async fn get_header(client: &TendermintRPCClient, height: u64) -> anyhow::Result<Header> {
137+
let block = client.fetch_header_by_height(height).await?;
138+
Ok(block.result.header)
139+
}
140+
136141
/// Retrieves the headers for the given range of block heights. Inclusive of start and end.
137142
pub async fn get_headers_in_range(
138143
client: &TendermintRPCClient,
@@ -165,7 +170,7 @@ pub async fn get_headers_in_range(
165170

166171
// Chunk the range into batches of DEFAULT_TENDERMINT_RPC_CONCURRENCY.
167172
let batch_headers: Vec<anyhow::Result<Header>> = (next_batch_start..batch_end)
168-
.map(|height| async move { Ok(get_block(client, height).await?.header) })
173+
.map(|height| async move { get_header(client, height).await })
169174
.collect::<FuturesOrdered<_>>()
170175
.collect::<Vec<_>>()
171176
.await;
@@ -179,7 +184,7 @@ pub async fn get_headers_in_range(
179184
failures += 1;
180185
}
181186

182-
tracing::debug!(
187+
tracing::warn!(
183188
"Got errors fetching headers, successful header count: {}",
184189
err
185190
);

0 commit comments

Comments
 (0)