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

Make substrate generic #169

Merged
merged 63 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
7fa1b17
Some initial work on RPC and client
gavofyork May 17, 2018
a43c5e7
Rephrase as params
gavofyork May 17, 2018
5348d4e
More work on traitifying substrate.
gavofyork May 18, 2018
0679ac6
Traitify in_mem.rs
gavofyork May 23, 2018
8b9b4ac
traitify client.rs
gavofyork May 24, 2018
047b294
Make new primitives (mainly traits) build again.
gavofyork May 24, 2018
f94f63e
Many (superficial) build fixes throughout.
gavofyork May 24, 2018
0fe8997
Fix remaining build issues up to bft interface.
gavofyork May 24, 2018
9f0113b
Make bft primitives be generic.
gavofyork May 24, 2018
967e478
Switch out MisBehaviorReport for generic version.
gavofyork May 24, 2018
0e1aa2e
Merge Hashing into Header.
gavofyork May 25, 2018
e508b8e
Update runtime for new generics (with Hashing).
gavofyork May 25, 2018
3ad0410
Update demo runtime.
gavofyork May 25, 2018
d2b8a35
Make runtime compile.
gavofyork May 25, 2018
5a88df4
Build fixes for runtime
gavofyork May 25, 2018
2f8bdd7
Remove old modules.
gavofyork May 25, 2018
d268ca0
port substrate-bft to use generic substrate types
rphmeier May 25, 2018
647aa7e
port client
rphmeier May 25, 2018
fedbaba
port substrate-test-runtime
rphmeier May 29, 2018
089367d
mostly port test-runtime to get compiling for std
rphmeier May 29, 2018
2a02f53
Ensure `AccountId` has a `Default`.
gavofyork May 29, 2018
33bc80f
Fix type deps.
gavofyork May 29, 2018
096150a
finish porting
rphmeier May 29, 2018
4e49ef7
initialize test_runtime from genesis correctly
rphmeier May 29, 2018
85a818f
remove commented code
rphmeier May 29, 2018
baf292e
Merge branch 'master' into gav-make-substrate-generic
rphmeier May 30, 2018
bf11a29
maybe unsigned signatures
rphmeier May 30, 2018
b1379de
runtimes compile
rphmeier May 30, 2018
65c84dd
port over most of network
rphmeier May 31, 2018
ea361d2
serialization for generic types
rphmeier May 31, 2018
e35a8d0
fix comment
rphmeier May 31, 2018
c8f064e
remove some unnecessary trait bounds
rphmeier May 31, 2018
34b4e0e
network compiles
rphmeier May 31, 2018
5ad3877
tests compile for sync
rphmeier Jun 1, 2018
b3cbe4f
fix deserialization
rphmeier Jun 1, 2018
1b7901b
Merge branch 'master' into gav-make-substrate-generic
rphmeier Jun 1, 2018
a36f328
temporarily remove deserialize derives
rphmeier Jun 1, 2018
d3ee03e
workarounds for serde issues for deriving deserialization
rphmeier Jun 1, 2018
c158c16
get demo-runtime compiling on std
rphmeier Jun 1, 2018
91409b6
port extrinsic-pool
rphmeier Jun 2, 2018
88629b3
primitives reshuffling
rphmeier Jun 2, 2018
defce82
get network compiling again
rphmeier Jun 2, 2018
4ec3670
remove debugging file
rphmeier Jun 2, 2018
d6bb4ea
runtime tests now passing
rphmeier Jun 2, 2018
f14b975
port client-db
rphmeier Jun 2, 2018
d01da87
start to port over substrate-rpc
rphmeier Jun 2, 2018
354de81
mostly port over PolkadotApi
rphmeier Jun 4, 2018
64accdd
test_runtime follows normal conventions
rphmeier Jun 4, 2018
0cf5f15
substrate runtime tests pass
rphmeier Jun 4, 2018
7d8272f
deal with inherent extrinsics correctly in polkadot-api
rphmeier Jun 4, 2018
13532f7
port transaction-pool
rphmeier Jun 5, 2018
ff852c8
port polkadot-consensus
rphmeier Jun 5, 2018
ddf5a5d
port substrate-rpc
rphmeier Jun 5, 2018
36c0b8e
everything compiles
rphmeier Jun 5, 2018
621be34
tests compile
rphmeier Jun 5, 2018
3bc9390
Merge branch 'master' into gav-make-substrate-generic
rphmeier Jun 5, 2018
a10bc64
fix grumbles
rphmeier Jun 6, 2018
5204fe4
test-runtime uses its own transfer type
rphmeier Jun 6, 2018
d9a87f9
switch to master branch of jsonrpc
rphmeier Jun 6, 2018
cb35573
fix network tests and some warnings
rphmeier Jun 6, 2018
ef3ec86
all tests pass locally
rphmeier Jun 6, 2018
40531bb
[ci-skip] add another comment about issue
rphmeier Jun 6, 2018
ccc65f7
remove some curlies
rphmeier Jun 6, 2018
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,250 changes: 697 additions & 553 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions demo/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,18 @@ pub mod error;

