Guard longest chain and GetNodeStateStats

This commit is contained in:
jl777
2018-11-11 05:06:36 -11:00
parent 34b1a40307
commit 58b0fc23ac
2 changed files with 64 additions and 45 deletions

View File

@@ -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<CNodeStats> vstats;
if ( depth < 0 )
depth = 0;
if ( depth == 0 )
{
//LOCK(cs_main);
CopyNodeStats(vstats);
depth++;
vector<CNodeStats> 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)