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

Use strong types in runtime for parachain heads and validation code #964

Merged
merged 5 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 0 additions & 3 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions network/src/protocol/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use polkadot_primitives::parachain::{
Id as ParaId, Chain, DutyRoster, ParachainHost, ValidatorId,
Retriable, CollatorId, AbridgedCandidateReceipt,
GlobalValidationSchedule, LocalValidationData, ErasureChunk, SigningContext,
PoVBlock, BlockData,
PoVBlock, BlockData, ValidationCode,
};
use polkadot_validation::{SharedTable, TableRouter};

Expand Down Expand Up @@ -163,8 +163,8 @@ sp_api::mock_impl_runtime_apis! {
self.data.lock().active_parachains.clone()
}

fn parachain_code(_: ParaId) -> Option<Vec<u8>> {
Some(Vec::new())
fn parachain_code(_: ParaId) -> Option<ValidationCode> {
Some(ValidationCode(Vec::new()))
}

fn global_validation_schedule() -> GlobalValidationSchedule {
Expand Down
20 changes: 16 additions & 4 deletions parachain/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,27 @@ use sp_core::bytes;
pub type RelayChainBlockNumber = u32;

/// Parachain head data included in the chain.
#[derive(PartialEq, Eq, Clone, PartialOrd, Ord, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug, Default))]
#[derive(PartialEq, Eq, Clone, PartialOrd, Ord, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Default))]
pub struct HeadData(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);

impl From<Vec<u8>> for HeadData {
fn from(head: Vec<u8>) -> Self {
HeadData(head)
}
}

/// Parachain validation code.
#[derive(Default, PartialEq, Eq, Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Default, PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct ValidationCode(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);

impl From<Vec<u8>> for ValidationCode {
fn from(code: Vec<u8>) -> Self {
ValidationCode(code)
}
}

/// Parachain block data.
///
/// Contains everything required to validate para-block, may contain block and witness data.
Expand Down
4 changes: 2 additions & 2 deletions primitives/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub struct CandidateCommitments {
/// The root of a block's erasure encoding Merkle tree.
pub erasure_root: Hash,
/// New validation code.
pub new_validation_code: Option<Vec<u8>>,
pub new_validation_code: Option<ValidationCode>,
}

/// Get a collator signature payload on a relay-parent, block-data combo.
Expand Down Expand Up @@ -672,7 +672,7 @@ sp_api::decl_runtime_apis! {
/// Get the local validation data for a particular parachain.
fn local_validation_data(id: Id) -> Option<LocalValidationData>;
/// Get the given parachain's head code blob.
fn parachain_code(id: Id) -> Option<Vec<u8>>;
fn parachain_code(id: Id) -> Option<ValidationCode>;
/// Extract the abridged head that was set in the extrinsics.
fn get_heads(extrinsics: Vec<<Block as BlockT>::Extrinsic>)
-> Option<Vec<AbridgedCandidateReceipt>>;
Expand Down
2 changes: 0 additions & 2 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ offences = { package = "pallet-offences", git = "https:/paritytech/s
frame-benchmarking = { git = "https:/paritytech/substrate", branch = "master", default-features = false, optional = true }

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

[dev-dependencies]
Expand All @@ -57,7 +56,6 @@ std = [
"codec/std",
"inherents/std",
"sp-core/std",
"polkadot-parachain/std",
"sp-api/std",
"sp-std/std",
"sp-io/std",
Expand Down
42 changes: 21 additions & 21 deletions runtime/common/src/crowdfund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use crate::slots;
use codec::{Encode, Decode};
use sp_std::vec::Vec;
use sp_core::storage::well_known_keys::CHILD_STORAGE_KEY_PREFIX;
use primitives::parachain::Id as ParaId;
use primitives::parachain::{Id as ParaId, HeadData};

const MODULE_ID: ModuleId = ModuleId(*b"py/cfund");

Expand Down Expand Up @@ -124,7 +124,7 @@ pub enum LastContribution<BlockNumber> {
struct DeployData<Hash> {
code_hash: Hash,
code_size: u32,
initial_head_data: Vec<u8>,
initial_head_data: HeadData,
}

#[derive(Encode, Decode, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -358,7 +358,7 @@ decl_module! {
#[compact] index: FundIndex,
code_hash: T::Hash,
code_size: u32,
initial_head_data: Vec<u8>
initial_head_data: HeadData,
) {
let who = ensure_signed(origin)?;

Expand Down Expand Up @@ -582,7 +582,7 @@ mod tests {
};
use frame_support::traits::Contains;
use sp_core::H256;
use primitives::parachain::{Info as ParaInfo, Id as ParaId, Scheduling};
use primitives::parachain::{Info as ParaInfo, Id as ParaId, Scheduling, ValidationCode};
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are requried.
use sp_runtime::{
Expand Down Expand Up @@ -675,7 +675,7 @@ mod tests {
thread_local! {
pub static PARACHAIN_COUNT: RefCell<u32> = RefCell::new(0);
pub static PARACHAINS:
RefCell<HashMap<u32, (Vec<u8>, Vec<u8>)>> = RefCell::new(HashMap::new());
RefCell<HashMap<u32, (ValidationCode, HeadData)>> = RefCell::new(HashMap::new());
}

const MAX_CODE_SIZE: u32 = 100;
Expand Down Expand Up @@ -705,8 +705,8 @@ mod tests {
fn register_para(
id: ParaId,
_info: ParaInfo,
code: Vec<u8>,
initial_head_data: Vec<u8>
code: ValidationCode,
initial_head_data: HeadData,
) -> DispatchResult {
PARACHAINS.with(|p| {
if p.borrow().contains_key(&id.into()) {
Expand Down Expand Up @@ -919,7 +919,7 @@ mod tests {
0,
<Test as system::Trait>::Hash::default(),
0,
vec![0]
vec![0].into()
));

let fund = Crowdfund::funds(0).unwrap();
Expand All @@ -930,7 +930,7 @@ mod tests {
Some(DeployData {
code_hash: <Test as system::Trait>::Hash::default(),
code_size: 0,
initial_head_data: vec![0],
initial_head_data: vec![0].into(),
}),
);
});
Expand All @@ -949,7 +949,7 @@ mod tests {
0,
<Test as system::Trait>::Hash::default(),
0,
vec![0]),
vec![0].into()),
Error::<Test>::InvalidOrigin
);

Expand All @@ -959,7 +959,7 @@ mod tests {
1,
<Test as system::Trait>::Hash::default(),
0,
vec![0]),
vec![0].into()),
Error::<Test>::InvalidFundIndex
);

Expand All @@ -969,15 +969,15 @@ mod tests {
0,
<Test as system::Trait>::Hash::default(),
0,
vec![0]
vec![0].into(),
));

assert_noop!(Crowdfund::fix_deploy_data(
Origin::signed(1),
0,
<Test as system::Trait>::Hash::default(),
0,
vec![1]),
vec![1].into()),
Error::<Test>::ExistingDeployData
);
});
Expand All @@ -997,7 +997,7 @@ mod tests {
0,
<Test as system::Trait>::Hash::default(),
0,
vec![0]
vec![0].into(),
));

// Fund crowdfund
Expand Down Expand Up @@ -1043,7 +1043,7 @@ mod tests {
0,
<Test as system::Trait>::Hash::default(),
0,
vec![0]
vec![0].into(),
));

