Phase 1 of repo reorg (#719)

* Remove unneeded script

* Rename Substrate Demo -> Substrate

* Rename demo -> node

* Build wasm from last rename.

* Merge ed25519 into substrate-primitives

* Minor tweak

* Rename substrate -> core

* Move substrate-runtime-support to core/runtime/support

* Rename/move substrate-runtime-version

* Move codec up a level

* Rename substrate-codec -> parity-codec

* Move environmental up a level

* Move pwasm-* up to top, ready for removal

* Remove requirement of s-r-support from s-r-primitives

* Move core/runtime/primitives into core/runtime-primitives

* Remove s-r-support dep from s-r-version

* Remove dep of s-r-support from bft

* Remove dep of s-r-support from node/consensus

* Sever all other core deps from s-r-support

* Forgot the no_std directive

* Rename non-SRML modules to sr-* to avoid match clashes

* Move runtime/* to srml/*

* Rename substrate-runtime-* -> srml-*

* Move srml to top-level
This commit is contained in:
Gav Wood
2018-09-12 11:13:31 +02:00
committed by Arkadiy Paronyan
parent 8fe5aa4c81
commit 1e01162505
374 changed files with 2845 additions and 2902 deletions
@@ -0,0 +1,67 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Tool for creating the genesis block.
use std::collections::HashMap;
use runtime_io::twox_128;
use codec::{KeyedVec, Joiner};
use primitives::AuthorityId;
use runtime_primitives::traits::Block;
/// Configuration of a general Substrate test genesis block.
pub struct GenesisConfig {
pub authorities: Vec<AuthorityId>,
pub balances: Vec<(AuthorityId, u64)>,
}
impl GenesisConfig {
pub fn new_simple(authorities: Vec<AuthorityId>, balance: u64) -> Self {
GenesisConfig {
authorities: authorities.clone(),
balances: authorities.into_iter().map(|a| (a, balance)).collect(),
}
}
pub fn genesis_map(&self) -> HashMap<Vec<u8>, Vec<u8>> {
let wasm_runtime = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm").to_vec();
self.balances.iter()
.map(|&(account, balance)| (account.to_keyed_vec(b"balance:"), vec![].and(&balance)))
.map(|(k, v)| (twox_128(&k[..])[..].to_vec(), v.to_vec()))
.chain(vec![
(b":code"[..].into(), wasm_runtime),
(b":heappages"[..].into(), vec![].and(&(16 as u64))),
(b":auth:len"[..].into(), vec![].and(&(self.authorities.len() as u32))),
].into_iter())
.chain(self.authorities.iter()
.enumerate()
.map(|(i, account)| ((i as u32).to_keyed_vec(b":auth:"), vec![].and(account)))
)
.collect()
}
}
macro_rules! map {
($( $name:expr => $value:expr ),*) => (
vec![ $( ( $name, $value ) ),* ].into_iter().collect()
)
}
pub fn additional_storage_with_genesis(genesis_block: &::Block) -> HashMap<Vec<u8>, Vec<u8>> {
map![
twox_128(&b"latest"[..]).to_vec() => genesis_block.hash().0.to_vec()
]
}
+157
View File
@@ -0,0 +1,157 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
// tag::description[]
//! The Substrate runtime. This can be compiled with #[no_std], ready for Wasm.
// end::description[]
#![cfg_attr(not(feature = "std"), no_std)]
extern crate sr_std as rstd;
extern crate parity_codec as codec;
extern crate sr_primitives as runtime_primitives;
#[cfg(feature = "std")]
extern crate serde;
#[cfg(feature = "std")]
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate srml_support as runtime_support;
#[macro_use]
extern crate parity_codec_derive;
#[macro_use]
extern crate sr_io as runtime_io;
#[macro_use]
extern crate sr_version as runtime_version;
#[cfg(test)]
#[macro_use]
extern crate hex_literal;
#[cfg(test)]
extern crate substrate_keyring as keyring;
#[cfg_attr(test, macro_use)]
extern crate substrate_primitives as primitives;
#[cfg(feature = "std")] pub mod genesismap;
pub mod system;
use rstd::prelude::*;
use codec::{Encode, Decode};
use runtime_primitives::traits::{BlindCheckable, BlakeTwo256};
use runtime_primitives::Ed25519Signature;
use runtime_version::RuntimeVersion;
pub use primitives::hash::H256;
/// Test runtime version.
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: ver_str!("test"),
impl_name: ver_str!("parity-test"),
authoring_version: 1,
spec_version: 1,
impl_version: 1,
};
fn version() -> RuntimeVersion {
VERSION
}
/// Calls in transactions.
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub struct Transfer {
pub from: AccountId,
pub to: AccountId,
pub amount: u64,
pub nonce: u64,
}
/// Extrinsic for test-runtime.
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub struct Extrinsic {
pub transfer: Transfer,
pub signature: Ed25519Signature,
}
impl BlindCheckable for Extrinsic {
type Checked = Self;
fn check(self) -> Result<Self, &'static str> {
if ::runtime_primitives::verify_encoded_lazy(&self.signature, &self.transfer, &self.transfer.from) {
Ok(self)
} else {
Err("bad signature")
}
}
}
/// An identifier for an account on this system.
pub type AccountId = H256;
/// A simple hash type for all our hashing.
pub type Hash = H256;
/// The block number type used in this runtime.
pub type BlockNumber = u64;
/// Index of a transaction.
pub type Index = u64;
/// The item of a block digest.
pub type DigestItem = runtime_primitives::generic::DigestItem<u64>;
/// The digest of a block.
pub type Digest = runtime_primitives::generic::Digest<DigestItem>;
/// A test block.
pub type Block = runtime_primitives::generic::Block<Header, Extrinsic>;
/// A test block's header.
pub type Header = runtime_primitives::generic::Header<BlockNumber, BlakeTwo256, DigestItem>;
/// Run whatever tests we have.
pub fn run_tests(mut input: &[u8]) -> Vec<u8> {
use runtime_io::print;
print("run_tests...");
let block = Block::decode(&mut input).unwrap();
print("deserialised block.");
let stxs = block.extrinsics.iter().map(Encode::encode).collect::<Vec<_>>();
print("reserialised transactions.");
[stxs.len() as u8].encode()
}
fn test_event_json() -> &'static str {
"hallo"
}
pub mod api {
use system;
impl_stubs!(
version => |()| super::version(),
json_metadata => |()| {
let mut vec = ::runtime_support::metadata::Vec::new();
vec.push(::runtime_support::metadata::JsonMetadata::Events {
name: "Test", events: &[ ("event", super::test_event_json) ]
});
vec
},
authorities => |()| system::authorities(),
initialise_block => |header| system::initialise_block(header),
execute_block => |block| system::execute_block(block),
apply_extrinsic => |utx| system::execute_transaction(utx),
finalise_block => |()| system::finalise_block(),
balance_of => |a| system::balance_of(a)
);
}
+281
View File
@@ -0,0 +1,281 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! System manager: Handles all of the top-level stuff; executing block/transaction, setting code
//! and depositing logs.
use rstd::prelude::*;
use runtime_io::{storage_root, enumerated_trie_root};
use runtime_support::storage::{self, StorageValue, StorageMap};
use runtime_primitives::traits::{Hash as HashT, BlakeTwo256};
use runtime_primitives::{ApplyError, ApplyOutcome, ApplyResult};
use codec::{KeyedVec, Encode};
use super::{AccountId, BlockNumber, Extrinsic, H256 as Hash, Block, Header};
use primitives::Blake2Hasher;
const NONCE_OF: &[u8] = b"nonce:";
const BALANCE_OF: &[u8] = b"balance:";
const AUTHORITY_AT: &'static[u8] = b":auth:";
const AUTHORITY_COUNT: &'static[u8] = b":auth:len";
storage_items! {
ExtrinsicIndex: b"sys:xti" => required u32;
ExtrinsicData: b"sys:xtd" => required map [ u32 => Vec<u8> ];
// The current block number being processed. Set by `execute_block`.
Number: b"sys:num" => required BlockNumber;
ParentHash: b"sys:pha" => required Hash;
}
pub fn balance_of(who: AccountId) -> u64 {
storage::get_or(&who.to_keyed_vec(BALANCE_OF), 0)
}
pub fn nonce_of(who: AccountId) -> u64 {
storage::get_or(&who.to_keyed_vec(NONCE_OF), 0)
}
/// Get authorities ar given block.
pub fn authorities() -> Vec<::primitives::AuthorityId> {
let len: u32 = storage::unhashed::get(AUTHORITY_COUNT).expect("There are always authorities in test-runtime");
(0..len)
.map(|i| storage::unhashed::get(&i.to_keyed_vec(AUTHORITY_AT)).expect("Authority is properly encoded in test-runtime"))
.collect()
}
pub fn initialise_block(header: Header) {
// populate environment.
<Number>::put(&header.number);
<ParentHash>::put(&header.parent_hash);
<ExtrinsicIndex>::put(0);
}
/// Actually execute all transitioning for `block`.
pub fn execute_block(block: Block) {
let ref header = block.header;
// check transaction trie root represents the transactions.
let txs = block.extrinsics.iter().map(Encode::encode).collect::<Vec<_>>();
let txs = txs.iter().map(Vec::as_slice).collect::<Vec<_>>();
let txs_root = enumerated_trie_root::<Blake2Hasher>(&txs).into();
info_expect_equal_hash(&txs_root, &header.extrinsics_root);
assert!(txs_root == header.extrinsics_root, "Transaction trie root must be valid.");
// execute transactions
block.extrinsics.iter().for_each(|e| { execute_transaction_backend(e).map_err(|_| ()).expect("Extrinsic error"); });
// check storage root.
let storage_root = storage_root().into();
info_expect_equal_hash(&storage_root, &header.state_root);
assert!(storage_root == header.state_root, "Storage root must match that calculated.");
}
/// Execute a transaction outside of the block execution function.
/// This doesn't attempt to validate anything regarding the block.
pub fn execute_transaction(utx: Extrinsic) -> ApplyResult {
let extrinsic_index = ExtrinsicIndex::get();
ExtrinsicData::insert(extrinsic_index, utx.encode());
ExtrinsicIndex::put(extrinsic_index + 1);
execute_transaction_backend(&utx)
}
/// Finalise the block.
pub fn finalise_block() -> Header {
let extrinsic_index = ExtrinsicIndex::take();
let txs: Vec<_> = (0..extrinsic_index).map(ExtrinsicData::take).collect();
let txs = txs.iter().map(Vec::as_slice).collect::<Vec<_>>();
let extrinsics_root = enumerated_trie_root::<Blake2Hasher>(&txs).into();
let number = <Number>::take();
let parent_hash = <ParentHash>::take();
let storage_root = BlakeTwo256::storage_root();
Header {
number,
extrinsics_root,
state_root: storage_root,
parent_hash,
digest: Default::default(),
}
}
fn execute_transaction_backend(utx: &Extrinsic) -> ApplyResult {
use runtime_primitives::traits::BlindCheckable;
// check signature
let utx = match utx.clone().check() {
Ok(tx) => tx,
Err(_) => return Err(ApplyError::BadSignature),
};
let tx: ::Transfer = utx.transfer;
// check nonce
let nonce_key = tx.from.to_keyed_vec(NONCE_OF);
let expected_nonce: u64 = storage::get_or(&nonce_key, 0);
if !(tx.nonce == expected_nonce) {
return Err(ApplyError::Stale)
}
// increment nonce in storage
storage::put(&nonce_key, &(expected_nonce + 1));
// check sender balance
let from_balance_key = tx.from.to_keyed_vec(BALANCE_OF);
let from_balance: u64 = storage::get_or(&from_balance_key, 0);
// enact transfer
if !(tx.amount <= from_balance) {
return Err(ApplyError::CantPay)
}
let to_balance_key = tx.to.to_keyed_vec(BALANCE_OF);
let to_balance: u64 = storage::get_or(&to_balance_key, 0);
storage::put(&from_balance_key, &(from_balance - tx.amount));
storage::put(&to_balance_key, &(to_balance + tx.amount));
Ok(ApplyOutcome::Success)
}
#[cfg(feature = "std")]
fn info_expect_equal_hash(given: &Hash, expected: &Hash) {
use primitives::hexdisplay::HexDisplay;
if given != expected {
println!("Hash: given={}, expected={}", HexDisplay::from(&given.0), HexDisplay::from(&expected.0));
}
}
#[cfg(not(feature = "std"))]
fn info_expect_equal_hash(given: &Hash, expected: &Hash) {
if given != expected {
::runtime_io::print("Hash not equal");
::runtime_io::print(&given.0[..]);
::runtime_io::print(&expected.0[..]);
}
}
#[cfg(test)]
mod tests {
use super::*;
use runtime_io::{with_externalities, twox_128, TestExternalities};
use codec::{Joiner, KeyedVec};
use keyring::Keyring;
use ::{Header, Digest, Extrinsic, Transfer};
use primitives::Blake2Hasher;
fn new_test_ext() -> TestExternalities<Blake2Hasher> {
map![
twox_128(b"latest").to_vec() => vec![69u8; 32],
twox_128(b":auth:len").to_vec() => vec![].and(&3u32),
twox_128(&0u32.to_keyed_vec(b":auth:")).to_vec() => Keyring::Alice.to_raw_public().to_vec(),
twox_128(&1u32.to_keyed_vec(b":auth:")).to_vec() => Keyring::Bob.to_raw_public().to_vec(),
twox_128(&2u32.to_keyed_vec(b":auth:")).to_vec() => Keyring::Charlie.to_raw_public().to_vec(),
twox_128(&Keyring::Alice.to_raw_public().to_keyed_vec(b"balance:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
]
}
fn construct_signed_tx(tx: Transfer) -> Extrinsic {
let signature = Keyring::from_raw_public(tx.from.0).unwrap().sign(&tx.encode()).into();
Extrinsic { transfer: tx, signature }
}
#[test]
fn block_import_works() {
let mut t = new_test_ext();
let h = Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root: hex!("0c22599e15fb5e052c84f79a2aab179ba6bb238218fd86bdd4a74ebcc87adfcd").into(),
extrinsics_root: hex!("45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0").into(),
digest: Digest { logs: vec![], },
};
let b = Block {
header: h,
extrinsics: vec![],
};
with_externalities(&mut t, || {
execute_block(b);
});
}
#[test]
fn block_import_with_transaction_works() {
let mut t = new_test_ext();
with_externalities(&mut t, || {
assert_eq!(balance_of(Keyring::Alice.to_raw_public().into()), 111);
assert_eq!(balance_of(Keyring::Bob.to_raw_public().into()), 0);
});
let b = Block {
header: Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root: hex!("0425393fd07e2a806cfd7e990ee91dc92fe6bba34eab2bf45d5be7d67e24d467").into(),
extrinsics_root: hex!("83fd59e8fe7cee53d7421713a09fe0abae1aec5f4db94fe5193737b12195f013").into(),
digest: Digest { logs: vec![], },
},
extrinsics: vec![
construct_signed_tx(Transfer {
from: Keyring::Alice.to_raw_public().into(),
to: Keyring::Bob.to_raw_public().into(),
amount: 69,
nonce: 0,
})
],
};
with_externalities(&mut t, || {
execute_block(b.clone());
assert_eq!(balance_of(Keyring::Alice.to_raw_public().into()), 42);
assert_eq!(balance_of(Keyring::Bob.to_raw_public().into()), 69);
});
let b = Block {
header: Header {
parent_hash: b.header.hash(),
number: 2,
state_root: hex!("e32dd1d84d9133ca48078d2d83f2b0db19f9d47229ba98bf5ced0e9f86fac2c7").into(),
extrinsics_root: hex!("5d2d0a93201744f0df878c33b07da40cd38e24ac2358cc2811ea640835c31b68").into(),
digest: Digest { logs: vec![], },
},
extrinsics: vec![
construct_signed_tx(Transfer {
from: Keyring::Bob.to_raw_public().into(),
to: Keyring::Alice.to_raw_public().into(),
amount: 27,
nonce: 0,
}),
construct_signed_tx(Transfer {
from: Keyring::Alice.to_raw_public().into(),
to: Keyring::Charlie.to_raw_public().into(),
amount: 69,
nonce: 1,
}),
],
};
with_externalities(&mut t, || {
execute_block(b);
assert_eq!(balance_of(Keyring::Alice.to_raw_public().into()), 0);
assert_eq!(balance_of(Keyring::Bob.to_raw_public().into()), 42);
assert_eq!(balance_of(Keyring::Charlie.to_raw_public().into()), 69);
});
}
}