use std::sync::Arc;
use client::genesis;
use codec::Slicable;
use demo_primitives::Hash;
use demo_runtime::{GenesisConfig, ConsensusConfig, CouncilConfig, DemocracyConfig,
SessionConfig, StakingConfig, BuildExternalities};
use demo_runtime::{Block, Header, UncheckedExtrinsic};
use futures::{Future, Sink, Stream};

struct DummyPool;
impl extrinsic_pool::api::ExtrinsicPool for DummyPool {
impl extrinsic_pool::api::ExtrinsicPool<UncheckedExtrinsic, Hash> for DummyPool {
type Error = extrinsic_pool::txpool::Error;

fn submit(&self, _: Vec<primitives::block::Extrinsic>)
-> Result<Vec<primitives::block::ExtrinsicHash>, Self::Error>
fn submit(&self, _: Vec<UncheckedExtrinsic>)
-> Result<Vec<Hash>, Self::Error>
{
Err("unimplemented".into())
}
Expand Down Expand Up @@ -102,8 +103,8 @@ pub fn run<I, T>(args: I) -> error::Result<()> where

struct GenesisBuilder;

impl client::GenesisBuilder for GenesisBuilder {
fn build(self) -> (primitives::Header, Vec<(Vec<u8>, Vec<u8>)>) {
impl client::GenesisBuilder<Block> for GenesisBuilder {
fn build(self) -> (Header, Vec<(Vec<u8>, Vec<u8>)>) {
let god_key = hex!["3d866ec8a9190c8343c2fc593d21d8a6d0c5c4763aaab2349de3a6111d64d124"];
let genesis_config = GenesisConfig {
consensus: Some(ConsensusConfig {
Expand All @@ -113,15 +114,15 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
system: None,
// block_time: 5, // 5 second block time.
session: Some(SessionConfig {
validators: vec![god_key.clone()],
validators: vec![god_key.clone().into()],
session_length: 720, // that's 1 hour per session.
}),
staking: Some(StakingConfig {
current_era: 0,
intentions: vec![],
transaction_base_fee: 100,
transaction_byte_fee: 1,
balances: vec![(god_key.clone(), 1u64 << 63)].into_iter().collect(),
balances: vec![(god_key.clone().into(), 1u64 << 63)].into_iter().collect(),
validator_count: 12,
sessions_per_era: 24, // 24 hours per era.
bonding_duration: 90, // 90 days per bond.
Expand Down Expand Up @@ -149,8 +150,8 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
};

let storage = genesis_config.build_externalities();
let block = genesis::construct_genesis_block(&storage);
(primitives::block::Header::decode(&mut block.header.encode().as_ref()).expect("to_vec() always gives a valid serialisation; qed"), storage.into_iter().collect())
let block = genesis::construct_genesis_block::<Block>(&storage);
(block.header, storage.into_iter().collect())
}
}

Expand All @@ -160,7 +161,7 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
let _rpc_servers = {
let handler = || {
let chain = rpc::apis::chain::Chain::new(client.clone(), core.remote());
rpc::rpc_handler(client.clone(), chain, Arc::new(DummyPool), DummySystem)
rpc::rpc_handler::<Block, _, _, _, _>(client.clone(), chain, Arc::new(DummyPool), DummySystem)
};
let http_address = "127.0.0.1:9933".parse().unwrap();
let ws_address = "127.0.0.1:9944".parse().unwrap();
Expand Down
78 changes: 43 additions & 35 deletions demo/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mod tests {
use super::Executor;
use substrate_executor::WasmExecutor;
use codec::{Slicable, Joiner};
use keyring::Keyring::{self, Alice, Bob};
use keyring::Keyring;
use runtime_support::{Hashable, StorageValue, StorageMap};
use state_machine::{CodeExecutor, TestExternalities};
use primitives::twox_128;
Expand All @@ -63,13 +63,21 @@ mod tests {
)
}

fn alice() -> Hash {
Keyring::Alice.to_raw_public().into()
}

fn bob() -> Hash {
Keyring::Bob.to_raw_public().into()
}

fn xt() -> UncheckedExtrinsic {
let extrinsic = Extrinsic {
signed: Alice.into(),
signed: alice(),
index: 0,
function: Call::Staking(staking::Call::transfer::<Concrete>(Bob.into(), 69)),
function: Call::Staking(staking::Call::transfer::<Concrete>(bob(), 69)),
};
let signature = Keyring::from_raw_public(extrinsic.signed).unwrap()
let signature = Keyring::from_raw_public(extrinsic.signed.0).unwrap()
.sign(&extrinsic.encode()).into();

UncheckedExtrinsic { extrinsic, signature }
Expand All @@ -82,7 +90,7 @@ mod tests {
#[test]
fn panic_execution_with_foreign_code_gives_error() {
let mut t: TestExternalities = map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(*Alice)).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![70u8; 8],
twox_128(<staking::TransactionByteFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
Expand All @@ -97,7 +105,7 @@ mod tests {
#[test]
fn panic_execution_with_native_equivalent_code_gives_error() {
let mut t: TestExternalities = map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(*Alice)).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![70u8; 8],
twox_128(<staking::TransactionByteFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
Expand All @@ -112,7 +120,7 @@ mod tests {
#[test]
fn successful_execution_with_native_equivalent_code_gives_ok() {
let mut t: TestExternalities = map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(*Alice)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(<staking::TransactionByteFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
Expand All @@ -124,15 +132,15 @@ mod tests {
assert!(r.is_ok());

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

#[test]
fn successful_execution_with_foreign_code_gives_ok() {
let mut t: TestExternalities = map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(*Alice)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(<staking::TransactionByteFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
Expand All @@ -144,26 +152,26 @@ mod tests {
assert!(r.is_ok());

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

fn new_test_ext() -> TestExternalities {
use keyring::Keyring::*;
let three = [3u8; 32];
let three = [3u8; 32].into();
GenesisConfig {
consensus: Some(Default::default()),
system: Some(Default::default()),
session: Some(SessionConfig {
session_length: 2,
validators: vec![One.into(), Two.into(), three],
validators: vec![One.to_raw_public().into(), Two.to_raw_public().into(), three],
}),
staking: Some(StakingConfig {
sessions_per_era: 2,
current_era: 0,
balances: vec![(Alice.into(), 111)],
intentions: vec![Alice.into(), Bob.into(), Charlie.into()],
balances: vec![(alice(), 111)],
intentions: vec![alice(), bob(), Charlie.to_raw_public().into()],
validator_count: 3,
bonding_duration: 0,
transaction_base_fee: 1,
Expand All @@ -178,7 +186,7 @@ mod tests {
use triehash::ordered_trie_root;

let extrinsics = extrinsics.into_iter().map(|extrinsic| {
let signature = Pair::from(Keyring::from_public(Public::from_raw(extrinsic.signed)).unwrap())
let signature = Pair::from(Keyring::from_public(Public::from_raw(extrinsic.signed.0)).unwrap())
.sign(&extrinsic.encode()).into();

UncheckedExtrinsic { extrinsic, signature }
Expand All @@ -204,9 +212,9 @@ mod tests {
[69u8; 32].into(),
hex!("76b0393b4958d3cb98bb51d9f4edb316af48485142b8721e94c3b52c75ec3243").into(),
vec![Extrinsic {
signed: Alice.into(),
signed: alice(),
index: 0,
function: Call::Staking(staking::Call::transfer(Bob.into(), 69)),
function: Call::Staking(staking::Call::transfer(bob(), 69)),
}]
)
}
Expand All @@ -218,14 +226,14 @@ mod tests {
hex!("8ae9828a5988459d35fb428086170dead660176ee0766e89bc1a4b48153d4e88").into(),
vec![
Extrinsic {
signed: Bob.into(),
signed: bob(),
index: 0,
function: Call::Staking(staking::Call::transfer(Alice.into(), 5)),
function: Call::Staking(staking::Call::transfer(alice(), 5)),
},
Extrinsic {
signed: Alice.into(),
signed: alice(),
index: 1,
function: Call::Staking(staking::Call::transfer(Bob.into(), 15)),
function: Call::Staking(staking::Call::transfer(bob(), 15)),
}
]
)
Expand All @@ -238,15 +246,15 @@ mod tests {
Executor::new().call(&mut t, COMPACT_CODE, "execute_block", &block1().0).unwrap();

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

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

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

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

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

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

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

#[test]
fn panic_execution_gives_error() {
let mut t: TestExternalities = map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(*Alice)).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![70u8; 8],
twox_128(<staking::TransactionByteFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
Expand All @@ -288,7 +296,7 @@ mod tests {
#[test]
fn successful_execution_gives_ok() {
let mut t: TestExternalities = map![
twox_128(&<staking::FreeBalance<Concrete>>::key_for(*Alice)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(<staking::TransactionByteFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
Expand All @@ -301,8 +309,8 @@ mod tests {
assert!(r.is_ok());

runtime_io::with_externalities(&mut t, || {
assert_eq!(Staking::balance(&Alice), 42);
assert_eq!(Staking::balance(&Bob), 69);
assert_eq!(Staking::balance(&alice()), 42);
assert_eq!(Staking::balance(&bob()), 69);
});
}
}
2 changes: 1 addition & 1 deletion demo/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub type BlockNumber = u64;

/// 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`.
pub type AccountId = primitives::AuthorityId;
pub type AccountId = ::primitives::H256;

/// Balance of an account.
pub type Balance = u64;
Expand Down
32 changes: 25 additions & 7 deletions demo/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@
extern crate substrate_runtime_io as runtime_io;

#[macro_use]
extern crate substrate_runtime_support as runtime_support;
extern crate substrate_runtime_support;

#[macro_use]
extern crate substrate_runtime_primitives as runtime_primitives;

#[cfg(feature = "std")]
#[macro_use]
extern crate serde_derive;

#[cfg(feature = "std")]
extern crate serde;

extern crate substrate_runtime_std as rstd;
extern crate substrate_runtime_consensus as consensus;
extern crate substrate_runtime_council as council;
Expand All @@ -39,10 +46,9 @@ extern crate substrate_runtime_timestamp as timestamp;
extern crate demo_primitives;

use rstd::prelude::*;
use runtime_io::BlakeTwo256;
use demo_primitives::{AccountId, Balance, BlockNumber, Hash, Index, SessionKey, Signature};
use runtime_primitives::generic;
use runtime_primitives::traits::{Identity, HasPublicAux};
use runtime_primitives::traits::{Convert, HasPublicAux, BlakeTwo256};

#[cfg(any(feature = "std", test))]
pub use runtime_primitives::BuildExternalities;
Expand All @@ -61,7 +67,7 @@ impl system::Trait for Concrete {
type Hashing = BlakeTwo256;
type Digest = generic::Digest<Vec<u8>>;
type AccountId = AccountId;
type Header = generic::Header<BlockNumber, Hash, Vec<u8>>;
type Header = generic::Header<BlockNumber, BlakeTwo256, Vec<u8>>;
}

/// System module for this concrete runtime.
Expand All @@ -84,8 +90,16 @@ impl timestamp::Trait for Concrete {
/// Timestamp module for this concrete runtime.
pub type Timestamp = timestamp::Module<Concrete>;

/// Session key conversion.
pub struct SessionKeyConversion;
impl Convert<AccountId, SessionKey> for SessionKeyConversion {
fn convert(a: AccountId) -> SessionKey {
a.0
}
}

impl session::Trait for Concrete {
type ConvertAccountIdToSessionKey = Identity;
type ConvertAccountIdToSessionKey = SessionKeyConversion;
}

/// Session module for this concrete runtime.
Expand Down Expand Up @@ -114,6 +128,8 @@ pub type Council = council::Module<Concrete>;
pub type CouncilVoting = council::voting::Module<Concrete>;

impl_outer_dispatch! {
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub enum Call where aux: <Concrete as HasPublicAux>::PublicAux {
Consensus = 0,
Session = 1,
Expand All @@ -124,6 +140,8 @@ impl_outer_dispatch! {
CouncilVoting = 7,
}

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub enum PrivCall {
Consensus = 0,
Session = 1,
Expand All @@ -135,9 +153,9 @@ impl_outer_dispatch! {
}

/// Block header type as expected by this runtime.
pub type Header = generic::Header<BlockNumber, Hash, Vec<u8>>;
pub type Header = generic::Header<BlockNumber, BlakeTwo256, Vec<u8>>;
/// Block type as expected by this runtime.
pub type Block = generic::Block<BlockNumber, Hash, Vec<u8>, AccountId, Index, Call, Signature>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<AccountId, Index, Call, Signature>;
/// Extrinsic type as expected by this runtime.
Expand Down
Loading