Improve final report of ledger-tool capitalization (#13232)

This commit is contained in:
Ryo Onodera 2020-10-29 02:10:09 +09:00 committed by GitHub
parent ae91270961
commit 4698ee5e4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 14 deletions

View File

@ -2022,33 +2022,65 @@ fn main() {
println!("Epoch: {} => {}", base_bank.epoch(), warped_bank.epoch()); println!("Epoch: {} => {}", base_bank.epoch(), warped_bank.epoch());
assert_capitalization(&base_bank); assert_capitalization(&base_bank);
assert_capitalization(&warped_bank); assert_capitalization(&warped_bank);
let interest_per_epoch = ((warped_bank.capitalization() as f64)
/ (base_bank.capitalization() as f64)
* 100_f64)
- 100_f64;
let interest_per_year = interest_per_epoch
/ warped_bank.epoch_duration_in_years(base_bank.epoch());
println!( println!(
"Capitalization: {} => {} (+{} {}%)", "Capitalization: {} => {} (+{} {}%; annualized {}%)",
Sol(base_bank.capitalization()), Sol(base_bank.capitalization()),
Sol(warped_bank.capitalization()), Sol(warped_bank.capitalization()),
Sol(warped_bank.capitalization() - base_bank.capitalization()), Sol(warped_bank.capitalization() - base_bank.capitalization()),
((warped_bank.capitalization() as f64) interest_per_epoch,
/ (base_bank.capitalization() as f64) interest_per_year,
* 100_f64),
); );
let mut overall_delta = 0; let mut overall_delta = 0;
for (pubkey, warped_account) in let modified_accounts =
warped_bank.get_all_accounts_modified_since_parent() warped_bank.get_all_accounts_modified_since_parent();
{ let mut sorted_accounts = modified_accounts
.iter()
.map(|(pubkey, account)| {
(
pubkey,
account,
base_bank
.get_account(&pubkey)
.map(|a| a.lamports)
.unwrap_or_default(),
)
})
.collect::<Vec<_>>();
sorted_accounts.sort_unstable_by_key(|(pubkey, account, base_lamports)| {
(
account.owner,
*base_lamports,
account.lamports - base_lamports,
*pubkey,
)
});
for (pubkey, warped_account, _) in sorted_accounts {
if let Some(base_account) = base_bank.get_account(&pubkey) { if let Some(base_account) = base_bank.get_account(&pubkey) {
if base_account.lamports != warped_account.lamports { if base_account.lamports != warped_account.lamports {
let delta = warped_account.lamports - base_account.lamports; let delta = warped_account.lamports - base_account.lamports;
println!( println!(
"{}({}): {} => {} (+{})", "{:<45}({}): {} => {} (+{} {:>4.9}%)",
pubkey, format!("{}", pubkey), // format! is needed to pad/justify correctly.
base_account.owner, base_account.owner,
Sol(base_account.lamports), Sol(base_account.lamports),
Sol(warped_account.lamports), Sol(warped_account.lamports),
Sol(delta), Sol(delta),
((warped_account.lamports as f64)
/ (base_account.lamports as f64)
* 100_f64)
- 100_f64,
); );
overall_delta += delta; overall_delta += delta;
} }
} else {
error!("new account!?: {}", pubkey);
} }
} }
if overall_delta > 0 { if overall_delta > 0 {

View File

@ -1217,6 +1217,13 @@ impl Bank {
}); });
} }
pub fn epoch_duration_in_years(&self, prev_epoch: Epoch) -> f64 {
// period: time that has passed as a fraction of a year, basically the length of
// an epoch as a fraction of a year
// calculated as: slots_elapsed / (slots / year)
self.epoch_schedule.get_slots_in_epoch(prev_epoch) as f64 / self.slots_per_year
}
// update rewards based on the previous epoch // update rewards based on the previous epoch
fn update_rewards(&mut self, prev_epoch: Epoch) { fn update_rewards(&mut self, prev_epoch: Epoch) {
if prev_epoch == self.epoch() { if prev_epoch == self.epoch() {
@ -1228,11 +1235,7 @@ impl Bank {
let slot_in_year = let slot_in_year =
(self.epoch_schedule.get_last_slot_in_epoch(prev_epoch)) as f64 / self.slots_per_year; (self.epoch_schedule.get_last_slot_in_epoch(prev_epoch)) as f64 / self.slots_per_year;
// period: time that has passed as a fraction of a year, basically the length of let epoch_duration_in_years = self.epoch_duration_in_years(prev_epoch);
// an epoch as a fraction of a year
// calculated as: slots_elapsed / (slots / year)
let epoch_duration_in_years =
self.epoch_schedule.get_slots_in_epoch(prev_epoch) as f64 / self.slots_per_year;
let (validator_rate, foundation_rate) = { let (validator_rate, foundation_rate) = {
let inflation = self.inflation.read().unwrap(); let inflation = self.inflation.read().unwrap();