From fd9118ce7611df8fcfd664bbf2bbb0a524c48fe0 Mon Sep 17 00:00:00 2001 From: Andy Oknen Date: Tue, 7 Jan 2025 14:28:05 +0800 Subject: [PATCH] Refactor transaction logging in BlockAssembler - Updated logging categories from STAKEMODIF to SELECTCOINS for better clarity in transaction handling. - Added checks for spent coins and maturity of previous outputs in the TestTransaction method, enhancing transaction validation. - Improved logging for skipped transactions in both TestTransaction and TestPackageTransactions methods, providing more detailed insights into transaction processing decisions. These changes aim to improve the debugging process and ensure more robust transaction validation within the mining workflow. --- src/miner.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index f52541eb2..d0f59101f 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -239,7 +239,7 @@ bool BlockAssembler::TestTransaction(const CTransactionRef& tx, PocketBlockRef& // Payload should be in operative table Transactions if (!ptx) { - LogPrint(BCLog::STAKEMODIF, "Warning: build block skip transaction %s with result 'NOT FOUND'\n", + LogPrint(BCLog::SELECTCOINS, "Warning: build block skip transaction %s with result 'NOT FOUND'\n", tx->GetHash().GetHex()); return false; @@ -248,7 +248,7 @@ bool BlockAssembler::TestTransaction(const CTransactionRef& tx, PocketBlockRef& // Check consensus if (auto[ok, result] = PocketConsensus::SocialConsensusHelper::Check(tx, ptx, ChainActive().Height() + 1); !ok) { - LogPrint(BCLog::STAKEMODIF, "Warning: build block skip transaction %s with check result %d\n", + LogPrint(BCLog::SELECTCOINS, "Warning: build block skip transaction %s with check result %d\n", tx->GetHash().GetHex(), (int) result); return false; @@ -257,11 +257,30 @@ bool BlockAssembler::TestTransaction(const CTransactionRef& tx, PocketBlockRef& // Validate consensus if (auto[ok, result] = PocketConsensus::SocialConsensusHelper::Validate(tx, ptx, pblockTemplate, ChainActive().Height() + 1); !ok) { - LogPrint(BCLog::STAKEMODIF, "Warning: build block skip transaction %s with validate result %d\n", + LogPrint(BCLog::SELECTCOINS, "Warning: build block skip transaction %s with validate result %d\n", tx->GetHash().GetHex(), (int) result); return false; } + + CCoinsViewCache view(&::ChainstateActive().CoinsTip()); + for (unsigned int i = 0; i < tx->vin.size(); ++i) { + const COutPoint &prevout = tx->vin[i].prevout; + const Coin& coin = view.AccessCoin(prevout); + if (coin.IsSpent()) + { + LogPrint(BCLog::SELECTCOINS, "Warning: build block skip transaction: tx - %s, prev - %s is spent\n", tx->GetHash().GetHex(), prevout.hash.GetHex()); + return false; + } + + // If prev is pocketnet, check that it's matured + if (ChainActive().Height() + 1 - coin.nHeight < POCKETNET_MATURITY) + { + LogPrint(BCLog::SELECTCOINS, "Warning: build block skip transaction: tx - %s, prev - %s, %d - %d = %d < %d\n", + tx->GetHash().GetHex(), prevout.hash.GetHex(), ChainActive().Height() + 1, coin.nHeight, ChainActive().Height() + 1 - coin.nHeight, POCKETNET_MATURITY); + return false; + } + } // All is good - save for descendants pblockTemplate->push_back(ptx); @@ -289,10 +308,12 @@ bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& packa { if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff)) { + LogPrint(BCLog::SELECTCOINS, "Warning: build block skip transaction: tx - %s, height - %d, locktime - %d\n", it->GetTx().GetHash().GetHex(), nHeight, nLockTimeCutoff); return false; } if (!fIncludeWitness && it->GetTx().HasWitness()) { + LogPrint(BCLog::SELECTCOINS, "Warning: build block skip transaction: tx - %s, height - %d, HasWitness\n", it->GetTx().GetHash().GetHex(), nHeight); return false; } } @@ -473,7 +494,7 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda if (packageFees < minFee) { - LogPrintf("tx - %s, fee - %s, minFee - %s\n", iter->GetTx().GetHash().GetHex(), packageFees, minFee); + LogPrint(BCLog::SELECTCOINS, "Warning: build block skip transaction %s, fee (%s) < minFee (%s)\n", iter->GetTx().GetHash().GetHex(), packageFees, minFee); if (fUsingModified) {