Permalink
Browse files

active blockheader version 4.0

active blockheader version 4.0
  • Loading branch information...
cqtenq9@gmail.com
cqtenq9@gmail.com committed Apr 1, 2017
1 parent 4275165 commit bcc6a7f88763f5fd04107cb1eb9ea56ab8fbff70
Showing with 12 additions and 28 deletions.
  1. +0 −6 src/chain.h
  2. +11 −19 src/main.cpp
  3. +1 −3 src/primitives/block.h
View
@@ -144,7 +144,6 @@ class CBlockIndex
unsigned int nStatus;
//! block header
int cVersion;
int nVersion;
uint256 hashMerkleRoot;
unsigned int nTime;
@@ -174,7 +173,6 @@ class CBlockIndex
nStatus = 0;
nSequenceId = 0;
cVersion = 0;
nVersion = 0;
hashMerkleRoot = uint256();
nTime = 0;
@@ -193,7 +191,6 @@ class CBlockIndex
{
SetNull();
cVersion = block.cVersion;
nVersion = block.nVersion;
hashMerkleRoot = block.hashMerkleRoot;
nTime = block.nTime;
@@ -224,7 +221,6 @@ class CBlockIndex
CBlockHeader GetBlockHeader() const
{
CBlockHeader block;
block.cVersion = cVersion;
block.nVersion = nVersion;
if (pprev)
block.hashPrevBlock = pprev->GetBlockHash();
@@ -334,7 +330,6 @@ class CDiskBlockIndex : public CBlockIndex
READWRITE(VARINT(nUndoPos));
// block header
READWRITE(this->cVersion);
READWRITE(this->nVersion);
READWRITE(hashPrev);
READWRITE(hashMerkleRoot);
@@ -348,7 +343,6 @@ class CDiskBlockIndex : public CBlockIndex
uint256 GetBlockHash() const
{
CBlockHeader block;
block.cVersion = cVersion;
block.nVersion = nVersion;
block.hashPrevBlock = hashPrev;
block.hashMerkleRoot = hashMerkleRoot;
View
@@ -82,7 +82,7 @@ void EraseOrphansFor(NodeId peer);
* Returns true if there are nRequired or more blocks of minVersion or above
* in the last Consensus::Params::nMajorityWindow blocks, starting at pstart and going backwards.
*/
static bool IsSuperMajority(int minVersion, int min_cVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams);
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams);
static void CheckBlockIndex();
/** Constant stuff for coinbase transactions we create: */
@@ -2236,23 +2236,18 @@ void static UpdateTip(CBlockIndex *pindexNew) {
if (!IsInitialBlockDownload() && !fWarned)
{
int nUpgraded = 0;
int cUpgraded = 0;
const CBlockIndex* pindex = chainActive.Tip();
for (int i = 0; i < 500 && pindex != NULL; i++)
{
if (pindex->nVersion > CBlock::CURRENT_VERSION)
++nUpgraded;
if (pindex->cVersion > CBlock::ACTIVE_VERSION)
++cUpgraded;
pindex = pindex->pprev;
}
if (nUpgraded > 0)
LogPrintf("%s: %d of last 500 blocks above nVersion %d\n", __func__, nUpgraded, (int)CBlock::CURRENT_VERSION);
if (cUpgraded > 0)
LogPrintf("%s: %d of last 500 blocks above nVersion %d\n", __func__, nUpgraded, (int)CBlock::CURRENT_VERSION);
if ((nUpgraded > 500/2)||(cUpgraded > 500/2))
if (nUpgraded > 500/2)
{
// strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
strMiscWarning = _("Warning: This version is obsolete; upgrade required!");
@@ -3142,13 +3137,13 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
//拒绝旧的版本和非法版本
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
if (block.nVersion < 2 && IsSuperMajority(2,5, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
return state.Invalid(error("%s: rejected nVersion=1 block", __func__),
REJECT_OBSOLETE, "bad-version");
// Reject block.nVersion=2 blocks when 95% (75% on testnet) of the network has upgraded:
//testnet
if (block.nVersion < 3 && IsSuperMajority(3,5, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
{
if ((chainParams.NetworkIDString()=="test") && (nHeight >= nTestnetV4))
{
@@ -3169,7 +3164,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
REJECT_OBSOLETE, "bad-version");
// Reject block.nVersion=3 blocks when 95% (75% on testnet) of the network has upgraded:
if (block.nVersion < 4 && IsSuperMajority(4,5, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
if (block.nVersion < 4 && IsSuperMajority(4, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
{
if ((chainParams.NetworkIDString()=="test") && (nHeight >= nTestnetV4))
{
@@ -3185,7 +3180,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
}
//检查是否合法升级到5,是否拒绝小于5的版本,达到版本升级的目的。版本4是起点
if (block.cVersion < 5 && IsSuperMajority(4,5, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
if (block.cVersion < 5 && IsSuperMajority(4, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
return state.Invalid(error("%s: rejected block cVersion < 4.0 ", __func__),
REJECT_OBSOLETE, "bad-version");
@@ -3214,7 +3209,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
// if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
// if (block.nVersion >= 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams))
if (block.nVersion == 2 && block.nTime > nSwitchV2
&& IsSuperMajority(2, 5,pindexPrev, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams))
&& IsSuperMajority(2, pindexPrev, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams))
{
CScript expect = CScript() << nHeight;
if (block.vtx[0].vin[0].scriptSig.size() < expect.size() || !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin()))
@@ -3229,7 +3224,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
return state.DoS(100, error("%s: block nVersion 3 height mismatch in coinbase,block v3 was never enforced.", __func__), REJECT_INVALID, "bad-cb-height");
}
if (block.nVersion == 4 && block.nTime > MIN_BLOCKHEADER_VERSION4_SwitchTime
&& IsSuperMajority(4, 5, pindexPrev, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams))
&& IsSuperMajority(4, pindexPrev, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams))
{
CScript expect = CScript() << nHeight;
if (block.vtx[0].vin[0].scriptSig.size() < expect.size() || !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin()))
@@ -3377,20 +3372,17 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
return true;
}
static bool IsSuperMajority(int minVersion,int min_cVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams)
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams)
{
unsigned int nFound = 0;
unsigned int cFound = 0;
for (int i = 0; i < consensusParams.nMajorityWindow && nFound < nRequired && pstart != NULL; i++)
{
if (pstart->nVersion >= minVersion)
++nFound;
if (pstart->cVersion >= min_cVersion)
++cFound;
pstart = pstart->pprev;
}
LogPrintf("IsSuperMajority: minVersion=%d,min_cVersion=%d, nRequired=%d, nFound=%d, cFound=%d\n",minVersion, min_cVersion, nRequired, nFound, cFound);
return ((nFound >= nRequired)||(cFound >= nRequired));
LogPrintf("IsSuperMajority: minVersion=%d, nRequired=%d, nFound=%d\n",minVersion, nRequired, nFound);
return (nFound >= nRequired);
}
View
@@ -27,8 +27,7 @@ class CBlockHeader
{
public:
// header
static const int32_t ACTIVE_VERSION=4;
static const int32_t CURRENT_VERSION=2; //be locked
static const int32_t CURRENT_VERSION=4; //active new version
int32_t cVersion;
int32_t nVersion;
uint256 hashPrevBlock;
@@ -70,7 +69,6 @@ class CBlockHeader
void SetNull()
{
cVersion = CBlockHeader::ACTIVE_VERSION;
nVersion = CBlockHeader::CURRENT_VERSION;
hashPrevBlock.SetNull();
hashMerkleRoot.SetNull();

0 comments on commit bcc6a7f

Please sign in to comment.