Skip to content
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
14 changes: 14 additions & 0 deletions data_structures/src/staking/aux.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::{Debug, Display, Formatter};
use std::{rc::Rc, str::FromStr, sync::RwLock};

use failure::Error;
Expand Down Expand Up @@ -98,6 +99,19 @@ where
}
}

impl<Address> Display for StakeKey<Address>
where
Address: Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"validator: {} withdrawer: {}",
self.validator, self.withdrawer
)
}
}

/// Couples an amount of coins, a validator address and a withdrawer address together. This is meant to be used in
/// `Stakes` as the index of the `by_coins` index.
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
Expand Down
69 changes: 64 additions & 5 deletions data_structures/src/staking/errors.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,104 @@
use std::sync::PoisonError;

use crate::staking::aux::StakeKey;
use failure::Fail;
use std::{
convert::From,
fmt::{Debug, Display},
sync::PoisonError,
};

/// All errors related to the staking functionality.
#[derive(Debug, PartialEq)]
pub enum StakesError<Address, Coins, Epoch> {
#[derive(Debug, PartialEq, Fail)]
pub enum StakesError<Address, Coins, Epoch>
where
Address: Debug + Display + Sync + Send + 'static,
Coins: Debug + Display + Sync + Send + 'static,
Epoch: Debug + Display + Sync + Send + 'static,
{
/// The amount of coins being staked or the amount that remains after unstaking is below the
/// minimum stakeable amount.
#[fail(
display = "The amount of coins being staked ({}) or the amount that remains after unstaking is below the minimum stakeable amount ({})",
amount, minimum
)]
AmountIsBelowMinimum {
/// The number of coins being staked or remaining after staking.
amount: Coins,
/// The minimum stakeable amount.
minimum: Coins,
},
/// Tried to query `Stakes` for information that belongs to the past.
#[fail(
display = "Tried to query `Stakes` for information that belongs to the past. Query Epoch: {} Latest Epoch: {}",
epoch, latest
)]
EpochInThePast {
/// The Epoch being referred.
epoch: Epoch,
/// The latest Epoch.
latest: Epoch,
},
/// An operation thrown an Epoch value that overflows.
#[fail(
display = "An operation thrown an Epoch value that overflows. Computed Epoch: {} Maximum Epoch: {}",
computed, maximum
)]
EpochOverflow {
/// The computed Epoch value.
computed: u64,
/// The maximum Epoch.
maximum: Epoch,
},
/// Tried to query for a stake entry that is not registered in `Stakes`.
#[fail(
display = "Tried to query for a stake entry that is not registered in Stakes {}",
key
)]
EntryNotFound {
/// A validator and withdrawer address pair.
key: StakeKey<Address>,
},
/// Tried to obtain a lock on a write-locked piece of data that is already locked.
#[fail(
display = "The authentication signature contained within a stake transaction is not valid for the given validator and withdrawer addresses"
)]
PoisonedLock,
/// The authentication signature contained within a stake transaction is not valid for the given validator and
/// withdrawer addresses.
#[fail(
display = "The authentication signature contained within a stake transaction is not valid for the given validator and withdrawer addresses"
)]
InvalidAuthentication,
/// Tried to query for a stake entry by validator that is not registered in `Stakes`.
#[fail(
display = "Tried to query for a stake entry by validator ({}) that is not registered in Stakes",
validator
)]
ValidatorNotFound {
/// A validator address.
validator: Address,
},
/// Tried to query for a stake entry by withdrawer that is not registered in `Stakes`.
#[fail(
display = "Tried to query for a stake entry by withdrawer ({}) that is not registered in Stakes",
withdrawer
)]
WithdrawerNotFound {
/// A withdrawer address.
withdrawer: 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"
)]
EmptyQuery,
}

impl<T, Address, Coins, Epoch> From<PoisonError<T>> for StakesError<Address, Coins, Epoch> {
impl<T, Address, Coins, Epoch> From<PoisonError<T>> for StakesError<Address, Coins, Epoch>
where
Address: Debug + Display + Sync + Send + 'static,
Coins: Debug + Display + Sync + Send + 'static,
Epoch: Debug + Display + Sync + Send + 'static,
{
fn from(_value: PoisonError<T>) -> Self {
StakesError::PoisonedLock
}
Expand Down
19 changes: 16 additions & 3 deletions data_structures/src/staking/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{marker::PhantomData, ops::*};
use serde::{Deserialize, Serialize};

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 All @@ -23,16 +24,28 @@ where

impl<Address, Coins, Epoch, Power> Stake<Address, Coins, Epoch, Power>
where
Address: Default,
Address: Default + Debug + Display + Sync + Send,
Coins: Copy
+ From<u64>
+ PartialOrd
+ num_traits::Zero
+ Add<Output = Coins>
+ Sub<Output = Coins>
+ Mul
+ Mul<Epoch, Output = Power>,
Epoch: Copy + Default + num_traits::Saturating + Sub<Output = Epoch> + From<u32>,
+ Mul<Epoch, Output = Power>
+ Debug
+ Display
+ Send
+ Sync,
Epoch: Copy
+ Default
+ num_traits::Saturating
+ Sub<Output = Epoch>
+ From<u32>
+ Debug
+ Display
+ Sync
+ Send,
Power: Add<Output = Power> + Div<Output = Power>,
u64: From<Coins> + From<Power>,
{
Expand Down
Loading