Skip to content

Commit 3f13e45

Browse files
committed
Revert PR 6037
This reverts PR #6037 (commit 94fb3f2) for now. We'll want to understand more about how the block cache is used before rolling this out since it involves a db migration
1 parent 7765b77 commit 3f13e45

File tree

18 files changed

+168
-554
lines changed

18 files changed

+168
-554
lines changed

chain/ethereum/src/chain.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,8 @@ impl Chain {
400400
pub async fn block_number(
401401
&self,
402402
hash: &BlockHash,
403-
) -> Result<Option<(String, BlockNumber, Option<BlockTime>, Option<BlockHash>)>, StoreError>
404-
{
405-
self.chain_store.block_pointer(hash).await
403+
) -> Result<Option<(String, BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError> {
404+
self.chain_store.block_number(hash).await
406405
}
407406

408407
// TODO: This is only used to build the block stream which could prolly
@@ -1131,9 +1130,6 @@ pub struct FirehoseMapper {
11311130
impl BlockStreamMapper<Chain> for FirehoseMapper {
11321131
fn decode_block(
11331132
&self,
1134-
// We share the trait with substreams but for firehose the timestamp
1135-
// is in the block header so we don't need to use it here.
1136-
_timestamp: BlockTime,
11371133
output: Option<&[u8]>,
11381134
) -> Result<Option<BlockFinality>, BlockStreamError> {
11391135
let block = match output {
@@ -1202,19 +1198,12 @@ impl FirehoseMapperTrait<Chain> for FirehoseMapper {
12021198
// Check about adding basic information about the block in the firehose::Response or maybe
12031199
// define a slimmed down stuct that would decode only a few fields and ignore all the rest.
12041200
let block = codec::Block::decode(any_block.value.as_ref())?;
1205-
let timestamp = block
1206-
.header()
1207-
.timestamp
1208-
.map(|ts| BlockTime::since_epoch(ts.seconds, ts.nanos as u32))
1209-
.unwrap_or_default();
12101201

12111202
use firehose::ForkStep::*;
12121203
match step {
12131204
StepNew => {
12141205
// unwrap: Input cannot be None so output will be error or block.
1215-
let block = self
1216-
.decode_block(timestamp, Some(any_block.value.as_ref()))?
1217-
.unwrap();
1206+
let block = self.decode_block(Some(any_block.value.as_ref()))?.unwrap();
12181207
let block_with_triggers = self.block_with_triggers(logger, block).await?;
12191208

12201209
Ok(BlockStreamEvent::ProcessBlock(

chain/near/src/chain.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use graph::blockchain::client::ChainClient;
33
use graph::blockchain::firehose_block_ingestor::FirehoseBlockIngestor;
44
use graph::blockchain::substreams_block_stream::SubstreamsBlockStream;
55
use graph::blockchain::{
6-
BasicBlockchainBuilder, BlockIngestor, BlockTime, BlockchainBuilder, BlockchainKind,
7-
NoopDecoderHook, NoopRuntimeAdapter, Trigger, TriggerFilterWrapper,
6+
BasicBlockchainBuilder, BlockIngestor, BlockchainBuilder, BlockchainKind, NoopDecoderHook,
7+
NoopRuntimeAdapter, Trigger, TriggerFilterWrapper,
88
};
99
use graph::cheap_clone::CheapClone;
1010
use graph::components::network_provider::ChainName;
@@ -432,7 +432,6 @@ pub struct FirehoseMapper {
432432
impl BlockStreamMapper<Chain> for FirehoseMapper {
433433
fn decode_block(
434434
&self,
435-
_timestamp: BlockTime,
436435
output: Option<&[u8]>,
437436
) -> Result<Option<codec::Block>, BlockStreamError> {
438437
let block = match output {
@@ -529,10 +528,7 @@ impl FirehoseMapperTrait<Chain> for FirehoseMapper {
529528
// Check about adding basic information about the block in the bstream::BlockResponseV2 or maybe
530529
// define a slimmed down stuct that would decode only a few fields and ignore all the rest.
531530
// unwrap: Input cannot be None so output will be error or block.
532-
let block = self
533-
// the block time is inside the block.
534-
.decode_block(BlockTime::MIN, Some(any_block.value.as_ref()))?
535-
.unwrap();
531+
let block = self.decode_block(Some(any_block.value.as_ref()))?.unwrap();
536532

537533
use ForkStep::*;
538534
match step {

chain/substreams/src/chain.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ pub enum ParsedChanges {
4343
pub struct Block {
4444
pub hash: BlockHash,
4545
pub number: BlockNumber,
46-
pub timestamp: BlockTime,
4746
pub changes: EntityChanges,
4847
pub parsed_changes: Vec<ParsedChanges>,
4948
}
@@ -61,7 +60,7 @@ impl blockchain::Block for Block {
6160
}
6261

6362
fn timestamp(&self) -> BlockTime {
64-
self.timestamp
63+
BlockTime::NONE
6564
}
6665
}
6766

chain/substreams/src/mapper.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub struct WasmBlockMapper {
3232
impl BlockStreamMapper<Chain> for WasmBlockMapper {
3333
fn decode_block(
3434
&self,
35-
_timestamp: BlockTime,
3635
_output: Option<&[u8]>,
3736
) -> Result<Option<crate::Block>, BlockStreamError> {
3837
unreachable!("WasmBlockMapper does not do block decoding")
@@ -105,11 +104,7 @@ pub struct Mapper {
105104

106105
#[async_trait]
107106
impl BlockStreamMapper<Chain> for Mapper {
108-
fn decode_block(
109-
&self,
110-
timestamp: BlockTime,
111-
output: Option<&[u8]>,
112-
) -> Result<Option<Block>, BlockStreamError> {
107+
fn decode_block(&self, output: Option<&[u8]>) -> Result<Option<Block>, BlockStreamError> {
113108
let changes: EntityChanges = match output {
114109
Some(msg) => Message::decode(msg).map_err(SubstreamsError::DecodingError)?,
115110
None => EntityChanges {
@@ -130,7 +125,6 @@ impl BlockStreamMapper<Chain> for Mapper {
130125
number,
131126
changes,
132127
parsed_changes,
133-
timestamp,
134128
};
135129

136130
Ok(Some(block))
@@ -158,13 +152,9 @@ impl BlockStreamMapper<Chain> for Mapper {
158152
) -> Result<BlockStreamEvent<Chain>, BlockStreamError> {
159153
let block_number: BlockNumber = clock.number.try_into().map_err(Error::from)?;
160154
let block_hash = clock.id.as_bytes().to_vec().into();
161-
let timestamp = clock
162-
.timestamp
163-
.map(|ts| BlockTime::since_epoch(ts.seconds, ts.nanos as u32))
164-
.unwrap_or_default();
165155

166156
let block = self
167-
.decode_block(timestamp, Some(&block))?
157+
.decode_block(Some(&block))?
168158
.ok_or_else(|| anyhow!("expected block to not be empty"))?;
169159

170160
let block = self.block_with_triggers(logger, block).await.map(|bt| {

graph/src/blockchain/block_stream.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,7 @@ pub trait FirehoseMapper<C: Blockchain>: Send + Sync {
685685

686686
#[async_trait]
687687
pub trait BlockStreamMapper<C: Blockchain>: Send + Sync {
688-
fn decode_block(
689-
&self,
690-
timestamp: BlockTime,
691-
output: Option<&[u8]>,
692-
) -> Result<Option<C::Block>, BlockStreamError>;
688+
fn decode_block(&self, output: Option<&[u8]>) -> Result<Option<C::Block>, BlockStreamError>;
693689

694690
async fn block_with_triggers(
695691
&self,

graph/src/blockchain/mock.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,10 @@ impl ChainStore for MockChainStore {
546546
fn confirm_block_hash(&self, _number: BlockNumber, _hash: &BlockHash) -> Result<usize, Error> {
547547
unimplemented!()
548548
}
549-
async fn block_pointer(
549+
async fn block_number(
550550
&self,
551551
_hash: &BlockHash,
552-
) -> Result<Option<(String, BlockNumber, Option<BlockTime>, Option<BlockHash>)>, StoreError>
553-
{
552+
) -> Result<Option<(String, BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError> {
554553
unimplemented!()
555554
}
556555
async fn block_numbers(

graph/src/blockchain/types.rs

Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use anyhow::anyhow;
2-
use chrono::DateTime;
32
use diesel::deserialize::FromSql;
43
use diesel::pg::Pg;
54
use diesel::serialize::{Output, ToSql};
@@ -8,7 +7,6 @@ use diesel::sql_types::{Bytea, Nullable, Text};
87
use diesel_derives::{AsExpression, FromSqlRow};
98
use serde::{Deserialize, Deserializer};
109
use std::convert::TryFrom;
11-
use std::num::ParseIntError;
1210
use std::time::Duration;
1311
use std::{fmt, str::FromStr};
1412
use web3::types::{Block, H256, U256, U64};
@@ -18,9 +16,9 @@ use crate::components::store::BlockNumber;
1816
use crate::data::graphql::IntoValue;
1917
use crate::data::store::scalar::Timestamp;
2018
use crate::derive::CheapClone;
19+
use crate::object;
2120
use crate::prelude::{r, Value};
2221
use crate::util::stable_hash_glue::{impl_stable_hash, AsBytes};
23-
use crate::{bail, object};
2422

2523
/// A simple marker for byte arrays that are really block hashes
2624
#[derive(Clone, Default, PartialEq, Eq, Hash, FromSqlRow, AsExpression)]
@@ -479,7 +477,10 @@ impl TryFrom<(Option<H256>, Option<U64>, H256, U256)> for ExtendedBlockPtr {
479477
let block_number =
480478
i32::try_from(number).map_err(|_| anyhow!("Block number out of range"))?;
481479

482-
let block_time = BlockTime::try_from(timestamp_u256)?;
480+
// Convert `U256` to `BlockTime`
481+
let secs =
482+
i64::try_from(timestamp_u256).map_err(|_| anyhow!("Timestamp out of range for i64"))?;
483+
let block_time = BlockTime::since_epoch(secs, 0);
483484

484485
Ok(ExtendedBlockPtr {
485486
hash: hash.into(),
@@ -496,13 +497,16 @@ impl TryFrom<(H256, i32, H256, U256)> for ExtendedBlockPtr {
496497
fn try_from(tuple: (H256, i32, H256, U256)) -> Result<Self, Self::Error> {
497498
let (hash, block_number, parent_hash, timestamp_u256) = tuple;
498499

499-
let timestamp = BlockTime::try_from(timestamp_u256)?;
500+
// Convert `U256` to `BlockTime`
501+
let secs =
502+
i64::try_from(timestamp_u256).map_err(|_| anyhow!("Timestamp out of range for i64"))?;
503+
let block_time = BlockTime::since_epoch(secs, 0);
500504

501505
Ok(ExtendedBlockPtr {
502506
hash: hash.into(),
503507
number: block_number,
504508
parent_hash: parent_hash.into(),
505-
timestamp,
509+
timestamp: block_time,
506510
})
507511
}
508512
}
@@ -558,63 +562,14 @@ impl fmt::Display for ChainIdentifier {
558562
#[diesel(sql_type = Timestamptz)]
559563
pub struct BlockTime(Timestamp);
560564

561-
impl Default for BlockTime {
562-
fn default() -> Self {
563-
BlockTime::NONE
564-
}
565-
}
566-
567-
impl TryFrom<BlockTime> for U256 {
568-
type Error = anyhow::Error;
569-
570-
fn try_from(value: BlockTime) -> Result<Self, Self::Error> {
571-
if value.as_secs_since_epoch() < 0 {
572-
bail!("unable to convert block time into U256");
573-
}
574-
575-
Ok(U256::from(value.as_secs_since_epoch() as u64))
576-
}
577-
}
578-
579-
impl TryFrom<U256> for BlockTime {
580-
type Error = anyhow::Error;
581-
582-
fn try_from(value: U256) -> Result<Self, Self::Error> {
583-
i64::try_from(value)
584-
.map_err(|_| anyhow!("Timestamp out of range for i64"))
585-
.map(|ts| BlockTime::since_epoch(ts, 0))
586-
}
587-
}
588-
589-
impl TryFrom<Option<String>> for BlockTime {
590-
type Error = ParseIntError;
591-
592-
fn try_from(ts: Option<String>) -> Result<Self, Self::Error> {
593-
match ts {
594-
Some(str) => return BlockTime::from_hex_str(&str),
595-
None => return Ok(BlockTime::NONE),
596-
};
597-
}
598-
}
599-
600565
impl BlockTime {
601566
/// A timestamp from a long long time ago used to indicate that we don't
602567
/// have a timestamp
603-
pub const NONE: Self = Self::MIN;
568+
pub const NONE: Self = Self(Timestamp::NONE);
604569

605570
pub const MAX: Self = Self(Timestamp::MAX);
606571

607-
pub const MIN: Self = Self(Timestamp(DateTime::from_timestamp_nanos(0)));
608-
609-
pub fn from_hex_str(ts: &str) -> Result<Self, ParseIntError> {
610-
let (radix, idx) = if ts.starts_with("0x") {
611-
(16, 2)
612-
} else {
613-
(10, 0)
614-
};
615-
616-
u64::from_str_radix(&ts[idx..], radix).map(|ts| BlockTime::since_epoch(ts as i64, 0))
617-
}
572+
pub const MIN: Self = Self(Timestamp::MIN);
618573

619574
/// Construct a block time that is the given number of seconds and
620575
/// nanoseconds after the Unix epoch
@@ -631,12 +586,7 @@ impl BlockTime {
631586
/// hourly rollups in tests
632587
#[cfg(debug_assertions)]
633588
pub fn for_test(ptr: &BlockPtr) -> Self {
634-
Self::for_test_number(&ptr.number)
635-
}
636-
637-
#[cfg(debug_assertions)]
638-
pub fn for_test_number(number: &BlockNumber) -> Self {
639-
Self::since_epoch(*number as i64 * 45 * 60, 0)
589+
Self::since_epoch(ptr.number as i64 * 45 * 60, 0)
640590
}
641591

642592
pub fn as_secs_since_epoch(&self) -> i64 {

graph/src/components/store/traits.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,10 @@ pub trait ChainStore: ChainHeadStore {
576576
/// Currently, the timestamp is only returned if it's present in the top level block. This format is
577577
/// depends on the chain and the implementation of Blockchain::Block for the specific chain.
578578
/// eg: {"block": { "timestamp": 123123123 } }
579-
async fn block_pointer(
579+
async fn block_number(
580580
&self,
581581
hash: &BlockHash,
582-
) -> Result<Option<(String, BlockNumber, Option<BlockTime>, Option<BlockHash>)>, StoreError>;
582+
) -> Result<Option<(String, BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError>;
583583

584584
/// Do the same lookup as `block_number`, but in bulk
585585
async fn block_numbers(
@@ -665,10 +665,10 @@ pub trait QueryStore: Send + Sync {
665665
/// Returns the blocknumber, timestamp and the parentHash. Timestamp depends on the chain block type
666666
/// and can have multiple formats, it can also not be prevent. For now this is only available
667667
/// for EVM chains both firehose and rpc.
668-
async fn block_pointer(
668+
async fn block_number_with_timestamp_and_parent_hash(
669669
&self,
670670
block_hash: &BlockHash,
671-
) -> Result<Option<(BlockNumber, Option<BlockTime>, Option<BlockHash>)>, StoreError>;
671+
) -> Result<Option<(BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError>;
672672

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

graphql/src/runner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ where
111111
let latest_block = match store.block_ptr().await.ok().flatten() {
112112
Some(block) => Some(LatestBlockInfo {
113113
timestamp: store
114-
.block_pointer(&block.hash)
114+
.block_number_with_timestamp_and_parent_hash(&block.hash)
115115
.await
116116
.ok()
117117
.flatten()
118-
.and_then(|(_, t, _)| t.map(|ts| ts.as_secs_since_epoch() as u64)),
118+
.and_then(|(_, t, _)| t),
119119
hash: block.hash,
120120
number: block.number,
121121
}),

graphql/src/store/resolver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl StoreResolver {
186186
let (timestamp, parent_hash) = if lookup_needed(field) {
187187
match self
188188
.store
189-
.block_pointer(&block_ptr.hash)
189+
.block_number_with_timestamp_and_parent_hash(&block_ptr.hash)
190190
.await
191191
.map_err(Into::<QueryExecutionError>::into)?
192192
{
@@ -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_secs_since_epoch()))
222+
.map(|ts| r::Value::Int(ts as i64))
223223
.unwrap_or(r::Value::Null);
224224

225225
let parent_hash = parent_hash

justfile

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)