Erasure encoding availability (#345)

* Erasure encoding availability initial commit

 * Modifications to availability store to keep chunks as well as
   reconstructed blocks and extrinsics.
 * Gossip messages containig signed erasure chunks.
 * Requesting eraure chunks with polkadot-specific messages.
 * Validation of erasure chunk messages.

* Apply suggestions from code review

Co-Authored-By: Luke Schoen <ltfschoen@users.noreply.github.com>

* Fix build after a merge

* Gossip erasure chunk messages under their own topic

* erasure_chunks should use the appropriate topic

* Updates Cargo.lock

* Fixes after merge

* Removes a couple of leftover pieces of code

* Fixes simple stuff from review

* Updates erasure and storage for more flexible logic

* Changes validation and candidate receipt production.

* Adds add_erasure_chunks method

* Fixes most of the nits

* Better validate_collation and validate_receipt functions

* Fixes the tests

* Apply suggestions from code review

Co-Authored-By: Robert Habermeier <rphmeier@gmail.com>

* Removes unwrap() calls

* Removes ErasureChunks primitive

* Removes redundant fields from ErasureChunk struct

* AvailabilityStore should store CandidateReceipt

* Changes the way chunk messages are imported and validated.

 * Availability store now stores a validator_index and n_validators for
 each relay_parent.
 * Availability store now also stores candidate receipts.
 * Removes importing chunks in the table and moves it into network
 gossip validation.
 * Validation of erasure messages id done against receipts that are
 stored in the availability store.

* Correctly compute topics for erasure messages

* Removes an unused parameter

* Refactors availability db querying into a helper

* Adds the apis described in the writeup

* Adds a runtime api to extract erasure roots form raw extrinsics.

* Adds a barebone BlockImport impl for avalability store

* Adds the implementation of the availability worker

* Fix build after the merge with master.

* Make availability store API async

* Bring back the default wasmtime feature

* Lines width

* Bump runtime version

* Formatting and dead code elimination

* some style nits (#1)

* More nits and api cleanup

* Disable wasm CI for availability-store

* Another nit

* Formatting
This commit is contained in:
Fedor Sakharov
2019-12-03 17:49:07 +03:00
committed by Robert Habermeier
parent ec54d5b1e4
commit 99d164b5e7
29 changed files with 2957 additions and 572 deletions
+3 -70
View File
@@ -26,15 +26,14 @@
use std::{thread, time::{Duration, Instant}, sync::Arc};
use client::{BlockchainEvents, BlockBody};
use sp_blockchain::{HeaderBackend, Result as ClientResult};
use sp_blockchain::HeaderBackend;
use block_builder::BlockBuilderApi;
use consensus::SelectChain;
use availability_store::Store as AvailabilityStore;
use futures::prelude::*;
use futures03::{TryStreamExt as _, StreamExt as _};
use log::error;
use polkadot_primitives::{Block, BlockId};
use polkadot_primitives::parachain::{CandidateReceipt, ParachainHost};
use polkadot_primitives::Block;
use polkadot_primitives::parachain::ParachainHost;
use runtime_primitives::traits::{ProvideRuntimeApi};
use babe_primitives::BabeApi;
use keystore::KeyStorePtr;
@@ -47,62 +46,6 @@ use super::{Network, Collators};
type TaskExecutor = Arc<dyn futures::future::Executor<Box<dyn Future<Item = (), Error = ()> + Send>> + Send + Sync>;
/// Gets a list of the candidates in a block.
pub(crate) fn fetch_candidates<P: BlockBody<Block>>(client: &P, block: &BlockId)
-> ClientResult<Option<impl Iterator<Item=CandidateReceipt>>>
{
use codec::{Encode, Decode};
use polkadot_runtime::{Call, ParachainsCall, UncheckedExtrinsic as RuntimeExtrinsic};
let extrinsics = client.block_body(block)?;
Ok(match extrinsics {
Some(extrinsics) => extrinsics
.into_iter()
.filter_map(|ex| RuntimeExtrinsic::decode(&mut ex.encode().as_slice()).ok())
.filter_map(|ex| match ex.function {
Call::Parachains(ParachainsCall::set_heads(heads)) => {
Some(heads.into_iter().map(|c| c.candidate))
}
_ => None,
})
.next(),
None => None,
})
}
// creates a task to prune redundant entries in availability store upon block finalization
//
// NOTE: this will need to be changed to finality notification rather than
// block import notifications when the consensus switches to non-instant finality.
fn prune_unneeded_availability<P>(client: Arc<P>, availability_store: AvailabilityStore)
-> impl Future<Item=(),Error=()> + Send
where P: Send + Sync + BlockchainEvents<Block> + BlockBody<Block> + 'static
{
client.finality_notification_stream()
.map(|v| Ok::<_, ()>(v)).compat()
.for_each(move |notification| {
let hash = notification.hash;
let parent_hash = notification.header.parent_hash;
let candidate_hashes = match fetch_candidates(&*client, &BlockId::hash(hash)) {
Ok(Some(candidates)) => candidates.map(|c| c.hash()).collect(),
Ok(None) => {
warn!("Could not extract candidates from block body of imported block {:?}", hash);
return Ok(())
}
Err(e) => {
warn!("Failed to fetch block body for imported block {:?}: {:?}", hash, e);
return Ok(())
}
};
if let Err(e) = availability_store.candidates_finalized(parent_hash, candidate_hashes) {
warn!(target: "validation", "Failed to prune unneeded available data: {:?}", e);
}
Ok(())
})
}
/// Parachain candidate attestation service handle.
pub(crate) struct ServiceHandle {
thread: Option<thread::JoinHandle<()>>,
@@ -116,7 +59,6 @@ pub(crate) fn start<C, N, P, SC>(
parachain_validation: Arc<crate::ParachainValidation<C, N, P>>,
thread_pool: TaskExecutor,
keystore: KeyStorePtr,
availability_store: AvailabilityStore,
max_block_data_size: Option<u64>,
) -> ServiceHandle
where
@@ -197,15 +139,6 @@ pub(crate) fn start<C, N, P, SC>(
error!("Failed to spawn old sessions pruning task");
}
let prune_available = prune_unneeded_availability(client, availability_store)
.select(exit.clone())
.then(|_| Ok(()));
// spawn this on the tokio executor since it's fine on a thread pool.
if let Err(_) = thread_pool.execute(Box::new(prune_available)) {
error!("Failed to spawn available pruning task");
}
if let Err(e) = runtime.block_on(exit) {
debug!("BFT event loop error {:?}", e);
}
+231 -40
View File
@@ -21,10 +21,12 @@
use std::sync::Arc;
use polkadot_primitives::{Block, Hash, BlockId, Balance, parachain::{
CollatorId, ConsolidatedIngress, StructuredUnroutedIngress, CandidateReceipt, ParachainHost,
Id as ParaId, Collation, TargetedMessage, OutgoingMessages, UpwardMessage, FeeSchedule,
use polkadot_primitives::{BlakeTwo256, Block, Hash, HashT, BlockId, Balance, parachain::{
CollatorId, ConsolidatedIngress, StructuredUnroutedIngress, CandidateReceipt, CollationInfo, ParachainHost,
Id as ParaId, Collation, TargetedMessage, OutgoingMessages, UpwardMessage, FeeSchedule, ErasureChunk,
HeadData, PoVBlock,
}};
use polkadot_erasure_coding::{self as erasure};
use runtime_primitives::traits::ProvideRuntimeApi;
use parachain::{wasm_executor::{self, ExternalitiesError, ExecutionMode}, MessageRef, UpwardMessageRef};
use trie::TrieConfiguration;
@@ -100,10 +102,10 @@ impl<C: Collators, P> CollationFetch<C, P> {
impl<C: Collators, P: ProvideRuntimeApi> Future for CollationFetch<C, P>
where P::Api: ParachainHost<Block, Error = sp_blockchain::Error>,
{
type Item = (Collation, OutgoingMessages);
type Item = (Collation, OutgoingMessages, Balance);
type Error = C::Error;
fn poll(&mut self) -> Poll<(Collation, OutgoingMessages), C::Error> {
fn poll(&mut self) -> Poll<(Collation, OutgoingMessages, Balance), C::Error> {
loop {
let collation = {
let parachain = self.parachain.clone();
@@ -123,15 +125,15 @@ impl<C: Collators, P: ProvideRuntimeApi> Future for CollationFetch<C, P>
);
match res {
Ok(e) => {
return Ok(Async::Ready((collation, e)))
Ok((messages, fees)) => {
return Ok(Async::Ready((collation, messages, fees)))
}
Err(e) => {
debug!("Failed to validate parachain due to API error: {}", e);
// just continue if we got a bad collation or failed to validate
self.live_fetch = None;
self.collators.note_bad_collator(collation.receipt.collator)
self.collators.note_bad_collator(collation.info.collator)
}
}
}
@@ -145,6 +147,8 @@ pub enum Error {
Client(sp_blockchain::Error),
/// Wasm validation error
WasmValidation(wasm_executor::Error),
/// Erasure-encoding error.
Erasure(erasure::Error),
/// Collated for inactive parachain
#[display(fmt = "Collated for inactive parachain: {:?}", _0)]
InactiveParachain(ParaId),
@@ -175,6 +179,13 @@ pub enum Error {
/// Parachain validation produced wrong fees to charge to parachain.
#[display(fmt = "Parachain validation produced wrong relay-chain fees (expected: {:?}, got {:?})", expected, got)]
FeesChargedInvalid { expected: Balance, got: Balance },
/// Candidate block has an erasure-encoded root that mismatches the actual
/// erasure-encoded root of block data and extrinsics.
#[display(fmt = "Got unexpected erasure root (expected: {:?}, got {:?})", expected, got)]
ErasureRootMismatch { expected: Hash, got: Hash },
/// Candidate block collation info doesn't match candidate receipt.
#[display(fmt = "Got receipt mismatch for candidate {:?}", candidate)]
CandidateReceiptMismatch { candidate: Hash },
}
impl std::error::Error for Error {
@@ -325,29 +336,53 @@ impl Externalities {
// Performs final checks of validity, producing the outgoing message data.
fn final_checks(
self,
candidate: &CandidateReceipt,
) -> Result<OutgoingMessages, Error> {
if &self.upward != &candidate.upward_messages {
upward_messages: &[UpwardMessage],
egress_queue_roots: &[(ParaId, Hash)],
fees_charged: Option<Balance>,
) -> Result<(OutgoingMessages, Balance), Error> {
if self.upward != upward_messages {
return Err(Error::UpwardMessagesInvalid {
expected: candidate.upward_messages.clone(),
expected: upward_messages.to_vec(),
got: self.upward.clone(),
});
}
if self.fees_charged != candidate.fees {
return Err(Error::FeesChargedInvalid {
expected: candidate.fees.clone(),
got: self.fees_charged.clone(),
});
if let Some(fees_charged) = fees_charged {
if self.fees_charged != fees_charged {
return Err(Error::FeesChargedInvalid {
expected: fees_charged.clone(),
got: self.fees_charged.clone(),
});
}
}
check_egress(
let messages = check_egress(
self.outgoing,
&candidate.egress_queue_roots[..],
)
&egress_queue_roots[..],
)?;
Ok((messages, self.fees_charged))
}
}
/// Validate an erasure chunk against an expected root.
pub fn validate_chunk(
root: &Hash,
chunk: &ErasureChunk,
) -> Result<(), Error> {
let expected = erasure::branch_hash(root, &chunk.proof, chunk.index as usize)?;
let got = BlakeTwo256::hash(&chunk.chunk);
if expected != got {
return Err(Error::ErasureRootMismatch {
expected,
got,
})
}
Ok(())
}
/// Validate incoming messages against expected roots.
pub fn validate_incoming(
roots: &StructuredUnroutedIngress,
@@ -382,30 +417,34 @@ pub fn validate_incoming(
Ok(())
}
/// Check whether a given collation is valid. Returns `Ok` on success, error otherwise.
///
/// This assumes that basic validity checks have been done:
/// - Block data hash is the same as linked in candidate receipt.
pub fn validate_collation<P>(
// A utility function that implements most of the collation validation logic.
//
// Reused by `validate_collation` and `validate_receipt`.
// Returns outgoing messages and fees charged for later reuse.
fn do_validation<P>(
client: &P,
relay_parent: &BlockId,
collation: &Collation,
pov_block: &PoVBlock,
para_id: ParaId,
max_block_data_size: Option<u64>,
) -> Result<OutgoingMessages, Error> where
fees_charged: Option<Balance>,
head_data: &HeadData,
queue_roots: &Vec<(ParaId, Hash)>,
upward_messages: &Vec<UpwardMessage>,
) -> Result<(OutgoingMessages, Balance), Error> where
P: ProvideRuntimeApi,
P::Api: ParachainHost<Block, Error = sp_blockchain::Error>,
{
use parachain::{IncomingMessage, ValidationParams};
if let Some(max_size) = max_block_data_size {
let block_data_size = collation.pov.block_data.0.len() as u64;
let block_data_size = pov_block.block_data.0.len() as u64;
if block_data_size > max_size {
return Err(Error::BlockDataTooBig { size: block_data_size, max_size });
}
}
let api = client.runtime_api();
let para_id = collation.receipt.parachain_index;
let validation_code = api.parachain_code(relay_parent, para_id)?
.ok_or_else(|| Error::InactiveParachain(para_id))?;
@@ -415,12 +454,12 @@ pub fn validate_collation<P>(
let roots = api.ingress(relay_parent, para_id, None)?
.ok_or_else(|| Error::InactiveParachain(para_id))?;
validate_incoming(&roots, &collation.pov.ingress)?;
validate_incoming(&roots, &pov_block.ingress)?;
let params = ValidationParams {
parent_head: chain_status.head_data.0,
block_data: collation.pov.block_data.0.clone(),
ingress: collation.pov.ingress.0.iter()
block_data: pov_block.block_data.0.clone(),
ingress: pov_block.ingress.0.iter()
.flat_map(|&(source, ref messages)| {
messages.iter().map(move |msg| IncomingMessage {
source,
@@ -431,7 +470,7 @@ pub fn validate_collation<P>(
};
let mut ext = Externalities {
parachain_index: collation.receipt.parachain_index.clone(),
parachain_index: para_id.clone(),
outgoing: Vec::new(),
upward: Vec::new(),
free_balance: chain_status.balance,
@@ -441,11 +480,17 @@ pub fn validate_collation<P>(
match wasm_executor::validate_candidate(&validation_code, params, &mut ext, ExecutionMode::Remote) {
Ok(result) => {
if result.head_data == collation.receipt.head_data.0 {
ext.final_checks(&collation.receipt)
if result.head_data == head_data.0 {
let (messages, fees) = ext.final_checks(
upward_messages,
queue_roots,
fees_charged
)?;
Ok((messages, fees))
} else {
Err(Error::WrongHeadData {
expected: collation.receipt.head_data.0.clone(),
expected: head_data.0.clone(),
got: result.head_data
})
}
@@ -454,6 +499,132 @@ pub fn validate_collation<P>(
}
}
/// Produce a `CandidateReceipt` and erasure encoding chunks with a given collation.
///
/// To produce a `CandidateReceipt` among other things the root of erasure encoding of
/// the block data and messages needs to be known. To avoid redundant re-computations
/// of erasure encoding this method creates an encoding and produces a candidate with
/// encoding's root returning both for re-use.
pub fn produce_receipt_and_chunks(
n_validators: usize,
pov: &PoVBlock,
messages: &OutgoingMessages,
fees: Balance,
info: &CollationInfo,
) -> Result<(CandidateReceipt, Vec<ErasureChunk>), Error>
{
let erasure_chunks = erasure::obtain_chunks(
n_validators,
&pov.block_data,
Some(&messages.clone().into())
)?;
let branches = erasure::branches(erasure_chunks.as_ref());
let erasure_root = branches.root();
let chunks: Vec<_> = erasure_chunks
.iter()
.zip(branches.map(|(proof, _)| proof))
.enumerate()
.map(|(index, (chunk, proof))| ErasureChunk {
// branches borrows the original chunks, but this clone could probably be dodged.
chunk: chunk.clone(),
index: index as u32,
proof,
})
.collect();
let receipt = CandidateReceipt {
parachain_index: info.parachain_index,
collator: info.collator.clone(),
signature: info.signature.clone(),
head_data: info.head_data.clone(),
egress_queue_roots: info.egress_queue_roots.clone(),
fees,
block_data_hash: info.block_data_hash.clone(),
upward_messages: info.upward_messages.clone(),
erasure_root,
};
Ok((receipt, chunks))
}
/// Check if a given candidate receipt is valid with a given collation.
///
/// This assumes that basic validity checks have been done:
/// - Block data hash is the same as linked in collation info and a receipt.
pub fn validate_receipt<P>(
client: &P,
relay_parent: &BlockId,
pov_block: &PoVBlock,
receipt: &CandidateReceipt,
max_block_data_size: Option<u64>,
) -> Result<(OutgoingMessages, Vec<ErasureChunk>), Error> where
P: ProvideRuntimeApi,
P::Api: ParachainHost<Block, Error = sp_blockchain::Error>,
{
let (messages, _fees) = do_validation(
client,
relay_parent,
pov_block,
receipt.parachain_index,
max_block_data_size,
Some(receipt.fees),
&receipt.head_data,
&receipt.egress_queue_roots,
&receipt.upward_messages,
)?;
let api = client.runtime_api();
let validators = api.validators(&relay_parent)?;
let n_validators = validators.len();
let (validated_receipt, chunks) = produce_receipt_and_chunks(
n_validators,
pov_block,
&messages,
receipt.fees,
&receipt.clone().into(),
)?;
if validated_receipt.erasure_root != receipt.erasure_root {
return Err(Error::ErasureRootMismatch {
expected: validated_receipt.erasure_root,
got: receipt.erasure_root,
});
}
Ok((messages, chunks))
}
/// Check whether a given collation is valid. Returns `Ok` on success, error otherwise.
///
/// This assumes that basic validity checks have been done:
/// - Block data hash is the same as linked in collation info.
pub fn validate_collation<P>(
client: &P,
relay_parent: &BlockId,
collation: &Collation,
max_block_data_size: Option<u64>,
) -> Result<(OutgoingMessages, Balance), Error> where
P: ProvideRuntimeApi,
P::Api: ParachainHost<Block, Error = sp_blockchain::Error>,
{
let para_id = collation.info.parachain_index;
do_validation(
client,
relay_parent,
&collation.pov,
para_id,
max_block_data_size,
None,
&collation.info.head_data,
&collation.info.egress_queue_roots,
&collation.info.upward_messages,
)
}
#[cfg(test)]
mod tests {
use super::*;
@@ -550,8 +721,13 @@ mod tests {
UpwardMessage{ data: vec![42], origin: ParachainDispatchOrigin::Signed },
UpwardMessage{ data: vec![69], origin: ParachainDispatchOrigin::Parachain },
],
erasure_root: [1u8; 32].into(),
};
assert!(ext().final_checks(&receipt).is_err());
assert!(ext().final_checks(
&receipt.upward_messages,
&receipt.egress_queue_roots,
Some(receipt.fees),
).is_err());
let receipt = CandidateReceipt {
parachain_index: 5.into(),
collator: Default::default(),
@@ -563,8 +739,13 @@ mod tests {
upward_messages: vec![
UpwardMessage{ data: vec![42], origin: ParachainDispatchOrigin::Signed },
],
erasure_root: [1u8; 32].into(),
};
assert!(ext().final_checks(&receipt).is_err());
assert!(ext().final_checks(
&receipt.upward_messages,
&receipt.egress_queue_roots,
Some(receipt.fees),
).is_err());
let receipt = CandidateReceipt {
parachain_index: 5.into(),
collator: Default::default(),
@@ -576,8 +757,13 @@ mod tests {
upward_messages: vec![
UpwardMessage{ data: vec![69], origin: ParachainDispatchOrigin::Parachain },
],
erasure_root: [1u8; 32].into(),
};
assert!(ext().final_checks(&receipt).is_err());
assert!(ext().final_checks(
&receipt.upward_messages,
&receipt.egress_queue_roots,
Some(receipt.fees),
).is_err());
let receipt = CandidateReceipt {
parachain_index: 5.into(),
collator: Default::default(),
@@ -589,8 +775,13 @@ mod tests {
upward_messages: vec![
UpwardMessage{ data: vec![42], origin: ParachainDispatchOrigin::Parachain },
],
erasure_root: [1u8; 32].into(),
};
assert!(ext().final_checks(&receipt).is_ok());
assert!(ext().final_checks(
&receipt.upward_messages,
&receipt.egress_queue_roots,
Some(receipt.fees),
).is_ok());
}
#[test]
+87 -39
View File
@@ -48,9 +48,10 @@ use availability_store::Store as AvailabilityStore;
use parking_lot::Mutex;
use polkadot_primitives::{Hash, Block, BlockId, BlockNumber, Header};
use polkadot_primitives::parachain::{
Id as ParaId, Chain, DutyRoster, OutgoingMessages, CandidateReceipt,
ParachainHost, AttestedCandidate, Statement as PrimitiveStatement, Message,
Collation, PoVBlock, ValidatorSignature, ValidatorPair, ValidatorId
Id as ParaId, Chain, DutyRoster, CandidateReceipt,
ParachainHost, AttestedCandidate, Statement as PrimitiveStatement, Message, OutgoingMessages,
Collation, PoVBlock, ErasureChunk, ValidatorSignature, ValidatorIndex,
ValidatorPair, ValidatorId,
};
use primitives::Pair;
use runtime_primitives::traits::{ProvideRuntimeApi, DigestFor};
@@ -60,7 +61,7 @@ use txpool_api::{TransactionPool, InPoolTransaction};
use attestation_service::ServiceHandle;
use futures::prelude::*;
use futures03::{future::{self, Either}, FutureExt, StreamExt};
use futures03::{future::{self, Either}, FutureExt, StreamExt, TryFutureExt};
use collation::CollationFetch;
use dynamic_inclusion::DynamicInclusion;
use inherents::InherentData;
@@ -69,10 +70,14 @@ use log::{info, debug, warn, trace, error};
use keystore::KeyStorePtr;
use sp_api::ApiExt;
type TaskExecutor = Arc<dyn futures::future::Executor<Box<dyn Future<Item = (), Error = ()> + Send>> + Send + Sync>;
type TaskExecutor =
Arc<
dyn futures::future::Executor<Box<dyn Future<Item = (), Error = ()> + Send>>
+ Send + Sync>;
pub use self::collation::{
validate_collation, validate_incoming, message_queue_root, egress_roots, Collators,
produce_receipt_and_chunks,
};
pub use self::error::Error;
pub use self::shared_table::{
@@ -106,7 +111,13 @@ pub trait TableRouter: Clone {
/// Call with local candidate data. This will make the data available on the network,
/// and sign, import, and broadcast a statement about the candidate.
fn local_collation(&self, collation: Collation, outgoing: OutgoingMessages);
fn local_collation(
&self,
collation: Collation,
receipt: CandidateReceipt,
outgoing: OutgoingMessages,
chunks: (ValidatorIndex, &[ErasureChunk])
);
/// Fetch validation proof for a specific candidate.
fn fetch_pov_block(&self, candidate: &CandidateReceipt) -> Self::FetchValidationProof;
@@ -158,7 +169,12 @@ pub fn sign_table_statement(statement: &Statement, key: &ValidatorPair, parent_h
}
/// Check signature on table statement.
pub fn check_statement(statement: &Statement, signature: &ValidatorSignature, signer: ValidatorId, parent_hash: &Hash) -> bool {
pub fn check_statement(
statement: &Statement,
signature: &ValidatorSignature,
signer: ValidatorId,
parent_hash: &Hash
) -> bool {
use runtime_primitives::traits::AppVerify;
let mut encoded = PrimitiveStatement::from(statement.clone()).encode();
@@ -181,12 +197,14 @@ pub fn make_group_info(
}
let mut local_validation = None;
let mut local_index = 0;
let mut map = HashMap::new();
let duty_iter = authorities.iter().zip(&roster.validator_duty);
for (authority, v_duty) in duty_iter {
for (i, (authority, v_duty)) in duty_iter.enumerate() {
if Some(authority) == local_id.as_ref() {
local_validation = Some(v_duty.clone());
local_index = i;
}
match *v_duty {
@@ -206,7 +224,8 @@ pub fn make_group_info(
let local_duty = local_validation.map(|v| LocalDuty {
validation: v
validation: v,
index: local_index as u32,
});
Ok((map, local_duty))
@@ -305,6 +324,21 @@ impl<C, N, P> ParachainValidation<C, N, P> where
debug!(target: "validation", "Active parachains: {:?}", active_parachains);
// If we are a validator, we need to store our index in this round in availability store.
// This will tell which erasure chunk we should store.
if let Some(ref local_duty) = local_duty {
if let Err(e) = self.availability_store.add_validator_index_and_n_validators(
&parent_hash,
local_duty.index,
validators.len() as u32,
) {
warn!(
target: "validation",
"Failed to add validator index and n_validators to the availability-store: {:?}", e
)
}
}
let table = Arc::new(SharedTable::new(
validators.clone(),
group_info,
@@ -322,8 +356,8 @@ impl<C, N, P> ParachainValidation<C, N, P> where
exit.clone(),
);
if let Some(Chain::Parachain(id)) = local_duty.as_ref().map(|d| d.validation) {
self.launch_work(parent_hash, id, router, max_block_data_size, exit);
if let Some((Chain::Parachain(id), index)) = local_duty.as_ref().map(|d| (d.validation, d.index)) {
self.launch_work(parent_hash, id, router, max_block_data_size, validators.len(), index, exit);
}
let tracker = Arc::new(AttestationTracker {
@@ -349,10 +383,10 @@ impl<C, N, P> ParachainValidation<C, N, P> where
validation_para: ParaId,
build_router: N::BuildTableRouter,
max_block_data_size: Option<u64>,
authorities_num: usize,
local_id: ValidatorIndex,
exit: exit_future::Exit,
) {
use availability_store::Data;
let (collators, client) = (self.collators.clone(), self.client.clone());
let availability_store = self.availability_store.clone();
@@ -362,42 +396,55 @@ impl<C, N, P> ParachainValidation<C, N, P> where
validation_para,
relay_parent,
collators,
client,
client.clone(),
max_block_data_size,
);
collation_work.then(move |result| match result {
Ok((collation, outgoing_targeted)) => {
let outgoing_queues = crate::outgoing_queues(&outgoing_targeted)
.map(|(_target, root, data)| (root, data))
.collect();
Ok((collation, outgoing_targeted, fees_charged)) => {
match produce_receipt_and_chunks(
authorities_num,
&collation.pov,
&outgoing_targeted,
fees_charged,
&collation.info,
) {
Ok((receipt, chunks)) => {
// Apparently the `async move` block is the only way to convince
// the compiler that we are not moving values out of borrowed context.
let av_clone = availability_store.clone();
let chunks_clone = chunks.clone();
let receipt_clone = receipt.clone();
let res = availability_store.make_available(Data {
relay_parent,
parachain_id: collation.receipt.parachain_index,
candidate_hash: collation.receipt.hash(),
block_data: collation.pov.block_data.clone(),
outgoing_queues: Some(outgoing_queues),
});
let res = async move {
if let Err(e) = av_clone.clone().add_erasure_chunks(
relay_parent.clone(),
receipt_clone,
chunks_clone,
).await {
warn!(target: "validation", "Failed to add erasure chunks: {}", e);
}
}
.unit_error()
.boxed()
.compat()
.then(move |_| {
router.local_collation(collation, receipt, outgoing_targeted, (local_id, &chunks));
Ok(())
});
match res {
Ok(()) => {
// TODO: https://github.com/paritytech/polkadot/issues/51
// Erasure-code and provide merkle branches.
router.local_collation(collation, outgoing_targeted);
Some(res)
}
Err(e) => {
warn!(target: "validation", "Failed to produce a receipt: {:?}", e);
None
}
Err(e) => warn!(
target: "validation",
"Failed to make collation data available: {:?}",
e,
),
}
Ok(())
}
Err(e) => {
warn!(target: "validation", "Failed to collate candidate: {:?}", e);
Ok(())
None
}
})
};
@@ -408,6 +455,7 @@ impl<C, N, P> ParachainValidation<C, N, P> where
warn!(target: "validation" , "Failed to build table router: {:?}", e);
})
.and_then(with_router)
.then(|_| Ok(()))
.select(exit)
.then(|_| Ok(()));
@@ -479,7 +527,6 @@ impl<C, N, P, SC, TxPool> ProposerFactory<C, N, P, SC, TxPool> where
parachain_validation.clone(),
thread_pool,
keystore.clone(),
availability_store,
max_block_data_size,
);
@@ -541,6 +588,7 @@ impl<C, N, P, SC, TxPool> consensus::Environment<Block> for ProposerFactory<C, N
#[derive(Debug)]
pub struct LocalDuty {
validation: Chain,
index: ValidatorIndex,
}
/// The Polkadot proposer logic.
+149 -71
View File
@@ -20,12 +20,12 @@
use std::collections::hash_map::{HashMap, Entry};
use std::sync::Arc;
use availability_store::{Data, Store as AvailabilityStore};
use availability_store::{Store as AvailabilityStore};
use table::{self, Table, Context as TableContextTrait};
use polkadot_primitives::{Block, BlockId, Hash};
use polkadot_primitives::parachain::{
Id as ParaId, Collation, OutgoingMessages, CandidateReceipt, ValidatorPair, ValidatorId,
AttestedCandidate, ParachainHost, PoVBlock, ValidatorIndex
Id as ParaId, OutgoingMessages, CandidateReceipt, ValidatorPair, ValidatorId,
AttestedCandidate, ParachainHost, PoVBlock, ValidatorIndex, ErasureChunk,
};
use parking_lot::Mutex;
@@ -146,7 +146,6 @@ impl SharedTableInner {
let local_index = context.local_index()?;
let para_member = context.is_member_of(local_index, &summary.group_id);
let digest = &summary.candidate;
// TODO: consider a strategy based on the number of candidate votes as well.
@@ -189,6 +188,7 @@ impl SharedTableInner {
availability_store: self.availability_store.clone(),
relay_parent: context.parent_hash.clone(),
work,
local_index: local_index as usize,
max_block_data_size,
})
}
@@ -262,6 +262,7 @@ impl Validated {
pub struct ParachainWork<Fetch> {
work: Work<Fetch>,
relay_parent: Hash,
local_index: usize,
availability_store: AvailabilityStore,
max_block_data_size: Option<u64>,
}
@@ -272,23 +273,28 @@ impl<Fetch: Future> ParachainWork<Fetch> {
pub fn prime<P: ProvideRuntimeApi>(self, api: Arc<P>)
-> PrimedParachainWork<
Fetch,
impl Send + FnMut(&BlockId, &Collation) -> Result<OutgoingMessages, ()>,
impl Send + FnMut(&BlockId, &PoVBlock, &CandidateReceipt) -> Result<(OutgoingMessages, ErasureChunk), ()>,
>
where
P: Send + Sync + 'static,
P::Api: ParachainHost<Block, Error = sp_blockchain::Error>,
{
let max_block_data_size = self.max_block_data_size;
let validate = move |id: &_, collation: &_| {
let res = crate::collation::validate_collation(
let local_index = self.local_index;
let validate = move |id: &_, pov_block: &_, receipt: &_| {
let res = crate::collation::validate_receipt(
&*api,
id,
collation,
pov_block,
receipt,
max_block_data_size,
);
match res {
Ok(e) => Ok(e),
Ok((messages, mut chunks)) => {
Ok((messages, chunks.swap_remove(local_index)))
}
Err(e) => {
debug!(target: "validation", "Encountered bad collation: {}", e);
Err(())
@@ -301,7 +307,7 @@ impl<Fetch: Future> ParachainWork<Fetch> {
/// Prime the parachain work with a custom validation function.
pub fn prime_with<F>(self, validate: F) -> PrimedParachainWork<Fetch, F>
where F: FnMut(&BlockId, &Collation) -> Result<OutgoingMessages, ()>
where F: FnMut(&BlockId, &PoVBlock, &CandidateReceipt) -> Result<(OutgoingMessages, ErasureChunk), ()>
{
PrimedParachainWork { inner: self, validate }
}
@@ -318,23 +324,21 @@ pub struct PrimedParachainWork<Fetch, F> {
validate: F,
}
impl<Fetch, F, Err> Future for PrimedParachainWork<Fetch, F>
impl<Fetch, F, Err> PrimedParachainWork<Fetch, F>
where
Fetch: Future<Item=PoVBlock,Error=Err>,
F: FnMut(&BlockId, &Collation) -> Result<OutgoingMessages, ()>,
F: FnMut(&BlockId, &PoVBlock, &CandidateReceipt) -> Result<(OutgoingMessages, ErasureChunk), ()>,
Err: From<::std::io::Error>,
{
type Item = Validated;
type Error = Err;
pub async fn validate(mut self) -> Result<(Validated, Option<ErasureChunk>), Err> {
use futures03::compat::Future01CompatExt;
let candidate = &self.inner.work.candidate_receipt;
let pov_block = self.inner.work.fetch.compat().await?;
fn poll(&mut self) -> Poll<Validated, Err> {
let work = &mut self.inner.work;
let candidate = &work.candidate_receipt;
let pov_block = futures::try_ready!(work.fetch.poll());
let validation_res = (self.validate)(
&BlockId::hash(self.inner.relay_parent),
&Collation { pov: pov_block.clone(), receipt: candidate.clone() },
&pov_block,
&candidate,
);
let candidate_hash = candidate.hash();
@@ -342,35 +346,30 @@ impl<Fetch, F, Err> Future for PrimedParachainWork<Fetch, F>
debug!(target: "validation", "Making validity statement about candidate {}: is_good? {:?}",
candidate_hash, validation_res.is_ok());
let (validity_statement, result) = match validation_res {
Err(()) => (
GenericStatement::Invalid(candidate_hash),
Validation::Invalid(pov_block),
),
Ok(outgoing_targeted) => {
let outgoing_queues = crate::outgoing_queues(&outgoing_targeted)
.map(|(_target, root, data)| (root, data))
.collect();
match validation_res {
Err(()) => Ok((
Validated {
statement: GenericStatement::Invalid(candidate_hash),
result: Validation::Invalid(pov_block),
},
None,
)),
Ok((outgoing_targeted, our_chunk)) => {
self.inner.availability_store.add_erasure_chunk(
self.inner.relay_parent,
candidate.clone(),
our_chunk.clone(),
).await?;
self.inner.availability_store.make_available(Data {
relay_parent: self.inner.relay_parent,
parachain_id: work.candidate_receipt.parachain_index,
candidate_hash,
block_data: pov_block.block_data.clone(),
outgoing_queues: Some(outgoing_queues),
})?;
(
GenericStatement::Valid(candidate_hash),
Validation::Valid(pov_block, outgoing_targeted)
)
Ok((
Validated {
statement: GenericStatement::Valid(candidate_hash),
result: Validation::Valid(pov_block, outgoing_targeted),
},
Some(our_chunk),
))
}
};
Ok(Async::Ready(Validated {
statement: validity_statement,
result,
}))
}
}
}
@@ -573,8 +572,11 @@ mod tests {
use super::*;
use sp_keyring::Sr25519Keyring;
use primitives::crypto::UncheckedInto;
use polkadot_primitives::parachain::{BlockData, ConsolidatedIngress};
use futures::future;
use polkadot_primitives::parachain::{AvailableMessages, BlockData, ConsolidatedIngress, Collation};
use polkadot_erasure_coding::{self as erasure};
use availability_store::ProvideGossipMessages;
use futures::{future};
fn pov_block_with_data(data: Vec<u8>) -> PoVBlock {
PoVBlock {
@@ -583,14 +585,39 @@ mod tests {
}
}
#[derive(Clone)]
struct DummyGossipMessages;
impl ProvideGossipMessages for DummyGossipMessages {
fn gossip_messages_for(
&self,
_topic: Hash
) -> Box<dyn futures03::Stream<Item = (Hash, Hash, ErasureChunk)> + Unpin + Send> {
Box::new(futures03::stream::empty())
}
fn gossip_erasure_chunk(
&self,
_relay_parent: Hash,
_candidate_hash: Hash,
_erasure_root: Hash,
_chunk: ErasureChunk,
) {}
}
#[derive(Clone)]
struct DummyRouter;
impl TableRouter for DummyRouter {
type Error = ::std::io::Error;
type FetchValidationProof = future::FutureResult<PoVBlock,Self::Error>;
fn local_collation(&self, _collation: Collation, _outgoing: OutgoingMessages) {
}
fn local_collation(
&self,
_collation: Collation,
_candidate: CandidateReceipt,
_outgoing: OutgoingMessages,
_chunks: (ValidatorIndex, &[ErasureChunk])
) {}
fn fetch_pov_block(&self, _candidate: &CandidateReceipt) -> Self::FetchValidationProof {
future::ok(pov_block_with_data(vec![1, 2, 3, 4, 5]))
@@ -622,7 +649,7 @@ mod tests {
groups,
Some(local_key.clone()),
parent_hash,
AvailabilityStore::new_in_memory(),
AvailabilityStore::new_in_memory(DummyGossipMessages),
None,
);
@@ -635,6 +662,7 @@ mod tests {
fees: 1_000_000,
block_data_hash: [2; 32].into(),
upward_messages: Vec::new(),
erasure_root: [1u8; 32].into(),
};
let candidate_statement = GenericStatement::Candidate(candidate);
@@ -677,7 +705,7 @@ mod tests {
groups,
Some(local_key.clone()),
parent_hash,
AvailabilityStore::new_in_memory(),
AvailabilityStore::new_in_memory(DummyGossipMessages),
None,
);
@@ -690,6 +718,7 @@ mod tests {
fees: 1_000_000,
block_data_hash: [2; 32].into(),
upward_messages: Vec::new(),
erasure_root: [1u8; 32].into(),
};
let candidate_statement = GenericStatement::Candidate(candidate);
@@ -709,10 +738,13 @@ mod tests {
#[test]
fn evaluate_makes_block_data_available() {
let store = AvailabilityStore::new_in_memory();
let store = AvailabilityStore::new_in_memory(DummyGossipMessages);
let relay_parent = [0; 32].into();
let para_id = 5.into();
let pov_block = pov_block_with_data(vec![1, 2, 3]);
let block_data_hash = [2; 32].into();
let local_index = 0;
let n_validators = 2;
let candidate = CandidateReceipt {
parachain_index: para_id,
@@ -721,39 +753,62 @@ mod tests {
head_data: ::polkadot_primitives::parachain::HeadData(vec![1, 2, 3, 4]),
egress_queue_roots: Vec::new(),
fees: 1_000_000,
block_data_hash: [2; 32].into(),
block_data_hash,
upward_messages: Vec::new(),
erasure_root: [1u8; 32].into(),
};
let hash = candidate.hash();
store.add_validator_index_and_n_validators(
&relay_parent,
local_index as u32,
n_validators as u32,
).unwrap();
let producer: ParachainWork<future::FutureResult<_, ::std::io::Error>> = ParachainWork {
work: Work {
candidate_receipt: candidate,
fetch: future::ok(pov_block.clone()),
},
local_index,
relay_parent,
availability_store: store.clone(),
max_block_data_size: None,
};
let validated = producer.prime_with(|_, _| Ok(OutgoingMessages { outgoing_messages: Vec::new() }))
.wait()
.unwrap();
let validated = futures03::executor::block_on(producer.prime_with(|_, _, _| Ok((
OutgoingMessages { outgoing_messages: Vec::new() },
ErasureChunk {
chunk: vec![1, 2, 3],
index: local_index as u32,
proof: vec![],
},
))).validate()).unwrap();
assert_eq!(validated.pov_block(), &pov_block);
assert_eq!(validated.statement, GenericStatement::Valid(hash));
assert_eq!(validated.0.pov_block(), &pov_block);
assert_eq!(validated.0.statement, GenericStatement::Valid(hash));
assert_eq!(store.block_data(relay_parent, hash).unwrap(), pov_block.block_data);
// TODO: check that a message queue is included by root.
if let Some(messages) = validated.0.outgoing_messages() {
let available_messages: AvailableMessages = messages.clone().into();
for (root, queue) in available_messages.0 {
assert_eq!(store.queue_by_root(&root), Some(queue));
}
}
assert!(store.get_erasure_chunk(&relay_parent, block_data_hash, local_index).is_some());
assert!(store.get_erasure_chunk(&relay_parent, block_data_hash, local_index + 1).is_none());
}
#[test]
fn full_availability() {
let store = AvailabilityStore::new_in_memory();
let store = AvailabilityStore::new_in_memory(DummyGossipMessages);
let relay_parent = [0; 32].into();
let para_id = 5.into();
let pov_block = pov_block_with_data(vec![1, 2, 3]);
let block_data_hash = pov_block.block_data.hash();
let local_index = 0;
let n_validators = 2;
let ex = Some(AvailableMessages(Vec::new()));
let candidate = CandidateReceipt {
parachain_index: para_id,
@@ -764,27 +819,48 @@ mod tests {
fees: 1_000_000,
block_data_hash: [2; 32].into(),
upward_messages: Vec::new(),
erasure_root: [1u8; 32].into(),
};
let hash = candidate.hash();
let chunks = erasure::obtain_chunks(n_validators, &pov_block.block_data, ex.as_ref()).unwrap();
store.add_validator_index_and_n_validators(
&relay_parent,
local_index as u32,
n_validators as u32,
).unwrap();
let producer = ParachainWork {
work: Work {
candidate_receipt: candidate,
fetch: future::ok::<_, ::std::io::Error>(pov_block.clone()),
},
local_index,
relay_parent,
availability_store: store.clone(),
max_block_data_size: None,
};
let validated = producer.prime_with(|_, _| Ok(OutgoingMessages { outgoing_messages: Vec::new() }))
.wait()
.unwrap();
let validated = futures03::executor::block_on(producer.prime_with(|_, _, _| Ok((
OutgoingMessages { outgoing_messages: Vec::new() },
ErasureChunk {
chunk: chunks[local_index].clone(),
index: local_index as u32,
proof: vec![],
},
))).validate()).unwrap();
assert_eq!(validated.pov_block(), &pov_block);
assert_eq!(validated.0.pov_block(), &pov_block);
assert_eq!(store.block_data(relay_parent, hash).unwrap(), pov_block.block_data);
if let Some(messages) = validated.0.outgoing_messages() {
let available_messages: AvailableMessages = messages.clone().into();
for (root, queue) in available_messages.0 {
assert_eq!(store.queue_by_root(&root), Some(queue));
}
}
// This works since there are only two validators and one erasure chunk should be
// enough to reconstruct the block data.
assert_eq!(store.block_data(relay_parent, block_data_hash).unwrap(), pov_block.block_data);
// TODO: check that a message queue is included by root.
}
@@ -813,7 +889,7 @@ mod tests {
groups,
Some(local_key.clone()),
parent_hash,
AvailabilityStore::new_in_memory(),
AvailabilityStore::new_in_memory(DummyGossipMessages),
None,
);
@@ -826,6 +902,7 @@ mod tests {
fees: 1_000_000,
block_data_hash: [2; 32].into(),
upward_messages: Vec::new(),
erasure_root: [1u8; 32].into(),
};
let hash = candidate.hash();
@@ -879,7 +956,7 @@ mod tests {
groups,
Some(local_key.clone()),
parent_hash,
AvailabilityStore::new_in_memory(),
AvailabilityStore::new_in_memory(DummyGossipMessages),
None,
);
@@ -892,6 +969,7 @@ mod tests {
fees: 1_000_000,
block_data_hash: [2; 32].into(),
upward_messages: Vec::new(),
erasure_root: [1u8; 32].into(),
};
let hash = candidate.hash();