Skip to content

Commit ddc9eac

Browse files
author
Zoran Cvetkov
committed
test: fix reorg threshold
1 parent 63b2a5c commit ddc9eac

File tree

12 files changed

+43
-30
lines changed

12 files changed

+43
-30
lines changed

chain/ethereum/src/chain.rs

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

graph/src/data/subgraph/mod.rs

Lines changed: 2 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.

graph/src/env/mod.rs

Lines changed: 21 additions & 15 deletions
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.
@@ -259,16 +261,6 @@ impl EnvVars {
259261
let mapping_handlers = InnerMappingHandlers::init_from_env()?.into();
260262
let store = InnerStore::init_from_env()?.try_into()?;
261263

262-
// The default reorganization (reorg) threshold is set to 250.
263-
// For testing purposes, we need to set this threshold to 0 because:
264-
// 1. Many tests involve reverting blocks.
265-
// 2. Blocks cannot be reverted below the reorg threshold.
266-
// Therefore, during tests, we want to set the reorg threshold to 0.
267-
let reorg_threshold =
268-
inner
269-
.reorg_threshold
270-
.unwrap_or_else(|| if cfg!(debug_assertions) { 0 } else { 250 });
271-
272264
Ok(Self {
273265
graphql,
274266
mappings: mapping_handlers,
@@ -322,13 +314,15 @@ impl EnvVars {
322314
external_http_base_url: inner.external_http_base_url,
323315
external_ws_base_url: inner.external_ws_base_url,
324316
static_filters_threshold: inner.static_filters_threshold,
325-
reorg_threshold,
317+
reorg_threshold: inner.reorg_threshold,
326318
ingestor_polling_interval: Duration::from_millis(inner.ingestor_polling_interval),
327319
subgraph_settings: inner.subgraph_settings,
328320
prefer_substreams_block_streams: inner.prefer_substreams_block_streams,
329321
enable_dips_metrics: inner.enable_dips_metrics.0,
330322
history_blocks_override: inner.history_blocks_override,
331-
min_history_blocks: inner.min_history_blocks.unwrap_or(2 * reorg_threshold),
323+
min_history_blocks: inner
324+
.min_history_blocks
325+
.unwrap_or(2 * inner.reorg_threshold),
332326
dips_metrics_object_store_url: inner.dips_metrics_object_store_url,
333327
section_map: inner.section_map,
334328
firehose_grpc_max_decode_size_mb: inner.firehose_grpc_max_decode_size_mb,
@@ -375,6 +369,18 @@ impl EnvVars {
375369
.filter(|x| !x.is_empty())
376370
.collect()
377371
}
372+
pub fn reorg_threshold(&self) -> i32 {
373+
// The default reorganization (reorg) threshold is set to 250.
374+
// For testing purposes, we need to set this threshold to 0 because:
375+
// 1. Many tests involve reverting blocks.
376+
// 2. Blocks cannot be reverted below the reorg threshold.
377+
// Therefore, during tests, we want to set the reorg threshold to 0.
378+
if *TEST_WITH_NO_REORG.lock().unwrap() {
379+
0
380+
} else {
381+
self.reorg_threshold
382+
}
383+
}
378384
}
379385

380386
impl Default for EnvVars {
@@ -473,8 +479,8 @@ struct Inner {
473479
#[envconfig(from = "GRAPH_STATIC_FILTERS_THRESHOLD", default = "10000")]
474480
static_filters_threshold: usize,
475481
// JSON-RPC specific.
476-
#[envconfig(from = "ETHEREUM_REORG_THRESHOLD")]
477-
reorg_threshold: Option<BlockNumber>,
482+
#[envconfig(from = "ETHEREUM_REORG_THRESHOLD", default = "250")]
483+
reorg_threshold: BlockNumber,
478484
#[envconfig(from = "ETHEREUM_POLLING_INTERVAL", default = "1000")]
479485
ingestor_polling_interval: u64,
480486
#[envconfig(from = "GRAPH_EXPERIMENTAL_SUBGRAPH_SETTINGS")]

node/src/chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ pub async fn networks_as_chains(
460460
Arc::new(adapter_selector),
461461
Arc::new(EthereumRuntimeAdapterBuilder {}),
462462
eth_adapters,
463-
ENV_VARS.reorg_threshold,
463+
ENV_VARS.reorg_threshold(),
464464
polling_interval,
465465
true,
466466
);

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
@@ -503,7 +503,7 @@ impl BlockStore {
503503
};
504504

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

store/postgres/src/deployment.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,14 @@ pub fn revert_block_ptr(
546546
// Work around a Diesel issue with serializing BigDecimals to numeric
547547
let number = format!("{}::numeric", ptr.number);
548548

549+
// Intention is to revert to a block lower than the reorg threshold, on the other
550+
// hand the earliest we can possibly go is genesys block, so go to genesys even
551+
// if it's within the reorg threshold.
552+
let earliest_block = i32::max(ptr.number - ENV_VARS.reorg_threshold(), 0);
549553
let affected_rows = update(
550554
d::table
551555
.filter(d::deployment.eq(id.as_str()))
552-
.filter(d::earliest_block_number.le(ptr.number - ENV_VARS.reorg_threshold)),
556+
.filter(d::earliest_block_number.le(earliest_block)),
553557
)
554558
.set((
555559
d::latest_ethereum_block_number.eq(sql(&number)),

store/postgres/src/deployment_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ impl DeploymentStore {
12611261
let req = PruneRequest::new(
12621262
&site.as_ref().into(),
12631263
history_blocks,
1264-
ENV_VARS.reorg_threshold,
1264+
ENV_VARS.reorg_threshold(),
12651265
earliest_block,
12661266
latest_block,
12671267
)?;

tests/src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ impl Config {
175175
.stdout(stdout)
176176
.stderr(stderr)
177177
.args(args)
178-
.env("GRAPH_STORE_WRITE_BATCH_DURATION", "5");
178+
.env("GRAPH_STORE_WRITE_BATCH_DURATION", "5")
179+
.env("ETHEREUM_REORG_THRESHOLD", "0");
179180

180181
status!(
181182
"graph-node",

0 commit comments

Comments
 (0)