diff --git a/src/miner.h b/src/miner.h --- a/src/miner.h +++ b/src/miner.h @@ -26,11 +26,17 @@ struct CBlockTemplateEntry { CTransactionRef tx; - Amount fees; - int64_t sigOpCount; - - CBlockTemplateEntry(CTransactionRef _tx, Amount _fees, int64_t _sigOpCount) - : tx(_tx), fees(_fees), sigOpCount(_sigOpCount){}; + //!< Total real fees paid by the transaction and cached to avoid parent + //!< lookup + Amount txFee; + //!< Cached total size of the transaction to avoid reserializing transaction + size_t txSize; + //!< Cached total number of SigOps + uint64_t txSigOps; + + CBlockTemplateEntry(CTransactionRef _tx, Amount _fees, uint64_t _size, + int64_t _sigOps) + : tx(_tx), txFee(_fees), txSize(_size), txSigOps(_sigOps) {} }; struct CBlockTemplate { diff --git a/src/miner.cpp b/src/miner.cpp --- a/src/miner.cpp +++ b/src/miner.cpp @@ -140,7 +140,7 @@ pblock = &pblocktemplate->block; // Add dummy coinbase tx as first transaction. It is updated at the end. - pblocktemplate->entries.emplace_back(CTransactionRef(), -SATOSHI, -1); + pblocktemplate->entries.emplace_back(CTransactionRef(), -SATOSHI, 0, -1); LOCK2(cs_main, mempool->cs); CBlockIndex *pindexPrev = chainActive.Tip(); @@ -204,7 +204,11 @@ } pblocktemplate->entries[0].tx = MakeTransactionRef(coinbaseTx); - pblocktemplate->entries[0].fees = -1 * nFees; + // Note: For the Coinbase, the template entry fields aside from the `tx` are + // not used anywhere at the time of writing. The mining rpc throws out the + // entire transaction in fact. The tx itself is only used during regtest + // mode. + pblocktemplate->entries[0].txFee = -1 * nFees; uint64_t nSerializeSize = GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION); @@ -217,7 +221,7 @@ UpdateTime(pblock, *config, pindexPrev); pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, *config); pblock->nNonce = 0; - pblocktemplate->entries[0].sigOpCount = GetSigOpCountWithoutP2SH( + pblocktemplate->entries[0].txSigOps = GetSigOpCountWithoutP2SH( *pblocktemplate->entries[0].tx, STANDARD_CHECKDATASIG_VERIFY_FLAGS); // Copy all the transactions into the block @@ -355,6 +359,7 @@ void BlockAssembler::AddToBlock(CTxMemPool::txiter iter) { pblocktemplate->entries.emplace_back(iter->GetSharedTx(), iter->GetFee(), + iter->GetTxSize(), iter->GetSigOpCount()); nBlockSize += iter->GetTxSize(); ++nBlockTx; diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -628,10 +628,9 @@ entry.pushKV("data", EncodeHexTx(tx)); entry.pushKV("txid", txId.GetHex()); entry.pushKV("hash", tx.GetHash().GetHex()); - entry.pushKV("fee", - pblocktemplate->entries[index_in_template].fees / SATOSHI); - int64_t nTxSigOps = - pblocktemplate->entries[index_in_template].sigOpCount; + entry.pushKV("fee", pblocktemplate->entries[index_in_template].txFee / + SATOSHI); + int64_t nTxSigOps = pblocktemplate->entries[index_in_template].txSigOps; entry.pushKV("sigops", nTxSigOps); transactions.push_back(entry);