Compare commits
2 Commits
1ec6590fb7
...
7a62fc4877
| Author | SHA1 | Date | |
|---|---|---|---|
| 7a62fc4877 | |||
| 9a8f17b2c8 |
@@ -22,6 +22,18 @@ extern std::string randomSietchZaddr();
|
|||||||
|
|
||||||
// Serialized-size estimates for one spent input (kept in sync with rpcwallet.cpp)
|
// Serialized-size estimates for one spent input (kept in sync with rpcwallet.cpp)
|
||||||
static const size_t AUTOSHIELD_CTXIN_DUST_SIZE = 148;
|
static const size_t AUTOSHIELD_CTXIN_DUST_SIZE = 148;
|
||||||
|
// Every autoshield tx carries THREE Sapling OutputDescriptions -- the change
|
||||||
|
// note to destZaddr plus the two Sietch dummies -- at ~948 bytes each. Reserving
|
||||||
|
// 2000 for "header + sietch outputs" was ~900 bytes short before a single input
|
||||||
|
// was counted, so a large enough round could build a tx over MAX_TX_SIZE.
|
||||||
|
static const size_t AUTOSHIELD_SAPLING_OUTPUT_SIZE = 948;
|
||||||
|
static const size_t AUTOSHIELD_TX_OVERHEAD = (3 * AUTOSHIELD_SAPLING_OUTPUT_SIZE) + 256;
|
||||||
|
// Hard cap on inputs per round, mirroring z_shieldcoinbase's
|
||||||
|
// SHIELD_COINBASE_DEFAULT_LIMIT. The byte estimate alone is not a safe bound:
|
||||||
|
// with a P2PKH coinbase (-mineraddress) the 148-byte figure is exact rather than
|
||||||
|
// conservative, so an under-estimate translates directly into an oversize tx.
|
||||||
|
// The remainder is simply shielded on the next round.
|
||||||
|
static const size_t AUTOSHIELD_MAX_INPUTS = 400;
|
||||||
static const size_t AUTOSHIELD_CTXIN_P2SH_SIZE = 400;
|
static const size_t AUTOSHIELD_CTXIN_P2SH_SIZE = 400;
|
||||||
// Expire unmined autoshield txs after this many blocks, so a tx cannot straddle
|
// Expire unmined autoshield txs after this many blocks, so a tx cannot straddle
|
||||||
// a network-upgrade activation.
|
// a network-upgrade activation.
|
||||||
@@ -258,6 +270,27 @@ bool AsyncRPCOperation_autoshieldcoinbase::main_impl() {
|
|||||||
libzcash::SaplingPaymentAddress destZaddr;
|
libzcash::SaplingPaymentAddress destZaddr;
|
||||||
std::string destStr;
|
std::string destStr;
|
||||||
std::vector<ShieldCoinbaseUTXO> inputs;
|
std::vector<ShieldCoinbaseUTXO> inputs;
|
||||||
|
|
||||||
|
// Proof building below runs WITHOUT cs_wallet (deliberately, so wallet RPCs
|
||||||
|
// are not stalled), which leaves a multi-second window in which a manual
|
||||||
|
// z_shieldcoinbase or z_sendmany over the same miner address would re-select
|
||||||
|
// these same coinbase outputs. AvailableCoins honours IsLockedCoin, so lock
|
||||||
|
// them for the duration exactly as z_shieldcoinbase does. RAII because there
|
||||||
|
// are several early returns between here and commit, and a leaked lock would
|
||||||
|
// silently exclude those coins from every future round.
|
||||||
|
struct ScopedCoinLocks {
|
||||||
|
std::vector<COutPoint> locked;
|
||||||
|
~ScopedCoinLocks() {
|
||||||
|
// A destructor is noexcept by default; letting the lock acquisition
|
||||||
|
// escape would turn a contended mutex into std::terminate.
|
||||||
|
try {
|
||||||
|
if (locked.empty()) return;
|
||||||
|
LOCK2(cs_main, pwalletMain->cs_wallet);
|
||||||
|
// UnlockCoin takes a non-const reference (upstream signature).
|
||||||
|
for (COutPoint& op : locked) pwalletMain->UnlockCoin(op);
|
||||||
|
} catch (...) {}
|
||||||
|
}
|
||||||
|
} coinLocks;
|
||||||
CAmount shieldedValue = 0;
|
CAmount shieldedValue = 0;
|
||||||
unsigned int max_tx_size = MAX_TX_SIZE_AFTER_SAPLING;
|
unsigned int max_tx_size = MAX_TX_SIZE_AFTER_SAPLING;
|
||||||
|
|
||||||
@@ -280,7 +313,7 @@ bool AsyncRPCOperation_autoshieldcoinbase::main_impl() {
|
|||||||
// AvailableCoins with fOnlySpendable already excludes immature coinbase
|
// AvailableCoins with fOnlySpendable already excludes immature coinbase
|
||||||
// (< COINBASE_MATURITY) and outputs we don't own, so external
|
// (< COINBASE_MATURITY) and outputs we don't own, so external
|
||||||
// -mineraddress / pool coinbase naturally yields zero inputs.
|
// -mineraddress / pool coinbase naturally yields zero inputs.
|
||||||
size_t estimatedTxSize = 2000; // header + sietch outputs headroom
|
size_t estimatedTxSize = AUTOSHIELD_TX_OVERHEAD;
|
||||||
std::vector<COutput> vecOutputs;
|
std::vector<COutput> vecOutputs;
|
||||||
pwalletMain->AvailableCoins(vecOutputs, true, NULL, false, true);
|
pwalletMain->AvailableCoins(vecOutputs, true, NULL, false, true);
|
||||||
for (const COutput& out : vecOutputs) {
|
for (const COutput& out : vecOutputs) {
|
||||||
@@ -293,6 +326,11 @@ bool AsyncRPCOperation_autoshieldcoinbase::main_impl() {
|
|||||||
}
|
}
|
||||||
size_t increase = (boost::get<CScriptID>(&address) != nullptr)
|
size_t increase = (boost::get<CScriptID>(&address) != nullptr)
|
||||||
? AUTOSHIELD_CTXIN_P2SH_SIZE : AUTOSHIELD_CTXIN_DUST_SIZE;
|
? AUTOSHIELD_CTXIN_P2SH_SIZE : AUTOSHIELD_CTXIN_DUST_SIZE;
|
||||||
|
if (inputs.size() >= AUTOSHIELD_MAX_INPUTS) {
|
||||||
|
LogPrintf("%s: reached per-round input cap (%d); deferring remaining coinbase to next round\n",
|
||||||
|
opid, (int)AUTOSHIELD_MAX_INPUTS);
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (estimatedTxSize + increase >= max_tx_size) {
|
if (estimatedTxSize + increase >= max_tx_size) {
|
||||||
// Size-safe batch; the remainder is shielded next round.
|
// Size-safe batch; the remainder is shielded next round.
|
||||||
LogPrintf("%s: reached per-tx size cap; deferring remaining coinbase to next round\n", opid);
|
LogPrintf("%s: reached per-tx size cap; deferring remaining coinbase to next round\n", opid);
|
||||||
@@ -305,6 +343,12 @@ bool AsyncRPCOperation_autoshieldcoinbase::main_impl() {
|
|||||||
inputs.push_back(utxo);
|
inputs.push_back(utxo);
|
||||||
shieldedValue += out.tx->vout[out.i].nValue;
|
shieldedValue += out.tx->vout[out.i].nValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const ShieldCoinbaseUTXO& t : inputs) {
|
||||||
|
COutPoint outpt(t.txid, t.vout);
|
||||||
|
pwalletMain->LockCoin(outpt);
|
||||||
|
coinLocks.locked.push_back(outpt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CAmount fee = pwalletMain->autoShieldFee;
|
CAmount fee = pwalletMain->autoShieldFee;
|
||||||
|
|||||||
@@ -3408,6 +3408,12 @@ UniValue z_autoshieldstatus(const UniValue& params, bool fHelp, const CPubKey& m
|
|||||||
why = "disabled by -autoshield=0";
|
why = "disabled by -autoshield=0";
|
||||||
} else if (pwalletMain->IsLocked()) {
|
} else if (pwalletMain->IsLocked()) {
|
||||||
why = "wallet is locked; rounds are skipped until it is unlocked";
|
why = "wallet is locked; rounds are skipped until it is unlocked";
|
||||||
|
} else if (pwalletMain->fSweepRunning || pwalletMain->fConsolidationRunning) {
|
||||||
|
// Autoshield is mutually exclusive with sweep and consolidation. Without
|
||||||
|
// this the RPC reports autoshield=true, running=false and an empty
|
||||||
|
// reason while no round can actually start.
|
||||||
|
why = strprintf("deferred while %s is running; rounds resume when it finishes",
|
||||||
|
pwalletMain->fSweepRunning ? "z_sweep" : "sapling consolidation");
|
||||||
} else if (pwalletMain->autoShieldAddress.empty()) {
|
} else if (pwalletMain->autoShieldAddress.empty()) {
|
||||||
why = "";
|
why = "";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -592,6 +592,31 @@ void CWallet::RunSaplingSweep(int blockHeight) {
|
|||||||
// masked an unsynchronized mutation.) cs_wallet is recursive, so this is
|
// masked an unsynchronized mutation.) cs_wallet is recursive, so this is
|
||||||
// safe even on any path that already holds it.
|
// safe even on any path that already holds it.
|
||||||
LOCK(cs_wallet);
|
LOCK(cs_wallet);
|
||||||
|
|
||||||
|
// Stale-baton guard. A successful-but-incomplete sweep round deliberately
|
||||||
|
// returns with fSweepRunning still set and nextSweep unadvanced (see
|
||||||
|
// AsyncRPCOperation_sweep::main), as a "continue draining next block" baton.
|
||||||
|
// But every early return below leaves that baton set WITHOUT re-dispatching,
|
||||||
|
// and RunSaplingConsolidation -- which is gated on fSweepRunning -- then
|
||||||
|
// returns without advancing nextConsolidation, so the "consolidation is
|
||||||
|
// within 5 blocks" blackout at the top of this function never lifts. That
|
||||||
|
// is a self-sustaining three-way deadlock: sweep waits on consolidation,
|
||||||
|
// consolidation waits on sweep, and autoshield shares the same gate, so a
|
||||||
|
// wedged sweep silently disables coinbase shielding forever.
|
||||||
|
// Only honour the baton while a sweep operation genuinely is in flight.
|
||||||
|
if (fSweepRunning) {
|
||||||
|
std::shared_ptr<AsyncRPCQueue> sweepQueue = getAsyncRPCQueue();
|
||||||
|
std::shared_ptr<AsyncRPCOperation> inFlightSweep =
|
||||||
|
(sweepQueue != nullptr) ? sweepQueue->getOperationForId(saplingSweepOperationId) : nullptr;
|
||||||
|
bool inFlight = (inFlightSweep != nullptr) &&
|
||||||
|
(inFlightSweep->isReady() || inFlightSweep->isExecuting());
|
||||||
|
if (!inFlight) {
|
||||||
|
LogPrintf("%s: clearing stale fSweepRunning at blockHeight=%d (no sweep operation in flight)\n",
|
||||||
|
__func__, blockHeight);
|
||||||
|
fSweepRunning = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!fSweepEnabled) {
|
if (!fSweepEnabled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user