-
Notifications
You must be signed in to change notification settings - Fork 971
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, .. }) => { | ||
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, | ||
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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