Skip to content

Commit ea250b9

Browse files
committed
node, store: Add 'graphman chain ingest' command
1 parent 69c182b commit ea250b9

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

node/src/bin/manager.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use graph::components::network_provider::ChainName;
88
use graph::endpoint::EndpointMetrics;
99
use graph::env::ENV_VARS;
1010
use graph::log::logger_with_levels;
11-
use graph::prelude::{MetricsRegistry, BLOCK_NUMBER_MAX};
11+
use graph::prelude::{BlockNumber, MetricsRegistry, BLOCK_NUMBER_MAX};
1212
use graph::{data::graphql::load_manager::LoadManager, prelude::chrono, prometheus::Registry};
1313
use graph::{
1414
prelude::{
@@ -585,6 +585,19 @@ pub enum ChainCommand {
585585
#[clap(value_parser = clap::builder::NonEmptyStringValueParser::new())]
586586
chain_name: String,
587587
},
588+
589+
/// Ingest a block into the block cache.
590+
///
591+
/// This will overwrite any blocks we may already have in the block
592+
/// cache, and can therefore be used to get rid of duplicate blocks in
593+
/// the block cache as well as making sure that a certain block is in
594+
/// the cache
595+
Ingest {
596+
/// The name of the chain
597+
name: String,
598+
/// The block number to ingest
599+
number: BlockNumber,
600+
},
588601
}
589602

590603
#[derive(Clone, Debug, Subcommand)]
@@ -1450,6 +1463,12 @@ async fn main() -> anyhow::Result<()> {
14501463
}
14511464
}
14521465
}
1466+
Ingest { name, number } => {
1467+
let logger = ctx.logger.cheap_clone();
1468+
let (chain_store, ethereum_adapter) =
1469+
ctx.chain_store_and_adapter(&name).await?;
1470+
commands::chain::ingest(&logger, chain_store, ethereum_adapter, number).await
1471+
}
14531472
}
14541473
}
14551474
Stats(cmd) => {

node/src/manager/commands/chain.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ use graph::cheap_clone::CheapClone;
1010
use graph::components::network_provider::ChainIdentifierStore;
1111
use graph::components::network_provider::ChainName;
1212
use graph::components::store::StoreError;
13+
use graph::futures03::compat::Future01CompatExt as _;
1314
use graph::prelude::BlockNumber;
1415
use graph::prelude::ChainStore as _;
16+
use graph::prelude::LightEthereumBlockExt;
1517
use graph::prelude::{anyhow, anyhow::bail};
1618
use graph::slog::Logger;
1719
use graph::{components::store::BlockStore as _, prelude::anyhow::Error};
20+
use graph_chain_ethereum::chain::BlockFinality;
21+
use graph_chain_ethereum::EthereumAdapter;
22+
use graph_chain_ethereum::EthereumAdapterTrait as _;
1823
use graph_store_postgres::add_chain;
1924
use graph_store_postgres::find_chain;
2025
use graph_store_postgres::update_chain_name;
@@ -259,3 +264,31 @@ pub fn change_block_cache_shard(
259264

260265
Ok(())
261266
}
267+
268+
pub async fn ingest(
269+
logger: &Logger,
270+
chain_store: Arc<ChainStore>,
271+
ethereum_adapter: Arc<EthereumAdapter>,
272+
number: BlockNumber,
273+
) -> Result<(), Error> {
274+
let Some(block) = ethereum_adapter
275+
.block_by_number(logger, number)
276+
.compat()
277+
.await
278+
.map_err(|e| anyhow!("error getting block number {number}: {}", e))?
279+
else {
280+
bail!("block number {number} not found");
281+
};
282+
let ptr = block.block_ptr();
283+
// For inserting the block, it doesn't matter whether the block is final or not.
284+
let block = Arc::new(BlockFinality::Final(Arc::new(block)));
285+
chain_store.upsert_block(block).await?;
286+
287+
let rows = chain_store.confirm_block_hash(ptr.number, &ptr.hash)?;
288+
289+
println!("Inserted block {}", ptr);
290+
if rows > 0 {
291+
println!(" (also deleted {rows} duplicate row(s) with that number)");
292+
}
293+
Ok(())
294+
}

0 commit comments

Comments
 (0)