timer added to TxSearch

pull/1/head
moneroexamples 8 years ago
parent 871d6ce434
commit c617312ae4

@ -21,6 +21,7 @@
#include <thread>
#include <mutex>
#include <atomic>
#include <chrono>
namespace xmreg
{
@ -36,6 +37,9 @@ class TxSearchException: public std::runtime_error
class TxSearch
{
static constexpr uint64_t UPDATE_SCANNED_HEIGHT_INTERVAL = 10; // seconds
bool continue_search {true};
// represents a row in mysql's Accounts table
@ -83,12 +87,33 @@ public:
// this accont
uint64_t searched_blk_no = acc.scanned_block_height;
// start scanning from befor for tests.
searched_blk_no -= 100;
if (searched_blk_no > CurrentBlockchainStatus::current_height)
{
throw TxSearchException("searched_blk_no > CurrentBlockchainStatus::current_height");
}
while(continue_search) {
uint64_t current_timestamp = chrono::duration_cast<chrono::seconds>(
chrono::system_clock::now().time_since_epoch()).count();
uint64_t loop_idx {0};
while(continue_search)
{
++loop_idx;
uint64_t loop_timestamp {current_timestamp};
if (loop_idx % 2 == 0)
{
// get loop time every second iteration. no need to call it
// all the time.
loop_timestamp = chrono::duration_cast<chrono::seconds>(
chrono::system_clock::now().time_since_epoch()).count();
}
if (searched_blk_no > CurrentBlockchainStatus::current_height) {
fmt::print("searched_blk_no {:d} and current_height {:d}\n",
@ -185,6 +210,11 @@ public:
// check if generated public key matches the current output's key
bool mine_output = (txout_k.key == generated_tx_pubkey);
//cout << "Chekcing output: " << pod_to_hex(txout_k.key) << " "
// << "mine_output: " << mine_output << endl;
// if mine output has RingCT, i.e., tx version is 2
// need to decode its amount. otherwise its zero.
if (mine_output && tx.version == 2)
@ -291,7 +321,7 @@ public:
} // for (const transaction& tx: blk_txs)
if (searched_blk_no % 10 == 0)
if (loop_timestamp - current_timestamp > UPDATE_SCANNED_HEIGHT_INTERVAL)
{
// every 10 blocks updated scanned_block_height
@ -302,8 +332,11 @@ public:
if (xmr_accounts->update(acc, updated_acc))
{
// iff success, set acc to updated_acc;
cout << "scanned_block_height updated" << endl;
acc = updated_acc;
}
current_timestamp = loop_timestamp;
}
++searched_blk_no;
@ -324,6 +357,8 @@ public:
}
};
}

@ -183,8 +183,8 @@ public:
{
json j_request = body_to_json(body);
if (show_logs)
print_json_log("get_address_txs request: ", j_request);
// if (show_logs)
// print_json_log("get_address_txs request: ", j_request);
string xmr_address = j_request["address"];
@ -246,19 +246,34 @@ public:
// if (show_logs)
// print_json_log("get_address_info request: ", j_request);
string xmr_address = j_request["address"];
json j_response {
{"locked_funds", "0"},
{"total_received", "0"},
{"total_sent", "0"},
{"scanned_height", 2012470},
{"scanned_block_height", 1195850},
{"start_height", 2012470},
{"transaction_height", 2012470},
{"blockchain_height", 1195850},
{"scanned_height", 0},
{"scanned_block_height", 0},
{"start_height", 0},
{"transaction_height", 0},
{"blockchain_height", 0},
{"spent_outputs", nullptr}
};
string response_body = j_response.dump();
// a placeholder for exciting or new account data
xmreg::XmrAccount acc;
// select this account if its existing one
if (xmr_accounts->select(xmr_address, acc))
{
j_response["total_received"] = acc.total_received;
j_response["scanned_block_height"] = acc.scanned_block_height;
j_response["blockchain_height"] = CurrentBlockchainStatus::get_current_blockchain_height();
}
string response_body = j_response.dump();
auto response_headers = make_headers({{ "Content-Length", to_string(response_body.size())}});

Loading…
Cancel
Save