Skip to content

Commit

Permalink
feat(staking): discard nanoWits for power computation
Browse files Browse the repository at this point in the history
This aims to reduce the chance for overflows
  • Loading branch information
aesedepece committed Jul 2, 2024
1 parent c1b696a commit 3b0dd00
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 70 deletions.
6 changes: 5 additions & 1 deletion data_structures/src/staking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use serde::{
Deserialize, Deserializer, Serialize, Serializer,
};

use crate::{chain::PublicKeyHash, proto::ProtobufConvert, staking::prelude::*};
use crate::{
chain::PublicKeyHash, proto::ProtobufConvert, staking::prelude::*, wit::PrecisionLoss,
};

/// Just a type alias for consistency of using the same data type to represent power.
pub type Power = u64;
Expand Down Expand Up @@ -227,6 +229,7 @@ where
+ Mul<Output = Coins>
+ Mul<Epoch, Output = Power>
+ Ord
+ PrecisionLoss
+ Send
+ Sub<Output = Coins>
+ Sum
Expand Down Expand Up @@ -274,6 +277,7 @@ where
+ Mul<Output = Coins>
+ Mul<Epoch, Output = Power>
+ Ord
+ PrecisionLoss
+ Send
+ Sub<Output = Coins>
+ Sum
Expand Down
10 changes: 7 additions & 3 deletions data_structures/src/staking/stake.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::fmt::{Debug, Display};
use std::{marker::PhantomData, ops::*};

use serde::{Deserialize, Serialize};

use crate::wit::{PrecisionLoss, WIT_DECIMAL_PLACES};

use super::prelude::*;
use std::fmt::{Debug, Display};

/// A data structure that keeps track of a staker's staked coins and the epochs for different
/// capabilities.
Expand Down Expand Up @@ -36,7 +38,8 @@ where
+ Debug
+ Display
+ Send
+ Sync,
+ Sync
+ PrecisionLoss,
Epoch: Copy
+ Default
+ num_traits::Saturating
Expand Down Expand Up @@ -106,7 +109,8 @@ where
/// Derives the power of an identity in the network on a certain epoch from an entry. Most
/// normally, the epoch is the current epoch.
pub fn power(&self, capability: Capability, current_epoch: Epoch) -> Power {
self.coins * (current_epoch.saturating_sub(self.epochs.get(capability)))
self.coins.lose_precision(WIT_DECIMAL_PLACES)
* (current_epoch.saturating_sub(self.epochs.get(capability)))
}

/// Remove a certain amount of staked coins.
Expand Down
15 changes: 11 additions & 4 deletions data_structures/src/staking/stakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
chain::PublicKeyHash,
get_environment,
transaction::{StakeTransaction, UnstakeTransaction},
wit::Wit,
wit::{PrecisionLoss, Wit},
};

use super::prelude::*;
Expand Down Expand Up @@ -106,7 +106,8 @@ where
+ Send
+ Sync
+ Display
+ Sum,
+ Sum
+ PrecisionLoss,
Epoch: Copy
+ Default
+ Saturating
Expand Down Expand Up @@ -313,11 +314,17 @@ where
}
}

/// Creates an instance of `Stakes` that is initialized with a existing set of stake entries.
///
/// This is specially convenient after loading stakes from storage, as this function rebuilds
/// all the indexes at once to preserve write locks and reference counts.
pub fn with_entries(
entries: BTreeMap<StakeKey<Address>, SyncStake<Address, Coins, Epoch, Power>>,
) -> Self {
let mut stakes = Stakes::default();
stakes.by_key = entries;
let mut stakes = Stakes {
by_key: entries,
..Default::default()
};
stakes.reindex();

stakes
Expand Down
16 changes: 16 additions & 0 deletions data_structures/src/wit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ impl Sum for Wit {
}
}

pub trait PrecisionLoss: Copy {
fn lose_precision(self, digits: u8) -> Self;
}

impl PrecisionLoss for u64 {
fn lose_precision(self, digits: u8) -> u64 {
self / 10_u64.pow(digits as u32)

Check failure on line 160 in data_structures/src/wit.rs

View workflow job for this annotation

GitHub Actions / clippy

casting `u8` to `u32` may become silently lossy if you later change the type

error: casting `u8` to `u32` may become silently lossy if you later change the type --> data_structures/src/wit.rs:160:27 | 160 | self / 10_u64.pow(digits as u32) | ^^^^^^^^^^^^^ help: try: `u32::from(digits)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless = note: requested on the command line with `-D clippy::cast-lossless`

Check failure on line 160 in data_structures/src/wit.rs

View workflow job for this annotation

GitHub Actions / clippy

casting `u8` to `u32` may become silently lossy if you later change the type

error: casting `u8` to `u32` may become silently lossy if you later change the type --> data_structures/src/wit.rs:160:27 | 160 | self / 10_u64.pow(digits as u32) | ^^^^^^^^^^^^^ help: try: `u32::from(digits)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless = note: requested on the command line with `-D clippy::cast-lossless`
}
}

impl PrecisionLoss for Wit {
fn lose_precision(self, digits: u8) -> Wit {
Wit(self.0.lose_precision(digits))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion node/src/actors/chain_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3568,7 +3568,7 @@ mod tests {
dr_pool.process_commit(&co_tx, &Hash::default()).unwrap();
dr_pool.process_commit(&co_tx2, &Hash::default()).unwrap();
dr_pool.process_commit(&co_tx3, &Hash::default()).unwrap();
dr_pool.update_data_request_stages(None);
dr_pool.update_data_request_stages(None, None);
dr_pool.process_reveal(&re_tx1, &Hash::default()).unwrap();
dr_pool.process_reveal(&re_tx2, &Hash::default()).unwrap();

Expand Down
4 changes: 3 additions & 1 deletion validations/src/eligibility/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::{
iter::Sum,
ops::{Add, Div, Mul, Sub},
};
use witnet_data_structures::staking::prelude::*;

use witnet_data_structures::{staking::prelude::*, wit::PrecisionLoss};

const MINING_REPLICATION_FACTOR: usize = 4;
const WITNESSING_MAX_ROUNDS: usize = 4;
Expand Down Expand Up @@ -109,6 +110,7 @@ where
+ Sub<Output = Coins>
+ Mul
+ Mul<Epoch, Output = Power>
+ PrecisionLoss
+ Sync
+ Send
+ Sum
Expand Down
Loading

0 comments on commit 3b0dd00

Please sign in to comment.