Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collator protocol: validate descriptor version on the validator side #6011

Merged
merged 142 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
142 commits
Select commit Hold shift + click to select a range
793142e
WIP primitives
sandreim Jul 17, 2024
3a29fdf
WIP
sandreim Aug 5, 2024
4a53577
Working version.
sandreim Aug 7, 2024
8285ae7
Better version
sandreim Aug 8, 2024
2831c5e
Add missing primitives and fix things
sandreim Aug 8, 2024
c767d60
Implement v2 receipts in polkadot-runtime-parachains
sandreim Aug 8, 2024
96999e3
add missing stuff
sandreim Aug 12, 2024
c5f2dc3
Switch parachains runtime to use new primitives
sandreim Aug 12, 2024
dbb0160
use vstaging primitives
sandreim Aug 12, 2024
5efab68
update rococo and westend
sandreim Aug 12, 2024
c2232e4
client keeps using the old primitives
sandreim Aug 12, 2024
87b079f
no unsafe pls
sandreim Aug 12, 2024
00e8c13
move async backing primtiives to own file
sandreim Aug 12, 2024
cd4d02f
fix
sandreim Aug 12, 2024
5509e33
fix test build
sandreim Aug 12, 2024
f8b86d2
fix test-runtime
sandreim Aug 12, 2024
fe2fbfb
self review feedback
sandreim Aug 13, 2024
975e13b
review feedback
sandreim Aug 13, 2024
1c7ac55
feedback
sandreim Aug 13, 2024
653873b
feedback
sandreim Aug 13, 2024
dc98149
clippy
sandreim Aug 13, 2024
0a6bce3
chores
sandreim Aug 13, 2024
5e4dac2
Filter v2 candidate descriptors
sandreim Aug 14, 2024
f12ca7a
fix
sandreim Aug 14, 2024
13734de
fix prospective parachains tests
sandreim Aug 14, 2024
effb1cc
fix fix
sandreim Aug 14, 2024
3f75cba
fmt
sandreim Aug 14, 2024
75a47bb
fix comment
sandreim Aug 14, 2024
12ed853
another one
sandreim Aug 14, 2024
f2c0882
fix build
sandreim Aug 15, 2024
768e034
.
sandreim Aug 15, 2024
4bf0706
improve test and add comment
sandreim Aug 15, 2024
0c83201
add log
sandreim Aug 15, 2024
4296942
simplify check()
sandreim Aug 19, 2024
e1a7509
Merge branch 'sandreim/rfc103-primitives' of github.com:paritytech/po…
sandreim Aug 20, 2024
6fb7790
impl<H>
sandreim Aug 20, 2024
e6add9c
Merge branch 'sandreim/rfc103-primitives' of github.com:paritytech/po…
sandreim Aug 20, 2024
d0b3961
comment
sandreim Aug 20, 2024
66f7a96
add some tests
sandreim Aug 20, 2024
5c0c919
update
sandreim Aug 20, 2024
38ce589
prdoc
sandreim Aug 21, 2024
9f1d611
can't be happy if CI is sad
sandreim Aug 21, 2024
a6a7329
Merge branch 'master' of github.com:paritytech/polkadot-sdk into sand…
sandreim Aug 21, 2024
663817d
remove newlines
sandreim Aug 21, 2024
a1dacc1
match rfc 103 reserved field naming
sandreim Aug 21, 2024
33b80ea
remove default cq offset
sandreim Aug 21, 2024
d5b165f
Merge branch 'sandreim/rfc103-primitives' of github.com:paritytech/po…
sandreim Aug 21, 2024
29e4b47
Ignore UMP signals when checking and processing UMP queue
sandreim Aug 16, 2024
ab85fe3
wip
sandreim Aug 20, 2024
7d5636b
refactor a bit
sandreim Aug 20, 2024
2954bba
use descriptor core_index in `map_candidates_to_cores`
sandreim Aug 20, 2024
e7abe8b
nits
sandreim Aug 20, 2024
1db5eb0
Para Inherent: filter v2 candidate descriptors (#5362)
sandreim Aug 22, 2024
cdb49a6
increase test coverage
sandreim Aug 22, 2024
f6f714a
Merge branch 'sandreim/rfc103-primitives' of github.com:paritytech/po…
sandreim Aug 22, 2024
aa925cd
Improve usability of primitives
sandreim Aug 23, 2024
00d7c71
use committed core index if available in v1 receipts
sandreim Aug 23, 2024
af9f561
typo
sandreim Aug 23, 2024
fb2cefb
fix check
sandreim Aug 23, 2024
b53787d
typo
sandreim Aug 23, 2024
e2ef46e
add test for mixed v1 v2 scenario
sandreim Aug 26, 2024
2dfc542
comment
sandreim Aug 26, 2024
a38a243
add ump test
sandreim Aug 26, 2024
da381da
avoid one storage read
sandreim Aug 26, 2024
ca5c618
store claim queue snapshot in allowed relay parent info
sandreim Aug 27, 2024
4266665
check v2 receipts using claim queue snapshots
sandreim Aug 27, 2024
e93b983
typo
sandreim Aug 27, 2024
e01bf53
it was a bad idea to process commitments of v1 receipts
sandreim Aug 28, 2024
fb9fbe6
fmt
sandreim Aug 28, 2024
c507488
remove unused
sandreim Aug 28, 2024
178e201
Validate session index
sandreim Aug 28, 2024
984e8e1
add unknown version
sandreim Aug 29, 2024
fab215d
add check for unknown version and test
sandreim Aug 29, 2024
7300552
Merge branch 'sandreim/rfc103-primitives' of github.com:paritytech/po…
sandreim Aug 29, 2024
9bbe2cc
typo
sandreim Aug 29, 2024
4dda9df
adjust comments
sandreim Aug 29, 2024
cd3eb5f
Merge branch 'master' of github.com:paritytech/polkadot-sdk into sand…
sandreim Aug 30, 2024
f8ef4ce
fix merge damage
sandreim Aug 30, 2024
04e31a1
unused
sandreim Aug 30, 2024
5fd1279
fix
sandreim Aug 30, 2024
19d6f32
fix benchmark build
sandreim Sep 2, 2024
552078a
Merge branch 'sandreim/rfc103-primitives' of github.com:paritytech/po…
sandreim Sep 2, 2024
4ec3fc8
typos
sandreim Sep 2, 2024
2ba0a27
fmt
sandreim Sep 2, 2024
e468d62
fix comment
sandreim Sep 2, 2024
3fe368f
Merge branch 'master' of github.com:paritytech/polkadot-sdk into sand…
sandreim Sep 3, 2024
18a0496
mixed v1, v2, v2 without select core tests,
sandreim Sep 4, 2024
d320269
Add allowed relay parents storage migration
sandreim Sep 4, 2024
8490488
fix migration
sandreim Sep 5, 2024
db67486
fix
sandreim Sep 5, 2024
03cf8c1
clippy
sandreim Sep 5, 2024
43f6de7
feedback
sandreim Sep 5, 2024
70e48d2
sir, make it faster
sandreim Sep 5, 2024
1e26c73
fix
sandreim Sep 5, 2024
f4e3fb5
one last fix
sandreim Sep 5, 2024
2e87ad3
fixes
sandreim Sep 5, 2024
54432be
remove println
sandreim Sep 5, 2024
cfbecb0
add prdoc
sandreim Sep 6, 2024
3a518f2
fix comment
sandreim Sep 6, 2024
54106e2
refactor map_candidates_to_cores
sandreim Sep 6, 2024
b44a604
doc updates
sandreim Sep 9, 2024
4c5c707
Merge branch 'master' of github.com:paritytech/polkadot-sdk into sand…
sandreim Sep 9, 2024
0cc2e6c
switch nodes to vstaging primitives
sandreim Sep 11, 2024
f9ddb15
test fixes
sandreim Sep 11, 2024
add4592
fmt
sandreim Sep 11, 2024
ef51144
extract test helpers in MutateDescriptorV2
sandreim Sep 12, 2024
3981425
fix tests
sandreim Sep 12, 2024
32af174
more fixes
sandreim Sep 12, 2024
9eba1ef
more wip
sandreim Sep 13, 2024
caff543
feedback
sandreim Sep 13, 2024
218f530
refactor
sandreim Sep 13, 2024
216937a
fix try-runtime
sandreim Sep 13, 2024
e169d0d
Finally all tests compile
sandreim Sep 16, 2024
ceac8b2
:facepalm:
sandreim Sep 16, 2024
c0aee8c
check ump signal count and test
sandreim Sep 16, 2024
1ef7952
remove unused
sandreim Sep 18, 2024
5790b8e
fix prdoc
sandreim Sep 19, 2024
ba9d3ff
more tests cases
sandreim Sep 19, 2024
888880f
Merge branch 'sandreim/runtime_v2_descriptor_support' into sandreim/n…
sandreim Sep 19, 2024
0dcf739
workaround #64
sandreim Sep 20, 2024
604273b
stricter UMP signal checks and tests
sandreim Sep 23, 2024
9c4e2ae
stricter UMP signal checks and tests
sandreim Sep 23, 2024
43bbb9d
type alias
sandreim Sep 24, 2024
64febf4
Merge branch 'sandreim/runtime_v2_descriptor_support' of github.com:p…
sandreim Sep 24, 2024
a493225
remove unused
sandreim Sep 24, 2024
d7e57fd
Merge branch 'master' into sandreim/runtime_v2_descriptor_support
sandreim Sep 24, 2024
7e3a943
Merge branch 'sandreim/runtime_v2_descriptor_support' of github.com:p…
sandreim Sep 24, 2024
7352577
add prdoc
sandreim Sep 24, 2024
c9e44d1
fix merge damage
sandreim Sep 24, 2024
bf948a7
fix
sandreim Sep 24, 2024
fd9dfee
fix some builds
sandreim Sep 26, 2024
5bd465b
fix the fix
sandreim Sep 26, 2024
53bb5b8
toml fixes
sandreim Sep 27, 2024
6a2641a
collator protocol: validate descriptor version on the validator side
alindima Oct 10, 2024
c3aa77d
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Oct 28, 2024
a6eb4d8
oops
alindima Oct 28, 2024
d52a30c
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Nov 5, 2024
2625a0a
fix existing tests
alindima Nov 5, 2024
c1418a2
add prdoc
alindima Nov 5, 2024
5e9dfbd
feedback
alindima Nov 5, 2024
dcc3544
add more tests
alindima Nov 5, 2024
6fc8bd0
clippy
alindima Nov 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion polkadot/node/network/collator-protocol/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use polkadot_node_network_protocol::request_response::incoming;
use polkadot_node_primitives::UncheckedSignedFullStatement;
use polkadot_node_subsystem::{errors::SubsystemError, RuntimeApiError};
use polkadot_node_subsystem_util::{backing_implicit_view, runtime};
use polkadot_primitives::vstaging::CandidateDescriptorVersion;

use crate::LOG_TARGET;

Expand Down Expand Up @@ -63,6 +64,12 @@ pub enum Error {

#[error("CollationSeconded contained statement with invalid signature")]
InvalidStatementSignature(UncheckedSignedFullStatement),

#[error("Response receiver for session index request cancelled")]
CancelledSessionIndex(oneshot::Canceled),

#[error("Response receiver for claim queue request cancelled")]
CancelledClaimQueue(oneshot::Canceled),
}

/// An error happened on the validator side of the protocol when attempting
Expand All @@ -87,11 +94,23 @@ pub enum SecondingError {
#[error("Candidate hash doesn't match the advertisement")]
CandidateHashMismatch,

#[error("Relay parent hash doesn't match the advertisement")]
RelayParentMismatch,

#[error("Received duplicate collation from the peer")]
Duplicate,

#[error("The provided parent head data does not match the hash")]
ParentHeadDataMismatch,

#[error("Core index {0} present in descriptor is different than the assigned core {1}")]
InvalidCoreIndex(u32, u32),

#[error("Session index {0} present in descriptor is different than the expected one {1}")]
InvalidSessionIndex(u32, u32),

#[error("Invalid candidate receipt version {0:?}")]
InvalidReceiptVersion(CandidateDescriptorVersion),
}

impl SecondingError {
Expand All @@ -102,7 +121,11 @@ impl SecondingError {
self,
PersistedValidationDataMismatch |
CandidateHashMismatch |
Duplicate | ParentHeadDataMismatch
RelayParentMismatch |
Duplicate | ParentHeadDataMismatch |
InvalidCoreIndex(_, _) |
InvalidSessionIndex(_, _) |
InvalidReceiptVersion(_)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,32 @@ pub struct BlockedCollationId {
}

/// Performs a sanity check between advertised and fetched collations.
///
/// Since the persisted validation data is constructed using the advertised
/// parent head data hash, the latter doesn't require an additional check.
pub fn fetched_collation_sanity_check(
advertised: &PendingCollation,
fetched: &CandidateReceipt,
persisted_validation_data: &PersistedValidationData,
maybe_parent_head_and_hash: Option<(HeadData, Hash)>,
) -> Result<(), SecondingError> {
if persisted_validation_data.hash() != fetched.descriptor().persisted_validation_data_hash() {
Err(SecondingError::PersistedValidationDataMismatch)
} else if advertised
return Err(SecondingError::PersistedValidationDataMismatch)
}

if advertised
.prospective_candidate
.map_or(false, |pc| pc.candidate_hash() != fetched.hash())
{
Err(SecondingError::CandidateHashMismatch)
} else if maybe_parent_head_and_hash.map_or(false, |(head, hash)| head.hash() != hash) {
Err(SecondingError::ParentHeadDataMismatch)
} else {
Ok(())
return Err(SecondingError::CandidateHashMismatch)
}

if advertised.relay_parent != fetched.descriptor.relay_parent() {
return Err(SecondingError::RelayParentMismatch)
}

if maybe_parent_head_and_hash.map_or(false, |(head, hash)| head.hash() != hash) {
return Err(SecondingError::ParentHeadDataMismatch)
}

Ok(())
}

/// Identifier for a requested collation and the respective collator that advertised it.
Expand Down
159 changes: 107 additions & 52 deletions polkadot/node/network/collator-protocol/src/validator_side/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ use polkadot_node_subsystem::{
use polkadot_node_subsystem_util::{
backing_implicit_view::View as ImplicitView,
reputation::{ReputationAggregator, REPUTATION_CHANGE_INTERVAL},
runtime::{fetch_claim_queue, prospective_parachains_mode, ProspectiveParachainsMode},
request_claim_queue, request_session_index_for_child,
runtime::{prospective_parachains_mode, request_node_features, ProspectiveParachainsMode},
};
use polkadot_primitives::{
vstaging::CoreState, CandidateHash, CollatorId, Hash, HeadData, Id as ParaId,
OccupiedCoreAssumption, PersistedValidationData,
node_features,
vstaging::{CandidateDescriptorV2, CandidateDescriptorVersion, CoreState},
CandidateHash, CollatorId, CoreIndex, Hash, HeadData, Id as ParaId, OccupiedCoreAssumption,
PersistedValidationData, SessionIndex,
};

use crate::error::{Error, FetchError, Result, SecondingError};
Expand Down Expand Up @@ -369,16 +372,9 @@ struct PerRelayParent {
prospective_parachains_mode: ProspectiveParachainsMode,
assignment: GroupAssignments,
collations: Collations,
}

impl PerRelayParent {
fn new(mode: ProspectiveParachainsMode) -> Self {
Self {
prospective_parachains_mode: mode,
assignment: GroupAssignments { current: vec![] },
collations: Collations::default(),
}
}
v2_receipts: bool,
current_core: CoreIndex,
session_index: SessionIndex,
}

/// All state relevant for the validator side of the protocol lives here.
Expand Down Expand Up @@ -460,14 +456,15 @@ fn is_relay_parent_in_implicit_view(
}
}

async fn assign_incoming<Sender>(
async fn construct_per_relay_parent<Sender>(
sender: &mut Sender,
group_assignment: &mut GroupAssignments,
current_assignments: &mut HashMap<ParaId, usize>,
keystore: &KeystorePtr,
relay_parent: Hash,
relay_parent_mode: ProspectiveParachainsMode,
) -> Result<()>
v2_receipts: bool,
session_index: SessionIndex,
) -> Result<Option<PerRelayParent>>
where
Sender: CollatorProtocolSenderTrait,
{
Expand All @@ -494,25 +491,25 @@ where
rotation_info.core_for_group(group, cores.len())
} else {
gum::trace!(target: LOG_TARGET, ?relay_parent, "Not a validator");
return Ok(())
return Ok(None)
};

let paras_now = match fetch_claim_queue(sender, relay_parent).await.map_err(Error::Runtime)? {
// Runtime supports claim queue - use it
//
// `relay_parent_mode` is not examined here because if the runtime supports claim queue
// then it supports async backing params too (`ASYNC_BACKING_STATE_RUNTIME_REQUIREMENT`
// < `CLAIM_QUEUE_RUNTIME_REQUIREMENT`).
Some(mut claim_queue) => claim_queue.0.remove(&core_now),
// Claim queue is not supported by the runtime - use availability cores instead.
None => cores.get(core_now.0 as usize).and_then(|c| match c {
CoreState::Occupied(core) if relay_parent_mode.is_enabled() =>
core.next_up_on_available.as_ref().map(|c| [c.para_id].into_iter().collect()),
CoreState::Scheduled(core) => Some([core.para_id].into_iter().collect()),
CoreState::Occupied(_) | CoreState::Free => None,
}),
}
.unwrap_or_else(|| VecDeque::new());
let claim_queue = request_claim_queue(relay_parent, sender)
.await
.await
.map_err(Error::CancelledClaimQueue)??;

let paras_now = cores
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be scheduled_paras or something implying 'paras that are about to be scheduled on this core' but don't bother fixing it now. I'll handle it when merging your work in #6011

.get(core_now.0 as usize)
.and_then(|c| match (c, relay_parent_mode) {
(CoreState::Occupied(_), ProspectiveParachainsMode::Disabled) => None,
(
CoreState::Occupied(_),
ProspectiveParachainsMode::Enabled { max_candidate_depth: 0, .. },
) => None,
_ => claim_queue.get(&core_now).cloned(),
})
.unwrap_or_else(|| VecDeque::new());

for para_id in paras_now.iter() {
let entry = current_assignments.entry(*para_id).or_default();
Expand All @@ -527,9 +524,14 @@ where
}
}

*group_assignment = GroupAssignments { current: paras_now.into_iter().collect() };

Ok(())
Ok(Some(PerRelayParent {
prospective_parachains_mode: relay_parent_mode,
assignment: GroupAssignments { current: paras_now.into_iter().collect() },
collations: Collations::default(),
v2_receipts,
session_index,
current_core: core_now,
}))
}

