1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
|
// Copyright (c) 2014-2016, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include "string_tools.h"
#include "common/scoped_message_writer.h"
#include "daemon/rpc_command_executor.h"
#include "rpc/core_rpc_server_commands_defs.h"
#include "cryptonote_core/cryptonote_core.h"
#include "cryptonote_core/hardfork.h"
#include <boost/format.hpp>
#include <ctime>
#include <string>
namespace daemonize {
namespace {
void print_peer(std::string const & prefix, cryptonote::peer const & peer)
{
time_t now;
time(&now);
time_t last_seen = static_cast<time_t>(peer.last_seen);
std::string id_str;
std::string port_str;
std::string elapsed = epee::misc_utils::get_time_interval_string(now - last_seen);
std::string ip_str = epee::string_tools::get_ip_string_from_int32(peer.ip);
epee::string_tools::xtype_to_string(peer.id, id_str);
epee::string_tools::xtype_to_string(peer.port, port_str);
std::string addr_str = ip_str + ":" + port_str;
tools::msg_writer() << boost::format("%-10s %-25s %-25s %s") % prefix % id_str % addr_str % elapsed;
}
void print_block_header(cryptonote::block_header_responce const & header)
{
tools::success_msg_writer()
<< "timestamp: " << boost::lexical_cast<std::string>(header.timestamp) << std::endl
<< "previous hash: " << header.prev_hash << std::endl
<< "nonce: " << boost::lexical_cast<std::string>(header.nonce) << std::endl
<< "is orphan: " << header.orphan_status << std::endl
<< "height: " << boost::lexical_cast<std::string>(header.height) << std::endl
<< "depth: " << boost::lexical_cast<std::string>(header.depth) << std::endl
<< "hash: " << header.hash
<< "difficulty: " << boost::lexical_cast<std::string>(header.difficulty) << std::endl
<< "reward: " << boost::lexical_cast<std::string>(header.reward);
}
}
t_rpc_command_executor::t_rpc_command_executor(
uint32_t ip
, uint16_t port
, bool is_rpc
, cryptonote::core_rpc_server* rpc_server
)
: m_rpc_client(NULL), m_rpc_server(rpc_server)
{
if (is_rpc)
{
m_rpc_client = new tools::t_rpc_client(ip, port);
}
else
{
if (rpc_server == NULL)
{
throw std::runtime_error("If not calling commands via RPC, rpc_server pointer must be non-null");
}
}
m_is_rpc = is_rpc;
}
t_rpc_command_executor::~t_rpc_command_executor()
{
if (m_rpc_client != NULL)
{
delete m_rpc_client;
}
}
bool t_rpc_command_executor::print_peer_list() {
cryptonote::COMMAND_RPC_GET_PEER_LIST::request req;
cryptonote::COMMAND_RPC_GET_PEER_LIST::response res;
std::string failure_message = "Couldn't retrieve peer list";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/get_peer_list", failure_message.c_str()))
{
return false;
}
}
else
{
if (!m_rpc_server->on_get_peer_list(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << failure_message;
return false;
}
}
for (auto & peer : res.white_list)
{
print_peer("white", peer);
}
for (auto & peer : res.gray_list)
{
print_peer("gray", peer);
}
return true;
}
bool t_rpc_command_executor::save_blockchain() {
cryptonote::COMMAND_RPC_SAVE_BC::request req;
cryptonote::COMMAND_RPC_SAVE_BC::response res;
std::string fail_message = "Couldn't save blockchain";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/save_bc", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_save_bc(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
tools::success_msg_writer() << "Blockchain saved";
return true;
}
bool t_rpc_command_executor::show_hash_rate() {
cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::request req;
cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::response res;
req.visible = true;
std::string fail_message = "Unsuccessful";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/set_log_hash_rate", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_set_log_hash_rate(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
}
}
tools::success_msg_writer() << "Hash rate logging is on";
return true;
}
bool t_rpc_command_executor::hide_hash_rate() {
cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::request req;
cryptonote::COMMAND_RPC_SET_LOG_HASH_RATE::response res;
req.visible = false;
std::string fail_message = "Unsuccessful";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/set_log_hash_rate", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_set_log_hash_rate(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
tools::success_msg_writer() << "Hash rate logging is off";
return true;
}
bool t_rpc_command_executor::show_difficulty() {
cryptonote::COMMAND_RPC_GET_INFO::request req;
cryptonote::COMMAND_RPC_GET_INFO::response res;
std::string fail_message = "Problem fetching info";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/getinfo", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_get_info(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
tools::success_msg_writer() << "BH: " << res.height
<< ", TH: " << res.top_block_hash
<< ", DIFF: " << res.difficulty
<< ", HR: " << (int) res.difficulty / res.target << " H/s";
return true;
}
static std::string get_mining_speed(uint64_t hr)
{
std::cerr << "get_mining_speed called with " << hr << std::endl;
if (hr>1e9) return (boost::format("%.2f GH/s") % (hr/1e9)).str();
if (hr>1e6) return (boost::format("%.2f MH/s") % (hr/1e6)).str();
if (hr>1e3) return (boost::format("%.2f kH/s") % (hr/1e3)).str();
return (boost::format("%.0f H/s") % hr).str();
}
bool t_rpc_command_executor::show_status() {
cryptonote::COMMAND_RPC_GET_INFO::request ireq;
cryptonote::COMMAND_RPC_GET_INFO::response ires;
cryptonote::COMMAND_RPC_HARD_FORK_INFO::request hfreq;
cryptonote::COMMAND_RPC_HARD_FORK_INFO::response hfres;
cryptonote::COMMAND_RPC_MINING_STATUS::request mreq;
cryptonote::COMMAND_RPC_MINING_STATUS::response mres;
epee::json_rpc::error error_resp;
std::string fail_message = "Problem fetching info";
hfreq.version = 0;
bool mining_busy = false;
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(ireq, ires, "/getinfo", fail_message.c_str()))
{
return true;
}
if (!m_rpc_client->json_rpc_request(hfreq, hfres, "hard_fork_info", fail_message.c_str()))
{
return true;
}
if (!m_rpc_client->rpc_request(mreq, mres, "/mining_status", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_get_info(ireq, ires) || ires.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
if (!m_rpc_server->on_hard_fork_info(hfreq, hfres, error_resp) || hfres.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
if (!m_rpc_server->on_mining_status(mreq, mres))
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
if (mres.status == CORE_RPC_STATUS_BUSY)
{
mining_busy = true;
}
else if (mres.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
tools::success_msg_writer() << boost::format("Height: %llu/%llu (%.1f%%) on %s, %s, net hash %s, v%u, %s, %u+%u connections")
% (unsigned long long)ires.height
% (unsigned long long)(ires.target_height >= ires.height ? ires.target_height : ires.height)
% (100.0f * ires.height / (ires.target_height ? ires.target_height < ires.height ? ires.height : ires.target_height : ires.height))
% (ires.testnet ? "testnet" : "mainnet")
% (mining_busy ? "syncing" : mres.active ? "mining at " + get_mining_speed(mres.speed) : "not mining")
% get_mining_speed(ires.difficulty / ires.target)
% (unsigned)hfres.version
% (hfres.state == cryptonote::HardFork::Ready ? "up to date" : hfres.state == cryptonote::HardFork::UpdateNeeded ? "update needed" : "out of date, likely forked")
% (unsigned)ires.outgoing_connections_count % (unsigned)ires.incoming_connections_count
;
return true;
}
bool t_rpc_command_executor::print_connections() {
cryptonote::COMMAND_RPC_GET_CONNECTIONS::request req;
cryptonote::COMMAND_RPC_GET_CONNECTIONS::response res;
epee::json_rpc::error error_resp;
std::string fail_message = "Unsuccessful";
if (m_is_rpc)
{
if (!m_rpc_client->json_rpc_request(req, res, "get_connections", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_get_connections(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
tools::msg_writer() << std::setw(30) << std::left << "Remote Host"
<< std::setw(20) << "Peer id"
<< std::setw(30) << "Recv/Sent (inactive,sec)"
<< std::setw(25) << "State"
<< std::setw(20) << "Livetime(sec)"
<< std::setw(12) << "Down (kB/s)"
<< std::setw(14) << "Down(now)"
<< std::setw(10) << "Up (kB/s)"
<< std::setw(13) << "Up(now)"
<< std::endl;
for (auto & info : res.connections)
{
std::string address = info.incoming ? "INC " : "OUT ";
address += info.ip + ":" + info.port;
//std::string in_out = info.incoming ? "INC " : "OUT ";
tools::msg_writer()
//<< std::setw(30) << std::left << in_out
<< std::setw(30) << std::left << address
<< std::setw(20) << info.peer_id
<< 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) + ")"
<< std::setw(25) << info.state
<< std::setw(20) << info.live_time
<< std::setw(12) << info.avg_download
<< std::setw(14) << info.current_download
<< std::setw(10) << info.avg_upload
<< std::setw(13) << info.current_upload
<< std::left << (info.localhost ? "[LOCALHOST]" : "")
<< std::left << (info.local_ip ? "[LAN]" : "");
//tools::msg_writer() << boost::format("%-25s peer_id: %-25s %s") % address % info.peer_id % in_out;
}
return true;
}
bool t_rpc_command_executor::print_blockchain_info(uint64_t start_block_index, uint64_t end_block_index) {
// this function appears to not exist in the json rpc api, and so is commented
// until such a time as it does.
/*
cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::request req;
cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::response res;
epee::json_rpc::error error_resp;
req.start_height = start_block_index;
req.end_height = end_block_index;
std::string fail_message = "Unsuccessful";
if (m_is_rpc)
{
if (!m_rpc_client->json_rpc_request(req, res, "getblockheadersrange", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_getblockheadersrange(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
for (auto & header : res.headers)
{
std::cout
<< "major version: " << header.major_version << std::endl
<< "minor version: " << header.minor_version << std::endl
<< "height: " << header.height << ", timestamp: " << header.timestamp << ", difficulty: " << header.difficulty << std::endl
<< "block id: " << header.hash << std::endl
<< "previous block id: " << header.prev_hash << std::endl
<< "difficulty: " << header.difficulty << ", nonce " << header.nonce << std::endl;
}
*/
return true;
}
bool t_rpc_command_executor::set_log_level(int8_t level) {
cryptonote::COMMAND_RPC_SET_LOG_LEVEL::request req;
cryptonote::COMMAND_RPC_SET_LOG_LEVEL::response res;
req.level = level;
std::string fail_message = "Unsuccessful";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/set_log_level", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_set_log_level(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
tools::success_msg_writer() << "Log level is now " << std::to_string(level);
return true;
}
bool t_rpc_command_executor::print_height() {
cryptonote::COMMAND_RPC_GET_HEIGHT::request req;
cryptonote::COMMAND_RPC_GET_HEIGHT::response res;
std::string fail_message = "Unsuccessful";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/getheight", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_get_height(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
tools::success_msg_writer() << boost::lexical_cast<std::string>(res.height);
return true;
}
bool t_rpc_command_executor::print_block_by_hash(crypto::hash block_hash) {
cryptonote::COMMAND_RPC_GET_BLOCK::request req;
cryptonote::COMMAND_RPC_GET_BLOCK::response res;
epee::json_rpc::error error_resp;
req.hash = epee::string_tools::pod_to_hex(block_hash);
std::string fail_message = "Unsuccessful";
if (m_is_rpc)
{
if (!m_rpc_client->json_rpc_request(req, res, "getblock", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_get_block(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
print_block_header(res.block_header);
tools::success_msg_writer() << res.json << ENDL;
return true;
}
bool t_rpc_command_executor::print_block_by_height(uint64_t height) {
cryptonote::COMMAND_RPC_GET_BLOCK::request req;
cryptonote::COMMAND_RPC_GET_BLOCK::response res;
epee::json_rpc::error error_resp;
req.height = height;
std::string fail_message = "Unsuccessful";
if (m_is_rpc)
{
if (!m_rpc_client->json_rpc_request(req, res, "getblock", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_get_block(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
print_block_header(res.block_header);
tools::success_msg_writer() << res.json << ENDL;
return true;
}
bool t_rpc_command_executor::print_transaction(crypto::hash transaction_hash) {
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req;
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res;
std::string fail_message = "Problem fetching transaction";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/gettransactions", fail_message.c_str()))
{
return true;
}
}
else
{
req.txs_hashes.push_back(epee::string_tools::pod_to_hex(transaction_hash));
if (!m_rpc_server->on_get_transactions(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
if (1 == res.txs_as_hex.size())
{
// first as hex
tools::success_msg_writer() << res.txs_as_hex.front();
// then as json
crypto::hash tx_hash, tx_prefix_hash;
cryptonote::transaction tx;
cryptonote::blobdata blob;
if (!string_tools::parse_hexstr_to_binbuff(res.txs_as_hex.front(), blob))
{
tools::fail_msg_writer() << "Failed to parse tx";
}
else if (!cryptonote::parse_and_validate_tx_from_blob(blob, tx, tx_hash, tx_prefix_hash))
{
tools::fail_msg_writer() << "Failed to parse tx blob";
}
else
{
tools::success_msg_writer() << cryptonote::obj_to_json_str(tx) << std::endl;
}
}
else
{
tools::fail_msg_writer() << "transaction wasn't found: " << transaction_hash << std::endl;
}
return true;
}
bool t_rpc_command_executor::is_key_image_spent(const crypto::key_image &ki) {
cryptonote::COMMAND_RPC_IS_KEY_IMAGE_SPENT::request req;
cryptonote::COMMAND_RPC_IS_KEY_IMAGE_SPENT::response res;
std::string fail_message = "Problem checking key image";
req.key_images.push_back(epee::string_tools::pod_to_hex(ki));
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/is_key_image_spent", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_is_key_image_spent(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
if (1 == res.spent_status.size())
{
// first as hex
tools::success_msg_writer() << ki << ": " << (res.spent_status.front() ? "spent" : "unspent");
}
else
{
tools::fail_msg_writer() << "key image status could not be determined" << std::endl;
}
return true;
}
bool t_rpc_command_executor::print_transaction_pool_long() {
cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request req;
cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response res;
std::string fail_message = "Problem fetching transaction pool";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/get_transaction_pool", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_get_transaction_pool(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
if (res.transactions.empty() && res.spent_key_images.empty())
{
tools::msg_writer() << "Pool is empty" << std::endl;
}
if (! res.transactions.empty())
{
tools::msg_writer() << "Transactions: ";
for (auto & tx_info : res.transactions)
{
tools::msg_writer() << "id: " << tx_info.id_hash << std::endl
<< tx_info.tx_json << std::endl
<< "blob_size: " << tx_info.blob_size << std::endl
<< "fee: " << cryptonote::print_money(tx_info.fee) << std::endl
<< "receive_time: " << tx_info.receive_time << std::endl
<< "kept_by_block: " << (tx_info.kept_by_block ? 'T' : 'F') << std::endl
<< "max_used_block_height: " << tx_info.max_used_block_height << std::endl
<< "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl
<< "last_failed_height: " << tx_info.last_failed_height << std::endl
<< "last_failed_id: " << tx_info.last_failed_id_hash << std::endl;
}
if (res.spent_key_images.empty())
{
tools::msg_writer() << "WARNING: Inconsistent pool state - no spent key images";
}
}
if (! res.spent_key_images.empty())
{
tools::msg_writer() << ""; // one newline
tools::msg_writer() << "Spent key images: ";
for (const cryptonote::spent_key_image_info& kinfo : res.spent_key_images)
{
tools::msg_writer() << "key image: " << kinfo.id_hash;
if (kinfo.txs_hashes.size() == 1)
{
tools::msg_writer() << " tx: " << kinfo.txs_hashes[0];
}
else if (kinfo.txs_hashes.size() == 0)
{
tools::msg_writer() << " WARNING: spent key image has no txs associated";
}
else
{
tools::msg_writer() << " NOTE: key image for multiple txs: " << kinfo.txs_hashes.size();
for (const std::string& tx_id : kinfo.txs_hashes)
{
tools::msg_writer() << " tx: " << tx_id;
}
}
}
if (res.transactions.empty())
{
tools::msg_writer() << "WARNING: Inconsistent pool state - no transactions";
}
}
return true;
}
bool t_rpc_command_executor::print_transaction_pool_short() {
cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request req;
cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response res;
std::string fail_message = "Problem fetching transaction pool";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/get_transaction_pool", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_get_transaction_pool(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
if (res.transactions.empty())
{
tools::msg_writer() << "Pool is empty" << std::endl;
}
for (auto & tx_info : res.transactions)
{
tools::msg_writer() << "id: " << tx_info.id_hash << std::endl
<< "blob_size: " << tx_info.blob_size << std::endl
<< "fee: " << cryptonote::print_money(tx_info.fee) << std::endl
<< "receive_time: " << tx_info.receive_time << std::endl
<< "kept_by_block: " << (tx_info.kept_by_block ? 'T' : 'F') << std::endl
<< "max_used_block_height: " << tx_info.max_used_block_height << std::endl
<< "max_used_block_id: " << tx_info.max_used_block_id_hash << std::endl
<< "last_failed_height: " << tx_info.last_failed_height << std::endl
<< "last_failed_id: " << tx_info.last_failed_id_hash << std::endl;
}
return true;
}
bool t_rpc_command_executor::start_mining(cryptonote::account_public_address address, uint64_t num_threads, bool testnet) {
cryptonote::COMMAND_RPC_START_MINING::request req;
cryptonote::COMMAND_RPC_START_MINING::response res;
req.miner_address = cryptonote::get_account_address_as_str(testnet, address);
req.threads_count = num_threads;
std::string fail_message = "Mining did not start";
if (m_is_rpc)
{
if (m_rpc_client->rpc_request(req, res, "/start_mining", fail_message.c_str()))
{
tools::success_msg_writer() << "Mining started";
}
}
else
{
if (!m_rpc_server->on_start_mining(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
return true;
}
bool t_rpc_command_executor::stop_mining() {
cryptonote::COMMAND_RPC_STOP_MINING::request req;
cryptonote::COMMAND_RPC_STOP_MINING::response res;
std::string fail_message = "Mining did not stop";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/stop_mining", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_stop_mining(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
tools::success_msg_writer() << "Mining stopped";
return true;
}
bool t_rpc_command_executor::stop_daemon()
{
cryptonote::COMMAND_RPC_STOP_DAEMON::request req;
cryptonote::COMMAND_RPC_STOP_DAEMON::response res;
//# ifdef WIN32
// // Stop via service API
// // TODO - this is only temporary! Get rid of hard-coded constants!
// bool ok = windows::stop_service("BitMonero Daemon");
// ok = windows::uninstall_service("BitMonero Daemon");
// //bool ok = windows::stop_service(SERVICE_NAME);
// //ok = windows::uninstall_service(SERVICE_NAME);
// if (ok)
// {
// return true;
// }
//# endif
// Stop via RPC
std::string fail_message = "Daemon did not stop";
if (m_is_rpc)
{
if(!m_rpc_client->rpc_request(req, res, "/stop_daemon", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_stop_daemon(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
tools::success_msg_writer() << "Stop signal sent";
return true;
}
bool t_rpc_command_executor::print_status()
{
if (!m_is_rpc)
{
tools::success_msg_writer() << "print_status makes no sense in interactive mode";
return true;
}
bool daemon_is_alive = m_rpc_client->check_connection();
if(daemon_is_alive) {
tools::success_msg_writer() << "bitmonerod is running";
}
else {
tools::fail_msg_writer() << "bitmonerod is NOT running";
}
return true;
}
bool t_rpc_command_executor::get_limit()
{
int limit_down = epee::net_utils::connection_basic::get_rate_down_limit( );
int limit_up = epee::net_utils::connection_basic::get_rate_up_limit( );
std::cout << "limit-down is " << limit_down/1024 << " kB/s" << std::endl;
std::cout << "limit-up is " << limit_up/1024 << " kB/s" << std::endl;
return true;
}
bool t_rpc_command_executor::set_limit(int limit)
{
epee::net_utils::connection_basic::set_rate_down_limit( limit );
epee::net_utils::connection_basic::set_rate_up_limit( limit );
std::cout << "Set limit-down to " << limit/1024 << " kB/s" << std::endl;
std::cout << "Set limit-up to " << limit/1024 << " kB/s" << std::endl;
return true;
}
bool t_rpc_command_executor::get_limit_up()
{
int limit_up = epee::net_utils::connection_basic::get_rate_up_limit( );
std::cout << "limit-up is " << limit_up/1024 << " kB/s" << std::endl;
return true;
}
bool t_rpc_command_executor::set_limit_up(int limit)
{
epee::net_utils::connection_basic::set_rate_up_limit( limit );
std::cout << "Set limit-up to " << limit/1024 << " kB/s" << std::endl;
return true;
}
bool t_rpc_command_executor::get_limit_down()
{
int limit_down = epee::net_utils::connection_basic::get_rate_down_limit( );
std::cout << "limit-down is " << limit_down/1024 << " kB/s" << std::endl;
return true;
}
bool t_rpc_command_executor::set_limit_down(int limit)
{
epee::net_utils::connection_basic::set_rate_down_limit( limit );
std::cout << "Set limit-down to " << limit/1024 << " kB/s" << std::endl;
return true;
}
bool t_rpc_command_executor::fast_exit()
{
cryptonote::COMMAND_RPC_FAST_EXIT::request req;
cryptonote::COMMAND_RPC_FAST_EXIT::response res;
std::string fail_message = "Daemon did not stop";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/fast_exit", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_fast_exit(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
tools::success_msg_writer() << "Daemon stopped";
return true;
}
bool t_rpc_command_executor::out_peers(uint64_t limit)
{
cryptonote::COMMAND_RPC_OUT_PEERS::request req;
cryptonote::COMMAND_RPC_OUT_PEERS::response res;
epee::json_rpc::error error_resp;
req.out_peers = limit;
std::string fail_message = "Unsuccessful";
if (m_is_rpc)
{
if (!m_rpc_client->json_rpc_request(req, res, "out_peers", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_out_peers(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
std::cout << "Max number of out peers set to " << limit << std::endl;
return true;
}
bool t_rpc_command_executor::start_save_graph()
{
cryptonote::COMMAND_RPC_START_SAVE_GRAPH::request req;
cryptonote::COMMAND_RPC_START_SAVE_GRAPH::response res;
std::string fail_message = "Unsuccessful";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/start_save_graph", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_start_save_graph(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
return true;
}
bool t_rpc_command_executor::stop_save_graph()
{
cryptonote::COMMAND_RPC_STOP_SAVE_GRAPH::request req;
cryptonote::COMMAND_RPC_STOP_SAVE_GRAPH::response res;
std::string fail_message = "Unsuccessful";
if (m_is_rpc)
{
if (!m_rpc_client->rpc_request(req, res, "/stop_save_graph", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_stop_save_graph(req, res) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
return true;
}
bool t_rpc_command_executor::hard_fork_info(uint8_t version)
{
cryptonote::COMMAND_RPC_HARD_FORK_INFO::request req;
cryptonote::COMMAND_RPC_HARD_FORK_INFO::response res;
std::string fail_message = "Unsuccessful";
epee::json_rpc::error error_resp;
req.version = version;
if (m_is_rpc)
{
if (!m_rpc_client->json_rpc_request(req, res, "hard_fork_info", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_hard_fork_info(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
version = version > 0 ? version : res.voting;
tools::msg_writer() << "version " << (uint32_t)version << " " << (res.enabled ? "enabled" : "not enabled") <<
", " << res.votes << "/" << res.window << " votes, threshold " << res.threshold;
tools::msg_writer() << "current version " << (uint32_t)res.version << ", voting for version " << (uint32_t)res.voting;
return true;
}
bool t_rpc_command_executor::print_bans()
{
cryptonote::COMMAND_RPC_GETBANS::request req;
cryptonote::COMMAND_RPC_GETBANS::response res;
std::string fail_message = "Unsuccessful";
epee::json_rpc::error error_resp;
if (m_is_rpc)
{
if (!m_rpc_client->json_rpc_request(req, res, "get_bans", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_get_bans(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
time_t now = time(nullptr);
for (auto i = res.bans.begin(); i != res.bans.end(); ++i)
{
time_t seconds = i->seconds - now;
tools::msg_writer() << epee::string_tools::get_ip_string_from_int32(i->ip) << " banned for " << seconds << " seconds";
}
return true;
}
bool t_rpc_command_executor::ban(const std::string &ip, time_t seconds)
{
cryptonote::COMMAND_RPC_SETBANS::request req;
cryptonote::COMMAND_RPC_SETBANS::response res;
std::string fail_message = "Unsuccessful";
epee::json_rpc::error error_resp;
cryptonote::COMMAND_RPC_SETBANS::ban ban;
if (!epee::string_tools::get_ip_int32_from_string(ban.ip, ip))
{
tools::fail_msg_writer() << "Invalid IP";
return true;
}
ban.ban = true;
ban.seconds = seconds;
req.bans.push_back(ban);
if (m_is_rpc)
{
if (!m_rpc_client->json_rpc_request(req, res, "set_bans", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_set_bans(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
return true;
}
bool t_rpc_command_executor::unban(const std::string &ip)
{
cryptonote::COMMAND_RPC_SETBANS::request req;
cryptonote::COMMAND_RPC_SETBANS::response res;
std::string fail_message = "Unsuccessful";
epee::json_rpc::error error_resp;
cryptonote::COMMAND_RPC_SETBANS::ban ban;
if (!epee::string_tools::get_ip_int32_from_string(ban.ip, ip))
{
tools::fail_msg_writer() << "Invalid IP";
return true;
}
ban.ban = false;
ban.seconds = 0;
req.bans.push_back(ban);
if (m_is_rpc)
{
if (!m_rpc_client->json_rpc_request(req, res, "set_bans", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_set_bans(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
return true;
}
bool t_rpc_command_executor::flush_txpool(const std::string &txid)
{
cryptonote::COMMAND_RPC_FLUSH_TRANSACTION_POOL::request req;
cryptonote::COMMAND_RPC_FLUSH_TRANSACTION_POOL::response res;
std::string fail_message = "Unsuccessful";
epee::json_rpc::error error_resp;
if (!txid.empty())
req.txids.push_back(txid);
if (m_is_rpc)
{
if (!m_rpc_client->json_rpc_request(req, res, "flush_txpool", fail_message.c_str()))
{
return true;
}
}
else
{
if (!m_rpc_server->on_flush_txpool(req, res, error_resp))
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
}
return true;
}
}// namespace daemonize
|