diff --git a/basilisk/basilisk.c b/basilisk/basilisk.c index aeb33f134..0d8b6df03 100755 --- a/basilisk/basilisk.c +++ b/basilisk/basilisk.c @@ -133,9 +133,9 @@ int32_t basilisk_submit(struct supernet_info *myinfo,cJSON *reqjson,int32_t time return(n); } -struct basilisk_item *basilisk_issueremote(struct supernet_info *myinfo,char *methodstr,char *symbol,cJSON *vals,int32_t timeoutmillis,int32_t fanout,int32_t minresults,uint32_t basilisktag) +struct basilisk_item *basilisk_issueremote(struct supernet_info *myinfo,char *methodstr,char *symbol,cJSON *vals,int32_t timeoutmillis,int32_t fanout,int32_t minresults,uint32_t basilisktag,void *metricfunc) { - double expiration; struct basilisk_item *ptr; cJSON *hexjson; + struct basilisk_item *ptr; cJSON *hexjson; if ( basilisktag == 0 ) basilisktag = rand(); hexjson = cJSON_CreateObject(); @@ -143,22 +143,27 @@ struct basilisk_item *basilisk_issueremote(struct supernet_info *myinfo,char *me jaddnum(hexjson,"basilisktag",basilisktag); jaddstr(hexjson,"method",methodstr); jaddstr(hexjson,"activecoin",symbol); + jaddnum(hexjson,"timeout",timeoutmillis); if ( vals != 0 ) jadd(hexjson,"vals",jduplicate(vals)); - printf("issue.(%s)\n",jprint(hexjson,0)); + printf("issue.(%s) timeout.%d\n",jprint(hexjson,0),timeoutmillis); ptr = calloc(1,sizeof(*ptr)); ptr->basilisktag = basilisktag; ptr->numrequired = minresults; + if ( (ptr->metricfunc= metricfunc) != 0 ) + ptr->vals = jduplicate(vals); + strcpy(ptr->symbol,symbol); + ptr->expiration = OS_milliseconds() + timeoutmillis; queue_enqueue("submitQ",&myinfo->basilisks.submitQ,&ptr->DL,0); if ( basilisk_submit(myinfo,hexjson,timeoutmillis,fanout,ptr) > 0 ) { - if ( timeoutmillis > 0 ) + /*if ( timeoutmillis > 0 ) { printf("unexpected blocking\n"); expiration = OS_milliseconds() + ((timeoutmillis == 0) ? BASILISK_TIMEOUT : timeoutmillis); while ( OS_milliseconds() < expiration && ptr->finished == 0 && ptr->numresults < ptr->numrequired ) usleep(timeoutmillis/100 + 1); - } + }*/ } free_json(hexjson); return(ptr); @@ -208,9 +213,9 @@ int32_t basilisk_besti(struct basilisk_item *ptr) else bestmetric = 0.; for (i=0; inumresults; i++) { - if ( (metric= ptr->metrics[i]) != 0. ) + if ( (metric= ptr->metrics[i]) > 0. ) { - if ( (ptr->metricdir < 0 && (bestmetric > 0. || metric < bestmetric)) || (ptr->metricdir > 0 && (bestmetric < 0. || metric > bestmetric)) || (ptr->metricdir == 0 && bestmetric == 0.) ) + if ( (ptr->metricdir < 0 && (bestmetric == 0. || metric < bestmetric)) || (ptr->metricdir > 0 && (bestmetric == 0. || metric > bestmetric)) || (ptr->metricdir == 0 && bestmetric == 0.) ) { bestmetric = metric; besti = i; @@ -219,16 +224,43 @@ int32_t basilisk_besti(struct basilisk_item *ptr) } if ( besti >= 0 ) { - for (i=0; inumresults; i++) + for (ptr->numexact=i=0; inumresults; i++) if ( fabs(ptr->metrics[i] - bestmetric) < SMALLVAL ) ptr->numexact++; } return(besti); } +char *basilisk_iscomplete(struct basilisk_item *ptr) +{ + int32_t i,numvalid,besti=-1; char *errstr = 0,*retstr = 0; + if ( ptr->retstr != 0 ) + return(ptr->retstr); + if ( (numvalid= ptr->numresults) >= ptr->numrequired ) + { + for (i=numvalid=0; inumresults; i++) + { + if ( ptr->metrics[i] != 0. ) + numvalid++; + } + } + if ( numvalid < ptr->numrequired ) + { + printf("%u: numvalid.%d < required.%d m %f\n",ptr->basilisktag,numvalid,ptr->numrequired,ptr->metrics[0]); + return(0); + } + if ( ptr->uniqueflag == 0 && ptr->numexact != ptr->numresults && ptr->numexact < (ptr->numresults >> 1) ) + besti = -1, errstr = "{\"error\":\"basilisk non-consensus results\"}"; + else besti = basilisk_besti(ptr), errstr = "{\"error\":\"basilisk no valid results\"}"; + printf("%u complete besti.%d\n",ptr->basilisktag,besti); + retstr = basilisk_finish(ptr,besti,errstr); + printf("%u besti.%d numexact.%d numresults.%d -> (%s)\n",ptr->basilisktag,besti,ptr->numexact,ptr->numresults,retstr); + return(retstr); +} + char *basilisk_block(struct supernet_info *myinfo,struct iguana_info *coin,char *remoteaddr,struct basilisk_item *Lptr,struct basilisk_item *ptr) { - int32_t besti,i,j,numvalid; char *retstr = 0,*errstr; struct iguana_peer *addr; cJSON *hexobj,*retjson,*valsobj; + int32_t i,j; char *retstr = 0; struct iguana_peer *addr; cJSON *hexobj,*retjson,*valsobj; if ( ptr == Lptr ) { if ( (retstr= Lptr->retstr) == 0 ) @@ -241,26 +273,9 @@ char *basilisk_block(struct supernet_info *myinfo,struct iguana_info *coin,char ptr->numrequired = 1; while ( OS_milliseconds() < ptr->expiration ) { - if ( (numvalid= ptr->numresults) >= ptr->numrequired ) - { - for (i=numvalid=0; inumresults; i++) - { - if ( ptr->metrics[i] != 0. ) - numvalid++; - } - } - if ( numvalid < ptr->numrequired ) - { - usleep(1000000); - printf("%u: numvalid.%d < required.%d\n",ptr->basilisktag,numvalid,ptr->numrequired); - continue; - } - if ( ptr->uniqueflag == 0 && ptr->numexact <= (ptr->numresults >> 1) ) - besti = -1, errstr = "{\"error\":\"basilisk non-consensus results\"}"; - else besti = basilisk_besti(ptr), errstr = "{\"error\":\"basilisk no valid results\"}"; - retstr = basilisk_finish(ptr,besti,errstr); - printf("besti.%d numexact.%d numresults.%d -> (%s)\n",besti,ptr->numexact,ptr->numresults,retstr); - break; + if ( (retstr= basilisk_iscomplete(ptr)) != 0 ) + break; + usleep(1000000); } if ( retstr == 0 ) retstr = basilisk_finish(ptr,-1,"{\"error\":\"basilisk timeout\"}"); @@ -486,12 +501,14 @@ char *basilisk_hexmsg(struct supernet_info *myinfo,struct category_info *cat,voi void basilisks_loop(void *arg) { - basilisk_metricfunc metricfunc; struct basilisk_item *ptr,*tmp,*pending,*parent; int32_t i,flag,n; struct supernet_info *myinfo = arg; + basilisk_metricfunc metricfunc; struct basilisk_item *ptr,*tmp,*pending,*parent; int32_t i,iter,flag,n; char *retstr; struct supernet_info *myinfo = arg; //uint8_t *blockspace; struct OS_memspace RAWMEM; //memset(&RAWMEM,0,sizeof(RAWMEM)); //blockspace = calloc(1,IGUANA_MAXPACKETSIZE); + iter = 0; while ( 1 ) { + iter++; //for (i=0; iRELAYNODE == 0 && coin->VALIDATENODE == 0 && coin->active != 0 && coin->chain->userpass[0] != 0 && coin->MAXPEERS == 1 ) // basilisk_bitcoinscan(coin,blockspace,&RAWMEM); @@ -500,9 +517,8 @@ void basilisks_loop(void *arg) if ( ptr->finished == 0 ) HASH_ADD(hh,myinfo->basilisks.issued,basilisktag,sizeof(ptr->basilisktag),ptr); else free(ptr); - continue; } - else if ( (ptr= queue_dequeue(&myinfo->basilisks.resultsQ,0)) != 0 ) + if ( (ptr= queue_dequeue(&myinfo->basilisks.resultsQ,0)) != 0 ) { HASH_FIND(hh,myinfo->basilisks.issued,&ptr->basilisktag,sizeof(ptr->basilisktag),pending); if ( pending != 0 ) @@ -510,70 +526,70 @@ void basilisks_loop(void *arg) if ( (n= pending->numresults) < sizeof(pending->results)/sizeof(*pending->results) ) { pending->results[n] = ptr->retstr; - printf("%p Add results[%d] <- (%s)\n",&pending->results[n],n,ptr->retstr); pending->numresults++; if ( (metricfunc= pending->metricfunc) == 0 ) pending->metrics[n] = n + 1; else pending->metrics[n] = (*metricfunc)(myinfo,pending,pending->results[n]); + printf("%u Add results[%d] <- (%s) metric %f\n",pending->basilisktag,n,ptr->retstr,pending->metrics[n]); } } free(ptr); - continue; } - else + flag = 0; + HASH_ITER(hh,myinfo->basilisks.issued,pending,tmp) { - flag = 0; - HASH_ITER(hh,myinfo->basilisks.issued,pending,tmp) - { - for (i=0; inumresults; i++) - if ( pending->metrics[i] == 0. ) - { - if ( (metricfunc= pending->metricfunc) != 0 ) - { - pending->metrics[i] = (*metricfunc)(myinfo,pending,pending->results[i]); - //printf("poll metrics\n"); - } - flag++; - } - if ( OS_milliseconds() > pending->expiration ) + printf("pending.%u numresults.%d m %f func.%p\n",pending->basilisktag,pending->numresults,pending->metrics[0],pending->metricfunc); + for (i=0; inumresults; i++) + if ( pending->metrics[i] == 0. ) { - if ( pending->finished == 0 ) + if ( (metricfunc= pending->metricfunc) != 0 && pending->metrics[i] == 0. ) { - if ( (parent= pending->parent) != 0 ) - { - pending->parent = 0; - parent->childrendone++; - } - pending->finished = (uint32_t)time(NULL); + pending->metrics[i] = (*metricfunc)(myinfo,pending,pending->results[i]); + //printf("iter.%d %p.[%d] poll metrics.%u metric %f\n",iter,pending,i,pending->basilisktag,pending->metrics[i]); } - if ( pending->retstr == 0 ) - pending->retstr = clonestr("{\"error\":\"basilisk timeout\"}"); - printf("timeout call metrics.%u\n",pending->basilisktag); - for (i=0; inumresults; i++) - if ( (metricfunc= pending->metricfunc) != 0 ) - pending->metrics[i] = (*metricfunc)(myinfo,pending,pending->results[i]); + flag++; } - if ( pending->finished != 0 ) + if ( (retstr= basilisk_iscomplete(pending)) != 0 ) + printf("completed.(%s)\n",retstr); + if ( OS_milliseconds() > pending->expiration ) + { + if ( pending->finished == 0 ) { - if ( pending->dependents == 0 || pending->childrendone >= pending->numchildren ) + if ( (parent= pending->parent) != 0 ) { - HASH_DELETE(hh,myinfo->basilisks.issued,pending); - if ( pending->dependents != 0 ) - free(pending->dependents); - printf("free ptr.%u\n",pending->basilisktag); - for (i=0; inumresults; i++) - if ( pending->results[i] != 0 ) - free(pending->results[i]); - if ( pending->vals != 0 ) - free_json(pending->vals); - free(pending); - flag++; + pending->parent = 0; + parent->childrendone++; } + pending->finished = (uint32_t)time(NULL); + } + if ( pending->retstr == 0 ) + pending->retstr = clonestr("{\"error\":\"basilisk timeout\"}"); + printf("timeout call metrics.%u lag %f - %f\n",pending->basilisktag,OS_milliseconds(),pending->expiration); + for (i=0; inumresults; i++) + if ( (metricfunc= pending->metricfunc) != 0 ) + pending->metrics[i] = (*metricfunc)(myinfo,pending,pending->results[i]); + } + if ( pending->finished != 0 && time(NULL) > pending->finished+60 ) + { + if ( pending->dependents == 0 || pending->childrendone >= pending->numchildren ) + { + HASH_DELETE(hh,myinfo->basilisks.issued,pending); + if ( pending->dependents != 0 ) + free(pending->dependents); + printf("HASH_DELETE free ptr.%u\n",pending->basilisktag); + for (i=0; inumresults; i++) + if ( pending->results[i] != 0 ) + free(pending->results[i]); + if ( pending->vals != 0 ) + free_json(pending->vals); + free(pending); + flag++; } } - if ( flag == 0 ) - usleep(100000); } + if ( flag == 0 ) + sleep(1); + else sleep(3); } } diff --git a/basilisk/basilisk.h b/basilisk/basilisk.h index 70fe4105e..36f82418a 100755 --- a/basilisk/basilisk.h +++ b/basilisk/basilisk.h @@ -39,7 +39,7 @@ struct basilisk_info struct basilisk_value values[8192]; int32_t numvalues; }; -struct basilisk_item *basilisk_issueremote(struct supernet_info *myinfo,char *methodstr,char *symbol,cJSON *vals,int32_t timeoutmillis,int32_t fanout,int32_t minresults,uint32_t basilisktag); +struct basilisk_item *basilisk_issueremote(struct supernet_info *myinfo,char *methodstr,char *symbol,cJSON *vals,int32_t timeoutmillis,int32_t fanout,int32_t minresults,uint32_t basilisktag,void *metricfunc); void basilisks_init(struct supernet_info *myinfo); #endif diff --git a/basilisk/basilisk_bitcoin.c b/basilisk/basilisk_bitcoin.c index c629c0889..259b71557 100755 --- a/basilisk/basilisk_bitcoin.c +++ b/basilisk/basilisk_bitcoin.c @@ -372,6 +372,7 @@ char *basilisk_valuestr(struct iguana_info *coin,char *coinaddr,uint64_t value,i { cJSON *retjson = cJSON_CreateObject(); jaddnum(retjson,"result",dstr(value)); + jaddstr(retjson,"address",coinaddr); jadd64bits(retjson,"value",value); jaddnum(retjson,"height",height); jaddbits256(retjson,"txid",txid); @@ -417,7 +418,7 @@ void *basilisk_bitcoinvalue(struct basilisk_item *Lptr,struct supernet_info *myi Lptr->retstr = clonestr("{\"error\":\"basilisk value missing address\"}"); return(Lptr); } - printf("Scan basilisks values\n"); + //printf("Scan basilisks values\n"); if ( (v= myinfo->basilisks.values) != 0 ) { for (i=0; ibasilisks.numvalues; i++,v++) @@ -430,8 +431,8 @@ void *basilisk_bitcoinvalue(struct basilisk_item *Lptr,struct supernet_info *myi } } } - printf("bitcoinvalue issue remote\n"); - return(basilisk_issueremote(myinfo,"value",coin->symbol,valsobj,0,juint(valsobj,"fanout"),juint(valsobj,"minresults"),basilisktag)); + //printf("bitcoinvalue issue remote\n"); + return(basilisk_issueremote(myinfo,"value",coin->symbol,valsobj,timeoutmillis,juint(valsobj,"fanout"),juint(valsobj,"minresults"),basilisktag,coin->basilisk_valuemetric)); } double basilisk_bitcoin_rawtxmetric_dependents(struct supernet_info *myinfo,struct iguana_info *coin,struct basilisk_item *ptr,struct bitcoin_rawtxdependents *dependents) @@ -500,7 +501,7 @@ double basilisk_bitcoin_rawtxmetric_dependents(struct supernet_info *myinfo,stru double basilisk_bitcoin_rawtxmetric(struct supernet_info *myinfo,struct basilisk_item *ptr,char *resultstr) { - cJSON *txobj,*vouts,*vin,*sobj,*addrs,*vins,*argvals,*resultsobj,*addresses; int64_t outputsum=0,amount=0,cost = -1; int32_t i,m,numaddrs,spendlen,n; struct iguana_msgtx msgtx; uint8_t extraspace[8192],script[IGUANA_MAXSCRIPTSIZE],asmtype; struct vin_info V; char *scriptstr,*changeaddr,*coinaddr,*rawtx,*spendscriptstr; bits256 txid; struct iguana_info *coin; struct basilisk_item Lsubptr,*child; struct bitcoin_rawtxdependents *dependents=0; double metric; uint32_t locktime; + cJSON *txobj,*vouts,*vin,*sobj,*addrs,*vins,*argvals,*resultsobj,*addresses; int64_t outputsum=0,amount=0,cost = -1; int32_t i,m,numaddrs,spendlen,n; struct iguana_msgtx msgtx; uint8_t extraspace[8192],script[IGUANA_MAXSCRIPTSIZE],serialized[16384],asmtype; struct vin_info V; char *scriptstr,*changeaddr,*coinaddr,*rawtx,*spendscriptstr; bits256 txid; struct iguana_info *coin; struct basilisk_item Lsubptr,*child; struct bitcoin_rawtxdependents *dependents=0; double metric; uint32_t locktime; if ( (coin= iguana_coinfind(ptr->symbol)) != 0 ) { if ( (dependents= ptr->dependents) != 0 ) @@ -520,16 +521,18 @@ double basilisk_bitcoin_rawtxmetric(struct supernet_info *myinfo,struct basilisk printf("resultstr error.(%s)\n",resultstr); return(-1.); // error } - spendscriptstr = jstr(ptr->vals,"spendscript"); + if ( (spendscriptstr= jstr(ptr->vals,"spendscript")) != 0 ) + { + spendlen = (int32_t)strlen(spendscriptstr) >> 1; + decode_hex(script,spendlen,spendscriptstr); + } changeaddr = jstr(ptr->vals,"changeaddr"); locktime = juint(ptr->vals,"locktime"); amount = j64bits(ptr->vals,"amount"); addresses = jarray(&numaddrs,ptr->vals,"addresses"); - spendlen = (int32_t)strlen(spendscriptstr) >> 1; - decode_hex(script,spendlen,spendscriptstr); - if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,rawtx,extraspace,sizeof(extraspace))) != 0 ) + if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,rawtx,extraspace,sizeof(extraspace),serialized)) != 0 ) { - printf("GOT VINS.(%s)\n",jprint(vins,0)); + printf("GOT VINS.(%s) rawtx.(%s) out0 %.8f\n",jprint(vins,0),rawtx,dstr(msgtx.vouts[0].value)); if ( juint(txobj,"locktime") != locktime ) { printf("locktime mismatch %u != %u\n",juint(txobj,"locktime"),locktime); @@ -558,6 +561,7 @@ double basilisk_bitcoin_rawtxmetric(struct supernet_info *myinfo,struct basilisk if ( (argvals= cJSON_CreateObject()) != 0 ) { jaddbits256(argvals,"txid",jbits256(vin,"txid")); + jaddnum(argvals,"timeout",ptr->expiration - OS_milliseconds()); jaddnum(argvals,"vout",jint(vin,"vout")); jaddstr(argvals,"address",coinaddr); if ( (dependents->ptrs[i]= basilisk_bitcoinvalue(&Lsubptr,myinfo,coin,0,rand(),(ptr->expiration - OS_milliseconds()) * .777,argvals)) != 0 ) @@ -566,7 +570,11 @@ double basilisk_bitcoin_rawtxmetric(struct supernet_info *myinfo,struct basilisk { dependents->results[i] = Lsubptr.retstr; dependents->ptrs[i] = 0; - } else dependents->ptrs[i]->parent = ptr; + } + else + { + dependents->ptrs[i]->parent = ptr; + } } free_json(argvals); } @@ -577,8 +585,12 @@ double basilisk_bitcoin_rawtxmetric(struct supernet_info *myinfo,struct basilisk for (i=0; ispentsatoshis = msgtx.vouts[i].value; continue; } @@ -611,6 +623,8 @@ double basilisk_bitcoin_rawtxmetric(struct supernet_info *myinfo,struct basilisk printf("illegal outputsum %.8f\n",dstr(outputsum)); return(-1001.); // error } + if ( cost == 0 ) + cost = 1; dependents->cost = cost; return(0.); } @@ -672,6 +686,6 @@ void *basilisk_bitcoinrawtx(struct basilisk_item *Lptr,struct supernet_info *myi Lptr->retstr = clonestr("{\"error\":\"couldnt create rawtx\"}"); return(Lptr); } - return(basilisk_issueremote(myinfo,"rawtx",coin->symbol,valsobj,0,juint(valsobj,"fanout"),juint(valsobj,"minresults"),basilisktag)); + return(basilisk_issueremote(myinfo,"rawtx",coin->symbol,valsobj,timeoutmillis,juint(valsobj,"fanout"),juint(valsobj,"minresults"),basilisktag,coin->basilisk_rawtxmetric)); } diff --git a/iguana/iguana777.h b/iguana/iguana777.h index d595c58c5..032ca9a86 100755 --- a/iguana/iguana777.h +++ b/iguana/iguana777.h @@ -840,7 +840,7 @@ cJSON *iguana_blockjson(struct iguana_info *coin,struct iguana_block *block,int3 void calc_rmd160_sha256(uint8_t rmd160[20],uint8_t *data,int32_t datalen); int32_t bitcoin_checklocktimeverify(uint8_t *script,int32_t n,uint32_t locktime); struct bitcoin_spend *iguana_spendset(struct supernet_info *myinfo,struct iguana_info *coin,int64_t amount,int64_t txfee,cJSON *addresses,int32_t minconf); -cJSON *bitcoin_hex2json(struct iguana_info *coin,bits256 *txidp,struct iguana_msgtx *msgtx,char *txbytes,uint8_t *extrapace,int32_t extralen); +cJSON *bitcoin_hex2json(struct iguana_info *coin,bits256 *txidp,struct iguana_msgtx *msgtx,char *txbytes,uint8_t *extrapace,int32_t extralen,uint8_t *serialized); cJSON *iguana_signtx(struct supernet_info *myinfo,struct iguana_info *coin,bits256 *txidp,char **signedtxp,struct bitcoin_spend *spend,cJSON *txobj,cJSON *vins); void iguana_addscript(struct iguana_info *coin,cJSON *dest,uint8_t *script,int32_t scriptlen,char *fieldname); diff --git a/iguana/iguana_payments.c b/iguana/iguana_payments.c index 09a91567d..abbb88919 100755 --- a/iguana/iguana_payments.c +++ b/iguana/iguana_payments.c @@ -768,7 +768,7 @@ HASH_AND_INT(bitcoinrpc,getrawtransaction,txid,verbose) if ( verbose != 0 ) { extraspace = calloc(1,extralen); - txobj = bitcoin_hex2json(coin,&checktxid,0,txbytes,extraspace,extralen); + txobj = bitcoin_hex2json(coin,&checktxid,0,txbytes,extraspace,extralen,0); free(extraspace); free(txbytes); if ( txobj != 0 ) @@ -821,7 +821,7 @@ STRING_ARG(bitcoinrpc,validaterawtransaction,rawtx) return(clonestr("{\"error\":\"rawtx hex has odd length\"}")); memset(&msgtx,0,sizeof(msgtx)); extraspace = calloc(1,extralen); - if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,rawtx,extraspace,extralen)) != 0 ) + if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,rawtx,extraspace,extralen,0)) != 0 ) { //printf("txobj.(%s)\n",jprint(txobj,0)); if ( (checkstr= bitcoin_json2hex(myinfo,coin,&txid,txobj,0)) != 0 ) @@ -836,7 +836,7 @@ STRING_ARG(bitcoinrpc,validaterawtransaction,rawtx) break; jaddnum(retjson,"mismatch position",i); jadd(retjson,"origtx",txobj); - if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,checkstr,extraspace,extralen)) != 0 ) + if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,checkstr,extraspace,extralen,0)) != 0 ) jadd(retjson,"checktx",txobj); free(checkstr); free(extraspace); @@ -894,7 +894,7 @@ STRING_ARG(bitcoinrpc,decoderawtransaction,rawtx) if ( (strlen(rawtx) & 1) != 0 ) return(clonestr("{\"error\":\"rawtx hex has odd length\"}")); extraspace = calloc(1,extralen); - txobj = bitcoin_hex2json(coin,&txid,0,rawtx,extraspace,extralen); + txobj = bitcoin_hex2json(coin,&txid,0,rawtx,extraspace,extralen,0); free(extraspace); //char str[65]; printf("got txid.(%s)\n",bits256_str(str,txid)); } diff --git a/iguana/iguana_sign.c b/iguana/iguana_sign.c index d304ac151..2b68360cb 100755 --- a/iguana/iguana_sign.c +++ b/iguana/iguana_sign.c @@ -340,7 +340,7 @@ int32_t iguana_rwmsgtx(struct iguana_info *coin,int32_t rwflag,cJSON *json,uint8 jaddnum(json,"version",msg->version); array = cJSON_CreateArray(); if ( rwflag == 0 ) - sigser = calloc(1,maxsize); + sigser = calloc(1,maxsize*2); //printf("json.%p array.%p sigser.%p\n",json,array,sigser); } //printf("version.%d\n",msg->version); @@ -384,8 +384,9 @@ int32_t iguana_rwmsgtx(struct iguana_info *coin,int32_t rwflag,cJSON *json,uint8 { if ( sigser != 0 ) { + //set spendscript here printf("vini.%d spendscript.%p spendlen.%d\n",i,msg->vins[i].spendscript,msg->vins[i].spendlen); - sigtxid = bitcoin_sigtxid(coin,sigser,maxsize,msg,i,msg->vins[i].spendscript,msg->vins[i].spendlen,SIGHASH_ALL,vpnstr); + sigtxid = bitcoin_sigtxid(coin,sigser,maxsize*2,msg,i,msg->vins[i].spendscript,msg->vins[i].spendlen,SIGHASH_ALL,vpnstr); } else memset(sigtxid.bytes,0,sizeof(sigtxid)); jaddi(array,iguana_vinjson(coin,&msg->vins[i],sigtxid)); } @@ -558,9 +559,9 @@ char *bitcoin_json2hex(struct supernet_info *myinfo,struct iguana_info *coin,bit return(txbytes); } -cJSON *bitcoin_hex2json(struct iguana_info *coin,bits256 *txidp,struct iguana_msgtx *msgtx,char *txbytes,uint8_t *extraspace,int32_t extralen) +cJSON *bitcoin_hex2json(struct iguana_info *coin,bits256 *txidp,struct iguana_msgtx *msgtx,char *txbytes,uint8_t *extraspace,int32_t extralen,uint8_t *serialized) { - int32_t n,len; char vpnstr[64]; struct iguana_msgtx M; uint8_t *serialized; cJSON *txobj; + int32_t n,len; char vpnstr[64]; struct iguana_msgtx M; cJSON *txobj; if ( coin == 0 || txbytes == 0 ) return(0); txobj = cJSON_CreateObject(); @@ -568,7 +569,6 @@ cJSON *bitcoin_hex2json(struct iguana_info *coin,bits256 *txidp,struct iguana_ms msgtx = &M; memset(msgtx,0,sizeof(M)); len = (int32_t)strlen(txbytes) >> 1; - serialized = malloc(len); decode_hex(serialized,len,txbytes); vpnstr[0] = 0; memset(txidp,0,sizeof(*txidp)); @@ -579,7 +579,6 @@ cJSON *bitcoin_hex2json(struct iguana_info *coin,bits256 *txidp,struct iguana_ms jaddstr(txobj,"error","couldnt decode transaction"); jaddstr(txobj,"coin",coin->symbol); } - free(serialized); return(txobj); } @@ -1050,17 +1049,18 @@ P2SH_SPENDAPI(iguana,spendmsig,activecoin,vintxid,vinvout,destaddress,destamount int32_t iguana_signrawtransaction(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_msgtx *msgtx,char **signedtxp,bits256 *signedtxidp,struct vin_info *V,int32_t numinputs,char *rawtx,cJSON *vins,cJSON *privkeys) { - uint8_t *serialized,*serialized2,*serialized3,*extraspace; int32_t i,len,n,maxsize,complete = 0,extralen = 65536; char *checkstr,*privkeystr,*signedtx = 0; bits256 privkey,txid; cJSON *item; cJSON *txobj = 0; + uint8_t *serialized,*serialized2,*serialized3,*serialized4,*extraspace; int32_t i,len,n,maxsize,complete = 0,extralen = 65536; char *checkstr,*privkeystr,*signedtx = 0; bits256 privkey,txid; cJSON *item; cJSON *txobj = 0; maxsize = 1000000; if ( rawtx != 0 && rawtx[0] != 0 && (len= (int32_t)strlen(rawtx)>>1) < maxsize ) { serialized = malloc(maxsize); serialized2 = malloc(maxsize); serialized3 = malloc(maxsize); + serialized4 = malloc(maxsize); extraspace = malloc(extralen); memset(msgtx,0,sizeof(*msgtx)); decode_hex(serialized,len,rawtx); - if ( (txobj= bitcoin_hex2json(coin,&txid,msgtx,rawtx,extraspace,extralen)) != 0 ) + if ( (txobj= bitcoin_hex2json(coin,&txid,msgtx,rawtx,extraspace,extralen,serialized4)) != 0 ) { //printf("txobj.(%s)\n",jprint(txobj,0)); if ( (checkstr= bitcoin_json2hex(myinfo,coin,&txid,txobj,0)) != 0 ) @@ -1070,7 +1070,7 @@ int32_t iguana_signrawtransaction(struct supernet_info *myinfo,struct iguana_inf printf("RAW.(%s) ->\nNEW.(%s)\n",rawtx,checkstr); free_json(txobj); free(checkstr); - free(serialized), free(serialized2), free(serialized3); + free(serialized), free(serialized2), free(serialized3), free(serialized4); free(extraspace); return(-2); } @@ -1106,7 +1106,7 @@ int32_t iguana_signrawtransaction(struct supernet_info *myinfo,struct iguana_inf } } free(extraspace); - free(serialized), free(serialized2), free(serialized3); + free(serialized), free(serialized2), free(serialized3), free(serialized4); } else return(-1); if ( txobj != 0 ) free_json(txobj); diff --git a/iguana/swaps/iguana_BTCswap.c b/iguana/swaps/iguana_BTCswap.c index a054110c1..496ffa9c6 100755 --- a/iguana/swaps/iguana_BTCswap.c +++ b/iguana/swaps/iguana_BTCswap.c @@ -251,11 +251,11 @@ struct bitcoin_statetx *instantdex_feetx(struct supernet_info *myinfo,struct ins int32_t instantdex_feetxverify(struct supernet_info *myinfo,struct iguana_info *coin,struct bitcoin_swapinfo *swap,cJSON *argjson) { cJSON *txobj; bits256 txid; uint32_t n; int32_t i,retval = -1,extralen=65536; int64_t insurance; uint64_t r; - struct iguana_msgtx msgtx; uint8_t script[512],*extraspace=0; char coinaddr[64]; + struct iguana_msgtx msgtx; uint8_t script[512],serialized[8192],*extraspace=0; char coinaddr[64]; if ( swap->otherfee != 0 ) { extraspace = calloc(1,extralen); - if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,swap->otherfee->txbytes,extraspace,extralen)) != 0 ) + if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,swap->otherfee->txbytes,extraspace,extralen,serialized)) != 0 ) { r = swap->other.orderid; if ( strcmp(coin->symbol,"BTC") == 0 ) @@ -313,14 +313,14 @@ struct bitcoin_statetx *instantdex_bobtx(struct supernet_info *myinfo,struct bit int32_t instantdex_paymentverify(struct supernet_info *myinfo,struct iguana_info *coin,struct bitcoin_swapinfo *swap,cJSON *argjson,int32_t depositflag) { cJSON *txobj; bits256 txid; uint32_t n,locktime; int32_t i,secretstart,retval = -1,extralen=65536; uint64_t x; - struct iguana_msgtx msgtx; uint8_t script[512],*extraspace=0; int64_t amount; + struct iguana_msgtx msgtx; uint8_t script[512],serialized[8192],*extraspace=0; int64_t amount; if ( coin != 0 && swap->deposit != 0 ) { amount = swap->BTCsatoshis + depositflag*swap->insurance*100 + swap->coinbtc->chain->txfee; if ( (n= instantdex_bobscript(script,0,&locktime,&secretstart,swap,depositflag)) <= 0 ) return(retval); extraspace = calloc(1,extralen); - if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,swap->deposit->txbytes,extraspace,extralen)) != 0 ) + if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,swap->deposit->txbytes,extraspace,extralen,serialized)) != 0 ) { memcpy(&script[secretstart],&msgtx.vouts[0].pk_script[secretstart],20); printf("locktime.%u amount %.8f satoshis %.8f\n",locktime,dstr(amount),dstr(amount)); @@ -355,11 +355,11 @@ int32_t instantdex_paymentverify(struct supernet_info *myinfo,struct iguana_info int32_t instantdex_altpaymentverify(struct supernet_info *myinfo,struct iguana_info *coin,struct bitcoin_swapinfo *swap,cJSON *argjson) { cJSON *txobj; bits256 txid; uint32_t n; int32_t i,retval = -1,extralen = 65536; - struct iguana_msgtx msgtx; uint8_t script[512],*extraspace=0; char *altmsigaddr=0,msigaddr[64]; + struct iguana_msgtx msgtx; uint8_t script[512],serialized[8192],*extraspace=0; char *altmsigaddr=0,msigaddr[64]; if ( swap->altpayment != 0 && (altmsigaddr= jstr(argjson,"altmsigaddr")) != 0 ) { extraspace = calloc(1,extralen); - if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,swap->altpayment->txbytes,extraspace,extralen)) != 0 ) + if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,swap->altpayment->txbytes,extraspace,extralen,serialized)) != 0 ) { n = instantdex_alicescript(script,0,msigaddr,coin->chain->p2shtype,swap->pubAm,swap->pubBn); if ( strcmp(msigaddr,altmsigaddr) == 0 && n == msgtx.vouts[0].pk_scriptlen )