diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 178cd1e3b..817815538 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -52,7 +52,7 @@ void emscripten_usleep(int32_t x); // returns immediate, no sense for sleeping #endif //#define LP_STRICTPEERS -#define LP_DISABLE_DISTCOMBINE +//#define LP_DISABLE_DISTCOMBINE #define LP_MAXVINS 64 #define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL) @@ -479,7 +479,7 @@ int32_t LP_forward(void *ctx,char *myipaddr,int32_t pubsock,bits256 pubkey,char struct LP_peerinfo *LP_peerfind(uint32_t ipbits,uint16_t port); uint64_t LP_value_extract(cJSON *obj,int32_t addinterest); int32_t LP_swap_getcoinaddr(char *symbol,char *coinaddr,bits256 txid,int32_t vout); -char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen); +char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,int32_t stats_JSONonly); int64_t LP_kmdvalue(char *symbol,int64_t satoshis); int64_t LP_komodo_interest(bits256 txid,int64_t value); void LP_availableset(bits256 txid,int32_t vout); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 4778c7b60..c1fab2477 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -85,7 +85,7 @@ void LP_millistats_update(struct LP_millistats *mp) } #include "LP_include.h" -portable_mutex_t LP_peermutex,LP_UTXOmutex,LP_utxomutex,LP_commandmutex,LP_cachemutex,LP_swaplistmutex,LP_forwardmutex,LP_pubkeymutex,LP_networkmutex,LP_psockmutex,LP_coinmutex,LP_messagemutex,LP_portfoliomutex,LP_electrummutex,LP_butxomutex,LP_reservedmutex,LP_nanorecvsmutex,LP_tradebotsmutex,LP_gcmutex,LP_inusemutex,LP_cJSONmutex,LP_logmutex,LP_statslogmutex,LP_tradesmutex; +portable_mutex_t LP_peermutex,LP_UTXOmutex,LP_utxomutex,LP_commandmutex,LP_cachemutex,LP_swaplistmutex,LP_forwardmutex,LP_pubkeymutex,LP_networkmutex,LP_psockmutex,LP_coinmutex,LP_messagemutex,LP_portfoliomutex,LP_electrummutex,LP_butxomutex,LP_reservedmutex,LP_nanorecvsmutex,LP_tradebotsmutex,LP_gcmutex,LP_inusemutex,LP_cJSONmutex,LP_logmutex,LP_statslogmutex,LP_tradesmutex,LP_commandQmutex; int32_t LP_canbind; char *Broadcaststr,*Reserved_msgs[2][1000]; int32_t num_Reserved_msgs[2],max_Reserved_msgs[2]; @@ -192,14 +192,14 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_ #include "LP_messages.c" #include "LP_commands.c" -char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen) +char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,int32_t stats_JSONonly) { char *retstr=0; cJSON *retjson; bits256 zero; if ( jobj(argjson,"result") != 0 || jobj(argjson,"error") != 0 ) return(0); - if ( LP_tradecommand(ctx,myipaddr,pubsock,argjson,data,datalen) <= 0 ) + if ( stats_JSONonly != 0 || LP_tradecommand(ctx,myipaddr,pubsock,argjson,data,datalen) <= 0 ) { - if ( (retstr= stats_JSON(ctx,myipaddr,pubsock,argjson,"127.0.0.1",0)) != 0 ) + if ( (retstr= stats_JSON(ctx,myipaddr,pubsock,argjson,"127.0.0.1",stats_JSONonly)) != 0 ) { //printf("%s PULL.[%d]-> (%s)\n",myipaddr != 0 ? myipaddr : "127.0.0.1",datalen,retstr); //if ( pubsock >= 0 ) //strncmp("{\"error\":",retstr,strlen("{\"error\":")) != 0 && @@ -331,9 +331,10 @@ char *LP_process_message(void *ctx,char *typestr,char *myipaddr,int32_t pubsock, } else { - if ( (retstr= LP_command_process(ctx,myipaddr,pubsock,argjson,&((uint8_t *)ptr)[len],recvlen - len)) != 0 ) - { - } + LP_queuecommand(0,jsonstr,pubsock,0); + //if ( (retstr= LP_command_process(ctx,myipaddr,pubsock,argjson,&((uint8_t *)ptr)[len],recvlen - len)) != 0 ) + //{ + //} } } if ( argjson != 0 ) @@ -349,7 +350,7 @@ char *LP_process_message(void *ctx,char *typestr,char *myipaddr,int32_t pubsock, int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int32_t sock,char *remoteaddr,int32_t maxdepth) { static char *line; - int32_t recvlen=1,msglen,nonz = 0; cJSON *argjson,*recvjson; void *ptr,*msg; char methodstr[64],*decodestr,*retstr,*str; struct nn_pollfd pfd; + int32_t recvlen=1,msglen,nonz = 0; cJSON *recvjson; void *ptr,*msg; char methodstr[64],*decodestr,*retstr,*str; struct nn_pollfd pfd; if ( line == 0 ) line = calloc(1,1024*1024); if ( sock >= 0 ) @@ -407,12 +408,14 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int { if ( (retstr= LP_process_message(ctx,typestr,myipaddr,pubsock,msg,msglen,sock)) != 0 ) free(retstr); + if ( Broadcaststr != 0 ) { //printf("self broadcast.(%s)\n",Broadcaststr); str = Broadcaststr; Broadcaststr = 0; - if ( (argjson= cJSON_Parse(str)) != 0 ) + LP_queuecommand(0,str,pubsock,0); + /*if ( (argjson= cJSON_Parse(str)) != 0 ) { //portable_mutex_lock(&LP_commandmutex); if ( LP_tradecommand(ctx,myipaddr,pubsock,argjson,0,0) <= 0 ) @@ -422,7 +425,7 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int } //portable_mutex_unlock(&LP_commandmutex); free_json(argjson); - } + }*/ free(str); } } @@ -1151,11 +1154,11 @@ int32_t LP_reserved_msg(int32_t priority,char *base,char *rel,bits256 pubkey,cha { pubp->pairsock = -1; LP_peer_pairsock(pubkey); - printf("mark cmdchannel %d closed sentbytes.%d\n",pubp->pairsock,sentbytes); + //printf("mark cmdchannel %d closed sentbytes.%d\n",pubp->pairsock,sentbytes); } else { - printf("sent %d bytes to cmdchannel.%d\n",sentbytes,pubp->pairsock); + //printf("sent %d bytes to cmdchannel.%d\n",sentbytes,pubp->pairsock); return(sentbytes); } } @@ -1257,6 +1260,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu portable_mutex_init(&LP_logmutex); portable_mutex_init(&LP_statslogmutex); portable_mutex_init(&LP_tradesmutex); + portable_mutex_init(&LP_commandQmutex); myipaddr = clonestr("127.0.0.1"); #ifndef _WIN32 #ifndef FROM_JS @@ -1397,6 +1401,11 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu printf("error launching LP_swapsloop for ctx.%p\n",ctx); exit(-1); } + if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_commandQ_loop,ctx) != 0 ) + { + printf("error launching LP_commandQ_loop for ctx.%p\n",ctx); + exit(-1); + } int32_t nonz,didremote=0; LP_statslog_parse(); bitcoind_RPC_inittime = 0; @@ -1464,7 +1473,10 @@ char *barterDEX(char *argstr) printf("barterDEX.(%s)\n",argstr); if ( (argjson= cJSON_Parse(argstr)) != 0 ) { - retstr = LP_command_process(ctx,LP_myipaddr,LP_mypubsock,argjson,(uint8_t *)argstr,(int32_t)strlen(argstr)); + LP_queuecommand(&retstr,argstr,LP_mypubsock); + //retstr = LP_command_process(ctx,LP_myipaddr,LP_mypubsock,argjson,(uint8_t *)argstr,(int32_t)strlen(argstr)); + while ( retstr == 0 ) + usleep(50000); free_json(argjson); } else retstr = clonestr("{\"error\":\"couldnt parse request\"}"); return(retstr); diff --git a/iguana/exchanges/LP_network.c b/iguana/exchanges/LP_network.c index 3c033b3c8..081038caf 100644 --- a/iguana/exchanges/LP_network.c +++ b/iguana/exchanges/LP_network.c @@ -414,10 +414,68 @@ uint32_t LP_swapsend(int32_t pairsock,struct basilisk_swap *swap,uint32_t msgbit return(nextbits); } +struct LP_queuedcommand +{ + struct LP_queuedcommand *next,*prev; + char **retstrp; + int32_t responsesock,msglen,stats_JSONonly; + char msg[]; +} *LP_commandQ; + +void LP_commandQ_loop(void *ctx) +{ + struct LP_queuedcommand *ptr,*tmp; int32_t size,nonz; char *retstr; cJSON *argjson; + while ( LP_STOP_RECEIVED == 0 ) + { + nonz = 0; + DL_FOREACH_SAFE(LP_commandQ,ptr,tmp) + { + nonz++; + portable_mutex_lock(&LP_commandQmutex); + DL_DELETE(LP_commandQ,ptr); + portable_mutex_unlock(&LP_commandQmutex); + if ( (argjson= cJSON_Parse(ptr->msg)) != 0 ) + { + if ( (retstr= LP_command_process(ctx,"127.0.0.1",ptr->responsesock,argjson,(uint8_t *)ptr->msg,ptr->msglen,ptr->stats_JSONonly)) != 0 ) + { + //printf("processed.(%s)\n",retstr); + if ( ptr->responsesock >= 0 && (size= nn_send(ptr->responsesock,retstr,(int32_t)strlen(retstr)+1,0)) <= 0 ) + printf("error sending result\n"); + if ( ptr->retstrp != 0 ) + (*ptr->retstrp) = retstr; + else free(retstr); + } + else if ( ptr->retstrp != 0 ) + (*ptr->retstrp) = clonestr("{\"error\":\"timeout\"}"); + free_json(argjson); + } + free(ptr); + } + if ( nonz == 0 ) + sleep (1); + } +} + +void LP_queuecommand(char **retstrp,char *buf,int32_t responsesock,int32_t stats_JSONonly) +{ + struct LP_queuedcommand *ptr; int32_t msglen; + msglen = (int32_t)strlen(buf) + 1; + portable_mutex_lock(&LP_commandQmutex); + ptr = calloc(1,sizeof(*ptr) + msglen); + if ( (ptr->retstrp= retstrp) != 0 ) + *retstrp = 0; + ptr->responsesock = responsesock; + ptr->msglen = msglen; + ptr->stats_JSONonly = stats_JSONonly; + memcpy(ptr->msg,buf,msglen); + DL_APPEND(LP_commandQ,ptr); + portable_mutex_unlock(&LP_commandQmutex); +} + void LP_psockloop(void *_ptr) { static struct nn_pollfd *pfds; - int32_t nexti=0,j,i,n,nonz,iter,retval,sentbytes,size=0,sendsock = -1; uint32_t now; struct psock *ptr=0; void *buf=0; char keepalive[512]; void *ctx = bitcoin_ctx(); + int32_t nexti=0,j,i,n,nonz,iter,retval,sentbytes,size=0,sendsock = -1; uint32_t now; struct psock *ptr=0; void *buf=0; char keepalive[512]; strcpy(LP_psockloop_stats.name,"LP_psockloop"); LP_psockloop_stats.threshold = 1000.; while ( LP_STOP_RECEIVED == 0 ) @@ -476,22 +534,7 @@ void LP_psockloop(void *_ptr) ptr->lasttime = now; if ( ptr->cmdchannel == 0 ) sendsock = ptr->sendsock; - else - { - char *retstr; cJSON *argjson; - //printf("nn_recv.(%s)\n",(char *)buf); - if ( (argjson= cJSON_Parse((char *)buf)) != 0 ) - { - if ( (retstr= LP_command_process(ctx,"127.0.0.0",ptr->publicsock,argjson,buf,size)) != 0 ) - { - printf("processed.(%s)\n",retstr); - if ( (size= nn_send(ptr->publicsock,retstr,(int32_t)strlen(retstr)+1,0)) <= 0 ) - printf("error sending result\n"); - free(retstr); - } - free_json(argjson); - } - } + else LP_queuecommand(0,(char *)buf,ptr->publicsock,0); nexti = i+1; break; } @@ -671,7 +714,7 @@ char *_LP_psock_create(int32_t *pullsockp,int32_t *pubsockp,char *ipaddr,uint16_ { if ( pubp->pairsock > 0 ) { - printf("warning %s already has pairsock.%d, mark for purge\n",bits256_str(str,pubkey),pubp->pairsock); + //printf("warning %s already has pairsock.%d, mark for purge\n",bits256_str(str,pubkey),pubp->pairsock); for (i=0; ipairsock ) { @@ -779,7 +822,7 @@ uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired,int32_ { if ( (retjson= cJSON_Parse(retstr)) != 0 ) { - printf("from %s:%u (%s)\n",peer->ipaddr,peer->port,retstr); + //printf("from %s:%u (%s)\n",peer->ipaddr,peer->port,retstr); if ( (addr= jstr(retjson,"publicaddr")) != 0 ) safecopy(publicaddr,addr,128); if ( (addr= jstr(retjson,"connectaddr")) != 0 ) @@ -791,7 +834,7 @@ uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired,int32_ //printf("got.(%s) connect.%s public.%s publicport.%u\n",retstr,connectaddr,publicaddr,publicport); free(retstr); return(publicport); - } else printf("error psock from %s:%u\n",peer->ipaddr,peer->port); + } //else printf("error psock from %s:%u\n",peer->ipaddr,peer->port); } return(0); } diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 3db5769e0..fa2a51683 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -411,7 +411,7 @@ struct LP_utxoinfo *LP_address_myutxopair(struct LP_utxoinfo *butxo,int32_t iamb { up = utxos[mini]; utxos[mini] = 0; - printf("found mini.%d %.8f for targetval %.8f -> targetval2 %.8f, ratio %.2f\n",mini,dstr(up->U.value),dstr(targetval),dstr(targetval2),(double)up->U.value/targetval); + //printf("found mini.%d %.8f for targetval %.8f -> targetval2 %.8f, ratio %.2f\n",mini,dstr(up->U.value),dstr(targetval),dstr(targetval2),(double)up->U.value/targetval); if ( (double)up->U.value/targetval < ratio-1 ) { @@ -871,9 +871,9 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru printf("request from blacklisted %s, ignore\n",bits256_str(str,qp->desthash)); return(0); } - printf("LP_address_utxo_reset\n"); + printf("LP_address_utxo_reset.%s\n",coin->symbol); LP_address_utxo_reset(coin); - printf("done LP_address_utxo_reset\n"); + printf("done LP_address_utxo_reset.%s\n",coin->symbol); if ( (butxo= LP_address_myutxopair(butxo,1,utxos,max,LP_coinfind(qp->srccoin),qp->coinaddr,qp->txfee,dstr(qp->destsatoshis),price,qp->desttxfee)) != 0 ) { strcpy(qp->gui,G.gui); @@ -1019,7 +1019,7 @@ void LP_tradesloop(void *ctx) HASH_FIND(hh,LP_trades,&qtp->aliceid,sizeof(qtp->aliceid),tp); if ( tp == 0 ) { - if ( 0 && now > Q.timestamp+LP_AUTOTRADE_TIMEOUT*20 ) // eat expired + if ( now > Q.timestamp+LP_AUTOTRADE_TIMEOUT*20 ) // eat expired free(qtp); else { @@ -1135,7 +1135,7 @@ void LP_tradesloop(void *ctx) timeout += LP_AUTOTRADE_TIMEOUT * .5; if ( now > tp->firstprocessed+timeout*10 ) { - printf("purge swap aliceid.%llu\n",(long long)tp->aliceid); + //printf("purge swap aliceid.%llu\n",(long long)tp->aliceid); portable_mutex_lock(&LP_tradesmutex); HASH_DELETE(hh,LP_trades,tp); portable_mutex_unlock(&LP_tradesmutex); @@ -1180,13 +1180,12 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson, LP_requestinit(&Q.R,Q.srchash,Q.desthash,Q.srccoin,Q.satoshis-Q.txfee,Q.destcoin,Q.destsatoshis-Q.desttxfee,Q.timestamp,Q.quotetime,DEXselector); LP_tradecommand_log(argjson); rq = ((uint64_t)Q.R.requestid << 32) | Q.R.quoteid; - printf("%-4d (%-10u %10u) %12s id.%-20llu %5s/%-5s %12.8f -> %12.8f (%11.8f) | RT.%d %d n%d\n",(uint32_t)time(NULL) % 3600,Q.R.requestid,Q.R.quoteid,method,(long long)Q.aliceid,Q.srccoin,Q.destcoin,dstr(Q.satoshis),dstr(Q.destsatoshis),(double)Q.destsatoshis/Q.satoshis,LP_RTcount,LP_swapscount,G.netid); if ( Q.timestamp > 0 && time(NULL) > Q.timestamp + LP_AUTOTRADE_TIMEOUT*20 ) // eat expired packets, some old timestamps floating about? { printf("aliceid.%llu is expired by %d\n",(long long)Q.aliceid,(uint32_t)time(NULL) - (Q.timestamp + LP_AUTOTRADE_TIMEOUT*20)); return(1); } - //LP_autoprices_update(method,Q.srccoin,dstr(Q.satoshis),Q.destcoin,dstr(Q.destsatoshis)); + printf("%-4d (%-10u %10u) %12s id.%-20llu %5s/%-5s %12.8f -> %12.8f (%11.8f) | RT.%d %d n%d\n",(uint32_t)time(NULL) % 3600,Q.R.requestid,Q.R.quoteid,method,(long long)Q.aliceid,Q.srccoin,Q.destcoin,dstr(Q.satoshis),dstr(Q.destsatoshis),(double)Q.destsatoshis/Q.satoshis,LP_RTcount,LP_swapscount,G.netid); retval = 1; aliceid = j64bits(argjson,"aliceid"); qprice = jdouble(argjson,"price"); diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 995964069..bbafba65b 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -235,7 +235,7 @@ bits256 basilisk_swap_privbob_extract(char *symbol,bits256 spendtxid,int32_t vin privkey.bytes[31 - i] = script[siglen+2+i]; else privkey.bytes[i] = script[siglen+2+i]; } - char str[65]; printf("extracted privbob.(%s)\n",bits256_str(str,privkey)); + //char str[65]; printf("extracted privbob.(%s)\n",bits256_str(str,privkey)); } return(privkey); } @@ -1027,7 +1027,9 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti for (j=0; j<32; j++) rev.bytes[j] = rswap.myprivs[0].bytes[31 - j]; if ( (rswap.txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"alicespend",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[0],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBPAYMENT],0,0,rswap.pubkey33,1,rswap.expiration,&rswap.values[BASILISK_ALICESPEND],0,0,rswap.bobpaymentaddr,1,bob->zcash)) != 0 ) - printf("alicespend.(%s)\n",rswap.txbytes[BASILISK_ALICESPEND]); + { + //printf("alicespend.(%s)\n",rswap.txbytes[BASILISK_ALICESPEND]); + } } LP_txbytes_update("alicespend",rswap.bobcoin,rswap.txbytes[BASILISK_ALICESPEND],&rswap.txids[BASILISK_ALICESPEND],&rswap.paymentspent,&rswap.sentflags[BASILISK_ALICESPEND]); } @@ -1137,7 +1139,9 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti LP_txbytes_update("bobreclaim",rswap.bobcoin,rswap.txbytes[BASILISK_BOBRECLAIM],&rswap.txids[BASILISK_BOBRECLAIM],&rswap.paymentspent,&rswap.sentflags[BASILISK_BOBRECLAIM]); } else if ( flag == 0 ) - printf("bobpayment: now.%u < expiration %u\n",(uint32_t)time(NULL),rswap.expiration); + { + //printf("bobpayment: now.%u < expiration %u\n",(uint32_t)time(NULL),rswap.expiration); + } } if ( rswap.sentflags[BASILISK_BOBREFUND] == 0 && rswap.sentflags[BASILISK_BOBDEPOSIT] != 0 && bits256_nonz(rswap.txids[BASILISK_BOBDEPOSIT]) != 0 && bits256_nonz(rswap.depositspent) == 0 ) { diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 76a4f820d..0e2daec6b 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -735,7 +735,7 @@ char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON * printf("incomplete signing %s (%s)\n",rawtx,jprint(vins,0)); if ( signedtx != 0 ) free(signedtx), signedtx = 0; - } else printf("basilisk_swap_bobtxspend %s -> %s\n",rawtx,bits256_str(str,*signedtxidp)); + } // else printf("basilisk_swap_bobtxspend %s -> %s\n",rawtx,bits256_str(str,*signedtxidp)); if ( signedtx == 0 ) { retjson = cJSON_CreateObject(); diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index 0cacc3ac5..3f873cf76 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -680,7 +680,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ else { LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,clonestr(msg)); - LP_reserved_msg(0,qp->srccoin,qp->destcoin,zero,clonestr(msg)); + //LP_reserved_msg(0,qp->srccoin,qp->destcoin,zero,clonestr(msg)); } free(msg); /*portable_mutex_lock(&LP_reservedmutex); diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 9f619527d..a4b9abf09 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -625,7 +625,7 @@ int32_t LP_rawtx_spendscript(struct basilisk_swap *swap,int32_t height,struct ba if ( recvlen != datalen+rawtx->I.redeemlen+75 ) printf("RECVLEN %d != %d + %d\n",recvlen,datalen,rawtx->I.redeemlen); txid = bits256_calctxid(coin->symbol,data,datalen); - char str[65]; printf("rawtx.%s txid %s\n",rawtx->name,bits256_str(str,txid)); + //char str[65]; printf("rawtx.%s txid %s\n",rawtx->name,bits256_str(str,txid)); if ( bits256_cmp(txid,rawtx->I.actualtxid) != 0 && bits256_nonz(rawtx->I.actualtxid) == 0 ) rawtx->I.actualtxid = txid; if ( (txobj= bitcoin_data2json(coin->symbol,coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,height,&rawtx->I.signedtxid,&rawtx->msgtx,rawtx->extraspace,sizeof(rawtx->extraspace),data,datalen,0,suppress_pubkeys,coin->zcash)) != 0 ) @@ -659,9 +659,8 @@ int32_t LP_rawtx_spendscript(struct basilisk_swap *swap,int32_t height,struct ba retval = 0; if ( rawtx == &swap->otherfee ) { - char str[65]; LP_swap_coinaddr(coin,rawtx->p2shaddr,0,data,datalen,0); - printf("got %s txid.%s (%s) -> %s\n",rawtx->name,bits256_str(str,rawtx->I.signedtxid),jprint(txobj,0),rawtx->p2shaddr); + //printf("got %s txid.%s (%s) -> %s\n",rawtx->name,bits256_str(str,rawtx->I.signedtxid),jprint(txobj,0),rawtx->p2shaddr); } else bitcoin_address(coin->symbol,rawtx->p2shaddr,coin->taddr,coin->p2shtype,rawtx->spendscript,hexlen); } } else printf("%s satoshis %.8f ERROR.(%s) txfees.[%.8f %.8f: %.8f] amount.%.8f -> %.8f\n",rawtx->name,dstr(j64bits(vout,"satoshis")),jprint(txobj,0),dstr(swap->I.Atxfee),dstr(swap->I.Btxfee),dstr(txfee),dstr(rawtx->I.amount),dstr(rawtx->I.amount)-dstr(txfee)); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 665272fd1..ce0b1fc90 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -744,7 +744,7 @@ void test_validate(struct iguana_info *coin,char *signedtx) char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,uint8_t wiftaddr,uint8_t taddr,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,void *ctx,bits256 privkey,bits256 *privkey2p,uint8_t *redeemscript,int32_t redeemlen,uint8_t *userdata,int32_t userdatalen,bits256 utxotxid,int32_t utxovout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr,char *vinaddr,int32_t suppress_pubkeys,int32_t zcash) { - char *rawtxbytes=0,*signedtx=0,str[65],tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *txobj,*vins,*obj,*vouts,*item,*privkeys; int32_t completed,spendlen,n,ignore_cltverr=1; struct vin_info V[8]; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value=0,change = 0; struct iguana_msgtx msgtx; struct iguana_info *coin; + char *rawtxbytes=0,*signedtx=0,tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *txobj,*vins,*obj,*vouts,*item,*privkeys; int32_t completed,spendlen,n,ignore_cltverr=1; struct vin_info V[8]; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value=0,change = 0; struct iguana_msgtx msgtx; struct iguana_info *coin; LP_mark_spent(symbol,utxotxid,utxovout); *destamountp = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); @@ -771,7 +771,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch } else printf("cant gettx\n"); if ( value == 0 ) { - printf("basilisk_swap_bobtxspend.%s %s utxo.(%s).v%d already spent or doesnt exist\n",name,symbol,bits256_str(str,utxotxid),utxovout); + //printf("basilisk_swap_bobtxspend.%s %s utxo.(%s).v%d already spent or doesnt exist\n",name,symbol,bits256_str(str,utxotxid),utxovout); return(0); } #endif @@ -790,7 +790,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch else if ( value == satoshis && (double)txfee/value < 0.25 ) { satoshis = value - txfee; - printf("txfee allocation from value %.8f identical to satoshis: %.8f txfee %.8f\n",dstr(value),dstr(satoshis),dstr(txfee)); + //printf("txfee allocation from value %.8f identical to satoshis: %.8f txfee %.8f\n",dstr(value),dstr(satoshis),dstr(txfee)); } else { @@ -800,7 +800,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch } if ( value > satoshis+txfee ) change = value - (satoshis + txfee); - printf("utxo %.8f, destamount %.8f change %.8f txfee %.8f\n",dstr(value),dstr(satoshis),dstr(change),dstr(txfee)); + //printf("utxo %.8f, destamount %.8f change %.8f txfee %.8f\n",dstr(value),dstr(satoshis),dstr(change),dstr(txfee)); } else if ( value > txfee ) satoshis = value - txfee; else @@ -904,7 +904,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch printf("incomplete signing suppress.%d %s (%s)\n",suppress_pubkeys,name,jprint(vins,0)); if ( signedtx != 0 ) free(signedtx), signedtx = 0; - } else printf("basilisk_swap_bobtxspend %s -> %s\n",name,bits256_str(str,*signedtxidp)); + } // else printf("basilisk_swap_bobtxspend %s -> %s\n",name,bits256_str(str,*signedtxidp)); free(rawtxbytes); } else printf("error making rawtx suppress.%d\n",suppress_pubkeys); free_json(privkeys); @@ -1231,7 +1231,7 @@ char *LP_createrawtransaction(cJSON **txobjp,int32_t *numvinsp,struct iguana_inf return(0); } amount += value; - printf("vout.%d %.8f -> total %.8f\n",i,dstr(value),dstr(amount)); + //printf("vout.%d %.8f -> total %.8f\n",i,dstr(value),dstr(amount)); } else { @@ -1274,7 +1274,7 @@ char *LP_createrawtransaction(cJSON **txobjp,int32_t *numvinsp,struct iguana_inf txobj = bitcoin_txcreate(coin->symbol,coin->isPoS,locktime,coin->txversion,timestamp); jdelete(txobj,"vin"); jadd(txobj,"vin",jduplicate(vins)); - printf("change %.8f = total %.8f - amount %.8f, adjust %.8f numvouts.%d\n",dstr(change),dstr(total),dstr(amount),dstr(adjust),numvouts); + //printf("change %.8f = total %.8f - amount %.8f, adjust %.8f numvouts.%d\n",dstr(change),dstr(total),dstr(amount),dstr(adjust),numvouts); for (i=0; i %s\n",coin->symbol,jprint(argjson,0),bits256_str(str,signedtxid)); + } //else printf("LP_withdraw.%s %s -> %s\n",coin->symbol,jprint(argjson,0),bits256_str(str,signedtxid)); if ( signedtx == 0 ) break; datalen = (int32_t)strlen(signedtx) / 2; @@ -1906,7 +1906,7 @@ void LP_swap_coinaddr(struct iguana_info *coin,char *coinaddr,uint64_t *valuep,u int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,int32_t genflag) { - int32_t j; char coinaddr[64],checkaddr[64]; struct iguana_info *coin; + char coinaddr[64],checkaddr[64]; struct iguana_info *coin; if ( (coin= LP_coinfind(swap->I.bobstr)) != 0 ) { bitcoin_address(coin->symbol,coinaddr,coin->taddr,coin->pubtype,swap->changermd160,20); @@ -1931,13 +1931,13 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i } else { - for (j=0; jbobpayment.I.datalen; j++) + /*for (j=0; jbobpayment.I.datalen; j++) printf("%02x",swap->bobpayment.txbytes[j]); printf(" <- bobpayment.%d\n",swap->bobpayment.I.datalen); for (j=0; jbobpayment.I.redeemlen; j++) printf("%02x",swap->bobpayment.redeemscript[j]); printf(" <- redeem.%d\n",swap->bobpayment.I.redeemlen); - printf(" <- GENERATED BOB PAYMENT.%d destaddr.(%s)\n",swap->bobpayment.I.datalen,swap->bobpayment.I.destaddr); + printf(" <- GENERATED BOB PAYMENT.%d destaddr.(%s)\n",swap->bobpayment.I.datalen,swap->bobpayment.I.destaddr);*/ LP_swap_coinaddr(coin,checkaddr,0,swap->bobpayment.txbytes,swap->bobpayment.I.datalen,0); if ( strcmp(swap->bobpayment.I.destaddr,checkaddr) != 0 ) { @@ -1968,9 +1968,9 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i } else { - for (j=0; jbobdeposit.I.datalen; j++) - printf("%02x",swap->bobdeposit.txbytes[j]); - printf(" <- GENERATED BOB DEPOSIT.%d (%s)\n",swap->bobdeposit.I.datalen,swap->bobdeposit.I.destaddr); + //for (j=0; jbobdeposit.I.datalen; j++) + // printf("%02x",swap->bobdeposit.txbytes[j]); + //printf(" <- GENERATED BOB DEPOSIT.%d (%s)\n",swap->bobdeposit.I.datalen,swap->bobdeposit.I.destaddr); LP_swap_coinaddr(coin,checkaddr,0,swap->bobdeposit.txbytes,swap->bobdeposit.I.datalen,0); if ( strcmp(swap->bobdeposit.I.destaddr,checkaddr) != 0 ) { @@ -1978,7 +1978,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i return(-1); } LP_unspents_mark(coin->symbol,swap->bobdeposit.vins); - printf("bobscripts set completed\n"); + //printf("bobscripts set completed\n"); return(0); } } @@ -2044,26 +2044,26 @@ int32_t basilisk_alicetxs(int32_t pairsock,struct basilisk_swap *swap,uint8_t *d //LP_importaddress(coin->symbol,swap->alicepayment.I.destaddr); strcpy(swap->alicepayment.p2shaddr,swap->alicepayment.I.destaddr); retval = 0; - for (i=0; ialicepayment.I.datalen; i++) - printf("%02x",swap->alicepayment.txbytes[i]); - printf(" ALICE PAYMENT created.(%s)\n",swap->alicepayment.I.destaddr); + //for (i=0; ialicepayment.I.datalen; i++) + // printf("%02x",swap->alicepayment.txbytes[i]); + //printf(" ALICE PAYMENT created.(%s)\n",swap->alicepayment.I.destaddr); LP_unspents_mark(coin->symbol,swap->alicepayment.vins); //LP_importaddress(coin->symbol,swap->alicepayment.I.destaddr); //basilisk_txlog(swap,&swap->alicepayment,-1); } if ( swap->myfee.I.datalen == 0 ) { - printf("%s generate fee %.8f from.%s\n",coin->symbol,dstr(strcmp(coin->symbol,"BTC") == 0 ? LP_MIN_TXFEE : coin->txfee),coin->smartaddr); + //printf("%s generate fee %.8f from.%s\n",coin->symbol,dstr(strcmp(coin->symbol,"BTC") == 0 ? LP_MIN_TXFEE : coin->txfee),coin->smartaddr); bitcoin_address(coin->symbol,coinaddr,coin->taddr,coin->pubtype,swap->changermd160,20); if ( basilisk_rawtx_gen(swap->ctx,"myfee",swap->I.started,swap->persistent_pubkey33,swap->I.iambob,1,&swap->myfee,swap->myfee.I.locktime,swap->myfee.spendscript,swap->myfee.I.spendlen,strcmp(coin->symbol,"BTC") == 0 ? LP_MIN_TXFEE : coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,coinaddr) == 0 ) { - printf("rawtxsend %s %.8f\n",coin->symbol,dstr(strcmp(coin->symbol,"BTC") == 0 ? LP_MIN_TXFEE : coin->txfee)); + //printf("rawtxsend %s %.8f\n",coin->symbol,dstr(strcmp(coin->symbol,"BTC") == 0 ? LP_MIN_TXFEE : coin->txfee)); swap->I.statebits |= LP_swapdata_rawtxsend(pairsock,swap,0x80,data,maxlen,&swap->myfee,0x40,0); LP_unspents_mark(swap->I.iambob!=0?coin->symbol:coin->symbol,swap->myfee.vins); //basilisk_txlog(swap,&swap->myfee,-1); - for (i=0; imyfee.I.datalen; i++) - printf("%02x",swap->myfee.txbytes[i]); - printf(" <- fee state.%x\n",swap->I.statebits); + //for (i=0; imyfee.I.datalen; i++) + // printf("%02x",swap->myfee.txbytes[i]); + //printf(" <- fee state.%x\n",swap->I.statebits); swap->I.statebits |= 0x40; } else @@ -2074,7 +2074,7 @@ int32_t basilisk_alicetxs(int32_t pairsock,struct basilisk_swap *swap,uint8_t *d } if ( swap->alicepayment.I.datalen != 0 && swap->alicepayment.I.spendlen > 0 && swap->myfee.I.datalen != 0 && swap->myfee.I.spendlen > 0 ) { - printf("fee sent\n"); + //printf("fee sent\n"); return(0); } } else printf("basilisk alicetx cant find (%s)\n",swap->I.alicestr); @@ -2088,14 +2088,16 @@ int32_t LP_verify_otherfee(struct basilisk_swap *swap,uint8_t *data,int32_t data { if ( LP_rawtx_spendscript(swap,coin->longestchain,&swap->otherfee,0,data,datalen,0) == 0 ) { - printf("otherfee amount %.8f -> %s vs %s locktime %u vs %u\n",dstr(swap->otherfee.I.amount),swap->otherfee.p2shaddr,swap->otherfee.I.destaddr,swap->otherfee.I.locktime,swap->I.started+1); + //printf("otherfee amount %.8f -> %s vs %s locktime %u vs %u\n",dstr(swap->otherfee.I.amount),swap->otherfee.p2shaddr,swap->otherfee.I.destaddr,swap->otherfee.I.locktime,swap->I.started+1); if ( strcmp(swap->otherfee.I.destaddr,swap->otherfee.p2shaddr) == 0 ) { diff = swap->otherfee.I.locktime - (swap->I.started+1); if ( diff < 0 ) diff = -diff; if ( diff < LP_AUTOTRADE_TIMEOUT ) - printf("dexfee verified\n"); + { + //printf("dexfee verified\n"); + } else printf("locktime mismatch in otherfee, reject %u vs %u\n",swap->otherfee.I.locktime,swap->I.started+1); return(0); } else printf("destaddress mismatch in other fee, reject (%s) vs (%s)\n",swap->otherfee.I.destaddr,swap->otherfee.p2shaddr); @@ -2162,12 +2164,12 @@ int32_t LP_verify_bobdeposit(struct basilisk_swap *swap,uint8_t *data,int32_t da retval = 0; if ( (retval= basilisk_rawtx_sign(coin->symbol,coin->wiftaddr,coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,swap,&swap->aliceclaim,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160,swap->bobdeposit.I.destaddr,coin->zcash)) == 0 ) { - int32_t i; for (i=0; ibobdeposit.I.datalen; i++) + /*int32_t i; for (i=0; ibobdeposit.I.datalen; i++) printf("%02x",swap->bobdeposit.txbytes[i]); printf(" <- bobdeposit\n"); for (i=0; ialiceclaim.I.datalen; i++) printf("%02x",swap->aliceclaim.txbytes[i]); - printf(" <- aliceclaim\n"); + printf(" <- aliceclaim\n");*/ //basilisk_txlog(swap,&swap->aliceclaim,swap->I.putduration+swap->I.callduration); return(LP_waitmempool(coin->symbol,swap->bobdeposit.I.destaddr,swap->bobdeposit.I.signedtxid,0,60)); } else printf("error signing aliceclaim suppress.%d vin.(%s)\n",swap->aliceclaim.I.suppress_pubkeys,swap->bobdeposit.I.destaddr); diff --git a/iguana/exchanges/LP_utxo.c b/iguana/exchanges/LP_utxo.c index 4887f76af..58ea12c7a 100644 --- a/iguana/exchanges/LP_utxo.c +++ b/iguana/exchanges/LP_utxo.c @@ -454,10 +454,10 @@ struct LP_address *LP_address_utxo_reset(struct iguana_info *coin) printf("LP_address_utxo_reset: cant find address data\n"); return(0); } - if ( time(NULL) < coin->lastresetutxo+30 ) + if ( IAMLP != 0 && time(NULL) < coin->lastresetutxo+30 ) return(ap); coin->lastresetutxo = (uint32_t)time(NULL); - if ( (array= LP_listunspent(coin->symbol,coin->smartaddr,zero,zero)) != 0 ) + if ( coin->electrum != 0 && (array= LP_listunspent(coin->symbol,coin->smartaddr,zero,zero)) != 0 ) { //printf("clear ap->utxos\n"); DL_FOREACH_SAFE(ap->utxos,up,tmp) diff --git a/iguana/exchanges/stats.c b/iguana/exchanges/stats.c index d08202993..74469d96f 100644 --- a/iguana/exchanges/stats.c +++ b/iguana/exchanges/stats.c @@ -29,6 +29,7 @@ #define STATS_DEST "/var/www/html/DEXstats.json" #include "DEXstats.h" char *stats_JSON(void *ctx,char *myipaddr,int32_t mypubsock,cJSON *argjson,char *remoteaddr,uint16_t port); +void LP_queuecommand(char **retstrp,char *buf,int32_t responsesock,int32_t stats_JSONonly); extern uint32_t DOCKERFLAG; char *stats_validmethods[] = @@ -333,7 +334,7 @@ extern void *bitcoin_ctx(); char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *postflagp,char *urlstr,char *remoteaddr,char *filetype,uint16_t port) { static void *ctx; - cJSON *tokens,*argjson,*origargjson,*tmpjson=0,*json = 0; long filesize; char *myipaddr="127.0.0.1",symbol[64],buf[4096],*userpass=0,urlmethod[16],*data,url[8192],furl[8192],*retstr=0,*filestr,*token = 0; int32_t i,j,n,num=0; + cJSON *tokens,*argjson,*origargjson,*tmpjson=0,*json = 0; long filesize; char symbol[64],buf[4096],*userpass=0,urlmethod[16],*data,url[8192],furl[8192],*retstr=0,*filestr,*token = 0; int32_t i,j,n,num=0; if ( ctx == 0 ) ctx = bitcoin_ctx(); for (i=0; i 0 ) { - cJSON *retitem,*retarray = cJSON_CreateArray(); + char *buf; cJSON *retitem,*retarray = cJSON_CreateArray(); origargjson = argjson; symbol[0] = 0; for (i=0; i 0 ) { - if ( (retstr= stats_JSON(ctx,myipaddr,-1,argjson,remoteaddr,port)) != 0 ) + buf = jprint(argjson,0); + LP_queuecommand(&retstr,buf,-1,1); + free(buf); + while ( retstr == 0 ) + usleep(10000); + //if ( (retstr= stats_JSON(ctx,myipaddr,-1,argjson,remoteaddr,port)) != 0 ) { if ( (retitem= cJSON_Parse(retstr)) != 0 ) jaddi(retarray,retitem); @@ -531,7 +537,12 @@ char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *po } } else retstr = clonestr("{\"error\":\"invalid remote method\"}"); #else - if ( (retstr= stats_JSON(ctx,myipaddr,-1,argjson,remoteaddr,port)) != 0 ) + buf = jprint(argjson,0); + LP_queuecommand(&retstr,buf,-1,1); + free(buf); + while ( retstr == 0 ) + usleep(10000); + //if ( (retstr= stats_JSON(ctx,myipaddr,-1,argjson,remoteaddr,port)) != 0 ) { if ( (retitem= cJSON_Parse(retstr)) != 0 ) jaddi(retarray,retitem); @@ -545,7 +556,7 @@ char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *po } else { - cJSON *arg; + cJSON *arg; char *buf; if ( jstr(argjson,"agent") != 0 && strcmp(jstr(argjson,"agent"),"bitcoinrpc") != 0 && jobj(argjson,"params") != 0 ) { arg = jobj(argjson,"params"); @@ -557,10 +568,21 @@ char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *po jaddstr(arg,"userpass",userpass); #ifdef FROM_MARKETMAKER if ( strcmp(remoteaddr,"127.0.0.1") == 0 || LP_valid_remotemethod(arg) > 0 ) - retstr = stats_JSON(ctx,myipaddr,-1,arg,remoteaddr,port); - else retstr = clonestr("{\"error\":\"invalid remote method\"}"); + { + buf = jprint(arg,0); + LP_queuecommand(&retstr,buf,-1,1); + free(buf); + while ( retstr == 0 ) + usleep(10000); + //retstr = stats_JSON(ctx,myipaddr,-1,arg,remoteaddr,port); + } else retstr = clonestr("{\"error\":\"invalid remote method\"}"); #else - retstr = stats_JSON(ctx,myipaddr,-1,arg,remoteaddr,port); + buf = jprint(arg,0); + LP_queuecommand(&retstr,buf,-1,1); + free(buf); + while ( retstr == 0 ) + usleep(10000); + //retstr = stats_JSON(ctx,myipaddr,-1,arg,remoteaddr,port); #endif } free_json(argjson); @@ -568,7 +590,7 @@ char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *po free_json(json); if ( tmpjson != 0 ) free(tmpjson); - //printf("stats_JSON rpc return.(%s)\n",retstr); +//printf("stats_JSON rpc return.(%s)\n",retstr); return(retstr); } free_json(argjson);