From 38f44770b83ce36b528d79b497ca4c02f9c03ad2 Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Sat, 19 Apr 2025 23:25:22 -0400 Subject: [PATCH] Fixed several outstanding bugs in subaddresses. startRefresh needs work. --- src/backend.cpp | 216 ++++++++++++++++++------------------- src/backend.h | 11 +- src/subaddress_account.cpp | 11 +- src/subaddress_account.h | 4 +- src/subaddress_minor.cpp | 21 ++-- src/subaddress_minor.h | 4 +- src/wallet.cpp | 103 ++++++++++++++---- src/wallet.h | 8 ++ 8 files changed, 223 insertions(+), 155 deletions(-) diff --git a/src/backend.cpp b/src/backend.cpp index b936c8e..99c1849 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -147,7 +147,7 @@ namespace lwsf { namespace internal { namespace backend // do not store server lookahead, reset on each server connection wire::object(format, wire::optional_field("detail", wire::trusted_array(std::ref(self.detail))), - WIRE_FIELD_DEFAULTED(used, 0) + WIRE_FIELD_DEFAULTED(last, 0) ); } @@ -318,7 +318,7 @@ namespace lwsf { namespace internal { namespace backend { if (self.subaccounts.size() <= sub.maj_i) return true; - return self.subaccounts[sub.maj_i].used <= sub.min_i; + return self.subaccounts[sub.maj_i].last < sub.min_i; // last is inclusive } std::optional> update_tx(const account& self, transaction& out, const rpc::transaction& source) @@ -508,9 +508,9 @@ namespace lwsf { namespace internal { namespace backend throw std::runtime_error{"merge_response exceeded max size_t value"}; if (self.primary.subaccounts.size() <= sub.key) self.primary.subaccounts.resize(std::size_t(sub.key) + 1); - + auto& acct = self.primary.subaccounts.at(sub.key); - acct.used = std::max(acct.used, add_uint32_clamp(unsigned(1), std::get<1>(sub.head))); + acct.last = std::max(acct.last, std::get<1>(sub.head)); } // Update txes _after_ subaccounts table @@ -537,11 +537,13 @@ namespace lwsf { namespace internal { namespace backend throw std::logic_error{"update_lookaheads does not work with lookahead {0, 0}"}; self.server_lookahead = {error::subaddr_server}; + static_assert(std::is_samekey)>()); const std::uint32_t server_lookahead = all.empty() ? std::uint32_t(0) : all.rbegin()->key; // Ensure major lookahead starts at zero, has no gaps, and "far" enough. - bool min_met = add_uint32_clamp(self.primary.subaccounts.size() - 1, maj_lookahead) <= server_lookahead; + const std::size_t last_account = std::max(self.primary.subaccounts.size(), std::size_t(1)) - 1; + bool min_met = add_uint32_clamp(last_account, maj_lookahead) <= server_lookahead; min_met &= all.size() == std::uint64_t(server_lookahead) + 1; for (std::size_t i = 0; i < self.primary.subaccounts.size(); ++i) @@ -551,7 +553,7 @@ namespace lwsf { namespace internal { namespace backend const bool has_range = elem != all.end(); const std::uint32_t lookahead = has_range ? std::get<1>(elem->head) : 0; - min_met &= add_uint32_clamp(acct.used, min_lookahead) <= lookahead; + min_met &= add_uint32_clamp(acct.last, min_lookahead) <= lookahead; min_met &= has_range && std::get<0>(elem->head) == 0; // Only increase lookahead, server has no delete subaddr command @@ -559,13 +561,14 @@ namespace lwsf { namespace internal { namespace backend } // Ensure that unused minor lookahead is far enough + const std::uint32_t last_minor = std::max(min_lookahead, std::uint32_t(1)) - 1; for (std::size_t i = self.primary.subaccounts.size(); i < all.size(); ++i) { if (!min_met) break; const auto& range = all.nth(i)->head; - min_met &= min_lookahead <= std::get<1>(range); + min_met &= last_minor <= std::get<1>(range); min_met &= std::get<0>(range) == 0; } @@ -577,15 +580,20 @@ namespace lwsf { namespace internal { namespace backend void fill_upsert(const wallet& self, boost::container::flat_set>& out) { static constexpr const std::uint32_t max_index = - std::numeric_limits::max(); + std::numeric_limits::max(); + + // remember upsert_subaddrs is inclusive in the minor ranges const auto& accts = self.primary.subaccounts; - const std::uint32_t minor = self.primary.min_lookahead; for (std::size_t i = 0; i < accts.size() && i <= max_index; ++i) - out.insert(out.end(), rpc::subaddrs{std::uint32_t(i)})->head = {0, add_uint32_clamp(accts[i].used, minor)}; + out.insert(out.end(), rpc::subaddrs{std::uint32_t(i)})->head = {0, add_uint32_clamp(accts[i].last, self.primary.min_lookahead)}; - const std::uint32_t major = add_uint32_clamp(accts.size(), self.primary.maj_lookahead); - for (std::size_t i = accts.size(); i < major; ++i) + /* `0 < maj_lookahead & min_lookahead == 0` is a strange edge case. + Assume `min_lookahead == 1` in that scenario. */ + + const std::uint32_t minor = std::max(std::uint32_t(1), self.primary.min_lookahead) - 1; + const std::size_t end = accts.size() + self.primary.maj_lookahead; + for (std::size_t i = accts.size(); i < end && i <= max_index; ++i) out.insert(out.end(), rpc::subaddrs{std::uint32_t(i)})->head = {0, minor}; } @@ -605,7 +613,7 @@ namespace lwsf { namespace internal { namespace backend { // Enforce special account {0,0} exists and is labeled self.detail.try_emplace(0).first->second.label = std::string{config::default_primary_name}; - self.used = 1; + self.last = 0; self.server_lookahead = 0; } } // anonymous @@ -631,7 +639,7 @@ namespace lwsf { namespace internal { namespace backend { map_account(dest, source); } sub_account::sub_account() - : detail(), used(1), server_lookahead(0) + : detail(), last(0), server_lookahead(0) {} std::string_view sub_account::sub_label(const std::uint32_t minor) const noexcept @@ -684,8 +692,9 @@ namespace lwsf { namespace internal { namespace backend : listener(nullptr), client(), primary{}, + refresh_status(), server_lookahead(default_subaddr_state), - last_sync(0), + last_sync(), blockchain_height(0), per_byte_fee(0), fee_mask(0), @@ -834,40 +843,18 @@ namespace lwsf { namespace internal { namespace backend { // Remember that this function provides the strong exception guarantee. + boost::unique_lock lock_refresh{sync_refresh}; + const auto now = std::chrono::steady_clock::now(); + if (!mandatory) { - const auto now = std::chrono::steady_clock::now(); - const std::chrono::steady_clock::time_point start{ - std::chrono::steady_clock::time_point::duration{last_sync.load()} - }; - if (now - start < config::refresh_interval_min) - return {}; - } - - boost::unique_lock lock_refresh{sync_refresh}; - boost::unique_lock lock{sync}; - const auto now = std::chrono::steady_clock::now(); - const auto get_rc = [this, &lock] () -> std::error_code - { - if (!lock.owns_lock()) - lock.lock(); - if (!primary.generated_locally && (primary.maj_lookahead || primary.min_lookahead)) - return server_lookahead.error(); - return {}; - }; - - if (!mandatory) // double-check - { - const std::chrono::steady_clock::time_point start{ - std::chrono::steady_clock::time_point::duration{last_sync.load()} - }; - if (now - start < config::refresh_interval_min) - return get_rc(); + if (now - last_sync < config::refresh_interval_min) + return refresh_status; } struct call_refreshed { - void operator()(wallet* ptr) const noexcept + void operator()(wallet* ptr) const { if (!ptr) return; const boost::lock_guard lock{ptr->sync_listener}; @@ -877,6 +864,8 @@ namespace lwsf { namespace internal { namespace backend }; std::unique_ptr refresh_on_exit{this}; + + boost::unique_lock lock{sync}; const bool try_login = !passed_login; const rpc::login login{primary.address, primary.view.sec}; @@ -887,8 +876,8 @@ namespace lwsf { namespace internal { namespace backend if (failed_login) { lock.lock(); - last_sync = now.time_since_epoch().count(); - return failed_login; + last_sync = now; + return refresh_status = failed_login; } } @@ -901,17 +890,17 @@ namespace lwsf { namespace internal { namespace backend } lock.lock(); - last_sync = now.time_since_epoch().count(); + last_sync = now; passed_login = bool(txs_response) && bool(outs_response); // reset state if (!txs_response) { if (txs_response == rpc_unapproved) - return {error::approval}; - return txs_response.error(); + return refresh_status = {error::approval}; + return refresh_status = txs_response.error(); } if (!outs_response) - return outs_response.error(); + return refresh_status = outs_response.error(); const std::uint64_t orig_scan_height = primary.scan_height; auto merged = merge_response(*this, *txs_response, *outs_response); @@ -945,9 +934,10 @@ namespace lwsf { namespace internal { namespace backend if (primary.maj_lookahead) { - const std::uint32_t maj_i = add_uint32_clamp(unsigned(1), upsert_request.subaddrs_.rbegin()->key); + const std::uint32_t last_used = upsert_request.subaddrs_.rbegin()->key; + const std::uint32_t maj_i = add_uint32_clamp(unsigned(1), last_used); const std::uint32_t maj_count = primary.maj_lookahead; - const std::uint32_t min_count = add_uint32_clamp(unsigned(1), primary.min_lookahead); + const std::uint32_t min_count = std::max(std::uint32_t(1), primary.min_lookahead); const rpc::provision_subaddrs_request provision_request{ login, maj_i, 0, maj_count, min_count, false /* get_all */ }; @@ -959,22 +949,21 @@ namespace lwsf { namespace internal { namespace backend if (provision_response) { if (server_lookahead) // in case another thread jumped in - server_lookahead = std::max(*server_lookahead, add_uint32_clamp(maj_i, maj_count - 1)); + server_lookahead = std::max(*server_lookahead, add_uint32_clamp(last_used, maj_count)); } else // if !provision_response server_lookahead = handle_lookahead_error(provision_response.error()); } } else // if !upsert_response - server_lookahead = handle_lookahead_error(upsert_response.error()); + server_lookahead = handle_lookahead_error(upsert_response.error()); } // if new subaddress(es) // return error if subaddresses enabled, and recovered wallet - const std::error_code rc = get_rc(); refresh_on_exit.release(); // release before acquiring `sync_listener`. const boost::lock_guard lock_listener{sync_listener}; if (!listener) - return rc; + return refresh_status = server_lookahead.error(); // Call listener functions without holding `sync`, in case a call is made // back into the library. @@ -1004,94 +993,88 @@ namespace lwsf { namespace internal { namespace backend listener->moneySpent(txid, tx->amount); } - return rc; + return refresh_status = server_lookahead.error(); } - std::error_code wallet::add_subaccount(std::string label) + std::error_code wallet::register_subaccount(const std::uint32_t maj_i) { boost::unique_lock lock{sync}; - if (std::numeric_limits::max() < primary.subaccounts.size()) - throw std::runtime_error{"wallet::add_subaccount exceeded subaddress indexes"}; - - std::uint32_t maj_lookahead = 0; - const std::uint32_t minor_count = add_uint32_clamp(unsigned(1), primary.min_lookahead); - for (;;) + if (primary.subaccounts.size() <= maj_i) + throw std::runtime_error{"wallet::register_subaccount exceeded subaddress indexes"}; + + if (!passed_login) { - if (!server_lookahead) - break; - - maj_lookahead = *server_lookahead; - const std::uint32_t maj_i = primary.subaccounts.size(); - const std::uint32_t needed_maj_lookahead = add_uint32_clamp(maj_i, primary.maj_lookahead); - if (needed_maj_lookahead <= maj_lookahead) - break; - - // Increase the lookahead by one - const rpc::provision_subaddrs_request request{ - rpc::login{primary.address, primary.view.sec}, - needed_maj_lookahead, 0, 1, minor_count, false /* get_all */ - }; - lock.unlock(); - const auto response = rpc::invoke(client, request); + const std::error_code error = login(); + if (error) + return error; lock.lock(); - if (!response) - server_lookahead = handle_lookahead_error(response.error()); - - // if NOT another call to `add_subaccount` during rpc unlock phase - if (maj_i == primary.subaccounts.size()) - { - if (response) - server_lookahead = needed_maj_lookahead; - break; - } } - // Allow account to be added if server previously had enough lookahead - if (primary.subaccounts.size() <= maj_lookahead) - { - auto& maj = primary.subaccounts.emplace_back(); - maj.detail.try_emplace(0).first->second.label = std::move(label); - maj.server_lookahead = minor_count - 1; - } + if (!server_lookahead) + return server_lookahead.error(); - if (server_lookahead) + const std::uint32_t needed_maj_i = add_uint32_clamp(maj_i, primary.maj_lookahead); + if (needed_maj_i <= *server_lookahead) return {}; + + // Increase the lookahead by one + const std::uint32_t major_count = needed_maj_i - *server_lookahead; + const std::uint32_t minor_count = std::max(std::uint32_t(1), primary.min_lookahead); + const rpc::provision_subaddrs_request request{ + rpc::login{primary.address, primary.view.sec}, + *server_lookahead + 1, 0, major_count, minor_count, false /* get_all */ + }; + + lock.unlock(); + const auto response = rpc::invoke(client, request); + lock.lock(); + if (response && server_lookahead) + server_lookahead = std::max(*server_lookahead, needed_maj_i); + else if (!response) + server_lookahead = handle_lookahead_error(response.error()); + return server_lookahead.error(); } - std::error_code wallet::add_subaddress(const std::uint32_t accountIndex, std::string label) + std::error_code wallet::register_subaddress(const std::uint32_t maj_i, const std::uint32_t min_i) { boost::unique_lock lock{sync}; - auto maj = std::addressof(primary.subaccounts.at(accountIndex)); - if (std::numeric_limits::max() <= maj->used) - throw std::runtime_error{"wallet::add_subaddress exceeded subaddress indexes"}; + if (primary.subaccounts.size() <= maj_i) + throw std::invalid_argument{"wallet::register_subaddress exceeded major index"}; - const std::uint32_t min_i = ++maj->used; // exceptions could leak an index :/ - const std::uint32_t needed_min_lookahead = add_uint32_clamp(min_i, primary.min_lookahead); + auto maj = std::addressof(primary.subaccounts.at(maj_i)); + if (maj->last < min_i) + throw std::invalid_argument{"wallet::register_subaddress exceeded minor index"}; - if (needed_min_lookahead <= maj->server_lookahead) + if (!passed_login) { - maj->detail.try_emplace(min_i).first->second.label = std::move(label); - return {}; + lock.unlock(); + const std::error_code error = login(); + if (error) + return error; + lock.lock(); } + const std::uint32_t needed_min_i = add_uint32_clamp(min_i, primary.min_lookahead); + if (needed_min_i <= maj->server_lookahead) + return {}; + + const std::uint32_t needed_count = add_uint32_clamp(unsigned(1), needed_min_i); const rpc::provision_subaddrs_request request{ rpc::login{primary.address, primary.view.sec}, - accountIndex, 0, 1, needed_min_lookahead, false /* get_all */ + maj_i, 0, 1, needed_count, false /* get_all */ }; lock.unlock(); const auto response = rpc::invoke(client, request); lock.lock(); - // address could've changed during unlock - maj = std::addressof(primary.subaccounts.at(accountIndex)); - if (response) { - maj->server_lookahead = std::max(maj->server_lookahead, needed_min_lookahead); - maj->detail.try_emplace(min_i - 1).first->second.label = std::move(label); + // address could've changed during unlock + maj = std::addressof(primary.subaccounts.at(maj_i)); + maj->server_lookahead = std::max(maj->server_lookahead, needed_min_i); return {}; } else if (server_lookahead) @@ -1116,6 +1099,15 @@ namespace lwsf { namespace internal { namespace backend return {}; } + if (!passed_login) + { + lock.unlock(); + const std::error_code error = login(); + if (error) + return error; + lock.lock(); + } + rpc::upsert_subaddrs_request request{ rpc::login{primary.address, primary.view.sec}, {}, false /* get_all */ }; diff --git a/src/backend.h b/src/backend.h index 898352b..7e0dace 100644 --- a/src/backend.h +++ b/src/backend.h @@ -96,7 +96,7 @@ namespace lwsf { namespace internal { namespace backend struct sub_account { boost::container::flat_map detail; //!< Minor address info - std::uint32_t used; //!< Number of minor indexes in use. + std::uint32_t last; //!< Last minor index in use (inclusive) std::uint32_t server_lookahead; //!< Status of server (minor) lookahead. Inclusive //! Creates 1 subaddress entry (key == `0`) with default label @@ -222,8 +222,9 @@ namespace lwsf { namespace internal { namespace backend Monero::WalletListener* listener; rpc::http_client client; account primary; + std::error_code refresh_status; //!< Cached because `refresh(...)` is rate limited expect server_lookahead; //!< Status of major lookahead server-side. Inclusive - std::atomic last_sync; + std::chrono::steady_clock::time_point last_sync; std::uint64_t blockchain_height; std::uint64_t per_byte_fee; std::uint64_t fee_mask; @@ -250,9 +251,11 @@ namespace lwsf { namespace internal { namespace backend //! Refreshes txes information. Strong exception guarantee. Spuriously returns success. std::error_code refresh(bool mandatory = false); - std::error_code add_subaccount(std::string label); + //! Notify server that new major accounts need to be watched. + std::error_code register_subaccount(std::uint32_t maj_i); - std::error_code add_subaddress(const std::uint32_t accountIndex, std::string label); + //! Notify server that new minor accounts need to be watched. + std::error_code register_subaddress(std::uint32_t maj_i, std::uint32_t min_i); std::error_code set_lookahead(std::uint32_t major, std::uint32_t minor); diff --git a/src/subaddress_account.cpp b/src/subaddress_account.cpp index 81febf8..345a75d 100644 --- a/src/subaddress_account.cpp +++ b/src/subaddress_account.cpp @@ -66,10 +66,10 @@ namespace lwsf { namespace internal } } - subaddress_account::subaddress_account(std::shared_ptr data) - : data_(std::move(data)), rows_() + subaddress_account::subaddress_account(internal::wallet* wal, std::shared_ptr data) + : wal_(wal), data_(std::move(data)), rows_() { - if (!data_) + if (!wal_ || !data_) throw std::invalid_argument{"lwsf::internal::subaddress_account cannot be given nullptr"}; } @@ -80,7 +80,8 @@ namespace lwsf { namespace internal void subaddress_account::addRow(const std::string &label) { - data_->add_subaccount(label); + // do not call `data_` function directly, put API requests onto work thread + wal_->addSubaddressAccount(label); refresh(); } @@ -88,7 +89,7 @@ namespace lwsf { namespace internal { { const boost::lock_guard lock{data_->sync}; - data_->primary.subaccounts.at(accountIndex).detail[0].label = label; + data_->primary.subaccounts.at(accountIndex).detail.try_emplace(0).first->second.label = label; } refresh(); } diff --git a/src/subaddress_account.h b/src/subaddress_account.h index 237ff31..d92ece2 100644 --- a/src/subaddress_account.h +++ b/src/subaddress_account.h @@ -31,6 +31,7 @@ #include #include #include +#include "wallet.h" #include "wallet/api/wallet2_api.h" // monero/src namespace lwsf { namespace internal @@ -39,12 +40,13 @@ namespace lwsf { namespace internal class subaddress_account final : public ::Monero::SubaddressAccount { + internal::wallet* const wal_; const std::shared_ptr data_; std::vector rows_; public: - explicit subaddress_account(std::shared_ptr data); + explicit subaddress_account(internal::wallet* wal, std::shared_ptr data); subaddress_account(const subaddress_account&) = delete; subaddress_account(subaddress_account&&) = delete; diff --git a/src/subaddress_minor.cpp b/src/subaddress_minor.cpp index 9e23e2d..eb18cdb 100644 --- a/src/subaddress_minor.cpp +++ b/src/subaddress_minor.cpp @@ -46,10 +46,10 @@ namespace lwsf { namespace internal } } - subaddress_minor::subaddress_minor(std::shared_ptr data) - : data_(std::move(data)), rows_() + subaddress_minor::subaddress_minor(internal::wallet* wal, std::shared_ptr data) + : wal_(wal), data_(std::move(data)), rows_() { - if (!data_) + if (!wal_ || !data_) throw std::invalid_argument{"lwsf::internal::subaddress_minor cannot be given nullptr"}; } @@ -69,14 +69,14 @@ namespace lwsf { namespace internal return; const auto& acct = data_->primary.subaccounts[accountIndex]; - static_assert(std::is_same()); - rows_.reserve(acct.used); - for (std::uint32_t i = 0; i < acct.used; ++i) + static_assert(std::is_same()); + rows_.reserve(std::size_t(acct.last) + 1); + for (std::uint64_t i = 0; i <= acct.last; ++i) { rows_.push_back( new Monero::SubaddressRow{ - i, - data_->get_spend_address({accountIndex, i}), + std::uint32_t(i), + data_->get_spend_address({accountIndex, std::uint32_t(i)}), std::string{acct.sub_label(i)} } ); @@ -90,8 +90,9 @@ namespace lwsf { namespace internal } void subaddress_minor::addRow(const std::uint32_t accountIndex, const std::string &label) - { - data_->add_subaddress(accountIndex, label); + { + // do not call `data_` function directly, put API requests onto work thread + wal_->add_subaddress(accountIndex, label); refresh(accountIndex); } diff --git a/src/subaddress_minor.h b/src/subaddress_minor.h index 761235d..1cd6c9c 100644 --- a/src/subaddress_minor.h +++ b/src/subaddress_minor.h @@ -31,6 +31,7 @@ #include #include #include +#include "wallet.h" #include "wallet/api/wallet2_api.h" // monero/src namespace lwsf { namespace internal @@ -39,12 +40,13 @@ namespace lwsf { namespace internal class subaddress_minor final : public ::Monero::Subaddress { + internal::wallet* const wal_; const std::shared_ptr data_; std::vector rows_; public: - explicit subaddress_minor(std::shared_ptr data); + explicit subaddress_minor(internal::wallet* wal, std::shared_ptr data); subaddress_minor(const subaddress_minor&) = delete; subaddress_minor(subaddress_minor&&) = delete; diff --git a/src/wallet.cpp b/src/wallet.cpp index 8e314ef..a853382 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -348,10 +348,7 @@ namespace lwsf { namespace internal { const boost::lock_guard lock{error_sync_}; if (!update_iff_error || status) - { status_ = status; - exception_error_.clear(); - } return !status_; } @@ -362,6 +359,19 @@ namespace lwsf { namespace internal status_.clear(); } + template + void wallet::queue_work(F&& f) + { + try + { + const boost::lock_guard lock{refresh_sync_}; + work_queue_.push_back(std::forward(f)); + } + catch (const std::exception& e) + { set_critical(e); } + startRefresh(); + } + void wallet::stop_refresh_loop() { const boost::lock_guard lock{thread_sync_}; @@ -386,22 +396,34 @@ namespace lwsf { namespace internal }; try { + std::chrono::steady_clock::time_point last_refresh; boost::unique_lock lock{refresh_sync_}; const std::unique_ptr set_stop{std::addressof(thread_state_)}; - while (mandatory_refresh_ || thread_state_ != state::stop) + while (mandatory_refresh_ || thread_state_ != state::stop || !work_queue_.empty()) { + while (!work_queue_.empty()) + { + const std::function work{std::move(work_queue_.front())}; + work_queue_.pop_front(); + lock.unlock(); + if (work) + set_error(work(), true); + lock.lock(); + } + const bool mandatory_refresh = mandatory_refresh_; mandatory_refresh_ = false; - - if (thread_state_ == state::run || mandatory_refresh) + + const auto now = std::chrono::steady_clock::now(); + if (refresh_interval_ <= now - last_refresh && (thread_state_ == state::run || mandatory_refresh)) { // refresh has strong exception guarantee - never in partial state. lock.unlock(); - set_error(data_->refresh(mandatory_refresh), true); + set_error(data_->refresh(mandatory_refresh), false); lock.lock(); } else if (thread_state_ == state::skip_once) - thread_state_ = state::run; + thread_state_ = state::run; // check while holding lock and before a wait call if (thread_state_ == state::stop) @@ -409,8 +431,8 @@ namespace lwsf { namespace internal const auto last_state = thread_state_; refresh_notify_.wait_for( - lock, to_boost(config::refresh_interval), [this, last_state] () { - return mandatory_refresh_ || thread_state_ != last_state; + lock, to_boost(refresh_interval_), [this, last_state] () { + return mandatory_refresh_ || thread_state_ != last_state || !work_queue_.empty(); }); } } @@ -474,6 +496,7 @@ namespace lwsf { namespace internal subaddress_minor_(), filename_(std::move(filename)), password_(std::move(password)), + work_queue_(), exception_error_(), status_(), thread_(), @@ -505,6 +528,7 @@ namespace lwsf { namespace internal subaddress_minor_(), filename_(std::move(filename)), password_(std::move(password)), + work_queue_(), exception_error_(), status_(), thread_(), @@ -564,6 +588,7 @@ namespace lwsf { namespace internal subaddress_minor_(), filename_(std::move(filename)), password_(std::move(password)), + work_queue_(), exception_error_(), status_(), thread_(), @@ -603,6 +628,7 @@ namespace lwsf { namespace internal subaddress_minor_(), filename_(std::move(filename)), password_(std::move(password)), + work_queue_(), exception_error_(), status_(), thread_(), @@ -651,6 +677,29 @@ namespace lwsf { namespace internal wallet::~wallet() { stop_refresh_loop(); } + void wallet::add_subaddress(const std::uint32_t accountIndex, std::string label) + { + try + { + const boost::lock_guard lock{data_->sync}; + auto& accts = data_->primary.subaccounts; + + if (accts.size() <= accountIndex) + throw std::runtime_error{"add_subaddress: account does not exist"}; + + auto& acct = accts.at(accountIndex); + if (std::numeric_limits::max() <= acct.last) + throw std::runtime_error{"add_subaddress: exceeded minor indexes"}; + + const std::uint32_t min_i = ++acct.last; + acct.detail.try_emplace(min_i).first->second.label = std::move(label); + + queue_work([=] () { return data_->register_subaddress(accountIndex, min_i); }); + } + catch (const std::exception& e) + { set_critical(e); } + } + std::string wallet::seed(const std::string& seed_offset) const { std::string language; @@ -852,10 +901,9 @@ namespace lwsf { namespace internal return true; } - void wallet::setRefreshFromBlockHeight(uint64_t refresh_from_block_height) + void wallet::setRefreshFromBlockHeight(uint64_t height) { - try { set_error(data_->restore_height(refresh_from_block_height), true); } - catch (const std::exception& e) { set_critical(e); } + queue_work([this, height] () { return data_->restore_height(height); }); } uint64_t wallet::getRefreshFromBlockHeight() const @@ -866,8 +914,7 @@ namespace lwsf { namespace internal void wallet::setSubaddressLookahead(uint32_t major, uint32_t minor) { - try { set_error(data_->set_lookahead(major, minor), false); } - catch (const std::exception& e) { set_critical(e); } + queue_work([this, major, minor] () { return data_->set_lookahead(major, minor); }); } bool wallet::connectToDaemon() @@ -1010,7 +1057,7 @@ namespace lwsf { namespace internal if (no_refresh) { thread_state_ = state::stop; - if (!mandatory_refresh_) + if (!mandatory_refresh_ && work_queue_.empty()) return; } @@ -1071,8 +1118,20 @@ namespace lwsf { namespace internal void wallet::addSubaddressAccount(const std::string& label) { - try { set_error(data_->add_subaccount(label), false); } - catch (const std::exception& e) { set_critical(e); } + try + { + const boost::lock_guard lock{data_->sync}; + auto& accts = data_->primary.subaccounts; + + const std::size_t index = accts.size(); + if (std::numeric_limits::max() < index) + throw std::runtime_error{"addSubddressAccount exceeded subaddress indexes"}; + + accts.emplace_back().detail.try_emplace(0).first->second.label = label; + queue_work([this, index] () { return data_->register_subaccount(index); }); + } + catch (const std::exception& e) + { set_critical(e); } } std::size_t wallet::numSubaddressAccounts() const @@ -1086,7 +1145,7 @@ namespace lwsf { namespace internal static_assert(std::numeric_limits::max() <= std::numeric_limits::max()); const boost::lock_guard lock{data_->sync}; if (accountIndex < data_->primary.subaccounts.size()) - return data_->primary.subaccounts.at(accountIndex).used; + return std::size_t(data_->primary.subaccounts.at(accountIndex).last) + 1; set_critical(std::runtime_error{"numSubaddresses failed, " + std::to_string(accountIndex) + " does not exist"}); return 0; } @@ -1108,7 +1167,7 @@ namespace lwsf { namespace internal if (accountIndex < data_->primary.subaccounts.size()) { auto& acct = data_->primary.subaccounts[accountIndex]; - if (addressIndex < acct.used) + if (addressIndex <= acct.last) { acct.detail.try_emplace(addressIndex).first->second.label = label; return; @@ -1163,7 +1222,7 @@ namespace lwsf { namespace internal Monero::Subaddress* wallet::subaddress() { if (!subaddress_minor_) - subaddress_minor_ = std::make_unique(data_); + subaddress_minor_ = std::make_unique(this, data_); return subaddress_minor_.get(); } @@ -1177,7 +1236,7 @@ namespace lwsf { namespace internal Monero::SubaddressAccount* wallet::subaddressAccount() { if (!subaddresses_) - subaddresses_ = std::make_unique(data_); + subaddresses_ = std::make_unique(this, data_); return subaddresses_.get(); } diff --git a/src/wallet.h b/src/wallet.h index 4f08958..478071b 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include #include #include @@ -59,6 +61,7 @@ namespace internal const std::string filename_; std::string password_; std::string language_; + std::deque> work_queue_; mutable std::string exception_error_; mutable std::error_code status_; boost::thread thread_; @@ -75,6 +78,9 @@ namespace internal bool set_error(std::error_code status, bool update_iff_error) const; void set_critical(const std::exception& e) const; + template + void queue_work(F&& f); + void stop_refresh_loop(); void refresh_loop(); @@ -99,6 +105,8 @@ namespace internal wallet& operator=(const wallet&) = delete; wallet& operator=(wallet&&) = delete; + void add_subaddress(std::uint32_t accountIndex, std::string label); + virtual std::string seed(const std::string& seed_offset = "") const override; virtual std::string getSeedLanguage() const override;