From 78fa6faa82f5b998dc118311bd791d1be040e2de Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 19:11:06 +0300 Subject: [PATCH 01/22] -60 test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 7b746c3d2..15f3f92db 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3759,7 +3759,7 @@ bool CheckBlockHeader(int32_t *futureblockp,int32_t height,CBlockIndex *pindex, } } *futureblockp = 0; - if (blockhdr.GetBlockTime() > GetAdjustedTime() + 60) + if (blockhdr.GetBlockTime() > GetAdjustedTime() - 60) { CBlockIndex *tipindex; fprintf(stderr,"ht.%d future block %u vs time.%u + 60\n",height,(uint32_t)blockhdr.GetBlockTime(),(uint32_t)GetAdjustedTime()); From 48f12344d29ff1315094b7b38147c24ce7e760e6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 19:11:44 +0300 Subject: [PATCH 02/22] revert --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 15f3f92db..7b746c3d2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3759,7 +3759,7 @@ bool CheckBlockHeader(int32_t *futureblockp,int32_t height,CBlockIndex *pindex, } } *futureblockp = 0; - if (blockhdr.GetBlockTime() > GetAdjustedTime() - 60) + if (blockhdr.GetBlockTime() > GetAdjustedTime() + 60) { CBlockIndex *tipindex; fprintf(stderr,"ht.%d future block %u vs time.%u + 60\n",height,(uint32_t)blockhdr.GetBlockTime(),(uint32_t)GetAdjustedTime()); From 06ce10a9a7d9bce8c071ce6ed231583a898b8d4b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 19:24:14 +0300 Subject: [PATCH 03/22] Test --- src/main.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 7b746c3d2..c4c3e29db 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4012,7 +4012,11 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat if (ppindex) *ppindex = pindex; if ( pindex != 0 && pindex->nStatus & BLOCK_FAILED_MASK ) - return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate"); + { + if (!CheckBlockHeader(futureblockp,*ppindex!=0?(*ppindex)->nHeight:0,*ppindex, block, state,0)) + return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate"); + else pindex->nStatus &= ~BLOCK_FAILED_MASK; + } /*if ( pindex != 0 && hash == komodo_requestedhash ) { fprintf(stderr,"AddToBlockIndex A komodo_requestedhash %s\n",komodo_requestedhash.ToString().c_str()); From 85f1853e3553203c3483af406efbf4cbd117cd26 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 19:28:42 +0300 Subject: [PATCH 04/22] Revert --- src/main.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c4c3e29db..7b746c3d2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4012,11 +4012,7 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat if (ppindex) *ppindex = pindex; if ( pindex != 0 && pindex->nStatus & BLOCK_FAILED_MASK ) - { - if (!CheckBlockHeader(futureblockp,*ppindex!=0?(*ppindex)->nHeight:0,*ppindex, block, state,0)) - return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate"); - else pindex->nStatus &= ~BLOCK_FAILED_MASK; - } + return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate"); /*if ( pindex != 0 && hash == komodo_requestedhash ) { fprintf(stderr,"AddToBlockIndex A komodo_requestedhash %s\n",komodo_requestedhash.ToString().c_str()); From 42c2e91bbfdecec09709b765d46869fe045346ce Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 19:32:48 +0300 Subject: [PATCH 05/22] Test --- src/main.cpp | 2 +- src/main.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 7b746c3d2..31a95a43a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4110,7 +4110,7 @@ bool AcceptBlock(int32_t *futureblockp,CBlock& block, CValidationState& state, C // blocks which are too close in height to the tip. Apply this test // regardless of whether pruning is enabled; it should generally be safe to // not process unrequested blocks. - bool fTooFarAhead = (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP)); + bool fTooFarAhead = (pindex->nHeight > int(chainActive.Height() + BLOCK_DOWNLOAD_WINDOW)); //MIN_BLOCKS_TO_KEEP)); // TODO: deal better with return value and error conditions for duplicate // and unrequested blocks. diff --git a/src/main.h b/src/main.h index ef46cd83d..956d8351e 100644 --- a/src/main.h +++ b/src/main.h @@ -94,7 +94,7 @@ static const unsigned int MAX_HEADERS_RESULTS = 160; * Larger windows tolerate larger download speed differences between peer, but increase the potential * degree of disordering of blocks on disk (which make reindexing and in the future perhaps pruning * harder). We'll probably want to make this a per-peer adaptive value at some point. */ -static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024; +static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024 * 2; /** Time to wait (in seconds) between writing blocks/block index to disk. */ static const unsigned int DATABASE_WRITE_INTERVAL = 60 * 60; /** Time to wait (in seconds) between flushing chainstate to disk. */ From e8314e82f50412bf8c244fdd0752a1caba0e920f Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 20:23:29 +0300 Subject: [PATCH 06/22] +logprints --- src/main.cpp | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 31a95a43a..af10d91be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3774,6 +3774,7 @@ bool CheckBlockHeader(int32_t *futureblockp,int32_t height,CBlockIndex *pindex, { if (blockhdr.GetBlockTime() < GetAdjustedTime() + 600) *futureblockp = 1; + LogPrintf("CheckBlockHeader block from future %d error",blockhdr.GetBlockTime() - GetAdjustedTime()); return false; //state.Invalid(error("CheckBlockHeader(): block timestamp too far in the future"),REJECT_INVALID, "time-too-new"); } } @@ -3815,8 +3816,10 @@ bool CheckBlock(int32_t *futureblockp,int32_t height,CBlockIndex *pindex,const C if (!CheckBlockHeader(futureblockp,height,pindex,block,state,fCheckPOW)) { if ( *futureblockp == 0 ) + { + LogPrintf("CheckBlock header error"); return false; - //else fprintf(stderr,"checkblockheader PoW.%d got futureblock\n",fCheckPOW); + } } if ( fCheckPOW ) { @@ -3888,6 +3891,7 @@ bool CheckBlock(int32_t *futureblockp,int32_t height,CBlockIndex *pindex,const C //static uint32_t counter; //if ( counter++ < 100 && ASSETCHAINS_STAKED == 0 ) // fprintf(stderr,"check deposit rejection\n"); + LogPrintf("CheckBlockHeader komodo_check_deposit error"); return(false); } return true; @@ -4026,8 +4030,10 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat if (!CheckBlockHeader(futureblockp,*ppindex!=0?(*ppindex)->nHeight:0,*ppindex, block, state,0)) { if ( *futureblockp == 0 ) + { + LogPrintf("AcceptBlockHeader CheckBlockHeader error"); return false; - //else fprintf(stderr,"AcceptBlockHeader: CheckBlockHeader got future block\n"); + } } // Get prev block index CBlockIndex* pindexPrev = NULL; @@ -4042,6 +4048,7 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat komodo_requestedhash = block.hashPrevBlock; komodo_requestedcount = 0; }*/ + LogPrintf("AcceptBlockHeader hashPrevBlock %s not found",block.hashPrevBlock.ToString().c_str()); return(false); //return state.DoS(10, error("%s: prev block not found", __func__), 0, "bad-prevblk"); } @@ -4054,6 +4061,7 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat komodo_requestedhash = block.hashPrevBlock; komodo_requestedcount = 0; }*/ + LogPrintf("AcceptBlockHeader hashPrevBlock %s no pindexPrev",block.hashPrevBlock.ToString().c_str()); return(false); } if ( (pindexPrev->nStatus & BLOCK_FAILED_MASK) ) @@ -4062,13 +4070,14 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat if (!ContextualCheckBlockHeader(block, state, pindexPrev)) { //fprintf(stderr,"AcceptBlockHeader ContextualCheckBlockHeader failed\n"); + LogPrintf("AcceptBlockHeader ContextualCheckBlockHeader failed"); return false; } if (pindex == NULL) { if ( (pindex= AddToBlockIndex(block)) == 0 ) { - fprintf(stderr,"AcceptBlockHeader couldnt add to block index\n"); + //fprintf(stderr,"AcceptBlockHeader couldnt add to block index\n"); } } if (ppindex) @@ -4091,12 +4100,14 @@ bool AcceptBlock(int32_t *futureblockp,CBlock& block, CValidationState& state, C if (!AcceptBlockHeader(futureblockp,block, state, &pindex)) { if ( *futureblockp == 0 ) + { + LogPrintf("AcceptBlock AcceptBlockHeader error"); return false; - //else fprintf(stderr,"AcceptBlock AcceptBlockHeader got future block\n"); + } } if ( pindex == 0 ) { - //fprintf(stderr,"AcceptBlock error null pindex\n"); + LogPrintf("AcceptBlock null pindex error"); return false; } //fprintf(stderr,"acceptblockheader passed\n"); @@ -4132,8 +4143,9 @@ bool AcceptBlock(int32_t *futureblockp,CBlock& block, CValidationState& state, C pindex->nStatus |= BLOCK_FAILED_VALID; setDirtyBlockIndex.insert(pindex); } + LogPrintf("AcceptBlock CheckBlock or ContextualCheckBlock error"); return false; - } else fprintf(stderr,"CheckBlock or ContextualCheckBlock got futureblock\n"); + } } int nHeight = pindex->nHeight; @@ -4159,7 +4171,8 @@ bool AcceptBlock(int32_t *futureblockp,CBlock& block, CValidationState& state, C FlushStateToDisk(state, FLUSH_STATE_NONE); // we just allocated more disk space for block files if ( *futureblockp == 0 ) return true; - else return false; + LogPrintf("AcceptBlock block from future error"); + return false; } static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams) From 395f10cf91a0e08e6e4f8dbec64a5f08f82972cb Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 21:09:51 +0300 Subject: [PATCH 07/22] Test --- src/main.cpp | 3 +++ src/main.h | 2 +- src/miner.cpp | 41 +++++++++++++++++++++-------------------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index af10d91be..c1ca3f9a8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -56,6 +56,7 @@ extern uint8_t NOTARY_PUBKEY33[33]; extern int32_t KOMODO_LOADINGBLOCKS,KOMODO_LONGESTCHAIN; int32_t KOMODO_NEWBLOCKS; int32_t komodo_block2pubkey33(uint8_t *pubkey33,CBlock *block); +void komodo_broadcast(CBlock *pblock,int32_t limit); BlockMap mapBlockIndex; CChain chainActive; @@ -3218,6 +3219,8 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1; LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001); LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001); + if ( ASSETHCHAINS_SYMBOL[0] == 0 ) + komodo_broadcast(pblock,8); return true; } diff --git a/src/main.h b/src/main.h index 956d8351e..ef46cd83d 100644 --- a/src/main.h +++ b/src/main.h @@ -94,7 +94,7 @@ static const unsigned int MAX_HEADERS_RESULTS = 160; * Larger windows tolerate larger download speed differences between peer, but increase the potential * degree of disordering of blocks on disk (which make reindexing and in the future perhaps pruning * harder). We'll probably want to make this a per-peer adaptive value at some point. */ -static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024 * 2; +static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024; /** Time to wait (in seconds) between writing blocks/block index to disk. */ static const unsigned int DATABASE_WRITE_INTERVAL = 60 * 60; /** Time to wait (in seconds) between flushing chainstate to disk. */ diff --git a/src/miner.cpp b/src/miner.cpp index d9da98f3c..25c71918d 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -584,6 +584,26 @@ CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey) return CreateNewBlock(scriptPubKey); } +void komodo_broadcast(CBlock *pblock,int32_t limit) +{ + int32_t n = 1; + //fprintf(stderr,"broadcast new block t.%u\n",(uint32_t)time(NULL)); + { + LOCK(cs_vNodes); + BOOST_FOREACH(CNode* pnode, vNodes) + { + if ( pnode->hSocket == INVALID_SOCKET ) + continue; + if ( (rand() % n) == 0 ) + { + pnode->PushMessage("block", *pblock); + if ( n++ > limit ) + break; + } + } + } + //fprintf(stderr,"finished broadcast new block t.%u\n",(uint32_t)time(NULL)); +} static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) #else @@ -635,26 +655,7 @@ static bool ProcessBlockFound(CBlock* pblock) return error("KomodoMiner: ProcessNewBlock, block not accepted"); TrackMinedBlock(pblock->GetHash()); - //if ( vNodes.size() < KOMODO_LIMITED_NETWORKSIZE*2 ) - { - int32_t n = 1; - //fprintf(stderr,"broadcast new block t.%u\n",(uint32_t)time(NULL)); - { - LOCK(cs_vNodes); - BOOST_FOREACH(CNode* pnode, vNodes) - { - if ( pnode->hSocket == INVALID_SOCKET ) - continue; - if ( (rand() % n) == 0 ) - { - pnode->PushMessage("block", *pblock); - if ( n++ > 8 ) - break; - } - } - } - //fprintf(stderr,"finished broadcast new block t.%u\n",(uint32_t)time(NULL)); - } + komodo_broadcast(pblock,16); return true; } From c4318f427bce4c72365c4476802bd72babdd4552 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 21:11:32 +0300 Subject: [PATCH 08/22] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index c1ca3f9a8..6ba882d2e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3219,7 +3219,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1; LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001); LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001); - if ( ASSETHCHAINS_SYMBOL[0] == 0 ) + if ( ASSETCHAINS_SYMBOL[0] == 0 ) komodo_broadcast(pblock,8); return true; } From 857c644fadddc4db4a86525001c1b7433b1f5dd2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 22:46:23 +0300 Subject: [PATCH 09/22] Auto set index --- src/main.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6ba882d2e..8046ec64c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4104,14 +4104,18 @@ bool AcceptBlock(int32_t *futureblockp,CBlock& block, CValidationState& state, C { if ( *futureblockp == 0 ) { - LogPrintf("AcceptBlock AcceptBlockHeader error"); + LogPrintf("AcceptBlock AcceptBlockHeader error\n"); return false; } } if ( pindex == 0 ) { - LogPrintf("AcceptBlock null pindex error"); - return false; + *ppindex = pindex = AddToBlockIndex(block); + if ( pindex == 0 ) + { + LogPrintf("AcceptBlock null pindex error\n"); + return false; + } } //fprintf(stderr,"acceptblockheader passed\n"); // Try to process all requested blocks that we don't have, but only @@ -4174,7 +4178,7 @@ bool AcceptBlock(int32_t *futureblockp,CBlock& block, CValidationState& state, C FlushStateToDisk(state, FLUSH_STATE_NONE); // we just allocated more disk space for block files if ( *futureblockp == 0 ) return true; - LogPrintf("AcceptBlock block from future error"); + LogPrintf("AcceptBlock block from future error\n"); return false; } @@ -4203,7 +4207,7 @@ CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) 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 ) + if ( hash != chainparams.GetConsensus().hashGenesisBlock ) { miSelf = mapBlockIndex.find(pblock->hashPrevBlock); if ( miSelf == mapBlockIndex.end() ) @@ -4211,7 +4215,7 @@ CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) miSelf->second = InsertBlockIndex(pblock->hashPrevBlock); fprintf(stderr,"autocreate previndex %s\n",pblock->hashPrevBlock.ToString().c_str()); } - }*/ + } } } From b1af3ff1653739e9ef18361208fb75a9ec7e9f7d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 22:47:45 +0300 Subject: [PATCH 10/22] Test --- src/main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8046ec64c..7878970a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4034,7 +4034,7 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat { if ( *futureblockp == 0 ) { - LogPrintf("AcceptBlockHeader CheckBlockHeader error"); + LogPrintf("AcceptBlockHeader CheckBlockHeader error\n"); return false; } } @@ -4051,7 +4051,7 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat komodo_requestedhash = block.hashPrevBlock; komodo_requestedcount = 0; }*/ - LogPrintf("AcceptBlockHeader hashPrevBlock %s not found",block.hashPrevBlock.ToString().c_str()); + LogPrintf("AcceptBlockHeader hashPrevBlock %s not found\n",block.hashPrevBlock.ToString().c_str()); return(false); //return state.DoS(10, error("%s: prev block not found", __func__), 0, "bad-prevblk"); } @@ -4064,7 +4064,7 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat komodo_requestedhash = block.hashPrevBlock; komodo_requestedcount = 0; }*/ - LogPrintf("AcceptBlockHeader hashPrevBlock %s no pindexPrev",block.hashPrevBlock.ToString().c_str()); + LogPrintf("AcceptBlockHeader hashPrevBlock %s no pindexPrev\n",block.hashPrevBlock.ToString().c_str()); return(false); } if ( (pindexPrev->nStatus & BLOCK_FAILED_MASK) ) @@ -4073,7 +4073,7 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat if (!ContextualCheckBlockHeader(block, state, pindexPrev)) { //fprintf(stderr,"AcceptBlockHeader ContextualCheckBlockHeader failed\n"); - LogPrintf("AcceptBlockHeader ContextualCheckBlockHeader failed"); + LogPrintf("AcceptBlockHeader ContextualCheckBlockHeader failed\n"); return false; } if (pindex == NULL) @@ -4150,7 +4150,7 @@ bool AcceptBlock(int32_t *futureblockp,CBlock& block, CValidationState& state, C pindex->nStatus |= BLOCK_FAILED_VALID; setDirtyBlockIndex.insert(pindex); } - LogPrintf("AcceptBlock CheckBlock or ContextualCheckBlock error"); + LogPrintf("AcceptBlock CheckBlock or ContextualCheckBlock error\n"); return false; } } From 169abf82c5efe290d7d94e1b056df31b77f86caa Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 24 Apr 2018 22:53:46 +0300 Subject: [PATCH 11/22] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 7878970a7..6d225faa8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4207,7 +4207,7 @@ CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) 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 ) + if ( hash != Params().GetConsensus().hashGenesisBlock ) { miSelf = mapBlockIndex.find(pblock->hashPrevBlock); if ( miSelf == mapBlockIndex.end() ) From b571143669dee70248c952e24b543916d3491830 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Apr 2018 00:01:34 +0300 Subject: [PATCH 12/22] Update komodo_ensure --- src/main.cpp | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6d225faa8..ca7f8517a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4110,12 +4110,8 @@ bool AcceptBlock(int32_t *futureblockp,CBlock& block, CValidationState& state, C } if ( pindex == 0 ) { - *ppindex = pindex = AddToBlockIndex(block); - if ( pindex == 0 ) - { - LogPrintf("AcceptBlock null pindex error\n"); - return false; - } + LogPrintf("AcceptBlock null pindex error\n"); + return false; } //fprintf(stderr,"acceptblockheader passed\n"); // Try to process all requested blocks that we don't have, but only @@ -4198,25 +4194,45 @@ void komodo_currentheight_set(int32_t height); CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) { - CBlockIndex *pindex; + CBlockIndex *pindex=0,*previndex=0; BlockMap::iterator miSelf = mapBlockIndex.find(hash); - if ( miSelf != mapBlockIndex.end() ) + if ( miSelf == mapBlockIndex.end() ) { - if ( (pindex= miSelf->second) == 0 ) // create pindex so first Accept block doesnt fail + pindex = InsertBlockIndex(hash); + LogPrintf("autocreate pindex %s\n",hash.ToString().c_str()); + } + if ( (miSelf= mapBlockIndex.find(hash)) != mapBlockIndex.end() ) + { + if ( 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 ( pindex == 0 ) + pindex = AddToBlockIndex(*pblock); + if ( pindex != 0 ) + { + miSelf->second = pindex; + LogPrintf("Block header %s is already known, but without pindex -> ensured %p\n",hash.ToString().c_str(),miSelf->second); + } else LogPrintf("komodo_ensure unexpected null pindex\n"); } if ( hash != Params().GetConsensus().hashGenesisBlock ) { miSelf = mapBlockIndex.find(pblock->hashPrevBlock); if ( miSelf == mapBlockIndex.end() ) + previndex = InsertBlockIndex(pblock->hashPrevBlock); + if ( (miSelf= mapBlockIndex.find(pblock->hashPrevBlock)) != mapBlockIndex.end() ) { - miSelf->second = InsertBlockIndex(pblock->hashPrevBlock); - fprintf(stderr,"autocreate previndex %s\n",pblock->hashPrevBlock.ToString().c_str()); - } + if ( miSelf->second == 0 ) // create pindex so first Accept block doesnt fail + { + if ( previndex == 0 ) + previndex = InsertBlockIndex(pblock->hashPrevBlock); + if ( previndex != 0 ) + { + miSelf->second = previndex; + LogPrintf("autocreate previndex %s\n",pblock->hashPrevBlock.ToString().c_str()); + } else LogPrintf("komodo_ensure unexpected null previndex\n"); + } + } else LogPrintf("komodo_ensure unexpected null miprev\n"); } - } + } else LogPrintf("komodo_ensure unexpected error creating pindex %s\n",hash.ToString().c_str()); } bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp) From 5d214ce738f1c0d29ed36fdb67c149c8313d1d0e Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Apr 2018 00:24:07 +0300 Subject: [PATCH 13/22] New approach for komodo_ensure --- src/main.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ca7f8517a..ee418a022 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3561,7 +3561,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 } } @@ -4195,25 +4195,25 @@ void komodo_currentheight_set(int32_t height); CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) { CBlockIndex *pindex=0,*previndex=0; + if ( (pindex= mapBlockIndex[hash]) == 0 ) + pindex = InsertBlockIndex(hash); BlockMap::iterator miSelf = mapBlockIndex.find(hash); if ( miSelf == mapBlockIndex.end() ) { - pindex = InsertBlockIndex(hash); - LogPrintf("autocreate pindex %s\n",hash.ToString().c_str()); + LogPrintf("komodo_ensure unexpected missing hash %s\n",hash.ToString().c_str()); + return(0); } - if ( (miSelf= mapBlockIndex.find(hash)) != mapBlockIndex.end() ) + if ( miSelf->second == 0 ) // create pindex so first Accept block doesnt fail { - if ( miSelf->second == 0 ) // create pindex so first Accept block doesnt fail + if ( pindex == 0 ) + pindex = AddToBlockIndex(*pblock); + if ( pindex != 0 ) { - if ( pindex == 0 ) - pindex = AddToBlockIndex(*pblock); - if ( pindex != 0 ) - { - miSelf->second = pindex; - LogPrintf("Block header %s is already known, but without pindex -> ensured %p\n",hash.ToString().c_str(),miSelf->second); - } else LogPrintf("komodo_ensure unexpected null pindex\n"); - } - if ( hash != Params().GetConsensus().hashGenesisBlock ) + miSelf->second = pindex; + LogPrintf("Block header %s is already known, but without pindex -> ensured %p\n",hash.ToString().c_str(),miSelf->second); + } else LogPrintf("komodo_ensure unexpected null pindex\n"); + } + /*if ( hash != Params().GetConsensus().hashGenesisBlock ) { miSelf = mapBlockIndex.find(pblock->hashPrevBlock); if ( miSelf == mapBlockIndex.end() ) @@ -4232,7 +4232,8 @@ CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) } } else LogPrintf("komodo_ensure unexpected null miprev\n"); } - } else LogPrintf("komodo_ensure unexpected error creating pindex %s\n",hash.ToString().c_str()); + }*/ + return(pindex); } bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp) From d1c9d1b49281c201674abe5c6184182787afdf08 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Apr 2018 00:45:38 +0300 Subject: [PATCH 14/22] Test --- src/main.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index ee418a022..81e5ee1c9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4196,7 +4196,13 @@ CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) { CBlockIndex *pindex=0,*previndex=0; if ( (pindex= mapBlockIndex[hash]) == 0 ) - pindex = InsertBlockIndex(hash); + { + pindex = new CBlockIndex(); + if (!pindex) + throw runtime_error("komodo_ensure: new CBlockIndex failed"); + BlockMap::iterator mi = mapBlockIndex.insert(make_pair(hash, pindex)).first; + pindex->phashBlock = &((*mi).first); + } BlockMap::iterator miSelf = mapBlockIndex.find(hash); if ( miSelf == mapBlockIndex.end() ) { @@ -4206,7 +4212,10 @@ CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) if ( miSelf->second == 0 ) // create pindex so first Accept block doesnt fail { if ( pindex == 0 ) + { pindex = AddToBlockIndex(*pblock); + fprintf(stderr,"ensure call addtoblockindex, got %p\n",pindex); + } if ( pindex != 0 ) { miSelf->second = pindex; From 4270df91a344352c54de70e67d1a492aeb6690ca Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Apr 2018 01:01:26 +0300 Subject: [PATCH 15/22] -ensure --- src/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.cpp b/src/main.cpp index 81e5ee1c9..3d1b289bf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4274,6 +4274,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo } // Store to disk CBlockIndex *pindex = NULL; + if ( 0 ) { // 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); From 55c01f138dd9309bbba6039baefdcab5bc6d7088 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Apr 2018 01:14:19 +0300 Subject: [PATCH 16/22] Audo addtoblockindex --- src/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 3d1b289bf..cdafada5e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4015,7 +4015,8 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat if (miSelf != mapBlockIndex.end()) { // Block header is already known. - pindex = miSelf->second; + if ( (pindex= miSelf->second) == 0 ) + pindex = AddToBlockIndex(block); if (ppindex) *ppindex = pindex; if ( pindex != 0 && pindex->nStatus & BLOCK_FAILED_MASK ) @@ -4026,6 +4027,7 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat memset(&komodo_requestedhash,0,sizeof(komodo_requestedhash)); komodo_requestedcount = 0; }*/ + //if ( pindex == 0 ) // fprintf(stderr,"accepthdr %s already known but no pindex\n",hash.ToString().c_str()); return true; From 163fbf95a1768ae93867204bc8b11dd48dc1f324 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Apr 2018 01:29:00 +0300 Subject: [PATCH 17/22] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index cdafada5e..a6d2fee8f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4276,7 +4276,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo } // Store to disk CBlockIndex *pindex = NULL; - if ( 0 ) + if ( 1 ) { // 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); From 4ac854ea908bb5446b0713df1e8467f527d08bab Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Apr 2018 01:37:43 +0300 Subject: [PATCH 18/22] Simplify ensure --- src/main.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index a6d2fee8f..250a72f72 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4195,6 +4195,32 @@ 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 + { + 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 != Params.GetConsensus().hashGenesisBlock ) + { + miSelf = mapBlockIndex.find(pblock->hashPrevBlock); + if ( miSelf != mapBlockIndex.end() ) + { + if ( miSelf->second == 0 ) + { + miSelf->second = InsertBlockIndex(pblock->hashPrevBlock); + fprintf(stderr,"autocreate previndex %s\n",pblock->hashPrevBlock.ToString().c_str()); + } + } + } + } +} + +CBlockIndex *oldkomodo_ensure(CBlock *pblock,uint256 hash) { CBlockIndex *pindex=0,*previndex=0; if ( (pindex= mapBlockIndex[hash]) == 0 ) From 85328429bb72a4ec378bf592971046dddbd8b075 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Apr 2018 01:41:44 +0300 Subject: [PATCH 19/22] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 250a72f72..f0a2c1e0e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4205,7 +4205,7 @@ CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) 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 != Params.GetConsensus().hashGenesisBlock ) + if ( hash != Params().GetConsensus().hashGenesisBlock ) { miSelf = mapBlockIndex.find(pblock->hashPrevBlock); if ( miSelf != mapBlockIndex.end() ) From 58ef04c7dc617e2740fff57da05ea5b934a273c9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Apr 2018 02:28:14 +0300 Subject: [PATCH 20/22] Set mi->second --- src/main.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f0a2c1e0e..6d2198d4e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3588,6 +3588,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) setDirtyBlockIndex.insert(pindexNew); //fprintf(stderr,"added to block index %s %p\n",hash.ToString().c_str(),pindexNew); + it->second = pindexNew; return pindexNew; } @@ -4016,7 +4017,7 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat { // Block header is already known. if ( (pindex= miSelf->second) == 0 ) - pindex = AddToBlockIndex(block); + miSelf->second = pindex = AddToBlockIndex(block); if (ppindex) *ppindex = pindex; if ( pindex != 0 && pindex->nStatus & BLOCK_FAILED_MASK ) @@ -4080,8 +4081,11 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat } if (pindex == NULL) { - if ( (pindex= AddToBlockIndex(block)) == 0 ) + if ( (pindex= AddToBlockIndex(block)) != 0 ) { + miSelf = mapBlockIndex.find(hash); + if (miSelf != mapBlockIndex.end()) + miSelf->second = pindex; //fprintf(stderr,"AcceptBlockHeader couldnt add to block index\n"); } } @@ -4196,7 +4200,7 @@ void komodo_currentheight_set(int32_t height); CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) { - CBlockIndex *pindex; + CBlockIndex *pindex = 0; BlockMap::iterator miSelf = mapBlockIndex.find(hash); if ( miSelf != mapBlockIndex.end() ) { @@ -4205,7 +4209,7 @@ CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) 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 != Params().GetConsensus().hashGenesisBlock ) + /*if ( hash != Params().GetConsensus().hashGenesisBlock ) { miSelf = mapBlockIndex.find(pblock->hashPrevBlock); if ( miSelf != mapBlockIndex.end() ) @@ -4216,8 +4220,9 @@ CBlockIndex *komodo_ensure(CBlock *pblock,uint256 hash) fprintf(stderr,"autocreate previndex %s\n",pblock->hashPrevBlock.ToString().c_str()); } } - } + }*/ } + return(pindex); } CBlockIndex *oldkomodo_ensure(CBlock *pblock,uint256 hash) From 9cdde30c2c3cbc0a20debf9e7fba51f0150a98ef Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Apr 2018 02:32:14 +0300 Subject: [PATCH 21/22] Test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 6d2198d4e..886d9eef2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3588,7 +3588,7 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) setDirtyBlockIndex.insert(pindexNew); //fprintf(stderr,"added to block index %s %p\n",hash.ToString().c_str(),pindexNew); - it->second = pindexNew; + mi->second = pindexNew; return pindexNew; } From f190f354b0ed0d4e36d9a8eebfc9b9bcbbc2f9d3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 25 Apr 2018 02:51:17 +0300 Subject: [PATCH 22/22] -print --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 886d9eef2..515833651 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3561,7 +3561,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 } }