Skip to content

Commit

Permalink
Keep track of chain PoS power
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed May 23, 2023
1 parent cc3a592 commit 358cba4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/blockchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub trait Blockchain<K: KvStore> {
fn database(&self) -> &K;
fn database_mut(&mut self) -> &mut K;
fn epoch_slot(&self, timestamp: u32) -> (u32, u32);
fn get_power(&self) -> Result<f64, BlockchainError>;
fn get_mpn_account_count(&self) -> Result<u64, BlockchainError>;
fn get_mpn_account_indices(&self, addr: MpnAddress) -> Result<Vec<u64>, BlockchainError>;
fn get_stake(&self, addr: Address) -> Result<Amount, BlockchainError>;
Expand Down Expand Up @@ -609,6 +610,13 @@ impl<K: KvStore> Blockchain<K> for KvStoreChain<K> {
Ok(None)
}

fn get_power(&self) -> Result<f64, BlockchainError> {
Ok(match self.database.get(keys::power())? {
Some(b) => b.try_into()?,
None => 0.into(),
})
}

fn get_stake(&self, addr: Address) -> Result<Amount, BlockchainError> {
Ok(match self.database.get(keys::stake(&addr))? {
Some(b) => b.try_into()?,
Expand Down
3 changes: 3 additions & 0 deletions src/blockchain/ops/apply_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub fn apply_block<K: KvStore>(
) -> Result<(), BlockchainError> {
let (ops, _) = chain.isolated(|chain| {
let curr_height = chain.get_height()?;
let mut curr_pow = chain.get_power()?;

if let Some(height_limit) = chain.config.testnet_height_limit {
if block.header.number >= height_limit {
Expand All @@ -26,6 +27,7 @@ pub fn apply_block<K: KvStore>(
if !is_genesis {
if chain.config.check_validator {
if let Some(proof) = block.header.proof_of_stake.proof.clone() {
curr_pow += 1f64 / ((proof.attempt + 1) as f64);
if !chain.is_validator(
block.header.proof_of_stake.timestamp,
block.header.proof_of_stake.validator.clone(),
Expand Down Expand Up @@ -134,6 +136,7 @@ pub fn apply_block<K: KvStore>(
}

chain.database.update(&[
WriteOp::Put(keys::power(), curr_pow.into()),
WriteOp::Put(keys::height(), (curr_height + 1).into()),
WriteOp::Put(
keys::header(block.header.number),
Expand Down
4 changes: 4 additions & 0 deletions src/db/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub fn randomness() -> StringKey {
"RND".into()
}

pub fn power() -> StringKey {
"PWR".into()
}

pub fn block(index: u64) -> StringKey {
format!("BLK-{:010}", index).into()
}
Expand Down
2 changes: 2 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ gen_try_into!(
u32,
u64,
u128,
f64,
usize,
<Hasher as Hash>::Output,
Ratio,
Expand Down Expand Up @@ -122,6 +123,7 @@ gen_from!(
u32,
u64,
u128,
f64,
usize,
<Hasher as Hash>::Output,
Ratio,
Expand Down

0 comments on commit 358cba4

Please sign in to comment.