thiserror, docs, remove general Failure case (#9741)

automerge
This commit is contained in:
anatoly yakovenko
2020-04-29 18:12:51 -07:00
committed by GitHub
parent 230df0ec0c
commit a0514eb2ae
8 changed files with 54 additions and 26 deletions

View File

@ -1,5 +1,6 @@
//! The `hash` module provides functions for creating SHA-256 hashes.
use crate::sanitize::Sanitize;
use sha2::{Digest, Sha256};
use std::{convert::TryFrom, fmt, mem, str::FromStr};
use thiserror::Error;
@ -30,6 +31,8 @@ impl Hasher {
}
}
impl Sanitize for Hash {}
impl AsRef<[u8]> for Hash {
fn as_ref(&self) -> &[u8] {
&self.0[..]

View File

@ -1,5 +1,6 @@
//! Defines a composable Instruction type and a memory-efficient CompiledInstruction.
use crate::sanitize::Sanitize;
use crate::{pubkey::Pubkey, short_vec, system_instruction::SystemError};
use bincode::serialize;
use serde::Serialize;
@ -252,6 +253,8 @@ pub struct CompiledInstruction {
pub data: Vec<u8>,
}
impl Sanitize for CompiledInstruction {}
impl CompiledInstruction {
pub fn new<T: Serialize>(program_ids_index: u8, data: &T, accounts: Vec<u8>) -> Self {
let data = serialize(data).unwrap();

View File

@ -184,6 +184,9 @@ impl Sanitize for Message {
}
}
}
self.account_keys.sanitize()?;
self.recent_blockhash.sanitize()?;
self.instructions.sanitize()?;
Ok(())
}
}

View File

@ -1,11 +1,22 @@
#[derive(PartialEq, Debug)]
use thiserror::Error;
#[derive(PartialEq, Debug, Error, Eq, Clone)]
pub enum SanitizeError {
Failed,
#[error("index out of bounds")]
IndexOutOfBounds,
ValueOutOfRange,
#[error("value out of bounds")]
ValueOutOfBounds,
#[error("invalid value")]
InvalidValue,
}
/// Trait for sanitizing values and members of over the wire messages.
/// Implementation should recursively decent through the data structure
/// and sanitize all struct members and enum clauses. Sanitize excludes
/// signature verification checks, those are handled by another pass.
/// Sanitize checks should include but are not limited too:
/// * All index values are in range
/// * All values are within their static max/min bounds
pub trait Sanitize {
fn sanitize(&self) -> Result<(), SanitizeError> {
Ok(())