Update Substrate & Polkadot (#141)

This commit is contained in:
Bastian Köcher
2020-07-09 15:28:45 +02:00
committed by GitHub
parent fa0a3c1f42
commit e40bef8641
10 changed files with 520 additions and 418 deletions
+400 -321
View File
File diff suppressed because it is too large Load Diff
+6 -7
View File
@@ -37,6 +37,7 @@ use sp_consensus::{
BlockImport, BlockImportParams, BlockOrigin, BlockStatus, Environment, Error as ConsensusError,
ForkChoiceStrategy, Proposal, Proposer, RecordProof,
};
use sp_core::traits::SpawnNamed;
use sp_inherents::{InherentData, InherentDataProviders};
use sp_runtime::{
generic::BlockId,
@@ -56,7 +57,6 @@ use codec::{Decode, Encode};
use log::{debug, error, trace};
use futures::prelude::*;
use futures::task::Spawn;
use std::{marker::PhantomData, pin::Pin, sync::Arc, time::Duration};
@@ -81,7 +81,7 @@ impl<Block: BlockT, PF, BI, BS> Collator<Block, PF, BI, BS> {
collator_network: impl CollatorNetwork + Clone + 'static,
block_import: BI,
block_status: Arc<BS>,
spawner: Arc<dyn Spawn + Send + Sync>,
spawner: Arc<dyn SpawnNamed + Send + Sync>,
announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
) -> Self {
let collator_network = Arc::new(collator_network);
@@ -446,7 +446,7 @@ where
+ 'static,
PClient::Api: RuntimeApiCollection<Extrinsic>,
<PClient::Api as ApiExt<PBlock>>::StateBackend: StateBackend<HashFor<PBlock>>,
Spawner: Spawn + Clone + Send + Sync + 'static,
Spawner: SpawnNamed + Clone + Send + Sync + 'static,
Extrinsic: codec::Codec + Send + Sync + 'static,
{
self.delayed_block_announce_validator
@@ -463,9 +463,7 @@ where
}
};
spawner
.spawn_obj(Box::new(follow.map(|_| ())).into())
.map_err(|_| error!("Could not spawn parachain server!"))?;
spawner.spawn("cumulus-follow-polkadot", follow.map(|_| ()).boxed());
Ok(Collator::new(
self.proposer_factory,
@@ -498,6 +496,7 @@ mod tests {
use polkadot_primitives::parachain::Id as ParaId;
use sp_blockchain::Result as ClientResult;
use sp_core::testing::SpawnBlockingExecutor;
use sp_inherents::InherentData;
use sp_keyring::Sr25519Keyring;
use sp_runtime::traits::{DigestFor, Header as HeaderT};
@@ -602,7 +601,7 @@ mod tests {
fn collates_produces_a_block() {
let id = ParaId::from(100);
let _ = env_logger::try_init();
let spawner = futures::executor::ThreadPool::new().unwrap();
let spawner = SpawnBlockingExecutor::new();
let announce_block = |_, _| ();
let block_announce_validator = DelayedBlockAnnounceValidator::new();
let client = Arc::new(TestClientBuilder::new().build());
+1
View File
@@ -11,6 +11,7 @@ sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "cum
sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
# polkadot deps
polkadot-collator = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch" }
+46 -31
View File
@@ -24,12 +24,16 @@ mod tests;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::{Error as ClientError, HeaderBackend};
use sp_consensus::block_validation::{BlockAnnounceValidator, Validation};
use sp_runtime::{generic::BlockId, traits::{Block as BlockT, Header as HeaderT}};
use sp_core::traits::SpawnNamed;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT},
};
use polkadot_collator::Network as CollatorNetwork;
use polkadot_network::legacy::gossip::{GossipMessage, GossipStatement};
use polkadot_primitives::{
parachain::{ParachainHost, Id as ParaId},
parachain::{Id as ParaId, ParachainHost},
Block as PBlock, Hash as PHash,
};
use polkadot_statement_table::{SignedStatement, Statement};
@@ -38,14 +42,11 @@ use polkadot_validation::check_statement;
use cumulus_primitives::HeadData;
use codec::{Decode, Encode};
use futures::{pin_mut, select, StreamExt};
use futures::channel::oneshot;
use futures::future::FutureExt;
use futures::task::Spawn;
use log::{error, trace};
use futures::{channel::oneshot, future::FutureExt, pin_mut, select, StreamExt};
use log::trace;
use std::{marker::PhantomData, sync::Arc};
use parking_lot::Mutex;
use std::{marker::PhantomData, sync::Arc};
/// Validate that data is a valid justification from a relay-chain validator that the block is a
/// valid parachain-block candidate.
@@ -91,10 +92,17 @@ where
.local_validation_data(&runtime_api_block_id, self.para_id)
.map_err(|e| Box::new(ClientError::Msg(format!("{:?}", e))) as Box<_>)?
.ok_or_else(|| {
Box::new(ClientError::Msg("Could not find parachain head in relay chain".into())) as Box<_>
Box::new(ClientError::Msg(
"Could not find parachain head in relay chain".into(),
)) as Box<_>
})?;
let parent_head = HeadData::<B>::decode(&mut &local_validation_data.parent_head.0[..])
.map_err(|e| Box::new(ClientError::Msg(format!("Failed to decode parachain head: {:?}", e))) as Box<_>)?;
.map_err(|e| {
Box::new(ClientError::Msg(format!(
"Failed to decode parachain head: {:?}",
e
))) as Box<_>
})?;
let known_best_number = parent_head.header.number();
return Ok(if block_number >= known_best_number {
@@ -145,11 +153,13 @@ where
"could not find block number for {}: {}",
relay_chain_leaf, err,
))));
},
Ok(Some(x)) if x == best_number => {},
}
Ok(Some(x)) if x == best_number => {}
Ok(None) => {
return Err(Box::new(ClientError::UnknownBlock(relay_chain_leaf.to_string())));
},
return Err(Box::new(ClientError::UnknownBlock(
relay_chain_leaf.to_string(),
)));
}
Ok(Some(_)) => {
trace!(
target: "cumulus-network",
@@ -159,7 +169,7 @@ where
);
return Ok(Validation::Failure);
},
}
}
let runtime_api_block_id = BlockId::Hash(relay_chain_leaf);
@@ -168,7 +178,8 @@ where
.map_err(|e| Box::new(ClientError::Msg(format!("{:?}", e))) as Box<_>)?;
// Check that the signer is a legit validator.
let authorities = runtime_api.validators(&runtime_api_block_id)
let authorities = runtime_api
.validators(&runtime_api_block_id)
.map_err(|e| Box::new(ClientError::Msg(format!("{:?}", e))) as Box<_>)?;
let signer = authorities.get(sender as usize).ok_or_else(|| {
Box::new(ClientError::BadJustification(
@@ -208,7 +219,9 @@ where
/// A `BlockAnnounceValidator` that will be able to validate data when its internal
/// `BlockAnnounceValidator` is set.
pub struct DelayedBlockAnnounceValidator<B: BlockT>(Arc<Mutex<Option<Box<dyn BlockAnnounceValidator<B> + Send>>>>);
pub struct DelayedBlockAnnounceValidator<B: BlockT>(
Arc<Mutex<Option<Box<dyn BlockAnnounceValidator<B> + Send>>>>,
);
impl<B: BlockT> DelayedBlockAnnounceValidator<B> {
pub fn new() -> DelayedBlockAnnounceValidator<B> {
@@ -232,7 +245,9 @@ impl<B: BlockT> BlockAnnounceValidator<B> for DelayedBlockAnnounceValidator<B> {
header: &B::Header,
data: &[u8],
) -> Result<Validation, Box<dyn std::error::Error + Send>> {
self.0.lock().as_mut()
self.0
.lock()
.as_mut()
.expect("BlockAnnounceValidator is set before validating the first announcement; qed")
.validate(header, data)
}
@@ -244,7 +259,7 @@ impl<B: BlockT> BlockAnnounceValidator<B> for DelayedBlockAnnounceValidator<B> {
/// This object will spawn a new task every time the method `wait_to_announce` is called and cancel
/// the previous task running.
pub struct WaitToAnnounce<Block: BlockT> {
spawner: Arc<dyn Spawn + Send + Sync>,
spawner: Arc<dyn SpawnNamed + Send + Sync>,
announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
collator_network: Arc<dyn CollatorNetwork>,
current_trigger: oneshot::Sender<()>,
@@ -253,7 +268,7 @@ pub struct WaitToAnnounce<Block: BlockT> {
impl<Block: BlockT> WaitToAnnounce<Block> {
/// Create the `WaitToAnnounce` object
pub fn new(
spawner: Arc<dyn Spawn + Send + Sync>,
spawner: Arc<dyn SpawnNamed + Send + Sync>,
announce_block: Arc<dyn Fn(Block::Hash, Vec<u8>) + Send + Sync>,
collator_network: Arc<dyn CollatorNetwork>,
) -> WaitToAnnounce<Block> {
@@ -281,14 +296,17 @@ impl<Block: BlockT> WaitToAnnounce<Block> {
self.current_trigger = tx;
if let Err(err) = self.spawner.spawn_obj(Box::pin(async move {
self.spawner.spawn(
"cumulus-wait-to-announce",
async move {
let t1 = wait_to_announce::<Block>(
hash,
relay_chain_leaf,
announce_block,
collator_network,
&head_data,
).fuse();
)
.fuse();
let t2 = rx.fuse();
pin_mut!(t1, t2);
@@ -312,13 +330,9 @@ impl<Block: BlockT> WaitToAnnounce<Block> {
);
}
}
}).into()) {
error!(
target: "cumulus-network",
"Could not spawn a new task to wait for the announce block: {:?}",
err,
);
}
.boxed(),
);
}
}
@@ -337,13 +351,14 @@ async fn wait_to_announce<Block: BlockT>(
let gossip_message: GossipMessage = GossipStatement {
relay_chain_leaf,
signed_statement: statement,
}.into();
}
.into();
announce_block(hash, gossip_message.encode());
break;
},
_ => {},
}
_ => {}
}
}
}
+2 -2
View File
@@ -22,7 +22,7 @@ sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus
sp-std = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
system = { package = "frame-system", git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
# Other Dependencies
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"]}
@@ -45,6 +45,6 @@ std = [
'sp-runtime/std',
'sp-io/std',
'sp-std/std',
'system/std',
'frame-system/std',
'cumulus-primitives/std',
]
+13 -12
View File
@@ -41,16 +41,16 @@ use parachain::primitives::RelayChainBlockNumber;
use sp_core::storage::well_known_keys;
use sp_inherents::{InherentData, InherentIdentifier, ProvideInherent};
use sp_std::vec::Vec;
use system::ensure_none;
use frame_system::ensure_none;
/// A ValidationFunction is a compiled WASM blob which, on execution, validates parachain blocks.
pub type ValidationFunction = Vec<u8>;
type System<T> = system::Module<T>;
type System<T> = frame_system::Module<T>;
/// The pallet's configuration trait.
pub trait Trait: system::Trait {
pub trait Trait: frame_system::Trait {
/// The overarching event type.
type Event: From<Event> + Into<<Self as system::Trait>::Event>;
type Event: From<Event> + Into<<Self as frame_system::Trait>::Event>;
/// Something which can be notified when the validation function params are set.
///
@@ -246,10 +246,10 @@ mod tests {
Perbill,
};
use sp_version::RuntimeVersion;
use system::{InitKind, RawOrigin};
use frame_system::{InitKind, RawOrigin};
impl_outer_origin! {
pub enum Origin for Test {}
pub enum Origin for Test where system = frame_system {}
}
mod parachain_upgrade {
@@ -258,7 +258,7 @@ mod tests {
impl_outer_event! {
pub enum TestEvent for Test {
system<T>,
frame_system<T>,
parachain_upgrade,
}
}
@@ -283,7 +283,7 @@ mod tests {
transaction_version: 1,
};
}
impl system::Trait for Test {
impl frame_system::Trait for Test {
type Origin = Origin;
type Call = ();
type Index = u64;
@@ -308,6 +308,7 @@ mod tests {
type BlockExecutionWeight = ();
type ExtrinsicBaseWeight = ();
type BaseCallFilter = ();
type SystemWeightInfo = ();
}
impl Trait for Test {
type Event = TestEvent;
@@ -319,7 +320,7 @@ mod tests {
// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
fn new_test_ext() -> sp_io::TestExternalities {
system::GenesisConfig::default()
frame_system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap()
.into()
@@ -356,7 +357,7 @@ mod tests {
}
struct BlockTest {
n: <Test as system::Trait>::BlockNumber,
n: <Test as frame_system::Trait>::BlockNumber,
within_block: Box<dyn Fn()>,
after_block: Option<Box<dyn Fn()>>,
}
@@ -384,7 +385,7 @@ mod tests {
self
}
fn add<F>(self, n: <Test as system::Trait>::BlockNumber, within_block: F) -> Self
fn add<F>(self, n: <Test as frame_system::Trait>::BlockNumber, within_block: F) -> Self
where
F: 'static + Fn(),
{
@@ -397,7 +398,7 @@ mod tests {
fn add_with_post_test<F1, F2>(
self,
n: <Test as system::Trait>::BlockNumber,
n: <Test as frame_system::Trait>::BlockNumber,
within_block: F1,
after_block: F2,
) -> Self
+3 -3
View File
@@ -7,10 +7,10 @@ edition = "2018"
[dependencies]
# Other dependencies
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = [ "derive" ] }
memory-db = { version = "0.18.0", default-features = false }
memory-db = { version = "0.24.0", default-features = false }
hash-db = { version = "0.15.2", default-features = false }
trie-db = { version = "0.21.0", default-features = false }
hashbrown = "0.6.1"
trie-db = { version = "0.22.0", default-features = false }
hashbrown = "0.8.0"
# Cumulus dependencies
cumulus-primitives = { path = "../primitives", default-features = false }
@@ -192,6 +192,7 @@ impl frame_system::Trait for Runtime {
type BlockExecutionWeight = ();
type MaximumExtrinsicWeight = MaximumExtrinsicWeight;
type BaseCallFilter = ();
type SystemWeightInfo = ();
}
parameter_types! {
@@ -203,6 +204,7 @@ impl pallet_timestamp::Trait for Runtime {
type Moment = u64;
type OnTimestampSet = ();
type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
}
parameter_types! {
@@ -220,6 +222,7 @@ impl pallet_balances::Trait for Runtime {
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
}
impl pallet_transaction_payment::Trait for Runtime {
+7 -12
View File
@@ -43,10 +43,13 @@ impl SubstrateCli for Cli {
}
fn description() -> String {
format!("Cumulus test parachain collator\n\nThe command-line arguments provided first will be \
format!(
"Cumulus test parachain collator\n\nThe command-line arguments provided first will be \
passed to the parachain node, while the arguments provided after -- will be passed \
to the relaychain node.\n\n\
{} [parachain-args] -- [relaychain-args]", Self::executable_name())
{} [parachain-args] -- [relaychain-args]",
Self::executable_name()
)
}
fn author() -> String {
@@ -61,10 +64,6 @@ impl SubstrateCli for Cli {
2017
}
fn executable_name() -> String {
"cumulus-test-parachain-collator".into()
}
fn load_spec(&self, _id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
// Such a hack :(
Ok(Box::new(chain_spec::get_chain_spec(
@@ -106,10 +105,6 @@ impl SubstrateCli for PolkadotCli {
2017
}
fn executable_name() -> String {
"cumulus-test-parachain-collator".into()
}
fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
polkadot_cli::Cli::from_iter([PolkadotCli::executable_name().to_string()].iter())
.load_spec(id)
@@ -161,7 +156,7 @@ pub fn run() -> Result<()> {
})
}
Some(Subcommand::ExportGenesisState(params)) => {
sc_cli::init_logger("");
sc_cli::init_logger("", &<sc_cli::LogRotationOpt as structopt::StructOpt>::from_args())?;
let block = generate_genesis_state(params.parachain_id.into())?;
let header_hex = format!("0x{:?}", HexDisplay::from(&block.header().encode()));
@@ -200,7 +195,7 @@ pub fn run() -> Result<()> {
})
}
Some(Subcommand::PolkadotValidationWorker(cmd)) => {
sc_cli::init_logger("");
sc_cli::init_logger("", &<sc_cli::LogRotationOpt as structopt::StructOpt>::from_args())?;
polkadot_service::run_validation_worker(&cmd.mem_id)?;
Ok(())
+13 -4
View File
@@ -17,7 +17,7 @@
use ansi_term::Color;
use cumulus_collator::{prepare_collator_config, CollatorBuilder};
use cumulus_network::DelayedBlockAnnounceValidator;
use futures::{FutureExt, future::ready};
use futures::{future::ready, FutureExt};
use polkadot_primitives::parachain::CollatorPair;
use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
@@ -51,7 +51,10 @@ macro_rules! new_full_start {
.with_select_chain(|_config, backend| Ok(sc_consensus::LongestChain::new(backend.clone())))?
.with_transaction_pool(|builder| {
let client = builder.client();
let pool_api = Arc::new(sc_transaction_pool::FullChainApi::new(client.clone()));
let pool_api = Arc::new(sc_transaction_pool::FullChainApi::new(
client.clone(),
builder.prometheus_registry(),
));
let pool = sc_transaction_pool::BasicPool::new(
builder.config().transaction_pool.clone(),
pool_api,
@@ -139,9 +142,15 @@ pub fn run_collator(
polkadot_collator::start_collator(builder, id, key, polkadot_config)?;
// Make sure the polkadot task manager survives as long as the service.
let polkadot_future = polkadot_future.then(move |_| { let _ = task_manager; ready(())});
let polkadot_future = polkadot_future.then(move |_| {
let _ = task_manager;
ready(())
});
service.task_manager.spawn_essential_handle().spawn("polkadot", polkadot_future);
service
.task_manager
.spawn_essential_handle()
.spawn("polkadot", polkadot_future);
Ok(service.task_manager)
}