Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Simple cap by including sysvars and native programs (#13884)
Browse files Browse the repository at this point in the history
* Simpler cap by including sysvars and native programs

* Fix tests

* Add comment

* revert some unrelated code

* Update test_bank_update_sysvar_account for cap.

* Test cap. for add_native_program using new helper

* Improve the cap adjustment with new tests

* Fix typo...

* Adjust test for improved code coverage

* Rename simpler_capitalization => simple_capitalization

* More rename and bonus commenting
  • Loading branch information
ryoqun authored Dec 14, 2020
1 parent 4bcc8af commit de9ac43
Show file tree
Hide file tree
Showing 8 changed files with 439 additions and 63 deletions.
5 changes: 4 additions & 1 deletion accounts-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ fn main() {
} else {
let mut pubkeys: Vec<Pubkey> = vec![];
let mut time = Measure::start("hash");
let hash = accounts.accounts_db.update_accounts_hash(0, &ancestors).0;
let hash = accounts
.accounts_db
.update_accounts_hash(0, &ancestors, true)
.0;
time.stop();
println!("hash: {} {}", hash, time);
create_test_accounts(&accounts, &mut pubkeys, 1, 0);
Expand Down
51 changes: 47 additions & 4 deletions ledger-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,13 @@ fn main() {
.help("Enable stake program v2 (several inflation-related staking \
bugs are feature-gated behind this)"),
)
.arg(
Arg::with_name("enable_simple_capitalization")
.required(false)
.long("enable-simple-capitalization")
.takes_value(false)
.help("Enable simple capitalization to test hardcoded cap adjustments"),
)
.arg(
Arg::with_name("recalculate_capitalization")
.required(false)
Expand Down Expand Up @@ -2115,11 +2122,47 @@ fn main() {
.lazy_rent_collection
.store(true, std::sync::atomic::Ordering::Relaxed);

let feature_account_balance = std::cmp::max(
genesis_config.rent.minimum_balance(Feature::size_of()),
1,
);
if arg_matches.is_present("enable_simple_capitalization") {
if base_bank
.get_account(&feature_set::simple_capitalization::id())
.is_none()
{
base_bank.store_account(
&feature_set::simple_capitalization::id(),
&feature::create_account(
&Feature { activated_at: None },
feature_account_balance,
),
);
if base_bank
.get_account(&feature_set::cumulative_rent_related_fixes::id())
.is_some()
{
// steal some lamports from the pretty old feature not to affect
// capitalizaion, which doesn't affect inflation behavior!
base_bank.store_account(
&feature_set::cumulative_rent_related_fixes::id(),
&Account::default(),
);
} else {
let old_cap = base_bank.set_capitalization();
let new_cap = base_bank.capitalization();
warn!(
"Skewing capitalization a bit to enable simple capitalization as \
requested: increasing {} from {} to {}",
feature_account_balance, old_cap, new_cap,
);
assert_eq!(old_cap + feature_account_balance, new_cap);
}
} else {
warn!("Already simple_capitalization is activated (or scheduled)");
}
}
if arg_matches.is_present("enable_stake_program_v2") {
let feature_account_balance = std::cmp::max(
genesis_config.rent.minimum_balance(Feature::size_of()),
1,
);
let mut force_enabled_count = 0;
if base_bank
.get_account(&feature_set::stake_program_v2::id())
Expand Down
12 changes: 9 additions & 3 deletions runtime/benches/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ fn test_accounts_hash_bank_hash(bencher: &mut Bencher) {
let slot = 0;
create_test_accounts(&accounts, &mut pubkeys, num_accounts, slot);
let ancestors = vec![(0, 0)].into_iter().collect();
let (_, total_lamports) = accounts.accounts_db.update_accounts_hash(0, &ancestors);
bencher.iter(|| assert!(accounts.verify_bank_hash_and_lamports(0, &ancestors, total_lamports)));
let (_, total_lamports) = accounts
.accounts_db
.update_accounts_hash(0, &ancestors, true);
bencher.iter(|| {
assert!(accounts.verify_bank_hash_and_lamports(0, &ancestors, total_lamports, true))
});
}

#[bench]
Expand All @@ -109,7 +113,9 @@ fn test_update_accounts_hash(bencher: &mut Bencher) {
create_test_accounts(&accounts, &mut pubkeys, 50_000, 0);
let ancestors = vec![(0, 0)].into_iter().collect();
bencher.iter(|| {
accounts.accounts_db.update_accounts_hash(0, &ancestors);
accounts
.accounts_db
.update_accounts_hash(0, &ancestors, true);
});
}

Expand Down
18 changes: 13 additions & 5 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,11 @@ impl Accounts {
accounts_balances
}

pub fn calculate_capitalization(&self, ancestors: &Ancestors) -> u64 {
pub fn calculate_capitalization(
&self,
ancestors: &Ancestors,
simple_capitalization_enabled: bool,
) -> u64 {
let balances =
self.load_all_unchecked(ancestors)
.into_iter()
Expand All @@ -469,6 +473,7 @@ impl Accounts {
account.lamports,
&account.owner,
account.executable,
simple_capitalization_enabled,
)
});

Expand All @@ -481,11 +486,14 @@ impl Accounts {
slot: Slot,
ancestors: &Ancestors,
total_lamports: u64,
simple_capitalization_enabled: bool,
) -> bool {
if let Err(err) =
self.accounts_db
.verify_bank_hash_and_lamports(slot, ancestors, total_lamports)
{
if let Err(err) = self.accounts_db.verify_bank_hash_and_lamports(
slot,
ancestors,
total_lamports,
simple_capitalization_enabled,
) {
warn!("verify_bank_hash failed: {:?}", err);
false
} else {
Expand Down
Loading

0 comments on commit de9ac43

Please sign in to comment.