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

Commit

Permalink
HRMP: Acceptance & Enactment
Browse files Browse the repository at this point in the history
  • Loading branch information
pepyakin committed Oct 28, 2020
1 parent 7b7eb41 commit 1427dd1
Show file tree
Hide file tree
Showing 11 changed files with 716 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions runtime/common/src/paras_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ mod tests {

impl router::Trait for Test {
type UmpSink = ();
type Origin = Origin;
}

impl pallet_session::historical::Trait for Test {
Expand Down
2 changes: 2 additions & 0 deletions runtime/parachains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pallet-vesting = { git = "https://github.com/paritytech/substrate", branch = "ma
pallet-offences = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }

xcm = { package = "xcm", path = "../../xcm", default-features = false }
primitives = { package = "polkadot-primitives", path = "../../primitives", default-features = false }
libsecp256k1 = { version = "0.3.2", default-features = false, optional = true }

Expand Down Expand Up @@ -84,6 +85,7 @@ std = [
"frame-system/std",
"pallet-timestamp/std",
"pallet-vesting/std",
"xcm/std",
]
runtime-benchmarks = [
"libsecp256k1/hmac",
Expand Down
60 changes: 60 additions & 0 deletions runtime/parachains/src/inclusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ decl_error! {
IncorrectDownwardMessageHandling,
/// At least one upward message sent does not pass the acceptance criteria.
InvalidUpwardMessages,
/// The candidate didn't follow the rules of HRMP watermark advancement.
HrmpWatermarkMishandling,
/// The HRMP messages sent by the candidate is not valid.
InvalidOutboundHrmp,
}
}

Expand Down Expand Up @@ -415,6 +419,8 @@ impl<T: Trait> Module<T> {
&candidate.candidate.commitments.new_validation_code,
candidate.candidate.commitments.processed_downward_messages,
&candidate.candidate.commitments.upward_messages,
candidate.candidate.commitments.hrmp_watermark,
&candidate.candidate.commitments.horizontal_messages,
)?;

for (i, assignment) in scheduled[skip..].iter().enumerate() {
Expand Down Expand Up @@ -548,6 +554,8 @@ impl<T: Trait> Module<T> {
&validation_outputs.new_validation_code,
validation_outputs.processed_downward_messages,
&validation_outputs.upward_messages,
validation_outputs.hrmp_watermark,
&validation_outputs.horizontal_messages,
)
}

Expand Down Expand Up @@ -578,6 +586,14 @@ impl<T: Trait> Module<T> {
receipt.descriptor.para_id,
commitments.upward_messages,
);
weight += <router::Module<T>>::prune_hrmp(
receipt.descriptor.para_id,
T::BlockNumber::from(commitments.hrmp_watermark),
);
weight += <router::Module<T>>::queue_outbound_hrmp(
receipt.descriptor.para_id,
commitments.horizontal_messages,
);

Self::deposit_event(
Event::<T>::CandidateIncluded(plain, commitments.head_data.clone())
Expand Down Expand Up @@ -702,6 +718,8 @@ impl<T: Trait> CandidateCheckContext<T> {
new_validation_code: &Option<primitives::v1::ValidationCode>,
processed_downward_messages: u32,
upward_messages: &[primitives::v1::UpwardMessage],
hrmp_watermark: primitives::v1::BlockNumber,
horizontal_messages: &[primitives::v1::OutboundHrmpMessage<ParaId>],
) -> Result<(), DispatchError> {
ensure!(
head_data.0.len() <= self.config.max_head_data_size as _,
Expand Down Expand Up @@ -739,6 +757,23 @@ impl<T: Trait> CandidateCheckContext<T> {
),
Error::<T>::InvalidUpwardMessages,
);
ensure!(
<router::Module<T>>::check_hrmp_watermark(
para_id,
self.relay_parent_number,
// TODO: Hmm, we should settle on a single represenation of T::BlockNumber
T::BlockNumber::from(hrmp_watermark),
),
Error::<T>::HrmpWatermarkMishandling,
);
ensure!(
<router::Module<T>>::check_outbound_hrmp(
&self.config,
para_id,
horizontal_messages,
),
Error::<T>::InvalidOutboundHrmp,
);

Ok(())
}
Expand Down Expand Up @@ -946,6 +981,7 @@ mod tests {
relay_parent: Hash,
persisted_validation_data_hash: Hash,
new_validation_code: Option<ValidationCode>,
hrmp_watermark: BlockNumber,
}

impl TestCandidateBuilder {
Expand All @@ -961,6 +997,7 @@ mod tests {
commitments: CandidateCommitments {
head_data: self.head_data,
new_validation_code: self.new_validation_code,
hrmp_watermark: self.hrmp_watermark,
..Default::default()
},
}
Expand Down Expand Up @@ -1359,6 +1396,9 @@ mod tests {
let chain_b = ParaId::from(2);
let thread_a = ParaId::from(3);

// The block number of the relay-parent for testing.
const RELAY_PARENT_NUM: BlockNumber = 4;

let paras = vec![(chain_a, true), (chain_b, true), (thread_a, false)];
let validators = vec![
Sr25519Keyring::Alice,
Expand Down Expand Up @@ -1421,6 +1461,7 @@ mod tests {
relay_parent: System::parent_hash(),
pov_hash: Hash::from([1; 32]),
persisted_validation_data_hash: make_vdata_hash(chain_a).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();
collator_sign_candidate(
Expand Down Expand Up @@ -1454,13 +1495,15 @@ mod tests {
relay_parent: System::parent_hash(),
pov_hash: Hash::from([1; 32]),
persisted_validation_data_hash: make_vdata_hash(chain_a).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();
let mut candidate_b = TestCandidateBuilder {
para_id: chain_b,
relay_parent: System::parent_hash(),
pov_hash: Hash::from([2; 32]),
persisted_validation_data_hash: make_vdata_hash(chain_b).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();

Expand Down Expand Up @@ -1510,6 +1553,7 @@ mod tests {
relay_parent: System::parent_hash(),
pov_hash: Hash::from([1; 32]),
persisted_validation_data_hash: make_vdata_hash(chain_a).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();
collator_sign_candidate(
Expand Down Expand Up @@ -1579,6 +1623,7 @@ mod tests {
relay_parent: System::parent_hash(),
pov_hash: Hash::from([1; 32]),
persisted_validation_data_hash: make_vdata_hash(thread_a).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();

Expand Down Expand Up @@ -1618,6 +1663,7 @@ mod tests {
relay_parent: System::parent_hash(),
pov_hash: Hash::from([1; 32]),
persisted_validation_data_hash: make_vdata_hash(thread_a).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();

Expand Down Expand Up @@ -1656,6 +1702,7 @@ mod tests {
relay_parent: System::parent_hash(),
pov_hash: Hash::from([1; 32]),
persisted_validation_data_hash: make_vdata_hash(chain_a).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();

Expand Down Expand Up @@ -1703,6 +1750,7 @@ mod tests {
relay_parent: System::parent_hash(),
pov_hash: Hash::from([1; 32]),
persisted_validation_data_hash: make_vdata_hash(chain_a).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();

Expand Down Expand Up @@ -1743,6 +1791,7 @@ mod tests {
pov_hash: Hash::from([1; 32]),
new_validation_code: Some(vec![5, 6, 7, 8].into()),
persisted_validation_data_hash: make_vdata_hash(chain_a).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();

Expand Down Expand Up @@ -1785,6 +1834,7 @@ mod tests {
relay_parent: System::parent_hash(),
pov_hash: Hash::from([1; 32]),
persisted_validation_data_hash: [42u8; 32].into(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();

Expand Down Expand Up @@ -1820,6 +1870,9 @@ mod tests {
let chain_b = ParaId::from(2);
let thread_a = ParaId::from(3);

// The block number of the relay-parent for testing.
const RELAY_PARENT_NUM: BlockNumber = 4;

let paras = vec![(chain_a, true), (chain_b, true), (thread_a, false)];
let validators = vec![
Sr25519Keyring::Alice,
Expand Down Expand Up @@ -1880,6 +1933,7 @@ mod tests {
relay_parent: System::parent_hash(),
pov_hash: Hash::from([1; 32]),
persisted_validation_data_hash: make_vdata_hash(chain_a).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();
collator_sign_candidate(
Expand All @@ -1892,6 +1946,7 @@ mod tests {
relay_parent: System::parent_hash(),
pov_hash: Hash::from([2; 32]),
persisted_validation_data_hash: make_vdata_hash(chain_b).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();
collator_sign_candidate(
Expand All @@ -1904,6 +1959,7 @@ mod tests {
relay_parent: System::parent_hash(),
pov_hash: Hash::from([3; 32]),
persisted_validation_data_hash: make_vdata_hash(thread_a).unwrap(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();
collator_sign_candidate(
Expand Down Expand Up @@ -2001,6 +2057,9 @@ mod tests {
fn can_include_candidate_with_ok_code_upgrade() {
let chain_a = ParaId::from(1);

// The block number of the relay-parent for testing.
const RELAY_PARENT_NUM: BlockNumber = 4;

let paras = vec![(chain_a, true)];
let validators = vec![
Sr25519Keyring::Alice,
Expand Down Expand Up @@ -2044,6 +2103,7 @@ mod tests {
pov_hash: Hash::from([1; 32]),
persisted_validation_data_hash: make_vdata_hash(chain_a).unwrap(),
new_validation_code: Some(vec![1, 2, 3].into()),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}.build();
collator_sign_candidate(
Expand Down
1 change: 1 addition & 0 deletions runtime/parachains/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl crate::paras::Trait for Test {
}

impl crate::router::Trait for Test {
type Origin = Origin;
type UmpSink = crate::router::MockUmpSink;
}

Expand Down
6 changes: 6 additions & 0 deletions runtime/parachains/src/paras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,12 @@ impl<T: Trait> Module<T> {
}
}

/// Returns whether the given ID refers to a valid para.
pub(crate) fn is_valid_para(id: ParaId) -> bool {
Self::parachains().binary_search(&id).is_ok()
|| Self::is_parathread(id)
}

/// Whether a para ID corresponds to any live parathread.
pub(crate) fn is_parathread(id: ParaId) -> bool {
Parathreads::get(&id).is_some()
Expand Down
Loading

0 comments on commit 1427dd1

Please sign in to comment.