Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 2c9296d

Browse files
authored
Track occupied depth in backing per parachain (#5778)
1 parent 88be445 commit 2c9296d

File tree

3 files changed

+214
-56
lines changed

3 files changed

+214
-56
lines changed

node/core/backing/src/lib.rs

+28-11
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,13 @@ impl ProspectiveParachainsMode {
244244
struct ActiveLeafState {
245245
prospective_parachains_mode: ProspectiveParachainsMode,
246246
/// The candidates seconded at various depths under this active
247-
/// leaf. A candidate can only be seconded when its hypothetical
248-
/// depth under every active leaf has an empty entry in this map.
247+
/// leaf with respect to parachain id. A candidate can only be
248+
/// seconded when its hypothetical depth under every active leaf
249+
/// has an empty entry in this map.
249250
///
250251
/// When prospective parachains are disabled, the only depth
251252
/// which is allowed is 0.
252-
seconded_at_depth: BTreeMap<usize, CandidateHash>,
253+
seconded_at_depth: HashMap<ParaId, BTreeMap<usize, CandidateHash>>,
253254
}
254255

255256
/// The state of the subsystem.
@@ -869,7 +870,7 @@ async fn handle_active_leaves_update<Context>(
869870
// when prospective parachains are disabled is the leaf hash and 0,
870871
// respectively. We've just learned about the leaf hash, so we cannot
871872
// have any candidates seconded with it as a relay-parent yet.
872-
seconded_at_depth: BTreeMap::new(),
873+
seconded_at_depth: HashMap::new(),
873874
},
874875
);
875876

@@ -895,7 +896,8 @@ async fn handle_active_leaves_update<Context>(
895896

896897
for (candidate_hash, para_id) in remaining_seconded {
897898
let (tx, rx) = oneshot::channel();
898-
membership_answers.push(rx.map_ok(move |membership| (candidate_hash, membership)));
899+
membership_answers
900+
.push(rx.map_ok(move |membership| (para_id, candidate_hash, membership)));
899901

900902
ctx.send_message(ProspectiveParachainsMessage::GetTreeMembership(
901903
para_id,
@@ -905,7 +907,7 @@ async fn handle_active_leaves_update<Context>(
905907
.await;
906908
}
907909

908-
let mut seconded_at_depth = BTreeMap::new();
910+
let mut seconded_at_depth = HashMap::new();
909911
for response in membership_answers.next().await {
910912
match response {
911913
Err(oneshot::Canceled) => {
@@ -916,15 +918,17 @@ async fn handle_active_leaves_update<Context>(
916918

917919
continue
918920
},
919-
Ok((candidate_hash, membership)) => {
921+
Ok((para_id, candidate_hash, membership)) => {
920922
// This request gives membership in all fragment trees. We have some
921923
// wasted data here, and it can be optimized if it proves
922924
// relevant to performance.
923925
if let Some((_, depths)) =
924926
membership.into_iter().find(|(leaf_hash, _)| leaf_hash == &leaf.hash)
925927
{
928+
let para_entry: &mut BTreeMap<usize, CandidateHash> =
929+
seconded_at_depth.entry(para_id).or_default();
926930
for depth in depths {
927-
seconded_at_depth.insert(depth, candidate_hash);
931+
para_entry.insert(depth, candidate_hash);
928932
}
929933
}
930934
},
@@ -1163,7 +1167,11 @@ async fn seconding_sanity_check<Context>(
11631167
responses.push(rx.map_ok(move |depths| (depths, head, leaf_state)).boxed());
11641168
} else {
11651169
if head == &candidate_relay_parent {
1166-
if leaf_state.seconded_at_depth.contains_key(&0) {
1170+
if leaf_state
1171+
.seconded_at_depth
1172+
.get(&candidate_para)
1173+
.map_or(false, |occupied| occupied.contains_key(&0))
1174+
{
11671175
// The leaf is already occupied.
11681176
return SecondingAllowed::No
11691177
}
@@ -1188,7 +1196,11 @@ async fn seconding_sanity_check<Context>(
11881196
},
11891197
Ok((depths, head, leaf_state)) => {
11901198
for depth in &depths {
1191-
if leaf_state.seconded_at_depth.contains_key(&depth) {
1199+
if leaf_state
1200+
.seconded_at_depth
1201+
.get(&candidate_para)
1202+
.map_or(false, |occupied| occupied.contains_key(&depth))
1203+
{
11921204
gum::debug!(
11931205
target: LOG_TARGET,
11941206
?candidate_hash,
@@ -1323,8 +1335,13 @@ async fn handle_validated_candidate_command<Context>(
13231335
Some(d) => d,
13241336
};
13251337

1338+
let seconded_at_depth = leaf_data
1339+
.seconded_at_depth
1340+
.entry(candidate.descriptor().para_id)
1341+
.or_default();
1342+
13261343
for depth in depths {
1327-
leaf_data.seconded_at_depth.insert(depth, candidate_hash);
1344+
seconded_at_depth.insert(depth, candidate_hash);
13281345
}
13291346
}
13301347

node/core/backing/src/tests/mod.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ use polkadot_node_subsystem::{
3232
};
3333
use polkadot_node_subsystem_test_helpers as test_helpers;
3434
use polkadot_primitives::v2::{
35-
CandidateDescriptor, CollatorId, GroupRotationInfo, HeadData, PersistedValidationData,
36-
ScheduledCore,
35+
CandidateDescriptor, GroupRotationInfo, HeadData, PersistedValidationData, ScheduledCore,
3736
};
3837
use sp_application_crypto::AppKey;
3938
use sp_keyring::Sr25519Keyring;
@@ -90,9 +89,8 @@ impl Default for TestState {
9089
fn default() -> Self {
9190
let chain_a = ParaId::from(1);
9291
let chain_b = ParaId::from(2);
93-
let thread_a = ParaId::from(3);
9492

95-
let chain_ids = vec![chain_a, chain_b, thread_a];
93+
let chain_ids = vec![chain_a, chain_b];
9694

9795
let validators = vec![
9896
Sr25519Keyring::Alice,
@@ -114,25 +112,21 @@ impl Default for TestState {
114112

115113
let validator_public = validator_pubkeys(&validators);
116114

117-
let validator_groups = vec![vec![2, 0, 3, 5], vec![1], vec![4]]
115+
let validator_groups = vec![vec![2, 0, 3, 5], vec![1]]
118116
.into_iter()
119117
.map(|g| g.into_iter().map(ValidatorIndex).collect())
120118
.collect();
121119
let group_rotation_info =
122120
GroupRotationInfo { session_start_block: 0, group_rotation_frequency: 100, now: 1 };
123121

124-
let thread_collator: CollatorId = Sr25519Keyring::Two.public().into();
125122
let availability_cores = vec![
126123
CoreState::Scheduled(ScheduledCore { para_id: chain_a, collator: None }),
127124
CoreState::Scheduled(ScheduledCore { para_id: chain_b, collator: None }),
128-
CoreState::Scheduled(ScheduledCore {
129-
para_id: thread_a,
130-
collator: Some(thread_collator.clone()),
131-
}),
132125
];
133126

134127
let mut head_data = HashMap::new();
135128
head_data.insert(chain_a, HeadData(vec![4, 5, 6]));
129+
head_data.insert(chain_b, HeadData(vec![5, 6, 7]));
136130

137131
let relay_parent = Hash::repeat_byte(5);
138132

0 commit comments

Comments
 (0)