Skip to content

Commit

Permalink
refactor nChainTrust to CBigNum
Browse files Browse the repository at this point in the history
  • Loading branch information
senadj committed Aug 21, 2015
1 parent 850508d commit c6119d9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 45 deletions.
52 changes: 26 additions & 26 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ int nCoinbaseMaturity = 500;
CBlockIndex* pindexGenesisBlock = NULL;
int nBestHeight = -1;

uint256 nBestChainTrust = 0;
uint256 nBestInvalidTrust = 0;
CBigNum bnBestChainTrust(0);
CBigNum bnBestInvalidTrust(0);

uint256 hashBestChain = 0;
CBlockIndex* pindexBest = NULL;
Expand Down Expand Up @@ -1459,24 +1459,24 @@ bool IsInitialBlockDownload()

void static InvalidChainFound(CBlockIndex* pindexNew)
{
if (pindexNew->nChainTrust > nBestInvalidTrust)
if (pindexNew->bnChainTrust > bnBestInvalidTrust)
{
nBestInvalidTrust = pindexNew->nChainTrust;
CTxDB().WriteBestInvalidTrust(CBigNum(nBestInvalidTrust));
bnBestInvalidTrust = pindexNew->bnChainTrust;
CTxDB().WriteBestInvalidTrust(bnBestInvalidTrust);
uiInterface.NotifyBlocksChanged();
}

uint256 nBestInvalidBlockTrust = pindexNew->nChainTrust - pindexNew->pprev->nChainTrust;
uint256 nBestBlockTrust = pindexBest->nHeight != 0 ? (pindexBest->nChainTrust - pindexBest->pprev->nChainTrust) : pindexBest->nChainTrust;
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=%" PRId64 " date=%s\n",
pindexNew->GetBlockHash().ToString().substr(0,20).c_str(), pindexNew->nHeight,
CBigNum(pindexNew->nChainTrust).ToString().c_str(), nBestInvalidBlockTrust.Get64(),
(pindexNew->bnChainTrust).ToString().c_str(), bnBestInvalidBlockTrust.getuint64(),
DateTimeStrFormat("%x %H:%M:%S", pindexNew->GetBlockTime()).c_str());
printf("InvalidChainFound: current best=%s height=%d trust=%s blocktrust=%" PRId64 " date=%s\n",
hashBestChain.ToString().substr(0,20).c_str(), nBestHeight,
CBigNum(pindexBest->nChainTrust).ToString().c_str(),
nBestBlockTrust.Get64(),
(pindexBest->bnChainTrust).ToString().c_str(),
bnBestBlockTrust.getuint64(),
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
}

Expand Down Expand Up @@ -2169,7 +2169,7 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)

// Reorganize is costly in terms of db load, as it works in a single db transaction.
// Try to limit how much needs to be done inside
while (pindexIntermediate->pprev && pindexIntermediate->pprev->nChainTrust > pindexBest->nChainTrust)
while (pindexIntermediate->pprev && pindexIntermediate->pprev->bnChainTrust > pindexBest->bnChainTrust)
{
vpindexSecondary.push_back(pindexIntermediate);
pindexIntermediate = pindexIntermediate->pprev;
Expand Down Expand Up @@ -2218,16 +2218,16 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
pindexBest = pindexNew;
pblockindexFBBHLast = NULL;
nBestHeight = pindexBest->nHeight;
nBestChainTrust = pindexNew->nChainTrust;
bnBestChainTrust = pindexNew->bnChainTrust;
nTimeBestReceived = GetTime();
nTransactionsUpdated++;

uint256 nBestBlockTrust = pindexBest->nHeight != 0 ? (pindexBest->nChainTrust - pindexBest->pprev->nChainTrust) : pindexBest->nChainTrust;
CBigNum bnBestBlockTrust = pindexBest->nHeight != 0 ? (pindexBest->bnChainTrust - pindexBest->pprev->bnChainTrust) : pindexBest->bnChainTrust;

printf("SetBestChain: new best=%s height=%d trust=%s blocktrust=%" PRId64 " date=%s\n",
hashBestChain.ToString().substr(0,20).c_str(), nBestHeight,
CBigNum(nBestChainTrust).ToString().c_str(),
nBestBlockTrust.Get64(),
bnBestChainTrust.ToString().c_str(),
bnBestBlockTrust.getuint64(),
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());

// Check the version of the last 100 blocks to see if we need to upgrade:
Expand Down Expand Up @@ -2349,7 +2349,7 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
}

// ppcoin: compute chain trust score
pindexNew->nChainTrust = (pindexNew->pprev ? pindexNew->pprev->nChainTrust : 0) + pindexNew->GetBlockTrust();
pindexNew->bnChainTrust = (pindexNew->pprev ? pindexNew->pprev->bnChainTrust : CBigNum(0)) + pindexNew->GetBlockTrust();

