Repository navigation
Expand file tree
/
Copy pathCurrentBlockchainStatus.cpp
More file actions
executable file
·1149 lines (907 loc) · 29.5 KB
/
Copy pathCurrentBlockchainStatus.cpp
File metadata and controls
executable file
·1149 lines (907 loc) · 29.5 KB
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
//
// Created by mwo on 7/01/17.
//
#include "CurrentBlockchainStatus.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "openloki"
namespace xmreg
{
CurrentBlockchainStatus::CurrentBlockchainStatus(
BlockchainSetup _bc_setup,
std::unique_ptr<MicroCore> _mcore,
std::unique_ptr<RPCCalls> _rpc)
: bc_setup {_bc_setup},
mcore {std::move(_mcore)},
rpc {std::move(_rpc)}
{
}
void
CurrentBlockchainStatus::monitor_blockchain()
{
TxSearch::set_search_thread_life(
bc_setup.search_thread_life_in_seconds);
stop_blockchain_monitor_loop = false;
if (!is_running)
{
is_running = true;
while (true)
{
if (stop_blockchain_monitor_loop)
break;
update_current_blockchain_height();
read_mempool();
OMINFO << "Current blockchain height: " << current_height
<< ", no of mempool txs: " << mempool_txs.size();
clean_search_thread_map();
std::this_thread::sleep_for(
std::chrono::seconds(
bc_setup.refresh_block_status_every_seconds));
}
is_running = false;
}
}
uint64_t
CurrentBlockchainStatus::get_current_blockchain_height()
{
return current_height;
}
void
CurrentBlockchainStatus::update_current_blockchain_height()
{
current_height = mcore->get_current_blockchain_height() - 1;
}
bool
CurrentBlockchainStatus::init_loki_blockchain()
{
// initialize the core using the blockchain path
return mcore->init(bc_setup.blockchain_path, bc_setup.net_type);}
// taken from
// bool wallet2::is_transfer_unlocked(uint64_t unlock_time,
//uint64_t block_height) const
bool
CurrentBlockchainStatus::is_tx_unlocked(
uint64_t unlock_time,
uint64_t block_height,
TxUnlockChecker const& tx_unlock_checker)
{
return tx_unlock_checker.is_unlocked(bc_setup.net_type,
current_height,
unlock_time,
block_height);
}
bool
CurrentBlockchainStatus::get_block(uint64_t height, block& blk)
{
return mcore->get_block_from_height(height, blk);
}
vector<block>
CurrentBlockchainStatus::get_blocks_range(
uint64_t const& h1, uint64_t const& h2)
{
try
{
return mcore->get_blocks_range(h1, h2);
}
catch (std::exception& e)
{
cerr << e.what() << endl;
}
return {};
}
bool
CurrentBlockchainStatus::get_block_txs(
const block &blk,
vector<transaction>& blk_txs,
vector<crypto::hash>& missed_txs)
{
// get all transactions in the block found
// initialize the first list with transaction for solving
// the block i.e. coinbase tx.
blk_txs.push_back(blk.miner_tx);
if (!mcore->get_transactions(blk.tx_hashes, blk_txs, missed_txs))
{
OMERROR << "Cant get transactions in block: "
<< get_block_hash(blk);
return false;
}
return true;
}
bool
CurrentBlockchainStatus::get_txs(
vector<crypto::hash> const& txs_to_get,
vector<transaction>& txs,
vector<crypto::hash>& missed_txs)
{
if (!mcore->get_transactions(txs_to_get, txs, missed_txs))
{
OMERROR << "CurrentBlockchainStatus::get_txs: "
"cant get transactions!";
return false;
}
return true;
}
bool
CurrentBlockchainStatus::tx_exist(const crypto::hash& tx_hash)
{
return mcore->have_tx(tx_hash);
}
bool
CurrentBlockchainStatus::tx_exist(
const crypto::hash& tx_hash,
uint64_t& tx_index)
{
return mcore->tx_exists(tx_hash, tx_index);
}
bool
CurrentBlockchainStatus::tx_exist(
const string& tx_hash_str,
uint64_t& tx_index)
{
crypto::hash tx_hash;
if (hex_to_pod(tx_hash_str, tx_hash))
{
return tx_exist(tx_hash, tx_index);
}
return false;
}
bool
CurrentBlockchainStatus::get_tx_with_output(
uint64_t output_idx, uint64_t amount,
transaction& tx, uint64_t& output_idx_in_tx)
{
tx_out_index tx_out_idx;
try
{
// get pair pair<crypto::hash, uint64_t> where first is tx hash
// and second is local index of the output i in that tx
tx_out_idx = mcore->get_output_tx_and_index(amount, output_idx);
}
catch (const std::exception& e)
{
string out_msg = fmt::format(
"Output with amount {:d} and index {:d} does not exist!",
amount, output_idx
);
OMERROR << out_msg << ' ' << e.what();
return false;
}
output_idx_in_tx = tx_out_idx.second;
if (!mcore->get_tx(tx_out_idx.first, tx))
{
OMERROR << "Cant get tx: " << tx_out_idx.first;
return false;
}
return true;
}
bool
CurrentBlockchainStatus::get_output_keys(
const uint64_t& amount,
const vector<uint64_t>& absolute_offsets,
vector<cryptonote::output_data_t>& outputs)
{
try
{
mcore->get_output_key(amount, absolute_offsets, outputs);
return true;
}
catch (const std::exception& e)
{
OMERROR << "get_output_keys: " << e.what();
}
return false;
}
string
CurrentBlockchainStatus::get_account_integrated_address_as_str(
crypto::hash8 const& payment_id8)
{
return cryptonote::get_account_integrated_address_as_str(
bc_setup.net_type,
bc_setup.import_payment_address.address, payment_id8);
}
string
CurrentBlockchainStatus::get_account_integrated_address_as_str(
string const& payment_id8_str)
{
crypto::hash8 payment_id8;
if (!hex_to_pod(payment_id8_str, payment_id8))
return string {};
return get_account_integrated_address_as_str(payment_id8);
}
bool
CurrentBlockchainStatus::get_amount_specific_indices(
const crypto::hash& tx_hash,
vector<uint64_t>& out_indices)
{
try
{
// this index is lmdb index of a tx, not tx hash
uint64_t tx_index;
if (mcore->tx_exists(tx_hash, tx_index))
{
out_indices = mcore->get_tx_amount_output_indices(tx_index);
return true;
}
}
catch(const exception& e)
{
cerr << e.what() << endl;
}
return false;
}
unique_ptr<RandomOutputs>
CurrentBlockchainStatus::create_random_outputs_object(
vector<uint64_t> const& amounts,
uint64_t outs_count) const
{
return make_unique<RandomOutputs>(*mcore, amounts, outs_count);
}
bool
CurrentBlockchainStatus::get_random_outputs(
vector<uint64_t> const& amounts,
uint64_t outs_count,
RandomOutputs::outs_for_amount_v& found_outputs)
{
unique_ptr<RandomOutputs> ro
= create_random_outputs_object(amounts, outs_count);
if (!ro->find_random_outputs())
{
OMERROR << "!ro.find_random_outputs()";
return false;
}
found_outputs = ro->get_found_outputs();
return true;
}
bool
CurrentBlockchainStatus::get_output(
const uint64_t amount,
const uint64_t global_output_index,
COMMAND_RPC_GET_OUTPUTS_BIN::outkey& output_info)
{
COMMAND_RPC_GET_OUTPUTS_BIN::request req;
COMMAND_RPC_GET_OUTPUTS_BIN::response res;
req.outputs.push_back(get_outputs_out {amount, global_output_index});
if (!mcore->get_outs(req, res))
{
OMERROR << "mcore->get_outs(req, res) failed";
return false;
}
output_info = res.outs.at(0);
return true;
}
uint64_t
CurrentBlockchainStatus::get_dynamic_per_kb_fee_estimate() const
{
const double byte_to_kbyte_factor = 1024;
uint64_t fee_per_byte = mcore->get_dynamic_base_fee_estimate(
FEE_ESTIMATE_GRACE_BLOCKS);
uint64_t fee_per_kB = static_cast<uint64_t>(
fee_per_byte * byte_to_kbyte_factor);
return fee_per_kB;
}
uint64_t
CurrentBlockchainStatus::get_dynamic_base_fee_estimate() const
{
return mcore->get_dynamic_base_fee_estimate(
FEE_ESTIMATE_GRACE_BLOCKS);
}
uint64_t
CurrentBlockchainStatus::get_tx_unlock_time(
crypto::hash const& tx_hash) const
{
return mcore->get_tx_unlock_time(tx_hash);
}
bool
CurrentBlockchainStatus::commit_tx(
const string& tx_blob,
string& error_msg,
bool do_not_relay)
{
if (!rpc->commit_tx(tx_blob, error_msg, do_not_relay))
{
OMERROR << "rpc->commit_tx() commit_tx failed, reason: " << error_msg;
return false;
}
return true;
}
bool
CurrentBlockchainStatus::read_mempool()
{
// get txs in the mempool
std::vector<tx_info> mempool_tx_info;
vector<spent_key_image_info> key_image_infos;
if (!mcore->get_mempool_txs(mempool_tx_info, key_image_infos))
{
OMERROR << "Getting mempool failed ";
return false;
}
// not using this info at present
(void) key_image_infos;
std::lock_guard<std::mutex> lck (getting_mempool_txs);
// clear current mempool txs vector
// repopulate it with each execution of read_mempool()
// not very efficient but good enough for now.
mempool_txs.clear();
// if dont have tx_blob member, construct tx
// from json obtained from the rpc call
for (size_t i = 0; i < mempool_tx_info.size(); ++i)
{
// get transaction info of the tx in the mempool
tx_info const& _tx_info = mempool_tx_info.at(i);
transaction tx;
crypto::hash tx_hash;
crypto::hash tx_prefix_hash;
if (!parse_and_validate_tx_from_blob(
_tx_info.tx_blob, tx, tx_hash, tx_prefix_hash))
{
OMERROR << "Cant make tx from _tx_info.tx_blob";
return false;
}
(void) tx_hash;
(void) tx_prefix_hash;
mempool_txs.emplace_back(_tx_info.receive_time, tx);
} // for (size_t i = 0; i < mempool_tx_info.size(); ++i)
return true;
}
CurrentBlockchainStatus::mempool_txs_t
CurrentBlockchainStatus::get_mempool_txs()
{
std::lock_guard<std::mutex> lck (getting_mempool_txs);
return mempool_txs;
}
bool
CurrentBlockchainStatus::search_if_payment_made(
const string& payment_id_str,
const uint64_t& desired_amount,
string& tx_hash_with_payment)
{
mempool_txs_t mempool_transactions = get_mempool_txs();
uint64_t current_blockchain_height = get_current_blockchain_height();
cout << "current_blockchain_height: "
<< current_blockchain_height << '\n';
vector<transaction> txs_to_check;
for (auto& mtx: mempool_transactions)
{
txs_to_check.push_back(mtx.second);
}
// apend txs in last to blocks into the txs_to_check vector
for (uint64_t blk_i = current_blockchain_height - 10;
blk_i <= current_blockchain_height;
++blk_i)
{
// get block cointaining this tx
block blk;
if (!get_block(blk_i, blk)) {
OMERROR << "Cant get block of height: " << blk_i;
return false;
}
vector<cryptonote::transaction> blk_txs;
vector<crypto::hash> missed_txs;
if (!get_block_txs(blk, blk_txs, missed_txs))
{
OMERROR << "Cant get transactions from block: " << blk_i;
return false;
}
// combine mempool txs and txs from given number of
// last blocks
txs_to_check.insert(txs_to_check.end(),
blk_txs.begin(), blk_txs.end());
}
for (transaction& tx: txs_to_check)
{
if (is_coinbase(tx))
{
// not interested in coinbase txs
continue;
}
string tx_payment_id_str = get_payment_id_as_string(tx);
// we are interested only in txs with encrypted payments id8
// they have length of 16 characters.
if (tx_payment_id_str.length() != 16)
{
continue;
}
// we have some tx with encrypted payment_id8
// need to decode it using tx public key, and our
// private view key, before we can comapre it is
// what we are after.
crypto::hash8 encrypted_payment_id8;
if (!hex_to_pod(tx_payment_id_str, encrypted_payment_id8))
{
OMERROR << "Failed parsing hex to pod for encrypted_payment_id8";
continue;
}
// decrypt the encrypted_payment_id8
std::vector<public_key> tx_pub_keys = xmreg::get_tx_pub_keys_from_received_outs(tx);
public_key tx_pub_key;
// xmr puts 2 keys sometimes in normal transactions. previously the above
// function returned the second of the two in this case, rather than all pub
// keys.
if (tx_pub_keys.size() == 2)
{
tx_pub_key = tx_pub_keys[1];
}
else if (tx_pub_keys.size())
{
tx_pub_key = tx_pub_keys[0];
}
else
{
tx_pub_key = null_pkey;
}
// public transaction key is combined with our viewkey
// to create, so called, derived key.
key_derivation derivation;
if (!generate_key_derivation(tx_pub_key,
bc_setup.import_payment_viewkey,
derivation))
{
OMERROR << "Cant get derived key for: " << "\n"
<< "pub_tx_key: " << tx_pub_key << " and "
<< "prv_view_key" << bc_setup.import_payment_viewkey;
return false;
}
// decrypt encrypted payment id, as used in integreated addresses
crypto::hash8 decrypted_payment_id8 = encrypted_payment_id8;
if (decrypted_payment_id8 != null_hash8)
{
if (!mcore->decrypt_payment_id(
decrypted_payment_id8, tx_pub_key,
bc_setup.import_payment_viewkey))
{
OMERROR << "Cant decrypt decrypted_payment_id8: "
<< pod_to_hex(decrypted_payment_id8);
continue;
}
}
string decrypted_tx_payment_id_str
= pod_to_hex(decrypted_payment_id8);
// check if decrypted payment id matches what we have stored
// in mysql.
if (payment_id_str != decrypted_tx_payment_id_str)
{
// check tx having specific payment id only
continue;
}
// if everything ok with payment id, we proceed with
// checking if the amount transfered is correct.
// for each output, in a tx, check if it belongs
// to the given account of specific address and viewkey
// <public_key , amount , out idx>
std::vector<outputs_tuple> outputs;
outputs = get_outputs_tuple(tx);
string tx_hash_str = pod_to_hex(get_transaction_hash(tx));
uint64_t total_received {0};
for (auto& out: outputs)
{
if (std::get<0>(out).type() != typeid(txout_to_key))
{
continue;
}
const txout_to_key& txout_k
= boost::get<cryptonote::txout_to_key>(std::get<0>(out));
uint64_t amount = std::get<1>(out);
uint64_t output_idx_in_tx = std::get<2>(out);
// get the tx output public key
// that normally would be generated for us,
// if someone had sent us some xmr.
public_key generated_tx_pubkey;
derive_public_key(derivation,
output_idx_in_tx,
bc_setup.import_payment_address
.address.m_spend_public_key,
generated_tx_pubkey);
// check if generated public key matches the current
// output's key
bool mine_output = (txout_k.key == generated_tx_pubkey);
// if mine output has RingCT, i.e., tx version is 2
// need to decode its amount. otherwise its zero.
if (mine_output && tx.version >= cryptonote::txversion::v2_ringct)
{
// initialize with regular amount
uint64_t rct_amount = amount;
// coinbase txs have amounts in plain sight.
// so use amount from RingCT, only for non-coinbase txs
if (!is_coinbase(tx))
{
bool r;
r = decode_ringct(tx.rct_signatures,
tx_pub_key,
bc_setup.import_payment_viewkey,
output_idx_in_tx,
tx.rct_signatures
.ecdhInfo[output_idx_in_tx].mask,
rct_amount);
if (!r)
{
OMERROR << "Cant decode ringCT!";
return false;
}
amount = rct_amount;
}
} // if (mine_output && tx.version == 2)
if (mine_output)
total_received += amount;
}
OMINFO << " Payment id check in tx: "
<< tx_hash_str
<< " found: " << total_received;
if (total_received >= desired_amount)
{
// the payment has been made.
tx_hash_with_payment = tx_hash_str;
OMINFO << "Import payment done";
return true;
}
}
return false;
}
string
CurrentBlockchainStatus::get_payment_id_as_string(const transaction& tx)
{
crypto::hash payment_id = null_hash;
crypto::hash8 payment_id8 = null_hash8;
get_payment_id(tx, payment_id, payment_id8);
string payment_id_str{""};
if (payment_id != null_hash)
{
payment_id_str = pod_to_hex(payment_id);
}
else if (payment_id8 != null_hash8)
{
payment_id_str = pod_to_hex(payment_id8);
}
return payment_id_str;
}
output_data_t
CurrentBlockchainStatus::get_output_key(
uint64_t amount,
uint64_t global_amount_index)
{
return mcore->get_output_key(amount, global_amount_index);
}
bool
CurrentBlockchainStatus::start_tx_search_thread(
XmrAccount acc, std::unique_ptr<TxSearch> tx_search)
{
std::lock_guard<std::mutex> lck (searching_threads_map_mtx);
if (search_thread_exist(acc.address))
{
// thread for this address exist, don't make new one
//cout << "Thread exists, don't make new one\n";
return true; // this is still OK, so return true.
}
try
{
// launch SearchTx thread for the given xmr account
searching_threads.insert(
{acc.address, ThreadRAII2<TxSearch>(std::move(tx_search))});
OMINFO << "Search thread created for address: " << acc.address;
}
catch (const std::exception& e)
{
OMERROR << "Faild created a search thread: " << e.what();
return false;
}
return true;
}
bool
CurrentBlockchainStatus::ping_search_thread(const string& address)
{
std::lock_guard<std::mutex> lck (searching_threads_map_mtx);
if (!search_thread_exist(address))
{
// thread does not exist
OMERROR << "thread for " << address << " does not exist";
return false;
}
get_search_thread(address).ping();
return true;
}
bool
CurrentBlockchainStatus::get_searched_blk_no(const string& address,
uint64_t& searched_blk_no)
{
std::lock_guard<std::mutex> lck (searching_threads_map_mtx);
if (!search_thread_exist(address))
{
// thread does not exist
OMERROR << "thread for " << address << " does not exist";
return false;
}
searched_blk_no = get_search_thread(address).get_searched_blk_no();
return true;
}
bool
CurrentBlockchainStatus::get_known_outputs_keys(
string const& address,
unordered_map<public_key, uint64_t>& known_outputs_keys)
{
std::lock_guard<std::mutex> lck (searching_threads_map_mtx);
if (!search_thread_exist(address))
{
// thread does not exist
OMERROR << "thread for " << address << " does not exist";
return false;
}
known_outputs_keys = get_search_thread(address).get_known_outputs_keys();
return true;
}
bool
CurrentBlockchainStatus::search_thread_exist(const string& address)
{
// no mutex here, as this will be executed
// from other methods, which do use mutex.
// so if you put mutex here, you will get into deadlock.
return searching_threads.count(address) > 0;
}
bool
CurrentBlockchainStatus::get_xmr_address_viewkey(
const string& address_str,
address_parse_info& address,
secret_key& viewkey)
{
std::lock_guard<std::mutex> lck (searching_threads_map_mtx);
if (!search_thread_exist(address_str))
{
// thread does not exist
OMERROR << "thread for " << address_str << " does not exist";
return false;
}
address = get_search_thread(address_str).get_xmr_address_viewkey().first;
viewkey = get_search_thread(address_str).get_xmr_address_viewkey().second;
return true;
}
bool
CurrentBlockchainStatus::find_txs_in_mempool(
const string& address_str,
json& transactions)
{
std::lock_guard<std::mutex> lck (getting_mempool_txs);
if (searching_threads.count(address_str) == 0)
{
// thread does not exist
OMERROR << "thread for " << address_str << " does not exist";
return false;
}
get_search_thread(address_str)
.find_txs_in_mempool(mempool_txs, &transactions);
return true;
}
bool
CurrentBlockchainStatus::find_tx_in_mempool(
crypto::hash const& tx_hash,
transaction& tx)
{
std::lock_guard<std::mutex> lck (getting_mempool_txs);
for (auto const& mtx: mempool_txs)
{
const transaction &m_tx = mtx.second;
if (get_transaction_hash(m_tx) == tx_hash)
{
tx = m_tx;
return true;
}
}
return false;
}
bool
CurrentBlockchainStatus::find_key_images_in_mempool(
std::vector<txin_v> const& vin)
{
std::lock_guard<std::mutex> lck (getting_mempool_txs);
// perform exhostive search to check if any key image in vin vector
// is in the mempool. This is used to check if a tx generated
// by the frontend
// is using any key images that area already in the mempool.
for (auto const& kin: vin)
{
if(kin.type() != typeid(txin_to_key))
continue;
// get tx input key
const txin_to_key& tx_in_to_key
= boost::get<cryptonote::txin_to_key>(kin);
for (auto const& mtx: mempool_txs)
{
transaction const& m_tx = mtx.second;
vector<txin_to_key> input_key_imgs
= xmreg::get_key_images(m_tx);
for (auto const& mki: input_key_imgs)
{
// if a matching key image found in the mempool
if (mki.k_image == tx_in_to_key.k_image)
return true;
}
}
}
return false;
}
bool
CurrentBlockchainStatus::find_key_images_in_mempool(
transaction const& tx)
{
return find_key_images_in_mempool(tx.vin);
}
bool
CurrentBlockchainStatus::get_tx(
crypto::hash const& tx_hash,
transaction& tx)
{
return mcore->get_tx(tx_hash, tx);
}
bool
CurrentBlockchainStatus::get_tx(
string const& tx_hash_str,
transaction& tx)
{
crypto::hash tx_hash;
if (!hex_to_pod(tx_hash_str, tx_hash))
{
return false;
}
return mcore->get_tx(tx_hash, tx);
}
bool
CurrentBlockchainStatus::get_tx_block_height(
crypto::hash const& tx_hash,
int64_t& tx_height)
{
if (!tx_exist(tx_hash))
return false;
tx_height = mcore->get_tx_block_height(tx_hash);
return true;
}
bool
CurrentBlockchainStatus::set_new_searched_blk_no(
const string& address, uint64_t new_value)
{
std::lock_guard<std::mutex> lck (searching_threads_map_mtx);
if (searching_threads.count(address) == 0)
{
// thread does not exist
OMERROR << " thread does not exist";
return false;
}
get_search_thread(address).set_searched_blk_no(new_value);
return true;
}
TxSearch&
CurrentBlockchainStatus::get_search_thread(string const& acc_address)
{
// do not need locking mutex here, as this function should be only
// executed from others that do lock the mutex already.
auto it = searching_threads.find(acc_address);
if (it == searching_threads.end())
{
OMERROR << "Search thread does not exisit for addr: " << acc_address;
throw std::runtime_error("Trying to accesses "
"non-existing search thread");
}
return searching_threads.find(acc_address)->second.get_functor();
}
void
CurrentBlockchainStatus::clean_search_thread_map()
{
std::lock_guard<std::mutex> lck (searching_threads_map_mtx);
for (auto& st: searching_threads)
{