1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

#[cfg(test)]
mod test;

mod command;

use crate::command::rpc::RpcCommand;
use cfxcore::NodeType;
use clap::{crate_version, load_yaml, App, ArgMatches};
use client::{
    archive::ArchiveClient,
    common::{shutdown_handler, ClientTrait},
    configuration::Configuration,
    full::FullClient,
    light::LightClient,
};
use command::account::{AccountCmd, ImportAccounts, ListAccounts, NewAccount};
use log::{info, LevelFilter};
use log4rs::{
    append::{console::ConsoleAppender, file::FileAppender},
    config::{Appender, Config as LogConfig, Logger, Root},
    encode::pattern::PatternEncoder,
};
use network::throttling::THROTTLING_SERVICE;
use parking_lot::{Condvar, Mutex};
use std::sync::Arc;

fn main() -> Result<(), String> {
    #[cfg(feature = "deadlock-detection")]
    {
        // only for #[cfg]
        use parking_lot::deadlock;
        use std::{thread, time::Duration};

        // Create a background thread which checks for deadlocks every 10s
        thread::spawn(move || loop {
            thread::sleep(Duration::from_secs(10));
            let deadlocks = deadlock::check_deadlock();
            if deadlocks.is_empty() {
                continue;
            }

            eprintln!("{} deadlocks detected", deadlocks.len());
            for (i, threads) in deadlocks.iter().enumerate() {
                eprintln!("Deadlock #{}", i);
                for t in threads {
                    eprintln!("Thread Id {:#?}", t.thread_id());
                    eprintln!("{:#?}", t.backtrace());
                }
            }
        });
    } // only for #[cfg]

    let yaml = load_yaml!("cli.yaml");
    let version = parity_version::version(crate_version!());
    let matches = App::from_yaml(yaml).version(version.as_str()).get_matches();

    if let Some(output) = handle_sub_command(&matches)? {
        println!("{}", output);
        return Ok(());
    }

    let conf = Configuration::parse(&matches)?;

    // If log_conf is provided, use it for log configuration and ignore
    // log_file and log_level. Otherwise, set stdout to INFO and set
    // all our crate log to log_level.
    match conf.raw_conf.log_conf {
        Some(ref log_conf) => {
            log4rs::init_file(log_conf, Default::default()).map_err(|e| {
                format!(
                    "failed to initialize log with log config file: {:?}",
                    e
                )
            })?;
        }
        None => {
            let mut conf_builder =
                LogConfig::builder().appender(Appender::builder().build(
                    "stdout",
                    Box::new(ConsoleAppender::builder().build()),
                ));
            let mut root_builder = Root::builder().appender("stdout");
            if let Some(ref log_file) = conf.raw_conf.log_file {
                conf_builder =
                    conf_builder.appender(Appender::builder().build(
                        "logfile",
                        Box::new(
                            FileAppender::builder().encoder(
                                Box::new(
                                    PatternEncoder::new(
                                        "{d} {h({l}):5.5} {T:<20.20} {t:12.12} - {m}{n}")))
                                .build(log_file)
                                .map_err(
                                    |e| format!("failed to build log pattern: {:?}", e))?,
                        ),
                    ));
                root_builder = root_builder.appender("logfile");
            };
            // Should add new crate names here
            for crate_name in [
                "blockgen",
                "cfxcore",
                "cfx_statedb",
                "cfx_storage",
                "conflux",
                "db",
                "keymgr",
                "network",
                "txgen",
                "client",
                "primitives",
                "io",
            ]
            .iter()
            {
                conf_builder = conf_builder.logger(
                    Logger::builder()
                        .build(*crate_name, conf.raw_conf.log_level),
                );
            }
            let log_config = conf_builder
                .build(root_builder.build(LevelFilter::Info))
                .map_err(|e| format!("failed to build log config: {:?}", e))?;
            log4rs::init_config(log_config).map_err(|e| {
                format!("failed to initialize log with config: {:?}", e)
            })?;
        }
    };

    THROTTLING_SERVICE.write().initialize(
        conf.raw_conf.egress_queue_capacity,
        conf.raw_conf.egress_min_throttle,
        conf.raw_conf.egress_max_throttle,
    );

    let exit = Arc::new((Mutex::new(false), Condvar::new()));

    info!(
        "
:'######:::'#######::'##::: ##:'########:'##:::::::'##::::'##:'##::::'##:
'##... ##:'##.... ##: ###:: ##: ##.....:: ##::::::: ##:::: ##:. ##::'##::
 ##:::..:: ##:::: ##: ####: ##: ##::::::: ##::::::: ##:::: ##::. ##'##:::
 ##::::::: ##:::: ##: ## ## ##: ######::: ##::::::: ##:::: ##:::. ###::::
 ##::::::: ##:::: ##: ##. ####: ##...:::: ##::::::: ##:::: ##::: ## ##:::
 ##::: ##: ##:::: ##: ##:. ###: ##::::::: ##::::::: ##:::: ##:: ##:. ##::
. ######::. #######:: ##::. ##: ##::::::: ########:. #######:: ##:::. ##:
:......::::.......:::..::::..::..::::::::........:::.......:::..:::::..::
Current Version: {}
",
        version
    );

    let client_handle: Box<dyn ClientTrait>;
    client_handle = match conf.node_type() {
        NodeType::Archive => {
            info!("Starting archive client...");
            ArchiveClient::start(conf, exit.clone())
                .map_err(|e| format!("failed to start archive client: {}", e))?
        }
        NodeType::Full => {
            info!("Starting full client...");
            FullClient::start(conf, exit.clone())
                .map_err(|e| format!("failed to start full client: {}", e))?
        }
        NodeType::Light => {
            info!("Starting light client...");
            LightClient::start(conf, exit.clone())
                .map_err(|e| format!("failed to start light client: {}", e))?
        }
        NodeType::Unknown => return Err("Unknown node type".into()),
    };
    info!("Conflux client started");
    shutdown_handler::run(client_handle, exit);

    Ok(())
}

fn handle_sub_command(matches: &ArgMatches) -> Result<Option<String>, String> {
    if matches.subcommand_name().is_none() {
        return Ok(None);
    }

    // account sub-commands
    if let ("account", Some(account_matches)) = matches.subcommand() {
        let account_cmd = match account_matches.subcommand() {
            ("new", Some(new_acc_matches)) => {
                AccountCmd::New(NewAccount::new(new_acc_matches))
            }
            ("list", Some(list_acc_matches)) => {
                AccountCmd::List(ListAccounts::new(list_acc_matches))
            }
            ("import", Some(import_acc_matches)) => {
                AccountCmd::Import(ImportAccounts::new(import_acc_matches))
            }
            _ => unreachable!(),
        };
        let execute_output = command::account::execute(account_cmd)?;
        return Ok(Some(execute_output));
    }

    // general RPC commands
    let mut subcmd_matches = matches;
    while let Some(m) = subcmd_matches.subcommand().1 {
        subcmd_matches = m;
    }

    if let Some(cmd) = RpcCommand::parse(subcmd_matches)? {
        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(cmd.execute())?;
        return Ok(Some(result));
    }

    Ok(None)
}