From c1d67186bb7fc70f94d59ea37a37c19fae9d35ee Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 2 Mar 2018 16:22:35 +0200 Subject: [PATCH] Swap events --- iguana/exchanges/LP_include.h | 1 + iguana/exchanges/LP_ordermatch.c | 34 ++++++++++++++++++----- iguana/exchanges/LP_remember.c | 12 +++++++++ iguana/exchanges/LP_signatures.c | 13 ++++++--- iguana/exchanges/LP_stats.c | 12 ++++++--- iguana/exchanges/LP_swap.c | 46 ++++++++++++++++---------------- 6 files changed, 81 insertions(+), 37 deletions(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index f5eba97a2..2b0366424 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -490,6 +490,7 @@ int32_t LP_pullsock_check(void *ctx,char **retstrp,char *myipaddr,int32_t pubsoc int64_t LP_listunspent_parseitem(struct iguana_info *coin,bits256 *txidp,int32_t *voutp,int32_t *heightp,cJSON *item); void LP_unspents_cache(char *symbol,char *addr,char *arraystr,int32_t updatedflag); uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired,int32_t cmdchannel,char *ipaddr); +void LP_failedmsg(uint32_t requestid,uint32_t quoteid,double val); //void LP_utxo_clientpublish(struct LP_utxoinfo *utxo); //int32_t LP_coinbus(uint16_t coin_busport); int32_t LP_nanomsg_recvs(void *ctx); diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 95b1c1e06..8834fab67 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -24,6 +24,23 @@ bits256 LP_Alicedestpubkey,LP_bobs_reserved; uint32_t Alice_expiration,Bob_expiration; struct { uint64_t aliceid; double bestprice; uint32_t starttime,counter; } Bob_competition[512]; + +void LP_failedmsg(uint32_t requestid,uint32_t quoteid,double val) +{ + char *msg; cJSON *retjson; + if ( IPC_ENDPOINT >= 0 ) + { + retjson = cJSON_CreateObject(); + jaddstr(retjson,"method","failed"); + jaddnum(retjson,"error",val); + jaddnum(retjson,"requestid",requestid); + jaddnum(retjson,"quoteid",quoteid); + msg = jprint(retjson,1); + LP_queuecommand(0,msg,IPC_ENDPOINT,-1,0); + free(msg); + } +} + double LP_bob_competition(int32_t *counterp,uint64_t aliceid,double price,int32_t counter) { int32_t i,firsti = -1; uint32_t now = (uint32_t)time(NULL); @@ -1016,7 +1033,7 @@ struct LP_quoteinfo *LP_trades_gotreserved(void *ctx,struct LP_quoteinfo *qp,str if ( (retstr= LP_quotereceived(qp)) != 0 ) free(retstr); return(qp); - } + } else LP_failedmsg(qp->R.requestid,qp->R.quoteid,qprice); return(0); } @@ -1037,17 +1054,22 @@ struct LP_quoteinfo *LP_trades_gotconnect(void *ctx,struct LP_quoteinfo *qp,stru printf("CONNECT STARTBOB!\n"); LP_connectstartbob(ctx,LP_mypubsock,qp->srccoin,qp->destcoin,qprice,qp); return(qp); - } else printf("connect message from non-reserved (%llu)\n",(long long)qp->aliceid); + } + else + { + LP_failedmsg(qp->R.requestid,qp->R.quoteid,-1); + printf("connect message from non-reserved (%llu)\n",(long long)qp->aliceid); + } return(0); } struct LP_quoteinfo *LP_trades_gotconnected(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp,char *pairstr) { - char *retstr; int32_t changed; + char *retstr; int32_t changed; double val; //char str[65]; printf("alice %s received CONNECTED.(%llu)\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid); *newqp = *qp; qp = newqp; - if ( LP_trades_alicevalidate(ctx,qp) > 0. ) + if ( (val= LP_trades_alicevalidate(ctx,qp)) > 0. ) { printf("CONNECTED ALICE\n"); LP_aliceid(qp->tradeid,qp->aliceid,"connected",0,0); @@ -1056,7 +1078,7 @@ struct LP_quoteinfo *LP_trades_gotconnected(void *ctx,struct LP_quoteinfo *qp,st LP_mypriceset(&changed,qp->destcoin,qp->srccoin,0.); LP_alicequery_clear(); return(qp); - } + } else LP_failedmsg(qp->R.requestid,qp->R.quoteid,val); //printf("LP_trades_alicevalidate error\n"); return(0); } @@ -1289,13 +1311,13 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson, if ( Q.satoshis < Q.txfee ) return(1); 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; 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_tradecommand_log(argjson); qprice = (double)Q.destsatoshis / (Q.satoshis - Q.txfee); //jdouble(argjson,"price"); //printf("%s\n",jprint(argjson,0)); 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),qprice,LP_RTcount,LP_swapscount,G.netid); diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 3fd79029d..8fbea7bdf 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -235,6 +235,18 @@ void basilisk_dontforget_update(struct basilisk_swap *swap,struct basilisk_rawtx } else if ( rawtx == &swap->bobreclaim ) basilisk_dontforget(swap,&swap->bobreclaim,swap->bobpayment.I.locktime,triggertxid); + if ( IPC_ENDPOINT >= 0 ) + { + char fname[512],*fstr; long fsize; + sprintf(fname,"%s/SWAPS/%u-%u",GLOBAL_DBDIR,swap->I.req.requestid,swap->I.req.quoteid), OS_compatible_path(fname); + if ( rawtx != 0 ) + sprintf(fname+strlen(fname),".%s",rawtx->name); + if ( (fstr= OS_filestr(&fsize,fname)) != 0 ) + { + LP_queuecommand(0,fstr,IPC_ENDPOINT,-1,0); + free(fstr); + } + } } bits256 basilisk_swap_privbob_extract(char *symbol,bits256 spendtxid,int32_t vini,int32_t revflag) diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index 778c31161..774e77b05 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -467,10 +467,13 @@ char *LP_postprice_recv(cJSON *argjson) { if ( LP_price_sigcheck(juint(argjson,"timestamp"),jstr(argjson,"sig"),jstr(argjson,"pubsecp"),pubkey,base,rel,j64bits(argjson,"price64")) == 0 ) { - if ( (argstr= jprint(argjson,0)) != 0 ) + if ( IPC_ENDPOINT >= 0 ) { - LP_queuecommand(0,argstr,IPC_ENDPOINT,-1,0); - free(argstr); + if ( (argstr= jprint(argjson,0)) != 0 ) + { + LP_queuecommand(0,argstr,IPC_ENDPOINT,-1,0); + free(argstr); + } } //printf("call pricefeed update\n"); LP_pricefeedupdate(pubkey,base,rel,price,jstr(argjson,"utxocoin"),jint(argjson,"n"),jdouble(argjson,"bal")*SATOSHIDEN,jdouble(argjson,"min")*SATOSHIDEN,jdouble(argjson,"max")*SATOSHIDEN,jdouble(argjson,"credits")*SATOSHIDEN); @@ -703,7 +706,9 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ jadd(reqjson,"proof",LP_instantdex_txids(0,coin->smartaddr)); } msg = jprint(reqjson,1); - printf("QUERY.(%s)\n",msg); + //printf("QUERY.(%s)\n",msg); + if ( IPC_ENDPOINT >= 0 ) + LP_queuecommand(0,msg,IPC_ENDPOINT,-1,0); memset(&zero,0,sizeof(zero)); if ( bits256_nonz(qp->srchash) != 0 ) LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg)); diff --git a/iguana/exchanges/LP_stats.c b/iguana/exchanges/LP_stats.c index 538be3d90..21f6868a5 100644 --- a/iguana/exchanges/LP_stats.c +++ b/iguana/exchanges/LP_stats.c @@ -110,10 +110,14 @@ void LP_tradecommand_log(cJSON *argjson) } if ( logfp != 0 ) { - jsonstr = jprint(argjson,0); - fprintf(logfp,"%s\n",jsonstr); - free(jsonstr); - fflush(logfp); + if ( (jsonstr= jprint(argjson,0)) != 0 ) + { + if ( IPC_ENDPOINT >= 0 ) + LP_queuecommand(0,jsonstr,IPC_ENDPOINT,-1,0); + fprintf(logfp,"%s\n",jsonstr); + free(jsonstr); + fflush(logfp); + } } } diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 2fba88890..e5308c632 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -830,7 +830,7 @@ uint32_t LP_swapwait(uint32_t expiration,uint32_t requestid,uint32_t quoteid,int void LP_bobloop(void *_swap) { - uint8_t *data; char bobstr[65],alicestr[65]; int32_t maxlen,m,n; uint32_t expiration; struct basilisk_swap *swap = _swap; + uint8_t *data; char bobstr[65],alicestr[65]; int32_t maxlen,m,n,err=0; uint32_t expiration; struct basilisk_swap *swap = _swap; G.LP_pendingswaps++; printf("start swap iambob\n"); LP_etomicsymbol(bobstr,swap->I.bobtomic,swap->I.bobstr); @@ -841,13 +841,13 @@ void LP_bobloop(void *_swap) if ( swap != 0 ) { if ( LP_waitsend("pubkeys",120,swap->N.pair,swap,data,maxlen,LP_pubkeys_verify,LP_pubkeys_data) < 0 ) - printf("error waitsend pubkeys\n"); + err = -2000, printf("error waitsend pubkeys\n"); else if ( LP_waitsend("choosei",LP_SWAPSTEP_TIMEOUT,swap->N.pair,swap,data,maxlen,LP_choosei_verify,LP_choosei_data) < 0 ) - printf("error waitsend choosei\n"); + err = -2001, printf("error waitsend choosei\n"); else if ( LP_waitsend("mostprivs",LP_SWAPSTEP_TIMEOUT,swap->N.pair,swap,data,maxlen,LP_mostprivs_verify,LP_mostprivs_data) < 0 ) - printf("error waitsend mostprivs\n"); + err = -2002, printf("error waitsend mostprivs\n"); else if ( basilisk_bobscripts_set(swap,1,1) < 0 ) - printf("error bobscripts deposit\n"); + err = -2003, printf("error bobscripts deposit\n"); else { uint8_t error = 0; @@ -860,27 +860,24 @@ void LP_bobloop(void *_swap) if ( LP_waitfor(swap->N.pair,swap,LP_SWAPSTEP_TIMEOUT*10,LP_verify_otherfee) < 0 ) { error = 1; - printf("error waiting for alicefee\n"); + err = -2004, printf("error waiting for alicefee\n"); } - if ( error == 0 ) { if ( LP_swapdata_rawtxsend(swap->N.pair,swap,0x200,data,maxlen,&swap->bobdeposit,0x100,0) == 0 ) { error = 1; - printf("error sending bobdeposit\n"); + err = -2005, printf("error sending bobdeposit\n"); } } - if ( error == 0 && LP_waitfor(swap->N.pair,swap,1800,LP_verify_alicepayment) < 0 ) { error = 1; - printf("error waiting for alicepayment\n"); + err = -2006, printf("error waiting for alicepayment\n"); } - if (error == 0) { LP_swap_critical = (uint32_t)time(NULL); if ( basilisk_bobscripts_set(swap,0,1) < 0 ) - printf("error bobscripts payment\n"); + err = -2007, printf("error bobscripts payment\n"); else { m = swap->I.aliceconfirms; @@ -891,9 +888,8 @@ void LP_bobloop(void *_swap) sleep(10); } LP_swap_critical = (uint32_t)time(NULL); - if ( LP_swapdata_rawtxsend(swap->N.pair,swap,0x8000,data,maxlen,&swap->bobpayment,0x4000,0) == 0 ) { - printf("error sending bobpayment\n"); + err = -2008, printf("error sending bobpayment\n"); } //if ( LP_waitfor(swap->N.pair,swap,10,LP_verify_alicespend) < 0 ) // printf("error waiting for alicespend\n"); @@ -908,6 +904,8 @@ void LP_bobloop(void *_swap) } } else printf("swap timed out\n"); LP_swap_endcritical = (uint32_t)time(NULL); + if ( err < 0 ) + LP_failedmsg(swap->I.req.requestid,swap->I.req.quoteid,err); sleep(13); LP_pendswap_add(swap->I.expiration,swap->I.req.requestid,swap->I.req.quoteid); //swap->I.finished = LP_swapwait(swap->I.expiration,swap->I.req.requestid,swap->I.req.quoteid,LP_atomic_locktime(swap->I.bobstr,swap->I.alicestr)*3,swap->I.aliceconfirms == 0 ? 3 : 30); @@ -919,7 +917,7 @@ void LP_bobloop(void *_swap) void LP_aliceloop(void *_swap) { - uint8_t *data; char bobstr[65],alicestr[65]; int32_t maxlen,n,m; uint32_t expiration; struct basilisk_swap *swap = _swap; + uint8_t *data; char bobstr[65],alicestr[65]; int32_t maxlen,n,m,err=0; uint32_t expiration; struct basilisk_swap *swap = _swap; G.LP_pendingswaps++; LP_etomicsymbol(bobstr,swap->I.bobtomic,swap->I.bobstr); LP_etomicsymbol(alicestr,swap->I.alicetomic,swap->I.alicestr); @@ -930,21 +928,21 @@ void LP_aliceloop(void *_swap) { printf("start swap iamalice pair.%d\n",swap->N.pair); if ( LP_sendwait("pubkeys",120,swap->N.pair,swap,data,maxlen,LP_pubkeys_verify,LP_pubkeys_data) < 0 ) - printf("error LP_sendwait pubkeys\n"); + err = -1000, printf("error LP_sendwait pubkeys\n"); else if ( LP_sendwait("choosei",LP_SWAPSTEP_TIMEOUT,swap->N.pair,swap,data,maxlen,LP_choosei_verify,LP_choosei_data) < 0 ) - printf("error LP_sendwait choosei\n"); + err = -1001, printf("error LP_sendwait choosei\n"); else if ( LP_sendwait("mostprivs",LP_SWAPSTEP_TIMEOUT,swap->N.pair,swap,data,maxlen,LP_mostprivs_verify,LP_mostprivs_data) < 0 ) - printf("error LP_sendwait mostprivs\n"); + err = -1002, printf("error LP_sendwait mostprivs\n"); else if ( basilisk_alicetxs(swap->N.pair,swap,data,maxlen) != 0 ) - printf("basilisk_alicetxs error\n"); + err = -1003, printf("basilisk_alicetxs error\n"); else { //LP_swapsfp_update(&swap->I.req); LP_swap_critical = (uint32_t)time(NULL); if ( LP_swapdata_rawtxsend(swap->N.pair,swap,0x80,data,maxlen,&swap->myfee,0x40,0) == 0 ) - printf("error sending alicefee\n"); + err = -1004, printf("error sending alicefee\n"); else if ( LP_waitfor(swap->N.pair,swap,1800,LP_verify_bobdeposit) < 0 ) - printf("error waiting for bobdeposit\n"); + err = -1005, printf("error waiting for bobdeposit\n"); else { m = swap->I.bobconfirms; @@ -955,7 +953,7 @@ void LP_aliceloop(void *_swap) sleep(10); } if ( LP_swapdata_rawtxsend(swap->N.pair,swap,0x1000,data,maxlen,&swap->alicepayment,0x800,0) == 0 ) - printf("error sending alicepayment\n"); + err = -1006, printf("error sending alicepayment\n"); else { m = swap->I.aliceconfirms; @@ -968,7 +966,7 @@ void LP_aliceloop(void *_swap) //swap->sentflag = 1; LP_swap_critical = (uint32_t)time(NULL); if ( LP_waitfor(swap->N.pair,swap,1800,LP_verify_bobpayment) < 0 ) - printf("error waiting for bobpayment\n"); + err = -1007, printf("error waiting for bobpayment\n"); else { LP_swap_endcritical = (uint32_t)time(NULL); @@ -985,6 +983,8 @@ void LP_aliceloop(void *_swap) } } LP_swap_endcritical = (uint32_t)time(NULL); + if ( err < 0 ) + LP_failedmsg(swap->I.req.requestid,swap->I.req.quoteid,err); sleep(13); LP_pendswap_add(swap->I.expiration,swap->I.req.requestid,swap->I.req.quoteid); //swap->I.finished = LP_swapwait(swap->I.expiration,swap->I.req.requestid,swap->I.req.quoteid,LP_atomic_locktime(swap->I.bobstr,swap->I.alicestr)*3,swap->I.aliceconfirms == 0 ? 3 : 30);