Skip to content

Commit 044a125

Browse files
committed
get timestamp from pointers table
1 parent 727b845 commit 044a125

File tree

9 files changed

+30
-19
lines changed

9 files changed

+30
-19
lines changed

chain/ethereum/src/chain.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ impl Chain {
400400
pub async fn block_number(
401401
&self,
402402
hash: &BlockHash,
403-
) -> Result<Option<(String, BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError> {
403+
) -> Result<Option<(String, BlockNumber, Option<BlockTime>, Option<BlockHash>)>, StoreError>
404+
{
404405
self.chain_store.block_number(hash).await
405406
}
406407

graph/src/blockchain/mock.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ impl ChainStore for MockChainStore {
549549
async fn block_number(
550550
&self,
551551
_hash: &BlockHash,
552-
) -> Result<Option<(String, BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError> {
552+
) -> Result<Option<(String, BlockNumber, Option<BlockTime>, Option<BlockHash>)>, StoreError>
553+
{
553554
unimplemented!()
554555
}
555556
async fn block_numbers(

graph/src/components/store/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ pub trait ChainStore: ChainHeadStore {
579579
async fn block_number(
580580
&self,
581581
hash: &BlockHash,
582-
) -> Result<Option<(String, BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError>;
582+
) -> Result<Option<(String, BlockNumber, Option<BlockTime>, Option<BlockHash>)>, StoreError>;
583583

584584
/// Do the same lookup as `block_number`, but in bulk
585585
async fn block_numbers(
@@ -668,7 +668,7 @@ pub trait QueryStore: Send + Sync {
668668
async fn block_number_with_timestamp_and_parent_hash(
669669
&self,
670670
block_hash: &BlockHash,
671-
) -> Result<Option<(BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError>;
671+
) -> Result<Option<(BlockNumber, Option<BlockTime>, Option<BlockHash>)>, StoreError>;
672672

673673
fn wait_stats(&self) -> PoolWaitStats;
674674

graphql/src/runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ where
115115
.await
116116
.ok()
117117
.flatten()
118-
.and_then(|(_, t, _)| t),
118+
.and_then(|(_, t, _)| t.map(|ts| ts.as_secs_since_epoch() as u64)),
119119
hash: block.hash,
120120
number: block.number,
121121
}),

graphql/src/store/resolver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl StoreResolver {
219219
.unwrap_or(r::Value::Null);
220220

221221
let timestamp = timestamp
222-
.map(|ts| r::Value::Int(ts as i64))
222+
.map(|ts| r::Value::Int(ts.as_secs_since_epoch()))
223223
.unwrap_or(r::Value::Null);
224224

225225
let parent_hash = parent_hash

server/index-node/src/resolver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ impl<S: Store> IndexNodeResolver<S> {
328328
block: object! {
329329
hash: cached_call.block_ptr.hash.hash_hex(),
330330
number: cached_call.block_ptr.number,
331-
timestamp: timestamp,
331+
timestamp: timestamp.map(|ts| ts.as_secs_since_epoch() as u64),
332332
},
333333
contractAddress: &cached_call.contract_address[..],
334334
returnValue: &cached_call.return_value[..],

store/postgres/src/chain_store.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use diesel::prelude::*;
44
use diesel::r2d2::{ConnectionManager, PooledConnection};
55
use diesel::sql_types::Text;
66
use diesel::{insert_into, update};
7+
use graph::blockchain::BlockTime;
78
use graph::components::store::ChainHeadStore;
89
use graph::data::store::ethereum::call;
910
use graph::derive::CheapClone;
@@ -97,7 +98,7 @@ mod data {
9798
sql_types::{BigInt, Bytea, Integer, Jsonb},
9899
update,
99100
};
100-
use graph::blockchain::{Block, BlockHash};
101+
use graph::blockchain::{Block, BlockHash, BlockTime};
101102
use graph::data::store::scalar::Bytes;
102103
use graph::internal_error;
103104
use graph::prelude::ethabi::ethereum_types::H160;
@@ -225,8 +226,8 @@ mod data {
225226
self.table.column::<Bytea, _>("parent_hash")
226227
}
227228

228-
fn timestamp(&self) -> DynColumn<i64> {
229-
self.table.column::<i64, _>("timestamp")
229+
fn timestamp(&self) -> DynColumn<Timestamptz> {
230+
self.table.column::<Timestamptz, _>("timestamp")
230231
}
231232
}
232233

@@ -894,7 +895,8 @@ mod data {
894895
&self,
895896
conn: &mut PgConnection,
896897
hash: &BlockHash,
897-
) -> Result<Option<(BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError> {
898+
) -> Result<Option<(BlockNumber, Option<BlockTime>, Option<BlockHash>)>, StoreError>
899+
{
898900
const TIMESTAMP_QUERY: &str =
899901
"coalesce(data->'block'->>'timestamp', data->>'timestamp')";
900902

@@ -912,6 +914,10 @@ mod data {
912914
.first::<(i64, Option<String>, Option<String>)>(conn)
913915
.optional()?
914916
.map(|(number, ts, parent_hash)| {
917+
let ts = crate::chain_store::try_parse_timestamp(ts)
918+
.unwrap_or_default()
919+
.map(|ts| BlockTime::since_epoch(ts as i64, 0));
920+
915921
// Convert parent_hash from Hex String to Vec<u8>
916922
let parent_hash_bytes = parent_hash
917923
.map(|h| hex::decode(&h).expect("Invalid hex in parent_hash"));
@@ -922,13 +928,13 @@ mod data {
922928
.table()
923929
.select((
924930
block_pointers.number(),
925-
sql::<Nullable<Text>>(TIMESTAMP_QUERY),
931+
block_pointers.timestamp(),
926932
block_pointers.parent_hash(),
927933
))
928934
.filter(block_pointers.hash().eq(hash.as_slice()))
929-
.first::<(i64, Option<String>, Vec<u8>)>(conn)
935+
.first::<(i64, BlockTime, Vec<u8>)>(conn)
930936
.optional()?
931-
.map(|(number, ts, parent_hash)| (number, ts, Some(parent_hash))),
937+
.map(|(number, ts, parent_hash)| (number, Some(ts), Some(parent_hash))),
932938
};
933939

934940
match number {
@@ -938,7 +944,8 @@ mod data {
938944
.map_err(|e| StoreError::QueryExecutionError(e.to_string()))?;
939945
Ok(Some((
940946
number,
941-
crate::chain_store::try_parse_timestamp(ts)?,
947+
ts,
948+
// crate::chain_store::try_parse_timestamp(ts)?,
942949
parent_hash.map(|h| BlockHash::from(h)),
943950
)))
944951
}
@@ -2609,7 +2616,8 @@ impl ChainStoreTrait for ChainStore {
26092616
async fn block_number(
26102617
&self,
26112618
hash: &BlockHash,
2612-
) -> Result<Option<(String, BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError> {
2619+
) -> Result<Option<(String, BlockNumber, Option<BlockTime>, Option<BlockHash>)>, StoreError>
2620+
{
26132621
let hash = hash.clone();
26142622
let storage = self.storage.clone();
26152623
let chain = self.chain.clone();

store/postgres/src/query_store.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::HashMap;
22
use std::time::Instant;
33

44
use crate::deployment_store::{DeploymentStore, ReplicaId};
5+
use graph::blockchain::BlockTime;
56
use graph::components::store::{DeploymentId, QueryPermit, QueryStore as QueryStoreTrait};
67
use graph::data::query::Trace;
78
use graph::data::store::QueryObject;
@@ -72,7 +73,7 @@ impl QueryStoreTrait for QueryStore {
7273
async fn block_number_with_timestamp_and_parent_hash(
7374
&self,
7475
block_hash: &BlockHash,
75-
) -> Result<Option<(BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError> {
76+
) -> Result<Option<(BlockNumber, Option<BlockTime>, Option<BlockHash>)>, StoreError> {
7677
// We should also really check that the block with the given hash is
7778
// on the chain starting at the subgraph's current head. That check is
7879
// very expensive though with the data structures we have currently

store/test-store/tests/postgres/store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ fn parse_timestamp() {
16981698
.expect("block_number to return correct number and timestamp")
16991699
.unwrap();
17001700
assert_eq!(number, 3);
1701-
assert_eq!(timestamp.unwrap(), EXPECTED_TS);
1701+
assert_eq!(timestamp.unwrap().as_secs_since_epoch() as u64, EXPECTED_TS);
17021702
})
17031703
}
17041704

@@ -1732,7 +1732,7 @@ fn parse_timestamp_firehose() {
17321732
.expect("block_number to return correct number and timestamp")
17331733
.unwrap();
17341734
assert_eq!(number, 3);
1735-
assert_eq!(timestamp.unwrap(), EXPECTED_TS);
1735+
assert_eq!(timestamp.unwrap().as_secs_since_epoch() as u64, EXPECTED_TS);
17361736
})
17371737
}
17381738

0 commit comments

Comments
 (0)