mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-22 14:55:41 +00:00
erasure-coding: do not panic on 1 validator (#2022)
* erasure-coding: do not panic on 1 validator * tabify fuzz tests * remove num_validators length check * remove unused import * move erasure_coding_fuzzer to fuzzer * change the authors to admit (at) parity.io * Apply suggestions from code review Co-authored-by: Sergei Shulepov <sergei@parity.io> Co-authored-by: Sergei Shulepov <sergei@parity.io>
This commit is contained in:
@@ -1,20 +0,0 @@
|
|||||||
use polkadot_erasure_coding::*;
|
|
||||||
use primitives::v1::AvailableData;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use honggfuzz::fuzz;
|
|
||||||
|
|
||||||
fn main(){
|
|
||||||
loop {
|
|
||||||
fuzz!(|data: (usize, Vec<(Vec<u8>, usize)>)| {
|
|
||||||
let (num_validators, chunk_input) = data;
|
|
||||||
if num_validators <= 1 || num_validators > 10_000 {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let reconstructed: Result<AvailableData, _> = reconstruct_v1(
|
|
||||||
num_validators,
|
|
||||||
chunk_input.iter().map(|t| (&*t.0, t.1)).collect::<Vec<(&[u8], usize)>>()
|
|
||||||
);
|
|
||||||
println!("reconstructed {:?}", reconstructed);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
use polkadot_erasure_coding::*;
|
|
||||||
use primitives::v1::{AvailableData, BlockData, PoV};
|
|
||||||
use std::sync::Arc;
|
|
||||||
use honggfuzz::fuzz;
|
|
||||||
|
|
||||||
|
|
||||||
fn main(){
|
|
||||||
loop {
|
|
||||||
fuzz!(|data: &[u8]| {
|
|
||||||
let pov_block = PoV {
|
|
||||||
block_data: BlockData(data.iter().cloned().collect()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let available_data = AvailableData {
|
|
||||||
pov: Arc::new(pov_block),
|
|
||||||
validation_data: Default::default(),
|
|
||||||
};
|
|
||||||
let chunks = obtain_chunks_v1(
|
|
||||||
10,
|
|
||||||
&available_data,
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(chunks.len(), 10);
|
|
||||||
|
|
||||||
// any 4 chunks should work.
|
|
||||||
let reconstructed: AvailableData = reconstruct_v1(
|
|
||||||
10,
|
|
||||||
[
|
|
||||||
(&*chunks[1], 1),
|
|
||||||
(&*chunks[4], 4),
|
|
||||||
(&*chunks[6], 6),
|
|
||||||
(&*chunks[9], 9),
|
|
||||||
].iter().cloned(),
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(reconstructed, available_data);
|
|
||||||
println!("{:?}", reconstructed);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "erasure_coding_fuzzer"
|
name = "erasure_coding_fuzzer"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Vincent Ulitzsch <vincent@srlabs.de>"]
|
authors = ["Parity Technologies <admin@parity.io>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
use polkadot_erasure_coding::*;
|
||||||
|
use primitives::v1::AvailableData;
|
||||||
|
use honggfuzz::fuzz;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
loop {
|
||||||
|
fuzz!(|data: (usize, Vec<(Vec<u8>, usize)>)| {
|
||||||
|
let (num_validators, chunk_input) = data;
|
||||||
|
let reconstructed: Result<AvailableData, _> = reconstruct_v1(
|
||||||
|
num_validators,
|
||||||
|
chunk_input.iter().map(|t| (&*t.0, t.1)).collect::<Vec<(&[u8], usize)>>()
|
||||||
|
);
|
||||||
|
println!("reconstructed {:?}", reconstructed);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
use polkadot_erasure_coding::*;
|
||||||
|
use primitives::v1::{AvailableData, BlockData, PoV};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use honggfuzz::fuzz;
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
loop {
|
||||||
|
fuzz!(|data: &[u8]| {
|
||||||
|
let pov_block = PoV {
|
||||||
|
block_data: BlockData(data.iter().cloned().collect()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let available_data = AvailableData {
|
||||||
|
pov: Arc::new(pov_block),
|
||||||
|
validation_data: Default::default(),
|
||||||
|
};
|
||||||
|
let chunks = obtain_chunks_v1(
|
||||||
|
10,
|
||||||
|
&available_data,
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(chunks.len(), 10);
|
||||||
|
|
||||||
|
// any 4 chunks should work.
|
||||||
|
let reconstructed: AvailableData = reconstruct_v1(
|
||||||
|
10,
|
||||||
|
[
|
||||||
|
(&*chunks[1], 1),
|
||||||
|
(&*chunks[4], 4),
|
||||||
|
(&*chunks[6], 6),
|
||||||
|
(&*chunks[9], 9),
|
||||||
|
].iter().cloned(),
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(reconstructed, available_data);
|
||||||
|
println!("{:?}", reconstructed);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,9 +45,9 @@ pub enum Error {
|
|||||||
/// Returned when there are too many validators.
|
/// Returned when there are too many validators.
|
||||||
#[error("There are too many validators")]
|
#[error("There are too many validators")]
|
||||||
TooManyValidators,
|
TooManyValidators,
|
||||||
/// Cannot encode something for no validators
|
/// Cannot encode something for zero or one validator
|
||||||
#[error("Validator set is empty")]
|
#[error("Expected at least 2 validators")]
|
||||||
EmptyValidators,
|
NotEnoughValidators,
|
||||||
/// Cannot reconstruct: wrong number of validators.
|
/// Cannot reconstruct: wrong number of validators.
|
||||||
#[error("Validator count mismatches between encoding and decoding")]
|
#[error("Validator count mismatches between encoding and decoding")]
|
||||||
WrongValidatorCount,
|
WrongValidatorCount,
|
||||||
@@ -122,7 +122,7 @@ impl CodeParams {
|
|||||||
|
|
||||||
fn code_params(n_validators: usize) -> Result<CodeParams, Error> {
|
fn code_params(n_validators: usize) -> Result<CodeParams, Error> {
|
||||||
if n_validators > MAX_VALIDATORS { return Err(Error::TooManyValidators) }
|
if n_validators > MAX_VALIDATORS { return Err(Error::TooManyValidators) }
|
||||||
if n_validators == 0 { return Err(Error::EmptyValidators) }
|
if n_validators <= 1 { return Err(Error::NotEnoughValidators) }
|
||||||
|
|
||||||
let n_faulty = n_validators.saturating_sub(1) / 3;
|
let n_faulty = n_validators.saturating_sub(1) / 3;
|
||||||
let n_good = n_validators - n_faulty;
|
let n_good = n_validators - n_faulty;
|
||||||
@@ -406,12 +406,9 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_code_params() {
|
fn test_code_params() {
|
||||||
assert_eq!(code_params(0), Err(Error::EmptyValidators));
|
assert_eq!(code_params(0), Err(Error::NotEnoughValidators));
|
||||||
|
|
||||||
assert_eq!(code_params(1), Ok(CodeParams {
|
assert_eq!(code_params(1), Err(Error::NotEnoughValidators));
|
||||||
data_shards: 1,
|
|
||||||
parity_shards: 0,
|
|
||||||
}));
|
|
||||||
|
|
||||||
assert_eq!(code_params(2), Ok(CodeParams {
|
assert_eq!(code_params(2), Ok(CodeParams {
|
||||||
data_shards: 1,
|
data_shards: 1,
|
||||||
@@ -487,6 +484,15 @@ mod tests {
|
|||||||
assert_eq!(reconstructed, available_data);
|
assert_eq!(reconstructed, available_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reconstruct_does_not_panic_on_low_validator_count() {
|
||||||
|
let reconstructed = reconstruct_v1(
|
||||||
|
1,
|
||||||
|
[].iter().cloned(),
|
||||||
|
);
|
||||||
|
assert_eq!(reconstructed, Err(Error::NotEnoughValidators));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn construct_valid_branches() {
|
fn construct_valid_branches() {
|
||||||
let pov_block = PoVBlock {
|
let pov_block = PoVBlock {
|
||||||
|
|||||||
Reference in New Issue
Block a user