Skip to content
Snippets Groups Projects
Commit 39d0a539 authored by Wei Tang's avatar Wei Tang
Browse files

Refactor slot generic

parent 1d91b390
No related merge requests found
......@@ -21,11 +21,15 @@ use codec_derive::{Encode, Decode};
use crate::store::{self, ValidatorStore, PendingAttestationsStore, BlockStore};
use crate::context::{
Attestation, AttestationOf, EpochOf, BalanceContext, BalanceOf,
Attestation, AttestationOf, EpochOf, ValidatorContext, BalanceOf,
ValidatorIdOf,
};
/// Return whether given two attestations satisfy Casper slashing conditions.
pub fn slashable<C: Attestation>(a: &C, b: &C) -> Vec<C::ValidatorId> {
pub fn slashable<C: ValidatorContext>(
a: &AttestationOf<C>,
b: &AttestationOf<C>
) -> Vec<ValidatorIdOf<C>> {
let slashable = {
// Two attestations must be different.
if a == b {
......@@ -65,7 +69,7 @@ pub fn slashable<C: Attestation>(a: &C, b: &C) -> Vec<C::ValidatorId> {
/// Data needed for casper consensus.
#[derive(Clone, Eq, PartialEq, Encode, Decode)]
pub struct CasperProcess<C: BalanceContext> {
pub struct CasperProcess<C: ValidatorContext> {
/// Bitfield holding justification information.
pub justification_bitfield: u64,
/// Current epoch.
......@@ -78,13 +82,13 @@ pub struct CasperProcess<C: BalanceContext> {
pub previous_justified_epoch: EpochOf<C>,
}
impl<C: BalanceContext> Default for CasperProcess<C> {
impl<C: ValidatorContext> Default for CasperProcess<C> {
fn default() -> Self {
Self::new(Zero::zero())
}
}
impl<C: BalanceContext> CasperProcess<C> {
impl<C: ValidatorContext> CasperProcess<C> {
/// Create a new Casper context.
pub fn new(genesis_epoch: EpochOf<C>) -> Self {
Self {
......
......@@ -20,23 +20,45 @@ use num_traits::{One, Zero};
use core::ops::{Add, AddAssign, Sub, SubAssign, Mul, Div};
use codec::{Encode, Decode};
/// Casper attestation. The source should always be canon.
pub trait Attestation: PartialEq + Eq {
/// Type of validator Id.
type ValidatorId: PartialEq + Eq + Clone + Copy;
/// Block context.
pub trait BlockContext: Eq + PartialEq + Clone {
/// Type of epoch.
type Epoch: PartialEq + Eq + PartialOrd + Ord + Clone + Copy + Add<Output=Self::Epoch> + AddAssign + Sub<Output=Self::Epoch> + SubAssign + One + Zero + Encode + Decode;
/// Attestation slot.
type Slot: PartialEq + Eq + PartialOrd + Ord + Clone + Copy + Add<Output=Self::Slot> + AddAssign + Sub<Output=Self::Slot> + SubAssign + One + Zero + Encode + Decode;
}
/// Validator context.
pub trait ValidatorContext: BlockContext {
/// Attestation of this context.
type Attestation: Attestation<Context=Self>;
/// Balance of this context.
type Balance: PartialEq + Eq + PartialOrd + Ord + Clone + Copy + Add<Output=Self::Balance> + AddAssign + Sub<Output=Self::Balance> + SubAssign + Mul<Output=Self::Balance> + Div<Output=Self::Balance> + From<u8> + One + Zero;
/// Type of validator Id.
type ValidatorId: PartialEq + Eq + Clone + Copy;
}
/// Casper attestation. The source should always be canon.
pub trait Attestation: PartialEq + Eq {
/// Validator context of this attestation.
type Context: ValidatorContext;
/// Get slot of this attestation.
fn slot(&self) -> SlotOf<Self::Context>;
/// Whether this attestation's slot is on canon chain.
fn is_slot_canon(&self) -> bool;
/// This attestation's inclusion distance.
fn inclusion_distance(&self) -> SlotOf<Self::Context>;
/// Get validator Ids of this attestation.
fn validator_ids(&self) -> Vec<Self::ValidatorId>;
fn validator_ids(&self) -> Vec<ValidatorIdOf<Self::Context>>;
/// Whether this attestation's source is on canon chain.
fn is_source_canon(&self) -> bool;
/// Whether this attestation's target is on canon chain.
fn is_target_canon(&self) -> bool;
/// Get the source epoch of this attestation.
fn source_epoch(&self) -> Self::Epoch;
fn source_epoch(&self) -> EpochOf<Self::Context>;
/// Get the target epoch of this attestation.
fn target_epoch(&self) -> Self::Epoch;
fn target_epoch(&self) -> EpochOf<Self::Context>;
/// Whether this attestation's source and target is on canon chain.
fn is_casper_canon(&self) -> bool {
......@@ -44,38 +66,13 @@ pub trait Attestation: PartialEq + Eq {
}
}
/// Casper attestation with specific slot.
pub trait SlotAttestation: Attestation {
/// Attestation slot.
type Slot: PartialEq + Eq + PartialOrd + Ord + Clone + Copy + Add<Output=Self::Slot> + AddAssign + Sub<Output=Self::Slot> + SubAssign + One + Zero + Encode + Decode;
/// Get slot of this attestation.
fn slot(&self) -> Self::Slot;
/// Whether this attestation's slot is on canon chain.
fn is_slot_canon(&self) -> bool;
/// This attestation's inclusion distance.
fn inclusion_distance(&self) -> Self::Slot;
}
/// Basic epoch context for Casper.
pub trait BalanceContext: Eq + PartialEq + Clone {
/// Attestation of this context.
type Attestation: Attestation;
/// Balance of this context.
type Balance: PartialEq + Eq + PartialOrd + Ord + Clone + Copy + Add<Output=Self::Balance> + AddAssign + Sub<Output=Self::Balance> + SubAssign + Mul<Output=Self::Balance> + Div<Output=Self::Balance> + From<u8> + One + Zero;
}
/// Context with slot, suitable for collecting attestations across multiple blocks.
pub trait SlotContext: BalanceContext where
AttestationOf<Self>: SlotAttestation { }
/// Epoch of a context.
pub type EpochOf<C> = <AttestationOf<C> as Attestation>::Epoch;
pub type EpochOf<C> = <C as BlockContext>::Epoch;
/// Attestation of a context.
pub type AttestationOf<C> = <C as BalanceContext>::Attestation;
pub type AttestationOf<C> = <C as ValidatorContext>::Attestation;
/// Slot of a context.
pub type SlotOf<C> = <AttestationOf<C> as SlotAttestation>::Slot;
pub type SlotOf<C> = <C as BlockContext>::Slot;
/// Validator id of a context.
pub type ValidatorIdOf<C> = <AttestationOf<C> as Attestation>::ValidatorId;
pub type ValidatorIdOf<C> = <C as ValidatorContext>::ValidatorId;
/// Balance of a context.
pub type BalanceOf<C> = <C as BalanceContext>::Balance;
pub type BalanceOf<C> = <C as ValidatorContext>::Balance;
......@@ -22,8 +22,8 @@ use core::ops::{Add, Div};
use crate::casper::CasperProcess;
use crate::store::{ValidatorStore, PendingAttestationsStore, BlockStore};
use crate::context::{
Attestation, ValidatorIdOf, EpochOf, BalanceContext, BalanceOf,
SlotOf, SlotContext, AttestationOf, SlotAttestation,
Attestation, ValidatorIdOf, EpochOf, ValidatorContext, BalanceOf,
SlotOf,
};
/// Rewards for Casper.
......@@ -41,9 +41,7 @@ pub enum CasperRewardType {
/// Rewards for beacon chain.
#[derive(Eq, PartialEq, Clone)]
pub enum BeaconRewardType<C: SlotContext> where
AttestationOf<C>: SlotAttestation,
{
pub enum BeaconRewardType<C: ValidatorContext> {
/// The validator attested on the expected head.
ExpectedHead,
/// The validator is active, but does not attest on the epxected head.
......@@ -52,7 +50,11 @@ pub enum BeaconRewardType<C: SlotContext> where
InclusionDistance(SlotOf<C>),
}
fn push_rewards<A, T>(rewards: &mut Vec<(A::ValidatorId, T)>, attestation: &A, reward: T) where
fn push_rewards<A, T>(
rewards: &mut Vec<(<A::Context as ValidatorContext>::ValidatorId, T)>,
attestation: &A,
reward: T
) where
A: Attestation,
T: Clone,
{
......@@ -62,10 +64,9 @@ fn push_rewards<A, T>(rewards: &mut Vec<(A::ValidatorId, T)>, attestation: &A, r
}
/// Get rewards for beacon chain.
pub fn beacon_rewards<C: SlotContext, S>(
pub fn beacon_rewards<C: ValidatorContext, S>(
store: &S
) -> Vec<(ValidatorIdOf<C>, BeaconRewardType<C>)> where
AttestationOf<C>: SlotAttestation,
S: PendingAttestationsStore<C> + BlockStore<C> + ValidatorStore<C>,
{
let mut no_expected_head_validators = store.active_validators(store.previous_epoch()).into_iter().collect::<Vec<_>>();
......@@ -93,7 +94,7 @@ pub fn beacon_rewards<C: SlotContext, S>(
/// Get rewards for casper. Note that this usually needs to be called before `advance_epoch`, but after all pending
/// attestations have been pushed.
pub fn casper_rewards<C: BalanceContext, S>(
pub fn casper_rewards<C: ValidatorContext, S>(
context: &CasperProcess<C>,
store: &S
) -> Vec<(ValidatorIdOf<C>, CasperRewardType)> where
......@@ -131,9 +132,7 @@ pub fn casper_rewards<C: BalanceContext, S>(
}
/// Config for default reward scheme.
pub struct DefaultSchemeConfig<C: BalanceContext + SlotContext> where
AttestationOf<C>: SlotAttestation,
{
pub struct DefaultSchemeConfig<C: ValidatorContext> {
/// Base reward quotient.
pub base_reward_quotient: BalanceOf<C>,
/// Inactivity penalty quotient.
......@@ -147,7 +146,7 @@ pub struct DefaultSchemeConfig<C: BalanceContext + SlotContext> where
}
/// Reward action.
pub enum RewardAction<C: BalanceContext> {
pub enum RewardAction<C: ValidatorContext> {
/// Add balance to reward.
Add(BalanceOf<C>),
/// Sub balance to reward. Should wrap at zero.
......@@ -186,14 +185,13 @@ fn combined_validators<ValidatorId, T>(
}
/// Use default scheme for reward calculation. This only contains justification and finalization rewards.
pub fn default_scheme_rewards<C: BalanceContext + SlotContext, S>(
pub fn default_scheme_rewards<C: ValidatorContext, S>(
store: &S,
beacon_rewards: &[(ValidatorIdOf<C>, BeaconRewardType<C>)],
casper_rewards: &[(ValidatorIdOf<C>, CasperRewardType)],
epochs_since_finality: EpochOf<C>,
config: &DefaultSchemeConfig<C>,
) -> Vec<(ValidatorIdOf<C>, RewardAction<C>)> where
AttestationOf<C>: SlotAttestation,
EpochOf<C>: From<u8>,
BalanceOf<C>: From<EpochOf<C>> + From<SlotOf<C>>,
S: ValidatorStore<C> + BlockStore<C>,
......@@ -298,14 +296,13 @@ pub fn default_scheme_rewards<C: BalanceContext + SlotContext, S>(
}
/// Use default scheme for penalization.
pub fn default_scheme_penalties<C: BalanceContext + SlotContext, S>(
pub fn default_scheme_penalties<C: ValidatorContext, S>(
store: &S,
whistleblower: &ValidatorIdOf<C>,
slashings: &[ValidatorIdOf<C>],
epochs_since_finality: EpochOf<C>,
config: &DefaultSchemeConfig<C>,
) -> Vec<(ValidatorIdOf<C>, RewardAction<C>)> where
AttestationOf<C>: SlotAttestation,
EpochOf<C>: From<u8>,
BalanceOf<C>: From<EpochOf<C>> + From<SlotOf<C>>,
S: ValidatorStore<C> + BlockStore<C>,
......
......@@ -18,12 +18,12 @@
use num_traits::{One, Zero};
use crate::context::{
BalanceContext, BalanceOf, EpochOf, AttestationOf, Attestation,
ValidatorContext, BalanceOf, EpochOf, AttestationOf, Attestation,
ValidatorIdOf,
};
/// Store that holds validator active and balance information.
pub trait ValidatorStore<C: BalanceContext> {
pub trait ValidatorStore<C: ValidatorContext> {
/// Get total balance of given validator Ids.
fn total_balance(&self, validators: &[ValidatorIdOf<C>]) -> BalanceOf<C>;
/// Get all active validators at given epoch.
......@@ -31,7 +31,7 @@ pub trait ValidatorStore<C: BalanceContext> {
}
/// Store that holds pending attestations.
pub trait PendingAttestationsStore<C: BalanceContext> {
pub trait PendingAttestationsStore<C: ValidatorContext> {
/// Get the current list of attestations.
fn attestations(&self) -> Vec<AttestationOf<C>>;
/// Retain specific attestations and remove the rest.
......@@ -39,7 +39,7 @@ pub trait PendingAttestationsStore<C: BalanceContext> {
}
/// Store that holds general block information.
pub trait BlockStore<C: BalanceContext> {
pub trait BlockStore<C: ValidatorContext> {
/// Get the current epoch.
fn epoch(&self) -> EpochOf<C>;
/// Get the next epoch.
......@@ -57,7 +57,7 @@ pub trait BlockStore<C: BalanceContext> {
}
/// Attesting canon target balance at epoch.
pub fn canon_target_attesting_balance<C: BalanceContext, S>(
pub fn canon_target_attesting_balance<C: ValidatorContext, S>(
store: &S,
epoch: EpochOf<C>
) -> BalanceOf<C> where
......@@ -75,7 +75,7 @@ pub fn canon_target_attesting_balance<C: BalanceContext, S>(
}
/// Attesting canon source balance at epoch.
pub fn canon_source_attesting_balance<C: BalanceContext, S>(
pub fn canon_source_attesting_balance<C: ValidatorContext, S>(
store: &S,
epoch: EpochOf<C>
) -> BalanceOf<C> where
......@@ -93,7 +93,7 @@ pub fn canon_source_attesting_balance<C: BalanceContext, S>(
}
/// Total balance at epoch.
pub fn active_total_balance<C: BalanceContext, S>(
pub fn active_total_balance<C: ValidatorContext, S>(
store: &S,
epoch: EpochOf<C>
) -> BalanceOf<C> where
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment