changeset 23704:18da788aaa57

fix miner sigop generation test
author Andrew Stone <g.andrew.stone@gmail.com>
date Tue, 23 Feb 2016 22:35:54 -0500
parents 5f9289efe67a
children bac3c81409ed
files src/main.cpp src/main.h src/miner.cpp src/unlimited.cpp src/unlimited.h
diffstat 5 files changed, 40 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.cpp	Tue Feb 23 21:32:59 2016 -0500
+++ b/src/main.cpp	Tue Feb 23 22:35:54 2016 -0500
@@ -2950,7 +2950,7 @@
     return true;
 }
 
-bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bool fCheckMerkleRoot)
+bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bool fCheckMerkleRoot, bool fConservative)
 {
     // These are checks that are independent of context.
 
@@ -3012,9 +3012,9 @@
         nSigOps += GetLegacySigOpCount(tx);
     }
 
-    // if (nSigOps > MAX_BLOCK_SIGOPS)
-    //    return state.DoS(100, error("CheckBlock(): out-of-bounds SigOpCount"),
-    //                     REJECT_INVALID, "bad-blk-sigops", true);
+    if (fConservative && (nSigOps > BLOCKSTREAM_CORE_MAX_BLOCK_SIGOPS))  // BU only enforce sigops during block generation not acceptance
+      return state.DoS(100, error("CheckBlock(): out-of-bounds SigOpCount"),
+        REJECT_INVALID, "bad-blk-sigops", true);
 
     if (fCheckPOW && fCheckMerkleRoot)
         block.fChecked = true;
@@ -3024,7 +3024,7 @@
     return true;
 }
 
-static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash)
+bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash)
 {
     if (*pindexPrev->phashBlock == chainparams.GetConsensus().hashGenesisBlock)
         return true;
--- a/src/main.h	Tue Feb 23 21:32:59 2016 -0500
+++ b/src/main.h	Tue Feb 23 22:35:54 2016 -0500
@@ -417,7 +417,7 @@
 
 /** Context-independent validity checks */
 bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW = true);
-bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
+bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW = true, bool fCheckMerkleRoot = true, bool conservative = false);
 
 /** Context-dependent validity checks */
 bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex *pindexPrev);
@@ -426,7 +426,8 @@
 /** Check a block is completely valid from start to finish (only works on top of our current best block, with cs_main held) */
 bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
 
-// BU needed?
+// BU needed in unlimited.cpp
+bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash);
 /** Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
 bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex **pindex, bool fRequested, CDiskBlockPos* dbp);
 bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex **ppindex= NULL);
--- a/src/miner.cpp	Tue Feb 23 21:32:59 2016 -0500
+++ b/src/miner.cpp	Tue Feb 23 22:35:54 2016 -0500
@@ -294,7 +294,7 @@
         pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
 
         CValidationState state;
-        if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
+        if (!TestConservativeBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
             throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
         }
     }
--- a/src/unlimited.cpp	Tue Feb 23 21:32:59 2016 -0500
+++ b/src/unlimited.cpp	Tue Feb 23 22:35:54 2016 -0500
@@ -702,3 +702,30 @@
         }
     }
 }
+
+// Similar to TestBlockValidity but is very conservative in parameters (used in mining)
+bool TestConservativeBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
+{
+    AssertLockHeld(cs_main);
+    assert(pindexPrev && pindexPrev == chainActive.Tip());
+    if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, block.GetHash()))
+        return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
+
+    CCoinsViewCache viewNew(pcoinsTip);
+    CBlockIndex indexDummy(block);
+    indexDummy.pprev = pindexPrev;
+    indexDummy.nHeight = pindexPrev->nHeight + 1;
+
+    // NOTE: CheckBlockHeader is called by CheckBlock
+    if (!ContextualCheckBlockHeader(block, state, pindexPrev))
+        return false;
+    if (!CheckBlock(block, state, fCheckPOW, fCheckMerkleRoot, true))
+        return false;
+    if (!ContextualCheckBlock(block, state, pindexPrev))
+        return false;
+    if (!ConnectBlock(block, state, &indexDummy, viewNew, true))
+        return false;
+    assert(state.IsValid());
+
+    return true;
+}
--- a/src/unlimited.h	Tue Feb 23 21:32:59 2016 -0500
+++ b/src/unlimited.h	Tue Feb 23 22:35:54 2016 -0500
@@ -22,6 +22,7 @@
 class CValidationState;
 class CDiskBlockPos;
 class CNode;
+class CChainParams;
 
 extern uint64_t maxGeneratedBlock;
 extern unsigned int excessiveBlockSize;
@@ -39,6 +40,9 @@
 
 extern void UnlimitedLogBlock(const CBlock& block, const std::string& hash, uint64_t receiptTime);
 
+// used during mining
+extern bool TestConservativeBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot);
+
 // Check whether this block is bigger in some metric than we really want to accept
 extern bool CheckExcessive(const CBlock& block, uint64_t blockSize, uint64_t nSigOps, uint64_t nTx);