Skip to content

Commit

Permalink
Inline BlockConfig::ticks_per_slot
Browse files Browse the repository at this point in the history
  • Loading branch information
garious authored and solana-grimes committed Feb 22, 2019
1 parent fb904e7 commit 778583a
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 206 deletions.
12 changes: 6 additions & 6 deletions ledger-tool/tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use solana::blocktree::{create_tmp_sample_ledger, BlocktreeConfig};
use solana_sdk::signature::{Keypair, KeypairUtil};

use assert_cmd::prelude::*;
use solana::blocktree::create_tmp_sample_ledger;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT;
use std::process::Command;
use std::process::Output;
use std::sync::Arc;
Expand Down Expand Up @@ -32,15 +32,15 @@ fn bad_arguments() {
#[test]
fn nominal() {
let keypair = Arc::new(Keypair::new());
let blocktree_config = BlocktreeConfig::default();
let ticks_per_slot = DEFAULT_TICKS_PER_SLOT;
let (_mint_keypair, ledger_path, tick_height, _last_entry_height, _last_id, _last_entry_id) =
create_tmp_sample_ledger(
"test_ledger_tool_nominal",
100,
blocktree_config.ticks_per_slot - 2,
ticks_per_slot - 2,
keypair.pubkey(),
50,
&blocktree_config,
ticks_per_slot,
);

// Basic validation
Expand Down
53 changes: 14 additions & 39 deletions src/blocktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,23 +310,6 @@ impl LedgerColumnFamilyRaw for ErasureCf {
}
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct BlocktreeConfig {
pub ticks_per_slot: u64,
}

impl BlocktreeConfig {
pub fn new(ticks_per_slot: u64) -> Self {
BlocktreeConfig { ticks_per_slot }
}
}

impl Default for BlocktreeConfig {
fn default() -> Self {
Self::new(DEFAULT_TICKS_PER_SLOT)
}
}

// ledger window
pub struct Blocktree {
// Underlying database is automatically closed in the Drop implementation of DB
Expand Down Expand Up @@ -380,8 +363,6 @@ impl Blocktree {
// Create the erasure column family
let erasure_cf = ErasureCf::new(db.clone());

// TODO: make these constructor arguments
// Issue: https://github.com/solana-labs/solana/issues/2458
let ticks_per_slot = DEFAULT_TICKS_PER_SLOT;
Ok(Blocktree {
db,
Expand All @@ -401,22 +382,20 @@ impl Blocktree {
Ok((blocktree, signal_receiver))
}

#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn open_config(ledger_path: &str, config: &BlocktreeConfig) -> Result<Self> {
pub fn open_config(ledger_path: &str, ticks_per_slot: u64) -> Result<Self> {
let mut blocktree = Self::open(ledger_path)?;
blocktree.ticks_per_slot = config.ticks_per_slot;
blocktree.ticks_per_slot = ticks_per_slot;
Ok(blocktree)
}

#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn open_with_config_signal(
ledger_path: &str,
config: &BlocktreeConfig,
ticks_per_slot: u64,
) -> Result<(Self, Receiver<bool>)> {
let mut blocktree = Self::open(ledger_path)?;
let (signal_sender, signal_receiver) = sync_channel(1);
blocktree.new_blobs_signals = vec![signal_sender];
blocktree.ticks_per_slot = config.ticks_per_slot;
blocktree.ticks_per_slot = ticks_per_slot;

Ok((blocktree, signal_receiver))
}
Expand Down Expand Up @@ -1270,12 +1249,12 @@ pub fn create_new_ledger(
ledger_path: &str,
genesis_block: &GenesisBlock,
) -> Result<(u64, u64, Hash)> {
let config = BlocktreeConfig::new(genesis_block.ticks_per_slot);
let ticks_per_slot = genesis_block.ticks_per_slot;
Blocktree::destroy(ledger_path)?;
genesis_block.write(&ledger_path)?;

// Add a single tick linked back to the genesis_block to bootstrap the ledger
let blocktree = Blocktree::open_config(ledger_path, &config)?;
let blocktree = Blocktree::open_config(ledger_path, ticks_per_slot)?;
let entries = crate::entry::create_ticks(1, genesis_block.last_id());
blocktree.write_entries(DEFAULT_SLOT_HEIGHT, 0, 0, &entries)?;

Expand Down Expand Up @@ -1318,19 +1297,18 @@ pub fn get_tmp_ledger_path(name: &str) -> String {
path
}

#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn create_tmp_sample_ledger(
name: &str,
num_tokens: u64,
num_extra_ticks: u64,
bootstrap_leader_id: Pubkey,
bootstrap_leader_tokens: u64,
config: &BlocktreeConfig,
ticks_per_slot: u64,
) -> (Keypair, String, u64, u64, Hash, Hash) {
// TODO: Pass in a genesis block instead of all its parameters.
let (mut genesis_block, mint_keypair) =
GenesisBlock::new_with_leader(num_tokens, bootstrap_leader_id, bootstrap_leader_tokens);
genesis_block.ticks_per_slot = config.ticks_per_slot;
genesis_block.ticks_per_slot = ticks_per_slot;

let ledger_path = get_tmp_ledger_path(name);
let (mut entry_height, mut tick_height, mut last_entry_id) =
Expand All @@ -1340,7 +1318,7 @@ pub fn create_tmp_sample_ledger(
if num_extra_ticks > 0 {
let entries = crate::entry::create_ticks(num_extra_ticks, last_entry_id);

let blocktree = Blocktree::open_config(&ledger_path, config).unwrap();
let blocktree = Blocktree::open_config(&ledger_path, ticks_per_slot).unwrap();
blocktree
.write_entries(DEFAULT_SLOT_HEIGHT, tick_height, entry_height, &entries)
.unwrap();
Expand All @@ -1359,16 +1337,15 @@ pub fn create_tmp_sample_ledger(
)
}

#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn tmp_copy_ledger(from: &str, name: &str, config: &BlocktreeConfig) -> String {
pub fn tmp_copy_ledger(from: &str, name: &str, ticks_per_slot: u64) -> String {
let path = get_tmp_ledger_path(name);

let blocktree = Blocktree::open_config(from, config).unwrap();
let blocktree = Blocktree::open_config(from, ticks_per_slot).unwrap();
let blobs = blocktree.read_ledger_blobs();
let genesis_block = GenesisBlock::load(from).unwrap();

Blocktree::destroy(&path).expect("Expected successful database destruction");
let blocktree = Blocktree::open_config(&path, config).unwrap();
let blocktree = Blocktree::open_config(&path, ticks_per_slot).unwrap();
blocktree.write_blobs(blobs).unwrap();
genesis_block.write(&path).unwrap();

Expand Down Expand Up @@ -1397,8 +1374,7 @@ pub mod tests {
let ticks_per_slot = 10;
let num_slots = 10;
let num_ticks = ticks_per_slot * num_slots;
let config = BlocktreeConfig::new(ticks_per_slot);
let ledger = Blocktree::open_config(&ledger_path, &config).unwrap();
let ledger = Blocktree::open_config(&ledger_path, ticks_per_slot).unwrap();

let ticks = create_ticks(num_ticks, Hash::default());
ledger.write_entries(0, 0, 0, ticks.clone()).unwrap();
Expand Down Expand Up @@ -1779,8 +1755,7 @@ pub mod tests {
pub fn test_insert_data_blobs_consecutive() {
let blocktree_path = get_tmp_ledger_path("test_insert_data_blobs_consecutive");
{
let config = BlocktreeConfig::new(32);
let blocktree = Blocktree::open_config(&blocktree_path, &config).unwrap();
let blocktree = Blocktree::open_config(&blocktree_path, 32).unwrap();
let slot = 0;
let parent_slot = 0;
// Write entries
Expand Down
49 changes: 20 additions & 29 deletions src/blocktree_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,22 +274,23 @@ pub fn process_blocktree(
#[cfg(test)]
mod tests {
use super::*;
use crate::blocktree::create_tmp_sample_ledger;
use crate::blocktree::tests::entries_to_blobs;
use crate::blocktree::{create_tmp_sample_ledger, BlocktreeConfig};
use crate::entry::{create_ticks, next_entry, Entry};
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::native_program::ProgramError;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_transaction::SystemTransaction;
use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT;

fn fill_blocktree_slot_with_ticks(
blocktree: &Blocktree,
blocktree_config: &BlocktreeConfig,
ticks_per_slot: u64,
slot: u64,
parent_slot: u64,
last_entry_id: Hash,
) -> Hash {
let entries = create_ticks(blocktree_config.ticks_per_slot, last_entry_id);
let entries = create_ticks(ticks_per_slot, last_entry_id);
let last_entry_id = entries.last().unwrap().id;

let blobs = entries_to_blobs(&entries, slot, parent_slot);
Expand All @@ -303,20 +304,20 @@ mod tests {
solana_logger::setup();

let leader_scheduler = Arc::new(RwLock::new(LeaderScheduler::default()));
let blocktree_config = &BlocktreeConfig::default();
let ticks_per_slot = DEFAULT_TICKS_PER_SLOT;

// Create a new ledger with slot 0 full of ticks
let (_mint_keypair, ledger_path, tick_height, _entry_height, _last_id, mut last_entry_id) =
create_tmp_sample_ledger(
"blocktree_with_two_forks",
10_000,
blocktree_config.ticks_per_slot - 1,
ticks_per_slot - 1,
Keypair::new().pubkey(),
123,
&blocktree_config,
ticks_per_slot,
);
debug!("ledger_path: {:?}", ledger_path);
assert_eq!(tick_height, blocktree_config.ticks_per_slot);
assert_eq!(tick_height, ticks_per_slot);

/*
Build a blocktree in the ledger with the following fork structure:
Expand All @@ -334,30 +335,20 @@ mod tests {
*/
let genesis_block =
GenesisBlock::load(&ledger_path).expect("Expected to successfully open genesis block");
let blocktree = Blocktree::open_config(&ledger_path, &blocktree_config)
let blocktree = Blocktree::open_config(&ledger_path, ticks_per_slot)
.expect("Expected to successfully open database ledger");

// Fork 1, ending at slot 3
let last_slot1_entry_id =
fill_blocktree_slot_with_ticks(&blocktree, &blocktree_config, 1, 0, last_entry_id);
last_entry_id = fill_blocktree_slot_with_ticks(
&blocktree,
&blocktree_config,
2,
1,
last_slot1_entry_id,
);
fill_blocktree_slot_with_ticks(&blocktree, ticks_per_slot, 1, 0, last_entry_id);
last_entry_id =
fill_blocktree_slot_with_ticks(&blocktree, ticks_per_slot, 2, 1, last_slot1_entry_id);
let last_fork1_entry_id =
fill_blocktree_slot_with_ticks(&blocktree, &blocktree_config, 3, 2, last_entry_id);
fill_blocktree_slot_with_ticks(&blocktree, ticks_per_slot, 3, 2, last_entry_id);

// Fork 2, ending at slot 4
let last_fork2_entry_id = fill_blocktree_slot_with_ticks(
&blocktree,
&blocktree_config,
4,
1,
last_slot1_entry_id,
);
let last_fork2_entry_id =
fill_blocktree_slot_with_ticks(&blocktree, ticks_per_slot, 4, 1, last_slot1_entry_id);

info!("last_fork1_entry_id: {:?}", last_fork1_entry_id);
info!("last_fork2_entry_id: {:?}", last_fork2_entry_id);
Expand All @@ -370,15 +361,15 @@ mod tests {
bank_forks_info[0],
BankForksInfo {
bank_id: 2, // Fork 1 diverged with slot 2
entry_height: blocktree_config.ticks_per_slot * 4,
entry_height: ticks_per_slot * 4,
last_entry_id: last_fork1_entry_id,
}
);
assert_eq!(
bank_forks_info[1],
BankForksInfo {
bank_id: 4, // Fork 2 diverged with slot 4
entry_height: blocktree_config.ticks_per_slot * 3,
entry_height: ticks_per_slot * 3,
last_entry_id: last_fork2_entry_id,
}
);
Expand Down Expand Up @@ -472,7 +463,7 @@ mod tests {

#[test]
fn test_process_ledger_simple() {
let blocktree_config = BlocktreeConfig::default();
let ticks_per_slot = DEFAULT_TICKS_PER_SLOT;
let leader_scheduler = Arc::new(RwLock::new(LeaderScheduler::default()));

let (
Expand All @@ -488,7 +479,7 @@ mod tests {
0,
Keypair::new().pubkey(),
50,
&blocktree_config,
ticks_per_slot,
);
debug!("ledger_path: {:?}", ledger_path);
let genesis_block =
Expand Down Expand Up @@ -518,7 +509,7 @@ mod tests {
last_id = last_entry_id;
entries.push(tick);

let blocktree = Blocktree::open_config(&ledger_path, &blocktree_config)
let blocktree = Blocktree::open_config(&ledger_path, ticks_per_slot)
.expect("Expected to successfully open database ledger");

blocktree
Expand Down
6 changes: 2 additions & 4 deletions src/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn chacha_cbc_encrypt_ledger(
#[cfg(test)]
mod tests {
use crate::blocktree::get_tmp_ledger_path;
use crate::blocktree::{Blocktree, BlocktreeConfig, DEFAULT_SLOT_HEIGHT};
use crate::blocktree::{Blocktree, DEFAULT_SLOT_HEIGHT};
use crate::chacha::chacha_cbc_encrypt_ledger;
use crate::entry::Entry;
use ring::signature::Ed25519KeyPair;
Expand Down Expand Up @@ -145,9 +145,7 @@ mod tests {
let ledger_dir = "chacha_test_encrypt_file";
let ledger_path = get_tmp_ledger_path(ledger_dir);
let ticks_per_slot = 16;
let blocktree = Arc::new(
Blocktree::open_config(&ledger_path, &BlocktreeConfig::new(ticks_per_slot)).unwrap(),
);
let blocktree = Arc::new(Blocktree::open_config(&ledger_path, ticks_per_slot).unwrap());
let out_path = Path::new("test_chacha_encrypt_file_output.txt.enc");

let entries = make_tiny_deterministic_test_entries(32);
Expand Down
10 changes: 3 additions & 7 deletions src/chacha_cuda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn chacha_cbc_encrypt_file_many_keys(
#[cfg(test)]
mod tests {
use crate::blocktree::get_tmp_ledger_path;
use crate::blocktree::{Blocktree, BlocktreeConfig, DEFAULT_SLOT_HEIGHT};
use crate::blocktree::{Blocktree, DEFAULT_SLOT_HEIGHT};
use crate::chacha::chacha_cbc_encrypt_ledger;
use crate::chacha_cuda::chacha_cbc_encrypt_file_many_keys;
use crate::entry::make_tiny_test_entries;
Expand All @@ -128,9 +128,7 @@ mod tests {
let ledger_dir = "test_encrypt_file_many_keys_single";
let ledger_path = get_tmp_ledger_path(ledger_dir);
let ticks_per_slot = 16;
let blocktree = Arc::new(
Blocktree::open_config(&ledger_path, &BlocktreeConfig::new(ticks_per_slot)).unwrap(),
);
let blocktree = Arc::new(Blocktree::open_config(&ledger_path, ticks_per_slot).unwrap());

blocktree
.write_entries(DEFAULT_SLOT_HEIGHT, 0, 0, &entries)
Expand Down Expand Up @@ -166,9 +164,7 @@ mod tests {
let ledger_dir = "test_encrypt_file_many_keys_multiple";
let ledger_path = get_tmp_ledger_path(ledger_dir);
let ticks_per_slot = 16;
let blocktree = Arc::new(
Blocktree::open_config(&ledger_path, &BlocktreeConfig::new(ticks_per_slot)).unwrap(),
);
let blocktree = Arc::new(Blocktree::open_config(&ledger_path, ticks_per_slot).unwrap());
blocktree
.write_entries(DEFAULT_SLOT_HEIGHT, 0, 0, &entries)
.unwrap();
Expand Down
Loading

0 comments on commit 778583a

Please sign in to comment.