introduce errors with info (#1834)

This commit is contained in:
Bernhard Schuster
2020-10-27 08:10:03 +01:00
committed by GitHub
parent 40ea09389c
commit f345123748
58 changed files with 1983 additions and 2030 deletions
+15 -6
View File
@@ -30,6 +30,7 @@ use primitives::v0::{self, Hash as H256, BlakeTwo256, HashT};
use primitives::v1;
use sp_core::Blake2Hasher;
use trie::{EMPTY_PREFIX, MemoryDB, Trie, TrieMut, trie_types::{TrieDBMut, TrieDB}};
use thiserror::Error;
use self::wrapped_shard::WrappedShard;
@@ -39,35 +40,43 @@ mod wrapped_shard;
const MAX_VALIDATORS: usize = <galois_16::Field as reed_solomon::Field>::ORDER;
/// Errors in erasure coding.
#[derive(Debug, Clone, PartialEq, derive_more::Display)]
#[derive(Debug, Clone, PartialEq, Error)]
pub enum Error {
/// Returned when there are too many validators.
#[error("There are too many validators")]
TooManyValidators,
/// Cannot encode something for no validators
#[error("Validator set is empty")]
EmptyValidators,
/// Cannot reconstruct: wrong number of validators.
#[error("Validator count mismatches between encoding and decoding")]
WrongValidatorCount,
/// Not enough chunks present.
#[error("Not enough chunks to reconstruct message")]
NotEnoughChunks,
/// Too many chunks present.
#[error("Too many chunks present")]
TooManyChunks,
/// Chunks not of uniform length or the chunks are empty.
#[error("Chunks are not unform, mismatch in length or are zero sized")]
NonUniformChunks,
/// An uneven byte-length of a shard is not valid for GF(2^16) encoding.
#[error("Uneven length is not valid for field GF(2^16)")]
UnevenLength,
/// Chunk index out of bounds.
#[display(fmt = "Chunk is out of bounds: {} {}", _0, _1)]
ChunkIndexOutOfBounds(usize, usize),
#[error("Chunk is out of bounds: {chunk_index} not included in 0..{n_validators}")]
ChunkIndexOutOfBounds{ chunk_index: usize, n_validators: usize },
/// Bad payload in reconstructed bytes.
#[error("Reconstructed payload invalid")]
BadPayload,
/// Invalid branch proof.
#[error("Invalid branch proof")]
InvalidBranchProof,
/// Branch out of bounds.
#[error("Branch is out of bounds")]
BranchOutOfBounds,
}
impl std::error::Error for Error { }
#[derive(Debug, PartialEq)]
struct CodeParams {
data_shards: usize,
@@ -206,7 +215,7 @@ fn reconstruct<'a, I: 'a, T: Decode>(n_validators: usize, chunks: I) -> Result<T
let mut shard_len = None;
for (chunk_data, chunk_idx) in chunks.into_iter().take(n_validators) {
if chunk_idx >= n_validators {
return Err(Error::ChunkIndexOutOfBounds(chunk_idx, n_validators));
return Err(Error::ChunkIndexOutOfBounds{ chunk_index: chunk_idx, n_validators });
}
let shard_len = shard_len.get_or_insert_with(|| chunk_data.len());