Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
shasper
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
shasper
Commits
39d0a539
Commit
39d0a539
authored
6 years ago
by
Wei Tang
Browse files
Options
Downloads
Patches
Plain Diff
Refactor slot generic
parent
1d91b390
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
casper/src/casper.rs
+9
-5
9 additions, 5 deletions
casper/src/casper.rs
casper/src/context.rs
+34
-37
34 additions, 37 deletions
casper/src/context.rs
casper/src/reward.rs
+14
-17
14 additions, 17 deletions
casper/src/reward.rs
casper/src/store.rs
+7
-7
7 additions, 7 deletions
casper/src/store.rs
with
64 additions
and
66 deletions
casper/src/casper.rs
+
9
−
5
View file @
39d0a539
...
...
@@ -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
:
B
al
ance
Context
>
{
pub
struct
CasperProcess
<
C
:
V
al
idator
Context
>
{
/// 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
:
B
al
ance
Context
>
Default
for
CasperProcess
<
C
>
{
impl
<
C
:
V
al
idator
Context
>
Default
for
CasperProcess
<
C
>
{
fn
default
()
->
Self
{
Self
::
new
(
Zero
::
zero
())
}
}
impl
<
C
:
B
al
ance
Context
>
CasperProcess
<
C
>
{
impl
<
C
:
V
al
idator
Context
>
CasperProcess
<
C
>
{
/// Create a new Casper context.
pub
fn
new
(
genesis_epoch
:
EpochOf
<
C
>
)
->
Self
{
Self
{
...
...
This diff is collapsed.
Click to expand it.
casper/src/context.rs
+
34
−
37
View file @
39d0a539
...
...
@@ -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
<
ValidatorId
Of
<
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
B
al
ance
Context
>
::
Attestation
;
pub
type
AttestationOf
<
C
>
=
<
C
as
V
al
idator
Context
>
::
Attestation
;
/// Slot of a context.
pub
type
SlotOf
<
C
>
=
<
AttestationOf
<
C
>
as
S
lo
tAttestation
>
::
Slot
;
pub
type
SlotOf
<
C
>
=
<
C
as
B
lo
ckContext
>
::
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
B
al
ance
Context
>
::
Balance
;
pub
type
BalanceOf
<
C
>
=
<
C
as
V
al
idator
Context
>
::
Balance
;
This diff is collapsed.
Click to expand it.
casper/src/reward.rs
+
14
−
17
View file @
39d0a539
...
...
@@ -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
,
B
al
ance
Context
,
BalanceOf
,
SlotOf
,
SlotContext
,
AttestationOf
,
SlotAttestation
,
Attestation
,
ValidatorIdOf
,
EpochOf
,
V
al
idator
Context
,
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
:
Slot
Context
,
S
>
(
pub
fn
beacon_rewards
<
C
:
Validator
Context
,
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
:
B
al
ance
Context
,
S
>
(
pub
fn
casper_rewards
<
C
:
V
al
idator
Context
,
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
:
B
al
ance
Context
>
{
pub
enum
RewardAction
<
C
:
V
al
idator
Context
>
{
/// 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
:
B
al
anceContext
+
Slot
Context
,
S
>
(
pub
fn
default_scheme_rewards
<
C
:
V
al
idator
Context
,
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
:
B
al
anceContext
+
Slot
Context
,
S
>
(
pub
fn
default_scheme_penalties
<
C
:
V
al
idator
Context
,
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
>
,
...
...
This diff is collapsed.
Click to expand it.
casper/src/store.rs
+
7
−
7
View file @
39d0a539
...
...
@@ -18,12 +18,12 @@
use
num_traits
::{
One
,
Zero
};
use
crate
::
context
::{
B
al
ance
Context
,
BalanceOf
,
EpochOf
,
AttestationOf
,
Attestation
,
V
al
idator
Context
,
BalanceOf
,
EpochOf
,
AttestationOf
,
Attestation
,
ValidatorIdOf
,
};
/// Store that holds validator active and balance information.
pub
trait
ValidatorStore
<
C
:
B
al
ance
Context
>
{
pub
trait
ValidatorStore
<
C
:
V
al
idator
Context
>
{
/// 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
:
B
al
ance
Context
>
{
pub
trait
PendingAttestationsStore
<
C
:
V
al
idator
Context
>
{
/// 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
:
B
al
ance
Context
>
{
pub
trait
BlockStore
<
C
:
V
al
idator
Context
>
{
/// 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
:
B
al
ance
Context
,
S
>
(
pub
fn
canon_target_attesting_balance
<
C
:
V
al
idator
Context
,
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
:
B
al
ance
Context
,
S
>
(
pub
fn
canon_source_attesting_balance
<
C
:
V
al
idator
Context
,
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
:
B
al
ance
Context
,
S
>
(
pub
fn
active_total_balance
<
C
:
V
al
idator
Context
,
S
>
(
store
:
&
S
,
epoch
:
EpochOf
<
C
>
)
->
BalanceOf
<
C
>
where
...
...
This diff is collapsed.
Click to expand it.
gabriel klawitter
@gabriel
mentioned in commit
e56f09bd
·
6 years ago
mentioned in commit
e56f09bd
mentioned in commit e56f09bde5fbfde6d01fa78b3de4113afffa2830
Toggle commit list
Wei Tang
@sorpaas
mentioned in commit
b84ba156
·
6 years ago
mentioned in commit
b84ba156
mentioned in commit b84ba156c87aa8e111c28025cd30e94222312981
Toggle commit list
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment