diff --git a/basilisk/basilisk.c b/basilisk/basilisk.c index 3eada5ba0..fa4e92cae 100755 --- a/basilisk/basilisk.c +++ b/basilisk/basilisk.c @@ -292,16 +292,23 @@ int32_t basilisk_hashstampset(struct iguana_info *coin,struct hashstamp *stamp,i return(-1); } +void basilisk_ensure(struct basilisk_sequence *seq,int32_t num) +{ + int32_t oldmax,incr = 1000; + if ( num >= seq->maxstamps ) + { + oldmax = seq->maxstamps; + seq->maxstamps = ((num + 2*incr) / incr) * incr; + seq->stamps = realloc(seq->stamps,sizeof(*seq->stamps) * seq->maxstamps); + memset(&seq->stamps[oldmax],0,sizeof(*seq->stamps) * (seq->maxstamps - oldmax)); + } +} + int32_t basilisk_hashstampsupdate(struct iguana_info *coin,struct basilisk_sequence *seq,int32_t firstpossible) { while ( (firstpossible + seq->numstamps) < coin->blocks.hwmchain.height ) { - if ( seq->numstamps >= seq->maxstamps ) - { - seq->maxstamps += coin->chain->bundlesize; - seq->stamps = realloc(seq->stamps,sizeof(*seq->stamps) * seq->maxstamps); - memset(&seq->stamps[seq->numstamps],0,sizeof(*seq->stamps) * (seq->maxstamps - seq->numstamps)); - } + basilisk_ensure(seq,seq->numstamps); if ( basilisk_hashstampset(coin,&seq->stamps[seq->numstamps],firstpossible + seq->numstamps) < 0 ) break; else seq->numstamps++; @@ -701,7 +708,7 @@ struct basilisk_item *basilisk_requestservice(struct supernet_info *myinfo,char char *basilisk_standardservice(char *CMD,struct supernet_info *myinfo,bits256 hash,cJSON *valsobj,char *hexstr,int32_t blockflag) // client side { - struct iguana_info *coin,*btcd,*btc; bits256 prevhash; uint32_t nBits = 0; char chainname[64],str[65]; uint8_t space[8192],*allocptr=0,*data = 0; struct basilisk_item *ptr,Lptr; int32_t doneflag,datalen = 0; struct iguana_block *block; cJSON *retjson; + struct iguana_info *coin; bits256 prevhash; uint32_t nBits = 0; char chainname[64],str[65]; uint8_t space[8192],*allocptr=0,*data = 0; struct basilisk_item *ptr,Lptr; int32_t datalen = 0; struct iguana_block *block; cJSON *retjson; retjson = cJSON_CreateObject(); if ( strcmp(CMD,"SET") == 0 || strcmp(CMD,"GET") == 0 ) { @@ -724,26 +731,6 @@ char *basilisk_standardservice(char *CMD,struct supernet_info *myinfo,bits256 ha jdelete(valsobj,"prev"); } } - else if ( strcmp(CMD,"SEQ") == 0 ) - { - doneflag = 0; - if ( (btcd= iguana_coinfind("BTCD")) != 0 ) - { - if ( btcd->RELAYNODE != 0 || btcd->VALIDATENODE != 0 ) - doneflag |= (btcd->SEQ.BTCD.numstamps+BASILISK_FIRSTPOSSIBLEBTCD+1 >= btcd->longestchain); - if ( (btc= iguana_coinfind("BTC")) != 0 && (btc->RELAYNODE != 0 || btc->VALIDATENODE != 0) ) - doneflag |= (btcd->SEQ.BTC.numstamps+BASILISK_FIRSTPOSSIBLEBTC+1 >= btcd->SEQ.BTC.longestchain) << 1; - if ( doneflag == 3 ) - return(clonestr("{\"result\":\"both BTC and BTCD in full relay mode and current\"}")); - } - if ( jobj(valsobj,"done") != 0 ) - jdelete(valsobj,"done"); - jaddnum(valsobj,"done",doneflag); - if ( (doneflag & 1) == 0 ) - jaddnum(valsobj,"BTCD",btcd->SEQ.BTCD.numstamps+BASILISK_FIRSTPOSSIBLEBTCD); - if ( (doneflag & 2) == 0 ) - jaddnum(valsobj,"BTC",btcd->SEQ.BTC.numstamps+BASILISK_FIRSTPOSSIBLEBTC); - } data = get_dataptr(&allocptr,&datalen,space,sizeof(space),hexstr); ptr = basilisk_requestservice(myinfo,CMD,valsobj,hash,data,datalen,nBits); if ( allocptr != 0 ) @@ -1218,22 +1205,33 @@ void basilisks_loop(void *arg) iter++; if ( (btcd= iguana_coinfind("BTCD")) != 0 ) { - done = 0; + done = 3; if ( btcd->RELAYNODE != 0 || btcd->VALIDATENODE != 0 ) { if ( (now= (uint32_t)time(NULL)) > btcd->SEQ.BTCD.lastupdate+10 ) if ( basilisk_update("BTCD",now) >= 0 ) - done = 1; + done &= ~1; } if ( (now= (uint32_t)time(NULL)) > btcd->SEQ.BTC.lastupdate+30 ) { if ( basilisk_update("BTC",now) >= 0 ) - done |= 2; + done &= ~2; } if ( done != 3 ) { valsobj = cJSON_CreateObject(); - basilisk_standardservice("SEQ",myinfo,GENESIS_PUBKEY,valsobj,0,0); + if ( btcd->RELAYNODE == 0 && btcd->VALIDATENODE == 0 ) + { + jaddnum(valsobj,"BTCD",btcd->SEQ.BTCD.numstamps+BASILISK_FIRSTPOSSIBLEBTCD); + basilisk_standardservice("SEQ",myinfo,GENESIS_PUBKEY,valsobj,0,0); + } + if ( (done & 2) == 0 ) + { + free_json(valsobj); + valsobj = cJSON_CreateObject(); + jaddnum(valsobj,"BTC",btcd->SEQ.BTC.numstamps+BASILISK_FIRSTPOSSIBLEBTC); + basilisk_standardservice("SEQ",myinfo,GENESIS_PUBKEY,valsobj,0,0); + } free_json(valsobj); } } @@ -1265,6 +1263,7 @@ void basilisks_loop(void *arg) { if ( (retjson= cJSON_Parse(ptr->retstr)) != 0 ) { + basilisk_seqresult(myinfo,ptr->retstr); free_json(retjson); } } diff --git a/basilisk/basilisk.h b/basilisk/basilisk.h index 438745da6..b6c51b536 100755 --- a/basilisk/basilisk.h +++ b/basilisk/basilisk.h @@ -34,7 +34,7 @@ #define BASILISK_FIRSTPOSSIBLEBTC 414000 #define BASILISK_FIRSTPOSSIBLEBTCD 1100000 -struct hashstamp { bits256 hash2; uint32_t timestamp; int32_t height; }; +struct hashstamp { bits256 hash2; uint32_t timestamp; int32_t height; uint8_t RO[80]; }; struct basilisk_sequence { struct hashstamp *stamps; int32_t lastupdate,maxstamps,numstamps,lasti,longestchain; }; struct basilisk_sequences { struct basilisk_sequence BTC,BTCD; }; diff --git a/basilisk/basilisk_CMD.c b/basilisk/basilisk_CMD.c index 0c3bb0d5a..ec89ce429 100755 --- a/basilisk/basilisk_CMD.c +++ b/basilisk/basilisk_CMD.c @@ -37,18 +37,20 @@ int32_t iguana_rwhashstamp(int32_t rwflag,uint8_t *serialized,struct hashstamp * len += iguana_rwbignum(rwflag,&serialized[len],sizeof(stamp->hash2),stamp->hash2.bytes); len += iguana_rwnum(rwflag,&serialized[len],sizeof(stamp->timestamp),&stamp->timestamp); len += iguana_rwnum(rwflag,&serialized[len],sizeof(stamp->height),&stamp->height); + len += iguana_rwblock80(rwflag,&serialized[len],(void *)stamp->RO); return(len); } cJSON *basilisk_sequencejson(struct basilisk_sequence *seq,int32_t startheight,int32_t firstpossible) { - int32_t i,n,len=0,num = 0; cJSON *item; uint8_t *data; + int32_t i,n,len=0,datalen,num = 0; cJSON *item; uint8_t *data; char strbuf[8192],*hexstr=0; if ( startheight < firstpossible ) startheight = firstpossible; if ( (i= (startheight - firstpossible) ) < 0 || i >= seq->numstamps ) return(0); item = cJSON_CreateObject(); n = (seq->numstamps - i); + datalen = (int32_t)(n * sizeof(*seq->stamps)); data = calloc(n,sizeof(*seq->stamps)); for (; inumstamps && numlastupdate); jaddnum(item,"longest",seq->longestchain); + basilisk_addhexstr(&hexstr,item,strbuf,sizeof(strbuf),data,datalen); + if ( hexstr != 0 ) + free(hexstr); return(item); } +void basilisk_seqresult(struct supernet_info *myinfo,char *retstr) +{ + struct iguana_info *btcd; struct hashstamp stamp; struct basilisk_sequence *seq = 0; cJSON *resultjson; uint8_t *allocptr = 0,space[8192],*data = 0; int32_t ind,startheight,datalen,lastupdate,longestchain,i,num,firstpossible,len = 0; char *hexstr; + if ( (btcd= iguana_coinfind("BTCD")) != 0 && (resultjson= cJSON_Parse(retstr)) != 0 ) + { + if ( jstr(resultjson,"BTCD") != 0 ) + seq = &btcd->SEQ.BTCD, firstpossible = BASILISK_FIRSTPOSSIBLEBTCD; + else if ( jstr(resultjson,"BTC") != 0 ) + seq = &btcd->SEQ.BTC, firstpossible = BASILISK_FIRSTPOSSIBLEBTC; + if ( seq != 0 ) + { + startheight = jint(resultjson,"start"); + if ( (ind= startheight-firstpossible) < 0 ) + { + free_json(resultjson); + return; + } + num = jint(resultjson,"num"); + lastupdate = jint(resultjson,"lastupdate"); + longestchain = jint(resultjson,"longest"); + hexstr = jstr(resultjson,"data"); + printf("got startheight.%d num.%d lastupdate.%d longest.%d (%s)\n",startheight,num,lastupdate,longestchain,hexstr!=0?hexstr:""); + if ( hexstr != 0 && (data= get_dataptr(&allocptr,&datalen,space,sizeof(space),hexstr)) != 0 ) + { + basilisk_ensure(seq,ind + num); + for (i=0; istamps[ind] = stamp; + } + } + if ( allocptr != 0 ) + free(allocptr); + } + free_json(resultjson); + } +} + char *basilisk_respond_hashstamps(struct supernet_info *myinfo,char *CMD,struct iguana_peer *addr,char *remoteaddr,uint32_t basilisktag,cJSON *valsobj,uint8_t *data,int32_t datalen,bits256 prevhash,int32_t from_basilisk) { - int32_t doneflag; struct iguana_info *btcd; cJSON *retjson = cJSON_CreateObject(); - if ( (btcd= iguana_coinfind("BTCD")) != 0 && (doneflag= juint(valsobj,"done")) != 3 ) + int32_t startheight; struct iguana_info *btcd; cJSON *retjson = cJSON_CreateObject(); + if ( (btcd= iguana_coinfind("BTCD")) != 0 ) { - if ( (doneflag & 1) == 0 ) - jadd(retjson,"BTCD",basilisk_sequencejson(&btcd->SEQ.BTCD,juint(valsobj,"BTCD"),BASILISK_FIRSTPOSSIBLEBTCD)); - else if ( (doneflag & 2) == 0 ) - jadd(retjson,"BTC",basilisk_sequencejson(&btcd->SEQ.BTC,juint(valsobj,"BTC"),BASILISK_FIRSTPOSSIBLEBTC)); + if ( (startheight= juint(valsobj,"BTCD")) != 0 ) + jadd(retjson,"BTCD",basilisk_sequencejson(&btcd->SEQ.BTCD,startheight,BASILISK_FIRSTPOSSIBLEBTCD)); + else if ( (startheight= juint(valsobj,"BTC")) != 0 ) + jadd(retjson,"BTC",basilisk_sequencejson(&btcd->SEQ.BTC,startheight,BASILISK_FIRSTPOSSIBLEBTC)); } return(jprint(retjson,1)); } @@ -111,6 +155,7 @@ char *basilisk_respond_getfield(struct supernet_info *myinfo,char *CMD,struct ig struct iguana_info *coin; cJSON *retjson; if ( (coin= basilisk_chain(myinfo,valsobj)) == 0 ) return(clonestr("{\"error\":\"couldnt get basilisk_chain\"}")); + printf("getfield\n"); retjson = cJSON_CreateObject(); return(jprint(retjson,1)); } diff --git a/iguana/iguana777.h b/iguana/iguana777.h index ed4459471..59901beef 100755 --- a/iguana/iguana777.h +++ b/iguana/iguana777.h @@ -1039,6 +1039,7 @@ bits256 basilisk_blockhash(struct iguana_info *coin,bits256 prevhash2); void calc_scrypthash(uint32_t *hash,void *data); int32_t iguana_rwvarstr(int32_t rwflag,uint8_t *serialized,int32_t maxlen,char *endianedp); bits256 bitcoin_sharedsecret(void *ctx,bits256 privkey,uint8_t *pubkey,int32_t plen); +int32_t iguana_rwblock80(int32_t rwflag,uint8_t *serialized,struct iguana_msgblock *msg); extern int32_t HDRnet,netBLOCKS;