github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/cmd/dummy_neb/main.cpp (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  #include "cmd/dummy_neb/dummy_callback.h"
    21  #include "cmd/dummy_neb/dummy_common.h"
    22  #include "cmd/dummy_neb/dummy_driver.h"
    23  #include "common/configuration.h"
    24  #include "fs/util.h"
    25  #include <boost/process.hpp>
    26  #include <boost/program_options.hpp>
    27  
    28  namespace po = boost::program_options;
    29  namespace bp = boost::process;
    30  po::variables_map get_variables_map(int argc, char *argv[]) {
    31    po::options_description desc("Dummy neb");
    32    // clang-format off
    33    desc.add_options()("help", "show help message")
    34      ("list-dummies", "show all dummy names")
    35      ("run-dummy", po::value<std::string>()->default_value("default_random"), "run a dummy with name (from list-dummies, default [default_random])")
    36      ("block-interval", po::value<uint64_t>()->default_value(3), "block interval with seconds")
    37      ("without-clean-db", po::value<bool>()->default_value(true), "run a dummy without clean previous db")
    38      ("clean-dummy-db", po::value<std::string>(), "clean the db file of a dummy")
    39      ("glog-log-to-stderr", po::value<bool>()->default_value(false), "glog to stderr")
    40      ("use-test-blockchain", po::value<bool>()->default_value(true), "use test blockchain")
    41      ("nipc-listen", po::value<std::string>()->default_value("127.0.0.1"), "nipc listen")
    42      ("nipc-port", po::value<uint16_t>()->default_value(6987), "nipc port")
    43      ("rpc-listen", po::value<std::string>()->default_value("127.0.0.1"), "nipc listen")
    44      ("rpc-port", po::value<uint16_t>()->default_value(0x1958), "nipc port")
    45      ("enable-nbre-killer", po::value<uint64_t>()->default_value(24*60), "kill nbre periodically in miniutes");
    46    // clang-format on
    47  
    48    po::variables_map vm;
    49    po::store(po::parse_command_line(argc, argv, desc), vm);
    50    po::notify(vm);
    51  
    52    if (vm.count("help")) {
    53      std::cout << desc << "\n";
    54      exit(1);
    55    }
    56  
    57    return vm;
    58  }
    59  
    60  void init_dummy_driver(dummy_driver &dd, const std::string &rpc_listen,
    61                         uint16_t rpc_port) {
    62    auto default_dummy = std::make_shared<random_dummy>(
    63        "default_random", 20, 10000_nas, 0.05, rpc_listen, rpc_port);
    64    default_dummy->enable_auth_gen_with_ratio(1, 1);
    65    default_dummy->enable_nr_ir_with_ratio(1, 20);
    66    default_dummy->enable_dip_ir_with_ratio(1, 20);
    67    default_dummy->enable_call_tx_with_ratio(1, 1);
    68    dd.add_dummy(default_dummy);
    69  
    70    // auto stress = std::make_shared<stress_dummy>(
    71    //"stress", 20, 10000_nas, 100, 10, 10000, 1000, rpc_listen, rpc_port);
    72    // dd.add_dummy(stress);
    73  }
    74  
    75  void init_and_start_nbre(const address_t &auth_admin_addr,
    76                           const std::string &neb_db_dir,
    77                           const std::string &nipc_listen, uint16_t nipc_port) {
    78  
    79    const char *root_dir = neb::configuration::instance().nbre_root_dir().c_str();
    80    std::string nbre_path = neb::fs::join_path(root_dir, "bin/nbre");
    81  
    82    set_recv_nbre_version_callback(nbre_version_callback);
    83    set_recv_nbre_ir_list_callback(nbre_ir_list_callback);
    84    set_recv_nbre_ir_versions_callback(nbre_ir_versions_callback);
    85    set_recv_nbre_nr_handle_callback(nbre_nr_handle_callback);
    86    set_recv_nbre_nr_result_by_handle_callback(nbre_nr_result_callback);
    87    set_recv_nbre_nr_result_by_height_callback(nbre_nr_result_by_height_callback);
    88    set_recv_nbre_nr_sum_callback(nbre_nr_sum_callback);
    89    set_recv_nbre_dip_reward_callback(nbre_dip_reward_callback);
    90  
    91    uint64_t nbre_start_height = 1;
    92    std::string auth_addr_str = auth_admin_addr.to_base58();
    93    neb::configuration::instance().neb_db_dir() = neb_db_dir;
    94    nbre_params_t params{
    95        root_dir,
    96        nbre_path.c_str(),
    97        neb::configuration::instance().neb_db_dir().c_str(),
    98        neb::configuration::instance().nbre_db_dir().c_str(),
    99        neb::configuration::instance().nbre_log_dir().c_str(),
   100        auth_addr_str.c_str(),
   101        nbre_start_height,
   102    };
   103  
   104    LOG(INFO) << "auth admin addr: " << auth_admin_addr.to_hex();
   105    LOG(INFO) << "init auth admin addr: " << params.m_admin_pub_addr;
   106    params.m_nipc_listen = nipc_listen.c_str();
   107    params.m_nipc_port = nipc_port;
   108  
   109    auto ret = start_nbre_ipc(params);
   110    if (ret != ipc_status_succ) {
   111      nbre_ipc_shutdown();
   112      exit(-1);
   113    }
   114  }
   115  
   116  int main(int argc, char *argv[]) {
   117  
   118    dummy_driver dd;
   119    std::thread quiter_thrd([&]() {
   120      char c = 'a';
   121      while (c != 'x') {
   122        std::cin >> c;
   123      }
   124      neb::core::command_queue::instance().send_command(
   125          std::make_shared<neb::core::exit_command>());
   126      nbre_ipc_shutdown();
   127      LOG(INFO) << "nbre_ipc_shutdown shut down done";
   128  
   129      dd.shutdown();
   130    });
   131    struct _t {
   132      _t(std::thread &t) : thread(t) {}
   133      ~_t() {
   134        thread.join();
   135        neb::fs::bc_storage_session::instance().release();
   136      }
   137      std::thread &thread;
   138    } __(quiter_thrd);
   139  
   140    std::string root_dir = neb::configuration::instance().nbre_root_dir();
   141    neb::configuration::instance().neb_db_dir() =
   142        neb::fs::join_path(root_dir, std::string("dummy_db"));
   143  
   144    FLAGS_logtostderr = false;
   145  
   146    po::variables_map vm = get_variables_map(argc, argv);
   147    neb::glog_log_to_stderr = vm["glog-log-to-stderr"].as<bool>();
   148    neb::use_test_blockchain = vm["use-test-blockchain"].as<bool>();
   149    LOG(INFO) << "log-to-stderr: " << neb::glog_log_to_stderr;
   150  
   151    std::string rpc_listen = vm["rpc-listen"].as<std::string>();
   152    uint16_t rpc_port = vm["rpc-port"].as<uint16_t>();
   153  
   154    init_dummy_driver(dd, rpc_listen, rpc_port);
   155    if (vm.count("list-dummies")) {
   156      auto dummies = dd.get_all_dummy_names();
   157      std::for_each(dummies.begin(), dummies.end(), [](const std::string &s) {
   158        std::cout << "\t" << s << std::endl;
   159      });
   160      return 1;
   161    }
   162  
   163    if (vm.count("run-dummy")) {
   164      std::string dummy_name = vm["run-dummy"].as<std::string>();
   165      uint64_t interval = vm["block-interval"].as<uint64_t>();
   166      bool without_clean_db = vm["without-clean-db"].as<bool>();
   167      if (!without_clean_db) {
   168        dd.reset_dummy(dummy_name);
   169      }
   170      auto dummy = dd.get_dummy_with_name(dummy_name);
   171      std::string db_path = dummy->db_path();
   172      bc_storage_session::instance().init(db_path,
   173                                          neb::fs::storage_open_for_readwrite);
   174      auto admin_addr = dummy->get_auth_admin_addr();
   175  
   176      std::thread thrd([&dd, dummy_name, interval]() {
   177        dd.run(dummy_name, interval);
   178        nbre_ipc_shutdown();
   179      });
   180  
   181      std::this_thread::sleep_for(std::chrono::seconds(interval));
   182  
   183      std::string nipc_listen = vm["nipc-listen"].as<std::string>();
   184      uint16_t nipc_port = vm["nipc-port"].as<uint16_t>();
   185      init_and_start_nbre(admin_addr, db_path, nipc_listen, nipc_port);
   186      LOG(INFO) << "init nbre done!";
   187      thrd.join();
   188      LOG(INFO) << "to quit nbre";
   189      return 0;
   190    }
   191    if (vm.count("clean-dummy-db")) {
   192      std::string dummy_name = vm["clean-dummy-db"].as<std::string>();
   193      dd.reset_dummy(dummy_name);
   194      return 0;
   195    }
   196  
   197    return 0;
   198  }