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

Modularised dispatch #95

Merged
merged 17 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

50 changes: 29 additions & 21 deletions demo/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ mod tests {
use codec::{KeyedVec, Slicable, Joiner};
use keyring::Keyring::{self, Alice, Bob};
use runtime_support::Hashable;
use demo_runtime::runtime::staking::{self, balance, BALANCE_OF};
use state_machine::{CodeExecutor, TestExternalities};
use primitives::twox_128;
use demo_primitives::{Hash, Header, BlockNumber, Block, Digest, Transaction,
UncheckedTransaction, Function};
use demo_primitives::{Hash, Header, BlockNumber, Digest};
use demo_runtime::transaction::{Transaction, UncheckedTransaction};
use demo_runtime::block::Block;
use demo_runtime::runtime::staking::{self, balance, BALANCE_OF};
use demo_runtime::dispatch;
use ed25519::{Public, Pair};

const BLOATY_CODE: &[u8] = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm");
Expand All @@ -62,7 +64,7 @@ mod tests {
let transaction = Transaction {
signed: Alice.into(),
nonce: 0,
function: Function::StakingTransfer(Bob.into(), 69),
function: dispatch::PubCall::Staking(staking::public::Call::transfer(Bob.into(), 69)),
};
let signature = Keyring::from_raw_public(transaction.signed).unwrap()
.sign(&transaction.encode());
Expand All @@ -73,7 +75,8 @@ mod tests {
#[test]
fn panic_execution_with_foreign_code_gives_error() {
let mut t: TestExternalities = map![
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(staking::TRANSACTION_FEE).to_vec() => vec![0u8; 8]
];

let r = Executor::new().call(&mut t, BLOATY_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
Expand All @@ -83,7 +86,8 @@ mod tests {
#[test]
fn panic_execution_with_native_equivalent_code_gives_error() {
let mut t: TestExternalities = map![
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(staking::TRANSACTION_FEE).to_vec() => vec![0u8; 8]
];

let r = Executor::new().call(&mut t, COMPACT_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
Expand All @@ -93,7 +97,8 @@ mod tests {
#[test]
fn successful_execution_with_native_equivalent_code_gives_ok() {
let mut t: TestExternalities = map![
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(staking::TRANSACTION_FEE).to_vec() => vec![0u8; 8]
];

let r = Executor::new().call(&mut t, COMPACT_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
Expand All @@ -108,7 +113,8 @@ mod tests {
#[test]
fn successful_execution_with_foreign_code_gives_ok() {
let mut t: TestExternalities = map![
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(staking::TRANSACTION_FEE).to_vec() => vec![0u8; 8]
];

let r = Executor::new().call(&mut t, BLOATY_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
Expand Down Expand Up @@ -152,11 +158,11 @@ mod tests {
construct_block(
1,
[69u8; 32].into(),
hex!("970ae19447bef129c88ee80c72797fa9dfeda4ca1a26d10102b669d776eb0ccf").into(),
hex!("cfb76a83e40aa6a0d3f92255e6229e74808cae31d9f46053f31129b797540d03").into(),
vec![Transaction {
signed: Alice.into(),
nonce: 0,
function: Function::StakingTransfer(Bob.into(), 69),
function: dispatch::PubCall::Staking(staking::public::Call::transfer(Bob.into(), 69)),
}]
)
}
Expand All @@ -165,17 +171,17 @@ mod tests {
construct_block(
2,
block1().1,
hex!("347ece6ef0d193bd7c2bfbda17706b82eb24c0965f415784a44b138f0df034cd").into(),
hex!("c713bd003e303648e8d904bcfa44084865c9b70c398547e678028cc7cf60907f").into(),
vec![
Transaction {
signed: Bob.into(),
nonce: 0,
function: Function::StakingTransfer(Alice.into(), 5),
function: dispatch::PubCall::Staking(staking::public::Call::transfer(Alice.into(), 5)),
},
Transaction {
signed: Alice.into(),
nonce: 1,
function: Function::StakingTransfer(Bob.into(), 15),
function: dispatch::PubCall::Staking(staking::public::Call::transfer(Bob.into(), 15)),
}
]
)
Expand All @@ -188,15 +194,15 @@ mod tests {
Executor::new().call(&mut t, COMPACT_CODE, "execute_block", &block1().0).unwrap();

runtime_io::with_externalities(&mut t, || {
assert_eq!(balance(&Alice), 42);
assert_eq!(balance(&Alice), 41);
Copy link
Member

Choose a reason for hiding this comment

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

Just wondering why have these changed?

Copy link
Member Author

Choose a reason for hiding this comment

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

there's a transaction fee now.

assert_eq!(balance(&Bob), 69);
});

Executor::new().call(&mut t, COMPACT_CODE, "execute_block", &block2().0).unwrap();

runtime_io::with_externalities(&mut t, || {
assert_eq!(balance(&Alice), 32);
assert_eq!(balance(&Bob), 79);
assert_eq!(balance(&Alice), 30);
assert_eq!(balance(&Bob), 78);
});
}

Expand All @@ -207,22 +213,23 @@ mod tests {
WasmExecutor.call(&mut t, COMPACT_CODE, "execute_block", &block1().0).unwrap();

runtime_io::with_externalities(&mut t, || {
assert_eq!(balance(&Alice), 42);
assert_eq!(balance(&Alice), 41);
assert_eq!(balance(&Bob), 69);
});

WasmExecutor.call(&mut t, COMPACT_CODE, "execute_block", &block2().0).unwrap();

runtime_io::with_externalities(&mut t, || {
assert_eq!(balance(&Alice), 32);
assert_eq!(balance(&Bob), 79);
assert_eq!(balance(&Alice), 30);
assert_eq!(balance(&Bob), 78);
});
}

#[test]
fn panic_execution_gives_error() {
let mut t: TestExternalities = map![
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(staking::TRANSACTION_FEE).to_vec() => vec![0u8; 8]
];

let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm");
Expand All @@ -233,7 +240,8 @@ mod tests {
#[test]
fn successful_execution_gives_ok() {
let mut t: TestExternalities = map![
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
twox_128(&Alice.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(staking::TRANSACTION_FEE).to_vec() => vec![0u8; 8]
];

let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm");
Expand Down
33 changes: 0 additions & 33 deletions demo/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use primitives::bytes;
use primitives::H256;
use rstd::vec::Vec;
use codec::{Input, Slicable};
use transaction::UncheckedTransaction;

pub use primitives::block::Id;

Expand All @@ -31,9 +30,6 @@ pub type Number = u64;
/// Hash used to refer to a block hash.
pub type HeaderHash = H256;

/// Hash used to refer to a transaction hash.
pub type TransactionHash = H256;

/// Execution log (event)
#[derive(PartialEq, Eq, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
Expand Down Expand Up @@ -69,35 +65,6 @@ impl Slicable for Digest {
}
}

/// The block "body": A bunch of transactions.
pub type Body = Vec<UncheckedTransaction>;

/// A block on the chain.
#[derive(PartialEq, Eq, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct Block {
/// The block header.
pub header: Header,
/// All relay-chain transactions.
pub transactions: Body,
}

impl Slicable for Block {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
let (header, transactions) = try_opt!(Slicable::decode(input));
Some(Block { header, transactions })
}

fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();

v.extend(self.header.encode());
v.extend(self.transactions.encode());

v
}
}

/// Header for a block.
#[derive(PartialEq, Eq, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
Expand Down
4 changes: 1 addition & 3 deletions demo/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ macro_rules! try_opt {
}

pub mod block;
pub mod transaction;

pub use self::block::{Header, Block, Log, Digest};
pub use self::block::{Header, Log, Digest};
pub use self::block::Number as BlockNumber;
pub use self::transaction::{Transaction, UncheckedTransaction, Function, Proposal, VoteThreshold};

/// Alias to Ed25519 pubkey that identifies an account on the relay chain. This will almost
/// certainly continue to be the same as the substrate's `AuthorityId`.
Expand Down
Loading