github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/cmd/dummy_neb/cli/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/cli/pkg.h" 21 #include "common/configuration.h" 22 #include "fs/util.h" 23 #include "util/controller.h" 24 #include <boost/process.hpp> 25 #include <boost/program_options.hpp> 26 #include <ff/network.h> 27 28 namespace po = boost::program_options; 29 namespace bp = boost::process; 30 31 po::variables_map get_variables_map(int argc, char *argv[]) { 32 po::options_description desc("Dummy CLI tool"); 33 // clang-format off 34 desc.add_options()("help", "show help message") 35 ("brief", "show current dummy brief") 36 ("payload", po::value<std::string>(), "payload file path") 37 ("submit", po::value<std::string>(), "auth, nr, dip") 38 ("query", po::value<std::string>(), "nr, nr-result, dip-reward") 39 ("start-block", po::value<uint64_t>(), "start block height") 40 ("end-block", po::value<uint64_t>(), "end block height") 41 ("version", po::value<std::string>()->default_value("0.0.1"), "x.x.x") 42 ("handle", po::value<std::string>(), "request handle") 43 ("height", po::value<uint64_t>()->default_value(0), "request height") 44 ("rpc-listen", po::value<std::string>()->default_value("127.0.0.1"), "nipc listen") 45 ("rpc-port", po::value<uint16_t>()->default_value(0x1958), "nipc port") 46 ("kill-nbre", "kill nbre immediatelly"); 47 /* 48 ("run-dummy", po::value<std::string>()->default_value("default_random"), "run a dummy with name (from list-dummies, default [default_random])") 49 ("block-interval", po::value<uint64_t>()->default_value(3), "block interval with seconds") 50 ("without-clean-db", "run a dummy without clean previous db") 51 ("clean-dummy-db", po::value<std::string>(), "clean the db file of a dummy") 52 ("enable-nbre-killer", po::value<uint64_t>()->default_value(24*60), "kill nbre periodically in miniutes"); 53 */ 54 // clang-format on 55 56 po::variables_map vm; 57 po::store(po::parse_command_line(argc, argv, desc), vm); 58 po::notify(vm); 59 60 if (vm.count("help")) { 61 std::cout << desc << "\n"; 62 exit(1); 63 } 64 65 return vm; 66 } 67 68 class cli_executor { 69 public: 70 cli_executor(const std::string &rpc_listen, uint16_t rpc_port) 71 : m_rpc_listen(rpc_listen), m_rpc_port(rpc_port) {} 72 73 void send_brief_req() { 74 std::shared_ptr<cli_brief_req_t> req = std::make_shared<cli_brief_req_t>(); 75 m_package = req; 76 start_and_join(); 77 } 78 void send_submit_ir(const std::string &type, const std::string &fp) { 79 std::ifstream ifs; 80 ifs.open(fp); 81 if (!ifs.is_open()) { 82 std::cout << "cannot open file: " << fp << std::endl; 83 } 84 ifs.seekg(0, ifs.end); 85 std::ifstream::pos_type size = ifs.tellg(); 86 neb::bytes buf(size); 87 88 ifs.seekg(0, ifs.beg); 89 ifs.read((char *)buf.value(), buf.size()); 90 ifs.close(); 91 92 std::shared_ptr<cli_submit_ir_t> req = std::make_shared<cli_submit_ir_t>(); 93 req->set<p_type>(type); 94 req->set<p_payload>(neb::byte_to_string(buf)); 95 m_package = req; 96 start_and_join(); 97 } 98 99 void send_nr_req(uint64_t start_block, uint64_t end_block, uint64_t version) { 100 std::shared_ptr<nbre_nr_handle_req> req = 101 std::make_shared<nbre_nr_handle_req>(); 102 req->set<p_holder>(reinterpret_cast<uint64_t>(this)); 103 req->set<p_start_block>(start_block); 104 req->set<p_end_block>(end_block); 105 req->set<p_nr_version>(version); 106 m_package = req; 107 start_and_join(); 108 } 109 void send_nr_result_req(const std::string &handle) { 110 std::shared_ptr<nbre_nr_result_by_handle_req> req = 111 std::make_shared<nbre_nr_result_by_handle_req>(); 112 req->set<p_holder>(reinterpret_cast<uint64_t>(this)); 113 req->set<p_nr_handle>(handle); 114 m_package = req; 115 start_and_join(); 116 } 117 118 void send_nr_result_by_height_req(uint64_t height) { 119 std::shared_ptr<nbre_nr_result_by_height_req> req = 120 std::make_shared<nbre_nr_result_by_height_req>(); 121 req->set<p_holder>(reinterpret_cast<uint64_t>(this)); 122 req->set<p_height>(height); 123 m_package = req; 124 start_and_join(); 125 } 126 127 void send_nr_sum_req(uint64_t height) { 128 std::shared_ptr<nbre_nr_sum_req> req = std::make_shared<nbre_nr_sum_req>(); 129 req->set<p_holder>(reinterpret_cast<uint64_t>(this)); 130 req->set<p_height>(height); 131 m_package = req; 132 start_and_join(); 133 } 134 135 void send_dip_reward_req(uint64_t height, uint64_t version) { 136 std::shared_ptr<nbre_dip_reward_req> req = 137 std::make_shared<nbre_dip_reward_req>(); 138 req->set<p_holder>(reinterpret_cast<uint64_t>(this)); 139 req->set<p_height>(height); 140 req->set<p_version>(version); 141 m_package = req; 142 start_and_join(); 143 } 144 145 protected: 146 void start_and_join() { 147 148 ff::net::net_nervure nn; 149 150 ff::net::typed_pkg_hub hub; 151 ff::net::tcp_connection_base_ptr conn; 152 nn.get_event_handler()->listen<::ff::net::event::tcp_get_connection>( 153 [&, this](::ff::net::tcp_connection_base *conn) { 154 conn->send(m_package); 155 }); 156 157 hub.to_recv_pkg<cli_brief_ack_t>([&](std::shared_ptr<cli_brief_ack_t> ack) { 158 std::cout << "\t height: " << ack->get<p_height>() << std::endl; 159 std::cout << "\t account num: " << ack->get<p_account_num>() << std::endl; 160 std::cout << "\t " << ack->get<p_checker_status>() << std::endl; 161 ; 162 conn->close(); 163 exit(-1); 164 }); 165 166 hub.to_recv_pkg<cli_submit_ack_t>( 167 [&](std::shared_ptr<cli_submit_ack_t> ack) { 168 std::cout << "\t result: " << ack->get<p_result>() << std::endl; 169 conn->close(); 170 exit(-1); 171 }); 172 hub.to_recv_pkg<nbre_nr_result_by_handle_ack>( 173 [&](std::shared_ptr<nbre_nr_result_by_handle_ack> ack) { 174 std::cout << "\t " << ack->get<p_nr_result>() << std::endl; 175 conn->close(); 176 exit(-1); 177 }); 178 hub.to_recv_pkg<nbre_nr_handle_ack>( 179 [&](std::shared_ptr<nbre_nr_handle_ack> ack) { 180 std::cout << "\t" << ack->get<p_nr_handle>() << std::endl; 181 conn->close(); 182 exit(-1); 183 }); 184 185 hub.to_recv_pkg<nbre_nr_result_by_height_ack>( 186 [&](std::shared_ptr<nbre_nr_result_by_height_ack> ack) { 187 std::cout << "\t " << ack->get<p_nr_result>() << std::endl; 188 conn->close(); 189 exit(-1); 190 }); 191 hub.to_recv_pkg<nbre_nr_sum_ack>([&](std::shared_ptr<nbre_nr_sum_ack> ack) { 192 std::cout << "\t " << ack->get<p_nr_sum>() << std::endl; 193 conn->close(); 194 exit(-1); 195 }); 196 hub.to_recv_pkg<nbre_dip_reward_ack>( 197 [&](std::shared_ptr<nbre_dip_reward_ack> ack) { 198 std::cout << "\t" << ack->get<p_dip_reward>() << std::endl; 199 conn->close(); 200 exit(-1); 201 }); 202 nn.add_pkg_hub(hub); 203 conn = nn.add_tcp_client(m_rpc_listen, m_rpc_port); 204 205 nn.run(); 206 } 207 208 protected: 209 std::shared_ptr<ff::net::package> m_package; 210 std::string m_rpc_listen; 211 uint16_t m_rpc_port; 212 }; 213 214 int main(int argc, char *argv[]) { 215 po::variables_map vm = get_variables_map(argc, argv); 216 std::string rpc_listen = vm["rpc-listen"].as<std::string>(); 217 uint16_t rpc_port = vm["rpc-port"].as<uint16_t>(); 218 219 if (vm.count("submit")) { 220 std::string type = vm["submit"].as<std::string>(); 221 if (type != "nr" && type != "auth" && type != "dip") { 222 std::cout << "invalid type " << type << std::endl; 223 exit(-1); 224 } 225 if (!vm.count("payload")) { 226 std::cout << "no payload " << std::endl; 227 exit(-1); 228 } 229 std::string fp = vm["payload"].as<std::string>(); 230 231 cli_executor ce(rpc_listen, rpc_port); 232 ce.send_submit_ir(type, fp); 233 } else if (vm.count("brief")) { 234 cli_executor ce(rpc_listen, rpc_port); 235 ce.send_brief_req(); 236 } else if (vm.count("query")) { 237 std::string type = vm["query"].as<std::string>(); 238 if (type != "nr" && type != "nr-result" && type != "nr-sum" && 239 type != "dip-reward") { 240 std::cout << "invalid type " << type << std::endl; 241 exit(-1); 242 } 243 if (type == "nr") { 244 if (!vm.count("start-block") || !vm.count("end-block") || 245 !vm.count("version")) { 246 std::cout << "no start, end block, or version" << std::endl; 247 exit(-1); 248 } 249 auto start_block = vm["start-block"].as<uint64_t>(); 250 auto end_block = vm["end-block"].as<uint64_t>(); 251 auto version_str = vm["version"].as<std::string>(); 252 neb::version v; 253 v.from_string(version_str); 254 cli_executor ce(rpc_listen, rpc_port); 255 ce.send_nr_req(start_block, end_block, v.data()); 256 } 257 if (type == "nr-result") { 258 if (vm.count("handle")) { 259 auto handle = vm["handle"].as<std::string>(); 260 cli_executor ce(rpc_listen, rpc_port); 261 ce.send_nr_result_req(handle); 262 } else if (vm.count("height")) { 263 auto height = vm["height"].as<uint64_t>(); 264 LOG(INFO) << "cli query nr-result by height " << height; 265 cli_executor ce(rpc_listen, rpc_port); 266 ce.send_nr_result_by_height_req(height); 267 } 268 } 269 if (type == "nr-sum") { 270 if (vm.count("height")) { 271 auto height = vm["height"].as<uint64_t>(); 272 LOG(INFO) << "cli query nr-sum by height " << height; 273 cli_executor ce(rpc_listen, rpc_port); 274 ce.send_nr_sum_req(height); 275 } 276 } 277 if (type == "dip-reward") { 278 auto height = vm["height"].as<uint64_t>(); 279 auto version_str = vm["version"].as<std::string>(); 280 neb::version v; 281 v.from_string(version_str); 282 cli_executor ce(rpc_listen, rpc_port); 283 ce.send_dip_reward_req(height, v.data()); 284 } 285 } else if (vm.count("kill-nbre")) { 286 neb::util::magic_wand mw; 287 mw.kill_nbre(); 288 } 289 return 0; 290 }