A more comprehensive model for PoV-Blocks and Candidate receipts (#843)

* encode the candidate statement as only the hash

* refactor CandidateReceipt and CollationInfo

* introduce an abridged candidate receipt type

* erasure coding stores candidate receipt

* store omitted data instead and introduce AvailableData type

* refactor availability-store schema

* tweak schema and APIs a bit more

* get availability-store tests passing

* accept AbridgedCandidateReceipt in `set_heads`

* change statement type in primitives to be hash-only

* fix parachains runtime tests

* fix bad merge

* rewrite validation pipeline

* remove evaluation module

* use abridged candidate hash as canonical

* statement table uses abridged candidate receipts

* kill availability_store::Data struct

* port shared table to new validation pipelines

* extract full validation pipeline to helper

* remove old validation pipeline from collation module

* polkadot-validation compiles

* polkadot-validation tests compile

* make local collation available in validation service

* port legacy network code

* polkadot-network fully ported

* network: ensure fresh statement is propagated

* remove pov_block_hash from LocalValidationData

* remove candidate_hash field from AttestedCandidate and update runtime

* port runtimes to new ParachainHost definition

* port over polkadot-collator

* fix test compilation

* better fix

* remove unrelated validation work dispatch fix

* address grumbles

* fix equality check
This commit is contained in:
Robert Habermeier
2020-02-25 15:16:58 -08:00
committed by GitHub
parent 1f9d2af08e
commit b7d30aa379
29 changed files with 1718 additions and 1704 deletions
+67 -81
View File
@@ -28,7 +28,8 @@ use keystore::KeyStorePtr;
use polkadot_primitives::{
Hash, Block,
parachain::{
Id as ParaId, BlockData, CandidateReceipt, ErasureChunk, ParachainHost
PoVBlock, AbridgedCandidateReceipt, ErasureChunk,
ParachainHost, AvailableData, OmittedValidationData,
},
};
use sp_runtime::traits::{BlakeTwo256, Hash as HashT, HasherFor};
@@ -37,6 +38,7 @@ use client::{
BlockchainEvents, BlockBody,
};
use sp_api::{ApiExt, ProvideRuntimeApi};
use codec::{Encode, Decode};
use log::warn;
@@ -50,9 +52,10 @@ mod worker;
mod store;
pub use worker::AvailabilityBlockImport;
pub use store::AwaitedFrontierEntry;
use worker::{
Worker, WorkerHandle, Chunks, ParachainBlocks, WorkerMsg, MakeAvailable,
Worker, WorkerHandle, Chunks, IncludedParachainBlocks, WorkerMsg, MakeAvailable,
};
use store::{Store as InnerStore};
@@ -116,15 +119,14 @@ pub trait ProvideGossipMessages {
);
}
/// Some data to keep available about a parachain block candidate.
#[derive(Debug)]
pub struct Data {
/// The relay chain parent hash this should be localized to.
pub relay_parent: Hash,
/// The parachain index for this candidate.
pub parachain_id: ParaId,
/// Block data.
pub block_data: BlockData,
/// Data which, when combined with an `AbridgedCandidateReceipt`, is enough
/// to fully re-execute a block.
#[derive(Debug, Encode, Decode, PartialEq)]
pub struct ExecutionData {
/// The `PoVBlock`.
pub pov_block: PoVBlock,
/// The data omitted from the `AbridgedCandidateReceipt`.
pub omitted_validation: OmittedValidationData,
}
/// Handle to the availability store.
@@ -220,17 +222,17 @@ impl Store {
/// in order to persist that data to disk and so it can be queried and provided
/// to other nodes in the network.
///
/// The message data of `Data` is optional but is expected
/// to be present with the exception of the case where there is no message data
/// due to the block's invalidity. Determination of invalidity is beyond the
/// scope of this function.
/// Determination of invalidity is beyond the scope of this function.
///
/// This method will send the `Data` to the background worker, allowing caller to
/// asynchrounously wait for the result.
pub async fn make_available(&self, data: Data) -> io::Result<()> {
/// This method will send the data to the background worker, allowing the caller to
/// asynchronously wait for the result.
pub async fn make_available(&self, candidate_hash: Hash, available_data: AvailableData)
-> io::Result<()>
{
let (s, r) = oneshot::channel();
let msg = WorkerMsg::MakeAvailable(MakeAvailable {
data,
candidate_hash,
available_data,
result: s,
});
@@ -244,41 +246,11 @@ impl Store {
}
/// Get a set of all chunks we are waiting for grouped by
/// `(relay_parent, erasure_root, candidate_hash, our_id)`.
pub fn awaited_chunks(&self) -> Option<HashSet<(Hash, Hash, Hash, u32)>> {
/// Get a set of all chunks we are waiting for.
pub fn awaited_chunks(&self) -> Option<HashSet<AwaitedFrontierEntry>> {
self.inner.awaited_chunks()
}
/// Qery which candidates were included in the relay chain block by block's parent.
pub fn get_candidates_in_relay_block(&self, relay_block: &Hash) -> Option<Vec<Hash>> {
self.inner.get_candidates_in_relay_block(relay_block)
}
/// Make a validator's index and a number of validators at a relay parent available.
///
/// This information is needed before the `add_candidates_in_relay_block` is called
/// since that call forms the awaited frontier of chunks.
/// In the current implementation this function is called in the `get_or_instantiate` at
/// the start of the parachain agreement process on top of some parent hash.
pub fn add_validator_index_and_n_validators(
&self,
relay_parent: &Hash,
validator_index: u32,
n_validators: u32,
) -> io::Result<()> {
self.inner.add_validator_index_and_n_validators(
relay_parent,
validator_index,
n_validators,
)
}
/// Query a validator's index and n_validators by relay parent.
pub fn get_validator_index_and_n_validators(&self, relay_parent: &Hash) -> Option<(u32, u32)> {
self.inner.get_validator_index_and_n_validators(relay_parent)
}
/// Adds an erasure chunk to storage.
///
/// The chunk should be checked for validity against the root of encoding
@@ -288,11 +260,10 @@ impl Store {
/// asynchrounously wait for the result.
pub async fn add_erasure_chunk(
&self,
relay_parent: Hash,
receipt: CandidateReceipt,
candidate: AbridgedCandidateReceipt,
chunk: ErasureChunk,
) -> io::Result<()> {
self.add_erasure_chunks(relay_parent, receipt, vec![chunk]).await
self.add_erasure_chunks(candidate, vec![chunk]).await
}
/// Adds a set of erasure chunks to storage.
@@ -304,16 +275,17 @@ impl Store {
/// asynchrounously waiting for the result.
pub async fn add_erasure_chunks<I>(
&self,
relay_parent: Hash,
receipt: CandidateReceipt,
candidate: AbridgedCandidateReceipt,
chunks: I,
) -> io::Result<()>
where I: IntoIterator<Item = ErasureChunk>
{
self.add_candidate(relay_parent, receipt.clone()).await?;
let candidate_hash = candidate.hash();
let relay_parent = candidate.relay_parent;
self.add_candidate(candidate).await?;
let (s, r) = oneshot::channel();
let chunks = chunks.into_iter().collect();
let candidate_hash = receipt.hash();
let msg = WorkerMsg::Chunks(Chunks {
relay_parent,
candidate_hash,
@@ -330,27 +302,44 @@ impl Store {
}
}
/// Queries an erasure chunk by its block's parent and hash and index.
/// Queries an erasure chunk by the candidate hash and validator index.
pub fn get_erasure_chunk(
&self,
relay_parent: &Hash,
block_data_hash: Hash,
index: usize,
candidate_hash: &Hash,
validator_index: usize,
) -> Option<ErasureChunk> {
self.inner.get_erasure_chunk(relay_parent, block_data_hash, index)
self.inner.get_erasure_chunk(candidate_hash, validator_index)
}
/// Stores a candidate receipt.
pub async fn add_candidate(
/// Note a validator's index and a number of validators at a relay parent in the
/// store.
///
/// This should be done before adding erasure chunks with this relay parent.
pub fn note_validator_index_and_n_validators(
&self,
relay_parent: Hash,
receipt: CandidateReceipt,
relay_parent: &Hash,
validator_index: u32,
n_validators: u32,
) -> io::Result<()> {
self.inner.note_validator_index_and_n_validators(
relay_parent,
validator_index,
n_validators,
)
}
// Stores a candidate receipt.
async fn add_candidate(
&self,
candidate: AbridgedCandidateReceipt,
) -> io::Result<()> {
let (s, r) = oneshot::channel();
let msg = WorkerMsg::ParachainBlocks(ParachainBlocks {
relay_parent,
blocks: vec![(receipt, None)],
let msg = WorkerMsg::IncludedParachainBlocks(IncludedParachainBlocks {
blocks: vec![crate::worker::IncludedParachainBlock {
candidate,
available_data: None,
}],
result: s,
});
@@ -363,20 +352,17 @@ impl Store {
}
}
/// Queries a candidate receipt by it's hash.
pub fn get_candidate(&self, candidate_hash: &Hash) -> Option<CandidateReceipt> {
/// Queries a candidate receipt by its hash.
pub fn get_candidate(&self, candidate_hash: &Hash)
-> Option<AbridgedCandidateReceipt>
{
self.inner.get_candidate(candidate_hash)
}
/// Query block data.
pub fn block_data(&self, relay_parent: Hash, block_data_hash: Hash) -> Option<BlockData> {
self.inner.block_data(relay_parent, block_data_hash)
}
/// Query block data by corresponding candidate receipt's hash.
pub fn block_data_by_candidate(&self, relay_parent: Hash, candidate_hash: Hash)
-> Option<BlockData>
/// Query execution data by pov-block hash.
pub fn execution_data(&self, candidate_hash: &Hash)
-> Option<ExecutionData>
{
self.inner.block_data_by_candidate(relay_parent, candidate_hash)
self.inner.execution_data(candidate_hash)
}
}
+220 -230
View File
@@ -21,8 +21,11 @@ use codec::{Encode, Decode};
use polkadot_erasure_coding::{self as erasure};
use polkadot_primitives::{
Hash,
parachain::{BlockData, CandidateReceipt, ErasureChunk},
parachain::{
ErasureChunk, AvailableData, AbridgedCandidateReceipt,
},
};
use parking_lot::Mutex;
use log::{trace, warn};
use std::collections::HashSet;
@@ -30,7 +33,7 @@ use std::sync::Arc;
use std::iter::FromIterator;
use std::io;
use crate::{LOG_TARGET, Data, Config};
use crate::{LOG_TARGET, Config, ExecutionData};
mod columns {
pub const DATA: u32 = 0;
@@ -41,42 +44,51 @@ mod columns {
#[derive(Clone)]
pub struct Store {
inner: Arc<dyn KeyValueDB>,
candidate_descendents_lock: Arc<Mutex<()>>
}
fn block_data_key(relay_parent: &Hash, block_data_hash: &Hash) -> Vec<u8> {
(relay_parent, block_data_hash, 0i8).encode()
// data keys
fn execution_data_key(candidate_hash: &Hash) -> Vec<u8> {
(candidate_hash, 0i8).encode()
}
fn erasure_chunks_key(relay_parent: &Hash, block_data_hash: &Hash) -> Vec<u8> {
(relay_parent, block_data_hash, 1i8).encode()
}
fn awaited_chunks_key() -> Vec<u8> {
"awaited_chunks_key".encode()
}
fn available_chunks_key(relay_parent: &Hash, erasure_root: &Hash) -> Vec<u8> {
(relay_parent, erasure_root, 2i8).encode()
}
fn block_to_candidate_key(block_data_hash: &Hash) -> Vec<u8> {
(block_data_hash, 1i8).encode()
fn erasure_chunks_key(candidate_hash: &Hash) -> Vec<u8> {
(candidate_hash, 1i8).encode()
}
fn candidate_key(candidate_hash: &Hash) -> Vec<u8> {
(candidate_hash, 2i8).encode()
}
fn validator_index_and_n_validators_key(relay_parent: &Hash) -> Vec<u8> {
(relay_parent, 3i8).encode()
fn available_chunks_key(relay_parent: &Hash, erasure_root: &Hash) -> Vec<u8> {
(relay_parent, erasure_root, 3i8).encode()
}
fn candidates_in_relay_chain_block_key(relay_block: &Hash) -> Vec<u8> {
fn candidates_with_relay_parent_key(relay_block: &Hash) -> Vec<u8> {
(relay_block, 4i8).encode()
}
fn erasure_roots_in_relay_chain_block_key(relay_block: &Hash) -> Vec<u8> {
(relay_block, 5i8).encode()
// meta keys
fn awaited_chunks_key() -> [u8; 14] {
*b"awaited_chunks"
}
fn validator_index_and_n_validators_key(relay_parent: &Hash) -> Vec<u8> {
(relay_parent, 1i8).encode()
}
/// An entry in the awaited frontier of chunks we are interested in.
#[derive(Encode, Decode, Debug, Hash, PartialEq, Eq, Clone)]
pub struct AwaitedFrontierEntry {
/// The relay-chain parent block hash.
pub relay_parent: Hash,
/// The erasure-chunk trie root we are comparing against.
///
/// We index by erasure-root because there may be multiple candidates
/// with the same erasure root.
pub erasure_root: Hash,
/// The index of the validator we represent.
pub validator_index: u32,
}
impl Store {
@@ -103,6 +115,7 @@ impl Store {
Ok(Store {
inner: Arc::new(db),
candidate_descendents_lock: Arc::new(Mutex::new(())),
})
}
@@ -110,31 +123,37 @@ impl Store {
pub(super) fn new_in_memory() -> Self {
Store {
inner: Arc::new(::kvdb_memorydb::create(columns::NUM_COLUMNS)),
candidate_descendents_lock: Arc::new(Mutex::new(())),
}
}
/// Make some data available provisionally.
pub(crate) fn make_available(&self, data: Data) -> io::Result<()> {
pub(crate) fn make_available(&self, candidate_hash: Hash, available_data: AvailableData)
-> io::Result<()>
{
let mut tx = DBTransaction::new();
// note the meta key.
let mut v = self.query_inner(columns::META, data.relay_parent.as_ref()).unwrap_or(Vec::new());
v.push(data.block_data.hash());
tx.put_vec(columns::META, &data.relay_parent[..], v.encode());
// at the moment, these structs are identical. later, we will also
// keep outgoing message queues available, and these are not needed
// for execution.
let AvailableData { pov_block, omitted_validation } = available_data;
let execution_data = ExecutionData {
pov_block,
omitted_validation,
};
tx.put_vec(
columns::DATA,
block_data_key(&data.relay_parent, &data.block_data.hash()).as_slice(),
data.block_data.encode()
execution_data_key(&candidate_hash).as_slice(),
execution_data.encode(),
);
self.inner.write(tx)
}
/// Get a set of all chunks we are waiting for grouped by
/// `(relay_parent, erasure_root, candidate_hash, our_id)`.
pub fn awaited_chunks(&self) -> Option<HashSet<(Hash, Hash, Hash, u32)>> {
self.query_inner(columns::META, &awaited_chunks_key()).map(|vec: Vec<(Hash, Hash, Hash, u32)>| {
/// Get a set of all chunks we are waiting for.
pub fn awaited_chunks(&self) -> Option<HashSet<AwaitedFrontierEntry>> {
self.query_inner(columns::META, &awaited_chunks_key()).map(|vec: Vec<AwaitedFrontierEntry>| {
HashSet::from_iter(vec.into_iter())
})
}
@@ -147,60 +166,49 @@ impl Store {
/// This method modifies the erasure chunks awaited frontier by adding this validator's
/// chunks from `candidates` to it. In order to do so the information about this validator's
/// position at parent `relay_parent` should be known to the store prior to calling this
/// method, in other words `add_validator_index_and_n_validators` should be called for
/// method, in other words `note_validator_index_and_n_validators` should be called for
/// the given `relay_parent` before calling this function.
pub(crate) fn add_candidates_in_relay_block(
pub(crate) fn note_candidates_with_relay_parent(
&self,
relay_parent: &Hash,
candidates: Vec<Hash>,
candidates: &[Hash],
) -> io::Result<()> {
let mut tx = DBTransaction::new();
let dbkey = candidates_in_relay_chain_block_key(relay_parent);
let dbkey = candidates_with_relay_parent_key(relay_parent);
// This call can race against another call to `note_candidates_with_relay_parent`
// with a different set of descendents.
let _lock = self.candidate_descendents_lock.lock();
if let Some((validator_index, _)) = self.get_validator_index_and_n_validators(relay_parent) {
let candidates = candidates.clone();
let awaited_frontier: Vec<(Hash, Hash, Hash, u32)> = self
let awaited_frontier: Vec<AwaitedFrontierEntry> = self
.query_inner(columns::META, &awaited_chunks_key())
.unwrap_or_else(|| Vec::new());
let mut awaited_frontier: HashSet<(Hash, Hash, Hash, u32)> =
let mut awaited_frontier: HashSet<AwaitedFrontierEntry> =
HashSet::from_iter(awaited_frontier.into_iter());
awaited_frontier.extend(candidates.into_iter().filter_map(|candidate| {
self.get_candidate(&candidate)
.map(|receipt| (relay_parent.clone(), receipt.erasure_root, candidate, validator_index))
awaited_frontier.extend(candidates.iter().filter_map(|candidate| {
self.get_candidate(&candidate).map(|receipt| AwaitedFrontierEntry {
relay_parent: relay_parent.clone(),
erasure_root: receipt.commitments.erasure_root,
validator_index,
})
}));
let awaited_frontier = Vec::from_iter(awaited_frontier.into_iter());
tx.put_vec(columns::META, &awaited_chunks_key(), awaited_frontier.encode());
}
tx.put_vec(columns::DATA, &dbkey, candidates.encode());
self.inner.write(tx)
}
/// Qery which candidates were included in the relay chain block by block's parent.
pub fn get_candidates_in_relay_block(&self, relay_block: &Hash) -> Option<Vec<Hash>> {
let dbkey = candidates_in_relay_chain_block_key(relay_block);
self.query_inner(columns::DATA, &dbkey)
}
/// Adds a set of erasure chunk roots that were included in a relay block by block's parent.
pub(crate) fn add_erasure_roots_in_relay_block(
&self,
relay_parent: &Hash,
erasure_roots: Vec<Hash>,
) -> io::Result<()> {
let mut tx = DBTransaction::new();
let dbkey = erasure_roots_in_relay_chain_block_key(relay_parent);
tx.put_vec(columns::DATA, &dbkey, erasure_roots.encode());
let mut descendent_candidates = self.get_candidates_with_relay_parent(relay_parent);
descendent_candidates.extend(candidates.iter().cloned());
tx.put_vec(columns::DATA, &dbkey, descendent_candidates.encode());
self.inner.write(tx)
}
/// Make a validator's index and a number of validators at a relay parent available.
pub(crate) fn add_validator_index_and_n_validators(
pub(crate) fn note_validator_index_and_n_validators(
&self,
relay_parent: &Hash,
validator_index: u32,
@@ -215,7 +223,7 @@ impl Store {
}
/// Query a validator's index and n_validators by relay parent.
pub fn get_validator_index_and_n_validators(&self, relay_parent: &Hash) -> Option<(u32, u32)> {
pub(crate) fn get_validator_index_and_n_validators(&self, relay_parent: &Hash) -> Option<(u32, u32)> {
let dbkey = validator_index_and_n_validators_key(relay_parent);
self.query_inner(columns::META, &dbkey)
@@ -224,11 +232,9 @@ impl Store {
/// Add a set of chunks.
///
/// The same as `add_erasure_chunk` but adds a set of chunks in one atomic transaction.
/// Checks that all chunks have the same `relay_parent`, `block_data_hash` and `parachain_id` fields.
pub fn add_erasure_chunks<I>(
&self,
n_validators: u32,
relay_parent: &Hash,
candidate_hash: &Hash,
chunks: I,
) -> io::Result<()>
@@ -236,16 +242,19 @@ impl Store {
{
if let Some(receipt) = self.get_candidate(candidate_hash) {
let mut tx = DBTransaction::new();
let dbkey = erasure_chunks_key(relay_parent, &receipt.block_data_hash);
let dbkey = erasure_chunks_key(candidate_hash);
let mut v = self.query_inner(columns::DATA, &dbkey).unwrap_or(Vec::new());
let av_chunks_key = available_chunks_key(relay_parent, &receipt.erasure_root);
let av_chunks_key = available_chunks_key(
&receipt.relay_parent,
&receipt.commitments.erasure_root,
);
let mut have_chunks = self.query_inner(columns::META, &av_chunks_key).unwrap_or(Vec::new());
let awaited_frontier: Option<Vec<(Hash, Hash, Hash, u32)>> = self.query_inner(
let awaited_frontier: Option<Vec<AwaitedFrontierEntry>> = self.query_inner(
columns::META,
&awaited_chunks_key()
&awaited_chunks_key(),
);
for chunk in chunks.into_iter() {
@@ -256,31 +265,23 @@ impl Store {
}
if let Some(mut awaited_frontier) = awaited_frontier {
awaited_frontier.retain(|&(p, r, c, index)| {
awaited_frontier.retain(|entry| {
!(
*relay_parent == p &&
r == receipt.erasure_root &&
c == receipt.hash() &&
have_chunks.contains(&index)
entry.relay_parent == receipt.relay_parent &&
entry.erasure_root == receipt.commitments.erasure_root &&
have_chunks.contains(&entry.validator_index)
)
});
tx.put_vec(columns::META, &awaited_chunks_key(), awaited_frontier.encode());
}
// If therea are no block data and messages in the store at this point,
// If therea are no block data in the store at this point,
// check that they can be reconstructed now and add them to store if they can.
if let Ok(None) = self.inner.get(
columns::DATA,
&block_data_key(&relay_parent, &receipt.block_data_hash)
) {
if let Ok(block_data) = erasure::reconstruct(
if self.execution_data(&candidate_hash).is_none() {
if let Ok(available_data) = erasure::reconstruct(
n_validators as usize,
v.iter().map(|chunk| (chunk.chunk.as_ref(), chunk.index as usize))) {
self.make_available(Data {
relay_parent: *relay_parent,
parachain_id: receipt.parachain_index,
block_data,
})?;
self.make_available(*candidate_hash, available_data)?;
}
}
@@ -294,14 +295,13 @@ impl Store {
}
}
/// Queries an erasure chunk by its block's parent and hash and index.
/// Queries an erasure chunk by its block's relay-parent, the candidate hash, and index.
pub fn get_erasure_chunk(
&self,
relay_parent: &Hash,
block_data_hash: Hash,
candidate_hash: &Hash,
index: usize,
) -> Option<ErasureChunk> {
self.query_inner(columns::DATA, &erasure_chunks_key(&relay_parent, &block_data_hash))
self.query_inner(columns::DATA, &erasure_chunks_key(candidate_hash))
.and_then(|chunks: Vec<ErasureChunk>| {
chunks.iter()
.find(|chunk: &&ErasureChunk| chunk.index == index as u32)
@@ -310,70 +310,66 @@ impl Store {
}
/// Stores a candidate receipt.
pub fn add_candidate(&self, receipt: &CandidateReceipt) -> io::Result<()> {
let dbkey = candidate_key(&receipt.hash());
pub fn add_candidate(
&self,
receipt: &AbridgedCandidateReceipt,
) -> io::Result<()> {
let candidate_hash = receipt.hash();
let dbkey = candidate_key(&candidate_hash);
let mut tx = DBTransaction::new();
tx.put_vec(columns::DATA, &dbkey, receipt.encode());
tx.put_vec(columns::META, &block_to_candidate_key(&receipt.block_data_hash), receipt.hash().encode());
self.inner.write(tx)
}
/// Queries a candidate receipt by it's hash.
pub fn get_candidate(&self, candidate_hash: &Hash) -> Option<CandidateReceipt> {
/// Queries a candidate receipt by the relay parent hash and its hash.
pub(crate) fn get_candidate(&self, candidate_hash: &Hash)
-> Option<AbridgedCandidateReceipt>
{
self.query_inner(columns::DATA, &candidate_key(candidate_hash))
}
/// Note that a set of candidates have been included in a finalized block with given hash and parent hash.
pub fn candidates_finalized(
pub(crate) fn candidates_finalized(
&self,
parent: Hash,
relay_parent: Hash,
finalized_candidates: HashSet<Hash>,
) -> io::Result<()> {
let mut tx = DBTransaction::new();
let v = self.query_inner(columns::META, &parent[..]).unwrap_or(Vec::new());
tx.delete(columns::META, &parent[..]);
let awaited_frontier: Option<Vec<(Hash, Hash, Hash, u32)>> = self
let awaited_frontier: Option<Vec<AwaitedFrontierEntry>> = self
.query_inner(columns::META, &awaited_chunks_key());
if let Some(mut awaited_frontier) = awaited_frontier {
awaited_frontier.retain(|&(p, c, _, _)| (p != parent && !finalized_candidates.contains(&c)));
awaited_frontier.retain(|entry| entry.relay_parent != relay_parent);
tx.put_vec(columns::META, &awaited_chunks_key(), awaited_frontier.encode());
}
for block_data_hash in v {
if let Some(candidate_hash) = self.block_hash_to_candidate_hash(block_data_hash) {
if !finalized_candidates.contains(&candidate_hash) {
tx.delete(columns::DATA, block_data_key(&parent, &block_data_hash).as_slice());
tx.delete(columns::DATA, &erasure_chunks_key(&parent, &block_data_hash));
tx.delete(columns::DATA, &candidate_key(&candidate_hash));
tx.delete(columns::META, &block_to_candidate_key(&block_data_hash));
}
}
let candidates = self.get_candidates_with_relay_parent(&relay_parent);
for candidate in candidates.into_iter().filter(|c| !finalized_candidates.contains(c)) {
// we only delete this data for candidates which were not finalized.
// we keep all data for the finalized chain forever at the moment.
tx.delete(columns::DATA, execution_data_key(&candidate).as_slice());
tx.delete(columns::DATA, &erasure_chunks_key(&candidate));
tx.delete(columns::DATA, &candidate_key(&candidate));
}
self.inner.write(tx)
}
/// Query block data.
pub fn block_data(&self, relay_parent: Hash, block_data_hash: Hash) -> Option<BlockData> {
self.query_inner(columns::DATA, &block_data_key(&relay_parent, &block_data_hash))
/// Query execution data by relay parent and candidate hash.
pub(crate) fn execution_data(&self, candidate_hash: &Hash) -> Option<ExecutionData> {
self.query_inner(columns::DATA, &execution_data_key(candidate_hash))
}
/// Query block data by corresponding candidate receipt's hash.
pub fn block_data_by_candidate(&self, relay_parent: Hash, candidate_hash: Hash) -> Option<BlockData> {
let receipt_key = candidate_key(&candidate_hash);
self.query_inner(columns::DATA, &receipt_key[..]).and_then(|receipt: CandidateReceipt| {
self.block_data(relay_parent, receipt.block_data_hash)
})
}
fn block_hash_to_candidate_hash(&self, block_hash: Hash) -> Option<Hash> {
self.query_inner(columns::META, &block_to_candidate_key(&block_hash))
/// Get candidates which pinned to the environment of the given relay parent.
/// Note that this is not necessarily the same as candidates that were included in a direct
/// descendent of the given relay-parent.
fn get_candidates_with_relay_parent(&self, relay_parent: &Hash) -> Vec<Hash> {
let key = candidates_with_relay_parent_key(relay_parent);
self.query_inner(columns::DATA, &key[..]).unwrap_or_default()
}
fn query_inner<T: Decode>(&self, column: u32, key: &[u8]) -> Option<T> {
@@ -394,8 +390,27 @@ impl Store {
#[cfg(test)]
mod tests {
use super::*;
use polkadot_erasure_coding as erasure;
use polkadot_primitives::parachain::Id as ParaId;
use polkadot_erasure_coding::{self as erasure};
use polkadot_primitives::parachain::{
Id as ParaId, BlockData, AvailableData, PoVBlock, OmittedValidationData,
};
fn available_data(block_data: &[u8]) -> AvailableData {
AvailableData {
pov_block: PoVBlock {
block_data: BlockData(block_data.to_vec()),
},
omitted_validation: OmittedValidationData {
global_validation: Default::default(),
local_validation: Default::default(),
}
}
}
fn execution_data(available: &AvailableData) -> ExecutionData {
let AvailableData { pov_block, omitted_validation } = available.clone();
ExecutionData { pov_block, omitted_validation }
}
#[test]
fn finalization_removes_unneeded() {
@@ -404,8 +419,23 @@ mod tests {
let para_id_1 = 5.into();
let para_id_2 = 6.into();
let block_data_1 = BlockData(vec![1, 2, 3]);
let block_data_2 = BlockData(vec![4, 5, 6]);
let mut candidate_1 = AbridgedCandidateReceipt::default();
let mut candidate_2 = AbridgedCandidateReceipt::default();
candidate_1.parachain_index = para_id_1;
candidate_1.commitments.erasure_root = [6; 32].into();
candidate_1.relay_parent = relay_parent;
candidate_2.parachain_index = para_id_2;
candidate_2.commitments.erasure_root = [6; 32].into();
candidate_2.relay_parent = relay_parent;
let candidate_1_hash = candidate_1.hash();
let candidate_2_hash = candidate_2.hash();
let available_data_1 = available_data(&[1, 2, 3]);
let available_data_2 = available_data(&[4, 5, 6]);
let erasure_chunk_1 = ErasureChunk {
chunk: vec![10, 20, 30],
@@ -420,112 +450,59 @@ mod tests {
};
let store = Store::new_in_memory();
store.make_available(Data {
relay_parent,
parachain_id: para_id_1,
block_data: block_data_1.clone(),
}).unwrap();
store.make_available(candidate_1_hash, available_data_1.clone()).unwrap();
store.make_available(Data {
relay_parent,
parachain_id: para_id_2,
block_data: block_data_2.clone(),
}).unwrap();
let candidate_1 = CandidateReceipt {
parachain_index: para_id_1,
collator: Default::default(),
signature: Default::default(),
head_data: Default::default(),
parent_head: Default::default(),
fees: 0,
block_data_hash: block_data_1.hash(),
upward_messages: Vec::new(),
erasure_root: [6; 32].into(),
};
let candidate_2 = CandidateReceipt {
parachain_index: para_id_2,
collator: Default::default(),
signature: Default::default(),
head_data: Default::default(),
parent_head: Default::default(),
fees: 0,
block_data_hash: block_data_2.hash(),
upward_messages: Vec::new(),
erasure_root: [6; 32].into(),
};
store.make_available(candidate_2_hash, available_data_2.clone()).unwrap();
store.add_candidate(&candidate_1).unwrap();
store.add_candidate(&candidate_2).unwrap();
assert!(store.add_erasure_chunks(3, &relay_parent, &candidate_1.hash(), vec![erasure_chunk_1.clone()]).is_ok());
assert!(store.add_erasure_chunks(3, &relay_parent, &candidate_2.hash(), vec![erasure_chunk_2.clone()]).is_ok());
store.note_candidates_with_relay_parent(&relay_parent, &[candidate_1_hash, candidate_2_hash]).unwrap();
assert_eq!(store.block_data(relay_parent, block_data_1.hash()).unwrap(), block_data_1);
assert_eq!(store.block_data(relay_parent, block_data_2.hash()).unwrap(), block_data_2);
assert!(store.add_erasure_chunks(3, &candidate_1_hash, vec![erasure_chunk_1.clone()]).is_ok());
assert!(store.add_erasure_chunks(3, &candidate_2_hash, vec![erasure_chunk_2.clone()]).is_ok());
assert_eq!(store.get_erasure_chunk(&relay_parent, block_data_1.hash(), 1).as_ref(), Some(&erasure_chunk_1));
assert_eq!(store.get_erasure_chunk(&relay_parent, block_data_2.hash(), 1), Some(erasure_chunk_2));
assert_eq!(store.execution_data(&candidate_1_hash).unwrap(), execution_data(&available_data_1));
assert_eq!(store.execution_data(&candidate_2_hash).unwrap(), execution_data(&available_data_2));
assert_eq!(store.get_candidate(&candidate_1.hash()), Some(candidate_1.clone()));
assert_eq!(store.get_candidate(&candidate_2.hash()), Some(candidate_2.clone()));
assert_eq!(store.get_erasure_chunk(&candidate_1_hash, 1).as_ref(), Some(&erasure_chunk_1));
assert_eq!(store.get_erasure_chunk(&candidate_2_hash, 1), Some(erasure_chunk_2));
assert_eq!(store.block_data_by_candidate(relay_parent, candidate_1.hash()).unwrap(), block_data_1);
assert_eq!(store.block_data_by_candidate(relay_parent, candidate_2.hash()).unwrap(), block_data_2);
assert_eq!(store.get_candidate(&candidate_1_hash), Some(candidate_1.clone()));
assert_eq!(store.get_candidate(&candidate_2_hash), Some(candidate_2.clone()));
store.candidates_finalized(relay_parent, [candidate_1.hash()].iter().cloned().collect()).unwrap();
store.candidates_finalized(relay_parent, [candidate_1_hash].iter().cloned().collect()).unwrap();
assert_eq!(store.get_erasure_chunk(&relay_parent, block_data_1.hash(), 1).as_ref(), Some(&erasure_chunk_1));
assert!(store.get_erasure_chunk(&relay_parent, block_data_2.hash(), 1).is_none());
assert_eq!(store.get_erasure_chunk(&candidate_1_hash, 1).as_ref(), Some(&erasure_chunk_1));
assert!(store.get_erasure_chunk(&candidate_2_hash, 1).is_none());
assert_eq!(store.get_candidate(&candidate_1.hash()), Some(candidate_1));
assert_eq!(store.get_candidate(&candidate_2.hash()), None);
assert_eq!(store.get_candidate(&candidate_1_hash), Some(candidate_1));
assert_eq!(store.get_candidate(&candidate_2_hash), None);
assert_eq!(store.block_data(relay_parent, block_data_1.hash()).unwrap(), block_data_1);
assert!(store.block_data(relay_parent, block_data_2.hash()).is_none());
}
#[test]
fn queues_available_by_queue_root() {
let relay_parent = [1; 32].into();
let para_id = 5.into();
let block_data = BlockData(vec![1, 2, 3]);
let store = Store::new_in_memory();
store.make_available(Data {
relay_parent,
parachain_id: para_id,
block_data,
}).unwrap();
assert_eq!(store.execution_data(&candidate_1_hash).unwrap(), execution_data(&available_data_1));
assert!(store.execution_data(&candidate_2_hash).is_none());
}
#[test]
fn erasure_coding() {
let relay_parent: Hash = [1; 32].into();
let para_id: ParaId = 5.into();
let block_data = BlockData(vec![42; 8]);
let block_data_hash = block_data.hash();
let available_data = available_data(&[42; 8]);
let n_validators = 5;
let erasure_chunks = erasure::obtain_chunks(
n_validators,
&block_data,
&available_data,
).unwrap();
let branches = erasure::branches(erasure_chunks.as_ref());
let candidate = CandidateReceipt {
parachain_index: para_id,
collator: Default::default(),
signature: Default::default(),
head_data: Default::default(),
parent_head: Default::default(),
fees: 0,
block_data_hash: block_data.hash(),
upward_messages: Vec::new(),
erasure_root: [6; 32].into(),
};
let mut candidate = AbridgedCandidateReceipt::default();
candidate.parachain_index = para_id;
candidate.commitments.erasure_root = [6; 32].into();
candidate.relay_parent = relay_parent;
let candidate_hash = candidate.hash();
let chunks: Vec<_> = erasure_chunks
.iter()
@@ -541,13 +518,13 @@ mod tests {
let store = Store::new_in_memory();
store.add_candidate(&candidate).unwrap();
store.add_erasure_chunks(n_validators as u32, &relay_parent, &candidate.hash(), vec![chunks[0].clone()]).unwrap();
assert_eq!(store.get_erasure_chunk(&relay_parent, block_data_hash, 0), Some(chunks[0].clone()));
store.add_erasure_chunks(n_validators as u32, &candidate_hash, vec![chunks[0].clone()]).unwrap();
assert_eq!(store.get_erasure_chunk(&candidate_hash, 0), Some(chunks[0].clone()));
assert!(store.block_data(relay_parent, block_data_hash).is_none());
assert!(store.execution_data(&candidate_hash).is_none());
store.add_erasure_chunks(n_validators as u32, &relay_parent, &candidate.hash(), chunks).unwrap();
assert_eq!(store.block_data(relay_parent, block_data_hash), Some(block_data));
store.add_erasure_chunks(n_validators as u32, &candidate_hash, chunks).unwrap();
assert_eq!(store.execution_data(&candidate_hash), Some(execution_data(&available_data)));
}
#[test]
@@ -555,7 +532,7 @@ mod tests {
let relay_parent = [42; 32].into();
let store = Store::new_in_memory();
store.add_validator_index_and_n_validators(&relay_parent, 42, 24).unwrap();
store.note_validator_index_and_n_validators(&relay_parent, 42, 24).unwrap();
assert_eq!(store.get_validator_index_and_n_validators(&relay_parent).unwrap(), (42, 24));
}
@@ -566,8 +543,8 @@ mod tests {
let candidates = vec![[1; 32].into(), [2; 32].into(), [3; 32].into()];
store.add_candidates_in_relay_block(&relay_parent, candidates.clone()).unwrap();
assert_eq!(store.get_candidates_in_relay_block(&relay_parent).unwrap(), candidates);
store.note_candidates_with_relay_parent(&relay_parent, &candidates).unwrap();
assert_eq!(store.get_candidates_with_relay_parent(&relay_parent), candidates);
}
#[test]
@@ -578,25 +555,32 @@ mod tests {
let relay_parent = [42; 32].into();
let erasure_root_1 = [11; 32].into();
let erasure_root_2 = [12; 32].into();
let mut receipt_1 = CandidateReceipt::default();
let mut receipt_2 = CandidateReceipt::default();
let mut receipt_1 = AbridgedCandidateReceipt::default();
let mut receipt_2 = AbridgedCandidateReceipt::default();
receipt_1.parachain_index = 1.into();
receipt_1.erasure_root = erasure_root_1;
receipt_1.commitments.erasure_root = erasure_root_1;
receipt_1.relay_parent = relay_parent;
receipt_2.parachain_index = 2.into();
receipt_2.erasure_root = erasure_root_2;
receipt_2.commitments.erasure_root = erasure_root_2;
receipt_2.relay_parent = relay_parent;
let receipt_1_hash = receipt_1.hash();
let receipt_2_hash = receipt_2.hash();
let chunk = ErasureChunk {
chunk: vec![1, 2, 3],
index: validator_index,
proof: Vec::new(),
};
let candidates = vec![receipt_1.hash(), receipt_2.hash()];
let candidates = vec![receipt_1_hash, receipt_2_hash];
let erasure_roots = vec![erasure_root_1, erasure_root_2];
let store = Store::new_in_memory();
store.add_validator_index_and_n_validators(
store.note_validator_index_and_n_validators(
&relay_parent,
validator_index,
n_validators
@@ -605,7 +589,7 @@ mod tests {
store.add_candidate(&receipt_2).unwrap();
// We are waiting for chunks from two candidates.
store.add_candidates_in_relay_block(&relay_parent, candidates.clone()).unwrap();
store.note_candidates_with_relay_parent(&relay_parent, &candidates).unwrap();
let awaited_frontier = store.awaited_chunks().unwrap();
warn!(target: "availability", "awaited {:?}", awaited_frontier);
@@ -613,18 +597,24 @@ mod tests {
.clone()
.into_iter()
.zip(erasure_roots.iter())
.map(|(c, e)| (relay_parent, *e, c, validator_index))
.map(|(_c, &e)| AwaitedFrontierEntry {
relay_parent,
erasure_root: e,
validator_index,
})
.collect();
assert_eq!(awaited_frontier, expected);
// We add chunk from one of the candidates.
store.add_erasure_chunks(n_validators, &relay_parent, &receipt_1.hash(), vec![chunk]).unwrap();
store.add_erasure_chunks(n_validators, &receipt_1_hash, vec![chunk]).unwrap();
let awaited_frontier = store.awaited_chunks().unwrap();
// Now we wait for the other chunk that we haven't received yet.
let expected: HashSet<_> = vec![
(relay_parent, erasure_roots[1], candidates[1], validator_index)
].into_iter().collect();
let expected: HashSet<_> = vec![AwaitedFrontierEntry {
relay_parent,
erasure_root: erasure_roots[1],
validator_index,
}].into_iter().collect();
assert_eq!(awaited_frontier, expected);
+101 -124
View File
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::io;
use std::sync::Arc;
use std::thread;
@@ -34,15 +34,15 @@ use consensus_common::{
};
use polkadot_primitives::{Block, BlockId, Hash};
use polkadot_primitives::parachain::{
CandidateReceipt, ParachainHost, ValidatorId,
ValidatorPair, ErasureChunk, PoVBlock,
ParachainHost, ValidatorId, AbridgedCandidateReceipt, AvailableData,
ValidatorPair, ErasureChunk,
};
use futures::{prelude::*, future::select, channel::{mpsc, oneshot}, task::{Spawn, SpawnExt}};
use keystore::KeyStorePtr;
use tokio::runtime::{Handle, Runtime as LocalRuntime};
use crate::{LOG_TARGET, Data, ProvideGossipMessages, erasure_coding_topic};
use crate::{LOG_TARGET, ProvideGossipMessages, erasure_coding_topic};
use crate::store::Store;
/// Errors that may occur.
@@ -65,32 +65,27 @@ pub(crate) enum Error {
/// * when the `Store` api is used by outside code.
#[derive(Debug)]
pub(crate) enum WorkerMsg {
ErasureRoots(ErasureRoots),
ParachainBlocks(ParachainBlocks),
IncludedParachainBlocks(IncludedParachainBlocks),
ListenForChunks(ListenForChunks),
Chunks(Chunks),
CandidatesFinalized(CandidatesFinalized),
MakeAvailable(MakeAvailable),
}
/// The erasure roots of the heads included in the block with a given parent.
/// A notification of a parachain block included in the relay chain.
#[derive(Debug)]
pub(crate) struct ErasureRoots {
/// The relay parent of the block these roots belong to.
pub relay_parent: Hash,
/// The roots themselves.
pub erasure_roots: Vec<Hash>,
/// A sender to signal the result asynchronously.
pub result: oneshot::Sender<Result<(), Error>>,
pub(crate) struct IncludedParachainBlock {
/// The abridged candidate receipt, extracted from a relay-chain block.
pub candidate: AbridgedCandidateReceipt,
/// The data to keep available from the candidate, if known.
pub available_data: Option<AvailableData>,
}
/// The receipts of the heads included into the block with a given parent.
#[derive(Debug)]
pub(crate) struct ParachainBlocks {
/// The relay parent of the block these parachain blocks belong to.
pub relay_parent: Hash,
pub(crate) struct IncludedParachainBlocks {
/// The blocks themselves.
pub blocks: Vec<(CandidateReceipt, Option<PoVBlock>)>,
pub blocks: Vec<IncludedParachainBlock>,
/// A sender to signal the result asynchronously.
pub result: oneshot::Sender<Result<(), Error>>,
}
@@ -98,8 +93,6 @@ pub(crate) struct ParachainBlocks {
/// Listen gossip for these chunks.
#[derive(Debug)]
pub(crate) struct ListenForChunks {
/// The relay parent of the block the chunks from we want to listen to.
pub relay_parent: Hash,
/// The hash of the candidate chunk belongs to.
pub candidate_hash: Hash,
/// The index of the chunk we need.
@@ -126,15 +119,17 @@ pub(crate) struct Chunks {
pub(crate) struct CandidatesFinalized {
/// The relay parent of the block that was finalized.
relay_parent: Hash,
/// The parachain heads that were finalized in this block.
candidate_hashes: Vec<Hash>,
/// The hashes of candidates that were finalized in this block.
included_candidates: HashSet<Hash>,
}
/// The message that corresponds to `make_available` call of the crate API.
#[derive(Debug)]
pub(crate) struct MakeAvailable {
/// The data being made available.
pub data: Data,
/// The hash of the candidate for which we are publishing data.
pub candidate_hash: Hash,
/// The data to make available.
pub available_data: AvailableData,
/// A sender to signal the result asynchronously.
pub result: oneshot::Sender<Result<(), Error>>,
}
@@ -205,7 +200,7 @@ where
fn fetch_candidates<P>(client: &P, extrinsics: Vec<<Block as BlockT>::Extrinsic>, parent: &BlockId)
-> ClientResult<Option<Vec<CandidateReceipt>>>
-> ClientResult<Option<Vec<AbridgedCandidateReceipt>>>
where
P: ProvideRuntimeApi<Block>,
P::Api: ParachainHost<Block, Error = sp_blockchain::Error>,
@@ -262,12 +257,15 @@ where
}
};
let candidate_hashes = match fetch_candidates(
let included_candidates = match fetch_candidates(
&*client,
extrinsics,
&BlockId::hash(parent_hash)
&BlockId::hash(parent_hash),
) {
Ok(Some(candidates)) => candidates.into_iter().map(|c| c.hash()).collect(),
Ok(Some(candidates)) => candidates
.into_iter()
.map(|c| c.hash())
.collect(),
Ok(None) => {
warn!(
target: LOG_TARGET,
@@ -286,7 +284,7 @@ where
let msg = WorkerMsg::CandidatesFinalized(CandidatesFinalized {
relay_parent: parent_hash,
candidate_hashes
included_candidates
});
if let Err(_) = sender.send(msg).await {
@@ -315,12 +313,12 @@ where
sender: &mut mpsc::UnboundedSender<WorkerMsg>,
) {
if let Some(awaited_chunks) = self.availability_store.awaited_chunks() {
for chunk in awaited_chunks {
for awaited_chunk in awaited_chunks {
if let Err(e) = self.register_chunks_listener(
runtime_handle,
sender,
chunk.0,
chunk.1,
awaited_chunk.relay_parent,
awaited_chunk.erasure_root,
) {
warn!(target: LOG_TARGET, "Failed to register gossip listener: {}", e);
}
@@ -366,33 +364,36 @@ where
&mut self,
runtime_handle: &Handle,
sender: &mut mpsc::UnboundedSender<WorkerMsg>,
relay_parent: Hash,
blocks: Vec<(CandidateReceipt, Option<PoVBlock>)>,
blocks: Vec<IncludedParachainBlock>,
) -> Result<(), Error> {
let hashes: Vec<_> = blocks.iter().map(|(c, _)| c.hash()).collect();
// First we have to add the receipts themselves.
for (candidate, block) in blocks.into_iter() {
for IncludedParachainBlock { candidate, available_data }
in blocks.into_iter()
{
let _ = self.availability_store.add_candidate(&candidate);
if let Some(_block) = block {
if let Some(_available_data) = available_data {
// Should we be breaking block into chunks here and gossiping it and so on?
}
if let Err(e) = self.register_chunks_listener(
runtime_handle,
sender,
relay_parent,
candidate.erasure_root
candidate.relay_parent,
candidate.commitments.erasure_root,
) {
warn!(target: LOG_TARGET, "Failed to register chunk listener: {}", e);
}
}
let _ = self.availability_store.add_candidates_in_relay_block(
&relay_parent,
hashes
);
// This leans on the codebase-wide assumption that the `relay_parent`
// of all candidates in a block matches the parent hash of that block.
//
// In the future this will not always be true.
let _ = self.availability_store.note_candidates_with_relay_parent(
&candidate.relay_parent,
&[candidate.hash()],
);
}
Ok(())
}
@@ -415,7 +416,11 @@ where
.ok_or(Error::CandidateNotFound { candidate_hash })?;
for chunk in &chunks {
let topic = erasure_coding_topic(relay_parent, receipt.erasure_root, chunk.index);
let topic = erasure_coding_topic(
relay_parent,
receipt.commitments.erasure_root,
chunk.index,
);
// need to remove gossip listener and stop it.
if let Some(signal) = self.registered_gossip_streams.remove(&topic) {
let _ = signal.fire();
@@ -424,7 +429,6 @@ where
self.availability_store.add_erasure_chunks(
n_validators,
&relay_parent,
&candidate_hash,
chunks,
)?;
@@ -432,17 +436,6 @@ where
Ok(())
}
// Adds the erasure roots into the store.
fn on_erasure_roots_received(
&mut self,
relay_parent: Hash,
erasure_roots: Vec<Hash>
) -> Result<(), Error> {
self.availability_store.add_erasure_roots_in_relay_block(&relay_parent, erasure_roots)?;
Ok(())
}
// Processes the `ListenForChunks` message.
//
// When the worker receives a `ListenForChunk` message, it double-checks that
@@ -451,7 +444,6 @@ where
&mut self,
runtime_handle: &Handle,
sender: &mut mpsc::UnboundedSender<WorkerMsg>,
relay_parent: Hash,
candidate_hash: Hash,
id: usize
) -> Result<(), Error> {
@@ -459,13 +451,13 @@ where
.ok_or(Error::CandidateNotFound { candidate_hash })?;
if self.availability_store
.get_erasure_chunk(&relay_parent, candidate.block_data_hash, id)
.get_erasure_chunk(&candidate_hash, id)
.is_none() {
if let Err(e) = self.register_chunks_listener(
runtime_handle,
sender,
relay_parent,
candidate.erasure_root
candidate.relay_parent,
candidate.commitments.erasure_root
) {
warn!(target: LOG_TARGET, "Failed to register a gossip listener: {}", e);
}
@@ -498,7 +490,7 @@ where
let runtime_handle = runtime.handle().clone();
// On startup, registers listeners (gossip streams) for all
// (relay_parent, erasure-root, i) in the awaited frontier.
// entries in the awaited frontier.
worker.register_listeners(runtime.handle(), &mut sender);
let process_notification = async move {
@@ -506,18 +498,8 @@ where
trace!(target: LOG_TARGET, "Received message {:?}", msg);
let res = match msg {
WorkerMsg::ErasureRoots(msg) => {
let ErasureRoots { relay_parent, erasure_roots, result} = msg;
let res = worker.on_erasure_roots_received(
relay_parent,
erasure_roots,
);
let _ = result.send(res);
Ok(())
}
WorkerMsg::ListenForChunks(msg) => {
let ListenForChunks {
relay_parent,
candidate_hash,
index,
result,
@@ -526,7 +508,6 @@ where
let res = worker.on_listen_for_chunks_received(
&runtime_handle,
&mut sender,
relay_parent,
candidate_hash,
index as usize,
);
@@ -536,9 +517,8 @@ where
}
Ok(())
}
WorkerMsg::ParachainBlocks(msg) => {
let ParachainBlocks {
relay_parent,
WorkerMsg::IncludedParachainBlocks(msg) => {
let IncludedParachainBlocks {
blocks,
result,
} = msg;
@@ -546,7 +526,6 @@ where
let res = worker.on_parachain_blocks_received(
&runtime_handle,
&mut sender,
relay_parent,
blocks,
);
@@ -565,16 +544,17 @@ where
Ok(())
}
WorkerMsg::CandidatesFinalized(msg) => {
let CandidatesFinalized { relay_parent, candidate_hashes } = msg;
let CandidatesFinalized { relay_parent, included_candidates } = msg;
worker.availability_store.candidates_finalized(
relay_parent,
candidate_hashes.into_iter().collect(),
included_candidates,
)
}
WorkerMsg::MakeAvailable(msg) => {
let MakeAvailable { data, result } = msg;
let res = worker.availability_store.make_available(data)
let MakeAvailable { candidate_hash, available_data, result } = msg;
let res = worker.availability_store
.make_available(candidate_hash, available_data)
.map_err(|e| e.into());
let _ = result.send(res);
Ok(())
@@ -651,7 +631,6 @@ impl<I, P> BlockImport<Block> for AvailabilityBlockImport<I, P> where
);
if let Some(ref extrinsics) = block.body {
let relay_parent = *block.header.parent_hash();
let parent_id = BlockId::hash(*block.header.parent_hash());
// Extract our local position i from the validator set of the parent.
let validators = self.client.runtime_api().validators(&parent_id)
@@ -675,16 +654,15 @@ impl<I, P> BlockImport<Block> for AvailabilityBlockImport<I, P> where
);
for candidate in &candidates {
let candidate_hash = candidate.hash();
// If we don't yet have our chunk of this candidate,
// tell the worker to listen for one.
if self.availability_store.get_erasure_chunk(
&relay_parent,
candidate.block_data_hash,
&candidate_hash,
our_id as usize,
).is_none() {
let msg = WorkerMsg::ListenForChunks(ListenForChunks {
relay_parent,
candidate_hash: candidate.hash(),
candidate_hash,
index: our_id as u32,
result: None,
});
@@ -693,27 +671,19 @@ impl<I, P> BlockImport<Block> for AvailabilityBlockImport<I, P> where
}
}
let erasure_roots: Vec<_> = candidates
.iter()
.map(|c| c.erasure_root)
.collect();
// Inform the worker about new (relay_parent, erasure_roots) pairs
let (s, _) = oneshot::channel();
let msg = WorkerMsg::ErasureRoots(ErasureRoots {
relay_parent,
erasure_roots,
result: s,
});
let _ = self.to_worker.unbounded_send(msg);
let (s, _) = oneshot::channel();
// Inform the worker about the included parachain blocks.
let msg = WorkerMsg::ParachainBlocks(ParachainBlocks {
relay_parent,
blocks: candidates.into_iter().map(|c| (c, None)).collect(),
let blocks = candidates
.into_iter()
.map(|c| IncludedParachainBlock {
candidate: c,
available_data: None,
})
.collect();
let msg = WorkerMsg::IncludedParachainBlocks(IncludedParachainBlocks {
blocks,
result: s,
});
@@ -806,6 +776,7 @@ mod tests {
use std::sync::{Arc, Mutex, Condvar};
use std::pin::Pin;
use tokio::runtime::Runtime;
use crate::store::AwaitedFrontierEntry;
// Just contains topic->channel mapping to give to outer code on `gossip_messages_for` calls.
struct TestGossipMessages {
@@ -866,7 +837,7 @@ mod tests {
let store = Store::new_in_memory();
// Tell the store our validator's position and the number of validators at given point.
store.add_validator_index_and_n_validators(&relay_parent, local_id, n_validators).unwrap();
store.note_validator_index_and_n_validators(&relay_parent, local_id, n_validators).unwrap();
let (gossip_sender, gossip_receiver) = mpsc::unbounded();
@@ -879,9 +850,10 @@ mod tests {
].into_iter().collect()))
};
let mut candidate = CandidateReceipt::default();
let mut candidate = AbridgedCandidateReceipt::default();
candidate.erasure_root = erasure_root;
candidate.commitments.erasure_root = erasure_root;
candidate.relay_parent = relay_parent;
let candidate_hash = candidate.hash();
// At this point we shouldn't be waiting for any chunks.
@@ -889,9 +861,11 @@ mod tests {
let (s, r) = oneshot::channel();
let msg = WorkerMsg::ParachainBlocks(ParachainBlocks {
relay_parent,
blocks: vec![(candidate, None)],
let msg = WorkerMsg::IncludedParachainBlocks(IncludedParachainBlocks {
blocks: vec![IncludedParachainBlock {
candidate,
available_data: None,
}],
result: s,
});
@@ -907,7 +881,11 @@ mod tests {
// Make sure that at this point we are waiting for the appropriate chunk.
assert_eq!(
store.awaited_chunks().unwrap(),
vec![(relay_parent, erasure_root, candidate_hash, local_id)].into_iter().collect()
vec![AwaitedFrontierEntry {
relay_parent,
erasure_root,
validator_index: local_id,
}].into_iter().collect()
);
let msg = (
@@ -941,25 +919,27 @@ mod tests {
let relay_parent = [1; 32].into();
let erasure_root_1 = [2; 32].into();
let erasure_root_2 = [3; 32].into();
let block_data_hash_1 = [4; 32].into();
let block_data_hash_2 = [5; 32].into();
let pov_block_hash_1 = [4; 32].into();
let pov_block_hash_2 = [5; 32].into();
let local_id = 2;
let n_validators = 4;
let mut candidate_1 = CandidateReceipt::default();
candidate_1.erasure_root = erasure_root_1;
candidate_1.block_data_hash = block_data_hash_1;
let mut candidate_1 = AbridgedCandidateReceipt::default();
candidate_1.commitments.erasure_root = erasure_root_1;
candidate_1.pov_block_hash = pov_block_hash_1;
candidate_1.relay_parent = relay_parent;
let candidate_1_hash = candidate_1.hash();
let mut candidate_2 = CandidateReceipt::default();
candidate_2.erasure_root = erasure_root_2;
candidate_2.block_data_hash = block_data_hash_2;
let mut candidate_2 = AbridgedCandidateReceipt::default();
candidate_2.commitments.erasure_root = erasure_root_2;
candidate_2.pov_block_hash = pov_block_hash_2;
candidate_2.relay_parent = relay_parent;
let candidate_2_hash = candidate_2.hash();
let store = Store::new_in_memory();
// Tell the store our validator's position and the number of validators at given point.
store.add_validator_index_and_n_validators(&relay_parent, local_id, n_validators).unwrap();
store.note_validator_index_and_n_validators(&relay_parent, local_id, n_validators).unwrap();
// Let the store know about the candidates
store.add_candidate(&candidate_1).unwrap();
@@ -968,7 +948,6 @@ mod tests {
// And let the store know about the chunk from the second candidate.
store.add_erasure_chunks(
n_validators,
&relay_parent,
&candidate_2_hash,
vec![ErasureChunk {
chunk: vec![1, 2, 3],
@@ -999,7 +978,6 @@ mod tests {
let (s2, r2) = oneshot::channel();
// Tell the worker to listen for chunks from candidate 2 (we alredy have a chunk from it).
let listen_msg_2 = WorkerMsg::ListenForChunks(ListenForChunks {
relay_parent,
candidate_hash: candidate_2_hash,
index: local_id as u32,
result: Some(s2),
@@ -1016,7 +994,6 @@ mod tests {
// Tell the worker to listen for chunks from candidate 1.
// (we don't have a chunk from it yet).
let listen_msg_1 = WorkerMsg::ListenForChunks(ListenForChunks {
relay_parent,
candidate_hash: candidate_1_hash,
index: local_id as u32,
result: Some(s1),