From 16fd9e8b57a60baf84e7eebcc799b0179fef04ab Mon Sep 17 00:00:00 2001 From: Waclaw Banasik Date: Fri, 11 Oct 2024 16:44:52 +0100 Subject: [PATCH] fix(gc) - fix the chunk_tail <= tail invariant (#12207) According to this line in the storage checker, the chunk tail should always be less or equal than the tail: https://github.com/near/nearcore/blob/99ecfa4e8b97eda5e369d82ef00cfc06e5291552/chain/chain/src/store_validator/validate.rs#L146 This invariant is broken, I'm guessing since #12025 as shown by nayduck gc test failing at this check: https://nayduck.nearone.org/#/test/108874 - test1 stderr last line This is a simple mitigation but I'm not 100% sure if correct - please let me know if it makes sense or not. Even if not entirely correct it should not cause any problems. At worst gc will attempt to clean a little bit more data that isn't there. I hope. The only real change here is the added `saturating_sub(1)` to the line where we set the tail. --- chain/chain/src/chain.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/chain/chain/src/chain.rs b/chain/chain/src/chain.rs index 5b790cf7aa0..10e78da066a 100644 --- a/chain/chain/src/chain.rs +++ b/chain/chain/src/chain.rs @@ -1679,7 +1679,13 @@ impl Chain { tracing::debug!(target: "sync", ?min_height_included, ?new_tail, "adjusting tail for missing chunks"); new_tail = std::cmp::min(new_tail, min_height_included.saturating_sub(1)); - let new_chunk_tail = prev_block.chunks().iter().map(|x| x.height_created()).min().unwrap(); + + // In order to find the right new_chunk_tail we need to find the minimum + // of chunk height_created for chunks in the new tail block. + let new_tail_block = self.get_block_by_height(new_tail)?; + let new_chunk_tail = + new_tail_block.chunks().iter().map(|chunk| chunk.height_created()).min().unwrap(); + let tip = Tip::from_header(prev_block.header()); let final_head = Tip::from_header(self.genesis.header()); // Update related heads now.