From 147561088083493338eb1268199db49649a433a1 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 20 Oct 2018 22:15:28 +0200 Subject: [PATCH] Remove nonsense code and options, fix score and generally make subkey work (#936) * Remove nonsense code and options, fix score and generally make subkey work. * Rename Demo -> Node --- substrate/core/service/src/lib.rs | 2 +- substrate/node/cli/src/service.rs | 4 +-- substrate/subkey/src/main.rs | 23 +++++++------- substrate/subkey/src/vanity.rs | 50 ++++++++++--------------------- 4 files changed, 30 insertions(+), 49 deletions(-) diff --git a/substrate/core/service/src/lib.rs b/substrate/core/service/src/lib.rs index 47472feea3..848c96ae90 100644 --- a/substrate/core/service/src/lib.rs +++ b/substrate/core/service/src/lib.rs @@ -490,7 +490,7 @@ macro_rules! construct_simple_service { /// // Declare the block type /// Block = Block, /// // Declare the network protocol and give an initializer. -/// NetworkProtocol = DemoProtocol { |config| Ok(DemoProtocol::new()) }, +/// NetworkProtocol = NodeProtocol { |config| Ok(NodeProtocol::new()) }, /// RuntimeDispatch = node_executor::Executor, /// FullTransactionPoolApi = transaction_pool::ChainApi, FullExecutor, Block> /// { |config, client| Ok(TransactionPool::new(config, transaction_pool::ChainApi::new(client))) }, diff --git a/substrate/node/cli/src/service.rs b/substrate/node/cli/src/service.rs index 470ac3db78..4e718816e1 100644 --- a/substrate/node/cli/src/service.rs +++ b/substrate/node/cli/src/service.rs @@ -22,7 +22,7 @@ use std::sync::Arc; use transaction_pool::{self, txpool::{Pool as TransactionPool}}; use node_primitives::Block; use node_runtime::GenesisConfig; -use node_network::Protocol as DemoProtocol; +use node_network::Protocol as NodeProtocol; use substrate_service::{ FactoryFullConfiguration, LightComponents, FullComponents, FullBackend, LightBackend, FullExecutor, LightExecutor @@ -61,7 +61,7 @@ construct_simple_service!(Service); construct_service_factory! { struct Factory { Block = Block, - NetworkProtocol = DemoProtocol { |config| Ok(DemoProtocol::new()) }, + NetworkProtocol = NodeProtocol { |config| Ok(NodeProtocol::new()) }, RuntimeDispatch = node_executor::Executor, FullTransactionPoolApi = transaction_pool::ChainApi, FullExecutor, Block> { |config, client| Ok(TransactionPool::new(config, transaction_pool::ChainApi::new(client))) }, diff --git a/substrate/subkey/src/main.rs b/substrate/subkey/src/main.rs index afe78f2a0b..a89f7911ac 100644 --- a/substrate/subkey/src/main.rs +++ b/substrate/subkey/src/main.rs @@ -34,17 +34,12 @@ fn main() { match matches.subcommand() { ("vanity", Some(matches)) => { let desired: String = matches.value_of("pattern").map(str::to_string).unwrap_or_default(); - let amount_of_keys = matches.value_of("number") - .expect("`number` has a default value; thus it can't be None; qed"); - let amount_of_keys: usize = amount_of_keys.parse::().expect("Failed to parse number"); - - let keys = vanity::generate_key(&desired, amount_of_keys, true).expect("Key generation failed"); - for key in keys { - println!("{} - {} ({}%)", - key.pair.public().to_ss58check(), - HexDisplay::from(&key.seed), - key.score); - } + let key = vanity::generate_key(&desired).expect("Key generation failed"); + println!("Seed {} (hex: 0x{}) - {} ({}%)", + key.pair.public().to_ss58check(), + HexDisplay::from(&key.pair.public().0), + HexDisplay::from(&key.seed), + key.score); } ("restore", Some(matches)) => { let mut raw_seed = matches.value_of("seed") @@ -63,7 +58,11 @@ fn main() { seed[..len].copy_from_slice(&raw_seed[..len]); let pair = Pair::from_seed(&seed); - println!("{}: {}", HexDisplay::from(&seed), pair.public().to_ss58check()); + println!("Seed 0x{} is account:\n SS58: {}\n Hex: 0x{}", + HexDisplay::from(&seed), + pair.public().to_ss58check(), + HexDisplay::from(&pair.public().0) + ); }, _ => print_usage(&matches), } diff --git a/substrate/subkey/src/vanity.rs b/substrate/subkey/src/vanity.rs index a3c0dab37d..91e53405c0 100644 --- a/substrate/subkey/src/vanity.rs +++ b/substrate/subkey/src/vanity.rs @@ -16,7 +16,6 @@ use rand::{OsRng, Rng}; use substrate_primitives::ed25519::Pair; -use std::cmp; fn good_waypoint(done: u64) -> u64 { match done { @@ -52,29 +51,26 @@ fn calculate_score(_desired: &str, key: &str) -> usize { let snip_size = _desired.len() - truncate; let truncated = &_desired[0..snip_size]; if let Some(pos) = key.find(truncated) { - let score = cmp::min(100, (51 - pos) + (snip_size * 50 / _desired.len())); - return score; + println!("pos is {} {}", pos, key); + return (47 - pos) + (snip_size * 48); } } 0 } -pub fn generate_key(_desired: &str, _amount: usize, paranoiac: bool) -> Result, &str> { - println!("Generating {} keys with pattern '{}'", _amount, &_desired); +pub fn generate_key(_desired: &str) -> Result { + println!("Generating key containing pattern '{}'", _desired); - let top = 30 + (_desired.len() * 32); + let top = 45 + (_desired.len() * 48); let mut best = 0; let mut seed = [0u8; 32]; let mut done = 0; - let mut res = vec![]; OsRng::new().unwrap().fill_bytes(&mut seed[..]); loop { - if res.len() >= _amount { break; } - - // reset to a new random seed at beginning and regularly after for paranoia. - if paranoiac || done % 100000 == 0 { + // reset to a new random seed at beginning and regularly thereafter + if done % 100000 == 0 { OsRng::new().unwrap().fill_bytes(&mut seed[..]); } @@ -88,22 +84,18 @@ pub fn generate_key(_desired: &str, _amount: usize, paranoiac: bool) -> Result