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.

679 lines
29 KiB

9 years ago
/******************************************************************************
8 years ago
* Copyright © 2014-2017 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"
void iguana_initQ(queue_t *Q,char *name)
{
8 years ago
struct stritem *tst,*item;
9 years ago
memset(Q,0,sizeof(*Q));
8 years ago
item = calloc(1,sizeof(*item)+100);
strcpy(item->str,"hello, world");
9 years ago
strcpy(Q->name,name);
8 years ago
queue_enqueue(name,Q,&item->DL);
if ( (tst= queue_dequeue(Q)) != 0 )
free(tst);
9 years ago
}
void iguana_initQs(struct iguana_info *coin)
{
9 years ago
int32_t i;
9 years ago
iguana_initQ(&coin->acceptQ,"acceptQ");
9 years ago
iguana_initQ(&coin->hdrsQ,"hdrsQ");
iguana_initQ(&coin->blocksQ,"blocksQ");
iguana_initQ(&coin->priorityQ,"priorityQ");
iguana_initQ(&coin->possibleQ,"possibleQ");
9 years ago
iguana_initQ(&coin->msgrequestQ,"msgrequestQ");
9 years ago
iguana_initQ(&coin->cacheQ,"cacheQ");
9 years ago
iguana_initQ(&coin->recvQ,"recvQ");
iguana_initQ(&coin->jsonQ,"jsonQ");
iguana_initQ(&coin->finishedQ,"finishedQ");
9 years ago
if ( coin->MAXPEERS > 0 && coin->peers != 0 )
9 years ago
{
for (i=0; i<IGUANA_MAXPEERS; i++)
iguana_initQ(&coin->peers->active[i].sendQ,"addrsendQ");
}
9 years ago
}
9 years ago
void iguana_initpeer(struct iguana_info *coin,struct iguana_peer *addr,uint64_t ipbits)
9 years ago
{
memset(addr,0,sizeof(*addr));
addr->ipbits = ipbits;
addr->usock = -1;
9 years ago
expand_ipbits(addr->ipaddr,(uint32_t)addr->ipbits);
9 years ago
//addr->pending = (uint32_t)time(NULL);
strcpy(addr->symbol,coin->symbol);
strcpy(addr->coinname,coin->name);
9 years ago
iguana_initQ(&addr->sendQ,"addrsendQ");
}
9 years ago
void iguana_initcoin(struct iguana_info *coin,cJSON *argjson)
9 years ago
{
9 years ago
int32_t i; char dirname[1024];
if ( coin->instance_nonce == 0 )
9 years ago
{
sprintf(dirname,"%s/%s",GLOBAL_TMPDIR,coin->symbol), OS_portable_path(dirname);
9 years ago
portable_mutex_init(&coin->RTmutex);
8 years ago
portable_mutex_init(&coin->kmdmutex);
portable_mutex_init(&coin->peers_mutex);
portable_mutex_init(&coin->blocks_mutex);
8 years ago
portable_mutex_init(&coin->special_mutex);
portable_mutex_init(&coin->allcoins_mutex);
coin->txfee = 10000;
iguana_meminit(&coin->blockMEM,"blockMEM",coin->blockspace,coin->blockspacesize,0);
iguana_initQs(coin);
coin->bindsock = -1;
8 years ago
coin->notarychain = -1;
OS_randombytes((unsigned char *)&coin->instance_nonce,sizeof(coin->instance_nonce));
coin->startutc = (uint32_t)time(NULL);
while ( time(NULL) == coin->startutc )
usleep(1);
coin->startutc++;
printf("start.%u\n",coin->startutc);
coin->startmillis = OS_milliseconds(), coin->starttime = tai_now();
coin->avetime = 1 * 100;
//coin->R.maxrecvbundles = IGUANA_INITIALBUNDLES;
9 years ago
if ( coin->MAXPEERS > 0 && coin->peers != 0 )
{
for (i=0; i<IGUANA_MAXPEERS; i++)
coin->peers->active[i].usock = -1;
}
9 years ago
}
9 years ago
}
8 years ago
bits256 iguana_genesis(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_chain *chain)
9 years ago
{
struct iguana_block *block,*ptr; struct iguana_msgzblock zmsg; bits256 hash2; char str[65],str2[65]; uint8_t buf[8192],blockspace[sizeof(*block)+sizeof(struct iguana_zblock)]; int32_t height,auxback;
9 years ago
if ( coin == 0 || chain == 0 )
return(GENESIS_PUBKEY);
block = (void *)blockspace;
memset(block,0,sizeof(blockspace));
9 years ago
block->RO.allocsize = chain->zcash != 0 ? sizeof(struct iguana_zblock) : sizeof(struct iguana_block);
iguana_blocksizecheck("genesis",chain->zcash,block);
9 years ago
if ( chain->genesis_hex == 0 )
{
printf("no genesis_hex for %s\n",coin->symbol);
memset(hash2.bytes,0,sizeof(hash2));
return(hash2);
}
9 years ago
decode_hex(buf,(int32_t)strlen(chain->genesis_hex)/2,(char *)chain->genesis_hex);
hash2 = iguana_calcblockhash(coin->symbol,coin->chain->hashalgo,buf,chain->zcash == 0 ? sizeof(struct iguana_msgblockhdr) : sizeof(struct iguana_msgzblockhdr));
auxback = chain->auxpow, chain->auxpow = 0;
iguana_rwblock(myinfo,coin->symbol,coin->chain->zcash,chain->auxpow,chain->hashalgo,0,&hash2,buf,(void *)&zmsg,sizeof(buf));
chain->auxpow = auxback;
if ( coin->virtualchain == 0 && coin->MAXPEERS > 1 )
9 years ago
{
if ( chain->debug == 0 && memcmp(hash2.bytes,chain->genesis_hashdata,sizeof(hash2)) != 0 )
{
bits256_str(str,hash2);
printf("genesis mismatch? zcash.%d calculated %s vs %s\n",chain->zcash,str,bits256_str(str2,*(bits256 *)chain->genesis_hashdata));
memcpy(hash2.bytes,chain->genesis_hashdata,sizeof(hash2));
}
if ( coin->chain->debug != 0 )
memcpy(hash2.bytes,chain->genesis_hashdata,sizeof(hash2));
9 years ago
} else memcpy(hash2.bytes,chain->genesis_hashdata,sizeof(hash2));
9 years ago
bits256_str(str,hash2);
if ( chain->debug != 0 )
printf("genesis.(%s) zcash.%d len.%d hash.%s\n",chain->genesis_hex,chain->zcash,(int32_t)sizeof(zmsg.zH),str);
iguana_blockconv(chain->zcash,chain->auxpow,(void *)block,&zmsg,hash2,0);
block->RO.txn_count = 1;
block->RO.numvouts = 1;
if ( chain->zcash != 0 )
8 years ago
block->RO.allocsize = sizeof(struct iguana_zblock);
else block->RO.allocsize = (int32_t)sizeof(*block);
9 years ago
iguana_gotdata(coin,0,0);
9 years ago
if ( (ptr= iguana_blockhashset("genesis0",coin,0,hash2,1)) != 0 )
9 years ago
{
iguana_blockcopy(chain->zcash,chain->auxpow,coin,ptr,block);
iguana_blocksizecheck("genesis ptr",chain->zcash,ptr);
9 years ago
ptr->mainchain = 1;
ptr->height = 0;
//coin->blocks.RO[0] = block.RO;
8 years ago
if ( coin->virtualchain != 0 || (height= iguana_chainextend(myinfo,coin,ptr)) == 0 )
9 years ago
{
iguana_blockzcopy(chain->zcash,block,ptr);
iguana_blockzcopy(chain->zcash,(void *)&coin->blocks.hwmchain,ptr);
printf("size.%d genesis block PoW %f ptr %f\n",block->RO.allocsize,block->PoW,ptr->PoW);
9 years ago
coin->blocks.recvblocks = coin->blocks.issuedblocks = 1;
9 years ago
} else printf("genesis block doesnt validate for %s ht.%d\n",coin->symbol,height);
9 years ago
} else printf("couldnt hashset genesis\n");
return(hash2);
}
int32_t iguana_savehdrs(struct iguana_info *coin)
{
9 years ago
char fname[512],shastr[65],tmpfname[512],tmpfname2[512],str2[65],str[65],oldfname[512];
8 years ago
bits256 sha256all; FILE *fp=0,*fp2=0; struct iguana_bundle *bp; int32_t hdrsi,n,retval = 0;
9 years ago
n = coin->blocks.hwmchain.height + 1;
9 years ago
sprintf(tmpfname,"%s/%s/hdrs.txt",GLOBAL_TMPDIR,coin->symbol), OS_compatible_path(tmpfname);
sprintf(tmpfname2,"%s/%s/hdrs.h",GLOBAL_TMPDIR,coin->symbol), OS_compatible_path(tmpfname);
sprintf(oldfname,"%s/%s_oldhdrs.txt",GLOBAL_CONFSDIR,coin->symbol), OS_compatible_path(oldfname);
sprintf(fname,"%s/%s_hdrs.txt",GLOBAL_CONFSDIR,coin->symbol), OS_compatible_path(fname);
9 years ago
if ( (fp= fopen(tmpfname,"w")) != 0 )
{
9 years ago
if ( (fp2= fopen(tmpfname2,"w")) != 0 )
fprintf(fp2,"char *%s_hdrs[][4] = {\n",coin->symbol);
9 years ago
fprintf(fp,"%d\n",n);
9 years ago
for (hdrsi=0; hdrsi<coin->bundlescount; hdrsi++)
9 years ago
{
9 years ago
if ( (bp= coin->bundles[hdrsi]) != 0 && bp->numhashes >= bp->n )
9 years ago
{
9 years ago
shastr[0] = 0;
if ( bits256_nonz(bp->allhash) == 0 )
9 years ago
{
9 years ago
vcalc_sha256(shastr,sha256all.bytes,bp->hashes[0].bytes,sizeof(*bp->hashes) * coin->chain->bundlesize);
bp->allhash = sha256all;
9 years ago
}
9 years ago
else
{
sha256all = bp->allhash;
bits256_str(shastr,bp->allhash);
}
fprintf(fp,"%d %s %s %s\n",bp->bundleheight,bits256_str(str,bp->hashes[0]),shastr,bits256_str(str2,bp->hashes[1]));
9 years ago
if ( fp2 != 0 )
fprintf(fp2,"{ \"%d\", \"%s\", \"%s\", \"%s\"},\n",bp->bundleheight,bits256_str(str,bp->hashes[0]),shastr,bits256_str(str2,bp->hashes[1]));
9 years ago
}
else
{
if ( bp != 0 && bits256_nonz(bp->hashes[0]) != 0 )
9 years ago
{
9 years ago
fprintf(fp,"%d %s\n",bp->bundleheight,bits256_str(str,bp->hashes[0]));
9 years ago
if ( fp2 != 0 )
fprintf(fp2,"{ \"%d\", \"%s\", \"%s\", \"%s\"},\n",bp->bundleheight,bits256_str(str,bp->hashes[0]),"","");
}
9 years ago
break;
}
9 years ago
}
9 years ago
//printf("compare hdrs.txt %ld vs (%s) %ld\n",ftell(fp),fname,(long)OS_filesize(fname));
9 years ago
if ( (long)ftell(fp) > OS_filesize(fname) )
9 years ago
{
printf("new hdrs.txt %ld vs (%s) %ld\n",ftell(fp),fname,(long)OS_filesize(fname));
fclose(fp);
OS_renamefile(fname,oldfname);
8 years ago
OS_renamefile(tmpfname,fname);
//OS_copyfile(tmpfname,fname,1);
9 years ago
} else fclose(fp);
9 years ago
if ( fp2 != 0 )
{
fprintf(fp2,"};\n");
fclose(fp2);
}
9 years ago
}
9 years ago
else
{
printf("iguana_savehdrs: couldnt create.(%s)\n",tmpfname);
return(-1);
}
9 years ago
return(retval);
}
8 years ago
int32_t iguana_bundleinitmap(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_bundle *bp,int32_t height,bits256 hash2,bits256 hash1)
9 years ago
{
char str[65]; struct iguana_block *block;
bp->bundleheight = height;
if ( bits256_nonz(hash1) != 0 )
{
if ( (block= iguana_blockhashset("inithash1",coin,height+1,hash1,1)) != 0 )
{
iguana_bundlehashadd(coin,bp,1,block);
block->mainchain = 1;
}
}
if ( height == 0 && coin->current == 0 )
coin->current = coin->bundles[0] = bp;
if ( (block= iguana_blockfind("parse",coin,hash2)) != 0 )
block->mainchain = 1, block->height = height;
8 years ago
if ( iguana_bundleload(myinfo,coin,&bp->ramchain,bp,2) != 0 )
9 years ago
{
if ( coin->current != 0 && coin->current->hdrsi+1 == bp->hdrsi )
coin->current = bp;
9 years ago
bp->emitfinish = (uint32_t)time(NULL) + 1;
8 years ago
//printf("[%d].%p ",bp->hdrsi,bp->ramchain.H.data);
9 years ago
return(0);
9 years ago
}
else
{
init_hexbytes_noT(str,hash2.bytes,sizeof(hash2));
bp->emitfinish = 0;
iguana_blockQ("init",coin,bp,0,hash2,1);
//printf("init reqhdrs.%d\n",bp->bundleheight);
8 years ago
//queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(str),1);
9 years ago
memset(&hash2,0,sizeof(hash2));
9 years ago
bp->emitfinish = 0;
9 years ago
return(-1);
9 years ago
}
}
8 years ago
void iguana_parseline(struct supernet_info *myinfo,struct iguana_info *coin,int32_t iter,FILE *fp)
9 years ago
{
8 years ago
int32_t j,k,m,c,flag,bundlei,lastheight=0,missing=0,height = -1; char checkstr[1024],line[1024];
9 years ago
struct iguana_peer *addr; struct iguana_bundle *bp; bits256 allhash,hash2,hash1,zero,lastbundle;
8 years ago
if ( coin->FULLNODE == 0 && coin->VALIDATENODE == 0 && iter > 0 )
8 years ago
return;
8 years ago
OS_randombytes((uint8_t *)&j,sizeof(j));
if ( j < 0 )
j = -j;
srand(j);
9 years ago
memset(&zero,0,sizeof(zero));
lastbundle = zero;
9 years ago
if ( coin->MAXPEERS > IGUANA_MAXPEERS )
coin->MAXPEERS = IGUANA_MAXPEERS;
#ifdef oldway
9 years ago
if ( iter == 1 && 0 )
9 years ago
{
int32_t i; FILE *fp; char fname[512]; struct iguana_blockRO blockRO;
sprintf(fname,"blocks.%s",coin->symbol), OS_compatible_path(fname);
if ( (fp= fopen(fname,"rb")) != 0 )
{
for (i=0; i<100000000; i++)
{
if ( fread(&blockRO,1,sizeof(blockRO),fp) != sizeof(blockRO) )
break;
if ( i > (coin->blocks.maxbits - 1000) )
iguana_recvalloc(coin,i + 100000);
coin->blocks.RO[i] = blockRO;
char str[65];
if ( bits256_nonz(blockRO.hash2) > 0 )
printf("init.%d %s\n",i,bits256_str(str,blockRO.hash2));
}
fclose(fp);
printf("validate.%d blocks that were read in\n",i);
}
}
#endif
8 years ago
OS_randombytes((void *)&m,sizeof(m));
srand(m);
9 years ago
m = flag = 0;
allhash = zero;
9 years ago
memset(line,0,sizeof(line));
9 years ago
while ( fgets(line,sizeof(line),fp) > 0 )
{
j = (int32_t)strlen(line) - 1;
line[j] = 0;
//printf("parse line.(%s) maxpeers.%d\n",line,coin->MAXPEERS);
9 years ago
if ( iter == 0 )
{
if ( m < coin->MAXPEERS/2 )
9 years ago
{
8 years ago
if ( (0) && m == 0 && coin->seedipaddr[0] != 0 )
9 years ago
{
9 years ago
addr = &coin->peers->active[m++];
8 years ago
iguana_initpeer(coin,addr,(uint32_t)calc_ipbits(coin->seedipaddr));
printf("SEED_IPADDR initpeer.(%s)\n",addr->ipaddr);
9 years ago
iguana_launch(coin,"connection",iguana_startconnection,addr,IGUANA_CONNTHREAD);
}
#ifndef IGUANA_DISABLEPEERS
8 years ago
//if ( (rand() % 2) == 0 )
{
addr = &coin->peers->active[m++];
iguana_initpeer(coin,addr,(uint32_t)calc_ipbits(line));
//printf("call initpeer.(%s)\n",addr->ipaddr);
iguana_launch(coin,"connection",iguana_startconnection,addr,IGUANA_CONNTHREAD);
} //else
9 years ago
#endif
}
iguana_possible_peer(coin,line);
9 years ago
}
else
{
9 years ago
lastheight = height = -1;
if ( coin->bundlescount > 0 && (bp= coin->bundles[coin->bundlescount - 1]) != 0 )
lastheight = bp->bundleheight, lastbundle = bp->hashes[0];
9 years ago
for (k=height=0; k<j-1; k++)
{
if ( (c= line[k]) == ' ' )
break;
else if ( c >= '0' && c <= '9' )
height = (height * 10) + (line[k] - '0');
else break;
}
if ( line[k] == ' ' )
{
decode_hex(hash2.bytes,sizeof(hash2),line+k+1);
9 years ago
if ( coin->virtualchain != 0 )
9 years ago
printf("%s line.(%s) k.%d (%c)(%c)(%d)\n",coin->symbol,line,k,line[k+63],line[k+64],line[k+65]);
9 years ago
if ( height >= 0 && bits256_nonz(hash2) != 0 )
{
if ( (bp= iguana_bundlecreate(coin,&bundlei,height,hash2,zero,0)) != 0 )
{
//printf("created bundle.%d\n",bp->hdrsi);
9 years ago
memset(hash1.bytes,0,sizeof(hash1));
8 years ago
iguana_bundleinitmap(myinfo,coin,bp,height,hash2,hash1);
9 years ago
lastbundle = hash2;
}
}
9 years ago
if ( line[k + 65] != 0 && line[k+65] != '\n' && line[k+65] != '\r' )
9 years ago
{
//if ( height > (coin->blocks.maxbits - 1000) )
// iguana_recvalloc(coin,height + 100000);
9 years ago
decode_hex(allhash.bytes,sizeof(allhash),line+k+1 + 64 + 1);
init_hexbytes_noT(checkstr,allhash.bytes,sizeof(allhash));
9 years ago
//printf("parseline: k.%d %d height.%d m.%d bundlesize.%d (%s) check.(%s)\n",k,line[k],height,m,coin->chain->bundlesize,&line[k+1+65],checkstr);// + strlen(line+k+1)]);
if ( strncmp(checkstr,line+k+1 + 64 + 1,64) == 0 )
9 years ago
{
init_hexbytes_noT(checkstr,hash2.bytes,sizeof(hash2));
9 years ago
if ( strlen(line+k+1 + 2*64 + 2) == sizeof(hash1)*2 )
decode_hex(hash1.bytes,sizeof(hash1),line+k+1 + 2*64 + 2);
else memset(hash1.bytes,0,sizeof(hash1));
9 years ago
if ( coin->virtualchain != 0 )
memset(hash1.bytes,0,sizeof(hash1));
//char str[65],str2[65]; printf(">>>> %s bundle.%d last.%d got (%s)/(%s) allhash.(%s)\n",coin->symbol,height,lastheight,bits256_str(str,hash1),checkstr,bits256_str(str2,allhash));
9 years ago
if ( (bp= iguana_bundlecreate(coin,&bundlei,height,hash2,allhash,0)) != 0 )
{
9 years ago
if ( bits256_cmp(allhash,bp->allhash) != 0 )
{
printf("mismatched allhash.[%d]\n",bp->hdrsi);
bp->allhash = allhash;
}
9 years ago
if ( height >= lastheight )
9 years ago
{
8 years ago
if ( iguana_bundleinitmap(myinfo,coin,bp,height,hash2,hash1) == 0 )
9 years ago
lastbundle = hash2, lastheight = height;
8 years ago
else if ( missing++ > coin->MAXBUNDLES && strcmp("BTCD",coin->symbol) != 0 )
{
printf("missing.%d\n",missing);
break;
}
9 years ago
}
9 years ago
}
}
}
}
}
9 years ago
memset(line,0,sizeof(line));
9 years ago
}
9 years ago
if ( iter == 1 && bits256_nonz(lastbundle) != 0 )
9 years ago
{
9 years ago
printf("%s parseline ht.%d\n",coin->symbol,lastheight);
8 years ago
iguana_initfinal(myinfo,coin,lastbundle);
9 years ago
}
9 years ago
}
9 years ago
long iguana_bundlesload(struct supernet_info *myinfo,struct iguana_info *coin)
{
8 years ago
char fname[1024]; int32_t iter; FILE *fp; long fpos = -1;
for (iter=0; iter<2; iter++)
9 years ago
{
8 years ago
sprintf(fname,"%s/%s_%s.txt",GLOBAL_CONFSDIR,coin->symbol,(iter == 0) ? "hdrs" : "oldhdrs"), OS_compatible_path(fname);
if ( (fp= fopen(fname,"r")) != 0 )
{
iguana_parseline(myinfo,coin,1,fp);
printf("done parsefile.%d (%s) size.%ld\n",iter,fname,fpos);
fpos = ftell(fp);
fclose(fp);
break;
}
9 years ago
}
return(fpos);
}
9 years ago
void iguana_ramchainpurge(struct iguana_info *coin,struct iguana_bundle *bp,struct iguana_ramchain *ramchain)
{
9 years ago
iguana_ramchain_free(coin,ramchain,1);
9 years ago
}
void iguana_bundlepurge(struct iguana_info *coin,struct iguana_bundle *bp)
{
9 years ago
int32_t i; static const bits256 zero;
9 years ago
iguana_ramchainpurge(coin,bp,&bp->ramchain);
9 years ago
if ( bp->speculative != 0 )
9 years ago
{
for (i=0; i<bp->n; i++)
if ( bp->speculativecache[i] != 0 )
{
9 years ago
free(bp->speculativecache[i]);
9 years ago
bp->speculativecache[i] = 0;
}
9 years ago
myfree(bp->speculative,sizeof(*bp->speculative) * bp->numspec);
9 years ago
}
9 years ago
bp->numspec = 0;
bp->speculative = 0;
memset(bp->hashes,0,sizeof(bp->hashes));
memset(bp->issued,0,sizeof(bp->issued));
bp->prevbundlehash2 = bp->nextbundlehash2 = bp->allhash = zero;
}
void iguana_blockpurge(struct iguana_info *coin,struct iguana_block *block)
{
8 years ago
if ( block->req != 0 )
9 years ago
{
8 years ago
printf("purge req inside block\n");
myfree(block->req,block->req->allocsize);
9 years ago
}
8 years ago
free(block);
9 years ago
}
void iguana_blockspurge(struct iguana_info *coin)
{
8 years ago
struct iguana_block *block,*tmp;
9 years ago
if ( 1 && coin->blocks.hash != 0 )
9 years ago
{
HASH_ITER(hh,coin->blocks.hash,block,tmp)
{
HASH_DEL(coin->blocks.hash,block);
iguana_blockpurge(coin,block);
}
coin->blocks.hash = 0;
}
8 years ago
coin->blocks.maxblocks = coin->blocks.initblocks = coin->blocks.hashblocks = coin->blocks.issuedblocks = coin->blocks.recvblocks = coin->blocks.emitblocks = coin->blocks.parsedblocks = coin->blocks.dirty = 0;
8 years ago
printf("clear hwmchain\n");
9 years ago
memset(&coin->blocks.hwmchain,0,sizeof(coin->blocks.hwmchain));
}
void iguana_coinpurge(struct iguana_info *coin)
{
8 years ago
int32_t i,saved; struct iguana_bundle *bp; struct iguana_bundlereq *req; struct iguana_blockreq *breq; struct iguana_helper *ptr; struct stritem *hashitem;
9 years ago
saved = coin->active, coin->active = 0;
coin->started = 0;
9 years ago
while ( coin->idletime == 0 && coin->emitbusy > 0 )
{
printf("coinpurge.%s waiting for idle %u emitbusy.%d\n",coin->symbol,(uint32_t)time(NULL),coin->emitbusy);
9 years ago
sleep(1);
}
9 years ago
coin->RTgenesis = 0;
8 years ago
while ( (ptr= queue_dequeue(&bundlesQ)) != 0 )
9 years ago
myfree(ptr,ptr->allocsize);
9 years ago
if ( 1 )
9 years ago
{
8 years ago
while ( (hashitem= queue_dequeue(&coin->hdrsQ)) != 0 )
free(hashitem);
while ( (breq= queue_dequeue(&coin->blocksQ)) != 0 )
9 years ago
myfree(breq,sizeof(*breq));
8 years ago
while ( (breq= queue_dequeue(&coin->priorityQ)) != 0 )
9 years ago
myfree(breq,sizeof(*breq));
8 years ago
while ( (req= queue_dequeue(&coin->cacheQ)) != 0 )
9 years ago
myfree(req,req->allocsize);
8 years ago
while ( (req= queue_dequeue(&coin->recvQ)) != 0 )
9 years ago
{
if ( req->blocks != 0 )
myfree(req->blocks,sizeof(*req->blocks) * req->n), req->blocks = 0;
9 years ago
if ( req->hashes != 0 )
9 years ago
myfree(req->hashes,sizeof(*req->hashes) * req->n), req->hashes = 0;
myfree(req,req->allocsize);
}
9 years ago
}
8 years ago
//iguana_RTramchainfree(coin,coin->current);
iguana_RTdataset_free(coin);
9 years ago
coin->bundlescount = 0;
for (i=0; i<coin->bundlescount; i++)
if ( (bp= coin->bundles[i]) != 0 )
iguana_bundlepurge(coin,bp);
coin->current = coin->lastpending = 0;
memset(coin->bundles,0,sizeof(coin->bundles));
iguana_blockspurge(coin);
9 years ago
coin->active = saved;
9 years ago
}
8 years ago
int32_t iguana_isnotarychain(char *symbol)
{
int32_t i,n,notarychain = -1; char *jsonstr; cJSON *chains;
if ( (jsonstr= dpow_notarychains(0,0,0,0)) != 0 )
{
if ( (chains= cJSON_Parse(jsonstr)) != 0 )
{
if ( (n= cJSON_GetArraySize(chains)) > 0 )
{
for (i=0; i<n; i++)
if ( strcmp(symbol,jstri(chains,i)) == 0 )
{
notarychain = i;
break;
}
}
free_json(chains);
}
free(jsonstr);
}
return(notarychain);
}
8 years ago
struct iguana_info *iguana_coinstart(struct supernet_info *myinfo,struct iguana_info *coin,int32_t initialheight,int32_t mapflags)
9 years ago
{
8 years ago
FILE *fp; char fname[512],*symbol; int32_t j,iter; long fpos; bits256 lastbundle;
9 years ago
/*if ( coin->peers == 0 )
9 years ago
{
printf("cant start privatechain directly\n");
return(0);
9 years ago
}*/
9 years ago
coin->sleeptime = 10000;
symbol = coin->symbol;
9 years ago
if ( coin->peers != 0 && iguana_peerslotinit(coin,&coin->internaladdr,IGUANA_MAXPEERS,calc_ipbits("127.0.0.1:7777")) < 0 )
9 years ago
{
printf("iguana_coinstart: error creating peerslot\n");
return(0);
}
9 years ago
if ( initialheight < coin->chain->bundlesize*10 )
initialheight = coin->chain->bundlesize*10;
9 years ago
if ( coin->longestchain == 0 )
coin->longestchain = 1;
9 years ago
memset(&coin->blocks.hwmchain,0,sizeof(coin->blocks.hwmchain));
9 years ago
coin->blocks.hwmchain.height = 0;
9 years ago
coin->blocks.hwmchain.RO.allocsize = coin->chain->zcash != 0 ? sizeof(struct iguana_zblock) : sizeof(struct iguana_block);
9 years ago
printf("%s MYSERVICES.%llx\n",coin->symbol,(long long)coin->myservices);
9 years ago
if ( coin->virtualchain == 0 && coin->peers != 0 )
9 years ago
{
if ( myinfo->IAMNOTARY != 0 || (coin->myservices & NODE_NETWORK) != 0 || (coin->FULLNODE > 0 || coin->VALIDATENODE > 0) )
9 years ago
{
9 years ago
if ( coin->peers->acceptloop == 0 && coin->peers->localaddr == 0 )
9 years ago
{
9 years ago
coin->peers->acceptloop = malloc(sizeof(pthread_t));
if ( OS_thread_create(coin->peers->acceptloop,NULL,(void *)iguana_acceptloop,(void *)coin) != 0 )
{
free(coin->peers->acceptloop);
coin->peers->acceptloop = 0;
printf("error launching accept thread for port.%u\n",coin->chain->portp2p);
}
9 years ago
}
9 years ago
}
8 years ago
if ( coin->rpcloop == 0 && strcmp(coin->chain->symbol,"RELAY") != 0 )
9 years ago
{
9 years ago
myinfo->argport = coin->chain->rpcport;
coin->rpcloop = malloc(sizeof(pthread_t));
if ( OS_thread_create(coin->rpcloop,NULL,(void *)iguana_rpcloop,(void *)myinfo) != 0 )
{
free(coin->rpcloop);
coin->rpcloop = 0;
printf("error launching rpcloop for %s port.%u\n",coin->symbol,coin->chain->rpcport);
}
9 years ago
}
}
if ( (coin->notarychain= iguana_isnotarychain(coin->symbol)) >= 0 && coin->FULLNODE == 0 )
8 years ago
{
8 years ago
printf("SET %s NOTARYCHAIN.%d\n",coin->symbol,coin->notarychain);
8 years ago
return(coin);
8 years ago
}
9 years ago
//coin->firstblock = coin->blocks.parsedblocks + 1;
8 years ago
iguana_genesis(myinfo,coin,coin->chain);
9 years ago
int32_t bundlei = -2;
static const bits256 zero;
iguana_bundlecreate(coin,&bundlei,0,*(bits256 *)coin->chain->genesis_hashdata,zero,1);
if ( coin->virtualchain == 0 )
9 years ago
{
8 years ago
_iguana_chainlink(myinfo,coin,iguana_blockfind("genesis",coin,*(bits256 *)coin->chain->genesis_hashdata));
if ( coin->blocks.hwmchain.height != 0 || memcmp(coin->blocks.hwmchain.RO.hash2.bytes,coin->chain->genesis_hashdata,sizeof(coin->chain->genesis_hashdata)) != 0 )
{
char str[65]; printf("%s genesis values mismatch hwmheight.%d %.15f %.15f %s\n",coin->name,coin->blocks.hwmchain.height,coin->blocks.hwmchain.PoW,coin->blocks.hwmchain.PoW,bits256_str(str,coin->blocks.hwmchain.RO.hash2));
getchar();
}
9 years ago
}
9 years ago
memset(&lastbundle,0,sizeof(lastbundle));
9 years ago
if ( coin->peers == 0 )
iter = 2;
else iter = (coin->peers->numranked > 8);
for (; iter<2; iter++)
9 years ago
{
9 years ago
#ifdef __PNACL__
9 years ago
if ( iter == 0 )
9 years ago
{
9 years ago
char **ipaddrs = 0; int32_t j,num;
9 years ago
char *BTC_ipaddrs[] = { "5.9.102.210", "130.211.146.81", "176.9.29.76", "108.58.252.82", "148.251.151.48", "74.207.233.193" };
9 years ago
char *BTCD_ipaddrs[] = { "5.9.102.210", "89.248.160.236", "89.248.160.237", "89.248.160.238", "89.248.160.239", "89.248.160.240", "89.248.160.241", "89.248.160.242", "89.248.160.243", "89.248.160.244", "89.248.160.245", "78.47.58.62", "67.212.70.88", "94.102.50.69", "50.179.58.158", "194.135.94.30", "109.236.85.42", "104.236.127.154", "68.45.147.145", "37.59.14.7", "78.47.115.250", "188.40.138.8", "62.75.143.120", "82.241.71.230", "217.23.6.2", "73.28.172.128", "45.55.149.34", "192.0.242.54", "81.181.155.53", "91.66.185.97", "85.25.217.233", "144.76.239.66", "95.80.9.112", "80.162.193.118", "173.65.129.85", "2.26.173.58", "78.14.250.69", "188.226.253.77", "58.107.67.39", "124.191.37.212", "176.226.137.238", "69.145.25.85", "24.168.14.28", "73.201.180.47", "76.188.171.53", "63.247.147.166", "121.108.241.247", "36.74.36.125", "106.186.119.171", "188.166.91.37", "223.134.228.208", "89.248.160.244", "178.33.209.212", "71.53.156.38", "88.198.10.165", "24.117.221.0", "74.14.104.57", "158.69.27.82", "110.174.129.213", "75.130.163.51" };
if ( strcmp(coin->symbol,"BTCD") == 0 )
ipaddrs = BTCD_ipaddrs, num = (int32_t)(sizeof(BTCD_ipaddrs)/sizeof(*BTCD_ipaddrs));
else if ( strcmp(coin->symbol,"BTC") == 0 )
ipaddrs = BTC_ipaddrs, num = (int32_t)(sizeof(BTC_ipaddrs)/sizeof(*BTC_ipaddrs));
9 years ago
if ( ipaddrs != 0 && coin->virtualchain == 0 )
9 years ago
{
9 years ago
for (j=0; j<num; j++)
9 years ago
{
9 years ago
//printf("%s ",ipaddrs[j]);
9 years ago
if ( 0 && j < IGUANA_MINPEERS )
8 years ago
iguana_launchpeer(coin,ipaddrs[j],0);
9 years ago
else iguana_possible_peer(coin,ipaddrs[j]);
9 years ago
}
}
}
9 years ago
else
{
}
9 years ago
#endif
8 years ago
fpos = -1;
for (j=0; j<2; j++)
9 years ago
{
/**
* macro switch for easy debug from Visual Studio IDE
* @author-fadedreamz@gmail.com
*/
#if defined(WIN32) && defined(_DEBUG)
sprintf(fname, "%s/%s/%s_%s%s.txt", "iguana", GLOBAL_CONFSDIR, coin->symbol, j == 0 ? "" : "old", (iter == 0) ? "peers" : "hdrs"), OS_compatible_path(fname);
#else
8 years ago
sprintf(fname,"%s/%s_%s%s.txt",GLOBAL_CONFSDIR,coin->symbol,j==0?"":"old",(iter == 0) ? "peers" : "hdrs"), OS_compatible_path(fname);
#endif
8 years ago
//sprintf(fname,"confs/%s_%s.txt",coin->symbol,(iter == 0) ? "peers" : "hdrs");
//sprintf(fname,"tmp/%s/%s.txt",coin->symbol,(iter == 0) ? "peers" : "hdrs");
OS_compatible_path(fname);
printf("check.(%s)\n",fname);
8 years ago
if ( (fp= fopen(fname,"r")) != 0 )
9 years ago
{
8 years ago
if ( coin->virtualchain == 0 || iter > 0 )
{
printf("parsefile.%d %s\n",iter,fname);
iguana_parseline(myinfo,coin,iter,fp);
printf("done parsefile.%d (%s) size.%ld\n",iter,fname,fpos);
}
fpos = ftell(fp);
fclose(fp);
break;
9 years ago
}
8 years ago
}
9 years ago
}
#ifndef IGUANA_DEDICATED_THREADS
9 years ago
coin->peers->peersloop = iguana_launch("peersloop",iguana_peersloop,coin,IGUANA_PERMTHREAD);
9 years ago
#endif
9 years ago
printf("started.%s %p active.%d\n",coin->symbol,coin->started,coin->active);
9 years ago
return(coin);
}