Update Substrate & Polkadot (#387)

* Update Substrate & Polkadot

* Enforce `ParachainSetCode`
This commit is contained in:
Bastian Köcher
2021-04-02 16:17:04 +02:00
committed by GitHub
parent dc666c7e82
commit 1d4c02cc4e
14 changed files with 957 additions and 635 deletions
Generated
+884 -550
View File
File diff suppressed because it is too large Load Diff
+6 -4
View File
@@ -31,18 +31,19 @@ use sp_runtime::{
use sp_state_machine::InspectState;
use cumulus_client_consensus_common::ParachainConsensus;
use polkadot_node_primitives::{Collation, CollationGenerationConfig, CollationResult};
use polkadot_node_primitives::{
BlockData, Collation, CollationGenerationConfig, CollationResult, PoV,
};
use polkadot_node_subsystem::messages::{CollationGenerationMessage, CollatorProtocolMessage};
use polkadot_overseer::OverseerHandler;
use polkadot_primitives::v1::{
BlockData, BlockNumber as PBlockNumber, CollatorPair, Hash as PHash, HeadData, Id as ParaId,
PoV, UpwardMessage,
BlockNumber as PBlockNumber, CollatorPair, Hash as PHash, HeadData, Id as ParaId, UpwardMessage,
};
use codec::{Decode, Encode};
use futures::{channel::oneshot, FutureExt};
use std::sync::Arc;
use parking_lot::Mutex;
use std::sync::Arc;
use tracing::Instrument;
/// The logging target.
@@ -429,6 +430,7 @@ mod tests {
self.client
.import(BlockOrigin::Own, block.clone())
.await
.expect("Imports the block");
Some(ParachainCandidate {
+18 -17
View File
@@ -204,7 +204,7 @@ where
h,
&*parachain,
&mut unset_best_header,
),
).await,
None => {
tracing::debug!(
target: "cumulus-consensus",
@@ -221,7 +221,7 @@ where
&mut unset_best_header,
&*parachain,
&*announce_block,
),
).await,
None => {
tracing::debug!(
target: "cumulus-consensus",
@@ -236,11 +236,11 @@ where
}
/// Handle a new import block of the parachain.
fn handle_new_block_imported<Block, P>(
async fn handle_new_block_imported<Block, P>(
notification: BlockImportNotification<Block>,
unset_best_header_opt: &mut Option<Block::Header>,
parachain: &P,
announce_block: &dyn Fn(Block::Hash, Option<Vec<u8>>),
announce_block: &(dyn Fn(Block::Hash, Option<Vec<u8>>) + Send + Sync),
) where
Block: BlockT,
P: UsageProvider<Block> + Send + Sync + BlockBackend<Block>,
@@ -280,7 +280,7 @@ fn handle_new_block_imported<Block, P>(
.take()
.expect("We checked above that the value is set; qed");
import_block_as_new_best(unset_hash, unset_best_header, parachain);
import_block_as_new_best(unset_hash, unset_best_header, parachain).await;
}
state => tracing::debug!(
target: "cumulus-consensus",
@@ -293,7 +293,7 @@ fn handle_new_block_imported<Block, P>(
}
/// Handle the new best parachain head as extracted from the new best relay chain.
fn handle_new_best_parachain_head<Block, P>(
async fn handle_new_best_parachain_head<Block, P>(
head: Vec<u8>,
parachain: &P,
unset_best_header: &mut Option<Block::Header>,
@@ -328,7 +328,7 @@ fn handle_new_best_parachain_head<Block, P>(
Ok(BlockStatus::InChainWithState) => {
unset_best_header.take();
import_block_as_new_best(hash, parachain_head, parachain);
import_block_as_new_best(hash, parachain_head, parachain).await;
}
Ok(BlockStatus::InChainPruned) => {
tracing::error!(
@@ -359,11 +359,8 @@ fn handle_new_best_parachain_head<Block, P>(
}
}
fn import_block_as_new_best<Block, P>(
hash: Block::Hash,
header: Block::Header,
parachain: &P,
) where
async fn import_block_as_new_best<Block, P>(hash: Block::Hash, header: Block::Header, parachain: &P)
where
Block: BlockT,
P: UsageProvider<Block> + Send + Sync + BlockBackend<Block>,
for<'a> &'a P: BlockImport<Block>,
@@ -373,7 +370,10 @@ fn import_block_as_new_best<Block, P>(
block_import_params.fork_choice = Some(ForkChoiceStrategy::Custom(true));
block_import_params.import_existing = true;
if let Err(err) = (&*parachain).import_block(block_import_params, Default::default()) {
if let Err(err) = (&*parachain)
.import_block(block_import_params, Default::default())
.await
{
tracing::warn!(
target: "cumulus-consensus",
block_hash = ?hash,
@@ -555,7 +555,9 @@ impl<B: BlockT> ParachainConsensus<B> for Box<dyn ParachainConsensus<B> + Send +
relay_parent: PHash,
validation_data: &PersistedValidationData,
) -> Option<ParachainCandidate<B>> {
(*self).produce_candidate(parent, relay_parent, validation_data).await
(*self)
.produce_candidate(parent, relay_parent, validation_data)
.await
}
}
@@ -653,9 +655,7 @@ mod tests {
block_import_params.fork_choice = Some(ForkChoiceStrategy::Custom(false));
block_import_params.body = Some(body);
client
.import_block(block_import_params, Default::default())
.unwrap();
block_on(client.import_block(block_import_params, Default::default())).unwrap();
assert_eq!(0, client.chain_info().best_number);
block
@@ -865,6 +865,7 @@ mod tests {
// Now import the unkown block to make it "known"
client
.import_block(block_import_params, Default::default())
.await
.unwrap();
loop {
@@ -38,13 +38,14 @@ struct Verifier<Client, Block> {
_marker: PhantomData<Block>,
}
#[async_trait::async_trait]
impl<Client, Block> VerifierT<Block> for Verifier<Client, Block>
where
Block: BlockT,
Client: ProvideRuntimeApi<Block> + Send + Sync,
<Client as ProvideRuntimeApi<Block>>::Api: BlockBuilderApi<Block>,
{
fn verify(
async fn verify(
&mut self,
origin: BlockOrigin,
header: Block::Header,
+4 -2
View File
@@ -62,7 +62,7 @@ pub struct RelayChainConsensus<B, PF, BI, RClient, RBackend> {
_phantom: PhantomData<B>,
proposer_factory: Arc<Mutex<PF>>,
inherent_data_providers: InherentDataProviders,
block_import: Arc<Mutex<BI>>,
block_import: Arc<futures::lock::Mutex<BI>>,
relay_chain_client: Arc<RClient>,
relay_chain_backend: Arc<RBackend>,
}
@@ -101,7 +101,7 @@ where
para_id,
proposer_factory: Arc::new(Mutex::new(proposer_factory)),
inherent_data_providers,
block_import: Arc::new(Mutex::new(block_import)),
block_import: Arc::new(futures::lock::Mutex::new(block_import)),
relay_chain_backend: polkadot_backend,
relay_chain_client: polkadot_client,
_phantom: PhantomData,
@@ -212,7 +212,9 @@ where
if let Err(err) = self
.block_import
.lock()
.await
.import_block(block_import_params, Default::default())
.await
{
tracing::error!(
target: LOG_TARGET,
+10 -1
View File
@@ -321,6 +321,7 @@ fn relay_parent_not_imported_when_block_announce_is_processed() {
client
.import(BlockOrigin::Own, block)
.await
.expect("Imports the block");
assert!(matches!(
@@ -386,7 +387,11 @@ sp_api::mock_impl_runtime_apis! {
Vec::new()
}
fn persisted_validation_data(&self, _: ParaId, _: OccupiedCoreAssumption) -> Option<PersistedValidationData<BlockNumber>> {
fn persisted_validation_data(
&self,
_: ParaId,
_: OccupiedCoreAssumption,
) -> Option<PersistedValidationData<PHash, BlockNumber>> {
Some(PersistedValidationData {
parent_head: HeadData(default_header().encode()),
..Default::default()
@@ -430,5 +435,9 @@ sp_api::mock_impl_runtime_apis! {
) -> BTreeMap<ParaId, Vec<InboundHrmpMessage<BlockNumber>>> {
BTreeMap::new()
}
fn validation_code_by_hash(_: PHash) -> Option<ValidationCode> {
None
}
}
}
@@ -165,9 +165,7 @@ mod tests {
let (mut client, backend, block) = build_client_backend_and_block();
let hash = block.hash();
client
.import(BlockOrigin::Own, block)
.expect("Imports the block");
block_on(client.import(BlockOrigin::Own, block)).expect("Imports the block");
let wait = WaitOnRelayChainBlock::new(backend, client);
@@ -195,6 +193,7 @@ mod tests {
// Import the block that should fire the notification
client
.import(BlockOrigin::Own, block)
.await
.expect("Imports the block");
// Now it should have received the notification and report that the block was imported
@@ -246,6 +245,7 @@ mod tests {
// Import the block that should fire the notification
client
.import(BlockOrigin::Own, block2)
.await
.expect("Imports the second block");
// The import notification of the second block should not make this one finish
@@ -255,6 +255,7 @@ mod tests {
client
.import(BlockOrigin::Own, block)
.await
.expect("Imports the first block");
// Now it should be ready
+3 -3
View File
@@ -21,7 +21,7 @@
use cumulus_client_consensus_common::ParachainConsensus;
use cumulus_primitives_core::ParaId;
use futures::FutureExt;
use polkadot_primitives::v1::{Block as PBlock, CollatorId, CollatorPair};
use polkadot_primitives::v1::{Block as PBlock, CollatorPair};
use polkadot_service::{AbstractClient, Client as PClient, ClientHandle, RuntimeApiCollection};
use sc_client_api::{
Backend as BackendT, BlockBackend, BlockchainEvents, Finalizer, UsageProvider,
@@ -233,7 +233,7 @@ pub fn prepare_node_config(mut parachain_config: Configuration) -> Configuration
#[sc_tracing::logging::prefix_logs_with("Relaychain")]
pub fn build_polkadot_full_node(
config: Configuration,
collator_id: CollatorId,
collator_pair: CollatorPair,
telemetry_worker_handle: Option<TelemetryWorkerHandle>,
) -> Result<RFullNode<PClient>, polkadot_service::Error> {
let is_light = matches!(config.role, Role::Light);
@@ -244,7 +244,7 @@ pub fn build_polkadot_full_node(
} else {
polkadot_service::build_full(
config,
polkadot_service::IsCollator::Yes(collator_id),
polkadot_service::IsCollator::Yes(collator_pair),
None,
None,
telemetry_worker_handle,
+17 -46
View File
@@ -54,7 +54,7 @@ mod relay_state_snapshot;
pub mod validate_block;
/// The pallet's configuration trait.
pub trait Config: frame_system::Config {
pub trait Config: frame_system::Config<OnSetCode = ParachainSetCode<Self>> {
/// The overarching event type.
type Event: From<Event> + Into<<Self as frame_system::Config>::Event>;
@@ -144,24 +144,6 @@ decl_module! {
// this is needed only if you are using events in your pallet
fn deposit_event() = default;
// TODO: figure out a better weight than this
// TODO: Bring back the correct validation checks: #374
#[weight = (0, DispatchClass::Operational)]
pub fn schedule_upgrade(origin, validation_function: Vec<u8>) {
ensure_root(origin)?;
Self::schedule_upgrade_impl(validation_function)?;
}
/// Schedule a validation function upgrade without further checks.
///
/// Same as [`Module::schedule_upgrade`], but without checking that the new `validation_function`
/// is correct. This makes it more flexible, but also opens the door to easily brick the chain.
#[weight = (0, DispatchClass::Operational)]
pub fn schedule_upgrade_without_checks(origin, validation_function: Vec<u8>) {
ensure_root(origin)?;
Self::schedule_upgrade_impl(validation_function)?;
}
/// Force an already scheduled validation function upgrade to happen on a particular block.
///
/// Note that coordinating this block for the upgrade has to happen independently on the relay
@@ -654,8 +636,8 @@ impl<T: Config> Module<T> {
Some(vfp.relay_parent_number + cfg.validation_upgrade_delay)
}
/// The implementation of the runtime upgrade scheduling.
fn schedule_upgrade_impl(validation_function: Vec<u8>) -> DispatchResult {
/// The implementation of the runtime upgrade functionality for parachains.
fn set_code_impl(validation_function: Vec<u8>) -> DispatchResult {
ensure!(
!PendingValidationFunction::exists(),
Error::<T>::OverlappingUpgrades
@@ -685,6 +667,14 @@ impl<T: Config> Module<T> {
}
}
pub struct ParachainSetCode<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> frame_system::SetCode for ParachainSetCode<T> {
fn set_code(code: Vec<u8>) -> DispatchResult {
Module::<T>::set_code_impl(code)
}
}
/// This struct provides ability to extend a message queue chain (MQC) and compute a new head.
///
/// MQC is an instance of a [hash chain] applied to a message queue. Using a hash chain it's possible
@@ -975,6 +965,7 @@ mod tests {
type BaseCallFilter = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ParachainSetCode<Self>;
}
impl Config for Test {
type Event = Event;
@@ -1242,26 +1233,6 @@ mod tests {
});
}
#[test]
fn requires_root() {
BlockTests::new().add(123, || {
assert_eq!(
ParachainSystem::schedule_upgrade(Origin::signed(1), Default::default()),
Err(sp_runtime::DispatchError::BadOrigin),
);
});
}
#[test]
fn requires_root_2() {
BlockTests::new().add(123, || {
assert_ok!(ParachainSystem::schedule_upgrade(
RawOrigin::Root.into(),
Default::default()
));
});
}
#[test]
fn events() {
BlockTests::new()
@@ -1271,7 +1242,7 @@ mod tests {
.add_with_post_test(
123,
|| {
assert_ok!(ParachainSystem::schedule_upgrade(
assert_ok!(System::set_code(
RawOrigin::Root.into(),
Default::default()
));
@@ -1304,14 +1275,14 @@ mod tests {
builder.host_config.validation_upgrade_delay = 1000;
})
.add(123, || {
assert_ok!(ParachainSystem::schedule_upgrade(
assert_ok!(System::set_code(
RawOrigin::Root.into(),
Default::default()
));
})
.add(234, || {
assert_eq!(
ParachainSystem::schedule_upgrade(RawOrigin::Root.into(), Default::default()),
System::set_code(RawOrigin::Root.into(), Default::default()),
Err(Error::<Test>::OverlappingUpgrades.into()),
)
});
@@ -1325,7 +1296,7 @@ mod tests {
!PendingValidationFunction::exists(),
"validation function must not exist yet"
);
assert_ok!(ParachainSystem::schedule_upgrade(
assert_ok!(System::set_code(
RawOrigin::Root.into(),
Default::default()
));
@@ -1354,7 +1325,7 @@ mod tests {
})
.add(123, || {
assert_eq!(
ParachainSystem::schedule_upgrade(RawOrigin::Root.into(), vec![0; 64]),
System::set_code(RawOrigin::Root.into(), vec![0; 64]),
Err(Error::<Test>::TooBig.into()),
);
});
+1
View File
@@ -183,6 +183,7 @@ impl frame_system::Config for Runtime {
type BlockWeights = RuntimeBlockWeights;
type BlockLength = RuntimeBlockLength;
type SS58Prefix = SS58Prefix;
type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
}
parameter_types! {
+1 -2
View File
@@ -29,7 +29,6 @@ use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
use sc_service::{Configuration, PartialComponents, Role, TFullBackend, TFullClient, TaskManager};
use sc_telemetry::{Telemetry, TelemetryWorker, TelemetryWorkerHandle};
use sp_core::Pair;
use sp_runtime::traits::BlakeTwo256;
use sp_trie::PrefixedMemoryDB;
use std::sync::Arc;
@@ -154,7 +153,7 @@ where
let polkadot_full_node =
cumulus_client_service::build_polkadot_full_node(
polkadot_config,
collator_key.public(),
collator_key.clone(),
telemetry_worker_handle,
)
.map_err(|e| match e {
+5 -5
View File
@@ -19,7 +19,7 @@ use cumulus_primitives_core::PersistedValidationData;
use cumulus_primitives_parachain_inherent::{ParachainInherentData, INHERENT_IDENTIFIER};
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
use cumulus_test_runtime::{Block, GetLastTimestamp};
use polkadot_primitives::v1::BlockNumber as PBlockNumber;
use polkadot_primitives::v1::{BlockNumber as PBlockNumber, Hash as PHash};
use sc_block_builder::{BlockBuilder, BlockBuilderProvider};
use sp_api::ProvideRuntimeApi;
use sp_runtime::generic::BlockId;
@@ -35,7 +35,7 @@ pub trait InitBlockBuilder {
/// just use a default one.
fn init_block_builder(
&self,
validation_data: Option<PersistedValidationData<PBlockNumber>>,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
) -> sc_block_builder::BlockBuilder<Block, Client, Backend>;
@@ -46,7 +46,7 @@ pub trait InitBlockBuilder {
fn init_block_builder_at(
&self,
at: &BlockId<Block>,
validation_data: Option<PersistedValidationData<PBlockNumber>>,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
) -> sc_block_builder::BlockBuilder<Block, Client, Backend>;
}
@@ -54,7 +54,7 @@ pub trait InitBlockBuilder {
impl InitBlockBuilder for Client {
fn init_block_builder(
&self,
validation_data: Option<PersistedValidationData<PBlockNumber>>,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
) -> BlockBuilder<Block, Client, Backend> {
let chain_info = self.chain_info();
@@ -68,7 +68,7 @@ impl InitBlockBuilder for Client {
fn init_block_builder_at(
&self,
at: &BlockId<Block>,
validation_data: Option<PersistedValidationData<PBlockNumber>>,
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
relay_sproof_builder: RelayStateSproofBuilder,
) -> BlockBuilder<Block, Client, Backend> {
let mut block_builder = self
+1
View File
@@ -162,6 +162,7 @@ impl frame_system::Config for Runtime {
type BlockWeights = RuntimeBlockWeights;
type BlockLength = RuntimeBlockLength;
type SS58Prefix = SS58Prefix;
type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
}
parameter_types! {
+1 -1
View File
@@ -158,7 +158,7 @@ where
let relay_chain_full_node = polkadot_test_service::new_full(
relay_chain_config,
if let Some(ref key) = collator_key {
polkadot_service::IsCollator::Yes(key.public())
polkadot_service::IsCollator::Yes(key.clone())
} else {
polkadot_service::IsCollator::No
},