Skip to content

Commit d80f3f6

Browse files
committed
Fix tests.
1 parent 61f13f5 commit d80f3f6

File tree

3 files changed

+153
-28
lines changed

3 files changed

+153
-28
lines changed

polkadot/node/network/collator-protocol/src/collator_side/tests/prospective_parachains.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,7 @@ pub(super) async fn update_view(
7575
let ancestry_numbers = (min_number..=leaf_number).rev();
7676
let mut ancestry_iter = ancestry_hashes.clone().zip(ancestry_numbers).peekable();
7777
if let Some((hash, number)) = ancestry_iter.next() {
78-
assert_matches!(
79-
overseer_recv_with_timeout(virtual_overseer, Duration::from_millis(50)).await.unwrap(),
80-
AllMessages::ChainApi(ChainApiMessage::BlockHeader(.., tx)) => {
81-
let header = Header {
82-
parent_hash: get_parent_hash(hash),
83-
number,
84-
state_root: Hash::zero(),
85-
extrinsics_root: Hash::zero(),
86-
digest: Default::default(),
87-
};
88-
89-
tx.send(Ok(Some(header))).unwrap();
90-
}
91-
);
92-
78+
// fetch_ancestors is called first, which requests session for the leaf
9379
assert_matches!(
9480
overseer_recv_with_timeout(virtual_overseer, Duration::from_millis(50)).await.unwrap(),
9581
AllMessages::RuntimeApi(
@@ -135,20 +121,37 @@ pub(super) async fn update_view(
135121
tx.send(Ok(hashes)).unwrap();
136122
}
137123
);
138-
}
139124

140-
for _ in ancestry_iter.clone() {
125+
// fetch_ancestors checks session for each ancestor
126+
for _ in ancestry_iter.clone() {
127+
assert_matches!(
128+
overseer_recv_with_timeout(virtual_overseer, Duration::from_millis(50)).await.unwrap(),
129+
AllMessages::RuntimeApi(
130+
RuntimeApiMessage::Request(
131+
..,
132+
RuntimeApiRequest::SessionIndexForChild(
133+
tx
134+
)
135+
)
136+
) => {
137+
tx.send(Ok(1)).unwrap();
138+
}
139+
);
140+
}
141+
142+
// Now fetch_fresh_leaf_and_insert_ancestry requests block headers
141143
assert_matches!(
142144
overseer_recv_with_timeout(virtual_overseer, Duration::from_millis(50)).await.unwrap(),
143-
AllMessages::RuntimeApi(
144-
RuntimeApiMessage::Request(
145-
..,
146-
RuntimeApiRequest::SessionIndexForChild(
147-
tx
148-
)
149-
)
150-
) => {
151-
tx.send(Ok(1)).unwrap();
145+
AllMessages::ChainApi(ChainApiMessage::BlockHeader(.., tx)) => {
146+
let header = Header {
147+
parent_hash: get_parent_hash(hash),
148+
number,
149+
state_root: Hash::zero(),
150+
extrinsics_root: Hash::zero(),
151+
digest: Default::default(),
152+
};
153+
154+
tx.send(Ok(Some(header))).unwrap();
152155
}
153156
);
154157
}

polkadot/node/network/collator-protocol/src/validator_side/tests/prospective_parachains.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,60 @@ pub(super) async fn update_view(
127127
)
128128
.await;
129129

130-
let min_number = leaf_number.saturating_sub(test_state.scheduling_lookahead);
130+
// activate_leaf calls fetch_ancestors
131+
assert_matches!(
132+
overseer_recv(virtual_overseer).await,
133+
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
134+
_,
135+
RuntimeApiRequest::SessionIndexForChild(tx)
136+
)) => {
137+
tx.send(Ok(test_state.session_index)).unwrap();
138+
}
139+
);
131140

141+
assert_matches!(
142+
overseer_recv(virtual_overseer).await,
143+
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
144+
_,
145+
RuntimeApiRequest::SchedulingLookahead(_, tx)
146+
)) => {
147+
tx.send(Ok(test_state.scheduling_lookahead)).unwrap();
148+
}
149+
);
150+
151+
let min_number = leaf_number.saturating_sub(test_state.scheduling_lookahead);
132152
let ancestry_len = leaf_number + 1 - min_number;
133153
let ancestry_hashes = std::iter::successors(Some(leaf_hash), |h| Some(get_parent_hash(*h)))
134154
.take(ancestry_len as usize);
155+
156+
let returned_ancestors = assert_matches!(
157+
overseer_recv(virtual_overseer).await,
158+
AllMessages::ChainApi(ChainApiMessage::Ancestors {
159+
k,
160+
response_channel: tx,
161+
..
162+
}) => {
163+
assert_eq!(k, test_state.scheduling_lookahead.saturating_sub(1) as usize);
164+
let hashes: Vec<_> = ancestry_hashes.clone().skip(1).collect();
165+
let returned = hashes.clone();
166+
tx.send(Ok(hashes)).unwrap();
167+
returned
168+
}
169+
);
170+
171+
// fetch_ancestors checks session for each ancestor that was returned
172+
for _ in 0..returned_ancestors.len() {
173+
assert_matches!(
174+
overseer_recv(virtual_overseer).await,
175+
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
176+
_,
177+
RuntimeApiRequest::SessionIndexForChild(tx)
178+
)) => {
179+
tx.send(Ok(test_state.session_index)).unwrap();
180+
}
181+
);
182+
}
183+
135184
let ancestry_numbers = (min_number..=leaf_number).rev();
136185
let ancestry_iter = ancestry_hashes.clone().zip(ancestry_numbers).peekable();
137186

