diff --git a/runtime/src/accounts_index.rs b/runtime/src/accounts_index.rs index fae7fc1055..90428c3e5d 100644 --- a/runtime/src/accounts_index.rs +++ b/runtime/src/accounts_index.rs @@ -66,9 +66,11 @@ impl AccountsIndex { self.roots.contains(&fork) } pub fn add_root(&mut self, fork: Fork) { - if fork > self.last_root { - self.last_root = fork; - } + assert!( + (self.last_root == 0 && fork == 0) || (fork > self.last_root), + "new roots must be increasing" + ); + self.last_root = fork; self.roots.insert(fork); } /// Remove the fork when the storage for the fork is freed @@ -156,15 +158,22 @@ mod tests { fn test_max_last_root() { let mut index = AccountsIndex::::default(); index.add_root(1); - index.add_root(0); assert_eq!(index.last_root, 1); } + #[test] + #[should_panic] + fn test_max_last_root_old() { + let mut index = AccountsIndex::::default(); + index.add_root(1); + index.add_root(0); + } + #[test] fn test_cleanup_first() { let mut index = AccountsIndex::::default(); - index.add_root(1); index.add_root(0); + index.add_root(1); index.cleanup_dead_fork(0); assert!(index.is_root(1)); assert!(!index.is_root(0)); @@ -174,8 +183,8 @@ mod tests { fn test_cleanup_last() { //this behavior might be undefined, clean up should only occur on older forks let mut index = AccountsIndex::::default(); - index.add_root(1); index.add_root(0); + index.add_root(1); index.cleanup_dead_fork(1); assert!(!index.is_root(1)); assert!(index.is_root(0)); diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 101355342a..808817e832 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -293,7 +293,7 @@ impl Bank { *self.parent.write().unwrap() = None; let squash_accounts_start = Instant::now(); - for p in &parents { + for p in parents.iter().rev() { // root forks cannot be purged self.accounts.add_root(p.slot()); }