Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Consensus works with Origin
  • Loading branch information
gavofyork committed Sep 6, 2018
commit 8ba8dc6646540256d8e89d6533ff09ad23ca30c9
27 changes: 14 additions & 13 deletions substrate/runtime/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ use runtime_support::{storage, Parameter};
use runtime_support::dispatch::Result;
use runtime_support::storage::StorageValue;
use runtime_support::storage::unhashed::StorageVec;
use primitives::traits::{MaybeSerializeDebug, MaybeEmpty, OnFinalise, Member, AuthoritiesChangeDigest};
use primitives::traits::{MaybeSerializeDebug, OnFinalise, Member, AuthoritiesChangeDigest};
use primitives::bft::MisbehaviorReport;
use system::{ensure_signed, ensure_inherent, ensure_root};

#[cfg(any(feature = "std", test))]
use substrate_primitives::KeccakHasher;
Expand Down Expand Up @@ -148,12 +149,8 @@ decl_module! {
fn report_misbehavior(aux, report: MisbehaviorReport<T::Hash, T::BlockNumber>) -> Result;
fn note_offline(aux, offline_val_indices: Vec<u32>) -> Result;
fn remark(aux, remark: Vec<u8>) -> Result;
}

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum PrivCall {
fn set_code(new: Vec<u8>) -> Result;
fn set_storage(items: Vec<KeyValue>) -> Result;
fn set_code(aux, new: Vec<u8>) -> Result;
fn set_storage(aux, items: Vec<KeyValue>) -> Result;
}
}

Expand All @@ -164,30 +161,33 @@ impl<T: Trait> Module<T> {
}

/// Set the new code.
fn set_code(new: Vec<u8>) -> Result {
fn set_code(origin: T::Origin, new: Vec<u8>) -> Result {
ensure_root(origin)?;
storage::unhashed::put_raw(CODE, &new);
Ok(())
}

/// Set some items of storage.
fn set_storage(items: Vec<KeyValue>) -> Result {
fn set_storage(origin: T::Origin, items: Vec<KeyValue>) -> Result {
ensure_root(origin)?;
for i in &items {
storage::unhashed::put_raw(&i.0, &i.1);
}
Ok(())
}

/// Report some misbehaviour.
fn report_misbehavior(_aux: T::Origin, _report: MisbehaviorReport<T::Hash, T::BlockNumber>) -> Result {
fn report_misbehavior(origin: T::Origin, _report: MisbehaviorReport<T::Hash, T::BlockNumber>) -> Result {
ensure_signed(origin)?;
// TODO.
Ok(())
}

/// Note the previous block's validator missed their opportunity to propose a block. This only comes in
/// if 2/3+1 of the validators agree that no proposal was submitted. It's only relevant
/// for the previous block.
fn note_offline(aux: T::Origin, offline_val_indices: Vec<u32>) -> Result {
assert!(aux.is_empty());
fn note_offline(origin: T::Origin, offline_val_indices: Vec<u32>) -> Result {
ensure_inherent(origin)?;
assert!(
<system::Module<T>>::extrinsic_index() == Some(T::NOTE_OFFLINE_POSITION),
"note_offline extrinsic must be at position {} in the block",
Expand All @@ -202,7 +202,8 @@ impl<T: Trait> Module<T> {
}

/// Make some on-chain remark.
fn remark(_aux: T::Origin, _remark: Vec<u8>) -> Result {
fn remark(origin: T::Origin, _remark: Vec<u8>) -> Result {
ensure_signed(origin)?;
Ok(())
}

Expand Down
9 changes: 9 additions & 0 deletions substrate/runtime/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ pub fn ensure_root<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), &'stati
}
}

pub fn ensure_inherent<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), &'static str>
where OuterOrigin: Into<Option<RawOrigin<AccountId>>>
{
match o.into() {
Some(RawOrigin::Inherent) => Ok(()),
_ => Err("bad origin"),
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we leave a comment to each of this ensure_ functions?


impl<T: Trait> Module<T> {
/// Start the execution of a particular block.
pub fn initialise(number: &T::BlockNumber, parent_hash: &T::Hash, txs_root: &T::Hash) {
Expand Down