// ppcoin: compute stake entropy bit for stake modifier
if (!pindexNew->SetStakeEntropyBit(GetStakeEntropyBit(pindexNew->nHeight)))
Expand Down Expand Up @@ -2388,7 +2388,7 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
return false;

// New best
if (pindexNew->nChainTrust > nBestChainTrust)
if (pindexNew->bnChainTrust > bnBestChainTrust)
if (!SetBestChain(txdb, pindexNew))
return false;

Expand Down Expand Up @@ -2608,24 +2608,24 @@ int32_t CBlockIndex::GetSpacingThreshold() const
}


uint256 CBlockIndex::GetBlockTrust() const
CBigNum CBlockIndex::GetBlockTrust() const
{
CBigNum bnTarget;
bnTarget.SetCompact(nBits);
if (bnTarget <= 0)
return 0;
return CBigNum(0);

// saironiq: new trust rules (since CONSECUTIVE_STAKE_SWITCH_TIME on mainnet and always on testnet)
if (GetBlockTime() >= CONSECUTIVE_STAKE_SWITCH_TIME || fTestNet) {
// first block trust - for future compatibility (i.e., forks :P)
if (pprev == NULL)
return 1;
return CBigNum(1);

// PoS after PoS? no trust for ya!
// (no need to explicitly disallow consecutive PoS
// blocks now as they won't get any trust anyway)
if (IsProofOfStake() && pprev->IsProofOfStake())
return 0;
return CBigNum(0);

// PoS after PoW? trust = prev_trust + 1!
if (IsProofOfStake() && pprev->IsProofOfWork())
Expand All @@ -2639,13 +2639,13 @@ uint256 CBlockIndex::GetBlockTrust() const
// double the trust if previous block was PoS
// (to prevent orphaning of PoS)
if (pprev->IsProofOfStake())
return (bnTrust *=2).getuint256();
return (bnTrust *=2);
}
// what the hell?!
return 0;
return CBigNum(0);
}

return (IsProofOfStake()? ((CBigNum(1)<<256) / (bnTarget+1)).getuint256() : 1);
return (IsProofOfStake()? (CBigNum(1)<<256) / (bnTarget+1) : CBigNum(1));
}

bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired, unsigned int nToCheck)
Expand Down Expand Up @@ -2928,8 +2928,8 @@ void UnloadBlockIndex()
setStakeSeen.clear();
pindexGenesisBlock = NULL;
nBestHeight = 0;
nBestChainTrust = 0;
nBestInvalidTrust = 0;
bnBestChainTrust = CBigNum(0);
bnBestInvalidTrust = CBigNum(0);
hashBestChain = 0;
pindexBest = NULL;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ extern unsigned int nNodeLifespan;
extern unsigned int nStakeMinAge;
extern int nCoinbaseMaturity;
extern int nBestHeight;
extern uint256 nBestChainTrust;
extern uint256 nBestInvalidTrust;
extern CBigNum bnBestChainTrust;
extern CBigNum bnBestInvalidTrust;
extern uint256 hashBestChain;
extern CBlockIndex* pindexBest;
extern unsigned int nTransactionsUpdated;
Expand Down Expand Up @@ -1169,7 +1169,7 @@ class CBlockIndex
CBlockIndex* pnext;
uint32_t nFile;
uint32_t nBlockPos;
uint256 nChainTrust; // ppcoin: trust score of block chain
CBigNum bnChainTrust; // ppcoin: trust score of block chain
int32_t nHeight;
int32_t nPosBlockCount; // yacoin2015
int32_t nBitsMA; // yacoin2015
Expand Down Expand Up @@ -1210,7 +1210,7 @@ class CBlockIndex
nHeight = 0;
nPosBlockCount = 0;
nBitsMA = 0;
nChainTrust = 0;
bnChainTrust = CBigNum(0);
nMint = 0;
nMoneySupply = 0;
nFlags = 0;
Expand All @@ -1237,7 +1237,7 @@ class CBlockIndex
nHeight = 0;
nPosBlockCount = 0;
nBitsMA = 0;
nChainTrust = 0;
bnChainTrust = CBigNum(0);
nMint = 0;
nMoneySupply = 0;
nFlags = 0;
Expand Down Expand Up @@ -1290,7 +1290,7 @@ class CBlockIndex

int32_t GetSpacingThreshold() const;

uint256 GetBlockTrust() const;
CBigNum GetBlockTrust() const;

