Add ping endpoint

pull/155/head
moneroexamples 5 years ago
parent 6f092bcd7c
commit ada4b03c8c

@ -327,6 +327,18 @@ Example output:
{"generated_locally":false,"new_address":true,"start_height":0,"status":"success"} {"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_address_txs
Get the list of all txs for the given user with their possible spendings. Get the list of all txs for the given user with their possible spendings.

@ -259,6 +259,7 @@ xmreg::OpenMoneroRequests open_monero(mysql_accounts, current_bc_status);
// create Open Monero APIs // create Open Monero APIs
MAKE_RESOURCE(login); MAKE_RESOURCE(login);
MAKE_RESOURCE(ping);
MAKE_RESOURCE(get_address_txs); MAKE_RESOURCE(get_address_txs);
MAKE_RESOURCE(get_address_info); MAKE_RESOURCE(get_address_info);
MAKE_RESOURCE(get_unspent_outs); MAKE_RESOURCE(get_unspent_outs);
@ -274,6 +275,7 @@ Service service;
// Publish the Open Monero API created so that front end can use it // Publish the Open Monero API created so that front end can use it
service.publish(login); service.publish(login);
service.publish(ping);
service.publish(get_address_txs); service.publish(get_address_txs);
service.publish(get_address_info); service.publish(get_address_info);
service.publish(get_unspent_outs); service.publish(get_unspent_outs);

@ -896,6 +896,19 @@ CurrentBlockchainStatus::search_thread_exist(const string& address)
return searching_threads.count(address) > 0; return searching_threads.count(address) > 0;
} }
bool
CurrentBlockchainStatus::search_thread_exist(
string const& address,
string const& viewkey)
{
std::lock_guard<std::mutex> lck (searching_threads_map_mtx);
if (!search_thread_exist(address))
return false;
return get_search_thread(address).get_viewkey() == viewkey;
}
bool bool
CurrentBlockchainStatus::get_xmr_address_viewkey( CurrentBlockchainStatus::get_xmr_address_viewkey(
const string& address_str, const string& address_str,

@ -213,6 +213,9 @@ public:
virtual bool virtual bool
search_thread_exist(const string& address); search_thread_exist(const string& address);
virtual bool
search_thread_exist(string const& address, string const& viewkey);
virtual bool virtual bool
get_xmr_address_viewkey(const string& address_str, get_xmr_address_viewkey(const string& address_str,

@ -16,7 +16,6 @@
namespace xmreg namespace xmreg
{ {
handel_::handel_(const fetch_func_t& callback): handel_::handel_(const fetch_func_t& callback):
request_callback {callback} request_callback {callback}
{} {}
@ -25,7 +24,7 @@ void
handel_::operator()(const shared_ptr< Session > session) handel_::operator()(const shared_ptr< Session > session)
{ {
const auto request = session->get_request( ); 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); session->fetch(content_length, this->request_callback);
} }
@ -154,6 +153,67 @@ OpenMoneroRequests::login(const shared_ptr<Session> session, const Bytes & body)
session_close(session, j_response); session_close(session, j_response);
} }
void
OpenMoneroRequests::ping(const shared_ptr<Session> session, const Bytes & body)
{
json j_response;
json j_request;
vector<string> 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 void
OpenMoneroRequests::get_address_txs( OpenMoneroRequests::get_address_txs(
const shared_ptr< Session > session, const Bytes & body) const shared_ptr< Session > session, const Bytes & body)

@ -28,7 +28,7 @@
// advance which version they will stop working with // advance which version they will stop working with
// Don't go over 32767 for any of these // Don't go over 32767 for any of these
#define OPENMONERO_RPC_VERSION_MAJOR 1 #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 MAKE_OPENMONERO_RPC_VERSION(major,minor) (((major)<<16)|(minor))
#define OPENMONERO_RPC_VERSION \ #define OPENMONERO_RPC_VERSION \
MAKE_OPENMONERO_RPC_VERSION(OPENMONERO_RPC_VERSION_MAJOR, OPENMONERO_RPC_VERSION_MINOR) MAKE_OPENMONERO_RPC_VERSION(OPENMONERO_RPC_VERSION_MAJOR, OPENMONERO_RPC_VERSION_MINOR)
@ -81,6 +81,9 @@ public:
*/ */
void void
login(const shared_ptr<Session> session, const Bytes & body); login(const shared_ptr<Session> session, const Bytes & body);
void
ping(const shared_ptr<Session> session, const Bytes & body);
void void
get_address_txs(const shared_ptr< Session > session, const Bytes & body); get_address_txs(const shared_ptr< Session > session, const Bytes & body);

@ -683,11 +683,12 @@ TxSearch::get_current_timestamp() const
void void
TxSearch::ping() TxSearch::ping()
{ {
//OMINFO << "New last_ping_timestamp: "
// << last_ping_timestamp.count();
last_ping_timestamp = chrono::duration_cast<seconds>( last_ping_timestamp = chrono::duration_cast<seconds>(
chrono::system_clock::now().time_since_epoch()); chrono::system_clock::now().time_since_epoch());
OMVLOG2 << address_prefix
<< ": last_ping_timestamp updated to: "
<< last_ping_timestamp.count();
} }
bool 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 void
TxSearch::set_search_thread_life(seconds life_seconds) TxSearch::set_search_thread_life(seconds life_seconds)
{ {

@ -176,6 +176,9 @@ public:
virtual addr_view_t virtual addr_view_t
get_xmr_address_viewkey() const; get_xmr_address_viewkey() const;
virtual string
get_viewkey() const;
static void static void
set_search_thread_life(seconds life_seconds); set_search_thread_life(seconds life_seconds);

Loading…
Cancel
Save