Skip to content

Commit

Permalink
run rent exempt check on validator_accounts_file entries
Browse files Browse the repository at this point in the history
  • Loading branch information
gregcusack committed Oct 28, 2024
1 parent cae626f commit 84798e7
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions genesis/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn load_validator_accounts(
commission,
rent,
None,
);
)?;
}

Ok(())
Expand Down Expand Up @@ -243,7 +243,12 @@ fn add_validator_accounts(
commission: u8,
rent: &Rent,
authorized_pubkey: Option<&Pubkey>,
) {
) -> io::Result<()> {
rent_exempt_check(
stake_lamports,
rent.minimum_balance(StakeStateV2::size_of()),
)?;

loop {
let Some(identity_pubkey) = pubkeys_iter.next() else {
break;
Expand Down Expand Up @@ -276,6 +281,20 @@ fn add_validator_accounts(
);
genesis_config.add_account(*vote_pubkey, vote_account);
}
Ok(())
}

fn rent_exempt_check(stake_lamports: u64, exempt: u64) -> io::Result<()> {
if stake_lamports < exempt {
Err(io::Error::new(
io::ErrorKind::Other,
format!(
"error: insufficient validator stake lamports: {stake_lamports} for rent exemption, requires {exempt}"
),
))
} else {
Ok(())
}
}

#[allow(clippy::cognitive_complexity)]
Expand Down Expand Up @@ -611,21 +630,6 @@ fn main() -> Result<(), Box<dyn error::Error>> {
burn_percent: value_t_or_exit!(matches, "rent_burn_percentage", u8),
};

fn rent_exempt_check(matches: &ArgMatches<'_>, name: &str, exempt: u64) -> io::Result<u64> {
let lamports = value_t_or_exit!(matches, name, u64);

if lamports < exempt {
Err(io::Error::new(
io::ErrorKind::Other,
format!(
"error: insufficient {name}: {lamports} for rent exemption, requires {exempt}"
),
))
} else {
Ok(lamports)
}
}

let bootstrap_validator_pubkeys = pubkeys_of(&matches, "bootstrap_validator").unwrap();
assert_eq!(bootstrap_validator_pubkeys.len() % 3, 0);

Expand All @@ -643,11 +647,8 @@ fn main() -> Result<(), Box<dyn error::Error>> {
let bootstrap_validator_lamports =
value_t_or_exit!(matches, "bootstrap_validator_lamports", u64);

let bootstrap_validator_stake_lamports = rent_exempt_check(
&matches,
"bootstrap_validator_stake_lamports",
rent.minimum_balance(StakeStateV2::size_of()),
)?;
let bootstrap_validator_stake_lamports =
value_t_or_exit!(matches, "bootstrap_validator_stake_lamports", u64);

let bootstrap_stake_authorized_pubkey =
pubkey_of(&matches, "bootstrap_stake_authorized_pubkey");
Expand Down Expand Up @@ -747,7 +748,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
commission,
&rent,
bootstrap_stake_authorized_pubkey.as_ref(),
);
)?;

if let Some(creation_time) = unix_timestamp_from_rfc3339_datetime(&matches, "creation_time") {
genesis_config.creation_time = creation_time;
Expand Down

0 comments on commit 84798e7

Please sign in to comment.