Skip to content

Commit

Permalink
thiserror, docs, remove general Failure case
Browse files Browse the repository at this point in the history
  • Loading branch information
aeyakovenko committed Apr 27, 2020
1 parent 839ff51 commit 3b1da1c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
10 changes: 9 additions & 1 deletion core/src/contact_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct ContactInfo {
impl Sanitize for ContactInfo {
fn sanitize(&self) -> std::result::Result<(), SanitizeError> {
if self.wallclock >= MAX_WALLCLOCK {
return Err(SanitizeError::Failed);
return Err(SanitizeError::ValueOutOfRange);
}
Ok(())
}
Expand Down Expand Up @@ -325,4 +325,12 @@ mod tests {
ci.rpc = socketaddr!("127.0.0.1:234");
assert!(ci.valid_client_facing_addr().is_some());
}

#[test]
fn test_sanitize() {
let mut ci = ContactInfo::default();
assert_eq!(ci.sanitize(), Ok(()));
ci.wallclock = MAX_WALLCLOCK;
assert_eq!(ci.sanitize(), Err(SanitizeError::ValueOutOfRange));
}
}
16 changes: 8 additions & 8 deletions core/src/crds_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Sanitize for CrdsData {
CrdsData::ContactInfo(val) => val.sanitize(),
CrdsData::Vote(ix, val) => {
if *ix >= MAX_VOTES {
return Err(SanitizeError::Failed);
return Err(SanitizeError::ValueOutOfRange);
}
val.sanitize()
}
Expand All @@ -92,7 +92,7 @@ impl Sanitize for CrdsData {
CrdsData::AccountsHashes(val) => val.sanitize(),
CrdsData::EpochSlots(ix, val) => {
if *ix as usize >= MAX_EPOCH_SLOTS as usize {
return Err(SanitizeError::Failed);
return Err(SanitizeError::ValueOutOfRange);
}
val.sanitize()
}
Expand All @@ -110,11 +110,11 @@ pub struct SnapshotHash {
impl Sanitize for SnapshotHash {
fn sanitize(&self) -> Result<(), SanitizeError> {
if self.wallclock >= MAX_WALLCLOCK {
return Err(SanitizeError::Failed);
return Err(SanitizeError::ValueOutOfRange);
}
for (slot, _) in &self.hashes {
if *slot >= MAX_SLOT {
return Err(SanitizeError::Failed);
return Err(SanitizeError::ValueOutOfRange);
}
}
self.from.sanitize()
Expand Down Expand Up @@ -156,10 +156,10 @@ impl LowestSlot {
impl Sanitize for LowestSlot {
fn sanitize(&self) -> Result<(), SanitizeError> {
if self.wallclock >= MAX_WALLCLOCK {
return Err(SanitizeError::Failed);
return Err(SanitizeError::ValueOutOfRange);
}
if self.lowest >= MAX_SLOT {
return Err(SanitizeError::Failed);
return Err(SanitizeError::ValueOutOfRange);
}
self.from.sanitize()
}
Expand All @@ -175,7 +175,7 @@ pub struct Vote {
impl Sanitize for Vote {
fn sanitize(&self) -> Result<(), SanitizeError> {
if self.wallclock >= MAX_WALLCLOCK {
return Err(SanitizeError::Failed);
return Err(SanitizeError::ValueOutOfRange);
}
self.from.sanitize()?;
self.transaction.sanitize()
Expand Down Expand Up @@ -465,7 +465,7 @@ mod test {
),
&keypair,
);
assert!(item.sanitize().is_err());
assert_eq!(item.sanitize(), Err(SanitizeError::ValueOutOfRange));
}
#[test]
fn test_compute_vote_index_empty() {
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/hash.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -30,6 +31,8 @@ impl Hasher {
}
}

impl Sanitize for Hash {}

impl AsRef<[u8]> for Hash {
fn as_ref(&self) -> &[u8] {
&self.0[..]
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -232,6 +233,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();
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ impl Sanitize for Message {
}
}
}
self.account_keys.sanitize()?;
self.recent_blockhash.sanitize()?;
self.instructions.sanitize()?;
Ok(())
}
}
Expand Down
14 changes: 12 additions & 2 deletions sdk/src/sanitize.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
#[derive(PartialEq, Debug)]
use thiserror::Error;

#[derive(PartialEq, Debug, Error, Eq, Clone)]
pub enum SanitizeError {
Failed,
#[error("index out of bounds")]
IndexOutOfBounds,
#[error("value out of range")]
ValueOutOfRange,
}

/// 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(())
Expand Down

0 comments on commit 3b1da1c

Please sign in to comment.