fn remove_outgoing(
Expand Down Expand Up @@ -1249,18 +1251,31 @@ where
let added = view.iter().filter(|h| !current_leaves.contains_key(h));

for leaf in added {
let session_index = request_session_index_for_child(*leaf, sender)
.await
.await
.map_err(Error::CancelledSessionIndex)??;
let mode = prospective_parachains_mode(sender, *leaf).await?;

let mut per_relay_parent = PerRelayParent::new(mode);
assign_incoming(
let v2_receipts = request_node_features(*leaf, session_index, sender)
.await?
.unwrap_or_default()
.get(node_features::FeatureIndex::CandidateReceiptV2 as usize)
.map(|b| *b)
.unwrap_or(false);

let Some(per_relay_parent) = construct_per_relay_parent(
sender,
&mut per_relay_parent.assignment,
&mut state.current_assignments,
keystore,
*leaf,
mode,
v2_receipts,
session_index,
)
.await?;
.await?
else {
continue
};

state.active_leaves.insert(*leaf, mode);
state.per_relay_parent.insert(*leaf, per_relay_parent);
Expand All @@ -1279,18 +1294,21 @@ where
.unwrap_or_default();
for block_hash in allowed_ancestry {
if let Entry::Vacant(entry) = state.per_relay_parent.entry(*block_hash) {
let mut per_relay_parent = PerRelayParent::new(mode);
assign_incoming(
// Safe to use the same v2 receipts config for the allowed relay parents as well
// as the same session index since they must be in the same session.
if let Some(per_relay_parent) = construct_per_relay_parent(
sender,
&mut per_relay_parent.assignment,
&mut state.current_assignments,
keystore,
*block_hash,
mode,
v2_receipts,
session_index,
)
.await?;

entry.insert(per_relay_parent);
.await?
{
entry.insert(per_relay_parent);
}
}
}
}
Expand Down Expand Up @@ -1621,11 +1639,10 @@ async fn run_inner<Context>(
Ok(FromOrchestra::Signal(OverseerSignal::Conclude)) | Err(_) => break,
Ok(FromOrchestra::Signal(_)) => continue,
}
}
},
_ = next_inactivity_stream.next() => {
disconnect_inactive_peers(ctx.sender(), &eviction_policy, &state.peer_data).await;
}

},
resp = state.collation_requests.select_next_some() => {
let res = match handle_collation_fetch_response(
&mut state,
Expand Down Expand Up @@ -1684,7 +1701,7 @@ async fn run_inner<Context>(
}
Ok(true) => {}
}
}
},
res = state.collation_fetch_timeouts.select_next_some() => {
let (collator_id, maybe_candidate_hash, relay_parent) = res;
gum::debug!(
Expand Down Expand Up @@ -1814,6 +1831,10 @@ async fn kick_off_seconding<Context>(
return Ok(false)
},
};

// Sanity check of the candidate receipt version.
descriptor_version_sanity_check(candidate_receipt.descriptor(), per_relay_parent)?;

let collations = &mut per_relay_parent.collations;

let fetched_collation = FetchedCollation::from(&candidate_receipt);
Expand Down Expand Up @@ -2024,7 +2045,9 @@ async fn handle_collation_fetch_response(
},
Ok(
request_v1::CollationFetchingResponse::Collation(receipt, _) |
request_v1::CollationFetchingResponse::CollationWithParentHeadData { receipt, .. },
request_v2::CollationFetchingResponse::Collation(receipt, _) |
request_v1::CollationFetchingResponse::CollationWithParentHeadData { receipt, .. } |
request_v2::CollationFetchingResponse::CollationWithParentHeadData { receipt, .. },
) if receipt.descriptor().para_id() != pending_collation.para_id => {
gum::debug!(
target: LOG_TARGET,
Expand Down Expand Up @@ -2086,3 +2109,35 @@ async fn handle_collation_fetch_response(
state.metrics.on_request(metrics_result);
result
}

// Sanity check the candidate descriptor version.
fn descriptor_version_sanity_check(
descriptor: &CandidateDescriptorV2,
per_relay_parent: &PerRelayParent,
) -> std::result::Result<(), SecondingError> {
match descriptor.version() {
CandidateDescriptorVersion::V1 => Ok(()),
CandidateDescriptorVersion::V2 if per_relay_parent.v2_receipts => {
if let Some(core_index) = descriptor.core_index() {
if core_index != per_relay_parent.current_core {
return Err(SecondingError::InvalidCoreIndex(
core_index.0,
per_relay_parent.current_core.0,
))
}
}

if let Some(session_index) = descriptor.session_index() {
if session_index != per_relay_parent.session_index {
return Err(SecondingError::InvalidSessionIndex(
session_index,
per_relay_parent.session_index,
))
}
}

Ok(())
},
descriptor_version => Err(SecondingError::InvalidReceiptVersion(descriptor_version)),
}
}
Loading
Loading