Skip to content

Commit

Permalink
make timestamp numeric
Browse files Browse the repository at this point in the history
  • Loading branch information
mangas committed Sep 7, 2022
1 parent 675dcf0 commit ea7fb2b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
4 changes: 2 additions & 2 deletions graph/src/components/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ pub trait ChainStore: Send + Sync + 'static {
async fn block_number(
&self,
hash: &BlockHash,
) -> Result<Option<(String, BlockNumber, Option<String>)>, StoreError>;
) -> Result<Option<(String, BlockNumber, Option<u64>)>, StoreError>;

/// Tries to retrieve all transactions receipts for a given block.
async fn transaction_receipts_in_block(
Expand Down Expand Up @@ -443,7 +443,7 @@ pub trait QueryStore: Send + Sync {
async fn block_number_with_timestamp(
&self,
block_hash: &BlockHash,
) -> Result<Option<(BlockNumber, Option<String>)>, StoreError>;
) -> Result<Option<(BlockNumber, Option<u64>)>, StoreError>;

fn wait_stats(&self) -> Result<PoolWaitStats, StoreError>;

Expand Down
4 changes: 2 additions & 2 deletions graphql/src/schema/meta.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ type _Block_ {
hash: Bytes
"The block number"
number: Int!
"Timestamp of the block if available, format depends on the chain"
timestamp: String
"Integer representation of the timestamp store in blocks for the chain"
timestamp: Int
}

enum _SubgraphErrorPolicy_ {
Expand Down
6 changes: 3 additions & 3 deletions graphql/src/store/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct StoreResolver {
#[derive(Clone, Debug)]
pub(crate) struct BlockPtrTs {
pub ptr: BlockPtr,
pub timestamp: Option<String>,
pub timestamp: Option<u64>,
}

impl From<BlockPtr> for BlockPtrTs {
Expand Down Expand Up @@ -142,7 +142,7 @@ impl StoreResolver {
async fn get_block_ts(
store: &dyn QueryStore,
ptr: &BlockPtr,
) -> Result<Option<String>, QueryExecutionError> {
) -> Result<Option<u64>, QueryExecutionError> {
match store
.block_number_with_timestamp(&ptr.hash)
.await
Expand Down Expand Up @@ -247,7 +247,7 @@ impl StoreResolver {
let timestamp = self.block_ptr.as_ref().map(|ptr| {
ptr.timestamp
.clone()
.map(|ts| r::Value::String(ts))
.map(|ts| r::Value::Int(ts as i64))
.unwrap_or(r::Value::Null)
});

Expand Down
28 changes: 25 additions & 3 deletions store/postgres/src/chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ mod data {
&self,
conn: &PgConnection,
hash: &BlockHash,
) -> Result<Option<(BlockNumber, Option<String>)>, StoreError> {
) -> Result<Option<(BlockNumber, Option<u64>)>, StoreError> {
const TIMESTAMP_QUERY: &str =
"coalesce(data->'block'->>'timestamp', data->>'timestamp')";

Expand All @@ -622,7 +622,7 @@ mod data {
Some((number, ts)) => {
let number = BlockNumber::try_from(number)
.map_err(|e| StoreError::QueryExecutionError(e.to_string()))?;
Ok(Some((number, ts)))
Ok(Some((number, crate::chain_store::try_parse_timestamp(ts)?)))
}
}
}
Expand Down Expand Up @@ -1701,7 +1701,7 @@ impl ChainStoreTrait for ChainStore {
async fn block_number(
&self,
hash: &BlockHash,
) -> Result<Option<(String, BlockNumber, Option<String>)>, StoreError> {
) -> Result<Option<(String, BlockNumber, Option<u64>)>, StoreError> {
let hash = hash.clone();
let storage = self.storage.clone();
let chain = self.chain.clone();
Expand Down Expand Up @@ -1731,6 +1731,28 @@ impl ChainStoreTrait for ChainStore {
}
}

fn try_parse_timestamp(ts: Option<String>) -> Result<Option<u64>, StoreError> {
let ts = match ts {
Some(str) => str,
None => return Ok(None),
};

let (radix, idx) = if ts.starts_with("0x") {
(16, 2)
} else {
(10, 0)
};

u64::from_str_radix(&ts[idx..], radix)
.map_err(|err| {
StoreError::QueryExecutionError(format!(
"unexpected timestamp format {}, err: {}",
ts, err
))
})
.map(Some)
}

impl EthereumCallCache for ChainStore {
fn get_call(
&self,
Expand Down
2 changes: 1 addition & 1 deletion store/postgres/src/query_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl QueryStoreTrait for QueryStore {
async fn block_number_with_timestamp(
&self,
block_hash: &BlockHash,
) -> Result<Option<(BlockNumber, Option<String>)>, StoreError> {
) -> Result<Option<(BlockNumber, Option<u64>)>, StoreError> {
// We should also really check that the block with the given hash is
// on the chain starting at the subgraph's current head. That check is
// very expensive though with the data structures we have currently
Expand Down
2 changes: 1 addition & 1 deletion store/postgres/tests/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,7 @@ fn cleanup_cached_blocks() {
/// checks if retrieving the timestamp from the data blob works.
/// on ethereum, the block has timestamp as U256 so it will always have a value
fn parse_timestamp() {
const EXPECTED_TS: &str = "0x62ceae26";
const EXPECTED_TS: u64 = 1657712166;

run_test(|store, _, _| async move {
use block_store::*;
Expand Down

0 comments on commit ea7fb2b

Please sign in to comment.