Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Equivocations slashing #3314

Closed
wants to merge 211 commits into from
Closed
Changes from all commits
Commits
Show all changes
211 commits
Select commit Hold shift + click to select a range
4f24928
Remove offline slashing logic from staking.
tomusdrw Aug 6, 2019
9f1cad1
Initial version of reworked offence module, can report offences
folsen Aug 6, 2019
3fc6eaa
Merge branch 'retreat-slashing' of github.com:paritytech/substrate in…
tomusdrw Aug 6, 2019
89e623b
Clean up staking example.
tomusdrw Aug 6, 2019
0e1d398
Commit SlashingOffence
pepyakin Aug 6, 2019
d5c9cd1
Force new era on slash.
tomusdrw Aug 6, 2019
45e5907
Add offenders in the SlashingOffence trait.
pepyakin Aug 6, 2019
b93118f
Introduce the ReportOffence trait.
pepyakin Aug 6, 2019
051001a
Rename `Offence`.
pepyakin Aug 6, 2019
2dae936
Add on_before_session_ending handler.
tomusdrw Aug 6, 2019
5c3a249
Move offence related stuff under sr-primitives.
pepyakin Aug 6, 2019
aaa8159
Fix cargo check.
tomusdrw Aug 6, 2019
06ee813
Import new im-online implementation.
tomusdrw Aug 6, 2019
a807dae
Adding validator count to historical session storage as it's needed f…
folsen Aug 6, 2019
32cba20
Add a comment about offence.
pepyakin Aug 6, 2019
39f7ed0
Add BabeEquivocationOffence
pepyakin Aug 6, 2019
c5579b3
GrandpaEquivocationOffence
pepyakin Aug 6, 2019
ecd8d81
slash_fraction and fix
pepyakin Aug 6, 2019
cfe194d
current_era_start_session_index
pepyakin Aug 6, 2019
4cb0bfe
UnresponsivnessOffence
pepyakin Aug 7, 2019
7ede025
Finalise OnOffenceHandler traits, and stub impl for staking.
tomusdrw Aug 7, 2019
a56a7b4
slash_fraction doesn't really need &self
pepyakin Aug 7, 2019
da2cc83
Note that offenders count is greater than 0
pepyakin Aug 7, 2019
0e2047a
Merge branch 'master' into retreat-slashing
tomusdrw Aug 7, 2019
61c6c22
Add a test to ensure that I got the math right
pepyakin Aug 7, 2019
c94f253
Merge remote-tracking branch 'origin/retreat-slashing' into retreat-s…
tomusdrw Aug 7, 2019
f7a8711
Use FullIdentification in offences.
tomusdrw Aug 7, 2019
7b14a37
Use FullIndentification.
tomusdrw Aug 7, 2019
cea7edc
Hook up the offences module.
pepyakin Aug 7, 2019
da01b46
Report unresponsive validators
pepyakin Aug 7, 2019
e25b43a
Make sure eras have the same length.
tomusdrw Aug 7, 2019
55aeff5
Slashing and rewards.
tomusdrw Aug 7, 2019
0c8b03f
Fix compilation.
tomusdrw Aug 7, 2019
382ffa3
Distribute rewards.
tomusdrw Aug 7, 2019
ac05b4f
Merge branch 'master' into retreat-slashing
tomusdrw Aug 7, 2019
ea68a83
Supply validators_count
pepyakin Aug 7, 2019
fae60b9
Use identificationTuple in Unresponsivness report
pepyakin Aug 7, 2019
6af9ffc
Fix merge.
pepyakin Aug 7, 2019
f2f91ab
Make sure we don't slash if amount is zero.
tomusdrw Aug 7, 2019
9c91502
We don't return an error from report_offence anymo
pepyakin Aug 7, 2019
fdbc3bd
We actually can use vec!
pepyakin Aug 7, 2019
c101666
Prevent division by zero if the reporters is empty
pepyakin Aug 7, 2019
2d1d103
offence_forces_new_era/nominators_also_get_slashed
pepyakin Aug 7, 2019
b7989c9
advance_session
pepyakin Aug 7, 2019
21c1131
Fix tests.
pepyakin Aug 7, 2019
a60252e
Merge branch 'master' into retreat-slashing
tomusdrw Aug 7, 2019
0db09ad
Merge remote-tracking branch 'origin/retreat-slashing' into retreat-s…
tomusdrw Aug 7, 2019
e9029f8
Update srml/staking/src/lib.rs
tomusdrw Aug 7, 2019
e40e448
slashing_performed_according_exposure
pepyakin Aug 7, 2019
63f08a0
Check that reporters receive their slice.
pepyakin Aug 7, 2019
fdb9d15
Small clean-up.
tomusdrw Aug 7, 2019
108869c
Merge branch 'retreat-slashing' of github.com:paritytech/substrate in…
tomusdrw Aug 7, 2019
39cdeb6
invulnerables_are_not_slashed
pepyakin Aug 7, 2019
67b0a50
Minor clean ups.
pepyakin Aug 7, 2019
763d32d
Improve docs.
pepyakin Aug 7, 2019
9c43140
dont_slash_if_fraction_is_zero
pepyakin Aug 7, 2019
c65854c
Remove session dependency from offences.
tomusdrw Aug 7, 2019
77c23dc
Introduce sr-staking-primitives
pepyakin Aug 7, 2019
4384e6c
Move offence under sr_staking_primitives
pepyakin Aug 7, 2019
7d94eaf
rename session_index
pepyakin Aug 7, 2019
97d4f5c
Resolves todos re using SessionIndex
pepyakin Aug 7, 2019
b8f65a8
Fix staking tests.
pepyakin Aug 7, 2019
fe75ebe
Properly scale denominator.
pepyakin Aug 7, 2019
2ff220b
Fix UnresponsivnessOffence
pepyakin Aug 7, 2019
00a8506
Fix compilation.
tomusdrw Aug 8, 2019
eb790f2
Tests for offences.
tomusdrw Aug 8, 2019
bdf5177
Merge branch 'master' into retreat-slashing
tomusdrw Aug 8, 2019
2d0881a
Clean offences tests.
pepyakin Aug 8, 2019
8b4cd8e
Fix staking doc test.
tomusdrw Aug 8, 2019
b90e397
Merge branch 'retreat-slashing' of github.com:paritytech/substrate in…
tomusdrw Aug 8, 2019
b9fa71b
Bump spec version
tomusdrw Aug 8, 2019
6eb6cab
Fix aura tests.
tomusdrw Aug 8, 2019
9e7e8ce
Fix node_executor
pepyakin Aug 8, 2019
f23b2f3
Deposit an event on offence.
pepyakin Aug 8, 2019
2de1c86
Fix compilation of node-runtime
pepyakin Aug 8, 2019
6420e80
Remove aura slashing logic.
tomusdrw Aug 8, 2019
591fa51
Remove HandleReport
pepyakin Aug 8, 2019
0c4760f
Update docs for timeslot.
pepyakin Aug 8, 2019
48a3366
rename with_on_offence_fractions
pepyakin Aug 8, 2019
4bc4e6c
Add should_properly_count_offences
pepyakin Aug 8, 2019
eb1de19
Replace ValidatorIdByIndex with CurrentElectedSet
pepyakin Aug 9, 2019
b015094
Clarify babe equivocation
pepyakin Aug 12, 2019
04ac719
Merge master
pepyakin Aug 12, 2019
1facbf8
Fix offences.
pepyakin Aug 12, 2019
57bc7a2
Rename validators_count to validator_set_count
tomusdrw Aug 13, 2019
0b57b0d
Fix squaring.
pepyakin Aug 14, 2019
12baeda
Update core/sr-staking-primitives/src/offence.rs
tomusdrw Aug 14, 2019
a88c5ac
Docs for CurrentElectedSet.
pepyakin Aug 14, 2019
53b2c64
Don't punish only invulnerables
pepyakin Aug 14, 2019
2b81cc2
Merge 'origin/master' into retreat-slashing
pepyakin Aug 14, 2019
2c30785
Use `get/insert` instead of `mutate`.
pepyakin Aug 14, 2019
fa1d88e
Fix compilation
pepyakin Aug 14, 2019
192e811
Update core/sr-staking-primitives/src/offence.rs
tomusdrw Aug 14, 2019
455dcac
Update srml/offences/src/lib.rs
tomusdrw Aug 14, 2019
3c46391
Update srml/im-online/src/lib.rs
tomusdrw Aug 14, 2019
7257ca9
Update srml/im-online/src/lib.rs
tomusdrw Aug 14, 2019
542de32
Update srml/im-online/src/lib.rs
tomusdrw Aug 14, 2019
e02ef2f
Update srml/babe/src/lib.rs
tomusdrw Aug 14, 2019
08366c0
Update core/sr-staking-primitives/src/offence.rs
tomusdrw Aug 14, 2019
3f398c4
Update core/sr-staking-primitives/src/offence.rs
tomusdrw Aug 14, 2019
fe3094c
Update core/sr-staking-primitives/src/offence.rs
tomusdrw Aug 14, 2019
9b4263c
Update core/sr-staking-primitives/src/offence.rs
tomusdrw Aug 14, 2019
ce5e8ed
Update core/sr-staking-primitives/src/offence.rs
tomusdrw Aug 14, 2019
758a1ec
Add aura todo.
tomusdrw Aug 14, 2019
d58208b
Allow multiple reports for single offence report.
tomusdrw Aug 14, 2019
4feb6e3
Fix slash_fraction calculation.
tomusdrw Aug 14, 2019
dd58c06
Fix typos.
tomusdrw Aug 14, 2019
0c65697
Fix compilation and tests.
tomusdrw Aug 14, 2019
6ddad41
srml-contracts: Refactor away some duplication in runtime functions. …
jimpo Aug 8, 2019
246b5b1
Child storage tests and genesis fix. (#3185)
cheme Aug 8, 2019
cf31399
Add transaction pool to Babe check header
seerscode Jul 27, 2019
907ceaa
Add tx pool to Aura import_queue
seerscode Jul 30, 2019
af31771
Fix tests, node-template
seerscode Jul 30, 2019
a9c58c5
Add comments regarding unused _transaction_pool
seerscode Jul 30, 2019
ee582c4
Add AuthorshipEquivocation trait
seerscode Aug 6, 2019
d9a869f
Add BabeEquivocation struct
seerscode Aug 6, 2019
5bcedda
Add report_equivocation to Babe
seerscode Aug 6, 2019
0d34076
Add historical to babe
seerscode Aug 6, 2019
dd3f533
Move find_pre_digest to primitives
seerscode Aug 6, 2019
cdc43f2
Use get_slot
seerscode Aug 6, 2019
106d2f0
Add tx pool to environment
seerscode Aug 6, 2019
c62f64c
Create grandpa equivocation
seerscode Aug 6, 2019
98a7d52
Add report_equivocation to grandpa
seerscode Aug 6, 2019
9c6e323
Add proof to construct_report
seerscode Aug 6, 2019
4a8e88b
add construct report to babe
seerscode Aug 6, 2019
7b2dcde
Fix signature and public tmp
seerscode Aug 6, 2019
a7e959e
Add SessionIndex
seerscode Aug 7, 2019
f0108ac
Fix signature and public tmp
seerscode Aug 6, 2019
1b01ad4
Fix merge; update grandpa
seerscode Aug 7, 2019
916af40
Add small trait to transaction pool
seerscode Aug 7, 2019
1bbd700
Submit to tx pool from grandpa
seerscode Aug 7, 2019
26efab0
Submit call from Babe
seerscode Aug 7, 2019
d5ae058
Rename construct methods
seerscode Aug 7, 2019
405f995
add prove
seerscode Aug 7, 2019
231d1bd
Merge master
seerscode Aug 7, 2019
41bd05a
Get key for grandpa
seerscode Aug 7, 2019
e753bc6
Remove proof from grandpa equi
seerscode Aug 7, 2019
b28d05c
Add proof to report
seerscode Aug 7, 2019
96a9ffe
fix cargo.lock
seerscode Aug 10, 2019
a51ef4b
Add ValidateUnsigned to babe
seerscode Aug 12, 2019
5a2efb9
Sign babe tx
seerscode Aug 12, 2019
fa95730
Sign grandpa tx, grandpa unsigned
seerscode Aug 12, 2019
f29ddec
submit tx works
seerscode Aug 12, 2019
b5084fc
merge retreat-slashing
seerscode Aug 12, 2019
9919e29
Add ReportEquivocation type to Babe
seerscode Aug 13, 2019
95a01d6
Remove dup function
seerscode Aug 13, 2019
6158fdb
Add keystore
seerscode Aug 13, 2019
35c2e87
Use construct_with_context to get keys
seerscode Aug 14, 2019
efc7248
Cleaning
seerscode Aug 14, 2019
870a7bd
Fix decode error
seerscode Aug 14, 2019
ad3797f
Add test extrinsic
seerscode Aug 14, 2019
0f998dd
fix delimiter
seerscode Aug 14, 2019
6c5e5d9
add signature to aura
seerscode Aug 14, 2019
4d59551
Uncomment check_equivocation in Aura temporarily
seerscode Aug 14, 2019
fc927d1
Fix staking tests.
tomusdrw Aug 14, 2019
f3a2d68
Update srml/im-online/src/lib.rs
tomusdrw Aug 14, 2019
f8236f5
Finish removing keystore
seerscode Aug 15, 2019
46015f8
Fix test-runtime
seerscode Aug 15, 2019
97bde5b
Add extrinsic to mock grandpa
seerscode Aug 15, 2019
782e46d
Fix tx pool test
seerscode Aug 15, 2019
ab32ead
move construct back to node
seerscode Aug 15, 2019
de9fccc
simplify slots
seerscode Aug 15, 2019
10741ec
update babe
seerscode Aug 15, 2019
9f82296
update node
seerscode Aug 15, 2019
940bef6
Update consensus
seerscode Aug 15, 2019
f9b82db
fix test
seerscode Aug 15, 2019
b7f44fc
Fix doc on time_slot
pepyakin Aug 15, 2019
dcf04c9
fix stuff
seerscode Aug 15, 2019
ce31bf1
Add new SubmitExtrinsic trait
seerscode Aug 16, 2019
50da947
last changes
seerscode Aug 16, 2019
a99d228
Merge branch 'master' into marcio/all-equivocation-0
tomusdrw Aug 16, 2019
369f8f4
post-merge fixes.
tomusdrw Aug 16, 2019
2d0fe68
Allow slashing only on current era (#3411)
rphmeier Aug 16, 2019
7fd484d
Fix some more stuff.
tomusdrw Aug 16, 2019
6d2b7f8
Pave the way for pruning
pepyakin Aug 15, 2019
03734e8
Merge 'origin/master' into retreat-slashing
pepyakin Aug 16, 2019
2803ed3
Make it compile, clean up dependencies.
tomusdrw Aug 16, 2019
7781bc9
Address issues.
pepyakin Aug 16, 2019
dbd49d7
More compilation fixes.
tomusdrw Aug 16, 2019
967ca96
Try to refactor collect_offence_reports
pepyakin Aug 16, 2019
ae41ebc
Even more compilation fixes.
tomusdrw Aug 16, 2019
6bc58a8
Other fixes.
pepyakin Aug 16, 2019
bbc837b
More fixes.
pepyakin Aug 16, 2019
9430a2d
Merge 'origin/master' into retreat-slashing
pepyakin Aug 16, 2019
7ae6fb8
Merge branch 'master' into marcio/all-equivocation-0
tomusdrw Aug 16, 2019
fadd9e7
Merge branch 'retreat-slashing' into marcio/all-equivocation-0
tomusdrw Aug 16, 2019
2a7601e
Clean up
tomusdrw Aug 16, 2019
3112203
Untangle babe and historical sessions.
tomusdrw Aug 16, 2019
94ae5f6
Fix test-runtime.
tomusdrw Aug 16, 2019
c74f722
Merge branch 'master' into marcio/all-equivocation-0
tomusdrw Aug 16, 2019
40eb793
Move aura to app-crypto bounds.
tomusdrw Aug 16, 2019
c2b83df
Fix more tests.
tomusdrw Aug 16, 2019
98265a5
Fix finality-grandpa tests.
tomusdrw Aug 16, 2019
e7be080
Impl grandpa for test-runtime.
tomusdrw Aug 16, 2019
0ff99e7
Fix warnings.
tomusdrw Aug 16, 2019
8d345e4
Fix should return runtime version test.
seerscode Aug 16, 2019
799496d
Fix line width
seerscode Aug 16, 2019
4f95ff5
Fix tests.
tomusdrw Aug 16, 2019
4f51e27
Merge branch 'marcio/all-equivocation-0' of github.com:paritytech/sub…
tomusdrw Aug 16, 2019
68468e4
Remove ValidateUnsigned from babe srml.
seerscode Aug 16, 2019
6e5068b
Remove ValidateUnsigned from Grandpa srml.
seerscode Aug 16, 2019
ea6b4fa
Add commented report_equivocation to Grandpa.
seerscode Aug 16, 2019
3ed3e26
Make stuff no-std for Extra.
seerscode Aug 16, 2019
cf17c79
Add implementation for signed tx to Babe node.
seerscode Aug 16, 2019
fb3002e
Remove signature from validate equivocation in Babe srml.
seerscode Aug 16, 2019
2ded0a7
Implemente signed tx for Grandpa node.
seerscode Aug 17, 2019
8530934
Move implementations of construct_equivocation to srmls.
seerscode Aug 17, 2019
4dde8a2
Make tests compile.
seerscode Aug 17, 2019
e3cbd31
Merge master
seerscode Aug 19, 2019
9348769
Fix node-template
seerscode Aug 20, 2019
8fe54c3
Merge master
seerscode Aug 20, 2019
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
1,271 changes: 638 additions & 633 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions core/authority-discovery/primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -14,8 +14,8 @@ rstd = { package = "sr-std", path = "../../sr-std", default-features = false }
[features]
default = ["std"]
std = [
"rstd/std",
"client/std",
"codec/std",
"sr-primitives/std"
"codec/std",
"client/std",
"sr-primitives/std",
"rstd/std",
]
57 changes: 26 additions & 31 deletions core/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
@@ -28,8 +28,9 @@
//!
//! NOTE: Aura itself is designed to be generic over the crypto used.
#![forbid(missing_docs, unsafe_code)]
use std::{sync::Arc, time::Duration, thread, marker::PhantomData, hash::Hash, fmt::Debug, pin::Pin};
use std::{sync::Arc, time::Duration, thread, marker::PhantomData, pin::Pin};

use app_crypto::{AppPair, RuntimeAppPublic};
use codec::{Encode, Decode, Codec};
use consensus_common::{self, BlockImport, Environment, Proposer,
ForkChoiceStrategy, BlockImportParams, BlockOrigin, Error as ConsensusError,
@@ -44,9 +45,9 @@ use client::{
};

use sr_primitives::{generic::{BlockId, OpaqueDigestItemId}, Justification};
use sr_primitives::traits::{Block as BlockT, Header, DigestItemFor, ProvideRuntimeApi, Zero, Member};

use sr_primitives::traits::{Block as BlockT, Header, DigestItemFor, ProvideRuntimeApi, Zero};
use primitives::crypto::Pair;

use inherents::{InherentDataProviders, InherentData};

use futures::prelude::*;
@@ -96,7 +97,7 @@ impl SlotDuration {
}

/// Get slot author for given block along with authorities.
fn slot_author<P: Pair>(slot_num: u64, authorities: &[AuthorityId<P>]) -> Option<&AuthorityId<P>> {
fn slot_author<P: AppPair>(slot_num: u64, authorities: &[AuthorityId<P>]) -> Option<&AuthorityId<P>> {
if authorities.is_empty() { return None }

let idx = slot_num % (authorities.len() as u64);
@@ -147,9 +148,8 @@ pub fn start_aura<B, C, SC, E, I, P, SO, Error, H>(
E: Environment<B, Error=Error> + Send + Sync + 'static,
E::Proposer: Proposer<B, Error=Error>,
<E::Proposer as Proposer<B>>::Create: Unpin + Send,
P: Pair + Send + Sync,
P::Public: Hash + Member + Encode + Decode,
P::Signature: Hash + Member + Encode + Decode,
P: AppPair,
<P as Pair>::Signature: Codec,
H: Header<Hash=B::Hash>,
I: BlockImport<B> + Send + Sync + 'static,
Error: ::std::error::Error + Send + From<::consensus_common::Error> + From<I::Error> + 'static,
@@ -197,9 +197,8 @@ impl<H, B, C, E, I, P, Error, SO> slots::SimpleSlotWorker<B> for AuraWorker<C, E
<E::Proposer as Proposer<B>>::Create: Unpin + Send,
H: Header<Hash=B::Hash>,
I: BlockImport<B> + Send + Sync + 'static,
P: Pair + Send + Sync,
P::Public: Member + Encode + Decode + Hash,
P::Signature: Member + Encode + Decode + Hash + Debug,
P: AppPair,
<P as Pair>::Signature: Codec,
SO: SyncOracle + Send + Clone,
Error: ::std::error::Error + Send + From<::consensus_common::Error> + From<I::Error> + 'static,
{
@@ -295,9 +294,8 @@ impl<H, B: BlockT, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I,
<E::Proposer as Proposer<B>>::Create: Unpin + Send + 'static,
H: Header<Hash=B::Hash>,
I: BlockImport<B> + Send + Sync + 'static,
P: Pair + Send + Sync,
P::Public: Member + Encode + Decode + Hash,
P::Signature: Member + Encode + Decode + Hash + Debug,
P: AppPair,
<P as Pair>::Signature: Codec,
SO: SyncOracle + Send + Sync + Clone,
Error: ::std::error::Error + Send + From<::consensus_common::Error> + From<I::Error> + 'static,
{
@@ -317,10 +315,8 @@ macro_rules! aura_err {
};
}

fn find_pre_digest<B: BlockT, P: Pair>(header: &B::Header) -> Result<u64, String>
fn find_pre_digest<B: BlockT, P: AppPair>(header: &B::Header) -> Result<u64, String>
where DigestItemFor<B>: CompatibleDigestItem<P>,
P::Signature: Decode,
P::Public: Encode + Decode + PartialEq + Clone,
{
let mut pre_digest: Option<u64> = None;
for log in header.digest().logs() {
@@ -338,10 +334,7 @@ fn find_pre_digest<B: BlockT, P: Pair>(header: &B::Header) -> Result<u64, String
/// if it's successful, returns the pre-header and the digest item containing the seal.
///
/// This digest item will always return `Some` when used with `as_aura_seal`.
//
// FIXME #1018 needs misbehavior types. The `transaction_pool` parameter will be
// used to submit such misbehavior reports.
fn check_header<C, B: BlockT, P: Pair, T>(
fn check_header<C, B: BlockT, P: AppPair, T>(
client: &C,
slot_now: u64,
mut header: B::Header,
@@ -350,9 +343,9 @@ fn check_header<C, B: BlockT, P: Pair, T>(
_transaction_pool: Option<&T>,
) -> Result<CheckedHeader<B::Header, (u64, DigestItemFor<B>)>, String> where
DigestItemFor<B>: CompatibleDigestItem<P>,
P::Signature: Decode,
C: client::backend::AuxStore,
P::Public: Encode + Decode + PartialEq + Clone,
AuthorityId<P>: RuntimeAppPublic<Signature = <P as Pair>::Signature>,
<P as Pair>::Signature: Codec + Clone,
T: Send + Sync + 'static,
{
let seal = match header.digest_mut().pop() {
@@ -385,13 +378,15 @@ fn check_header<C, B: BlockT, P: Pair, T>(
slot_now,
slot_num,
&header,
sig,
expected_author,
).map_err(|e| e.to_string())? {
// TODO [slashing] report misbehaviour in future.
info!(
"Slot author is equivocating at slot {} with headers {:?} and {:?}",
slot_num,
equivocation_proof.fst_header().hash(),
equivocation_proof.snd_header().hash(),
equivocation_proof.first_header.hash(),
equivocation_proof.second_header.hash(),
);
}

@@ -467,9 +462,9 @@ impl<B: BlockT, C, P, T> Verifier<B> for AuraVerifier<C, P, T> where
C: ProvideRuntimeApi + Send + Sync + client::backend::AuxStore + ProvideCache<B> + BlockOf,
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>>,
DigestItemFor<B>: CompatibleDigestItem<P>,
P: Pair + Send + Sync + 'static,
P::Public: Send + Sync + Hash + Eq + Clone + Decode + Encode + Debug + 'static,
P::Signature: Encode + Decode,
P: AppPair,
AuthorityId<P>: RuntimeAppPublic<Signature=<P as Pair>::Signature>,
<P as Pair>::Signature: Codec + Clone,
T: Send + Sync + 'static,
{
fn verify(
@@ -648,9 +643,9 @@ pub fn import_queue<B, C, P, T>(
C: 'static + ProvideRuntimeApi + BlockOf + ProvideCache<B> + Send + Sync + AuxStore,
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>>,
DigestItemFor<B>: CompatibleDigestItem<P>,
P: Pair + Send + Sync + 'static,
P::Public: Clone + Eq + Send + Sync + Hash + Debug + Encode + Decode,
P::Signature: Encode + Decode,
P: AppPair,
AuthorityId<P>: RuntimeAppPublic<Signature=<P as Pair>::Signature>,
<P as Pair>::Signature: Codec + Clone,
T: Send + Sync + 'static,
{
register_aura_inherent_data_provider(&inherent_data_providers, slot_duration.get())?;
@@ -758,8 +753,8 @@ mod tests {
AuraVerifier {
client,
inherent_data_providers,
transaction_pool: Default::default(),
phantom: Default::default(),
transaction_pool: Default::default(),
}
},
PeersClient::Light(_) => unreachable!("No (yet) tests for light client + Aura"),
3 changes: 2 additions & 1 deletion core/consensus/babe/Cargo.toml
Original file line number Diff line number Diff line change
@@ -20,9 +20,10 @@ inherents = { package = "substrate-inherents", path = "../../inherents" }
substrate-telemetry = { path = "../../telemetry" }
keystore = { package = "substrate-keystore", path = "../../keystore" }
srml-babe = { path = "../../../srml/babe" }

client = { package = "substrate-client", path = "../../client" }
consensus-common = { package = "substrate-consensus-common", path = "../common" }
uncles = { package = "substrate-consensus-uncles", path = "../uncles" }
consensus_common = { package = "substrate-consensus-common", path = "../common" }
slots = { package = "substrate-consensus-slots", path = "../slots" }
sr-primitives = { path = "../../sr-primitives" }
fork-tree = { path = "../../utils/fork-tree" }
20 changes: 11 additions & 9 deletions core/consensus/babe/primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -6,22 +6,24 @@ description = "Primitives for BABE consensus"
edition = "2018"

[dependencies]
substrate-client = { path = "../../../client", default-features = false }
rstd = { package = "sr-std", path = "../../../sr-std", default-features = false }
sr-primitives = { path = "../../../sr-primitives", default-features = false }
app-crypto = { package = "substrate-application-crypto", path = "../../../application-crypto", default-features = false }
slots = { package = "substrate-consensus-slots", path = "../../slots", optional = true }
schnorrkel = { version = "0.8.4", features = ["preaudit_deprecated"], optional = true }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
rstd = { package = "sr-std", path = "../../../sr-std", default-features = false }
schnorrkel = { version = "0.8.4", features = ["preaudit_deprecated"], optional = true }
slots = { package = "substrate-consensus-slots", path = "../../slots", optional = true }
sr-primitives = { path = "../../../sr-primitives", default-features = false }
sr-staking-primitives = { path = "../../../sr-staking-primitives", default-features = false }
substrate-client = { path = "../../../client", default-features = false }

[features]
default = ["std"]
std = [
"rstd/std",
"sr-primitives/std",
"substrate-client/std",
"app-crypto/std",
"codec/std",
"rstd/std",
"schnorrkel",
"slots",
"app-crypto/std",
"sr-primitives/std",
"sr-staking-primitives/std",
"substrate-client/std",
]
57 changes: 37 additions & 20 deletions core/consensus/babe/primitives/src/digest.rs
Original file line number Diff line number Diff line change
@@ -16,20 +16,15 @@

//! Private implementation details of BABE digests.

#[cfg(feature = "std")]
use super::AuthoritySignature;
#[cfg(feature = "std")]
use super::{BABE_ENGINE_ID, Epoch};
#[cfg(not(feature = "std"))]
use super::{VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH};
use super::{AuthorityIndex, BabeBlockWeight, SlotNumber};
#[cfg(feature = "std")]
use sr_primitives::{DigestItem, generic::OpaqueDigestItemId};
use sr_primitives::traits::{Header, DigestItemForHeader};
use codec::{Decode, Encode, Codec};
#[cfg(feature = "std")]
use std::fmt::Debug;
use codec::{Decode, Encode};
#[cfg(feature = "std")]
use codec::{Codec, Input, Error};
use codec::{Input, Error};
#[cfg(feature = "std")]
use schnorrkel::{
SignatureError, errors::MultiSignatureStage,
@@ -93,6 +88,30 @@ impl BabePreDigest {
}
}

/// Get the slot.
pub fn get_slot<H: Header>(header: &H) -> Result<SlotNumber, &str>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation and function naming needs real improvement

where DigestItemForHeader<H>: CompatibleDigestItem,
{
find_pre_digest::<H, RawBabePreDigest>(header)
.map(|raw_pre_digest| raw_pre_digest.slot_number())
}

/// Extract the BABE pre digest from the given header. Pre-runtime digests are
/// mandatory, the function will return `Err` if none is found.
pub fn find_pre_digest<H: Header, D: Codec>(header: &H) -> Result<D, &str>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's strange to return a str with the lifetime of the given header as opposed to a &'static str.

where DigestItemForHeader<H>: CompatibleDigestItem,
{
let mut pre_digest: Option<_> = None;
for log in header.digest().logs() {
match (log.as_babe_pre_digest(), pre_digest.is_some()) {
(Some(_), true) => Err("Multiple BABE pre-runtime digests, rejecting!")?,
(None, _) => {},
(s, false) => pre_digest = s,
}
}
pre_digest.ok_or_else(|| "No BABE pre-runtime digest found")
}

/// The prefix used by BABE for its VRF keys.
pub const BABE_VRF_PREFIX: &'static [u8] = b"substrate-babe-vrf";

@@ -197,43 +216,41 @@ impl Decode for BabePreDigest {
Ok(pre_digest)
}
}

// TODO [slashing] This should use concrete types.
/// A digest item which is usable with BABE consensus.
#[cfg(feature = "std")]
pub trait CompatibleDigestItem: Sized {
/// Construct a digest item which contains a BABE pre-digest.
fn babe_pre_digest(seal: BabePreDigest) -> Self;
fn babe_pre_digest<D: Codec>(seal: D) -> Self;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed? The runtime should be using RawBabePreDigest anyway. This function signature doesn't make a lot of sense, it looks like you can give it anything and it will make a BABE seal which is not the case at all, since you need to give it some specific struct to make a proper BABE seal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it should be a better way to do it. But it also using a non raw pre digest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this really feels wrong.


/// If this item is an BABE pre-digest, return it.
fn as_babe_pre_digest(&self) -> Option<BabePreDigest>;
fn as_babe_pre_digest<D: Codec>(&self) -> Option<D>;

/// Construct a digest item which contains a BABE seal.
fn babe_seal(signature: AuthoritySignature) -> Self;
fn babe_seal<S: Codec>(signature: S) -> Self;

/// If this item is a BABE signature, return the signature.
fn as_babe_seal(&self) -> Option<AuthoritySignature>;
fn as_babe_seal<S: Codec>(&self) -> Option<S>;

/// If this item is a BABE epoch, return it.
fn as_babe_epoch(&self) -> Option<Epoch>;
}

#[cfg(feature = "std")]
impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
Hash: Debug + Send + Sync + Eq + Clone + Codec + 'static
Hash: Send + Sync + Eq + Clone + Codec + 'static
{
fn babe_pre_digest(digest: BabePreDigest) -> Self {
fn babe_pre_digest<D: Codec>(digest: D) -> Self {
DigestItem::PreRuntime(BABE_ENGINE_ID, digest.encode())
}

fn as_babe_pre_digest(&self) -> Option<BabePreDigest> {
fn as_babe_pre_digest<D: Codec>(&self) -> Option<D> {
self.try_to(OpaqueDigestItemId::PreRuntime(&BABE_ENGINE_ID))
}

fn babe_seal(signature: AuthoritySignature) -> Self {
fn babe_seal<S: Codec>(signature: S) -> Self {
DigestItem::Seal(BABE_ENGINE_ID, signature.encode())
}

fn as_babe_seal(&self) -> Option<AuthoritySignature> {
fn as_babe_seal<S: Codec>(&self) -> Option<S> {
self.try_to(OpaqueDigestItemId::Seal(&BABE_ENGINE_ID))
}

18 changes: 12 additions & 6 deletions core/consensus/babe/primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -15,22 +15,23 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Primitives for BABE.
#![deny(warnings)]
#![forbid(unsafe_code, missing_docs, unused_variables, unused_imports)]
// #![deny(warnings)]
// #![forbid(unsafe_code, missing_docs, unused_variables, unused_imports)]
#![cfg_attr(not(feature = "std"), no_std)]

mod digest;

use codec::{Encode, Decode};
use rstd::vec::Vec;
use sr_primitives::ConsensusEngineId;
use sr_primitives::{ConsensusEngineId, EquivocationProof};
use substrate_client::decl_runtime_apis;

#[cfg(feature = "std")]
pub use digest::{BabePreDigest, CompatibleDigestItem};
pub use digest::{BABE_VRF_PREFIX, RawBabePreDigest};
pub use digest::BabePreDigest;
pub use digest::{BABE_VRF_PREFIX, RawBabePreDigest, get_slot, CompatibleDigestItem, find_pre_digest};

mod app {
/// Crypto App module for Babe.
pub mod app {
use app_crypto::{app_crypto, key_types::BABE, sr25519};
app_crypto!(sr25519, BABE);
}
@@ -156,5 +157,10 @@ decl_runtime_apis! {

/// Get the current epoch data for Babe.
fn epoch() -> Epoch;

/// Construct a transaction to report the equivocation.
fn construct_equivocation_transaction(
equivocation: EquivocationProof<Block::Header, AuthorityId, AuthoritySignature>
) -> Option<Vec<u8>>;
}
}
Loading