From 80c81aef63272231fc39c2af4b8db9f3f2e9d328 Mon Sep 17 00:00:00 2001 From: sunerok Date: Fri, 6 Apr 2018 23:28:07 +0200 Subject: [PATCH] Force different algos --- src/main.cpp | 42 ++++++++++++++++++++++++++++-------------- src/main.h | 4 +++- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 453e2e7ba..d8018b1fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2285,6 +2285,26 @@ bool CBlock::CheckBlock(bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig) c return true; } +bool CBlock::CheckPrevAlgo(CBlockIndex* pIndex) +{ + unsigned checkedBlocks = 0; + unsigned sameAlgoBlocks = 0; + + while (pIndex && checkedBlocks != 2*SAME_ALGO_MAX_COUNT) + { + if (::GetAlgo(pIndex->nVersion) == GetAlgo()) + ++sameAlgoBlocks; + + ++checkedBlocks; + pIndex = pIndex->pprev; + } + + if (sameAlgoBlocks > SAME_ALGO_MAX_COUNT) + return false; + + return true; +} + bool CBlock::AcceptBlock() { // Check for duplicate @@ -2299,25 +2319,19 @@ bool CBlock::AcceptBlock() CBlockIndex* pindexPrev = (*mi).second; int nHeight = pindexPrev->nHeight+1; + if (nHeight > ALGO_RULES_SWITCH_BLOCK) + { + if (!CheckPrevAlgo(pindexPrev)) + return error("AcceptBlock() : too many blocks found using same algorithm"); + } + // Check proof-of-work or proof-of-stake if (nBits != GetNextTargetRequired(pindexPrev, IsProofOfStake(), GetAlgo())) return DoS(100, error("AcceptBlock() : incorrect %s", IsProofOfWork() ? "proof-of-work" : "proof-of-stake")); // Check timestamp against prev - if (nHeight <= TIMESTAMP_RULES_SWITCH_BLOCK) - { - const unsigned oldMaxDrift = 7200; //2 hours - if (GetBlockTime() <= pindexPrev->GetMedianTimePast() || GetBlockTime() + oldMaxDrift < pindexPrev->GetBlockTime()) - return error("AcceptBlock() : block's timestamp is too early"); - } - else - { - if (GetBlockTime() < pindexPrev->GetBlockTime()) - return error("AcceptBlock() : block's timestamp is too early"); - - if (GetBlockTime() > GetAdjustedTime() + nMaxClockDrift) - return error("AcceptBlock() : block too much in the future"); - } + if (GetBlockTime() <= pindexPrev->GetMedianTimePast() || GetBlockTime() + nMaxClockDrift < pindexPrev->GetBlockTime()) + return error("AcceptBlock() : block's timestamp is too early"); // Check that all transactions are finalized BOOST_FOREACH(const CTransaction& tx, vtx) diff --git a/src/main.h b/src/main.h index 9856af00b..c6216c82b 100644 --- a/src/main.h +++ b/src/main.h @@ -31,7 +31,8 @@ class CNode; static const int MULTI_ALGO_SWITCH_BLOCK = 340000; static const int STEALTH_TX_SWITCH_BLOCK = 1824150; -static const int TIMESTAMP_RULES_SWITCH_BLOCK = 2040000; +static const int ALGO_RULES_SWITCH_BLOCK = 2042000; +static const int SAME_ALGO_MAX_COUNT = 5; static const unsigned int MAX_BLOCK_SIZE = 1000000; static const unsigned int MAX_BLOCK_SIZE_GEN = MAX_BLOCK_SIZE/2; static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50; @@ -1008,6 +1009,7 @@ class CBlock : public CBlockHeader } int GetAlgo() const { return ::GetAlgo(nVersion); } + bool CheckPrevAlgo(CBlockIndex* pIndex); IMPLEMENT_SERIALIZE (