@@ -150,7 +199,18 @@ pub(super) async fn update_view(
150199

151200
let msg = match next_overseer_message.take() {
152201
Some(msg) => msg,
153-
None => overseer_recv(virtual_overseer).await,
202+
None => match overseer_recv_with_timeout(
203+
virtual_overseer,
204+
Duration::from_millis(50),
205+
)
206+
.await
207+
{
208+
Some(msg) => msg,
209+
None => {
210+
// No message arrived - ancestry is cached
211+
break
212+
},
213+
},
154214
};
155215

156216
if !matches!(&msg, AllMessages::ChainApi(ChainApiMessage::BlockHeader(..))) {

polkadot/node/network/statement-distribution/src/v2/tests/mod.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,68 @@ async fn handle_leaf_activation(
600600
claim_queue,
601601
} = leaf;
602602

603+
// activate_leaf calls fetch_ancestors first
604+
assert_matches!(
605+
virtual_overseer.recv().await,
606+
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
607+
_,
608+
RuntimeApiRequest::SessionIndexForChild(tx),
609+
)) => {
610+
tx.send(Ok(*session)).unwrap();
611+
}
612+
);
613+
614+
assert_matches!(
615+
virtual_overseer.recv().await,
616+
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
617+
_,
618+
RuntimeApiRequest::SchedulingLookahead(_, tx),
619+
)) => {
620+
// Assuming scheduling lookahead of 2 for tests
621+
tx.send(Ok(2)).unwrap();
622+
}
623+
);
624+
625+
let ancestors = assert_matches!(
626+
virtual_overseer.recv().await,
627+
AllMessages::ChainApi(ChainApiMessage::Ancestors {
628+
k,
629+
response_channel: tx,
630+
..
631+
}) => {
632+
// Calculate ancestors based on block number
633+
let mut ancestors = Vec::new();
634+
let mut current_hash = *parent_hash;
635+
let mut current_number = *number - 1;
636+
637+
for _ in 0..k {
638+
if current_number == 0 {
639+
break;
640+
}
641+
ancestors.push(current_hash);
642+
// For tests, generate predictable parent hashes
643+
current_hash = Hash::repeat_byte(current_hash.as_ref()[0].wrapping_sub(1));
644+
current_number -= 1;
645+
}
646+
647+
tx.send(Ok(ancestors.clone())).unwrap();
648+
ancestors
649+
}
650+
);
651+
652+
// fetch_ancestors checks session for each returned ancestor
653+
for _ in 0..ancestors.len() {
654+
assert_matches!(
655+
virtual_overseer.recv().await,
656+
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
657+
_,
658+
RuntimeApiRequest::SessionIndexForChild(tx),
659+
)) => {
660+
tx.send(Ok(*session)).unwrap();
661+
}
662+
);
663+
}
664+
603665
let header = Header {
604666
parent_hash: *parent_hash,
605667
number: *number,

0 commit comments

Comments
 (0)