The sweep took the first maxInputs notes of an address and asked GetSaplingNoteWitnesses for their witnesses. If one had none -- a missing witness, or a desynced one that does not match the anchor the others agree on -- it logged "Missing Witnesses! Stopping.", built anyway with that note's value still counted, failed the build, and broke out of the address loop. The round failed, every address after that one went unswept, and the same thing happened again every interval. And because that anchor is a majority vote, a desynced group that outnumbers the good notes at an address (as one did on a real wallet) makes the "agreed" root one the chain never had. SelectSweepableNotes now picks each transaction's notes. It copies each note's latest cached witness under cs_wallet (GetSaplingNoteLatestWitness, which does no hashing), computes each root once with no lock held, groups the notes by root, and takes the largest group whose root is an anchor the chainstate knows -- the same lookup consensus applies to shielded spends. It reads the notes in windows of 2 * maxInputs and stops as soon as a known group can fill the transaction, so a healthy address costs about what the old first-maxInputs selection did (a Merkle root is ~1-2 ms, and an earlier all-notes pass held cs_main for ~30 s on a 13k-note wallet), while a run of bad notes at the front cannot hide good ones behind it. Notes left out are logged once per address with a count and the first outpoint, and looked at again next round. In the round itself: - an address with no spendable notes is skipped rather than failing the round; - a failed build moves on to the next address (nothing was recorded); a failed commit still stops the round, because CommitTransaction has already written the tx to the wallet; - a round counts as incomplete only once it has committed something, so an address whose notes are all unspendable no longer has the scheduler re-dispatch on every block -- it is retried an interval later. New gtest SweepNoteSelection, 9 cases, including a desynced majority, a desynced run ahead of good notes, a lone good note behind five desynced groups, two real anchors, and a healthy address being read one window at a time. Accepting every root as an anchor fails 4 of them, and reading every note fails the window test (200 notes took 244 ms against one window's worth). End to end on regtest, with gdb making the wallet's first witness lookup find nothing: the other 4 notes at that address and every other address were swept in the same round, the round succeeded, the next round swept the skipped note, and the wallet drained fully, with no per-note desync lines in the log. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
159 lines
5.6 KiB
C++
159 lines
5.6 KiB
C++
// Copyright (c) 2016-2024 The Hush developers
|
|
// Distributed under the GPLv3 software license, see the accompanying
|
|
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <map>
|
|
#include <set>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
#include "primitives/transaction.h"
|
|
#include "uint256.h"
|
|
#include "wallet/asyncrpcoperation_sweep.h"
|
|
#include "zcash/IncrementalMerkleTree.hpp"
|
|
|
|
namespace {
|
|
|
|
// A witness in a one-leaf tree: each leaf gives a different root().
|
|
SaplingWitness WitnessAt(const std::string& leaf)
|
|
{
|
|
SaplingMerkleTree tree;
|
|
tree.append(uint256S(leaf));
|
|
return tree.witness();
|
|
}
|
|
|
|
const SaplingWitness GOOD = WitnessAt("0a"); // at an anchor the chain knows
|
|
const SaplingWitness OTHER = WitnessAt("0c"); // at another known anchor
|
|
SaplingWitness Bad(int i) { return WitnessAt("b0" + std::to_string(i)); } // desynced: roots the chain never had
|
|
|
|
SaplingOutPoint Op(int i) { return SaplingOutPoint(uint256S("f0"), i); }
|
|
|
|
// Stands in for the wallet: each note's latest cached witness (none = no witness) and the chainstate's
|
|
// anchors. Counts how many notes the selection asked about.
|
|
struct FakeWallet {
|
|
std::map<SaplingOutPoint, boost::optional<SaplingWitness>> latest;
|
|
std::set<uint256> knownAnchors{GOOD.root(), OTHER.root()};
|
|
int calls = 0;
|
|
size_t asked = 0;
|
|
|
|
SweepNoteSelection Select(size_t n, size_t maxInputs)
|
|
{
|
|
std::vector<SaplingOutPoint> ops;
|
|
for (size_t i = 0; i < n; i++)
|
|
ops.push_back(Op(i));
|
|
return SelectSweepableNotes(ops, maxInputs,
|
|
[this](const std::vector<SaplingOutPoint>& o) {
|
|
calls++;
|
|
asked += o.size();
|
|
std::vector<boost::optional<SaplingWitness>> out;
|
|
for (const SaplingOutPoint& op : o)
|
|
out.push_back(latest[op]);
|
|
return out;
|
|
},
|
|
[this](const uint256& root) { return knownAnchors.count(root) > 0; });
|
|
}
|
|
};
|
|
|
|
std::vector<size_t> V(std::initializer_list<size_t> l) { return std::vector<size_t>(l); }
|
|
std::vector<size_t> Range(size_t from, size_t to) { std::vector<size_t> v; for (size_t i = from; i < to; i++) v.push_back(i); return v; }
|
|
|
|
}
|
|
|
|
TEST(SweepNoteSelection, TakesAtMostMaxInputsAndDoesNotSkipTheRest) {
|
|
FakeWallet w;
|
|
for (int i = 0; i < 7; i++) w.latest[Op(i)] = GOOD;
|
|
SweepNoteSelection sel = w.Select(7, 5);
|
|
EXPECT_EQ(Range(0, 5), sel.chosen);
|
|
EXPECT_EQ(5u, sel.witnesses.size());
|
|
EXPECT_EQ(GOOD.root(), sel.anchor);
|
|
EXPECT_TRUE(sel.skipped.empty()) << "notes past the cap are left for the next round, not skipped";
|
|
}
|
|
|
|
// A healthy address costs about what the old first-maxInputs selection did: one window of 2*maxInputs.
|
|
TEST(SweepNoteSelection, AHealthyAddressReadsOneWindow) {
|
|
FakeWallet w;
|
|
for (int i = 0; i < 200; i++) w.latest[Op(i)] = GOOD;
|
|
SweepNoteSelection sel = w.Select(200, 5);
|
|
EXPECT_EQ(Range(0, 5), sel.chosen);
|
|
EXPECT_EQ(1, w.calls);
|
|
EXPECT_EQ(10u, w.asked);
|
|
EXPECT_TRUE(sel.skipped.empty());
|
|
}
|
|
|
|
TEST(SweepNoteSelection, SkipsNotesWithoutAWitness) {
|
|
FakeWallet w;
|
|
for (int i = 0; i < 5; i++) w.latest[Op(i)] = GOOD;
|
|
w.latest[Op(0)] = boost::none;
|
|
w.latest[Op(2)] = boost::none;
|
|
SweepNoteSelection sel = w.Select(5, 5);
|
|
EXPECT_EQ(V({1, 3, 4}), sel.chosen);
|
|
EXPECT_EQ(V({0, 2}), sel.skipped);
|
|
}
|
|
|
|
// The case seen on a real wallet: desynced notes outnumber the good ones at their address.
|
|
TEST(SweepNoteSelection, PassesOverADesyncedMajority) {
|
|
FakeWallet w;
|
|
for (int i = 0; i < 3; i++) w.latest[Op(i)] = Bad(0);
|
|
for (int i = 3; i < 5; i++) w.latest[Op(i)] = GOOD;
|
|
SweepNoteSelection sel = w.Select(5, 5);
|
|
EXPECT_EQ(V({3, 4}), sel.chosen);
|
|
EXPECT_EQ(GOOD.root(), sel.anchor);
|
|
EXPECT_EQ(V({0, 1, 2}), sel.skipped);
|
|
}
|
|
|
|
// A run of bad notes at the front must not hide good notes behind it.
|
|
TEST(SweepNoteSelection, ADesyncedRunAtTheFrontDoesNotHideTheRest) {
|
|
FakeWallet w;
|
|
for (int i = 0; i < 12; i++) w.latest[Op(i)] = Bad(0);
|
|
for (int i = 12; i < 14; i++) w.latest[Op(i)] = GOOD;
|
|
SweepNoteSelection sel = w.Select(14, 5);
|
|
EXPECT_EQ(V({12, 13}), sel.chosen);
|
|
EXPECT_EQ(Range(0, 12), sel.skipped);
|
|
}
|
|
|
|
// However the desynced notes split, a good group is found: no limit on the number of bad roots.
|
|
TEST(SweepNoteSelection, ALoneGoodNoteBehindManyDesyncedGroups) {
|
|
FakeWallet w;
|
|
for (int g = 0; g < 5; g++)
|
|
for (int j = 0; j < 2; j++)
|
|
w.latest[Op(g * 2 + j)] = Bad(g);
|
|
w.latest[Op(10)] = GOOD;
|
|
SweepNoteSelection sel = w.Select(11, 5);
|
|
EXPECT_EQ(V({10}), sel.chosen);
|
|
EXPECT_EQ(Range(0, 10), sel.skipped);
|
|
}
|
|
|
|
TEST(SweepNoteSelection, NothingWhenNoWitnessIsAtAKnownAnchor) {
|
|
FakeWallet w;
|
|
for (int i = 0; i < 4; i++) w.latest[Op(i)] = Bad(i % 2);
|
|
w.latest[Op(4)] = boost::none;
|
|
SweepNoteSelection sel = w.Select(5, 5);
|
|
EXPECT_TRUE(sel.chosen.empty());
|
|
EXPECT_TRUE(sel.witnesses.empty());
|
|
EXPECT_EQ(Range(0, 5), sel.skipped);
|
|
}
|
|
|
|
// Two real anchors: one transaction can only use one, so the larger group is spent and the other waits
|
|
// for a later round.
|
|
TEST(SweepNoteSelection, OneAnchorPerTransaction) {
|
|
FakeWallet w;
|
|
for (int i = 0; i < 3; i++) w.latest[Op(i)] = GOOD;
|
|
for (int i = 3; i < 5; i++) w.latest[Op(i)] = OTHER;
|
|
SweepNoteSelection sel = w.Select(5, 5);
|
|
EXPECT_EQ(V({0, 1, 2}), sel.chosen);
|
|
EXPECT_EQ(GOOD.root(), sel.anchor);
|
|
EXPECT_EQ(V({3, 4}), sel.skipped);
|
|
}
|
|
|
|
TEST(SweepNoteSelection, NoCandidates) {
|
|
FakeWallet w;
|
|
SweepNoteSelection sel = w.Select(0, 5);
|
|
EXPECT_TRUE(sel.chosen.empty());
|
|
EXPECT_TRUE(sel.skipped.empty());
|
|
EXPECT_EQ(0, w.calls);
|
|
}
|