Skip to content

Commit

Permalink
thiserror, docs, remove general Failure case (#9741)
Browse files Browse the repository at this point in the history
automerge

(cherry picked from commit a0514eb)

# Conflicts:
#	core/src/crds_value.rs
#	core/src/epoch_slots.rs
#	sdk/src/sanitize.rs
  • Loading branch information
aeyakovenko authored and mergify-bot committed Apr 30, 2020
1 parent e7748c6 commit 8ca451b
Show file tree
Hide file tree
Showing 8 changed files with 611 additions and 11 deletions.
4 changes: 2 additions & 2 deletions core/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub struct PruneData {
impl Sanitize for PruneData {
fn sanitize(&self) -> std::result::Result<(), SanitizeError> {
if self.wallclock >= MAX_WALLCLOCK {
return Err(SanitizeError::ValueOutOfRange);
return Err(SanitizeError::ValueOutOfBounds);
}
Ok(())
}
Expand Down Expand Up @@ -2641,7 +2641,7 @@ mod tests {
let mut pd = PruneData::default();
pd.wallclock = MAX_WALLCLOCK;
let msg = Protocol::PruneMessage(Pubkey::default(), pd);
assert_eq!(msg.sanitize(), Err(SanitizeError::ValueOutOfRange));
assert_eq!(msg.sanitize(), Err(SanitizeError::ValueOutOfBounds));
}

// computes the maximum size for pull request blooms
Expand Down
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::ValueOutOfBounds);
}
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::ValueOutOfBounds));
}
}
62 changes: 56 additions & 6 deletions core/src/crds_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,24 @@ impl Sanitize for CrdsData {
CrdsData::ContactInfo(val) => val.sanitize(),
CrdsData::Vote(ix, val) => {
if *ix >= MAX_VOTES {
return Err(SanitizeError::Failed);
return Err(SanitizeError::ValueOutOfBounds);
}
val.sanitize()
}
<<<<<<< HEAD
=======
CrdsData::LowestSlot(ix, val) => {
if *ix as usize >= 1 {
return Err(SanitizeError::ValueOutOfBounds);
}
val.sanitize()
}
>>>>>>> a0514eb2a... thiserror, docs, remove general Failure case (#9741)
CrdsData::SnapshotHashes(val) => val.sanitize(),
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::ValueOutOfBounds);
}
val.sanitize()
}
Expand All @@ -136,11 +145,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::ValueOutOfBounds);
}
for (slot, _) in &self.hashes {
if *slot >= MAX_SLOT {
return Err(SanitizeError::Failed);
return Err(SanitizeError::ValueOutOfBounds);
}
}
self.from.sanitize()
Expand Down Expand Up @@ -183,10 +192,20 @@ impl EpochSlots {
impl Sanitize for EpochSlots {
fn sanitize(&self) -> Result<(), SanitizeError> {
if self.wallclock >= MAX_WALLCLOCK {
<<<<<<< HEAD
return Err(SanitizeError::Failed);
}
if self.lowest >= MAX_SLOT {
return Err(SanitizeError::Failed);
=======
return Err(SanitizeError::ValueOutOfBounds);
}
if self.lowest >= MAX_SLOT {
return Err(SanitizeError::ValueOutOfBounds);
}
if self.root != 0 {
return Err(SanitizeError::InvalidValue);
>>>>>>> a0514eb2a... thiserror, docs, remove general Failure case (#9741)
}
if self.root >= MAX_SLOT {
return Err(SanitizeError::Failed);
Expand All @@ -211,7 +230,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::ValueOutOfBounds);
}
self.from.sanitize()?;
self.transaction.sanitize()
Expand Down Expand Up @@ -436,8 +455,39 @@ mod test {
EpochSlots::new(Pubkey::default(), 0, 0),
));
assert_eq!(v.wallclock(), 0);
<<<<<<< HEAD
let key = v.clone().epoch_slots().unwrap().from;
assert_eq!(v.label(), CrdsValueLabel::EpochSlots(key));
=======
let key = v.clone().lowest_slot().unwrap().from;
assert_eq!(v.label(), CrdsValueLabel::LowestSlot(key));
}

#[test]
fn test_lowest_slot_sanitize() {
let ls = LowestSlot::new(Pubkey::default(), 0, 0);
let v = CrdsValue::new_unsigned(CrdsData::LowestSlot(0, ls.clone()));
assert_eq!(v.sanitize(), Ok(()));

let mut o = ls.clone();
o.root = 1;
let v = CrdsValue::new_unsigned(CrdsData::LowestSlot(0, o.clone()));
assert_eq!(v.sanitize(), Err(SanitizeError::InvalidValue));

let o = ls.clone();
let v = CrdsValue::new_unsigned(CrdsData::LowestSlot(1, o.clone()));
assert_eq!(v.sanitize(), Err(SanitizeError::ValueOutOfBounds));

let mut o = ls.clone();
o.slots.insert(1);
let v = CrdsValue::new_unsigned(CrdsData::LowestSlot(0, o.clone()));
assert_eq!(v.sanitize(), Err(SanitizeError::InvalidValue));

let mut o = ls.clone();
o.stash.push(deprecated::EpochIncompleteSlots::default());
let v = CrdsValue::new_unsigned(CrdsData::LowestSlot(0, o.clone()));
assert_eq!(v.sanitize(), Err(SanitizeError::InvalidValue));
>>>>>>> a0514eb2a... thiserror, docs, remove general Failure case (#9741)
}

#[test]
Expand Down Expand Up @@ -484,7 +534,7 @@ mod test {
),
&keypair,
);
assert!(item.sanitize().is_err());
assert_eq!(item.sanitize(), Err(SanitizeError::ValueOutOfBounds));
}
#[test]
fn test_compute_vote_index_empty() {
Expand Down
Loading

0 comments on commit 8ca451b

Please sign in to comment.