Skip to content

Commit

Permalink
optimize and test
Browse files Browse the repository at this point in the history
  • Loading branch information
AmbientTea committed Feb 26, 2025
1 parent e4b1d94 commit 31806da
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
4 changes: 1 addition & 3 deletions toolkit/pallets/block-production-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ pub mod pallet {
}

pub fn drop_prefix<'a>(slot: Slot) {
let entries_left: Vec<_> =
Log::<T>::get().into_iter().filter(move |(s, _)| s > &slot).collect();
Log::<T>::put(entries_left);
Log::<T>::mutate(|vec| vec.retain(move |(s, _)| s > &slot))
}
}
}
46 changes: 46 additions & 0 deletions toolkit/pallets/block-production-log/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,49 @@ fn take_prefix_when_there_are_two_entries_for_the_same_slot() {
);
})
}

#[test]
fn drop_prefix() {
new_test_ext().execute_with(|| {
Log::<Test>::put(vec![
(Slot::from(100), make_id(0)),
(Slot::from(101), make_id(1)),
(Slot::from(102), make_id(2)),
(Slot::from(103), make_id(3)),
(Slot::from(104), make_id(4)),
]);

BlockProductionLog::drop_prefix(Slot::from(102));

let left_in_storage = Log::<Test>::get().to_vec();

assert_eq!(
left_in_storage.to_vec(),
vec![(Slot::from(103), make_id(3)), (Slot::from(104), make_id(4)),]
);
})
}

#[test]
fn peek_prefix() {
new_test_ext().execute_with(|| {
Log::<Test>::put(vec![
(Slot::from(100), make_id(0)),
(Slot::from(101), make_id(1)),
(Slot::from(102), make_id(2)),
(Slot::from(103), make_id(3)),
(Slot::from(104), make_id(4)),
]);

let prefix = BlockProductionLog::peek_prefix(Slot::from(102));

assert_eq!(
prefix.collect::<Vec<_>>(),
vec![
(Slot::from(100), make_id(0)),
(Slot::from(101), make_id(1)),
(Slot::from(102), make_id(2)),
]
);
})
}

0 comments on commit 31806da

Please sign in to comment.