Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A set of fixes and features for wit\2 #2472

Merged
merged 11 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions data_structures/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ pub enum BlockError {
mint_epoch: Epoch,
block_epoch: Epoch,
},
#[fail(
display = "Mint transaction should be set to default after the activation of Witnet 2.0"
)]
InvalidMintTransaction,
#[fail(display = "The block has an invalid PoE")]
NotValidPoe,
#[fail(
Expand Down
9 changes: 9 additions & 0 deletions data_structures/src/staking/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ where
/// A withdrawer address.
withdrawer: Address,
},
/// Tried to add stake to a validator with a different withdrawer than the one initially set.
#[fail(
display = "Validator {} already has a different withdrawer set",
validator
)]
DifferentWithdrawer {
/// A validator address.
validator: Address,
},
/// Tried to query for a stake entry without providing a validator or a withdrawer address.
#[fail(
display = "Tried to query a stake entry without providing a validator or a withdrawer address"
Expand Down
22 changes: 15 additions & 7 deletions data_structures/src/staking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
fmt::{Debug, Display, Formatter},
iter::Sum,
marker::PhantomData,
ops::{Add, Div, Mul, Sub},
ops::{Add, Div, Mul, Rem, Sub},
rc::Rc,
str::FromStr,
sync::RwLock,
Expand Down Expand Up @@ -219,7 +219,7 @@ where

impl<'de, Address, Coins, Epoch, Power> Deserialize<'de> for Stakes<Address, Coins, Epoch, Power>
where
Address: Clone + Debug + Default + DeserializeOwned + Display + Ord + Send + Sync,
Address: Clone + Debug + Default + DeserializeOwned + Display + Ord + Send + Sync + 'static,
Coins: Copy
+ Debug
+ Default
Expand All @@ -234,7 +234,9 @@ where
+ Sub<Output = Coins>
+ Sum
+ Sync
+ Zero,
+ Zero
+ Div<Output = Coins>
+ Rem<Output = Coins>,
Epoch: Copy
+ Debug
+ Default
Expand All @@ -244,7 +246,9 @@ where
+ Saturating
+ Send
+ Sub<Output = Epoch>
+ Sync,
+ Sync
+ Add<Output = Epoch>
+ Div<Output = Epoch>,
Power:
Add<Output = Power> + Copy + Default + DeserializeOwned + Div<Output = Power> + Ord + Sum,
u64: From<Coins> + From<Power>,
Expand All @@ -267,7 +271,7 @@ struct StakesVisitor<Address, Coins, Epoch, Power> {

impl<'de, Address, Coins, Epoch, Power> Visitor<'de> for StakesVisitor<Address, Coins, Epoch, Power>
where
Address: Clone + Debug + Default + Deserialize<'de> + Display + Ord + Send + Sync,
Address: Clone + Debug + Default + Deserialize<'de> + Display + Ord + Send + Sync + 'static,
Coins: Copy
+ Debug
+ Default
Expand All @@ -282,7 +286,9 @@ where
+ Sub<Output = Coins>
+ Sum
+ Sync
+ Zero,
+ Zero
+ Div<Output = Coins>
+ Rem<Output = Coins>,
Epoch: Copy
+ Debug
+ Default
Expand All @@ -292,7 +298,9 @@ where
+ Send
+ Saturating
+ Sub<Output = Epoch>
+ Sync,
+ Sync
+ Add<Output = Epoch>
+ Div<Output = Epoch>,
Power: Add<Output = Power> + Copy + Default + Div<Output = Power> + Ord + Sum,
u64: From<Coins> + From<Power>,
{
Expand Down
8 changes: 5 additions & 3 deletions data_structures/src/staking/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ where

for capability in ALL_CAPABILITIES {
let epoch_before = self.epochs.get(capability);
let product_before = coins_before * epoch_before;
let product_added = coins * epoch;
let product_before = coins_before.lose_precision(WIT_DECIMAL_PLACES) * epoch_before;
let product_added = coins.lose_precision(WIT_DECIMAL_PLACES) * epoch;

#[allow(clippy::cast_possible_truncation)]
let epoch_after = Epoch::from(
(u64::from(product_before + product_added) / u64::from(coins_after)) as u32,
(u64::from(product_before + product_added)
/ u64::from(coins_after.lose_precision(WIT_DECIMAL_PLACES)))
as u32,
);
self.epochs.update(capability, epoch_after);
}
Expand Down
Loading
Loading