Skip to content

Commit

Permalink
Improve final report of ledger-tool capitalization (#13232)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoqun authored Oct 28, 2020
1 parent ae91270 commit 4698ee5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
50 changes: 41 additions & 9 deletions ledger-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2022,33 +2022,65 @@ fn main() {
println!("Epoch: {} => {}", base_bank.epoch(), warped_bank.epoch());
assert_capitalization(&base_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!(
"Capitalization: {} => {} (+{} {}%)",
"Capitalization: {} => {} (+{} {}%; annualized {}%)",
Sol(base_bank.capitalization()),
Sol(warped_bank.capitalization()),
Sol(warped_bank.capitalization() - base_bank.capitalization()),
((warped_bank.capitalization() as f64)
/ (base_bank.capitalization() as f64)
* 100_f64),
interest_per_epoch,
interest_per_year,
);

let mut overall_delta = 0;
for (pubkey, warped_account) in
warped_bank.get_all_accounts_modified_since_parent()
{
let modified_accounts =
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 base_account.lamports != warped_account.lamports {
let delta = warped_account.lamports - base_account.lamports;
println!(
"{}({}): {} => {} (+{})",
pubkey,
"{:<45}({}): {} => {} (+{} {:>4.9}%)",
format!("{}", pubkey), // format! is needed to pad/justify correctly.
base_account.owner,
Sol(base_account.lamports),
Sol(warped_account.lamports),
Sol(delta),
((warped_account.lamports as f64)
/ (base_account.lamports as f64)
* 100_f64)
- 100_f64,
);
overall_delta += delta;
}
} else {
error!("new account!?: {}", pubkey);
}
}
if overall_delta > 0 {
Expand Down
13 changes: 8 additions & 5 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
fn update_rewards(&mut self, prev_epoch: Epoch) {
if prev_epoch == self.epoch() {
Expand All @@ -1228,11 +1235,7 @@ impl Bank {
let slot_in_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
// 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 epoch_duration_in_years = self.epoch_duration_in_years(prev_epoch);

let (validator_rate, foundation_rate) = {
let inflation = self.inflation.read().unwrap();
Expand Down

0 comments on commit 4698ee5

Please sign in to comment.