diff --git a/README.md b/README.md index 65d3487..d909338 100755 --- a/README.md +++ b/README.md @@ -327,6 +327,18 @@ Example output: {"generated_locally":false,"new_address":true,"start_height":0,"status":"success"} ``` +### ping + +Pings a search thread for a given account to extend its life. + +```bash +curl -w "\n" -X POST http://127.0.0.1:1984/ping -d '{"address": "A2VTvE8bC9APsWFn3mQzgW8Xfcy2SP2CRUArD6ZtthNaWDuuvyhtBcZ8WDuYMRt1HhcnNQvpXVUavEiZ9waTbyBhP6RM8TV", "view_key": "041a241325326f9d86519b714a9b7f78b29111551757eeb6334d39c21f8b7400"}' +``` + +Example output: +```json +{"generated_locally":false,"new_address":true,"start_height":0,"status":"success"} +``` #### get_address_txs Get the list of all txs for the given user with their possible spendings. diff --git a/main.cpp b/main.cpp index 55b3607..68dc7da 100755 --- a/main.cpp +++ b/main.cpp @@ -259,6 +259,7 @@ xmreg::OpenMoneroRequests open_monero(mysql_accounts, current_bc_status); // create Open Monero APIs MAKE_RESOURCE(login); +MAKE_RESOURCE(ping); MAKE_RESOURCE(get_address_txs); MAKE_RESOURCE(get_address_info); MAKE_RESOURCE(get_unspent_outs); @@ -274,6 +275,7 @@ Service service; // Publish the Open Monero API created so that front end can use it service.publish(login); +service.publish(ping); service.publish(get_address_txs); service.publish(get_address_info); service.publish(get_unspent_outs); diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp index 000b955..cd33609 100755 --- a/src/CurrentBlockchainStatus.cpp +++ b/src/CurrentBlockchainStatus.cpp @@ -896,6 +896,19 @@ CurrentBlockchainStatus::search_thread_exist(const string& address) return searching_threads.count(address) > 0; } +bool +CurrentBlockchainStatus::search_thread_exist( + string const& address, + string const& viewkey) +{ + std::lock_guard lck (searching_threads_map_mtx); + + if (!search_thread_exist(address)) + return false; + + return get_search_thread(address).get_viewkey() == viewkey; +} + bool CurrentBlockchainStatus::get_xmr_address_viewkey( const string& address_str, diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h index 31f7364..8bc8279 100755 --- a/src/CurrentBlockchainStatus.h +++ b/src/CurrentBlockchainStatus.h @@ -213,6 +213,9 @@ public: virtual bool search_thread_exist(const string& address); + + virtual bool + search_thread_exist(string const& address, string const& viewkey); virtual bool get_xmr_address_viewkey(const string& address_str, diff --git a/src/OpenMoneroRequests.cpp b/src/OpenMoneroRequests.cpp index d36a418..dab3131 100755 --- a/src/OpenMoneroRequests.cpp +++ b/src/OpenMoneroRequests.cpp @@ -16,7 +16,6 @@ namespace xmreg { - handel_::handel_(const fetch_func_t& callback): request_callback {callback} {} @@ -25,7 +24,7 @@ void handel_::operator()(const shared_ptr< Session > session) { const auto request = session->get_request( ); - size_t content_length = request->get_header( "Content-Length", 0); + size_t content_length = request->get_header("Content-Length", 0); session->fetch(content_length, this->request_callback); } @@ -154,6 +153,67 @@ OpenMoneroRequests::login(const shared_ptr session, const Bytes & body) session_close(session, j_response); } +void +OpenMoneroRequests::ping(const shared_ptr session, const Bytes & body) +{ + json j_response; + json j_request; + + vector required_values {"address", "view_key"}; + + if (!parse_request(body, required_values, j_request, j_response)) + { + session_close(session, j_response); + return; + } + + string xmr_address; + string view_key; + + try + { + xmr_address = j_request["address"]; + view_key = j_request["view_key"]; + } + catch (json::exception const& e) + { + OMERROR << "json exception: " << e.what(); + session_close(session, j_response, UNPROCESSABLE_ENTITY); + return; + } + + if (!current_bc_status->search_thread_exist(xmr_address, view_key)) + { + OMERROR << xmr_address.substr(0,6) + ": search thread does not exist"; + session_close(session, j_response, UNPROCESSABLE_ENTITY); + return; + } + + // ping the search thread that we still need it. + // otherwise it will finish after some time. + if (!current_bc_status->ping_search_thread(xmr_address)) + { + j_response = json {{"status", "error"}, + {"reason", "Pinging search thread failed."}}; + + // some error with loggin in or search thread start + session_close(session, j_response); + return; + + } + + OMINFO << xmr_address.substr(0,6) + ": search thread ping successful"; + + j_response["status"] = "success"; + + string response_body = j_response.dump(); + + auto response_headers = make_headers({{ "Content-Length", + to_string(response_body.size())}}); + + session->close(OK, response_body, response_headers); +} + void OpenMoneroRequests::get_address_txs( const shared_ptr< Session > session, const Bytes & body) diff --git a/src/OpenMoneroRequests.h b/src/OpenMoneroRequests.h index c53ba7d..f0ed361 100755 --- a/src/OpenMoneroRequests.h +++ b/src/OpenMoneroRequests.h @@ -28,7 +28,7 @@ // advance which version they will stop working with // Don't go over 32767 for any of these #define OPENMONERO_RPC_VERSION_MAJOR 1 -#define OPENMONERO_RPC_VERSION_MINOR 5 +#define OPENMONERO_RPC_VERSION_MINOR 6 #define MAKE_OPENMONERO_RPC_VERSION(major,minor) (((major)<<16)|(minor)) #define OPENMONERO_RPC_VERSION \ MAKE_OPENMONERO_RPC_VERSION(OPENMONERO_RPC_VERSION_MAJOR, OPENMONERO_RPC_VERSION_MINOR) @@ -81,6 +81,9 @@ public: */ void login(const shared_ptr session, const Bytes & body); + + void + ping(const shared_ptr session, const Bytes & body); void get_address_txs(const shared_ptr< Session > session, const Bytes & body); diff --git a/src/TxSearch.cpp b/src/TxSearch.cpp index 10ee9eb..a03bbd1 100755 --- a/src/TxSearch.cpp +++ b/src/TxSearch.cpp @@ -683,11 +683,12 @@ TxSearch::get_current_timestamp() const void TxSearch::ping() { - //OMINFO << "New last_ping_timestamp: " - // << last_ping_timestamp.count(); - last_ping_timestamp = chrono::duration_cast( chrono::system_clock::now().time_since_epoch()); + + OMVLOG2 << address_prefix + << ": last_ping_timestamp updated to: " + << last_ping_timestamp.count(); } bool @@ -944,6 +945,15 @@ TxSearch::get_xmr_address_viewkey() const } +string +TxSearch::get_viewkey() const +{ + static string viewkey = pod_to_hex(this->viewkey); + + return viewkey; +} + + void TxSearch::set_search_thread_life(seconds life_seconds) { diff --git a/src/TxSearch.h b/src/TxSearch.h index 6a1aece..257fae0 100755 --- a/src/TxSearch.h +++ b/src/TxSearch.h @@ -176,6 +176,9 @@ public: virtual addr_view_t get_xmr_address_viewkey() const; + + virtual string + get_viewkey() const; static void set_search_thread_life(seconds life_seconds);