From e23d51bc1683e4aa369ba0f2889b96113eeb4e3a Mon Sep 17 00:00:00 2001 From: Justin Berman Date: Thu, 26 Jun 2025 17:32:12 +0000 Subject: wallet: improve lookahead logic & make rpc persistent --- src/wallet/wallet2.cpp | 51 ++++++++++++++++++---------- src/wallet/wallet_rpc_server.cpp | 21 +++++++++--- src/wallet/wallet_rpc_server_commands_defs.h | 2 ++ 3 files changed, 52 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 808a6fdc2..64dfc2d3b 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -1965,26 +1965,41 @@ void wallet2::set_subaddress_lookahead(size_t major, size_t minor) THROW_WALLET_EXCEPTION_IF(minor == 0, error::wallet_internal_error, "Subaddress minor lookahead may not be zero"); THROW_WALLET_EXCEPTION_IF(minor > 0xffffffff, error::wallet_internal_error, "Subaddress minor lookahead is too large"); - if (major > m_subaddress_lookahead_major) { // if increasing the lookahead - // then generate new subaddress pubkeys and add them to m_subaddresses table - for (uint32_t i = m_subaddress_labels.size()+m_subaddress_lookahead_major; i < m_subaddress_labels.size()+major; i++) { // m_subaddress_labels are the accounts the user is conciously keeping track of. We want that number plus the lookahead major accounts in our key table - for (uint32_t j = 0; j < minor; j++) { // these are newly made accounts, minor index will start from zero - cryptonote::subaddress_index idx = {i,j}; - create_one_off_subaddress(idx); // then generate the key and add it to the table - } - } - } - if (minor > m_subaddress_lookahead_minor) { // if increasing the minor lookahead we need to also go back and expand the existing accounts - for (uint32_t i = 0; i < m_subaddress_labels.size()+m_subaddress_lookahead_major; i++) { - uint32_t minor_idx_start = i < m_subaddress_labels.size() ? m_subaddress_labels[i].size()+m_subaddress_lookahead_minor : m_subaddress_lookahead_minor; // if there are existing minor indices being tracked under this account we need to account for that - for (uint32_t j = minor_idx_start; j < minor; j++) { - cryptonote::subaddress_index idx = {i,j}; - create_one_off_subaddress(idx); - } - } - } + const uint32_t old_major_lookahead = m_subaddress_lookahead_major; + const uint32_t old_minor_lookahead = m_subaddress_lookahead_minor; + m_subaddress_lookahead_major = major; m_subaddress_lookahead_minor = minor; + + if (old_major_lookahead >= major && old_minor_lookahead >= minor) + return; + + // Expand the subaddresses map so that outputs received to the higher lookaheads will be identified in the scan loop + hw::device &hwdev = m_account.get_device(); + cryptonote::subaddress_index index2; + const uint32_t max_major_idx = this->get_num_subaddress_accounts() > 0 ? (this->get_num_subaddress_accounts() - 1) : 0; + const uint32_t major_end = get_subaddress_clamped_sum(max_major_idx, major); + for (index2.major = 0; index2.major < major_end; ++index2.major) + { + // The existing minor addresses already set for this account + const uint32_t n_minor_subaddrs = this->get_num_subaddresses(index2.major); + + // The subaddress lookahead is expected to expand from the max index in expand_subaddresses + const uint32_t max_minor_idx = n_minor_subaddrs > 0 ? (n_minor_subaddrs - 1) : 0; + const uint32_t begin = (n_minor_subaddrs || index2.major < old_major_lookahead) ? get_subaddress_clamped_sum(max_minor_idx, old_minor_lookahead) : 0; + // The expected new n minor subaddresses allocated for this account + const uint32_t end = get_subaddress_clamped_sum(max_minor_idx, minor); + + if (begin >= end) + continue; + + const std::vector pkeys = hwdev.get_subaddress_spend_public_keys(m_account.get_keys(), index2.major, begin, end); + for (index2.minor = begin; index2.minor < end; ++index2.minor) + { + const crypto::public_key &D = pkeys.at(index2.minor - begin); + m_subaddresses[D] = index2; + } + } } //---------------------------------------------------------------------------------------------------- /*! diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index 37e0968b8..15ca1500e 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -653,11 +653,24 @@ namespace tools bool wallet_rpc_server::on_set_subaddr_lookahead(const wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD::request& req, wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD::response& res, epee::json_rpc::error& er, const connection_context *ctx) { if (!m_wallet) return not_open(er); - try { - m_wallet->set_subaddress_lookahead(req.major_idx, req.minor_idx); + CHECK_IF_BACKGROUND_SYNCING(); + const std::string wallet_file = m_wallet->get_wallet_file(); + if (wallet_file == "" || m_wallet->verify_password(req.password)) + { + try + { + m_wallet->set_subaddress_lookahead(req.major_idx, req.minor_idx); + m_wallet->rewrite(wallet_file, req.password); + } + catch (const std::exception& e) { + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); + return false; + } } - catch (const std::exception& e) { - handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); + else + { + er.code = WALLET_RPC_ERROR_CODE_INVALID_PASSWORD; + er.message = "Invalid password."; return false; } return true; diff --git a/src/wallet/wallet_rpc_server_commands_defs.h b/src/wallet/wallet_rpc_server_commands_defs.h index 4a11845d4..ebcd1135c 100644 --- a/src/wallet/wallet_rpc_server_commands_defs.h +++ b/src/wallet/wallet_rpc_server_commands_defs.h @@ -186,9 +186,11 @@ namespace wallet_rpc { struct request_t { + std::string password; uint32_t major_idx; uint32_t minor_idx; BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(password) KV_SERIALIZE(major_idx) KV_SERIALIZE(minor_idx) END_KV_SERIALIZE_MAP() -- cgit v1.2.3