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 -14
View File
@@ -33,7 +33,7 @@ use kvdb_rocksdb::{Database, DatabaseConfig};
use kvdb::{KeyValueDB, DBTransaction};
use polkadot_primitives::v1::{
Hash, AvailableData, BlockNumber, CandidateEvent, ErasureChunk, ValidatorIndex,
Hash, AvailableData, BlockNumber, CandidateEvent, ErasureChunk, ValidatorIndex, CandidateHash,
};
use polkadot_subsystem::{
FromOverseer, OverseerSignal, SubsystemError, Subsystem, SubsystemContext, SpawnedSubsystem,
@@ -242,7 +242,7 @@ enum CandidateState {
#[derive(Debug, Decode, Encode, Eq)]
struct PoVPruningRecord {
candidate_hash: Hash,
candidate_hash: CandidateHash,
block_number: BlockNumber,
candidate_state: CandidateState,
prune_at: PruningDelay,
@@ -272,7 +272,7 @@ impl PartialOrd for PoVPruningRecord {
#[derive(Debug, Decode, Encode, Eq)]
struct ChunkPruningRecord {
candidate_hash: Hash,
candidate_hash: CandidateHash,
block_number: BlockNumber,
candidate_state: CandidateState,
chunk_index: u32,
@@ -387,11 +387,11 @@ impl AvailabilityStoreSubsystem {
}
}
fn available_data_key(candidate_hash: &Hash) -> Vec<u8> {
fn available_data_key(candidate_hash: &CandidateHash) -> Vec<u8> {
(candidate_hash, 0i8).encode()
}
fn erasure_chunk_key(candidate_hash: &Hash, index: u32) -> Vec<u8> {
fn erasure_chunk_key(candidate_hash: &CandidateHash, index: u32) -> Vec<u8> {
(candidate_hash, index, 0i8).encode()
}
@@ -564,7 +564,7 @@ where
log::trace!(
target: LOG_TARGET,
"Updating pruning record for finalized block {}",
record.candidate_hash,
record.block_number,
);
record.prune_at = PruningDelay::into_the_future(
@@ -583,7 +583,7 @@ where
log::trace!(
target: LOG_TARGET,
"Updating chunk pruning record for finalized block {}",
record.candidate_hash,
record.block_number,
);
record.prune_at = PruningDelay::into_the_future(
@@ -620,7 +620,7 @@ where
for event in events.into_iter() {
if let CandidateEvent::CandidateIncluded(receipt, _) = event {
log::trace!(target: LOG_TARGET, "Candidate {} was included", receipt.hash());
log::trace!(target: LOG_TARGET, "Candidate {:?} was included", receipt.hash());
included.insert(receipt.hash());
}
}
@@ -729,7 +729,10 @@ where
Ok(())
}
fn available_data(db: &Arc<dyn KeyValueDB>, candidate_hash: &Hash) -> Option<StoredAvailableData> {
fn available_data(
db: &Arc<dyn KeyValueDB>,
candidate_hash: &CandidateHash,
) -> Option<StoredAvailableData> {
query_inner(db, columns::DATA, &available_data_key(candidate_hash))
}
@@ -835,7 +838,7 @@ where
fn store_available_data(
subsystem: &mut AvailabilityStoreSubsystem,
candidate_hash: &Hash,
candidate_hash: &CandidateHash,
id: Option<ValidatorIndex>,
n_validators: u32,
available_data: AvailableData,
@@ -872,7 +875,7 @@ fn store_available_data(
}
let pruning_record = PoVPruningRecord {
candidate_hash: candidate_hash.clone(),
candidate_hash: *candidate_hash,
block_number,
candidate_state: CandidateState::Stored,
prune_at,
@@ -901,7 +904,7 @@ fn store_available_data(
fn store_chunk(
subsystem: &mut AvailabilityStoreSubsystem,
candidate_hash: &Hash,
candidate_hash: &CandidateHash,
_n_validators: u32,
chunk: ErasureChunk,
block_number: BlockNumber,
@@ -952,7 +955,7 @@ fn store_chunk(
fn get_chunk(
subsystem: &mut AvailabilityStoreSubsystem,
candidate_hash: &Hash,
candidate_hash: &CandidateHash,
index: u32,
) -> Result<Option<ErasureChunk>, Error> {
if let Some(chunk) = query_inner(
@@ -981,7 +984,11 @@ fn get_chunk(
Ok(None)
}
fn query_inner<D: Decode>(db: &Arc<dyn KeyValueDB>, column: u32, key: &[u8]) -> Option<D> {
fn query_inner<D: Decode>(
db: &Arc<dyn KeyValueDB>,
column: u32,
key: &[u8],
) -> Option<D> {
match db.get(column, key) {
Ok(Some(raw)) => {
let res = D::decode(&mut &raw[..]).expect("all stored data serialized correctly; qed");
+9 -9
View File
@@ -27,7 +27,7 @@ use smallvec::smallvec;
use polkadot_primitives::v1::{
AvailableData, BlockData, CandidateDescriptor, CandidateReceipt, HeadData,
PersistedValidationData, PoV, Id as ParaId,
PersistedValidationData, PoV, Id as ParaId, CandidateHash,
};
use polkadot_node_subsystem_util::TimeoutExt;
use polkadot_subsystem::{
@@ -199,7 +199,7 @@ fn runtime_api_error_does_not_stop_the_subsystem() {
// but that's fine, we're still alive
let (tx, rx) = oneshot::channel();
let candidate_hash = Hash::repeat_byte(33);
let candidate_hash = CandidateHash(Hash::repeat_byte(33));
let validator_index = 5;
let query_chunk = AvailabilityStoreMessage::QueryChunk(
candidate_hash,
@@ -220,7 +220,7 @@ fn store_chunk_works() {
test_harness(PruningConfig::default(), store.clone(), |test_harness| async move {
let TestHarness { mut virtual_overseer } = test_harness;
let relay_parent = Hash::repeat_byte(32);
let candidate_hash = Hash::repeat_byte(33);
let candidate_hash = CandidateHash(Hash::repeat_byte(33));
let validator_index = 5;
let chunk = ErasureChunk {
@@ -273,7 +273,7 @@ fn store_block_works() {
let test_state = TestState::default();
test_harness(test_state.pruning_config.clone(), store.clone(), |test_harness| async move {
let TestHarness { mut virtual_overseer } = test_harness;
let candidate_hash = Hash::from([1; 32]);
let candidate_hash = CandidateHash(Hash::from([1; 32]));
let validator_index = 5;
let n_validators = 10;
@@ -327,7 +327,7 @@ fn store_pov_and_query_chunk_works() {
test_harness(test_state.pruning_config.clone(), store.clone(), |test_harness| async move {
let TestHarness { mut virtual_overseer } = test_harness;
let candidate_hash = Hash::from([1; 32]);
let candidate_hash = CandidateHash(Hash::from([1; 32]));
let n_validators = 10;
let pov = PoV {
@@ -370,7 +370,7 @@ fn stored_but_not_included_chunk_is_pruned() {
test_harness(test_state.pruning_config.clone(), store.clone(), |test_harness| async move {
let TestHarness { mut virtual_overseer } = test_harness;
let candidate_hash = Hash::repeat_byte(1);
let candidate_hash = CandidateHash(Hash::repeat_byte(1));
let relay_parent = Hash::repeat_byte(2);
let validator_index = 5;
@@ -425,7 +425,7 @@ fn stored_but_not_included_data_is_pruned() {
test_harness(test_state.pruning_config.clone(), store.clone(), |test_harness| async move {
let TestHarness { mut virtual_overseer } = test_harness;
let candidate_hash = Hash::repeat_byte(1);
let candidate_hash = CandidateHash(Hash::repeat_byte(1));
let n_validators = 10;
let pov = PoV {
@@ -852,7 +852,7 @@ fn forkfullness_works() {
async fn query_available_data(
virtual_overseer: &mut test_helpers::TestSubsystemContextHandle<AvailabilityStoreMessage>,
candidate_hash: Hash,
candidate_hash: CandidateHash,
) -> Option<AvailableData> {
let (tx, rx) = oneshot::channel();
@@ -864,7 +864,7 @@ async fn query_available_data(
async fn query_chunk(
virtual_overseer: &mut test_helpers::TestSubsystemContextHandle<AvailabilityStoreMessage>,
candidate_hash: Hash,
candidate_hash: CandidateHash,
index: u32,
) -> Option<ErasureChunk> {
let (tx, rx) = oneshot::channel();