aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2015-11-30 00:19:44 +0200
committerRiccardo Spagni <ric@spagni.net>2015-11-30 00:19:47 +0200
commitbc1bc4adb246b08adee2144ce2c7e3784f7899f1 (patch)
tree297f1fde9102ffe8a76f84a9e4e4bdd181224f27 /src
parentMerge pull request #506 (diff)
parentwallet: optional automatic refresh from the daemon (diff)
downloadmonero-bc1bc4adb246b08adee2144ce2c7e3784f7899f1.tar.xz
Merge pull request #507
62e49a5 wallet: optional automatic refresh from the daemon (moneromooo-monero)
Diffstat (limited to 'src')
-rw-r--r--src/simplewallet/simplewallet.cpp113
-rw-r--r--src/simplewallet/simplewallet.h9
-rw-r--r--src/wallet/wallet2.cpp4
-rw-r--r--src/wallet/wallet2.h7
4 files changed, 122 insertions, 11 deletions
diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp
index 2d99b3da0..2ede0f5ee 100644
--- a/src/simplewallet/simplewallet.cpp
+++ b/src/simplewallet/simplewallet.cpp
@@ -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>()*/)
{
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()
: m_daemon_port(0)
, 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("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("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("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("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"));
@@ -462,7 +500,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
{
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;
}
else
@@ -527,6 +565,21 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
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)");
return true;
@@ -1028,6 +1081,12 @@ bool simple_wallet::open_wallet(const string &wallet_file, const std::string& pa
//----------------------------------------------------------------------------------------------------
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();
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)
{
- 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)
{
- message_writer(epee::log_space::console_color_green, false) <<
+ message_writer(epee::log_space::console_color_green, false) << "\r" <<
tr("Height ") << height << ", " <<
tr("transaction ") << get_transaction_hash(tx) << ", " <<
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)
{
- message_writer(epee::log_space::console_color_magenta, false) <<
+ message_writer(epee::log_space::console_color_magenta, false) << "\r" <<
tr("Height ") << height << ", " <<
tr("transaction ") << get_transaction_hash(spend_tx) << ", " <<
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)
{
- message_writer(epee::log_space::console_color_red, true) <<
+ message_writer(epee::log_space::console_color_red, true) << "\r" <<
tr("Height ") << height << ", " <<
tr("transaction ") << get_transaction_hash(tx) << ", " <<
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)
{
+ std::unique_lock<std::mutex> lock(m_auto_refresh_mutex);
+
if (!try_connect_to_daemon())
return true;
@@ -2146,9 +2217,33 @@ bool simple_wallet::show_transfers(const std::vector<std::string> &args_)
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()
{
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 + "]: ", "");
}
//----------------------------------------------------------------------------------------------------
diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h
index 2ad54d4ca..be510ffb2 100644
--- a/src/simplewallet/simplewallet.h
+++ b/src/simplewallet/simplewallet.h
@@ -77,6 +77,8 @@ namespace cryptonote
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 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,
@@ -101,6 +103,7 @@ namespace cryptonote
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_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 start_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;
epee::net_utils::http::http_simple_client m_http_client;
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;
};
}
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp
index 6136480d6..8a172be90 100644
--- a/src/wallet/wallet2.cpp
+++ b/src/wallet/wallet2.cpp
@@ -822,6 +822,9 @@ bool wallet2::store_keys(const std::string& keys_file_name, const std::string& p
value2.SetUint(m_default_mixin);
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
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
@@ -906,6 +909,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))
|| (json.HasMember("store_tx_info") && (json["store_tx_info"].GetInt() != 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();
diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h
index b71697c1f..d6aea182d 100644
--- a/src/wallet/wallet2.h
+++ b/src/wallet/wallet2.h
@@ -87,10 +87,10 @@ namespace tools
};
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:
- 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
{
uint64_t m_block_height;
@@ -330,6 +330,8 @@ namespace tools
void store_tx_info(bool store) { m_store_tx_info = store; }
uint32_t default_mixin() const { return m_default_mixin; }
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;
@@ -401,6 +403,7 @@ namespace tools
bool m_store_tx_info; /*!< request txkey to be returned in RPC, and store in the wallet cache file */
uint32_t m_default_mixin;
RefreshType m_refresh_type;
+ bool m_auto_refresh;
};
}
BOOST_CLASS_VERSION(tools::wallet2, 10)