Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't panic if sent a bad packet #369

Merged
merged 1 commit into from
Jun 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The `ledger` module provides functions for parallel verification of the
//! Proof of History ledger.

use bincode::{deserialize, serialize_into};
use bincode::{self, deserialize, serialize_into};
use entry::{next_entry, Entry};
use hash::Hash;
use packet;
Expand Down Expand Up @@ -104,12 +104,12 @@ pub fn next_entries(
entries
}

pub fn reconstruct_entries_from_blobs(blobs: &VecDeque<SharedBlob>) -> Vec<Entry> {
pub fn reconstruct_entries_from_blobs(blobs: &VecDeque<SharedBlob>) -> bincode::Result<Vec<Entry>> {
let mut entries_to_apply: Vec<Entry> = Vec::new();
let mut last_id = Hash::default();
for msgs in blobs {
let blob = msgs.read().unwrap();
let entries: Vec<Entry> = deserialize(&blob.data()[..blob.meta.size]).unwrap();
let entries: Vec<Entry> = deserialize(&blob.data()[..blob.meta.size])?;
for entry in entries {
if entry.id == last_id {
if let Some(last_entry) = entries_to_apply.last_mut() {
Expand All @@ -120,9 +120,8 @@ pub fn reconstruct_entries_from_blobs(blobs: &VecDeque<SharedBlob>) -> Vec<Entry
entries_to_apply.push(entry);
}
}
//TODO respond back to leader with hash of the state
}
entries_to_apply
Ok(entries_to_apply)
}

#[cfg(test)]
Expand All @@ -131,6 +130,7 @@ mod tests {
use hash::hash;
use packet::BlobRecycler;
use signature::{KeyPair, KeyPairUtil};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use transaction::Transaction;

#[test]
Expand Down Expand Up @@ -161,7 +161,15 @@ mod tests {
let mut blob_q = VecDeque::new();
entries.to_blobs(&blob_recycler, &mut blob_q);

assert_eq!(reconstruct_entries_from_blobs(&blob_q), entries);
assert_eq!(reconstruct_entries_from_blobs(&blob_q).unwrap(), entries);
}

#[test]
fn test_bad_blobs_attack() {
let blob_recycler = BlobRecycler::default();
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8000);
let blobs_q = packet::to_blobs(vec![(0, addr)], &blob_recycler).unwrap(); // <-- attack!
assert!(reconstruct_entries_from_blobs(&blobs_q).is_err());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/replicate_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl ReplicateStage {
) -> Result<()> {
let timer = Duration::new(1, 0);
let blobs = blob_receiver.recv_timeout(timer)?;
let entries = ledger::reconstruct_entries_from_blobs(&blobs);
let entries = ledger::reconstruct_entries_from_blobs(&blobs)?;
let res = bank.process_entries(entries);
if res.is_err() {
error!("process_entries {} {:?}", blobs.len(), res);
Expand Down