Skip to content

Commit c051672

Browse files
authored
test: fix reorg threshold
1 parent 17360f5 commit c051672

File tree

12 files changed

+55
-30
lines changed

12 files changed

+55
-30
lines changed

chain/ethereum/src/chain.rs

+1-1
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

+2-2
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

+31-15
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ use crate::{
1515
runtime::gas::CONST_MAX_GAS_PER_HANDLER,
1616
};
1717

18+
#[cfg(debug_assertions)]
19+
use std::sync::Mutex;
20+
1821
lazy_static! {
1922
pub static ref ENV_VARS: EnvVars = EnvVars::from_env().unwrap();
2023
}
24+
#[cfg(debug_assertions)]
25+
lazy_static! {
26+
pub static ref TEST_WITH_NO_REORG: Mutex<bool> = Mutex::new(false);
27+
}
2128

2229
/// Panics if:
2330
/// - The value is not UTF8.
@@ -181,7 +188,7 @@ pub struct EnvVars {
181188
pub static_filters_threshold: usize,
182189
/// Set by the environment variable `ETHEREUM_REORG_THRESHOLD`. The default
183190
/// value is 250 blocks.
184-
pub reorg_threshold: BlockNumber,
191+
reorg_threshold: BlockNumber,
185192
/// The time to wait between polls when using polling block ingestor.
186193
/// The value is set by `ETHERUM_POLLING_INTERVAL` in millis and the
187194
/// default is 1000.
@@ -259,16 +266,6 @@ impl EnvVars {
259266
let mapping_handlers = InnerMappingHandlers::init_from_env()?.into();
260267
let store = InnerStore::init_from_env()?.try_into()?;
261268

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-
272269
Ok(Self {
273270
graphql,
274271
mappings: mapping_handlers,
@@ -322,13 +319,15 @@ impl EnvVars {
322319
external_http_base_url: inner.external_http_base_url,
323320
external_ws_base_url: inner.external_ws_base_url,
324321
static_filters_threshold: inner.static_filters_threshold,
325-
reorg_threshold,
322+
reorg_threshold: inner.reorg_threshold,
326323
ingestor_polling_interval: Duration::from_millis(inner.ingestor_polling_interval),
327324
subgraph_settings: inner.subgraph_settings,
328325
prefer_substreams_block_streams: inner.prefer_substreams_block_streams,
329326
enable_dips_metrics: inner.enable_dips_metrics.0,
330327
history_blocks_override: inner.history_blocks_override,
331-
min_history_blocks: inner.min_history_blocks.unwrap_or(2 * reorg_threshold),
328+
min_history_blocks: inner
329+
.min_history_blocks
330+
.unwrap_or(2 * inner.reorg_threshold),
332331
dips_metrics_object_store_url: inner.dips_metrics_object_store_url,
333332
section_map: inner.section_map,
334333
firehose_grpc_max_decode_size_mb: inner.firehose_grpc_max_decode_size_mb,
@@ -375,6 +374,23 @@ impl EnvVars {
375374
.filter(|x| !x.is_empty())
376375
.collect()
377376
}
377+
#[cfg(debug_assertions)]
378+
pub fn reorg_threshold(&self) -> i32 {
379+
// The default reorganization (reorg) threshold is set to 250.
380+
// For testing purposes, we need to set this threshold to 0 because:
381+
// 1. Many tests involve reverting blocks.
382+
// 2. Blocks cannot be reverted below the reorg threshold.
383+
// Therefore, during tests, we want to set the reorg threshold to 0.
384+
if *TEST_WITH_NO_REORG.lock().unwrap() {
385+
0
386+
} else {
387+
self.reorg_threshold
388+
}
389+
}
390+
#[cfg(not(debug_assertions))]
391+
pub fn reorg_threshold(&self) -> i32 {
392+
self.reorg_threshold
393+
}
378394
}
379395

380396
impl Default for EnvVars {
@@ -473,8 +489,8 @@ struct Inner {
473489
#[envconfig(from = "GRAPH_STATIC_FILTERS_THRESHOLD", default = "10000")]
474490
static_filters_threshold: usize,
475491
// JSON-RPC specific.
476-
#[envconfig(from = "ETHEREUM_REORG_THRESHOLD")]
477-
reorg_threshold: Option<BlockNumber>,
492+
#[envconfig(from = "ETHEREUM_REORG_THRESHOLD", default = "250")]
493+
reorg_threshold: BlockNumber,
478494
#[envconfig(from = "ETHEREUM_POLLING_INTERVAL", default = "1000")]
479495
ingestor_polling_interval: u64,
480496
#[envconfig(from = "GRAPH_EXPERIMENTAL_SUBGRAPH_SETTINGS")]

node/src/chain.rs

+1-1
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

+3-3
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

+2-2
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

+1-1
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

+5-1
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

+1-1
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

+2-1
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",

tests/src/fixture/ethereum.rs

+1-1
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,

tests/tests/runner_tests.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use graph::data::store::scalar::Bytes;
1212
use graph::data::subgraph::schema::{SubgraphError, SubgraphHealth};
1313
use graph::data::value::Word;
1414
use graph::data_source::CausalityRegion;
15-
use graph::env::EnvVars;
15+
use graph::env::{EnvVars, TEST_WITH_NO_REORG};
1616
use graph::ipfs;
1717
use graph::ipfs::test_utils::add_files_to_local_ipfs_node_for_testing;
1818
use graph::object;
@@ -109,6 +109,8 @@ fn assert_eq_ignore_backtrace(err: &SubgraphError, expected: &SubgraphError) {
109109

110110
#[tokio::test]
111111
async fn data_source_revert() -> anyhow::Result<()> {
112+
*TEST_WITH_NO_REORG.lock().unwrap() = true;
113+
112114
let RunnerTestRecipe { stores, test_info } =
113115
RunnerTestRecipe::new("data_source_revert", "data-source-revert").await;
114116

@@ -179,6 +181,8 @@ async fn data_source_revert() -> anyhow::Result<()> {
179181
// since it uses the same deployment id.
180182
data_source_long_revert().await.unwrap();
181183

184+
*TEST_WITH_NO_REORG.lock().unwrap() = false;
185+
182186
Ok(())
183187
}
184188

0 commit comments

Comments
 (0)