wallet: optional automatic refresh from the daemon

The daemon will be polled every 90 seconds for new blocks.
It is enabled by default, and can be turned on/off with
set auto-refresh 1 and set auto-refresh 0 in the wallet.
This commit is contained in:
moneromooo-monero 2015-11-28 12:38:58 +00:00
parent 4061a32082
commit 62e49a5f02
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3
5 changed files with 143 additions and 20 deletions

View File

@ -251,27 +251,33 @@ namespace epee
m_stdin_reader.stop(); m_stdin_reader.stop();
} }
void print_prompt()
{
if (!m_prompt.empty())
{
epee::log_space::set_console_color(epee::log_space::console_color_yellow, true);
std::cout << m_prompt;
if (' ' != m_prompt.back())
std::cout << ' ';
epee::log_space::reset_console_color();
std::cout.flush();
}
}
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(const std::string& prompt, const std::string& usage, const t_cmd_handler& cmd_handler, std::function<void(void)> exit_handler)
{ {
TRY_ENTRY(); TRY_ENTRY();
bool continue_handle = true; bool continue_handle = true;
m_prompt = prompt;
while(continue_handle) while(continue_handle)
{ {
if (!m_running) if (!m_running)
{ {
break; break;
} }
if (!prompt.empty()) print_prompt();
{
epee::log_space::set_console_color(epee::log_space::console_color_yellow, true);
std::cout << prompt;
if (' ' != prompt.back())
std::cout << ' ';
epee::log_space::reset_console_color();
std::cout.flush();
}
std::string command; std::string command;
bool get_line_ret = m_stdin_reader.get_line(command); bool get_line_ret = m_stdin_reader.get_line(command);
@ -313,6 +319,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;
}; };
@ -447,6 +454,11 @@ namespace epee
{ {
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);
} }
void print_prompt()
{
m_console_handler.print_prompt();
}
}; };
///* work around because of broken boost bind */ ///* work around because of broken boost bind */

View File

