From dbb5ff777ee200bbe90cf72196bf7a842bf7665c Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 15 Jun 2013 16:55:53 +0400 Subject: [PATCH] Add trust score calculation switch at 20 Sep 2013 --- src/main.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- src/main.h | 10 +----- 2 files changed, 87 insertions(+), 14 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d1f435a..20a01f1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1161,15 +1161,21 @@ void static InvalidChainFound(CBlockIndex* pindexNew) uiInterface.NotifyBlocksChanged(); } - printf("InvalidChainFound: invalid block=%s height=%d trust=%s date=%s\n", + CBigNum bnBestInvalidBlockTrust = pindexNew->bnChainTrust - pindexNew->pprev->bnChainTrust; + CBigNum bnBestBlockTrust = pindexBest->nHeight != 0 ? (pindexBest->bnChainTrust - pindexBest->pprev->bnChainTrust) : pindexBest->bnChainTrust; + + printf("InvalidChainFound: invalid block=%s height=%d trust=%s blocktrust=%s date=%s\n", pindexNew->GetBlockHash().ToString().substr(0,20).c_str(), pindexNew->nHeight, - pindexNew->bnChainTrust.ToString().c_str(), DateTimeStrFormat("%x %H:%M:%S", - pindexNew->GetBlockTime()).c_str()); - printf("InvalidChainFound: current best=%s height=%d trust=%s date=%s\n", - hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainTrust.ToString().c_str(), + pindexNew->bnChainTrust.ToString().c_str(), bnBestInvalidBlockTrust.ToString().c_str(), + DateTimeStrFormat("%x %H:%M:%S", pindexNew->GetBlockTime()).c_str()); + printf("InvalidChainFound: current best=%s height=%d trust=%s blocktrust=%s date=%s\n", + hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, + bnBestChainTrust.ToString().c_str(), + bnBestBlockTrust.ToString().c_str(), DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str()); } + void CBlock::UpdateTime(const CBlockIndex* pindexPrev) { nTime = max(GetBlockTime(), GetAdjustedTime()); @@ -1855,8 +1861,12 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew) bnBestChainTrust = pindexNew->bnChainTrust; nTimeBestReceived = GetTime(); nTransactionsUpdated++; - printf("SetBestChain: new best=%s height=%d trust=%s date=%s\n", + + CBigNum bnBestBlockTrust = pindexBest->nHeight != 0 ? (pindexBest->bnChainTrust - pindexBest->pprev->bnChainTrust) : pindexBest->bnChainTrust; + + printf("SetBestChain: new best=%s height=%d trust=%s blocktrust=%s date=%s\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainTrust.ToString().c_str(), + bnBestBlockTrust.ToString().c_str(), DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str()); // Check the version of the last 100 blocks to see if we need to upgrade: @@ -2220,6 +2230,75 @@ bool CBlock::AcceptBlock() return true; } +CBigNum CBlockIndex::GetBlockTrust() const +{ + CBigNum bnTarget; + + // Old protocol + if (!fTestNet && GetBlockTime() < CHAINCHECKS_SWITCH_TIME) + { + CBigNum bnTarget; + bnTarget.SetCompact(nBits); + + if (bnTarget <= 0) + return 0; + return (IsProofOfStake()? (CBigNum(1)<<256) / (bnTarget+1) : 1); + } + + // New protocol + if (pprev == NULL || pprev->nHeight < 10) + return 1; + + const CBlockIndex* currentIndex = pprev; + + if(IsProofOfStake()) + { + bnTarget.SetCompact(nBits); + if (bnTarget <= 0) + return 0; + + if (!pprev->IsProofOfWork()) + return (CBigNum(1)<<256) / (3 * (bnTarget+1)); + + int nPoWCount = 0; + + // Check last 12 blocks type + while (pprev->nHeight - currentIndex->nHeight < 12) + { + if (currentIndex->IsProofOfWork()) + nPoWCount++; + currentIndex = currentIndex->pprev; + } + + // Return 1/3 of score if less than 3 PoW blocks found + if (nPoWCount < 3) + return (CBigNum(1)<<256) / (3 * (bnTarget+1)); + + return (CBigNum(1)<<256) / (bnTarget+1); + } + else + { + if (!(pprev->IsProofOfStake() && pprev->pprev->IsProofOfStake())) + return 1 + (2 * (pprev->bnChainTrust - pprev->pprev->bnChainTrust) / 3); + + int nPoSCount = 0; + + // Check last 12 blocks type + while (pprev->nHeight - currentIndex->nHeight < 12) + { + if (currentIndex->IsProofOfStake()) + nPoSCount++; + currentIndex = currentIndex->pprev; + } + + // Return 2/3 of previous block score if less than 7 PoS blocks found + if (nPoSCount < 7) + return 1 + (2 * (pprev->bnChainTrust - pprev->pprev->bnChainTrust) / 3); + + return (pprev->bnChainTrust - pprev->pprev->bnChainTrust); + } +} + bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired, unsigned int nToCheck) { unsigned int nFound = 0; diff --git a/src/main.h b/src/main.h index bc79d87..2384c43 100644 --- a/src/main.h +++ b/src/main.h @@ -39,6 +39,7 @@ static const int64 MIN_TXOUT_AMOUNT = MIN_TX_FEE; static const unsigned int STAKE_SWITCH_TIME = 1371686400; // 20 Jun 2013 00:00:00 static const unsigned int TARGETS_SWITCH_TIME = 1374278400; // 20 Jul 2013 00:00:00 static const unsigned int LOCKS_SWITCH_TIME = 1376956800; // 20 Aug 2013 00:00:00 +static const unsigned int CHAINCHECKS_SWITCH_TIME = 1379635200; // 20 Sep 2013 00:00:00 inline bool MoneyRange(int64 nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } // Threshold for nLockTime: below this value it is interpreted as block number, otherwise as UNIX timestamp. @@ -1241,14 +1242,7 @@ public: return (int64)nTime; } - CBigNum GetBlockTrust() const - { - CBigNum bnTarget; - bnTarget.SetCompact(nBits); - if (bnTarget <= 0) - return 0; - return (IsProofOfStake()? (CBigNum(1)<<256) / (bnTarget+1) : 1); - } + CBigNum GetBlockTrust() const; bool IsInMainChain() const { -- 1.7.1