Skip to content

Commit a313261

Browse files
author
Zoran Cvetkov
committed
make reorg_threshold private so that it can be modified for tests
1 parent 51023d2 commit a313261

File tree

11 files changed

+35
-16
lines changed

11 files changed

+35
-16
lines changed

chain/ethereum/src/chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ impl Blockchain for Chain {
610610
// present in the DB.
611611
Box::new(PollingBlockIngestor::new(
612612
logger,
613-
graph::env::ENV_VARS.reorg_threshold,
613+
graph::env::ENV_VARS.reorg_threshold(),
614614
self.chain_client(),
615615
self.chain_store().cheap_clone(),
616616
self.polling_ingestor_interval,

graph/src/data/subgraph/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,9 @@ impl Graft {
504504
// The graft point must be at least `reorg_threshold` blocks
505505
// behind the subgraph head so that a reorg can not affect the
506506
// data that we copy for grafting
507-
(Some(ptr), true) if self.block + ENV_VARS.reorg_threshold > ptr.number => Err(GraftBaseInvalid(format!(
507+
(Some(ptr), true) if self.block + ENV_VARS.reorg_threshold() > ptr.number => Err(GraftBaseInvalid(format!(
508508
"failed to graft onto `{}` at block {} since it's only at block {} which is within the reorg threshold of {} blocks",
509-
self.base, self.block, ptr.number, ENV_VARS.reorg_threshold
509+
self.base, self.block, ptr.number, ENV_VARS.reorg_threshold()
510510
))),
511511
// If the base deployment is failed *and* the `graft.block` is not
512512
// less than the `base.block`, the graft shouldn't be permitted.
@@ -696,36 +696,43 @@ impl<C: Blockchain> UnvalidatedSubgraphManifest<C> {
696696
validate_graft_base: bool,
697697
) -> Result<SubgraphManifest<C>, Vec<SubgraphManifestValidationError>> {
698698
let mut errors: Vec<SubgraphManifestValidationError> = vec![];
699+
println!("validate 1");
699700

700701
// Validate that the manifest has at least one data source
701702
if self.0.data_sources.is_empty() {
702703
errors.push(SubgraphManifestValidationError::NoDataSources);
703704
}
705+
println!("validate 2");
704706

705707
for ds in &self.0.data_sources {
706708
errors.extend(ds.validate(&self.0.spec_version).into_iter().map(|e| {
707709
SubgraphManifestValidationError::DataSourceValidation(ds.name().to_owned(), e)
708710
}));
709711
}
712+
println!("validate 3");
710713

711714
// For API versions newer than 0.0.5, validate that all mappings uses the same api_version
712715
if let Err(different_api_versions) = self.0.unified_mapping_api_version() {
713716
errors.push(different_api_versions.into());
714717
};
718+
println!("validate 4 errors: {:?}", errors);
715719

716720
let mut networks = self
717721
.0
718722
.data_sources
719723
.iter()
720724
.filter_map(|d| Some(d.network()?.to_string()))
721725
.collect::<Vec<String>>();
726+
println!("validate 5");
722727
networks.sort();
723728
networks.dedup();
729+
println!("validate 6");
724730
match networks.len() {
725731
0 => errors.push(SubgraphManifestValidationError::EthereumNetworkRequired),
726732
1 => (),
727733
_ => errors.push(SubgraphManifestValidationError::MultipleEthereumNetworks),
728734
}
735+
println!("validate 7 errors: {:?}", errors);
729736

730737
if let Some(graft) = &self.0.graft {
731738
if validate_graft_base {
@@ -734,13 +741,15 @@ impl<C: Blockchain> UnvalidatedSubgraphManifest<C> {
734741
}
735742
}
736743
}
744+
println!("validate 8 errors: {:?}", errors);
737745

738746
// Validate subgraph feature usage and declaration.
739747
if self.0.spec_version >= SPEC_VERSION_0_0_4 {
740748
if let Err(feature_validation_error) = validate_subgraph_features(&self.0) {
741749
errors.push(feature_validation_error.into())
742750
}
743751
}
752+
println!("validate 9");
744753

745754
match errors.is_empty() {
746755
true => Ok(self.0),

graph/src/env/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod store;
55
use envconfig::Envconfig;
66
use lazy_static::lazy_static;
77
use semver::Version;
8+
use std::sync::Mutex;
89
use std::{collections::HashSet, env::VarError, fmt, str::FromStr, time::Duration};
910

1011
use self::graphql::*;
@@ -17,6 +18,7 @@ use crate::{
1718

1819
lazy_static! {
1920
pub static ref ENV_VARS: EnvVars = EnvVars::from_env().unwrap();
21+
pub static ref TEST_WITH_NO_REORG: Mutex<bool> = Mutex::new(false);
2022
}
2123

2224
/// Panics if:
@@ -181,7 +183,7 @@ pub struct EnvVars {
181183
pub static_filters_threshold: usize,
182184
/// Set by the environment variable `ETHEREUM_REORG_THRESHOLD`. The default
183185
/// value is 250 blocks.
184-
pub reorg_threshold: BlockNumber,
186+
reorg_threshold: BlockNumber,
185187
/// The time to wait between polls when using polling block ingestor.
186188
/// The value is set by `ETHERUM_POLLING_INTERVAL` in millis and the
187189
/// default is 1000.
@@ -362,6 +364,13 @@ impl EnvVars {
362364
.filter(|x| !x.is_empty())
363365
.collect()
364366
}
367+
pub fn reorg_threshold(&self) -> i32 {
368+
if *TEST_WITH_NO_REORG.lock().unwrap() {
369+
0
370+
} else {
371+
self.reorg_threshold
372+
}
373+
}
365374
}
366375

367376
impl Default for EnvVars {

node/src/chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ pub async fn networks_as_chains(
457457
Arc::new(adapter_selector),
458458
Arc::new(EthereumRuntimeAdapterBuilder {}),
459459
eth_adapters,
460-
ENV_VARS.reorg_threshold,
460+
ENV_VARS.reorg_threshold(),
461461
polling_interval,
462462
true,
463463
);

node/src/manager/commands/prune.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ pub async fn run(
188188

189189
println!("prune {deployment}");
190190
println!(" latest: {latest}");
191-
println!(" final: {}", latest - ENV_VARS.reorg_threshold);
191+
println!(" final: {}", latest - ENV_VARS.reorg_threshold());
192192
println!(" earliest: {}\n", latest - history);
193193

194194
let mut req = PruneRequest::new(
195195
&deployment,
196196
history,
197-
ENV_VARS.reorg_threshold,
197+
ENV_VARS.reorg_threshold(),
198198
status.earliest_block_number,
199199
latest,
200200
)?;
@@ -217,7 +217,7 @@ pub async fn run(
217217
store.subgraph_store().set_history_blocks(
218218
&deployment,
219219
history,
220-
ENV_VARS.reorg_threshold,
220+
ENV_VARS.reorg_threshold(),
221221
)?;
222222
}
223223

node/src/manager/commands/rewind.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ pub async fn run(
133133
let deployment_details = deployment_store.deployment_details_for_id(locator)?;
134134
let block_number_to = block_ptr_to.as_ref().map(|b| b.number).unwrap_or(0);
135135

136-
if block_number_to < deployment_details.earliest_block_number + ENV_VARS.reorg_threshold {
136+
if block_number_to < deployment_details.earliest_block_number + ENV_VARS.reorg_threshold() {
137137
bail!(
138138
"The block number {} is not safe to rewind to for deployment {}. The earliest block number of this deployment is {}. You can only safely rewind to block number {}",
139139
block_ptr_to.as_ref().map(|b| b.number).unwrap_or(0),
140140
locator,
141141
deployment_details.earliest_block_number,
142-
deployment_details.earliest_block_number + ENV_VARS.reorg_threshold
142+
deployment_details.earliest_block_number + ENV_VARS.reorg_threshold()
143143
);
144144
}
145145
}

store/postgres/src/block_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl BlockStore {
507507
};
508508

509509
if let Some(head_block) = store.remove_cursor(&&store.chain)? {
510-
let lower_bound = head_block.saturating_sub(ENV_VARS.reorg_threshold * 2);
510+
let lower_bound = head_block.saturating_sub(ENV_VARS.reorg_threshold() * 2);
511511
info!(&self.logger, "Removed cursor for non-firehose chain, now cleaning shallow blocks"; "network" => &store.chain, "lower_bound" => lower_bound);
512512
store.cleanup_shallow_blocks(lower_bound)?;
513513
}

store/postgres/src/deployment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ pub fn revert_block_ptr(
547547
let number = format!("{}::numeric", ptr.number);
548548

549549
// Block numbers can't be negative, so make it >= 0
550-
let earliest_block = i32::max(ptr.number - ENV_VARS.reorg_threshold, 0);
550+
let earliest_block = i32::max(ptr.number - ENV_VARS.reorg_threshold(), 0);
551551
let affected_rows = update(
552552
d::table
553553
.filter(d::deployment.eq(id.as_str()))

store/postgres/src/deployment_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ impl DeploymentStore {
12721272
let req = PruneRequest::new(
12731273
&site.as_ref().into(),
12741274
history_blocks,
1275-
ENV_VARS.reorg_threshold,
1275+
ENV_VARS.reorg_threshold(),
12761276
earliest_block,
12771277
latest_block,
12781278
)?;

tests/src/fixture/ethereum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub async fn chain(
6464
triggers_adapter,
6565
Arc::new(NoopRuntimeAdapterBuilder {}),
6666
eth_adapters,
67-
ENV_VARS.reorg_threshold,
67+
ENV_VARS.reorg_threshold(),
6868
ENV_VARS.ingestor_polling_interval,
6969
// We assume the tested chain is always ingestible for now
7070
true,

0 commit comments

Comments
 (0)