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

Commit

Permalink
New extrinsic dispatch model (#678)
Browse files Browse the repository at this point in the history
* System and Balances switched to new call model

* Fixed up executive for new pardigm

* Consensus works with Origin

* Timestamp ported over

* Session now compatible

* Ported staking

* Treasury compatible

* Expunge MaybeEmpty

* Make democracy work

* Council compatible

* Remove some aux-dispatch stuff and fix balances

* Rename aux -> origin, fix contracts

* Fix test

* Fix example

* Update macro to remove superfluous cruft

* Make work with new macro

* Tests

* Fix some tests

* Tests

* Fix grumbles

* Final grumble
  • Loading branch information
gavofyork authored Sep 7, 2018
1 parent f19a0d7 commit da05763
Show file tree
Hide file tree
Showing 46 changed files with 1,752 additions and 1,917 deletions.
4 changes: 0 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 38 additions & 35 deletions demo/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ mod tests {
use primitives::{twox_128, KeccakHasher};
use demo_primitives::{Hash, BlockNumber, AccountId};
use runtime_primitives::traits::Header as HeaderT;
use runtime_primitives::{ApplyOutcome, ApplyError, ApplyResult, MaybeUnsigned};
use runtime_primitives::{ApplyOutcome, ApplyError, ApplyResult};
use {balances, staking, session, system, consensus};
use system::{EventRecord, Phase};
use demo_runtime::{Header, Block, UncheckedExtrinsic, Extrinsic, Call, Runtime, Balances,
BuildStorage, GenesisConfig, BalancesConfig, SessionConfig, StakingConfig, BareExtrinsic, System, Event};
use demo_runtime::{Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances,
BuildStorage, GenesisConfig, BalancesConfig, SessionConfig, StakingConfig, System, Event};
use ed25519::{Public, Pair};

const BLOATY_CODE: &[u8] = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm");
Expand All @@ -77,20 +77,32 @@ mod tests {
AccountId::from(Keyring::Bob.to_raw_public())
}

fn sign(xt: CheckedExtrinsic) -> UncheckedExtrinsic {
match xt.signed {
Some(signed) => {
let payload = (xt.index, xt.function);
let pair = Pair::from(Keyring::from_public(Public::from_raw(signed.clone().into())).unwrap());
let signature = pair.sign(&payload.encode()).into();
UncheckedExtrinsic {
signature: Some((balances::address::Address::Id(signed), signature)),
index: payload.0,
function: payload.1,
}
}
None => UncheckedExtrinsic {
signature: None,
index: xt.index,
function: xt.function,
},
}
}

fn xt() -> UncheckedExtrinsic {
let extrinsic = BareExtrinsic {
signed: alice(),
sign(CheckedExtrinsic {
signed: Some(alice()),
index: 0,
function: Call::Balances(balances::Call::transfer::<Runtime>(bob().into(), 69)),
};
let signature = MaybeUnsigned(Keyring::from_raw_public(extrinsic.signed.0.clone()).unwrap()
.sign(&extrinsic.encode()).into());
let extrinsic = Extrinsic {
signed: extrinsic.signed.into(),
index: extrinsic.index,
function: extrinsic.function,
};
UncheckedExtrinsic::new(extrinsic, signature)
})
}

fn from_block_number(n: u64) -> Header {
Expand Down Expand Up @@ -225,19 +237,10 @@ mod tests {
}.build_storage().unwrap().into()
}

fn construct_block(number: BlockNumber, parent_hash: Hash, state_root: Hash, extrinsics: Vec<BareExtrinsic>) -> (Vec<u8>, Hash) {
fn construct_block(number: BlockNumber, parent_hash: Hash, state_root: Hash, extrinsics: Vec<CheckedExtrinsic>) -> (Vec<u8>, Hash) {
use triehash::ordered_trie_root;

let extrinsics = extrinsics.into_iter().map(|extrinsic| {
let signature = MaybeUnsigned(Pair::from(Keyring::from_public(Public::from_raw(extrinsic.signed.0.clone())).unwrap())
.sign(&extrinsic.encode()).into());
let extrinsic = Extrinsic {
signed: extrinsic.signed.into(),
index: extrinsic.index,
function: extrinsic.function,
};
UncheckedExtrinsic::new(extrinsic, signature)
}).collect::<Vec<_>>();
let extrinsics = extrinsics.into_iter().map(sign).collect::<Vec<_>>();

let extrinsics_root = ordered_trie_root::<KeccakHasher, _, _>(extrinsics.iter().map(Encode::encode)).0.into();

Expand All @@ -261,8 +264,8 @@ mod tests {
// hex!("3437bf4b182ab17bb322af5c67e55f6be487a77084ad2b4e27ddac7242e4ad21").into(),
// Keccak
hex!("508a68a0918f614b86b2ccfd0975754f6d2abe1026a34e42d6d8d5abdf4db010").into(),
vec![BareExtrinsic {
signed: alice(),
vec![CheckedExtrinsic {
signed: Some(alice()),
index: 0,
function: Call::Balances(balances::Call::transfer(bob().into(), 69)),
}]
Expand All @@ -276,15 +279,15 @@ mod tests {
// Blake
// hex!("741fcb660e6fa9f625fbcd993b49f6c1cc4040f5e0cc8727afdedf11fd3c464b").into(),
// Keccak
hex!("171f1b2c01c9c616e40ee2d842a699286b50a5a74874b56d826094dadedffb27").into(),
hex!("a72ec570c7642d9ad06ef0e5dd37be65fb04b71e0ab52b3927d760ed6c777a1f").into(),
vec![
BareExtrinsic {
signed: bob(),
CheckedExtrinsic {
signed: Some(bob()),
index: 0,
function: Call::Balances(balances::Call::transfer(alice().into(), 5)),
},
BareExtrinsic {
signed: alice(),
CheckedExtrinsic {
signed: Some(alice()),
index: 1,
function: Call::Balances(balances::Call::transfer(bob().into(), 15)),
}
Expand All @@ -300,10 +303,10 @@ mod tests {
// hex!("2c7231a9c210a7aa4bea169d944bc4aaacd517862b244b8021236ffa7f697991").into(),
// Keccak
hex!("e45221804da3a3609454d4e09debe6364cc6af63c2ff067d802d1af62fea32ae").into(),
vec![BareExtrinsic {
signed: alice(),
vec![CheckedExtrinsic {
signed: Some(alice()),
index: 0,
function: Call::Consensus(consensus::Call::remark(vec![0; 60000])),
function: Call::Consensus(consensus::Call::remark(vec![0; 60000000])),
}]
)
}
Expand Down
2 changes: 1 addition & 1 deletion demo/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ pub type Index = u64;
pub type Hash = primitives::H256;

/// Alias to 512-bit hash when used in the context of a signature on the relay chain.
pub type Signature = runtime_primitives::MaybeUnsigned<runtime_primitives::Ed25519Signature>;
pub type Signature = runtime_primitives::Ed25519Signature;
78 changes: 30 additions & 48 deletions demo/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub use runtime_primitives::BuildStorage;
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
/// Runtime runtime type used to parameterize the various modules.
/// Runtime type used to collate and parameterize the various modules.
pub struct Runtime;

/// Runtime version.
Expand All @@ -76,15 +76,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 0,
};

/// Version module for this concrete runtime.
pub type Version = version::Module<Runtime>;

impl version::Trait for Runtime {
const VERSION: RuntimeVersion = VERSION;
}

impl system::Trait for Runtime {
type PublicAux = Self::AccountId;
type Origin = Origin;
type Index = Index;
type BlockNumber = BlockNumber;
type Hash = Hash;
Expand Down Expand Up @@ -154,7 +147,7 @@ impl staking::Trait for Runtime {
pub type Staking = staking::Module<Runtime>;

impl democracy::Trait for Runtime {
type Proposal = PrivCall;
type Proposal = Call;
}

/// Democracy module for this concrete runtime.
Expand All @@ -179,20 +172,13 @@ impl_outer_log! {
}
}

impl DigestItem for Log {
type AuthoritiesChange = consensus::AuthoritiesChange<SessionKey>;

fn as_authorities_change(&self) -> Option<&Self::AuthoritiesChange> {
match *self {
Log::consensus(ref item) => item.as_authorities_change(),
}
impl_outer_origin! {
pub enum Origin for Runtime {
}
}

impl_outer_dispatch! {
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub enum Call where aux: <Runtime as system::Trait>::PublicAux {
pub enum Call where origin: Origin {
Consensus,
Balances,
Session,
Expand All @@ -202,17 +188,28 @@ impl_outer_dispatch! {
Council,
CouncilVoting,
}
}

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub enum PrivCall {
Consensus,
Balances,
Session,
Staking,
Democracy,
Council,
CouncilVoting,
impl_outer_config! {
pub struct GenesisConfig for Runtime {
ConsensusConfig => consensus,
SystemConfig => system,
BalancesConfig => balances,
SessionConfig => session,
StakingConfig => staking,
DemocracyConfig => democracy,
CouncilConfig => council,
TimestampConfig => timestamp,
}
}

impl DigestItem for Log {
type AuthoritiesChange = consensus::AuthoritiesChange<SessionKey>;

fn as_authorities_change(&self) -> Option<&Self::AuthoritiesChange> {
match *self {
Log::consensus(ref item) => item.as_authorities_change(),
}
}
}

Expand All @@ -226,30 +223,15 @@ pub type Block = generic::Block<Header, UncheckedExtrinsic>;
pub type BlockId = generic::BlockId<Block>;
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Index, Call, Signature>;
/// Extrinsic type as expected by this runtime. This is not the type that is signed.
pub type Extrinsic = generic::Extrinsic<Address, Index, Call>;
/// Extrinsic type that is signed.
pub type BareExtrinsic = generic::Extrinsic<AccountId, Index, Call>;
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Index, Call>;
/// Executive: handles dispatch to the various modules.
pub type Executive = executive::Executive<Runtime, Block, Balances, Balances,
(((((), Council), Democracy), Staking), Session)>;

impl_outer_config! {
pub struct GenesisConfig for Runtime {
ConsensusConfig => consensus,
SystemConfig => system,
BalancesConfig => balances,
SessionConfig => session,
StakingConfig => staking,
DemocracyConfig => democracy,
CouncilConfig => council,
TimestampConfig => timestamp,
}
}

pub mod api {
impl_stubs!(
version => |()| super::Version::version(),
version => |()| super::VERSION,
authorities => |()| super::Consensus::authorities(),
events => |()| super::System::events(),
initialise_block => |header| super::Executive::initialise_block(&header),
Expand Down
1 change: 0 additions & 1 deletion demo/runtime/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit da05763

Please sign in to comment.