Find subaddress payments in mempool scan (#305)
What changed, and why it matters
This commit fixes a bug in the Monero Light Wallet Server (LWS) where payments sent to subaddresses were not being detected when scanning the mempool (the pool of pending transactions). Before the fix, users would not see incoming subaddress payments until they were confirmed in a block. The patch also adds safety checks for empty database storage and null readers to prevent crashes.
Deploy the patched version to ensure users receive timely notifications of mempool payments to subaddresses. Review whether the missed-mempool bug could have caused any wallet clients to double-spend or assume non-payment. No emergency mitigation is indicated beyond applying the fix.
Security signals we found
Functional bug in transaction scanning causing missed subaddress payments in mempool
Incorrect cache invalidation when subaddress set changes
Null/empty storage reader hardening added
No direct memory corruption, authentication bypass, or cryptographic weakness visible in diff
Evidence from the diff
The mempool scanner previously only checked the primary account address and did not consult the stored subaddress dictionary when matching transaction outputs. The fix threads a db::storage_reader into mempool::scan_account so it can fetch the account’s subaddresses, then enables subaddress scanning via ownership_test::enable_subaddresses(reader). The address cache is keyed by both address and subaddress set so that adding/removing subaddresses invalidates cached ‘no-match’ txids. A new subaddress_reader constructor borrows an existing storage_reader to avoid nested read transactions. Additional hardening includes storage::none(), an explicit bool operator, and null checks in subaddress_reader.
Changed components
src/mempool.cppsrc/mempool.hsrc/rpc/light_wallet.cppsrc/util/ownership_test.cppsrc/util/ownership_test.hsrc/db/storage.hInspect captured patch +242 / −25
### src/db/storage.h
@@ -223,6 +223,12 @@ namespace db
//! \return A copy of the LMDB environment, but not reusable txn/cursors.
storage clone() const noexcept;
+ //! \return Storage without an LMDB environment, like a moved-from object.
+ static storage none() noexcept { return storage{nullptr}; }
+
+ //! \return False if `this` has no LMDB environment.
+ explicit operator bool() const noexcept { return bool(db); }
+
//! Rollback chain and accounts to `height`.
expect<void> rollback(block_id height);
### src/mempool.cpp
@@ -32,6 +32,7 @@
#include <boost/thread/lock_guard.hpp>
#include "common/error.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
+#include "db/storage.h"
#include "error.h"
#include "misc_log_ex.h"
#include "rpc/daemon_messages.h" // external/monero/src
@@ -66,19 +67,20 @@ namespace lws
state_ = std::move(txs);
}
- std::unordered_set<crypto::hash> mempool::address_cache::get(const db::account_address& address) const
+ std::unordered_set<crypto::hash> mempool::address_cache::get(const db::account_address& address, const std::vector<db::subaddress_dict>& subaddrs) const
{
auto it = map_.find(address);
- if (it == map_.end())
+ if (it == map_.end() || it->second.subaddrs != subaddrs)
return {};
return {it->second.txids.begin(), it->second.txids.end()};
}
- void mempool::address_cache::set(const db::account_address& address, std::vector<crypto::hash>&& txids)
+ void mempool::address_cache::set(const db::account_address& address, std::vector<db::subaddress_dict>&& subaddrs, std::vector<crypto::hash>&& txids)
{
// find or create entry for this address
auto status = map_.emplace(address, entry{});
entry& e = status.first->second;
+ e.subaddrs = std::move(subaddrs);
e.txids = std::move(txids);
if (status.second)
{
@@ -99,13 +101,22 @@ namespace lws
order_.splice(order_.begin(), order_, e.order);
}
- std::vector<found_pool_tx> mempool::scan_account(lws::account& user) const
+ std::vector<found_pool_tx> mempool::scan_account(lws::account& user, db::storage_reader& reader) const
{
+ std::vector<db::subaddress_dict> subaddrs;
+ {
+ auto read = reader.get_subaddresses(user.id());
+ if (read)
+ subaddrs = std::move(*read);
+ else
+ MERROR("Failed to read subaddresses of " << user.address() << ": " << read.error().message());
+ }
+
std::unordered_set<crypto::hash> skip;
std::shared_ptr<const pool_snapshot> snapshot;
{
const boost::lock_guard<boost::mutex> lock{mutex_};
- skip = cache_.get(user.db_address());
+ skip = cache_.get(user.db_address(), subaddrs);
if (!snapshot_)
{
@@ -133,6 +144,8 @@ namespace lws
found.back().outputs.push_back(std::move(output));
}
};
+ // The caller has a read txn open, and a second one on this thread fails
+ scan_transaction.enable_subaddresses(reader);
// uint64::max is for txpool
static const std::vector<std::uint64_t> fake_outs(
@@ -156,7 +169,7 @@ namespace lws
{
const boost::lock_guard<boost::mutex> lock{mutex_};
- cache_.set(user.db_address(), std::move(skipped));
+ cache_.set(user.db_address(), std::move(subaddrs), std::move(skipped));
}
return found;
### src/mempool.h
@@ -81,20 +81,22 @@ namespace lws
void reset_txs(pool_table&&);
//! Scans the pool for transactions belonging to an address. Thread-safe.
- std::vector<found_pool_tx> scan_account(lws::account& user) const;
+ std::vector<found_pool_tx> scan_account(lws::account& user, db::storage_reader& reader) const;
private:
//! Remembers which txid's that do *not* belong to an address,
// since there is no need for the scanner to revisit these.
+ // A change of the subaddresses makes the entry not valid.
class address_cache
{
public:
- std::unordered_set<crypto::hash> get(const db::account_address& address) const;
- void set(const db::account_address& address, std::vector<crypto::hash>&& txids);
+ std::unordered_set<crypto::hash> get(const db::account_address& address, const std::vector<db::subaddress_dict>& subaddrs) const;
+ void set(const db::account_address& address, std::vector<db::subaddress_dict>&& subaddrs, std::vector<crypto::hash>&& txids);
private:
struct entry
{
+ std::vector<db::subaddress_dict> subaddrs;
std::vector<crypto::hash> txids;
std::list<db::account_address>::iterator order;
};
### src/rpc/light_wallet.cpp
@@ -632,11 +632,11 @@ namespace lws
vec_lmdb_<T> vec_lmdb(const std::vector<T>& src) { return {src.begin(), src.end()}; }
template<typename T, typename U>
- std::pair<std::vector<rpc::get_transaction>, std::uint64_t> merge_into_txes(T output, U spend, db::account const* const user, mempool const* const pool, const std::size_t reserve, const bool all_outputs, const bool skip_spend_meta)
+ std::pair<std::vector<rpc::get_transaction>, std::uint64_t> merge_into_txes(T output, U spend, db::account const* const user, db::storage_reader* const reader, mempool const* const pool, const std::size_t reserve, const bool all_outputs, const bool skip_spend_meta)
{
// merge input and output info into a single set of txes.
- const bool has_pool = (user && pool);
+ const bool has_pool = (user && reader && pool);
std::uint64_t total = 0;
std::vector<rpc::get_transaction> out{};
std::vector<db::output::spend_meta_> metas{};
@@ -746,7 +746,7 @@ namespace lws
// Add mempool transactions. Order is not important, since
// mempool txs cannot depend on each other.
lws::account full_user{*user, std::move(receives), {}};
- const auto pool_txs = pool->scan_account(full_user);
+ const auto pool_txs = pool->scan_account(full_user, *reader);
for (const auto& row: pool_txs)
{
// mempool update after block could be delayed
@@ -816,7 +816,7 @@ namespace lws
{
std::sort(outputs.begin(), outputs.end(), by_tx_link);
std::sort(spends.begin(), spends.end(), by_tx_link);
- return merge_into_txes(vec_lmdb(outputs), vec_lmdb(spends), nullptr, nullptr, outputs.size(), true, true).first;
+ return merge_into_txes(vec_lmdb(outputs), vec_lmdb(spends), nullptr, nullptr, nullptr, outputs.size(), true, true).first;
}
expect<rpc::get_address_txs_response> rpc::get_address_txs_response::load(db::storage_reader& reader, const db::account& acct, mempool const* const pool, const bool all_outputs)
@@ -842,7 +842,7 @@ namespace lws
resp.lookahead_fail = db::to_uint(acct.lookahead_fail);
resp.lookahead = acct.lookahead;
- auto out = merge_into_txes(outputs->make_iterator(), spends->make_iterator(), std::addressof(acct), pool, outputs->count(), all_outputs, false);
+ auto out = merge_into_txes(outputs->make_iterator(), spends->make_iterator(), std::addressof(acct), std::addressof(reader), pool, outputs->count(), all_outputs, false);
resp.total_received = safe_uint64(out.second);
resp.transactions = std::move(out.first);
return resp;
### src/util/ownership_test.cpp
@@ -55,6 +55,11 @@ namespace lws
subaddress.emplace(disk, max_subaddresses);
}
+ void ownership_test::enable_subaddresses(db::storage_reader& reader)
+ {
+ subaddress.emplace(reader);
+ }
+
void ownership_test::disable_subaddresses()
{
subaddress.reset();
@@ -304,11 +309,15 @@ namespace lws
}
subaddress_reader::subaddress_reader(db::storage const& disk_in, const std::uint32_t max_subaddresses)
- : reader(common_error::kInvalidArgument), disk(disk_in.clone()), cur(nullptr), max_subaddresses(max_subaddresses)
+ : reader(common_error::kInvalidArgument), borrowed(nullptr), disk(disk_in.clone()), cur(nullptr), max_subaddresses(max_subaddresses)
{
update_reader();
}
+ subaddress_reader::subaddress_reader(db::storage_reader& borrowed_in)
+ : reader(common_error::kInvalidArgument), borrowed(std::addressof(borrowed_in)), disk(db::storage::none()), cur(nullptr), max_subaddresses(0)
+ {}
+
void subaddress_reader::update_reader()
{
// Release the current read txn before opening the next one - `reader = disk.start_read()`
@@ -323,16 +332,20 @@ namespace lws
expect<db::address_index> subaddress_reader::find_subaddress(const account& user, crypto::public_key const& pubkey)
{
- if (!reader)
+ db::storage_reader* const active = get_reader();
+ if (!active)
return {lmdb::error(MDB_NOTFOUND)};
- return reader->find_subaddress(user.id(), pubkey, cur);
+ return active->find_subaddress(user.id(), pubkey, cur);
}
expect<void> subaddress_reader::update_lookahead(const account& user, const db::address_index& match, db::block_id height)
{
if (match.is_zero())
return {}; // keep subaddress disabled servers quick
+ if (!disk)
+ return {}; // borrowed reader, the scanner updates the lookahead
+
if (height == db::block_id::txpool)
height = user.scan_height();
### src/util/ownership_test.h
@@ -42,18 +42,24 @@ namespace lws
public:
subaddress_reader(db::storage const& disk_in, const std::uint32_t max_subaddresses);
+ //! Lookup only, the lookahead does not change. `borrowed_in` must stay valid.
+ explicit subaddress_reader(db::storage_reader& borrowed_in);
+
expect<db::address_index> find_subaddress(const account& user, crypto::public_key const& pubkey);
expect<void> update_lookahead(const account& user, const db::address_index& match, db::block_id height);
db::storage_reader* get_reader() noexcept
{
+ if (borrowed)
+ return borrowed;
if (reader)
return std::addressof(*reader);
return nullptr;
}
private:
expect<db::storage_reader> reader;
+ db::storage_reader* borrowed;
db::storage disk;
db::cursor::subaddress_indexes cur;
const std::uint32_t max_subaddresses;
@@ -97,6 +103,8 @@ namespace lws
);
void enable_subaddresses(const db::storage& disk, const std::uint32_t max_subaddresses);
+ //! Lookup only, the lookahead does not change. `reader` must stay valid.
+ void enable_subaddresses(db::storage_reader& reader);
void disable_subaddresses();
private:
### tests/unit/mempool.test.cpp
@@ -27,11 +27,15 @@
#include "framework.test.h"
+#include <algorithm>
#include <cstring>
#include "cryptonote_basic/cryptonote_format_utils.h" // monero/src
#include "cryptonote_core/cryptonote_tx_utils.h" // monero/src
#include "db/account.h"
#include "db/data.h"
+#include "db/storage.h"
+#include "db/storage.test.h"
+#include "device/device_default.hpp" // monero/src
#include "mempool.h"
#include "util/account.test.h"
#include "util/transaction.test.h"
@@ -55,12 +59,15 @@ LWS_CASE("mempool")
SETUP("mempool")
{
+ lws::db::test::cleanup_db on_scope_exit{};
+ lws::db::storage db = lws::db::test::get_fresh_db();
lws::mempool pool{};
lws::account account{make_account(base), {}, {}};
SECTION("empty")
{
- EXPECT(pool.scan_account(account).empty());
+ auto reader = MONERO_UNWRAP(db.start_read());
+ EXPECT(pool.scan_account(account, reader).empty());
}
SECTION("add_txs")
@@ -81,15 +88,16 @@ LWS_CASE("mempool")
std::array<cryptonote::transaction, 2> txes{{tx1.tx, tx2.tx}};
pool.add_txs(epee::to_mut_span(txes));
- auto results = pool.scan_account(account);
+ auto reader = MONERO_UNWRAP(db.start_read());
+ auto results = pool.scan_account(account, reader);
EXPECT(results.size() == 1);
EXPECT(results.at(0).hash == get_transaction_hash(tx1.tx));
EXPECT(results.at(0).spends.size() == 0);
EXPECT(results.at(0).outputs.size() == 1);
EXPECT(results.at(0).outputs.at(0).link.height == lws::db::block_id::txpool);
EXPECT(results.at(0).outputs.at(0).link.tx_hash == get_transaction_hash(tx1.tx));
- results = pool.scan_account(account);
+ results = pool.scan_account(account, reader);
EXPECT(results.size() == 1);
EXPECT(results.at(0).hash == get_transaction_hash(tx1.tx));
EXPECT(results.at(0).spends.size() == 0);
@@ -98,7 +106,7 @@ LWS_CASE("mempool")
EXPECT(results.at(0).outputs.at(0).link.tx_hash == get_transaction_hash(tx1.tx));
pool.reset_txs(lws::mempool::pool_table{});
- results = pool.scan_account(account);
+ results = pool.scan_account(account, reader);
EXPECT(results.empty());
}
@@ -125,13 +133,128 @@ LWS_CASE("mempool")
}
);
- auto results = pool.scan_account(account);
+ auto reader = MONERO_UNWRAP(db.start_read());
+ auto results = pool.scan_account(account, reader);
EXPECT(results.size() == 1);
EXPECT(results.at(0).hash == get_transaction_hash(tx1.tx));
EXPECT(results.at(0).spends.size() == 0);
EXPECT(results.at(0).outputs.size() == 1);
EXPECT(results.at(0).outputs.at(0).link.height == lws::db::block_id::txpool);
EXPECT(results.at(0).outputs.at(0).link.tx_hash == get_transaction_hash(tx1.tx));
}
+
+ SECTION("subaddress")
+ {
+ const lws::db::account_address address{
+ base.m_account_address.m_view_public_key,
+ base.m_account_address.m_spend_public_key
+ };
+ EXPECT(db.add_account(address, base.m_view_secret_key));
+ {
+ const std::vector<lws::db::subaddress_dict> indexes{
+ lws::db::subaddress_dict{
+ lws::db::major_index::primary,
+ lws::db::index_ranges{
+ {lws::db::index_range{lws::db::minor_index(1), lws::db::minor_index(2)}}
+ }
+ }
+ };
+ EXPECT(db.upsert_subaddresses(lws::db::account_id(1), address, base.m_view_secret_key, indexes, 2));
+ }
+
+ hw::core::device_default hw{};
+ std::vector<cryptonote::tx_destination_entry> destinations;
+ destinations.emplace_back();
+ destinations.back().amount = 8000;
+ destinations.back().addr = base.m_account_address;
+ destinations.emplace_back();
+ destinations.back().amount = 2000;
+ destinations.back().addr = hw.get_subaddress(base, cryptonote::subaddress_index{0, 1});
+ destinations.back().is_subaddress = true;
+ const auto tx = lws_test::make_tx(lest_env, base, destinations, 20, true);
+
+ std::array<cryptonote::transaction, 1> txes{{tx.tx}};
+ pool.add_txs(epee::to_mut_span(txes));
+
+ lws::db::address_index lookahead{};
+ {
+ auto reader = MONERO_UNWRAP(db.start_read());
+ const lws::db::account db_account = MONERO_UNWRAP(reader.get_account(address)).second;
+ lookahead = db_account.lookahead;
+ lws::account full_account{db_account, {}, {}};
+
+ const auto results = pool.scan_account(full_account, reader);
+ EXPECT(results.size() == 1);
+ EXPECT(results.at(0).hash == get_transaction_hash(tx.tx));
+ EXPECT(results.at(0).outputs.size() == 2);
+
+ const lws::db::address_index subaddress{lws::db::major_index::primary, lws::db::minor_index(1)};
+ const auto& outputs = results.at(0).outputs;
+ const auto to_subaddress = std::find_if(outputs.begin(), outputs.end(), [&] (const lws::db::output& out) {
+ return out.recipient == subaddress;
+ });
+ EXPECT(to_subaddress != outputs.end());
+ EXPECT(to_subaddress->spend_meta.amount == 2000u);
+ }
+
+ // The REST scan must not change the lookahead
+ EXPECT(MONERO_UNWRAP(MONERO_UNWRAP(db.start_read()).get_account(address)).second.lookahead == lookahead);
+ }
+
+ SECTION("subaddress after skip")
+ {
+ // make_tx decodes all outputs with the view key of the sender, so the
+ // scanned account uses the same view key with a different spend key
+ cryptonote::account_keys other = lws_test::make_account();
+ other.m_account_address.m_view_public_key = base.m_account_address.m_view_public_key;
+ other.m_view_secret_key = base.m_view_secret_key;
+
+ const lws::db::account_address address{
+ other.m_account_address.m_view_public_key,
+ other.m_account_address.m_spend_public_key
+ };
+ EXPECT(db.add_account(address, other.m_view_secret_key));
+
+ hw::core::device_default hw{};
+ std::vector<cryptonote::tx_destination_entry> destinations;
+ destinations.emplace_back();
+ destinations.back().amount = 8000;
+ destinations.back().addr = base.m_account_address;
+ destinations.emplace_back();
+ destinations.back().amount = 2000;
+ destinations.back().addr = hw.get_subaddress(other, cryptonote::subaddress_index{0, 1});
+ destinations.back().is_subaddress = true;
+ const auto tx = lws_test::make_tx(lest_env, base, destinations, 20, true);
+
+ std::array<cryptonote::transaction, 1> txes{{tx.tx}};
+ pool.add_txs(epee::to_mut_span(txes));
+
+ {
+ auto reader = MONERO_UNWRAP(db.start_read());
+ lws::account full_account{MONERO_UNWRAP(reader.get_account(address)).second, {}, {}};
+ EXPECT(pool.scan_account(full_account, reader).empty());
+ }
+
+ {
+ const std::vector<lws::db::subaddress_dict> indexes{
+ lws::db::subaddress_dict{
+ lws::db::major_index::primary,
+ lws::db::index_ranges{
+ {lws::db::index_range{lws::db::minor_index(1), lws::db::minor_index(2)}}
+ }
+ }
+ };
+ EXPECT(db.upsert_subaddresses(lws::db::account_id(1), address, other.m_view_secret_key, indexes, 2));
+ }
+
+ auto reader = MONERO_UNWRAP(db.start_read());
+ lws::account full_account{MONERO_UNWRAP(reader.get_account(address)).second, {}, {}};
+ const auto results = pool.scan_account(full_account, reader);
+ EXPECT(results.size() == 1);
+ EXPECT(results.at(0).hash == get_transaction_hash(tx.tx));
+ EXPECT(results.at(0).outputs.size() == 1);
+ EXPECT(results.at(0).outputs.at(0).recipient == (lws::db::address_index{lws::db::major_index::primary, lws::db::minor_index(1)}));
+ EXPECT(results.at(0).outputs.at(0).spend_meta.amount == 2000u);
+ }
}
}
### tests/unit/rest.test.cpp
@@ -39,6 +39,7 @@
#include "db/print.test.h"
#include "db/storage.test.h"
#include "db/string.h"
+#include "device/device_default.hpp" // monero/src
#include "error.h"
#include "hex.h" // monero/epee/contrib/include
#include "mempool.h"
@@ -1046,6 +1047,56 @@ LWS_CASE("rest_server")
verify_json(lest_env, "/get_address_txs receives", response, expected);
}
+ SECTION("mempool subaddress")
+ {
+ const auto pool = std::make_shared<lws::mempool>();
+
+ EXPECT(client.disconnect());
+ init_server(pool);
+ EXPECT(client.connect(std::chrono::milliseconds{500}));
+
+ {
+ const std::vector<lws::db::subaddress_dict> indexes{
+ lws::db::subaddress_dict{
+ lws::db::major_index::primary,
+ lws::db::index_ranges{
+ {lws::db::index_range{lws::db::minor_index(1), lws::db::minor_index(2)}}
+ }
+ }
+ };
+ EXPECT(db.upsert_subaddresses(account.id, account_address, view, indexes, 2));
+ }
+
+ hw::core::device_default hw{};
+ std::vector<cryptonote::tx_destination_entry> destinations;
+ destinations.emplace_back();
+ destinations.back().amount = 8000;
+ destinations.back().addr = base.m_account_address;
+ destinations.emplace_back();
+ destinations.back().amount = 2000;
+ destinations.back().addr = hw.get_subaddress(base, cryptonote::subaddress_index{0, 1});
+ destinations.back().is_subaddress = true;
+ const auto tx = lws_test::make_tx(lest_env, base, destinations, 20, true);
+
+ pool->reset_txs(
+ lws::mempool::pool_table{
+ {get_transaction_hash(tx.tx), std::make_shared<lws::mempool::pool_entry>(cryptonote::transaction{tx.tx}, now)}
+ }
+ );
+
+ message = "{\"address\":\"" + address + "\",\"view_key\":\"" + viewkey + "\"}";
+ response = invoke(client, "/get_address_txs", message);
+
+ rapidjson::Document doc;
+ doc.Parse(response.c_str());
+ EXPECT(!doc.HasParseError());
+ EXPECT(doc["total_received"] == "10000");
+ EXPECT(doc["transactions"].Size() == 1u);
+ EXPECT(doc["transactions"][0u]["mempool"] == true);
+ EXPECT(doc["transactions"][0u]["total_received"] == "10000");
+ EXPECT(get_account().lookahead == account.lookahead);
+ }
+
SECTION("provision_subaddrs")
{
const std::string scan_height = std::to_string(std::uint64_t(account.scan_height) + 5);
@@ -1126,23 +1177,24 @@ LWS_CASE("rest_server")
boost::this_thread::sleep_for(boost::chrono::milliseconds{10});
auto full_account = get_full_account();
+ auto reader = MONERO_UNWRAP(db.start_read());
const auto bad_hex = invoke_base(client, "/submit_raw_tx", "{\"tx\":\"zz\"}");
EXPECT(bad_hex.second == 400);
EXPECT(bad_hex.first.empty());
response = invoke(client, "/submit_raw_tx", message);
EXPECT(response == "{\"status\":\"OK\",\"reason\":\"Not relayed\",\"not_relayed\":true}");
- EXPECT(pool->scan_account(full_account).empty());
+ EXPECT(pool->scan_account(full_account, reader).empty());
const auto rejected = invoke_base(client, "/submit_raw_tx", message);
EXPECT(rejected.second == 422);
EXPECT(rejected.first == "{\"status\":\"Failed\",\"reason\":\"double spend and fee too low\"}");
- EXPECT(pool->scan_account(full_account).empty());
+ EXPECT(pool->scan_account(full_account, reader).empty());
response = invoke(client, "/submit_raw_tx", message);
EXPECT(response == "{\"status\":\"OK\"}");
- EXPECT(pool->scan_account(full_account).size() == 1);
+ EXPECT(pool->scan_account(full_account, reader).size() == 1);
}
SECTION("CORS")Why this scored 50/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.