// Cannot onboard fund with incorrect parachain id
Expand Down Expand Up @@ -1071,7 +1071,7 @@ mod tests {
0,
<Test as system::Trait>::Hash::default(),
0,
vec![0]
vec![0].into(),
));

// Fund crowdfund
Expand Down Expand Up @@ -1114,7 +1114,7 @@ mod tests {
0,
<Test as system::Trait>::Hash::default(),
0,
vec![0]
vec![0].into(),
));

// Fund crowdfund
Expand Down Expand Up @@ -1256,7 +1256,7 @@ mod tests {
0,
<Test as system::Trait>::Hash::default(),
0,
vec![0]
vec![0].into(),
));
assert_ok!(Crowdfund::onboard(Origin::signed(1), 0, 0.into()));

Expand Down Expand Up @@ -1285,7 +1285,7 @@ mod tests {
0,
<Test as system::Trait>::Hash::default(),
0,
vec![0]
vec![0].into(),
));
// Move to the end of auction...
run_to_block(12);
Expand Down Expand Up @@ -1324,14 +1324,14 @@ mod tests {
0,
<Test as system::Trait>::Hash::default(),
0,
vec![0]
vec![0].into(),
));
assert_ok!(Crowdfund::fix_deploy_data(
Origin::signed(2),
1,
<Test as system::Trait>::Hash::default(),
0,
vec![0]
vec![0].into(),
));

// End the current auction, fund 0 wins!
Expand Down
Loading