From 47ec0d31a9e228fcb037b98ff998df73e0db9b25 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 20 Jun 2017 23:55:47 +0300 Subject: [PATCH] Test --- iguana/exchanges/LP_bitcoin.c | 29 +++++++++++++++++++- iguana/exchanges/LP_commands.c | 12 ++++----- iguana/exchanges/LP_forwarding.c | 8 +++--- iguana/exchanges/LP_include.h | 6 ++--- iguana/exchanges/LP_nativeDEX.c | 46 ++++++++++++++++---------------- iguana/exchanges/LP_network.c | 8 +++--- iguana/exchanges/LP_ordermatch.c | 30 ++++++++++----------- iguana/exchanges/LP_utxos.c | 8 +++--- iguana/exchanges/mm.c | 24 +---------------- iguana/exchanges/stats.c | 9 +++++-- 10 files changed, 95 insertions(+), 85 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 9c2492c7e..3185da0f0 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -2055,7 +2055,7 @@ int32_t bitcoin_addr2rmd160(uint8_t taddr,uint8_t *addrtypep,uint8_t rmd160[20], } for (i=0; i 0 ) + if ( LP_forward(ctx,myipaddr,pubsock,profitmargin,jbits256(argjson,"pubkey"),jprint(reqjson,1),1) > 0 ) retstr = clonestr("{\"result\":\"success\"}"); else retstr = clonestr("{\"error\":\"error forwarding\"}"); } else retstr = clonestr("{\"error\":\"cant recurse forwards\"}"); @@ -237,7 +237,7 @@ forwardhex(pubkey,hex)\n\ else if ( strcmp(method,"lookup") == 0 ) return(LP_lookup(jbits256(argjson,"client"))); else if ( strcmp(method,"forwardhex") == 0 ) - retstr = LP_forwardhex(pubsock,jbits256(argjson,"pubkey"),jstr(argjson,"hex")); + retstr = LP_forwardhex(ctx,pubsock,jbits256(argjson,"pubkey"),jstr(argjson,"hex")); else if ( strcmp(method,"psock") == 0 ) { if ( myipaddr == 0 || myipaddr[0] == 0 || strcmp(myipaddr,"127.0.0.1") == 0 ) diff --git a/iguana/exchanges/LP_forwarding.c b/iguana/exchanges/LP_forwarding.c index 505ae8a33..cb368afe2 100644 --- a/iguana/exchanges/LP_forwarding.c +++ b/iguana/exchanges/LP_forwarding.c @@ -190,7 +190,7 @@ cJSON *LP_dereference(cJSON *argjson,char *excludemethod) return(reqjson); } -char *LP_forwardhex(int32_t pubsock,bits256 pubkey,char *hexstr) +char *LP_forwardhex(void *ctx,int32_t pubsock,bits256 pubkey,char *hexstr) { struct LP_forwardinfo *ptr=0; uint8_t *data; int32_t datalen=0,sentbytes=0; char *retstr=0; cJSON *retjson=0,*argjson=0,*reqjson=0; if ( hexstr == 0 || hexstr[0] == 0 ) @@ -204,7 +204,7 @@ char *LP_forwardhex(int32_t pubsock,bits256 pubkey,char *hexstr) { if ( reqjson != 0 ) { - retstr = LP_command_process(LP_mypeer != 0 ? LP_mypeer->ipaddr : "127.0.0.1",LP_mypubsock,reqjson,0,0,LP_profitratio - 1.); + retstr = LP_command_process(ctx,LP_mypeer != 0 ? LP_mypeer->ipaddr : "127.0.0.1",LP_mypubsock,reqjson,0,0,LP_profitratio - 1.); //printf("LP_forwardhex.(%s) -> (%s)\n",jprint(reqjson,0),retstr!=0?retstr:""); if ( pubsock >= 0 ) LP_send(pubsock,jprint(reqjson,0),0); @@ -248,7 +248,7 @@ char *LP_forwardhex(int32_t pubsock,bits256 pubkey,char *hexstr) return(retstr); } -int32_t LP_forward(char *myipaddr,int32_t pubsock,double profitmargin,bits256 pubkey,char *jsonstr,int32_t freeflag) +int32_t LP_forward(void *ctx,char *myipaddr,int32_t pubsock,double profitmargin,bits256 pubkey,char *jsonstr,int32_t freeflag) { struct LP_forwardinfo *ptr; struct LP_peerinfo *peer,*tmp; char *hexstr,*retstr; int32_t len,retval = -1; cJSON *retjson,*reqjson,*argjson; if ( jsonstr == 0 || jsonstr[0] == 0 ) @@ -260,7 +260,7 @@ int32_t LP_forward(char *myipaddr,int32_t pubsock,double profitmargin,bits256 pu printf("GOT FORWARDED.(%s)\n",myipaddr); if ( (argjson= cJSON_Parse(jsonstr)) != 0 ) { - if ( (retstr= LP_command_process(myipaddr,pubsock,argjson,0,0,profitmargin)) != 0 ) + if ( (retstr= LP_command_process(ctx,myipaddr,pubsock,argjson,0,0,profitmargin)) != 0 ) free(retstr); free_json(argjson); } diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 79127306d..b00ef444f 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -228,14 +228,14 @@ uint32_t LP_swapdata_rawtxsend(int32_t pairsock,struct basilisk_swap *swap,uint3 //double LP_query(char *method,struct LP_quoteinfo *qp,char *base,char *rel,bits256 mypub); int32_t LP_rawtx_spendscript(struct basilisk_swap *swap,int32_t height,struct basilisk_rawtx *rawtx,int32_t v,uint8_t *recvbuf,int32_t recvlen,int32_t suppress_pubkeys); void LP_quotesinit(char *base,char *rel); -int32_t LP_forward(char *myipaddr,int32_t pubsock,double profitmargin,bits256 pubkey,char *jsonstr,int32_t freeflag); +int32_t LP_forward(void *ctx,char *myipaddr,int32_t pubsock,double profitmargin,bits256 pubkey,char *jsonstr,int32_t freeflag); int32_t LP_ismine(struct LP_utxoinfo *utxo); int32_t LP_isavailable(struct LP_utxoinfo *utxo); struct LP_peerinfo *LP_peerfind(uint32_t ipbits,uint16_t port); -char *LP_command_process(char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin); +char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin); void LP_availableset(struct LP_utxoinfo *utxo); int32_t LP_iseligible(uint64_t *valp,uint64_t *val2p,int32_t iambob,char *symbol,bits256 txid,int32_t vout,uint64_t satoshis,bits256 txid2,int32_t vout2,bits256 pubkey); -int32_t LP_pullsock_check(char **retstrp,char *myipaddr,int32_t pubsock,int32_t pullsock,double profitmargin); +int32_t LP_pullsock_check(void *ctx,char **retstrp,char *myipaddr,int32_t pubsock,int32_t pullsock,double profitmargin); uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 9dde8e1cd..a1dadda84 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -76,14 +76,14 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_ #include "LP_forwarding.c" #include "LP_commands.c" -char *LP_command_process(char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) +char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { char *retstr=0; if ( jobj(argjson,"result") != 0 || jobj(argjson,"error") != 0 ) return(0); - if ( LP_tradecommand(myipaddr,pubsock,argjson,data,datalen,profitmargin) <= 0 ) + if ( LP_tradecommand(ctx,myipaddr,pubsock,argjson,data,datalen,profitmargin) <= 0 ) { - if ( (retstr= stats_JSON(myipaddr,pubsock,profitmargin,argjson,"127.0.0.1",0)) != 0 ) + if ( (retstr= stats_JSON(ctx,myipaddr,pubsock,profitmargin,argjson,"127.0.0.1",0)) != 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 && @@ -93,7 +93,7 @@ char *LP_command_process(char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t * return(retstr); } -char *LP_process_message(char *typestr,char *myipaddr,int32_t pubsock,double profitmargin,void *ptr,int32_t recvlen,int32_t recvsock) +char *LP_process_message(void *ctx,char *typestr,char *myipaddr,int32_t pubsock,double profitmargin,void *ptr,int32_t recvlen,int32_t recvsock) { int32_t len,datalen=0; char *retstr=0,*jsonstr=0; cJSON *argjson,*reqjson; if ( (datalen= is_hexstr((char *)ptr,0)) > 0 ) @@ -112,7 +112,7 @@ char *LP_process_message(char *typestr,char *myipaddr,int32_t pubsock,double pro if ( jstr(argjson,"method") != 0 && strcmp(jstr(argjson,"method"),"forwardhex") == 0 ) { //printf("got forwardhex\n"); - if ( (retstr= LP_forwardhex(pubsock,jbits256(argjson,"pubkey"),jstr(argjson,"hex"))) != 0 ) + if ( (retstr= LP_forwardhex(ctx,pubsock,jbits256(argjson,"pubkey"),jstr(argjson,"hex"))) != 0 ) { } } @@ -125,7 +125,7 @@ char *LP_process_message(char *typestr,char *myipaddr,int32_t pubsock,double pro if ( pubsock >= 0 && (reqjson= LP_dereference(argjson,"publish")) != 0 ) LP_send(pubsock,jprint(reqjson,1),1); } - else if ( (retstr= LP_command_process(myipaddr,pubsock,argjson,&((uint8_t *)ptr)[len],recvlen - len,profitmargin)) != 0 ) + else if ( (retstr= LP_command_process(ctx,myipaddr,pubsock,argjson,&((uint8_t *)ptr)[len],recvlen - len,profitmargin)) != 0 ) { } portable_mutex_unlock(&LP_commandmutex); @@ -154,7 +154,7 @@ char *LP_process_message(char *typestr,char *myipaddr,int32_t pubsock,double pro return(retstr); } -int32_t LP_pullsock_check(char **retstrp,char *myipaddr,int32_t pubsock,int32_t pullsock,double profitmargin) +int32_t LP_pullsock_check(void *ctx,char **retstrp,char *myipaddr,int32_t pubsock,int32_t pullsock,double profitmargin) { void *ptr; int32_t recvlen=-1,nonz = 0; *retstrp = 0; @@ -163,13 +163,13 @@ int32_t LP_pullsock_check(char **retstrp,char *myipaddr,int32_t pubsock,int32_t while ( (recvlen= nn_recv(pullsock,&ptr,NN_MSG,0)) > 0 ) { nonz++; - *retstrp = LP_process_message("PULL",myipaddr,pubsock,profitmargin,ptr,recvlen,pullsock); + *retstrp = LP_process_message(ctx,"PULL",myipaddr,pubsock,profitmargin,ptr,recvlen,pullsock); } } return(nonz); } -int32_t LP_subsock_check(char *myipaddr,int32_t pubsock,int32_t sock,double profitmargin) +int32_t LP_subsock_check(void *ctx,char *myipaddr,int32_t pubsock,int32_t sock,double profitmargin) { int32_t recvlen,nonz = 0; void *ptr; char *retstr; if ( sock >= 0 ) @@ -177,7 +177,7 @@ int32_t LP_subsock_check(char *myipaddr,int32_t pubsock,int32_t sock,double prof while ( (recvlen= nn_recv(sock,&ptr,NN_MSG,0)) > 0 ) { nonz++; - if ( (retstr= LP_process_message("SUB",myipaddr,pubsock,profitmargin,ptr,recvlen,sock)) != 0 ) + if ( (retstr= LP_process_message(ctx,"SUB",myipaddr,pubsock,profitmargin,ptr,recvlen,sock)) != 0 ) free(retstr); } } @@ -205,10 +205,10 @@ void LP_utxo_spentcheck(int32_t pubsock,struct LP_utxoinfo *utxo,double profitma } } -void LP_myutxo_updates(int32_t pubsock,char *passphrase,double profitmargin) +void LP_myutxo_updates(void *ctx,int32_t pubsock,char *passphrase,double profitmargin) { //LP_utxopurge(0); not good to disrupt existing pointers - LP_privkey_updates(pubsock,passphrase,0); + LP_privkey_updates(ctx,pubsock,passphrase,0); } int32_t LP_peer_utxosquery(struct LP_peerinfo *mypeer,uint16_t myport,int32_t pubsock,struct LP_peerinfo *peer,uint32_t now,double profitmargin,int32_t interval) @@ -229,7 +229,7 @@ int32_t LP_peer_utxosquery(struct LP_peerinfo *mypeer,uint16_t myport,int32_t pu return(n); } -int32_t LP_mainloop_iter(char *myipaddr,struct LP_peerinfo *mypeer,int32_t pubsock,char *pushaddr,uint16_t pushport,int32_t pullsock,uint16_t myport,char *passphrase,double profitmargin) +int32_t LP_mainloop_iter(void *ctx,char *myipaddr,struct LP_peerinfo *mypeer,int32_t pubsock,char *pushaddr,uint16_t pushport,int32_t pullsock,uint16_t myport,char *passphrase,double profitmargin) { static uint32_t counter,lastforward,numpeers; struct LP_utxoinfo *utxo,*utmp; char *retstr,*origipaddr; struct LP_peerinfo *peer,*tmp; uint32_t now; int32_t nonz = 0,n=0,lastn=-1; @@ -265,12 +265,12 @@ int32_t LP_mainloop_iter(char *myipaddr,struct LP_peerinfo *mypeer,int32_t pubso LP_peer_pricesquery(peer->ipaddr,peer->port); peer->diduquery = now; } - nonz += LP_subsock_check(origipaddr,pubsock,peer->subsock,profitmargin); + nonz += LP_subsock_check(ctx,origipaddr,pubsock,peer->subsock,profitmargin); } //if ( LP_canbind == 0 ) printf("counter.%d canbind.%d forwarding\n",counter,LP_canbind); if ( (counter % 600) == 60 ) { - LP_myutxo_updates(pubsock,passphrase,profitmargin); + LP_myutxo_updates(ctx,pubsock,passphrase,profitmargin); if ( lastforward < now-3600 ) { LP_forwarding_register(LP_mypubkey,pushaddr,pushport,10); @@ -301,7 +301,7 @@ int32_t LP_mainloop_iter(char *myipaddr,struct LP_peerinfo *mypeer,int32_t pubso } } //if ( LP_canbind == 0 ) printf("counter.%d canbind.%d pullsock check\n",counter,LP_canbind); - nonz += LP_pullsock_check(&retstr,myipaddr,pubsock,pullsock,profitmargin); + nonz += LP_pullsock_check(ctx,&retstr,myipaddr,pubsock,pullsock,profitmargin); if ( retstr != 0 ) free(retstr); //if ( LP_canbind == 0 ) printf("counter.%d canbind.%d hellos\n",counter,LP_canbind); @@ -320,7 +320,7 @@ int32_t LP_mainloop_iter(char *myipaddr,struct LP_peerinfo *mypeer,int32_t pubso return(nonz); } -void LP_initcoins(int32_t pubsock,cJSON *coins,char *passphrase) +void LP_initcoins(void *ctx,int32_t pubsock,cJSON *coins,char *passphrase) { int32_t i,n; cJSON *item; for (i=0; i= 0 ) nn_close(pullsock); - pullsock = LP_initpublicaddr(&mypullport,pushaddr,myipaddr,mypullport,0); + pullsock = LP_initpublicaddr(ctx,&mypullport,pushaddr,myipaddr,mypullport,0); LP_deadman_switch = (uint32_t)time(NULL); LP_forwarding_register(LP_mypubkey,pushaddr,mypullport,100000); } diff --git a/iguana/exchanges/LP_network.c b/iguana/exchanges/LP_network.c index 29a4313f1..1d669009a 100644 --- a/iguana/exchanges/LP_network.c +++ b/iguana/exchanges/LP_network.c @@ -359,7 +359,7 @@ char *LP_psock(char *myipaddr,int32_t ispaired) */ -int32_t nn_tests(int32_t pullsock,char *pushaddr,int32_t nnother) +int32_t nn_tests(void *ctx,int32_t pullsock,char *pushaddr,int32_t nnother) { int32_t sock,n,m,timeout,retval = -1; char msg[512],*retstr; printf("nn_tests.(%s)\n",pushaddr); @@ -375,7 +375,7 @@ int32_t nn_tests(int32_t pullsock,char *pushaddr,int32_t nnother) sprintf(msg,"{\"method\":\"nn_tests\",\"ipaddr\":\"%s\"}",pushaddr); n = LP_send(sock,msg,0); sleep(3); - LP_pullsock_check(&retstr,"127.0.0.1",-1,pullsock,0.); + LP_pullsock_check(ctx,&retstr,"127.0.0.1",-1,pullsock,0.); sprintf(msg,"{\"method\":\"nn_tests2\",\"ipaddr\":\"%s\"}",pushaddr); m = LP_send(pullsock,msg,0); printf(">>>>>>>>>>>>>>>>>>>>>> sent %d+%d bytes -> pullsock.%d retstr.(%s)\n",n,m,pullsock,retstr!=0?retstr:""); @@ -390,7 +390,7 @@ int32_t nn_tests(int32_t pullsock,char *pushaddr,int32_t nnother) return(retval); } -int32_t LP_initpublicaddr(uint16_t *mypullportp,char *publicaddr,char *myipaddr,uint16_t mypullport,int32_t ispaired) +int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char *myipaddr,uint16_t mypullport,int32_t ispaired) { int32_t nntype,pullsock,timeout,maxsize; char bindaddr[128],connectaddr[128]; *mypullportp = mypullport; @@ -444,7 +444,7 @@ int32_t LP_initpublicaddr(uint16_t *mypullportp,char *publicaddr,char *myipaddr, nn_setsockopt(pullsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); //LP_send(pullsock,"hello init",0); } - if ( LP_canbind == 0 && ispaired == 0 && nn_tests(pullsock,publicaddr,NN_PUSH) < 0 ) + if ( LP_canbind == 0 && ispaired == 0 && nn_tests(ctx,pullsock,publicaddr,NN_PUSH) < 0 ) { printf("command socket didnt work\n"); exit(-1); diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index c67c105bb..8416d8ad9 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -176,7 +176,7 @@ char *LP_quotereceived(cJSON *argjson) } else return(clonestr("{\"error\":\"nullptr\"}")); } -char *LP_pricepings(char *myipaddr,int32_t pubsock,double profitmargin,char *base,char *rel,double price) +char *LP_pricepings(void *ctx,char *myipaddr,int32_t pubsock,double profitmargin,char *base,char *rel,double price) { bits256 zero; cJSON *reqjson = cJSON_CreateObject(); jaddbits256(reqjson,"pubkey",LP_mypubkey); @@ -194,7 +194,7 @@ char *LP_pricepings(char *myipaddr,int32_t pubsock,double profitmargin,char *bas jaddstr(reqjson,"method","forward"); jaddstr(reqjson,"method2","postprice"); memset(zero.bytes,0,sizeof(zero)); - LP_forward(myipaddr,pubsock,profitmargin,zero,jprint(reqjson,1),1); + LP_forward(ctx,myipaddr,pubsock,profitmargin,zero,jprint(reqjson,1),1); } return(clonestr("{\"result\":\"success\"}")); } @@ -294,7 +294,7 @@ int32_t LP_arrayfind(cJSON *array,bits256 txid,int32_t vout) return(-1); } -double LP_query(char *myipaddr,int32_t mypubsock,double profitmargin,char *method,struct LP_quoteinfo *qp) +double LP_query(void *ctx,char *myipaddr,int32_t mypubsock,double profitmargin,char *method,struct LP_quoteinfo *qp) { cJSON *reqjson; int32_t i,flag = 0; double price = 0.; struct LP_utxoinfo *utxo; if ( strcmp(method,"request") == 0 ) @@ -321,7 +321,7 @@ double LP_query(char *myipaddr,int32_t mypubsock,double profitmargin,char *metho jaddstr(reqjson,"method2",method); jaddstr(reqjson,"method","forward"); jaddbits256(reqjson,"pubkey",qp->srchash); - LP_forward(myipaddr,mypubsock,profitmargin,qp->srchash,jprint(reqjson,1),1); + LP_forward(ctx,myipaddr,mypubsock,profitmargin,qp->srchash,jprint(reqjson,1),1); } for (i=0; i<30; i++) { @@ -338,7 +338,7 @@ double LP_query(char *myipaddr,int32_t mypubsock,double profitmargin,char *metho return(price); } -int32_t LP_nanobind(char *pairstr,char *myipaddr) +int32_t LP_nanobind(void *ctx,char *pairstr,char *myipaddr) { int32_t i,timeout,r,pairsock = -1; uint16_t mypullport; char bindaddr[128]; if ( LP_canbind != 0 ) @@ -372,11 +372,11 @@ int32_t LP_nanobind(char *pairstr,char *myipaddr) } else printf("error binding to %s for %s\n",bindaddr,pairstr); } } - } else pairsock = LP_initpublicaddr(&mypullport,pairstr,myipaddr,0,0); + } else pairsock = LP_initpublicaddr(ctx,&mypullport,pairstr,myipaddr,0,0); return(pairsock); } -int32_t LP_connectstartbob(int32_t pubsock,struct LP_utxoinfo *utxo,cJSON *argjson,char *myipaddr,char *base,char *rel,double profitmargin,double price,struct LP_quoteinfo *qp) +int32_t LP_connectstartbob(void *ctx,int32_t pubsock,struct LP_utxoinfo *utxo,cJSON *argjson,char *myipaddr,char *base,char *rel,double profitmargin,double price,struct LP_quoteinfo *qp) { char pairstr[512]; cJSON *retjson; bits256 privkey; int32_t pair=-1,retval = -1,DEXselector = 0; struct basilisk_swap *swap; struct iguana_info *coin; printf("LP_connectstartbob.(%s) with.(%s)\n",myipaddr,jprint(argjson,0)); @@ -389,7 +389,7 @@ int32_t LP_connectstartbob(int32_t pubsock,struct LP_utxoinfo *utxo,cJSON *argjs privkey = LP_privkey(utxo->coinaddr,coin->taddr); if ( bits256_nonz(privkey) != 0 && qp->quotetime >= qp->timestamp-3 && qp->quotetime <= utxo->T.swappending && bits256_cmp(LP_mypubkey,qp->srchash) == 0 ) { - if ( (pair= LP_nanobind(pairstr,myipaddr)) >= 0 ) + if ( (pair= LP_nanobind(ctx,pairstr,myipaddr)) >= 0 ) { LP_requestinit(&qp->R,qp->srchash,qp->desthash,base,qp->satoshis,rel,qp->destsatoshis,qp->timestamp,qp->quotetime,DEXselector); swap = LP_swapinit(1,0,privkey,&qp->R,qp); @@ -409,7 +409,7 @@ int32_t LP_connectstartbob(int32_t pubsock,struct LP_utxoinfo *utxo,cJSON *argjs jdelete(retjson,"method"); jaddstr(retjson,"method2","connected"); jaddstr(retjson,"method","forward"); - LP_forward(myipaddr,pubsock,profitmargin,utxo->S.otherpubkey,jprint(retjson,1),1); + LP_forward(ctx,myipaddr,pubsock,profitmargin,utxo->S.otherpubkey,jprint(retjson,1),1); retval = 0; } else printf("error launching swaploop\n"); } else printf("couldnt bind to any port %s\n",pairstr); @@ -496,7 +496,7 @@ char *LP_connectedalice(cJSON *argjson) // alice } } -int32_t LP_tradecommand(char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) +int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { char *method; cJSON *retjson; double qprice,price,bid,ask; struct LP_utxoinfo *autxo,*butxo; int32_t retval = -1; struct LP_quoteinfo Q; if ( (method= jstr(argjson,"method")) != 0 && (strcmp(method,"request") == 0 ||strcmp(method,"connect") == 0) ) @@ -541,7 +541,7 @@ int32_t LP_tradecommand(char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *d jdelete(retjson,"method"); jaddstr(retjson,"method2","reserved"); jaddstr(retjson,"method","forward"); - LP_forward(myipaddr,pubsock,profitmargin,butxo->S.otherpubkey,jprint(retjson,1),1); + LP_forward(ctx,myipaddr,pubsock,profitmargin,butxo->S.otherpubkey,jprint(retjson,1),1); butxo->T.lasttime = (uint32_t)time(NULL); printf("set swappending.%u accept qprice %.8f, min %.8f\n",butxo->T.swappending,qprice,price); } else printf("warning swappending.%u swap.%p\n",butxo->T.swappending,butxo->S.swap); @@ -550,7 +550,7 @@ int32_t LP_tradecommand(char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *d { retval = 4; if ( butxo->T.swappending != 0 && butxo->S.swap == 0 ) - LP_connectstartbob(pubsock,butxo,argjson,myipaddr,Q.srccoin,Q.destcoin,profitmargin,qprice,&Q); + LP_connectstartbob(ctx,pubsock,butxo,argjson,myipaddr,Q.srccoin,Q.destcoin,profitmargin,qprice,&Q); else printf("pend.%u swap %p when connect came in (%s)\n",butxo->T.swappending,butxo->S.swap,jprint(argjson,0)); } } @@ -558,7 +558,7 @@ int32_t LP_tradecommand(char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *d return(retval); } -char *LP_autotrade(char *myipaddr,int32_t mypubsock,double profitmargin,char *base,char *rel,double maxprice,double volume,int32_t timeout) +char *LP_autotrade(void *ctx,char *myipaddr,int32_t mypubsock,double profitmargin,char *base,char *rel,double maxprice,double volume,int32_t timeout) { int64_t satoshis,destsatoshis,desttxfee,txfee,bestdestsatoshis=0; bits256 txid,pubkey; char *obookstr; cJSON *orderbook,*asks,*item,*bestitem=0; struct LP_utxoinfo *autxo,*butxo,*bestutxo = 0; int32_t i,vout,numasks,DEXselector=0; uint32_t expiration; double ordermatchprice,bestmetric,metric,bestprice=0.,vol,price; struct LP_quoteinfo Q; if ( maxprice <= 0. || volume <= 0. || LP_priceinfofind(base) == 0 || LP_priceinfofind(rel) == 0 ) @@ -636,13 +636,13 @@ char *LP_autotrade(char *myipaddr,int32_t mypubsock,double profitmargin,char *ba return(clonestr("{\"error\":\"cant set ordermatch quote\"}")); if ( LP_quotedestinfo(&Q,autxo->payment.txid,autxo->payment.vout,autxo->fee.txid,autxo->fee.vout,LP_mypubkey,autxo->coinaddr) < 0 ) return(clonestr("{\"error\":\"cant set ordermatch quote info\"}")); - price = LP_query(myipaddr,mypubsock,profitmargin,"request",&Q); + price = LP_query(ctx,myipaddr,mypubsock,profitmargin,"request",&Q); bestitem = LP_quotejson(&Q); if ( price > SMALLVAL ) { if ( price <= maxprice ) { - price = LP_query(myipaddr,mypubsock,profitmargin,"connect",&Q); + price = LP_query(ctx,myipaddr,mypubsock,profitmargin,"connect",&Q); LP_requestinit(&Q.R,Q.srchash,Q.desthash,base,Q.satoshis,Q.destcoin,Q.destsatoshis,Q.timestamp,Q.quotetime,DEXselector); expiration = (uint32_t)time(NULL) + timeout; while ( time(NULL) < expiration ) diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 80f6de979..66f239e59 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -722,7 +722,7 @@ uint64_t LP_privkey_init(int32_t mypubsock,struct iguana_info *coin,bits256 mypr return(total); } -bits256 LP_privkeycalc(uint8_t *pubkey33,bits256 *pubkeyp,struct iguana_info *coin,char *passphrase,char *wifstr) +bits256 LP_privkeycalc(void *ctx,uint8_t *pubkey33,bits256 *pubkeyp,struct iguana_info *coin,char *passphrase,char *wifstr) { static uint32_t counter; bits256 privkey,userpub,userpass; char tmpstr[128]; cJSON *retjson; uint8_t tmptype,rmd160[20]; @@ -733,7 +733,7 @@ bits256 LP_privkeycalc(uint8_t *pubkey33,bits256 *pubkeyp,struct iguana_info *co privkey = iguana_wif2privkey(wifstr); //printf("WIF.(%s) -> %s\n",wifstr,bits256_str(str,privkey)); } - iguana_priv2pub(pubkey33,coin->smartaddr,privkey,coin->pubtype); + iguana_priv2pub(ctx,pubkey33,coin->smartaddr,privkey,coin->taddr,coin->pubtype); if ( coin->counter == 0 ) { coin->counter++; @@ -757,7 +757,7 @@ bits256 LP_privkeycalc(uint8_t *pubkey33,bits256 *pubkeyp,struct iguana_info *co return(privkey); } -void LP_privkey_updates(int32_t pubsock,char *passphrase,int32_t initonly) +void LP_privkey_updates(void *ctx,int32_t pubsock,char *passphrase,int32_t initonly) { int32_t i; struct iguana_info *coin; bits256 pubkey,privkey; uint8_t pubkey33[33]; memset(privkey.bytes,0,sizeof(privkey)); @@ -768,7 +768,7 @@ void LP_privkey_updates(int32_t pubsock,char *passphrase,int32_t initonly) if ( (coin= LP_coinfind(LP_coins[i].symbol)) != 0 ) { if ( bits256_nonz(privkey) == 0 || coin->smartaddr[0] == 0 ) - privkey = LP_privkeycalc(pubkey33,&pubkey,coin,passphrase,""); + privkey = LP_privkeycalc(ctx,pubkey33,&pubkey,coin,passphrase,""); if ( coin->inactive == 0 && initonly == 0 ) LP_privkey_init(pubsock,coin,privkey,pubkey,pubkey33); } diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index a80982185..1babc9d76 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -24,7 +24,7 @@ #include #include "OS_portable.h" #define MAX(a,b) ((a) > (b) ? (a) : (b)) -char *stats_JSON(char *myipaddr,int32_t pubsock,double profitmargin,cJSON *argjson,char *remoteaddr,uint16_t port); +char *stats_JSON(void *ctx,char *myipaddr,int32_t pubsock,double profitmargin,cJSON *argjson,char *remoteaddr,uint16_t port); #include "stats.c" void LP_priceupdate(char *base,char *rel,double price,double avebid,double aveask,double highbid,double lowask,double PAXPRICES[32]); @@ -220,28 +220,6 @@ bits256 iguana_wif2privkey(char *wifstr) return(privkey); } -void iguana_priv2pub(uint8_t *pubkey33,char *coinaddr,bits256 privkey,uint8_t addrtype) -{ - char privstr[65],url[512],postdata[1024],*retstr,*pubstr,*addr; cJSON *retjson; - memset(pubkey33,0,33); - coinaddr[0] = 0; - bits256_str(privstr,privkey); - sprintf(url,"%s/?",IGUANA_URL); - sprintf(postdata,"{\"agent\":\"SuperNET\",\"method\":\"priv2pub\",\"privkey\":\"%s\",\"addrtype\":%u}",privstr,addrtype); - if ( (retstr= bitcoind_RPC(0,"SuperNET",url,0,"priv2pub",postdata,0)) != 0 ) - { - if ( (retjson= cJSON_Parse(retstr)) != 0 ) - { - if ( (pubstr= jstr(retjson,"secp256k1")) != 0 && strlen(pubstr) == 66 ) - decode_hex(pubkey33,33,pubstr); - if ( (addr= jstr(retjson,"result")) != 0 && strlen(addr) < 64 ) - strcpy(coinaddr,addr); - free_json(retjson); - } - free(retstr); - } -} - double bittrex_balance(char *base,char *coinaddr) { char *retstr; cJSON *retjson; double balance = 0.; diff --git a/iguana/exchanges/stats.c b/iguana/exchanges/stats.c index b0b4b11d8..64e4f985e 100644 --- a/iguana/exchanges/stats.c +++ b/iguana/exchanges/stats.c @@ -302,10 +302,15 @@ cJSON *SuperNET_urlconv(char *value,int32_t bufsize,char *urlstr) return(json); } +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; double profitmargin = 0.; char *myipaddr="127.0.0.1",symbol[64],buf[4096],*userpass=0,urlmethod[16],*data,url[8192],furl[8192],*retstr,*filestr,*token = 0; int32_t i,j,n,num=0; //printf("rpcparse.(%s)\n",urlstr); + if ( ctx == 0 ) + ctx = bitcoin_ctx(); for (i=0; i