@ -421,6 +421,42 @@ bool simple_wallet::set_default_mixin(const std::vector<std::string> &args/* = s
} }
} }
bool simple_wallet::set_auto_refresh(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{
bool success = false;
tools::password_container pwd_container;
success = pwd_container.read_password();
if (!success)
{
fail_msg_writer() << tr("failed to read wallet password");
return true;
}
/* verify password before using so user doesn't accidentally set a new password for rewritten wallet */
success = m_wallet->verify_password(pwd_container.password());
if (!success)
{
fail_msg_writer() << tr("invalid password");
return true;
}
bool auto_refresh = is_it_true(args[1]);
m_wallet->auto_refresh(auto_refresh);
if (auto_refresh && !m_auto_refresh_run.load(std::memory_order_relaxed))
{
m_auto_refresh_run.store(true, std::memory_order_relaxed);
m_auto_refresh_thread = std::thread([&]{wallet_refresh_thread();});
}
else if (!auto_refresh && m_auto_refresh_run.load(std::memory_order_relaxed))
{
m_auto_refresh_run.store(false, std::memory_order_relaxed);
m_auto_refresh_thread.join();
}
m_wallet->rewrite(m_wallet_file, pwd_container.password());
return true;
}
bool simple_wallet::help(const std::vector<std::string> &args/* = std::vector<std::string>()*/) bool simple_wallet::help(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{ {
success_msg_writer() << get_commands_str(); success_msg_writer() << get_commands_str();
@ -430,6 +466,8 @@ bool simple_wallet::help(const std::vector<std::string> &args/* = std::vector<st
simple_wallet::simple_wallet() simple_wallet::simple_wallet()
: m_daemon_port(0) : m_daemon_port(0)
, m_refresh_progress_reporter(*this) , m_refresh_progress_reporter(*this)
, m_auto_refresh_run(false)
, m_auto_refresh_refreshing(false)
{ {
m_cmd_binder.set_handler("start_mining", boost::bind(&simple_wallet::start_mining, this, _1), tr("start_mining [<number_of_threads>] - Start mining in daemon")); m_cmd_binder.set_handler("start_mining", boost::bind(&simple_wallet::start_mining, this, _1), tr("start_mining [<number_of_threads>] - Start mining in daemon"));
m_cmd_binder.set_handler("stop_mining", boost::bind(&simple_wallet::stop_mining, this, _1), tr("Stop mining in daemon")); m_cmd_binder.set_handler("stop_mining", boost::bind(&simple_wallet::stop_mining, this, _1), tr("Stop mining in daemon"));
@ -450,7 +488,7 @@ simple_wallet::simple_wallet()
m_cmd_binder.set_handler("viewkey", boost::bind(&simple_wallet::viewkey, this, _1), tr("Get viewkey")); m_cmd_binder.set_handler("viewkey", boost::bind(&simple_wallet::viewkey, this, _1), tr("Get viewkey"));
m_cmd_binder.set_handler("spendkey", boost::bind(&simple_wallet::spendkey, this, _1), tr("Get spendkey")); m_cmd_binder.set_handler("spendkey", boost::bind(&simple_wallet::spendkey, this, _1), tr("Get spendkey"));
m_cmd_binder.set_handler("seed", boost::bind(&simple_wallet::seed, this, _1), tr("Get deterministic seed")); m_cmd_binder.set_handler("seed", boost::bind(&simple_wallet::seed, this, _1), tr("Get deterministic seed"));
m_cmd_binder.set_handler("set", boost::bind(&simple_wallet::set_variable, this, _1), tr("available options: seed language - Set wallet seed langage; always-confirm-transfers <1|0> - whether to confirm unsplit txes; store-tx-info <1|0> - whether to store per outgoing tx info (destination address, payment id, tx secret key) for future reference; default_mixin <n> - set default mixin (default default is 4")); m_cmd_binder.set_handler("set", boost::bind(&simple_wallet::set_variable, this, _1), tr("available options: seed language - Set wallet seed langage; always-confirm-transfers <1|0> - whether to confirm unsplit txes; store-tx-info <1|0> - whether to store per outgoing tx info (destination address, payment id, tx secret key) for future reference; default_mixin <n> - set default mixin (default default is 4; auto-refresh <1|0> - whether to automatically refresh new blocks from the daemon"));
m_cmd_binder.set_handler("rescan_spent", boost::bind(&simple_wallet::rescan_spent, this, _1), tr("Rescan blockchain for spent outputs")); m_cmd_binder.set_handler("rescan_spent", boost::bind(&simple_wallet::rescan_spent, this, _1), tr("Rescan blockchain for spent outputs"));
m_cmd_binder.set_handler("get_tx_key", boost::bind(&simple_wallet::get_tx_key, this, _1), tr("Get transaction key (r) for a given tx")); m_cmd_binder.set_handler("get_tx_key", boost::bind(&simple_wallet::get_tx_key, this, _1), tr("Get transaction key (r) for a given tx"));
m_cmd_binder.set_handler("check_tx_key", boost::bind(&simple_wallet::check_tx_key, this, _1), tr("Check amount going to a given address in a partcular tx")); m_cmd_binder.set_handler("check_tx_key", boost::bind(&simple_wallet::check_tx_key, this, _1), tr("Check amount going to a given address in a partcular tx"));
@ -462,7 +500,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
{ {
if (args.empty()) if (args.empty())
{ {
fail_msg_writer() << tr("set: needs an argument. available options: seed, always-confirm-transfers, default-mixin"); fail_msg_writer() << tr("set: needs an argument. available options: seed, always-confirm-transfers, default-mixin, auto-refresh");
return true; return true;
} }
else else
@ -527,6 +565,21 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
return true; return true;
} }
} }
else if (args[0] == "auto-refresh")
{
if (args.size() <= 1)
{
fail_msg_writer() << tr("set auto-refresh: needs an argument (0 or 1)");
return true;
}
else
{
std::vector<std::string> local_args = args;
local_args.erase(local_args.begin(), local_args.begin()+2);
set_auto_refresh(local_args);
return true;
}
}
} }
fail_msg_writer() << tr("set: unrecognized argument(s)"); fail_msg_writer() << tr("set: unrecognized argument(s)");
return true; return true;
@ -1028,6 +1081,12 @@ bool simple_wallet::open_wallet(const string &wallet_file, const std::string& pa
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
bool simple_wallet::close_wallet() bool simple_wallet::close_wallet()
{ {
if (m_auto_refresh_run.load(std::memory_order_relaxed))
{
m_auto_refresh_run.store(false, std::memory_order_relaxed);
m_auto_refresh_thread.join();
}
bool r = m_wallet->deinit(); bool r = m_wallet->deinit();
if (!r) if (!r)
{ {
@ -1175,38 +1234,50 @@ bool simple_wallet::save_bc(const std::vector<std::string>& args)
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
void simple_wallet::on_new_block(uint64_t height, const cryptonote::block& block) void simple_wallet::on_new_block(uint64_t height, const cryptonote::block& block)
{ {
m_refresh_progress_reporter.update(height, false); if (!m_auto_refresh_refreshing)
m_refresh_progress_reporter.update(height, false);
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
void simple_wallet::on_money_received(uint64_t height, const cryptonote::transaction& tx, size_t out_index) void simple_wallet::on_money_received(uint64_t height, const cryptonote::transaction& tx, size_t out_index)
{ {
message_writer(epee::log_space::console_color_green, false) << message_writer(epee::log_space::console_color_green, false) << "\r" <<
tr("Height ") << height << ", " << tr("Height ") << height << ", " <<
tr("transaction ") << get_transaction_hash(tx) << ", " << tr("transaction ") << get_transaction_hash(tx) << ", " <<
tr("received ") << print_money(tx.vout[out_index].amount); tr("received ") << print_money(tx.vout[out_index].amount);
m_refresh_progress_reporter.update(height, true); if (m_auto_refresh_refreshing)
m_cmd_binder.print_prompt();
else
m_refresh_progress_reporter.update(height, true);
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
void simple_wallet::on_money_spent(uint64_t height, const cryptonote::transaction& in_tx, size_t out_index, const cryptonote::transaction& spend_tx) void simple_wallet::on_money_spent(uint64_t height, const cryptonote::transaction& in_tx, size_t out_index, const cryptonote::transaction& spend_tx)
{ {
message_writer(epee::log_space::console_color_magenta, false) << message_writer(epee::log_space::console_color_magenta, false) << "\r" <<
tr("Height ") << height << ", " << tr("Height ") << height << ", " <<
tr("transaction ") << get_transaction_hash(spend_tx) << ", " << tr("transaction ") << get_transaction_hash(spend_tx) << ", " <<
tr("spent ") << print_money(in_tx.vout[out_index].amount); tr("spent ") << print_money(in_tx.vout[out_index].amount);
m_refresh_progress_reporter.update(height, true); if (m_auto_refresh_refreshing)
m_cmd_binder.print_prompt();
else
m_refresh_progress_reporter.update(height, true);
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
void simple_wallet::on_skip_transaction(uint64_t height, const cryptonote::transaction& tx) void simple_wallet::on_skip_transaction(uint64_t height, const cryptonote::transaction& tx)
{ {
message_writer(epee::log_space::console_color_red, true) << message_writer(epee::log_space::console_color_red, true) << "\r" <<
tr("Height ") << height << ", " << tr("Height ") << height << ", " <<
tr("transaction ") << get_transaction_hash(tx) << ", " << tr("transaction ") << get_transaction_hash(tx) << ", " <<
tr("unsupported transaction format"); tr("unsupported transaction format");
m_refresh_progress_reporter.update(height, true); if (m_auto_refresh_refreshing)
m_cmd_binder.print_prompt();
else
m_refresh_progress_reporter.update(height, true);
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
bool simple_wallet::refresh(const std::vector<std::string>& args) bool simple_wallet::refresh(const std::vector<std::string>& args)
{ {
std::unique_lock<std::mutex> lock(m_auto_refresh_mutex);
if (!try_connect_to_daemon()) if (!try_connect_to_daemon())
return true; return true;
@ -2146,9 +2217,33 @@ bool simple_wallet::show_transfers(const std::vector<std::string> &args_)
return true; return true;
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
void simple_wallet::wallet_refresh_thread()
{
while (true)
{
std::unique_lock<std::mutex> lock(m_auto_refresh_mutex);
m_auto_refresh_refreshing = true;
try
{
uint64_t fetched_blocks;
m_wallet->refresh(0, fetched_blocks);
}
catch(...) {}
m_auto_refresh_refreshing = false;
m_auto_refresh_cond.wait_for(lock, chrono::seconds(90));
if (!m_auto_refresh_run.load(std::memory_order_relaxed))
break;
}
}
//----------------------------------------------------------------------------------------------------
bool simple_wallet::run() bool simple_wallet::run()
{ {
std::string addr_start = m_wallet->get_account().get_public_address_str(m_wallet->testnet()).substr(0, 6); std::string addr_start = m_wallet->get_account().get_public_address_str(m_wallet->testnet()).substr(0, 6);
m_auto_refresh_run = m_wallet->auto_refresh();
if (m_auto_refresh_run)
{
m_auto_refresh_thread = std::thread([&]{wallet_refresh_thread();});
}
return m_cmd_binder.run_handling(std::string("[") + tr("wallet") + " " + addr_start + "]: ", ""); return m_cmd_binder.run_handling(std::string("[") + tr("wallet") + " " + addr_start + "]: ", "");
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------

View File

@ -77,6 +77,8 @@ namespace cryptonote
bool run_console_handler(); bool run_console_handler();
void wallet_refresh_thread();
bool new_wallet(const std::string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key, bool new_wallet(const std::string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key,
bool recover, bool two_random, bool testnet, const std::string &old_language); bool recover, bool two_random, bool testnet, const std::string &old_language);
bool new_wallet(const std::string &wallet_file, const std::string& password, const cryptonote::account_public_address& address, bool new_wallet(const std::string &wallet_file, const std::string& password, const cryptonote::account_public_address& address,
@ -101,6 +103,7 @@ namespace cryptonote
bool set_always_confirm_transfers(const std::vector<std::string> &args = std::vector<std::string>()); bool set_always_confirm_transfers(const std::vector<std::string> &args = std::vector<std::string>());
bool set_store_tx_info(const std::vector<std::string> &args = std::vector<std::string>()); bool set_store_tx_info(const std::vector<std::string> &args = std::vector<std::string>());
bool set_default_mixin(const std::vector<std::string> &args = std::vector<std::string>()); bool set_default_mixin(const std::vector<std::string> &args = std::vector<std::string>());
bool set_auto_refresh(const std::vector<std::string> &args = std::vector<std::string>());
bool help(const std::vector<std::string> &args = std::vector<std::string>()); bool help(const std::vector<std::string> &args = std::vector<std::string>());
bool start_mining(const std::vector<std::string> &args); bool start_mining(const std::vector<std::string> &args);
bool stop_mining(const std::vector<std::string> &args); bool stop_mining(const std::vector<std::string> &args);
@ -229,5 +232,11 @@ namespace cryptonote
std::unique_ptr<tools::wallet2> m_wallet; std::unique_ptr<tools::wallet2> m_wallet;
epee::net_utils::http::http_simple_client m_http_client; epee::net_utils::http::http_simple_client m_http_client;
refresh_progress_reporter_t m_refresh_progress_reporter; refresh_progress_reporter_t m_refresh_progress_reporter;
std::atomic<bool> m_auto_refresh_run;
bool m_auto_refresh_refreshing;
std::thread m_auto_refresh_thread;
std::mutex m_auto_refresh_mutex;
std::condition_variable m_auto_refresh_cond;
}; };
} }

View File

@ -744,6 +744,9 @@ bool wallet2::store_keys(const std::string& keys_file_name, const std::string& p
value2.SetUint(m_default_mixin); value2.SetUint(m_default_mixin);
json.AddMember("default_mixin", value2, json.GetAllocator()); json.AddMember("default_mixin", value2, json.GetAllocator());
value2.SetInt(m_auto_refresh ? 1 :0);
json.AddMember("auto_refresh", value2, json.GetAllocator());
// Serialize the JSON object // Serialize the JSON object
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
@ -828,6 +831,7 @@ void wallet2::load_keys(const std::string& keys_file_name, const std::string& pa
m_store_tx_info = (json.HasMember("store_tx_keys") && (json["store_tx_keys"].GetInt() != 0)) m_store_tx_info = (json.HasMember("store_tx_keys") && (json["store_tx_keys"].GetInt() != 0))
|| (json.HasMember("store_tx_info") && (json["store_tx_info"].GetInt() != 0)); || (json.HasMember("store_tx_info") && (json["store_tx_info"].GetInt() != 0));
m_default_mixin = json.HasMember("default_mixin") ? json["default_mixin"].GetUint() : 0; m_default_mixin = json.HasMember("default_mixin") ? json["default_mixin"].GetUint() : 0;
m_auto_refresh = json.HasMember("auto_refresh") && (json["auto_refresh"].GetInt() != 0);
} }
const cryptonote::account_keys& keys = m_account.get_keys(); const cryptonote::account_keys& keys = m_account.get_keys();

View File

@ -87,10 +87,10 @@ namespace tools
}; };
private: private:
wallet2(const wallet2&) : m_run(true), m_callback(0), m_testnet(false), m_always_confirm_transfers (false), m_store_tx_info(true), m_default_mixin(0), m_refresh_type(RefreshOptimizeCoinbase) {} wallet2(const wallet2&) : m_run(true), m_callback(0), m_testnet(false), m_always_confirm_transfers (false), m_store_tx_info(true), m_default_mixin(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true) {}
public: public:
wallet2(bool testnet = false, bool restricted = false) : m_run(true), m_callback(0), m_testnet(testnet), m_restricted(restricted), is_old_file_format(false), m_store_tx_info(true), m_default_mixin(0), m_refresh_type(RefreshOptimizeCoinbase) {} wallet2(bool testnet = false, bool restricted = false) : m_run(true), m_callback(0), m_testnet(testnet), m_restricted(restricted), is_old_file_format(false), m_store_tx_info(true), m_default_mixin(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true) {}
struct transfer_details struct transfer_details
{ {
uint64_t m_block_height; uint64_t m_block_height;
@ -330,6 +330,8 @@ namespace tools
void store_tx_info(bool store) { m_store_tx_info = store; } void store_tx_info(bool store) { m_store_tx_info = store; }
uint32_t default_mixin() const { return m_default_mixin; } uint32_t default_mixin() const { return m_default_mixin; }
void default_mixin(uint32_t m) { m_default_mixin = m; } void default_mixin(uint32_t m) { m_default_mixin = m; }
bool auto_refresh() const { return m_auto_refresh; }
void auto_refresh(bool r) { m_auto_refresh = r; }
bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const; bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const;
@ -397,6 +399,7 @@ namespace tools
bool m_store_tx_info; /*!< request txkey to be returned in RPC, and store in the wallet cache file */ bool m_store_tx_info; /*!< request txkey to be returned in RPC, and store in the wallet cache file */
uint32_t m_default_mixin; uint32_t m_default_mixin;
RefreshType m_refresh_type; RefreshType m_refresh_type;
bool m_auto_refresh;
}; };
} }
BOOST_CLASS_VERSION(tools::wallet2, 10) BOOST_CLASS_VERSION(tools::wallet2, 10)