|
| 1 | +// Copyright 2017 Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Substrate Demo. |
| 3 | + |
| 4 | +// Substrate Demo is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Substrate Demo is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Substrate Demo. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//! Substrate Demo CLI library. |
| 18 | +
|
| 19 | +#![warn(missing_docs)] |
| 20 | + |
| 21 | +extern crate env_logger; |
| 22 | +extern crate ed25519; |
| 23 | +extern crate triehash; |
| 24 | +extern crate substrate_codec as codec; |
| 25 | +extern crate substrate_state_machine as state_machine; |
| 26 | +extern crate substrate_client as client; |
| 27 | +extern crate substrate_primitives as primitives; |
| 28 | +extern crate substrate_rpc_servers as rpc; |
| 29 | +extern crate demo_primitives; |
| 30 | +extern crate demo_executor; |
| 31 | +extern crate demo_runtime; |
| 32 | + |
| 33 | +#[macro_use] |
| 34 | +extern crate hex_literal; |
| 35 | +#[macro_use] |
| 36 | +extern crate clap; |
| 37 | +#[macro_use] |
| 38 | +extern crate error_chain; |
| 39 | +#[macro_use] |
| 40 | +extern crate log; |
| 41 | + |
| 42 | +pub mod error; |
| 43 | + |
| 44 | +use codec::Slicable; |
| 45 | +use demo_runtime::genesismap::{additional_storage_with_genesis, GenesisConfig}; |
| 46 | +use client::genesis; |
| 47 | + |
| 48 | +/// Parse command line arguments and start the node. |
| 49 | +/// |
| 50 | +/// IANA unassigned port ranges that we could use: |
| 51 | +/// 6717-6766 Unassigned |
| 52 | +/// 8504-8553 Unassigned |
| 53 | +/// 9556-9591 Unassigned |
| 54 | +/// 9803-9874 Unassigned |
| 55 | +/// 9926-9949 Unassigned |
| 56 | +pub fn run<I, T>(args: I) -> error::Result<()> where |
| 57 | + I: IntoIterator<Item = T>, |
| 58 | + T: Into<std::ffi::OsString> + Clone, |
| 59 | +{ |
| 60 | + let yaml = load_yaml!("./cli.yml"); |
| 61 | + let matches = clap::App::from_yaml(yaml).version(crate_version!()).get_matches_from_safe(args)?; |
| 62 | + |
| 63 | + // TODO [ToDr] Split parameters parsing from actual execution. |
| 64 | + let log_pattern = matches.value_of("log").unwrap_or(""); |
| 65 | + init_logger(log_pattern); |
| 66 | + |
| 67 | + // Create client |
| 68 | + let executor = demo_executor::Executor::new(); |
| 69 | + let mut storage = Default::default(); |
| 70 | + let god_key = hex!["3d866ec8a9190c8343c2fc593d21d8a6d0c5c4763aaab2349de3a6111d64d124"]; |
| 71 | + |
| 72 | + let genesis_config = GenesisConfig { |
| 73 | + validators: vec![god_key.clone()], |
| 74 | + authorities: vec![god_key.clone()], |
| 75 | + balances: vec![(god_key.clone(), 1u64 << 63)].into_iter().collect(), |
| 76 | + block_time: 5, // 5 second block time. |
| 77 | + session_length: 720, // that's 1 hour per session. |
| 78 | + sessions_per_era: 24, // 24 hours per era. |
| 79 | + bonding_duration: 90, // 90 days per bond. |
| 80 | + launch_period: 120 * 24 * 14, // 2 weeks per public referendum |
| 81 | + voting_period: 120 * 24 * 28, // 4 weeks to discuss & vote on an active referendum |
| 82 | + minimum_deposit: 1000, // 1000 as the minimum deposit for a referendum |
| 83 | + candidacy_bond: 1000, // 1000 to become a council candidate |
| 84 | + voter_bond: 100, // 100 down to vote for a candidate |
| 85 | + present_slash_per_voter: 1, // slash by 1 per voter for an invalid presentation. |
| 86 | + carry_count: 24, // carry over the 24 runners-up to the next council election |
| 87 | + presentation_duration: 120 * 24, // one day for presenting winners. |
| 88 | + council_election_voting_period: 7 * 120 * 24, // one week period between possible council elections. |
| 89 | + council_term_duration: 180 * 120 * 24, // 180 day term duration for the council. |
| 90 | + desired_seats: 0, // start with no council: we'll raise this once the stake has been dispersed a bit. |
| 91 | + inactive_grace_period: 1, // one addition vote should go by before an inactive voter can be reaped. |
| 92 | + cooloff_period: 90 * 120 * 24, // 90 day cooling off period if council member vetoes a proposal. |
| 93 | + council_proposal_voting_period: 7 * 120 * 24, // 7 day voting period for council members. |
| 94 | + }; |
| 95 | + let prepare_genesis = || { |
| 96 | + storage = genesis_config.genesis_map(); |
| 97 | + let block = genesis::construct_genesis_block(&storage); |
| 98 | + storage.extend(additional_storage_with_genesis(&block)); |
| 99 | + (primitives::block::Header::decode(&mut block.header.encode().as_ref()).expect("to_vec() always gives a valid serialisation; qed"), storage.into_iter().collect()) |
| 100 | + }; |
| 101 | + let client = client::new_in_mem(executor, prepare_genesis)?; |
| 102 | + |
| 103 | + let address = "127.0.0.1:9933".parse().unwrap(); |
| 104 | + let handler = rpc::rpc_handler(client); |
| 105 | + let server = rpc::start_http(&address, handler)?; |
| 106 | + |
| 107 | + if let Some(_) = matches.subcommand_matches("validator") { |
| 108 | + info!("Starting validator."); |
| 109 | + server.wait(); |
| 110 | + return Ok(()); |
| 111 | + } |
| 112 | + |
| 113 | + println!("No command given.\n"); |
| 114 | + let _ = clap::App::from_yaml(yaml).print_long_help(); |
| 115 | + |
| 116 | + Ok(()) |
| 117 | +} |
| 118 | + |
| 119 | +fn init_logger(pattern: &str) { |
| 120 | + let mut builder = env_logger::LogBuilder::new(); |
| 121 | + // Disable info logging by default for some modules: |
| 122 | + builder.filter(Some("hyper"), log::LogLevelFilter::Warn); |
| 123 | + // Enable info for others. |
| 124 | + builder.filter(None, log::LogLevelFilter::Info); |
| 125 | + |
| 126 | + if let Ok(lvl) = std::env::var("RUST_LOG") { |
| 127 | + builder.parse(&lvl); |
| 128 | + } |
| 129 | + |
| 130 | + builder.parse(pattern); |
| 131 | + |
| 132 | + |
| 133 | + builder.init().expect("Logger initialized only once."); |
| 134 | +} |
0 commit comments