-
Notifications
You must be signed in to change notification settings - Fork 2.7k
DigestItem trait #636
DigestItem trait #636
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,7 @@ extern crate demo_primitives; | |
use rstd::prelude::*; | ||
use demo_primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, SessionKey, Signature}; | ||
use runtime_primitives::generic; | ||
use runtime_primitives::traits::{Convert, HasPublicAux, BlakeTwo256}; | ||
use runtime_primitives::traits::{Convert, HasPublicAux, BlakeTwo256, Identity, DigestItem, AuthoritiesChangeDigest}; | ||
use version::RuntimeVersion; | ||
|
||
#[cfg(any(feature = "std", test))] | ||
|
@@ -94,9 +94,9 @@ impl system::Trait for Concrete { | |
type BlockNumber = BlockNumber; | ||
type Hash = Hash; | ||
type Hashing = BlakeTwo256; | ||
type Digest = generic::Digest<Vec<u8>>; | ||
type Digest = generic::Digest<ConcreteLog>; | ||
type AccountId = AccountId; | ||
type Header = generic::Header<BlockNumber, BlakeTwo256, Vec<u8>>; | ||
type Header = generic::Header<BlockNumber, BlakeTwo256, ConcreteLog>; | ||
type Event = Event; | ||
} | ||
|
||
|
@@ -118,6 +118,7 @@ impl consensus::Trait for Concrete { | |
const NOTE_OFFLINE_POSITION: u32 = 1; | ||
type SessionKey = SessionKey; | ||
type OnOfflineValidator = Staking; | ||
type ConvertSessionKeyToAuthorityId = Identity; | ||
} | ||
|
||
/// Consensus module for this concrete runtime. | ||
|
@@ -170,6 +171,42 @@ pub type Council = council::Module<Concrete>; | |
/// Council voting module for this concrete runtime. | ||
pub type CouncilVoting = council::voting::Module<Concrete>; | ||
|
||
/// Concrete log type. | ||
#[derive(Clone, PartialEq, Eq, Encode, Decode)] | ||
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))] | ||
pub enum ConcreteLog { | ||
/// Authority changes log. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more information. describe specifically under what circumstances this item is used as opposed to any other. |
||
AuthorityChanges(Vec<SessionKey>), | ||
} | ||
|
||
impl Default for ConcreteLog { | ||
fn default() -> Self { | ||
unreachable!("shouldn't be called ever") | ||
} | ||
} | ||
|
||
impl DigestItem for ConcreteLog { | ||
type AuthoritiesChange = Vec<SessionKey>; | ||
|
||
fn as_authorities_change(&self) -> Option<&Self::AuthoritiesChange> { | ||
match self { | ||
ConcreteLog::AuthorityChanges(ref authorities_change) => Some(authorities_change), | ||
} | ||
} | ||
} | ||
|
||
impl AuthoritiesChangeDigest<ConcreteLog> for Vec<SessionKey> { | ||
type AuthorityId = SessionKey; | ||
|
||
fn new(authorities: Vec<SessionKey>) -> Option<ConcreteLog> { | ||
Some(ConcreteLog::AuthorityChanges(authorities)) | ||
} | ||
|
||
fn authorities(&self) -> &[SessionKey] { | ||
&self[..] | ||
} | ||
} | ||
|
||
impl_outer_event! { | ||
pub enum Event for Concrete { | ||
balances, session, staking | ||
|
@@ -206,7 +243,7 @@ impl_outer_dispatch! { | |
/// The address format for describing accounts. | ||
pub type Address = balances::Address<Concrete>; | ||
/// Block header type as expected by this runtime. | ||
pub type Header = generic::Header<BlockNumber, BlakeTwo256, Vec<u8>>; | ||
pub type Header = generic::Header<BlockNumber, BlakeTwo256, ConcreteLog>; | ||
/// Block type as expected by this runtime. | ||
pub type Block = generic::Block<Header, UncheckedExtrinsic>; | ||
/// BlockId type as expected by this runtime. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,10 +39,11 @@ extern crate substrate_runtime_system as system; | |
extern crate substrate_primitives; | ||
|
||
use rstd::prelude::*; | ||
use runtime_support::{storage, Parameter}; | ||
use runtime_support::{storage, StorageValue, Parameter}; | ||
use runtime_support::dispatch::Result; | ||
use runtime_support::storage::unhashed::StorageVec; | ||
use primitives::traits::{MaybeSerializeDebug, MaybeEmpty}; | ||
use primitives::traits::{MaybeSerializeDebug, MaybeEmpty, Convert, | ||
Executable, DigestItem, AuthoritiesChangeDigest}; | ||
use primitives::bft::MisbehaviorReport; | ||
|
||
#[cfg(any(feature = "std", test))] | ||
|
@@ -77,6 +78,9 @@ pub trait Trait: system::Trait { | |
|
||
type SessionKey: Parameter + Default + MaybeSerializeDebug; | ||
type OnOfflineValidator: OnOfflineValidator; | ||
type ConvertSessionKeyToAuthorityId: Convert<Self::SessionKey, | ||
<<system::DigestItemFor<Self> as DigestItem>::AuthoritiesChange as | ||
AuthoritiesChangeDigest<system::DigestItemFor<Self>>>::AuthorityId>; | ||
} | ||
|
||
decl_module! { | ||
|
@@ -96,6 +100,14 @@ decl_module! { | |
} | ||
} | ||
|
||
decl_storage! { | ||
trait Store for Module<T: Trait> as Consensus { | ||
// Authorities set actual at the block execution start. IsSome only if | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// the set has been changed. | ||
SavedAuthorities get(saved_authorities): default Vec<T::SessionKey>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider rename to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also consider removing |
||
} | ||
} | ||
|
||
impl<T: Trait> Module<T> { | ||
/// Get the current set of authorities. These are the session keys. | ||
pub fn authorities() -> Vec<T::SessionKey> { | ||
|
@@ -149,7 +161,15 @@ impl<T: Trait> Module<T> { | |
/// | ||
/// Called by `next_session` only. | ||
pub fn set_authorities(authorities: &[T::SessionKey]) { | ||
AuthorityStorageVec::<T::SessionKey>::set_items(authorities); | ||
let previous_authorities = AuthorityStorageVec::<T::SessionKey>::items(); | ||
if previous_authorities != authorities { | ||
let saved_authorities = Self::saved_authorities(); | ||
if saved_authorities.is_empty() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would then become |
||
<SavedAuthorities<T>>::put(previous_authorities); | ||
} | ||
|
||
AuthorityStorageVec::<T::SessionKey>::set_items(authorities); | ||
} | ||
} | ||
|
||
/// Set a single authority by index. | ||
|
@@ -158,6 +178,15 @@ impl<T: Trait> Module<T> { | |
} | ||
} | ||
|
||
/// Finalization hook for the consensus module. | ||
impl<T: Trait> Executable for Module<T> { | ||
fn execute() { | ||
let _saved_authorities = <SavedAuthorities<T>>::take(); | ||
|
||
// TODO: call deposit_log for saved_authorities | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "std", test))] | ||
#[derive(Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -322,13 +322,6 @@ impl<T> MaybeDisplay for T {} | |
pub trait Member: Send + Sync + Sized + MaybeSerializeDebug + Eq + PartialEq + Clone + 'static {} | ||
impl<T: Send + Sync + Sized + MaybeSerializeDebug + Eq + PartialEq + Clone + 'static> Member for T {} | ||
|
||
/// Something that acts like a `Digest` - it can have `Log`s `push`ed onto it and these `Log`s are | ||
/// each `Codec`. | ||
pub trait Digest { | ||
type Item: Member; | ||
fn push(&mut self, item: Self::Item); | ||
} | ||
|
||
/// Something which fulfills the abstract idea of a Substrate header. It has types for a `Number`, | ||
/// a `Hash` and a `Digest`. It provides access to an `extrinsics_root`, `state_root` and | ||
/// `parent_hash`, as well as a `digest` and a block `number`. | ||
|
@@ -434,3 +427,46 @@ pub trait Applyable: Sized + Send + Sync { | |
fn sender(&self) -> &Self::AccountId; | ||
fn apply(self) -> Result<(), &'static str>; | ||
} | ||
|
||
/// Something that acts like a `Digest` - it can have `Log`s `push`ed onto it and these `Log`s are | ||
/// each `Codec`. | ||
pub trait Digest { | ||
type Item: DigestItem; | ||
|
||
fn push(&mut self, item: Self::Item); | ||
} | ||
|
||
/// Single digest item. | ||
pub trait DigestItem: Member { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider copying the |
||
/// Type of authorities change log entry. | ||
type AuthoritiesChange: AuthoritiesChangeDigest<Self>; // TODO: = StubDigestItem when associated type defaults are stabilized | ||
|
||
/// Returns Some if the entry is the 'authorities change' log entry. | ||
fn as_authorities_change(&self) -> Option<&Self::AuthoritiesChange> { | ||
None | ||
} | ||
} | ||
|
||
/// Authorities change digest item. | ||
pub trait AuthoritiesChangeDigest<I: DigestItem> { | ||
/// Type of authority Id. | ||
type AuthorityId: Member; | ||
|
||
/// Make new authorities change log entry. | ||
fn new(authorities: Vec<Self::AuthorityId>) -> Option<I>; | ||
|
||
/// Get new authorities set. | ||
fn authorities(&self) -> &[Self::AuthorityId]; | ||
} | ||
|
||
/// Empty digest item that is never created and used. | ||
/// | ||
/// Should be used as a stub for items that are not supported by runtimes. | ||
pub struct StubDigestItem; | ||
|
||
impl<I: DigestItem> AuthoritiesChangeDigest<I> for StubDigestItem { | ||
type AuthorityId = (); | ||
|
||
fn new(_authorities: Vec<Self::AuthorityId>) -> Option<I> { None } | ||
fn authorities(&self) -> &[Self::AuthorityId] { unreachable!("StubDigestItem is never created") } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,7 +293,7 @@ mod tests { | |
use runtime_io::with_externalities; | ||
use substrate_primitives::{H256, KeccakHasher}; | ||
use primitives::BuildStorage; | ||
use primitives::traits::{HasPublicAux, Identity, BlakeTwo256}; | ||
use primitives::traits::{HasPublicAux, Empty, Identity, BlakeTwo256}; | ||
use primitives::testing::{Digest, Header}; | ||
|
||
#[derive(Clone, Eq, PartialEq)] | ||
|
@@ -305,6 +305,7 @@ mod tests { | |
const NOTE_OFFLINE_POSITION: u32 = 1; | ||
type SessionKey = u64; | ||
type OnOfflineValidator = (); | ||
type ConvertSessionKeyToAuthorityId = Empty; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
} | ||
impl system::Trait for Test { | ||
type PublicAux = <Self as HasPublicAux>::PublicAux; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could do with more information. consider what it is in relation to other types and why it exists as a type at all.