Merge pull request #2240

b7d6ec83 simplewallet: add (out of sync) or (no daemon) markers in the prompt (moneromooo-monero)
fa23a500 wallet2: add a is_synced function (moneromooo-monero)
f1307bbd node_rpc_proxy: add a proxy for target height (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2017-08-15 20:46:31 +02:00
commit 6f60613ffb
No known key found for this signature in database
GPG key ID: 55432DF31CCD4FCD
7 changed files with 89 additions and 28 deletions

View file

@ -293,13 +293,13 @@ namespace epee
} }
template<class t_server, class chain_handler> template<class t_server, class chain_handler>
bool run(t_server* psrv, chain_handler ch_handler, const std::string& prompt = "#", const std::string& usage = "") bool run(t_server* psrv, chain_handler ch_handler, std::function<std::string(void)> prompt, const std::string& usage = "")
{ {
return run(prompt, usage, [&](const std::string& cmd) { return ch_handler(psrv, cmd); }, [&] { psrv->send_stop_signal(); }); return run(prompt, usage, [&](const std::string& cmd) { return ch_handler(psrv, cmd); }, [&] { psrv->send_stop_signal(); });
} }
template<class chain_handler> template<class chain_handler>
bool run(chain_handler ch_handler, const std::string& prompt = "#", const std::string& usage = "", std::function<void(void)> exit_handler = NULL) bool run(chain_handler ch_handler, std::function<std::string(void)> prompt, const std::string& usage = "", std::function<void(void)> exit_handler = NULL)
{ {
return run(prompt, usage, [&](const std::string& cmd) { return ch_handler(cmd); }, exit_handler); return run(prompt, usage, [&](const std::string& cmd) { return ch_handler(cmd); }, exit_handler);
} }
@ -312,18 +312,19 @@ namespace epee
void print_prompt() void print_prompt()
{ {
if (!m_prompt.empty()) std::string prompt = m_prompt();
if (!prompt.empty())
{ {
#ifdef HAVE_READLINE #ifdef HAVE_READLINE
std::string color_prompt = "\001\033[1;33m\002" + m_prompt; std::string color_prompt = "\001\033[1;33m\002" + prompt;
if (' ' != m_prompt.back()) if (' ' != prompt.back())
color_prompt += " "; color_prompt += " ";
color_prompt += "\001\033[0m\002"; color_prompt += "\001\033[0m\002";
m_stdin_reader.get_readline_buffer().set_prompt(color_prompt); m_stdin_reader.get_readline_buffer().set_prompt(color_prompt);
#else #else
epee::set_console_color(epee::console_color_yellow, true); epee::set_console_color(epee::console_color_yellow, true);
std::cout << m_prompt; std::cout << prompt;
if (' ' != m_prompt.back()) if (' ' != prompt.back())
std::cout << ' '; std::cout << ' ';
epee::reset_console_color(); epee::reset_console_color();
std::cout.flush(); std::cout.flush();
@ -333,7 +334,7 @@ namespace epee
private: private:
template<typename t_cmd_handler> template<typename t_cmd_handler>
bool run(const std::string& prompt, const std::string& usage, const t_cmd_handler& cmd_handler, std::function<void(void)> exit_handler) bool run(std::function<std::string(void)> prompt, const std::string& usage, const t_cmd_handler& cmd_handler, std::function<void(void)> exit_handler)
{ {
bool continue_handle = true; bool continue_handle = true;
m_prompt = prompt; m_prompt = prompt;
@ -394,7 +395,7 @@ namespace epee
private: private:
async_stdin_reader m_stdin_reader; async_stdin_reader m_stdin_reader;
std::atomic<bool> m_running = {true}; std::atomic<bool> m_running = {true};
std::string m_prompt; std::function<std::string(void)> m_prompt;
}; };
@ -516,19 +517,23 @@ namespace epee
std::unique_ptr<boost::thread> m_console_thread; std::unique_ptr<boost::thread> m_console_thread;
async_console_handler m_console_handler; async_console_handler m_console_handler;
public: public:
bool start_handling(const std::string& prompt, const std::string& usage_string = "", std::function<void(void)> exit_handler = NULL) bool start_handling(std::function<std::string(void)> prompt, const std::string& usage_string = "", std::function<void(void)> exit_handler = NULL)
{ {
m_console_thread.reset(new boost::thread(boost::bind(&console_handlers_binder::run_handling, this, prompt, usage_string, exit_handler))); m_console_thread.reset(new boost::thread(boost::bind(&console_handlers_binder::run_handling, this, prompt, usage_string, exit_handler)));
m_console_thread->detach(); m_console_thread->detach();
return true; return true;
} }
bool start_handling(const std::string &prompt, const std::string& usage_string = "", std::function<void(void)> exit_handler = NULL)
{
return start_handling([prompt](){ return prompt; }, usage_string, exit_handler);
}
void stop_handling() void stop_handling()
{ {
m_console_handler.stop(); m_console_handler.stop();
} }
bool run_handling(const std::string& prompt, const std::string& usage_string, std::function<void(void)> exit_handler = NULL) bool run_handling(std::function<std::string(void)> prompt, const std::string& usage_string, std::function<void(void)> exit_handler = NULL)
{ {
return m_console_handler.run(boost::bind(&console_handlers_binder::process_command_str, this, _1), prompt, usage_string, exit_handler); return m_console_handler.run(boost::bind(&console_handlers_binder::process_command_str, this, _1), prompt, usage_string, exit_handler);
} }

View file

@ -4191,6 +4191,19 @@ void simple_wallet::wallet_idle_thread()
} }
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
std::string simple_wallet::get_prompt() const
{
std::string addr_start = m_wallet->get_account().get_public_address_str(m_wallet->testnet()).substr(0, 6);
std::string prompt = std::string("[") + tr("wallet") + " " + addr_start;
uint32_t version;
if (!m_wallet->check_connection(&version))
prompt += tr(" (no daemon)");
else if (!m_wallet->is_synced())
prompt += tr(" (out of sync)");
prompt += "]: ";
return prompt;
}
//----------------------------------------------------------------------------------------------------
bool simple_wallet::run() bool simple_wallet::run()
{ {
// check and display warning, but go on anyway // check and display warning, but go on anyway
@ -4201,9 +4214,8 @@ bool simple_wallet::run()
m_auto_refresh_enabled = m_wallet->auto_refresh(); m_auto_refresh_enabled = m_wallet->auto_refresh();
m_idle_thread = boost::thread([&]{wallet_idle_thread();}); m_idle_thread = boost::thread([&]{wallet_idle_thread();});
std::string addr_start = m_wallet->get_account().get_public_address_str(m_wallet->testnet()).substr(0, 6);
message_writer(console_color_green, false) << "Background refresh thread started"; message_writer(console_color_green, false) << "Background refresh thread started";
return m_cmd_binder.run_handling(std::string("[") + tr("wallet") + " " + addr_start + "]: ", ""); return m_cmd_binder.run_handling([this](){return get_prompt();}, "");
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
void simple_wallet::stop() void simple_wallet::stop()

View file

@ -182,6 +182,7 @@ namespace cryptonote
bool accept_loaded_tx(const tools::wallet2::unsigned_tx_set &txs); bool accept_loaded_tx(const tools::wallet2::unsigned_tx_set &txs);
bool accept_loaded_tx(const tools::wallet2::signed_tx_set &txs); bool accept_loaded_tx(const tools::wallet2::signed_tx_set &txs);
bool print_ring_members(const std::vector<tools::wallet2::pending_tx>& ptx_vector, std::ostream& ostr); bool print_ring_members(const std::vector<tools::wallet2::pending_tx>& ptx_vector, std::ostream& ostr);
std::string get_prompt() const;
/*! /*!
* \brief Prints the seed with a nice message * \brief Prints the seed with a nice message

View file

@ -48,6 +48,8 @@ NodeRPCProxy::NodeRPCProxy(epee::net_utils::http::http_simple_client &http_clien
, m_dynamic_per_kb_fee_estimate_cached_height(0) , m_dynamic_per_kb_fee_estimate_cached_height(0)
, m_dynamic_per_kb_fee_estimate_grace_blocks(0) , m_dynamic_per_kb_fee_estimate_grace_blocks(0)
, m_rpc_version(0) , m_rpc_version(0)
, m_target_height(0)
, m_target_height_time(0)
{} {}
void NodeRPCProxy::invalidate() void NodeRPCProxy::invalidate()
@ -60,9 +62,11 @@ void NodeRPCProxy::invalidate()
m_dynamic_per_kb_fee_estimate_cached_height = 0; m_dynamic_per_kb_fee_estimate_cached_height = 0;
m_dynamic_per_kb_fee_estimate_grace_blocks = 0; m_dynamic_per_kb_fee_estimate_grace_blocks = 0;
m_rpc_version = 0; m_rpc_version = 0;
m_target_height = 0;
m_target_height_time = 0;
} }
boost::optional<std::string> NodeRPCProxy::get_rpc_version(uint32_t &rpc_version) boost::optional<std::string> NodeRPCProxy::get_rpc_version(uint32_t &rpc_version) const
{ {
const time_t now = time(NULL); const time_t now = time(NULL);
if (m_rpc_version == 0) if (m_rpc_version == 0)
@ -84,7 +88,7 @@ boost::optional<std::string> NodeRPCProxy::get_rpc_version(uint32_t &rpc_version
return boost::optional<std::string>(); return boost::optional<std::string>();
} }
boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height) boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height) const
{ {
const time_t now = time(NULL); const time_t now = time(NULL);
if (m_height == 0 || now >= m_height_time + 30) // re-cache every 30 seconds if (m_height == 0 || now >= m_height_time + 30) // re-cache every 30 seconds
@ -110,7 +114,32 @@ void NodeRPCProxy::set_height(uint64_t h)
m_height = h; m_height = h;
} }
boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version, uint64_t &earliest_height) boost::optional<std::string> NodeRPCProxy::get_target_height(uint64_t &height) const
{
const time_t now = time(NULL);
if (m_height == 0 || now >= m_height_time + 30) // re-cache every 30 seconds
{
epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_INFO::request> req_t = AUTO_VAL_INIT(req_t);
epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_INFO::response, std::string> resp_t = AUTO_VAL_INIT(resp_t);
req_t.jsonrpc = "2.0";
req_t.id = epee::serialization::storage_entry(0);
req_t.method = "get_info";
m_daemon_rpc_mutex.lock();
bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client, rpc_timeout);
m_daemon_rpc_mutex.unlock();
CHECK_AND_ASSERT_MES(r, std::string(), "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(resp_t.result.status != CORE_RPC_STATUS_BUSY, resp_t.result.status, "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(resp_t.result.status == CORE_RPC_STATUS_OK, resp_t.result.status, "Failed to get target blockchain height");
m_target_height = resp_t.result.target_height;
m_target_height_time = now;
}
height = m_target_height;
return boost::optional<std::string>();
}
boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version, uint64_t &earliest_height) const
{ {
if (m_earliest_height[version] == 0) if (m_earliest_height[version] == 0)
{ {
@ -134,7 +163,7 @@ boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version,
return boost::optional<std::string>(); return boost::optional<std::string>();
} }
boost::optional<std::string> NodeRPCProxy::get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee) boost::optional<std::string> NodeRPCProxy::get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee) const
{ {
uint64_t height; uint64_t height;

View file

@ -43,23 +43,26 @@ public:
void invalidate(); void invalidate();
boost::optional<std::string> get_rpc_version(uint32_t &version); boost::optional<std::string> get_rpc_version(uint32_t &version) const;
boost::optional<std::string> get_height(uint64_t &height); boost::optional<std::string> get_height(uint64_t &height) const;
void set_height(uint64_t h); void set_height(uint64_t h);
boost::optional<std::string> get_earliest_height(uint8_t version, uint64_t &earliest_height); boost::optional<std::string> get_target_height(uint64_t &height) const;
boost::optional<std::string> get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee); boost::optional<std::string> get_earliest_height(uint8_t version, uint64_t &earliest_height) const;
boost::optional<std::string> get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee) const;
private: private:
epee::net_utils::http::http_simple_client &m_http_client; epee::net_utils::http::http_simple_client &m_http_client;
boost::mutex &m_daemon_rpc_mutex; boost::mutex &m_daemon_rpc_mutex;
uint64_t m_height; mutable uint64_t m_height;
time_t m_height_time; mutable time_t m_height_time;
uint64_t m_earliest_height[256]; mutable uint64_t m_earliest_height[256];
uint64_t m_dynamic_per_kb_fee_estimate; mutable uint64_t m_dynamic_per_kb_fee_estimate;
uint64_t m_dynamic_per_kb_fee_estimate_cached_height; mutable uint64_t m_dynamic_per_kb_fee_estimate_cached_height;
uint64_t m_dynamic_per_kb_fee_estimate_grace_blocks; mutable uint64_t m_dynamic_per_kb_fee_estimate_grace_blocks;
uint32_t m_rpc_version; mutable uint32_t m_rpc_version;
mutable uint64_t m_target_height;
mutable time_t m_target_height_time;
}; };
} }

View file

@ -5711,6 +5711,15 @@ uint64_t wallet2::get_blockchain_height_by_date(uint16_t year, uint8_t month, ui
} }
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
bool wallet2::is_synced() const
{
uint64_t height;
boost::optional<std::string> result = m_node_rpc_proxy.get_target_height(height);
if (result && *result != CORE_RPC_STATUS_OK)
return false;
return get_blockchain_current_height() >= height;
}
//----------------------------------------------------------------------------------------------------
void wallet2::generate_genesis(cryptonote::block& b) { void wallet2::generate_genesis(cryptonote::block& b) {
if (m_testnet) if (m_testnet)
{ {

View file

@ -600,6 +600,8 @@ namespace tools
uint64_t get_blockchain_height_by_date(uint16_t year, uint8_t month, uint8_t day); // 1<=month<=12, 1<=day<=31 uint64_t get_blockchain_height_by_date(uint16_t year, uint8_t month, uint8_t day); // 1<=month<=12, 1<=day<=31
bool is_synced() const;
private: private:
/*! /*!
* \brief Stores wallet information to wallet file. * \brief Stores wallet information to wallet file.