From 4f48647cdc65bf3b86b812b316a11ae2d55d4486 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 10:30:40 +0300 Subject: [PATCH 01/31] Test --- src/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index f892b77e2..90c835da9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4021,7 +4021,7 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, } if ( pindex == 0 ) { - //fprintf(stderr,"unexpected AcceptBlock error null pindex\n"); + fprintf(stderr,"AcceptBlock error null pindex\n"); return false; } //fprintf(stderr,"acceptblockheader passed\n"); @@ -4132,6 +4132,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo CheckBlockIndex(); if (!ret) return error("%s: AcceptBlock FAILED", __func__); + else fprintf(stderr,"added block %s\n",pindex->GetBlockHash().ToString().c_str()); } if (!ActivateBestChain(state, pblock)) From 6d3df1e66c09e5a5cca4b1d2a518091c1ad39af7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 10:47:27 +0300 Subject: [PATCH 02/31] Test --- src/main.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 90c835da9..b027b7126 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3963,7 +3963,7 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc // Check for duplicate uint256 hash = block.GetHash(); BlockMap::iterator miSelf = mapBlockIndex.find(hash); - CBlockIndex *tipindex,*pindex = NULL; + CBlockIndex *pindex = NULL; if (miSelf != mapBlockIndex.end()) { // Block header is already known. @@ -3972,6 +3972,7 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc *ppindex = pindex; if ( pindex != 0 && pindex->nStatus & BLOCK_FAILED_MASK ) return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate"); + fprintf(stderr,"accepthdr %s already known but no pindex.%p\n",hash.ToString().c_str(),pindex); return true; } if (!CheckBlockHeader(*ppindex!=0?(*ppindex)->nHeight:0,*ppindex, block, state,0)) @@ -3993,14 +3994,14 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc } if (!ContextualCheckBlockHeader(block, state, pindexPrev)) { - //fprintf(stderr,"ContextualCheckBlockHeader failed\n"); + fprintf(stderr,"ContextualCheckBlockHeader failed\n"); return false; } if (pindex == NULL) { if ( (pindex= AddToBlockIndex(block)) == 0 ) { - //fprintf(stderr,"couldnt add to block index\n"); + fprintf(stderr,"couldnt add to block index\n"); } } if (ppindex) @@ -4039,6 +4040,7 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, // TODO: deal better with return value and error conditions for duplicate // and unrequested blocks. + fprintf(stderr,"Accept %s flags already.%d requested.%d morework.%d farahead.%d\n",pindex->GetBlockHash().ToString().c_str(),fAlreadyHave,fRequested,fHasMoreWork,fTooFarAhead); if (fAlreadyHave) return true; if (!fRequested) { // If we didn't ask for it: if (pindex->nTx != 0) return true; // This is a previously-processed block that was pruned @@ -4132,7 +4134,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo CheckBlockIndex(); if (!ret) return error("%s: AcceptBlock FAILED", __func__); - else fprintf(stderr,"added block %s\n",pindex->GetBlockHash().ToString().c_str()); + else fprintf(stderr,"added block %s %p\n",pindex->GetBlockHash().ToString().c_str(),pindex->pprev); } if (!ActivateBestChain(state, pblock)) From e14073653c38964bc9b135ee616aeae1c7bd5618 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 10:55:12 +0300 Subject: [PATCH 03/31] Test --- src/main.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b027b7126..cc71a6d78 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3566,7 +3566,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) pindexBestHeader = pindexNew; setDirtyBlockIndex.insert(pindexNew); - + fprintf(stderr,"added to block index %s\n",hash.ToString().c_str()); return pindexNew; } @@ -4353,7 +4353,8 @@ CBlockIndex * InsertBlockIndex(uint256 hash) throw runtime_error("LoadBlockIndex(): new CBlockIndex failed"); mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first; pindexNew->phashBlock = &((*mi).first); - + fprintf(stderr,"inserted to block index %s\n",hash.ToString().c_str()); + return pindexNew; } From 415f5097cff01493778cc707affbf7ceba85c731 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 10:59:21 +0300 Subject: [PATCH 04/31] Test --- src/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index cc71a6d78..dc4e29942 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3972,7 +3972,8 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc *ppindex = pindex; if ( pindex != 0 && pindex->nStatus & BLOCK_FAILED_MASK ) return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate"); - fprintf(stderr,"accepthdr %s already known but no pindex.%p\n",hash.ToString().c_str(),pindex); + if ( pindex == 0 ) + fprintf(stderr,"accepthdr %s already known but no pindex\n",hash.ToString().c_str()); return true; } if (!CheckBlockHeader(*ppindex!=0?(*ppindex)->nHeight:0,*ppindex, block, state,0)) @@ -4104,6 +4105,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo // Preliminary checks bool checked; auto verifier = libzcash::ProofVerifier::Disabled(); + fprintf(stderr,"process newblock %s\n",pblock->GetHash().ToString().c_str()); if ( chainActive.Tip() != 0 ) komodo_currentheight_set(chainActive.Tip()->nHeight); checked = CheckBlock(height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0); From 17522a555707c378c692a4644b047cd19fddeaad Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 11:19:35 +0300 Subject: [PATCH 05/31] Test --- src/main.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index dc4e29942..3afdc77b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4129,6 +4129,18 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo // Store to disk CBlockIndex *pindex = NULL; + if ( ASSETCHAINS_STAKED != 0 ) + { + BlockMap::iterator miSelf = mapBlockIndex.find(hash); + if ( miSelf != mapBlockIndex.end() ) + { + if ( (pindex= miSelf->second) == 0 ) + { + fprint(stderr,"Block header is already known, but without pindex\n",hash.ToString().c_str()); + AddToBlockIndex(block); + } + } + } bool ret = AcceptBlock(*pblock, state, &pindex, fRequested, dbp); if (pindex && pfrom) { mapBlockSource[pindex->GetBlockHash()] = pfrom->GetId(); From bee39cbbbbcf410565171c0a1a386173af15f0f6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 11:20:11 +0300 Subject: [PATCH 06/31] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 3afdc77b9..26f913863 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4137,7 +4137,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo if ( (pindex= miSelf->second) == 0 ) { fprint(stderr,"Block header is already known, but without pindex\n",hash.ToString().c_str()); - AddToBlockIndex(block); + AddToBlockIndex(*pblock); } } } From 4aad3872a990e4d6ceabe14954bf78c40d1008bf Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 11:20:57 +0300 Subject: [PATCH 07/31] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 26f913863..fe0621d16 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4136,7 +4136,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo { if ( (pindex= miSelf->second) == 0 ) { - fprint(stderr,"Block header is already known, but without pindex\n",hash.ToString().c_str()); + fprint(stderr,"Block header %s is already known, but without pindex\n",hash.ToString().c_str()); AddToBlockIndex(*pblock); } } From bc2bcf85512b2a9df34b455f71e87d31b7b6cfe1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 11:24:01 +0300 Subject: [PATCH 08/31] Test --- src/main.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index fe0621d16..d6eaa3bc1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4103,15 +4103,16 @@ void komodo_currentheight_set(int32_t height); 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; auto verifier = libzcash::ProofVerifier::Disabled(); - fprintf(stderr,"process newblock %s\n",pblock->GetHash().ToString().c_str()); + hash = pblock->GetHash(); + fprintf(stderr,"process newblock %s\n",hash.ToString().c_str()); if ( chainActive.Tip() != 0 ) komodo_currentheight_set(chainActive.Tip()->nHeight); checked = CheckBlock(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 != 0 && komodo_checkPOW(from_miner && ASSETCHAINS_STAKED == 0,pblock,height) < 0 ) { @@ -4126,15 +4127,14 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo } return error("%s: CheckBlock FAILED", __func__); } - // Store to disk CBlockIndex *pindex = NULL; - if ( ASSETCHAINS_STAKED != 0 ) + if ( ASSETCHAINS_STAKED != 0 ) // or other low node count networks { BlockMap::iterator miSelf = mapBlockIndex.find(hash); if ( miSelf != mapBlockIndex.end() ) { - if ( (pindex= miSelf->second) == 0 ) + if ( (pindex= miSelf->second) == 0 ) // create pindex so first Accept block doesnt fail { fprint(stderr,"Block header %s is already known, but without pindex\n",hash.ToString().c_str()); AddToBlockIndex(*pblock); From 592f6fa210cf1660d1629924cffde29dd401f4a4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 11:24:41 +0300 Subject: [PATCH 09/31] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index d6eaa3bc1..55ab32ba4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4136,7 +4136,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo { if ( (pindex= miSelf->second) == 0 ) // create pindex so first Accept block doesnt fail { - fprint(stderr,"Block header %s is already known, but without pindex\n",hash.ToString().c_str()); + fprintf(stderr,"Block header %s is already known, but without pindex\n",hash.ToString().c_str()); AddToBlockIndex(*pblock); } } From fd835d9406483aa4348426abf97ea189d08a2093 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 11:29:12 +0300 Subject: [PATCH 10/31] Test --- src/main.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 55ab32ba4..29e398e06 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3542,8 +3542,10 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) uint256 hash = block.GetHash(); BlockMap::iterator it = mapBlockIndex.find(hash); if (it != mapBlockIndex.end()) + { + fprintf(stderr,"addtoblockindex already there %p vs end.%p\n",it->second,mapBlockIndex.end()); return it->second; - + } // Construct new block index object CBlockIndex* pindexNew = new CBlockIndex(block); assert(pindexNew); @@ -4136,8 +4138,8 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo { if ( (pindex= miSelf->second) == 0 ) // create pindex so first Accept block doesnt fail { - fprintf(stderr,"Block header %s is already known, but without pindex\n",hash.ToString().c_str()); - AddToBlockIndex(*pblock); + pindex = AddToBlockIndex(*pblock); + fprintf(stderr,"Block header %s is already known, but without pindex -> %p\n",hash.ToString().c_str(),pindex); } } } From 9850dffd80bb3c3722a48ccf17b120d6ddee8421 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 11:30:27 +0300 Subject: [PATCH 11/31] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 29e398e06..eaae62f05 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3543,7 +3543,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) BlockMap::iterator it = mapBlockIndex.find(hash); if (it != mapBlockIndex.end()) { - fprintf(stderr,"addtoblockindex already there %p vs end.%p\n",it->second,mapBlockIndex.end()); + fprintf(stderr,"addtoblockindex already there %p\n",it->second); return it->second; } // Construct new block index object From 7732dbc26b2d57786b76d6c9bdd00b9782fca612 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 11:34:37 +0300 Subject: [PATCH 12/31] Test --- src/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index eaae62f05..448146ed1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3544,7 +3544,8 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) if (it != mapBlockIndex.end()) { fprintf(stderr,"addtoblockindex already there %p\n",it->second); - return it->second; + if ( ASSETCHAINS_STAKED == 0 || it->second != 0 ) + return it->second; } // Construct new block index object CBlockIndex* pindexNew = new CBlockIndex(block); From b92dfb1eb56a5f94facaf5e2de4b39f0d87118bf Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 11:53:02 +0300 Subject: [PATCH 13/31] Test --- src/komodo_bitcoind.h | 18 ++++++++++++-- src/main.cpp | 55 +++++++++++++++++++------------------------ 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 923b86b0c..374e1e646 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1272,7 +1272,7 @@ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) { if ( (pprev= mapBlockIndex[pblock->hashPrevBlock]) != 0 ) height = pprev->nHeight + 1; - fprintf(stderr,"komodo_checkPOW slowflag.%d ht.%d zeroheight\n",slowflag,height); + //fprintf(stderr,"komodo_checkPOW slowflag.%d ht.%d zeroheight\n",slowflag,height); //if ( height == 0 ) return(0); } @@ -1337,9 +1337,23 @@ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) } } } - fprintf(stderr,"komodo_checkPOW slowflag.%d ht.%d notaryid.%d failed.%d\n",slowflag,height,notaryid,failed); + //fprintf(stderr,"komodo_checkPOW slowflag.%d ht.%d notaryid.%d failed.%d\n",slowflag,height,notaryid,failed); if ( failed != 0 && notaryid < 0 ) return(-1); else return(0); } +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 + { + pindex = AddToBlockIndex(*pblock); + fprintf(stderr,"Block header %s is already known, but without pindex -> ensured %p\n",hash.ToString().c_str(),pindex); + } + } +} + diff --git a/src/main.cpp b/src/main.cpp index 448146ed1..27ac26108 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3543,7 +3543,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) BlockMap::iterator it = mapBlockIndex.find(hash); if (it != mapBlockIndex.end()) { - fprintf(stderr,"addtoblockindex already there %p\n",it->second); + //fprintf(stderr,"addtoblockindex already there %p\n",it->second); if ( ASSETCHAINS_STAKED == 0 || it->second != 0 ) return it->second; } @@ -3559,8 +3559,8 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock); if (miPrev != mapBlockIndex.end()) { - pindexNew->pprev = (*miPrev).second; - pindexNew->nHeight = pindexNew->pprev->nHeight + 1; + if ( (pindexNew->pprev= (*miPrev).second) != 0 ) + pindexNew->nHeight = pindexNew->pprev->nHeight + 1; pindexNew->BuildSkip(); } pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew); @@ -3569,7 +3569,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) pindexBestHeader = pindexNew; setDirtyBlockIndex.insert(pindexNew); - fprintf(stderr,"added to block index %s\n",hash.ToString().c_str()); + //fprintf(stderr,"added to block index %s\n",hash.ToString().c_str()); return pindexNew; } @@ -3782,7 +3782,7 @@ bool CheckBlock(int32_t height,CBlockIndex *pindex,const CBlock& block, CValidat // redundant with the call in AcceptBlockHeader. if (!CheckBlockHeader(height,pindex,block,state,fCheckPOW)) { - fprintf(stderr,"checkblockheader error PoW.%d\n",fCheckPOW); + //fprintf(stderr,"checkblockheader error PoW.%d\n",fCheckPOW); return false; } if ( fCheckPOW ) @@ -3854,7 +3854,7 @@ bool CheckBlock(int32_t height,CBlockIndex *pindex,const CBlock& block, CValidat { //static uint32_t counter; //if ( counter++ < 100 && ASSETCHAINS_STAKED == 0 ) - fprintf(stderr,"check deposit rejection\n"); + // fprintf(stderr,"check deposit rejection\n"); return(false); } return true; @@ -3975,13 +3975,13 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc *ppindex = pindex; if ( pindex != 0 && pindex->nStatus & BLOCK_FAILED_MASK ) return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate"); - if ( pindex == 0 ) - fprintf(stderr,"accepthdr %s already known but no pindex\n",hash.ToString().c_str()); + //if ( pindex == 0 ) + // fprintf(stderr,"accepthdr %s already known but no pindex\n",hash.ToString().c_str()); return true; } if (!CheckBlockHeader(*ppindex!=0?(*ppindex)->nHeight:0,*ppindex, block, state,0)) { - fprintf(stderr,"CheckBlockHeader failed\n"); + //fprintf(stderr,"CheckBlockHeader failed\n"); return false; } // Get prev block index @@ -3998,14 +3998,14 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc } if (!ContextualCheckBlockHeader(block, state, pindexPrev)) { - fprintf(stderr,"ContextualCheckBlockHeader failed\n"); + //fprintf(stderr,"ContextualCheckBlockHeader failed\n"); return false; } if (pindex == NULL) { if ( (pindex= AddToBlockIndex(block)) == 0 ) { - fprintf(stderr,"couldnt add to block index\n"); + //fprintf(stderr,"couldnt add to block index\n"); } } if (ppindex) @@ -4021,12 +4021,12 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, CBlockIndex *&pindex = *ppindex; if (!AcceptBlockHeader(block, state, &pindex)) { - fprintf(stderr,"AcceptBlockHeader rejected\n"); + //fprintf(stderr,"AcceptBlockHeader rejected\n"); return false; } if ( pindex == 0 ) { - fprintf(stderr,"AcceptBlock error null pindex\n"); + //fprintf(stderr,"AcceptBlock error null pindex\n"); return false; } //fprintf(stderr,"acceptblockheader passed\n"); @@ -4044,7 +4044,7 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, // TODO: deal better with return value and error conditions for duplicate // and unrequested blocks. - fprintf(stderr,"Accept %s flags already.%d requested.%d morework.%d farahead.%d\n",pindex->GetBlockHash().ToString().c_str(),fAlreadyHave,fRequested,fHasMoreWork,fTooFarAhead); + //fprintf(stderr,"Accept %s flags already.%d requested.%d morework.%d farahead.%d\n",pindex->GetBlockHash().ToString().c_str(),fAlreadyHave,fRequested,fHasMoreWork,fTooFarAhead); if (fAlreadyHave) return true; if (!fRequested) { // If we didn't ask for it: if (pindex->nTx != 0) return true; // This is a previously-processed block that was pruned @@ -4060,7 +4060,7 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, pindex->nStatus |= BLOCK_FAILED_VALID; setDirtyBlockIndex.insert(pindex); } - fprintf(stderr,"CheckBlock or ContextualCheckBlock failed\n"); + //fprintf(stderr,"CheckBlock or ContextualCheckBlock failed\n"); return false; } @@ -4109,7 +4109,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo bool checked; uint256 hash; auto verifier = libzcash::ProofVerifier::Disabled(); hash = pblock->GetHash(); - fprintf(stderr,"process newblock %s\n",hash.ToString().c_str()); + //fprintf(stderr,"process newblock %s\n",hash.ToString().c_str()); if ( chainActive.Tip() != 0 ) komodo_currentheight_set(chainActive.Tip()->nHeight); checked = CheckBlock(height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0); @@ -4120,7 +4120,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo 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); + //fprintf(stderr,"passed checkblock but failed checkPOW.%d\n",from_miner && ASSETCHAINS_STAKED == 0); } if (!checked) { @@ -4134,15 +4134,8 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo CBlockIndex *pindex = NULL; if ( ASSETCHAINS_STAKED != 0 ) // or other low node count networks { - BlockMap::iterator miSelf = mapBlockIndex.find(hash); - if ( miSelf != mapBlockIndex.end() ) - { - if ( (pindex= miSelf->second) == 0 ) // create pindex so first Accept block doesnt fail - { - pindex = AddToBlockIndex(*pblock); - fprintf(stderr,"Block header %s is already known, but without pindex -> %p\n",hash.ToString().c_str(),pindex); - } - } + //komodo_ensure(pblock->hashPrevBlock); + komodo_ensure(pblock,hash); } bool ret = AcceptBlock(*pblock, state, &pindex, fRequested, dbp); if (pindex && pfrom) { @@ -4151,7 +4144,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo CheckBlockIndex(); if (!ret) return error("%s: AcceptBlock FAILED", __func__); - else fprintf(stderr,"added block %s %p\n",pindex->GetBlockHash().ToString().c_str(),pindex->pprev); + //else fprintf(stderr,"added block %s %p\n",pindex->GetBlockHash().ToString().c_str(),pindex->pprev); } if (!ActivateBestChain(state, pblock)) @@ -4174,22 +4167,22 @@ bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex // NOTE: CheckBlockHeader is called by CheckBlock if (!ContextualCheckBlockHeader(block, state, pindexPrev)) { - fprintf(stderr,"TestBlockValidity failure A checkPOW.%d\n",fCheckPOW); + //fprintf(stderr,"TestBlockValidity failure A checkPOW.%d\n",fCheckPOW); return false; } if (!CheckBlock(indexDummy.nHeight,0,block, state, verifier, fCheckPOW, fCheckMerkleRoot)) { - fprintf(stderr,"TestBlockValidity failure B checkPOW.%d\n",fCheckPOW); + //fprintf(stderr,"TestBlockValidity failure B checkPOW.%d\n",fCheckPOW); return false; } if (!ContextualCheckBlock(block, state, pindexPrev)) { - fprintf(stderr,"TestBlockValidity failure C checkPOW.%d\n",fCheckPOW); + //fprintf(stderr,"TestBlockValidity failure C checkPOW.%d\n",fCheckPOW); return false; } if (!ConnectBlock(block, state, &indexDummy, viewNew, true,fCheckPOW)) { - fprintf(stderr,"TestBlockValidity failure D checkPOW.%d\n",fCheckPOW); + //fprintf(stderr,"TestBlockValidity failure D checkPOW.%d\n",fCheckPOW); return false; } assert(state.IsValid()); From b5d0fe2e24c3e55e1f9741833440327f27907dc9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 11:55:06 +0300 Subject: [PATCH 14/31] Test --- src/komodo_bitcoind.h | 14 -------------- src/main.cpp | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 374e1e646..9468338aa 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1343,17 +1343,3 @@ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) else return(0); } -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 - { - pindex = AddToBlockIndex(*pblock); - fprintf(stderr,"Block header %s is already known, but without pindex -> ensured %p\n",hash.ToString().c_str(),pindex); - } - } -} - diff --git a/src/main.cpp b/src/main.cpp index 27ac26108..0509da5ed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4103,6 +4103,20 @@ static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned void komodo_currentheight_set(int32_t height); +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 + { + pindex = AddToBlockIndex(*pblock); + fprintf(stderr,"Block header %s is already known, but without pindex -> ensured %p\n",hash.ToString().c_str(),pindex); + } + } +} + bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp) { // Preliminary checks From 8dcf7f3fab69b5ba83a92f51168d6776fe1f61b5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 12:07:57 +0300 Subject: [PATCH 15/31] Test --- src/main.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0509da5ed..9537a4869 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3543,8 +3543,8 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) BlockMap::iterator it = mapBlockIndex.find(hash); if (it != mapBlockIndex.end()) { - //fprintf(stderr,"addtoblockindex already there %p\n",it->second); - if ( ASSETCHAINS_STAKED == 0 || it->second != 0 ) + fprintf(stderr,"addtoblockindex already there %p\n",it->second); + //if ( ASSETCHAINS_STAKED == 0 || it->second != 0 ) return it->second; } // Construct new block index object @@ -3561,6 +3561,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) { if ( (pindexNew->pprev= (*miPrev).second) != 0 ) pindexNew->nHeight = pindexNew->pprev->nHeight + 1; + else fprintf(stderr,"unexpected null pprev %s\n",hash.ToString().c_str()); pindexNew->BuildSkip(); } pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew); @@ -3569,7 +3570,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) pindexBestHeader = pindexNew; setDirtyBlockIndex.insert(pindexNew); - //fprintf(stderr,"added to block index %s\n",hash.ToString().c_str()); + fprintf(stderr,"added to block index %s\n",hash.ToString().c_str()); return pindexNew; } @@ -4123,7 +4124,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo bool checked; uint256 hash; auto verifier = libzcash::ProofVerifier::Disabled(); hash = pblock->GetHash(); - //fprintf(stderr,"process newblock %s\n",hash.ToString().c_str()); + fprintf(stderr,"process newblock %s\n",hash.ToString().c_str()); if ( chainActive.Tip() != 0 ) komodo_currentheight_set(chainActive.Tip()->nHeight); checked = CheckBlock(height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0); @@ -4146,11 +4147,11 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo } // Store to disk CBlockIndex *pindex = NULL; - if ( ASSETCHAINS_STAKED != 0 ) // or other low node count networks + /*if ( ASSETCHAINS_STAKED != 0 ) // or other low node count networks { //komodo_ensure(pblock->hashPrevBlock); komodo_ensure(pblock,hash); - } + }*/ bool ret = AcceptBlock(*pblock, state, &pindex, fRequested, dbp); if (pindex && pfrom) { mapBlockSource[pindex->GetBlockHash()] = pfrom->GetId(); @@ -6003,6 +6004,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, Misbehaving(pfrom->GetId(), 20); return error("non-continuous headers sequence"); } + fprintf(stderr,"headers msg nCount.%d\n",(int32_t)nCount); if (!AcceptBlockHeader(header, state, &pindexLast)) { int nDoS; if (state.IsInvalid(nDoS)) From 5295318ec48a3866ae2a02059a2bb6c1850e4cce Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 12:08:51 +0300 Subject: [PATCH 16/31] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 9537a4869..632e6f723 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4027,7 +4027,7 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, } if ( pindex == 0 ) { - //fprintf(stderr,"AcceptBlock error null pindex\n"); + fprintf(stderr,"AcceptBlock error null pindex\n"); return false; } //fprintf(stderr,"acceptblockheader passed\n"); From 0f102ea7e379876f3bd2e5dbf04e51298318ac97 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 12:14:11 +0300 Subject: [PATCH 17/31] Test --- src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 632e6f723..8ee3236ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4147,11 +4147,11 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo } // Store to disk CBlockIndex *pindex = NULL; - /*if ( ASSETCHAINS_STAKED != 0 ) // or other low node count networks + if ( ASSETCHAINS_STAKED != 0 ) // or other low node count networks { //komodo_ensure(pblock->hashPrevBlock); komodo_ensure(pblock,hash); - }*/ + } bool ret = AcceptBlock(*pblock, state, &pindex, fRequested, dbp); if (pindex && pfrom) { mapBlockSource[pindex->GetBlockHash()] = pfrom->GetId(); From 519578c9c93d2d8cbe2c9ebbd28cc03021c3bcb3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 12:17:02 +0300 Subject: [PATCH 18/31] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 8ee3236ba..fa298ac67 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4112,7 +4112,7 @@ CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) { if ( (pindex= miSelf->second) == 0 ) // create pindex so first Accept block doesnt fail { - pindex = AddToBlockIndex(*pblock); + miSelf->second = AddToBlockIndex(*pblock); fprintf(stderr,"Block header %s is already known, but without pindex -> ensured %p\n",hash.ToString().c_str(),pindex); } } From b1d85b23e6edf7e2eb7e86b2770959359482d5fc Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 12:21:01 +0300 Subject: [PATCH 19/31] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index fa298ac67..c1aa631a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3544,7 +3544,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) if (it != mapBlockIndex.end()) { fprintf(stderr,"addtoblockindex already there %p\n",it->second); - //if ( ASSETCHAINS_STAKED == 0 || it->second != 0 ) + if ( ASSETCHAINS_STAKED == 0 || it->second != 0 ) return it->second; } // Construct new block index object From 66dd02d288fb4b9a1d310ccd7a166711264b0429 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 12:32:39 +0300 Subject: [PATCH 20/31] Test --- src/main.cpp | 17 ++++++++++------- src/miner.cpp | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c1aa631a7..4fdf9710e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3543,9 +3543,12 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) BlockMap::iterator it = mapBlockIndex.find(hash); if (it != mapBlockIndex.end()) { - fprintf(stderr,"addtoblockindex already there %p\n",it->second); - if ( ASSETCHAINS_STAKED == 0 || it->second != 0 ) + if ( ASSETCHAINS_STAKED == 0 || it->second != 0 ) // change behavior to allow komodo_ensure to work + { + // this is the strange case where somehow the hash is in the mapBlockIndex via as yet undetermined process, but the pindex for the hash is not there. Theoretically it is due to processing the block headers, but I have seen it get this case without having received it from the block headers or anywhere else... jl777 + //fprintf(stderr,"addtoblockindex already there %p\n",it->second); return it->second; + } } // Construct new block index object CBlockIndex* pindexNew = new CBlockIndex(block); @@ -3570,7 +3573,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) pindexBestHeader = pindexNew; setDirtyBlockIndex.insert(pindexNew); - fprintf(stderr,"added to block index %s\n",hash.ToString().c_str()); + //fprintf(stderr,"added to block index %s %p\n",hash.ToString().c_str(),pindexNew); return pindexNew; } @@ -4113,7 +4116,7 @@ CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) 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(),pindex); + fprintf(stderr,"Block header %s is already known, but without pindex -> ensured %p\n",hash.ToString().c_str(),miSelf->second); } } } @@ -4149,7 +4152,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo CBlockIndex *pindex = NULL; if ( ASSETCHAINS_STAKED != 0 ) // or other low node count networks { - //komodo_ensure(pblock->hashPrevBlock); + // 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(*pblock, state, &pindex, fRequested, dbp); @@ -4378,7 +4381,7 @@ CBlockIndex * InsertBlockIndex(uint256 hash) throw runtime_error("LoadBlockIndex(): new CBlockIndex failed"); mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first; pindexNew->phashBlock = &((*mi).first); - fprintf(stderr,"inserted to block index %s\n",hash.ToString().c_str()); + //fprintf(stderr,"inserted to block index %s\n",hash.ToString().c_str()); return pindexNew; } @@ -6004,7 +6007,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, Misbehaving(pfrom->GetId(), 20); return error("non-continuous headers sequence"); } - fprintf(stderr,"headers msg nCount.%d\n",(int32_t)nCount); + //fprintf(stderr,"headers msg nCount.%d\n",(int32_t)nCount); if (!AcceptBlockHeader(header, state, &pindexLast)) { int nDoS; if (state.IsInvalid(nDoS)) diff --git a/src/miner.cpp b/src/miner.cpp index 7b7b81a19..4b5b49bf4 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -637,7 +637,7 @@ static bool ProcessBlockFound(CBlock* pblock) TrackMinedBlock(pblock->GetHash()); if ( ASSETCHAINS_STAKED != 0 ) { - fprintf(stderr,"broadcast new block t.%u\n",(uint32_t)time(NULL)); + //fprintf(stderr,"broadcast new block t.%u\n",(uint32_t)time(NULL)); { LOCK(cs_vNodes); BOOST_FOREACH(CNode* pnode, vNodes) @@ -647,7 +647,7 @@ static bool ProcessBlockFound(CBlock* pblock) pnode->PushMessage("block", *pblock); } } - fprintf(stderr,"finished broadcast new block t.%u\n",(uint32_t)time(NULL)); + //fprintf(stderr,"finished broadcast new block t.%u\n",(uint32_t)time(NULL)); } return true; } From b85130925bef33690e0ac708a9c48ab99a629adc Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 12:42:27 +0300 Subject: [PATCH 21/31] Test --- src/main.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 4fdf9710e..af935ef0c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3541,6 +3541,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) // Check for duplicate uint256 hash = block.GetHash(); BlockMap::iterator it = mapBlockIndex.find(hash); + BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock); if (it != mapBlockIndex.end()) { if ( ASSETCHAINS_STAKED == 0 || it->second != 0 ) // change behavior to allow komodo_ensure to work @@ -3549,6 +3550,11 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) //fprintf(stderr,"addtoblockindex already there %p\n",it->second); return it->second; } + if ( miPrev != mapBlockIndex.end() && (*miPrev).second == 0 ) + { + fprintf(stderr,"edge case of both block and prevblock in the strange state\n") + return(0); // return here to avoid the state of pindex->nHeight not set and pprev NULL + } } // Construct new block index object CBlockIndex* pindexNew = new CBlockIndex(block); @@ -3559,7 +3565,6 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) pindexNew->nSequenceId = 0; BlockMap::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first; pindexNew->phashBlock = &((*mi).first); - BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock); if (miPrev != mapBlockIndex.end()) { if ( (pindexNew->pprev= (*miPrev).second) != 0 ) From 820600d13f768296d23775418ddac958d9376f4b Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 12:43:08 +0300 Subject: [PATCH 22/31] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index af935ef0c..f6d63874a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3552,7 +3552,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) } if ( miPrev != mapBlockIndex.end() && (*miPrev).second == 0 ) { - fprintf(stderr,"edge case of both block and prevblock in the strange state\n") + fprintf(stderr,"edge case of both block and prevblock in the strange state\n"); return(0); // return here to avoid the state of pindex->nHeight not set and pprev NULL } } From 9464ac21bc1c14cc16bb32f62aa37a9993293079 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 13:03:27 +0300 Subject: [PATCH 23/31] Test --- src/komodo_bitcoind.h | 29 +++++++++++++++++++---------- src/main.cpp | 2 +- src/miner.cpp | 5 +++-- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 9468338aa..b0dac8445 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -658,7 +658,7 @@ int32_t komodo_block2height(CBlock *block) return(height); } -void komodo_block2pubkey33(uint8_t *pubkey33,CBlock *block) +int32_t komodo_block2pubkey33(uint8_t *pubkey33,CBlock *block) { int32_t n; if ( KOMODO_LOADINGBLOCKS == 0 ) @@ -674,8 +674,12 @@ void komodo_block2pubkey33(uint8_t *pubkey33,CBlock *block) //komodo_init(0); n = block->vtx[0].vout[0].scriptPubKey.size(); if ( n == 35 ) + { memcpy(pubkey33,ptr+1,33); + return(1); + } } + return(0); } int32_t komodo_blockload(CBlock& block,CBlockIndex *pindex) @@ -1045,7 +1049,7 @@ int32_t komodo_validate_interest(const CTransaction &tx,int32_t txheight,uint32_ } /* - komodo_checkPOW (fast) is called early in the process and should only refer to data immediately available. it is a filter to prevent bad blocks from going into the local DB + komodo_checkPOW (fast) is called early in the process and should only refer to data immediately available. it is a filter to prevent bad blocks from going into the local DB. The more blocks we can filter out at this stage, the less junk in the local DB that will just get purged later on. komodo_checkPOW (slow) is called right before connecting blocks so all prior blocks can be assumed to be there and all checks must pass @@ -1261,28 +1265,28 @@ int32_t komodo_is_PoSblock(int32_t slowflag,int32_t height,CBlock *pblock,arith_ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) { - uint256 hash; arith_uint256 bnTarget,bhash; bool fNegative,fOverflow; uint8_t *script,pubkey33[33],pubkeys[64][33]; int32_t i,PoSperc,is_PoSblock=0,n,failed = 0,notaryid = -1; int64_t checktoshis,value; CBlockIndex *pprev; + uint256 hash; arith_uint256 bnTarget,bhash; bool fNegative,fOverflow; uint8_t *script,pubkey33[33],pubkeys[64][33]; int32_t i,possible,PoSperc,is_PoSblock=0,n,failed = 0,notaryid = -1; int64_t checktoshis,value; CBlockIndex *pprev; if ( !CheckEquihashSolution(pblock, Params()) ) { fprintf(stderr,"komodo_checkPOW slowflag.%d ht.%d CheckEquihashSolution failed\n",slowflag,height); return(-1); } hash = pblock->GetHash(); - if ( height == 0 ) + bnTarget.SetCompact(pblock->nBits,&fNegative,&fOverflow); + bhash = UintToArith256(hash); + possible = komodo_block2pubkey33(pubkey33,pblock); + if ( height == 0 && slowflag != 0 ) // we need to assume all prior height is in the block index { if ( (pprev= mapBlockIndex[pblock->hashPrevBlock]) != 0 ) height = pprev->nHeight + 1; - //fprintf(stderr,"komodo_checkPOW slowflag.%d ht.%d zeroheight\n",slowflag,height); + fprintf(stderr,"komodo_checkPOW slowflag.%d zeroheight -> calcht.%d \n",slowflag,height); //if ( height == 0 ) - return(0); + return(-1); } - bnTarget.SetCompact(pblock->nBits,&fNegative,&fOverflow); - bhash = UintToArith256(hash); - komodo_block2pubkey33(pubkey33,pblock); if ( bhash > bnTarget ) { failed = 1; - if ( ASSETCHAINS_SYMBOL[0] == 0 ) // for the fast case + if ( height > 0 && ASSETCHAINS_SYMBOL[0] == 0 ) // for the fast case { if ( (n= komodo_notaries(pubkeys,height,pblock->nTime)) > 0 ) { @@ -1294,6 +1298,11 @@ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) } } } + else if ( possible == 0 || ASSETCHAINS_SYMBOL[0] != 0 ) + { + fprintf(stderr,"pow violation and no chance it is notary ht.%d %s\n",height,hash.ToString().c_str()); + return(-1); + } } else if ( ASSETCHAINS_STAKED != 0 && height >= 2 ) // must PoS or have at least 16x better PoW { diff --git a/src/main.cpp b/src/main.cpp index f6d63874a..c23c297e8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,7 +55,7 @@ CCriticalSection cs_main; extern uint8_t NOTARY_PUBKEY33[33]; extern int32_t KOMODO_LOADINGBLOCKS,KOMODO_LONGESTCHAIN; int32_t KOMODO_NEWBLOCKS; -void komodo_block2pubkey33(uint8_t *pubkey33,CBlock *block); +int32_t komodo_block2pubkey33(uint8_t *pubkey33,CBlock *block); BlockMap mapBlockIndex; CChain chainActive; diff --git a/src/miner.cpp b/src/miner.cpp index 4b5b49bf4..bc9d06729 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -401,8 +401,9 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) pblocktemplate->vTxSigOps.push_back(GetLegacySigOpCount(txStaked)); nFees += txfees; pblock->nTime = blocktime; - if ( GetAdjustedTime() < pblock->nTime ) + if ( GetAdjustedTime() < pblock->nTime || pblock->GetBlockTime() > GetAdjustedTime() + 60) { + return(0); printf("need to wait %d seconds to submit: ",(int32_t)(pblock->nTime - GetAdjustedTime())); while ( GetAdjustedTime()+30 < pblock->nTime ) { @@ -413,7 +414,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) //sleep(pblock->nTime - GetAdjustedTime()); } - } else fprintf(stderr,"no utxos eligible for staking\n"); + } else return(0); //fprintf(stderr,"no utxos eligible for staking\n"); } // Create coinbase tx From e16d22ee0fa187f4ac09027449d6ad79bc6c8fa4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 13:13:18 +0300 Subject: [PATCH 24/31] Test --- src/miner.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/miner.cpp b/src/miner.cpp index bc9d06729..813fafe2a 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -392,7 +392,8 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) { uint64_t txfees,utxovalue; uint32_t txtime; uint256 utxotxid,revtxid; int32_t i,siglen,numsigs,utxovout; uint8_t utxosig[128],*ptr; CMutableTransaction txStaked = CreateNewContextualCMutableTransaction(Params().GetConsensus(), chainActive.Height() + 1); - blocktime += 2; + if ( blocktime > pindexPrev->GetMedianTimePast()+60 ) + blocktime = pindexPrev->GetMedianTimePast() + 60; if ( (siglen= komodo_staked(txStaked,pblock->nBits,&blocktime,&txtime,&utxotxid,&utxovout,&utxovalue,utxosig)) > 0 ) { CAmount txfees = 0; @@ -766,6 +767,7 @@ void static BitcoinMiner() static uint32_t counter; if ( counter++ < 100 && ASSETCHAINS_STAKED == 0 ) fprintf(stderr,"created illegal block, retry\n"); + sleep(1); continue; } unique_ptr pblocktemplate(ptr); From dd66e3af3aa926911ab103acd6a70e25a180eac6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 13:20:26 +0300 Subject: [PATCH 25/31] Test --- src/miner.cpp | 2 +- src/wallet/rpcwallet.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index 813fafe2a..d1f348b64 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -767,7 +767,7 @@ void static BitcoinMiner() static uint32_t counter; if ( counter++ < 100 && ASSETCHAINS_STAKED == 0 ) fprintf(stderr,"created illegal block, retry\n"); - sleep(1); + sleep(3); continue; } unique_ptr pblocktemplate(ptr); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 010f51319..c6b5557cd 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4537,7 +4537,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt for (i=0; i Date: Fri, 20 Apr 2018 13:24:48 +0300 Subject: [PATCH 26/31] Test --- src/miner.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index d1f348b64..d793cd92f 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -402,16 +402,15 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) pblocktemplate->vTxSigOps.push_back(GetLegacySigOpCount(txStaked)); nFees += txfees; pblock->nTime = blocktime; - if ( GetAdjustedTime() < pblock->nTime || pblock->GetBlockTime() > GetAdjustedTime() + 60) + if ( GetAdjustedTime() < pblock->nTime )//|| pblock->GetBlockTime() > GetAdjustedTime() + 60) { - return(0); printf("need to wait %d seconds to submit: ",(int32_t)(pblock->nTime - GetAdjustedTime())); - while ( GetAdjustedTime()+30 < pblock->nTime ) + /*while ( GetAdjustedTime()+30 < pblock->nTime ) { sleep(30); fprintf(stderr,"%d ",(int32_t)(pblock->nTime - GetAdjustedTime())); } - fprintf(stderr,"finished waiting\n"); + fprintf(stderr,"finished waiting\n");*/ //sleep(pblock->nTime - GetAdjustedTime()); } From cfb55edbbc77a73d6859ee64c3198b38e8150452 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 13:25:22 +0300 Subject: [PATCH 27/31] Test --- src/miner.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index d793cd92f..62dc915d6 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -392,8 +392,8 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) { uint64_t txfees,utxovalue; uint32_t txtime; uint256 utxotxid,revtxid; int32_t i,siglen,numsigs,utxovout; uint8_t utxosig[128],*ptr; CMutableTransaction txStaked = CreateNewContextualCMutableTransaction(Params().GetConsensus(), chainActive.Height() + 1); - if ( blocktime > pindexPrev->GetMedianTimePast()+60 ) - blocktime = pindexPrev->GetMedianTimePast() + 60; + //if ( blocktime > pindexPrev->GetMedianTimePast()+60 ) + // blocktime = pindexPrev->GetMedianTimePast() + 60; if ( (siglen= komodo_staked(txStaked,pblock->nBits,&blocktime,&txtime,&utxotxid,&utxovout,&utxovalue,utxosig)) > 0 ) { CAmount txfees = 0; From 563d507c6156afcca5f6d984f43ba1c783748c0e Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 13:30:54 +0300 Subject: [PATCH 28/31] Test --- src/miner.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index 62dc915d6..c2a7bf1fd 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -404,16 +404,15 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) pblock->nTime = blocktime; if ( GetAdjustedTime() < pblock->nTime )//|| pblock->GetBlockTime() > GetAdjustedTime() + 60) { - printf("need to wait %d seconds to submit: ",(int32_t)(pblock->nTime - GetAdjustedTime())); - /*while ( GetAdjustedTime()+30 < pblock->nTime ) + fprintf(stderr,"need to wait %d seconds to mine:\n",(int32_t)(pblock->nTime - GetAdjustedTime())); + while ( GetAdjustedTime()+30 < pblock->nTime ) { sleep(30); fprintf(stderr,"%d ",(int32_t)(pblock->nTime - GetAdjustedTime())); } - fprintf(stderr,"finished waiting\n");*/ + fprintf(stderr,"finished waiting\n"); //sleep(pblock->nTime - GetAdjustedTime()); } - } else return(0); //fprintf(stderr,"no utxos eligible for staking\n"); } From 3128a24af6c11473583a892e0c61931b6d9c27ab Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 13:35:16 +0300 Subject: [PATCH 29/31] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index c23c297e8..257b133ff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4132,7 +4132,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo bool checked; uint256 hash; auto verifier = libzcash::ProofVerifier::Disabled(); hash = pblock->GetHash(); - fprintf(stderr,"process newblock %s\n",hash.ToString().c_str()); + //fprintf(stderr,"process newblock %s\n",hash.ToString().c_str()); if ( chainActive.Tip() != 0 ) komodo_currentheight_set(chainActive.Tip()->nHeight); checked = CheckBlock(height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0); From a772476349ba0faca67832f92a670d84762480f4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 16:55:34 +0300 Subject: [PATCH 30/31] Test --- src/main.cpp | 6 +++--- src/miner.cpp | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 257b133ff..e85ca46c3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3544,7 +3544,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock); if (it != mapBlockIndex.end()) { - if ( ASSETCHAINS_STAKED == 0 || it->second != 0 ) // change behavior to allow komodo_ensure to work + if ( vNodes.size() < 4 || it->second != 0 ) // change behavior to allow komodo_ensure to work { // this is the strange case where somehow the hash is in the mapBlockIndex via as yet undetermined process, but the pindex for the hash is not there. Theoretically it is due to processing the block headers, but I have seen it get this case without having received it from the block headers or anywhere else... jl777 //fprintf(stderr,"addtoblockindex already there %p\n",it->second); @@ -3754,7 +3754,7 @@ bool CheckBlockHeader(int32_t height,CBlockIndex *pindex, const CBlockHeader& bl } if (blockhdr.GetBlockTime() > GetAdjustedTime() + 60) return state.Invalid(error("CheckBlockHeader(): block timestamp too far in the future"),REJECT_INVALID, "time-too-new"); - else if ( ASSETCHAINS_STAKED != 0 && pindex != 0 && pindex->pprev != 0 && pindex->nTime <= pindex->pprev->nTime ) + else if ( ASSETCHAINS_STAKED != 0 && pindex != 0 && pindex->pprev != 0 && pindex->nTime < pindex->pprev->nTime ) { fprintf(stderr,"ht.%d %u vs ht.%d %u, is not monotonic\n",pindex->nHeight,pindex->nTime,pindex->pprev->nHeight,pindex->pprev->nTime); return state.Invalid(error("CheckBlockHeader(): block timestamp needs to always increase"),REJECT_INVALID, "time-too-new"); @@ -4155,7 +4155,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo } // Store to disk CBlockIndex *pindex = NULL; - if ( ASSETCHAINS_STAKED != 0 ) // or other low node count networks + if ( vNodes.size() < 4 ) { // 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); diff --git a/src/miner.cpp b/src/miner.cpp index c2a7bf1fd..ec40fb449 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -635,8 +635,9 @@ static bool ProcessBlockFound(CBlock* pblock) return error("KomodoMiner: ProcessNewBlock, block not accepted"); TrackMinedBlock(pblock->GetHash()); - if ( ASSETCHAINS_STAKED != 0 ) + if ( vNodes.size() < 8 ) { + int32_t n = 1; //fprintf(stderr,"broadcast new block t.%u\n",(uint32_t)time(NULL)); { LOCK(cs_vNodes); @@ -644,7 +645,11 @@ static bool ProcessBlockFound(CBlock* pblock) { if ( pnode->hSocket == INVALID_SOCKET ) continue; - pnode->PushMessage("block", *pblock); + if ( (rand() % n) == 0 ) + { + pnode->PushMessage("block", *pblock); + n++; + } } } //fprintf(stderr,"finished broadcast new block t.%u\n",(uint32_t)time(NULL)); From 2ac071da4751996495ce3b4bb02c827d2438926b Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Apr 2018 17:16:06 +0300 Subject: [PATCH 31/31] KOMODO_LIMITED_NETWORKSIZE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prior commits relate to supporting a limited size network. It looks like there is an edge case where it is possible for a hash to be in the mapIndex, without a pindex. If a block with such a hash is tried to be Accepted, the AcceptBlockHeader returns true but with a null pindex, and AcceptBlock fails. The code says that this indicates that the hash was added from a blockheader without a block, but I didn’t see that happening. In any case, it happens a lot on a 2 node network. So much so that if one node is just mining, before too long the other node will not accept the block and once that happens, no subsequent block would work as the prior block is missing. Of course, with more nodes, these blocks will be around a lot more and likely it won’t be such an issue, but not so sure that it doesn’t require restarting the node to get it back on track again. These prior commits implement a KOMODO_LIMITED_NETWORKSIZE logic where if it sees that a block has come in which is in the mapIndex but has no pindex, it automatically addtoblockindex. There is one edge case still left where both the current block being processed and its previous block are in this limbo state. Since the pindex is not mapped to the block, it is problematic to retrieve the CBlock for the pprev and without the valid pprev the pindex will have a null pprev and no nHeight set. It is possible that all the other code will properly deal with such a case and automatically fix it, but rather than rely on that, in such a case the automatic addtoblockindex is not done. A node in such a state would need to exit and restart or somehow fill in the data from other nodes. From what I can tell, the above is the main reason why the PoS/PoW networks were having problems staying in sync. Since even with one mining node, it was just a matter of time before the other node got stuck, with more than one mining node we end up with independent forks that won’t reconcile until the node is restarted. --- src/komodo_defs.h | 1 + src/main.cpp | 4 ++-- src/miner.cpp | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index db792c44e..3f8b498fa 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -5,5 +5,6 @@ #define KOMODO_ELECTION_GAP 2000 #define ROUNDROBIN_DELAY 61 #define KOMODO_ASSETCHAIN_MAXLEN 65 +#define KOMODO_LIMITED_NETWORKSIZE 4 #endif diff --git a/src/main.cpp b/src/main.cpp index e85ca46c3..13d2ceb9e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3544,7 +3544,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock); if (it != mapBlockIndex.end()) { - if ( vNodes.size() < 4 || it->second != 0 ) // change behavior to allow komodo_ensure to work + if ( vNodes.size() < KOMODO_LIMITED_NETWORKSIZE || it->second != 0 ) // change behavior to allow komodo_ensure to work { // this is the strange case where somehow the hash is in the mapBlockIndex via as yet undetermined process, but the pindex for the hash is not there. Theoretically it is due to processing the block headers, but I have seen it get this case without having received it from the block headers or anywhere else... jl777 //fprintf(stderr,"addtoblockindex already there %p\n",it->second); @@ -4155,7 +4155,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo } // Store to disk CBlockIndex *pindex = NULL; - if ( vNodes.size() < 4 ) + if ( vNodes.size() < KOMODO_LIMITED_NETWORKSIZE ) { // 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); diff --git a/src/miner.cpp b/src/miner.cpp index ec40fb449..3bd4b1c66 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -635,7 +635,7 @@ static bool ProcessBlockFound(CBlock* pblock) return error("KomodoMiner: ProcessNewBlock, block not accepted"); TrackMinedBlock(pblock->GetHash()); - if ( vNodes.size() < 8 ) + if ( vNodes.size() < KOMODO_LIMITED_NETWORKSIZE*2 ) { int32_t n = 1; //fprintf(stderr,"broadcast new block t.%u\n",(uint32_t)time(NULL));