Skip to content

Commit

Permalink
fix: improve error message when trusted indexer fails to give timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Oct 7, 2024
1 parent 702b9bb commit a3e259c
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/network/subgraph_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use custom_debug::CustomDebug;
use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};
use serde_json::json;
use serde_with::serde_as;
use thegraph_core::{BlockHash, BlockNumber};
use thegraph_core::{BlockHash, BlockNumber, BlockTimestamp};
use thegraph_graphql_http::http::response::Error as GqlError;
use types::Subgraph;
use url::Url;
Expand Down Expand Up @@ -202,7 +202,13 @@ impl Client {
}
#[derive(Debug, Deserialize)]
pub struct Meta {
block: Block,
block: PartialBlock,
}
#[derive(Debug, Deserialize)]
pub struct PartialBlock {
pub number: BlockNumber,
pub hash: BlockHash,
pub timestamp: Option<BlockTimestamp>,
}

debug_assert!(self.page_size > 0);
Expand Down Expand Up @@ -246,19 +252,28 @@ impl Client {
let mut data = response
.data
.ok_or_else(|| anyhow!("response missing data"))?;
if let Some(block) = &query_block {
ensure!(block == &data.meta.block);
let block = Block {
number: data.meta.block.number,
hash: data.meta.block.hash,
timestamp: data
.meta
.block
.timestamp
.ok_or_else(|| anyhow!("response missing block timestamp"))?,
};
if let Some(query_block) = &query_block {
ensure!(query_block == &block);
} else {
ensure!(
data.meta.block.number
>= self.latest_block.as_ref().map(|b| b.number).unwrap_or(0),
"response block before latest",
);
ensure!(
(unix_timestamp() / 1_000).saturating_sub(data.meta.block.timestamp) < 120,
(unix_timestamp() / 1_000).saturating_sub(block.timestamp) < 120,
"response too far behind",
);
query_block = Some(data.meta.block);
query_block = Some(block);
}
last_id = data.results.last().map(|entry| entry.id.to_string());
let page_len = data.results.len();
Expand Down

0 comments on commit a3e259c

Please sign in to comment.