// 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 #include #include #include #include #include #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> latest; std::set knownAnchors{GOOD.root(), OTHER.root()}; int calls = 0; size_t asked = 0; SweepNoteSelection Select(size_t n, size_t maxInputs) { std::vector ops; for (size_t i = 0; i < n; i++) ops.push_back(Op(i)); return SelectSweepableNotes(ops, maxInputs, [this](const std::vector& o) { calls++; asked += o.size(); std::vector> out; for (const SaplingOutPoint& op : o) out.push_back(latest[op]); return out; }, [this](const uint256& root) { return knownAnchors.count(root) > 0; }); } }; std::vector V(std::initializer_list l) { return std::vector(l); } std::vector Range(size_t from, size_t to) { std::vector 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); }