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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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();

View File

@ -15,12 +15,12 @@ use std::path::Path;
#[derive(Serialize, Deserialize, Debug)]
pub struct GenesisBlock {
pub accounts: Vec<(Pubkey, Account)>,
pub epoch_warmup: bool,
pub fee_calculator: FeeCalculator,
pub native_instruction_processors: Vec<(String, Pubkey)>,
pub ticks_per_slot: u64,
pub slots_per_epoch: u64,
pub stakers_slot_offset: u64,
pub ticks_per_slot: u64,
pub epoch_warmup: bool,
pub poh_config: PohConfig,
}
@ -39,6 +39,21 @@ pub fn create_genesis_block(lamports: u64) -> (GenesisBlock, Keypair) {
)
}
impl Default for GenesisBlock {
fn default() -> Self {
Self {
accounts: Vec::new(),
epoch_warmup: true,
fee_calculator: FeeCalculator::default(),
native_instruction_processors: Vec::new(),
slots_per_epoch: DEFAULT_SLOTS_PER_EPOCH,
stakers_slot_offset: DEFAULT_SLOTS_PER_EPOCH,
ticks_per_slot: DEFAULT_TICKS_PER_SLOT,
poh_config: PohConfig::default(),
}
}
}
impl GenesisBlock {
pub fn new(
accounts: &[(Pubkey, Account)],
@ -46,13 +61,8 @@ impl GenesisBlock {
) -> Self {
Self {
accounts: accounts.to_vec(),
epoch_warmup: true,
fee_calculator: FeeCalculator::default(),
native_instruction_processors: native_instruction_processors.to_vec(),
slots_per_epoch: DEFAULT_SLOTS_PER_EPOCH,
stakers_slot_offset: DEFAULT_SLOTS_PER_EPOCH,
ticks_per_slot: DEFAULT_TICKS_PER_SLOT,
poh_config: PohConfig::default(),
..GenesisBlock::default()
}
}