Rand selection of peer

This commit is contained in:
jl777
2019-07-08 00:34:55 -11:00
parent e80685bc2d
commit d43ef615ff

View File

@@ -97,13 +97,12 @@ void komodo_nSPVresp(CNode *pfrom,std::vector<uint8_t> response) // received a r
CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind)
{ {
int32_t iter,num; CNode *firstpnode = 0; uint32_t timestamp = (uint32_t)time(NULL); int32_t n,flag = 0; CNode *pnodes[64]; uint32_t timestamp = (uint32_t)time(NULL);
while ( pnode == 0 ) if ( pnode == 0 )
{ {
memset(pnodes,0,sizeof(pnodes));
LOCK(cs_vNodes); LOCK(cs_vNodes);
num = 0; n = 0;
for (iter=0; iter<2; iter++)
{
BOOST_FOREACH(CNode *ptr,vNodes) BOOST_FOREACH(CNode *ptr,vNodes)
{ {
if ( ptr->prevtimes[ind] > timestamp ) if ( ptr->prevtimes[ind] > timestamp )
@@ -112,26 +111,15 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind)
continue; continue;
if ( (ptr->nServices & mask) == mask && timestamp > ptr->prevtimes[ind] ) if ( (ptr->nServices & mask) == mask && timestamp > ptr->prevtimes[ind] )
{ {
if ( firstpnode == 0 ) flag = 1;
firstpnode = ptr; pnodes[n++] = ptr;
if ( iter != 0 ) if ( n == sizeof(pnodes)/sizeof(*pnodes) )
{
if ( num == 1 || (rand() % num) == 0 )
{
pnode = ptr;
break; break;
}
} else num++;
} // else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind); } // else fprintf(stderr,"nServices %llx vs mask %llx, t%u vs %u, ind.%d\n",(long long)ptr->nServices,(long long)mask,timestamp,ptr->prevtimes[ind],ind);
} }
if ( iter == 0 && num == 0 ) if ( n > 0 )
break; pnode = pnodes[rand() % n];
if ( pnode == 0 ) } else flag = 1;
pnode = firstpnode;
}
if ( pnode != 0 && time(NULL) > timestamp+2 )
break;
}
if ( pnode != 0 ) if ( pnode != 0 )
{ {
std::vector<uint8_t> request; std::vector<uint8_t> request;