Skip to content

Commit b5f3abf

Browse files
committed
prune submissions
1 parent 20aa637 commit b5f3abf

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

pallets/shield/src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,26 @@ pub mod pallet {
205205
// 4) Prune old epoch hashes with a sliding window of size KEY_EPOCH_HISTORY.
206206
let depth: BlockNumberFor<T> = KEY_EPOCH_HISTORY.into();
207207
if n >= depth {
208-
let prune_before = n.saturating_sub(depth);
209-
KeyHashByBlock::<T>::remove(prune_before);
208+
let prune_bn = n.saturating_sub(depth);
209+
KeyHashByBlock::<T>::remove(prune_bn);
210+
writes = writes.saturating_add(1);
211+
}
212+
213+
// 5) TTL-based pruning of stale submissions.
214+
let ttl: BlockNumberFor<T> = KEY_EPOCH_HISTORY.into();
215+
let threshold: BlockNumberFor<T> = n.saturating_sub(ttl);
216+
217+
let mut to_remove: Vec<T::Hash> = Vec::new();
218+
219+
for (id, sub) in Submissions::<T>::iter() {
220+
reads = reads.saturating_add(1);
221+
if sub.submitted_in < threshold {
222+
to_remove.push(id);
223+
}
224+
}
225+
226+
for id in to_remove {
227+
Submissions::<T>::remove(id);
210228
writes = writes.saturating_add(1);
211229
}
212230

pallets/shield/src/tests.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,68 @@ fn key_hash_by_block_prunes_old_entries() {
472472
});
473473
}
474474

475+
#[test]
476+
fn submissions_pruned_after_ttl_window() {
477+
new_test_ext().execute_with(|| {
478+
// This must match KEY_EPOCH_HISTORY in the pallet.
479+
const KEEP: u64 = 100;
480+
const TOTAL: u64 = KEEP + 5;
481+
482+
let pair = test_sr25519_pair();
483+
let who: AccountId32 = pair.public().into();
484+
485+
// Helper: create a submission at a specific block with a tagged commitment.
486+
let make_submission = |block: u64, tag: &[u8]| -> TestHash {
487+
System::set_block_number(block);
488+
let commitment: TestHash = <Test as frame_system::Config>::Hashing::hash(tag);
489+
let ciphertext_bytes = vec![block as u8; 4];
490+
let ciphertext: BoundedVec<u8, FrameConstU32<8192>> =
491+
BoundedVec::truncate_from(ciphertext_bytes);
492+
493+
assert_ok!(MevShield::submit_encrypted(
494+
RuntimeOrigin::signed(who.clone()),
495+
commitment,
496+
ciphertext.clone(),
497+
));
498+
499+
<Test as frame_system::Config>::Hashing::hash_of(&(
500+
who.clone(),
501+
commitment,
502+
&ciphertext,
503+
))
504+
};
505+
506+
// With n = TOTAL and depth = KEEP, prune_before = n - KEEP = 5.
507+
let stale_block1: u64 = 1; // < 5, should be pruned
508+
let stale_block2: u64 = 4; // < 5, should be pruned
509+
let keep_block1: u64 = 5; // == prune_before, should be kept
510+
let keep_block2: u64 = TOTAL; // latest, should be kept
511+
512+
let id_stale1 = make_submission(stale_block1, b"stale-1");
513+
let id_stale2 = make_submission(stale_block2, b"stale-2");
514+
let id_keep1 = make_submission(keep_block1, b"keep-1");
515+
let id_keep2 = make_submission(keep_block2, b"keep-2");
516+
517+
// Sanity: all are present before pruning.
518+
assert!(Submissions::<Test>::get(id_stale1).is_some());
519+
assert!(Submissions::<Test>::get(id_stale2).is_some());
520+
assert!(Submissions::<Test>::get(id_keep1).is_some());
521+
assert!(Submissions::<Test>::get(id_keep2).is_some());
522+
523+
// Run on_initialize at block TOTAL, triggering TTL pruning over Submissions.
524+
let n_final: TestBlockNumber = TOTAL.saturated_into();
525+
MevShield::on_initialize(n_final);
526+
527+
// Submissions with submitted_in < prune_before (5) should be gone.
528+
assert!(Submissions::<Test>::get(id_stale1).is_none());
529+
assert!(Submissions::<Test>::get(id_stale2).is_none());
530+
531+
// Submissions at or after prune_before should remain.
532+
assert!(Submissions::<Test>::get(id_keep1).is_some());
533+
assert!(Submissions::<Test>::get(id_keep2).is_some());
534+
});
535+
}
536+
475537
#[test]
476538
fn validate_unsigned_accepts_local_source_for_execute_revealed() {
477539
new_test_ext().execute_with(|| {

0 commit comments

Comments
 (0)