Skip to content

Commit

Permalink
LocalCluster - rename, refactor consts, verify initial transfers (#4437)
Browse files Browse the repository at this point in the history
* cluster_lamports -> mint_lamports

* Move default const. Actually use it as the default

* verify mint has enough for initial funding transfers

* >=
  • Loading branch information
apfitzge authored Jan 13, 2025
1 parent 68b6afd commit 9861e66
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 64 deletions.
4 changes: 2 additions & 2 deletions accounts-cluster-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ pub mod test {
initialize_and_add_secondary_indexes(&mut validator_config);
let num_nodes = 1;
let mut config = ClusterConfig {
cluster_lamports: 10_000_000,
mint_lamports: 10_000_000,
poh_config: PohConfig::new_sleep(Duration::from_millis(50)),
node_stakes: vec![100; num_nodes],
validator_configs: make_identical_validator_configs(&validator_config, num_nodes),
Expand Down Expand Up @@ -1482,7 +1482,7 @@ pub mod test {
initialize_and_add_secondary_indexes(&mut validator_config);
let num_nodes = 1;
let mut config = ClusterConfig {
cluster_lamports: 10_000_000,
mint_lamports: 10_000_000,
poh_config: PohConfig::new_sleep(Duration::from_millis(50)),
node_stakes: vec![100; num_nodes],
validator_configs: make_identical_validator_configs(&validator_config, num_nodes),
Expand Down
2 changes: 1 addition & 1 deletion bench-tps/tests/bench_tps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn test_bench_tps_local_cluster(config: Config) {
let cluster = LocalCluster::new(
&mut ClusterConfig {
node_stakes: vec![999_990; NUM_NODES],
cluster_lamports: 200_000_000,
mint_lamports: 200_000_000,
validator_configs: make_identical_validator_configs(
&ValidatorConfig {
rpc_config: JsonRpcConfig {
Expand Down
2 changes: 1 addition & 1 deletion dos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ pub mod test {
let cluster = LocalCluster::new(
&mut ClusterConfig {
node_stakes: vec![999_990; num_nodes],
cluster_lamports: 200_000_000,
mint_lamports: 200_000_000,
validator_configs: make_identical_validator_configs(
&ValidatorConfig {
rpc_config: JsonRpcConfig {
Expand Down
7 changes: 3 additions & 4 deletions local-cluster/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ use {
pub const RUST_LOG_FILTER: &str =
"error,solana_core::replay_stage=warn,solana_local_cluster=info,local_cluster=info";

pub const DEFAULT_CLUSTER_LAMPORTS: u64 = 10_000_000 * LAMPORTS_PER_SOL;
pub const DEFAULT_NODE_STAKE: u64 = 10 * LAMPORTS_PER_SOL;

pub fn last_vote_in_tower(tower_path: &Path, node_pubkey: &Pubkey) -> Option<(Slot, Hash)> {
Expand Down Expand Up @@ -319,7 +318,7 @@ pub fn run_cluster_partition<C>(
.map(|stake_weight| 100 * *stake_weight as u64)
.collect();
assert_eq!(node_stakes.len(), num_nodes);
let cluster_lamports = node_stakes.iter().sum::<u64>() * 2;
let mint_lamports = node_stakes.iter().sum::<u64>() * 2;
let turbine_disabled = Arc::new(AtomicBool::new(false));
let mut validator_config = ValidatorConfig {
turbine_disabled: turbine_disabled.clone(),
Expand Down Expand Up @@ -351,7 +350,7 @@ pub fn run_cluster_partition<C>(

let slots_per_epoch = 2048;
let mut config = ClusterConfig {
cluster_lamports,
mint_lamports,
node_stakes,
validator_configs: make_identical_validator_configs(&validator_config, num_nodes),
validator_keys: Some(
Expand Down Expand Up @@ -490,7 +489,7 @@ pub fn test_faulty_node(
}

let mut cluster_config = ClusterConfig {
cluster_lamports: 10_000,
mint_lamports: 10_000,
node_stakes,
validator_configs,
validator_keys: Some(validator_keys.clone()),
Expand Down
40 changes: 27 additions & 13 deletions local-cluster/src/local_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use {
epoch_schedule::EpochSchedule,
genesis_config::{ClusterType, GenesisConfig},
message::Message,
native_token::LAMPORTS_PER_SOL,
poh_config::PohConfig,
pubkey::Pubkey,
signature::{Keypair, Signature, Signer},
Expand Down Expand Up @@ -68,6 +69,7 @@ use {
},
};

pub const DEFAULT_MINT_LAMPORTS: u64 = 10_000_000 * LAMPORTS_PER_SOL;
const DUMMY_SNAPSHOT_CONFIG_PATH_MARKER: &str = "dummy";

pub struct ClusterConfig {
Expand All @@ -85,8 +87,8 @@ pub struct ClusterConfig {
pub node_stakes: Vec<u64>,
/// Optional vote keypairs to use for each node
pub node_vote_keys: Option<Vec<Arc<Keypair>>>,
/// The total lamports available to the cluster
pub cluster_lamports: u64,
/// The number of lamports in the mint account
pub mint_lamports: u64,
pub ticks_per_slot: u64,
pub slots_per_epoch: u64,
pub stakers_slot_offset: u64,
Expand All @@ -103,12 +105,12 @@ pub struct ClusterConfig {
impl ClusterConfig {
pub fn new_with_equal_stakes(
num_nodes: usize,
cluster_lamports: u64,
mint_lamports: u64,
lamports_per_node: u64,
) -> Self {
Self {
node_stakes: vec![lamports_per_node; num_nodes],
cluster_lamports,
mint_lamports,
validator_configs: make_identical_validator_configs(
&ValidatorConfig::default_for_test(),
num_nodes,
Expand All @@ -126,7 +128,7 @@ impl Default for ClusterConfig {
validator_keys: None,
node_stakes: vec![],
node_vote_keys: None,
cluster_lamports: 0,
mint_lamports: DEFAULT_MINT_LAMPORTS,
ticks_per_slot: DEFAULT_TICKS_PER_SLOT,
slots_per_epoch: DEFAULT_DEV_SLOTS_PER_EPOCH,
stakers_slot_offset: DEFAULT_DEV_SLOTS_PER_EPOCH,
Expand Down Expand Up @@ -155,16 +157,12 @@ pub struct LocalCluster {
impl LocalCluster {
pub fn new_with_equal_stakes(
num_nodes: usize,
cluster_lamports: u64,
mint_lamports: u64,
lamports_per_node: u64,
socket_addr_space: SocketAddrSpace,
) -> Self {
Self::new(
&mut ClusterConfig::new_with_equal_stakes(
num_nodes,
cluster_lamports,
lamports_per_node,
),
&mut ClusterConfig::new_with_equal_stakes(num_nodes, mint_lamports, lamports_per_node),
socket_addr_space,
)
}
Expand Down Expand Up @@ -254,6 +252,10 @@ impl LocalCluster {
}
};

// Mint used to fund validator identities for non-genesis accounts.
// Verify we have enough lamports in the mint address to do those transfers.
let mut required_mint_lamports = 0;

// Bootstrap leader should always be in genesis block
validator_keys[0].1 = true;
let (keys_in_genesis, stakes_in_genesis): (Vec<ValidatorVoteKeypairs>, Vec<u64>) =
Expand All @@ -278,10 +280,18 @@ impl LocalCluster {
stake,
))
} else {
required_mint_lamports += Self::required_validator_funding(*stake);
None
}
})
.unzip();

// Verify mint has enough lamports to fund all required validators.
assert!(
config.mint_lamports >= required_mint_lamports,
"mint requires additional lamports to fund validators"
);

let leader_keypair = &keys_in_genesis[0].node_keypair;
let leader_vote_keypair = &keys_in_genesis[0].vote_keypair;
let leader_pubkey = leader_keypair.pubkey();
Expand All @@ -292,7 +302,7 @@ impl LocalCluster {
mint_keypair,
..
} = create_genesis_config_with_vote_accounts_and_cluster_type(
config.cluster_lamports,
config.mint_lamports,
&keys_in_genesis,
stakes_in_genesis,
config.cluster_type,
Expand Down Expand Up @@ -519,7 +529,7 @@ impl LocalCluster {
&client,
&self.funding_keypair,
&validator_pubkey,
stake * 2 + 2,
Self::required_validator_funding(stake),
);
info!(
"validator {} balance {}",
Expand Down Expand Up @@ -972,6 +982,10 @@ impl LocalCluster {

Ok(tpu_client)
}

fn required_validator_funding(stake: u64) -> u64 {
stake.saturating_mul(2).saturating_add(2)
}
}

impl Cluster for LocalCluster {
Expand Down
Loading

0 comments on commit 9861e66

Please sign in to comment.