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.

734 lines
33 KiB

9 years ago
/******************************************************************************
* Copyright © 2014-2016 The SuperNET Developers. *
* *
* 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"
8 years ago
//#define ENABLE_RAMCHAIN
9 years ago
void iguana_RTramchainfree(struct iguana_info *coin,struct iguana_bundle *bp)
{
8 years ago
//return;
8 years ago
#ifdef ENABLE_RAMCHAIN
9 years ago
int32_t hdrsi;
8 years ago
//portable_mutex_lock(&coin->RTmutex);
9 years ago
if ( coin->utxotable != 0 )
{
printf("free RTramchain\n");
8 years ago
iguana_utxoupdate(coin,-1,0,0,0,0,-1,0); // free hashtables
8 years ago
coin->lastRTheight = coin->RTheight = 0;//(coin->bundlescount-1) * coin->chain->bundlesize;
9 years ago
coin->RTgenesis = 0;
8 years ago
iguana_utxoaddrs_purge(coin);
9 years ago
iguana_ramchain_free(coin,&coin->RTramchain,1);
if ( bp != 0 )
bp->ramchain = coin->RTramchain;
iguana_mempurge(&coin->RTmem);
iguana_mempurge(&coin->RThashmem);
for (hdrsi=coin->bundlescount-1; hdrsi>0; hdrsi--)
if ( (bp= coin->bundles[hdrsi]) == 0 && bp != coin->current )
{
iguana_volatilespurge(coin,&bp->ramchain);
if ( iguana_volatilesmap(coin,&bp->ramchain) != 0 )
printf("error mapping bundle.[%d]\n",hdrsi);
}
8 years ago
coin->RTdatabad = 0;
9 years ago
printf("done RTramchain\n");
}
8 years ago
//portable_mutex_unlock(&coin->RTmutex);
8 years ago
#endif
9 years ago
}
8 years ago
void *iguana_ramchainfile(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_ramchain *dest,struct iguana_ramchain *R,struct iguana_bundle *bp,int32_t bundlei,struct iguana_block *block)
9 years ago
{
8 years ago
//return(0);
8 years ago
#ifdef ENABLE_RAMCHAIN
9 years ago
char fname[1024]; long filesize; int32_t err; void *ptr=0;
if ( block == bp->blocks[bundlei] && (ptr= iguana_bundlefile(coin,fname,&filesize,bp,bundlei)) != 0 )
{
if ( iguana_mapchaininit(fname,coin,R,bp,bundlei,block,ptr,filesize) >= 0 )
{
if ( dest != 0 && dest->H.data != 0 )
8 years ago
err = iguana_ramchain_iterate(myinfo,coin,dest,R,bp,bundlei);
9 years ago
else err = 0;
if ( err != 0 || dest->H.data == 0 || bits256_cmp(R->H.data->firsthash2,block->RO.hash2) != 0 )
{
char str[65];
printf("ERROR [%d:%d] %s vs ",bp->hdrsi,bundlei,bits256_str(str,block->RO.hash2));
printf("mapped.%s\n",bits256_str(str,R->H.data->firsthash2));
} else return(ptr);
}
iguana_blockunmark(coin,block,bp,bundlei,1);
iguana_ramchain_free(coin,R,1);
} //else printf("ramchainfile ptr.%p block.%p\n",ptr,block);
8 years ago
#endif
8 years ago
return(0);
9 years ago
}
void iguana_RTramchainalloc(char *fname,struct iguana_info *coin,struct iguana_bundle *bp)
{
8 years ago
//return;
8 years ago
#ifdef ENABLE_RAMCHAIN
9 years ago
uint32_t i,changed = 0; struct iguana_ramchaindata *rdata; struct iguana_ramchain *dest = &coin->RTramchain; struct iguana_blockRO *B; struct iguana_bundle *tmpbp;
8 years ago
//portable_mutex_lock(&coin->RTmutex);
9 years ago
if ( (rdata= dest->H.data) != 0 )
{
i = 0;
8 years ago
if ( coin->RTheight != coin->lastRTheight )
9 years ago
changed++;
else
{
9 years ago
B = RAMCHAIN_PTR(rdata,Boffset);
9 years ago
for (i=0; i<rdata->numblocks; i++)
if ( bits256_cmp(B[i].hash2,bp->hashes[i]) != 0 )
{
char str[65],str2[65]; printf("mismatched hash2 at %d %s vs %s\n",bp->bundleheight+i,bits256_str(str,B[i].hash2),bits256_str(str2,bp->hashes[i]));
changed++;
8 years ago
iguana_blockunmark(coin,bp->blocks[i],bp,i,1);
9 years ago
break;
}
}
if ( changed != 0 )
{
printf("RTramchain changed %d bundlei.%d | coin->RTheight %d != %d bp->bundleheight + %d coin->RTramchain.H.data->numblocks\n",coin->RTheight,i,coin->RTheight,bp->bundleheight,rdata->numblocks);
iguana_RTramchainfree(coin,bp);
}
}
if ( coin->RTramchain.H.data == 0 )
{
iguana_ramchainopen(fname,coin,dest,&coin->RTmem,&coin->RThashmem,bp->bundleheight,bp->hashes[0]);
8 years ago
printf("ALLOC RTramchain.(%s) RTrdata %p rdata.%p\n",fname,coin->RTramchain.H.data,bp->ramchain.H.data);
9 years ago
dest->H.txidind = dest->H.unspentind = dest->H.spendind = dest->pkind = dest->H.data->firsti;
dest->externalind = dest->H.stacksize = 0;
dest->H.scriptoffset = 1;
if ( 1 )
{
for (i=0; i<bp->hdrsi; i++)
if ( (tmpbp= coin->bundles[i]) != 0 )
{
8 years ago
iguana_volatilespurge(coin,&tmpbp->ramchain);
9 years ago
iguana_volatilesmap(coin,&tmpbp->ramchain);
}
sleep(1);
}
}
8 years ago
//portable_mutex_unlock(&coin->RTmutex);
8 years ago
#endif
9 years ago
}
void iguana_rdataset(struct iguana_ramchain *dest,struct iguana_ramchaindata *rdest,struct iguana_ramchain *src)
{
8 years ago
//return;
8 years ago
#ifdef ENABLE_RAMCHAIN
9 years ago
*dest = *src;
dest->H.data = rdest;
*rdest = *src->H.data;
rdest->numpkinds = src->pkind;
rdest->numexternaltxids = src->externalind;
rdest->numtxids = src->H.txidind;
rdest->numunspents = src->H.unspentind;
rdest->numspends = src->H.spendind;
//printf("RT set numtxids.%u numspends.%u\n",rdest->numtxids,rdest->numspends);
8 years ago
#endif
9 years ago
}
void iguana_rdatarestore(struct iguana_ramchain *dest,struct iguana_ramchaindata *rdest,struct iguana_ramchain *src)
{
8 years ago
//return;
8 years ago
#ifdef ENABLE_RAMCHAIN
9 years ago
*src = *dest;
*src->H.data = *rdest;
src->pkind = rdest->numpkinds;
src->externalind = rdest->numexternaltxids;
src->H.txidind = rdest->numtxids;
src->H.unspentind = rdest->numunspents;
src->H.spendind = rdest->numspends;
printf("RT restore numtxids.%u numspends.%u\n",rdest->numtxids,rdest->numspends);
8 years ago
#endif
9 years ago
}
void iguana_RThdrs(struct iguana_info *coin,struct iguana_bundle *bp,int32_t numaddrs)
{
8 years ago
//return;
8 years ago
#ifdef ENABLE_RAMCHAIN
9 years ago
int32_t datalen,i; uint8_t serialized[512]; char str[65]; struct iguana_peer *addr;
8 years ago
if ( coin->peers == 0 )
return;
8 years ago
datalen = iguana_gethdrs(coin,serialized,coin->chain->gethdrsmsg,bits256_str(str,bp->hashes[0]));
9 years ago
for (i=0; i<numaddrs && i<coin->peers->numranked; i++)
9 years ago
{
8 years ago
queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(bits256_str(str,bp->hashes[0])),1);
8 years ago
if ( coin->chain->hasheaders == 0 )
queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(bits256_str(str,coin->blocks.hwmchain.RO.hash2)),1);
8 years ago
if ( (addr= coin->peers->ranked[i]) != 0 && addr->usock >= 0 && addr->dead == 0 && datalen > 0 )
9 years ago
{
iguana_send(coin,addr,serialized,datalen);
8 years ago
//addr->pendhdrs++;
9 years ago
}
}
8 years ago
#endif
9 years ago
}
8 years ago
void iguana_RTspendvectors(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_bundle *bp)
9 years ago
{
8 years ago
//return;
8 years ago
#ifdef ENABLE_RAMCHAIN
9 years ago
int32_t iterate,lasti,num,hdrsi,orignumemit; struct iguana_ramchain R; struct iguana_ramchaindata RDATA;
if ( bp->hdrsi <= 0 )
return;
8 years ago
printf("RTspendvectors [%d]\n",bp->hdrsi);
9 years ago
bp->ramchain = coin->RTramchain;
iguana_rdataset(&R,&RDATA,&coin->RTramchain);
if ( (lasti= (coin->RTheight - ((coin->RTheight/bp->n)*bp->n))) >= bp->n-1 )
lasti = bp->n - 1;
orignumemit = bp->numtmpspends;
iterate = 0;
8 years ago
if ( iguana_spendvectors(myinfo,coin,bp,&coin->RTramchain,coin->RTstarti%coin->chain->bundlesize,lasti,0,iterate) < 0 )
9 years ago
{
printf("RTutxo error -> RTramchainfree\n");
coin->RTdatabad = 1;
return;
}
else
{
9 years ago
//printf("RTspendvectors calculated to %d [%d]\n",coin->RTheight,bp->hdrsi);
9 years ago
bp->converted = 1;
for (hdrsi=num=0; hdrsi<bp->hdrsi; hdrsi++)
{
#ifdef __APPLE__
if ( coin->bundles[hdrsi]->lastprefetch == 0 )
{
iguana_ramchain_prefetch(coin,&coin->bundles[hdrsi]->ramchain,2);
coin->bundles[hdrsi]->lastprefetch = (uint32_t)time(NULL);
}
#endif
num += iguana_convert(coin,IGUANA_NUMHELPERS,coin->bundles[hdrsi],1,orignumemit);
}
9 years ago
//printf("RTspendvectors converted.%d to %d\n",num,coin->RTheight);
8 years ago
//iguana_rdatarestore(&R,&RDATA,&bp->ramchain);
9 years ago
bp->converted = (uint32_t)time(NULL);
8 years ago
if ( iguana_balancegen(coin,1,bp,coin->RTstarti,coin->RTheight > 0 ? coin->RTheight-1 : bp->bundleheight+bp->n-1,orignumemit) < 0 )
9 years ago
{
printf("balancegen error\n");
9 years ago
coin->RTdatabad = 1;
9 years ago
}
9 years ago
else if ( coin->RTgenesis == 0 && coin->firstRTgenesis == 0 )
9 years ago
coin->firstRTgenesis++, printf(">>>>>> IGUANA %s READY FOR REALTIME RPC <<<<<<\n",coin->symbol);
9 years ago
//printf("iguana_balancegen [%d] (%d to %d)\n",bp->hdrsi,coin->RTstarti,(coin->RTheight-1)%bp->n);
8 years ago
coin->RTstarti = coin->RTheight;
9 years ago
}
8 years ago
#endif
9 years ago
}
8 years ago
int32_t iguana_realtime_update(struct supernet_info *myinfo,struct iguana_info *coin)
9 years ago
{
8 years ago
int32_t flag = 0;
//return(0);
8 years ago
#ifdef ENABLE_RAMCHAIN
9 years ago
double startmillis0; static double totalmillis0; static int32_t num0;
8 years ago
struct iguana_bundle *bp; struct iguana_ramchaindata *rdata; int32_t offset,bundlei,i,n; bits256 hash2,*ptr; struct iguana_peer *addr;
9 years ago
struct iguana_block *block=0; struct iguana_blockRO *B; struct iguana_ramchain *dest=0,blockR;
8 years ago
if ( coin->peers == 0 && coin->virtualchain == 0 )
8 years ago
return(0);
8 years ago
offset = 0;//(strcmp("BTC",coin->symbol) != 0);
8 years ago
if ( coin->RTheight >= (coin->current->hdrsi+1)*coin->chain->bundlesize )
8 years ago
{
8 years ago
printf("inversion RT %d >= %d\n",coin->RTheight,(coin->current->hdrsi+1)*coin->chain->bundlesize);
8 years ago
coin->lastRTheight = coin->RTheight = coin->current->hdrsi*coin->chain->bundlesize;
iguana_utxoaddrs_purge(coin);
8 years ago
}
9 years ago
if ( coin->current != 0 && (coin->blocks.hwmchain.height % coin->chain->bundlesize) == coin->chain->bundlesize-1 && coin->blocks.hwmchain.height/coin->chain->bundlesize == coin->longestchain/coin->chain->bundlesize )
9 years ago
{
block = coin->current->blocks[coin->current->n - 1];
if ( _iguana_chainlink(coin,block) <= 0 )
{
8 years ago
printf("RT edge case couldnt link\n");
9 years ago
}
8 years ago
else
{
printf("RT edge case.%d\n",block->height);
if ( (bp= coin->bundles[coin->RTheight / coin->chain->bundlesize]) != 0 )
iguana_spendvectors(myinfo,coin,bp,&bp->ramchain,0,bp->n,0,0);
iguana_update_balances(coin);
}
9 years ago
}
8 years ago
if ( coin->spendvectorsaved <= 1 )
{
8 years ago
//printf("%s spendvectorsaved not yet\n",coin->symbol);
8 years ago
usleep(100000);
return(0);
}
8 years ago
//portable_mutex_lock(&coin->RTmutex);
9 years ago
for (i=0; i<coin->bundlescount-1; i++)
{
8 years ago
if ( (bp= coin->bundles[i]) != 0 && (i > 0 && bp->utxofinish == 0) && bp != coin->current )
9 years ago
{
8 years ago
if ( iguana_spendvectors(myinfo,coin,bp,&bp->ramchain,0,bp->n,0,0) < 0 )
9 years ago
{
8 years ago
//portable_mutex_unlock(&coin->RTmutex);
8 years ago
printf("error generating spendvectors.[%d], skipping\n",i);
return(0);
9 years ago
} // else printf("generated UTXO.[%d]\n",i);
coin->spendvectorsaved = 1;
}
}
8 years ago
//portable_mutex_unlock(&coin->RTmutex);
9 years ago
bp = coin->current;
8 years ago
if ( bp == 0 )//|| iguana_validated(coin) < bp->hdrsi )
9 years ago
{
8 years ago
//printf("bp.%p validated.%d vs hdrsi.%d\n",bp,iguana_validated(coin),bp->hdrsi);
9 years ago
return(0);
9 years ago
}
8 years ago
if ( 0 && coin->RTheight > 0 && coin->spendvectorsaved != 1 && coin->bundlescount-1 != coin->balanceswritten )
9 years ago
{
printf("RT mismatch %d != %d\n",coin->bundlescount-1,coin->balanceswritten);
8 years ago
iguana_RTramchainfree(coin,coin->current);
9 years ago
coin->spendvectorsaved = 0;
8 years ago
coin->lastRTheight = coin->RTheight = 0;
iguana_utxoaddrs_purge(coin);
8 years ago
/*while ( coin->spendvectorsaved <= 1 )
8 years ago
{
fprintf(stderr,"wait for spendvectorsaved\n");
sleep(3);
}*/
9 years ago
return(0);
}
8 years ago
if ( coin->RTdatabad == 0 && bp->hdrsi >= (coin->longestchain/coin->chain->bundlesize)-1 && bp->hdrsi >= coin->balanceswritten-2 && ((coin->RTheight < coin->blocks.hwmchain.height-offset && time(NULL) > bp->lastRT) || time(NULL) > bp->lastRT+1) ) //coin->RTheight >= bp->bundleheight && coin->RTheight < bp->bundleheight+bp->n &&
9 years ago
{
8 years ago
if ( coin->RTheight < bp->hdrsi*coin->chain->bundlesize )
8 years ago
{
coin->lastRTheight = coin->RTheight = bp->hdrsi*coin->chain->bundlesize;
iguana_utxoaddrs_purge(coin);
}
9 years ago
if ( (block= bp->blocks[0]) == 0 || block->txvalid == 0 || block->mainchain == 0 )
{
if ( block != 0 )
{
if ( _iguana_chainlink(coin,block) <= 0 )
{
iguana_blockunmark(coin,block,bp,0,0);
bp->issued[0] = 0;
hash2 = bp->hashes[0];
//char str[65]; printf("RT[0] [%d:%d] %s %p\n",bp->hdrsi,0,bits256_str(str,hash2),block);
8 years ago
if ( coin->peers != 0 )
{
addr = coin->peers->ranked[rand() % 8];
if ( addr != 0 && addr->usock >= 0 && addr->dead == 0 )
iguana_sendblockreqPT(coin,addr,bp,0,hash2,0);
}
9 years ago
}
}
}
8 years ago
//char str[65]; printf("check longest.%d RTheight.%d hwm.%d %s %p\n",coin->longestchain,coin->RTheight,coin->blocks.hwmchain.height,bits256_str(str,bp->hashes[0]),block);
9 years ago
if ( bits256_cmp(coin->RThash1,bp->hashes[1]) != 0 )
coin->RThash1 = bp->hashes[1];
8 years ago
//bp->lastRT = (uint32_t)time(NULL);
8 years ago
if ( coin->peers != 0 && coin->RTheight <= coin->longestchain-offset && coin->peers->numranked > 0 && time(NULL) > coin->RThdrstime+16 )
9 years ago
{
9 years ago
iguana_RThdrs(coin,bp,coin->peers->numranked);
8 years ago
coin->RThdrstime = (uint32_t)time(NULL);
9 years ago
}
bp->lastRT = (uint32_t)time(NULL);
iguana_RTramchainalloc("RTbundle",coin,bp);
bp->isRT = 1;
8 years ago
//printf("%s rdata.%p RTheight.%d hwm.%d RTdatabad.%d\n",coin->symbol,coin->RTramchain.H.data,coin->RTheight,coin->blocks.hwmchain.height,coin->RTdatabad);
8 years ago
while ( (rdata= coin->RTramchain.H.data) != 0 && coin->RTheight <= coin->blocks.hwmchain.height-offset && coin->RTdatabad == 0 )
9 years ago
{
dest = &coin->RTramchain;
9 years ago
B = RAMCHAIN_PTR(rdata,Boffset);
9 years ago
bundlei = (coin->RTheight % coin->chain->bundlesize);
if ( (block= iguana_bundleblock(coin,&hash2,bp,bundlei)) != 0 )
8 years ago
{
9 years ago
iguana_bundlehashadd(coin,bp,bundlei,block);
8 years ago
//printf("RT.%d vs hwm.%d starti.%d bp->n %d block.%p/%p ramchain.%p databad.%d prevnonz.%d\n",coin->RTheight,coin->blocks.hwmchain.height,coin->RTstarti,bp->n,block,bp->blocks[bundlei],dest->H.data,coin->RTdatabad,bits256_nonz(block->RO.prev_block));
8 years ago
}
else
{
8 years ago
//printf("cant find bundleblock [%d:%d]\n",bp->hdrsi,bundlei);
8 years ago
iguana_blockQ("RTmissing",coin,bp,bundlei,hash2,1);
8 years ago
break;
8 years ago
}
8 years ago
if ( coin->RTdatabad == 0 && block != 0 && (block->height == 0 || bits256_nonz(block->RO.prev_block) != 0) )
9 years ago
{
8 years ago
//printf("bundlei.%d blockht.%d RTheight.%d\n",bundlei,block->height,coin->RTheight);
9 years ago
iguana_blocksetcounters(coin,block,dest);
startmillis0 = OS_milliseconds();
8 years ago
if ( iguana_ramchainfile(myinfo,coin,dest,&blockR,bp,bundlei,block) == 0 )
9 years ago
{
for (i=0; i<bp->n; i++)
if ( GETBIT(bp->haveblock,i) == 0 )
bp->issued[i] = 0;
if ( (n= iguana_bundleissuemissing(coin,bp,3,1.)) > 0 )
printf("RT issued %d priority requests [%d] to unstick stuckiters.%d\n",n,bp->hdrsi,coin->stuckiters);
for (i=bundlei; i<bp->n; i++)
{
block = iguana_bundleblock(coin,&hash2,bp,i);
if ( bits256_nonz(hash2) != 0 && (block == 0 || block->txvalid == 0) )
{
uint8_t serialized[512]; int32_t len; struct iguana_peer *addr;
8 years ago
//char str[65]; printf("RT error [%d:%d] %s %p\n",bp->hdrsi,i,bits256_str(str,hash2),block);
8 years ago
if ( coin->peers != 0 )
{
addr = coin->peers->ranked[rand() % 8];
if ( addr != 0 && addr->usock >= 0 && addr->dead == 0 && (len= iguana_getdata(coin,serialized,MSG_BLOCK,&hash2,1)) > 0 )
iguana_send(coin,addr,serialized,len);
}
9 years ago
coin->RTgenesis = 0;
}
8 years ago
if ( bits256_nonz(hash2) != 0 )
iguana_blockQ("RTerr",coin,bp,i,hash2,1);
8 years ago
//break;
9 years ago
}
return(-1);
} else iguana_ramchain_free(coin,&blockR,1);
B[bundlei] = block->RO;
totalmillis0 += (OS_milliseconds() - startmillis0);
num0++;
flag++;
//coin->blocks.RO[bp->bundleheight+bundlei] = block->RO;
9 years ago
coin->RTheight++;
8 years ago
coin->lastRTheight = coin->RTheight;
8 years ago
//printf(">>>> RT.%d hwm.%d L.%d T.%d U.%d S.%d P.%d X.%d -> size.%ld\n",coin->RTheight,coin->blocks.hwmchain.height,coin->longestchain,dest->H.txidind,dest->H.unspentind,dest->H.spendind,dest->pkind,dest->externalind,(long)dest->H.data->allocsize);
9 years ago
if ( coin->RTramchain.H.data != 0 )
coin->RTramchain.H.data->numblocks = bundlei + 1;
else break;
} else break;
}
}
8 years ago
else
{
8 years ago
if ( coin->virtualchain == 0 )
{
8 years ago
//printf("%s skip RT.(%d %d %d %d %d %d %d %u)\n",coin->symbol,coin->RTdatabad,bp->hdrsi,coin->longestchain/coin->chain->bundlesize,coin->balanceswritten,coin->RTheight,bp->bundleheight,coin->blocks.hwmchain.height,bp->lastRT);
//sleep(1);
8 years ago
}
8 years ago
}
9 years ago
n = 0;
8 years ago
if ( coin->RTdatabad == 0 && dest != 0 && flag != 0 && coin->RTheight >= coin->blocks.hwmchain.height-offset )
9 years ago
{
8 years ago
printf("ramchainiterate.[%d] ave %.2f micros, total %.2f seconds starti.%d num.%d\n",num0,(totalmillis0*1000.)/num0,totalmillis0/1000.,coin->RTstarti,coin->RTheight%bp->n);
9 years ago
if ( (n= iguana_walkchain(coin,1)) == coin->RTheight-1+offset )
9 years ago
{
//printf("RTgenesis verified\n");
8 years ago
if ( (coin->RTheight % coin->chain->bundlesize) > 3 )
9 years ago
{
8 years ago
//portable_mutex_lock(&coin->RTmutex);
8 years ago
iguana_RTspendvectors(myinfo,coin,bp);
8 years ago
//portable_mutex_unlock(&coin->RTmutex);
9 years ago
coin->RTgenesis = (uint32_t)time(NULL);
}
9 years ago
}
else
{
9 years ago
printf("walkchain error n.%d != %d\n",n,coin->RTheight-1+offset);
9 years ago
coin->RTdatabad = 1;
}
9 years ago
}
if ( dest != 0 && flag != 0 )
8 years ago
printf("<<<< flag.%d RT.%d:%d hwm.%d L.%d T.%d U.%d S.%d P.%d X.%d -> size.%ld balance %.8f + %.8f - %.8f = supply %.8f\n",flag,coin->RTheight,n,coin->blocks.hwmchain.height,coin->longestchain,dest->H.txidind,dest->H.unspentind,dest->H.spendind,dest->pkind,dest->externalind,dest->H.data!=0?(long)dest->H.data->allocsize:-1,dstr(coin->histbalance),dstr(coin->RTcredits),dstr(coin->RTdebits),dstr(coin->histbalance + coin->RTcredits - coin->RTdebits));
9 years ago
if ( coin->RTdatabad != 0 )
{
8 years ago
bits256 lastbundle;
8 years ago
//portable_mutex_lock(&coin->RTmutex);
8 years ago
printf("START DATABAD fixing\n");
9 years ago
iguana_RTramchainfree(coin,bp);
8 years ago
if ( coin->RTdatabad < 0 )
{
memset(lastbundle.bytes,0,sizeof(lastbundle));
8 years ago
iguana_initfinal(myinfo,coin,lastbundle);
8 years ago
}
9 years ago
coin->RTdatabad = 0;
9 years ago
//memset(bp->hashes,0,sizeof(bp->hashes));
memset(bp->blocks,0,sizeof(bp->blocks));
if ( 0 && bp->speculative != 0 )
{
ptr = bp->speculative;
bp->speculative = 0;
memset(ptr,0,sizeof(*bp->speculative)*bp->n);
myfree(ptr,(bp->n+1)*sizeof(*bp->speculative));
}
iguana_RTramchainalloc("RTbundle",coin,bp);
8 years ago
printf("DONE DATABAD fixing\n");
8 years ago
//portable_mutex_unlock(&coin->RTmutex);
9 years ago
}
8 years ago
#endif
8 years ago
return(flag);
9 years ago
}
8 years ago
8 years ago
void iguana_RTunspent(struct iguana_info *coin,struct iguana_block *block,int64_t polarity,char *coinaddr,uint8_t *rmd160,int32_t type,uint8_t *script,int32_t scriptlen,bits256 txid,int32_t vout,int64_t value)
8 years ago
{
8 years ago
int32_t i;
// fill in array element and update counters
8 years ago
if ( 0 && strcmp("BTC",coin->symbol) != 0 )
8 years ago
{
for (i=0; i<20; i++)
printf("%02x",rmd160[i]);
printf(" %s vout.%d %.8f %lld\n",coinaddr,vout,dstr(value),(long long)polarity);
}
8 years ago
}
8 years ago
void iguana_RTspend(struct iguana_info *coin,struct iguana_block *block,int64_t polarity,uint8_t *script,int32_t scriptlen,bits256 txid,int32_t vini,bits256 prev_hash,int32_t prev_vout)
8 years ago
{
char str[65],str2[65];
8 years ago
// fill in array element and update counters
8 years ago
if ( 0 && strcmp("BTC",coin->symbol) != 0 )
8 years ago
printf("%s vini.%d spend.(%s/v%d) %lld\n",bits256_str(str,txid),vini,bits256_str(str2,prev_hash),prev_vout,(long long)polarity);
}
8 years ago
void iguana_RTtxid(struct iguana_info *coin,struct iguana_block *block,int64_t polarity,int32_t txi,int32_t txn_count,bits256 txid,int32_t numvouts,int32_t numvins,uint32_t locktime,uint32_t version,uint32_t timestamp,void *unspents,void *spends)
8 years ago
{
8 years ago
char str[65];
// add to hashtable block <-> txids[]
8 years ago
if ( 0 && strcmp("BTC",coin->symbol) != 0 )
8 years ago
printf("%s txid.(%s) vouts.%d vins.%d version.%d lock.%u t.%u %lld\n",coin->symbol,bits256_str(str,txid),numvouts,numvins,version,locktime,timestamp,(long long)polarity);
8 years ago
}
8 years ago
void iguana_RTreset(struct iguana_info *coin)
{
iguana_utxoaddrs_purge(coin);
iguana_utxoupdate(coin,-1,0,0,0,0,-1,0); // free hashtables
8 years ago
coin->lastRTheight = 0;
coin->RTheight = coin->firstRTheight;
8 years ago
printf("%s RTreset %d\n",coin->symbol,coin->RTheight);
8 years ago
}
8 years ago
void iguana_RTunmap(uint8_t *ptr,uint32_t len)
8 years ago
{
8 years ago
OS_releasemap(&ptr[-2*sizeof(len)],len+2*sizeof(len));
8 years ago
}
8 years ago
void *iguana_RTrawdata(struct iguana_info *coin,bits256 hash2,uint8_t *data,int32_t *recvlenp,int32_t *numtxp,int32_t checkonly)
8 years ago
{
8 years ago
FILE *fp; char fname[1024],str[65]; long filesize; int32_t len; uint8_t *ptr; uint32_t i,nonz,checknumtx,checklen;
8 years ago
sprintf(fname,"%s/%s/RT/%s.raw",GLOBAL_TMPDIR,coin->symbol,bits256_str(str,hash2));
8 years ago
OS_compatible_path(fname);
8 years ago
if ( *recvlenp == -1 )
OS_removefile(fname,0);
else
8 years ago
{
8 years ago
if ( (checkonly != 0 || *recvlenp > 0) && (fp= fopen(fname,"rb")) != 0 )
8 years ago
{
fseek(fp,0,SEEK_END);
filesize = ftell(fp);
rewind(fp);
8 years ago
if ( fread(&len,1,sizeof(len),fp) == sizeof(len) && len == filesize-sizeof(int32_t)*2 )
8 years ago
{
fclose(fp);
8 years ago
//printf("already have %s\n",bits256_str(str,hash2));
*recvlenp = 0;
if ( checkonly != 0 )
return((void *)"already have rawdata");
8 years ago
return(0);
}
8 years ago
//printf("len.%d filesize.%ld\n",len,filesize);
8 years ago
fclose(fp);
}
8 years ago
else if ( checkonly == 0 )
8 years ago
{
8 years ago
if ( *recvlenp > 0 )
8 years ago
{
8 years ago
if ( coin->RTheight == 0 && coin->blocks.hwmchain.height < coin->longestchain-coin->chain->bundlesize && iguana_utxofinished(coin) < coin->bundlescount-3 )
{
//printf("skip %s\n",bits256_str(str,hash2));
return(0);
}
if ( (fp= fopen(fname,"wb")) != 0 )
{
if ( fwrite(recvlenp,1,sizeof(*recvlenp),fp) != sizeof(*recvlenp) || fwrite(numtxp,1,sizeof(*numtxp),fp) != sizeof(*numtxp) || fwrite(data,1,*recvlenp,fp) != *recvlenp )
printf("error writing %s len.%d numtx.%d\n",bits256_str(str,hash2),*recvlenp,*numtxp);
fclose(fp);
//printf("numtx.%d len.%d %s\n",*numtxp,*recvlenp,fname);
} else printf("couldnt create %s\n",fname);
8 years ago
}
8 years ago
else if ( (ptr= OS_mapfile(fname,&filesize,0)) != 0 )
8 years ago
{
8 years ago
memcpy(&checklen,ptr,sizeof(checklen));
memcpy(&checknumtx,&ptr[sizeof(checklen)],sizeof(checknumtx));
*numtxp = checknumtx;
if ( checklen == (int32_t)(filesize - sizeof(checklen) - sizeof(checknumtx)) )//&& checknumtx == *numtxp )
{
for (i=nonz=0; i<checklen; i++)
if ( ptr[2*sizeof(checklen) + i] != 0 )
nonz++;
*recvlenp = (int32_t)(filesize - sizeof(checklen) - sizeof(checknumtx));
return(&ptr[sizeof(*recvlenp) + sizeof(checknumtx)]);
} else printf("checklen.%d vs %d, checknumtx %d vs %d\n",checklen,(int32_t)(filesize - sizeof(checklen) - sizeof(checknumtx)),checknumtx,*numtxp);
}
8 years ago
}
}
return(0);
}
8 years ago
8 years ago
void iguana_RTpurge(struct iguana_info *coin,int32_t lastheight)
{
8 years ago
int32_t hdrsi,bundlei,height,numtx=0,recvlen=-1; struct iguana_bundle *bp;
8 years ago
printf("start RTpurge from %d\n",lastheight - coin->chain->bundlesize*10);
8 years ago
for (height=lastheight-100000; height<lastheight; height++)
8 years ago
{
if ( height < 0 )
height = 0;
hdrsi = (height / coin->chain->bundlesize);
bundlei = (height % coin->chain->bundlesize);
if ( (bp= coin->bundles[hdrsi]) != 0 && bits256_nonz(bp->hashes[bundlei]) != 0 )
8 years ago
iguana_RTrawdata(coin,bp->hashes[bundlei],0,&recvlen,&numtx,0); // delete file
8 years ago
}
8 years ago
printf("end RTpurge.%d\n",lastheight);
8 years ago
}
8 years ago
int32_t iguana_RTiterate(struct iguana_info *coin,int32_t offset,struct iguana_block *block,int64_t polarity)
8 years ago
{
8 years ago
struct iguana_txblock txdata; uint8_t *serialized; int32_t i,n,errs=0,numtx,len; uint32_t recvlen = 0;
8 years ago
if ( (numtx= coin->RTnumtx[offset]) == 0 || (serialized= coin->RTrawdata[offset]) == 0 || (recvlen= coin->RTrecvlens[offset]) == 0 )
8 years ago
{
8 years ago
char str[65];
8 years ago
//printf("errs.%d cant load %s ht.%d polarity.%lld numtx.%d %p recvlen.%d\n",errs,bits256_str(str,block->RO.hash2),block->height,(long long)polarity,coin->RTnumtx[offset],coin->RTrawdata[offset],coin->RTrecvlens[offset]);
coin->RTrawdata[offset] = iguana_RTrawdata(coin,block->RO.hash2,0,&coin->RTrecvlens[offset],&coin->RTnumtx[offset],0);
if ( (numtx= coin->RTnumtx[offset]) == 0 || (serialized= coin->RTrawdata[offset]) == 0 || (recvlen= coin->RTrecvlens[offset]) == 0 )
{
printf("B errs.%d cant load %s ht.%d polarity.%lld numtx.%d %p recvlen.%d\n",errs,bits256_str(str,block->RO.hash2),block->height,(long long)polarity,coin->RTnumtx[offset],coin->RTrawdata[offset],coin->RTrecvlens[offset]);
8 years ago
struct iguana_peer *addr;
iguana_blockQ("RTiterate",coin,0,-1,block->RO.hash2,1);
if ( coin->peers != 0 && coin->peers->numranked > 0 )
{
for (i=0; i<coin->peers->numranked&&i<8; i++)
if ( (addr= coin->peers->ranked[i]) != 0 )
iguana_sendblockreqPT(coin,addr,0,-1,block->RO.hash2,1);
}
8 years ago
return(-1);
8 years ago
}
8 years ago
}
8 years ago
printf("%s RTiterate.%lld offset.%d numtx.%d len.%d\n",coin->symbol,(long long)polarity,offset,coin->RTnumtx[offset],coin->RTrecvlens[offset]);
8 years ago
memset(&txdata,0,sizeof(txdata));
if ( coin->RTrawmem.ptr == 0 )
iguana_meminit(&coin->RTrawmem,"RTrawmem",0,IGUANA_MAXPACKETSIZE * 2,0);
if ( coin->RTmem.ptr == 0 )
iguana_meminit(&coin->RTmem,"RTmem",0,IGUANA_MAXPACKETSIZE * 2,0);
if ( coin->RThashmem.ptr == 0 )
iguana_meminit(&coin->RThashmem,"RThashmem",0,IGUANA_MAXPACKETSIZE * 2,0);
iguana_memreset(&coin->RTrawmem), iguana_memreset(&coin->RTmem), iguana_memreset(&coin->RThashmem);
if ( (n= iguana_gentxarray(coin,&coin->RTrawmem,&txdata,&len,serialized,recvlen)) > 0 )
{
8 years ago
iguana_RTramchaindata(coin,&coin->RTmem,&coin->RThashmem,polarity,block,coin->RTrawmem.ptr,numtx);
8 years ago
return(0);
8 years ago
} else printf("gentxarray n.%d RO.txn_count.%d recvlen.%d\n",n,numtx,recvlen);
8 years ago
iguana_RTreset(coin);
return(-1);
8 years ago
}
struct iguana_block *iguana_RTblock(struct iguana_info *coin,int32_t height)
{
int32_t offset;
offset = height - coin->firstRTheight;
//printf("%s iguana_RTblock.%d offset.%d\n",coin->symbol,height,offset);
if ( offset < sizeof(coin->RTblocks)/sizeof(*coin->RTblocks) )
return(coin->RTblocks[offset]);
else printf("RTblock offset.%d too big\n",offset);
return(0);
}
8 years ago
int32_t iguana_RTblockadd(struct iguana_info *coin,struct iguana_block *block)
8 years ago
{
int32_t offset;
if ( block != 0 )
{
8 years ago
offset = block->height - coin->firstRTheight;
8 years ago
if ( coin->RTrawdata[offset] == 0 )
8 years ago
coin->RTrawdata[offset] = iguana_RTrawdata(coin,block->RO.hash2,0,&coin->RTrecvlens[offset],&coin->RTnumtx[offset],0);
8 years ago
//printf("%s RTblockadd.%d offset.%d numtx.%d len.%d\n",coin->symbol,block->height,offset,coin->RTnumtx[offset],coin->RTrecvlens[offset]);
8 years ago
block->RO.txn_count = coin->RTnumtx[offset];
8 years ago
coin->RTblocks[offset] = block;
8 years ago
if ( iguana_RTiterate(coin,offset,block,1) < 0 )
return(-1);
8 years ago
}
8 years ago
return(0);
8 years ago
}
8 years ago
int32_t iguana_RTblocksub(struct iguana_info *coin,struct iguana_block *block)
8 years ago
{
int32_t offset;
if ( block != 0 )
{
8 years ago
offset = block->height - coin->firstRTheight;
8 years ago
block->RO.txn_count = coin->RTnumtx[offset];
8 years ago
//printf("%s RTblocksub.%d offset.%d\n",coin->symbol,block->height,offset);
8 years ago
if ( iguana_RTiterate(coin,offset,block,-1) < 0 )
return(-1);
8 years ago
if ( coin->RTrawdata[offset] != 0 && coin->RTrecvlens[offset] != 0 )
iguana_RTunmap(coin->RTrawdata[offset],coin->RTrecvlens[offset]);
coin->RTrawdata[offset] = 0;
coin->RTrecvlens[offset] = 0;
8 years ago
coin->RTnumtx[offset] = 0;
8 years ago
coin->RTblocks[offset] = 0;
}
8 years ago
return(0);
8 years ago
}
void iguana_RTnewblock(struct iguana_info *coin,struct iguana_block *block)
{
int32_t i,n,height,hdrsi,bundlei; struct iguana_block *addblock,*subblock; struct iguana_bundle *bp;
if ( coin->RTheight > 0 )
{
if ( block->height > coin->lastRTheight )
{
n = (block->height - coin->RTheight) + 1;
8 years ago
for (i=0; i<n; i++)
8 years ago
{
height = (coin->RTheight + i);
hdrsi = (height / coin->chain->bundlesize);
bundlei = (height % coin->chain->bundlesize);
if ( (bp= coin->bundles[hdrsi]) != 0 && (addblock= bp->blocks[bundlei]) != 0 && addblock->height == coin->RTheight+i )
{
8 years ago
if ( iguana_RTblockadd(coin,addblock) < 0 )
8 years ago
break;
8 years ago
coin->lastRTheight = addblock->height;
8 years ago
}
else
{
printf("missing RTaddblock at i.%d RTheight.%d vs %p %d\n",i,coin->RTheight,addblock,addblock!=0?addblock->height:-1);
8 years ago
break;
8 years ago
}
8 years ago
}
8 years ago
coin->RTheight += i;
8 years ago
printf(">= RTnewblock RTheight %d prev %d\n",coin->RTheight,coin->lastRTheight);
}
else if ( block->height == coin->lastRTheight )
{
if ( (subblock= iguana_RTblock(coin,block->height)) != 0 && subblock != block )
{
8 years ago
if ( iguana_RTblocksub(coin,subblock) < 0 || iguana_RTblockadd(coin,block) < 0 )
return;
8 years ago
printf("== RTnewblock RTheight %d prev %d\n",coin->RTheight,coin->lastRTheight);
}
}
else
{
if ( block->height < coin->firstRTheight )
{
if ( coin->lastRTheight > 0 )
printf("ht.%d reorg past firstRTheight.%d\n",block->height,coin->firstRTheight);
iguana_RTreset(coin);
}
else
{
while ( coin->lastRTheight >= block->height )
8 years ago
{
if ( iguana_RTblocksub(coin,iguana_RTblock(coin,coin->lastRTheight--)) < 0 )
return;
}
if ( iguana_RTblockadd(coin,block) < 0 )
return;
8 years ago
coin->lastRTheight = block->height;
}
}
}
}