Skip to content

Commit 06d592d

Browse files
committed
node: Add cache_size to chain TOML config
Add a per-chain `cache_size` configuration parameter that controls how many blocks from chain head to keep in the block cache. Defaults to 500. Validated to be greater than reorg_threshold.
1 parent d13f710 commit 06d592d

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

graph/src/components/store/traits.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,11 @@ pub trait ChainIdStore: Send + Sync + 'static {
525525
) -> Result<(), Error>;
526526
}
527527

528+
/// The default size for the block cache, i.e., how many blocks behind
529+
/// the chain head we should keep in the database cache. The
530+
/// configuration can change this for individual chains
531+
pub const BLOCK_CACHE_SIZE: BlockNumber = 500;
532+
528533
/// Common trait for blockchain store implementations.
529534
#[async_trait]
530535
pub trait ChainStore: ChainHeadStore {

node/src/config.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use graph::{
22
anyhow::Error,
33
blockchain::BlockchainKind,
4-
components::network_provider::{AmpChainNames, ChainName},
4+
components::{
5+
network_provider::{AmpChainNames, ChainName},
6+
store::BLOCK_CACHE_SIZE,
7+
},
58
env::ENV_VARS,
69
firehose::{SubgraphLimit, SUBGRAPHS_PER_CONN},
710
itertools::Itertools,
@@ -462,8 +465,17 @@ impl ChainSection {
462465
fn validate(&mut self) -> Result<()> {
463466
NodeId::new(&self.ingestor)
464467
.map_err(|node| anyhow!("invalid node id for ingestor {}", node))?;
465-
for (_, chain) in self.chains.iter_mut() {
466-
chain.validate()?
468+
let reorg_threshold = ENV_VARS.reorg_threshold();
469+
for (name, chain) in self.chains.iter_mut() {
470+
chain.validate()?;
471+
if chain.cache_size <= reorg_threshold {
472+
return Err(anyhow!(
473+
"chain '{}': cache_size ({}) must be greater than reorg_threshold ({})",
474+
name,
475+
chain.cache_size,
476+
reorg_threshold
477+
));
478+
}
467479
}
468480

469481
// Validate that effective AMP names are unique and don't collide
@@ -587,6 +599,7 @@ impl ChainSection {
587599
polling_interval: default_polling_interval(),
588600
providers: vec![],
589601
amp: None,
602+
cache_size: default_cache_size(),
590603
});
591604
entry.providers.push(provider);
592605
}
@@ -611,6 +624,15 @@ pub struct Chain {
611624
/// resolve to this chain. Defaults to the chain name.
612625
#[serde(default)]
613626
pub amp: Option<String>,
627+
/// Number of blocks from chain head for which to keep block data
628+
/// cached. When `GRAPH_STORE_IGNORE_BLOCK_CACHE` is set, blocks
629+
/// older than this are treated as if they have no data.
630+
#[serde(default = "default_cache_size")]
631+
pub cache_size: i32,
632+
}
633+
634+
fn default_cache_size() -> i32 {
635+
BLOCK_CACHE_SIZE
614636
}
615637

616638
fn default_blockchain_kind() -> BlockchainKind {
@@ -1297,7 +1319,7 @@ where
12971319
#[cfg(test)]
12981320
mod tests {
12991321

1300-
use crate::config::{default_polling_interval, ChainSection, Web3Rule};
1322+
use crate::config::{default_cache_size, default_polling_interval, ChainSection, Web3Rule};
13011323

13021324
use super::{
13031325
Chain, Config, FirehoseProvider, Provider, ProviderDetails, Shard, Transport, Web3Provider,
@@ -1345,6 +1367,7 @@ mod tests {
13451367
polling_interval: default_polling_interval(),
13461368
providers: vec![],
13471369
amp: None,
1370+
cache_size: default_cache_size(),
13481371
},
13491372
actual
13501373
);
@@ -1368,6 +1391,7 @@ mod tests {
13681391
polling_interval: default_polling_interval(),
13691392
providers: vec![],
13701393
amp: None,
1394+
cache_size: default_cache_size(),
13711395
},
13721396
actual
13731397
);

0 commit comments

Comments
 (0)