mirror of
https://github.com/jwinterm/myriadcoin.git
synced 2026-11-26 11:34:07 +01:00
+73
-4
@@ -99,9 +99,42 @@ namespace {
|
||||
}
|
||||
};
|
||||
|
||||
struct CBlockIndexWorkComparator2
|
||||
{
|
||||
bool operator()(CBlockIndex *pa, CBlockIndex *pb)
|
||||
{
|
||||
// First sort by most work per algo, ...
|
||||
int aWins = 0, bWins = 0;
|
||||
for (int i = 0; i < NUM_ALGOS; i++)
|
||||
{
|
||||
if (pa->nAlgoWork[i] > pb->nAlgoWork[i]) aWins++;
|
||||
if (pa->nAlgoWork[i] < pb->nAlgoWork[i]) bWins++;
|
||||
}
|
||||
if (aWins > bWins) return false;
|
||||
if (aWins < bWins) return true;
|
||||
|
||||
// ... then by number of blocks ...
|
||||
if (pa->nHeight > pb->nHeight) return false;
|
||||
if (pa->nHeight < pb->nHeight) return true;
|
||||
|
||||
// ... then by earliest time received, ...
|
||||
if (pa->nSequenceId < pb->nSequenceId) return false;
|
||||
if (pa->nSequenceId > pb->nSequenceId) return true;
|
||||
|
||||
// Use pointer address as tie breaker (should only happen with blocks
|
||||
// loaded from disk, as those all have id 0).
|
||||
if (pa < pb) return false;
|
||||
if (pa > pb) return true;
|
||||
|
||||
// Identical blocks.
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
CBlockIndex *pindexBestInvalid;
|
||||
// may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
|
||||
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid;
|
||||
set<CBlockIndex*, CBlockIndexWorkComparator2> setBlockIndexValid2; // interim measure to transition between comparator1 and comparator2
|
||||
|
||||
CCriticalSection cs_LastBlockFile;
|
||||
CBlockFileInfo infoLastBlockFile;
|
||||
@@ -1623,6 +1656,7 @@ void static InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state
|
||||
pindex->nStatus |= BLOCK_FAILED_VALID;
|
||||
pblocktree->WriteBlockIndex(CDiskBlockIndex(pindex));
|
||||
setBlockIndexValid.erase(pindex);
|
||||
setBlockIndexValid2.erase(pindex);
|
||||
InvalidChainFound(pindex);
|
||||
}
|
||||
}
|
||||
@@ -2199,23 +2233,39 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew) {
|
||||
|
||||
// Make chainMostWork correspond to the chain with the most work in it, that isn't
|
||||
// known to be invalid (it's however far from certain to be valid).
|
||||
void static FindMostWorkChain() {
|
||||
void static FindMostWorkChain()
|
||||
{
|
||||
|
||||
int CompareVersion = 1;
|
||||
if (chainActive.Tip() && (chainActive.Tip()->nHeight >= nBlockAlgoCountWorkStart))
|
||||
CompareVersion = 2;
|
||||
|
||||
CBlockIndex *pindexNew = NULL;
|
||||
|
||||
// In case the current best is invalid, do not consider it.
|
||||
while (chainMostWork.Tip() && (chainMostWork.Tip()->nStatus & BLOCK_FAILED_MASK)) {
|
||||
setBlockIndexValid.erase(chainMostWork.Tip());
|
||||
setBlockIndexValid2.erase(chainMostWork.Tip());
|
||||
chainMostWork.SetTip(chainMostWork.Tip()->pprev);
|
||||
}
|
||||
|
||||
do {
|
||||
do
|
||||
{
|
||||
// Find the best candidate header.
|
||||
if (CompareVersion == 1)
|
||||
{
|
||||
std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexValid.rbegin();
|
||||
if (it == setBlockIndexValid.rend())
|
||||
return;
|
||||
pindexNew = *it;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::set<CBlockIndex*, CBlockIndexWorkComparator2>::reverse_iterator it = setBlockIndexValid2.rbegin();
|
||||
if (it == setBlockIndexValid2.rend())
|
||||
return;
|
||||
pindexNew = *it;
|
||||
}
|
||||
|
||||
// Check whether all blocks on the path between the currently active chain and the candidate are valid.
|
||||
// Just going until the active chain is an optimization, as we know all blocks in it are valid already.
|
||||
@@ -2229,6 +2279,7 @@ void static FindMostWorkChain() {
|
||||
while (pindexTest != pindexFailed) {
|
||||
pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
|
||||
setBlockIndexValid.erase(pindexFailed);
|
||||
setBlockIndexValid2.erase(pindexFailed);
|
||||
pindexFailed = pindexFailed->pprev;
|
||||
}
|
||||
fInvalidAncestor = true;
|
||||
@@ -2243,8 +2294,16 @@ void static FindMostWorkChain() {
|
||||
} while(true);
|
||||
|
||||
// Check whether it's actually an improvement.
|
||||
if (chainMostWork.Tip() && !CBlockIndexWorkComparator()(chainMostWork.Tip(), pindexNew))
|
||||
return;
|
||||
if (CompareVersion == 1)
|
||||
{
|
||||
if (chainMostWork.Tip() && !CBlockIndexWorkComparator()(chainMostWork.Tip(), pindexNew))
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (chainMostWork.Tip() && !CBlockIndexWorkComparator2()(chainMostWork.Tip(), pindexNew))
|
||||
return;
|
||||
}
|
||||
|
||||
// We have a new best.
|
||||
chainMostWork.SetTip(pindexNew);
|
||||
@@ -2323,12 +2382,18 @@ bool AddToBlockIndex(CBlock& block, CValidationState& state, const CDiskBlockPos
|
||||
}
|
||||
pindexNew->nTx = block.vtx.size();
|
||||
pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + pindexNew->GetBlockWorkAdjusted().getuint256();
|
||||
for (int i = 0; i < NUM_ALGOS; i++)
|
||||
{
|
||||
pindexNew->nAlgoWork[i] = (pindexNew->pprev ? pindexNew->pprev->nAlgoWork[i] : 0) +
|
||||
(i == pindexNew->GetAlgo() ? pindexNew->GetBlockWork().getuint256() : 0);
|
||||
}
|
||||
pindexNew->nChainTx = (pindexNew->pprev ? pindexNew->pprev->nChainTx : 0) + pindexNew->nTx;
|
||||
pindexNew->nFile = pos.nFile;
|
||||
pindexNew->nDataPos = pos.nPos;
|
||||
pindexNew->nUndoPos = 0;
|
||||
pindexNew->nStatus = BLOCK_VALID_TRANSACTIONS | BLOCK_HAVE_DATA;
|
||||
setBlockIndexValid.insert(pindexNew);
|
||||
setBlockIndexValid2.insert(pindexNew);
|
||||
|
||||
if (!pblocktree->WriteBlockIndex(CDiskBlockIndex(pindexNew)))
|
||||
return state.Abort(_("Failed to write block index"));
|
||||
@@ -3033,7 +3098,10 @@ bool static LoadBlockIndexDB()
|
||||
pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + pindex->GetBlockWorkAdjusted().getuint256();
|
||||
pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
|
||||
if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS && !(pindex->nStatus & BLOCK_FAILED_MASK))
|
||||
{
|
||||
setBlockIndexValid.insert(pindex);
|
||||
setBlockIndexValid2.insert(pindex);
|
||||
}
|
||||
if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
|
||||
pindexBestInvalid = pindex;
|
||||
}
|
||||
@@ -3144,6 +3212,7 @@ void UnloadBlockIndex()
|
||||
{
|
||||
mapBlockIndex.clear();
|
||||
setBlockIndexValid.clear();
|
||||
setBlockIndexValid2.clear();
|
||||
chainActive.SetTip(NULL);
|
||||
pindexBestInvalid = NULL;
|
||||
}
|
||||
|
||||
@@ -689,6 +689,7 @@ enum BlockStatus {
|
||||
|
||||
const int64_t nBlockAlgoWorkWeightStart = 142000; // block where algo work weighting starts
|
||||
const int64_t nBlockAlgoNormalisedWorkStart = 740000; // block where algo combined weight starts
|
||||
const int64_t nBlockAlgoCountWorkStart = 857000; // block where new work comparison starts
|
||||
const int64_t nBlockSequentialAlgoRuleStart = 740000; // block where sequential algo rule starts
|
||||
const int64_t nBlockSequentialAlgoRuleStart2 = 766000; // block where sequential algo rule starts
|
||||
const int nBlockSequentialAlgoMaxCount = 6; // maximum sequential blocks of same algo
|
||||
@@ -726,6 +727,8 @@ public:
|
||||
// (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
|
||||
uint256 nChainWork;
|
||||
|
||||
uint256 nAlgoWork[NUM_ALGOS];
|
||||
|
||||
// Number of transactions in this block.
|
||||
// Note: in a potential headers-first mode, this number cannot be relied upon
|
||||
unsigned int nTx;
|
||||
@@ -755,6 +758,8 @@ public:
|
||||
nDataPos = 0;
|
||||
nUndoPos = 0;
|
||||
nChainWork = 0;
|
||||
for (int i = 0; i < NUM_ALGOS; i++)
|
||||
nAlgoWork[i] = 0;
|
||||
nTx = 0;
|
||||
nChainTx = 0;
|
||||
nStatus = 0;
|
||||
@@ -776,6 +781,8 @@ public:
|
||||
nDataPos = 0;
|
||||
nUndoPos = 0;
|
||||
nChainWork = 0;
|
||||
for (int i = 0; i < NUM_ALGOS; i++)
|
||||
nAlgoWork[i] = 0;
|
||||
nTx = 0;
|
||||
nChainTx = 0;
|
||||
nStatus = 0;
|
||||
|
||||
Reference in New Issue
Block a user