Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it actually work #2

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1,942 changes: 188 additions & 1,754 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 18 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,43 @@ name = "substrate-test-runner"
version = "0.1.0"
authors = ["Tomasz Drwięga <tomusdrw@gmail.com>"]
edition = "2018"
build = "build.rs"

[workspace]
members = [
"runtime"
]

[build-dependencies]
substrate-build-script-utils = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
Copy link
Contributor

Choose a reason for hiding this comment

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

Mind updating to master?


[dependencies]
runtime = { package = "test-runtime", path = "./runtime" }
node-cli = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sc-executor = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }

sc-service = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sc-informant = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sc-network = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sc-cli = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sc-rpc-api = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sc-basic-authorship = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sc-rpc = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sc-consensus = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-blockchain = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-block-builder = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }

sp-api = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }

sc-transaction-pool = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sc-client-db = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sc-client-api = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-transaction-pool = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sc-keystore = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }

sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-session = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-offchain = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-inherents = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-timestamp = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
Expand All @@ -44,6 +60,7 @@ futures = { package = "futures", version = "0.3", features = ["compat"] }
rand = "0.7"
parking_lot = "0.10"
tokio = { version = "0.2", features = ["full"] }
sp-keyring = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }

# Calling RPC
jsonrpc-core-client = { version = "14.2", features = ["ws"] }
Expand All @@ -57,5 +74,3 @@ primitive-types = "0.7"
sp-externalities = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-storage = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }

