1use std::collections::VecDeque;
21
22use crate::LOG_TARGET;
23use polkadot_primitives::{Hash, Id as ParaId};
24
25#[derive(Debug, PartialEq)]
28struct ClaimInfo {
29 hash: Option<Hash>,
31 claim: Option<ParaId>,
33 claim_queue_len: usize,
36 claimed: bool,
38}
39
40pub(crate) struct ClaimQueueState {
86 block_state: VecDeque<ClaimInfo>,
87 future_blocks: VecDeque<ClaimInfo>,
88}
89
90impl ClaimQueueState {
91 pub(crate) fn new() -> Self {
92 Self { block_state: VecDeque::new(), future_blocks: VecDeque::new() }
93 }
94
95 pub(crate) fn add_leaf(&mut self, hash: &Hash, claim_queue: &Vec<ParaId>) {
97 if self.block_state.iter().any(|s| s.hash == Some(*hash)) {
98 return
99 }
100
101 for (idx, expected_claim) in claim_queue.iter().enumerate() {
105 match self.future_blocks.get_mut(idx) {
106 Some(future_block) =>
107 if future_block.claim.as_ref() != Some(expected_claim) {
108 future_block.claimed = false;
111 future_block.claim = Some(*expected_claim);
112 },
113 None => {
114 self.future_blocks.push_back(ClaimInfo {
115 hash: None,
116 claim: Some(*expected_claim),
117 claim_queue_len: 1,
123 claimed: false,
124 });
125 },
126 }
127 }
128
129 let claim_info = if let Some(new_leaf) = self.future_blocks.pop_front() {
131 ClaimInfo {
132 hash: Some(*hash),
133 claim: claim_queue.first().copied(),
134 claim_queue_len: claim_queue.len(),
135 claimed: new_leaf.claimed,
136 }
137 } else {
138 ClaimInfo {
140 hash: Some(*hash),
141 claim: claim_queue.first().copied(),
142 claim_queue_len: claim_queue.len(),
143 claimed: false,
144 }
145 };
146
147 self.future_blocks.truncate(claim_queue.len().saturating_sub(1));
151
152 self.block_state.push_back(claim_info);
153 }
154
155 fn get_window<'a>(
156 &'a mut self,
157 relay_parent: &'a Hash,
158 ) -> impl Iterator<Item = &'a mut ClaimInfo> + 'a {
159 let mut window = self
160 .block_state
161 .iter_mut()
162 .skip_while(|b| b.hash != Some(*relay_parent))
163 .peekable();
164 let cq_len = window.peek().map_or(0, |b| b.claim_queue_len);
165 window.chain(self.future_blocks.iter_mut()).take(cq_len)
166 }
167
168 pub(crate) fn claim_at(&mut self, relay_parent: &Hash, para_id: &ParaId) -> bool {
169 gum::trace!(
170 target: LOG_TARGET,
171 ?para_id,
172 ?relay_parent,
173 "claim_at"
174 );
175 self.find_a_claim(relay_parent, para_id, true)
176 }
177
178 pub(crate) fn can_claim_at(&mut self, relay_parent: &Hash, para_id: &ParaId) -> bool {
179 gum::trace!(
180 target: LOG_TARGET,
181 ?para_id,
182 ?relay_parent,
183 "can_claim_at"
184 );
185
186 self.find_a_claim(relay_parent, para_id, false)
187 }
188
189 fn find_a_claim(&mut self, relay_parent: &Hash, para_id: &ParaId, claim_it: bool) -> bool {
193 let window = self.get_window(relay_parent);
194
195 for w in window {
196 gum::trace!(
197 target: LOG_TARGET,
198 ?para_id,
199 ?relay_parent,
200 claim_info=?w,
201 ?claim_it,
202 "Checking claim"
203 );
204
205 if !w.claimed && w.claim == Some(*para_id) {
206 w.claimed = claim_it;
207 return true
208 }
209 }
210
211 false
212 }
213
214 pub(crate) fn unclaimed_at(&mut self, relay_parent: &Hash) -> Vec<ParaId> {
215 let window = self.get_window(relay_parent);
216
217 window.filter(|b| !b.claimed).filter_map(|b| b.claim).collect()
218 }
219}
220
221#[cfg(test)]
222mod test {
223 use super::*;
224
225 #[test]
226 fn sane_initial_state() {
227 let mut state = ClaimQueueState::new();
228 let relay_parent = Hash::from_low_u64_be(1);
229 let para_id = ParaId::new(1);
230
231 assert!(!state.can_claim_at(&relay_parent, ¶_id));
232 assert!(!state.claim_at(&relay_parent, ¶_id));
233 assert_eq!(state.unclaimed_at(&relay_parent), vec![]);
234 }
235
236 #[test]
237 fn add_leaf_works() {
238 let mut state = ClaimQueueState::new();
239 let relay_parent_a = Hash::from_low_u64_be(1);
240 let para_id = ParaId::new(1);
241 let claim_queue = vec![para_id, para_id, para_id];
242
243 state.add_leaf(&relay_parent_a, &claim_queue);
244 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id, para_id, para_id]);
245
246 assert_eq!(
247 state.block_state,
248 VecDeque::from(vec![ClaimInfo {
249 hash: Some(relay_parent_a),
250 claim: Some(para_id),
251 claim_queue_len: 3,
252 claimed: false,
253 },])
254 );
255 assert_eq!(
256 state.future_blocks,
257 VecDeque::from(vec![
258 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: false },
259 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: false }
260 ])
261 );
262
263 state.add_leaf(&relay_parent_a, &claim_queue);
265 assert_eq!(state.block_state.len(), 1);
266 assert_eq!(state.future_blocks.len(), 2);
267
268 let relay_parent_b = Hash::from_low_u64_be(2);
270 state.add_leaf(&relay_parent_b, &claim_queue);
271
272 assert_eq!(
273 state.block_state,
274 VecDeque::from(vec![
275 ClaimInfo {
276 hash: Some(relay_parent_a),
277 claim: Some(para_id),
278 claim_queue_len: 3,
279 claimed: false,
280 },
281 ClaimInfo {
282 hash: Some(relay_parent_b),
283 claim: Some(para_id),
284 claim_queue_len: 3,
285 claimed: false,
286 }
287 ])
288 );
289 assert_eq!(
290 state.future_blocks,
291 VecDeque::from(vec![
292 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: false },
293 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: false }
294 ])
295 );
296
297 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id, para_id, para_id]);
298 assert_eq!(state.unclaimed_at(&relay_parent_b), vec![para_id, para_id, para_id]);
299 }
300
301 #[test]
302 fn claims_at_separate_relay_parents_work() {
303 let mut state = ClaimQueueState::new();
304 let relay_parent_a = Hash::from_low_u64_be(1);
305 let relay_parent_b = Hash::from_low_u64_be(2);
306 let para_id = ParaId::new(1);
307 let claim_queue = vec![para_id, para_id, para_id];
308
309 state.add_leaf(&relay_parent_a, &claim_queue);
310 state.add_leaf(&relay_parent_b, &claim_queue);
311
312 assert!(state.can_claim_at(&relay_parent_a, ¶_id));
314 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id, para_id, para_id]);
315 assert!(state.claim_at(&relay_parent_a, ¶_id));
316
317 assert!(state.can_claim_at(&relay_parent_b, ¶_id));
319 assert_eq!(state.unclaimed_at(&relay_parent_b), vec![para_id, para_id, para_id]);
320 assert!(state.claim_at(&relay_parent_b, ¶_id));
321
322 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id]);
324 assert_eq!(state.unclaimed_at(&relay_parent_b), vec![para_id, para_id]);
326
327 assert_eq!(
328 state.block_state,
329 VecDeque::from(vec![
330 ClaimInfo {
331 hash: Some(relay_parent_a),
332 claim: Some(para_id),
333 claim_queue_len: 3,
334 claimed: true,
335 },
336 ClaimInfo {
337 hash: Some(relay_parent_b),
338 claim: Some(para_id),
339 claim_queue_len: 3,
340 claimed: true,
341 }
342 ])
343 );
344 assert_eq!(
345 state.future_blocks,
346 VecDeque::from(vec![
347 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: false },
348 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: false }
349 ])
350 );
351 }
352
353 #[test]
354 fn claims_are_transferred_to_next_slot() {
355 let mut state = ClaimQueueState::new();
356 let relay_parent_a = Hash::from_low_u64_be(1);
357 let para_id = ParaId::new(1);
358 let claim_queue = vec![para_id, para_id, para_id];
359
360 state.add_leaf(&relay_parent_a, &claim_queue);
361
362 assert!(state.can_claim_at(&relay_parent_a, ¶_id));
364 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id, para_id, para_id]);
365 assert!(state.claim_at(&relay_parent_a, ¶_id));
366
367 assert!(state.can_claim_at(&relay_parent_a, ¶_id));
368 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id, para_id]);
369 assert!(state.claim_at(&relay_parent_a, ¶_id));
370
371 assert_eq!(
372 state.block_state,
373 VecDeque::from(vec![ClaimInfo {
374 hash: Some(relay_parent_a),
375 claim: Some(para_id),
376 claim_queue_len: 3,
377 claimed: true,
378 },])
379 );
380 assert_eq!(
381 state.future_blocks,
382 VecDeque::from(vec![
383 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: true },
384 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: false }
385 ])
386 );
387
388 assert!(state.can_claim_at(&relay_parent_a, ¶_id));
390 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id]);
391 assert!(state.claim_at(&relay_parent_a, ¶_id));
392
393 assert_eq!(
394 state.block_state,
395 VecDeque::from(vec![ClaimInfo {
396 hash: Some(relay_parent_a),
397 claim: Some(para_id),
398 claim_queue_len: 3,
399 claimed: true,
400 },])
401 );
402 assert_eq!(
403 state.future_blocks,
404 VecDeque::from(vec![
405 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: true },
406 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: true }
407 ])
408 );
409
410 assert!(!state.can_claim_at(&relay_parent_a, ¶_id));
412 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![]);
413 }
414
415 #[test]
416 fn claims_are_transferred_to_new_leaves() {
417 let mut state = ClaimQueueState::new();
418 let relay_parent_a = Hash::from_low_u64_be(1);
419 let para_id = ParaId::new(1);
420 let claim_queue = vec![para_id, para_id, para_id];
421
422 state.add_leaf(&relay_parent_a, &claim_queue);
423
424 for _ in 0..3 {
425 assert!(state.can_claim_at(&relay_parent_a, ¶_id));
426 assert!(state.claim_at(&relay_parent_a, ¶_id));
427 }
428
429 assert_eq!(
430 state.block_state,
431 VecDeque::from(vec![ClaimInfo {
432 hash: Some(relay_parent_a),
433 claim: Some(para_id),
434 claim_queue_len: 3,
435 claimed: true,
436 },])
437 );
438 assert_eq!(
439 state.future_blocks,
440 VecDeque::from(vec![
441 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: true },
442 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: true }
443 ])
444 );
445
446 assert!(!state.can_claim_at(&relay_parent_a, ¶_id));
448
449 let relay_parent_b = Hash::from_low_u64_be(2);
451 state.add_leaf(&relay_parent_b, &claim_queue);
452
453 assert_eq!(
454 state.block_state,
455 VecDeque::from(vec![
456 ClaimInfo {
457 hash: Some(relay_parent_a),
458 claim: Some(para_id),
459 claim_queue_len: 3,
460 claimed: true,
461 },
462 ClaimInfo {
463 hash: Some(relay_parent_b),
464 claim: Some(para_id),
465 claim_queue_len: 3,
466 claimed: true,
467 }
468 ])
469 );
470 assert_eq!(
471 state.future_blocks,
472 VecDeque::from(vec![
473 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: true },
474 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: false }
475 ])
476 );
477
478 assert!(!state.can_claim_at(&relay_parent_a, ¶_id));
480
481 assert!(state.can_claim_at(&relay_parent_b, ¶_id));
483 assert!(state.claim_at(&relay_parent_b, ¶_id));
484
485 assert_eq!(
486 state.block_state,
487 VecDeque::from(vec![
488 ClaimInfo {
489 hash: Some(relay_parent_a),
490 claim: Some(para_id),
491 claim_queue_len: 3,
492 claimed: true,
493 },
494 ClaimInfo {
495 hash: Some(relay_parent_b),
496 claim: Some(para_id),
497 claim_queue_len: 3,
498 claimed: true,
499 }
500 ])
501 );
502 assert_eq!(
503 state.future_blocks,
504 VecDeque::from(vec![
505 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: true },
506 ClaimInfo { hash: None, claim: Some(para_id), claim_queue_len: 1, claimed: true }
507 ])
508 );
509 }
510
511 #[test]
512 fn two_paras() {
513 let mut state = ClaimQueueState::new();
514 let relay_parent_a = Hash::from_low_u64_be(1);
515 let para_id_a = ParaId::new(1);
516 let para_id_b = ParaId::new(2);
517 let claim_queue = vec![para_id_a, para_id_b, para_id_a];
518
519 state.add_leaf(&relay_parent_a, &claim_queue);
520 assert!(state.can_claim_at(&relay_parent_a, ¶_id_a));
521 assert!(state.can_claim_at(&relay_parent_a, ¶_id_b));
522 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id_a, para_id_b, para_id_a]);
523
524 assert_eq!(
525 state.block_state,
526 VecDeque::from(vec![ClaimInfo {
527 hash: Some(relay_parent_a),
528 claim: Some(para_id_a),
529 claim_queue_len: 3,
530 claimed: false,
531 },])
532 );
533 assert_eq!(
534 state.future_blocks,
535 VecDeque::from(vec![
536 ClaimInfo {
537 hash: None,
538 claim: Some(para_id_b),
539 claim_queue_len: 1,
540 claimed: false
541 },
542 ClaimInfo {
543 hash: None,
544 claim: Some(para_id_a),
545 claim_queue_len: 1,
546 claimed: false
547 }
548 ])
549 );
550
551 assert!(state.claim_at(&relay_parent_a, ¶_id_a));
552 assert!(state.can_claim_at(&relay_parent_a, ¶_id_a));
553 assert!(state.can_claim_at(&relay_parent_a, ¶_id_b));
554 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id_b, para_id_a]);
555
556 assert_eq!(
557 state.block_state,
558 VecDeque::from(vec![ClaimInfo {
559 hash: Some(relay_parent_a),
560 claim: Some(para_id_a),
561 claim_queue_len: 3,
562 claimed: true,
563 },])
564 );
565 assert_eq!(
566 state.future_blocks,
567 VecDeque::from(vec![
568 ClaimInfo {
569 hash: None,
570 claim: Some(para_id_b),
571 claim_queue_len: 1,
572 claimed: false
573 },
574 ClaimInfo {
575 hash: None,
576 claim: Some(para_id_a),
577 claim_queue_len: 1,
578 claimed: false
579 }
580 ])
581 );
582
583 assert!(state.claim_at(&relay_parent_a, ¶_id_a));
584 assert!(!state.can_claim_at(&relay_parent_a, ¶_id_a));
585 assert!(state.can_claim_at(&relay_parent_a, ¶_id_b));
586 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id_b]);
587
588 assert_eq!(
589 state.block_state,
590 VecDeque::from(vec![ClaimInfo {
591 hash: Some(relay_parent_a),
592 claim: Some(para_id_a),
593 claim_queue_len: 3,
594 claimed: true,
595 },])
596 );
597 assert_eq!(
598 state.future_blocks,
599 VecDeque::from(vec![
600 ClaimInfo {
601 hash: None,
602 claim: Some(para_id_b),
603 claim_queue_len: 1,
604 claimed: false
605 },
606 ClaimInfo { hash: None, claim: Some(para_id_a), claim_queue_len: 1, claimed: true }
607 ])
608 );
609
610 assert!(state.claim_at(&relay_parent_a, ¶_id_b));
611 assert!(!state.can_claim_at(&relay_parent_a, ¶_id_a));
612 assert!(!state.can_claim_at(&relay_parent_a, ¶_id_b));
613 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![]);
614
615 assert_eq!(
616 state.block_state,
617 VecDeque::from(vec![ClaimInfo {
618 hash: Some(relay_parent_a),
619 claim: Some(para_id_a),
620 claim_queue_len: 3,
621 claimed: true,
622 },])
623 );
624 assert_eq!(
625 state.future_blocks,
626 VecDeque::from(vec![
627 ClaimInfo { hash: None, claim: Some(para_id_b), claim_queue_len: 1, claimed: true },
628 ClaimInfo { hash: None, claim: Some(para_id_a), claim_queue_len: 1, claimed: true }
629 ])
630 );
631 }
632
633 #[test]
634 fn claim_queue_changes_unexpectedly() {
635 let mut state = ClaimQueueState::new();
636 let relay_parent_a = Hash::from_low_u64_be(1);
637 let para_id_a = ParaId::new(1);
638 let para_id_b = ParaId::new(2);
639 let claim_queue_a = vec![para_id_a, para_id_b, para_id_a];
640
641 state.add_leaf(&relay_parent_a, &claim_queue_a);
642 assert!(state.can_claim_at(&relay_parent_a, ¶_id_a));
643 assert!(state.can_claim_at(&relay_parent_a, ¶_id_b));
644 assert!(state.claim_at(&relay_parent_a, ¶_id_a));
645 assert!(state.claim_at(&relay_parent_a, ¶_id_a));
646 assert!(state.claim_at(&relay_parent_a, ¶_id_b));
647 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![]);
648
649 assert_eq!(
650 state.block_state,
651 VecDeque::from(vec![ClaimInfo {
652 hash: Some(relay_parent_a),
653 claim: Some(para_id_a),
654 claim_queue_len: 3,
655 claimed: true,
656 },])
657 );
658 assert_eq!(
659 state.future_blocks,
660 VecDeque::from(vec![
661 ClaimInfo { hash: None, claim: Some(para_id_b), claim_queue_len: 1, claimed: true },
662 ClaimInfo { hash: None, claim: Some(para_id_a), claim_queue_len: 1, claimed: true }
663 ])
664 );
665
666 let relay_parent_b = Hash::from_low_u64_be(2);
667 let claim_queue_b = vec![para_id_a, para_id_a, para_id_a]; state.add_leaf(&relay_parent_b, &claim_queue_b);
669
670 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id_a]);
673
674 assert_eq!(
675 state.block_state,
676 VecDeque::from(vec![
677 ClaimInfo {
678 hash: Some(relay_parent_a),
679 claim: Some(para_id_a),
680 claim_queue_len: 3,
681 claimed: true,
682 },
683 ClaimInfo {
684 hash: Some(relay_parent_b),
685 claim: Some(para_id_a),
686 claim_queue_len: 3,
687 claimed: false,
688 }
689 ])
690 );
691 assert_eq!(
692 state.future_blocks,
693 VecDeque::from(vec![
696 ClaimInfo { hash: None, claim: Some(para_id_a), claim_queue_len: 1, claimed: true },
697 ClaimInfo {
698 hash: None,
699 claim: Some(para_id_a),
700 claim_queue_len: 1,
701 claimed: false
702 }
703 ])
704 );
705 }
706
707 #[test]
708 fn claim_queue_changes_unexpectedly_with_two_blocks() {
709 let mut state = ClaimQueueState::new();
710 let relay_parent_a = Hash::from_low_u64_be(1);
711 let para_id_a = ParaId::new(1);
712 let para_id_b = ParaId::new(2);
713 let claim_queue_a = vec![para_id_a, para_id_b, para_id_b];
714
715 state.add_leaf(&relay_parent_a, &claim_queue_a);
716 assert!(state.can_claim_at(&relay_parent_a, ¶_id_a));
717 assert!(state.can_claim_at(&relay_parent_a, ¶_id_b));
718 assert!(state.claim_at(&relay_parent_a, ¶_id_a));
719 assert!(state.claim_at(&relay_parent_a, ¶_id_b));
720 assert!(state.claim_at(&relay_parent_a, ¶_id_b));
721 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![]);
722
723 assert_eq!(
724 state.block_state,
725 VecDeque::from(vec![ClaimInfo {
726 hash: Some(relay_parent_a),
727 claim: Some(para_id_a),
728 claim_queue_len: 3,
729 claimed: true,
730 },])
731 );
732 assert_eq!(
733 state.future_blocks,
734 VecDeque::from(vec![
735 ClaimInfo { hash: None, claim: Some(para_id_b), claim_queue_len: 1, claimed: true },
736 ClaimInfo { hash: None, claim: Some(para_id_b), claim_queue_len: 1, claimed: true }
737 ])
738 );
739
740 let relay_parent_b = Hash::from_low_u64_be(2);
741 let claim_queue_b = vec![para_id_a, para_id_a, para_id_a]; state.add_leaf(&relay_parent_b, &claim_queue_b);
743
744 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id_a, para_id_a]);
747
748 assert_eq!(
749 state.block_state,
750 VecDeque::from(vec![
751 ClaimInfo {
752 hash: Some(relay_parent_a),
753 claim: Some(para_id_a),
754 claim_queue_len: 3,
755 claimed: true,
756 },
757 ClaimInfo {
758 hash: Some(relay_parent_b),
759 claim: Some(para_id_a),
760 claim_queue_len: 3,
761 claimed: false,
762 }
763 ])
764 );
765 assert_eq!(
766 state.future_blocks,
767 VecDeque::from(vec![
768 ClaimInfo {
769 hash: None,
770 claim: Some(para_id_a),
771 claim_queue_len: 1,
772 claimed: false
773 },
774 ClaimInfo {
775 hash: None,
776 claim: Some(para_id_a),
777 claim_queue_len: 1,
778 claimed: false
779 }
780 ])
781 );
782 }
783
784 #[test]
785 fn empty_claim_queue() {
786 let mut state = ClaimQueueState::new();
787 let relay_parent_a = Hash::from_low_u64_be(1);
788 let para_id_a = ParaId::new(1);
789 let claim_queue_a = vec![];
790
791 state.add_leaf(&relay_parent_a, &claim_queue_a);
792 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![]);
793
794 assert_eq!(
795 state.block_state,
796 VecDeque::from(vec![ClaimInfo {
797 hash: Some(relay_parent_a),
798 claim: None,
799 claim_queue_len: 0,
800 claimed: false,
801 },])
802 );
803 assert!(state.future_blocks.is_empty());
805
806 assert!(!state.can_claim_at(&relay_parent_a, ¶_id_a));
807 assert!(!state.claim_at(&relay_parent_a, ¶_id_a));
808 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![]);
809
810 let relay_parent_b = Hash::from_low_u64_be(2);
811 let claim_queue_b = vec![para_id_a];
812 state.add_leaf(&relay_parent_b, &claim_queue_b);
813
814 assert_eq!(
815 state.block_state,
816 VecDeque::from(vec![
817 ClaimInfo {
818 hash: Some(relay_parent_a),
819 claim: None,
820 claim_queue_len: 0,
821 claimed: false,
822 },
823 ClaimInfo {
824 hash: Some(relay_parent_b),
825 claim: Some(para_id_a),
826 claim_queue_len: 1,
827 claimed: false,
828 },
829 ])
830 );
831 assert!(state.future_blocks.is_empty());
833
834 assert!(!state.can_claim_at(&relay_parent_a, ¶_id_a));
835 assert!(!state.claim_at(&relay_parent_a, ¶_id_a));
836 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![]);
837
838 assert!(state.can_claim_at(&relay_parent_b, ¶_id_a));
839 assert_eq!(state.unclaimed_at(&relay_parent_b), vec![para_id_a]);
840 assert!(state.claim_at(&relay_parent_b, ¶_id_a));
841
842 let relay_parent_c = Hash::from_low_u64_be(3);
843 let claim_queue_c = vec![para_id_a, para_id_a];
844 state.add_leaf(&relay_parent_c, &claim_queue_c);
845
846 assert_eq!(
847 state.block_state,
848 VecDeque::from(vec![
849 ClaimInfo {
850 hash: Some(relay_parent_a),
851 claim: None,
852 claim_queue_len: 0,
853 claimed: false,
854 },
855 ClaimInfo {
856 hash: Some(relay_parent_b),
857 claim: Some(para_id_a),
858 claim_queue_len: 1,
859 claimed: true,
860 },
861 ClaimInfo {
862 hash: Some(relay_parent_c),
863 claim: Some(para_id_a),
864 claim_queue_len: 2,
865 claimed: false,
866 },
867 ])
868 );
869 assert_eq!(
871 state.future_blocks,
872 VecDeque::from(vec![ClaimInfo {
873 hash: None,
874 claim: Some(para_id_a),
875 claim_queue_len: 1,
876 claimed: false,
877 },])
878 );
879
880 assert!(!state.can_claim_at(&relay_parent_a, ¶_id_a));
881 assert!(!state.claim_at(&relay_parent_a, ¶_id_a));
882 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![]);
883
884 assert!(!state.can_claim_at(&relay_parent_b, ¶_id_a));
886 assert_eq!(state.unclaimed_at(&relay_parent_b), vec![]);
887 assert!(!state.claim_at(&relay_parent_b, ¶_id_a));
888
889 assert!(state.can_claim_at(&relay_parent_c, ¶_id_a));
890 assert_eq!(state.unclaimed_at(&relay_parent_c), vec![para_id_a, para_id_a]);
891 }
892
893 #[test]
894 fn claim_queue_becomes_shorter() {
895 let mut state = ClaimQueueState::new();
896 let relay_parent_a = Hash::from_low_u64_be(1);
897 let para_id_a = ParaId::new(1);
898 let para_id_b = ParaId::new(2);
899 let claim_queue_a = vec![para_id_a, para_id_b, para_id_a];
900
901 state.add_leaf(&relay_parent_a, &claim_queue_a);
902 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id_a, para_id_b, para_id_a]);
903
904 assert_eq!(
905 state.block_state,
906 VecDeque::from(vec![ClaimInfo {
907 hash: Some(relay_parent_a),
908 claim: Some(para_id_a),
909 claim_queue_len: 3,
910 claimed: false,
911 },])
912 );
913 assert_eq!(
914 state.future_blocks,
915 VecDeque::from(vec![
916 ClaimInfo {
917 hash: None,
918 claim: Some(para_id_b),
919 claim_queue_len: 1,
920 claimed: false
921 },
922 ClaimInfo {
923 hash: None,
924 claim: Some(para_id_a),
925 claim_queue_len: 1,
926 claimed: false
927 }
928 ])
929 );
930
931 let relay_parent_b = Hash::from_low_u64_be(2);
932 let claim_queue_b = vec![para_id_a, para_id_b]; state.add_leaf(&relay_parent_b, &claim_queue_b);
934
935 assert_eq!(state.unclaimed_at(&relay_parent_b), vec![para_id_a, para_id_b]);
936 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id_a, para_id_a, para_id_b]);
938
939 assert_eq!(
940 state.block_state,
941 VecDeque::from(vec![
942 ClaimInfo {
943 hash: Some(relay_parent_a),
944 claim: Some(para_id_a),
945 claim_queue_len: 3,
946 claimed: false,
947 },
948 ClaimInfo {
949 hash: Some(relay_parent_b),
950 claim: Some(para_id_a),
951 claim_queue_len: 2,
952 claimed: false,
953 }
954 ])
955 );
956 assert_eq!(
957 state.future_blocks,
958 VecDeque::from(vec![ClaimInfo {
959 hash: None,
960 claim: Some(para_id_b),
961 claim_queue_len: 1,
962 claimed: false
963 },])
964 );
965 }
966
967 #[test]
968 fn claim_queue_becomes_shorter_and_drops_future_claims() {
969 let mut state = ClaimQueueState::new();
970 let relay_parent_a = Hash::from_low_u64_be(1);
971 let para_id_a = ParaId::new(1);
972 let para_id_b = ParaId::new(2);
973 let claim_queue_a = vec![para_id_a, para_id_b, para_id_a, para_id_b];
974
975 state.add_leaf(&relay_parent_a, &claim_queue_a);
976
977 assert_eq!(
978 state.unclaimed_at(&relay_parent_a),
979 vec![para_id_a, para_id_b, para_id_a, para_id_b]
980 );
981
982 assert_eq!(
984 state.block_state,
985 VecDeque::from(vec![ClaimInfo {
986 hash: Some(relay_parent_a),
987 claim: Some(para_id_a),
988 claim_queue_len: 4,
989 claimed: false,
990 },])
991 );
992 assert_eq!(
994 state.future_blocks,
995 VecDeque::from(vec![
996 ClaimInfo {
997 hash: None,
998 claim: Some(para_id_b),
999 claim_queue_len: 1,
1000 claimed: false
1001 },
1002 ClaimInfo {
1003 hash: None,
1004 claim: Some(para_id_a),
1005 claim_queue_len: 1,
1006 claimed: false
1007 },
1008 ClaimInfo {
1009 hash: None,
1010 claim: Some(para_id_b),
1011 claim_queue_len: 1,
1012 claimed: false
1013 }
1014 ])
1015 );
1016
1017 let relay_parent_b = Hash::from_low_u64_be(2);
1019 let para_id_a = ParaId::new(1);
1020 let para_id_b = ParaId::new(2);
1021 let claim_queue_b = vec![para_id_b, para_id_a];
1022 state.add_leaf(&relay_parent_b, &claim_queue_b);
1023
1024 assert_eq!(state.unclaimed_at(&relay_parent_a), vec![para_id_a, para_id_b, para_id_a]);
1025 assert_eq!(state.unclaimed_at(&relay_parent_b), vec![para_id_b, para_id_a]);
1026
1027 assert_eq!(
1028 state.block_state,
1029 VecDeque::from(vec![
1030 ClaimInfo {
1031 hash: Some(relay_parent_a),
1032 claim: Some(para_id_a),
1033 claim_queue_len: 4,
1034 claimed: false,
1035 },
1036 ClaimInfo {
1037 hash: Some(relay_parent_b),
1038 claim: Some(para_id_b),
1039 claim_queue_len: 2,
1040 claimed: false,
1041 }
1042 ])
1043 );
1044
1045 assert_eq!(
1046 state.future_blocks,
1047 VecDeque::from(vec![ClaimInfo {
1048 hash: None,
1049 claim: Some(para_id_a),
1050 claim_queue_len: 1,
1051 claimed: false
1052 },])
1053 );
1054 }
1055}