<commit_before>use std::thread;
use std::net::SocketAddr;

use log::{LogRecord, LogLevel, SetLoggerError};
use env_logger::LogBuilder;
use colored::*;
use router::Router;
use iron::{Chain, Iron};

use std::env;
use std::time::{SystemTime, UNIX_EPOCH};

use blockchain::{GenesisConfig, ApiContext};
use node::{NodeConfig, Node};
use crypto::gen_keypair;
use explorer::ExplorerApi;
use api::Api;

pub mod clap;

pub fn init_logger() -> Result<(), SetLoggerError> {
    let mut builder = LogBuilder::new();
    builder.format(format_log_record);

    if env::var("RUST_LOG").is_ok() {
        builder.parse(&env::var("RUST_LOG").unwrap());
    }

    builder.init()
}

pub fn generate_testnet_config(count: u8, start_port: u16) -> Vec<NodeConfig> {
    let validators = (0..count as usize)
        .map(|_| gen_keypair())
        .collect::<Vec<_>>();
    let genesis = GenesisConfig::new(validators.iter().map(|x| x.0));
    let peers = (0..validators.len())
        .map(|x| {
                 format!("127.0.0.1:{}", start_port + x as u16)
                     .parse()
                     .unwrap()
             })
        .collect::<Vec<_>>();

    validators
        .into_iter()
        .enumerate()
        .map(|(idx, validator)| {
            NodeConfig {
                listen_address: peers[idx],
                network: Default::default(),
                peers: peers.clone(),
                public_key: validator.0,
                secret_key: validator.1,
                genesis: genesis.clone(),
            }
        })
        .collect::<Vec<_>>()
}

/// Options for the `run_node_with_api` function.
pub struct NodeRunOptions {
    /// Enable api endpoints for the `blockchain_explorer` on public api address.
    pub enable_explorer: bool,
    /// Listen address for public api endpoints
    pub public_api_address: Option<SocketAddr>,
    /// Listen address for private api endpoints
    pub private_api_address: Option<SocketAddr>,
}

/// A generic implementation that launches `Node` and optionally creates threads 
/// for public and private api handlers.
pub fn run_node_with_api(mut node: Node, options: NodeRunOptions) {
    let blockchain = node.handler().blockchain.clone();
    let private_config_api_thread = match options.private_api_address {
        Some(listen_address) => {
            let mut router = Router::new();
            let api_context = ApiContext::new(&node);
            api_context.wire_private_api(&mut router);

            let thread = thread::spawn(move || {
                info!("Private exonum api started on {}", listen_address);
                let chain = Chain::new(router);
                Iron::new(chain).http(listen_address).unwrap();<commit_msg>Fix clippy warning<commit_after>use std::thread;
use std::net::SocketAddr;

use log::{LogRecord, LogLevel, SetLoggerError};
use env_logger::LogBuilder;
use colored::*;
use router::Router;
use iron::{Chain, Iron};

use std::env;
use std::time::{SystemTime, UNIX_EPOCH};

use blockchain::{GenesisConfig, ApiContext};
use node::{NodeConfig, Node};
use crypto::gen_keypair;
use explorer::ExplorerApi;
use api::Api;

pub mod clap;

pub fn init_logger() -> Result<(), SetLoggerError> {
    let mut builder = LogBuilder::new();
    builder.format(format_log_record);

    if env::var("RUST_LOG").is_ok() {
        builder.parse(&env::var("RUST_LOG").unwrap());
    }

    builder.init()
}

pub fn generate_testnet_config(count: u8, start_port: u16) -> Vec<NodeConfig> {
    let validators = (0..count as usize)
        .map(|_| gen_keypair())
        .collect::<Vec<_>>();
    let genesis = GenesisConfig::new(validators.iter().map(|x| x.0));
    let peers = (0..validators.len())
        .map(|x| {
                 format!("127.0.0.1:{}", start_port + x as u16)
                     .parse()
                     .unwrap()
             })
        .collect::<Vec<_>>();

    validators
        .into_iter()
        .enumerate()
        .map(|(idx, validator)| {
            NodeConfig {
                listen_address: peers[idx],
                network: Default::default(),
                peers: peers.clone(),
                public_key: validator.0,
                secret_key: validator.1,
                genesis: genesis.clone(),
            }
        })
        .collect::<Vec<_>>()
}

/// Options for the `run_node_with_api` function.
pub struct NodeRunOptions {
    /// Enable api endpoints for the `blockchain_explorer` on public api address.
    pub enable_explorer: bool,
    /// Listen address for public api endpoints
    pub public_api_address: Option<SocketAddr>,
    /// Listen address for private api endpoints
    pub private_api_address: Option<SocketAddr>,
}

/// A generic implementation that launches `Node` and optionally creates threads 
/// for public and private api handlers.
pub fn run_node_with_api(mut node: Node, options: &NodeRunOptions) {
    let blockchain = node.handler().blockchain.clone();
    let private_config_api_thread = match options.private_api_address {
        Some(listen_address) => {
            let mut router = Router::new();
            let api_context = ApiContext::new(&node);
            api_context.wire_private_api(&mut router);

            let thread = thread::spawn(move || {
                info!("Private exonum api started on {}", listen_address);
                let chain = Chain::new(router);
                Iron::new(chain).http(listen_address).unwrap();
{'fraction_non_alphanumeric': 0.13783584836216414, 'fraction_numerical': 0.005888847994111152, 'mean_word_length': 2.620919387075283, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4118569', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1590, 'n_words': 387}