[dev-dependencies]
sp-keyring = { git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
8 changes: 8 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use substrate_build_script_utils::{generate_cargo_keys, rerun_if_git_head_changed};
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this thing doing exactly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nothing now that cli has been removed


fn main() {
generate_cargo_keys();

rerun_if_git_head_changed();
}

2 changes: 2 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ codec = { package = "parity-scale-codec", version = "1.3.1", default-features =
serde = { version = "1.0.101", optional = true, features = ["derive"] }

balances = { default-features = false, package = "pallet-balances", git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
indices = { default-features = false, package = "pallet-indices", git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
randomness-collective-flip = { default-features = false, package = "pallet-randomness-collective-flip", git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sudo = { default-features = false, package = "pallet-sudo", git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
Expand All @@ -34,6 +35,7 @@ sp-session = { default-features = false, git = "https://github.com/paritytech/su
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-transaction-pool = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-version = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
sp-application-crypto = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }

[build-dependencies]
wasm-builder-runner = { version = "1.0.5", package = "substrate-wasm-builder-runner", git = "https://github.com/paritytech/substrate.git", branch = "seun-substrate-test-runner" }
Expand Down
96 changes: 92 additions & 4 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use codec::Encode;
use frame_support::debug;
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::traits::{BlakeTwo256, Block as BlockT, IdentifyAccount, IdentityLookup, Saturating, Verify};
use sp_runtime::traits::{BlakeTwo256, Block as BlockT, IdentifyAccount, Saturating, StaticLookup, Verify};
use sp_runtime::SaturatedConversion;
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
create_runtime_str, generic, impl_opaque_keys, traits,
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, MultiSignature,
};
Expand Down Expand Up @@ -133,7 +136,7 @@ impl system::Trait for Runtime {
/// The aggregated dispatch type that is available for extrinsics.
type Call = Call;
/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
type Lookup = IdentityLookup<AccountId>;
type Lookup = Indices;
Copy link
Contributor

Choose a reason for hiding this comment

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

Are Indices strictly necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably not

/// The index type for storing how many extrinsics an account has signed.
type Index = Index;
/// The index type for blocks.
Expand Down Expand Up @@ -184,6 +187,22 @@ impl system::Trait for Runtime {
type SystemWeightInfo = ();
}

parameter_types! {
pub const IndexDeposit: Balance = 1 * DOLLARS;
}

pub const MILLICENTS: Balance = 1_000_000_000;
pub const CENTS: Balance = 1_000 * MILLICENTS;
pub const DOLLARS: Balance = 100 * CENTS;

impl indices::Trait for Runtime {
type AccountIndex = AccountIndex;
type Currency = Balances;
type Deposit = IndexDeposit;
type Event = Event;
type WeightInfo = ();
}

parameter_types! {
pub const MinimumPeriod: u64 = SLOT_DURATION / 2;
}
Expand Down Expand Up @@ -240,13 +259,80 @@ construct_runtime!(
RandomnessCollectiveFlip: randomness_collective_flip::{Module, Call, Storage},
Timestamp: timestamp::{Module, Call, Storage, Inherent},
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
Indices: indices::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage},
Sudo: sudo::{Module, Call, Config<T>, Storage, Event<T>},
}
);

impl system::offchain::SigningTypes for Runtime {
type Public = <Signature as traits::Verify>::Signer;
type Signature = MultiSignature;
}

use sp_application_crypto::sr25519;
use sp_runtime::MultiSigner;

pub struct RuntimeKeyType;

impl system::offchain::AppCrypto<MultiSigner, MultiSignature> for RuntimeKeyType {
type RuntimeAppPublic = sr25519::AppPublic;
type GenericSignature = sp_core::sr25519::Signature;
type GenericPublic = sp_core::sr25519::Public;
}

impl<C> system::offchain::SendTransactionTypes<C> for Runtime
where
Call: From<C>,
{
type Extrinsic = UncheckedExtrinsic;
type OverarchingCall = Call;
}

impl<LocalCall> system::offchain::CreateSignedTransaction<LocalCall> for Runtime
where
Call: From<LocalCall>,
{
fn create_transaction<C: system::offchain::AppCrypto<Self::Public, Self::Signature>>(
call: Call,
public: <Signature as traits::Verify>::Signer,
account: AccountId,
nonce: Index,
) -> Option<(Call, <UncheckedExtrinsic as traits::Extrinsic>::SignaturePayload)> {
// take the biggest period possible.
let period = BlockHashCount::get()
.checked_next_power_of_two()
.map(|c| c / 2)
.unwrap_or(2) as u64;
let current_block = System::block_number()
.saturated_into::<u64>()
// The `System::block_number` is initialized with `n+1`,
// so the actual block number is `n`.
.saturating_sub(1);
let tip = 0;
let extra: SignedExtra = (
system::CheckSpecVersion::<Runtime>::new(),
system::CheckTxVersion::<Runtime>::new(),
system::CheckGenesis::<Runtime>::new(),
system::CheckEra::<Runtime>::from(generic::Era::mortal(period, current_block)),
system::CheckNonce::<Runtime>::from(nonce),
system::CheckWeight::<Runtime>::new(),
transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
);
let raw_payload = SignedPayload::new(call, extra)
.map_err(|e| {
debug::warn!("Unable to create signed payload: {:?}", e);
})
.ok()?;
let signature = raw_payload.using_encoded(|payload| C::sign(payload, public))?;
let address = Indices::unlookup(account);
Copy link
Contributor

Choose a reason for hiding this comment

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

Without Indices it should be just let address = account;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahh, didn't know that

let (call, extra, _) = raw_payload.deconstruct();
Some((call, (address, signature.into(), extra)))
}
}

/// The address format for describing accounts.
pub type Address = AccountId;
pub type Address = <Indices as StaticLookup>::Source;
/// Block header type as expected by this runtime.
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
/// Block type as expected by this runtime.
Expand All @@ -271,6 +357,8 @@ pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Call, Signatu
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Call, SignedExtra>;
/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<Runtime, Block, system::ChainContext<Runtime>, Runtime, AllModules>;
/// Signed payload type
pub type SignedPayload = generic::SignedPayload<Call, SignedExtra>;

impl_runtime_apis! {
impl sp_api::Core<Block> for Runtime {
Expand Down
91 changes: 91 additions & 0 deletions src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use runtime::{
AccountId, Signature, WASM_BINARY,
BalancesConfig, GenesisConfig, IndicesConfig, SudoConfig, SystemConfig,
};
use sc_service::ChainType;
use sp_core::{sr25519, Pair, Public};
use sp_runtime::traits::{IdentifyAccount, Verify};

/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig>;

/// Generate a crypto pair from seed.
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}

type AccountPublic = <Signature as Verify>::Signer;

pub fn spec_factory(_: String) -> Result<Box<dyn sc_service::ChainSpec>, String> {
// we supply our own chainspec
Ok(Box::new(development_config()?))
}

/// Generate an account ID from seed.
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}

pub fn development_config() -> Result<ChainSpec, String> {
Ok(ChainSpec::from_genesis(
"Development",
"dev",
ChainType::Development,
move || {
testnet_genesis(
WASM_BINARY,
get_account_id_from_seed::<sr25519::Public>("Alice"),
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Dave"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
],
true,
)
},
vec![],
None,
None,
None,
None,
))
}

/// Configure initial storage state for FRAME modules.
fn testnet_genesis(
wasm_binary: &[u8],
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool,
) -> GenesisConfig {
GenesisConfig {
system: Some(SystemConfig {
// Add Wasm runtime to storage.
code: wasm_binary.to_vec(),
changes_trie_config: Default::default(),
}),
balances: Some(BalancesConfig {
// Configure endowed accounts with initial balance of 1 << 60.
balances: endowed_accounts.iter().cloned().map(|k| (k, 1 << 60)).collect(),
}),
indices: Some(IndicesConfig { indices: vec![] }),
sudo: Some(SudoConfig {
// Assign network admin rights.
key: root_key,
}),
}
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub mod node;
pub mod rpc;
pub mod subxt;
pub mod chain_spec;
pub mod test;
pub mod types;



pub mod prelude {
pub use super::rpc::RpcExtension;
pub use super::test::*;
Expand Down
Loading