From 4f0dd021b00a7c5d48ec8a0251e90012ad2b4906 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 24 Mar 2016 07:56:19 -0300 Subject: [PATCH] test --- iguana/iguana777.c | 18 ++++--- iguana/iguana777.h | 3 +- iguana/iguana_bundles.c | 11 ++-- iguana/iguana_init.c | 106 +-------------------------------------- iguana/iguana_unspents.c | 101 ++++++++++++++++++++++++++++++++++++- 5 files changed, 120 insertions(+), 119 deletions(-) diff --git a/iguana/iguana777.c b/iguana/iguana777.c index 53868133f..2895fdc90 100755 --- a/iguana/iguana777.c +++ b/iguana/iguana777.c @@ -535,26 +535,28 @@ struct iguana_info *iguana_setcoin(char *symbol,void *launched,int32_t maxpeers, coin->mapflags = mapflags; mult = (strcmp("BTC",coin->symbol) != 0) ? 512 : 1; maxval = (strcmp("BTC",coin->symbol) != 0) ? 2048 : 64; + coin->MAXMEM = juint(json,"RAM"); + if ( coin->MAXMEM == 0 ) + coin->MAXMEM = IGUANA_DEFAULTRAM; + if ( strcmp("BTC",coin->symbol) == 0 && coin->MAXMEM <= 4 ) + maxval = coin->MAXMEM; + coin->MAXMEM *= (1024L * 1024 * 1024); if ( (coin->startPEND= juint(json,"startpend")) == 0 ) coin->startPEND = IGUANA_MAXPENDBUNDLES * mult; - if ( coin->startPEND > 2048 ) - coin->startPEND = 2048; + if ( coin->startPEND > maxval ) + coin->startPEND = maxval; else if ( coin->startPEND < 2 ) coin->startPEND = 2; coin->MAXBUNDLES = coin->startPEND; if ( (coin->endPEND= juint(json,"endpend")) == 0 ) coin->endPEND = IGUANA_MINPENDBUNDLES * mult; - if ( coin->endPEND > 2048 ) - coin->endPEND = 2048; + if ( coin->endPEND > maxval ) + coin->endPEND = maxval; else if ( coin->endPEND < 2 ) coin->endPEND = 2; coin->enableCACHE = (strcmp("BTC",coin->symbol) != 0); if ( jobj(json,"cache") != 0 ) coin->enableCACHE = juint(json,"cache"); - coin->MAXMEM = juint(json,"RAM"); - if ( coin->MAXMEM == 0 ) - coin->MAXMEM = IGUANA_DEFAULTRAM; - coin->MAXMEM *= (1024 * 1024 * 1024); if ( (coin->polltimeout= juint(json,"poll")) <= 0 ) coin->polltimeout = 10; char str[65]; printf("MAXMEM.%s enablecache.%d\n",mbstr(str,coin->MAXMEM),coin->enableCACHE); diff --git a/iguana/iguana777.h b/iguana/iguana777.h index 53ef31c92..6f141c9e9 100755 --- a/iguana/iguana777.h +++ b/iguana/iguana777.h @@ -458,7 +458,7 @@ struct iguana_info struct iguana_peers peers; uint64_t instance_nonce,myservices,totalsize,totalrecv,totalpackets,sleeptime; int64_t mining,totalfees,TMPallocated,MAXRECVCACHE,MAXMEM,estsize,activebundles; - int32_t MAXPEERS,MAXPENDING,MAXBUNDLES,active,closestbundle,numemitted,lastsweep,startutc,newramchain,numcached,cachefreed,helperdepth,startPEND,endPEND,enableCACHE,RELAYNODE,VALIDATENODE,balanceswritten; bits256 balancehash; + int32_t MAXPEERS,MAXPENDING,MAXBUNDLES,active,closestbundle,numemitted,lastsweep,startutc,newramchain,numcached,cachefreed,helperdepth,startPEND,endPEND,enableCACHE,RELAYNODE,VALIDATENODE,balanceswritten,RTheight; bits256 balancehash; uint32_t longestchain,lastsync,parsetime,numiAddrs,firstblock,lastpossible,bundlescount,savedblocks; struct tai starttime; double startmillis; struct iguana_chain *chain; @@ -806,6 +806,7 @@ void iguana_ramchain_prefetch(struct iguana_info *coin,struct iguana_ramchain *r void iguana_realtime_update(struct iguana_info *coin); int32_t iguana_mapvolatiles(struct iguana_info *coin,struct iguana_ramchain *ramchain); void iguana_purgevolatiles(struct iguana_info *coin,struct iguana_ramchain *ramchain); +int32_t iguana_volatileinit(struct iguana_info *coin); extern int32_t HDRnet,netBLOCKS; diff --git a/iguana/iguana_bundles.c b/iguana/iguana_bundles.c index b146645d2..9d33b8ead 100755 --- a/iguana/iguana_bundles.c +++ b/iguana/iguana_bundles.c @@ -557,7 +557,7 @@ int32_t iguana_bundleissue(struct iguana_info *coin,struct iguana_bundle *bp,int { if ( (block= bp->blocks[i]) != 0 ) { - if ( block->fpipbits == 0 || block->RO.recvlen == 0 || block->fpos < 0 || (bp->hdrsi == 0 && i == 0) || bits256_nonz(block->RO.prev_block) > 0 ) + if ( block->fpipbits == 0 || block->RO.recvlen == 0 || block->fpos < 0 || ((bp->hdrsi != 0 || i > 0) && bits256_nonz(block->RO.prev_block) == 0) ) { if ( block->issued == 0 || now > block->issued+lag ) { @@ -769,10 +769,11 @@ int64_t iguana_bundlecalcs(struct iguana_info *coin,struct iguana_bundle *bp) int32_t iguana_bundlefinish(struct iguana_info *coin,struct iguana_bundle *bp) { struct iguana_bundle *prevbp; int32_t i,retval; -#ifdef IGUANA_SERIALIZE_BALANCEGEN - if ( (prevbp= coin->current) != 0 && prevbp->hdrsi < (coin->longestchain / coin->chain->bundlesize)-0*coin->MAXBUNDLES ) - return(0); -#endif + if ( coin->MAXMEM <= 4*(1024L * 1024 * 1024) ) + { + if ( (prevbp= coin->current) != 0 && prevbp->hdrsi < (coin->longestchain / coin->chain->bundlesize)-0*coin->MAXBUNDLES ) + return(0); + } for (i=0; ihdrsi; i++) if ( (prevbp= coin->bundles[i]) == 0 || prevbp->emitfinish < coin->startutc ) break; diff --git a/iguana/iguana_init.c b/iguana/iguana_init.c index 95b9e5fb9..5426e17a7 100755 --- a/iguana/iguana_init.c +++ b/iguana/iguana_init.c @@ -184,37 +184,9 @@ int32_t iguana_savehdrs(struct iguana_info *coin) return(retval); } -void iguana_truncatebalances(struct iguana_info *coin) -{ - int32_t i; struct iguana_bundle *bp; struct iguana_ramchain *ramchain; - for (i=0; ibalanceswritten; i++) - { - if ( (bp= coin->bundles[i]) != 0 ) - { - bp->balancefinish = 0; - ramchain = &bp->ramchain; - if ( ramchain->debitsfileptr != 0 ) - { - munmap(ramchain->debitsfileptr,ramchain->debitsfilesize); - ramchain->debitsfileptr = 0; - ramchain->debitsfilesize = 0; - ramchain->A = 0; - } - if ( ramchain->lastspendsfileptr != 0 ) - { - munmap(ramchain->lastspendsfileptr,ramchain->lastspendsfilesize); - ramchain->lastspendsfileptr = 0; - ramchain->lastspendsfilesize = 0; - ramchain->Uextras = 0; - } - } - } - coin->balanceswritten = 0; -} - void iguana_parseline(struct iguana_info *coin,int32_t iter,FILE *fp) { - int32_t i,j,k,m,c,height,flag,bundlei,from_ro; char checkstr[1024],line[1024]; + int32_t i,j,k,m,c,height,flag,bundlei; char checkstr[1024],line[1024]; struct iguana_peer *addr; struct iguana_bundle *bp; bits256 allhash,hash2,zero,lastbundle; struct iguana_block *block; memset(&zero,0,sizeof(zero)); @@ -329,81 +301,7 @@ void iguana_parseline(struct iguana_info *coin,int32_t iter,FILE *fp) if ( iter == 1 ) { if ( coin->balanceswritten > 0 ) - { - from_ro = 1; - for (i=0; ibalanceswritten; i++) - { - if ( (bp= coin->bundles[i]) == 0 || bp->emitfinish <= 1 || bp->utxofinish <= 1 ) - { - printf("hdrsi.[%d] emitfinish.%u utxofinish.%u\n",i,bp->emitfinish,bp->utxofinish); - break; - } - if ( bp->ramchain.from_ro == 0 || bp->ramchain.from_roX == 0 || bp->ramchain.from_roA == 0 || bp->ramchain.from_roU == 0 ) - from_ro = 0; - } - if ( i != coin->balanceswritten ) - { - printf("TRUNCATE balances written.%d -> %d\n",coin->balanceswritten,i); - iguana_truncatebalances(coin); - } - else - { - bits256 balancehash; struct iguana_utxo *Uptr; struct iguana_account *Aptr; struct sha256_vstate vstate; int32_t numpkinds,numunspents; uint32_t crc,filecrc; FILE *fp; char crcfname[512],str[65],str2[65]; - vupdate_sha256(balancehash.bytes,&vstate,0,0); - filecrc = 0; - sprintf(crcfname,"DB/%s/balancecrc.%d",coin->symbol,coin->balanceswritten); - if ( (fp= fopen(crcfname,"rb")) != 0 ) - { - if ( fread(&filecrc,1,sizeof(filecrc),fp) != sizeof(filecrc) ) - filecrc = 0; - else if ( fread(&balancehash,1,sizeof(balancehash),fp) != sizeof(balancehash) ) - filecrc = 0; - else if ( memcmp(&balancehash,&coin->balancehash,sizeof(balancehash)) != 0 ) - filecrc = 0; - fclose(fp); - } - if ( filecrc != 0 ) - printf("have filecrc.%08x for %s milli.%.0f\n",filecrc,bits256_str(str,balancehash),OS_milliseconds()); - if ( from_ro == 0 ) - { - if ( filecrc == 0 ) - vupdate_sha256(balancehash.bytes,&vstate,0,0); - for (i=crc=0; ibalanceswritten; i++) - { - numpkinds = numunspents = 0; - Aptr = 0, Uptr = 0; - if ( (bp= coin->bundles[i]) != 0 && bp->ramchain.H.data != 0 && (numpkinds= bp->ramchain.H.data->numpkinds) > 0 && (numunspents= bp->ramchain.H.data->numunspents) > 0 && (Aptr= bp->ramchain.A) != 0 && (Uptr= bp->ramchain.Uextras) != 0 ) - { - if ( filecrc == 0 ) - { - vupdate_sha256(balancehash.bytes,&vstate,(void *)Aptr,sizeof(*Aptr)*numpkinds); - vupdate_sha256(balancehash.bytes,&vstate,(void *)Uptr,sizeof(*Uptr)*numunspents); - } - crc = calc_crc32(crc,(void *)Aptr,(int32_t)(sizeof(*Aptr) * numpkinds)); - crc = calc_crc32(crc,(void *)Uptr,(int32_t)(sizeof(*Uptr) * numunspents)); - } else printf("missing hdrs.[%d] data.%p num.(%u %d) %p %p\n",i,bp->ramchain.H.data,numpkinds,numunspents,Aptr,Uptr); - } - } else crc = filecrc; - printf("millis %.0f from_ro.%d written.%d crc.%08x/%08x balancehash.(%s) vs (%s)\n",OS_milliseconds(),from_ro,coin->balanceswritten,crc,filecrc,bits256_str(str,balancehash),bits256_str(str2,coin->balancehash)); - if ( (filecrc != 0 && filecrc != crc) || memcmp(balancehash.bytes,coin->balancehash.bytes,sizeof(balancehash)) != 0 ) - { - printf("balancehash or crc mismatch\n"); - iguana_truncatebalances(coin); - } - else - { - printf("MATCHED balancehash numhdrsi.%d crc.%08x\n",coin->balanceswritten,crc); - if ( (fp= fopen(crcfname,"wb")) != 0 ) - { - if ( fwrite(&crc,1,sizeof(crc),fp) != sizeof(crc) || fwrite(&balancehash,1,sizeof(balancehash),fp) != sizeof(balancehash) ) - printf("error writing.(%s)\n",crcfname); - fclose(fp); - } - } - } - } - char buf[2048]; - iguana_bundlestats(coin,buf); + coin->balanceswritten = iguana_volatileinit(coin); if ( coin->balanceswritten > 0 ) { for (i=0; ibalanceswritten; i++) diff --git a/iguana/iguana_unspents.c b/iguana/iguana_unspents.c index 3705ceb3c..c4a52a3c9 100755 --- a/iguana/iguana_unspents.c +++ b/iguana/iguana_unspents.c @@ -601,10 +601,13 @@ int32_t iguana_balancegen(struct iguana_info *coin,struct iguana_bundle *bp,int3 return(-errs); } - void iguana_purgevolatiles(struct iguana_info *coin,struct iguana_ramchain *ramchain) { + if ( ramchain->allocatedA != 0 && ramchain->A != 0 ) + free(ramchain->A); ramchain->A = 0; + if ( ramchain->allocatedU != 0 && ramchain->Uextras != 0 ) + free(ramchain->Uextras); ramchain->Uextras = 0; if ( ramchain->debitsfileptr != 0 ) { @@ -694,6 +697,101 @@ void iguana_allocvolatile(struct iguana_info *coin,struct iguana_ramchain *ramch } else printf("illegal ramchain.%p\n",ramchain); } +void iguana_truncatebalances(struct iguana_info *coin) +{ + int32_t i; struct iguana_bundle *bp; struct iguana_ramchain *ramchain; + for (i=0; ibalanceswritten; i++) + { + if ( (bp= coin->bundles[i]) != 0 ) + { + bp->balancefinish = 0; + ramchain = &bp->ramchain; + iguana_purgevolatiles(coin,ramchain); + } + } + coin->balanceswritten = 0; +} + +int32_t iguana_volatileinit(struct iguana_info *coin) +{ + bits256 balancehash; struct iguana_utxo *Uptr; struct iguana_account *Aptr; + struct sha256_vstate vstate; int32_t i,from_ro,numpkinds,numunspents; struct iguana_bundle *bp; + uint32_t crc,filecrc; FILE *fp; char crcfname[512],str[65],str2[65],buf[2048]; + from_ro = 1; + for (i=0; ibalanceswritten; i++) + { + if ( (bp= coin->bundles[i]) == 0 || bp->emitfinish <= 1 || bp->utxofinish <= 1 ) + { + printf("hdrsi.[%d] emitfinish.%u utxofinish.%u\n",i,bp->emitfinish,bp->utxofinish); + break; + } + if ( bp->ramchain.from_ro == 0 || bp->ramchain.from_roX == 0 || bp->ramchain.from_roA == 0 || bp->ramchain.from_roU == 0 ) + from_ro = 0; + } + if ( i != coin->balanceswritten ) + { + printf("TRUNCATE balances written.%d -> %d\n",coin->balanceswritten,i); + iguana_truncatebalances(coin); + } + else + { + vupdate_sha256(balancehash.bytes,&vstate,0,0); + filecrc = 0; + sprintf(crcfname,"DB/%s/balancecrc.%d",coin->symbol,coin->balanceswritten); + if ( (fp= fopen(crcfname,"rb")) != 0 ) + { + if ( fread(&filecrc,1,sizeof(filecrc),fp) != sizeof(filecrc) ) + filecrc = 0; + else if ( fread(&balancehash,1,sizeof(balancehash),fp) != sizeof(balancehash) ) + filecrc = 0; + else if ( memcmp(&balancehash,&coin->balancehash,sizeof(balancehash)) != 0 ) + filecrc = 0; + fclose(fp); + } + if ( filecrc != 0 ) + printf("have filecrc.%08x for %s milli.%.0f\n",filecrc,bits256_str(str,balancehash),OS_milliseconds()); + if ( from_ro == 0 ) + { + if ( filecrc == 0 ) + vupdate_sha256(balancehash.bytes,&vstate,0,0); + for (i=crc=0; ibalanceswritten; i++) + { + numpkinds = numunspents = 0; + Aptr = 0, Uptr = 0; + if ( (bp= coin->bundles[i]) != 0 && bp->ramchain.H.data != 0 && (numpkinds= bp->ramchain.H.data->numpkinds) > 0 && (numunspents= bp->ramchain.H.data->numunspents) > 0 && (Aptr= bp->ramchain.A) != 0 && (Uptr= bp->ramchain.Uextras) != 0 ) + { + if ( filecrc == 0 ) + { + vupdate_sha256(balancehash.bytes,&vstate,(void *)Aptr,sizeof(*Aptr)*numpkinds); + vupdate_sha256(balancehash.bytes,&vstate,(void *)Uptr,sizeof(*Uptr)*numunspents); + } + crc = calc_crc32(crc,(void *)Aptr,(int32_t)(sizeof(*Aptr) * numpkinds)); + crc = calc_crc32(crc,(void *)Uptr,(int32_t)(sizeof(*Uptr) * numunspents)); + } else printf("missing hdrs.[%d] data.%p num.(%u %d) %p %p\n",i,bp->ramchain.H.data,numpkinds,numunspents,Aptr,Uptr); + } + } else crc = filecrc; + printf("millis %.0f from_ro.%d written.%d crc.%08x/%08x balancehash.(%s) vs (%s)\n",OS_milliseconds(),from_ro,coin->balanceswritten,crc,filecrc,bits256_str(str,balancehash),bits256_str(str2,coin->balancehash)); + if ( (filecrc != 0 && filecrc != crc) || memcmp(balancehash.bytes,coin->balancehash.bytes,sizeof(balancehash)) != 0 ) + { + printf("balancehash or crc mismatch\n"); + iguana_truncatebalances(coin); + } + else + { + printf("MATCHED balancehash numhdrsi.%d crc.%08x\n",coin->balanceswritten,crc); + if ( (fp= fopen(crcfname,"wb")) != 0 ) + { + if ( fwrite(&crc,1,sizeof(crc),fp) != sizeof(crc) || fwrite(&balancehash,1,sizeof(balancehash),fp) != sizeof(balancehash) ) + printf("error writing.(%s)\n",crcfname); + fclose(fp); + } + } + } + coin->RTheight = coin->balanceswritten * coin->chain->bundlesize; + iguana_bundlestats(coin,buf); + return(coin->balanceswritten); +} + int32_t iguana_balanceflush(struct iguana_info *coin,int32_t refhdrsi,int32_t purgedist) { int32_t hdrsi,numpkinds,iter,numhdrsi,numunspents,err; struct iguana_bundle *bp; @@ -812,6 +910,7 @@ int32_t iguana_balanceflush(struct iguana_info *coin,int32_t refhdrsi,int32_t pu printf("error mapping bundle.[%d]\n",hdrsi); } char str[65]; printf("BALANCES WRITTEN for %d bundles %s\n",coin->balanceswritten,bits256_str(str,coin->balancehash)); + coin->balanceswritten = iguana_volatileinit(coin); return(coin->balanceswritten); }