| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include "string_tools.h" |
| 32 | #include "common/password.h" |
| 33 | #include "common/scoped_message_writer.h" |
| 34 | #include "common/pruning.h" |
| 35 | #include "daemon/rpc_command_executor.h" |
| 36 | #include "rpc/core_rpc_server_commands_defs.h" |
| 37 | #include "cryptonote_core/cryptonote_core.h" |
| 38 | #include "cryptonote_basic/difficulty.h" |
| 39 | #include "cryptonote_basic/hardfork.h" |
| 40 | #include "rpc/rpc_payment_signature.h" |
| 41 | #include "rpc/rpc_version_str.h" |
| 42 | #include <boost/format.hpp> |
| 43 | #include <ctime> |
| 44 | #include <string> |
| 45 | |
| 46 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 47 | #define MONERO_DEFAULT_LOG_CATEGORY "daemon" |
| 48 | |
| 49 | namespace daemonize { |
| 50 | |
| 51 | namespace { |
| 52 | const char *get_address_type_name(epee::net_utils::address_type address_type) |
| 53 | { |
| 54 | switch (address_type) |
| 55 | { |
| 56 | default: |
| 57 | case epee::net_utils::address_type::invalid: return "invalid"; |
| 58 | case epee::net_utils::address_type::ipv4: return "IPv4"; |
| 59 | case epee::net_utils::address_type::ipv6: return "IPv6"; |
| 60 | case epee::net_utils::address_type::i2p: return "I2P"; |
| 61 | case epee::net_utils::address_type::tor: return "Tor"; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | std::string print_float(float f, int prec) |
| 66 | { |
| 67 | char buf[16]; |
| 68 | snprintf(buf, sizeof(buf), "%*.*f", prec, prec, f); |
| 69 | return buf; |
| 70 | } |
| 71 | |
| 72 | void print_peer(std::string const & prefix, cryptonote::peer const & peer, bool pruned_only, bool publicrpc_only) |
| 73 | { |
| 74 | if (pruned_only && peer.pruning_seed == 0) |
| 75 | return; |
| 76 | if (publicrpc_only && peer.rpc_port == 0) |
| 77 | return; |
| 78 | |
| 79 | time_t now; |
| 80 | time(&now); |
| 81 | time_t last_seen = static_cast<time_t>(peer.last_seen); |
| 82 | |
| 83 | std::string elapsed = peer.last_seen == 0 ? "never" : epee::misc_utils::get_time_interval_string(now - last_seen); |
| 84 | std::string id_str = epee::string_tools::pad_string(epee::string_tools::to_string_hex(peer.id), 16, '0', true); |
| 85 | std::string port_str; |
| 86 | epee::string_tools::xtype_to_string(peer.port, port_str); |
| 87 | std::string addr_str = peer.host + ":" + port_str; |
| 88 | std::string rpc_port = peer.rpc_port ? std::to_string(peer.rpc_port) : "-"; |
| 89 | std::string rpc_credits_per_hash = peer.rpc_credits_per_hash ? print_float(peer.rpc_credits_per_hash / RPC_CREDITS_PER_HASH_SCALE, 2) : "-"; |
| 90 | std::string pruning_seed = epee::string_tools::to_string_hex(peer.pruning_seed); |
| 91 | tools::msg_writer() << boost::format("%-10s %-25s %-25s %-5s %-5s %-4s %s") % prefix % id_str % addr_str % rpc_port % rpc_credits_per_hash % pruning_seed % elapsed; |
| 92 | } |
| 93 | |
| 94 | void print_block_header(cryptonote::block_header_response const & header) |
| 95 | { |
| 96 | tools::success_msg_writer() |
| 97 | << "timestamp: " << boost::lexical_cast<std::string>(header.timestamp) << " (" << tools::get_human_readable_timestamp(header.timestamp) << ")" << std::endl |
| 98 | << "previous hash: " << header.prev_hash << std::endl |
| 99 | << "nonce: " << boost::lexical_cast<std::string>(header.nonce) << std::endl |
| 100 | << "is orphan: " << header.orphan_status << std::endl |
| 101 | << "height: " << boost::lexical_cast<std::string>(header.height) << std::endl |
| 102 | << "depth: " << boost::lexical_cast<std::string>(header.depth) << std::endl |
| 103 | << "hash: " << header.hash << std::endl |
| 104 | << "difficulty: " << cryptonote::difficulty_type(header.wide_difficulty) << std::endl |
| 105 | << "cumulative difficulty: " << cryptonote::difficulty_type(header.wide_cumulative_difficulty) << std::endl |
| 106 | << "POW hash: " << header.pow_hash << std::endl |
| 107 | << "block size: " << header.block_size << std::endl |
| 108 | << "block weight: " << header.block_weight << std::endl |
| 109 | << "long term weight: " << header.long_term_weight << std::endl |
| 110 | << "num txes: " << header.num_txes << std::endl |
| 111 | << "reward: " << cryptonote::print_money(header.reward) << std::endl |
| 112 | << "miner tx hash: " << header.miner_tx_hash; |
| 113 | } |
| 114 | |
| 115 | std::string get_human_time_ago(time_t t, time_t now) |
| 116 | { |
| 117 | if (t == now) |
| 118 | return "now"; |
| 119 | time_t dt = t > now ? t - now : now - t; |
| 120 | std::string s; |
| 121 | if (dt < 90) |
| 122 | s = boost::lexical_cast<std::string>(dt) + " seconds"; |
| 123 | else if (dt < 90 * 60) |
| 124 | s = boost::lexical_cast<std::string>(dt/60) + " minutes"; |
| 125 | else if (dt < 36 * 3600) |
| 126 | s = boost::lexical_cast<std::string>(dt/3600) + " hours"; |
| 127 | else |
| 128 | s = boost::lexical_cast<std::string>(dt/(3600*24)) + " days"; |
| 129 | return s + " " + (t > now ? "in the future" : "ago"); |
| 130 | } |
| 131 | |
| 132 | std::string get_time_hms(time_t t) |
| 133 | { |
| 134 | unsigned int hours, minutes, seconds; |
| 135 | char buffer[24]; |
| 136 | hours = t / 3600; |
| 137 | t %= 3600; |
| 138 | minutes = t / 60; |
| 139 | t %= 60; |
| 140 | seconds = t; |
| 141 | snprintf(buffer, sizeof(buffer), "%02u:%02u:%02u", hours, minutes, seconds); |
| 142 | return std::string(buffer); |
| 143 | } |
| 144 | |
| 145 | std::string make_error(const std::string &base, const std::string &status) |
| 146 | { |
| 147 | if (status == CORE_RPC_STATUS_OK) |
| 148 | return base; |
| 149 | return base + " -- " + status; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | t_rpc_command_executor::t_rpc_command_executor( |
| 154 | uint32_t ip |
| 155 | , uint16_t port |
| 156 | , const boost::optional<tools::login>& login |
| 157 | , const epee::net_utils::ssl_options_t& ssl_options |
| 158 | , bool is_rpc |
| 159 | , cryptonote::core_rpc_server* rpc_server |
| 160 | ) |
| 161 | : m_rpc_client(NULL), m_rpc_server(rpc_server) |
| 162 | { |
| 163 | if (is_rpc) |
| 164 | { |
| 165 | boost::optional<epee::net_utils::http::login> http_login{}; |
| 166 | if (login) |
| 167 | http_login.emplace(login->username, login->password.password()); |
| 168 | m_rpc_client = new tools::t_rpc_client(ip, port, std::move(http_login), ssl_options); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | if (rpc_server == NULL) |
| 173 | { |
| 174 | throw std::runtime_error("If not calling commands via RPC, rpc_server pointer must be non-null"); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | m_is_rpc = is_rpc; |
| 179 | } |
| 180 | |
| 181 | t_rpc_command_executor::~t_rpc_command_executor() |
| 182 | { |
| 183 | if (m_rpc_client != NULL) |
| 184 | { |
| 185 | delete m_rpc_client; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | bool t_rpc_command_executor::print_peer_list(bool white, bool gray, size_t limit, bool pruned_only, bool publicrpc_only) { |
| 190 | cryptonote::COMMAND_RPC_GET_PEER_LIST::request req; |
| 191 | cryptonote::COMMAND_RPC_GET_PEER_LIST::response res; |
| 192 | |
| 193 | std::string failure_message = "Couldn't retrieve peer list"; |
| 194 | |
| 195 | req.include_blocked = true; |
| 196 | |
| 197 | if (m_is_rpc) |
| 198 | { |
| 199 | if (!m_rpc_client->rpc_request(req, res, "/get_peer_list", failure_message.c_str())) |
| 200 | { |
| 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | if (!m_rpc_server->on_get_peer_list(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 207 | { |
| 208 | tools::fail_msg_writer() << failure_message; |
| 209 | return false; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (white) |
| 214 | { |
| 215 | auto peer = res.white_list.cbegin(); |
| 216 | const auto end = limit ? peer + std::min(limit, res.white_list.size()) : res.white_list.cend(); |
| 217 | for (; peer != end; ++peer) |
| 218 | { |
| 219 | print_peer("white", *peer, pruned_only, publicrpc_only); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if (gray) |
| 224 | { |
| 225 | auto peer = res.gray_list.cbegin(); |
| 226 | const auto end = limit ? peer + std::min(limit, res.gray_list.size()) : res.gray_list.cend(); |
| 227 | for (; peer != end; ++peer) |
| 228 | { |
| 229 | print_peer("gray", *peer, pruned_only, publicrpc_only); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | bool t_rpc_command_executor::print_peer_list_stats() { |
| 237 | cryptonote::COMMAND_RPC_GET_PEER_LIST::request req; |
| 238 | cryptonote::COMMAND_RPC_GET_PEER_LIST::response res; |
| 239 | |
| 240 | std::string failure_message = "Couldn't retrieve peer list"; |
| 241 | |
| 242 | req.public_only = false; |
| 243 | req.include_blocked = true; |
| 244 | |
| 245 | if (m_is_rpc) |
| 246 | { |
| 247 | if (!m_rpc_client->rpc_request(req, res, "/get_peer_list", failure_message.c_str())) |
| 248 | { |
| 249 | return false; |
| 250 | } |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | if (!m_rpc_server->on_get_peer_list(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 255 | { |
| 256 | tools::fail_msg_writer() << failure_message; |
| 257 | return false; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | tools::msg_writer() |
| 262 | << "White list size: " << res.white_list.size() << "/" << P2P_LOCAL_WHITE_PEERLIST_LIMIT << " (" << res.white_list.size() * 100.0 / P2P_LOCAL_WHITE_PEERLIST_LIMIT << "%)" << std::endl |
| 263 | << "Gray list size: " << res.gray_list.size() << "/" << P2P_LOCAL_GRAY_PEERLIST_LIMIT << " (" << res.gray_list.size() * 100.0 / P2P_LOCAL_GRAY_PEERLIST_LIMIT << "%)"; |
| 264 | |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | bool t_rpc_command_executor::save_blockchain() { |
| 269 | cryptonote::COMMAND_RPC_SAVE_BC::request req; |
| 270 | cryptonote::COMMAND_RPC_SAVE_BC::response res; |
| 271 | |
| 272 | std::string fail_message = "Couldn't save blockchain"; |
| 273 | |
| 274 | if (m_is_rpc) |
| 275 | { |
| 276 | if (!m_rpc_client->rpc_request(req, res, "/save_bc", fail_message.c_str())) |
| 277 | { |
| 278 | return true; |
| 279 | } |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | if (!m_rpc_server->on_save_bc(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 284 | { |
| 285 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 286 | return true; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | tools::success_msg_writer() << "Blockchain saved"; |
| 291 | |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | bool t_rpc_command_executor::show_hash_rate() { |
| 296 | cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::request req; |
| 297 | cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::response res; |
| 298 | req.visible = true; |
| 299 | |
| 300 | std::string fail_message = "Unsuccessful"; |
| 301 | |
| 302 | if (m_is_rpc) |
| 303 | { |
| 304 | if (!m_rpc_client->rpc_request(req, res, "/set_log_hash_rate", fail_message.c_str())) |
| 305 | { |
| 306 | return true; |
| 307 | } |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | if (!m_rpc_server->on_set_log_hash_rate(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 312 | { |
| 313 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | tools::success_msg_writer() << "Hash rate logging is on"; |
| 318 | |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | bool t_rpc_command_executor::hide_hash_rate() { |
| 323 | cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::request req; |
| 324 | cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::response res; |
| 325 | req.visible = false; |
| 326 | |
| 327 | std::string fail_message = "Unsuccessful"; |
| 328 | |
| 329 | if (m_is_rpc) |
| 330 | { |
| 331 | if (!m_rpc_client->rpc_request(req, res, "/set_log_hash_rate", fail_message.c_str())) |
| 332 | { |
| 333 | return true; |
| 334 | } |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | if (!m_rpc_server->on_set_log_hash_rate(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 339 | { |
| 340 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 341 | return true; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | tools::success_msg_writer() << "Hash rate logging is off"; |
| 346 | |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | bool t_rpc_command_executor::show_difficulty() { |
| 351 | cryptonote::COMMAND_RPC_GET_INFO::request req; |
| 352 | cryptonote::COMMAND_RPC_GET_INFO::response res; |
| 353 | |
| 354 | std::string fail_message = "Problem fetching info"; |
| 355 | |
| 356 | if (m_is_rpc) |
| 357 | { |
| 358 | if (!m_rpc_client->rpc_request(req, res, "/getinfo", fail_message.c_str())) |
| 359 | { |
| 360 | return true; |
| 361 | } |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | if (!m_rpc_server->on_get_info(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 366 | { |
| 367 | tools::fail_msg_writer() << make_error(fail_message.c_str(), res.status); |
| 368 | return true; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | tools::success_msg_writer() << "BH: " << res.height |
| 373 | << ", TH: " << res.top_block_hash |
| 374 | << ", DIFF: " << cryptonote::difficulty_type(res.wide_difficulty) |
| 375 | << ", CUM_DIFF: " << cryptonote::difficulty_type(res.wide_cumulative_difficulty) |
| 376 | << ", HR: " << cryptonote::difficulty_type(res.wide_difficulty) / res.target << " H/s"; |
| 377 | |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | static void get_metric_prefix(cryptonote::difficulty_type hr, double& hr_d, char& prefix) |
| 382 | { |
| 383 | if (hr < 1000) |
| 384 | { |
| 385 | prefix = 0; |
| 386 | return; |
| 387 | } |
| 388 | static const char metric_prefixes[] = { 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' }; |
| 389 | for (size_t i = 0; i < sizeof(metric_prefixes); ++i) |
| 390 | { |
| 391 | if (hr < 1000000) |
| 392 | { |
| 393 | hr_d = hr.convert_to<double>() / 1000; |
| 394 | prefix = metric_prefixes[i]; |
| 395 | return; |
| 396 | } |
| 397 | hr /= 1000; |
| 398 | } |
| 399 | prefix = 0; |
| 400 | } |
| 401 | |
| 402 | static std::string get_mining_speed(cryptonote::difficulty_type hr) |
| 403 | { |
| 404 | double hr_d; |
| 405 | char prefix; |
| 406 | get_metric_prefix(hr, hr_d, prefix); |
| 407 | if (prefix == 0) return (boost::format("%.0f H/s") % hr).str(); |
| 408 | return (boost::format("%.2f %cH/s") % hr_d % prefix).str(); |
| 409 | } |
| 410 | |
| 411 | static std::string get_fork_extra_info(uint64_t t, uint64_t now, uint64_t block_time) |
| 412 | { |
| 413 | uint64_t blocks_per_day = 86400 / block_time; |
| 414 | |
| 415 | if (t == now) |
| 416 | return " (forking now)"; |
| 417 | |
| 418 | if (t > now) |
| 419 | { |
| 420 | uint64_t dblocks = t - now; |
| 421 | if (dblocks <= 30) |
| 422 | return (boost::format(" (next fork in %u blocks)") % (unsigned)dblocks).str(); |
| 423 | if (dblocks <= blocks_per_day / 2) |
| 424 | return (boost::format(" (next fork in %.1f hours)") % (dblocks / (float)(blocks_per_day / 24))).str(); |
| 425 | if (dblocks <= blocks_per_day * 30) |
| 426 | return (boost::format(" (next fork in %.1f days)") % (dblocks / (float)blocks_per_day)).str(); |
| 427 | return ""; |
| 428 | } |
| 429 | return ""; |
| 430 | } |
| 431 | |
| 432 | static float get_sync_percentage(uint64_t height, uint64_t target_height) |
| 433 | { |
| 434 | target_height = target_height ? target_height < height ? height : target_height : height; |
| 435 | float pc = 100.0f * height / target_height; |
| 436 | if (height < target_height && pc > 99.9f) |
| 437 | return 99.9f; // to avoid 100% when not fully synced |
| 438 | return pc; |
| 439 | } |
| 440 | static float get_sync_percentage(const cryptonote::COMMAND_RPC_GET_INFO::response &ires) |
| 441 | { |
| 442 | return get_sync_percentage(ires.height, ires.target_height); |
| 443 | } |
| 444 | |
| 445 | bool t_rpc_command_executor::show_status() { |
| 446 | cryptonote::COMMAND_RPC_GET_INFO::request ireq; |
| 447 | cryptonote::COMMAND_RPC_GET_INFO::response ires; |
| 448 | cryptonote::COMMAND_RPC_HARD_FORK_INFO::request hfreq; |
| 449 | cryptonote::COMMAND_RPC_HARD_FORK_INFO::response hfres; |
| 450 | cryptonote::COMMAND_RPC_MINING_STATUS::request mreq; |
| 451 | cryptonote::COMMAND_RPC_MINING_STATUS::response mres; |
| 452 | epee::json_rpc::error error_resp; |
| 453 | bool has_mining_info = true; |
| 454 | |
| 455 | std::string fail_message = "Problem fetching info"; |
| 456 | |
| 457 | hfreq.version = 0; |
| 458 | bool mining_busy = false; |
| 459 | if (m_is_rpc) |
| 460 | { |
| 461 | if (!m_rpc_client->rpc_request(ireq, ires, "/getinfo", fail_message.c_str())) |
| 462 | { |
| 463 | return true; |
| 464 | } |
| 465 | if (!m_rpc_client->json_rpc_request(hfreq, hfres, "hard_fork_info", fail_message.c_str())) |
| 466 | { |
| 467 | return true; |
| 468 | } |
| 469 | // mining info is only available non unrestricted RPC mode |
| 470 | has_mining_info = m_rpc_client->rpc_request(mreq, mres, "/mining_status", fail_message.c_str()); |
| 471 | } |
| 472 | else |
| 473 | { |
| 474 | if (!m_rpc_server->on_get_info(ireq, ires) || ires.status != CORE_RPC_STATUS_OK) |
| 475 | { |
| 476 | tools::fail_msg_writer() << make_error(fail_message, ires.status); |
| 477 | return true; |
| 478 | } |
| 479 | if (!m_rpc_server->on_hard_fork_info(hfreq, hfres, error_resp) || hfres.status != CORE_RPC_STATUS_OK) |
| 480 | { |
| 481 | tools::fail_msg_writer() << make_error(fail_message, hfres.status); |
| 482 | return true; |
| 483 | } |
| 484 | if (!m_rpc_server->on_mining_status(mreq, mres)) |
| 485 | { |
| 486 | tools::fail_msg_writer() << fail_message.c_str(); |
| 487 | return true; |
| 488 | } |
| 489 | |
| 490 | if (mres.status == CORE_RPC_STATUS_BUSY) |
| 491 | { |
| 492 | mining_busy = true; |
| 493 | } |
| 494 | else if (mres.status != CORE_RPC_STATUS_OK) |
| 495 | { |
| 496 | tools::fail_msg_writer() << make_error(fail_message, mres.status); |
| 497 | return true; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | std::time_t uptime = std::time(nullptr) - ires.start_time; |
| 502 | uint64_t net_height = ires.target_height > ires.height ? ires.target_height : ires.height; |
| 503 | std::string bootstrap_msg; |
| 504 | if (ires.was_bootstrap_ever_used) |
| 505 | { |
| 506 | bootstrap_msg = ", bootstrapping from " + ires.bootstrap_daemon_address; |
| 507 | if (ires.untrusted) |
| 508 | { |
| 509 | bootstrap_msg += (boost::format(", local height: %llu (%.1f%%)") % ires.height_without_bootstrap % get_sync_percentage(ires.height_without_bootstrap, net_height)).str(); |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | bootstrap_msg += " was used before"; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | std::stringstream str; |
| 518 | str << boost::format("Height: %llu/%llu (%.1f%%) on %s%s, %s, net hash %s, v%u%s, %u(out)+%u(in) connections") |
| 519 | % (unsigned long long)ires.height |
| 520 | % (unsigned long long)net_height |
| 521 | % get_sync_percentage(ires) |
| 522 | % (ires.testnet ? "testnet" : ires.stagenet ? "stagenet" : "mainnet") |
| 523 | % bootstrap_msg |
| 524 | % (!has_mining_info ? "mining info unavailable" : mining_busy ? "syncing" : mres.active ? ( ( mres.is_background_mining_enabled ? "smart " : "" ) + std::string("mining at ") + get_mining_speed(mres.speed)) : "not mining") |
| 525 | % get_mining_speed(cryptonote::difficulty_type(ires.wide_difficulty) / ires.target) |
| 526 | % (unsigned)hfres.version |
| 527 | % get_fork_extra_info(hfres.earliest_height, net_height, ires.target) |
| 528 | % (unsigned)ires.outgoing_connections_count |
| 529 | % (unsigned)ires.incoming_connections_count |
| 530 | ; |
| 531 | |
| 532 | // restricted RPC does not disclose start time |
| 533 | if (ires.start_time) |
| 534 | { |
| 535 | str << boost::format(", uptime %ud %uh %um %us") |
| 536 | % (unsigned int)floor(uptime / 60.0 / 60.0 / 24.0) |
| 537 | % (unsigned int)floor(fmod((uptime / 60.0 / 60.0), 24.0)) |
| 538 | % (unsigned int)floor(fmod((uptime / 60.0), 60.0)) |
| 539 | % (unsigned int)fmod(uptime, 60.0) |
| 540 | ; |
| 541 | } |
| 542 | |
| 543 | tools::success_msg_writer() << str.str(); |
| 544 | |
| 545 | return true; |
| 546 | } |
| 547 | |
| 548 | bool t_rpc_command_executor::mining_status() { |
| 549 | cryptonote::COMMAND_RPC_MINING_STATUS::request mreq; |
| 550 | cryptonote::COMMAND_RPC_MINING_STATUS::response mres; |
| 551 | epee::json_rpc::error error_resp; |
| 552 | bool has_mining_info = true; |
| 553 | |
| 554 | std::string fail_message = "Problem fetching info"; |
| 555 | |
| 556 | bool mining_busy = false; |
| 557 | if (m_is_rpc) |
| 558 | { |
| 559 | // mining info is only available non unrestricted RPC mode |
| 560 | has_mining_info = m_rpc_client->rpc_request(mreq, mres, "/mining_status", fail_message.c_str()); |
| 561 | } |
| 562 | else |
| 563 | { |
| 564 | if (!m_rpc_server->on_mining_status(mreq, mres)) |
| 565 | { |
| 566 | tools::fail_msg_writer() << fail_message.c_str(); |
| 567 | return true; |
| 568 | } |
| 569 | |
| 570 | if (mres.status == CORE_RPC_STATUS_BUSY) |
| 571 | { |
| 572 | mining_busy = true; |
| 573 | } |
| 574 | else if (mres.status != CORE_RPC_STATUS_OK) |
| 575 | { |
| 576 | tools::fail_msg_writer() << make_error(fail_message, mres.status); |
| 577 | return true; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (!has_mining_info) |
| 582 | { |
| 583 | tools::fail_msg_writer() << "Mining info unavailable"; |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | if (mining_busy || !mres.active) |
| 588 | { |
| 589 | tools::msg_writer() << "Not currently mining"; |
| 590 | } |
| 591 | else |
| 592 | { |
| 593 | tools::msg_writer() << "Mining at " << get_mining_speed(mres.speed) << " with " << mres.threads_count << " threads"; |
| 594 | } |
| 595 | |
| 596 | tools::msg_writer() << "PoW algorithm: " << mres.pow_algorithm; |
| 597 | if (mres.active || mres.is_background_mining_enabled) |
| 598 | { |
| 599 | tools::msg_writer() << "Mining address: " << mres.address; |
| 600 | } |
| 601 | |
| 602 | if (mres.is_background_mining_enabled) |
| 603 | { |
| 604 | tools::msg_writer() << "Smart mining enabled:"; |
| 605 | tools::msg_writer() << " Target: " << (unsigned)mres.bg_target << "% CPU"; |
| 606 | tools::msg_writer() << " Idle threshold: " << (unsigned)mres.bg_idle_threshold << "% CPU"; |
| 607 | tools::msg_writer() << " Min idle time: " << (unsigned)mres.bg_min_idle_seconds << " seconds"; |
| 608 | tools::msg_writer() << " Ignore battery: " << (mres.bg_ignore_battery ? "yes" : "no"); |
| 609 | } |
| 610 | |
| 611 | if (!mining_busy && mres.active && mres.speed > 0 && mres.block_target > 0 && mres.difficulty > 0) |
| 612 | { |
| 613 | double ratio = mres.speed * mres.block_target / (double)mres.difficulty; |
| 614 | uint64_t daily = 86400ull / mres.block_target * mres.block_reward * ratio; |
| 615 | uint64_t monthly = 86400ull / mres.block_target * 30.5 * mres.block_reward * ratio; |
| 616 | uint64_t yearly = 86400ull / mres.block_target * 356 * mres.block_reward * ratio; |
| 617 | tools::msg_writer() << "Expected: " << cryptonote::print_money(daily) << " monero daily, " |
| 618 | << cryptonote::print_money(monthly) << " monero monthly, " << cryptonote::print_money(yearly) << " yearly"; |
| 619 | } |
| 620 | |
| 621 | return true; |
| 622 | } |
| 623 | |
| 624 | bool t_rpc_command_executor::print_connections() { |
| 625 | cryptonote::COMMAND_RPC_GET_CONNECTIONS::request req; |
| 626 | cryptonote::COMMAND_RPC_GET_CONNECTIONS::response res; |
| 627 | epee::json_rpc::error error_resp; |
| 628 | |
| 629 | std::string fail_message = "Unsuccessful"; |
| 630 | |
| 631 | if (m_is_rpc) |
| 632 | { |
| 633 | if (!m_rpc_client->json_rpc_request(req, res, "get_connections", fail_message.c_str())) |
| 634 | { |
| 635 | return true; |
| 636 | } |
| 637 | } |
| 638 | else |
| 639 | { |
| 640 | if (!m_rpc_server->on_get_connections(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 641 | { |
| 642 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 643 | return true; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | tools::msg_writer() << std::setw(30) << std::left << "Remote Host" |
| 648 | << std::setw(8) << "Type" |
| 649 | << std::setw(6) << "SSL" |
| 650 | << std::setw(20) << "Peer id" |
| 651 | << std::setw(20) << "Support Flags" |
| 652 | << std::setw(30) << "Recv/Sent (inactive,sec)" |
| 653 | << std::setw(25) << "State" |
| 654 | << std::setw(20) << "Livetime(sec)" |
| 655 | << std::setw(12) << "Down (kB/s)" |
| 656 | << std::setw(14) << "Down(now)" |
| 657 | << std::setw(10) << "Up (kB/s)" |
| 658 | << std::setw(13) << "Up(now)" |
| 659 | << std::endl; |
| 660 | |
| 661 | for (auto & info : res.connections) |
| 662 | { |
| 663 | std::string address = info.incoming ? "INC " : "OUT "; |
| 664 | address += info.ip + ":" + info.port; |
| 665 | //std::string in_out = info.incoming ? "INC " : "OUT "; |
| 666 | tools::msg_writer() |
| 667 | //<< std::setw(30) << std::left << in_out |
| 668 | << std::setw(30) << std::left << address |
| 669 | << std::setw(8) << (get_address_type_name((epee::net_utils::address_type)info.address_type)) |
| 670 | << std::setw(6) << (info.ssl ? "yes" : "no") |
| 671 | << std::setw(20) << info.peer_id |
| 672 | << std::setw(20) << info.support_flags |
| 673 | << std::setw(30) << std::to_string(info.recv_count) + "(" + std::to_string(info.recv_idle_time) + ")/" + std::to_string(info.send_count) + "(" + std::to_string(info.send_idle_time) + ")" |
| 674 | << std::setw(25) << info.state |
| 675 | << std::setw(20) << info.live_time |
| 676 | << std::setw(12) << info.avg_download |
| 677 | << std::setw(14) << info.current_download |
| 678 | << std::setw(10) << info.avg_upload |
| 679 | << std::setw(13) << info.current_upload |
| 680 | |
| 681 | << std::left << (info.localhost ? "[LOCALHOST]" : "") |
| 682 | << std::left << (info.local_ip ? "[LAN]" : ""); |
| 683 | //tools::msg_writer() << boost::format("%-25s peer_id: %-25s %s") % address % info.peer_id % in_out; |
| 684 | |
| 685 | } |
| 686 | |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | bool t_rpc_command_executor::print_net_stats() |
| 691 | { |
| 692 | cryptonote::COMMAND_RPC_GET_NET_STATS::request net_stats_req; |
| 693 | cryptonote::COMMAND_RPC_GET_NET_STATS::response net_stats_res; |
| 694 | cryptonote::COMMAND_RPC_GET_LIMIT::request limit_req; |
| 695 | cryptonote::COMMAND_RPC_GET_LIMIT::response limit_res; |
| 696 | |
| 697 | std::string fail_message = "Unsuccessful"; |
| 698 | |
| 699 | if (m_is_rpc) |
| 700 | { |
| 701 | if (!m_rpc_client->rpc_request(net_stats_req, net_stats_res, "/get_net_stats", fail_message.c_str())) |
| 702 | { |
| 703 | return true; |
| 704 | } |
| 705 | if (!m_rpc_client->rpc_request(limit_req, limit_res, "/get_limit", fail_message.c_str())) |
| 706 | { |
| 707 | return true; |
| 708 | } |
| 709 | } |
| 710 | else |
| 711 | { |
| 712 | if (!m_rpc_server->on_get_net_stats(net_stats_req, net_stats_res) || net_stats_res.status != CORE_RPC_STATUS_OK) |
| 713 | { |
| 714 | tools::fail_msg_writer() << make_error(fail_message, net_stats_res.status); |
| 715 | return true; |
| 716 | } |
| 717 | if (!m_rpc_server->on_get_limit(limit_req, limit_res) || limit_res.status != CORE_RPC_STATUS_OK) |
| 718 | { |
| 719 | tools::fail_msg_writer() << make_error(fail_message, limit_res.status); |
| 720 | return true; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | uint64_t seconds = (uint64_t)time(NULL) - net_stats_res.start_time; |
| 725 | uint64_t average = seconds > 0 ? net_stats_res.total_bytes_in / seconds : 0; |
| 726 | uint64_t limit = limit_res.limit_down * 1024; // convert to bytes, as limits are always kB/s |
| 727 | double percent = (double)average / (double)limit * 100.0; |
| 728 | tools::success_msg_writer() << boost::format("Received %u bytes (%s) in %u packets in %s, average %s/s = %.2f%% of the limit of %s/s") |
| 729 | % net_stats_res.total_bytes_in |
| 730 | % tools::get_human_readable_bytes(net_stats_res.total_bytes_in) |
| 731 | % net_stats_res.total_packets_in |
| 732 | % tools::get_human_readable_timespan(seconds) |
| 733 | % tools::get_human_readable_bytes(average) |
| 734 | % percent |
| 735 | % tools::get_human_readable_bytes(limit); |
| 736 | |
| 737 | average = seconds > 0 ? net_stats_res.total_bytes_out / seconds : 0; |
| 738 | limit = limit_res.limit_up * 1024; |
| 739 | percent = (double)average / (double)limit * 100.0; |
| 740 | tools::success_msg_writer() << boost::format("Sent %u bytes (%s) in %u packets in %s, average %s/s = %.2f%% of the limit of %s/s") |
| 741 | % net_stats_res.total_bytes_out |
| 742 | % tools::get_human_readable_bytes(net_stats_res.total_bytes_out) |
| 743 | % net_stats_res.total_packets_out |
| 744 | % tools::get_human_readable_timespan(seconds) |
| 745 | % tools::get_human_readable_bytes(average) |
| 746 | % percent |
| 747 | % tools::get_human_readable_bytes(limit); |
| 748 | |
| 749 | return true; |
| 750 | } |
| 751 | |
| 752 | bool t_rpc_command_executor::print_blockchain_info(int64_t start_block_index, uint64_t end_block_index) { |
| 753 | cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::request req; |
| 754 | cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::response res; |
| 755 | epee::json_rpc::error error_resp; |
| 756 | std::string fail_message = "Problem fetching info"; |
| 757 | |
| 758 | // negative: relative to the end |
| 759 | if (start_block_index < 0) |
| 760 | { |
| 761 | cryptonote::COMMAND_RPC_GET_INFO::request ireq; |
| 762 | cryptonote::COMMAND_RPC_GET_INFO::response ires; |
| 763 | if (m_is_rpc) |
| 764 | { |
| 765 | if (!m_rpc_client->rpc_request(ireq, ires, "/getinfo", fail_message.c_str())) |
| 766 | { |
| 767 | return true; |
| 768 | } |
| 769 | } |
| 770 | else |
| 771 | { |
| 772 | if (!m_rpc_server->on_get_info(ireq, ires) || ires.status != CORE_RPC_STATUS_OK) |
| 773 | { |
| 774 | tools::fail_msg_writer() << make_error(fail_message, ires.status); |
| 775 | return true; |
| 776 | } |
| 777 | } |
| 778 | if (start_block_index < 0 && (uint64_t)-start_block_index >= ires.height) |
| 779 | { |
| 780 | tools::fail_msg_writer() << "start offset is larger than blockchain height"; |
| 781 | return true; |
| 782 | } |
| 783 | start_block_index = ires.height + start_block_index; |
| 784 | end_block_index = start_block_index + end_block_index - 1; |
| 785 | } |
| 786 | |
| 787 | req.start_height = start_block_index; |
| 788 | req.end_height = end_block_index; |
| 789 | req.fill_pow_hash = false; |
| 790 | |
| 791 | fail_message = "Failed calling getblockheadersrange"; |
| 792 | if (m_is_rpc) |
| 793 | { |
| 794 | if (!m_rpc_client->json_rpc_request(req, res, "getblockheadersrange", fail_message.c_str())) |
| 795 | { |
| 796 | return true; |
| 797 | } |
| 798 | } |
| 799 | else |
| 800 | { |
| 801 | if (!m_rpc_server->on_get_block_headers_range(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 802 | { |
| 803 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 804 | return true; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | bool first = true; |
| 809 | for (auto & header : res.headers) |
| 810 | { |
| 811 | if (!first) |
| 812 | tools::msg_writer() << "" << std::endl; |
| 813 | tools::msg_writer() |
| 814 | << "height: " << header.height << ", timestamp: " << header.timestamp << " (" << tools::get_human_readable_timestamp(header.timestamp) << ")" |
| 815 | << ", size: " << header.block_size << ", weight: " << header.block_weight << " (long term " << header.long_term_weight << "), transactions: " << header.num_txes << std::endl |
| 816 | << "major version: " << (unsigned)header.major_version << ", minor version: " << (unsigned)header.minor_version << std::endl |
| 817 | << "block id: " << header.hash << ", previous block id: " << header.prev_hash << std::endl |
| 818 | << "difficulty: " << cryptonote::difficulty_type(header.wide_difficulty) << ", nonce " << header.nonce << ", reward " << cryptonote::print_money(header.reward) << std::endl; |
| 819 | first = false; |
| 820 | } |
| 821 | |
| 822 | return true; |
| 823 | } |
| 824 | |
| 825 | bool t_rpc_command_executor::set_log_level(int8_t level) { |
| 826 | cryptonote::COMMAND_RPC_SET_LOG_LEVEL::request req; |
| 827 | cryptonote::COMMAND_RPC_SET_LOG_LEVEL::response res; |
| 828 | req.level = level; |
| 829 | |
| 830 | std::string fail_message = "Unsuccessful"; |
| 831 | |
| 832 | if (m_is_rpc) |
| 833 | { |
| 834 | if (!m_rpc_client->rpc_request(req, res, "/set_log_level", fail_message.c_str())) |
| 835 | { |
| 836 | return true; |
| 837 | } |
| 838 | } |
| 839 | else |
| 840 | { |
| 841 | if (!m_rpc_server->on_set_log_level(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 842 | { |
| 843 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 844 | return true; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | tools::success_msg_writer() << "Log level is now " << std::to_string(level); |
| 849 | |
| 850 | return true; |
| 851 | } |
| 852 | |
| 853 | bool t_rpc_command_executor::set_log_categories(const std::string &categories) { |
| 854 | cryptonote::COMMAND_RPC_SET_LOG_CATEGORIES::request req; |
| 855 | cryptonote::COMMAND_RPC_SET_LOG_CATEGORIES::response res; |
| 856 | req.categories = categories; |
| 857 | |
| 858 | std::string fail_message = "Unsuccessful"; |
| 859 | |
| 860 | if (m_is_rpc) |
| 861 | { |
| 862 | if (!m_rpc_client->rpc_request(req, res, "/set_log_categories", fail_message.c_str())) |
| 863 | { |
| 864 | return true; |
| 865 | } |
| 866 | } |
| 867 | else |
| 868 | { |
| 869 | if (!m_rpc_server->on_set_log_categories(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 870 | { |
| 871 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 872 | return true; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | tools::success_msg_writer() << "Log categories are now " << res.categories; |
| 877 | |
| 878 | return true; |
| 879 | } |
| 880 | |
| 881 | bool t_rpc_command_executor::print_height() { |
| 882 | cryptonote::COMMAND_RPC_GET_HEIGHT::request req; |
| 883 | cryptonote::COMMAND_RPC_GET_HEIGHT::response res; |
| 884 | |
| 885 | std::string fail_message = "Unsuccessful"; |
| 886 | |
| 887 | if (m_is_rpc) |
| 888 | { |
| 889 | if (!m_rpc_client->rpc_request(req, res, "/getheight", fail_message.c_str())) |
| 890 | { |
| 891 | return true; |
| 892 | } |
| 893 | } |
| 894 | else |
| 895 | { |
| 896 | if (!m_rpc_server->on_get_height(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 897 | { |
| 898 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 899 | return true; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | tools::success_msg_writer() << boost::lexical_cast<std::string>(res.height); |
| 904 | |
| 905 | return true; |
| 906 | } |
| 907 | |
| 908 | bool t_rpc_command_executor::print_block_by_hash(crypto::hash block_hash, bool include_hex) { |
| 909 | cryptonote::COMMAND_RPC_GET_BLOCK::request req; |
| 910 | cryptonote::COMMAND_RPC_GET_BLOCK::response res; |
| 911 | epee::json_rpc::error error_resp; |
| 912 | |
| 913 | req.hash = epee::string_tools::pod_to_hex(block_hash); |
| 914 | req.fill_pow_hash = true; |
| 915 | |
| 916 | std::string fail_message = "Unsuccessful"; |
| 917 | |
| 918 | if (m_is_rpc) |
| 919 | { |
| 920 | if (!m_rpc_client->json_rpc_request(req, res, "getblock", fail_message.c_str())) |
| 921 | { |
| 922 | return true; |
| 923 | } |
| 924 | } |
| 925 | else |
| 926 | { |
| 927 | if (!m_rpc_server->on_get_block(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 928 | { |
| 929 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 930 | return true; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | if (include_hex) |
| 935 | tools::success_msg_writer() << res.blob << std::endl; |
| 936 | print_block_header(res.block_header); |
| 937 | tools::success_msg_writer() << res.json << ENDL; |
| 938 | |
| 939 | return true; |
| 940 | } |
| 941 | |
| 942 | bool t_rpc_command_executor::print_block_by_height(uint64_t height, bool include_hex) { |
| 943 | cryptonote::COMMAND_RPC_GET_BLOCK::request req; |
| 944 | cryptonote::COMMAND_RPC_GET_BLOCK::response res; |
| 945 | epee::json_rpc::error error_resp; |
| 946 | |
| 947 | req.height = height; |
| 948 | req.fill_pow_hash = true; |
| 949 | |
| 950 | std::string fail_message = "Unsuccessful"; |
| 951 | |
| 952 | if (m_is_rpc) |
| 953 | { |
| 954 | if (!m_rpc_client->json_rpc_request(req, res, "getblock", fail_message.c_str())) |
| 955 | { |
| 956 | return true; |
| 957 | } |
| 958 | } |
| 959 | else |
| 960 | { |
| 961 | if (!m_rpc_server->on_get_block(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 962 | { |
| 963 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 964 | return true; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | if (include_hex) |
| 969 | tools::success_msg_writer() << res.blob << std::endl; |
| 970 | print_block_header(res.block_header); |
| 971 | tools::success_msg_writer() << res.json << ENDL; |
| 972 | |
| 973 | return true; |
| 974 | } |
| 975 | |
| 976 | bool t_rpc_command_executor::print_transaction(crypto::hash transaction_hash, |
| 977 | bool include_metadata, |
| 978 | bool include_hex, |
| 979 | bool include_json) { |
| 980 | cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req; |
| 981 | cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res; |
| 982 | |
| 983 | std::string fail_message = "Problem fetching transaction"; |
| 984 | |
| 985 | req.txs_hashes.push_back(epee::string_tools::pod_to_hex(transaction_hash)); |
| 986 | req.decode_as_json = false; |
| 987 | req.split = true; |
| 988 | req.prune = false; |
| 989 | if (m_is_rpc) |
| 990 | { |
| 991 | if (!m_rpc_client->rpc_request(req, res, "/gettransactions", fail_message.c_str())) |
| 992 | { |
| 993 | return true; |
| 994 | } |
| 995 | } |
| 996 | else |
| 997 | { |
| 998 | if (!m_rpc_server->on_get_transactions(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 999 | { |
| 1000 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1001 | return true; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | if (1 == res.txs.size() || 1 == res.txs_as_hex.size()) |
| 1006 | { |
| 1007 | if (1 == res.txs.size()) |
| 1008 | { |
| 1009 | // only available for new style answers |
| 1010 | static const std::string empty_hash = epee::string_tools::pod_to_hex(crypto::cn_fast_hash("", 0)); |
| 1011 | // prunable_hash will equal empty_hash when nothing is prunable (mostly when the transaction is coinbase) |
| 1012 | bool pruned = res.txs.front().prunable_as_hex.empty() && res.txs.front().prunable_hash != epee::string_tools::pod_to_hex(crypto::null_hash) && res.txs.front().prunable_hash != empty_hash; |
| 1013 | if (res.txs.front().in_pool) |
| 1014 | tools::success_msg_writer() << "Found in pool"; |
| 1015 | else |
| 1016 | tools::success_msg_writer() << "Found in blockchain at height " << res.txs.front().block_height << (pruned ? " (pruned)" : ""); |
| 1017 | } |
| 1018 | |
| 1019 | const std::string &as_hex = (1 == res.txs.size()) ? res.txs.front().as_hex : res.txs_as_hex.front(); |
| 1020 | const std::string &pruned_as_hex = (1 == res.txs.size()) ? res.txs.front().pruned_as_hex : ""; |
| 1021 | const std::string &prunable_as_hex = (1 == res.txs.size()) ? res.txs.front().prunable_as_hex : ""; |
| 1022 | // Print metadata if requested |
| 1023 | if (include_metadata) |
| 1024 | { |
| 1025 | if (!res.txs.front().in_pool) |
| 1026 | { |
| 1027 | tools::msg_writer() << "Block timestamp: " << res.txs.front().block_timestamp << " (" << tools::get_human_readable_timestamp(res.txs.front().block_timestamp) << ")"; |
| 1028 | } |
| 1029 | cryptonote::blobdata blob; |
| 1030 | if (epee::string_tools::parse_hexstr_to_binbuff(pruned_as_hex + prunable_as_hex, blob)) |
| 1031 | { |
| 1032 | cryptonote::transaction tx; |
| 1033 | if (cryptonote::parse_and_validate_tx_from_blob(blob, tx)) |
| 1034 | { |
| 1035 | tools::msg_writer() << "Size: " << blob.size(); |
| 1036 | tools::msg_writer() << "Weight: " << cryptonote::get_transaction_weight(tx); |
| 1037 | } |
| 1038 | else |
| 1039 | tools::fail_msg_writer() << "Error parsing transaction blob"; |
| 1040 | } |
| 1041 | else |
| 1042 | tools::fail_msg_writer() << "Error parsing transaction from hex"; |
| 1043 | } |
| 1044 | |
| 1045 | // Print raw hex if requested |
| 1046 | if (include_hex) |
| 1047 | { |
| 1048 | if (!as_hex.empty()) |
| 1049 | { |
| 1050 | tools::success_msg_writer() << as_hex << std::endl; |
| 1051 | } |
| 1052 | else |
| 1053 | { |
| 1054 | std::string output = pruned_as_hex + prunable_as_hex; |
| 1055 | tools::success_msg_writer() << output << std::endl; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | // Print json if requested |
| 1060 | if (include_json) |
| 1061 | { |
| 1062 | cryptonote::transaction tx; |
| 1063 | cryptonote::blobdata blob; |
| 1064 | std::string source = as_hex.empty() ? pruned_as_hex + prunable_as_hex : as_hex; |
| 1065 | bool pruned = !pruned_as_hex.empty() && prunable_as_hex.empty(); |
| 1066 | if (!epee::string_tools::parse_hexstr_to_binbuff(source, blob)) |
| 1067 | { |
| 1068 | tools::fail_msg_writer() << "Failed to parse tx to get json format"; |
| 1069 | } |
| 1070 | else |
| 1071 | { |
| 1072 | bool ret; |
| 1073 | if (pruned) |
| 1074 | ret = cryptonote::parse_and_validate_tx_base_from_blob(blob, tx); |
| 1075 | else |
| 1076 | ret = cryptonote::parse_and_validate_tx_from_blob(blob, tx); |
| 1077 | if (!ret) |
| 1078 | { |
| 1079 | tools::fail_msg_writer() << "Failed to parse tx blob to get json format"; |
| 1080 | } |
| 1081 | else |
| 1082 | { |
| 1083 | tools::success_msg_writer() << cryptonote::obj_to_json_str(tx) << std::endl; |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | else |
| 1089 | { |
| 1090 | tools::fail_msg_writer() << "Transaction wasn't found: " << transaction_hash << std::endl; |
| 1091 | } |
| 1092 | |
| 1093 | return true; |
| 1094 | } |
| 1095 | |
| 1096 | bool t_rpc_command_executor::is_key_image_spent(const crypto::key_image &ki) { |
| 1097 | cryptonote::COMMAND_RPC_IS_KEY_IMAGE_SPENT::request req; |
| 1098 | cryptonote::COMMAND_RPC_IS_KEY_IMAGE_SPENT::response res; |
| 1099 | |
| 1100 | std::string fail_message = "Problem checking key image"; |
| 1101 | |
| 1102 | req.key_images.push_back(epee::string_tools::pod_to_hex(ki)); |
| 1103 | if (m_is_rpc) |
| 1104 | { |
| 1105 | if (!m_rpc_client->rpc_request(req, res, "/is_key_image_spent", fail_message.c_str())) |
| 1106 | { |
| 1107 | return true; |
| 1108 | } |
| 1109 | } |
| 1110 | else |
| 1111 | { |
| 1112 | if (!m_rpc_server->on_is_key_image_spent(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1113 | { |
| 1114 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1115 | return true; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | if (1 == res.spent_status.size()) |
| 1120 | { |
| 1121 | // first as hex |
| 1122 | tools::success_msg_writer() << ki << ": " << (res.spent_status.front() ? "spent" : "unspent") << (res.spent_status.front() == cryptonote::COMMAND_RPC_IS_KEY_IMAGE_SPENT::SPENT_IN_POOL ? " (in pool)" : ""); |
| 1123 | } |
| 1124 | else |
| 1125 | { |
| 1126 | tools::fail_msg_writer() << "key image status could not be determined" << std::endl; |
| 1127 | } |
| 1128 | |
| 1129 | return true; |
| 1130 | } |
| 1131 | |
| 1132 | bool t_rpc_command_executor::print_transaction_pool_long() { |
| 1133 | cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request req; |
| 1134 | cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response res; |
| 1135 | |
| 1136 | std::string fail_message = "Problem fetching transaction pool"; |
| 1137 | |
| 1138 | if (m_is_rpc) |
| 1139 | { |
| 1140 | if (!m_rpc_client->rpc_request(req, res, "/get_transaction_pool", fail_message.c_str())) |
| 1141 | { |
| 1142 | return true; |
| 1143 | } |
| 1144 | } |
| 1145 | else |
| 1146 | { |
| 1147 | if (!m_rpc_server->on_get_transaction_pool(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1148 | { |
| 1149 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1150 | return true; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | if (res.transactions.empty() && res.spent_key_images.empty()) |
| 1155 | { |
| 1156 | tools::msg_writer() << "Pool is empty" << std::endl; |
| 1157 | } |
| 1158 | if (! res.transactions.empty()) |
| 1159 | { |
| 1160 | const time_t now = time(NULL); |
| 1161 | tools::msg_writer() << "Transactions: "; |
| 1162 | for (auto & tx_info : res.transactions) |
| 1163 | { |
| 1164 | tools::msg_writer() << "id: " << tx_info.id_hash << std::endl |
| 1165 | << tx_info.tx_json << std::endl |
| 1166 | << "blob_size: " << tx_info.blob_size << std::endl |
| 1167 | << "weight: " << tx_info.weight << std::endl |
| 1168 | << "fee: " << cryptonote::print_money(tx_info.fee) << std::endl |
| 1169 | << "fee/byte: " << cryptonote::print_money(tx_info.fee / (double)tx_info.weight) << std::endl |
| 1170 | << "receive_time: " << tx_info.receive_time << " (" << get_human_time_ago(tx_info.receive_time, now) << ")" << std::endl |
| 1171 | << "relayed: " << [&](const cryptonote::tx_info &tx_info)->std::string { if (!tx_info.relayed) return "no"; return boost::lexical_cast<std::string>(tx_info.last_relayed_time) + " (" + get_human_time_ago(tx_info.last_relayed_time, now) + ")"; } (tx_info) << std::endl |
| 1172 | << "do_not_relay: " << (tx_info.do_not_relay ? 'T' : 'F') << std::endl |
| 1173 | << "kept_by_block: " << (tx_info.kept_by_block ? 'T' : 'F') << std::endl |
| 1174 | << "double_spend_seen: " << (tx_info.double_spend_seen ? 'T' : 'F') << std::endl |
| 1175 | << "max_used_block_height: " << tx_info.max_used_block_height << std::endl |
| 1176 | << "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl |
| 1177 | << "last_failed_height: " << tx_info.last_failed_height << std::endl |
| 1178 | << "last_failed_id: " << tx_info.last_failed_id_hash << std::endl; |
| 1179 | } |
| 1180 | if (res.spent_key_images.empty()) |
| 1181 | { |
| 1182 | tools::msg_writer() << "WARNING: Inconsistent pool state - no spent key images"; |
| 1183 | } |
| 1184 | } |
| 1185 | if (! res.spent_key_images.empty()) |
| 1186 | { |
| 1187 | tools::msg_writer() << ""; // one newline |
| 1188 | tools::msg_writer() << "Spent key images: "; |
| 1189 | for (const cryptonote::spent_key_image_info& kinfo : res.spent_key_images) |
| 1190 | { |
| 1191 | tools::msg_writer() << "key image: " << kinfo.id_hash; |
| 1192 | if (kinfo.txs_hashes.size() == 1) |
| 1193 | { |
| 1194 | tools::msg_writer() << " tx: " << kinfo.txs_hashes[0]; |
| 1195 | } |
| 1196 | else if (kinfo.txs_hashes.size() == 0) |
| 1197 | { |
| 1198 | tools::msg_writer() << " WARNING: spent key image has no txs associated"; |
| 1199 | } |
| 1200 | else |
| 1201 | { |
| 1202 | tools::msg_writer() << " NOTE: key image for multiple txs: " << kinfo.txs_hashes.size(); |
| 1203 | for (const std::string& tx_id : kinfo.txs_hashes) |
| 1204 | { |
| 1205 | tools::msg_writer() << " tx: " << tx_id; |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | if (res.transactions.empty()) |
| 1210 | { |
| 1211 | tools::msg_writer() << "WARNING: Inconsistent pool state - no transactions"; |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | return true; |
| 1216 | } |
| 1217 | |
| 1218 | bool t_rpc_command_executor::print_transaction_pool_short() { |
| 1219 | cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request req; |
| 1220 | cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response res; |
| 1221 | |
| 1222 | std::string fail_message = "Problem fetching transaction pool"; |
| 1223 | |
| 1224 | if (m_is_rpc) |
| 1225 | { |
| 1226 | if (!m_rpc_client->rpc_request(req, res, "/get_transaction_pool", fail_message.c_str())) |
| 1227 | { |
| 1228 | return true; |
| 1229 | } |
| 1230 | } |
| 1231 | else |
| 1232 | { |
| 1233 | if (!m_rpc_server->on_get_transaction_pool(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1234 | { |
| 1235 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1236 | return true; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | if (res.transactions.empty()) |
| 1241 | { |
| 1242 | tools::msg_writer() << "Pool is empty" << std::endl; |
| 1243 | } |
| 1244 | else |
| 1245 | { |
| 1246 | const time_t now = time(NULL); |
| 1247 | for (auto & tx_info : res.transactions) |
| 1248 | { |
| 1249 | tools::msg_writer() << "id: " << tx_info.id_hash << std::endl |
| 1250 | << "blob_size: " << tx_info.blob_size << std::endl |
| 1251 | << "weight: " << tx_info.weight << std::endl |
| 1252 | << "fee: " << cryptonote::print_money(tx_info.fee) << std::endl |
| 1253 | << "fee/byte: " << cryptonote::print_money(tx_info.fee / (double)tx_info.weight) << std::endl |
| 1254 | << "receive_time: " << tx_info.receive_time << " (" << get_human_time_ago(tx_info.receive_time, now) << ")" << std::endl |
| 1255 | << "relayed: " << [&](const cryptonote::tx_info &tx_info)->std::string { if (!tx_info.relayed) return "no"; return boost::lexical_cast<std::string>(tx_info.last_relayed_time) + " (" + get_human_time_ago(tx_info.last_relayed_time, now) + ")"; } (tx_info) << std::endl |
| 1256 | << "do_not_relay: " << (tx_info.do_not_relay ? 'T' : 'F') << std::endl |
| 1257 | << "kept_by_block: " << (tx_info.kept_by_block ? 'T' : 'F') << std::endl |
| 1258 | << "double_spend_seen: " << (tx_info.double_spend_seen ? 'T' : 'F') << std::endl |
| 1259 | << "max_used_block_height: " << tx_info.max_used_block_height << std::endl |
| 1260 | << "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl |
| 1261 | << "last_failed_height: " << tx_info.last_failed_height << std::endl |
| 1262 | << "last_failed_id: " << tx_info.last_failed_id_hash << std::endl; |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | return true; |
| 1267 | } |
| 1268 | |
| 1269 | bool t_rpc_command_executor::print_transaction_pool_stats() { |
| 1270 | cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL_STATS::request req; |
| 1271 | cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL_STATS::response res; |
| 1272 | cryptonote::COMMAND_RPC_GET_INFO::request ireq; |
| 1273 | cryptonote::COMMAND_RPC_GET_INFO::response ires; |
| 1274 | |
| 1275 | std::string fail_message = "Problem fetching transaction pool stats"; |
| 1276 | |
| 1277 | if (m_is_rpc) |
| 1278 | { |
| 1279 | if (!m_rpc_client->rpc_request(req, res, "/get_transaction_pool_stats", fail_message.c_str())) |
| 1280 | { |
| 1281 | return true; |
| 1282 | } |
| 1283 | if (!m_rpc_client->rpc_request(ireq, ires, "/getinfo", fail_message.c_str())) |
| 1284 | { |
| 1285 | return true; |
| 1286 | } |
| 1287 | } |
| 1288 | else |
| 1289 | { |
| 1290 | res.pool_stats = {}; |
| 1291 | if (!m_rpc_server->on_get_transaction_pool_stats(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1292 | { |
| 1293 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1294 | return true; |
| 1295 | } |
| 1296 | if (!m_rpc_server->on_get_info(ireq, ires) || ires.status != CORE_RPC_STATUS_OK) |
| 1297 | { |
| 1298 | tools::fail_msg_writer() << make_error(fail_message, ires.status); |
| 1299 | return true; |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | size_t n_transactions = res.pool_stats.txs_total; |
| 1304 | const uint64_t now = time(NULL); |
| 1305 | size_t avg_bytes = n_transactions ? res.pool_stats.bytes_total / n_transactions : 0; |
| 1306 | |
| 1307 | std::string backlog_message; |
| 1308 | const uint64_t full_reward_zone = ires.block_weight_limit / 2; |
| 1309 | if (res.pool_stats.bytes_total <= full_reward_zone) |
| 1310 | { |
| 1311 | backlog_message = "no backlog"; |
| 1312 | } |
| 1313 | else |
| 1314 | { |
| 1315 | uint64_t backlog = (res.pool_stats.bytes_total + full_reward_zone - 1) / full_reward_zone; |
| 1316 | backlog_message = (boost::format("estimated %u block (%u minutes) backlog") % backlog % (backlog * DIFFICULTY_TARGET_V2 / 60)).str(); |
| 1317 | } |
| 1318 | |
| 1319 | tools::msg_writer() << n_transactions << " tx(es), " << res.pool_stats.bytes_total << " bytes total (min " << res.pool_stats.bytes_min << ", max " << res.pool_stats.bytes_max << ", avg " << avg_bytes << ", median " << res.pool_stats.bytes_med << ")" << std::endl |
| 1320 | << "fees " << cryptonote::print_money(res.pool_stats.fee_total) << " (avg " << cryptonote::print_money(n_transactions ? res.pool_stats.fee_total / n_transactions : 0) << " per tx" << ", " << cryptonote::print_money(res.pool_stats.bytes_total ? res.pool_stats.fee_total / res.pool_stats.bytes_total : 0) << " per byte)" << std::endl |
| 1321 | << res.pool_stats.num_double_spends << " double spends, " << res.pool_stats.num_not_relayed << " not relayed, " << res.pool_stats.num_failing << " failing, " << res.pool_stats.num_10m << " older than 10 minutes (oldest " << (res.pool_stats.oldest == 0 ? "-" : get_human_time_ago(res.pool_stats.oldest, now)) << "), " << backlog_message; |
| 1322 | |
| 1323 | if (n_transactions > 1 && res.pool_stats.histo.size()) |
| 1324 | { |
| 1325 | std::vector<uint64_t> times; |
| 1326 | uint64_t numer; |
| 1327 | size_t i, n = res.pool_stats.histo.size(), denom; |
| 1328 | times.resize(n); |
| 1329 | if (res.pool_stats.histo_98pc) |
| 1330 | { |
| 1331 | numer = res.pool_stats.histo_98pc; |
| 1332 | denom = n-1; |
| 1333 | for (i=0; i<denom; i++) |
| 1334 | times[i] = i * numer / denom; |
| 1335 | times[i] = now - res.pool_stats.oldest; |
| 1336 | } else |
| 1337 | { |
| 1338 | numer = now - res.pool_stats.oldest; |
| 1339 | denom = n; |
| 1340 | for (i=0; i<denom; i++) |
| 1341 | times[i] = i * numer / denom; |
| 1342 | } |
| 1343 | tools::msg_writer() << " Age Txes Bytes"; |
| 1344 | for (i=0; i<n; i++) |
| 1345 | { |
| 1346 | tools::msg_writer() << get_time_hms(times[i]) << std::setw(8) << res.pool_stats.histo[i].txs << std::setw(12) << res.pool_stats.histo[i].bytes; |
| 1347 | } |
| 1348 | } |
| 1349 | tools::msg_writer(); |
| 1350 | |
| 1351 | return true; |
| 1352 | } |
| 1353 | |
| 1354 | bool t_rpc_command_executor::start_mining(cryptonote::account_public_address address, uint64_t num_threads, cryptonote::network_type nettype, bool do_background_mining, bool ignore_battery) { |
| 1355 | cryptonote::COMMAND_RPC_START_MINING::request req; |
| 1356 | cryptonote::COMMAND_RPC_START_MINING::response res; |
| 1357 | req.miner_address = cryptonote::get_account_address_as_str(nettype, false, address); |
| 1358 | req.threads_count = num_threads; |
| 1359 | req.do_background_mining = do_background_mining; |
| 1360 | req.ignore_battery = ignore_battery; |
| 1361 | |
| 1362 | std::string fail_message = "Mining did not start"; |
| 1363 | |
| 1364 | if (m_is_rpc) |
| 1365 | { |
| 1366 | if (m_rpc_client->rpc_request(req, res, "/start_mining", fail_message.c_str())) |
| 1367 | { |
| 1368 | tools::success_msg_writer() << "Mining started"; |
| 1369 | } |
| 1370 | } |
| 1371 | else |
| 1372 | { |
| 1373 | if (!m_rpc_server->on_start_mining(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1374 | { |
| 1375 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1376 | return true; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | return true; |
| 1381 | } |
| 1382 | |
| 1383 | bool t_rpc_command_executor::stop_mining() { |
| 1384 | cryptonote::COMMAND_RPC_STOP_MINING::request req; |
| 1385 | cryptonote::COMMAND_RPC_STOP_MINING::response res; |
| 1386 | |
| 1387 | std::string fail_message = "Mining did not stop"; |
| 1388 | |
| 1389 | if (m_is_rpc) |
| 1390 | { |
| 1391 | if (!m_rpc_client->rpc_request(req, res, "/stop_mining", fail_message.c_str())) |
| 1392 | { |
| 1393 | return true; |
| 1394 | } |
| 1395 | } |
| 1396 | else |
| 1397 | { |
| 1398 | if (!m_rpc_server->on_stop_mining(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1399 | { |
| 1400 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1401 | return true; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | tools::success_msg_writer() << "Mining stopped"; |
| 1406 | return true; |
| 1407 | } |
| 1408 | |
| 1409 | bool t_rpc_command_executor::stop_daemon() |
| 1410 | { |
| 1411 | cryptonote::COMMAND_RPC_STOP_DAEMON::request req; |
| 1412 | cryptonote::COMMAND_RPC_STOP_DAEMON::response res; |
| 1413 | |
| 1414 | //# ifdef WIN32 |
| 1415 | // // Stop via service API |
| 1416 | // // TODO - this is only temporary! Get rid of hard-coded constants! |
| 1417 | // bool ok = windows::stop_service("BitMonero Daemon"); |
| 1418 | // ok = windows::uninstall_service("BitMonero Daemon"); |
| 1419 | // //bool ok = windows::stop_service(SERVICE_NAME); |
| 1420 | // //ok = windows::uninstall_service(SERVICE_NAME); |
| 1421 | // if (ok) |
| 1422 | // { |
| 1423 | // return true; |
| 1424 | // } |
| 1425 | //# endif |
| 1426 | |
| 1427 | // Stop via RPC |
| 1428 | std::string fail_message = "Daemon did not stop"; |
| 1429 | |
| 1430 | if (m_is_rpc) |
| 1431 | { |
| 1432 | if(!m_rpc_client->rpc_request(req, res, "/stop_daemon", fail_message.c_str())) |
| 1433 | { |
| 1434 | return true; |
| 1435 | } |
| 1436 | } |
| 1437 | else |
| 1438 | { |
| 1439 | if (!m_rpc_server->on_stop_daemon(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1440 | { |
| 1441 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1442 | return true; |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | tools::success_msg_writer() << "Stop signal sent"; |
| 1447 | |
| 1448 | return true; |
| 1449 | } |
| 1450 | |
| 1451 | bool t_rpc_command_executor::print_status() |
| 1452 | { |
| 1453 | if (!m_is_rpc) |
| 1454 | { |
| 1455 | tools::success_msg_writer() << "print_status makes no sense in interactive mode"; |
| 1456 | return true; |
| 1457 | } |
| 1458 | |
| 1459 | bool daemon_is_alive = m_rpc_client->check_connection(); |
| 1460 | |
| 1461 | if(daemon_is_alive) { |
| 1462 | tools::success_msg_writer() << "monerod is running"; |
| 1463 | } |
| 1464 | else { |
| 1465 | tools::fail_msg_writer() << "monerod is NOT running"; |
| 1466 | } |
| 1467 | |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
| 1471 | bool t_rpc_command_executor::get_limit() |
| 1472 | { |
| 1473 | cryptonote::COMMAND_RPC_GET_LIMIT::request req; |
| 1474 | cryptonote::COMMAND_RPC_GET_LIMIT::response res; |
| 1475 | |
| 1476 | std::string failure_message = "Couldn't get limit"; |
| 1477 | |
| 1478 | if (m_is_rpc) |
| 1479 | { |
| 1480 | if (!m_rpc_client->rpc_request(req, res, "/get_limit", failure_message.c_str())) |
| 1481 | { |
| 1482 | return true; |
| 1483 | } |
| 1484 | } |
| 1485 | else |
| 1486 | { |
| 1487 | if (!m_rpc_server->on_get_limit(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1488 | { |
| 1489 | tools::fail_msg_writer() << make_error(failure_message, res.status); |
| 1490 | return true; |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | tools::msg_writer() << "limit-down is " << res.limit_down << " kB/s"; |
| 1495 | tools::msg_writer() << "limit-up is " << res.limit_up << " kB/s"; |
| 1496 | return true; |
| 1497 | } |
| 1498 | |
| 1499 | bool t_rpc_command_executor::set_limit(int64_t limit_down, int64_t limit_up) |
| 1500 | { |
| 1501 | cryptonote::COMMAND_RPC_SET_LIMIT::request req; |
| 1502 | cryptonote::COMMAND_RPC_SET_LIMIT::response res; |
| 1503 | |
| 1504 | req.limit_down = limit_down; |
| 1505 | req.limit_up = limit_up; |
| 1506 | |
| 1507 | std::string failure_message = "Couldn't set limit"; |
| 1508 | |
| 1509 | if (m_is_rpc) |
| 1510 | { |
| 1511 | if (!m_rpc_client->rpc_request(req, res, "/set_limit", failure_message.c_str())) |
| 1512 | { |
| 1513 | return true; |
| 1514 | } |
| 1515 | } |
| 1516 | else |
| 1517 | { |
| 1518 | if (!m_rpc_server->on_set_limit(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1519 | { |
| 1520 | tools::fail_msg_writer() << make_error(failure_message, res.status); |
| 1521 | return true; |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | tools::msg_writer() << "Set limit-down to " << res.limit_down << " kB/s"; |
| 1526 | tools::msg_writer() << "Set limit-up to " << res.limit_up << " kB/s"; |
| 1527 | return true; |
| 1528 | } |
| 1529 | |
| 1530 | bool t_rpc_command_executor::get_limit_up() |
| 1531 | { |
| 1532 | cryptonote::COMMAND_RPC_GET_LIMIT::request req; |
| 1533 | cryptonote::COMMAND_RPC_GET_LIMIT::response res; |
| 1534 | |
| 1535 | std::string failure_message = "Couldn't get limit"; |
| 1536 | |
| 1537 | if (m_is_rpc) |
| 1538 | { |
| 1539 | if (!m_rpc_client->rpc_request(req, res, "/get_limit", failure_message.c_str())) |
| 1540 | { |
| 1541 | return true; |
| 1542 | } |
| 1543 | } |
| 1544 | else |
| 1545 | { |
| 1546 | if (!m_rpc_server->on_get_limit(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1547 | { |
| 1548 | tools::fail_msg_writer() << make_error(failure_message, res.status); |
| 1549 | return true; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | tools::msg_writer() << "limit-up is " << res.limit_up << " kB/s"; |
| 1554 | return true; |
| 1555 | } |
| 1556 | |
| 1557 | bool t_rpc_command_executor::get_limit_down() |
| 1558 | { |
| 1559 | cryptonote::COMMAND_RPC_GET_LIMIT::request req; |
| 1560 | cryptonote::COMMAND_RPC_GET_LIMIT::response res; |
| 1561 | |
| 1562 | std::string failure_message = "Couldn't get limit"; |
| 1563 | |
| 1564 | if (m_is_rpc) |
| 1565 | { |
| 1566 | if (!m_rpc_client->rpc_request(req, res, "/get_limit", failure_message.c_str())) |
| 1567 | { |
| 1568 | return true; |
| 1569 | } |
| 1570 | } |
| 1571 | else |
| 1572 | { |
| 1573 | if (!m_rpc_server->on_get_limit(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1574 | { |
| 1575 | tools::fail_msg_writer() << make_error(failure_message, res.status); |
| 1576 | return true; |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | tools::msg_writer() << "limit-down is " << res.limit_down << " kB/s"; |
| 1581 | return true; |
| 1582 | } |
| 1583 | |
| 1584 | bool t_rpc_command_executor::out_peers(bool set, uint32_t limit) |
| 1585 | { |
| 1586 | cryptonote::COMMAND_RPC_OUT_PEERS::request req; |
| 1587 | cryptonote::COMMAND_RPC_OUT_PEERS::response res; |
| 1588 | |
| 1589 | epee::json_rpc::error error_resp; |
| 1590 | |
| 1591 | req.set = set; |
| 1592 | req.out_peers = limit; |
| 1593 | |
| 1594 | std::string fail_message = "Unsuccessful"; |
| 1595 | |
| 1596 | if (m_is_rpc) |
| 1597 | { |
| 1598 | if (!m_rpc_client->rpc_request(req, res, "/out_peers", fail_message.c_str())) |
| 1599 | { |
| 1600 | return true; |
| 1601 | } |
| 1602 | } |
| 1603 | else |
| 1604 | { |
| 1605 | if (!m_rpc_server->on_out_peers(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1606 | { |
| 1607 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1608 | return true; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | const std::string s = res.out_peers == (uint32_t)-1 ? "unlimited" : std::to_string(res.out_peers); |
| 1613 | tools::msg_writer() << "Max number of out peers set to " << s << std::endl; |
| 1614 | |
| 1615 | return true; |
| 1616 | } |
| 1617 | |
| 1618 | bool t_rpc_command_executor::in_peers(bool set, uint32_t limit) |
| 1619 | { |
| 1620 | cryptonote::COMMAND_RPC_IN_PEERS::request req; |
| 1621 | cryptonote::COMMAND_RPC_IN_PEERS::response res; |
| 1622 | |
| 1623 | epee::json_rpc::error error_resp; |
| 1624 | |
| 1625 | req.set = set; |
| 1626 | req.in_peers = limit; |
| 1627 | |
| 1628 | std::string fail_message = "Unsuccessful"; |
| 1629 | |
| 1630 | if (m_is_rpc) |
| 1631 | { |
| 1632 | if (!m_rpc_client->rpc_request(req, res, "/in_peers", fail_message.c_str())) |
| 1633 | { |
| 1634 | return true; |
| 1635 | } |
| 1636 | } |
| 1637 | else |
| 1638 | { |
| 1639 | if (!m_rpc_server->on_in_peers(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 1640 | { |
| 1641 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1642 | return true; |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | const std::string s = res.in_peers == (uint32_t)-1 ? "unlimited" : std::to_string(res.in_peers); |
| 1647 | tools::msg_writer() << "Max number of in peers set to " << s << std::endl; |
| 1648 | |
| 1649 | return true; |
| 1650 | } |
| 1651 | |
| 1652 | bool t_rpc_command_executor::hard_fork_info(uint8_t version) |
| 1653 | { |
| 1654 | cryptonote::COMMAND_RPC_HARD_FORK_INFO::request req; |
| 1655 | cryptonote::COMMAND_RPC_HARD_FORK_INFO::response res; |
| 1656 | std::string fail_message = "Unsuccessful"; |
| 1657 | epee::json_rpc::error error_resp; |
| 1658 | |
| 1659 | req.version = version; |
| 1660 | |
| 1661 | if (m_is_rpc) |
| 1662 | { |
| 1663 | if (!m_rpc_client->json_rpc_request(req, res, "hard_fork_info", fail_message.c_str())) |
| 1664 | { |
| 1665 | return true; |
| 1666 | } |
| 1667 | } |
| 1668 | else |
| 1669 | { |
| 1670 | if (!m_rpc_server->on_hard_fork_info(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 1671 | { |
| 1672 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1673 | return true; |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | version = version > 0 ? version : res.voting; |
| 1678 | tools::msg_writer() << "version " << (uint32_t)version << " " << (res.enabled ? "enabled" : "not enabled") << |
| 1679 | ", " << res.votes << "/" << res.window << " votes, threshold " << res.threshold; |
| 1680 | tools::msg_writer() << "current version " << (uint32_t)res.version << ", voting for version " << (uint32_t)res.voting; |
| 1681 | |
| 1682 | return true; |
| 1683 | } |
| 1684 | |
| 1685 | bool t_rpc_command_executor::print_bans() |
| 1686 | { |
| 1687 | cryptonote::COMMAND_RPC_GETBANS::request req; |
| 1688 | cryptonote::COMMAND_RPC_GETBANS::response res; |
| 1689 | std::string fail_message = "Unsuccessful"; |
| 1690 | epee::json_rpc::error error_resp; |
| 1691 | |
| 1692 | if (m_is_rpc) |
| 1693 | { |
| 1694 | if (!m_rpc_client->json_rpc_request(req, res, "get_bans", fail_message.c_str())) |
| 1695 | { |
| 1696 | return true; |
| 1697 | } |
| 1698 | } |
| 1699 | else |
| 1700 | { |
| 1701 | if (!m_rpc_server->on_get_bans(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 1702 | { |
| 1703 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1704 | return true; |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | if (!res.bans.empty()) |
| 1709 | { |
| 1710 | for (auto i = res.bans.begin(); i != res.bans.end(); ++i) |
| 1711 | { |
| 1712 | tools::msg_writer() << i->host << " banned for " << i->seconds << " seconds"; |
| 1713 | } |
| 1714 | } |
| 1715 | else |
| 1716 | tools::msg_writer() << "No IPs are banned"; |
| 1717 | |
| 1718 | return true; |
| 1719 | } |
| 1720 | |
| 1721 | bool t_rpc_command_executor::ban(const std::string &address, time_t seconds) |
| 1722 | { |
| 1723 | cryptonote::COMMAND_RPC_SETBANS::request req; |
| 1724 | cryptonote::COMMAND_RPC_SETBANS::response res; |
| 1725 | std::string fail_message = "Unsuccessful"; |
| 1726 | epee::json_rpc::error error_resp; |
| 1727 | |
| 1728 | cryptonote::COMMAND_RPC_SETBANS::ban ban; |
| 1729 | ban.host = address; |
| 1730 | ban.ip = 0; |
| 1731 | ban.ban = true; |
| 1732 | ban.seconds = seconds; |
| 1733 | req.bans.push_back(ban); |
| 1734 | |
| 1735 | if (m_is_rpc) |
| 1736 | { |
| 1737 | if (!m_rpc_client->json_rpc_request(req, res, "set_bans", fail_message.c_str())) |
| 1738 | { |
| 1739 | return true; |
| 1740 | } |
| 1741 | } |
| 1742 | else |
| 1743 | { |
| 1744 | if (!m_rpc_server->on_set_bans(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 1745 | { |
| 1746 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1747 | return true; |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | return true; |
| 1752 | } |
| 1753 | |
| 1754 | bool t_rpc_command_executor::unban(const std::string &address) |
| 1755 | { |
| 1756 | cryptonote::COMMAND_RPC_SETBANS::request req; |
| 1757 | cryptonote::COMMAND_RPC_SETBANS::response res; |
| 1758 | std::string fail_message = "Unsuccessful"; |
| 1759 | epee::json_rpc::error error_resp; |
| 1760 | |
| 1761 | cryptonote::COMMAND_RPC_SETBANS::ban ban; |
| 1762 | ban.host = address; |
| 1763 | ban.ip = 0; |
| 1764 | ban.ban = false; |
| 1765 | ban.seconds = 0; |
| 1766 | req.bans.push_back(ban); |
| 1767 | |
| 1768 | if (m_is_rpc) |
| 1769 | { |
| 1770 | if (!m_rpc_client->json_rpc_request(req, res, "set_bans", fail_message.c_str())) |
| 1771 | { |
| 1772 | return true; |
| 1773 | } |
| 1774 | } |
| 1775 | else |
| 1776 | { |
| 1777 | if (!m_rpc_server->on_set_bans(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 1778 | { |
| 1779 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1780 | return true; |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | return true; |
| 1785 | } |
| 1786 | |
| 1787 | bool t_rpc_command_executor::banned(const std::string &address) |
| 1788 | { |
| 1789 | cryptonote::COMMAND_RPC_BANNED::request req; |
| 1790 | cryptonote::COMMAND_RPC_BANNED::response res; |
| 1791 | std::string fail_message = "Unsuccessful"; |
| 1792 | epee::json_rpc::error error_resp; |
| 1793 | |
| 1794 | req.address = address; |
| 1795 | |
| 1796 | if (m_is_rpc) |
| 1797 | { |
| 1798 | if (!m_rpc_client->json_rpc_request(req, res, "banned", fail_message.c_str())) |
| 1799 | { |
| 1800 | return true; |
| 1801 | } |
| 1802 | } |
| 1803 | else |
| 1804 | { |
| 1805 | if (!m_rpc_server->on_banned(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 1806 | { |
| 1807 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1808 | return true; |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | if (res.banned) |
| 1813 | tools::msg_writer() << address << " is banned for " << res.seconds << " seconds"; |
| 1814 | else |
| 1815 | tools::msg_writer() << address << " is not banned"; |
| 1816 | |
| 1817 | return true; |
| 1818 | } |
| 1819 | |
| 1820 | bool t_rpc_command_executor::flush_txpool(const std::string &txid) |
| 1821 | { |
| 1822 | cryptonote::COMMAND_RPC_FLUSH_TRANSACTION_POOL::request req; |
| 1823 | cryptonote::COMMAND_RPC_FLUSH_TRANSACTION_POOL::response res; |
| 1824 | std::string fail_message = "Unsuccessful"; |
| 1825 | epee::json_rpc::error error_resp; |
| 1826 | |
| 1827 | if (!txid.empty()) |
| 1828 | req.txids.push_back(txid); |
| 1829 | |
| 1830 | if (m_is_rpc) |
| 1831 | { |
| 1832 | if (!m_rpc_client->json_rpc_request(req, res, "flush_txpool", fail_message.c_str())) |
| 1833 | { |
| 1834 | return true; |
| 1835 | } |
| 1836 | } |
| 1837 | else |
| 1838 | { |
| 1839 | if (!m_rpc_server->on_flush_txpool(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 1840 | { |
| 1841 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1842 | return true; |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | tools::success_msg_writer() << "Pool successfully flushed"; |
| 1847 | return true; |
| 1848 | } |
| 1849 | |
| 1850 | bool t_rpc_command_executor::output_histogram(const std::vector<uint64_t> &amounts, uint64_t min_count, uint64_t max_count) |
| 1851 | { |
| 1852 | cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::request req; |
| 1853 | cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::response res; |
| 1854 | std::string fail_message = "Unsuccessful"; |
| 1855 | epee::json_rpc::error error_resp; |
| 1856 | |
| 1857 | req.amounts = amounts; |
| 1858 | req.min_count = min_count; |
| 1859 | req.max_count = max_count; |
| 1860 | req.unlocked = false; |
| 1861 | req.recent_cutoff = 0; |
| 1862 | |
| 1863 | if (m_is_rpc) |
| 1864 | { |
| 1865 | if (!m_rpc_client->json_rpc_request(req, res, "get_output_histogram", fail_message.c_str())) |
| 1866 | { |
| 1867 | return true; |
| 1868 | } |
| 1869 | } |
| 1870 | else |
| 1871 | { |
| 1872 | if (!m_rpc_server->on_get_output_histogram(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 1873 | { |
| 1874 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1875 | return true; |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | std::sort(res.histogram.begin(), res.histogram.end(), |
| 1880 | [](const cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::entry &e1, const cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::entry &e2)->bool { return e1.total_instances < e2.total_instances; }); |
| 1881 | for (const auto &e: res.histogram) |
| 1882 | { |
| 1883 | tools::msg_writer() << e.total_instances << " " << cryptonote::print_money(e.amount); |
| 1884 | } |
| 1885 | |
| 1886 | return true; |
| 1887 | } |
| 1888 | |
| 1889 | bool t_rpc_command_executor::print_coinbase_tx_sum(uint64_t height, uint64_t count) |
| 1890 | { |
| 1891 | cryptonote::COMMAND_RPC_GET_COINBASE_TX_SUM::request req; |
| 1892 | cryptonote::COMMAND_RPC_GET_COINBASE_TX_SUM::response res; |
| 1893 | epee::json_rpc::error error_resp; |
| 1894 | |
| 1895 | req.height = height; |
| 1896 | req.count = count; |
| 1897 | |
| 1898 | std::string fail_message = "Unsuccessful"; |
| 1899 | |
| 1900 | if (m_is_rpc) |
| 1901 | { |
| 1902 | if (!m_rpc_client->json_rpc_request(req, res, "get_coinbase_tx_sum", fail_message.c_str())) |
| 1903 | { |
| 1904 | return true; |
| 1905 | } |
| 1906 | } |
| 1907 | else |
| 1908 | { |
| 1909 | if (!m_rpc_server->on_get_coinbase_tx_sum(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 1910 | { |
| 1911 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1912 | return true; |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | tools::msg_writer() << "Sum of coinbase transactions between block heights [" |
| 1917 | << height << ", " << (height + count) << ") is " |
| 1918 | << cryptonote::print_money(boost::multiprecision::uint128_t(res.wide_emission_amount) + boost::multiprecision::uint128_t(res.wide_fee_amount)) << " " |
| 1919 | << "consisting of " << cryptonote::print_money(boost::multiprecision::uint128_t(res.wide_emission_amount)) |
| 1920 | << " in emissions, and " << cryptonote::print_money(boost::multiprecision::uint128_t(res.wide_fee_amount)) << " in fees"; |
| 1921 | return true; |
| 1922 | } |
| 1923 | |
| 1924 | bool t_rpc_command_executor::alt_chain_info(const std::string &tip, size_t above, uint64_t last_blocks) |
| 1925 | { |
| 1926 | cryptonote::COMMAND_RPC_GET_INFO::request ireq; |
| 1927 | cryptonote::COMMAND_RPC_GET_INFO::response ires; |
| 1928 | cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::request req; |
| 1929 | cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::response res; |
| 1930 | epee::json_rpc::error error_resp; |
| 1931 | |
| 1932 | std::string fail_message = "Unsuccessful"; |
| 1933 | |
| 1934 | if (m_is_rpc) |
| 1935 | { |
| 1936 | if (!m_rpc_client->rpc_request(ireq, ires, "/getinfo", fail_message.c_str())) |
| 1937 | { |
| 1938 | return true; |
| 1939 | } |
| 1940 | if (!m_rpc_client->json_rpc_request(req, res, "get_alternate_chains", fail_message.c_str())) |
| 1941 | { |
| 1942 | return true; |
| 1943 | } |
| 1944 | } |
| 1945 | else |
| 1946 | { |
| 1947 | if (!m_rpc_server->on_get_info(ireq, ires) || ires.status != CORE_RPC_STATUS_OK) |
| 1948 | { |
| 1949 | tools::fail_msg_writer() << make_error(fail_message, ires.status); |
| 1950 | return true; |
| 1951 | } |
| 1952 | if (!m_rpc_server->on_get_alternate_chains(req, res, error_resp)) |
| 1953 | { |
| 1954 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 1955 | return true; |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | if (tip.empty()) |
| 1960 | { |
| 1961 | auto chains = res.chains; |
| 1962 | std::sort(chains.begin(), chains.end(), [](const cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::chain_info &info0, cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::chain_info &info1){ return info0.height < info1.height; }); |
| 1963 | std::vector<size_t> display; |
| 1964 | for (size_t i = 0; i < chains.size(); ++i) |
| 1965 | { |
| 1966 | const auto &chain = chains[i]; |
| 1967 | if (chain.length <= above) |
| 1968 | continue; |
| 1969 | const uint64_t start_height = (chain.height - chain.length + 1); |
| 1970 | if (last_blocks > 0 && ires.height - 1 - start_height >= last_blocks) |
| 1971 | continue; |
| 1972 | display.push_back(i); |
| 1973 | } |
| 1974 | tools::msg_writer() << boost::lexical_cast<std::string>(display.size()) << " alternate chains found:"; |
| 1975 | for (const size_t idx: display) |
| 1976 | { |
| 1977 | const auto &chain = chains[idx]; |
| 1978 | const uint64_t start_height = (chain.height - chain.length + 1); |
| 1979 | tools::msg_writer() << chain.length << " blocks long, from height " << start_height << " (" << (ires.height - start_height - 1) |
| 1980 | << " deep), diff " << cryptonote::difficulty_type(chain.wide_difficulty) << ": " << chain.block_hash; |
| 1981 | } |
| 1982 | } |
| 1983 | else |
| 1984 | { |
| 1985 | const uint64_t now = time(NULL); |
| 1986 | const auto i = std::find_if(res.chains.begin(), res.chains.end(), [&tip](cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::chain_info &info){ return info.block_hash == tip; }); |
| 1987 | if (i != res.chains.end()) |
| 1988 | { |
| 1989 | const auto &chain = *i; |
| 1990 | tools::success_msg_writer() << "Found alternate chain with tip " << tip; |
| 1991 | uint64_t start_height = (chain.height - chain.length + 1); |
| 1992 | tools::msg_writer() << chain.length << " blocks long, from height " << start_height << " (" << (ires.height - start_height - 1) |
| 1993 | << " deep), diff " << cryptonote::difficulty_type(chain.wide_difficulty) << ":"; |
| 1994 | for (const std::string &block_id: chain.block_hashes) |
| 1995 | tools::msg_writer() << " " << block_id; |
| 1996 | tools::msg_writer() << "Chain parent on main chain: " << chain.main_chain_parent_block; |
| 1997 | cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request bhreq; |
| 1998 | cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response bhres; |
| 1999 | bhreq.hashes = chain.block_hashes; |
| 2000 | bhreq.hashes.push_back(chain.main_chain_parent_block); |
| 2001 | bhreq.fill_pow_hash = false; |
| 2002 | if (m_is_rpc) |
| 2003 | { |
| 2004 | if (!m_rpc_client->json_rpc_request(bhreq, bhres, "getblockheaderbyhash", fail_message.c_str())) |
| 2005 | { |
| 2006 | return true; |
| 2007 | } |
| 2008 | } |
| 2009 | else |
| 2010 | { |
| 2011 | if (!m_rpc_server->on_get_block_header_by_hash(bhreq, bhres, error_resp)) |
| 2012 | { |
| 2013 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 2014 | return true; |
| 2015 | } |
| 2016 | } |
| 2017 | if (bhres.block_headers.size() != chain.length + 1) |
| 2018 | { |
| 2019 | tools::fail_msg_writer() << "Failed to get block header info for alt chain"; |
| 2020 | return true; |
| 2021 | } |
| 2022 | uint64_t t0 = bhres.block_headers.front().timestamp, t1 = t0; |
| 2023 | for (const cryptonote::block_header_response &block_header: bhres.block_headers) |
| 2024 | { |
| 2025 | t0 = std::min<uint64_t>(t0, block_header.timestamp); |
| 2026 | t1 = std::max<uint64_t>(t1, block_header.timestamp); |
| 2027 | } |
| 2028 | const uint64_t dt = t1 - t0; |
| 2029 | const uint64_t age = std::max(dt, t0 < now ? now - t0 : 0); |
| 2030 | tools::msg_writer() << "Age: " << tools::get_human_readable_timespan(age); |
| 2031 | if (chain.length > 1) |
| 2032 | { |
| 2033 | tools::msg_writer() << "Time span: " << tools::get_human_readable_timespan(dt); |
| 2034 | cryptonote::difficulty_type start_difficulty = bhres.block_headers.back().difficulty; |
| 2035 | if (start_difficulty > 0) |
| 2036 | tools::msg_writer() << "Approximated " << 100.f * DIFFICULTY_TARGET_V2 * chain.length / dt << "% of network hash rate"; |
| 2037 | else |
| 2038 | tools::fail_msg_writer() << "Bad cmumulative difficulty reported by dameon"; |
| 2039 | } |
| 2040 | } |
| 2041 | else |
| 2042 | tools::fail_msg_writer() << "Block hash " << tip << " is not the tip of any known alternate chain"; |
| 2043 | } |
| 2044 | return true; |
| 2045 | } |
| 2046 | |
| 2047 | bool t_rpc_command_executor::print_blockchain_dynamic_stats(uint64_t nblocks) |
| 2048 | { |
| 2049 | cryptonote::COMMAND_RPC_GET_INFO::request ireq; |
| 2050 | cryptonote::COMMAND_RPC_GET_INFO::response ires; |
| 2051 | cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::request bhreq; |
| 2052 | cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::response bhres; |
| 2053 | cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::request fereq; |
| 2054 | cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::response feres; |
| 2055 | cryptonote::COMMAND_RPC_HARD_FORK_INFO::request hfreq; |
| 2056 | cryptonote::COMMAND_RPC_HARD_FORK_INFO::response hfres; |
| 2057 | epee::json_rpc::error error_resp; |
| 2058 | |
| 2059 | std::string fail_message = "Problem fetching info"; |
| 2060 | |
| 2061 | fereq.grace_blocks = 0; |
| 2062 | hfreq.version = HF_VERSION_PER_BYTE_FEE; |
| 2063 | if (m_is_rpc) |
| 2064 | { |
| 2065 | if (!m_rpc_client->rpc_request(ireq, ires, "/getinfo", fail_message.c_str())) |
| 2066 | { |
| 2067 | return true; |
| 2068 | } |
| 2069 | if (!m_rpc_client->json_rpc_request(fereq, feres, "get_fee_estimate", fail_message.c_str())) |
| 2070 | { |
| 2071 | return true; |
| 2072 | } |
| 2073 | if (!m_rpc_client->json_rpc_request(hfreq, hfres, "hard_fork_info", fail_message.c_str())) |
| 2074 | { |
| 2075 | return true; |
| 2076 | } |
| 2077 | } |
| 2078 | else |
| 2079 | { |
| 2080 | if (!m_rpc_server->on_get_info(ireq, ires) || ires.status != CORE_RPC_STATUS_OK) |
| 2081 | { |
| 2082 | tools::fail_msg_writer() << make_error(fail_message, ires.status); |
| 2083 | return true; |
| 2084 | } |
| 2085 | if (!m_rpc_server->on_get_base_fee_estimate(fereq, feres, error_resp) || feres.status != CORE_RPC_STATUS_OK) |
| 2086 | { |
| 2087 | tools::fail_msg_writer() << make_error(fail_message, feres.status); |
| 2088 | return true; |
| 2089 | } |
| 2090 | if (!m_rpc_server->on_hard_fork_info(hfreq, hfres, error_resp) || hfres.status != CORE_RPC_STATUS_OK) |
| 2091 | { |
| 2092 | tools::fail_msg_writer() << make_error(fail_message, hfres.status); |
| 2093 | return true; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | tools::msg_writer() << "Height: " << ires.height << ", diff " << cryptonote::difficulty_type(ires.wide_difficulty) << ", cum. diff " << cryptonote::difficulty_type(ires.wide_cumulative_difficulty) |
| 2098 | << ", target " << ires.target << " sec" << ", dyn fee " << cryptonote::print_money(feres.fee) << "/" << (hfres.enabled ? "byte" : "kB"); |
| 2099 | |
| 2100 | if (nblocks > 0) |
| 2101 | { |
| 2102 | if (nblocks > ires.height) |
| 2103 | nblocks = ires.height; |
| 2104 | |
| 2105 | bhreq.start_height = ires.height - nblocks; |
| 2106 | bhreq.end_height = ires.height - 1; |
| 2107 | bhreq.fill_pow_hash = false; |
| 2108 | if (m_is_rpc) |
| 2109 | { |
| 2110 | if (!m_rpc_client->json_rpc_request(bhreq, bhres, "getblockheadersrange", fail_message.c_str())) |
| 2111 | { |
| 2112 | return true; |
| 2113 | } |
| 2114 | } |
| 2115 | else |
| 2116 | { |
| 2117 | if (!m_rpc_server->on_get_block_headers_range(bhreq, bhres, error_resp) || bhres.status != CORE_RPC_STATUS_OK) |
| 2118 | { |
| 2119 | tools::fail_msg_writer() << make_error(fail_message, bhres.status); |
| 2120 | return true; |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | cryptonote::difficulty_type avgdiff = 0; |
| 2125 | double avgnumtxes = 0; |
| 2126 | double avgreward = 0; |
| 2127 | std::vector<uint64_t> weights; |
| 2128 | weights.reserve(nblocks); |
| 2129 | uint64_t earliest = std::numeric_limits<uint64_t>::max(), latest = 0; |
| 2130 | std::vector<unsigned> major_versions(256, 0), minor_versions(256, 0); |
| 2131 | for (const auto &bhr: bhres.headers) |
| 2132 | { |
| 2133 | avgdiff += cryptonote::difficulty_type(bhr.wide_difficulty); |
| 2134 | avgnumtxes += bhr.num_txes; |
| 2135 | avgreward += bhr.reward; |
| 2136 | weights.push_back(bhr.block_weight); |
| 2137 | static_assert(sizeof(bhr.major_version) == 1, "major_version expected to be uint8_t"); |
| 2138 | static_assert(sizeof(bhr.minor_version) == 1, "major_version expected to be uint8_t"); |
| 2139 | major_versions[(unsigned)bhr.major_version]++; |
| 2140 | minor_versions[(unsigned)bhr.minor_version]++; |
| 2141 | earliest = std::min(earliest, bhr.timestamp); |
| 2142 | latest = std::max(latest, bhr.timestamp); |
| 2143 | } |
| 2144 | avgdiff /= nblocks; |
| 2145 | avgnumtxes /= nblocks; |
| 2146 | avgreward /= nblocks; |
| 2147 | uint64_t median_block_weight = epee::misc_utils::median(weights); |
| 2148 | tools::msg_writer() << "Last " << nblocks << ": avg. diff " << avgdiff << ", " << (latest - earliest) / nblocks << " avg sec/block, avg num txes " << avgnumtxes |
| 2149 | << ", avg. reward " << cryptonote::print_money(avgreward) << ", median block weight " << median_block_weight; |
| 2150 | |
| 2151 | unsigned int max_major = 256, max_minor = 256; |
| 2152 | while (max_major > 0 && !major_versions[--max_major]); |
| 2153 | while (max_minor > 0 && !minor_versions[--max_minor]); |
| 2154 | std::string s = ""; |
| 2155 | for (unsigned n = 0; n <= max_major; ++n) |
| 2156 | if (major_versions[n]) |
| 2157 | s += (s.empty() ? "" : ", ") + boost::lexical_cast<std::string>(major_versions[n]) + std::string(" v") + boost::lexical_cast<std::string>(n); |
| 2158 | tools::msg_writer() << "Block versions: " << s; |
| 2159 | s = ""; |
| 2160 | for (unsigned n = 0; n <= max_minor; ++n) |
| 2161 | if (minor_versions[n]) |
| 2162 | s += (s.empty() ? "" : ", ") + boost::lexical_cast<std::string>(minor_versions[n]) + std::string(" v") + boost::lexical_cast<std::string>(n); |
| 2163 | tools::msg_writer() << "Voting for: " << s; |
| 2164 | } |
| 2165 | return true; |
| 2166 | } |
| 2167 | |
| 2168 | bool t_rpc_command_executor::update(const std::string &command) |
| 2169 | { |
| 2170 | cryptonote::COMMAND_RPC_UPDATE::request req; |
| 2171 | cryptonote::COMMAND_RPC_UPDATE::response res; |
| 2172 | epee::json_rpc::error error_resp; |
| 2173 | |
| 2174 | std::string fail_message = "Problem fetching info"; |
| 2175 | |
| 2176 | req.command = command; |
| 2177 | if (m_is_rpc) |
| 2178 | { |
| 2179 | if (!m_rpc_client->rpc_request(req, res, "/update", fail_message.c_str())) |
| 2180 | { |
| 2181 | return true; |
| 2182 | } |
| 2183 | } |
| 2184 | else |
| 2185 | { |
| 2186 | if (!m_rpc_server->on_update(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 2187 | { |
| 2188 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 2189 | return true; |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | if (!res.update) |
| 2194 | { |
| 2195 | tools::msg_writer() << "No update available"; |
| 2196 | return true; |
| 2197 | } |
| 2198 | |
| 2199 | tools::msg_writer() << "Update available: v" << res.version << ": " << res.user_uri << ", hash " << res.hash; |
| 2200 | if (command == "check") |
| 2201 | return true; |
| 2202 | |
| 2203 | if (!res.path.empty()) |
| 2204 | tools::msg_writer() << "Update downloaded to: " << res.path; |
| 2205 | else |
| 2206 | tools::msg_writer() << "Update download failed: " << res.status; |
| 2207 | if (command == "download") |
| 2208 | return true; |
| 2209 | |
| 2210 | tools::msg_writer() << "'update' not implemented yet"; |
| 2211 | |
| 2212 | return true; |
| 2213 | } |
| 2214 | |
| 2215 | bool t_rpc_command_executor::relay_tx(const std::string &txid) |
| 2216 | { |
| 2217 | cryptonote::COMMAND_RPC_RELAY_TX::request req; |
| 2218 | cryptonote::COMMAND_RPC_RELAY_TX::response res; |
| 2219 | std::string fail_message = "Unsuccessful"; |
| 2220 | epee::json_rpc::error error_resp; |
| 2221 | |
| 2222 | req.txids.push_back(txid); |
| 2223 | |
| 2224 | if (m_is_rpc) |
| 2225 | { |
| 2226 | if (!m_rpc_client->json_rpc_request(req, res, "relay_tx", fail_message.c_str())) |
| 2227 | { |
| 2228 | return true; |
| 2229 | } |
| 2230 | } |
| 2231 | else |
| 2232 | { |
| 2233 | if (!m_rpc_server->on_relay_tx(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 2234 | { |
| 2235 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 2236 | return true; |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | tools::success_msg_writer() << "Transaction successfully relayed"; |
| 2241 | return true; |
| 2242 | } |
| 2243 | |
| 2244 | bool t_rpc_command_executor::sync_info() |
| 2245 | { |
| 2246 | cryptonote::COMMAND_RPC_SYNC_INFO::request req; |
| 2247 | cryptonote::COMMAND_RPC_SYNC_INFO::response res; |
| 2248 | std::string fail_message = "Unsuccessful"; |
| 2249 | epee::json_rpc::error error_resp; |
| 2250 | |
| 2251 | if (m_is_rpc) |
| 2252 | { |
| 2253 | if (!m_rpc_client->json_rpc_request(req, res, "sync_info", fail_message.c_str())) |
| 2254 | { |
| 2255 | return true; |
| 2256 | } |
| 2257 | } |
| 2258 | else |
| 2259 | { |
| 2260 | if (!m_rpc_server->on_sync_info(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 2261 | { |
| 2262 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 2263 | return true; |
| 2264 | } |
| 2265 | } |
| 2266 | |
| 2267 | uint64_t target = res.target_height < res.height ? res.height : res.target_height; |
| 2268 | tools::success_msg_writer() << "Height: " << res.height << ", target: " << target << " (" << (100.0 * res.height / target) << "%)"; |
| 2269 | uint64_t current_download = 0; |
| 2270 | for (const auto &p: res.peers) |
| 2271 | current_download += p.info.current_download; |
| 2272 | tools::success_msg_writer() << "Downloading at " << current_download << " kB/s"; |
| 2273 | if (res.next_needed_pruning_seed) |
| 2274 | tools::success_msg_writer() << "Next needed pruning seed: " << res.next_needed_pruning_seed; |
| 2275 | |
| 2276 | tools::success_msg_writer() << std::to_string(res.peers.size()) << " peers"; |
| 2277 | tools::success_msg_writer() << "Remote Host Peer_ID State Prune_Seed Height DL kB/s, Queued Blocks / MB"; |
| 2278 | for (const auto &p: res.peers) |
| 2279 | { |
| 2280 | std::string address = epee::string_tools::pad_string(p.info.address, 24); |
| 2281 | uint64_t nblocks = 0, size = 0; |
| 2282 | for (const auto &s: res.spans) |
| 2283 | if (s.connection_id == p.info.connection_id) |
| 2284 | nblocks += s.nblocks, size += s.size; |
| 2285 | tools::success_msg_writer() << address << " " << p.info.peer_id << " " << |
| 2286 | epee::string_tools::pad_string(p.info.state, 16) << " " << |
| 2287 | epee::string_tools::pad_string(epee::string_tools::to_string_hex(p.info.pruning_seed), 8) << " " << p.info.height << " " << |
| 2288 | p.info.current_download << " kB/s, " << nblocks << " blocks / " << size/1e6 << " MB queued"; |
| 2289 | } |
| 2290 | |
| 2291 | uint64_t total_size = 0; |
| 2292 | for (const auto &s: res.spans) |
| 2293 | total_size += s.size; |
| 2294 | tools::success_msg_writer() << std::to_string(res.spans.size()) << " spans, " << total_size/1e6 << " MB"; |
| 2295 | tools::success_msg_writer() << res.overview; |
| 2296 | for (const auto &s: res.spans) |
| 2297 | { |
| 2298 | std::string address = epee::string_tools::pad_string(s.remote_address, 24); |
| 2299 | std::string pruning_seed = epee::string_tools::to_string_hex(tools::get_pruning_seed(s.start_block_height, std::numeric_limits<uint64_t>::max(), CRYPTONOTE_PRUNING_LOG_STRIPES)); |
| 2300 | if (s.size == 0) |
| 2301 | { |
| 2302 | tools::success_msg_writer() << address << " " << s.nblocks << "/" << pruning_seed << " (" << s.start_block_height << " - " << (s.start_block_height + s.nblocks - 1) << ") -"; |
| 2303 | } |
| 2304 | else |
| 2305 | { |
| 2306 | tools::success_msg_writer() << address << " " << s.nblocks << "/" << pruning_seed << " (" << s.start_block_height << " - " << (s.start_block_height + s.nblocks - 1) << ", " << (uint64_t)(s.size/1e3) << " kB) " << (unsigned)(s.rate/1e3) << " kB/s (" << s.speed/100.0f << ")"; |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | return true; |
| 2311 | } |
| 2312 | |
| 2313 | bool t_rpc_command_executor::pop_blocks(uint64_t num_blocks) |
| 2314 | { |
| 2315 | cryptonote::COMMAND_RPC_POP_BLOCKS::request req; |
| 2316 | cryptonote::COMMAND_RPC_POP_BLOCKS::response res; |
| 2317 | std::string fail_message = "pop_blocks failed"; |
| 2318 | |
| 2319 | req.nblocks = num_blocks; |
| 2320 | if (m_is_rpc) |
| 2321 | { |
| 2322 | if (!m_rpc_client->rpc_request(req, res, "/pop_blocks", fail_message.c_str())) |
| 2323 | { |
| 2324 | return true; |
| 2325 | } |
| 2326 | } |
| 2327 | else |
| 2328 | { |
| 2329 | if (!m_rpc_server->on_pop_blocks(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 2330 | { |
| 2331 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 2332 | return true; |
| 2333 | } |
| 2334 | } |
| 2335 | tools::success_msg_writer() << "new height: " << res.height; |
| 2336 | |
| 2337 | return true; |
| 2338 | } |
| 2339 | |
| 2340 | bool t_rpc_command_executor::prune_blockchain() |
| 2341 | { |
| 2342 | cryptonote::COMMAND_RPC_PRUNE_BLOCKCHAIN::request req; |
| 2343 | cryptonote::COMMAND_RPC_PRUNE_BLOCKCHAIN::response res; |
| 2344 | std::string fail_message = "Unsuccessful"; |
| 2345 | epee::json_rpc::error error_resp; |
| 2346 | |
| 2347 | req.check = false; |
| 2348 | |
| 2349 | if (m_is_rpc) |
| 2350 | { |
| 2351 | if (!m_rpc_client->json_rpc_request(req, res, "prune_blockchain", fail_message.c_str())) |
| 2352 | { |
| 2353 | return true; |
| 2354 | } |
| 2355 | } |
| 2356 | else |
| 2357 | { |
| 2358 | if (!m_rpc_server->on_prune_blockchain(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 2359 | { |
| 2360 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 2361 | return true; |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | tools::success_msg_writer() << "Blockchain pruned"; |
| 2366 | return true; |
| 2367 | } |
| 2368 | |
| 2369 | bool t_rpc_command_executor::check_blockchain_pruning() |
| 2370 | { |
| 2371 | cryptonote::COMMAND_RPC_PRUNE_BLOCKCHAIN::request req; |
| 2372 | cryptonote::COMMAND_RPC_PRUNE_BLOCKCHAIN::response res; |
| 2373 | std::string fail_message = "Unsuccessful"; |
| 2374 | epee::json_rpc::error error_resp; |
| 2375 | |
| 2376 | req.check = true; |
| 2377 | |
| 2378 | if (m_is_rpc) |
| 2379 | { |
| 2380 | if (!m_rpc_client->json_rpc_request(req, res, "prune_blockchain", fail_message.c_str())) |
| 2381 | { |
| 2382 | return true; |
| 2383 | } |
| 2384 | } |
| 2385 | else |
| 2386 | { |
| 2387 | if (!m_rpc_server->on_prune_blockchain(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 2388 | { |
| 2389 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 2390 | return true; |
| 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | if (res.pruning_seed) |
| 2395 | { |
| 2396 | tools::success_msg_writer() << "Blockchain is pruned"; |
| 2397 | } |
| 2398 | else |
| 2399 | { |
| 2400 | tools::success_msg_writer() << "Blockchain is not pruned"; |
| 2401 | } |
| 2402 | return true; |
| 2403 | } |
| 2404 | |
| 2405 | bool t_rpc_command_executor::set_bootstrap_daemon( |
| 2406 | const std::string &address, |
| 2407 | const std::string &username, |
| 2408 | const std::string &password, |
| 2409 | const std::string &proxy) |
| 2410 | { |
| 2411 | cryptonote::COMMAND_RPC_SET_BOOTSTRAP_DAEMON::request req; |
| 2412 | cryptonote::COMMAND_RPC_SET_BOOTSTRAP_DAEMON::response res; |
| 2413 | const std::string fail_message = "Unsuccessful"; |
| 2414 | |
| 2415 | req.address = address; |
| 2416 | req.username = username; |
| 2417 | req.password = password; |
| 2418 | req.proxy = proxy; |
| 2419 | |
| 2420 | if (m_is_rpc) |
| 2421 | { |
| 2422 | if (!m_rpc_client->rpc_request(req, res, "/set_bootstrap_daemon", fail_message)) |
| 2423 | { |
| 2424 | return true; |
| 2425 | } |
| 2426 | } |
| 2427 | else |
| 2428 | { |
| 2429 | if (!m_rpc_server->on_set_bootstrap_daemon(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 2430 | { |
| 2431 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 2432 | return true; |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | tools::success_msg_writer() |
| 2437 | << "Successfully set bootstrap daemon address to " |
| 2438 | << (!req.address.empty() ? req.address : "none"); |
| 2439 | |
| 2440 | return true; |
| 2441 | } |
| 2442 | |
| 2443 | bool t_rpc_command_executor::flush_cache(bool bad_blocks) |
| 2444 | { |
| 2445 | cryptonote::COMMAND_RPC_FLUSH_CACHE::request req; |
| 2446 | cryptonote::COMMAND_RPC_FLUSH_CACHE::response res; |
| 2447 | std::string fail_message = "Unsuccessful"; |
| 2448 | epee::json_rpc::error error_resp; |
| 2449 | |
| 2450 | req.bad_blocks = bad_blocks; |
| 2451 | |
| 2452 | if (m_is_rpc) |
| 2453 | { |
| 2454 | if (!m_rpc_client->json_rpc_request(req, res, "flush_cache", fail_message.c_str())) |
| 2455 | { |
| 2456 | return true; |
| 2457 | } |
| 2458 | } |
| 2459 | else |
| 2460 | { |
| 2461 | if (!m_rpc_server->on_flush_cache(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 2462 | { |
| 2463 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 2464 | return true; |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | return true; |
| 2469 | } |
| 2470 | |
| 2471 | bool t_rpc_command_executor::rpc_payments() |
| 2472 | { |
| 2473 | cryptonote::COMMAND_RPC_ACCESS_DATA::request req; |
| 2474 | cryptonote::COMMAND_RPC_ACCESS_DATA::response res; |
| 2475 | std::string fail_message = "Unsuccessful"; |
| 2476 | epee::json_rpc::error error_resp; |
| 2477 | |
| 2478 | if (m_is_rpc) |
| 2479 | { |
| 2480 | if (!m_rpc_client->json_rpc_request(req, res, "rpc_access_data", fail_message.c_str())) |
| 2481 | { |
| 2482 | return true; |
| 2483 | } |
| 2484 | } |
| 2485 | else |
| 2486 | { |
| 2487 | if (!m_rpc_server->on_rpc_access_data(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK) |
| 2488 | { |
| 2489 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 2490 | return true; |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | const uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count(); |
| 2495 | uint64_t balance = 0; |
| 2496 | tools::msg_writer() << boost::format("%64s %16u %16u %8u %8u %8u %8u %s") |
| 2497 | % "Client ID" % "Balance" % "Total mined" % "Good" % "Stale" % "Bad" % "Dupes" % "Last update"; |
| 2498 | for (const auto &entry: res.entries) |
| 2499 | { |
| 2500 | tools::msg_writer() << boost::format("%64s %16u %16u %8u %8u %8u %8u %s") |
| 2501 | % entry.client % entry.balance % entry.credits_total |
| 2502 | % entry.nonces_good % entry.nonces_stale % entry.nonces_bad % entry.nonces_dupe |
| 2503 | % (entry.last_update_time == 0 ? "never" : get_human_time_ago(entry.last_update_time, now).c_str()); |
| 2504 | balance += entry.balance; |
| 2505 | } |
| 2506 | tools::msg_writer() << res.entries.size() << " clients with a total of " << balance << " credits"; |
| 2507 | tools::msg_writer() << "Aggregated client hash rate: " << get_mining_speed(res.hashrate); |
| 2508 | |
| 2509 | return true; |
| 2510 | } |
| 2511 | |
| 2512 | bool t_rpc_command_executor::version() |
| 2513 | { |
| 2514 | cryptonote::COMMAND_RPC_GET_INFO::request req; |
| 2515 | cryptonote::COMMAND_RPC_GET_INFO::response res; |
| 2516 | |
| 2517 | const char *fail_message = "Problem fetching info"; |
| 2518 | |
| 2519 | if (m_is_rpc) |
| 2520 | { |
| 2521 | if (!m_rpc_client->rpc_request(req, res, "/getinfo", fail_message)) |
| 2522 | { |
| 2523 | return true; |
| 2524 | } |
| 2525 | } |
| 2526 | else |
| 2527 | { |
| 2528 | if (!m_rpc_server->on_get_info(req, res) || res.status != CORE_RPC_STATUS_OK) |
| 2529 | { |
| 2530 | tools::fail_msg_writer() << make_error(fail_message, res.status); |
| 2531 | return true; |
| 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | if (res.version.empty() || !cryptonote::rpc::is_version_string_valid(res.version)) |
| 2536 | { |
| 2537 | tools::fail_msg_writer() << "The daemon software version is not available."; |
| 2538 | } |
| 2539 | else |
| 2540 | { |
| 2541 | tools::success_msg_writer() << res.version; |
| 2542 | } |
| 2543 | |
| 2544 | return true; |
| 2545 | } |
| 2546 | |
| 2547 | }// namespace daemonize |