You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2314 lines
102 KiB

9 years ago
/******************************************************************************
* Copyright © 2014-2018 The SuperNET Developers. *
9 years ago
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
#include "iguana777.h"
// peer context, ie massively multithreaded -> bundlesQ
9 years ago
static int32_t numDuplicates,numAfteremit;
static int64_t sizeDuplicates,sizeAfteremit;
9 years ago
struct iguana_bundlereq *iguana_bundlereq(struct iguana_info *coin,struct iguana_peer *addr,int32_t type,uint8_t *data,int32_t datalen)
9 years ago
{
9 years ago
struct iguana_bundlereq *req; int32_t allocsize;
if ( data == 0 )
datalen = 0;
8 years ago
allocsize = (uint32_t)sizeof(*req) + datalen + 64;
9 years ago
req = mycalloc(type,1,allocsize);
req->allocsize = allocsize;
req->datalen = datalen;
req->addr = addr;
req->coin = coin;
req->type = type;
9 years ago
if ( data != 0 && datalen > 0 )
memcpy(req->serializeddata,data,datalen);
9 years ago
return(req);
}
9 years ago
int32_t iguana_speculativesearch(struct iguana_info *coin,struct iguana_block **blockptrp,bits256 hash2)
{
int32_t i,j; struct iguana_bundle *bp;
if ( blockptrp != 0 )
*blockptrp = 0;
for (i=0; i<coin->bundlescount; i++)
{
if ( (bp= coin->bundles[i]) != 0 && bp->speculative != 0 )
{
for (j=0; j<bp->n&&j<bp->numspec; j++)
if ( bits256_cmp(hash2,bp->speculative[j]) == 0 )
{
if ( blockptrp != 0 )
*blockptrp = bp->blocks[j];
if ( bp->speculativecache[j] != 0 )
return(1);
else return(-1);
}
}
}
return(0);
}
9 years ago
int32_t iguana_sendblockreqPT(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2,int32_t iamthreadsafe)
9 years ago
{
9 years ago
static bits256 lastreq,lastreq2;
8 years ago
int32_t len,j,n; struct iguana_bundle *checkbp; uint8_t serialized[sizeof(struct iguana_msghdr) + sizeof(uint32_t)*32 + sizeof(bits256)]; struct iguana_block *block=0;
9 years ago
char hexstr[65]; init_hexbytes_noT(hexstr,hash2.bytes,sizeof(hash2));
8 years ago
if ( addr == 0 && coin->peers != 0 && (n= coin->peers->numranked) > 0 )
addr = coin->peers->ranked[rand() % n];
8 years ago
if ( addr == 0 || addr->pendblocks > coin->MAXPENDINGREQUESTS )
8 years ago
return(0);
8 years ago
if ( addr->usock < 0 )
return(0);
8 years ago
if ( memcmp(lastreq.bytes,hash2.bytes,sizeof(hash2)) == 0 || memcmp(lastreq2.bytes,hash2.bytes,sizeof(hash2)) == 0 )
9 years ago
{
8 years ago
//printf("duplicate req %s or null addr.%p\n",bits256_str(hexstr,hash2),addr);
8 years ago
if ( iamthreadsafe == 0 && (rand() % 10) != 0 )
9 years ago
return(0);
9 years ago
}
9 years ago
checkbp = 0, j = -2;
if ( (checkbp= iguana_bundlefind(coin,&checkbp,&j,hash2)) != 0 && j >= 0 && j < checkbp->n && checkbp != coin->current )
9 years ago
{
8 years ago
if ( checkbp->utxofinish != 0 || ((block= checkbp->blocks[j]) != 0 && block->txvalid != 0 && block->mainchain != 0 && block->valid != 0 && block->bundlei != 0 && coin->RTheight == 0) )
8 years ago
return(0);
9 years ago
}
8 years ago
if ( checkbp != bp || j != bundlei )
bp = 0, bundlei = -1;
8 years ago
if ( (0) && coin->enableCACHE != 0 && iguana_speculativesearch(coin,&block,hash2) != 0 )
9 years ago
{
9 years ago
if ( block != 0 && block->hdrsi != 0 && block->bundlei != 0 )
{
8 years ago
//printf("found valid [%d:%d] in blockreqPT txvalid.%d\n",block!=0?block->hdrsi:-1,block!=0?block->bundlei:-1,block->txvalid);
8 years ago
if ( block->mainchain != 0 && block->valid != 0 && block->txvalid != 0 )
{
/*if ( (bp= coin->bundles[block->bundlei]) != 0 )
{
bp->hashes[block->bundlei] = block->RO.hash2;
bp->blocks[block->bundlei] = block;
}*/
9 years ago
return(0);
8 years ago
}
9 years ago
}
9 years ago
}
9 years ago
if ( addr->msgcounts.verack == 0 )
{
8 years ago
if ( (rand() % 10000) == 0 )
8 years ago
printf("iguana_sendblockreq (%s) addrind.%d hasn't verack'ed yet\n",addr->ipaddr,addr->addrind);
8 years ago
//iguana_send_version(coin,addr,coin->myservices);
8 years ago
//return(-1);
9 years ago
}
9 years ago
lastreq2 = lastreq;
lastreq = hash2;
9 years ago
if ( (len= iguana_getdata(coin,serialized,MSG_BLOCK,&hash2,1)) > 0 )
9 years ago
{
coin->numreqsent++;
addr->pendblocks++;
addr->pendtime = (uint32_t)time(NULL);
9 years ago
if ( bp != 0 && bundlei >= 0 && bundlei < bp->n )
8 years ago
{
8 years ago
if ( coin->RTheight == 0 && bp != coin->current && bp->issued[bundlei] > 1 && addr->pendtime < bp->issued[bundlei]+7 )
{
//printf("SKIP.(%s) [%d:%d] %s n.%d\n",bits256_str(hexstr,hash2),bundlei,bp!=0?bp->hdrsi:-1,addr->ipaddr,addr->pendblocks);
8 years ago
return(0);
}
9 years ago
bp->issued[bundlei] = addr->pendtime;
8 years ago
}
iguana_send(coin,addr,serialized,len);
9 years ago
if ( block != 0 )
block->issued = addr->pendtime;
8 years ago
if ( (0) && coin->current == bp )
8 years ago
printf("REQ.(%s) [%d:%d] %s n.%d\n",bits256_str(hexstr,hash2),bundlei,bp!=0?bp->hdrsi:-1,addr->ipaddr,addr->pendblocks);
9 years ago
} else printf("MSG_BLOCK null datalen.%d\n",len);
return(len);
}
int32_t iguana_sendtxidreq(struct iguana_info *coin,struct iguana_peer *addr,bits256 hash2)
{
uint8_t serialized[sizeof(struct iguana_msghdr) + sizeof(uint32_t)*32 + sizeof(bits256)];
9 years ago
int32_t len,i,r,j; //char hexstr[65]; init_hexbytes_noT(hexstr,hash2.bytes,sizeof(hash2));
if ( (len= iguana_getdata(coin,serialized,MSG_TX,&hash2,1)) > 0 )
9 years ago
{
9 years ago
if ( addr == 0 && coin->peers != 0 )
9 years ago
{
r = rand();
for (i=0; i<coin->MAXPEERS; i++)
{
j = (i + r) % coin->MAXPEERS;
9 years ago
addr = &coin->peers->active[j];
if ( coin->peers->active[j].usock >= 0 && coin->peers->active[j].dead == 0 )
9 years ago
{
iguana_send(coin,addr,serialized,len);
break;
}
}
} else iguana_send(coin,addr,serialized,len);
} else printf("MSG_TX null datalen.%d\n",len);
printf("send MSG_TX.%d\n",len);
return(len);
}
int32_t iguana_txidreq(struct iguana_info *coin,char **retstrp,bits256 txid)
{
int32_t i;
while ( coin->numreqtxids >= sizeof(coin->reqtxids)/sizeof(*coin->reqtxids) )
{
printf("txidreq full, wait\n");
sleep(1);
}
char str[65]; printf("txidreq.%s\n",bits256_str(str,txid));
coin->reqtxids[coin->numreqtxids++] = txid;
9 years ago
if ( coin->peers != 0 )
{
9 years ago
for (i=0; i<IGUANA_MAXPEERS; i++)
9 years ago
if ( coin->peers->active[i].usock >= 0 )
iguana_sendtxidreq(coin,coin->peers->ranked[i],txid);
}
9 years ago
return(0);
}
void iguana_gotunconfirmedM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msgtx *tx,uint8_t *data,int32_t datalen)
{
struct iguana_bundlereq *req;
char str[65]; printf("%s unconfirmed.%s\n",addr->ipaddr,bits256_str(str,tx->txid));
9 years ago
req = iguana_bundlereq(coin,addr,'U',data,datalen);
9 years ago
req->datalen = datalen;
req->txid = tx->txid;
memcpy(req->serializeddata,data,datalen);
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
9 years ago
}
9 years ago
#ifdef later
9 years ago
struct iguana_txblock *iguana_peertxdata(struct iguana_info *coin,int32_t *bundleip,char *fname,struct OS_memspace *mem,uint32_t ipbits,bits256 hash2)
{
int32_t bundlei,datalen,checki,hdrsi,fpos; char str[65],str2[65]; FILE *fp;
9 years ago
bits256 checkhash2; struct iguana_txblock *txdata = 0; static const bits256 zero;
9 years ago
if ( (bundlei= iguana_peerfname(coin,&hdrsi,GLOBAL_TMPDIR,fname,ipbits,hash2,zero,1)) >= 0 )
9 years ago
//if ( (bundlei= iguana_peerfname(coin,&hdrsi,fname,ipbits,hash2)) >= 0 )
{
if ( (fp= fopen(fname,"rb")) != 0 )
{
fseek(fp,bundlei * sizeof(bundlei),SEEK_SET);
fread(&fpos,1,sizeof(fpos),fp);
fseek(fp,fpos,SEEK_SET);
fread(&checki,1,sizeof(checki),fp);
if ( ftell(fp)-sizeof(checki) == fpos && bundlei == checki )
{
fread(&checkhash2,1,sizeof(checkhash2),fp);
if ( memcmp(hash2.bytes,checkhash2.bytes,sizeof(hash2)) == 0 )
{
fread(&datalen,1,sizeof(datalen),fp);
if ( datalen < (mem->totalsize - mem->used - 4) )
{
if ( (txdata= iguana_memalloc(mem,datalen,0)) != 0 )
{
fread(txdata,1,datalen,fp);
if ( txdata->datalen != datalen || txdata->block.bundlei != bundlei )
{
printf("%s peertxdata txdata->datalen.%d != %d bundlei.%d vs %d\n",bits256_str(str,txdata->block.RO.hash2),txdata->datalen,datalen,txdata->block.bundlei,bundlei);
9 years ago
//getchar();
9 years ago
txdata = 0;
iguana_memreset(mem);
} //else printf("SUCCESS txdata.%s bundlei.%d fpos.%d T.%d U.%d S.%d P.%d\n",bits256_str(str,txdata->block.hash2),bundlei,fpos,txdata->numtxids,txdata->numunspents,txdata->numspends,txdata->numpkinds);
} else printf("peertxdata error allocating txdata\n");
} else printf("mismatch peertxdata datalen %d vs %ld totalsize %ld\n",datalen,mem->totalsize - mem->used - 4,(long)mem->totalsize);
} else printf("peertxdata hash mismatch %s != %s\n",bits256_str(str,hash2),bits256_str(str2,checkhash2));
} else printf("peertxdata bundlei.%d != checki.%d, fpos.%d ftell.%ld\n",bundlei,checki,fpos,ftell(fp));
fclose(fp);
} else printf("cant find file.(%s)\n",fname);
} //else printf("bundlei.%d\n",bundlei);
*bundleip = bundlei;
return(txdata);
}
9 years ago
#endif
9 years ago
9 years ago
int32_t iguana_speculativefind(struct iguana_info *coin,struct iguana_bundle *bp,struct iguana_block *block,uint8_t *data,int32_t recvlen)
{
9 years ago
int32_t i,j,numcached,cachelen=0; uint8_t *tmp; char str[65];
9 years ago
if ( coin->enableCACHE == 0 || recvlen < 0 )
9 years ago
return(-1);
9 years ago
if ( recvlen < 0 || recvlen > IGUANA_MAXPACKETSIZE )
{
printf("iguana_speculativefind: illegal recvlen.%d\n",recvlen);
return(-1);
}
for (i=0; i<bp->n; i++)
9 years ago
{
if ( bits256_cmp(bp->speculative[i],block->RO.hash2) == 0 )
{
if ( (tmp= bp->speculativecache[i]) != 0 )
{
9 years ago
memcpy(&cachelen,tmp,sizeof(cachelen));
9 years ago
if ( cachelen < 0 || cachelen > IGUANA_MAXPACKETSIZE )
{
printf("illegal cachelen.%d %s [%d:%d] %p\n",cachelen,coin->symbol,bp->hdrsi,i,tmp);
bp->speculativecache[i] = 0;
continue;
}
9 years ago
if ( memcmp(&recvlen,tmp,sizeof(recvlen)) != 0 || memcmp(&tmp[sizeof(recvlen)],data,recvlen) != 0 )
9 years ago
{
//printf("cachedata ERROR [%d:%d] already has recvlen.%d vs %d for %s\n",bp->hdrsi,i,recvlen,cachelen,bits256_str(str,block->RO.hash2));
}
9 years ago
return(0);
}
9 years ago
bp->speculativecache[i] = calloc(1,recvlen + sizeof(recvlen));
9 years ago
memcpy(bp->speculativecache[i],&recvlen,sizeof(recvlen));
memcpy(&bp->speculativecache[i][sizeof(recvlen)],data,recvlen);
for (j=numcached=0; j<bp->n; j++)
if ( bp->speculativecache[j] != 0 )
numcached++;
8 years ago
if ( (0) && bp == coin->current )
9 years ago
printf("cache %s [%d:%d] h.%d s.%d c.%d -> %d\n",bits256_str(str,block->RO.hash2),bp->hdrsi,i,bp->numhashes,bp->numsaved,bp->numcached,numcached);
9 years ago
return(i);
}
}
9 years ago
return(-1);
9 years ago
}
int8_t iguana_blockstatus(struct iguana_info *coin,struct iguana_block *block)
{
int32_t status = 0;
if ( block->RO.recvlen != 0 )
status |= 1;
if ( block->fpipbits != 0 )
status |= 2;
if ( block->fpos >= 0 )
status |= 4;
if ( bits256_nonz(block->RO.prev_block) != 0 )
status |= 8;
if ( block->queued != 0 )
status |= 0xc0; // force negative, 0x80 can be +128
return(status);
}
9 years ago
void iguana_bundletime(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,struct iguana_block *block,int32_t duplicateflag)
{
uint32_t starttime; int32_t duration;
9 years ago
if ( bp != 0 && bundlei >= 0 && bundlei < bp->n && block != 0 )
9 years ago
{
starttime = block->issued;
9 years ago
if ( starttime == 0 || bp->issued[bundlei] > block->issued )
9 years ago
starttime = bp->issued[bundlei];
if ( starttime != 0 )
{
duration = (uint32_t)time(NULL) - starttime;
if ( duplicateflag != 0 )
{
bp->duplicatedurations += duration;
bp->duplicatescount++;
}
else
{
bp->totaldurations += duration;
bp->durationscount++;
}
8 years ago
if ( (block != 0 || (block= bp->blocks[bundlei]) != 0) && block->lag == 0 )
9 years ago
block->lag = duration;
9 years ago
}
}
}
8 years ago
void iguana_oldgotblockM(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_peer *addr,struct iguana_txblock *origtxdata,struct iguana_msgtx *txarray,struct iguana_msghdr *H,uint8_t *data,int32_t recvlen,int32_t fromcache)
9 years ago
{
8 years ago
struct iguana_bundlereq *req; struct iguana_txblock *txdata = 0; int32_t valid,speculative=0,i,j,bundlei,copyflag,numtx; struct iguana_block *block; struct iguana_bundle *bp; uint32_t now; char str[65];
9 years ago
if ( recvlen < 0 || recvlen > IGUANA_MAXPACKETSIZE )
{
printf("iguana_getblockM: illegal recvlen.%d\n",recvlen);
return;
}
8 years ago
if ( (0) )
9 years ago
{
for (i=0; i<txdata->space[0]; i++)
if ( txdata->space[i] != 0 )
break;
if ( i != txdata->space[0] )
{
for (i=0; i<txdata->space[0]; i++)
printf("%02x ",txdata->space[i]);
printf("extra\n");
}
}
if ( coin->numreqtxids > 0 )
{
for (i=0; i<origtxdata->zblock.RO.txn_count; i++)
9 years ago
{
for (j=0; j<coin->numreqtxids; j++)
{
if ( memcmp(coin->reqtxids[j].bytes,txarray[i].txid.bytes,sizeof(bits256)) == 0 )
{
char str[65]; printf("i.%d j.%d found txid.%s\n",i,j,bits256_str(str,coin->reqtxids[j]));
}
}
}
}
9 years ago
origtxdata->zblock.RO.allocsize = sizeof(origtxdata->zblock);
8 years ago
if ( iguana_blockvalidate(myinfo,coin,&valid,(struct iguana_block *)&origtxdata->zblock,1) < 0 )
9 years ago
{
printf("got block that doesnt validate? %s\n",bits256_str(str,origtxdata->zblock.RO.hash2));
9 years ago
return;
8 years ago
} else if ( (0) && coin->enableCACHE != 0 )
8 years ago
printf("c.%d v.(%s) %s n%d\n",coin->enableCACHE,bits256_str(str,origtxdata->zblock.RO.hash2),addr->ipaddr,addr->pendblocks);
origtxdata->zblock.txvalid = 1;
8 years ago
if ( fromcache == 0 && coin->virtualchain == 0 && addr != 0 && addr != &coin->internaladdr )
9 years ago
{
9 years ago
static uint64_t received[IGUANA_MAXPEERS],count[IGUANA_MAXPEERS],lastcount,lastreceived,last;
9 years ago
received[addr->addrind] += recvlen;
count[addr->addrind]++;
9 years ago
now = (uint32_t)time(NULL);
8 years ago
if ( ((rand() % 10000) == 0 && now > last+60) || now > last+600 )
9 years ago
{
9 years ago
int64_t sum2 = 0,sum = 0,diffr,diff; double bw = 0.;
9 years ago
for (i=0; i<sizeof(received)/sizeof(*received); i++)
9 years ago
sum += received[i], sum2 += count[i];
9 years ago
diffr = (sum - lastreceived), diff = (sum2 - lastcount);
//printf("diffr %d diff %d, last %d/%d\n",(int32_t)diffr,(int32_t)diff,(int32_t)lastreceived,(int32_t)lastcount);
lastreceived = sum, lastcount = sum2;
if ( diff != 0 )
{
bw = ((double)diffr / (now - last + 1));
if ( bw > coin->maxbandwidth )
coin->maxbandwidth = bw;
}
9 years ago
dxblend(&coin->bandwidth,bw,.9);
char str[65],str2[65],str3[65],str4[65]; printf("%s BLOCKS.%llu RECV %s ave %.1f | dup.%d %s after.%d %s %s/sec %.2f%% %s\n",coin->symbol,(long long)sum2,mbstr(str,sum),(double)sum/(sum2!=0?sum2:1),numDuplicates,mbstr(str2,sizeDuplicates),numAfteremit,mbstr(str3,sizeAfteremit),mbstr(str4,bw),coin->maxbandwidth!=0.?100.*coin->bandwidth/coin->maxbandwidth:0.,coin->maxbandwidth>4*coin->bandwidth?"SLOW":"");
9 years ago
last = now;
9 years ago
}
9 years ago
if ( coin->bandwidth < 0.25*coin->maxbandwidth )
{
9 years ago
//printf(">>SLOW.%d<< ",addr->addrind);
9 years ago
//iguana_blast(coin,addr);
9 years ago
}
9 years ago
}
9 years ago
copyflag = (coin->enableCACHE != 0) && (strcmp(coin->symbol,"BTC") != 0);
9 years ago
bp = 0, bundlei = -2;
bp = iguana_bundlefind(coin,&bp,&bundlei,origtxdata->zblock.RO.hash2);
8 years ago
printf("[%d:%d].(%s) %s n%d\n",bp!=0?bp->hdrsi:-1,bundlei,bits256_str(str,origtxdata->zblock.RO.hash2),addr->ipaddr,addr->pendblocks);
9 years ago
if ( bp != 0 && bundlei >= 0 && bundlei < bp->n )
{
9 years ago
block = bp->blocks[bundlei];
9 years ago
if ( bp->emitfinish != 0 )
{
9 years ago
numAfteremit++;
sizeAfteremit += recvlen;
9 years ago
if ( block != 0 )
9 years ago
iguana_bundletime(coin,bp,bundlei,block,1);
8 years ago
//printf("got [%d:%d] with emitfinish.%u\n",bp->hdrsi,bundlei,bp->emitfinish);
9 years ago
return;
}
bp->dirty++;
9 years ago
if ( bundlei >= 0 && block != 0 )
9 years ago
{
9 years ago
if ( block->fpipbits != 0 && block->txvalid != 0 )
9 years ago
{
9 years ago
numDuplicates++;
sizeDuplicates += recvlen;
9 years ago
//printf("duplicate [%d:%d] %s\n",bp->hdrsi,bundlei,bits256_str(str,block->RO.hash2));
8 years ago
if ( bits256_cmp(origtxdata->zblock.RO.hash2,block->RO.hash2) != 0 )
{
8 years ago
//printf("mismatched tx %s received? mainchain.%d\n",bits256_str(str,block->RO.hash2),block->mainchain);
9 years ago
return;
8 years ago
}
else
{
iguana_bundletime(coin,bp,bundlei,block,1);
8 years ago
iguana_blockzcopyRO(0*coin->chain->zcash,&block->RO,0,&origtxdata->zblock.RO,0);
8 years ago
block->txvalid = 1;
}
//if ( block->mainchain != 0 )
// return;
9 years ago
}
9 years ago
else
{
iguana_bundletime(coin,bp,bundlei,block,0);
8 years ago
if ( (0) && bp == coin->current )
9 years ago
printf("recv [%d:%d] %s\n",bp->hdrsi,bundlei,bits256_str(str,block->RO.hash2));
}
9 years ago
}
}
else
9 years ago
{
9 years ago
if ( (bp= coin->current) != 0 )
9 years ago
{
9 years ago
for (i=0; i<coin->bundlescount; i++)
9 years ago
{
9 years ago
if ( (bp= coin->bundles[i]) != 0 && bp->emitfinish == 0 && bp->speculative != 0 && bp->numhashes < bp->n )
9 years ago
{
if ( (j= iguana_speculativefind(coin,bp,(struct iguana_block *)&origtxdata->zblock,data,recvlen)) >= 0 )
9 years ago
{
9 years ago
copyflag = 0;
speculative = 1;
9 years ago
if ( bp->blocks[j] != 0 )
iguana_bundletime(coin,bp,j,bp->blocks[j],0);
9 years ago
break;
}
}
}
}
}
9 years ago
block = 0;
if ( copyflag != 0 && recvlen != 0 && (bp == 0 || bundlei < 0 || ((block= bp->blocks[bundlei]) != 0 && block->fpipbits == 0 && block->req == 0)) )
9 years ago
{
9 years ago
struct iguana_msghdr checkH;
req = iguana_bundlereq(coin,addr,'B',data,copyflag * recvlen);
9 years ago
req->copyflag = 1;
9 years ago
req->H = *H;
8 years ago
if ( (0) && iguana_sethdr(&checkH,coin->chain->netmagic,H->command,req->serializeddata,recvlen) > 0 && memcmp(&checkH,H,sizeof(checkH)) != 0 )
9 years ago
{
int z;
for (z=0; z<sizeof(checkH); z++)
printf("%02x",((uint8_t *)&checkH)[z]);
printf(" req->H datalen.%d crc.%08x error\n",recvlen,calc_crc32(0,data,recvlen));
}
9 years ago
}
else
{
copyflag = 0;
9 years ago
req = iguana_bundlereq(coin,addr,'B',0,0);
9 years ago
}
9 years ago
req->recvlen = recvlen;
if ( bits256_cmp(origtxdata->zblock.RO.hash2,coin->APIblockhash) == 0 )
9 years ago
{
printf("MATCHED APIblockhash\n");
coin->APIblockstr = calloc(1,recvlen*2+1);
init_hexbytes_noT(coin->APIblockstr,data,recvlen);
}
9 years ago
txdata = origtxdata;
if ( addr != 0 )
{
8 years ago
if ( fromcache == 0 )
{
if ( addr->pendblocks > 0 )
addr->pendblocks--;
addr->lastblockrecv = (uint32_t)time(NULL);
addr->recvblocks += 1.;
addr->recvtotal += recvlen;
}
if ( speculative == 0 && iguana_ramchain_data(myinfo,coin,addr,origtxdata,txarray,origtxdata->zblock.RO.txn_count,data,recvlen,bp,block) >= 0 )
9 years ago
{
txdata->zblock.fpipbits = (uint32_t)addr->ipbits;
txdata->zblock.RO.recvlen = recvlen;
txdata->zblock.fpos = 0;
9 years ago
req->datalen = txdata->datalen;
req->ipbits = txdata->zblock.fpipbits;
8 years ago
} //else printf("cant save block\n");
9 years ago
}
8 years ago
if ( txdata->zblock.fpos == 0 )
8 years ago
{
8 years ago
numtx = origtxdata->zblock.RO.txn_count;
8 years ago
for (i=0; i<coin->bundlescount; i++)
if ( (bp= coin->bundles[i]) != 0 && bp->utxofinish <= 1 )
8 years ago
break;
8 years ago
if ( (i > coin->bundlescount-2 && coin->blocks.hwmchain.height > coin->longestchain-coin->chain->bundlesize*2) || coin->RTheight > 0 )
8 years ago
{
portable_mutex_lock(&coin->RTmutex);
iguana_RTrawdata(coin,txdata->zblock.RO.hash2,data,&recvlen,&numtx,0);
portable_mutex_unlock(&coin->RTmutex);
}
8 years ago
req->zblock = txdata->zblock;
if ( coin->virtualchain != 0 )
printf("%s recvlen.%d ipbits.%x prev.(%s)\n",coin->symbol,req->zblock.RO.recvlen,req->zblock.fpipbits,bits256_str(str,txdata->zblock.RO.prev_block));
req->zblock.RO.txn_count = req->numtx = txdata->zblock.RO.txn_count;
if ( fromcache == 0 )
{
coin->recvcount++;
coin->recvtime = (uint32_t)time(NULL);
netBLOCKS++;
}
req->addr = addr;
8 years ago
//if ( (bits256_cmp(origtxdata->zblock.RO.hash2,coin->blocks.hwmchain.RO.hash2) == 0 || req->zblock.mainchain == 0 || req->zblock.valid == 0 || req->zblock.txvalid == 0) && iguana_RTrawdata(coin,origtxdata->zblock.RO.hash2,0,&len,&numtx,1) == 0 )
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
8 years ago
if ( (0) && strcmp("BTCD",coin->symbol) == 0 )
8 years ago
printf("%s Q.(%s)\n",coin->symbol,bits256_str(str,origtxdata->zblock.RO.hash2));
8 years ago
} else printf("nonz fpos.%d %s\n",txdata->zblock.fpos,bits256_str(str,origtxdata->zblock.RO.hash2));
9 years ago
}
8 years ago
void iguana_recvstats_update(struct iguana_info *coin,struct iguana_peer *addr,int32_t recvlen)
{
static uint64_t received[IGUANA_MAXPEERS],count[IGUANA_MAXPEERS],lastcount,lastreceived,last;
char str[65],str2[65],str3[65],str4[65]; uint32_t now; int32_t i; int64_t sum2= 0,sum = 0,diffr,diff; double bw = 0.;
coin->recvcount++;
coin->recvtime = (uint32_t)time(NULL);
netBLOCKS++;
if ( addr->pendblocks > 0 )
addr->pendblocks--;
addr->lastblockrecv = (uint32_t)time(NULL);
addr->recvblocks += 1.;
addr->recvtotal += recvlen;
received[addr->addrind] += recvlen;
count[addr->addrind]++;
now = (uint32_t)time(NULL);
if ( ((rand() % 10000) == 0 && now > last+60) || now > last+600 )
{
for (i=0; i<sizeof(received)/sizeof(*received); i++)
sum += received[i], sum2 += count[i];
diffr = (sum - lastreceived), diff = (sum2 - lastcount);
lastreceived = sum, lastcount = sum2;
if ( diff != 0 )
{
bw = ((double)diffr / (now - last + 1));
if ( bw > coin->maxbandwidth )
coin->maxbandwidth = bw;
}
dxblend(&coin->bandwidth,bw,.9);
printf("%s BLOCKS.%llu RECV %s ave %.1f | dup.%d %s after.%d %s %s/sec %.2f%% %s\n",coin->symbol,(long long)sum2,mbstr(str,sum),(double)sum/(sum2!=0?sum2:1),numDuplicates,mbstr(str2,sizeDuplicates),numAfteremit,mbstr(str3,sizeAfteremit),mbstr(str4,bw),coin->maxbandwidth!=0.?100.*coin->bandwidth/coin->maxbandwidth:0.,coin->maxbandwidth>4*coin->bandwidth?"SLOW":"");
last = now;
}
if ( coin->bandwidth < 0.25*coin->maxbandwidth )
{
//printf(">>SLOW.%d<< ",addr->addrind);
//iguana_blast(coin,addr);
}
}
int32_t iguana_bundlestats_update(struct iguana_info *coin,struct iguana_block **blockp,struct iguana_bundle *bp,int32_t bundlei,struct iguana_txblock *origtxdata,uint8_t *data,int32_t recvlen)
{
char str[65]; struct iguana_block *block; int32_t i,j;
*blockp = 0;
if ( bp != 0 && bundlei >= 0 && bundlei < bp->n )
{
*blockp = block = bp->blocks[bundlei];
if ( bp->emitfinish != 0 )
{
numAfteremit++;
sizeAfteremit += recvlen;
if ( block != 0 )
iguana_bundletime(coin,bp,bundlei,block,1);
8 years ago
//printf("got [%d:%d] with emitfinish.%u\n",bp->hdrsi,bundlei,bp->emitfinish);
8 years ago
return(-1);
}
bp->dirty++;
if ( bundlei >= 0 && block != 0 )
{
if ( block->fpipbits != 0 && block->txvalid != 0 )
{
numDuplicates++;
sizeDuplicates += recvlen;
//printf("duplicate [%d:%d] %s\n",bp->hdrsi,bundlei,bits256_str(str,block->RO.hash2));
if ( bits256_cmp(origtxdata->zblock.RO.hash2,block->RO.hash2) != 0 )
{
//printf("mismatched tx %s received? mainchain.%d\n",bits256_str(str,block->RO.hash2),block->mainchain);
return(-1);
}
else
{
iguana_bundletime(coin,bp,bundlei,block,1);
8 years ago
iguana_blockzcopyRO(0*coin->chain->zcash,&block->RO,0,&origtxdata->zblock.RO,0);
8 years ago
return(0);
}
}
else
{
iguana_bundletime(coin,bp,bundlei,block,0);
8 years ago
if ( (0) && bp == coin->current )
8 years ago
printf("recv [%d:%d] %s\n",bp->hdrsi,bundlei,bits256_str(str,block->RO.hash2));
return(0);
}
}
}
else
{
if ( (bp= coin->current) != 0 )
{
for (i=0; i<coin->bundlescount; i++)
{
if ( (bp= coin->bundles[i]) != 0 && bp->emitfinish == 0 && bp->speculative != 0 && bp->numhashes < bp->n )
{
if ( (j= iguana_speculativefind(coin,bp,(struct iguana_block *)&origtxdata->zblock,data,recvlen)) >= 0 )
{
printf("speculative found\n");
//copyflag = 0;
//speculative = 1;
if ( (*blockp= bp->blocks[j]) != 0 )
iguana_bundletime(coin,bp,j,*blockp,0);
return(1);
}
}
}
}
}
return(0);
}
struct iguana_bundlereq *iguana_recv_bundlereq(struct iguana_info *coin,struct iguana_peer *addr,int32_t copyflag,struct iguana_msghdr *H,uint8_t *data,int32_t recvlen,struct iguana_bundle *bp,int32_t bundlei,struct iguana_txblock *txdata)
{
struct iguana_bundlereq *req; struct iguana_block *block = 0;
if ( copyflag != 0 && recvlen != 0 && (bp == 0 || bundlei < 0 || ((block= bp->blocks[bundlei]) != 0 && block->fpipbits == 0 && block->req == 0)) )
{
struct iguana_msghdr checkH;
req = iguana_bundlereq(coin,addr,'B',data,copyflag * recvlen);
req->copyflag = 1;
req->H = *H;
8 years ago
if ( (0) && iguana_sethdr(&checkH,coin->chain->netmagic,H->command,req->serializeddata,recvlen) > 0 && memcmp(&checkH,H,sizeof(checkH)) != 0 )
8 years ago
{
int z;
for (z=0; z<sizeof(checkH); z++)
printf("%02x",((uint8_t *)&checkH)[z]);
printf(" req->H datalen.%d crc.%08x error\n",recvlen,calc_crc32(0,data,recvlen));
}
}
else
{
copyflag = 0;
req = iguana_bundlereq(coin,addr,'B',0,0);
}
req->recvlen = recvlen;
req->datalen = txdata->datalen;
req->ipbits = txdata->zblock.fpipbits;
req->zblock = txdata->zblock;
req->zblock.RO.txn_count = req->numtx = txdata->zblock.RO.txn_count;
req->addr = addr;
return(req);
}
void iguana_RTgotblock(struct iguana_info *coin,bits256 hash2,uint8_t *data,int32_t *recvlenp,int32_t *numtxp)
{
int32_t i; struct iguana_bundle *bp;
if ( coin->almostRT == 0 )
{
for (i=0; i<coin->bundlescount; i++)
if ( (bp= coin->bundles[i]) != 0 && bp->utxofinish <= 1 )
8 years ago
break;
if ( (i > coin->bundlescount-2 && coin->blocks.hwmchain.height > coin->longestchain-coin->chain->bundlesize*2) || coin->RTheight > 0 )
coin->almostRT = 1;
}
if ( coin->almostRT != 0 )
{
portable_mutex_lock(&coin->RTmutex);
iguana_RTrawdata(coin,hash2,data,recvlenp,numtxp,0);
portable_mutex_unlock(&coin->RTmutex);
}
}
int32_t iguana_txmerkle(struct iguana_info *coin,bits256 *tree,int32_t treesize,struct iguana_txblock *origtxdata,struct iguana_msgtx *txarray)
{
int32_t i,msize; bits256 merkle_root;
if ( bits256_nonz(origtxdata->zblock.RO.merkle_root) == 0 )
{
memset(&origtxdata->zblock.RO.prev_block,0,sizeof(bits256));
origtxdata->zblock.RO.recvlen = 0;
origtxdata->zblock.issued = 0;
return(-1);
}
msize = (int32_t)sizeof(bits256) * (origtxdata->zblock.RO.txn_count+1) * 2;
if ( msize <= treesize )
{
for (i=0; i<origtxdata->zblock.RO.txn_count; i++)
tree[i] = txarray[i].txid;
merkle_root = iguana_merkle(coin->symbol,tree,origtxdata->zblock.RO.txn_count);
8 years ago
if ( bits256_cmp(merkle_root,origtxdata->zblock.RO.merkle_root) != 0 )
{
char str[65],str2[65];
printf(">>>>>>>>>> %s merkle mismatch.[%d] calc.(%s) vs (%s)\n",coin->symbol,origtxdata->zblock.RO.txn_count,bits256_str(str,merkle_root),bits256_str(str2,origtxdata->zblock.RO.merkle_root));
origtxdata->zblock.RO.recvlen = 0;
origtxdata->zblock.issued = 0;
return(-1);
} //else printf("matched merkle.%d\n",txn_count);
return(0);
} else printf("not enough memory for merkle verify %d vs %u\n",(int32_t)(sizeof(bits256)*(origtxdata->zblock.RO.txn_count+1)),treesize);
return(-1);
}
8 years ago
void iguana_gotblockM(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_peer *addr,struct iguana_txblock *origtxdata,struct iguana_msgtx *txarray,struct iguana_msghdr *H,uint8_t *data,int32_t recvlen,int32_t fromcache,uint8_t zcash)
8 years ago
{
static uint64_t totalrecv;
struct iguana_bundlereq *req; struct iguana_txblock *txdata = 0; int32_t incr,i,numsaved,valid,speculative=0,bundlei,copyflag,numtx; struct iguana_bundle *bp; struct iguana_block *block; char str[65];
if ( addr == 0 )
addr = &coin->internaladdr;
if ( recvlen < 0 || recvlen > IGUANA_MAXPACKETSIZE )
{
printf("iguana_getblockM: illegal recvlen.%d\n",recvlen);
return;
}
if ( fromcache == 0 && coin->virtualchain == 0 && addr != 0 && addr != &coin->internaladdr )
{
iguana_recvstats_update(coin,addr,recvlen);
totalrecv += recvlen;
}
origtxdata->zblock.RO.allocsize = sizeof(origtxdata->zblock);
if ( iguana_blockvalidate(myinfo,coin,&valid,(struct iguana_block *)&origtxdata->zblock,1) < 0 )
{
printf("got block that doesnt validate? %s\n",bits256_str(str,origtxdata->zblock.RO.hash2));
return;
}
8 years ago
//printf("getblockM %s\n",bits256_str(str,origtxdata->zblock.RO.hash2));
8 years ago
iguana_peer_meminit(coin,addr);
if ( iguana_txmerkle(coin,addr->TXDATA.ptr,(int32_t)addr->TXDATA.totalsize,origtxdata,txarray) < 0 )
return;
origtxdata->zblock.txvalid = 1;
bp = 0, bundlei = -2;
if ( iguana_bundlefind(coin,&bp,&bundlei,origtxdata->zblock.RO.hash2) == 0 )
{
bp = 0, bundlei = -2;
if ( iguana_bundlefind(coin,&bp,&bundlei,origtxdata->zblock.RO.prev_block) == 0 )
{
8 years ago
//printf("gotblockM: RTblock? %s\n",bits256_str(str,origtxdata->zblock.RO.hash2));
8 years ago
numtx = origtxdata->zblock.RO.txn_count;
8 years ago
iguana_RTgotblock(coin,origtxdata->zblock.RO.hash2,data,&recvlen,&numtx);
8 years ago
req = iguana_recv_bundlereq(coin,addr,0,H,data,recvlen,0,-1,origtxdata);
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
8 years ago
return;
}
else if ( bundlei < coin->chain->bundlesize-1 )
8 years ago
{
8 years ago
bundlei++;
8 years ago
iguana_hash2set(coin,"gotblockM",bp,bundlei,origtxdata->zblock.RO.hash2);
}
8 years ago
else // new bundle case, but bad context to extend
{
8 years ago
bits256 zero;
memset(zero.bytes,0,sizeof(zero));
if ( (bp= iguana_bundlecreate(coin,&bundlei,bp->bundleheight + coin->chain->bundlesize,origtxdata->zblock.RO.hash2,zero,0)) == 0 )
{
origtxdata->zblock.issued = 0;
origtxdata->zblock.RO.recvlen = 0;
printf("gotblockM2: error finding block %s\n",bits256_str(str,origtxdata->zblock.RO.hash2));
return;
8 years ago
} //else printf("getblockM autoextended.[%d]\n",bp->hdrsi);
8 years ago
}
}
if ( bp == 0 )
{
printf("gotblockM no bp %s\n",bits256_str(str,origtxdata->zblock.RO.hash2));
req = iguana_recv_bundlereq(coin,addr,0,H,data,recvlen,0,-1,origtxdata);
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
8 years ago
return;
}
8 years ago
for (i=numsaved=0; i<coin->chain->bundlesize; i++)
{
if ( (block= bp->blocks[i]) != 0 && block->fpipbits != 0 && block->fpos >= 0 && block->txvalid != 0 )
numsaved++;
}
if ( (speculative= iguana_bundlestats_update(coin,&block,bp,bundlei,origtxdata,data,recvlen)) < 0 )
{
req = iguana_recv_bundlereq(coin,addr,0,H,data,recvlen,0,-1,origtxdata);
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
//printf("negative speculative return %s\n",bits256_str(str,origtxdata->zblock.RO.hash2));
8 years ago
return;
}
8 years ago
if ( bp == coin->current )
8 years ago
{
8 years ago
if ( block == 0 )
block = iguana_blockhashset("noblock",coin,bp->bundleheight+bundlei,origtxdata->zblock.RO.hash2,1);
8 years ago
if ( block != 0 && (block->hdrsi != bp->hdrsi || block->bundlei != bundlei) )
8 years ago
{
block->hdrsi = bp->hdrsi;
block->bundlei = bundlei;
}
8 years ago
//printf("getblockM update [%d:%d] %s %p\n",bp->hdrsi,bundlei,bits256_str(str,origtxdata->zblock.RO.hash2),block);
8 years ago
}
8 years ago
if ( (block= bp->blocks[bundlei]) == 0 || bits256_nonz(bp->hashes[bundlei]) == 0 )
8 years ago
{
8 years ago
//printf("SET [%d:%d]\n",bp->hdrsi,bundlei);
8 years ago
//iguana_hash2set(coin,"noblock",bp,bundlei,origtxdata->zblock.RO.hash2);
bp->hashes[bundlei] = origtxdata->zblock.RO.hash2;
if ( bp->speculative != 0 )
bp->speculative[bundlei] = bp->hashes[bundlei];
8 years ago
//bp->blocks[bundlei] = block;
8 years ago
}
8 years ago
numtx = origtxdata->zblock.RO.txn_count;
iguana_RTgotblock(coin,origtxdata->zblock.RO.hash2,data,&recvlen,&numtx);
8 years ago
if ( block != 0 )
8 years ago
{
8 years ago
block->hdrsi = bp->hdrsi;
block->bundlei = bundlei;
block->height = bp->hdrsi*coin->chain->bundlesize + bundlei;
8 years ago
block->txvalid = block->valid = 1;
8 years ago
block->RO = origtxdata->zblock.RO;
8 years ago
if ( block->fpipbits != 0 && block->fpos >= 0 )
8 years ago
{
8 years ago
static int32_t numredundant; static double redundantsize; static uint32_t lastdisp;
char str[65],str2[65];
numredundant++, redundantsize += recvlen;
if ( time(NULL) > lastdisp+30 )
{
lastdisp = (uint32_t)time(NULL);
printf("%s have %d:%d at %d | %d blocks %s redundant xfers total %s %.2f%% wasted\n",coin->symbol,bp->hdrsi,bundlei,block->fpos,numredundant,mbstr(str,redundantsize),mbstr(str2,totalrecv),100.*redundantsize/totalrecv);
}
if ( bundlei > 1 )
{
// printf("DUP s.%d [%d:%d].(%s) %s n%d\n",numsaved,bp!=0?bp->hdrsi:-1,bundlei,bits256_str(str,origtxdata->zblock.RO.hash2),addr->ipaddr,addr->pendblocks);
}
req = iguana_recv_bundlereq(coin,addr,0,H,data,recvlen,0,-1,origtxdata);
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
8 years ago
return;
8 years ago
}
}
8 years ago
txdata = origtxdata;
8 years ago
/*static portable_mutex_t mutex; static int32_t didinit;
8 years ago
if ( didinit == 0 )
{
portable_mutex_init(&mutex);
didinit = 1;
}
8 years ago
portable_mutex_lock(&mutex);*/
if ( iguana_ramchain_data(myinfo,coin,addr,origtxdata,txarray,origtxdata->zblock.RO.txn_count,data,recvlen,bp,block) >= 0 )
8 years ago
{
8 years ago
txdata->zblock.fpipbits = (uint32_t)addr->ipbits;
txdata->zblock.RO.recvlen = recvlen;
txdata->zblock.fpos = 0;
8 years ago
copyflag = (coin->enableCACHE != 0) && (strcmp(coin->symbol,"BTC") != 0);
req = iguana_recv_bundlereq(coin,addr,copyflag,H,data,recvlen,bp,bundlei,txdata);
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
8 years ago
if ( (0) && bp->hdrsi == 0 && strcmp("SYS",coin->symbol) == 0 )
8 years ago
printf("[%d:%d].s%d %s Q.(%s) %s\n",bp->hdrsi,bundlei,numsaved,coin->symbol,bits256_str(str,origtxdata->zblock.RO.hash2),addr->ipaddr);
8 years ago
if ( numsaved < coin->chain->bundlesize )
8 years ago
{
for (i=numsaved=0; i<coin->chain->bundlesize; i++)
{
if ( (block= bp->blocks[i]) != 0 && block->fpipbits != 0 && block->fpos >= 0 && block->txvalid != 0 )
numsaved++;
}
if ( numsaved < coin->chain->bundlesize && bp->startutxo == 0 )
{
if ( (incr= coin->peers->numranked) == 0 )
incr = 1;
i = addr->addrind % incr;
for (; i<coin->chain->bundlesize; i+=incr)
{
if ( (bp->issued[i] == 0 && bits256_nonz(bp->hashes[i]) != 0) && ((block= bp->blocks[i]) == 0 || bits256_cmp(bp->hashes[i],block->RO.hash2) != 0 || block->fpipbits == 0 || block->fpos < 0 || block->txvalid == 0) )
{
bp->issued[i] = 1;
iguana_sendblockreqPT(coin,addr,0,-1,bp->hashes[i],0);
//printf("numsaved.%d auto issue.[%d:%d] %s\n",numsaved,bp->hdrsi,i,addr->ipaddr);
break;
} //else printf("numsaved.%d SKIP auto issue.[%d:%d] %s\n",numsaved,bp->hdrsi,i,addr->ipaddr);
}
}
8 years ago
else if ( bp->queued == 0 && bp->startutxo == 0 )
8 years ago
{
iguana_bundleQ(myinfo,coin,bp,1000);
8 years ago
//printf("numsaved.%d [%d] %s\n",numsaved,bp->hdrsi,addr->ipaddr);
8 years ago
}
8 years ago
}
8 years ago
}
else
{
req = iguana_recv_bundlereq(coin,addr,0,H,data,recvlen,0,-1,origtxdata);
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
8 years ago
}
8 years ago
//portable_mutex_unlock(&mutex);
8 years ago
}
9 years ago
void iguana_gottxidsM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *txids,int32_t n)
{
struct iguana_bundlereq *req;
9 years ago
//printf("got %d txids from %s\n",n,addr->ipaddr);
9 years ago
req = iguana_bundlereq(coin,addr,'T',0,0);
9 years ago
req->hashes = txids, req->n = n;
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
9 years ago
}
8 years ago
int32_t iguana_gotheadersM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_zblock *zblocks,int32_t n)
9 years ago
{
8 years ago
struct iguana_bundlereq *req; int32_t i,num; struct iguana_bundle *bp;
8 years ago
if ( n <= 1 )
8 years ago
return(-1);
9 years ago
if ( addr != 0 )
{
9 years ago
static uint32_t hdrsreceived[IGUANA_MAXPEERS];
hdrsreceived[addr->addrind] += n;
char str[65];
if ( (rand() % 1000) == 0 )
{
uint32_t i,sum = 0;
for (i=0; i<sizeof(hdrsreceived)/sizeof(*hdrsreceived); i++)
sum += hdrsreceived[i];
8 years ago
printf("%s TOTAL HDRS RECEIVED %u -> %s\n",coin->symbol,sum,mbstr(str,sum*80));
9 years ago
}
9 years ago
addr->recvhdrs++;
if ( addr->pendhdrs > 0 )
addr->pendhdrs--;
//printf("%s blocks[%d] ht.%d gotheaders pend.%d %.0f\n",addr->ipaddr,n,blocks[0].height,addr->pendhdrs,milliseconds());
if ( bits256_cmp(zblocks[1].RO.hash2,coin->RThash1) == 0 )
9 years ago
{
num = (n < coin->chain->bundlesize ? n : coin->chain->bundlesize);
for (i=0; i<num; i++)
addr->RThashes[i] = zblocks[i].RO.hash2;
9 years ago
addr->numRThashes = num;
}
9 years ago
}
if ( strcmp("BTC",coin->symbol) != 0 && n == 2 )
iguana_sendblockreqPT(coin,addr,0,-1,zblocks[1].RO.hash2,0);
8 years ago
if ( 1 )
8 years ago
{
8 years ago
for (i=0; i<coin->bundlescount; i++)
{
if ( (bp= coin->bundles[i]) != 0 && bits256_cmp(zblocks[1].RO.hash2,bp->hashes[1]) == 0 && bp->numhashes >= coin->chain->bundlesize )
return(-1);
}
8 years ago
}
8 years ago
i = 0;
if ( n >= 2*coin->chain->bundlesize+1 )
{
while ( n-i*coin->chain->bundlesize >= 2*coin->chain->bundlesize+1 )
{
req = iguana_bundlereq(coin,addr,'H',0,0);
req->blocks = mycalloc('r',coin->chain->bundlesize,sizeof(*zblocks));
memcpy(req->blocks,&zblocks[i++ * coin->chain->bundlesize],coin->chain->bundlesize * sizeof(*zblocks));
req->n = coin->chain->bundlesize;
HDRnet++;
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
8 years ago
}
}
else
{
req = iguana_bundlereq(coin,addr,'H',0,0);
req->blocks = zblocks, req->n = n;
HDRnet++;
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
8 years ago
zblocks = 0;
}
if ( zblocks != 0 )
myfree(zblocks,sizeof(*zblocks)*n);
8 years ago
return(0);
9 years ago
}
void iguana_gotblockhashesM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *blockhashes,int32_t n)
{
8 years ago
struct iguana_bundlereq *req; int32_t i,num; //struct iguana_bundle *bp;
9 years ago
if ( addr != 0 )
{
addr->recvhdrs++;
if ( addr->pendhdrs > 0 )
addr->pendhdrs--;
9 years ago
if ( bits256_cmp(blockhashes[1],coin->RThash1) == 0 )
{
num = (n < coin->chain->bundlesize ? n : coin->chain->bundlesize);
memcpy(addr->RThashes,blockhashes,num * sizeof(*addr->RThashes));
addr->numRThashes = num;
}
9 years ago
}
9 years ago
req = iguana_bundlereq(coin,addr,'S',0,0);
9 years ago
req->hashes = blockhashes, req->n = n;
9 years ago
char str[65];
8 years ago
if ( (0) && n > 2 && addr != 0 )
9 years ago
printf("addr.%d %s [%d]\n",addr->rank,bits256_str(str,blockhashes[1]),n);
8 years ago
queue_enqueue("recvQ",&coin->recvQ,&req->DL);
8 years ago
if ( strcmp("BTC",coin->symbol) != 0 )
{
8 years ago
if ( n > coin->chain->bundlesize )
8 years ago
iguana_sendblockreqPT(coin,addr,0,-1,blockhashes[1],0);
8 years ago
if ( (0) && coin->RTheight > 0 )
8 years ago
{
for (i=1; i<num; i++)
8 years ago
iguana_sendblockreqPT(coin,0,0,-1,blockhashes[i],0);
8 years ago
}
8 years ago
}
9 years ago
}
8 years ago
uint32_t iguana_allhashcmp(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_bundle *bp,bits256 *blockhashes,int32_t num)
9 years ago
{
9 years ago
bits256 allhash; int32_t err,i,n; struct iguana_block *block,*prev;
9 years ago
if ( bits256_nonz(bp->allhash) > 0 && num >= coin->chain->bundlesize && bp->emitfinish == 0 )
9 years ago
{
vcalc_sha256(0,allhash.bytes,blockhashes[0].bytes,coin->chain->bundlesize * sizeof(*blockhashes));
8 years ago
//char str[65]; printf("allhashes.(%s)\n",bits256_str(str,allhash));
9 years ago
if ( memcmp(allhash.bytes,bp->allhash.bytes,sizeof(allhash)) == 0 )
9 years ago
{
9 years ago
if ( bp->bundleheight > 0 )
9 years ago
prev = iguana_blockfind("allhashcmp",coin,iguana_blockhash(coin,bp->bundleheight-1));
9 years ago
else prev = 0;
9 years ago
for (i=n=0; i<coin->chain->bundlesize&&i<bp->n; i++)
{
9 years ago
if ( (err= iguana_bundlehash2add(coin,&block,bp,i,blockhashes[i])) < 0 )
9 years ago
{
printf("error adding blockhash allhashes hdrsi.%d i.%d\n",bp->hdrsi,i);
9 years ago
return(err);
9 years ago
}
9 years ago
if ( block != 0 && block == bp->blocks[i] )
{
9 years ago
if ( i > 0 )
block->RO.prev_block = blockhashes[i-1];
9 years ago
block->height = bp->bundleheight + i;
9 years ago
//printf("allhashcmp ht.%d for %d\n",block->height,i);
9 years ago
block->mainchain = 1;
if ( prev != 0 )
{
9 years ago
block->RO.prev_block = prev->RO.hash2;
9 years ago
prev->hh.next = block;
block->hh.prev = prev;
}
8 years ago
if ( bp->startutxo == 0 && bp->issued[i] == 0 )
8 years ago
{
iguana_blockQ("allhashes",coin,bp,i,blockhashes[i],1);
iguana_blockQ("allhashes",coin,bp,i,blockhashes[i],0);
n++;
}
9 years ago
} else printf("no allhashes block.%p or mismatch.%p\n",block,bp->blocks[i]);
9 years ago
prev = block;
9 years ago
}
9 years ago
coin->allhashes++;
8 years ago
//n = 0;
//if ( bp->hdrsi < coin->MAXBUNDLES || (coin->current != 0 && coin->lastpending != 0 && bp->hdrsi >= coin->current->hdrsi && bp->hdrsi <= coin->lastpending->hdrsi) )
// n = iguana_bundleissuemissing(myinfo,coin,bp,1,3.);
8 years ago
if ( (0) && n > 2 )
9 years ago
printf("ALLHASHES FOUND! %d allhashes.%d issued %d\n",bp->bundleheight,coin->allhashes,n);
8 years ago
//if ( bp->queued == 0 )
// iguana_bundleQ(myinfo,coin,bp,bp->n*5 + (rand() % 500));
9 years ago
return(bp->queued);
9 years ago
}
}
9 years ago
return(0);
9 years ago
}
9 years ago
void iguana_bundlespeculate(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2,int32_t offset)
{
9 years ago
if ( bp == 0 )
return;
8 years ago
if ( time(NULL) > bp->hdrtime+3 && strcmp("BTC",coin->symbol) != 0 && bp->numhashes < bp->n && bundlei == 0 && bp->speculative == 0 && bp->bundleheight < coin->longestchain-coin->chain->bundlesize )
9 years ago
{
char str[65]; bits256_str(str,bp->hashes[0]);
9 years ago
//fprintf(stderr,"Afound block -> %d %d hdr.%s\n",bp->bundleheight,coin->longestchain-coin->chain->bundlesize,str);
8 years ago
bp->hdrtime = (uint32_t)time(NULL);
8 years ago
queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(str));
9 years ago
}
}
9 years ago
int32_t iguana_bundlehashadd(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,struct iguana_block *block)
{
static const bits256 zero;
9 years ago
struct iguana_ramchain blockR; int32_t hdrsi,firstflag=0,checki,retval=-1; long size = 0; FILE *fp; char fname[1024];
9 years ago
block->bundlei = bundlei;
block->hdrsi = bp->hdrsi;
if ( bits256_nonz(bp->hashes[bundlei]) != 0 && bits256_cmp(bp->hashes[bundlei],block->RO.hash2) != 0 )
{
char str[65],str2[65]; printf("mismatched.[%d:%d] %s <-? %s%s\n",bp->hdrsi,bundlei,bits256_str(str,bp->hashes[bundlei]),bits256_str(str2,block->RO.hash2),block->mainchain?".main":"");
if ( block == bp->blocks[bundlei] )
{
if ( block->mainchain == 0 )
9 years ago
{
printf("mainchain blockptr.%p vs %p\n",block,bp->blocks[bundlei]);
9 years ago
return(-1);
9 years ago
}
9 years ago
}
else if ( bp->blocks[bundlei] != 0 )
{
printf("mismatched blockptr.%p vs %p\n",block,bp->blocks[bundlei]);
return(-1);
}
}
8 years ago
if ( bp->blocks[bundlei] == 0 )
9 years ago
firstflag = 1;
//bp->blocks[bundlei] = block;
//bp->hashes[bundlei] = block->RO.hash2;
9 years ago
iguana_bundlehash2add(coin,0,bp,bundlei,block->RO.hash2);
9 years ago
if ( firstflag != 0 && bp->emitfinish == 0 )
9 years ago
{
9 years ago
//block->fpos = -1;
8 years ago
if ( (0) && iguana_ramchainfile(SuperNET_MYINFO(0),coin,0,&blockR,bp,bundlei,block) == 0 )
9 years ago
{
size = sizeof(blockR);
iguana_ramchain_free(coin,&blockR,1);
}
8 years ago
else if ( bp->hdrsi > 0 && bp->hdrsi == coin->longestchain/bp->n )
9 years ago
{
9 years ago
checki = iguana_peerfname(coin,&hdrsi,GLOBAL_TMPDIR,fname,0,block->RO.hash2,zero,1,0);
9 years ago
if ( (fp= fopen(fname,"rb")) != 0 )
{
fseek(fp,0,SEEK_END);
size = (uint32_t)ftell(fp);
fclose(fp);
}
}
if ( size != 0 )
{
9 years ago
retval = 0;
9 years ago
//printf("initialize with fp.[%d:%d] len.%ld\n",bp->hdrsi,bundlei,size);
9 years ago
block->RO.recvlen = (int32_t)size;
block->fpipbits = 1;
block->txvalid = 1;
block->fpos = 0;
block->issued = (uint32_t)time(NULL);
}
}
9 years ago
return(retval);
9 years ago
}
9 years ago
8 years ago
void iguana_bundle_set(struct iguana_info *coin,struct iguana_block *block,int32_t height)
{
8 years ago
int32_t hdrsi,bundlei; struct iguana_bundle *bp; char str[65];
8 years ago
if ( block->height < 0 || block->height == height )
{
hdrsi = (height / coin->chain->bundlesize);
bundlei = (height % coin->chain->bundlesize);
if ( hdrsi < coin->bundlescount && (bp= coin->bundles[hdrsi]) != 0 )
{
bp->blocks[bundlei] = block;
bp->hashes[bundlei] = block->RO.hash2;
iguana_bundlehash2add(coin,0,bp,bundlei,block->RO.hash2);
8 years ago
if ( bp->speculative != 0 )
bp->speculative[bundlei] = block->RO.hash2;
8 years ago
//char str[65]; printf("SET %s ht.%d in [%d:%d]\n",bits256_str(str,block->RO.hash2),height,hdrsi,bundlei);
8 years ago
} //else printf("iguana_bundle_set: no bundle at [%d]\n",hdrsi);
8 years ago
} else printf("iguana_bundle_set: %s mismatch ht.%d vs %d\n",bits256_str(str,block->RO.hash2),block->height,height);
8 years ago
}
void iguana_hwmchain_set(struct iguana_info *coin,struct iguana_block *block,int32_t height)
{
8 years ago
if ( coin->RTheight > 0 )
8 years ago
{
8 years ago
if ( block->height == height )
{
8 years ago
iguana_blockcopy(0*coin->chain->zcash,coin->chain->auxpow,coin,(struct iguana_block *)&coin->blocks.hwmchain,block);
8 years ago
char str[65]; printf("SET %s HWM.%s ht.%d\n",coin->symbol,bits256_str(str,block->RO.hash2),height);
8 years ago
} else printf("iguana_hwmchain_set: mismatched ht.%d vs %d\n",block->height,height);
}
8 years ago
}
8 years ago
void iguana_mainchain_clear(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_block *mainchain,struct iguana_block *oldhwm,int32_t n)
8 years ago
{
8 years ago
int32_t i,height; char str[65]; struct iguana_block *tmp = oldhwm;
8 years ago
if ( mainchain != oldhwm )
{
height = oldhwm->height;
for (i=0; i<n; i++,height--)
{
8 years ago
iguana_RTnewblock(myinfo,coin,tmp);
8 years ago
bits256_str(str,tmp->RO.hash2);
8 years ago
if ( tmp->mainchain == 0 )
8 years ago
{
// printf("%s iguana_mainchain_clear: ORPHANED ht.%d %s\n",coin->symbol,tmp->height,str);
}
8 years ago
else if ( tmp->height != height )
8 years ago
printf("iguana_mainchain_clear: unexpected ht.%d vs %d %s\n",tmp->height,height,str);
8 years ago
else
{
tmp->mainchain = 0;
8 years ago
//printf("CLEAR %s mainchain.%d %s\n",coin->symbol,height,str);
8 years ago
}
if ( (tmp= iguana_blockfind("clear",coin,tmp->RO.prev_block)) == 0 )
{
8 years ago
printf("iguana_mainchain_clear: got null tmp i.%d of %d %s\n",i,n,str);
8 years ago
return;
}
}
8 years ago
if ( (0) && tmp != mainchain && coin->RTheight > 0 )
8 years ago
printf("iguana_mainchain_clear: unexpected mismatch ht.%d vs %d %s\n",tmp->height,mainchain->height,bits256_str(str,tmp->RO.hash2));
8 years ago
}
}
int32_t iguana_height_estimate(struct iguana_info *coin,struct iguana_block **mainchainp,struct iguana_block *block)
{
int32_t i,n; struct iguana_block *tmp = block;
*mainchainp = 0;
8 years ago
for (i=n=0; i<coin->chain->bundlesize; i++)
8 years ago
{
if ( tmp != 0 && (tmp= iguana_blockfind("estimate",coin,tmp->RO.hash2)) != 0 )
{
8 years ago
if ( tmp->mainchain != 0 && tmp->height >= 0 )
8 years ago
{
8 years ago
char str[65];
8 years ago
if ( (0) && n > 0 && coin->RTheight > 0 )
8 years ago
printf("%s M.%d dist.%d -> %d\n",bits256_str(str,block->RO.hash2),tmp->height,n,tmp->height+n);
8 years ago
*mainchainp = tmp;
return(tmp->height + n);
}
n++;
tmp = iguana_blockfind("prevestimate",coin,tmp->RO.prev_block);
} else return(0);
}
return(0);
}
9 years ago
// main context, ie single threaded
8 years ago
struct iguana_bundle *iguana_bundleset(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_block **blockp,int32_t *bundleip,struct iguana_block *origblock)
9 years ago
{
8 years ago
struct iguana_block *block,*prevblock,*tmp,*mainchain,*hwmblock; bits256 zero,hash2,prevhash2; struct iguana_bundle *prevbp,*bp = 0; int32_t i,n,hdrsi,newheight,prevbundlei,bundlei = -2; // struct iguana_ramchain blockR;
9 years ago
*bundleip = -2; *blockp = 0;
if ( origblock == 0 )
return(0);
memset(zero.bytes,0,sizeof(zero));
9 years ago
hash2 = origblock->RO.hash2;
9 years ago
if ( (block= iguana_blockhashset("bundleset",coin,-1,hash2,1)) != 0 )
9 years ago
{
9 years ago
prevhash2 = origblock->RO.prev_block;
9 years ago
if ( block != origblock )
9 years ago
{
8 years ago
iguana_blockcopy(0*coin->chain->zcash,coin->chain->auxpow,coin,block,origblock);
8 years ago
//fprintf(stderr,"bundleset block.%p vs origblock.%p prev.%d bits.%x fpos.%d\n",block,origblock,bits256_nonz(prevhash2),block->fpipbits,block->fpos);
9 years ago
}
9 years ago
*blockp = block;
8 years ago
if ( coin->blocks.hwmchain.height > 0 && (hwmblock= iguana_blockfind("hwm",coin,coin->blocks.hwmchain.RO.hash2)) != 0 )
8 years ago
{
8 years ago
if ( (newheight= iguana_height_estimate(coin,&mainchain,block)) >= coin->blocks.hwmchain.height )
8 years ago
{
8 years ago
iguana_mainchain_clear(myinfo,coin,mainchain,hwmblock,coin->blocks.hwmchain.height-mainchain->height);
n = (newheight - mainchain->height);
for (i=1; i<n; i++)
8 years ago
{
8 years ago
hdrsi = ((mainchain->height+i) / coin->chain->bundlesize);
bundlei = ((mainchain->height+i) % coin->chain->bundlesize);
if ( hdrsi < coin->bundlescount && (bp= coin->bundles[hdrsi]) != 0 )
{
8 years ago
if ( (tmp= bp->blocks[bundlei]) != 0 && tmp->height == mainchain->height+i )
8 years ago
{
iguana_bundle_set(coin,tmp,mainchain->height+i);
iguana_RTnewblock(myinfo,coin,tmp);
8 years ago
} else break;
8 years ago
}
8 years ago
}
8 years ago
if ( i == n && mainchain != hwmblock )
8 years ago
{
8 years ago
iguana_hwmchain_set(coin,mainchain,mainchain->height); // trigger reprocess
8 years ago
iguana_RTnewblock(myinfo,coin,mainchain);
}
8 years ago
}
8 years ago
else //if ( coin->RTheight > 0 && newheight == coin->RTheight )
8 years ago
{
8 years ago
//printf("newheight.%d is RTheight\n",newheight);
8 years ago
}
8 years ago
}
9 years ago
//if ( 0 && bits256_nonz(prevhash2) > 0 )
// iguana_patch(coin,block);
9 years ago
bp = 0, bundlei = -2;
9 years ago
if ( (bp= iguana_bundlefind(coin,&bp,&bundlei,hash2)) != 0 && bundlei < coin->chain->bundlesize )
9 years ago
{
9 years ago
if ( iguana_bundlehashadd(coin,bp,bundlei,block) < 0 )
{
8 years ago
if ( bp->emitfinish == 0 && bp->issued[bundlei] == 0 && block->issued == 0 && strcmp("BTC",coin->symbol) != 0 )
iguana_blockQ("bundleset",coin,bp,bundlei,block->RO.hash2,1);
9 years ago
}
8 years ago
//fprintf(stderr,"bundle found %d:%d\n",bp->hdrsi,bundlei);
9 years ago
if ( bundlei > 0 )
9 years ago
{
9 years ago
//printf("bundlehashadd prev %d\n",bundlei);
9 years ago
if ( bits256_nonz(prevhash2) != 0 )
iguana_bundlehash2add(coin,0,bp,bundlei-1,prevhash2);
9 years ago
}
9 years ago
else if ( bp->hdrsi > 0 && (bp= coin->bundles[bp->hdrsi-1]) != 0 )
iguana_bundlehash2add(coin,0,bp,coin->chain->bundlesize-1,prevhash2);
9 years ago
if ( 1 && strcmp("BTC",coin->symbol) != 0 )
9 years ago
iguana_bundlespeculate(coin,bp,bundlei,hash2,1);
9 years ago
}
9 years ago
prevbp = 0, prevbundlei = -2;
if (bits256_nonz(prevhash2)) {
iguana_bundlefind(coin, &prevbp, &prevbundlei, prevhash2);
//if ( 0 && block->blockhashes != 0 )
// fprintf(stderr,"has blockhashes bp.%p[%d] prevbp.%p[%d]\n",bp,bundlei,prevbp,prevbundlei);
if (prevbp != 0 && prevbundlei >= 0 && (prevblock = iguana_blockfind("bundleset2", coin, prevhash2)) != 0)
{
if (prevbundlei < coin->chain->bundlesize)
{
if (prevbp->hdrsi + 1 == coin->bundlescount && prevbundlei == coin->chain->bundlesize - 1)
{
if ( (prevbp->hdrsi % 10) == 9 )
iguana_savehdrs(coin);
printf("%s AUTOCREATE.%d\n", coin->symbol, prevbp->bundleheight + coin->chain->bundlesize);
if ( (bp= iguana_bundlecreate(coin, bundleip, prevbp->bundleheight + coin->chain->bundlesize, hash2, zero, 0)) != 0)
{
if ( bp->queued == 0 )
iguana_bundleQ(myinfo, coin, bp, 1000);
}
}
if (prevbundlei < coin->chain->bundlesize - 1)
{
//printf("bundlehash2add next %d\n",prevbundlei);
iguana_bundlehash2add(coin, 0, prevbp, prevbundlei + 1, hash2);
}
if (1 && strcmp("BTC", coin->symbol) != 0)
iguana_bundlespeculate(coin, prevbp, prevbundlei, prevhash2, 2);
}
}
}
9 years ago
} else printf("iguana_bundleset: error adding blockhash\n");
9 years ago
bp = 0, *bundleip = -2;
return(iguana_bundlefind(coin,&bp,bundleip,hash2));
9 years ago
}
9 years ago
void iguana_checklongestchain(struct iguana_info *coin,struct iguana_bundle *bp,int32_t num)
{
9 years ago
int32_t i; struct iguana_peer *addr;
8 years ago
if ( coin->RTheight > 0 && num > 30 && num < bp->n )
9 years ago
{
9 years ago
if ( coin->longestchain > bp->bundleheight+num+10*coin->chain->minconfirms )
9 years ago
{
printf("strange.%d suspicious longestchain.%d vs [%d:%d] %d bp->n %d\n",coin->longestchain_strange,coin->longestchain,bp->hdrsi,num,bp->bundleheight+num,bp->n);
8 years ago
if ( coin->longestchain_strange++ > 100 )
9 years ago
{
coin->badlongestchain = coin->longestchain;
coin->longestchain = bp->bundleheight+num;
coin->longestchain_strange = 0;
9 years ago
if ( coin->peers != 0 )
{
for (i=0; i<coin->peers->numranked; i++)
if ( (addr= coin->peers->ranked[i]) != 0 && addr->height >= coin->badlongestchain )
{
printf("blacklist addr.(%s) height %d\n",addr->ipaddr,addr->height);
addr->dead = 1;
addr->rank = 0;
}
}
9 years ago
}
}
else if ( coin->longestchain_strange > 0 )
{
printf("not strange.%d suspicious longestchain.%d vs [%d:%d] %d bp->n %d\n",coin->longestchain_strange,coin->longestchain,bp->hdrsi,num,bp->bundleheight+num,bp->n);
9 years ago
coin->longestchain_strange--;
9 years ago
}
}
}
8 years ago
struct iguana_bundlereq *iguana_recvblockhdrs(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_bundlereq *req,struct iguana_zblock *zblocks,int32_t n,int32_t *newhwmp)
9 years ago
{
8 years ago
int32_t i,bundlei,match; struct iguana_block *block; bits256 prevhash2; uint8_t serialized[sizeof(struct iguana_msgblock) + sizeof(struct iguana_msgzblockhdr)]; struct iguana_peer *addr; struct iguana_bundle *bp,*firstbp = 0;
if ( zblocks == 0 )
9 years ago
{
printf("iguana_recvblockhdrs null blocks?\n");
return(req);
}
if ( zblocks != 0 && n > 0 )
9 years ago
{
memset(prevhash2.bytes,0,sizeof(prevhash2));
8 years ago
for (i=match=0; i<n&&i<coin->chain->bundlesize; i++)
9 years ago
{
9 years ago
//fprintf(stderr,"i.%d of %d bundleset\n",i,n);
9 years ago
if ( bits256_cmp(zblocks[i].RO.prev_block,coin->blocks.hwmchain.RO.hash2) == 0 )
{
bp = 0, bundlei = -2;
8 years ago
if ( (bp= iguana_bundleset(myinfo,coin,&block,&bundlei,(struct iguana_block *)&zblocks[i])) != 0 )
9 years ago
{
8 years ago
if ( firstbp == 0 )
8 years ago
firstbp = bp;
9 years ago
if ( block->height >= 0 && block->height+1 > coin->longestchain )
coin->longestchain = block->height+1;
8 years ago
_iguana_chainlink(myinfo,coin,block);
9 years ago
}
8 years ago
//char str[65]; printf("HWM in hdr's prev[%d] bp.%p bundlei.%d block.%p %s\n",i,bp,bundlei,block,bp!=0?bits256_str(str,bp->hashes[bundlei]):"()");
9 years ago
}
8 years ago
bp = 0, bundlei = -2;
8 years ago
if ( (bp= iguana_bundleset(myinfo,coin,&block,&bundlei,(struct iguana_block *)&zblocks[i])) == 0 )
9 years ago
{
9 years ago
bp = 0, bundlei = -2;
8 years ago
if ( (bp= iguana_bundlefind(coin,&bp,&bundlei,zblocks[i].RO.prev_block)) != 0 )
9 years ago
{
8 years ago
if ( bundlei < coin->chain->bundlesize-1 )
bundlei++;
else
{
8 years ago
bundlei = 0;
bp = coin->bundles[bp->hdrsi+1];
}
9 years ago
}
9 years ago
}
8 years ago
if ( bp != 0 )
{
8 years ago
if ( firstbp == 0 )
8 years ago
firstbp = bp, match++;
8 years ago
else if ( bp == firstbp )
8 years ago
match++;
8 years ago
bp->dirty++;
if ( bp->issued[bundlei] == 0 )//&& coin->RTheight > 0 )
{
bp->issued[bundlei] = 1;
iguana_blockQ("recvhdr",coin,bp,bundlei,block->RO.hash2,0);
}
}
prevhash2 = zblocks[i].RO.hash2;
9 years ago
}
9 years ago
char str[65];
8 years ago
if ( (0) && bp == coin->current )
printf("i.%d n.%d match.%d blockhdrs.%s hdrsi.%d\n",i,n,match,bits256_str(str,zblocks[0].RO.hash2),firstbp!=0?firstbp->hdrsi:-1);
8 years ago
/*if ( firstbp != 0 && match >= coin->chain->bundlesize-1 )
9 years ago
{
if ( firstbp->queued == 0 )
{
9 years ago
//fprintf(stderr,"firstbp blockQ %d\n",firstbp->bundleheight);
8 years ago
iguana_bundleQ(myinfo,coin,firstbp,1000);
9 years ago
}
8 years ago
}*/
8 years ago
if ( firstbp != 0 && (addr= req->addr) != 0 && n >= coin->chain->bundlesize )
9 years ago
{
8 years ago
addr->RThashes[0] = firstbp->hashes[0];
8 years ago
for (i=1; i<coin->chain->bundlesize; i++)
{
8 years ago
iguana_serialize_block(myinfo,coin->chain,&addr->RThashes[i],serialized,(struct iguana_block *)&zblocks[i]);
}
8 years ago
addr->numRThashes = coin->chain->bundlesize;
8 years ago
//printf("firstbp.[%d] call allhashes %s\n",firstbp->hdrsi,bits256_str(str,addr->RThashes[0]));
8 years ago
if ( iguana_allhashcmp(myinfo,coin,firstbp,addr->RThashes,coin->chain->bundlesize) > 0 )
return(req);
9 years ago
}
9 years ago
}
return(req);
}
8 years ago
void iguana_autoextend(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_bundle *bp)
9 years ago
{
9 years ago
char hashstr[65]; struct iguana_bundle *newbp; int32_t bundlei; static const bits256 zero;
9 years ago
if ( bp->hdrsi == coin->bundlescount-1 && bits256_nonz(bp->nextbundlehash2) != 0 )
{
init_hexbytes_noT(hashstr,bp->nextbundlehash2.bytes,sizeof(bits256));
9 years ago
newbp = 0, bundlei = -2;
if ( iguana_bundlefind(coin,&newbp,&bundlei,bp->nextbundlehash2) != 0 )
{
if ( newbp->bundleheight != bp->bundleheight+bp->n )
{
9 years ago
printf("%d vs %d found spurious extra hash for [%d:%d]\n",newbp->bundleheight,bp->bundleheight,bp->hdrsi,bp->n);
9 years ago
memset(&bp->nextbundlehash2,0,sizeof(bp->nextbundlehash2));
return;
}
}
9 years ago
newbp = iguana_bundlecreate(coin,&bundlei,bp->bundleheight+coin->chain->bundlesize,bp->nextbundlehash2,zero,1);
if ( newbp != 0 )
{
8 years ago
if ( time(NULL) > bp->hdrtime+3 && newbp->speculative == 0 )
{
bp->hdrtime = (uint32_t)time(NULL);
8 years ago
queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr));
8 years ago
}
9 years ago
//char str[65],str2[65]; printf("EXTEND last bundle %s/%s ht.%d\n",bits256_str(str,newbp->hashes[0]),bits256_str(str2,bp->nextbundlehash2),newbp->bundleheight);
9 years ago
if ( newbp->queued == 0 )
8 years ago
iguana_bundleQ(myinfo,coin,newbp,1000);
9 years ago
}
}
}
8 years ago
struct iguana_bundlereq *iguana_recvblockhashes(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_bundlereq *req,bits256 *blockhashes,int32_t num)
9 years ago
{
9 years ago
int32_t bundlei,i,starti; struct iguana_block *block; struct iguana_bundle *bp; bits256 allhash,zero; struct iguana_peer *addr; char str[65],str2[65]; // uint8_t serialized[512];
9 years ago
memset(zero.bytes,0,sizeof(zero));
9 years ago
bp = 0, bundlei = -2;
iguana_bundlefind(coin,&bp,&bundlei,blockhashes[1]);
8 years ago
if ( (0) && strcmp("BTCD",coin->symbol) == 0 )//0 && num >= coin->chain->bundlesize )
9 years ago
printf("blockhashes[%d] %d of %d %s bp.%d[%d]\n",num,bp==0?-1:bp->hdrsi,coin->bundlescount,bits256_str(str,blockhashes[1]),bp==0?-1:bp->bundleheight,bundlei);
9 years ago
if ( num < 2 )
return(req);
9 years ago
else
{
for (i=1; i<num; i++)
9 years ago
{
9 years ago
if ( (block= iguana_blockfind("prev",coin,blockhashes[i])) != 0 && block->height+1 > coin->longestchain )
9 years ago
{
9 years ago
coin->longestchain = block->height+1;
9 years ago
if ( bp != 0 && bp->speculative != 0 && i < bp->n )
9 years ago
bp->speculative[i] = blockhashes[i];
}
8 years ago
iguana_blockQ("recvhash",coin,0,-1,blockhashes[i],1);
9 years ago
}
9 years ago
}
9 years ago
if ( bp != 0 )
{
9 years ago
bp->dirty++;
9 years ago
bp->hdrtime = (uint32_t)time(NULL);
9 years ago
blockhashes[0] = bp->hashes[0];
9 years ago
//iguana_blockQ("recvhash0",coin,bp,0,blockhashes[0],1);
9 years ago
if ( num >= coin->chain->bundlesize )
{
9 years ago
if ( bits256_nonz(bp->nextbundlehash2) == 0 && num > coin->chain->bundlesize )
9 years ago
{
9 years ago
bp->nextbundlehash2 = blockhashes[coin->chain->bundlesize];
8 years ago
iguana_blockQ("recvhash1",coin,0,-1,bp->nextbundlehash2,1);
9 years ago
}
9 years ago
//printf("call allhashes\n");
8 years ago
if ( (0) && bp->hdrsi == coin->bundlescount-1 )
8 years ago
iguana_autoextend(myinfo,coin,bp);
8 years ago
if ( iguana_allhashcmp(myinfo,coin,bp,blockhashes,num) > 0 )
9 years ago
return(req);
9 years ago
//printf("done allhashes\n");
9 years ago
}
9 years ago
else if ( bp->hdrsi == coin->bundlescount-1 )
9 years ago
{
9 years ago
iguana_checklongestchain(coin,bp,num);
9 years ago
if ( bp->speculative != 0 && bp->numspec < num )
{
for (i=bp->numspec; i<num; i++)
{
if ( bits256_nonz(bp->speculative[i]) == 0 )
bp->speculative[i] = blockhashes[i];
}
}
}
8 years ago
if ( strcmp("BTCD",coin->symbol) == 0 && (bp->speculative == 0 || num > bp->numspec) && bp->emitfinish == 0 )
9 years ago
{
9 years ago
//printf("FOUND speculative.%s BLOCKHASHES[%d] ht.%d\n",bits256_str(str,blockhashes[1]),num,bp->bundleheight);
9 years ago
if ( bp->speculative == 0 )
9 years ago
{
9 years ago
bp->speculative = mycalloc('s',bp->n+1,sizeof(*bp->speculative));
8 years ago
//for (i=0; i<bp->n; i++)
// if ( GETBIT(bp->haveblock,i) == 0 )
// bp->issued[i] = 0;
//iguana_bundleissuemissing(myinfo,coin,bp,3,1.);
9 years ago
}
9 years ago
for (i=1; i<num&&i<=bp->n; i++)
9 years ago
{
9 years ago
bp->speculative[i] = blockhashes[i];
8 years ago
if ( bp->blocks[i] == 0 || bp->blocks[i]->issued == 0 )
iguana_blockQ("speculate",coin,bp,-i,blockhashes[i],0);
9 years ago
if ( bp->blocks[i] == 0 )
bp->blocks[i] = iguana_blockhashset("recvhashes3",coin,bp->bundleheight+i,blockhashes[i],1);
9 years ago
//printf("speculate new issue [%d:%d]\n",bp->hdrsi,i);
}
9 years ago
bp->speculative[0] = bp->hashes[0];
9 years ago
bp->numspec = (num <= bp->n+1) ? num : bp->n+1;
9 years ago
if ( bp == coin->current && (addr= req->addr) != 0 )
{
memcpy(addr->RThashes,blockhashes,bp->numspec * sizeof(*addr->RThashes));
addr->numRThashes = bp->numspec;
}
9 years ago
//iguana_blockQ(coin,0,-1,blockhashes[2],1);
9 years ago
}
9 years ago
}
9 years ago
else if ( num >= coin->chain->bundlesize )
9 years ago
{
9 years ago
starti = coin->current != 0 ? coin->current->hdrsi : 0;
for (i=coin->bundlescount-1; i>=starti; i--)
9 years ago
{
9 years ago
if ( (bp= coin->bundles[i]) != 0 )//&& bp->emitfinish > 1 )
9 years ago
{
blockhashes[0] = bp->hashes[0];
9 years ago
vcalc_sha256(0,allhash.bytes,blockhashes[0].bytes,coin->chain->bundlesize * sizeof(*blockhashes));
8 years ago
if ( (0) && i == starti )
9 years ago
printf("vcalc.(%s) [%d].(%s)\n",bits256_str(str,allhash),bp->hdrsi,bits256_str(str2,bp->hashes[0]));
9 years ago
if ( bits256_cmp(allhash,bp->allhash) == 0 )
9 years ago
{
8 years ago
if ( (0) && bp->speculative == 0 )
9 years ago
printf("matched allhashes.[%d]\n",bp->hdrsi);
9 years ago
if ( bp->queued != 0 )
bp->queued = 0;
8 years ago
if ( iguana_allhashcmp(myinfo,coin,bp,blockhashes,coin->chain->bundlesize) > 0 )
9 years ago
{
bp->hdrtime = (uint32_t)time(NULL);
9 years ago
//iguana_blockQ("recvhash2",coin,bp,1,blockhashes[1],1);
//iguana_blockQ("recvhash3",coin,bp,0,blockhashes[0],1);
9 years ago
//iguana_blockQ("recvhash4",coin,bp,coin->chain->bundlesize-1,blockhashes[coin->chain->bundlesize-1],0);
9 years ago
//printf("matched bundle.%d\n",bp->bundleheight);
9 years ago
return(req);
} else printf("unexpected mismatch??\n");
9 years ago
}
}
9 years ago
}
9 years ago
//printf("%s.[%d] no match to allhashes\n",bits256_str(str,blockhashes[1]),num);
9 years ago
struct iguana_block *block;
9 years ago
if ( (block= iguana_blockhashset("recvhashes",coin,-1,blockhashes[1],1)) != 0 )
9 years ago
{
9 years ago
//block->blockhashes = blockhashes, req->hashes = 0;
9 years ago
//printf("set block->blockhashes[%d]\n",num);
9 years ago
}
9 years ago
/*if ( (addr= coin->peers->ranked[0]) != 0 )
9 years ago
{
if ( (len= iguana_getdata(coin,serialized,MSG_BLOCK,&blockhashes[1],1)) > 0 )
{
iguana_send(coin,addr,serialized,len);
9 years ago
//char str[65]; printf("REQ.%s\n",bits256_str(str,blockhashes[1]));
9 years ago
}
9 years ago
}*/
9 years ago
iguana_blockQ("hdr1",coin,0,-1,blockhashes[1],1);
9 years ago
}
else
{
9 years ago
if ( (block= iguana_blockfind("recvhashes2",coin,blockhashes[1])) == 0 )
9 years ago
{
8 years ago
//iguana_blockhashset("recvhashes3",coin,-1,blockhashes[1],1);
9 years ago
if ( (block= iguana_blockfind("recvhashes4",coin,blockhashes[1])) != 0 )
iguana_blockQ("recvhash6",coin,0,-6,blockhashes[1],1); // should be RT block
9 years ago
}
9 years ago
if ( block != 0 )
block->newtx = 1;
8 years ago
iguana_blockQ("RTblock",coin,0,-7,blockhashes[1],1); // should be RT block
9 years ago
}
9 years ago
if ( strcmp("BTC",coin->symbol) != 0 )
9 years ago
{
8 years ago
//iguana_blockQ("recvhash7",coin,0,-7,blockhashes[1],1);
9 years ago
iguana_blockQ("recvhash7",coin,0,-7,blockhashes[num-1],1);
8 years ago
if ( 1 && coin->RTheight > 0 )
8 years ago
{
8 years ago
for (i=1; i<num; i++)
8 years ago
{
8 years ago
if ( iguana_bundlehash2_check(coin,blockhashes[i]) == 0 )
8 years ago
{
8 years ago
//fprintf(stderr,"%d ",i);
8 years ago
iguana_blockQ("recvhashRT",coin,0,-8,blockhashes[i],1);
8 years ago
//iguana_sendblockreqPT(coin,0,0,-1,blockhashes[i],0);
8 years ago
}
8 years ago
}
8 years ago
}
}
9 years ago
return(req);
}
8 years ago
struct iguana_bundlereq *iguana_recvblock(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_peer *addr,struct iguana_bundlereq *req,struct iguana_zblock *origblock,int32_t numtx,int32_t datalen,int32_t recvlen,int32_t *newhwmp)
9 years ago
{
9 years ago
struct iguana_bundle *bp=0,*prev; int32_t n,bundlei = -2; struct iguana_block *block,*next,*prevblock; char str[65]; bits256 hash2;
8 years ago
if ( (block= iguana_blockfind("recv",coin,origblock->RO.hash2)) != 0 )
8 years ago
iguana_blockcopy(0*coin->chain->zcash,coin->chain->auxpow,coin,block,(struct iguana_block *)origblock);
8 years ago
else if ( (block= iguana_blockhashset("recvblock",coin,-1,origblock->RO.hash2,1)) == 0 )
{
printf("error adding %s\n",bits256_str(str,origblock->RO.hash2));
return(req);
}
8 years ago
else block->txvalid = block->valid = 1;
9 years ago
if ( bits256_nonz(origblock->RO.prev_block) != 0 )
9 years ago
{
8 years ago
if ( (prevblock= iguana_blockfind("prev",coin,origblock->RO.prev_block)) != 0 )
{
if ( prevblock->height+1 > coin->longestchain )
coin->longestchain = prevblock->height+1;
} else iguana_blockQ("prev",coin,0,-1,origblock->RO.prev_block,1);
9 years ago
}
8 years ago
if ( (0) && block != 0 )
8 years ago
printf("%s received.(%s) [%d:%d]\n",coin->symbol,bits256_str(str,origblock->RO.hash2),block->hdrsi,block->bundlei);
8 years ago
if ( (bp= iguana_bundleset(myinfo,coin,&block,&bundlei,(struct iguana_block *)origblock)) != 0 && bp == coin->current && block != 0 && bp->speculative != 0 && bundlei >= 0 )
9 years ago
{
8 years ago
if ( (0) && strcmp("BTCD",coin->symbol) == 0 )
8 years ago
printf("%s received.(%s) %s\n",coin->symbol,bits256_str(str,origblock->RO.hash2),addr->ipaddr);
9 years ago
if ( bp->speculative != 0 && bp->numspec <= bundlei )
{
bp->speculative[bundlei] = block->RO.hash2;
bp->numspec = bundlei+1;
}
8 years ago
while ( bundlei < bp->n && block != 0 && bp->bundleheight+bundlei == coin->blocks.hwmchain.height+1 && _iguana_chainlink(myinfo,coin,block) != 0 )
9 years ago
{
//printf("MAIN.%d ",bp->bundleheight+bundlei);
bundlei++;
block = iguana_bundleblock(coin,&hash2,bp,bundlei);
}
//printf("autoadd [%d:%d]\n",bp->hdrsi,bundlei);
8 years ago
} //else printf("couldnt find.(%s)\n",bits256_str(str,block->RO.hash2));
9 years ago
if ( bp != 0 )
9 years ago
{
9 years ago
bp->dirty++;
9 years ago
if ( block != 0 && block->mainchain != 0 && bundlei == 0 && bp->hdrsi > 0 )
{
if ( (prev= coin->bundles[bp->hdrsi - 1]) != 0 )
{
9 years ago
//printf("found adjacent [%d:%d] speculative.%p\n",prev->hdrsi,bp->n-1,prev->speculative);
9 years ago
}
}
}
9 years ago
else if ( req->copyflag != 0 )
{
8 years ago
if ( bp == 0 && (block == 0 || block->queued == 0) )
9 years ago
{
//fprintf(stderr,"req.%p copyflag.%d data %d %d\n",req,req->copyflag,req->recvlen,recvlen);
coin->numcached++;
if ( block != 0 )
block->queued = 1;
8 years ago
queue_enqueue("cacheQ",&coin->cacheQ,&req->DL);
9 years ago
return(0);
}
else if ( block != 0 && block->req == 0 )
{
block->req = req;
req = 0;
} //else printf("already have cache entry.(%s)\n",bits256_str(str,origblock->RO.hash2));
}
9 years ago
if ( block != 0 )//&& bp != 0 && bp->hdrsi == coin->bundlescount-1 )
9 years ago
{
9 years ago
int32_t i,numsaved = 0; struct iguana_block *tmpblock; static int32_t numrecv;
9 years ago
numrecv++;
9 years ago
if ( bp != 0 )
9 years ago
{
for (i=numsaved=0; i<bp->n; i++)
if ( (tmpblock= bp->blocks[i]) != 0 && tmpblock->fpipbits != 0 && tmpblock->fpos >= 0 && ((bp->hdrsi == 0 && i == 0) || bits256_nonz(tmpblock->RO.prev_block) != 0) )
numsaved++;
}
8 years ago
// fprintf(stderr,"%s [%d:%d] block.%x | s.%d r.%d copy.%d mainchain.%d\n",bits256_str(str,origblock->RO.hash2),bp!=0?bp->hdrsi:-1,bundlei,block!=0?block->fpipbits:0,numsaved,numrecv,req!=0?req->copyflag:-1,block->mainchain);
8 years ago
if ( _iguana_chainlink(myinfo,coin,block) == 0 )
9 years ago
{
next = block;
9 years ago
for (i=n=0; i<coin->chain->bundlesize && n < 60; i++)
9 years ago
{
if ( (block= iguana_blockfind("recvblock",coin,block->RO.prev_block)) == 0 )
break;
8 years ago
if ( block->mainchain != 0 || _iguana_chainlink(myinfo,coin,block) != 0 )
9 years ago
{
8 years ago
_iguana_chainlink(myinfo,coin,next);
9 years ago
n++;
9 years ago
break;
}
next = block;
}
9 years ago
} // else printf("RECV MAINCHAIN.%d\n",coin->blocks.hwmchain.height);
9 years ago
}
8 years ago
if ( (0) && time(NULL) > bp->hdrtime+3 && bundlei == 1 && bp != 0 && bp->numhashes < bp->n && strcmp("BTC",coin->symbol) != 0 && bp->speculative == 0 && bp == coin->current )
9 years ago
{
9 years ago
printf("reissue hdrs request for [%d]\n",bp->hdrsi);
8 years ago
bp->hdrtime = (uint32_t)time(NULL);
8 years ago
queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(bits256_str(str,bp->hashes[0])));
9 years ago
}
9 years ago
if ( (block= iguana_blockhashset("recvblock",coin,-1,origblock->RO.hash2,1)) != 0 )
9 years ago
{
if ( block != (struct iguana_block *)origblock )
8 years ago
iguana_blockcopy(0*coin->chain->zcash,coin->chain->auxpow,coin,block,(struct iguana_block *)origblock);
9 years ago
if ( block->lag != 0 && block->issued != 0 )
block->lag = (uint32_t)time(NULL) - block->issued;
9 years ago
//printf("datalen.%d ipbits.%x\n",datalen,req->ipbits);
9 years ago
} else printf("cant create origblock.%p block.%p bp.%p bundlei.%d\n",origblock,block,bp,bundlei);
9 years ago
return(req);
}
struct iguana_bundlereq *iguana_recvtxids(struct iguana_info *coin,struct iguana_bundlereq *req,bits256 *txids,int32_t n)
{
9 years ago
int32_t i;
9 years ago
if ( n > 0 )
9 years ago
{
9 years ago
for (i=0; i<n; i++)
iguana_txidreport(coin,txids[i],req->addr);
9 years ago
}
9 years ago
return(req);
}
struct iguana_bundlereq *iguana_recvunconfirmed(struct iguana_info *coin,struct iguana_bundlereq *req,uint8_t *data,int32_t datalen)
{
int32_t i;
for (i=0; i<coin->numreqtxids; i++)
{
if ( memcmp(req->txid.bytes,coin->reqtxids[i].bytes,sizeof(req->txid)) == 0 )
{
char str[65]; printf("got reqtxid.%s datalen.%d | numreqs.%d\n",bits256_str(str,req->txid),req->datalen,coin->numreqtxids);
coin->reqtxids[i] = coin->reqtxids[--coin->numreqtxids];
}
}
return(req);
}
9 years ago
int32_t iguana_blockreq(struct iguana_info *coin,int32_t height,int32_t priority)
{
int32_t hdrsi,bundlei; struct iguana_bundle *bp;
hdrsi = height / coin->chain->bundlesize;
bundlei = height % coin->chain->bundlesize;
if ( (bp= coin->bundles[hdrsi]) != 0 && bits256_nonz(bp->hashes[bundlei]) != 0 )
{
9 years ago
iguana_blockQ("blockreq",coin,bp,bundlei,bp->hashes[bundlei],priority);
9 years ago
return(height);
}
return(-1);
}
8 years ago
int32_t iguana_reqblocks(struct supernet_info *myinfo,struct iguana_info *coin)
9 years ago
{
9 years ago
int32_t hdrsi,lflag,bundlei,iters=0,flag = 0; bits256 hash2; struct iguana_block *next,*block; struct iguana_bundle *bp;
8 years ago
if ( (block= iguana_blockfind("hwmcheck",coin,coin->blocks.hwmchain.RO.hash2)) == 0 || block->mainchain == 0 || block->height != coin->blocks.hwmchain.height )
{
8 years ago
//printf("HWM %s mismatch ht.%d vs %d or not mainchain.%d\n",coin->symbol,block->height,coin->blocks.hwmchain.height,block->mainchain);
8 years ago
if ( coin->blocks.hwmchain.height > 0 )
{
if ( (block= iguana_blockfind("hwmcheckb",coin,coin->blocks.hwmchain.RO.prev_block)) != 0 )
{
8 years ago
iguana_blockzcopy(0*coin->chain->zcash,(struct iguana_block *)&coin->blocks.hwmchain,block);
8 years ago
return(0);
}
}
}
9 years ago
if ( time(NULL) < coin->lastreqtime+2 )
return(0);
coin->lastreqtime = (uint32_t)time(NULL);
9 years ago
//printf("reqblocks %u\n",coin->lastreqtime);
8 years ago
hdrsi = (coin->blocks.hwmchain.height + 1) / coin->chain->bundlesize;
9 years ago
if ( (bp= coin->bundles[hdrsi]) != 0 )
{
8 years ago
/*for (bundlei=0; bundlei<coin->chain->bundlesize; bundlei++)
9 years ago
if ( (block= bp->blocks[bundlei]) != 0 && bits256_cmp(block->RO.hash2,bp->hashes[bundlei]) != 0 && bits256_nonz(bp->hashes[bundlei]) != 0 )
{
char str[65]; printf("%s [%d] bundlei.%d ht.%d vs expected %d\n",bits256_str(str,bp->hashes[bundlei]),hdrsi,bundlei,block->height,bp->bundleheight+bundlei);
bp->blocks[bundlei] = iguana_blockfind("fixit",coin,bp->hashes[bundlei]);
8 years ago
}*/
9 years ago
bundlei = (coin->blocks.hwmchain.height+1) % coin->chain->bundlesize;
9 years ago
if ( (next= bp->blocks[bundlei]) != 0 || (next= iguana_blockfind("reqblocks",coin,bp->hashes[bundlei])) != 0 )
9 years ago
{
if ( bits256_nonz(next->RO.prev_block) > 0 )
8 years ago
_iguana_chainlink(myinfo,coin,next);
9 years ago
else if ( next->queued == 0 && next->fpipbits == 0 && (rand() % 100) == 0 )
{
9 years ago
//printf("HWM next %d\n",coin->blocks.hwmchain.height+1);
9 years ago
iguana_blockQ("reqblocks",coin,bp,bundlei,next->RO.hash2,1);
9 years ago
}
}
}
lflag = 1;
9 years ago
while ( coin->active != 0 && iters < coin->longestchain/3+1 )
9 years ago
{
9 years ago
iters++;
9 years ago
lflag = 0;
hdrsi = (coin->blocks.hwmchain.height+1) / coin->chain->bundlesize;
bundlei = (coin->blocks.hwmchain.height+1) % coin->chain->bundlesize;
9 years ago
if ( 1 )
9 years ago
{
9 years ago
int32_t hdrsi0,bundlei0;
9 years ago
if ( (next= iguana_bundleblock(coin,&hash2,coin->bundles[hdrsi],bundlei)) == 0 )
{
9 years ago
hdrsi0 = (coin->blocks.hwmchain.height) / coin->chain->bundlesize;
bundlei0 = (coin->blocks.hwmchain.height) % coin->chain->bundlesize;
9 years ago
if ( coin->bundles[hdrsi0] != 0 && (block= iguana_bundleblock(coin,&hash2,coin->bundles[hdrsi0],bundlei0)) != 0 )
9 years ago
next = block->hh.next; //, next/block->mainchain = 1;
}
}
else
{
if ( (next= iguana_blockfind("reqloop",coin,iguana_blockhash(coin,coin->blocks.hwmchain.height+1))) == 0 )
{
}
9 years ago
}
if ( next == 0 && hdrsi < coin->bundlescount && (bp= coin->bundles[hdrsi]) != 0 && (next= bp->blocks[bundlei]) != 0 )
{
if ( bits256_nonz(next->RO.prev_block) == 0 )
{
printf(" next has null prev [%d:%d]\n",bp->hdrsi,bundlei);
9 years ago
iguana_blockQ("reqblocks0",coin,bp,bundlei,next->RO.hash2,1);
9 years ago
next = 0;
}
}
9 years ago
if ( next != 0 )//&& time(NULL) > coin->nextchecked+10 )
9 years ago
{
//printf("have next %d\n",coin->blocks.hwmchain.height);
if ( memcmp(next->RO.prev_block.bytes,coin->blocks.hwmchain.RO.hash2.bytes,sizeof(bits256)) == 0 )
{
9 years ago
coin->nextchecked = (uint32_t)time(NULL);
8 years ago
if ( _iguana_chainlink(myinfo,coin,next) != 0 )
9 years ago
lflag++, flag++;
//else printf("chainlink error for %d\n",coin->blocks.hwmchain.height+1);
}
9 years ago
}
if ( 1 )//queue_size(&coin->blocksQ) < _IGUANA_MAXPENDING )
{
double threshold,lag = OS_milliseconds() - coin->backstopmillis;
9 years ago
bp = coin->bundles[(coin->blocks.hwmchain.height+1)/coin->chain->bundlesize];
if ( bp != 0 && bp->durationscount != 0 )
threshold = (double)bp->totaldurations / bp->durationscount;
else
{
if ( coin->blocks.hwmchain.height >= coin->longestchain-1 )
9 years ago
threshold = 10000;
else threshold = 5000;
9 years ago
if ( strcmp(coin->symbol,"BTC") == 0 )
9 years ago
threshold *= 3;
9 years ago
}
9 years ago
if ( threshold < 1500 )
threshold = 1500;
9 years ago
if ( coin->blocks.hwmchain.height < coin->longestchain && ((strcmp(coin->symbol,"BTC") != 0 && coin->backstop != coin->blocks.hwmchain.height+1) || lag > threshold) )
9 years ago
{
9 years ago
coin->backstop = coin->blocks.hwmchain.height+1;
hash2 = iguana_blockhash(coin,coin->backstop);
bundlei = (coin->blocks.hwmchain.height+1) % coin->chain->bundlesize;
if ( bp != 0 && bits256_nonz(hash2) == 0 )
9 years ago
{
9 years ago
hash2 = bp->hashes[bundlei];
9 years ago
if ( bits256_nonz(hash2) == 0 && bp->speculative != 0 )
9 years ago
{
9 years ago
hash2 = bp->speculative[bundlei];
9 years ago
if ( bits256_nonz(hash2) > 0 )
{
9 years ago
if ( (block= iguana_blockfind("reqblocks",coin,hash2)) != 0 && bits256_cmp(block->RO.prev_block,coin->blocks.hwmchain.RO.hash2) == 0 )
9 years ago
{
9 years ago
//printf("speculative is next at %d\n",coin->backstop);
8 years ago
if ( _iguana_chainlink(myinfo,coin,block) != 0 )
9 years ago
lflag++, flag++;//, printf("NEWHWM.%d\n",coin->backstop);
9 years ago
}
}
9 years ago
}
9 years ago
}
9 years ago
if ( bits256_nonz(hash2) > 0 ) //strcmp("BTC",coin->symbol) == 0 &&
9 years ago
{
9 years ago
coin->backstopmillis = OS_milliseconds();
9 years ago
iguana_blockQ("mainchain",coin,0,-1,hash2,1);//lag > threshold);
9 years ago
flag++;
char str[65];
9 years ago
if ( 1 && (rand() % 100000) == 0 )//|| bp->bundleheight > coin->longestchain-coin->chain->bundlesize )
9 years ago
printf("%s %s MAIN.%d t %.3f lag %.3f\n",coin->symbol,bits256_str(str,hash2),coin->blocks.hwmchain.height+1,threshold,lag);
9 years ago
}
8 years ago
if ( (0) && bp != 0 && bundlei < bp->n-1 && (bits256_nonz(bp->hashes[bundlei+1]) != 0 || (bp->speculative != 0 && bits256_nonz(bp->speculative[bundlei+1]) != 0)) )
9 years ago
{
9 years ago
int32_t j;
9 years ago
//memset(bp->hashes[bundlei].bytes,0,sizeof(bp->hashes[bundlei]));
9 years ago
//bp->blocks[bundlei] = 0;
9 years ago
for (j=0; j<1&&bundlei+j+1<bp->n; j++)
9 years ago
{
9 years ago
if ( time(NULL) > bp->issued[bundlei+1+j]+10 )
{
8 years ago
bp->issued[bundlei+1+j] = 1;//(uint32_t)time(NULL);
9 years ago
printf("MAINCHAIN skip issue %d\n",bp->bundleheight+bundlei+1+j);
if ( bits256_nonz(bp->hashes[bundlei+1+j]) != 0 )
9 years ago
iguana_blockQ("mainskip",coin,bp,bundlei+1+j,bp->hashes[bundlei+1+j],1);
9 years ago
else if ( bp->speculative != 0 && bundlei+1+j < bp->numspec )
9 years ago
iguana_blockQ("mainskip",coin,bp,bundlei+1+j,bp->speculative[bundlei+1+j],1);
9 years ago
}
9 years ago
}
9 years ago
}
9 years ago
}
}
}
return(flag);
}
8 years ago
int32_t iguana_processrecvQ(struct supernet_info *myinfo,struct iguana_info *coin,int32_t *newhwmp) // single threaded
9 years ago
{
8 years ago
int32_t flag = 0; struct iguana_bundlereq *req;
9 years ago
*newhwmp = 0;
8 years ago
while ( flag < IGUANA_MAXITERATIONS && coin->active != 0 && (req= queue_dequeue(&coin->recvQ)) != 0 )
9 years ago
{
8 years ago
if ( req->type != 'H' )
flag++;
9 years ago
//fprintf(stderr,"%s flag.%d %s recvQ.%p type.%c n.%d\n",coin->symbol,flag,req->addr != 0 ? req->addr->ipaddr : "0",req,req->type,req->n);
9 years ago
if ( req->type == 'B' ) // one block with all txdata
9 years ago
{
9 years ago
netBLOCKS--;
8 years ago
req = iguana_recvblock(myinfo,coin,req->addr,req,&req->zblock,req->numtx,req->datalen,req->recvlen,newhwmp);
9 years ago
}
9 years ago
else if ( req->type == 'H' ) // blockhdrs (doesnt have txn_count!)
{
9 years ago
HDRnet--;
8 years ago
if ( (req= iguana_recvblockhdrs(myinfo,coin,req,req->blocks,req->n,newhwmp)) != 0 )
9 years ago
{
if ( req->blocks != 0 )
myfree(req->blocks,sizeof(struct iguana_zblock) * req->n), req->blocks = 0;
9 years ago
}
}
else if ( req->type == 'S' ) // blockhashes
{
8 years ago
if ( (req= iguana_recvblockhashes(myinfo,coin,req,req->hashes,req->n)) != 0 && req->hashes != 0 )
9 years ago
myfree(req->hashes,sizeof(*req->hashes) * req->n), req->hashes = 0;
}
else if ( req->type == 'U' ) // unconfirmed tx
req = iguana_recvunconfirmed(coin,req,req->serializeddata,req->datalen);
9 years ago
else if ( req->type == 'T' ) // txids from inv
{
if ( (req= iguana_recvtxids(coin,req,req->hashes,req->n)) != 0 )
myfree(req->hashes,(req->n+1) * sizeof(*req->hashes)), req->hashes = 0;
}
9 years ago
/*else if ( req->type == 'Q' ) // quotes from inv
9 years ago
{
9 years ago
if ( (req= instantdex_recvquotes(coin,req,req->hashes,req->n)) != 0 )
9 years ago
myfree(req->hashes,(req->n+1) * sizeof(*req->hashes)), req->hashes = 0;
9 years ago
}*/
9 years ago
else printf("iguana_updatebundles unknown type.%c\n",req->type);//, getchar();
9 years ago
//fprintf(stderr,"finished coin->recvQ\n");
9 years ago
if ( req != 0 )
myfree(req,req->allocsize), req = 0;
8 years ago
}
9 years ago
return(flag);
}
int32_t iguana_needhdrs(struct iguana_info *coin)
{
8 years ago
//if ( coin->longestchain == 0 || coin->blocks.hashblocks < coin->longestchain-coin->chain->bundlesize )
9 years ago
return(1);
8 years ago
//else return(0);
9 years ago
}
int32_t iguana_reqhdrs(struct iguana_info *coin)
{
8 years ago
int32_t i,lag,n = 0; struct iguana_bundle *bp; char hashstr[65]; uint32_t now = (uint32_t)time(NULL);
8 years ago
//if ( queue_size(&coin->hdrsQ) == 0 )
9 years ago
{
9 years ago
if ( coin->active != 0 )
9 years ago
{
for (i=0; i<coin->bundlescount; i++)
{
8 years ago
if ( (bp= coin->bundles[i]) != 0 && (bp == coin->current || bp->hdrsi == coin->blocks.hwmchain.height/coin->chain->bundlesize || i == coin->bundlescount-1 || bp->numhashes < bp->n) )
9 years ago
{
9 years ago
if ( bp == coin->current )
8 years ago
lag = 13;
8 years ago
else if ( coin->current == 0 || bp->hdrsi > coin->current->hdrsi+coin->MAXBUNDLES )
continue;
8 years ago
else lag = 30;
8 years ago
if ( now > bp->issuetime+lag && now > bp->hdrtime+3 )
9 years ago
{
8 years ago
bp->hdrtime = now;
8 years ago
if ( (0) && bp == coin->current )
8 years ago
printf("LAG.%d hdrsi.%d numhashes.%d:%d needhdrs.%d qsize.%d zcount.%d\n",(uint32_t)(now-bp->hdrtime),i,bp->numhashes,bp->n,iguana_needhdrs(coin),queue_size(&coin->hdrsQ),coin->zcount);
9 years ago
if ( bp->issuetime == 0 )
coin->numpendings++;
init_hexbytes_noT(hashstr,bp->hashes[0].bytes,sizeof(bits256));
8 years ago
queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr));
8 years ago
if ( bp == coin->current )
8 years ago
{
8 years ago
init_hexbytes_noT(hashstr,bp->hashes[0].bytes,sizeof(bits256));
8 years ago
queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr));
8 years ago
//printf("%s issue HWM HDRS [%d] %s\n",coin->symbol,bp->hdrsi,hashstr);
8 years ago
if ( coin->blocks.hwmchain.height > 10 )
{
bits256 hash2 = iguana_blockhash(coin,coin->blocks.hwmchain.height-10);
init_hexbytes_noT(hashstr,hash2.bytes,sizeof(bits256));
//printf("%s issue HWM HDRS %d-10 %s\n",coin->symbol,coin->blocks.hwmchain.height,hashstr);
8 years ago
queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr));
8 years ago
}
8 years ago
}
9 years ago
//printf("hdrsi.%d reqHDR.(%s) numhashes.%d\n",bp->hdrsi,hashstr,bp->numhashes);
9 years ago
if ( 1 )
9 years ago
{
9 years ago
iguana_blockQ("reqhdrs0",coin,bp,0,bp->hashes[0],1);
9 years ago
if ( bits256_nonz(bp->hashes[1]) > 0 )
9 years ago
iguana_blockQ("reqhdrs1",coin,bp,1,bp->hashes[1],1);
9 years ago
}
9 years ago
n++;
bp->hdrtime = bp->issuetime = (uint32_t)time(NULL);
}
}
}
8 years ago
if ( (0) && n > 0 )
9 years ago
printf("REQ HDRS pending.%d\n",n);
coin->zcount = 0;
}
9 years ago
} //else coin->zcount = 0;
9 years ago
return(n);
}
9 years ago
int32_t iguana_blockQ(char *argstr,struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2,int32_t priority)
9 years ago
{
9 years ago
queue_t *Q; char *str; uint32_t now; int32_t n,height = -1; struct iguana_blockreq *req,*ptr; struct iguana_block *block = 0;
9 years ago
if ( bits256_nonz(hash2) == 0 )
{
9 years ago
//printf("%s.cant queue zerohash bundlei.%d\n",argstr,bundlei);
9 years ago
//getchar();
9 years ago
return(-1);
}
8 years ago
if ( (0) && coin->enableCACHE != 0 && iguana_speculativesearch(coin,&block,hash2) != 0 && block != 0 && block->txvalid != 0 )
9 years ago
{
//printf("found valid [%d:%d] in blockQ\n",block!=0?block->hdrsi:-1,block!=0?block->bundlei:-1);
9 years ago
return(0);
9 years ago
}
9 years ago
//lag = (priority == 0) ? IGUANA_DEFAULTLAG*3 : IGUANA_DEFAULTLAG;
9 years ago
now = (uint32_t)time(NULL);
9 years ago
block = iguana_blockfind("blockQ",coin,hash2);
9 years ago
if ( priority != 0 || block == 0 )//|| iguana_blockstatus(coin,block) == 0 )
9 years ago
{
9 years ago
if ( bp != 0 )
9 years ago
{
9 years ago
if ( bits256_cmp(coin->APIblockhash,hash2) != 0 && bp->emitfinish != 0 )
return(0);
9 years ago
//if ( now < bp->issued[bundlei]+lag )
// return(0);
9 years ago
if ( bundlei >= 0 && bundlei < bp->n )
{
if ( block == 0 )
block = bp->blocks[bundlei];
height = bp->bundleheight + bundlei;
}
else
{
if ( priority == 0 && -bundlei >= 0 && -bundlei < bp->n && bp->speculative != 0 )
{
if ( bp->speculativecache[-bundlei] != 0 )
return(0);
}
bp = 0;
bundlei = -1;
}
9 years ago
}
9 years ago
if ( block != 0 )
9 years ago
{
9 years ago
if ( bits256_cmp(coin->APIblockhash,hash2) != 0 && (block->fpipbits != 0 || block->req != 0 || block->queued != 0) )
{
if ( block->fpipbits == 0 && block->queued == 0 && block->req != 0 )
{
block->queued = 1;
8 years ago
queue_enqueue("cacheQ",&coin->cacheQ,&block->req->DL);
9 years ago
block->req = 0;
9 years ago
//char str2[65]; printf("already have.(%s)\n",bits256_str(str2,block->RO.hash2));
9 years ago
}
9 years ago
return(0);
9 years ago
}
9 years ago
if ( block->queued != 0 || block->txvalid != 0 )//|| now < block->issued+lag )
9 years ago
return(0);
9 years ago
height = block->height;
9 years ago
}
8 years ago
if ( bp != 0 )
{
if ( bp->emitfinish != 0 )
return(0);
if ( bp != coin->current && coin->RTheight == 0 && bp->issued[bundlei] > 0 )
8 years ago
return(0);
}
9 years ago
if ( priority != 0 )
str = "priorityQ", Q = &coin->priorityQ;
else str = "blocksQ", Q = &coin->blocksQ;
if ( Q != 0 )
{
9 years ago
req = mycalloc('y',1,sizeof(*req));
9 years ago
req->hash2 = hash2;
9 years ago
if ( (req->bp= bp) != 0 && bundlei >= 0 )
9 years ago
{
9 years ago
height = bp->bundleheight + bundlei;
9 years ago
bp->issued[bundlei] = 1;
9 years ago
}
8 years ago
req->height = -1; //height;
req->bundlei = -1; //bundlei;
9 years ago
char str2[65];
9 years ago
//printf("%s %s %s [%d:%d] %d %s %d numranked.%d qsize.%d\n",coin->symbol,argstr,str,bp!=0?bp->hdrsi:-1,bundlei,req->height,bits256_str(str2,hash2),coin->blocks.recvblocks,coin->peers->numranked,queue_size(Q));
9 years ago
if ( (n= queue_size(Q)) > 100000 )
9 years ago
{
9 years ago
if ( 1 && n > 200000 )
9 years ago
printf("%s %s %s [%d:%d] %d %s %d numranked.%d qsize.%d\n",coin->symbol,argstr,str,bp!=0?bp->hdrsi:-1,bundlei,req->height,bits256_str(str2,hash2),coin->blocks.recvblocks,coin->peers != 0 ? coin->peers->numranked : -1,queue_size(Q));
8 years ago
while ( (ptr= queue_dequeue(Q)) != 0 )
9 years ago
myfree(ptr,sizeof(*ptr));
9 years ago
coin->backlog = n*10 + 1000000;
} else coin->backlog >>= 1;
9 years ago
if ( block != 0 )
{
9 years ago
//block->numrequests++;
9 years ago
block->issued = now;
9 years ago
}
8 years ago
queue_enqueue(str,Q,&req->DL);
9 years ago
return(1);
} else printf("null Q\n");
9 years ago
} //else printf("queueblock skip priority.%d bundlei.%d\n",bundlei,priority);
9 years ago
return(0);
}
int32_t iguana_pollQsPT(struct iguana_info *coin,struct iguana_peer *addr)
{
uint8_t serialized[sizeof(struct iguana_msghdr) + sizeof(uint32_t)*32 + sizeof(bits256)];
9 years ago
struct iguana_block *block; struct iguana_blockreq *req=0; char *hashstr=0; bits256 hash2;
8 years ago
int32_t bundlei,priority,i,m=0,z,pend,limit,height=-1,datalen,flag = 0; struct stritem *hashitem;
9 years ago
uint32_t now; struct iguana_bundle *bp; struct iguana_peer *ptr;
9 years ago
if ( addr->msgcounts.verack == 0 )
9 years ago
return(0);
9 years ago
//if ( netBLOCKS > IGUANA_NUMHELPERS*1000 )
// usleep(netBLOCKS);
9 years ago
now = (uint32_t)time(NULL);
9 years ago
if ( iguana_needhdrs(coin) != 0 && addr->pendhdrs < IGUANA_MAXPENDHDRS )
9 years ago
{
//printf("%s check hdrsQ\n",addr->ipaddr);
8 years ago
if ( (hashitem= queue_dequeue(&coin->hdrsQ)) != 0 )
9 years ago
{
8 years ago
hashstr = hashitem->str;
9 years ago
if ( (datalen= iguana_gethdrs(coin,serialized,coin->chain->gethdrsmsg,hashstr)) > 0 )
{
decode_hex(hash2.bytes,sizeof(hash2),hashstr);
if ( bits256_nonz(hash2) > 0 )
{
9 years ago
bp = 0, bundlei = -2;
9 years ago
bp = iguana_bundlefind(coin,&bp,&bundlei,hash2);
9 years ago
z = m = 0;
9 years ago
if ( bp != 0 )
9 years ago
{
if ( bp->bundleheight+coin->chain->bundlesize < coin->longestchain )
{
m = (coin->longestchain - bp->bundleheight);
if ( bp->numhashes < m )
z = 1;
}
9 years ago
else if ( bp->numhashes < bp->n )
9 years ago
z = 1;
}
9 years ago
if ( bp == 0 || bp->speculative == 0 || bp == coin->current || bp->hdrsi == coin->bundlescount-1 || bp->numhashes < bp->n )
9 years ago
{
8 years ago
if ( (0) && bp == coin->current )
9 years ago
printf("%s request HDR.(%s) numhashes.%d [%d]\n",addr!=0?addr->ipaddr:"local",hashstr,bp!=0?bp->numhashes:0,bp!=0?bp->hdrsi:-1);
9 years ago
iguana_send(coin,addr,serialized,datalen);
addr->pendhdrs++;
flag++;
9 years ago
} //else printf("skip hdrreq.%s m.%d z.%d bp.%p longest.%d queued.%d\n",hashstr,m,z,bp,bp->coin->longestchain,bp->queued);
9 years ago
}
9 years ago
//free_queueitem(hashstr);
//return(flag);
9 years ago
} else printf("datalen.%d from gethdrs\n",datalen);
8 years ago
free(hashitem);
9 years ago
hashstr = 0;
8 years ago
hashitem = 0;
9 years ago
}
}
9 years ago
//if ( netBLOCKS > coin->MAXPEERS*coin->MAXPENDING )
// usleep(netBLOCKS);
9 years ago
if ( (limit= addr->recvblocks) > coin->MAXPENDINGREQUESTS )
limit = coin->MAXPENDINGREQUESTS;
9 years ago
if ( limit < 1 )
limit = 1;
9 years ago
if ( addr->pendblocks >= limit )
{
//printf("%s %d overlimit.%d\n",addr->ipaddr,addr->pendblocks,limit);
return(0);
}
9 years ago
priority = 1;
9 years ago
pend = 0;
8 years ago
req = queue_dequeue(&coin->priorityQ);
9 years ago
if ( flag == 0 && req == 0 && addr->pendblocks < limit )
{
priority = 0;
9 years ago
if ( coin->peers != 0 )
9 years ago
{
9 years ago
for (i=m=pend=0; i<coin->peers->numranked; i++)
{
if ( (ptr= coin->peers->ranked[i]) != 0 && ptr->msgcounts.verack > 0 )
pend += ptr->pendblocks, m++;
}
9 years ago
}
9 years ago
if ( pend < coin->MAXPENDINGREQUESTS*m )
8 years ago
req = queue_dequeue(&coin->blocksQ);
9 years ago
}
9 years ago
if ( req != 0 )
{
hash2 = req->hash2;
height = req->height;
9 years ago
if ( (bp= req->bp) != 0 && req->bundlei >= 0 && req->bundlei < bp->n )
9 years ago
{
if ( bp->emitfinish != 0 )
{
9 years ago
//printf("skip emitting bundle [%d:%d]\n",bp->hdrsi,req->bundlei);
8 years ago
//free(req);
9 years ago
return(0);
}
9 years ago
block = bp->blocks[req->bundlei];
9 years ago
} else block = 0;
9 years ago
if ( priority == 0 && bp != 0 && req->bundlei >= 0 && req->bundlei < bp->n && req->bundlei < coin->chain->bundlesize && block != 0 && (block->fpipbits != 0 || block->queued != 0) )
9 years ago
{
9 years ago
if ( 1 && priority != 0 )
9 years ago
printf("SKIP %p[%d] %d\n",bp,bp!=0?bp->bundleheight:-1,req->bundlei);
}
else
{
9 years ago
//if ( block != 0 )
// block->numrequests++;
9 years ago
iguana_sendblockreqPT(coin,addr,bp,req->bundlei,hash2,0);
9 years ago
}
flag++;
8 years ago
free(req);
9 years ago
}
return(flag);
}
int32_t iguana_processrecv(struct supernet_info *myinfo,struct iguana_info *coin) // single threaded
9 years ago
{
9 years ago
int32_t newhwm = 0,hwmheight,flag = 0; char str[2000];
9 years ago
hwmheight = coin->blocks.hwmchain.height;
9 years ago
coin->RTramchain_busy = 1;
8 years ago
if ( coin->balanceflush != 0 && coin->longestchain > coin->chain->bundlesize )
9 years ago
{
9 years ago
fprintf(stderr,"%s call balanceflush\n",coin->symbol);
9 years ago
//portable_mutex_lock(&coin->RTmutex);
8 years ago
coin->disableUTXO = 1;
8 years ago
//iguana_utxoupdate(coin,-1,0,0,0,0,-1,0); // free hashtables
9 years ago
if ( iguana_balanceflush(myinfo,coin,coin->balanceflush) > 0 )
9 years ago
printf("balanceswritten.%d flushed coin->balanceflush %d vs %d coin->longestchain/coin->chain->bundlesize\n",coin->balanceswritten,coin->balanceflush,coin->longestchain/coin->chain->bundlesize);
9 years ago
//portable_mutex_unlock(&coin->RTmutex);
8 years ago
coin->disableUTXO = 0;
9 years ago
fprintf(stderr,"%s back balanceflush\n",coin->symbol);
9 years ago
coin->balanceflush = 0;
8 years ago
//iguana_utxoaddr_gen(myinfo,coin,(coin->balanceswritten - 1) * coin->chain->bundlesize);
9 years ago
}
8 years ago
if ( (rand() % 10) == 0 )
8 years ago
{
8 years ago
if ( coin->utxoaddrtable != 0 && coin->RTheight > 0 && coin->RTheight <= coin->blocks.hwmchain.height )
{
struct iguana_block *block;
if ( (block= iguana_blockfind("utxogen",coin,coin->blocks.hwmchain.RO.hash2)) != 0 )
iguana_RTnewblock(myinfo,coin,block);
}
8 years ago
}
8 years ago
flag += iguana_processrecvQ(myinfo,coin,&newhwm);
8 years ago
if ( coin->RTheight == 0 || (rand() % 7) == 0 )
8 years ago
flag += iguana_reqblocks(myinfo,coin);
8 years ago
if ( time(NULL) > coin->laststats+3 )
9 years ago
{
8 years ago
flag += iguana_reqhdrs(coin);
8 years ago
iguana_bundlestats(myinfo,coin,str,IGUANA_DEFAULTLAG);
8 years ago
coin->laststats = (uint32_t)time(NULL);
9 years ago
}
8 years ago
//iguana_realtime_update(myinfo,coin);
coin->RTramchain_busy = 0;
flag += iguana_process_msgrequestQ(myinfo,coin);
9 years ago
if ( hwmheight != coin->blocks.hwmchain.height )
flag = 1;
9 years ago
return(flag);
}