track market cap (#4643)

* track market cap

* fixup, rebase

* prettier
This commit is contained in:
Rob Walker
2019-06-11 17:04:13 -07:00
committed by GitHub
parent 697228a484
commit 575a897ffc
3 changed files with 43 additions and 9 deletions

View File

@ -212,6 +212,11 @@ pub struct Bank {
#[serde(deserialize_with = "deserialize_atomicusize")]
signature_count: AtomicUsize,
/// Total capitalization, used to calculate inflation
#[serde(serialize_with = "serialize_atomicusize")]
#[serde(deserialize_with = "deserialize_atomicusize")]
capitalization: AtomicUsize, // TODO: Use AtomicU64 if/when available
// Bank max_tick_height
max_tick_height: u64,
@ -478,6 +483,8 @@ impl Bank {
for (pubkey, account) in genesis_block.accounts.iter() {
self.store(pubkey, account);
self.capitalization
.fetch_add(account.lamports as usize, Ordering::Relaxed);
}
// highest staked node is the first collector
self.collector_id = self
@ -1154,6 +1161,14 @@ impl Bank {
self.tick_height.load(Ordering::Relaxed) as u64
}
/// Return the total capititalization of the Bank
pub fn capitalization(&self) -> u64 {
// capitalization is using an AtomicUSize because AtomicU64 is not yet a stable API.
// Until we can switch to AtomicU64, fail if usize is not the same as u64
assert_eq!(std::usize::MAX, 0xFFFF_FFFF_FFFF_FFFF);
self.capitalization.load(Ordering::Relaxed) as u64
}
/// Return this bank's max_tick_height
pub fn max_tick_height(&self) -> u64 {
self.max_tick_height
@ -1334,6 +1349,15 @@ mod tests {
);
}
#[test]
fn test_bank_capitalization() {
let bank = Bank::new(&GenesisBlock {
accounts: vec![(Pubkey::default(), Account::new(42, 0, &Pubkey::default()),); 42],
..GenesisBlock::default()
});
assert_eq!(bank.capitalization(), 42 * 42);
}
#[test]
fn test_two_payments_to_one_party() {
let (genesis_block, mint_keypair) = create_genesis_block(10_000);

View File

@ -24,7 +24,7 @@ pub fn create_genesis_block_with_leader(
bootstrap_leader_pubkey: &Pubkey,
bootstrap_leader_stake_lamports: u64,
) -> GenesisBlockInfo {
let bootstrap_leader_lamports = 42; // TODO: pass this in as an argument?
let bootstrap_leader_lamports = BOOTSTRAP_LEADER_LAMPORTS; // TODO: pass this in as an argument?
let mint_keypair = Keypair::new();
let voting_keypair = Keypair::new();
let staking_keypair = Keypair::new();