Debian Codegraph @

src/daemon/command_parser_executor.cpp

raw symbols SCIP
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 #include "common/dns_utils.h"
30 #include "common/command_line.h"
31 #include "net/parse.h"
32 #include "daemon/command_parser_executor.h"
33 #include <boost/filesystem.hpp>
34 #include <boost/algorithm/string/predicate.hpp>
35
36 #undef MONERO_DEFAULT_LOG_CATEGORY
37 #define MONERO_DEFAULT_LOG_CATEGORY "daemon"
38
39 namespace daemonize {
40
41 t_command_parser_executor::t_command_parser_executor(
42 uint32_t ip
43 , uint16_t port
44 , const boost::optional<tools::login>& login
45 , const epee::net_utils::ssl_options_t& ssl_options
46 , bool is_rpc
47 , cryptonote::core_rpc_server* rpc_server
48 )
49 : m_executor(ip, port, login, ssl_options, is_rpc, rpc_server)
50 {}
51
52 bool t_command_parser_executor::print_peer_list(const std::vector<std::string>& args)
53 {
54 if (args.size() > 3)
55 {
56 std::cout << "Invalid syntax: Too many parameters. For more details, use the help command." << std::endl;
57 return true;
58 }
59
60 bool white = false;
61 bool gray = false;
62 bool pruned = false;
63 bool publicrpc = false;
64 size_t limit = 0;
65 for (size_t i = 0; i < args.size(); ++i)
66 {
67 if (args[i] == "white")
68 {
69 white = true;
70 }
71 else if (args[i] == "gray")
72 {
73 gray = true;
74 }
75 else if (args[i] == "pruned")
76 {
77 pruned = true;
78 }
79 else if (args[i] == "publicrpc")
80 {
81 publicrpc = true;
82 }
83 else if (!epee::string_tools::get_xtype_from_string(limit, args[i]))
84 {
85 std::cout << "Invalid syntax: Unexpected parameter: " << args[i] << ". For more details, use the help command." << std::endl;
86 return true;
87 }
88 }
89
90 const bool print_both = !white && !gray;
91 return m_executor.print_peer_list(white | print_both, gray | print_both, limit, pruned, publicrpc);
92 }
93
94 bool t_command_parser_executor::print_peer_list_stats(const std::vector<std::string>& args)
95 {
96 if (!args.empty()) {
97 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
98 return true;
99 }
100
101 return m_executor.print_peer_list_stats();
102 }
103
104 bool t_command_parser_executor::save_blockchain(const std::vector<std::string>& args)
105 {
106 if (!args.empty()) {
107 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
108 return true;
109 }
110 return m_executor.save_blockchain();
111 }
112
113 bool t_command_parser_executor::show_hash_rate(const std::vector<std::string>& args)
114 {
115 if (!args.empty()) {
116 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
117 return true;
118 }
119
120 return m_executor.show_hash_rate();
121 }
122
123 bool t_command_parser_executor::hide_hash_rate(const std::vector<std::string>& args)
124 {
125 if (!args.empty()) {
126 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
127 return true;
128 }
129
130 return m_executor.hide_hash_rate();
131 }
132
133 bool t_command_parser_executor::show_difficulty(const std::vector<std::string>& args)
134 {
135 if (!args.empty()) {
136 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
137 return true;
138 }
139
140 return m_executor.show_difficulty();
141 }
142
143 bool t_command_parser_executor::show_status(const std::vector<std::string>& args)
144 {
145 if (!args.empty()) {
146 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
147 return true;
148 }
149
150 return m_executor.show_status();
151 }
152
153 bool t_command_parser_executor::print_connections(const std::vector<std::string>& args)
154 {
155 if (!args.empty()) {
156 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
157 return true;
158 }
159
160 return m_executor.print_connections();
161 }
162
163 bool t_command_parser_executor::print_net_stats(const std::vector<std::string>& args)
164 {
165 if (!args.empty()) {
166 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
167 return true;
168 }
169
170 return m_executor.print_net_stats();
171 }
172
173 bool t_command_parser_executor::print_blockchain_info(const std::vector<std::string>& args)
174 {
175 if(!args.size())
176 {
177 std::cout << "Invalid syntax: At least one parameter expected. For more details, use the help command." << std::endl;
178 return true;
179 }
180 uint64_t start_index = 0;
181 uint64_t end_index = 0;
182 if (args[0][0] == '-')
183 {
184 int64_t nblocks;
185 if(!epee::string_tools::get_xtype_from_string(nblocks, args[0]))
186 {
187 std::cout << "Invalid syntax: Wrong number of blocks. For more details, use the help command." << std::endl;
188 return true;
189 }
190 return m_executor.print_blockchain_info(nblocks, (uint64_t)-nblocks);
191 }
192 if(!epee::string_tools::get_xtype_from_string(start_index, args[0]))
193 {
194 std::cout << "Invalid syntax: Wrong starter block index parameter. For more details, use the help command." << std::endl;
195 return true;
196 }
197 if(args.size() >1 && !epee::string_tools::get_xtype_from_string(end_index, args[1]))
198 {
199 std::cout << "Invalid syntax: Wrong end block index parameter. For more details, use the help command." << std::endl;
200 return true;
201 }
202
203 return m_executor.print_blockchain_info(start_index, end_index);
204 }
205
206 bool t_command_parser_executor::set_log_level(const std::vector<std::string>& args)
207 {
208 if(args.size() > 1)
209 {
210 std::cout << "Invalid syntax: Too many parameters. For more details, use the help command." << std::endl;
211 return true;
212 }
213
214 if (args.empty())
215 {
216 return m_executor.set_log_categories("+");
217 }
218
219 uint16_t l = 0;
220 if(epee::string_tools::get_xtype_from_string(l, args[0]))
221 {
222 if(4 < l)
223 {
224 std::cout << "Invalid syntax: Wrong number range, use: set_log <log_level_number_0-4>. For more details, use the help command." << std::endl;
225 return true;
226 }
227 return m_executor.set_log_level(l);
228 }
229 else
230 {
231 return m_executor.set_log_categories(args.front());
232 }
233 }
234
235 bool t_command_parser_executor::print_height(const std::vector<std::string>& args)
236 {
237 if (!args.empty()) {
238 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
239 return true;
240 }
241
242 return m_executor.print_height();
243 }
244
245 bool t_command_parser_executor::print_block(const std::vector<std::string>& args)
246 {
247 bool include_hex = false;
248
249 // Assumes that optional flags come after mandatory argument <transaction_hash>
250 for (unsigned int i = 1; i < args.size(); ++i) {
251 if (args[i] == "+hex")
252 include_hex = true;
253 else
254 {
255 std::cout << "Invalid syntax: Unexpected parameter: " << args[i] << ". For more details, use the help command." << std::endl;
256 return true;
257 }
258 }
259 if (args.empty())
260 {
261 std::cout << "Invalid syntax: At least one parameter expected. For more details, use the help command." << std::endl;
262 return true;
263 }
264
265 const std::string& arg = args.front();
266 try
267 {
268 uint64_t height = boost::lexical_cast<uint64_t>(arg);
269 return m_executor.print_block_by_height(height, include_hex);
270 }
271 catch (const boost::bad_lexical_cast&)
272 {
273 crypto::hash block_hash;
274 if (parse_hash256(arg, block_hash))
275 {
276 return m_executor.print_block_by_hash(block_hash, include_hex);
277 }
278 }
279
280 return true;
281 }
282
283 bool t_command_parser_executor::print_transaction(const std::vector<std::string>& args)
284 {
285 bool include_metadata = false;
286 bool include_hex = false;
287 bool include_json = false;
288
289 // Assumes that optional flags come after mandatory argument <transaction_hash>
290 for (unsigned int i = 1; i < args.size(); ++i) {
291 if (args[i] == "+meta")
292 include_metadata = true;
293 else if (args[i] == "+hex")
294 include_hex = true;
295 else if (args[i] == "+json")
296 include_json = true;
297 else
298 {
299 std::cout << "Invalid syntax: Unexpected parameter: " << args[i] << ". For more details, use the help command." << std::endl;
300 return true;
301 }
302 }
303 if (args.empty())
304 {
305 std::cout << "Invalid syntax: At least one parameter expected. For more details, use the help command." << std::endl;
306 return true;
307 }
308
309 const std::string& str_hash = args.front();
310 crypto::hash tx_hash;
311 if (parse_hash256(str_hash, tx_hash))
312 {
313 m_executor.print_transaction(tx_hash, include_metadata, include_hex, include_json);
314 }
315
316 return true;
317 }
318
319 bool t_command_parser_executor::is_key_image_spent(const std::vector<std::string>& args)
320 {
321 if (args.empty())
322 {
323 std::cout << "Invalid syntax: At least one parameter expected. For more details, use the help command." << std::endl;
324 return true;
325 }
326
327 const std::string& str = args.front();
328 crypto::key_image ki;
329 crypto::hash hash;
330 if (parse_hash256(str, hash))
331 {
332 memcpy(&ki, &hash, sizeof(ki));
333 m_executor.is_key_image_spent(ki);
334 }
335
336 return true;
337 }
338
339 bool t_command_parser_executor::print_transaction_pool_long(const std::vector<std::string>& args)
340 {
341 if (!args.empty()) {
342 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
343 return true;
344 }
345
346 return m_executor.print_transaction_pool_long();
347 }
348
349 bool t_command_parser_executor::print_transaction_pool_short(const std::vector<std::string>& args)
350 {
351 if (!args.empty()) {
352 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
353 return true;
354 }
355
356 return m_executor.print_transaction_pool_short();
357 }
358
359 bool t_command_parser_executor::print_transaction_pool_stats(const std::vector<std::string>& args)
360 {
361 if (!args.empty()) {
362 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
363 return true;
364 }
365
366 return m_executor.print_transaction_pool_stats();
367 }
368
369 bool t_command_parser_executor::start_mining(const std::vector<std::string>& args)
370 {
371 if(!args.size())
372 {
373 std::cout << "Invalid syntax: At least one parameter expected. For more details, use the help command." << std::endl;
374 return true;
375 }
376
377 cryptonote::address_parse_info info;
378 cryptonote::network_type nettype = cryptonote::MAINNET;
379 if(!cryptonote::get_account_address_from_str(info, cryptonote::MAINNET, args.front()))
380 {
381 if(!cryptonote::get_account_address_from_str(info, cryptonote::TESTNET, args.front()))
382 {
383 if(!cryptonote::get_account_address_from_str(info, cryptonote::STAGENET, args.front()))
384 {
385 bool dnssec_valid;
386 std::string address_str = tools::dns_utils::get_account_address_as_str_from_url(args.front(), dnssec_valid,
387 [](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid){return addresses[0];});
388 if(!cryptonote::get_account_address_from_str(info, cryptonote::MAINNET, address_str))
389 {
390 if(!cryptonote::get_account_address_from_str(info, cryptonote::TESTNET, address_str))
391 {
392 if(!cryptonote::get_account_address_from_str(info, cryptonote::STAGENET, address_str))
393 {
394 std::cout << "Invalid syntax: Target account address has wrong format. For more details, use the help command." << std::endl;
395 return true;
396 }
397 else
398 {
399 nettype = cryptonote::STAGENET;
400 }
401 }
402 else
403 {
404 nettype = cryptonote::TESTNET;
405 }
406 }
407 }
408 else
409 {
410 nettype = cryptonote::STAGENET;
411 }
412 }
413 else
414 {
415 nettype = cryptonote::TESTNET;
416 }
417 }
418 if (info.is_subaddress)
419 {
420 tools::fail_msg_writer() << "subaddress for mining reward is not yet supported!" << std::endl;
421 return true;
422 }
423 if(nettype != cryptonote::MAINNET)
424 std::cout << "Mining to a " << (nettype == cryptonote::TESTNET ? "testnet" : "stagenet") << " address, make sure this is intentional!" << std::endl;
425 uint64_t threads_count = 1;
426 bool do_background_mining = false;
427 bool ignore_battery = false;
428 if(args.size() > 4)
429 {
430 std::cout << "Invalid syntax: Too many parameters. For more details, use the help command." << std::endl;
431 return true;
432 }
433
434 if(args.size() == 4)
435 {
436 if(args[3] == "true" || command_line::is_yes(args[3]) || args[3] == "1")
437 {
438 ignore_battery = true;
439 }
440 else if(args[3] != "false" && !command_line::is_no(args[3]) && args[3] != "0")
441 {
442 std::cout << "Invalid syntax: Invalid combination of parameters. For more details, use the help command." << std::endl;
443 return true;
444 }
445 }
446
447 if(args.size() >= 3)
448 {
449 if(args[2] == "true" || command_line::is_yes(args[2]) || args[2] == "1")
450 {
451 do_background_mining = true;
452 }
453 else if(args[2] != "false" && !command_line::is_no(args[2]) && args[2] != "0")
454 {
455 std::cout << "Invalid syntax: Invalid combination of parameters. For more details, use the help command." << std::endl;
456 return true;
457 }
458 }
459
460 if(args.size() >= 2)
461 {
462 if (args[1] == "auto" || args[1] == "autodetect")
463 {
464 threads_count = 0;
465 }
466 else
467 {
468 bool ok = epee::string_tools::get_xtype_from_string(threads_count, args[1]);
469 threads_count = (ok && 0 < threads_count) ? threads_count : 1;
470 }
471 }
472
473 m_executor.start_mining(info.address, threads_count, nettype, do_background_mining, ignore_battery);
474
475 return true;
476 }
477
478 bool t_command_parser_executor::stop_mining(const std::vector<std::string>& args)
479 {
480 if (!args.empty()) {
481 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
482 return true;
483 }
484
485 return m_executor.stop_mining();
486 }
487
488 bool t_command_parser_executor::mining_status(const std::vector<std::string>& args)
489 {
490 return m_executor.mining_status();
491 }
492
493 bool t_command_parser_executor::stop_daemon(const std::vector<std::string>& args)
494 {
495 if (!args.empty()) {
496 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
497 return true;
498 }
499
500 return m_executor.stop_daemon();
501 }
502
503 bool t_command_parser_executor::print_status(const std::vector<std::string>& args)
504 {
505 if (!args.empty()) {
506 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
507 return true;
508 }
509
510 return m_executor.print_status();
511 }
512
513 bool t_command_parser_executor::set_limit(const std::vector<std::string>& args)
514 {
515 if(args.size()>1) {
516 std::cout << "Invalid syntax: Too many parameters. For more details, use the help command." << std::endl;
517 return true;
518 }
519
520 if(args.size()==0) {
521 return m_executor.get_limit();
522 }
523 int64_t limit;
524 try {
525 limit = std::stoll(args[0]);
526 }
527 catch(const std::exception& ex) {
528 std::cout << "Invalid syntax: Failed to parse limit. For more details, use the help command." << std::endl;
529 return true;
530 }
531
532 return m_executor.set_limit(limit, limit);
533 }
534
535 bool t_command_parser_executor::set_limit_up(const std::vector<std::string>& args)
536 {
537 if(args.size()>1) {
538 std::cout << "Invalid syntax: Too many parameters. For more details, use the help command." << std::endl;
539 return true;
540 }
541
542 if(args.size()==0) {
543 return m_executor.get_limit_up();
544 }
545 int64_t limit;
546 try {
547 limit = std::stoll(args[0]);
548 }
549 catch(const std::exception& ex) {
550 std::cout << "Invalid syntax: Failed to parse limit. For more details, use the help command." << std::endl;
551 return true;
552 }
553
554 return m_executor.set_limit(0, limit);
555 }
556
557 bool t_command_parser_executor::set_limit_down(const std::vector<std::string>& args)
558 {
559 if(args.size()>1) {
560 std::cout << "Invalid syntax: Too many parameters. For more details, use the help command." << std::endl;
561 return true;
562 }
563
564 if(args.size()==0) {
565 return m_executor.get_limit_down();
566 }
567 int64_t limit;
568 try {
569 limit = std::stoll(args[0]);
570 }
571 catch(const std::exception& ex) {
572 std::cout << "Invalid syntax: Failed to parse limit. For more details, use the help command." << std::endl;
573 return true;
574 }
575
576 return m_executor.set_limit(limit, 0);
577 }
578
579 bool t_command_parser_executor::out_peers(const std::vector<std::string>& args)
580 {
581 bool set = false;
582 uint32_t limit = 0;
583 try {
584 if (!args.empty())
585 {
586 limit = std::stoi(args[0]);
587 set = true;
588 }
589 }
590
591 catch(const std::exception& ex) {
592 _erro("stoi exception");
593 std::cout << "Invalid syntax: Failed to parse number. For more details, use the help command." << std::endl;
594 return true;
595 }
596
597 return m_executor.out_peers(set, limit);
598 }
599
600 bool t_command_parser_executor::in_peers(const std::vector<std::string>& args)
601 {
602 bool set = false;
603 uint32_t limit = 0;
604 try {
605 if (!args.empty())
606 {
607 limit = std::stoi(args[0]);
608 set = true;
609 }
610 }
611
612 catch(const std::exception& ex) {
613 _erro("stoi exception");
614 std::cout << "Invalid syntax: Failed to parse number." << std::endl;
615 return true;
616 }
617
618 return m_executor.in_peers(set, limit);
619 }
620
621 bool t_command_parser_executor::hard_fork_info(const std::vector<std::string>& args)
622 {
623 int version;
624 if (args.size() == 0) {
625 version = 0;
626 }
627 else if (args.size() == 1) {
628 try {
629 version = std::stoi(args[0]);
630 }
631 catch(const std::exception& ex) {
632 std::cout << "Invalid syntax: Failed to parse version number. For more details, use the help command." << std::endl;
633 return true;
634 }
635 if (version <= 0 || version > 255) {
636 std::cout << "Invalid syntax: Unknown version number. Must be between 0 and 255. For more details, use the help command." << std::endl;
637 return true;
638 }
639 }
640 else {
641 std::cout << "Invalid syntax: Too many parameters. For more details, use the help command." << std::endl;
642 return true;
643 }
644 return m_executor.hard_fork_info(version);
645 }
646
647 bool t_command_parser_executor::show_bans(const std::vector<std::string>& args)
648 {
649 if (!args.empty()) {
650 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
651 return true;
652 }
653
654 return m_executor.print_bans();
655 }
656
657 bool t_command_parser_executor::ban(const std::vector<std::string>& args)
658 {
659 if (args.size() != 1 && args.size() != 2) {
660 std::cout << "Invalid syntax: Expects one or two parameters. For more details, use the help command." << std::endl;
661 return true;
662 }
663 time_t seconds = P2P_IP_BLOCKTIME;
664 if (args.size() > 1)
665 {
666 try
667 {
668 seconds = std::stoi(args[1]);
669 }
670 catch (const std::exception &e)
671 {
672 std::cout << "Invalid syntax: Failed to parse seconds. For more details, use the help command." << std::endl;
673 return true;
674 }
675 if (seconds == 0)
676 {
677 std::cout << "Seconds must be greater than 0." << std::endl;
678 return true;
679 }
680 }
681 if (boost::starts_with(args[0], "@"))
682 {
683 const std::string ban_list = args[0].substr(1);
684
685 try
686 {
687 const boost::filesystem::path ban_list_path(ban_list);
688 boost::system::error_code ec;
689 if (!boost::filesystem::exists(ban_list_path, ec))
690 {
691 std::cout << "Can't find ban list file " + ban_list + " - " + ec.message() << std::endl;
692 return true;
693 }
694
695 bool ret = true;
696 std::ifstream ifs(ban_list_path.string());
697 for (std::string line; std::getline(ifs, line); )
698 {
699 // ignore comments after '#' character
700 const size_t pound_idx = line.find('#');
701 if (pound_idx != std::string::npos)
702 line.resize(pound_idx);
703
704 // trim whitespace and ignore empty lines
705 boost::trim(line);
706 if (line.empty())
707 continue;
708
709 auto subnet = net::get_ipv4_subnet_address(line);
710 if (subnet)
711 {
712 ret &= m_executor.ban(subnet->str(), seconds);
713 continue;
714 }
715 const expect<epee::net_utils::network_address> parsed_addr = net::get_network_address(line, 0);
716 if (parsed_addr)
717 {
718 ret &= m_executor.ban(parsed_addr->host_str(), seconds);
719 continue;
720 }
721 std::cout << "Invalid IP address or IPv4 subnet: " << line << std::endl;
722 }
723 return ret;
724 }
725 catch (const std::exception &e)
726 {
727 std::cout << "Error loading ban list: " << e.what() << std::endl;
728 return false;
729 }
730 }
731 else
732 {
733 const std::string ip = args[0];
734 return m_executor.ban(ip, seconds);
735 }
736 }
737
738 bool t_command_parser_executor::unban(const std::vector<std::string>& args)
739 {
740 if (args.size() != 1) {
741 std::cout << "Invalid syntax: One parameter expected. For more details, use the help command." << std::endl;
742 return true;
743 }
744
745 std::string ip = args[0];
746 return m_executor.unban(ip);
747 }
748
749 bool t_command_parser_executor::banned(const std::vector<std::string>& args)
750 {
751 if (args.size() != 1) {
752 std::cout << "Invalid syntax: One parameter expected. For more details, use the help command." << std::endl;
753 return true;
754 }
755 std::string address = args[0];
756 return m_executor.banned(address);
757 }
758
759 bool t_command_parser_executor::flush_txpool(const std::vector<std::string>& args)
760 {
761 if (args.size() > 1) {
762 std::cout << "Invalid syntax: Too many parameters. For more details, use the help command." << std::endl;
763 return true;
764 }
765
766 std::string txid;
767 if (args.size() == 1)
768 {
769 crypto::hash hash;
770 if (!parse_hash256(args[0], hash))
771 {
772 std::cout << "Invalid syntax: Failed to parse tx id. For more details, use the help command." << std::endl;
773 return true;
774 }
775 txid = args[0];
776 }
777 return m_executor.flush_txpool(txid);
778 }
779
780 bool t_command_parser_executor::output_histogram(const std::vector<std::string>& args)
781 {
782 std::vector<uint64_t> amounts;
783 uint64_t min_count = 3;
784 uint64_t max_count = 0;
785 size_t n_raw = 0;
786
787 for (size_t n = 0; n < args.size(); ++n)
788 {
789 if (args[n][0] == '@')
790 {
791 amounts.push_back(boost::lexical_cast<uint64_t>(args[n].c_str() + 1));
792 }
793 else if (n_raw == 0)
794 {
795 min_count = boost::lexical_cast<uint64_t>(args[n]);
796 n_raw++;
797 }
798 else if (n_raw == 1)
799 {
800 max_count = boost::lexical_cast<uint64_t>(args[n]);
801 n_raw++;
802 }
803 else
804 {
805 std::cout << "Invalid syntax: More than two non-amount parameters. For more details, use the help command." << std::endl;
806 return true;
807 }
808 }
809 return m_executor.output_histogram(amounts, min_count, max_count);
810 }
811
812 bool t_command_parser_executor::print_coinbase_tx_sum(const std::vector<std::string>& args)
813 {
814 if(!args.size())
815 {
816 std::cout << "Invalid syntax: At least one parameter expected. For more details, use the help command." << std::endl;
817 return true;
818 }
819
820 uint64_t height = 0;
821 uint64_t count = 0;
822 if(!epee::string_tools::get_xtype_from_string(height, args[0]))
823 {
824 std::cout << "Invalid syntax: Wrong starter block height parameter. For more details, use the help command." << std::endl;
825 return true;
826 }
827 if(args.size() >1 && !epee::string_tools::get_xtype_from_string(count, args[1]))
828 {
829 std::cout << "wrong count parameter" << std::endl;
830 return true;
831 }
832
833 return m_executor.print_coinbase_tx_sum(height, count);
834 }
835
836 bool t_command_parser_executor::alt_chain_info(const std::vector<std::string>& args)
837 {
838 if(args.size() > 1)
839 {
840 std::cout << "Invalid syntax: Too many parameters. For more details, use the help command." << std::endl;
841 return true;
842 }
843
844 std::string tip;
845 size_t above = 0;
846 uint64_t last_blocks = 0;
847 if (args.size() == 1)
848 {
849 if (args[0].size() > 0 && args[0][0] == '>')
850 {
851 if (!epee::string_tools::get_xtype_from_string(above, args[0].c_str() + 1))
852 {
853 std::cout << "Invalid syntax: Invalid above parameter. For more details, use the help command." << std::endl;
854 return true;
855 }
856 }
857 else if (args[0].size() > 0 && args[0][0] == '-')
858 {
859 if (!epee::string_tools::get_xtype_from_string(last_blocks, args[0].c_str() + 1))
860 {
861 std::cout << "Invalid syntax: Invalid last_blocks parameter. For more details, use the help command." << std::endl;
862 return true;
863 }
864 }
865 else
866 {
867 tip = args[0];
868 }
869 }
870
871 return m_executor.alt_chain_info(tip, above, last_blocks);
872 }
873
874 bool t_command_parser_executor::print_blockchain_dynamic_stats(const std::vector<std::string>& args)
875 {
876 if(args.size() != 1)
877 {
878 std::cout << "Invalid syntax: One parameter expected. For more details, use the help command." << std::endl;
879 return true;
880 }
881
882 uint64_t nblocks = 0;
883 if(!epee::string_tools::get_xtype_from_string(nblocks, args[0]) || nblocks == 0)
884 {
885 std::cout << "Invalid syntax: Wrong number of blocks. For more details, use the help command." << std::endl;
886 return true;
887 }
888
889 return m_executor.print_blockchain_dynamic_stats(nblocks);
890 }
891
892 bool t_command_parser_executor::update(const std::vector<std::string>& args)
893 {
894 if (args.size() != 1)
895 {
896 std::cout << "Invalid syntax: One parameter expected. For more details, use the help command." << std::endl;
897 return true;
898 }
899
900 return m_executor.update(args.front());
901 }
902
903 bool t_command_parser_executor::relay_tx(const std::vector<std::string>& args)
904 {
905 if (args.size() != 1)
906 {
907 std::cout << "Invalid syntax: One parameter expected. For more details, use the help command." << std::endl;
908 return true;
909 }
910
911 std::string txid;
912 crypto::hash hash;
913 if (!parse_hash256(args[0], hash))
914 {
915 std::cout << "Invalid syntax: Failed to parse tx id. For more details, use the help command." << std::endl;
916 return true;
917 }
918 txid = args[0];
919 return m_executor.relay_tx(txid);
920 }
921
922 bool t_command_parser_executor::sync_info(const std::vector<std::string>& args)
923 {
924 if (args.size() != 0) {
925 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
926 return true;
927 }
928
929 return m_executor.sync_info();
930 }
931
932 bool t_command_parser_executor::pop_blocks(const std::vector<std::string>& args)
933 {
934 if (args.size() != 1)
935 {
936 std::cout << "Invalid syntax: One parameter expected. For more details, use the help command." << std::endl;
937 return true;
938 }
939
940 try
941 {
942 uint64_t nblocks = boost::lexical_cast<uint64_t>(args[0]);
943 if (nblocks < 1)
944 {
945 std::cout << "Invalid syntax: Number of blocks must be greater than 0. For more details, use the help command." << std::endl;
946 return true;
947 }
948 return m_executor.pop_blocks(nblocks);
949 }
950 catch (const boost::bad_lexical_cast&)
951 {
952 std::cout << "Invalid syntax: Number of blocks must be a number greater than 0. For more details, use the help command." << std::endl;
953 }
954 return true;
955 }
956
957 bool t_command_parser_executor::rpc_payments(const std::vector<std::string>& args)
958 {
959 if (args.size() != 0) {
960 std::cout << "Invalid syntax: No parameters expected. For more details, use the help command." << std::endl;
961 return true;
962 }
963
964 return m_executor.rpc_payments();
965 }
966
967 bool t_command_parser_executor::version(const std::vector<std::string>& args)
968 {
969 return m_executor.version();
970 }
971
972 bool t_command_parser_executor::prune_blockchain(const std::vector<std::string>& args)
973 {
974 if (args.size() > 1)
975 {
976 std::cout << "Invalid syntax: Too many parameters. For more details, use the help command." << std::endl;
977 return true;
978 }
979
980 if (args.empty() || args[0] != "confirm")
981 {
982 std::cout << "Warning: pruning from within monerod will not shrink the database file size." << std::endl;
983 std::cout << "Instead, parts of the file will be marked as free, so the file will not grow" << std::endl;
984 std::cout << "until that newly free space is used up. If you want a smaller file size now," << std::endl;
985 std::cout << "exit monerod and run monero-blockchain-prune (you will temporarily need more" << std::endl;
986 std::cout << "disk space for the database conversion though). If you are OK with the database" << std::endl;
987 std::cout << "file keeping the same size, re-run this command with the \"confirm\" parameter." << std::endl;
988 return true;
989 }
990
991 return m_executor.prune_blockchain();
992 }
993
994 bool t_command_parser_executor::check_blockchain_pruning(const std::vector<std::string>& args)
995 {
996 return m_executor.check_blockchain_pruning();
997 }
998
999 bool t_command_parser_executor::set_bootstrap_daemon(const std::vector<std::string>& args)
1000 {
1001 struct parsed_t
1002 {
1003 std::string address;
1004 std::string user;
1005 std::string password;
1006 std::string proxy;
1007 };
1008
1009 boost::optional<parsed_t> parsed = [&args]() -> boost::optional<parsed_t> {
1010 const size_t args_count = args.size();
1011 if (args_count == 0)
1012 {
1013 return {};
1014 }
1015 if (args[0] == "auto")
1016 {
1017 if (args_count == 1)
1018 {
1019 return {{args[0], "", "", ""}};
1020 }
1021 if (args_count == 2)
1022 {
1023 return {{args[0], "", "", args[1]}};
1024 }
1025 }
1026 else if (args[0] == "none")
1027 {
1028 if (args_count == 1)
1029 {
1030 return {{"", "", "", ""}};
1031 }
1032 }
1033 else
1034 {
1035 if (args_count == 1)
1036 {
1037 return {{args[0], "", "", ""}};
1038 }
1039 if (args_count == 2)
1040 {
1041 return {{args[0], "", "", args[1]}};
1042 }
1043 if (args_count == 3)
1044 {
1045 return {{args[0], args[1], args[2], ""}};
1046 }
1047 if (args_count == 4)
1048 {
1049 return {{args[0], args[1], args[2], args[3]}};
1050 }
1051 }
1052 return {};
1053 }();
1054
1055 if (!parsed)
1056 {
1057 std::cout << "Invalid syntax: Wrong number of parameters. For more details, use the help command." << std::endl;
1058 return true;
1059 }
1060
1061 return m_executor.set_bootstrap_daemon(parsed->address, parsed->user, parsed->password, parsed->proxy);
1062 }
1063
1064 bool t_command_parser_executor::flush_cache(const std::vector<std::string>& args)
1065 {
1066 bool bad_blocks = false;
1067 std::string arg;
1068
1069 if (args.empty())
1070 goto show_list;
1071
1072 for (size_t i = 0; i < args.size(); ++i)
1073 {
1074 arg = args[i];
1075 if (arg == "bad-blocks")
1076 bad_blocks = true;
1077 else
1078 goto show_list;
1079 }
1080 return m_executor.flush_cache(bad_blocks);
1081
1082 show_list:
1083 std::cout << "Invalid cache type: " << arg << std::endl;
1084 std::cout << "Cache types: bad-blocks" << std::endl;
1085 return true;
1086 }
1087
1088 } // namespace daemonize