From 7102d50a477eccca06ea2166a6ae8a9e87cd76e3 Mon Sep 17 00:00:00 2001 From: Duke Date: Sat, 22 Apr 2023 15:54:13 -0700 Subject: [PATCH 1/4] Try to prevent #266 We seem to have a bug that we don't find a peer when looking for a peer in peers.dat . Instead of doing an assertion and crashing the node, just keep iterating in the loop. The code which computes nId may have an off-by-one error, such that it looks up an incorrect bucket position and doesn't find a valid nId. --- src/addrman.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index 60f27347a..535151c2a 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -499,7 +499,12 @@ CAddrInfo CAddrMan::Select_(bool newOnly) MilliSleep(kRetrySleepInterval); } int nId = vvTried[nKBucket][nKBucketPos]; - assert(mapInfo.count(nId) == 1); + // assert(mapInfo.count(nId) == 1); + if(mapInfo.count(nId) != 1) { + fprintf(stderr,"%s: Could not find tried node with nId=%d=vvTried[%d][%d], mapInfo.count(%d)=%d\n", __func__, nId, nKBucket, nKBuckedPos, nId, mapInfo.count(nId) ); + continue; + } + CAddrInfo& info = mapInfo[nId]; if (info.IsReachableNetwork()) { //deprioritize unreachable networks @@ -534,7 +539,12 @@ CAddrInfo CAddrMan::Select_(bool newOnly) MilliSleep(kRetrySleepInterval); } int nId = vvNew[nUBucket][nUBucketPos]; - assert(mapInfo.count(nId) == 1); + + if(mapInfo.count(nId) != 1) { + fprintf(stderr,"%s: Could not find new node with nId=%d=vvNew[%d][%d], mapInfo.count(%d)=%d\n", __func__, nId, nUBucket, nUBuckedPos, nId, mapInfo.count(nId) ); + continue; + } + // assert(mapInfo.count(nId) == 1); CAddrInfo& info = mapInfo[nId]; if (info.IsReachableNetwork()) { //deprioritize unreachable networks From fbda7b8dec0d69c9807ecd60643b8e87fd79f7a7 Mon Sep 17 00:00:00 2001 From: Duke Date: Sat, 22 Apr 2023 19:28:24 -0400 Subject: [PATCH 2/4] Fix compile issues --- src/addrman.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index 535151c2a..15ced2f61 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -501,7 +501,7 @@ CAddrInfo CAddrMan::Select_(bool newOnly) int nId = vvTried[nKBucket][nKBucketPos]; // assert(mapInfo.count(nId) == 1); if(mapInfo.count(nId) != 1) { - fprintf(stderr,"%s: Could not find tried node with nId=%d=vvTried[%d][%d], mapInfo.count(%d)=%d\n", __func__, nId, nKBucket, nKBuckedPos, nId, mapInfo.count(nId) ); + fprintf(stderr,"%s: Could not find tried node with nId=%d=vvTried[%d][%d], mapInfo.count(%d)=%lu\n", __func__, nId, nKBucket, nKBucketPos, nId, mapInfo.count(nId) ); continue; } @@ -541,7 +541,7 @@ CAddrInfo CAddrMan::Select_(bool newOnly) int nId = vvNew[nUBucket][nUBucketPos]; if(mapInfo.count(nId) != 1) { - fprintf(stderr,"%s: Could not find new node with nId=%d=vvNew[%d][%d], mapInfo.count(%d)=%d\n", __func__, nId, nUBucket, nUBuckedPos, nId, mapInfo.count(nId) ); + fprintf(stderr,"%s: Could not find new node with nId=%d=vvNew[%d][%d], mapInfo.count(%d)=%lu\n", __func__, nId, nUBucket, nUBucketPos, nId, mapInfo.count(nId) ); continue; } // assert(mapInfo.count(nId) == 1); From d840a0ec62dd068b84be8fe197e764ae197b2e65 Mon Sep 17 00:00:00 2001 From: Duke Date: Sun, 23 Apr 2023 06:56:19 -0700 Subject: [PATCH 3/4] Only log nbits in ContextualCheckBlockHeader if -debug --- src/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 20ea1112f..6ac591353 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5114,7 +5114,9 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta // changing at run-time, from Consensus::Params being a const struct unsigned int nNextWork = GetNextWorkRequired(pindexPrev, &block, consensusParams); - LogPrintf("%s: nbits ,%d,%lu,%lu,%d\n",__func__, nHeight, nNextWork, block.nBits, nNextWork - block.nBits ); + if (fDebug) { + LogPrintf("%s: nbits ,%d,%lu,%lu,%d\n",__func__, nHeight, nNextWork, block.nBits, nNextWork - block.nBits ); + } if (block.nBits != nNextWork) { // Enforce correct nbits at DAA fork height, before that, ignore if (nHeight > daaForkHeight) { From 519743f704769ec2013c87324e3b943fc25a0f32 Mon Sep 17 00:00:00 2001 From: Duke Date: Sun, 23 Apr 2023 07:03:05 -0700 Subject: [PATCH 4/4] Only log to stderr in CalculateNextWorkRequired and GetNextWorkRequired if -debug --- src/pow.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/pow.cpp b/src/pow.cpp index 260785860..9ac8ef80e 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -503,8 +503,9 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead nbits = bnTarget.GetCompact(); nbits = (nbits & 0xfffffffc) | zawyflag; } - //if(fDebug) + if(fDebug) { fprintf(stderr,"%s: nbits=%d\n", __func__, nbits); + } return(nbits); } @@ -537,11 +538,15 @@ unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, if ( ASSETCHAINS_ADAPTIVEPOW <= 0 ) { if (nActualTimespan < params.MinActualTimespan()) { - fprintf(stderr,"%s: Adjusting nActualTimespan up to min value %li\n", __func__, params.MinActualTimespan() ); + if(fDebug) { + fprintf(stderr,"%s: Adjusting nActualTimespan up to min value %li\n", __func__, params.MinActualTimespan() ); + } nActualTimespan = params.MinActualTimespan(); } if (nActualTimespan > params.MaxActualTimespan()) { - fprintf(stderr,"%s: Adjusting nActualTimespan down to max value %li\n", __func__, params.MaxActualTimespan() ); + if(fDebug) { + fprintf(stderr,"%s: Adjusting nActualTimespan down to max value %li\n", __func__, params.MaxActualTimespan() ); + } nActualTimespan = params.MaxActualTimespan(); } }