bool IsInMainChain() const
{
Expand Down
2 changes: 1 addition & 1 deletion src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool fPri
result.push_back(Pair("bits", HexBits(block.nBits)));
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
result.push_back(Pair("blocktrust", leftTrim(blockindex->GetBlockTrust().GetHex(), '0')));
result.push_back(Pair("chaintrust", leftTrim(blockindex->nChainTrust.GetHex(), '0')));
result.push_back(Pair("chaintrust", leftTrim(blockindex->bnChainTrust.GetHex(), '0')));
if (blockindex->pprev)
result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
if (blockindex->pnext)
Expand Down
10 changes: 4 additions & 6 deletions src/txdb-bdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ bool CTxDB::LoadBlockIndex()
if (fRequestShutdown)
return true;

// Calculate nChainTrust
// Calculate bnChainTrust
vector<pair<int, CBlockIndex*> > vSortedByHeight;
vSortedByHeight.reserve(mapBlockIndex.size());
BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
Expand All @@ -189,7 +189,7 @@ bool CTxDB::LoadBlockIndex()
BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
{
CBlockIndex* pindex = item.second;
pindex->nChainTrust = (pindex->pprev ? pindex->pprev->nChainTrust : 0) + pindex->GetBlockTrust();
pindex->bnChainTrust = (pindex->pprev ? pindex->pprev->bnChainTrust : CBigNum(0)) + pindex->GetBlockTrust();
// ppcoin: calculate stake modifier checksum
pindex->nStakeModifierChecksum = GetStakeModifierChecksum(pindex);
if (!CheckStakeModifierCheckpoints(pindex->nHeight, pindex->nStakeModifierChecksum))
Expand All @@ -207,9 +207,9 @@ bool CTxDB::LoadBlockIndex()
return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
pindexBest = mapBlockIndex[hashBestChain];
nBestHeight = pindexBest->nHeight;
nBestChainTrust = pindexBest->nChainTrust;
bnBestChainTrust = pindexBest->bnChainTrust;
printf("LoadBlockIndex(): hashBestChain=%s height=%d trust=%s date=%s\n",
hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, CBigNum(nBestChainTrust).ToString().c_str(),
hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainTrust.ToString().c_str(),
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());

// ppcoin: load hashSyncCheckpoint
Expand All @@ -218,9 +218,7 @@ bool CTxDB::LoadBlockIndex()
printf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());

// Load bnBestInvalidTrust, OK if it doesn't exist
CBigNum bnBestInvalidTrust;
ReadBestInvalidTrust(bnBestInvalidTrust);
nBestInvalidTrust = bnBestInvalidTrust.getuint256();

// Verify blocks in the best chain
int nCheckLevel = GetArg("-checklevel", 1);
Expand Down
10 changes: 4 additions & 6 deletions src/txdb-leveldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ bool CTxDB::LoadBlockIndex()
if (fRequestShutdown)
return true;

// Calculate nChainTrust
// Calculate bnChainTrust
vector<pair<int, CBlockIndex*> > vSortedByHeight;
vSortedByHeight.reserve(mapBlockIndex.size());
BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
Expand All @@ -421,7 +421,7 @@ bool CTxDB::LoadBlockIndex()
CBlockIndex* pindex = item.second;
pindex->nPosBlockCount = ( pindex->pprev ? pindex->pprev->nPosBlockCount : 0 ) + ( pindex->IsProofOfStake() ? 1 : 0 );
pindex->nBitsMA = pindex->IsProofOfStake() ? GetProofOfWorkMA(pindex->pprev) : 0;
pindex->nChainTrust = (pindex->pprev ? pindex->pprev->nChainTrust : 0) + pindex->GetBlockTrust();
pindex->bnChainTrust = (pindex->pprev ? pindex->pprev->bnChainTrust : CBigNum(0)) + pindex->GetBlockTrust();
// NovaCoin: calculate stake modifier checksum
pindex->nStakeModifierChecksum = GetStakeModifierChecksum(pindex);
if (!CheckStakeModifierCheckpoints(pindex->nHeight, pindex->nStakeModifierChecksum))
Expand All @@ -439,10 +439,10 @@ bool CTxDB::LoadBlockIndex()
return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
pindexBest = mapBlockIndex[hashBestChain];
nBestHeight = pindexBest->nHeight;
nBestChainTrust = pindexBest->nChainTrust;
bnBestChainTrust = pindexBest->bnChainTrust;

printf("LoadBlockIndex(): hashBestChain=%s height=%d trust=%s date=%s\n",
hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, CBigNum(nBestChainTrust).ToString().c_str(),
hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainTrust.ToString().c_str(),
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());

// NovaCoin: load hashSyncCheckpoint
Expand All @@ -451,9 +451,7 @@ bool CTxDB::LoadBlockIndex()
printf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());

// Load bnBestInvalidTrust, OK if it doesn't exist
CBigNum bnBestInvalidTrust;
ReadBestInvalidTrust(bnBestInvalidTrust);
nBestInvalidTrust = bnBestInvalidTrust.getuint256();

// Verify blocks in the best chain
int nCheckLevel = GetArg("-checklevel", 1);
Expand Down

0 comments on commit c6119d9

Please sign in to comment.