Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphman: Add simpler chain rewind command to delete blocks #5645

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions node/src/bin/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,21 @@ pub enum ChainCommand {
#[clap(value_parser = clap::builder::NonEmptyStringValueParser::new())]
chain_name: String,
},
/// Rewind all blocks on a specific chain
///
/// Note that this doesn't rewind the deployments, only deletes blocks past the target and
/// updates the chain head.
Rewind {
/// Chain name (must be an existing chain, see 'chain list')
#[clap(required = true, value_parser = clap::builder::NonEmptyStringValueParser::new())]
chain_name: String,
/// The block hash of the target block
#[clap(long, short = 'H')]
block_hash: String,
/// The block number of the target block
#[clap(long, short = 'n')]
block_number: i32,
},
}

#[derive(Clone, Debug, Subcommand)]
Expand Down Expand Up @@ -1442,6 +1457,17 @@ async fn main() -> anyhow::Result<()> {
}
}
}
Rewind {
chain_name,
block_hash,
block_number,
} => {
let (block_store, _) = ctx.block_store_and_primary_pool();
let block_hash =
BlockHash::from_str(&block_hash).context("invalid block hash")?;

commands::chain::rewind(block_store, chain_name, block_hash, block_number)
}
}
}
Stats(cmd) => {
Expand Down
21 changes: 21 additions & 0 deletions node/src/manager/commands/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,24 @@ pub fn change_block_cache_shard(

Ok(())
}

pub fn rewind(
store: Arc<BlockStore>,
chain_name: String,
block_hash: BlockHash,
block_number: i32,
) -> Result<(), Error> {
let block_ptr_to = BlockPtr::new(block_hash.clone(), block_number);
let chain_store = store
.chain_store(&chain_name)
.ok_or_else(|| anyhow!("Chain store not found for {}", chain_name))?;

chain_store.rewind_chain(block_ptr_to)?;

println!(
"Successfully rewound {} to block {} ({})",
chain_name, block_number, block_hash
);

Ok(())
}
49 changes: 49 additions & 0 deletions store/postgres/src/chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,32 @@ mod data {
}
}

pub(super) fn delete_blocks_after(
&self,
conn: &mut PgConnection,
chain: &str,
block: i64,
) -> Result<usize, Error> {
match self {
Storage::Shared => {
use public::ethereum_blocks as b;

diesel::delete(b::table)
.filter(b::network_name.eq(chain))
.filter(b::number.gt(block))
.execute(conn)
.map_err(Error::from)
}
Storage::Private(Schema { blocks, .. }) => {
Copy link
Contributor

@mangas mangas Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to ensure this can work when the block cache shard is not the primary. Would also be awesome if we could have some integration tests for this functionality

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does work because the ChainStore should already always have the right pool when it gets created. If you already tested that's great otherwise maybe rewind something in staging

let query = format!("delete from {} where number > $1", blocks.qname);
sql_query(query)
.bind::<BigInt, _>(block)
.execute(conn)
.map_err(Error::from)
}
}
}

pub(super) fn delete_blocks_by_hash(
&self,
conn: &mut PgConnection,
Expand Down Expand Up @@ -1870,6 +1896,29 @@ impl ChainStore {
.await?;
Ok(values)
}

pub fn rewind_chain(&self, block_ptr_to: BlockPtr) -> Result<(), StoreError> {
let mut conn = self.pool.get()?;

conn.transaction(|conn| {
use public::ethereum_networks;

self.storage
.delete_blocks_after(conn, &self.chain, block_ptr_to.number as i64)?;

// Update the chain head
diesel::update(
ethereum_networks::table.filter(ethereum_networks::name.eq(&self.chain)),
)
.set((
ethereum_networks::head_block_number.eq(block_ptr_to.number as i64),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is somewhere we keep the firehose cursor, I think this table has a firehose cursor too, that should be set to empty string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here regarding tests

ethereum_networks::head_block_hash.eq(block_ptr_to.hash_hex()),
))
.execute(conn)?;

Ok(())
})
}
}

#[async_trait]
Expand Down
Loading