Make CandidateHash a real type (#1916)

* Make `CandidateHash` a real type

This pr adds a new type `CandidateHash` that is used instead of the
opaque `Hash` type. This helps to ensure on the type system level that
we are passing the correct types.

This pr also fixes wrong usage of `relay_parent` as `candidate_hash`
when communicating with the av storage.

* Update core-primitives/src/lib.rs

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>

* Wrap the lines

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2020-11-05 16:28:45 +01:00
committed by GitHub
parent 2cde7732da
commit 640264f38b
17 changed files with 161 additions and 182 deletions
+21 -16
View File
@@ -32,7 +32,7 @@ use futures::{
use sp_keystore::SyncCryptoStorePtr;
use polkadot_primitives::v1::{
CommittedCandidateReceipt, BackedCandidate, Id as ParaId, ValidatorId,
ValidatorIndex, SigningContext, PoV,
ValidatorIndex, SigningContext, PoV, CandidateHash,
CandidateDescriptor, AvailableData, ValidatorSignature, Hash, CandidateReceipt,
CandidateCommitments, CoreState, CoreIndex, CollatorId, ValidationOutputs,
};
@@ -103,12 +103,12 @@ struct CandidateBackingJob {
/// The collator required to author the candidate, if any.
required_collator: Option<CollatorId>,
/// We issued `Valid` or `Invalid` statements on about these candidates.
issued_statements: HashSet<Hash>,
issued_statements: HashSet<CandidateHash>,
/// `Some(h)` if this job has already issues `Seconded` statemt for some candidate with `h` hash.
seconded: Option<Hash>,
seconded: Option<CandidateHash>,
/// The candidates that are includable, by hash. Each entry here indicates
/// that we've sent the provisioner the backed candidate.
backed: HashSet<Hash>,
backed: HashSet<CandidateHash>,
/// We have already reported misbehaviors for these validators.
reported_misbehavior_for: HashSet<ValidatorIndex>,
keystore: SyncCryptoStorePtr,
@@ -131,12 +131,12 @@ struct TableContext {
impl TableContextTrait for TableContext {
type AuthorityId = ValidatorIndex;
type Digest = Hash;
type Digest = CandidateHash;
type GroupId = ParaId;
type Signature = ValidatorSignature;
type Candidate = CommittedCandidateReceipt;
fn candidate_digest(candidate: &CommittedCandidateReceipt) -> Hash {
fn candidate_digest(candidate: &CommittedCandidateReceipt) -> CandidateHash {
candidate.hash()
}
@@ -341,6 +341,7 @@ impl CandidateBackingJob {
// the collator, do not make available and report the collator.
let commitments_check = self.make_pov_available(
pov,
candidate_hash,
validation_data,
outputs,
|commitments| if commitments.hash() == candidate.commitments_hash {
@@ -525,7 +526,7 @@ impl CandidateBackingJob {
&mut self,
summary: TableSummary,
) -> Result<(), Error> {
let candidate_hash = summary.candidate.clone();
let candidate_hash = summary.candidate;
if self.issued_statements.contains(&candidate_hash) {
return Ok(())
@@ -559,6 +560,7 @@ impl CandidateBackingJob {
// If validation produces a new set of commitments, we vote the candidate as invalid.
let commitments_check = self.make_pov_available(
(&*pov).clone(),
candidate_hash,
validation_data,
outputs,
|commitments| if commitments == expected_commitments {
@@ -667,12 +669,13 @@ impl CandidateBackingJob {
&mut self,
id: Option<ValidatorIndex>,
n_validators: u32,
candidate_hash: CandidateHash,
available_data: AvailableData,
) -> Result<(), Error> {
let (tx, rx) = oneshot::channel();
self.tx_from.send(FromJob::AvailabilityStore(
AvailabilityStoreMessage::StoreAvailableData(
self.parent,
candidate_hash,
id,
n_validators,
available_data,
@@ -694,6 +697,7 @@ impl CandidateBackingJob {
async fn make_pov_available<T, E>(
&mut self,
pov: PoV,
candidate_hash: CandidateHash,
validation_data: polkadot_primitives::v1::PersistedValidationData,
outputs: ValidationOutputs,
with_commitments: impl FnOnce(CandidateCommitments) -> Result<T, E>,
@@ -727,6 +731,7 @@ impl CandidateBackingJob {
self.store_available_data(
self.table_context.validator.as_ref().map(|v| v.index()),
self.table_context.validators.len() as u32,
candidate_hash,
available_data,
).await?;
@@ -1206,8 +1211,8 @@ mod tests {
assert_matches!(
virtual_overseer.recv().await,
AllMessages::AvailabilityStore(
AvailabilityStoreMessage::StoreAvailableData(parent_hash, _, _, _, tx)
) if parent_hash == test_state.relay_parent => {
AvailabilityStoreMessage::StoreAvailableData(candidate_hash, _, _, _, tx)
) if candidate_hash == candidate.hash() => {
tx.send(Ok(())).unwrap();
}
);
@@ -1333,8 +1338,8 @@ mod tests {
assert_matches!(
virtual_overseer.recv().await,
AllMessages::AvailabilityStore(
AvailabilityStoreMessage::StoreAvailableData(parent_hash, _, _, _, tx)
) if parent_hash == test_state.relay_parent => {
AvailabilityStoreMessage::StoreAvailableData(candidate_hash, _, _, _, tx)
) if candidate_hash == candidate_a.hash() => {
tx.send(Ok(())).unwrap();
}
);
@@ -1482,8 +1487,8 @@ mod tests {
assert_matches!(
virtual_overseer.recv().await,
AllMessages::AvailabilityStore(
AvailabilityStoreMessage::StoreAvailableData(parent_hash, _, _, _, tx)
) if parent_hash == test_state.relay_parent => {
AvailabilityStoreMessage::StoreAvailableData(candidate_hash, _, _, _, tx)
) if candidate_hash == candidate_a.hash() => {
tx.send(Ok(())).unwrap();
}
);
@@ -1665,8 +1670,8 @@ mod tests {
assert_matches!(
virtual_overseer.recv().await,
AllMessages::AvailabilityStore(
AvailabilityStoreMessage::StoreAvailableData(parent_hash, _, _, _, tx)
) if parent_hash == test_state.relay_parent => {
AvailabilityStoreMessage::StoreAvailableData(candidate_hash, _, _, _, tx)
) if candidate_hash == candidate_b.hash() => {
tx.send(Ok(())).unwrap();
}
);