Skip to content

Commit

Permalink
Trying to fix the forking issue, which is now clearly a bug in sendin…
Browse files Browse the repository at this point in the history
…g and receiving headers. This checkin will enable debugging and may be a fix.
  • Loading branch information
miketout committed May 16, 2018
1 parent 3243043 commit 3da69a3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
7 changes: 3 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3999,7 +3999,6 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
{
const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
const Consensus::Params& consensusParams = Params().GetConsensus();
bool checkBlockOne = (nHeight == 1);

// Check that all transactions are finalized
BOOST_FOREACH(const CTransaction& tx, block.vtx) {
Expand Down Expand Up @@ -4087,7 +4086,7 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat
komodo_requestedhash = block.hashPrevBlock;
komodo_requestedcount = 0;
}*/
LogPrintf("AcceptBlockHeader hashPrevBlock %s not found\n",block.hashPrevBlock.ToString().c_str());
LogPrintf("AcceptBlockHeader %s\n hashPrevBlock %s not found\n", hash.ToString().c_str(), block.hashPrevBlock.ToString().c_str());
return(false);
//return state.DoS(10, error("%s: prev block not found", __func__), 0, "bad-prevblk");
}
Expand Down Expand Up @@ -6016,8 +6015,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
pindex = chainActive.Next(pindex);
}

// we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
vector<CBlock> vHeaders;
// we should probably use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
vector<CBlockHeader> vHeaders;
int nLimit = MAX_HEADERS_RESULTS;
LogPrint("net", "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString(), pfrom->id);
//if ( pfrom->lasthdrsreq >= chainActive.Height()-MAX_HEADERS_RESULTS || pfrom->lasthdrsreq != (int32_t)(pindex ? pindex->nHeight : -1) )// no need to ever suppress this
Expand Down
72 changes: 38 additions & 34 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, bool isStake)
// Fill in header
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
pblock->hashReserved = uint256();

if ( ASSETCHAINS_SYMBOL[0] == 0 || ASSETCHAINS_STAKED == 0 || NOTARY_PUBKEY33[0] == 0 )
{
UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
Expand Down Expand Up @@ -777,16 +778,22 @@ int32_t waitForPeers(const CChainParams &chainparams)
{
if (chainparams.MiningRequiresPeers())
{
do {
bool fvNodesEmpty;
{
LOCK(cs_vNodes);
fvNodesEmpty = vNodes.empty();
}
if (!fvNodesEmpty )
break;
MilliSleep(1000 + rand() % 1900);
} while (true);
bool fvNodesEmpty;
{
LOCK(cs_vNodes);
fvNodesEmpty = vNodes.empty();
}
if (fvNodesEmpty)
{
do {
MilliSleep(5000 + rand() % 5000);
{
LOCK(cs_vNodes);
fvNodesEmpty = vNodes.empty();
}
} while (fvNodesEmpty);
MilliSleep(5000 + rand() % 5000);
}
}
}

Expand Down Expand Up @@ -817,18 +824,17 @@ void static VerusStaker(CWallet *pwallet)
break;
}

SetThreadPriority(THREAD_PRIORITY_LOWEST);

// try a nice clean peer connection to start
waitForPeers(chainparams);
sleep(5);
CBlockIndex *curTip = chainActive.Tip(), *lastTip;
// try a nice clean peer connection to start
waitForPeers(chainparams);
CBlockIndex* pindexPrev;
do {
lastTip = curTip;
printf("Verifying block height %d \n", lastTip->nHeight);
MilliSleep(3000 + rand() % 1900);
curTip = chainActive.Tip();
} while (curTip != lastTip);

SetThreadPriority(THREAD_PRIORITY_LOWEST);
pindexPrev = chainActive.Tip();
MilliSleep(5000 + rand() % 5000);
} while (pindexPrev != chainActive.Tip());

sleep(5);
printf("Staking height %d for %s\n", chainActive.Tip()->nHeight + 1, ASSETCHAINS_SYMBOL);
Expand Down Expand Up @@ -940,11 +946,12 @@ void static VerusStaker(CWallet *pwallet)

ProcessBlockFound(pblock, *pwallet, reservekey);

sleep(3);
// Check for stop or if block needs to be rebuilt
boost::this_thread::interruption_point();
SetThreadPriority(THREAD_PRIORITY_LOWEST);

sleep(5);

// In regression test mode, stop mining after a block is found.
if (chainparams.MineBlocksOnDemand()) {
throw boost::thread_interrupted();
Expand Down Expand Up @@ -993,18 +1000,15 @@ void static BitcoinMiner_noeq()
break;
}

SetThreadPriority(THREAD_PRIORITY_LOWEST);

// try a nice clean peer connection to start
waitForPeers(chainparams);
MilliSleep(5000 + rand() % 5000);
CBlockIndex *curTip = chainActive.Tip(), *lastTip;
CBlockIndex* pindexPrev;
do {
lastTip = curTip;
printf("Verifying block height %d \n", lastTip->nHeight);
MilliSleep(1000 + rand() % 5000);
curTip = chainActive.Tip();
} while (curTip != lastTip);

SetThreadPriority(THREAD_PRIORITY_LOWEST);
pindexPrev = chainActive.Tip();
MilliSleep(5000 + rand() % 5000);
} while (pindexPrev != chainActive.Tip());

printf("Mining height %d\n", chainActive.Tip()->nHeight + 1);

Expand All @@ -1017,16 +1021,16 @@ void static BitcoinMiner_noeq()
miningTimer.stop();
waitForPeers(chainparams);

CBlockIndex* pindexPrev = chainActive.Tip();
pindexPrev = chainActive.Tip();
sleep(1);

// prevent forking on startup before the diff algorithm kicks in
if (pindexPrev->nHeight < 70)
if (pindexPrev->nHeight < 50 || pindexPrev != chainActive.Tip())
{
do {
lastTip = pindexPrev;
MilliSleep(3000 + rand() % 5000);
pindexPrev = chainActive.Tip();
} while (pindexPrev != lastTip);
MilliSleep(5000 + rand() % 5000);
} while (pindexPrev != chainActive.Tip());
}
miningTimer.start();

Expand Down
5 changes: 4 additions & 1 deletion src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ class CBlockHeader

uint256 GetHash() const
{
return (this->*hashFunction)();
if (hashPrevBlock.IsNull())
return (this->GetSHA256DHash());
else
return (this->*hashFunction)();
}

uint256 GetSHA256DHash() const;
Expand Down

0 comments on commit 3da69a3

Please sign in to comment.