Skip to content

Commit

Permalink
Verify number of hashes for each block of entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Oct 17, 2019
1 parent 7e6e7e8 commit 76115e7
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 85 deletions.
14 changes: 9 additions & 5 deletions core/benches/blocktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ fn setup_read_bench(
slot: u64,
) {
// Make some big and small entries
let entries = create_ticks(num_large_shreds * 4 + num_small_shreds * 2, Hash::default());
let entries = create_ticks(
num_large_shreds * 4 + num_small_shreds * 2,
0,
Hash::default(),
);

// Convert the entries to shreds, write the shreds to the ledger
let shreds = entries_to_test_shreds(entries, slot, slot.saturating_sub(1), true);
Expand All @@ -50,7 +54,7 @@ fn setup_read_bench(
fn bench_write_small(bench: &mut Bencher) {
let ledger_path = get_tmp_ledger_path!();
let num_entries = 32 * 1024;
let entries = create_ticks(num_entries, Hash::default());
let entries = create_ticks(num_entries, 0, Hash::default());
bench_write_shreds(bench, entries, &ledger_path);
}

Expand All @@ -60,7 +64,7 @@ fn bench_write_small(bench: &mut Bencher) {
fn bench_write_big(bench: &mut Bencher) {
let ledger_path = get_tmp_ledger_path!();
let num_entries = 32 * 1024;
let entries = create_ticks(num_entries, Hash::default());
let entries = create_ticks(num_entries, 0, Hash::default());
bench_write_shreds(bench, entries, &ledger_path);
}

Expand Down Expand Up @@ -129,7 +133,7 @@ fn bench_insert_data_shred_small(bench: &mut Bencher) {
let blocktree =
Blocktree::open(&ledger_path).expect("Expected to be able to open database ledger");
let num_entries = 32 * 1024;
let entries = create_ticks(num_entries, Hash::default());
let entries = create_ticks(num_entries, 0, Hash::default());
bench.iter(move || {
let shreds = entries_to_test_shreds(entries.clone(), 0, 0, true);
blocktree.insert_shreds(shreds, None).unwrap();
Expand All @@ -144,7 +148,7 @@ fn bench_insert_data_shred_big(bench: &mut Bencher) {
let blocktree =
Blocktree::open(&ledger_path).expect("Expected to be able to open database ledger");
let num_entries = 32 * 1024;
let entries = create_ticks(num_entries, Hash::default());
let entries = create_ticks(num_entries, 0, Hash::default());
bench.iter(move || {
let shreds = entries_to_test_shreds(entries.clone(), 0, 0, true);
blocktree.insert_shreds(shreds, None).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions core/src/blockstream_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ mod test {
let (slot_full_sender, slot_full_receiver) = channel();

// Create entries - 4 ticks + 1 populated entry + 1 tick
let mut entries = create_ticks(4, Hash::default());
let mut entries = create_ticks(4, 0, Hash::default());

let keypair = Keypair::new();
let mut blockhash = entries[3].hash;
Expand All @@ -147,7 +147,7 @@ mod test {
let entry = Entry::new(&mut blockhash, 1, vec![tx]);
blockhash = entry.hash;
entries.push(entry);
let final_tick = create_ticks(1, blockhash);
let final_tick = create_ticks(1, 0, blockhash);
entries.extend_from_slice(&final_tick);

let expected_entries = entries.clone();
Expand Down
35 changes: 14 additions & 21 deletions core/src/blocktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,14 +820,7 @@ impl Blocktree {
let mut remaining_ticks_in_slot = ticks_per_slot - num_ticks_in_start_slot;

let mut current_slot = start_slot;
let mut parent_slot = parent.map_or(
if current_slot == 0 {
current_slot
} else {
current_slot - 1
},
|v| v,
);
let mut parent_slot = parent.map_or(current_slot.saturating_sub(1), |v| v);
let mut shredder = Shredder::new(current_slot, parent_slot, 0.0, keypair.clone())
.expect("Failed to create entry shredder");
let mut all_shreds = vec![];
Expand Down Expand Up @@ -1547,14 +1540,14 @@ fn slot_has_updates(slot_meta: &SlotMeta, slot_meta_backup: &Option<SlotMeta>) -
//
// Returns the blockhash that can be used to append entries with.
pub fn create_new_ledger(ledger_path: &Path, genesis_block: &GenesisBlock) -> Result<Hash> {
let ticks_per_slot = genesis_block.ticks_per_slot;
Blocktree::destroy(ledger_path)?;
genesis_block.write(&ledger_path)?;

// Fill slot 0 with ticks that link back to the genesis_block to bootstrap the ledger.
let blocktree = Blocktree::open(ledger_path)?;

let entries = crate::entry::create_ticks(ticks_per_slot, genesis_block.hash());
let ticks_per_slot = genesis_block.ticks_per_slot;
let hashes_per_tick = genesis_block.poh_config.hashes_per_tick.unwrap_or(0);
let entries = crate::entry::create_ticks(ticks_per_slot, hashes_per_tick, genesis_block.hash());
let last_hash = entries.last().unwrap().hash;

let shredder = Shredder::new(0, 0, 0.0, Arc::new(Keypair::new()))
Expand Down Expand Up @@ -1669,7 +1662,7 @@ pub mod tests {
let (ledger_path, _blockhash) = create_new_tmp_ledger!(&genesis_block);
let ledger = Blocktree::open(&ledger_path).unwrap();

let ticks = create_ticks(genesis_block.ticks_per_slot, genesis_block.hash());
let ticks = create_ticks(genesis_block.ticks_per_slot, 0, genesis_block.hash());
let entries = ledger.get_slot_entries(0, 0, None).unwrap();

assert_eq!(ticks, entries);
Expand Down Expand Up @@ -1723,7 +1716,7 @@ pub mod tests {
let mut shreds_per_slot = vec![];

for i in 0..num_slots {
let mut new_ticks = create_ticks(ticks_per_slot, Hash::default());
let mut new_ticks = create_ticks(ticks_per_slot, 0, Hash::default());
let num_shreds = ledger
.write_entries(
i,
Expand Down Expand Up @@ -2052,7 +2045,7 @@ pub mod tests {
let blocktree_path = get_tmp_ledger_path("test_get_slot_entries1");
{
let blocktree = Blocktree::open(&blocktree_path).unwrap();
let entries = create_ticks(8, Hash::default());
let entries = create_ticks(8, 0, Hash::default());
let shreds = entries_to_test_shreds(entries[0..4].to_vec(), 1, 0, false);
blocktree
.insert_shreds(shreds, None)
Expand Down Expand Up @@ -2087,7 +2080,7 @@ pub mod tests {
let num_slots = 5 as u64;
let mut index = 0;
for slot in 0..num_slots {
let entries = create_ticks(slot + 1, Hash::default());
let entries = create_ticks(slot + 1, 0, Hash::default());
let last_entry = entries.last().unwrap().clone();
let mut shreds =
entries_to_test_shreds(entries, slot, slot.saturating_sub(1), false);
Expand Down Expand Up @@ -2119,13 +2112,13 @@ pub mod tests {
let num_slots = 5 as u64;
let shreds_per_slot = 5 as u64;
let entry_serialized_size =
bincode::serialized_size(&create_ticks(1, Hash::default())).unwrap();
bincode::serialized_size(&create_ticks(1, 0, Hash::default())).unwrap();
let entries_per_slot =
(shreds_per_slot * PACKET_DATA_SIZE as u64) / entry_serialized_size;

// Write entries
for slot in 0..num_slots {
let entries = create_ticks(entries_per_slot, Hash::default());
let entries = create_ticks(entries_per_slot, 0, Hash::default());
let shreds =
entries_to_test_shreds(entries.clone(), slot, slot.saturating_sub(1), false);
assert!(shreds.len() as u64 >= shreds_per_slot);
Expand Down Expand Up @@ -2908,7 +2901,7 @@ pub mod tests {
assert!(gap > 3);
// Create enough entries to ensure there are at least two shreds created
let num_entries = max_ticks_per_n_shreds(1) + 1;
let entries = create_ticks(num_entries, Hash::default());
let entries = create_ticks(num_entries, 0, Hash::default());
let mut shreds = entries_to_test_shreds(entries, slot, 0, true);
let num_shreds = shreds.len();
assert!(num_shreds > 1);
Expand Down Expand Up @@ -3000,7 +2993,7 @@ pub mod tests {
assert_eq!(blocktree.find_missing_data_indexes(slot, 4, 3, 1), empty);
assert_eq!(blocktree.find_missing_data_indexes(slot, 1, 2, 0), empty);

let entries = create_ticks(100, Hash::default());
let entries = create_ticks(100, 0, Hash::default());
let mut shreds = entries_to_test_shreds(entries, slot, 0, true);
assert!(shreds.len() > 2);
shreds.drain(2..);
Expand Down Expand Up @@ -3042,7 +3035,7 @@ pub mod tests {

// Write entries
let num_entries = 10;
let entries = create_ticks(num_entries, Hash::default());
let entries = create_ticks(num_entries, 0, Hash::default());
let shreds = entries_to_test_shreds(entries, slot, 0, true);
let num_shreds = shreds.len();

Expand Down Expand Up @@ -3474,7 +3467,7 @@ pub mod tests {
parent_slot: u64,
num_entries: u64,
) -> (Vec<Shred>, Vec<Entry>) {
let entries = create_ticks(num_entries, Hash::default());
let entries = create_ticks(num_entries, 0, Hash::default());
let shreds = entries_to_test_shreds(entries.clone(), slot, parent_slot, true);
(shreds, entries)
}
Expand Down
Loading

0 comments on commit 76115e7

Please sign in to comment.