Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Closed
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: 18 additions & 2 deletions genesis/src/genesis_accounts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
stakes::{create_and_add_stakes, StakerInfo},
unlocks::UnlockInfo,
validators::{create_and_add_validator, ValidatorInfo},
};
use solana_sdk::{genesis_config::GenesisConfig, native_token::sol_to_lamports};

Expand Down Expand Up @@ -614,7 +615,22 @@ fn add_stakes(
.sum::<u64>()
}

pub(crate) fn add_genesis_accounts(genesis_config: &mut GenesisConfig) -> u64 {
pub const VALIDATOR_INFOS: &[ValidatorInfo] = &[ValidatorInfo {
name: "aurel@ethereum.ro",
node: "GeZ5PrJi9muVCJiJAaFBNGoCEdxGEqTp7L2BmT2WTTy1",
vote: "7ZdRx2EBYoRuPfyeoNbuHodMUXcAQRcC37MUw3kP6akn",
node_sol: 500.0,
commission: 0,
}];

fn add_validators(genesis_config: &mut GenesisConfig, validator_infos: &[ValidatorInfo]) -> u64 {
validator_infos
.iter()
.map(|validator_info| create_and_add_validator(genesis_config, validator_info))
.sum::<u64>()
}

pub fn add_genesis_accounts(genesis_config: &mut GenesisConfig) -> u64 {
add_stakes(
genesis_config,
&BATCH_ONE_STAKER_INFOS,
Expand All @@ -630,7 +646,7 @@ pub(crate) fn add_genesis_accounts(genesis_config: &mut GenesisConfig) -> u64 {
&BATCH_THREE_STAKER_INFOS,
&BATCH_THREE_UNLOCK_INFO,
sol_to_lamports(1_000_000.0),
)
) + add_validators(genesis_config, &VALIDATOR_INFOS)
}

#[cfg(test)]
Expand Down
6 changes: 6 additions & 0 deletions genesis/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
pub mod address_generator;
pub mod genesis_accounts;
pub mod stakes;
pub mod unlocks;
pub mod validators;

use serde::{Deserialize, Serialize};

/// An account where the data is encoded as a Base64 string.
Expand Down
1 change: 1 addition & 0 deletions genesis/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod address_generator;
mod genesis_accounts;
mod stakes;
mod unlocks;
mod validators;

use crate::genesis_accounts::add_genesis_accounts;
use clap::{crate_description, crate_name, value_t, value_t_or_exit, App, Arg, ArgMatches};
Expand Down
70 changes: 50 additions & 20 deletions genesis/src/stakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use solana_sdk::{
pubkey::Pubkey, system_program, timing::years_as_slots,
};
use solana_stake_program::stake_state::{
create_lockup_stake_account, get_stake_rent_exempt_reserve, Authorized, Lockup,
create_lockup_stake_account, Authorized, Lockup, StakeState,
};

#[derive(Debug)]
Expand Down Expand Up @@ -53,19 +53,23 @@ pub fn create_and_add_stakes(

let total_lamports = sol_to_lamports(staker_info.sol);

let staker_lamports = calculate_staker_lamports(genesis_config);
let staker_account = (
authorized.staker,
Account::new(staker_lamports, 0, &system_program::id()),
);

let stakes_lamports = if !genesis_config.accounts.contains(&staker_account) {
genesis_config.accounts.push(staker_account);
// staker is a system account
let staker_rent_reserve = genesis_config.rent.minimum_balance(0).max(1);
let staker_fees = calculate_staker_fees(genesis_config, 1.0);

total_lamports - staker_lamports
} else {
total_lamports
};
// lamports required to run staking operations for one year
// the staker account needs to be rent exempt *and* carry enough
// lamports to cover TX fees (delegation) for one year,
// and we support one delegation per epoch
// a single staker may administer any number of accounts
genesis_config
.accounts
.entry(authorized.staker)
.or_insert_with(|| {
stakes_lamports -= staker_rent_reserve;
Account::new(staker_rent_reserve, 0, &system_program::id())
})
.lamports += staker_fees;

// the staker account needs to be rent exempt *and* carry enough
// lamports to cover TX fees (delegation) for one year
Expand All @@ -82,7 +86,7 @@ pub fn create_and_add_stakes(

let mut address_generator = AddressGenerator::new(&authorized.staker, staker_info.name);

let stake_rent_exempt_reserve = get_stake_rent_exempt_reserve(&genesis_config.rent);
let stake_rent_reserve = StakeState::get_rent_exempt_reserve(&genesis_config.rent);

for unlock in unlocks {
let lamports = unlock.amount(stakes_lamports);
Expand All @@ -108,7 +112,7 @@ pub fn create_and_add_stakes(
),
);
}
if remainder <= stake_rent_exempt_reserve {
if remainder <= stake_rent_reserve {
genesis_config.add_account(
address_generator.next(),
create_lockup_stake_account(
Expand Down Expand Up @@ -169,7 +173,7 @@ mod tests {
.iter()
.all(|(_pubkey, account)| account.lamports <= granularity
|| account.lamports - granularity
< get_stake_rent_exempt_reserve(&genesis_config.rent)));
<= StakeState::get_rent_exempt_reserve(&genesis_config.rent)));
}

#[test]
Expand All @@ -182,11 +186,12 @@ mod tests {
..Rent::default()
};

let reserve = get_stake_rent_exempt_reserve(&rent);
let reserve = StakeState::get_rent_exempt_reserve(&rent);
let staker_reserve = rent.minimum_balance(0);

// verify that a small remainder ends up in the last stake
let granularity = reserve;
let total_lamports = reserve + reserve * 2 + 1;
let total_lamports = staker_reserve + reserve * 2 + 1;
create_and_check_stakes(
&mut GenesisConfig {
rent,
Expand All @@ -212,7 +217,7 @@ mod tests {

// huge granularity doesn't blow up
let granularity = std::u64::MAX;
let total_lamports = reserve + reserve * 2 + 1;
let total_lamports = staker_reserve + reserve * 2 + 1;
create_and_check_stakes(
&mut GenesisConfig {
rent,
Expand All @@ -238,7 +243,32 @@ mod tests {

// exactly reserve as a remainder
let granularity = reserve * 3;
let total_lamports = reserve + (granularity + reserve) * 2;
let total_lamports = staker_reserve + (granularity + reserve) * 2;
create_and_check_stakes(
&mut GenesisConfig {
rent,
..GenesisConfig::default()
},
&StakerInfo {
name: "fun",
staker: "cafebabedeadbeef000000000000000000000000000000000000000000000000",
withdrawer: "cafebabedeadbeef000000000000000000000000000000000000000000000000",
sol: lamports_to_sol(total_lamports),
custodian: "0000000000000000000000000000000000000000000000000000000000000000",
},
&UnlockInfo {
cliff_fraction: 0.5,
cliff_years: 0.5,
unlocks: 1,
unlock_years: 0.5,
},
total_lamports,
granularity,
2 + 1,
);
// exactly reserve + 1 as a remainder, reserve + 1 gets its own stake
let granularity = reserve * 3;
let total_lamports = staker_reserve + (granularity + reserve + 1) * 2;
create_and_check_stakes(
&mut GenesisConfig {
rent,
Expand Down
209 changes: 209 additions & 0 deletions genesis/src/validators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
//! validators generator
use solana_sdk::{
account::Account, genesis_config::GenesisConfig, native_token::sol_to_lamports, pubkey::Pubkey,
system_program, timing::years_as_slots,
};
use solana_vote_program::vote_state::{self, VoteState};

#[derive(Debug)]
pub struct ValidatorInfo {
pub name: &'static str,
pub node: &'static str,
pub node_sol: f64,
pub vote: &'static str,
pub commission: u8,
}

// the node's account needs carry enough
// lamports to cover TX fees for voting for one year,
// validators can vote once per slot
fn calculate_voting_fees(genesis_config: &GenesisConfig, years: f64) -> u64 {
genesis_config.fee_calculator.max_lamports_per_signature
* years_as_slots(
years,
&genesis_config.poh_config.target_tick_duration,
genesis_config.ticks_per_slot,
) as u64
}

/// create and add vote and node id accounts for a validator
pub fn create_and_add_validator(
genesis_config: &mut GenesisConfig,
// information about this validator
validator_info: &ValidatorInfo,
) -> u64 {
let node: Pubkey = validator_info.node.parse().expect("invalide node");
let vote: Pubkey = validator_info.vote.parse().expect("invalide vote");
let node_lamports = sol_to_lamports(validator_info.node_sol);

// node is the system account from which votes will be issued
let node_rent_reserve = genesis_config.rent.minimum_balance(0).max(1);
let node_voting_fees = calculate_voting_fees(genesis_config, 1.0);

let vote_rent_reserve = VoteState::get_rent_exempt_reserve(&genesis_config.rent).max(1);

let mut total_lamports = node_voting_fees + vote_rent_reserve + node_lamports;

genesis_config
.accounts
.entry(node)
.or_insert_with(|| {
total_lamports += node_rent_reserve;
Account::new(node_rent_reserve, 0, &system_program::id())
})
.lamports += node_voting_fees + node_lamports;

assert!(
genesis_config.accounts.get(&vote).is_none(),
"{} is already in genesis",
vote
);

genesis_config.add_account(
vote,
vote_state::create_account(&vote, &node, validator_info.commission, vote_rent_reserve),
);

total_lamports
}

#[cfg(test)]
mod tests {
use super::*;
use solana_sdk::rent::Rent;

fn create_and_check_validators(
genesis_config: &mut GenesisConfig,
validator_infos: &[ValidatorInfo],
total_lamports: u64,
len: usize,
) {
assert_eq!(
validator_infos
.iter()
.map(|validator_info| create_and_add_validator(genesis_config, validator_info))
.sum::<u64>(),
total_lamports
);
assert_eq!(genesis_config.accounts.len(), len);
assert_eq!(
genesis_config
.accounts
.iter()
.map(|(_pubkey, account)| account.lamports)
.sum::<u64>(),
total_lamports,
);
assert!(genesis_config
.accounts
.iter()
.all(|(_pubkey, account)| account.lamports
>= genesis_config.rent.minimum_balance(0).max(1)));
}

#[test]
fn test_create_one_validator() {
let rent = Rent {
lamports_per_byte_year: 1,
exemption_threshold: 1.0,
..Rent::default()
};
let mut genesis_config = GenesisConfig {
rent,
..GenesisConfig::default()
};

let total_lamports = VoteState::get_rent_exempt_reserve(&rent)
+ calculate_voting_fees(&genesis_config, 1.0)
+ rent.minimum_balance(0);

create_and_check_validators(
&mut genesis_config,
&[ValidatorInfo {
name: "fun",
node: "AiTDdNHW2vNtHt7PqWMHx3B8cMPRDNgc7kMiLPJM25QC", // random pubkeys
node_sol: 0.0,
vote: "77TQYZTHodhnxJcSuVjUvx8GYRCkykPyHtmFTFLjj1Rc",
commission: 50,
}],
total_lamports,
2,
);
}

#[test]
fn test_create_one_validator_two_votes() {
let rent = Rent {
lamports_per_byte_year: 1,
exemption_threshold: 1.0,
..Rent::default()
};
let mut genesis_config = GenesisConfig {
rent,
..GenesisConfig::default()
};
let total_lamports = VoteState::get_rent_exempt_reserve(&rent) * 2
+ calculate_voting_fees(&genesis_config, 1.0) * 2 // two vote accounts
+ rent.minimum_balance(0) // one node account
+ sol_to_lamports(1.0); // 2nd vote account ask has SOL

// weird case, just wanted to verify that the duplicated node account gets double fees
create_and_check_validators(
&mut genesis_config,
&[
ValidatorInfo {
name: "fun",
node: "3VTm54dw8w6jTTsPH4BfoV5vo6mF985JAMtNDRYcaGFc", // random pubkeys
node_sol: 0.0,
vote: "GTKWbUoLw3Bv7Ld92crhyXcEk9zUu3VEKfzeuWJZdnfW",
commission: 50,
},
ValidatorInfo {
name: "unfun",
node: "3VTm54dw8w6jTTsPH4BfoV5vo6mF985JAMtNDRYcaGFc", // random pubkeys, same node
node_sol: 1.0,
vote: "8XrFPRULg98kSm535kFaLV4GMnK5JQSuAymyrCHXsUcy",
commission: 50,
},
],
total_lamports,
3,
);
}

#[test]
#[should_panic]
fn test_vote_collision() {
let rent = Rent {
lamports_per_byte_year: 1,
exemption_threshold: 1.0,
..Rent::default()
};
let mut genesis_config = GenesisConfig {
rent,
..GenesisConfig::default()
};

create_and_check_validators(
&mut genesis_config,
&[
ValidatorInfo {
name: "fun",
node: "3VTm54dw8w6jTTsPH4BfoV5vo6mF985JAMtNDRYcaGFc", // random pubkeys
node_sol: 0.0,
vote: "GTKWbUoLw3Bv7Ld92crhyXcEk9zUu3VEKfzeuWJZdnfW",
commission: 50,
},
ValidatorInfo {
name: "unfun",
node: "3VTm54dw8w6jTTsPH4BfoV5vo6mF985JAMtNDRYcaGFc", // random pubkeys, same node
node_sol: 0.0,
vote: "GTKWbUoLw3Bv7Ld92crhyXcEk9zUu3VEKfzeuWJZdnfW", // duplicate vote, bad juju
commission: 50,
},
],
0,
0,
);
}
}
Loading