// Copyright (c) 2016-2024 The Hush developers // Distributed under the GPLv3 software license, see the accompanying // file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html /****************************************************************************** * Copyright © 2014-2019 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. * * * ******************************************************************************/ // paxdeposit equivalent in reverse makes opreturn and HUSH does the same in reverse #include "hush_defs.h" void pax_keyset(uint8_t *buf,uint256 txid,uint16_t vout,uint8_t type) { memcpy(buf,&txid,32); memcpy(&buf[32],&vout,2); buf[34] = type; } struct pax_transaction *hush_paxfind(uint256 txid,uint16_t vout,uint8_t type) { struct pax_transaction *pax; uint8_t buf[35]; pthread_mutex_lock(&hush_mutex); pax_keyset(buf,txid,vout,type); HASH_FIND(hh,PAX,buf,sizeof(buf),pax); pthread_mutex_unlock(&hush_mutex); return(pax); } struct pax_transaction *hush_paxmark(int32_t height,uint256 txid,uint16_t vout,uint8_t type,int32_t mark) { struct pax_transaction *pax; return(pax); } void hush_paxdelete(struct pax_transaction *pax) { } void hush_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,uint8_t type,int32_t height,int32_t otherheight,char *source,int32_t approved) // assetchain context { } int32_t hush_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax) { return 0; } int32_t hush_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t *values,int64_t *srcvalues,int32_t *hushheights,int32_t *otherheights,int8_t *baseids,uint8_t *rmd160s,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo) { return 0; } int32_t hush_paxcmp(char *symbol,int32_t hushheight,uint64_t value,uint64_t checkvalue,uint64_t seed) { return 0; } uint64_t hush_paxtotal() { return(0); } int32_t hush_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo) { return(0); } void hush_passport_iteration(); const char *hush_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source) { uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t didstats,i,j,n,kvheight,len,tokomodo,hushheight,otherheights[64],hushheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fee,fiatoshis,puposhis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax,*pax2; struct hush_state *basesp; double diff; const char *typestr = "unknown"; if ( SMART_CHAIN_SYMBOL[0] != 0 && hush_baseid(SMART_CHAIN_SYMBOL) < 0 && opretbuf[0] != 'K' ) { //printf("hush_opreturn skip %s\n",SMART_CHAIN_SYMBOL); return("assetchain"); } memset(baseids,0xff,sizeof(baseids)); memset(values,0,sizeof(values)); memset(srcvalues,0,sizeof(srcvalues)); memset(rmd160s,0,sizeof(rmd160s)); memset(hushheights,0,sizeof(hushheights)); memset(otherheights,0,sizeof(otherheights)); tokomodo = (hush_is_issuer() == 0); if ( opretbuf[0] == 'K' && opretlen != 40 ) { hush_kvupdate(opretbuf,opretlen,value); return("kv"); } else if ( SMART_CHAIN_SYMBOL[0] == 0 && HUSH_PAX == 0 ) return("nopax"); if ( opretbuf[0] == 'D' ) { tokomodo = 0; if ( opretlen == 38 ) // any HUSH3 tx { dragon_rwnum(0,&opretbuf[34],sizeof(hushheight),&hushheight); memset(base,0,sizeof(base)); PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis); bitcoin_address(coinaddr,addrtype,rmd160,20); checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,hushheight,base,fiatoshis); if ( hush_paxcmp(base,hushheight,value,checktoshis,hushheight < 225000 ? seed : 0) != 0 ) checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,height,base,fiatoshis); typestr = "deposit"; if ( 0 && strcmp("NOK",base) == 0 ) { printf("[%s] %s paxdeposit height.%d vs hushheight.%d\n",SMART_CHAIN_SYMBOL,base,height,hushheight); printf("(%s) (%s) hushheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",SMART_CHAIN_SYMBOL,base,hushheight,height,dstr(checktoshis),dstr(value),hush_is_issuer(),strncmp(SMART_CHAIN_SYMBOL,base,strlen(base)) == 0,(long long)seed); for (i=0; i<32; i++) printf("%02x",((uint8_t *)&txid)[i]); printf(" <- txid.v%u ",vout); for (i=0; i<33; i++) printf("%02x",pubkey33[i]); printf(" checkpubkey check %.8f v %.8f dest.(%s) hushheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,hushheight,height); } if ( strcmp(base,SMART_CHAIN_SYMBOL) == 0 && (hushheight > 195000 || hushheight <= height) ) { didstats = 0; if ( hush_paxcmp(base,hushheight,value,checktoshis,hushheight < 225000 ? seed : 0) == 0 ) { if ( (pax= hush_paxfind(txid,vout,'D')) == 0 ) { if ( (basesp= hush_stateptrget(base)) != 0 ) { basesp->deposited += fiatoshis; didstats = 1; if ( 0 && strcmp(base,SMART_CHAIN_SYMBOL) == 0 ) printf("########### %p deposited %s += %.8f hushheight.%d %.8f\n",basesp,base,dstr(fiatoshis),hushheight,dstr(value)); } else printf("cant get stateptr.(%s)\n",base); hush_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',hushheight,height,(char *)"HUSH3",0); } if ( (pax= hush_paxfind(txid,vout,'D')) != 0 ) { pax->height = hushheight; pax->validated = value; pax->puposhis = value; pax->fiatoshis = fiatoshis; if ( didstats == 0 && pax->didstats == 0 ) { if ( (basesp= hush_stateptrget(base)) != 0 ) { basesp->deposited += fiatoshis; didstats = 1; if ( 0 && strcmp(base,SMART_CHAIN_SYMBOL) == 0 ) printf("########### %p depositedB %s += %.8f/%.8f hushheight.%d/%d %.8f/%.8f\n",basesp,base,dstr(fiatoshis),dstr(pax->fiatoshis),hushheight,pax->height,dstr(value),dstr(pax->puposhis)); } } // if ( didstats != 0 ) pax->didstats = 1; if ( (pax2= hush_paxfind(txid,vout,'I')) != 0 ) { pax2->fiatoshis = pax->fiatoshis; pax2->puposhis = pax->puposhis; pax->marked = pax2->marked = pax->height; pax2->height = pax->height = height; if ( pax2->didstats == 0 ) { if ( (basesp= hush_stateptrget(base)) != 0 ) { basesp->issued += pax2->fiatoshis; pax2->didstats = 1; if ( 0 && strcmp(base,"USD") == 0 ) printf("########### %p issueda %s += %.8f hushheight.%d %.8f other.%d [%d]\n",basesp,base,dstr(pax2->fiatoshis),pax2->height,dstr(pax2->puposhis),pax2->otherheight,height); } } } } } else { if ( (pax= hush_paxfind(txid,vout,'D')) != 0 ) pax->marked = checktoshis; if ( hushheight > 238000 && (hushheight > 214700 || strcmp(base,SMART_CHAIN_SYMBOL) == 0) ) //seed != 0 && printf("pax %s deposit %.8f rejected hushheight.%d %.8f HUSH3 check %.8f seed.%llu\n",base,dstr(fiatoshis),hushheight,dstr(value),dstr(checktoshis),(long long)seed); } } //else printf("[%s] %s paxdeposit height.%d vs hushheight.%d\n",SMART_CHAIN_SYMBOL,base,height,hushheight); } //else printf("unsupported size.%d for opreturn D\n",opretlen); } else if ( opretbuf[0] == 'I' ) { tokomodo = 0; if ( strncmp((char *)"HUSH3",(char *)&opretbuf[opretlen-4],3) != 0 && strncmp(SMART_CHAIN_SYMBOL,(char *)&opretbuf[opretlen-4],3) == 0 ) { if ( (n= hush_issued_opreturn(base,txids,vouts,values,srcvalues,hushheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 ) { for (i=0; itype = opretbuf[0]; strcpy(pax->source,(char *)&opretbuf[opretlen-4]); if ( (pax2= hush_paxfind(txids[i],vouts[i],'D')) != 0 && pax2->fiatoshis != 0 && pax2->puposhis != 0 ) { // realtime path? pax->fiatoshis = pax2->fiatoshis; pax->puposhis = pax2->puposhis; pax->marked = pax2->marked = pax2->height; if ( pax->didstats == 0 ) { if ( (basesp= hush_stateptrget(CURRENCIES[baseids[i]])) != 0 ) { basesp->issued += pax->fiatoshis; pax->didstats = 1; pax->height = pax2->height; pax->otherheight = height; if ( 1 && strcmp(CURRENCIES[baseids[i]],"USD") == 0 ) printf("########### %p issuedb %s += %.8f hushheight.%d %.8f other.%d [%d]\n",basesp,CURRENCIES[baseids[i]],dstr(pax->fiatoshis),pax->height,dstr(pax->puposhis),pax->otherheight,height); } } } } if ( (pax= hush_paxmark(height,txids[i],vouts[i],'I',height)) != 0 ) hush_paxdelete(pax); if ( (pax= hush_paxmark(height,txids[i],vouts[i],'D',height)) != 0 ) hush_paxdelete(pax); } } //else printf("opreturn none issued?\n"); } } else if ( height < 236000 && opretbuf[0] == 'W' && strncmp(SMART_CHAIN_SYMBOL,(char *)&opretbuf[opretlen-4],3) == 0 )//&& opretlen >= 38 ) { if ( hush_baseid((char *)&opretbuf[opretlen-4]) >= 0 && strcmp(SMART_CHAIN_SYMBOL,(char *)&opretbuf[opretlen-4]) == 0 ) { for (i=0; i (%s) len.%d\n",SMART_CHAIN_SYMBOL,base,hushheight,height,dstr(checktoshis),dstr(puposhis),dstr(value),hush_is_issuer(),strncmp(SMART_CHAIN_SYMBOL,base,strlen(base)) == 0,(long long)seed,coinaddr,opretlen); didstats = 0; //if ( hush_paxcmp(base,hushheight,puposhis,checktoshis,seed) == 0 ) { if ( value != 0 && ((pax= hush_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) ) { if ( (basesp= hush_stateptrget(base)) != 0 ) { basesp->withdrawn += value; didstats = 1; if ( 0 && strcmp(base,SMART_CHAIN_SYMBOL) == 0 ) printf("########### %p withdrawn %s += %.8f check %.8f\n",basesp,base,dstr(value),dstr(checktoshis)); } if ( 0 && strcmp(base,"RUB") == 0 && (pax == 0 || pax->approved == 0) ) printf("notarize %s %.8f -> %.8f HUSH3.%d other.%d\n",SMART_CHAIN_SYMBOL,dstr(value),dstr(puposhis),hushheight,height); } hush_gateway_deposit(coinaddr,0,(char *)"HUSH3",value,rmd160,txid,vout,'W',hushheight,height,source,0); if ( (pax= hush_paxfind(txid,vout,'W')) != 0 ) { pax->type = opretbuf[0]; strcpy(pax->source,base); strcpy(pax->symbol,"HUSH3"); pax->height = hushheight; pax->otherheight = height; pax->puposhis = puposhis; } } // else printf("withdraw %s paxcmp ht.%d %d error value %.8f -> %.8f vs %.8f\n",base,hushheight,height,dstr(value),dstr(puposhis),dstr(checktoshis)); // need to allocate pax } else if ( height < 236000 && tokomodo != 0 && opretbuf[0] == 'A' && SMART_CHAIN_SYMBOL[0] == 0 ) { tokomodo = 1; if ( 0 && SMART_CHAIN_SYMBOL[0] != 0 ) { for (i=0; i 0 ) { for (i=0; isymbol); printf("override neg1 with (%s)\n",pax->symbol); } if ( baseids[i] < 0 ) continue; } didstats = 0; seed = 0; checktoshis = hush_paxprice(&seed,hushheights[i],CURRENCIES[baseids[i]],(char *)"HUSH3",(uint64_t)values[i]); //printf("PAX_fiatdest ht.%d price %s %.8f -> HUSH3 %.8f vs %.8f\n",hushheights[i],CURRENCIES[baseids[i]],(double)values[i]/COIN,(double)srcvalues[i]/COIN,(double)checktoshis/COIN); if ( srcvalues[i] == checktoshis ) { if ( (pax= hush_paxfind(txids[i],vouts[i],'A')) == 0 ) { bitcoin_address(coinaddr,60,&rmd160s[i*20],20); hush_gateway_deposit(coinaddr,srcvalues[i],CURRENCIES[baseids[i]],values[i],&rmd160s[i*20],txids[i],vouts[i],'A',hushheights[i],otherheights[i],CURRENCIES[baseids[i]],hushheights[i]); if ( (pax= hush_paxfind(txids[i],vouts[i],'A')) == 0 ) printf("unexpected null pax for approve\n"); else pax->validated = checktoshis; if ( (pax2= hush_paxfind(txids[i],vouts[i],'W')) != 0 ) pax2->approved = hushheights[i]; hush_paxmark(height,txids[i],vouts[i],'W',height); //hush_paxmark(height,txids[i],vouts[i],'A',height); if ( values[i] != 0 && (basesp= hush_stateptrget(CURRENCIES[baseids[i]])) != 0 ) { basesp->approved += values[i]; didstats = 1; //printf("pax.%p ########### %p approved %s += %.8f -> %.8f/%.8f kht.%d %d\n",pax,basesp,CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]),dstr(checktoshis),hushheights[i],otherheights[i]); } //printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i])); } else if ( pax->didstats == 0 && srcvalues[i] != 0 ) { if ( (basesp= hush_stateptrget(CURRENCIES[baseids[i]])) != 0 ) { basesp->approved += values[i]; didstats = 1; //printf("pax.%p ########### %p approved %s += %.8f -> %.8f/%.8f kht.%d %d\n",pax,basesp,CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]),dstr(checktoshis),hushheights[i],otherheights[i]); } } //else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]); if ( (pax= hush_paxfind(txids[i],vouts[i],'A')) != 0 ) { pax->type = opretbuf[0]; pax->approved = hushheights[i]; pax->validated = checktoshis; if ( didstats != 0 ) pax->didstats = 1; //if ( strcmp(CURRENCIES[baseids[i]],SMART_CHAIN_SYMBOL) == 0 ) //printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,hushheights[i],pax); } } } } //else printf("n.%d from opreturns\n",n); //printf("extra.[%d] after %.8f\n",n,dstr(hush_paxtotal())); } else if ( height < 236000 && opretbuf[0] == 'X' && SMART_CHAIN_SYMBOL[0] == 0 ) { tokomodo = 1; if ( (n= hush_issued_opreturn(base,txids,vouts,values,srcvalues,hushheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 ) { for (i=0; itype = opretbuf[0]; if ( height < 121842 ) // fields got switched around due to legacy issues and approves value = srcvalues[i]; else value = values[i]; if ( baseids[i] >= 0 && value != 0 && (basesp= hush_stateptrget(CURRENCIES[baseids[i]])) != 0 ) { basesp->redeemed += value; pax->didstats = 1; if ( strcmp(CURRENCIES[baseids[i]],SMART_CHAIN_SYMBOL) == 0 ) printf("ht.%d %.8f ########### %p redeemed %s += %.8f %.8f kht.%d ht.%d\n",height,dstr(value),basesp,CURRENCIES[baseids[i]],dstr(value),dstr(srcvalues[i]),hushheights[i],otherheights[i]); } } if ( (pax= hush_paxmark(height,txids[i],vouts[i],'W',height)) != 0 ) hush_paxdelete(pax); if ( (pax= hush_paxmark(height,txids[i],vouts[i],'A',height)) != 0 ) hush_paxdelete(pax); if ( (pax= hush_paxmark(height,txids[i],vouts[i],'X',height)) != 0 ) hush_paxdelete(pax); } } //else printf("hush_issued_opreturn returned %d\n",n); } return(typestr); } int32_t hush_parsestatefiledata(struct hush_state *sp,uint8_t *filedata,long *fposp,long datalen,char *symbol,char *dest); void hush_stateind_set(struct hush_state *sp,uint32_t *inds,int32_t n,uint8_t *filedata,long datalen,char *symbol,char *dest) { uint8_t func; long lastK,lastT,lastN,lastV,fpos,lastfpos; int32_t i,count,doissue,iter,numn,numv,numN,numV,numR; uint32_t tmp,prevpos100,offset; count = numR = numN = numV = numn = numv = 0; lastK = lastT = lastN = lastV = -1; for (iter=0; iter<2; iter++) { for (lastfpos=fpos=prevpos100=i=0; i> 8); fpos = prevpos100 + offset; if ( lastfpos >= datalen || (filedata[lastfpos] != func && func != 0) ) printf("i.%d/n.%d lastfpos.%ld >= datalen.%ld or [%d] != func.%d\n",i,n,lastfpos,datalen,filedata[lastfpos],func); else if ( iter == 0 ) { switch ( func ) { default: case 'P': case 'U': case 'D': inds[i] &= 0xffffff00; break; case 'K': lastK = lastfpos; inds[i] &= 0xffffff00; break; case 'T': lastT = lastfpos; inds[i] &= 0xffffff00; break; case 'N': lastN = lastfpos; numN++; break; case 'V': lastV = lastfpos; numV++; break; case 'R': numR++; break; } } else { doissue = 0; if ( func == 'K' ) { if ( lastK == lastfpos ) doissue = 1; } else if ( func == 'T' ) { if ( lastT == lastfpos ) doissue = 1; } else if ( func == 'N' ) { if ( numn > numN-128 ) doissue = 1; numn++; } else if ( func == 'V' ) { if ( HUSH_PAX != 0 && numv > numV-1440 ) doissue = 1; numv++; } else if ( func == 'R' ) doissue = 1; if ( doissue != 0 ) { //printf("issue %c total.%d lastfpos.%ld\n",func,count,lastfpos); hush_parsestatefiledata(sp,filedata,&lastfpos,datalen,symbol,dest); count++; } } } lastfpos = fpos; } } printf("numR.%d numV.%d numN.%d count.%d\n",numR,numV,numN,count); /*else if ( func == 'K' ) // HUSH height: stop after 1st else if ( func == 'T' ) // HUSH height+timestamp: stop after 1st else if ( func == 'N' ) // notarization, scan backwards 1440+ blocks; else if ( func == 'V' ) // price feed: can stop after 1440+ else if ( func == 'R' ) // opreturn:*/ } void *OS_loadfile(char *fname,uint8_t **bufp,long *lenp,long *allocsizep) { FILE *fp; long filesize,buflen = *allocsizep; uint8_t *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 ) { *allocsizep = filesize; *bufp = buf = (uint8_t *)realloc(buf,(long)*allocsizep+64); } 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; uint8_t *buf = 0; void *retptr; *allocsizep = 0; retptr = OS_loadfile(fname,&buf,&filesize,allocsizep); return((uint8_t *)retptr); } long hush_stateind_validate(struct hush_state *sp,char *indfname,uint8_t *filedata,long datalen,uint32_t *prevpos100p,uint32_t *indcounterp,char *symbol,char *dest) { FILE *fp; long fsize,lastfpos=0,fpos=0; uint8_t *inds,func; int32_t i,n; uint32_t offset,tmp,prevpos100 = 0; *indcounterp = *prevpos100p = 0; if ( (inds= OS_fileptr(&fsize,indfname)) != 0 ) { lastfpos = 0; fprintf(stderr,"inds.%p validate %s fsize.%ld datalen.%ld n.%d lastfpos.%ld\n",inds,indfname,fsize,datalen,(int32_t)(fsize / sizeof(uint32_t)),lastfpos); if ( (fsize % sizeof(uint32_t)) == 0 ) { n = (int32_t)(fsize / sizeof(uint32_t)); for (i=0; i n-10 ) printf("%d: tmp.%08x [%c] prevpos100.%u\n",i,tmp,tmp&0xff,prevpos100); if ( (i % 100) == 0 ) prevpos100 = tmp; else { func = (tmp & 0xff); offset = (tmp >> 8); fpos = prevpos100 + offset; if ( lastfpos >= datalen || filedata[lastfpos] != func ) { printf("validate.%d error (%u %d) prev100 %u -> fpos.%ld datalen.%ld [%d] (%c) vs (%c) lastfpos.%ld\n",i,offset,func,prevpos100,fpos,datalen,lastfpos < datalen ? filedata[lastfpos] : -1,func,filedata[lastfpos],lastfpos); return(-1); } } lastfpos = fpos; } *indcounterp = n; *prevpos100p = prevpos100; if ( sp != 0 ) hush_stateind_set(sp,(uint32_t *)inds,n,filedata,fpos,symbol,dest); //printf("free inds.%p %s validated[%d] fpos.%ld datalen.%ld, offset %ld vs fsize.%ld\n",inds,indfname,i,fpos,datalen,i * sizeof(uint32_t),fsize); free(inds); return(fpos); } else printf("wrong filesize %s %ld\n",indfname,fsize); } free(inds); fprintf(stderr,"indvalidate return -1\n"); return(-1); } long hush_indfile_update(FILE *indfp,uint32_t *prevpos100p,long lastfpos,long newfpos,uint8_t func,uint32_t *indcounterp) { uint32_t tmp; if ( indfp != 0 ) { tmp = ((uint32_t)(newfpos - *prevpos100p) << 8) | (func & 0xff); if ( ftell(indfp)/sizeof(uint32_t) != *indcounterp ) printf("indfp fpos %ld -> ind.%ld vs counter.%u\n",ftell(indfp),ftell(indfp)/sizeof(uint32_t),*indcounterp); //fprintf(stderr,"ftell.%ld indcounter.%u lastfpos.%ld newfpos.%ld func.%02x\n",ftell(indfp),*indcounterp,lastfpos,newfpos,func); fwrite(&tmp,1,sizeof(tmp),indfp), (*indcounterp)++; if ( (*indcounterp % 100) == 0 ) { *prevpos100p = (uint32_t)newfpos; fwrite(prevpos100p,1,sizeof(*prevpos100p),indfp), (*indcounterp)++; } } return(newfpos); } int32_t hush_faststateinit(struct hush_state *sp,char *fname,char *symbol,char *dest) { FILE *indfp; char indfname[1024]; uint8_t *filedata; long validated=-1,datalen,fpos,lastfpos; uint32_t tmp,prevpos100,indcounter,starttime; int32_t func,finished = 0; starttime = (uint32_t)time(NULL); safecopy(indfname,fname,sizeof(indfname)-4); strcat(indfname,".ind"); if ( (filedata= OS_fileptr(&datalen,fname)) != 0 ) { if ( 1 )//datalen >= (1LL << 32) || GetArg("-genind",0) != 0 || (validated= hush_stateind_validate(0,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest)) < 0 ) { lastfpos = fpos = 0; indcounter = prevpos100 = 0; if ( (indfp= fopen(indfname,"wb")) != 0 ) fwrite(&prevpos100,1,sizeof(prevpos100),indfp), indcounter++; fprintf(stderr,"processing %s %ldKB, validated.%ld\n",fname,datalen/1024,validated); while ( (func= hush_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest)) >= 0 ) { lastfpos = hush_indfile_update(indfp,&prevpos100,lastfpos,fpos,func,&indcounter); } if ( indfp != 0 ) { fclose(indfp); if ( (fpos= hush_stateind_validate(0,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest)) < 0 ) printf("unexpected hushstate.ind validate failure %s datalen.%ld\n",indfname,datalen); else printf("%s validated fpos.%ld\n",indfname,fpos); } finished = 1; fprintf(stderr,"took %d seconds to process %s %ldKB\n",(int32_t)(time(NULL)-starttime),fname,datalen/1024); } else if ( validated > 0 ) { if ( (indfp= fopen(indfname,"rb+")) != 0 ) { lastfpos = fpos = validated; fprintf(stderr,"datalen.%ld validated %ld -> indcounter %u, prevpos100 %u offset.%d\n",datalen,validated,indcounter,prevpos100,(int32_t)(indcounter * sizeof(uint32_t))); if ( fpos < datalen ) { fseek(indfp,indcounter * sizeof(uint32_t),SEEK_SET); if ( ftell(indfp) == indcounter * sizeof(uint32_t) ) { while ( (func= hush_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest)) >= 0 ) { lastfpos = hush_indfile_update(indfp,&prevpos100,lastfpos,fpos,func,&indcounter); if ( lastfpos != fpos ) fprintf(stderr,"unexpected lastfpos.%ld != %ld\n",lastfpos,fpos); } } fclose(indfp); } if ( hush_stateind_validate(sp,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest) < 0 ) printf("unexpected hushstate.ind validate failure %s datalen.%ld\n",indfname,datalen); else { printf("%s validated updated from validated.%ld to %ld new.[%ld] -> indcounter %u, prevpos100 %u offset.%ld | elapsed %d seconds\n",indfname,validated,fpos,fpos-validated,indcounter,prevpos100,indcounter * sizeof(uint32_t),(int32_t)(time(NULL) - starttime)); finished = 1; } } } else printf("hush_faststateinit unexpected case\n"); free(filedata); return(finished == 1); } return(-1); } void hush_passport_iteration() { static long lastpos[34]; static char userpass[33][1024]; static uint32_t lasttime,callcounter,lastinterest; int32_t maxseconds = 10; FILE *fp; uint8_t *filedata; long fpos,datalen,lastfpos; int32_t baseid,limit,n,ht,isrealtime,expired,refid,blocks,longest; struct hush_state *sp,*refsp; char *retstr,fname[512],*base,symbol[HUSH_SMART_CHAIN_MAXLEN],dest[HUSH_SMART_CHAIN_MAXLEN]; uint32_t buf[3],starttime; uint64_t RTmask = 0; //CBlockIndex *pindex; expired = 0; while ( 0 && HUSH_INITDONE == 0 ) { fprintf(stderr,"[%s] PASSPORT iteration waiting for HUSH_INITDONE\n",SMART_CHAIN_SYMBOL); sleep(3); } if ( hush_chainactive_timestamp() > lastinterest ) { lastinterest = hush_chainactive_timestamp(); } refsp = hush_stateptr(symbol,dest); if ( SMART_CHAIN_SYMBOL[0] == 0 || strcmp(SMART_CHAIN_SYMBOL,"JUSTLIES") == 0 ) { refid = 33; limit = 10000000; } else { limit = 10000000; refid = hush_baseid(SMART_CHAIN_SYMBOL)+1; // illegal base -> baseid.-1 -> 0 if ( refid == 0 ) { HUSH_PASSPORT_INITDONE = 1; return; } } starttime = (uint32_t)time(NULL); if ( callcounter++ < 1 ) limit = 10000; lasttime = starttime; for (baseid=32; baseid>=0; baseid--) { if ( time(NULL) >= starttime+maxseconds ) break; sp = 0; isrealtime = 0; base = (char *)CURRENCIES[baseid]; //printf("PASSPORT %s baseid+1 %d refid.%d\n",SMART_CHAIN_SYMBOL,baseid+1,refid); if ( baseid+1 != refid ) // only need to import state from a different coin { if ( baseid == 32 ) // only care about HUSH3's state { refsp->RTmask &= ~(1LL << baseid); hush_statefname(fname,baseid<32?base:(char *)"",(char *)"hushstate"); hush_nameset(symbol,dest,base); sp = hush_stateptrget(symbol); n = 0; if ( lastpos[baseid] == 0 && (filedata= OS_fileptr(&datalen,fname)) != 0 ) { fpos = 0; fprintf(stderr,"%s processing %s %ldKB\n",SMART_CHAIN_SYMBOL,fname,datalen/1024); while ( hush_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest) >= 0 ) lastfpos = fpos; fprintf(stderr,"%s took %d seconds to process %s %ldKB\n",SMART_CHAIN_SYMBOL,(int32_t)(time(NULL)-starttime),fname,datalen/1024); lastpos[baseid] = lastfpos; free(filedata), filedata = 0; datalen = 0; } else if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 ) { fseek(fp,0,SEEK_END); //fprintf(stderr,"couldnt OS_fileptr(%s), freading %ldKB\n",fname,ftell(fp)/1024); if ( ftell(fp) > lastpos[baseid] ) { if ( SMART_CHAIN_SYMBOL[0] != 0 ) printf("%s passport refid.%d %s fname.(%s) base.%s %ld %ld\n",SMART_CHAIN_SYMBOL,refid,symbol,fname,base,ftell(fp),lastpos[baseid]); fseek(fp,lastpos[baseid],SEEK_SET); while ( hush_parsestatefile(sp,fp,symbol,dest) >= 0 && n < limit ) { if ( n == limit-1 ) { if ( time(NULL) < starttime+maxseconds ) n = 0; else { //printf("expire passport loop %s -> %s at %ld\n",SMART_CHAIN_SYMBOL,base,lastpos[baseid]); expired++; } } n++; } lastpos[baseid] = ftell(fp); if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"HUSH3") == 0 ) printf("from.(%s) lastpos[%s] %ld isrt.%d\n",SMART_CHAIN_SYMBOL,CURRENCIES[baseid],lastpos[baseid],hush_isrealtime(&ht)); } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp)); fclose(fp); } else fprintf(stderr,"load error.(%s) %p\n",fname,sp); hush_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime"); if ( (fp= fopen(fname,"rb")) != 0 ) { if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) ) { sp->CURRENT_HEIGHT = buf[0]; if ( buf[0] != 0 && buf[0] >= buf[1] && buf[2] > time(NULL)-60 ) { isrealtime = 1; RTmask |= (1LL << baseid); memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1])); } else if ( HUSH_PAX != 0 && (time(NULL)-buf[2]) > 60 && SMART_CHAIN_SYMBOL[0] != 0 ) fprintf(stderr,"[%s]: %s not RT %u %u %d\n",SMART_CHAIN_SYMBOL,base,buf[0],buf[1],(int32_t)(time(NULL)-buf[2])); } //else fprintf(stderr,"%s size error RT\n",base); fclose(fp); } //else fprintf(stderr,"%s open error RT\n",base); } } else { refsp->RTmask &= ~(1LL << baseid); hush_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime"); if ( (fp= fopen(fname,"wb")) != 0 ) { buf[0] = (uint32_t)chainActive.LastTip()->GetHeight(); buf[1] = (uint32_t)hush_longestchain(); if ( buf[0] != 0 && buf[0] == buf[1] ) { buf[2] = (uint32_t)time(NULL); RTmask |= (1LL << baseid); memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1])); if ( refid != 0 ) memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0])); } if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) ) fprintf(stderr,"[%s] %s error writing realtime\n",SMART_CHAIN_SYMBOL,base); fclose(fp); } else fprintf(stderr,"%s create error RT\n",base); } if ( sp != 0 && isrealtime == 0 ) refsp->RTbufs[0][2] = 0; } //hush_paxtotal(); // calls hush_isrealtime(), which calls hush_longestchain() refsp->RTmask |= RTmask; if ( expired == 0 && HUSH_PASSPORT_INITDONE == 0 ) { HUSH_PASSPORT_INITDONE = 1; printf("READY for %s RPC calls at %u! done PASSPORT %s refid.%d\n",SMART_CHAIN_SYMBOL,(uint32_t)time(NULL),SMART_CHAIN_SYMBOL,refid); } } extern std::vector Mineropret; // opreturn data set by the data gathering code #define PRICES_ERRORRATE (COIN / 100) // maximum acceptable change, set at 1% #define PRICES_SIZEBIT0 (sizeof(uint32_t) * 4) // 4 uint32_t unixtimestamp, BTCUSD, BTCGBP and BTCEUR #define HUSH_LOCALPRICE_CACHESIZE 13 #define HUSH_MAXPRICES 2048 #define PRICES_SMOOTHWIDTH 1 #define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"CBCOINBASE",cmdstr,0,0,0) int32_t hush_cbopretsize(uint64_t flags); int32_t _hush_heightpricebits(uint64_t *seedp,uint32_t *heightbits,CBlock *block) { return(-1); } // hush_heightpricebits() extracts the price data in the coinbase for nHeight int32_t hush_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight) { return(-1); } uint32_t hush_pricenew(char *maxflagp,uint32_t price,uint32_t refprice,int64_t tolerance) { return(0); } int32_t hush_pricecmp(int32_t nHeight,int32_t n,char *maxflags,uint32_t *pricebitsA,uint32_t *pricebitsB,int64_t tolerance) { return(0); } int32_t hush_priceclamp(int32_t n,uint32_t *pricebits,uint32_t *refprices,int64_t tolerance) { return(0); } // hush_mineropret() returns a valid pricedata to add to the coinbase opreturn for nHeight CScript hush_mineropret(int32_t nHeight) { CScript opret; char maxflags[HUSH_MAXPRICES]; uint32_t pricebits[HUSH_MAXPRICES],prevbits[HUSH_MAXPRICES]; int32_t maxflag,i,n,numzero=0; if ( Mineropret.size() >= PRICES_SIZEBIT0 ) { n = (int32_t)(Mineropret.size() / sizeof(uint32_t)); numzero = 1; while ( numzero > 0 ) { memcpy(pricebits,Mineropret.data(),Mineropret.size()); for (i=numzero=0; i 0 ) { memcpy(pricebits,Mineropret.data(),Mineropret.size()); memset(maxflags,0,sizeof(maxflags)); if ( hush_pricecmp(0,n,maxflags,pricebits,prevbits,PRICES_ERRORRATE) < 0 ) { // if the new prices are outside tolerance, update Mineropret with clamped prices hush_priceclamp(n,pricebits,prevbits,PRICES_ERRORRATE); //fprintf(stderr,"update Mineropret to clamped prices\n"); memcpy(Mineropret.data(),pricebits,Mineropret.size()); } } int32_t i; for (i=0; i0; i--) str[i] = str[i-1]; str[0] = '/'; str[n+1] = 0; }*/ #endif return(str); #endif } void *loadfile(char *fname,uint8_t **bufp,long *lenp,long *allocsizep) { FILE *fp; long filesize,buflen = *allocsizep; uint8_t *buf = *bufp; *lenp = 0; if ( (fp= fopen(portable_path(fname),"rb")) != 0 ) { fseek(fp,0,SEEK_END); filesize = ftell(fp); if ( filesize == 0 ) { fclose(fp); *lenp = 0; //printf("loadfile null size.(%s)\n",fname); return(0); } if ( filesize > buflen ) { *allocsizep = filesize; *bufp = buf = (uint8_t *)realloc(buf,(long)*allocsizep+64); } 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); } void *filestr(long *allocsizep,char *_fname) { long filesize = 0; char *fname,*buf = 0; void *retptr; *allocsizep = 0; fname = (char *)malloc(strlen(_fname)+1); strcpy(fname,_fname); retptr = loadfile(fname,(uint8_t **)&buf,&filesize,allocsizep); free(fname); return(retptr); } cJSON *send_curl(char *url,char *fname) { long fsize; char curlstr[1024],*jsonstr; cJSON *json=0; sprintf(curlstr,"wget -q \"%s\" -O %s",url,fname); if ( system(curlstr) == 0 ) { if ( (jsonstr= (char *)filestr((long *)&fsize,fname)) != 0 ) { json = cJSON_Parse(jsonstr); free(jsonstr); } } return(json); } // get_urljson just returns the JSON returned by the URL using issue_curl cJSON *get_urljson(char *url) { char *jsonstr; cJSON *json = 0; if ( (jsonstr= issue_curl(url)) != 0 ) { //fprintf(stderr,"(%s) -> (%s)\n",url,jsonstr); json = cJSON_Parse(jsonstr); free(jsonstr); } return(json); } int32_t hush_cbopretsize(uint64_t flags) { return 0; } void hush_cbopretupdate(int32_t forceflag) { } int32_t hush_pricesinit() { return(0); } void hush_pricesupdate(int32_t height,CBlock *pblock) { }