From 58b0fc23ace899f702bf5e9bcabf81ca186d0c66 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 11 Nov 2018 05:06:36 -1100 Subject: [PATCH] Guard longest chain and GetNodeStateStats --- src/main.cpp | 33 ++++++++++++++-------- src/rpcnet.cpp | 76 ++++++++++++++++++++++++++++---------------------- 2 files changed, 64 insertions(+), 45 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 64d48d7ae..bc371e4eb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -532,17 +532,28 @@ namespace { } // anon namespace -bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) { - LOCK(cs_main); - CNodeState *state = State(nodeid); - if (state == NULL) - return false; - stats.nMisbehavior = state->nMisbehavior; - stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1; - stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1; - BOOST_FOREACH(const QueuedBlock& queue, state->vBlocksInFlight) { - if (queue.pindex) - stats.vHeightInFlight.push_back(queue.pindex->nHeight); +bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) +{ + static int32_t depth; + if ( depth < 0 ) + depth = 0; + if ( depth == 0 ) + { + LOCK(cs_main); + CNodeState *state = State(nodeid); + if (state == NULL) + { + depth--; + return false; + } + stats.nMisbehavior = state->nMisbehavior; + stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1; + stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1; + BOOST_FOREACH(const QueuedBlock& queue, state->vBlocksInFlight) { + if (queue.pindex) + stats.vHeightInFlight.push_back(queue.pindex->nHeight); + } + depth--; } return true; } diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp index cf76ac9d3..3dcf52b59 100644 --- a/src/rpcnet.cpp +++ b/src/rpcnet.cpp @@ -168,43 +168,51 @@ UniValue getpeerinfo(const UniValue& params, bool fHelp) int32_t KOMODO_LONGESTCHAIN; int32_t komodo_longestchain() { + static int32_t depth; int32_t ht,n=0,num=0,maxheight=0,height = 0; - vector vstats; + if ( depth < 0 ) + depth = 0; + if ( depth == 0 ) { - //LOCK(cs_main); - CopyNodeStats(vstats); + depth++; + vector vstats; + { + //LOCK(cs_main); + CopyNodeStats(vstats); + } + BOOST_FOREACH(const CNodeStats& stats, vstats) + { + //fprintf(stderr,"komodo_longestchain iter.%d\n",n); + CNodeStateStats statestats; + bool fStateStats = GetNodeStateStats(stats.nodeid,statestats); + if ( statestats.nSyncHeight < 0 ) + continue; + ht = 0; + if ( stats.nStartingHeight > ht ) + ht = stats.nStartingHeight; + if ( statestats.nSyncHeight > ht ) + ht = statestats.nSyncHeight; + if ( statestats.nCommonHeight > ht ) + ht = statestats.nCommonHeight; + if ( maxheight == 0 || ht > maxheight*1.01 ) + maxheight = ht, num = 1; + else if ( ht > maxheight*0.99 ) + num++; + if ( ht > height ) + height = ht; + } + depth--; + if ( num > (n >> 1) ) + { + extern char ASSETCHAINS_SYMBOL[]; + if ( 0 && height != KOMODO_LONGESTCHAIN ) + fprintf(stderr,"set %s KOMODO_LONGESTCHAIN <- %d\n",ASSETCHAINS_SYMBOL,height); + KOMODO_LONGESTCHAIN = height; + return(height); + } + KOMODO_LONGESTCHAIN = 0; } - BOOST_FOREACH(const CNodeStats& stats, vstats) - { - //fprintf(stderr,"komodo_longestchain iter.%d\n",n); - CNodeStateStats statestats; - bool fStateStats = GetNodeStateStats(stats.nodeid,statestats); - if ( statestats.nSyncHeight < 0 ) - continue; - ht = 0; - if ( stats.nStartingHeight > ht ) - ht = stats.nStartingHeight; - if ( statestats.nSyncHeight > ht ) - ht = statestats.nSyncHeight; - if ( statestats.nCommonHeight > ht ) - ht = statestats.nCommonHeight; - if ( maxheight == 0 || ht > maxheight*1.01 ) - maxheight = ht, num = 1; - else if ( ht > maxheight*0.99 ) - num++; - if ( ht > height ) - height = ht; - } - if ( num > (n >> 1) ) - { - extern char ASSETCHAINS_SYMBOL[]; - if ( 0 && height != KOMODO_LONGESTCHAIN ) - fprintf(stderr,"set %s KOMODO_LONGESTCHAIN <- %d\n",ASSETCHAINS_SYMBOL,height); - KOMODO_LONGESTCHAIN = height; - return(height); - } - KOMODO_LONGESTCHAIN = 0; - return(0); + return(KOMODO_LONGESTCHAIN); } UniValue addnode(const UniValue& params, bool fHelp)