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

Commit 2e26322

Browse files
gavofyorkrphmeier
authored andcommitted
Make substrate generic (#169)
* Some initial work on RPC and client * Rephrase as params * More work on traitifying substrate. * Traitify in_mem.rs * traitify client.rs * Make new primitives (mainly traits) build again. * Many (superficial) build fixes throughout. * Fix remaining build issues up to bft interface. * Make bft primitives be generic. * Switch out MisBehaviorReport for generic version. * Merge Hashing into Header. * Update runtime for new generics (with Hashing). * Update demo runtime. * Make runtime compile. * Build fixes for runtime * Remove old modules. * port substrate-bft to use generic substrate types * port client * port substrate-test-runtime * mostly port test-runtime to get compiling for std * Ensure `AccountId` has a `Default`. * Fix type deps. * finish porting * initialize test_runtime from genesis correctly * remove commented code * maybe unsigned signatures * runtimes compile * port over most of network * serialization for generic types * fix comment * remove some unnecessary trait bounds * network compiles * tests compile for sync * fix deserialization * temporarily remove deserialize derives * workarounds for serde issues for deriving deserialization * get demo-runtime compiling on std * port extrinsic-pool * primitives reshuffling * get network compiling again * remove debugging file * runtime tests now passing * port client-db * start to port over substrate-rpc * mostly port over PolkadotApi * test_runtime follows normal conventions * substrate runtime tests pass * deal with inherent extrinsics correctly in polkadot-api * port transaction-pool * port polkadot-consensus * port substrate-rpc * everything compiles * tests compile * fix grumbles * test-runtime uses its own transfer type * switch to master branch of jsonrpc * fix network tests and some warnings * all tests pass locally * [ci-skip] add another comment about issue * remove some curlies
1 parent 74b3be9 commit 2e26322

File tree

132 files changed

+4648
-4256
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+4648
-4256
lines changed

Cargo.lock

Lines changed: 697 additions & 553 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/cli/src/lib.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,18 @@ pub mod error;
4949

5050
use std::sync::Arc;
5151
use client::genesis;
52-
use codec::Slicable;
52+
use demo_primitives::Hash;
5353
use demo_runtime::{GenesisConfig, ConsensusConfig, CouncilConfig, DemocracyConfig,
5454
SessionConfig, StakingConfig, BuildExternalities};
55+
use demo_runtime::{Block, Header, UncheckedExtrinsic};
5556
use futures::{Future, Sink, Stream};
5657

5758
struct DummyPool;
58-
impl extrinsic_pool::api::ExtrinsicPool for DummyPool {
59+
impl extrinsic_pool::api::ExtrinsicPool<UncheckedExtrinsic, Hash> for DummyPool {
5960
type Error = extrinsic_pool::txpool::Error;
6061

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

103104
struct GenesisBuilder;
104105

105-
impl client::GenesisBuilder for GenesisBuilder {
106-
fn build(self) -> (primitives::Header, Vec<(Vec<u8>, Vec<u8>)>) {
106+
impl client::GenesisBuilder<Block> for GenesisBuilder {
107+
fn build(self) -> (Header, Vec<(Vec<u8>, Vec<u8>)>) {
107108
let god_key = hex!["3d866ec8a9190c8343c2fc593d21d8a6d0c5c4763aaab2349de3a6111d64d124"];
108109
let genesis_config = GenesisConfig {
109110
consensus: Some(ConsensusConfig {
@@ -113,15 +114,15 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
113114
system: None,
114115
// block_time: 5, // 5 second block time.
115116
session: Some(SessionConfig {
116-
validators: vec![god_key.clone()],
117+
validators: vec![god_key.clone().into()],
117118
session_length: 720, // that's 1 hour per session.
118119
}),
119120
staking: Some(StakingConfig {
120121
current_era: 0,
121122
intentions: vec![],
122123
transaction_base_fee: 100,
123124
transaction_byte_fee: 1,
124-
balances: vec![(god_key.clone(), 1u64 << 63)].into_iter().collect(),
125+
balances: vec![(god_key.clone().into(), 1u64 << 63)].into_iter().collect(),
125126
validator_count: 12,
126127
sessions_per_era: 24, // 24 hours per era.
127128
bonding_duration: 90, // 90 days per bond.
@@ -149,8 +150,8 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
149150
};
150151

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

@@ -160,7 +161,7 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
160161
let _rpc_servers = {
161162
let handler = || {
162163
let chain = rpc::apis::chain::Chain::new(client.clone(), core.remote());
163-
rpc::rpc_handler(client.clone(), chain, Arc::new(DummyPool), DummySystem)
164+
rpc::rpc_handler::<Block, _, _, _, _>(client.clone(), chain, Arc::new(DummyPool), DummySystem)
164165
};
165166
let http_address = "127.0.0.1:9933".parse().unwrap();
166167
let ws_address = "127.0.0.1:9944".parse().unwrap();

demo/executor/src/lib.rs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mod tests {
4242
use super::Executor;
4343
use substrate_executor::WasmExecutor;
4444
use codec::{Slicable, Joiner};
45-
use keyring::Keyring::{self, Alice, Bob};
45+
use keyring::Keyring;
4646
use runtime_support::{Hashable, StorageValue, StorageMap};
4747
use state_machine::{CodeExecutor, TestExternalities};
4848
use primitives::twox_128;
@@ -63,13 +63,21 @@ mod tests {
6363
)
6464
}
6565

66+
fn alice() -> Hash {
67+
Keyring::Alice.to_raw_public().into()
68+
}
69+
70+
fn bob() -> Hash {
71+
Keyring::Bob.to_raw_public().into()
72+
}
73+
6674
fn xt() -> UncheckedExtrinsic {
6775
let extrinsic = Extrinsic {
68-
signed: Alice.into(),
76+
signed: alice(),
6977
index: 0,
70-
function: Call::Staking(staking::Call::transfer::<Concrete>(Bob.into(), 69)),
78+
function: Call::Staking(staking::Call::transfer::<Concrete>(bob(), 69)),
7179
};
72-
let signature = Keyring::from_raw_public(extrinsic.signed).unwrap()
80+
let signature = Keyring::from_raw_public(extrinsic.signed.0).unwrap()
7381
.sign(&extrinsic.encode()).into();
7482

7583
UncheckedExtrinsic { extrinsic, signature }
@@ -82,7 +90,7 @@ mod tests {
8290
#[test]
8391
fn panic_execution_with_foreign_code_gives_error() {
8492
let mut t: TestExternalities = map![
85-
twox_128(&<staking::FreeBalance<Concrete>>::key_for(*Alice)).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
93+
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
8694
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![70u8; 8],
8795
twox_128(<staking::TransactionByteFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
8896
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
@@ -97,7 +105,7 @@ mod tests {
97105
#[test]
98106
fn panic_execution_with_native_equivalent_code_gives_error() {
99107
let mut t: TestExternalities = map![
100-
twox_128(&<staking::FreeBalance<Concrete>>::key_for(*Alice)).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
108+
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
101109
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![70u8; 8],
102110
twox_128(<staking::TransactionByteFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
103111
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
@@ -112,7 +120,7 @@ mod tests {
112120
#[test]
113121
fn successful_execution_with_native_equivalent_code_gives_ok() {
114122
let mut t: TestExternalities = map![
115-
twox_128(&<staking::FreeBalance<Concrete>>::key_for(*Alice)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
123+
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
116124
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
117125
twox_128(<staking::TransactionByteFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
118126
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
@@ -124,15 +132,15 @@ mod tests {
124132
assert!(r.is_ok());
125133

126134
runtime_io::with_externalities(&mut t, || {
127-
assert_eq!(Staking::balance(&Alice), 42);
128-
assert_eq!(Staking::balance(&Bob), 69);
135+
assert_eq!(Staking::balance(&alice()), 42);
136+
assert_eq!(Staking::balance(&bob()), 69);
129137
});
130138
}
131139

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

146154
runtime_io::with_externalities(&mut t, || {
147-
assert_eq!(Staking::balance(&Alice), 42);
148-
assert_eq!(Staking::balance(&Bob), 69);
155+
assert_eq!(Staking::balance(&alice()), 42);
156+
assert_eq!(Staking::balance(&bob()), 69);
149157
});
150158
}
151159

152160
fn new_test_ext() -> TestExternalities {
153161
use keyring::Keyring::*;
154-
let three = [3u8; 32];
162+
let three = [3u8; 32].into();
155163
GenesisConfig {
156164
consensus: Some(Default::default()),
157165
system: Some(Default::default()),
158166
session: Some(SessionConfig {
159167
session_length: 2,
160-
validators: vec![One.into(), Two.into(), three],
168+
validators: vec![One.to_raw_public().into(), Two.to_raw_public().into(), three],
161169
}),
162170
staking: Some(StakingConfig {
163171
sessions_per_era: 2,
164172
current_era: 0,
165-
balances: vec![(Alice.into(), 111)],
166-
intentions: vec![Alice.into(), Bob.into(), Charlie.into()],
173+
balances: vec![(alice(), 111)],
174+
intentions: vec![alice(), bob(), Charlie.to_raw_public().into()],
167175
validator_count: 3,
168176
bonding_duration: 0,
169177
transaction_base_fee: 1,
@@ -178,7 +186,7 @@ mod tests {
178186
use triehash::ordered_trie_root;
179187

180188
let extrinsics = extrinsics.into_iter().map(|extrinsic| {
181-
let signature = Pair::from(Keyring::from_public(Public::from_raw(extrinsic.signed)).unwrap())
189+
let signature = Pair::from(Keyring::from_public(Public::from_raw(extrinsic.signed.0)).unwrap())
182190
.sign(&extrinsic.encode()).into();
183191

184192
UncheckedExtrinsic { extrinsic, signature }
@@ -204,9 +212,9 @@ mod tests {
204212
[69u8; 32].into(),
205213
hex!("76b0393b4958d3cb98bb51d9f4edb316af48485142b8721e94c3b52c75ec3243").into(),
206214
vec![Extrinsic {
207-
signed: Alice.into(),
215+
signed: alice(),
208216
index: 0,
209-
function: Call::Staking(staking::Call::transfer(Bob.into(), 69)),
217+
function: Call::Staking(staking::Call::transfer(bob(), 69)),
210218
}]
211219
)
212220
}
@@ -218,14 +226,14 @@ mod tests {
218226
hex!("8ae9828a5988459d35fb428086170dead660176ee0766e89bc1a4b48153d4e88").into(),
219227
vec![
220228
Extrinsic {
221-
signed: Bob.into(),
229+
signed: bob(),
222230
index: 0,
223-
function: Call::Staking(staking::Call::transfer(Alice.into(), 5)),
231+
function: Call::Staking(staking::Call::transfer(alice(), 5)),
224232
},
225233
Extrinsic {
226-
signed: Alice.into(),
234+
signed: alice(),
227235
index: 1,
228-
function: Call::Staking(staking::Call::transfer(Bob.into(), 15)),
236+
function: Call::Staking(staking::Call::transfer(bob(), 15)),
229237
}
230238
]
231239
)
@@ -238,15 +246,15 @@ mod tests {
238246
Executor::new().call(&mut t, COMPACT_CODE, "execute_block", &block1().0).unwrap();
239247

240248
runtime_io::with_externalities(&mut t, || {
241-
assert_eq!(Staking::balance(&Alice), 41);
242-
assert_eq!(Staking::balance(&Bob), 69);
249+
assert_eq!(Staking::balance(&alice()), 41);
250+
assert_eq!(Staking::balance(&bob()), 69);
243251
});
244252

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

247255
runtime_io::with_externalities(&mut t, || {
248-
assert_eq!(Staking::balance(&Alice), 30);
249-
assert_eq!(Staking::balance(&Bob), 78);
256+
assert_eq!(Staking::balance(&alice()), 30);
257+
assert_eq!(Staking::balance(&bob()), 78);
250258
});
251259
}
252260

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

259267
runtime_io::with_externalities(&mut t, || {
260-
assert_eq!(Staking::balance(&Alice), 41);
261-
assert_eq!(Staking::balance(&Bob), 69);
268+
assert_eq!(Staking::balance(&alice()), 41);
269+
assert_eq!(Staking::balance(&bob()), 69);
262270
});
263271

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

266274
runtime_io::with_externalities(&mut t, || {
267-
assert_eq!(Staking::balance(&Alice), 30);
268-
assert_eq!(Staking::balance(&Bob), 78);
275+
assert_eq!(Staking::balance(&alice()), 30);
276+
assert_eq!(Staking::balance(&bob()), 78);
269277
});
270278
}
271279

272280
#[test]
273281
fn panic_execution_gives_error() {
274282
let mut t: TestExternalities = map![
275-
twox_128(&<staking::FreeBalance<Concrete>>::key_for(*Alice)).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
283+
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0],
276284
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![70u8; 8],
277285
twox_128(<staking::TransactionByteFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
278286
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
@@ -288,7 +296,7 @@ mod tests {
288296
#[test]
289297
fn successful_execution_gives_ok() {
290298
let mut t: TestExternalities = map![
291-
twox_128(&<staking::FreeBalance<Concrete>>::key_for(*Alice)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
299+
twox_128(&<staking::FreeBalance<Concrete>>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0],
292300
twox_128(<staking::TransactionBaseFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
293301
twox_128(<staking::TransactionByteFee<Concrete>>::key()).to_vec() => vec![0u8; 8],
294302
twox_128(&<system::BlockHash<Concrete>>::key_for(0)).to_vec() => vec![0u8; 32]
@@ -301,8 +309,8 @@ mod tests {
301309
assert!(r.is_ok());
302310

303311
runtime_io::with_externalities(&mut t, || {
304-
assert_eq!(Staking::balance(&Alice), 42);
305-
assert_eq!(Staking::balance(&Bob), 69);
312+
assert_eq!(Staking::balance(&alice()), 42);
313+
assert_eq!(Staking::balance(&bob()), 69);
306314
});
307315
}
308316
}

demo/primitives/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub type BlockNumber = u64;
3333

3434
/// Alias to Ed25519 pubkey that identifies an account on the relay chain. This will almost
3535
/// certainly continue to be the same as the substrate's `AuthorityId`.
36-
pub type AccountId = primitives::AuthorityId;
36+
pub type AccountId = ::primitives::H256;
3737

3838
/// Balance of an account.
3939
pub type Balance = u64;

demo/runtime/src/lib.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@
2222
extern crate substrate_runtime_io as runtime_io;
2323

2424
#[macro_use]
25-
extern crate substrate_runtime_support as runtime_support;
25+
extern crate substrate_runtime_support;
2626

2727
#[macro_use]
2828
extern crate substrate_runtime_primitives as runtime_primitives;
2929

30+
#[cfg(feature = "std")]
31+
#[macro_use]
32+
extern crate serde_derive;
33+
34+
#[cfg(feature = "std")]
35+
extern crate serde;
36+
3037
extern crate substrate_runtime_std as rstd;
3138
extern crate substrate_runtime_consensus as consensus;
3239
extern crate substrate_runtime_council as council;
@@ -39,10 +46,9 @@ extern crate substrate_runtime_timestamp as timestamp;
3946
extern crate demo_primitives;
4047

4148
use rstd::prelude::*;
42-
use runtime_io::BlakeTwo256;
4349
use demo_primitives::{AccountId, Balance, BlockNumber, Hash, Index, SessionKey, Signature};
4450
use runtime_primitives::generic;
45-
use runtime_primitives::traits::{Identity, HasPublicAux};
51+
use runtime_primitives::traits::{Convert, HasPublicAux, BlakeTwo256};
4652

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

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

93+
/// Session key conversion.
94+
pub struct SessionKeyConversion;
95+
impl Convert<AccountId, SessionKey> for SessionKeyConversion {
96+
fn convert(a: AccountId) -> SessionKey {
97+
a.0
98+
}
99+
}
100+
87101
impl session::Trait for Concrete {
88-
type ConvertAccountIdToSessionKey = Identity;
102+
type ConvertAccountIdToSessionKey = SessionKeyConversion;
89103
}
90104

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

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

143+
#[derive(Clone, PartialEq, Eq)]
144+
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
127145
pub enum PrivCall {
128146
Consensus = 0,
129147
Session = 1,
@@ -135,9 +153,9 @@ impl_outer_dispatch! {
135153
}
136154

137155
/// Block header type as expected by this runtime.
138-
pub type Header = generic::Header<BlockNumber, Hash, Vec<u8>>;
156+
pub type Header = generic::Header<BlockNumber, BlakeTwo256, Vec<u8>>;
139157
/// Block type as expected by this runtime.
140-
pub type Block = generic::Block<BlockNumber, Hash, Vec<u8>, AccountId, Index, Call, Signature>;
158+
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
141159
/// Unchecked extrinsic type as expected by this runtime.
142160
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<AccountId, Index, Call, Signature>;
143161
/// Extrinsic type as expected by this runtime.

0 commit comments

Comments
 (0)