Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 3dbbb39

Browse files
rob-solanagarious
authored andcommitted
use next_entries() in recorder, recycle blobs in reconstruct_from_blobs
1 parent 17e8ad1 commit 3dbbb39

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

src/ledger.rs

+28-11
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,26 @@ impl Block for [Entry] {
4141
}
4242
}
4343

44-
pub fn reconstruct_entries_from_blobs(blobs: &VecDeque<SharedBlob>) -> bincode::Result<Vec<Entry>> {
44+
pub fn reconstruct_entries_from_blobs(
45+
blobs: VecDeque<SharedBlob>,
46+
blob_recycler: &packet::BlobRecycler,
47+
) -> bincode::Result<Vec<Entry>> {
4548
let mut entries: Vec<Entry> = Vec::with_capacity(blobs.len());
46-
for msgs in blobs {
47-
let blob = msgs.read().unwrap();
48-
let entry: Entry = deserialize(&blob.data()[..blob.meta.size])?;
49-
entries.push(entry);
49+
50+
for blob in blobs {
51+
let entry = {
52+
let msg = blob.read().unwrap();
53+
deserialize(&msg.data()[..msg.meta.size])
54+
};
55+
blob_recycler.recycle(blob);
56+
57+
match entry {
58+
Ok(entry) => entries.push(entry),
59+
Err(err) => {
60+
trace!("reconstruct_entry_from_blobs: {}", err);
61+
return Err(err);
62+
}
63+
}
5064
}
5165
Ok(entries)
5266
}
@@ -148,15 +162,18 @@ mod tests {
148162
let mut blob_q = VecDeque::new();
149163
entries.to_blobs(&blob_recycler, &mut blob_q);
150164

151-
assert_eq!(reconstruct_entries_from_blobs(&blob_q).unwrap(), entries);
165+
assert_eq!(
166+
reconstruct_entries_from_blobs(blob_q, &blob_recycler).unwrap(),
167+
entries
168+
);
152169
}
153170

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

162179
#[test]
@@ -206,10 +223,10 @@ mod bench {
206223
bencher.iter(|| {
207224
let mut blob_q = VecDeque::new();
208225
entries.to_blobs(&blob_recycler, &mut blob_q);
209-
assert_eq!(reconstruct_entries_from_blobs(&blob_q).unwrap(), entries);
210-
for blob in blob_q {
211-
blob_recycler.recycle(blob);
212-
}
226+
assert_eq!(
227+
reconstruct_entries_from_blobs(blob_q, &blob_recycler).unwrap(),
228+
entries
229+
);
213230
});
214231
}
215232

src/record_stage.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,15 @@ impl RecordStage {
9090
} else {
9191
vec![]
9292
};
93-
let entry = recorder.record(txs);
94-
sender.send(entry).map_err(|_| ())
93+
let entries = recorder.record(txs);
94+
let mut result = Ok(());
95+
for entry in entries {
96+
result = sender.send(entry).map_err(|_| ());
97+
if result.is_err() {
98+
break;
99+
}
100+
}
101+
result
95102
}
96103

97104
fn process_signals(

src/recorder.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
use entry::Entry;
55
use hash::{hash, Hash};
6+
use ledger::next_entries_mut;
67
use std::time::{Duration, Instant};
78
use transaction::Transaction;
89

@@ -26,15 +27,19 @@ impl Recorder {
2627
self.num_hashes += 1;
2728
}
2829

29-
pub fn record(&mut self, transactions: Vec<Transaction>) -> Entry {
30-
Entry::new_mut(&mut self.last_hash, &mut self.num_hashes, transactions)
30+
pub fn record(&mut self, transactions: Vec<Transaction>) -> Vec<Entry> {
31+
next_entries_mut(&mut self.last_hash, &mut self.num_hashes, transactions)
3132
}
3233

3334
pub fn tick(&mut self, start_time: Instant, tick_duration: Duration) -> Option<Entry> {
3435
if start_time.elapsed() > tick_duration * (self.num_ticks + 1) {
3536
// TODO: don't let this overflow u32
3637
self.num_ticks += 1;
37-
Some(self.record(vec![]))
38+
Some(Entry::new_mut(
39+
&mut self.last_hash,
40+
&mut self.num_hashes,
41+
vec![],
42+
))
3843
} else {
3944
None
4045
}

src/replicate_stage.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ impl ReplicateStage {
2323
) -> Result<()> {
2424
let timer = Duration::new(1, 0);
2525
let blobs = blob_receiver.recv_timeout(timer)?;
26-
let entries = ledger::reconstruct_entries_from_blobs(&blobs)?;
26+
let blobs_len = blobs.len();
27+
let entries = ledger::reconstruct_entries_from_blobs(blobs, &blob_recycler)?;
2728
let res = bank.process_entries(entries);
2829
if res.is_err() {
29-
error!("process_entries {} {:?}", blobs.len(), res);
30+
error!("process_entries {} {:?}", blobs_len, res);
3031
}
3132
res?;
32-
for blob in blobs {
33-
blob_recycler.recycle(blob);
34-
}
3533
Ok(())
3634
}
3735

0 commit comments

Comments
 (0)