From 74c068829967bbff60da3506613844df4da1ae80 Mon Sep 17 00:00:00 2001 From: bitflash-maintainer Date: Wed, 2 Sep 2026 14:14:48 +0000 Subject: mine: ask whether a transaction fits before appending it The node miner tested its size budget only on the outer while loop. The inner for walked all of mapTransactions with no test, so a single pass could append the entire mempool -- the overshoot was bounded by mempool size, not by one transaction as the comment claimed. The pool builder did test, but only after the push, which bounds the overshoot at one transaction. Nothing caps how big one transaction may be: neither CheckTransaction nor AcceptTransaction limits it, so the mempool will hold one of any size the wire allows and "one transaction" is worth up to MAX_SIZE. A lone 1.5 MB transaction offering a large fee is therefore enough to make every template unmineable, and the fee is never paid because the block never confirms. Both now ask before appending and skip what does not fit, so the overshoot is zero and an oversized transaction costs the template nothing rather than invalidating it. The budget moves to MAX_BLOCK_SIZE_GEN so the two assemblers cannot drift on it again -- drift is what produced this. The test is ordered before ConnectInputs on purpose: ConnectInputs credits nFees by reference on success, so declining a transaction after calling it would leave the coinbase claiming a fee the block does not collect. Assembly only. CheckBlock, CheckTransaction and AcceptTransaction are untouched: this changes what we produce, not what the network accepts. --- src/main.cpp | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 7b3e555..64eb290 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4470,7 +4470,6 @@ bool BitcoinMiner(int nThreadId) map mapTestPool; vector vfAlreadyAdded(mapTransactions.size()); bool fFoundSomething = true; - unsigned int nBlockSize = 0; // Assembly must stop below the consensus cap that CheckBlock() // enforces, or we mine a block our own node then rejects. // @@ -4481,9 +4480,19 @@ bool BitcoinMiner(int nThreadId) // meant solving a block and throwing it away -- with this pool at // ~90% of hashrate, that is the network stalling, not just us. // - // Half the cap, keeping the original margin: the test runs before - // a transaction is added, so the loop can overshoot by one tx. - while (fFoundSomething && nBlockSize < MAX_BLOCK_SIZE/2) + // The budget test then sat only on this outer while, and the + // comment claimed that bounded the overshoot to a single + // transaction. It did not. The inner for walks all of + // mapTransactions with no test of its own, so one pass could append + // the entire mempool: the overshoot was bounded by mempool size, + // which is to say not bounded at all. Ask per transaction, before + // appending, and the overshoot is zero rather than small. + // + // Seeded with what the block already weighs -- header, tx count, + // coinbase -- because CheckBlock() weighs the whole serialized + // block and not just the transactions we chose to add to it. + unsigned int nBlockSize = ::GetSerializeSize(*pblock, SER_DISK); + while (fFoundSomething && nBlockSize < MAX_BLOCK_SIZE_GEN) { fFoundSomething = false; unsigned int n = 0; @@ -4495,6 +4504,28 @@ bool BitcoinMiner(int nThreadId) if (tx.IsCoinBase() || !tx.IsFinal()) continue; + // Skip, not break: a transaction too big for the room left + // says nothing about the next one, which may still fit. + // + // This is also the only ceiling on a single transaction's + // size anywhere on the path into a block. CheckTransaction + // does not cap it and neither does AcceptTransaction, so + // the mempool will hold one of any size the wire allows; + // without this test a lone 1.5 MB transaction offering a + // fat fee is selected, and every template built from that + // mempool is one CheckBlock() throws away. The fee is never + // paid, because the block never confirms. + // + // Deliberately before ConnectInputs and not after: + // ConnectInputs adds this transaction's fee to nFees by + // reference on success. Passing it and then declining the + // transaction would credit the coinbase for a fee no + // transaction in the block pays, and the block would be + // rejected for a wholly different reason. + unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK); + if (!BlockTemplateFits(nBlockSize, nTxSize)) + continue; + // Transaction fee requirements, mainly only needed for flood control // Under 10K (about 80 inputs) is free for first 100 transactions // Base rate is 0.01 per KB @@ -4506,7 +4537,7 @@ bool BitcoinMiner(int nThreadId) swap(mapTestPool, mapTestPoolTmp); pblock->vtx.push_back(tx); - nBlockSize += ::GetSerializeSize(tx, SER_NETWORK); + nBlockSize += nTxSize; vfAlreadyAdded[n] = true; fFoundSomething = true; } -- cgit v1.2.3