From d81711c3989f0d1dae99c22efaf38e0f48a35794 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 22:12:58 +0300 Subject: [PATCH] komodo_ensure, process/testblock --- src/main.cpp | 69 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 002320365..e05b074ac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3958,21 +3958,49 @@ static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned void komodo_currentheight_set(int32_t height); -bool ProcessNewBlock(int32_t height,CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp) +CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) +{ + CBlockIndex *pindex; + BlockMap::iterator miSelf = mapBlockIndex.find(hash); + if ( miSelf != mapBlockIndex.end() ) + { + if ( (pindex= miSelf->second) == 0 ) // create pindex so first Accept block doesnt fail + { + miSelf->second = AddToBlockIndex(*pblock); + //fprintf(stderr,"Block header %s is already known, but without pindex -> ensured %p\n",hash.ToString().c_str(),miSelf->second); + } + /*if ( hash != chainparams.GetConsensus().hashGenesisBlock ) + { + miSelf = mapBlockIndex.find(pblock->hashPrevBlock); + if ( miSelf == mapBlockIndex.end() ) + { + miSelf->second = InsertBlockIndex(pblock->hashPrevBlock); + fprintf(stderr,"autocreate previndex %s\n",pblock->hashPrevBlock.ToString().c_str()); + } + }*/ + } +} + +bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp) { // Preliminary checks - bool checked; + bool checked; uint256 hash; int32_t futureblock=0; auto verifier = libzcash::ProofVerifier::Disabled(); + hash = pblock->GetHash(); + //fprintf(stderr,"process newblock %s\n",hash.ToString().c_str()); if ( chainActive.Tip() != 0 ) komodo_currentheight_set(chainActive.Tip()->nHeight); - if ( ASSETCHAINS_SYMBOL[0] == 0 ) - checked = CheckBlock(height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0); - else checked = CheckBlock(height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0); + checked = CheckBlock(&futureblock,height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0); { LOCK(cs_main); - bool fRequested = MarkBlockAsReceived(pblock->GetHash()); + bool fRequested = MarkBlockAsReceived(hash); fRequested |= fForceProcessing; - if (!checked) + if ( checked != 0 && komodo_checkPOW(from_miner && ASSETCHAINS_STAKED == 0,pblock,height) < 0 ) + { + checked = 0; + fprintf(stderr,"passed checkblock but failed checkPOW.%d\n",from_miner && ASSETCHAINS_STAKED == 0); + } + if (!checked && futureblock == 0) { if ( pfrom != 0 ) { @@ -3980,19 +4008,23 @@ bool ProcessNewBlock(int32_t height,CValidationState &state, CNode* pfrom, CBloc } return error("%s: CheckBlock FAILED", __func__); } - // Store to disk CBlockIndex *pindex = NULL; - bool ret = AcceptBlock(*pblock, state, &pindex, fRequested, dbp); + { + // without the komodo_ensure call, it is quite possible to get a non-error but null pindex returned from AcceptBlockHeader. In a 2 node network, it will be a long time before that block is reprocessed. Even though restarting makes it rescan, it seems much better to keep the nodes in sync + komodo_ensure(pblock,hash); + } + bool ret = AcceptBlock(&futureblock,*pblock, state, &pindex, fRequested, dbp); if (pindex && pfrom) { mapBlockSource[pindex->GetBlockHash()] = pfrom->GetId(); } CheckBlockIndex(); - if (!ret) + if (!ret && futureblock == 0) return error("%s: AcceptBlock FAILED", __func__); + //else fprintf(stderr,"added block %s %p\n",pindex->GetBlockHash().ToString().c_str(),pindex->pprev); } - if (!ActivateBestChain(state, pblock)) + if (futureblock == 0 && !ActivateBestChain(state, pblock)) return error("%s: ActivateBestChain failed", __func__); return true; @@ -4009,30 +4041,31 @@ bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex indexDummy.nHeight = pindexPrev->nHeight + 1; // JoinSplit proofs are verified in ConnectBlock auto verifier = libzcash::ProofVerifier::Disabled(); - // NOTE: CheckBlockHeader is called by CheckBlock if (!ContextualCheckBlockHeader(block, state, pindexPrev)) { - fprintf(stderr,"TestBlockValidity failure A\n"); + //fprintf(stderr,"TestBlockValidity failure A checkPOW.%d\n",fCheckPOW); return false; } - if (!CheckBlock(indexDummy.nHeight,0,block, state, verifier, fCheckPOW, fCheckMerkleRoot)) + int32_t futureblock; + if (!CheckBlock(&futureblock,indexDummy.nHeight,0,block, state, verifier, fCheckPOW, fCheckMerkleRoot)) { - //fprintf(stderr,"TestBlockValidity failure B\n"); + //fprintf(stderr,"TestBlockValidity failure B checkPOW.%d\n",fCheckPOW); return false; } if (!ContextualCheckBlock(block, state, pindexPrev)) { - //fprintf(stderr,"TestBlockValidity failure C\n"); + //fprintf(stderr,"TestBlockValidity failure C checkPOW.%d\n",fCheckPOW); return false; } if (!ConnectBlock(block, state, &indexDummy, viewNew, true,fCheckPOW)) { - fprintf(stderr,"TestBlockValidity failure D\n"); + //fprintf(stderr,"TestBlockValidity failure D checkPOW.%d\n",fCheckPOW); return false; } assert(state.IsValid()); - + if ( futureblock != 0 ) + return(false); return true; }