From 68312851cafc4c8d43bf2db7b6d45e1589e237fb Mon Sep 17 00:00:00 2001 From: Pankaj Garg Date: Mon, 8 May 2023 16:07:15 -0700 Subject: [PATCH 1/3] Use current root slot to unambiguate pruning of old programs --- program-runtime/src/loaded_programs.rs | 17 ++++++++++++++++- runtime/src/bank_forks.rs | 4 ++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/program-runtime/src/loaded_programs.rs b/program-runtime/src/loaded_programs.rs index ffa934319ce895..1f47d5efaa739b 100644 --- a/program-runtime/src/loaded_programs.rs +++ b/program-runtime/src/loaded_programs.rs @@ -46,6 +46,9 @@ pub enum BlockRelation { pub trait ForkGraph { /// Returns the BlockRelation of A to B fn relationship(&self, a: Slot, b: Slot) -> BlockRelation; + + /// Returns the current root slot + fn root(&self) -> Slot; } /// Provides information about current working slot, and its ancestors @@ -383,7 +386,10 @@ impl LoadedPrograms { let relation = fork_graph.relationship(entry.deployment_slot, new_root); if entry.deployment_slot >= new_root { matches!(relation, BlockRelation::Equal | BlockRelation::Descendant) - } else if !first_ancestor_found && matches!(relation, BlockRelation::Ancestor) { + } else if !first_ancestor_found + && (matches!(relation, BlockRelation::Ancestor) + || fork_graph.root() > entry.deployment_slot) + { first_ancestor_found = true; first_ancestor_found } else { @@ -938,6 +944,10 @@ mod tests { fn relationship(&self, _a: Slot, _b: Slot) -> BlockRelation { self.relation } + + fn root(&self) -> Slot { + 0 + } } #[test] @@ -987,6 +997,7 @@ mod tests { #[derive(Default)] struct TestForkGraphSpecific { forks: Vec>, + root: Slot, } impl TestForkGraphSpecific { @@ -1023,6 +1034,10 @@ mod tests { _ => BlockRelation::Unrelated, } } + + fn root(&self) -> Slot { + self.root + } } struct TestWorkingSlot { diff --git a/runtime/src/bank_forks.rs b/runtime/src/bank_forks.rs index 9dc5d2750a8057..f2bfcf0b80374f 100644 --- a/runtime/src/bank_forks.rs +++ b/runtime/src/bank_forks.rs @@ -668,6 +668,10 @@ impl ForkGraph for BankForks { }) .unwrap_or(BlockRelation::Unknown) } + + fn root(&self) -> Slot { + self.root() + } } #[cfg(test)] From 9a3b0149c44fbb5b96052120815e7dbd70f7c455 Mon Sep 17 00:00:00 2001 From: Pankaj Garg Date: Tue, 9 May 2023 08:23:58 -0700 Subject: [PATCH 2/3] simplify the change --- program-runtime/src/loaded_programs.rs | 15 ++------------- runtime/src/bank_forks.rs | 4 ---- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/program-runtime/src/loaded_programs.rs b/program-runtime/src/loaded_programs.rs index 1f47d5efaa739b..2a0580b6c03b10 100644 --- a/program-runtime/src/loaded_programs.rs +++ b/program-runtime/src/loaded_programs.rs @@ -46,9 +46,6 @@ pub enum BlockRelation { pub trait ForkGraph { /// Returns the BlockRelation of A to B fn relationship(&self, a: Slot, b: Slot) -> BlockRelation; - - /// Returns the current root slot - fn root(&self) -> Slot; } /// Provides information about current working slot, and its ancestors @@ -377,6 +374,7 @@ impl LoadedPrograms { /// Before rerooting the blockstore this removes all programs of orphan forks pub fn prune(&mut self, fork_graph: &F, new_root: Slot) { + let previous_root = self.latest_root; self.entries.retain(|_key, second_level| { let mut first_ancestor_found = false; *second_level = second_level @@ -388,7 +386,7 @@ impl LoadedPrograms { matches!(relation, BlockRelation::Equal | BlockRelation::Descendant) } else if !first_ancestor_found && (matches!(relation, BlockRelation::Ancestor) - || fork_graph.root() > entry.deployment_slot) + || previous_root > entry.deployment_slot) { first_ancestor_found = true; first_ancestor_found @@ -944,10 +942,6 @@ mod tests { fn relationship(&self, _a: Slot, _b: Slot) -> BlockRelation { self.relation } - - fn root(&self) -> Slot { - 0 - } } #[test] @@ -997,7 +991,6 @@ mod tests { #[derive(Default)] struct TestForkGraphSpecific { forks: Vec>, - root: Slot, } impl TestForkGraphSpecific { @@ -1034,10 +1027,6 @@ mod tests { _ => BlockRelation::Unrelated, } } - - fn root(&self) -> Slot { - self.root - } } struct TestWorkingSlot { diff --git a/runtime/src/bank_forks.rs b/runtime/src/bank_forks.rs index f2bfcf0b80374f..9dc5d2750a8057 100644 --- a/runtime/src/bank_forks.rs +++ b/runtime/src/bank_forks.rs @@ -668,10 +668,6 @@ impl ForkGraph for BankForks { }) .unwrap_or(BlockRelation::Unknown) } - - fn root(&self) -> Slot { - self.root() - } } #[cfg(test)] From eae13f0e60612ae13eaba20c2a80a69ce4540e98 Mon Sep 17 00:00:00 2001 From: Pankaj Garg Date: Tue, 9 May 2023 10:00:36 -0700 Subject: [PATCH 3/3] address comment --- program-runtime/src/loaded_programs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/program-runtime/src/loaded_programs.rs b/program-runtime/src/loaded_programs.rs index 2a0580b6c03b10..b833aa78f02c12 100644 --- a/program-runtime/src/loaded_programs.rs +++ b/program-runtime/src/loaded_programs.rs @@ -386,7 +386,7 @@ impl LoadedPrograms { matches!(relation, BlockRelation::Equal | BlockRelation::Descendant) } else if !first_ancestor_found && (matches!(relation, BlockRelation::Ancestor) - || previous_root > entry.deployment_slot) + || entry.deployment_slot < previous_root) { first_ancestor_found = true; first_ancestor_found