forked from arqma/blockchain-explorer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
834 lines (634 loc) · 27.9 KB
/
main.cpp
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
#define CROW_ENABLE_SSL
#include "src/page.h"
#include "ext/crow/crow.h"
#include "src/CmdLineOptions.h"
#include "src/MicroCore.h"
#include <fstream>
#include <regex>
using boost::filesystem::path;
using xmreg::remove_bad_chars;
using namespace std;
namespace myxmr
{
struct jsonresponse: crow::response
{
jsonresponse(const nlohmann::json& _body)
: crow::response {_body.dump()}
{
add_header("Access-Control-Allow-Origin", "*");
add_header("Access-Control-Allow-Headers", "Content-Type");
add_header("Content-Type", "application/json");
}
};
}
int
main(int ac, const char* av[])
{
// get command line options
xmreg::CmdLineOptions opts {ac, av};
auto help_opt = opts.get_option<bool>("help");
// if help was chosen, display help text and finish
if (*help_opt)
{
return EXIT_SUCCESS;
}
auto port_opt = opts.get_option<string>("port");
auto bindaddr_opt = opts.get_option<string>("bindaddr");
auto bc_path_opt = opts.get_option<string>("bc-path");
auto deamon_url_opt = opts.get_option<string>("deamon-url");
auto ssl_crt_file_opt = opts.get_option<string>("ssl-crt-file");
auto ssl_key_file_opt = opts.get_option<string>("ssl-key-file");
auto no_blocks_on_index_opt = opts.get_option<string>("no-blocks-on-index");
auto testnet_url = opts.get_option<string>("testnet-url");
auto stagenet_url = opts.get_option<string>("stagenet-url");
auto mainnet_url = opts.get_option<string>("mainnet-url");
auto mempool_info_timeout_opt = opts.get_option<string>("mempool-info-timeout");
auto mempool_refresh_time_opt = opts.get_option<string>("mempool-refresh-time");
auto testnet_opt = opts.get_option<bool>("testnet");
auto stagenet_opt = opts.get_option<bool>("stagenet");
auto enable_key_image_checker_opt = opts.get_option<bool>("enable-key-image-checker");
auto enable_output_key_checker_opt = opts.get_option<bool>("enable-output-key-checker");
auto enable_autorefresh_option_opt = opts.get_option<bool>("enable-autorefresh-option");
auto enable_pusher_opt = opts.get_option<bool>("enable-pusher");
auto enable_js_opt = opts.get_option<bool>("enable-js");
auto enable_mixin_details_opt = opts.get_option<bool>("enable-mixin-details");
auto enable_json_api_opt = opts.get_option<bool>("enable-json-api");
auto enable_as_hex_opt = opts.get_option<bool>("enable-as-hex");
auto enable_tx_cache_opt = opts.get_option<bool>("enable-tx-cache");
auto concurrency_opt = opts.get_option<size_t>("concurrency");
auto enable_block_cache_opt = opts.get_option<bool>("enable-block-cache");
auto show_cache_times_opt = opts.get_option<bool>("show-cache-times");
auto enable_emission_monitor_opt = opts.get_option<bool>("enable-emission-monitor");
bool testnet {*testnet_opt};
bool stagenet {*stagenet_opt};
if (testnet && stagenet)
{
cerr << "testnet and stagenet cannot be specified at the same time!" << endl;
return EXIT_FAILURE;
}
const cryptonote::network_type nettype = testnet ?
cryptonote::network_type::TESTNET : stagenet ?
cryptonote::network_type::STAGENET : cryptonote::network_type::MAINNET;
bool enable_pusher {*enable_pusher_opt};
bool enable_js {*enable_js_opt};
bool enable_key_image_checker {*enable_key_image_checker_opt};
bool enable_autorefresh_option {*enable_autorefresh_option_opt};
bool enable_output_key_checker {*enable_output_key_checker_opt};
bool enable_mixin_details {*enable_mixin_details_opt};
bool enable_json_api {*enable_json_api_opt};
bool enable_as_hex {*enable_as_hex_opt};
bool enable_tx_cache {*enable_tx_cache_opt};
bool enable_block_cache {*enable_block_cache_opt};
bool enable_emission_monitor {*enable_emission_monitor_opt};
bool show_cache_times {*show_cache_times_opt};
// set morelolution log output level
uint32_t log_level = 0;
mlog_configure("", true);
(void) log_level;
//cast port number in string to uint
uint16_t app_port = boost::lexical_cast<uint16_t>(*port_opt);
string bindaddr = *bindaddr_opt;
// cast no_blocks_on_index_opt to uint
uint64_t no_blocks_on_index = boost::lexical_cast<uint64_t>(*no_blocks_on_index_opt);
bool use_ssl {false};
string ssl_crt_file;
string ssl_key_file;
// check if ssl enabled and files exist
if (ssl_crt_file_opt and ssl_key_file_opt)
{
if (!boost::filesystem::exists(boost::filesystem::path(*ssl_crt_file_opt)))
{
cerr << "ssl_crt_file path: " << *ssl_crt_file_opt
<< "does not exist!" << endl;
return EXIT_FAILURE;
}
if (!boost::filesystem::exists(boost::filesystem::path(*ssl_key_file_opt)))
{
cerr << "ssl_key_file path: " << *ssl_key_file_opt
<< "does not exist!" << endl;
return EXIT_FAILURE;
}
ssl_crt_file = *ssl_crt_file_opt;
ssl_key_file = *ssl_key_file_opt;
use_ssl = true;
}
// get blockchain path
path blockchain_path;
if (!xmreg::get_blockchain_path(bc_path_opt, blockchain_path, nettype))
{
cerr << "Error getting blockchain path." << endl;
return EXIT_FAILURE;
}
cout << blockchain_path << endl;
// create instance of our MicroCore
// and make pointer to the Blockchain
xmreg::MicroCore mcore;
cryptonote::Blockchain* core_storage;
// initialize mcore and core_storage
if (!xmreg::init_blockchain(blockchain_path.string(), mcore, core_storage, nettype))
{
cerr << "Error accessing blockchain." << endl;
return EXIT_FAILURE;
}
string deamon_url {*deamon_url_opt};
if (testnet && deamon_url == "http://127.0.0.1:38422")
deamon_url = "http://127.0.0.1:47422";
if (stagenet && deamon_url == "http://127.0.0.1:38422")
deamon_url = "http://127.0.0.1:46422";
uint64_t mempool_info_timeout {5000};
try
{
mempool_info_timeout = boost::lexical_cast<uint64_t>(*mempool_info_timeout_opt);
}
catch (boost::bad_lexical_cast &e)
{
cout << "Cant cast " << (*mempool_info_timeout_opt) <<" into numbers. Using default values.\n";
}
uint64_t mempool_refresh_time {5};
if (enable_emission_monitor == true)
{
// This starts new thread, which aim is
// to calculate, store and monitor
// current total morelolution emission amount.
// This thread stores the current emission
// which it has caluclated in
// <blockchain_path>/emission_amount.txt file,
// e.g., ~/.mrllution/lmdb/emission_amount.txt.
// So instead of calcualting the emission
// from scrach whenever the explorer is started,
// the thread is initalized with the values
// found in emission_amount.txt file.
xmreg::CurrentBlockchainStatus::blockchain_path = blockchain_path;
xmreg::CurrentBlockchainStatus::nettype = nettype;
xmreg::CurrentBlockchainStatus::deamon_url = deamon_url;
xmreg::CurrentBlockchainStatus::set_blockchain_variables(&mcore, core_storage);
// launch the status monitoring thread so that it keeps track of blockchain
// info, e.g., current height. Information from this thread is used
// by tx searching threads that are launched for each user independently,
// when they log back or create new account.
xmreg::CurrentBlockchainStatus::start_monitor_blockchain_thread();
}
xmreg::MempoolStatus::blockchain_path = blockchain_path;
xmreg::MempoolStatus::nettype = nettype;
xmreg::MempoolStatus::deamon_url = deamon_url;
xmreg::MempoolStatus::set_blockchain_variables(&mcore, core_storage);
xmreg::MempoolStatus::network_info initial_info;
strcpy(initial_info.block_size_limit_str, "0.0");
strcpy(initial_info.block_size_median_str, "0.0");
xmreg::MempoolStatus::current_network_info = initial_info;
try
{
mempool_refresh_time = boost::lexical_cast<uint64_t>(*mempool_refresh_time_opt);
}
catch (boost::bad_lexical_cast &e)
{
cout << "Cant cast " << (*mempool_refresh_time_opt)
<< " into number. Using default value."
<< endl;
}
// launch the status monitoring thread so that it keeps track of blockchain
// info, e.g., current height. Information from this thread is used
// by tx searching threads that are launched for each user independently,
// when they log back or create new account.
xmreg::MempoolStatus::mempool_refresh_time = mempool_refresh_time;
xmreg::MempoolStatus::start_mempool_status_thread();
// create instance of page class which
// contains logic for the website
xmreg::page moreloblocks(&mcore,
core_storage,
deamon_url,
nettype,
enable_pusher,
enable_js,
enable_as_hex,
enable_key_image_checker,
enable_output_key_checker,
enable_autorefresh_option,
enable_mixin_details,
enable_tx_cache,
enable_block_cache,
show_cache_times,
no_blocks_on_index,
mempool_info_timeout,
*testnet_url,
*stagenet_url,
*mainnet_url);
// crow instance
crow::SimpleApp app;
// get domian url based on the request
auto get_domain = [&use_ssl](crow::request const& req)
{
auto frontend_host = req.get_header_value("X-Forwarded-Host");
auto frontend_ssl = req.get_header_value("X-Forwarded-Proto");
return ((use_ssl || frontend_ssl == "https") ? "https://" : "http://")
+ (frontend_host.empty() ? req.get_header_value("Host") : frontend_host);
};
CROW_ROUTE(app, "/")
([&](const crow::request& req) {
return crow::response(moreloblocks.index2());
});
CROW_ROUTE(app, "/page/<uint>")
([&](size_t page_no) {
return moreloblocks.index2(page_no);
});
CROW_ROUTE(app, "/block/<uint>")
([&](const crow::request& req, size_t block_height) {
return crow::response(moreloblocks.show_block(block_height));
});
CROW_ROUTE(app, "/block/<string>")
([&](const crow::request& req, string block_hash) {
return crow::response(moreloblocks.show_block(remove_bad_chars(block_hash)));
});
CROW_ROUTE(app, "/tx/<string>")
([&](string tx_hash) {
return crow::response(moreloblocks.show_tx(remove_bad_chars(tx_hash)));
});
if (enable_as_hex)
{
CROW_ROUTE(app, "/txhex/<string>")
([&](string tx_hash) {
return crow::response(moreloblocks.show_tx_hex(remove_bad_chars(tx_hash)));
});
CROW_ROUTE(app, "/ringmembershex/<string>")
([&](string tx_hash) {
return crow::response(moreloblocks.show_ringmembers_hex(remove_bad_chars(tx_hash)));
});
CROW_ROUTE(app, "/blockhex/<uint>")
([&](size_t block_height) {
return crow::response(moreloblocks.show_block_hex(block_height, false));
});
CROW_ROUTE(app, "/blockhexcomplete/<uint>")
([&](size_t block_height) {
return crow::response(moreloblocks.show_block_hex(block_height, true));
});
CROW_ROUTE(app, "/ringmemberstxhex/<string>")
([&](string tx_hash) {
return myxmr::jsonresponse {moreloblocks.show_ringmemberstx_jsonhex(remove_bad_chars(tx_hash))};
});
}
CROW_ROUTE(app, "/tx/<string>/<uint>")
([&](string tx_hash, uint16_t with_ring_signatures)
{
return moreloblocks.show_tx(remove_bad_chars(tx_hash), with_ring_signatures);
});
CROW_ROUTE(app, "/myoutputs").methods("POST"_method)
([&](const crow::request& req)
{
map<std::string, std::string> post_body
= xmreg::parse_crow_post_data(req.body);
if (post_body.count("morelo_address") == 0
|| post_body.count("viewkey") == 0
|| post_body.count("tx_hash") == 0)
{
return string("morelo address, viewkey or tx hash not provided");
}
string tx_hash = remove_bad_chars(post_body["tx_hash"]);
string morelo_address = remove_bad_chars(post_body["morelo_address"]);
string viewkey = remove_bad_chars(post_body["viewkey"]);
// this will be only not empty when checking raw tx data
// using tx pusher
string raw_tx_data = remove_bad_chars(post_body["raw_tx_data"]);
string domain = get_domain(req);
return moreloblocks.show_my_outputs(tx_hash, morelo_address, viewkey, raw_tx_data, domain);
});
CROW_ROUTE(app, "/myoutputs/<string>/<string>/<string>")
([&](const crow::request& req, string tx_hash,
string morelo_address, string viewkey)
{
string domain = get_domain(req);
return moreloblocks.show_my_outputs(remove_bad_chars(tx_hash), remove_bad_chars(morelo_address), remove_bad_chars(viewkey), string {}, domain);
});
CROW_ROUTE(app, "/prove").methods("POST"_method)
([&](const crow::request& req){
map<std::string, std::string> post_body
= xmreg::parse_crow_post_data(req.body);
if (post_body.count("moreloaddress") == 0
|| post_body.count("txprvkey") == 0
|| post_body.count("txhash") == 0)
{
return string("morelo address, tx private key or "
"tx hash not provided");
}
string tx_hash = remove_bad_chars(post_body["txhash"]);
string tx_prv_key = remove_bad_chars(post_body["txprvkey"]);
string morelo_address = remove_bad_chars(post_body["moreloaddress"]);
// this will be only not empty when checking raw tx data
// using tx pusher
string raw_tx_data = remove_bad_chars(post_body["raw_tx_data"]);
string domain = get_domain(req);
return moreloblocks.show_prove(tx_hash, morelo_address, tx_prv_key, raw_tx_data, domain);
});
CROW_ROUTE(app, "/prove/<string>/<string>/<string>")
([&](const crow::request& req, string tx_hash,
string morelo_address, string tx_prv_key) {
string domain = get_domain(req);
return moreloblocks.show_prove(remove_bad_chars(tx_hash), remove_bad_chars(morelo_address), remove_bad_chars(tx_prv_key), string {}, domain);
});
if (enable_pusher)
{
CROW_ROUTE(app, "/rawtx")
([&](const crow::request& req) {
return moreloblocks.show_rawtx();
});
CROW_ROUTE(app, "/checkandpush").methods("POST"_method)
([&](const crow::request& req) {
map<std::string, std::string> post_body
= xmreg::parse_crow_post_data(req.body);
if (post_body.count("rawtxdata") == 0 || post_body.count("action") == 0)
{
return string("Raw tx data or action not provided");
}
string raw_tx_data = remove_bad_chars(post_body["rawtxdata"]);
string action = remove_bad_chars(post_body["action"]);
if (action == "check")
return moreloblocks.show_checkrawtx(raw_tx_data, action);
else if (action == "push")
return moreloblocks.show_pushrawtx(raw_tx_data, action);
return string("Provided action is neither check nor push");
});
}
if (enable_key_image_checker)
{
CROW_ROUTE(app, "/rawkeyimgs")
([&](const crow::request& req) {
return moreloblocks.show_rawkeyimgs();
});
CROW_ROUTE(app, "/checkrawkeyimgs").methods("POST"_method)
([&](const crow::request& req) {
map<std::string, std::string> post_body
= xmreg::parse_crow_post_data(req.body);
if (post_body.count("rawkeyimgsdata") == 0)
{
return string("Raw key images data not given");
}
if (post_body.count("viewkey") == 0)
{
return string("Viewkey not provided. Cant decrypt key image file without it");
}
string raw_data = remove_bad_chars(post_body["rawkeyimgsdata"]);
string viewkey = remove_bad_chars(post_body["viewkey"]);
return moreloblocks.show_checkrawkeyimgs(raw_data, viewkey);
});
}
if (enable_output_key_checker)
{
CROW_ROUTE(app, "/rawoutputkeys")
([&](const crow::request& req) {
return moreloblocks.show_rawoutputkeys();
});
CROW_ROUTE(app, "/checkrawoutputkeys").methods("POST"_method)
([&](const crow::request& req) {
map<std::string, std::string> post_body
= xmreg::parse_crow_post_data(req.body);
if (post_body.count("rawoutputkeysdata") == 0)
{
return string("Raw output keys data not given");
}
if (post_body.count("viewkey") == 0)
{
return string("Viewkey not provided. Cant decrypt "
"key image file without it");
}
string raw_data = remove_bad_chars(post_body["rawoutputkeysdata"]);
string viewkey = remove_bad_chars(post_body["viewkey"]);
return moreloblocks.show_checkcheckrawoutput(raw_data, viewkey);
});
}
CROW_ROUTE(app, "/search").methods("GET"_method)
([&](const crow::request& req) {
return moreloblocks.search(remove_bad_chars(string(req.url_params.get("value"))));
});
CROW_ROUTE(app, "/mempool")
([&](const crow::request& req) {
return moreloblocks.mempool(true);
});
// alias to "/mempool"
CROW_ROUTE(app, "/txpool")
([&](const crow::request& req) {
return moreloblocks.mempool(true);
});
// CROW_ROUTE(app, "/altblocks")
// ([&](const crow::request& req) {
// return moreloblocks.altblocks();
// });
CROW_ROUTE(app, "/robots.txt")
([&]() {
string text = "User-agent: *\n"
"Disallow: ";
return text;
});
CROW_ROUTE(app, "/blockchain.js")([&]() { return moreloblocks.get_blockchain_js(); });
CROW_ROUTE(app, "/css/style.css")([&]() { return moreloblocks.get_css(); });
if (enable_js)
{
cout << "Enable JavaScript checking of outputs and proving txs\n";
CROW_ROUTE(app, "/js/jquery.min.js")
([&](const crow::request& req) {
return moreloblocks.get_js_file("jquery.min.js");
});
CROW_ROUTE(app, "/js/crc32.js")
([&](const crow::request& req) {
return moreloblocks.get_js_file("crc32.js");
});
CROW_ROUTE(app, "/js/biginteger.js")
([&](const crow::request& req) {
return moreloblocks.get_js_file("biginteger.js");
});
CROW_ROUTE(app, "/js/crypto.js")
([&](const crow::request& req) {
return moreloblocks.get_js_file("crypto.js");
});
CROW_ROUTE(app, "/js/config.js")
([&](const crow::request& req) {
return moreloblocks.get_js_file("config.js");
});
CROW_ROUTE(app, "/js/nacl-fast-cn.js")
([&](const crow::request& req) {
return moreloblocks.get_js_file("nacl-fast-cn.js");
});
CROW_ROUTE(app, "/js/base58.js")
([&](const crow::request& req) {
return moreloblocks.get_js_file("base58.js");
});
CROW_ROUTE(app, "/js/cn_util.js")
([&](const crow::request& req) {
return moreloblocks.get_js_file("cn_util.js");
});
CROW_ROUTE(app, "/js/sha3.js")
([&](const crow::request& req) {
return moreloblocks.get_js_file("sha3.js");
});
CROW_ROUTE(app, "/js/all_in_one.js")
([&](const crow::request& req) {
// /js/all_in_one.js file does not exist. it is generated on the fly
// from the above real files.
return moreloblocks.get_js_file("all_in_one.js");
});
} // if (enable_js)
if (enable_json_api)
{
cout << "Enable JSON API\n";
CROW_ROUTE(app, "/api")
([&](const crow::request& req) {
return crow::response(moreloblocks.api());
});
CROW_ROUTE(app, "/api/transaction/<string>")
([&](const crow::request &req, string tx_hash) {
myxmr::jsonresponse r{moreloblocks.json_transaction(remove_bad_chars(tx_hash))};
return r;
});
CROW_ROUTE(app, "/api/rawtransaction/<string>")
([&](const crow::request &req, string tx_hash) {
myxmr::jsonresponse r{moreloblocks.json_rawtransaction(remove_bad_chars(tx_hash))};
return r;
});
CROW_ROUTE(app, "/api/block/<string>")
([&](const crow::request &req, string block_no_or_hash) {
myxmr::jsonresponse r{moreloblocks.json_block(remove_bad_chars(block_no_or_hash))};
return r;
});
CROW_ROUTE(app, "/api/rawblock/<string>")
([&](const crow::request &req, string block_no_or_hash) {
myxmr::jsonresponse r{moreloblocks.json_rawblock(remove_bad_chars(block_no_or_hash))};
return r;
});
CROW_ROUTE(app, "/api/transactions").methods("GET"_method)
([&](const crow::request &req) {
string page = regex_search(req.raw_url, regex {"page=\\d+"}) ?
req.url_params.get("page") : "0";
string limit = regex_search(req.raw_url, regex {"limit=\\d+"}) ?
req.url_params.get("limit") : "25";
myxmr::jsonresponse r{moreloblocks.json_transactions(
remove_bad_chars(page), remove_bad_chars(limit))};
return r;
});
CROW_ROUTE(app, "/api/mempool").methods("GET"_method)
([&](const crow::request &req) {
string page = regex_search(req.raw_url, regex {"page=\\d+"}) ?
req.url_params.get("page") : "0";
// default value for limit is some large number, so that
// a call to api/mempool without any limit return all
// mempool txs
string limit = regex_search(req.raw_url, regex {"limit=\\d+"}) ?
req.url_params.get("limit") : "100000000";
myxmr::jsonresponse r{moreloblocks.json_mempool(
remove_bad_chars(page), remove_bad_chars(limit))};
return r;
});
CROW_ROUTE(app, "/api/search/<string>")
([&](const crow::request &req, string search_value) {
myxmr::jsonresponse r{moreloblocks.json_search(remove_bad_chars(search_value))};
return r;
});
CROW_ROUTE(app, "/api/networkinfo")
([&](const crow::request &req) {
myxmr::jsonresponse r{moreloblocks.json_networkinfo()};
return r;
});
CROW_ROUTE(app, "/api/emission")
([&](const crow::request &req) {
myxmr::jsonresponse r{moreloblocks.json_emission()};
return r;
});
CROW_ROUTE(app, "/api/outputs").methods("GET"_method)
([&](const crow::request &req) {
string tx_hash = regex_search(req.raw_url, regex {"txhash=\\w+"}) ?
req.url_params.get("txhash") : "";
string address = regex_search(req.raw_url, regex {"address=\\w+"}) ?
req.url_params.get("address") : "";
string viewkey = regex_search(req.raw_url, regex {"viewkey=\\w+"}) ?
req.url_params.get("viewkey") : "";
bool tx_prove{false};
try
{
tx_prove = regex_search(req.raw_url, regex {"txprove=[01]"}) ?
boost::lexical_cast<bool>(req.url_params.get("txprove")) :
false;
}
catch (const boost::bad_lexical_cast &e)
{
cerr << "Cant parse tx_prove as bool. Using default value" << endl;
}
myxmr::jsonresponse r{moreloblocks.json_outputs(
remove_bad_chars(tx_hash),
remove_bad_chars(address),
remove_bad_chars(viewkey),
tx_prove)};
return r;
});
CROW_ROUTE(app, "/api/outputsblocks").methods("GET"_method)
([&](const crow::request &req) {
string limit = regex_search(req.raw_url, regex {"limit=\\d+"}) ?
req.url_params.get("limit") : "3";
string address = regex_search(req.raw_url, regex {"address=\\w+"}) ?
req.url_params.get("address") : "";
string viewkey = regex_search(req.raw_url, regex {"viewkey=\\w+"}) ?
req.url_params.get("viewkey") : "";
bool in_mempool_aswell {false};
try
{
in_mempool_aswell = regex_search(req.raw_url, regex {"mempool=[01]"}) ?
boost::lexical_cast<bool>(req.url_params.get("mempool")) :
false;
}
catch (const boost::bad_lexical_cast &e)
{
cerr << "Cant parse tx_prove as bool. Using default value" << endl;
}
myxmr::jsonresponse r{moreloblocks.json_outputsblocks(
remove_bad_chars(limit),
remove_bad_chars(address),
remove_bad_chars(viewkey),
in_mempool_aswell)};
return r;
});
CROW_ROUTE(app, "/api/version")
([&](const crow::request &req) {
myxmr::jsonresponse r{moreloblocks.json_version()};
return r;
});
} // if (enable_json_api)
if (enable_autorefresh_option)
{
CROW_ROUTE(app, "/autorefresh")
([&]() {
uint64_t page_no {0};
bool refresh_page {true};
return moreloblocks.index2(page_no, refresh_page);
});
}
// run the crow http server
if (use_ssl)
{
cout << "Staring in ssl mode" << endl;
app.bindaddr(bindaddr).port(app_port).ssl_file(ssl_crt_file, ssl_key_file)
.multithreaded().run();
}
else
{
cout << "Staring in non-ssl mode" << endl;
if (*concurrency_opt == 0)
{
app.bindaddr(bindaddr).port(app_port).multithreaded().run();
}
else
{
app.bindaddr(bindaddr).port(app_port).concurrency(*concurrency_opt).run();
}
}
if (enable_emission_monitor == true)
{
// finish Emission monitoring thread in a cotrolled manner.
cout << "Waiting for emission monitoring thread to finish." << endl;
xmreg::CurrentBlockchainStatus::m_thread.interrupt();
xmreg::CurrentBlockchainStatus::m_thread.join();
cout << "Emission monitoring thread finished." << endl;
}
// finish mempool thread
cout << "Waiting for mempool monitoring thread to finish." << endl;
xmreg::MempoolStatus::m_thread.interrupt();
xmreg::MempoolStatus::m_thread.join();
cout << "Mempool monitoring thread finished." << endl;
cout << "The explorer is terminating." << endl;
return EXIT_SUCCESS;
}