wallet: spend consolidation notes only at a chain anchor
The round took its anchor from GetSaplingNoteWitnesses, a majority vote over the planned notes alone that never looks at the chain. A real wallet has had 108 notes desynced onto one root the chain never had, at the same address as 104 healthy ones. Smallest-first planning can pick a batch that is mostly desynced notes. The vote then made the healthy notes the "unusable" ones, and the tx was proved at a non-anchor: Build succeeds, since witnesses and anchor agree, but CommitTransaction adds it to the wallet before the mempool refuses it. Its notes then look spent, and since the plan is nearly deterministic this recurs every round. One failed tx also ended the round for every other address. PlanSaplingConsolidationSpends now plans over the notes that can be spent together. A planned note is used only if its newest witness root is a Sapling anchor the chain holds (GetSaplingAnchorAt on pcoinsTip, the check consensus makes) and is its address's anchor: the tip's root if any planned note is there, else the chain anchor most share. Other planned notes are left out and the round is re-planned until none are. The planner is pure apart from its callbacks and is tested, including a desynced majority. GetSaplingNoteLatestWitness is pass 1 of GetSaplingNoteWitnesses, split out; that function is unchanged. A tx that fails to build or commit now only skips the rest of its own address's txs; the first failure is the round's error. Skipped notes are logged once per round with a count and the first five outpoints, instead of once per process: a note that is unusable only during a witness rebuild was otherwise never reported again. The fee rule moves into SaplingConsolidationTxFee, with a test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#include "arith_uint256.h"
|
||||
#include "primitives/transaction.h"
|
||||
#include "random.h"
|
||||
#include "wallet/asyncrpcoperation_saplingconsolidation.h"
|
||||
@@ -230,3 +231,165 @@ TEST(SaplingConsolidationStats, RecordAccumulates) {
|
||||
EXPECT_EQ(stats.lastRound.skippedNotes, 2);
|
||||
EXPECT_EQ(stats.lastRound.error, failed.error);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
uint256 Root(int n) { return ArithToUint256(arith_uint256(n)); }
|
||||
|
||||
// Witness roots per note, for PlanSaplingConsolidationSpends; also counts its lookups.
|
||||
struct Witnesses {
|
||||
std::vector<std::vector<boost::optional<uint256>>> roots;
|
||||
std::set<uint256> chainAnchors;
|
||||
std::map<std::pair<size_t, size_t>, int> rootCalls;
|
||||
std::map<uint256, int> anchorCalls;
|
||||
|
||||
SaplingConsolidationSpendPlan Plan(const std::vector<std::vector<CAmount>>& groups,
|
||||
const SaplingConsolidationPolicy& policy, const uint256& tip) {
|
||||
return PlanSaplingConsolidationSpends(groups, policy, RngHigh,
|
||||
[this](size_t g, size_t i) { rootCalls[std::make_pair(g, i)]++; return roots[g][i]; },
|
||||
[this](const uint256& r) { anchorCalls[r]++; return chainAnchors.count(r) > 0; },
|
||||
tip);
|
||||
}
|
||||
};
|
||||
|
||||
void ExpectAllAt(const SaplingConsolidationSpendPlan& plan, const Witnesses& w) {
|
||||
for (const auto& b : plan.batches) {
|
||||
ASSERT_TRUE(plan.anchors.count(b.group));
|
||||
for (size_t i : b.notes) {
|
||||
ASSERT_TRUE(w.roots[b.group][i]);
|
||||
EXPECT_EQ(*w.roots[b.group][i], plan.anchors.at(b.group)) << "note " << i << " of address " << b.group;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(SaplingConsolidationSpends, HealthyWalletPlansAsBefore) {
|
||||
SaplingConsolidationPolicy policy;
|
||||
std::vector<std::vector<CAmount>> groups = { Values(60), Values(10), Values(40) };
|
||||
Witnesses w;
|
||||
w.chainAnchors = { Root(7) };
|
||||
for (const auto& g : groups)
|
||||
w.roots.push_back(std::vector<boost::optional<uint256>>(g.size(), Root(7)));
|
||||
|
||||
auto spends = w.Plan(groups, policy, Root(7));
|
||||
auto plain = PlanSaplingConsolidation(groups, policy, RngHigh);
|
||||
ASSERT_EQ(spends.batches.size(), plain.size());
|
||||
for (size_t k = 0; k < plain.size(); k++) {
|
||||
EXPECT_EQ(spends.batches[k].group, plain[k].group);
|
||||
EXPECT_EQ(spends.batches[k].notes, plain[k].notes);
|
||||
}
|
||||
EXPECT_TRUE(spends.unusable.empty());
|
||||
EXPECT_TRUE(spends.deferred.empty());
|
||||
EXPECT_EQ(spends.anchors.size(), 2u) << "the address under the threshold gets no anchor";
|
||||
EXPECT_EQ(spends.anchors.at(0), Root(7));
|
||||
// Only planned notes are looked at, each once, and the one root once.
|
||||
for (const auto& c : w.rootCalls)
|
||||
EXPECT_EQ(c.second, 1);
|
||||
EXPECT_EQ(w.rootCalls.size(), PlannedNotes(plain, 0) + PlannedNotes(plain, 2));
|
||||
EXPECT_EQ(w.anchorCalls.size(), 1u);
|
||||
EXPECT_EQ(w.anchorCalls[Root(7)], 1);
|
||||
}
|
||||
|
||||
// The case seen on a real wallet: many notes of one address share a root the chain never had, and smallest-first
|
||||
// planning picks mostly (here: only) those. They must not decide the anchor and take the healthy notes down.
|
||||
TEST(SaplingConsolidationSpends, DesyncedMajorityDoesNotWin) {
|
||||
SaplingConsolidationPolicy policy; // 25 / 15 / 3
|
||||
std::vector<std::vector<CAmount>> groups = { Values(100) };
|
||||
Witnesses w;
|
||||
w.chainAnchors = { Root(7) };
|
||||
w.roots.resize(1);
|
||||
for (size_t i = 0; i < 100; i++)
|
||||
w.roots[0].push_back(i < 50 ? Root(666) : Root(7)); // the 50 smallest are desynced
|
||||
|
||||
auto plan = w.Plan(groups, policy, Root(7));
|
||||
ASSERT_EQ(plan.batches.size(), 3u);
|
||||
EXPECT_EQ(plan.anchors.at(0), Root(7));
|
||||
ExpectAllAt(plan, w);
|
||||
for (const auto& b : plan.batches)
|
||||
for (size_t i : b.notes)
|
||||
EXPECT_GE(i, 50u);
|
||||
EXPECT_EQ(plan.unusable.size(), 50u);
|
||||
EXPECT_TRUE(plan.deferred.empty());
|
||||
EXPECT_EQ(w.anchorCalls[Root(666)], 1) << "a root is checked against the chain once";
|
||||
for (const auto& c : w.rootCalls)
|
||||
EXPECT_EQ(c.second, 1);
|
||||
|
||||
// The same when the tip's root is not among them: the chain anchor wins, not the majority.
|
||||
Witnesses old = w;
|
||||
old.chainAnchors = { Root(5) };
|
||||
for (size_t i = 50; i < 100; i++)
|
||||
old.roots[0][i] = Root(5);
|
||||
old.rootCalls.clear();
|
||||
old.anchorCalls.clear();
|
||||
auto plan2 = old.Plan(groups, policy, Root(7));
|
||||
ASSERT_EQ(plan2.batches.size(), 3u);
|
||||
EXPECT_EQ(plan2.anchors.at(0), Root(5));
|
||||
ExpectAllAt(plan2, old);
|
||||
}
|
||||
|
||||
TEST(SaplingConsolidationSpends, MissingWitnessesAreSkipped) {
|
||||
SaplingConsolidationPolicy policy;
|
||||
policy.maxTxs = 1;
|
||||
std::vector<std::vector<CAmount>> groups = { Values(40) };
|
||||
Witnesses w;
|
||||
w.chainAnchors = { Root(7) };
|
||||
w.roots.assign(1, std::vector<boost::optional<uint256>>(40, Root(7)));
|
||||
w.roots[0][0] = boost::none;
|
||||
w.roots[0][3] = boost::none;
|
||||
|
||||
auto plan = w.Plan(groups, policy, Root(7));
|
||||
ASSERT_EQ(plan.batches.size(), 1u);
|
||||
EXPECT_EQ(plan.batches[0].notes.size(), 15u) << "the dropped notes are replaced by the next smallest";
|
||||
ExpectAllAt(plan, w);
|
||||
std::set<std::pair<size_t, size_t>> unusable(plan.unusable.begin(), plan.unusable.end());
|
||||
EXPECT_EQ(unusable, (std::set<std::pair<size_t, size_t>>{ {0, 0}, {0, 3} }));
|
||||
}
|
||||
|
||||
// Witnesses at a different, real anchor (a rebuild still under way) are deferred, not called unusable.
|
||||
TEST(SaplingConsolidationSpends, OtherChainAnchorIsDeferred) {
|
||||
SaplingConsolidationPolicy policy;
|
||||
policy.maxTxs = 1;
|
||||
std::vector<std::vector<CAmount>> groups = { Values(40) };
|
||||
Witnesses w;
|
||||
w.chainAnchors = { Root(7), Root(6) };
|
||||
w.roots.assign(1, std::vector<boost::optional<uint256>>(40, Root(7)));
|
||||
w.roots[0][1] = Root(6);
|
||||
w.roots[0][2] = Root(6);
|
||||
|
||||
auto plan = w.Plan(groups, policy, Root(7));
|
||||
ASSERT_EQ(plan.batches.size(), 1u);
|
||||
EXPECT_EQ(plan.anchors.at(0), Root(7));
|
||||
ExpectAllAt(plan, w);
|
||||
EXPECT_TRUE(plan.unusable.empty());
|
||||
EXPECT_EQ(plan.deferred.size(), 2u);
|
||||
}
|
||||
|
||||
// Leaving notes out can bring an address down to the threshold; then it is not consolidated, and needs no anchor.
|
||||
TEST(SaplingConsolidationSpends, ThresholdCountsUsableNotes) {
|
||||
SaplingConsolidationPolicy policy;
|
||||
std::vector<std::vector<CAmount>> groups = { Values(26) };
|
||||
Witnesses w;
|
||||
w.chainAnchors = { Root(7) };
|
||||
w.roots.assign(1, std::vector<boost::optional<uint256>>(26, Root(7)));
|
||||
w.roots[0][0] = boost::none;
|
||||
|
||||
auto plan = w.Plan(groups, policy, Root(7));
|
||||
EXPECT_TRUE(plan.batches.empty());
|
||||
EXPECT_TRUE(plan.anchors.empty());
|
||||
EXPECT_EQ(plan.unusable.size(), 1u);
|
||||
|
||||
// Nothing at a chain anchor at all: nothing planned, every looked-at note reported.
|
||||
Witnesses bad;
|
||||
bad.roots.assign(1, std::vector<boost::optional<uint256>>(26, Root(666)));
|
||||
auto none = bad.Plan(groups, policy, Root(7));
|
||||
EXPECT_TRUE(none.batches.empty());
|
||||
EXPECT_EQ(none.unusable.size(), 25u) << "the first plan took 25 of the 26, then 1 was too few to plan";
|
||||
}
|
||||
|
||||
TEST(SaplingConsolidationFee, NotesWorthTheFeeGoFeeFree) {
|
||||
EXPECT_EQ(SaplingConsolidationTxFee(10000, 10000), 0);
|
||||
EXPECT_EQ(SaplingConsolidationTxFee(9999, 10000), 0);
|
||||
EXPECT_EQ(SaplingConsolidationTxFee(10001, 10000), 10000);
|
||||
EXPECT_EQ(SaplingConsolidationTxFee(5000000, 10000), 10000);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <set>
|
||||
|
||||
CAmount fConsolidationTxFee = DEFAULT_CONSOLIDATION_FEE;
|
||||
bool fConsolidationMapUsed = false;
|
||||
@@ -106,6 +107,113 @@ std::vector<SaplingConsolidationBatch> PlanSaplingConsolidation(
|
||||
return plan;
|
||||
}
|
||||
|
||||
SaplingConsolidationSpendPlan PlanSaplingConsolidationSpends(
|
||||
const std::vector<std::vector<CAmount>>& groups,
|
||||
const SaplingConsolidationPolicy& policy,
|
||||
const std::function<uint64_t(uint64_t)>& rng,
|
||||
const std::function<boost::optional<uint256>(size_t, size_t)>& rootOf,
|
||||
const std::function<bool(const uint256&)>& isChainAnchor,
|
||||
const uint256& preferredAnchor)
|
||||
{
|
||||
SaplingConsolidationSpendPlan result;
|
||||
std::map<std::pair<size_t, size_t>, boost::optional<uint256>> roots;
|
||||
std::map<uint256, bool> chainAnchor;
|
||||
// A planned note's root if it is a chain anchor, else none.
|
||||
auto usableRoot = [&](size_t g, size_t i) -> boost::optional<uint256> {
|
||||
const std::pair<size_t, size_t> key(g, i);
|
||||
auto ri = roots.find(key);
|
||||
if (ri == roots.end())
|
||||
ri = roots.insert(std::make_pair(key, rootOf(g, i))).first;
|
||||
if (!ri->second)
|
||||
return boost::none;
|
||||
auto ai = chainAnchor.find(*ri->second);
|
||||
if (ai == chainAnchor.end())
|
||||
ai = chainAnchor.insert(std::make_pair(*ri->second, isChainAnchor(*ri->second))).first;
|
||||
return ai->second ? ri->second : boost::none;
|
||||
};
|
||||
|
||||
// Each pass leaves out at least one more note or is the last, so this ends.
|
||||
std::vector<std::set<size_t>> excluded(groups.size());
|
||||
for (;;) {
|
||||
std::vector<std::vector<size_t>> live(groups.size());
|
||||
std::vector<std::vector<CAmount>> values(groups.size());
|
||||
for (size_t g = 0; g < groups.size(); g++) {
|
||||
for (size_t i = 0; i < groups[g].size(); i++) {
|
||||
if (excluded[g].count(i))
|
||||
continue;
|
||||
live[g].push_back(i);
|
||||
values[g].push_back(groups[g][i]);
|
||||
}
|
||||
}
|
||||
std::vector<SaplingConsolidationBatch> plan = PlanSaplingConsolidation(values, policy, rng);
|
||||
for (SaplingConsolidationBatch& b : plan)
|
||||
for (size_t& idx : b.notes)
|
||||
idx = live[b.group][idx];
|
||||
|
||||
// An address planned for the first time gets its anchor from its planned notes; it then keeps it.
|
||||
std::map<size_t, std::map<uint256, int>> votes;
|
||||
for (const SaplingConsolidationBatch& b : plan) {
|
||||
if (result.anchors.count(b.group))
|
||||
continue;
|
||||
for (size_t i : b.notes) {
|
||||
boost::optional<uint256> root = usableRoot(b.group, i);
|
||||
if (root)
|
||||
votes[b.group][*root]++;
|
||||
}
|
||||
}
|
||||
for (const auto& v : votes) {
|
||||
if (v.second.count(preferredAnchor)) {
|
||||
result.anchors[v.first] = preferredAnchor;
|
||||
continue;
|
||||
}
|
||||
int best = 0;
|
||||
for (const auto& rv : v.second) {
|
||||
if (rv.second > best) {
|
||||
best = rv.second;
|
||||
result.anchors[v.first] = rv.first;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool dropped = false;
|
||||
for (const SaplingConsolidationBatch& b : plan) {
|
||||
auto anchor = result.anchors.find(b.group);
|
||||
for (size_t i : b.notes) {
|
||||
boost::optional<uint256> root = usableRoot(b.group, i);
|
||||
if (root && anchor != result.anchors.end() && *root == anchor->second)
|
||||
continue;
|
||||
if (root)
|
||||
result.deferred.push_back(std::make_pair(b.group, i));
|
||||
else
|
||||
result.unusable.push_back(std::make_pair(b.group, i));
|
||||
excluded[b.group].insert(i);
|
||||
dropped = true;
|
||||
}
|
||||
}
|
||||
if (!dropped) {
|
||||
result.batches = std::move(plan);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// An address whose notes were all left out ends up without batches; it needs no anchor.
|
||||
std::set<size_t> planned;
|
||||
for (const SaplingConsolidationBatch& b : result.batches)
|
||||
planned.insert(b.group);
|
||||
for (auto it = result.anchors.begin(); it != result.anchors.end(); ) {
|
||||
if (planned.count(it->first))
|
||||
++it;
|
||||
else
|
||||
it = result.anchors.erase(it);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
CAmount SaplingConsolidationTxFee(CAmount amount, CAmount fee) {
|
||||
// Notes worth no more than the fee go fee-free rather than being burnt into it.
|
||||
return amount <= fee ? 0 : fee;
|
||||
}
|
||||
|
||||
AsyncRPCOperation_saplingconsolidation::AsyncRPCOperation_saplingconsolidation(int targetHeight, const SaplingConsolidationRequest& request)
|
||||
: targetHeight_(targetHeight), request_(request) {}
|
||||
|
||||
@@ -186,10 +294,6 @@ void AsyncRPCOperation_saplingconsolidation::main() {
|
||||
}
|
||||
}
|
||||
|
||||
// Notes whose missing or desynced witness has already been logged, so a note that can't be spent is
|
||||
// reported once rather than every round. Guarded by pwalletMain->cs_wallet.
|
||||
static std::set<SaplingOutPoint> setWitnessSkipLogged;
|
||||
|
||||
bool AsyncRPCOperation_saplingconsolidation::main_impl() {
|
||||
bool status=true;
|
||||
auto opid=getId();
|
||||
@@ -261,66 +365,57 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() {
|
||||
groups[it->second].notes.push_back(entry);
|
||||
}
|
||||
|
||||
// Plan the round, then fetch the planned notes' witnesses. A note without a usable witness is
|
||||
// dropped and the round re-planned without it, so it costs the round nothing but a log line.
|
||||
// Each pass drops at least one note, so this ends.
|
||||
std::set<SaplingOutPoint> excluded;
|
||||
for (;;) {
|
||||
std::vector<std::vector<size_t>> live(groups.size());
|
||||
std::vector<std::vector<CAmount>> values(groups.size());
|
||||
for (size_t g = 0; g < groups.size(); g++) {
|
||||
for (size_t i = 0; i < groups[g].notes.size(); i++) {
|
||||
if (excluded.count(groups[g].notes[i].op))
|
||||
continue;
|
||||
live[g].push_back(i);
|
||||
values[g].push_back(CAmount(groups[g].notes[i].note.value()));
|
||||
}
|
||||
}
|
||||
std::vector<SaplingConsolidationBatch> plan = PlanSaplingConsolidation(
|
||||
values, request_.policy, [](uint64_t n) { return GetRand(n); });
|
||||
// Plan the round over the notes that can be spent: a planned note whose witness is missing, or whose
|
||||
// root the chain never had as an anchor (a desynced witness), is left out and the round re-planned
|
||||
// without it. Checking the root against the chain, not just against the other notes, keeps a tx from
|
||||
// being proved at an anchor no node accepts when the desynced notes are the majority of a batch.
|
||||
const uint256 tipRoot = (chainActive.Tip() != NULL) ? chainActive.Tip()->hashFinalSaplingRoot : uint256();
|
||||
std::vector<std::vector<CAmount>> values(groups.size());
|
||||
for (size_t g = 0; g < groups.size(); g++)
|
||||
for (const SaplingNoteEntry& entry : groups[g].notes)
|
||||
values[g].push_back(CAmount(entry.note.value()));
|
||||
std::map<std::pair<size_t, size_t>, SaplingWitness> witnessOf;
|
||||
SaplingConsolidationSpendPlan spendPlan = PlanSaplingConsolidationSpends(
|
||||
values, request_.policy, [](uint64_t n) { return GetRand(n); },
|
||||
[&](size_t g, size_t i) -> boost::optional<uint256> {
|
||||
boost::optional<SaplingWitness> w = pwalletMain->GetSaplingNoteLatestWitness(groups[g].notes[i].op);
|
||||
if (!w)
|
||||
return boost::none;
|
||||
witnessOf.insert(std::make_pair(std::make_pair(g, i), *w));
|
||||
return w->root();
|
||||
},
|
||||
[](const uint256& root) {
|
||||
SaplingMerkleTree tree;
|
||||
return pcoinsTip->GetSaplingAnchorAt(root, tree);
|
||||
},
|
||||
tipRoot);
|
||||
|
||||
// Witnesses per address: every spend of one address's txs then shares that address's anchor.
|
||||
std::map<size_t, std::vector<SaplingOutPoint>> opsOf;
|
||||
for (const SaplingConsolidationBatch& b : plan)
|
||||
for (size_t idx : b.notes)
|
||||
opsOf[b.group].push_back(groups[b.group].notes[live[b.group][idx]].op);
|
||||
std::map<SaplingOutPoint, SaplingWitness> witnessOf;
|
||||
std::map<size_t, uint256> anchorOf;
|
||||
bool missing = false;
|
||||
for (const auto& p : opsOf) {
|
||||
std::vector<boost::optional<SaplingWitness>> witnesses;
|
||||
uint256 anchor;
|
||||
pwalletMain->GetSaplingNoteWitnesses(p.second, witnesses, anchor);
|
||||
anchorOf[p.first] = anchor;
|
||||
for (size_t i = 0; i < p.second.size(); i++) {
|
||||
if (witnesses[i]) {
|
||||
witnessOf.insert(std::make_pair(p.second[i], witnesses[i].get()));
|
||||
continue;
|
||||
}
|
||||
missing = true;
|
||||
excluded.insert(p.second[i]);
|
||||
if (setWitnessSkipLogged.insert(p.second[i]).second)
|
||||
LogPrintf("%s: skipping note %s:%u, it has no usable witness\n",
|
||||
opid, p.second[i].hash.ToString(), p.second[i].n);
|
||||
}
|
||||
for (const SaplingConsolidationBatch& b : spendPlan.batches) {
|
||||
PlannedTx tx;
|
||||
tx.group = b.group;
|
||||
tx.anchor = spendPlan.anchors.at(b.group);
|
||||
for (size_t i : b.notes) {
|
||||
tx.notes.push_back(groups[b.group].notes[i]);
|
||||
tx.witnesses.push_back(witnessOf.at(std::make_pair(b.group, i)));
|
||||
}
|
||||
if (missing)
|
||||
continue;
|
||||
|
||||
for (const SaplingConsolidationBatch& b : plan) {
|
||||
PlannedTx tx;
|
||||
tx.group = b.group;
|
||||
tx.anchor = anchorOf[b.group];
|
||||
for (size_t idx : b.notes) {
|
||||
const SaplingNoteEntry& entry = groups[b.group].notes[live[b.group][idx]];
|
||||
tx.notes.push_back(entry);
|
||||
tx.witnesses.push_back(witnessOf.at(entry.op));
|
||||
}
|
||||
txs.push_back(std::move(tx));
|
||||
}
|
||||
break;
|
||||
txs.push_back(std::move(tx));
|
||||
}
|
||||
tally.skippedNotes = excluded.size();
|
||||
|
||||
// One line per round, not one per note: a note can be unusable for a while (a witness rebuild in
|
||||
// progress) and fine later, so the next round reports it again rather than never.
|
||||
tally.skippedNotes = spendPlan.unusable.size() + spendPlan.deferred.size();
|
||||
if (!spendPlan.unusable.empty()) {
|
||||
std::string some;
|
||||
for (size_t k = 0; k < spendPlan.unusable.size() && k < 5; k++) {
|
||||
const SaplingOutPoint& op = groups[spendPlan.unusable[k].first].notes[spendPlan.unusable[k].second].op;
|
||||
some += strprintf("%s%s:%u", k ? ", " : "", op.hash.ToString(), op.n);
|
||||
}
|
||||
LogPrintf("%s: skipping %d notes without a usable witness (none, or not at a chain anchor): %s%s\n",
|
||||
opid, spendPlan.unusable.size(), some, spendPlan.unusable.size() > 5 ? ", ..." : "");
|
||||
}
|
||||
if (!spendPlan.deferred.empty())
|
||||
LogPrintf("%s: deferring %d notes whose witness is at another anchor than the rest of their address\n",
|
||||
opid, spendPlan.deferred.size());
|
||||
|
||||
// Lock just the notes this round spends, before the locks are released for proving, so a send
|
||||
// made meanwhile neither picks them nor goes without the rest of the wallet's notes.
|
||||
@@ -339,6 +434,18 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() {
|
||||
}
|
||||
LogPrint("zrpc", "%s: %d consolidation input notes locked for %d transactions\n", opid, z_sapling_inputs_.size(), txs.size());
|
||||
|
||||
// A tx that fails ends its own address's part of the round, not the other addresses'. The round's error
|
||||
// is the first such failure.
|
||||
std::set<size_t> failedGroups;
|
||||
auto failAddress = [&](size_t g, const std::string& why) {
|
||||
LogPrintf("%s: %s; skipping the rest of this round for address %s\n",
|
||||
opid, why, groups[g].addr.GetHash().ToString());
|
||||
failedGroups.insert(g);
|
||||
if (tally.error.empty())
|
||||
tally.error = why;
|
||||
status = false;
|
||||
};
|
||||
|
||||
for (const PlannedTx& planned : txs) {
|
||||
// The queue's cancel at shutdown never reaches an executing op (AsyncRPCOperation::cancel() is
|
||||
// not virtual), so look for the shutdown here, or a stop would wait for the whole round.
|
||||
@@ -356,6 +463,8 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() {
|
||||
tally.yielded = true;
|
||||
break;
|
||||
}
|
||||
if (failedGroups.count(planned.group))
|
||||
continue;
|
||||
const AddressNotes& group = groups[planned.group];
|
||||
|
||||
auto builder = TransactionBuilder(consensusParams, tipHeight, pwalletMain);
|
||||
@@ -364,8 +473,7 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() {
|
||||
CAmount amountToSend = 0;
|
||||
for (const SaplingNoteEntry& entry : planned.notes)
|
||||
amountToSend += CAmount(entry.note.value());
|
||||
// Notes worth no more than the fee go fee-free rather than being burnt into it.
|
||||
CAmount thisTxFee = amountToSend <= request_.fee ? 0 : request_.fee;
|
||||
CAmount thisTxFee = SaplingConsolidationTxFee(amountToSend, request_.fee);
|
||||
CAmount actualAmountToSend = amountToSend - thisTxFee;
|
||||
LogPrintf("%s: %s Beginning to create transaction with %d Sapling inputs, output amount=%s\n",
|
||||
__func__, opid, planned.notes.size(), FormatMoney(actualAmountToSend));
|
||||
@@ -380,10 +488,8 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() {
|
||||
LogPrint("zrpcunsafe", "%s: Added consolidation input %d\n", opid, i);
|
||||
}
|
||||
if (!spendsOk) {
|
||||
LogPrintf("%s: Consolidation inputs do not share one anchor! Stopping.\n", opid);
|
||||
tally.error = "inputs do not share one anchor";
|
||||
status=false;
|
||||
break;
|
||||
failAddress(planned.group, "inputs do not share one anchor");
|
||||
continue;
|
||||
}
|
||||
|
||||
LogPrint("zrpcunsafe", "%s: Using fee=%d\n", opid, thisTxFee);
|
||||
@@ -408,23 +514,20 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() {
|
||||
// actually add our sietch zoutput, the new way
|
||||
builder.AddSaplingOutput(group.extsk.expsk.ovk, sietchZoutput, amount);
|
||||
} else {
|
||||
LogPrintf("%s: Invalid payment address %s! Stopping.\n", opid, zdust);
|
||||
LogPrintf("%s: Invalid payment address %s!\n", opid, zdust);
|
||||
zoutsOk = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!zoutsOk) {
|
||||
tally.error = "invalid sietch address";
|
||||
status = false;
|
||||
break;
|
||||
failAddress(planned.group, "invalid sietch address");
|
||||
continue;
|
||||
}
|
||||
LogPrint("zrpcunsafe", "%s: Done adding %d sietch zouts\n", opid, MIN_ZOUTS);
|
||||
auto maybe_tx = builder.Build();
|
||||
if (!maybe_tx) {
|
||||
LogPrintf("%s: Failed to build transaction.\n",opid);
|
||||
tally.error = "failed to build transaction";
|
||||
status=false;
|
||||
break;
|
||||
failAddress(planned.group, "failed to build transaction");
|
||||
continue;
|
||||
}
|
||||
CTransaction tx = maybe_tx.get();
|
||||
|
||||
@@ -444,9 +547,8 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() {
|
||||
tally.fees += thisTxFee;
|
||||
} else {
|
||||
LogPrintf("%s: Consolidation transaction FAILED in CommitTransaction, txid=%s\n",opid , tx.GetHash().ToString());
|
||||
tally.error = "transaction was not accepted (CommitTransaction failed)";
|
||||
status = false;
|
||||
break;
|
||||
failAddress(planned.group, "transaction was not accepted (CommitTransaction failed)");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,12 @@
|
||||
#include "wallet.h" // for SaplingNoteEntry
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
|
||||
//Default fee used for consolidation transactions, in puposhis
|
||||
static const CAmount DEFAULT_CONSOLIDATION_FEE = 10000;
|
||||
extern CAmount fConsolidationTxFee;
|
||||
@@ -54,6 +58,35 @@ std::vector<SaplingConsolidationBatch> PlanSaplingConsolidation(
|
||||
const SaplingConsolidationPolicy& policy,
|
||||
const std::function<uint64_t(uint64_t)>& rng);
|
||||
|
||||
// A round planned over the notes it can spend (see PlanSaplingConsolidationSpends). Note indices are the
|
||||
// caller's: into the address's values as given.
|
||||
struct SaplingConsolidationSpendPlan {
|
||||
std::vector<SaplingConsolidationBatch> batches;
|
||||
std::map<size_t, uint256> anchors; // per address with batches: the anchor of all its spends
|
||||
std::vector<std::pair<size_t, size_t>> unusable; // (address, note): no witness, or one at no chain anchor
|
||||
std::vector<std::pair<size_t, size_t>> deferred; // (address, note): witness at another chain anchor
|
||||
};
|
||||
|
||||
// Plans a round as PlanSaplingConsolidation does, but only over notes that can be spent together. rootOf(g, i)
|
||||
// is the root of note i of address g's witness (none: there is none), isChainAnchor(root) whether the chain
|
||||
// holds that root as a Sapling anchor, which is what consensus checks a spend's anchor against. An address's
|
||||
// spends all use one anchor: preferredAnchor (the tip's root, where the wallet keeps its witnesses) when one
|
||||
// of its planned notes is there, else the chain anchor most of them share. A planned note whose witness is
|
||||
// missing or at no chain anchor is left out as unusable; one at another chain anchor is deferred to a later
|
||||
// round. The round is then planned again without them, until no planned note is left out. Planned notes on a
|
||||
// root the chain never had therefore can't outvote the address's good ones, however many they are. rootOf is
|
||||
// called at most once per note and isChainAnchor at most once per root.
|
||||
SaplingConsolidationSpendPlan PlanSaplingConsolidationSpends(
|
||||
const std::vector<std::vector<CAmount>>& groups,
|
||||
const SaplingConsolidationPolicy& policy,
|
||||
const std::function<uint64_t(uint64_t)>& rng,
|
||||
const std::function<boost::optional<uint256>(size_t, size_t)>& rootOf,
|
||||
const std::function<bool(const uint256&)>& isChainAnchor,
|
||||
const uint256& preferredAnchor);
|
||||
|
||||
// The fee of a consolidation tx spending amount: the fee, or none when the notes are worth no more than it.
|
||||
CAmount SaplingConsolidationTxFee(CAmount amount, CAmount fee);
|
||||
|
||||
// What a consolidation operation is asked to do, fixed when it is enqueued.
|
||||
struct SaplingConsolidationRequest {
|
||||
bool manual = false; // z_consolidate rather than the -consolidation scheduler
|
||||
|
||||
+17
-10
@@ -2402,6 +2402,20 @@ bool CWallet::IsSaplingNullifierFromMe(const uint256& nullifier) const
|
||||
return false;
|
||||
}
|
||||
|
||||
boost::optional<SaplingWitness> CWallet::GetSaplingNoteLatestWitness(const SaplingOutPoint& note) const
|
||||
{
|
||||
LOCK(cs_wallet);
|
||||
// find(), not operator[]: a lookup must not default-construct mapWallet / mapSaplingNoteData entries.
|
||||
auto wi = mapWallet.find(note.hash);
|
||||
if (wi == mapWallet.end())
|
||||
return boost::none;
|
||||
const mapSaplingNoteData_t& noteData = wi->second.mapSaplingNoteData;
|
||||
auto ni = noteData.find(note);
|
||||
if (ni == noteData.end() || ni->second.witnesses.empty())
|
||||
return boost::none;
|
||||
return ni->second.witnesses.front();
|
||||
}
|
||||
|
||||
void CWallet::GetSaplingNoteWitnesses(std::vector<SaplingOutPoint> notes,
|
||||
std::vector<boost::optional<SaplingWitness>>& witnesses,
|
||||
uint256 &final_anchor)
|
||||
@@ -2416,16 +2430,9 @@ void CWallet::GetSaplingNoteWitnesses(std::vector<SaplingOutPoint> notes,
|
||||
std::vector<boost::optional<SaplingWitness>> cand(notes.size());
|
||||
std::map<uint256, int> rootVotes;
|
||||
for (size_t i = 0; i < notes.size(); i++) {
|
||||
const SaplingOutPoint& note = notes[i];
|
||||
auto wi = mapWallet.find(note.hash);
|
||||
if (wi == mapWallet.end())
|
||||
continue;
|
||||
const mapSaplingNoteData_t& noteData = wi->second.mapSaplingNoteData;
|
||||
auto ni = noteData.find(note);
|
||||
if (ni == noteData.end() || ni->second.witnesses.empty())
|
||||
continue;
|
||||
cand[i] = ni->second.witnesses.front();
|
||||
rootVotes[cand[i]->root()]++;
|
||||
cand[i] = GetSaplingNoteLatestWitness(notes[i]);
|
||||
if (cand[i])
|
||||
rootVotes[cand[i]->root()]++;
|
||||
}
|
||||
|
||||
// Choose the anchor that the most witnesses agree on (robust even when the FIRST note is the
|
||||
|
||||
@@ -1360,6 +1360,8 @@ public:
|
||||
std::vector<SaplingOutPoint> notes,
|
||||
std::vector<boost::optional<SaplingWitness>>& witnesses,
|
||||
uint256 &final_anchor);
|
||||
//! The note's newest cached witness, whatever its root; none if the wallet has none for it.
|
||||
boost::optional<SaplingWitness> GetSaplingNoteLatestWitness(const SaplingOutPoint& note) const;
|
||||
|
||||
isminetype IsMine(const CTxIn& txin) const;
|
||||
CAmount GetDebit(const CTxIn& txin, const isminefilter& filter) const;
|
||||
|
||||
Reference in New Issue
Block a user