Faster init test
This commit is contained in:
133
src/komodo.h
133
src/komodo.h
@@ -192,7 +192,138 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char
|
||||
return(func);
|
||||
} else return(-1);
|
||||
}
|
||||
|
||||
|
||||
int32_t memread(void *dest,int32_t size,uint8_t *filedata,long *fposp,long datalen)
|
||||
{
|
||||
if ( *fposp+size <= datalen )
|
||||
{
|
||||
memcpy(dest,&filedata[*fposp],size);
|
||||
(*fposp) += size;
|
||||
return(size);
|
||||
}
|
||||
return(-1);
|
||||
}
|
||||
|
||||
long komodo_parsestatefiledata(struct komodo_state *sp,uint8_t *filedata,long *fposp,long datalen,char *symbol,char *dest)
|
||||
{
|
||||
static int32_t errs;
|
||||
int32_t func,ht,notarized_height,num,matched=0; uint256 notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33]; long fpos = *fposp;
|
||||
if ( fpos < datalen )
|
||||
{
|
||||
func = filedata[fpos++];
|
||||
if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 )
|
||||
matched = 1;
|
||||
else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0);
|
||||
if ( memread(&ht,sizeof(ht),filedata,&fpos,datalen) != sizeof(ht) )
|
||||
errs++;
|
||||
if ( func == 'P' )
|
||||
{
|
||||
if ( (num= filedata[fpos++]) <= 64 )
|
||||
{
|
||||
if ( memread(pubkeys,33*num,filedata,&fpos,datalen) != 33*num )
|
||||
errs++;
|
||||
else
|
||||
{
|
||||
//printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht);
|
||||
if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) )
|
||||
komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
|
||||
}
|
||||
} else printf("illegal num.%d\n",num);
|
||||
}
|
||||
else if ( func == 'N' )
|
||||
{
|
||||
if ( memread(¬arized_height,sizeof(notarized_height),filedata,&fpos,datalen) != sizeof(notarized_height) )
|
||||
errs++;
|
||||
if ( memread(¬arized_hash,sizeof(notarized_hash),filedata,&fpos,datalen) != sizeof(notarized_hash) )
|
||||
errs++;
|
||||
if ( memread(¬arized_desttxid,sizeof(notarized_desttxid),filedata,&fpos,datalen) != sizeof(notarized_desttxid) )
|
||||
errs++;
|
||||
//if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && sp != 0 )
|
||||
printf("%s load[%s.%d -> %s] NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,dest,notarized_height,notarized_hash.ToString().c_str());
|
||||
//if ( matched != 0 ) global independent states -> inside *sp
|
||||
komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height);
|
||||
}
|
||||
else if ( func == 'U' ) // deprecated
|
||||
{
|
||||
uint8_t n,nid; uint256 hash; uint64_t mask;
|
||||
n = filedata[fpos++];
|
||||
nid = filedata[fpos++];
|
||||
//printf("U %d %d\n",n,nid);
|
||||
if ( memread(&mask,sizeof(mask),filedata,&fpos,datalen) != sizeof(mask) )
|
||||
errs++;
|
||||
if ( memread(&hash,sizeof(hash),filedata,&fpos,datalen) != sizeof(hash) )
|
||||
errs++;
|
||||
}
|
||||
else if ( func == 'K' )
|
||||
{
|
||||
int32_t kheight;
|
||||
if ( memread(&kheight,sizeof(kheight),filedata,&fpos,datalen) != sizeof(kheight) )
|
||||
errs++;
|
||||
komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0);
|
||||
}
|
||||
else if ( func == 'T' )
|
||||
{
|
||||
int32_t kheight,ktimestamp;
|
||||
if ( memread(&kheight,sizeof(kheight),filedata,&fpos,datalen) != sizeof(kheight) )
|
||||
errs++;
|
||||
if ( memread(&ktimestamp,sizeof(ktimestamp),filedata,&fpos,datalen) != sizeof(ktimestamp) )
|
||||
errs++;
|
||||
//if ( matched != 0 ) global independent states -> inside *sp
|
||||
//printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
|
||||
komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp);
|
||||
}
|
||||
else if ( func == 'R' )
|
||||
{
|
||||
uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[16384];
|
||||
if ( memread(&txid,sizeof(txid),filedata,&fpos,datalen) != sizeof(txid) )
|
||||
errs++;
|
||||
if ( memread(&v,sizeof(v),filedata,&fpos,datalen) != sizeof(v) )
|
||||
errs++;
|
||||
if ( memread(&ovalue,sizeof(ovalue),filedata,&fpos,datalen) != sizeof(ovalue) )
|
||||
errs++;
|
||||
if ( memread(&olen,sizeof(olen),filedata,&fpos,datalen) != sizeof(olen) )
|
||||
errs++;
|
||||
if ( olen < sizeof(opret) )
|
||||
{
|
||||
if ( memread(opret,olen,filedata,&fpos,datalen) != olen )
|
||||
errs++;
|
||||
if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && matched != 0 )
|
||||
{
|
||||
int32_t i; for (i=0; i<olen; i++)
|
||||
printf("%02x",opret[i]);
|
||||
printf(" %s.%d load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,ht,symbol,opret[0],olen,(double)ovalue/COIN);
|
||||
}
|
||||
komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); // global shared state -> global PAX
|
||||
} else
|
||||
{
|
||||
int32_t i;
|
||||
for (i=0; i<olen; i++)
|
||||
filedata[fpos++];
|
||||
//printf("illegal olen.%u\n",olen);
|
||||
}
|
||||
}
|
||||
else if ( func == 'D' )
|
||||
{
|
||||
printf("unexpected function D[%d]\n",ht);
|
||||
}
|
||||
else if ( func == 'V' )
|
||||
{
|
||||
int32_t numpvals; uint32_t pvals[128];
|
||||
numpvals = filedata[fpos++];
|
||||
if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && memread(pvals,(int32_t)(sizeof(uint32_t)*numpvals),filedata,&fpos,datalen) == numpvals*sizeof(uint32_t) )
|
||||
{
|
||||
//if ( matched != 0 ) global shared state -> global PVALS
|
||||
//printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
|
||||
komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
|
||||
//printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
|
||||
} else printf("error loading pvals[%d]\n",numpvals);
|
||||
}
|
||||
else printf("[%s] %s illegal func.(%d %c)\n",ASSETCHAINS_SYMBOL,symbol,func,func);
|
||||
*fposp = fpos;
|
||||
return(fpos);
|
||||
} else return(-1);
|
||||
}
|
||||
|
||||
void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t KMDheight,uint32_t KMDtimestamp,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout)
|
||||
{
|
||||
static FILE *fp; static int32_t errs,didinit;
|
||||
|
||||
@@ -1351,11 +1351,59 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3
|
||||
return(typestr);
|
||||
}
|
||||
|
||||
void *OS_loadfile(char *fname,char **bufp,long *lenp,long *allocsizep)
|
||||
{
|
||||
FILE *fp;
|
||||
long filesize,buflen = *allocsizep;
|
||||
char *buf = *bufp;
|
||||
*lenp = 0;
|
||||
if ( (fp= fopen(fname,"rb")) != 0 )
|
||||
{
|
||||
fseek(fp,0,SEEK_END);
|
||||
filesize = ftell(fp);
|
||||
if ( filesize == 0 )
|
||||
{
|
||||
fclose(fp);
|
||||
*lenp = 0;
|
||||
printf("OS_loadfile null size.(%s)\n",fname);
|
||||
return(0);
|
||||
}
|
||||
if ( filesize > buflen-1 )
|
||||
{
|
||||
*allocsizep = filesize+1;
|
||||
*bufp = buf = realloc(buf,(long)*allocsizep);
|
||||
}
|
||||
rewind(fp);
|
||||
if ( buf == 0 )
|
||||
printf("Null buf ???\n");
|
||||
else
|
||||
{
|
||||
if ( fread(buf,1,(long)filesize,fp) != (unsigned long)filesize )
|
||||
printf("error reading filesize.%ld\n",(long)filesize);
|
||||
buf[filesize] = 0;
|
||||
}
|
||||
fclose(fp);
|
||||
*lenp = filesize;
|
||||
//printf("loaded.(%s)\n",buf);
|
||||
} //else printf("OS_loadfile couldnt load.(%s)\n",fname);
|
||||
return(buf);
|
||||
}
|
||||
|
||||
uint8_t *OS_fileptr(long *allocsizep,char *fname)
|
||||
{
|
||||
long filesize = 0; char *buf = 0; void *retptr;
|
||||
*allocsizep = 0;
|
||||
retptr = OS_loadfile(fname,&buf,&filesize,allocsizep);
|
||||
return(retptr);
|
||||
}
|
||||
|
||||
long komodo_parsestatefiledata(struct komodo_state *sp,uint8_t *filedata,long fpos,long datalen,char *symbol,char *dest);
|
||||
|
||||
void komodo_passport_iteration()
|
||||
{
|
||||
static long lastpos[34]; static char userpass[33][1024]; static uint32_t lasttime,callcounter;
|
||||
int32_t maxseconds = 10;
|
||||
FILE *fp; int32_t baseid,limit,n,ht,isrealtime,expired,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[16],dest[16]; uint32_t buf[3],starttime; cJSON *infoobj,*result; uint64_t RTmask = 0;
|
||||
FILE *fp; uint8_t *filedata; long fpos,datalen; int32_t baseid,limit,n,ht,isrealtime,expired,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[16],dest[16]; uint32_t buf[3],starttime; cJSON *infoobj,*result; uint64_t RTmask = 0;
|
||||
//printf("PASSPORT.(%s)\n",ASSETCHAINS_SYMBOL);
|
||||
expired = 0;
|
||||
while ( KOMODO_INITDONE == 0 )
|
||||
@@ -1411,7 +1459,17 @@ void komodo_passport_iteration()
|
||||
komodo_nameset(symbol,dest,base);
|
||||
sp = komodo_stateptrget(symbol);
|
||||
n = 0;
|
||||
if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 )
|
||||
if ( (filedata= OS_fileptr(&datalen,fname)) != 0 )
|
||||
{
|
||||
fpos = 0;
|
||||
while ( komodo_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest) >= 0 )
|
||||
;
|
||||
printf("took %d seconds to process %s %ldKB\n",(int32_t)(time(NULL)-starttime),fname,datalen/1024);
|
||||
lastpos[baseid] = fpos;
|
||||
free(filedata), filedata = 0;
|
||||
datalen = 0;
|
||||
}
|
||||
else if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 )
|
||||
{
|
||||
fseek(fp,0,SEEK_END);
|
||||
if ( ftell(fp) > lastpos[baseid] )
|
||||
|
||||
Reference in New Issue
Block a user