From 6f9176a724c746b3bae55512c74d7ae41df1d42e Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 10:20:04 +0300 Subject: [PATCH 001/339] Test --- iguana/exchanges/LP_include.h | 3 +- iguana/exchanges/LP_nativeDEX.c | 4 +- iguana/exchanges/LP_rpc.c | 312 +++++++++----------------------- 3 files changed, 88 insertions(+), 231 deletions(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 965b2a7ae..1c455f17e 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -174,7 +174,7 @@ struct iguana_info uint64_t txfee,estimatedfee; int32_t longestchain; uint8_t pubtype,p2shtype,isPoS,wiftype; - char symbol[16],changeaddr[64]; + char symbol[16],changeaddr[64],userpass[1024],serverport[128]; }; struct basilisk_swap @@ -465,5 +465,6 @@ enum opcodetype void basilisk_dontforget_update(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx); uint32_t basilisk_requestid(struct basilisk_request *rp); uint32_t basilisk_quoteid(struct basilisk_request *rp); +char *bitcoind_passthru(char *coinstr,char *serverport,char *userpass,char *method,char *params); #endif diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 0c2ecb51d..417968d91 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -675,7 +675,7 @@ void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin) } } -#ifdef __APPLE__ +/*#ifdef __APPLE__ int32_t nn_bind() { return(-1); } int32_t nn_close() { return(-1); } int32_t nn_connect() { return(-1); } @@ -686,4 +686,4 @@ int32_t nn_send() { return(-1); } int32_t nn_setsockopt() { return(-1); } int32_t nn_socket() { return(-1); } -#endif +#endif*/ diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index be1108238..ef731e08e 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -34,6 +34,24 @@ cJSON *basilisk_nullretjson(cJSON *retjson) return(retjson); } +char *dex_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_t skip) +{ + return(0); +} + +bits256 LP_privkey(char *coinaddr) +{ + bits256 privkey; + return(privkey); +} + +bits256 LP_pubkey(bits256 privkey) +{ + bits256 pubkey; + pubkey = curve25519(privkey,curve25519_basepoint9()); + return(pubkey); +} + void LP_unspentslock(char *symbol,cJSON *vins) { @@ -54,44 +72,20 @@ uint64_t LP_txfee(char *symbol) return(10000); } -char *dpow_validateaddress(struct supernet_info *myinfo,struct iguana_info *coin,char *address) +char *LP_validateaddress(char *symbol,char *address) { - char buf[128],*retstr=0; - if ( coin->FULLNODE < 0 ) - { - sprintf(buf,"\"%s\"",address); - retstr = bitcoind_passthru(coin->symbol,coin->chain->serverport,coin->chain->userpass,"validateaddress",buf); - usleep(10000); - } - else if ( coin->FULLNODE > 0 || coin->VALIDATENODE > 0 ) - { - retstr = bitcoinrpc_validateaddress(myinfo,coin,0,0,address); - } - else - { - return(0); - } + char buf[128],*retstr=0; struct iguana_info *coin = LP_coinfind(symbol); + sprintf(buf,"\"%s\"",address); + retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"validateaddress",buf); + usleep(10000); return(retstr); } -cJSON *dpow_gettxout(struct supernet_info *myinfo,struct iguana_info *coin,bits256 txid,int32_t vout) +cJSON *LP_gettxout(char *symbol,bits256 txid,int32_t vout) { - char buf[128],str[65],*retstr=0; cJSON *json = 0; + char buf[128],str[65],*retstr=0; cJSON *json=0; struct iguana_info *coin = LP_coinfind(symbol); sprintf(buf,"\"%s\", %d",bits256_str(str,txid),vout); - if ( coin->FULLNODE < 0 ) - { - retstr = bitcoind_passthru(coin->symbol,coin->chain->serverport,coin->chain->userpass,"gettxout",buf); - usleep(10000); - } - else if ( coin->FULLNODE > 0 || coin->VALIDATENODE > 0 ) - { - printf("need to test following call\n"); - retstr = bitcoinrpc_gettxout(myinfo,coin,0,buf,txid,1,0); // untested - } - else - { - return(0); - } + retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"gettxout",buf); if ( retstr != 0 ) { json = cJSON_Parse(retstr); @@ -101,48 +95,24 @@ cJSON *dpow_gettxout(struct supernet_info *myinfo,struct iguana_info *coin,bits2 return(json); } -char *dpow_decoderawtransaction(struct supernet_info *myinfo,struct iguana_info *coin,char *rawtx) +char *LP_decoderawtransaction(char *symbol,char *rawtx) { - char *retstr,*paramstr; cJSON *array; - if ( coin->FULLNODE < 0 ) - { - array = cJSON_CreateArray(); - jaddistr(array,rawtx); - paramstr = jprint(array,1); - retstr = bitcoind_passthru(coin->symbol,coin->chain->serverport,coin->chain->userpass,"decoderawtransaction",paramstr); - //printf("%s decoderawtransaction.(%s) <- (%s)\n",coin->symbol,retstr,paramstr); - free(paramstr); - usleep(10000); - } - else if ( coin->FULLNODE > 0 || coin->VALIDATENODE > 0 ) - { - retstr = bitcoinrpc_decoderawtransaction(myinfo,coin,0,0,rawtx,1); - } - else - { - return(0); - } + char *retstr,*paramstr; cJSON *array; struct iguana_info *coin = LP_coinfind(symbol); + array = cJSON_CreateArray(); + jaddistr(array,rawtx); + paramstr = jprint(array,1); + retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"decoderawtransaction",paramstr); + //printf("%s decoderawtransaction.(%s) <- (%s)\n",coin->symbol,retstr,paramstr); + free(paramstr); return(retstr); } -cJSON *dpow_gettransaction(struct supernet_info *myinfo,struct iguana_info *coin,bits256 txid) +cJSON *LP_gettransaction(char *symbol,bits256 txid) { - char buf[128],str[65],*retstr=0; cJSON *json = 0; - if ( coin->FULLNODE < 0 ) - { - sprintf(buf,"[\"%s\", 1]",bits256_str(str,txid)); - if ( (retstr= bitcoind_passthru(coin->symbol,coin->chain->serverport,coin->chain->userpass,"getrawtransaction",buf)) != 0 ) - { - } - usleep(10000); - } - else if ( coin->FULLNODE > 0 || coin->VALIDATENODE > 0 ) - { - retstr = bitcoinrpc_getrawtransaction(myinfo,coin,0,0,txid,1); - } - else + char buf[128],str[65],*retstr=0; cJSON *json = 0; struct iguana_info *coin = LP_coinfind(symbol); + sprintf(buf,"[\"%s\", 1]",bits256_str(str,txid)); + if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"getrawtransaction",buf)) != 0 ) { - return(0); } if ( retstr != 0 ) { @@ -152,140 +122,64 @@ cJSON *dpow_gettransaction(struct supernet_info *myinfo,struct iguana_info *coin return(json); } -cJSON *dpow_listunspent(struct supernet_info *myinfo,struct iguana_info *coin,char *coinaddr) +cJSON *LP_listunspent(char *symbol,char *coinaddr) { - char buf[128],*retstr; cJSON *array,*json = 0; - if ( coin->FULLNODE < 0 ) + char buf[128],*retstr; cJSON *json = 0; struct iguana_info *coin = LP_coinfind(symbol); + sprintf(buf,"0, 99999999, [\"%s\"]",coinaddr); + if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"listunspent",buf)) != 0 ) { - sprintf(buf,"0, 99999999, [\"%s\"]",coinaddr); - if ( (retstr= bitcoind_passthru(coin->symbol,coin->chain->serverport,coin->chain->userpass,"listunspent",buf)) != 0 ) - { - json = cJSON_Parse(retstr); - //printf("%s (%s) listunspent.(%s)\n",coin->symbol,buf,retstr); - free(retstr); - } else printf("%s null retstr from (%s)n",coin->symbol,buf); - } - else if ( coin->FULLNODE > 0 || coin->VALIDATENODE > 0 ) - { - array = cJSON_CreateArray(); - jaddistr(array,coinaddr); - json = iguana_listunspents(myinfo,coin,array,1,coin->longestchain,""); - free_json(array); - } - else - { - return(0); - } + json = cJSON_Parse(retstr); + //printf("%s (%s) listunspent.(%s)\n",coin->symbol,buf,retstr); + free(retstr); + } else printf("%s null retstr from (%s)n",coin->symbol,buf); return(json); } -cJSON *dpow_listtransactions(struct supernet_info *myinfo,struct iguana_info *coin,char *coinaddr,int32_t count,int32_t skip) +cJSON *LP_listtransactions(char *symbol,char *coinaddr,int32_t count,int32_t skip) { - char buf[128],*retstr; cJSON *json = 0; - if ( coin->FULLNODE < 0 ) + char buf[128],*retstr; cJSON *json = 0; struct iguana_info *coin = LP_coinfind(symbol); + if ( count == 0 ) + count = 100; + sprintf(buf,"[\"%s\", %d, %d, true]",coinaddr,count,skip); + if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"listtransactions",buf)) != 0 ) { - if ( count == 0 ) - count = 100; - sprintf(buf,"[\"%s\", %d, %d, true]",coinaddr,count,skip); - if ( (retstr= bitcoind_passthru(coin->symbol,coin->chain->serverport,coin->chain->userpass,"listtransactions",buf)) != 0 ) - { - //printf("LIST.(%s)\n",retstr); - json = cJSON_Parse(retstr); - free(retstr); - return(json); - } else printf("%s null retstr from (%s)n",coin->symbol,buf); - } + //printf("LIST.(%s)\n",retstr); + json = cJSON_Parse(retstr); + free(retstr); + return(json); + } else printf("%s null retstr from (%s)n",coin->symbol,buf); return(0); } -char *dpow_signrawtransaction(struct supernet_info *myinfo,struct iguana_info *coin,char *rawtx,cJSON *vins) +char *LP_signrawtransaction(char *symbol,char *rawtx,cJSON *vins) { - cJSON *array,*privkeys,*item; char *wifstr,*str,*paramstr,*retstr; uint8_t script[256]; int32_t i,n,len,hashtype; struct vin_info V; struct iguana_waddress *waddr; struct iguana_waccount *wacct; - if ( coin->FULLNODE < 0 ) - { - array = cJSON_CreateArray(); - jaddistr(array,rawtx); - jaddi(array,jduplicate(vins)); - paramstr = jprint(array,1); - //printf("signrawtransaction\n"); - retstr = bitcoind_passthru(coin->symbol,coin->chain->serverport,coin->chain->userpass,"signrawtransaction",paramstr); - //printf("%s signrawtransaction.(%s) params.(%s)\n",coin->symbol,retstr,paramstr); - free(paramstr); - usleep(10000); - return(retstr); - } - else if ( coin->FULLNODE > 0 || coin->VALIDATENODE > 0 ) - { - privkeys = cJSON_CreateArray(); - if ( (n= cJSON_GetArraySize(vins)) > 0 ) - { - for (i=0; i 0 && strlen(str) < sizeof(script)*2 ) - { - len = (int32_t)strlen(str) >> 1; - decode_hex(script,len,str); - V.spendlen = len; - memcpy(V.spendscript,script,len); - if ( (hashtype= _iguana_calcrmd160(coin,&V)) >= 0 && V.coinaddr[0] != 0 ) - { - if ( (waddr= iguana_waddresssearch(myinfo,&wacct,V.coinaddr)) != 0 ) - { - if ( bits256_nonz(waddr->privkey) != 0 ) - { - if ( bitcoin_priv2wif(waddr->wifstr,waddr->privkey,coin->chain->wiftype) > 0 ) - { - wifstr = waddr->wifstr; - } - } - } - } - } - jaddistr(privkeys,wifstr); - } - } - retstr = bitcoinrpc_signrawtransaction(myinfo,coin,0,0,rawtx,vins,privkeys,"ALL"); - printf("call sign.(%s) vins.(%s) privs.(%s) -> (%s)\n",rawtx,jprint(vins,0),jprint(privkeys,0),retstr); - free_json(privkeys); - return(retstr); - } - else - { - return(0); - } + cJSON *array; char *paramstr,*retstr; struct iguana_info *coin = LP_coinfind(symbol); + array = cJSON_CreateArray(); + jaddistr(array,rawtx); + jaddi(array,jduplicate(vins)); + paramstr = jprint(array,1); + //printf("signrawtransaction\n"); + retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"signrawtransaction",paramstr); + //printf("%s signrawtransaction.(%s) params.(%s)\n",coin->symbol,retstr,paramstr); + free(paramstr); + return(retstr); } -char *dpow_sendrawtransaction(struct supernet_info *myinfo,struct iguana_info *coin,char *signedtx) +char *LP_sendrawtransaction(char *symbol,char *signedtx) { - bits256 txid; cJSON *json,*array; char *paramstr,*retstr; - if ( coin->FULLNODE < 0 ) - { - array = cJSON_CreateArray(); - jaddistr(array,signedtx); - paramstr = jprint(array,1); - retstr = bitcoind_passthru(coin->symbol,coin->chain->serverport,coin->chain->userpass,"sendrawtransaction",paramstr); - printf(">>>>>>>>>>> %s dpow_sendrawtransaction.(%s) -> (%s)\n",coin->symbol,paramstr,retstr); - free(paramstr); - return(retstr); - } - else if ( coin->FULLNODE > 0 || coin->VALIDATENODE > 0 ) - { - txid = iguana_sendrawtransaction(myinfo,coin,signedtx); - json = cJSON_CreateObject(); - jaddbits256(json,"result",txid); - return(jprint(json,1)); - } - else - { - return(0); - } + cJSON *array; char *paramstr,*retstr; struct iguana_info *coin = LP_coinfind(symbol); + array = cJSON_CreateArray(); + jaddistr(array,signedtx); + paramstr = jprint(array,1); + retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"sendrawtransaction",paramstr); + printf(">>>>>>>>>>> %s dpow_sendrawtransaction.(%s) -> (%s)\n",coin->symbol,paramstr,retstr); + free(paramstr); + return(retstr); } char *LP_importaddress(char *symbol,char *address) { - char buf[1024],*retstr; cJSON *validatejson; int32_t isvalid=0,doneflag = 0; + char buf[1024],*retstr; cJSON *validatejson; int32_t isvalid=0,doneflag = 0; struct iguana_info *coin = LP_coinfind(symbol); if ( (retstr= LP_validateaddress(symbol,address)) != 0 ) { if ( (validatejson= cJSON_Parse(retstr)) != 0 ) @@ -302,27 +196,12 @@ char *LP_importaddress(char *symbol,char *address) } if ( isvalid == 0 ) return(clonestr("{\"isvalid\":false}")); - update_alladdresses(myinfo,coin,address); if ( doneflag != 0 ) return(0); // success - if ( coin->FULLNODE < 0 ) - { - sprintf(buf,"[\"%s\", \"%s\", false]",address,address); - retstr = bitcoind_passthru(coin->symbol,coin->chain->serverport,coin->chain->userpass,"importaddress",buf); - printf("%s importaddress.(%s) -> (%s)\n",coin->symbol,address,retstr); - return(retstr); - } - else return(0); -} - -char *LP_importaddress(char *symbol,char *coinaddr) -{ - return(0); -} - -char *LP_sendrawtransaction(char *symbol,char *signedtx) -{ - return(0); + sprintf(buf,"[\"%s\", \"%s\", false]",address,address); + retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"importaddress",buf); + printf("%s importaddress.(%s) -> (%s)\n",coin->symbol,address,retstr); + return(retstr); } char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON *vins,char *rawtxbytes,cJSON *privkeys,struct vin_info *V) @@ -330,33 +209,10 @@ char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON * return(0); } -cJSON *LP_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_t skip) -{ - return(0); -} - -char *dex_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_t skip) -{ - return(0); -} - -bits256 LP_privkey(char *coinaddr) -{ - bits256 privkey; - return(privkey); -} - -bits256 LP_pubkey(bits256 privkey) -{ - bits256 pubkey; - pubkey = curve25519(privkey,curve25519_basepoint9()); - return(pubkey); -} - cJSON *LP_swapgettxout(char *symbol,bits256 trigger,int32_t vout) { cJSON *retjson=0; //char *retstr; struct iguana_info *coin; - /*if ( ((coin= iguana_coinfind(symbol)) == 0 || coin->FULLNODE == 0) && iguana_isnotarychain(symbol) >= 0 ) + /*if ( ((coin= LP_coinfind(symbol)) == 0 || coin->FULLNODE == 0) && iguana_isnotarychain(symbol) >= 0 ) { if ( (retstr= dex_gettxout(0,0,0,trigger,symbol,vout)) != 0 ) { @@ -385,7 +241,7 @@ uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) cJSON *LP_swapgettx(char *symbol,bits256 txid) { cJSON *retjson=0; //char *retstr; struct iguana_info *coin; - /*if ( ((coin= iguana_coinfind(symbol)) == 0 || coin->FULLNODE == 0) && iguana_isnotarychain(symbol) >= 0 ) + /*if ( ((coin= LP_coinfind(symbol)) == 0 || coin->FULLNODE == 0) && iguana_isnotarychain(symbol) >= 0 ) { if ( (retstr= dex_gettransaction(0,0,0,txid,symbol)) != 0 ) { From 7eddb039bc50e968814b4d2f23524feb9237e908 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 10:22:20 +0300 Subject: [PATCH 002/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- iguana/exchanges/mm.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 417968d91..9243b0649 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -663,7 +663,7 @@ void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin) { len = (int32_t)strlen((char *)ptr) + 1; portable_mutex_lock(&LP_commandmutex); - LP_command(mypeer,mypub,argjson,&ptr[len],recvsize - len,profitmargin); + LP_command(mypeer,mypub,argjson,&((uint8_t *)ptr)[len],recvsize - len,profitmargin); portable_mutex_unlock(&LP_commandmutex); free_json(argjson); } diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 04f20f410..ca30cf3d2 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -35,6 +35,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port); #include "../../crypto777/nanosrc/pipeline.h" #include "../../crypto777/nanosrc/reqrep.h" #include "../../crypto777/nanosrc/tcp.h" +#include "../../crypto777/nanosrc/pair.h" #else #include "/usr/local/include/nanomsg/nn.h" #include "/usr/local/include/nanomsg/bus.h" @@ -42,6 +43,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port); #include "/usr/local/include/nanomsg/pipeline.h" #include "/usr/local/include/nanomsg/reqrep.h" #include "/usr/local/include/nanomsg/tcp.h" +#include "/usr/local/include/nanomsg/pair.h" #endif char DEX_baseaddr[64],DEX_reladdr[64]; From 9439511dec69bb40b00d535ab96d64c65489a0e9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:02:24 +0300 Subject: [PATCH 003/339] Test --- iguana/exchanges/LP_bitcoin.c | 38 ++++ iguana/exchanges/LP_include.h | 3 +- iguana/exchanges/LP_nativeDEX.c | 242 ++++++++++++++++---- iguana/exchanges/LP_remember.c | 24 +- iguana/exchanges/LP_rpc.c | 358 +++++++++--------------------- iguana/exchanges/LP_transaction.c | 115 ++++++++-- 6 files changed, 440 insertions(+), 340 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 078bc1d3f..79c78dc9c 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -18,6 +18,29 @@ // marketmaker // +struct { bits256 privkey; uint8_t rmd160[20]; } LP_privkeys[100]; int32_t LP_numprivkeys; + +bits256 LP_privkeyfind(uint8_t rmd160[20]) +{ + int32_t i; static bits256 zero; + for (i=0; isymbol,symbol); - return(coin); - } -} - -void tradebot_swap_balancingtrade(struct basilisk_swap *swap,int32_t iambob) -{ - -} - -void tradebot_pendingadd(cJSON *tradejson,char *base,double basevolume,char *rel,double relvolume) -{ - // add to trades -} - -char GLOBAL_DBDIR[] = "."; - -#include "LP_secp.c" -#include "LP_rpc.c" -#include "LP_bitcoin.c" -#include "LP_transaction.c" -#include "LP_remember.c" -#include "LP_statemachine.c" -#include "LP_swap.c" -#include "LP_commands.c" - void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin) { char *myipaddr=0,*retstr; long filesize,n; int32_t len,timeout,maxsize,recvsize,nonz,i,lastn,pullsock=-1,pubsock=-1; struct LP_peerinfo *peer,*tmp,*mypeer=0; char pushaddr[128],subaddr[128]; void *ptr; cJSON *argjson; diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 547f8ab9c..d8951c9a5 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -169,7 +169,7 @@ void basilisk_dontforget_userdata(char *userdataname,FILE *fp,uint8_t *script,in void basilisk_dontforget(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx,int32_t locktime,bits256 triggertxid) { - char zeroes[32],fname[512],str[65],coinaddr[64],secretAmstr[41],secretAm256str[65],secretBnstr[41],secretBn256str[65],*tmp; FILE *fp; int32_t i,len; uint8_t redeemscript[256],script[256]; + char zeroes[32],fname[512],str[65],coinaddr[64],secretAmstr[41],secretAm256str[65],secretBnstr[41],secretBn256str[65]; FILE *fp; int32_t i,len; uint8_t redeemscript[256],script[256]; sprintf(fname,"%s/SWAPS/%u-%u.%s",GLOBAL_DBDIR,swap->I.req.requestid,swap->I.req.quoteid,rawtx->name), OS_compatible_path(fname); coinaddr[0] = secretAmstr[0] = secretAm256str[0] = secretBnstr[0] = secretBn256str[0] = 0; memset(zeroes,0,sizeof(zeroes)); @@ -187,8 +187,7 @@ void basilisk_dontforget(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx basilisk_swap_coinaddr(swap,&swap->bobcoin,coinaddr,rawtx->txbytes,rawtx->I.datalen); if ( coinaddr[0] != 0 ) { - if ( (tmp= LP_importaddress(swap->bobcoin.symbol,coinaddr)) != 0 ) - free(tmp); + LP_importaddress(swap->bobcoin.symbol,coinaddr); if ( rawtx == &swap->bobdeposit ) safecopy(swap->Bdeposit,coinaddr,sizeof(swap->Bdeposit)); else safecopy(swap->Bpayment,coinaddr,sizeof(swap->Bpayment)); @@ -210,8 +209,7 @@ void basilisk_dontforget(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx if ( bits256_nonz(swap->I.pubAm) != 0 && bits256_nonz(swap->I.pubBn) != 0 ) { basilisk_alicescript(redeemscript,&len,script,0,coinaddr,swap->alicecoin.p2shtype,swap->I.pubAm,swap->I.pubBn); - if ( (tmp= LP_importaddress(swap->alicecoin.symbol,coinaddr)) != 0 ) - free(tmp); + LP_importaddress(swap->alicecoin.symbol,coinaddr); fprintf(fp,",\"Apayment\":\"%s\"",coinaddr); } /*basilisk_dontforget_userdata("Aclaim",fp,swap->I.userdata_aliceclaim,swap->I.userdata_aliceclaimlen); @@ -658,7 +656,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti safecopy(alicecoin,symbol,sizeof(alicecoin)); if ( finishedflag == 0 ) { - if ( (sentobj= LP_swapgettx(symbol,txid)) == 0 ) + if ( (sentobj= LP_gettx(symbol,txid)) == 0 ) { //printf("%s %s ready to broadcast\n",symbol,bits256_str(str2,txid)); } @@ -722,7 +720,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti printf("txbytes.%p Apayment.%s\n",txbytes[BASILISK_ALICEPAYMENT],bits256_str(str,txids[BASILISK_ALICEPAYMENT])); if ( txbytes[BASILISK_ALICEPAYMENT] != 0 ) sentflags[BASILISK_ALICEPAYMENT] = 1; - else if ( (sentobj= LP_swapgettx(alicecoin,txids[BASILISK_ALICEPAYMENT])) != 0 ) + else if ( (sentobj= LP_gettx(alicecoin,txids[BASILISK_ALICEPAYMENT])) != 0 ) { sentflags[BASILISK_ALICEPAYMENT] = 1; free_json(sentobj); @@ -756,7 +754,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti } if ( txbytes[BASILISK_ALICESPEND] != 0 ) { - txids[BASILISK_ALICESPEND] = basilisk_swap_sendrawtransaction("alicespend",bobcoin,txbytes[BASILISK_ALICESPEND]); + txids[BASILISK_ALICESPEND] = LP_broadcast("alicespend",bobcoin,txbytes[BASILISK_ALICESPEND]); if ( bits256_nonz(txids[BASILISK_ALICESPEND]) != 0 ) // tested { sentflags[BASILISK_ALICESPEND] = 1; @@ -781,7 +779,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti } if ( txbytes[BASILISK_ALICECLAIM] != 0 ) { - txids[BASILISK_ALICECLAIM] = basilisk_swap_sendrawtransaction("aliceclaim",bobcoin,txbytes[BASILISK_ALICECLAIM]); + txids[BASILISK_ALICECLAIM] = LP_broadcast("aliceclaim",bobcoin,txbytes[BASILISK_ALICECLAIM]); if ( bits256_nonz(txids[BASILISK_ALICECLAIM]) != 0 ) // tested { sentflags[BASILISK_ALICECLAIM] = 1; @@ -803,7 +801,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti } if ( txbytes[BASILISK_ALICERECLAIM] != 0 ) { - txids[BASILISK_ALICERECLAIM] = basilisk_swap_sendrawtransaction("alicereclaim",alicecoin,txbytes[BASILISK_ALICERECLAIM]); + txids[BASILISK_ALICERECLAIM] = LP_broadcast("alicereclaim",alicecoin,txbytes[BASILISK_ALICERECLAIM]); if ( bits256_nonz(txids[BASILISK_ALICERECLAIM]) != 0 ) // tested { sentflags[BASILISK_ALICERECLAIM] = 1; @@ -833,7 +831,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti } if ( txbytes[BASILISK_BOBSPEND] != 0 ) { - txids[BASILISK_BOBSPEND] = basilisk_swap_sendrawtransaction("bobspend",alicecoin,txbytes[BASILISK_BOBSPEND]); + txids[BASILISK_BOBSPEND] = LP_broadcast("bobspend",alicecoin,txbytes[BASILISK_BOBSPEND]); if ( bits256_nonz(txids[BASILISK_BOBSPEND]) != 0 ) // tested { sentflags[BASILISK_BOBSPEND] = 1; @@ -862,7 +860,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti } if ( txbytes[BASILISK_BOBRECLAIM] != 0 ) { - txids[BASILISK_BOBRECLAIM] = basilisk_swap_sendrawtransaction("bobreclaim",bobcoin,txbytes[BASILISK_BOBRECLAIM]); + txids[BASILISK_BOBRECLAIM] = LP_broadcast("bobreclaim",bobcoin,txbytes[BASILISK_BOBRECLAIM]); if ( bits256_nonz(txids[BASILISK_BOBRECLAIM]) != 0 ) // tested { sentflags[BASILISK_BOBRECLAIM] = 1; @@ -886,7 +884,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti } if ( txbytes[BASILISK_BOBREFUND] != 0 ) { - txids[BASILISK_BOBREFUND] = basilisk_swap_sendrawtransaction("bobrefund",bobcoin,txbytes[BASILISK_BOBREFUND]); + txids[BASILISK_BOBREFUND] = LP_broadcast("bobrefund",bobcoin,txbytes[BASILISK_BOBREFUND]); if ( bits256_nonz(txids[BASILISK_BOBREFUND]) != 0 ) // tested { sentflags[BASILISK_BOBREFUND] = 1; diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index ef731e08e..c72a80ec4 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -18,13 +18,18 @@ // marketmaker // +char *LP_getdatadir() +{ + return("/root"); +} + cJSON *basilisk_nullretjson(cJSON *retjson) { char *outstr; if ( retjson != 0 ) { outstr = jprint(retjson,0); - if ( strcmp(outstr,"{}") == 0 ) + if ( strcmp(outstr,"{}") == 0 || strcmp(outstr,"[]") == 0 ) { free_json(retjson); retjson = 0; @@ -34,328 +39,163 @@ cJSON *basilisk_nullretjson(cJSON *retjson) return(retjson); } -char *dex_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_t skip) +char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_t skip) { return(0); } -bits256 LP_privkey(char *coinaddr) -{ - bits256 privkey; - return(privkey); -} - -bits256 LP_pubkey(bits256 privkey) +cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params) { - bits256 pubkey; - pubkey = curve25519(privkey,curve25519_basepoint9()); - return(pubkey); -} - -void LP_unspentslock(char *symbol,cJSON *vins) -{ - + char *retstr; cJSON *retjson = 0; + if ( coin != 0 ) + { + retstr = bitcoind_passthru(coin->symbol,coin->serverport,coin->userpass,method,params); + if ( retstr != 0 && retstr[0] != 0 ) + { + retjson = cJSON_Parse(retstr); + free(retstr); + } + //printf("dpow_gettxout.(%s)\n",retstr); + } + return(basilisk_nullretjson(retjson)); } void LP_unspents_mark(char *symbol,cJSON *vins) { - -} - -uint64_t LP_getestimatedfee(char *symbol) -{ - return(200); -} - -uint64_t LP_txfee(char *symbol) -{ - return(10000); -} - -char *LP_validateaddress(char *symbol,char *address) -{ - char buf[128],*retstr=0; struct iguana_info *coin = LP_coinfind(symbol); - sprintf(buf,"\"%s\"",address); - retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"validateaddress",buf); - usleep(10000); - return(retstr); + printf("LOCK (%s)\n",jprint(vins,0)); } cJSON *LP_gettxout(char *symbol,bits256 txid,int32_t vout) { - char buf[128],str[65],*retstr=0; cJSON *json=0; struct iguana_info *coin = LP_coinfind(symbol); + char buf[128],str[65]; struct iguana_info *coin = LP_coinfind(symbol); sprintf(buf,"\"%s\", %d",bits256_str(str,txid),vout); - retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"gettxout",buf); - if ( retstr != 0 ) - { - json = cJSON_Parse(retstr); - free(retstr); - } - //printf("dpow_gettxout.(%s)\n",retstr); - return(json); + return(bitcoin_json(coin,"gettxout",buf)); } -char *LP_decoderawtransaction(char *symbol,char *rawtx) +cJSON *LP_gettx(char *symbol,bits256 txid) { - char *retstr,*paramstr; cJSON *array; struct iguana_info *coin = LP_coinfind(symbol); - array = cJSON_CreateArray(); - jaddistr(array,rawtx); - paramstr = jprint(array,1); - retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"decoderawtransaction",paramstr); - //printf("%s decoderawtransaction.(%s) <- (%s)\n",coin->symbol,retstr,paramstr); - free(paramstr); - return(retstr); -} - -cJSON *LP_gettransaction(char *symbol,bits256 txid) -{ - char buf[128],str[65],*retstr=0; cJSON *json = 0; struct iguana_info *coin = LP_coinfind(symbol); + char buf[128],str[65]; struct iguana_info *coin = LP_coinfind(symbol); sprintf(buf,"[\"%s\", 1]",bits256_str(str,txid)); - if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"getrawtransaction",buf)) != 0 ) - { - } - if ( retstr != 0 ) - { - json = cJSON_Parse(retstr); - free(retstr); - } - return(json); + return(bitcoin_json(coin,"getrawtransaction",buf)); } cJSON *LP_listunspent(char *symbol,char *coinaddr) { - char buf[128],*retstr; cJSON *json = 0; struct iguana_info *coin = LP_coinfind(symbol); + char buf[128]; struct iguana_info *coin = LP_coinfind(symbol); sprintf(buf,"0, 99999999, [\"%s\"]",coinaddr); - if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"listunspent",buf)) != 0 ) - { - json = cJSON_Parse(retstr); - //printf("%s (%s) listunspent.(%s)\n",coin->symbol,buf,retstr); - free(retstr); - } else printf("%s null retstr from (%s)n",coin->symbol,buf); - return(json); + return(bitcoin_json(coin,"listunspent",buf)); } cJSON *LP_listtransactions(char *symbol,char *coinaddr,int32_t count,int32_t skip) { - char buf[128],*retstr; cJSON *json = 0; struct iguana_info *coin = LP_coinfind(symbol); + char buf[128]; struct iguana_info *coin = LP_coinfind(symbol); if ( count == 0 ) count = 100; sprintf(buf,"[\"%s\", %d, %d, true]",coinaddr,count,skip); - if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"listtransactions",buf)) != 0 ) - { - //printf("LIST.(%s)\n",retstr); - json = cJSON_Parse(retstr); - free(retstr); - return(json); - } else printf("%s null retstr from (%s)n",coin->symbol,buf); - return(0); + return(bitcoin_json(coin,"listtransactions",buf)); } -char *LP_signrawtransaction(char *symbol,char *rawtx,cJSON *vins) +cJSON *LP_validateaddress(char *symbol,char *address) { - cJSON *array; char *paramstr,*retstr; struct iguana_info *coin = LP_coinfind(symbol); - array = cJSON_CreateArray(); - jaddistr(array,rawtx); - jaddi(array,jduplicate(vins)); - paramstr = jprint(array,1); - //printf("signrawtransaction\n"); - retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"signrawtransaction",paramstr); - //printf("%s signrawtransaction.(%s) params.(%s)\n",coin->symbol,retstr,paramstr); - free(paramstr); - return(retstr); -} - -char *LP_sendrawtransaction(char *symbol,char *signedtx) -{ - cJSON *array; char *paramstr,*retstr; struct iguana_info *coin = LP_coinfind(symbol); - array = cJSON_CreateArray(); - jaddistr(array,signedtx); - paramstr = jprint(array,1); - retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"sendrawtransaction",paramstr); - printf(">>>>>>>>>>> %s dpow_sendrawtransaction.(%s) -> (%s)\n",coin->symbol,paramstr,retstr); - free(paramstr); - return(retstr); + char buf[512]; struct iguana_info *coin = LP_coinfind(symbol); + sprintf(buf,"\"%s\"",address); + return(bitcoin_json(coin,"validateaddress",buf)); } -char *LP_importaddress(char *symbol,char *address) +int32_t LP_importaddress(char *symbol,char *address) { char buf[1024],*retstr; cJSON *validatejson; int32_t isvalid=0,doneflag = 0; struct iguana_info *coin = LP_coinfind(symbol); - if ( (retstr= LP_validateaddress(symbol,address)) != 0 ) + if ( coin == 0 ) + return(-2); + if ( (validatejson= LP_validateaddress(symbol,address)) != 0 ) { - if ( (validatejson= cJSON_Parse(retstr)) != 0 ) + if ( (isvalid= is_cJSON_True(jobj(validatejson,"isvalid")) != 0) != 0 ) { - if ( (isvalid= is_cJSON_True(jobj(validatejson,"isvalid")) != 0) != 0 ) - { - if ( is_cJSON_True(jobj(validatejson,"iswatchonly")) != 0 || is_cJSON_True(jobj(validatejson,"ismine")) != 0 ) - doneflag = 1; - } - free_json(validatejson); + if ( is_cJSON_True(jobj(validatejson,"iswatchonly")) != 0 || is_cJSON_True(jobj(validatejson,"ismine")) != 0 ) + doneflag = 1; } - free(retstr); - retstr = 0; + free_json(validatejson); } if ( isvalid == 0 ) - return(clonestr("{\"isvalid\":false}")); + return(-1); if ( doneflag != 0 ) return(0); // success sprintf(buf,"[\"%s\", \"%s\", false]",address,address); - retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"importaddress",buf); - printf("%s importaddress.(%s) -> (%s)\n",coin->symbol,address,retstr); - return(retstr); -} - -char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON *vins,char *rawtxbytes,cJSON *privkeys,struct vin_info *V) -{ - return(0); -} - -cJSON *LP_swapgettxout(char *symbol,bits256 trigger,int32_t vout) -{ - cJSON *retjson=0; //char *retstr; struct iguana_info *coin; - /*if ( ((coin= LP_coinfind(symbol)) == 0 || coin->FULLNODE == 0) && iguana_isnotarychain(symbol) >= 0 ) - { - if ( (retstr= dex_gettxout(0,0,0,trigger,symbol,vout)) != 0 ) - { - //printf("dexgettxout.(%s)\n",retstr); - retjson = cJSON_Parse(retstr); - free(retstr); - } - if ( 0 && strcmp("BTC",symbol) == 0 ) - printf("%s gettxout.(%s)\n",symbol,jprint(retjson,0)); - } - else - { - retjson = dpow_gettxout(coin,trigger,vout); - //printf("need to verify passthru has this info\n"); - //printf("dpowgettxout.(%s)\n",jprint(retjson,0)); - }*/ - return(basilisk_nullretjson(retjson)); -} - -uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) -{ - uint64_t value = 0; - return(value); -} - -cJSON *LP_swapgettx(char *symbol,bits256 txid) -{ - cJSON *retjson=0; //char *retstr; struct iguana_info *coin; - /*if ( ((coin= LP_coinfind(symbol)) == 0 || coin->FULLNODE == 0) && iguana_isnotarychain(symbol) >= 0 ) - { - if ( (retstr= dex_gettransaction(0,0,0,txid,symbol)) != 0 ) - { - retjson = cJSON_Parse(retstr); - free(retstr); - } - //if ( strcmp("BTC",symbol) == 0 ) - // printf("%s gettx.(%s)\n",symbol,jprint(retjson,0)); - } else retjson = dpow_gettransaction(coin,txid);*/ - return(basilisk_nullretjson(retjson)); + if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"importaddress",buf)) != 0 ) + free(retstr); + return(1); } -bits256 basilisk_swap_sendrawtransaction(char *txname,char *symbol,char *txbytes) +uint64_t LP_getestimatedrate(char *symbol) { - char *retstr; bits256 txid; int32_t i,sentflag = 0; - memset(&txid,0,sizeof(txid)); - for (i=0; i<3; i++) + char buf[512],*retstr; uint64_t rate = 200; struct iguana_info *coin = LP_coinfind(symbol); + if ( coin != 0 ) { - if ( (retstr= LP_sendrawtransaction(symbol,txbytes)) != 0 ) + sprintf(buf,"[%d]",3); + if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"importaddress",buf)) != 0 ) { - if ( is_hexstr(retstr,0) == 64 ) - { - decode_hex(txid.bytes,32,retstr); - sentflag = 1; - } - char str[65]; printf("[%s] %s RETSTR.(%s) %s.%s\n",txname,txbytes,retstr,symbol,bits256_str(str,txid)); + rate = atof(retstr); + printf("estimated rate %s -> %llu\n",retstr,(long long)rate); free(retstr); } - if ( sentflag != 0 ) - break; } - return(txid); + return(rate); } -bits256 LP_broadcast(char *name,char *symbol,uint8_t *data,int32_t datalen) +uint64_t LP_txfee(char *symbol) { - bits256 txid; char *signedtx,*retstr; int32_t i; - memset(txid.bytes,0,sizeof(txid)); - if ( data != 0 && datalen != 0 ) - { - char str[65]; -#ifdef BASILISK_DISABLESENDTX - txid = bits256_doublesha256(0,data,datalen); - printf("%s <- dont sendrawtransaction (%s)\n",name,bits256_str(str,txid)); - return(txid); -#endif - signedtx = malloc(datalen*2 + 1); - init_hexbytes_noT(signedtx,data,datalen); - for (i=0; i<3; i++) - { - if ( (retstr= LP_sendrawtransaction(symbol,signedtx)) != 0 ) - { - if ( is_hexstr(retstr,0) == 64 ) - { - decode_hex(txid.bytes,32,retstr); - free(retstr); - printf("sendrawtransaction.%s %s.(%s)\n",name,symbol,bits256_str(str,txid)); - break; - } - else - { - printf("sendrawtransaction.%s %s error.(%s)\n",name,symbol,retstr); - free(retstr); - } - } else printf("sendrawtransaction.%s %s got null return\n",name,symbol); - } - free(signedtx); - } - return(txid); + uint64_t txfee = 0; + if ( strcmp(symbol,"BTC") != 0 ) + txfee = 50000; + return(txfee); } -int32_t basilisk_confirmsobj(cJSON *item) +char *LP_sendrawtransaction(char *symbol,char *signedtx) { - int32_t height,numconfirms; - height = jint(item,"height"); - numconfirms = jint(item,"numconfirms"); - if ( height > 0 && numconfirms >= 0 ) - return(numconfirms); - printf("basilisk_confirmsobj height.%d numconfirms.%d (%s)\n",height,numconfirms,jprint(item,0)); - return(-1); + cJSON *array; char *paramstr,*retstr; struct iguana_info *coin = LP_coinfind(symbol); + if ( coin == 0 ) + return(0); + array = cJSON_CreateArray(); + jaddistr(array,signedtx); + paramstr = jprint(array,1); + retstr = bitcoind_passthru(symbol,coin->serverport,coin->userpass,"sendrawtransaction",paramstr); + printf(">>>>>>>>>>> %s dpow_sendrawtransaction.(%s) -> (%s)\n",coin->symbol,paramstr,retstr); + free(paramstr); + return(retstr); } -int32_t LP_numconfirms(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx) +char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON *vins,char *rawtx,cJSON *privkeys,struct vin_info *V) { -#ifdef BASILISK_DISABLEWAITTX - return(100); -#endif - /*cJSON *argjson,*valuearray=0; char *valstr; int32_t i,n,retval = -1; - argjson = cJSON_CreateObject(); - jaddbits256(argjson,"txid",rawtx->I.actualtxid); - jaddnum(argjson,"vout",0); - jaddstr(argjson,"coin",rawtx->coin->symbol); - if ( (valstr= basilisk_value(rawtx->coin,0,0,swap->persistent_pubkey,argjson,0)) != 0 ) + cJSON *array,*json; int32_t len; uint8_t *data; char *paramstr,*retstr,*hexstr,*signedtx=0; struct iguana_info *coin = LP_coinfind(symbol); + memset(signedtxidp,0,sizeof(*signedtxidp)); + *completedp = 0; + if ( coin == 0 ) + return(0); + array = cJSON_CreateArray(); + jaddistr(array,rawtx); + jaddi(array,jduplicate(vins)); + jaddi(array,jduplicate(privkeys)); + paramstr = jprint(array,1); + //printf("signrawtransaction\n"); + if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"signrawtransaction",paramstr)) != 0 ) { - char str[65]; printf("basilisk_numconfirms required.%d %s %s valstr.(%s)\n",rawtx->I.numconfirms,rawtx->name,bits256_str(str,rawtx->I.actualtxid),valstr); - //basilisk_numconfirms required.0 alicespend 29a2a6b4a61b1da82096d533c71b6762d61a82ca771a633269d97c0ccb94fe85 valstr.({"result":"success","numconfirms":0,"address":"1JGvZ67oTdM7kCya4J8kj1uErbSRAoq3wH","satoshis":"1413818","value":0.01413818,"height":462440,"txid":"29a2a6b4a61b1da82096d533c71b6762d61a82ca771a633269d97c0ccb94fe85","vout":0,"coin":"BTC"}) - - if ( (valuearray= cJSON_Parse(valstr)) != 0 ) +printf("%s signrawtransaction.(%s) params.(%s)\n",coin->symbol,retstr,paramstr); + if ( (json= cJSON_Parse(retstr)) != 0 ) { - if ( valstr[0] == '[' && is_cJSON_Array(valuearray) != 0 ) + if ( (hexstr= jstr(json,"hex")) != 0 ) { - n = cJSON_GetArraySize(valuearray); - for (i=0; i= 0 ) - break; - } - } else retval = basilisk_confirmsobj(valuearray); - free_json(valuearray); - } else printf("parse error\n"); - free(valstr); + len = (int32_t)strlen(hexstr); + signedtx = calloc(1,len+1); + strcpy(signedtx,hexstr); + *completedp = jint(json,"completed"); + data = malloc(len >> 1); + decode_hex(data,len>>1,hexstr); + *signedtxidp = bits256_doublesha256(0,data,len >> 1); + } + free_json(json); + } + free(retstr); } - free_json(argjson); - printf("numconfirms.%d returned\n",retval); - return(retval);*/ + free(paramstr); + return(signedtx); } diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 2512c83f9..74d820b5f 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -19,6 +19,81 @@ // +bits256 LP_broadcast(char *txname,char *symbol,char *txbytes) +{ + char *retstr; bits256 txid; int32_t i,sentflag = 0; + memset(&txid,0,sizeof(txid)); + for (i=0; i<3; i++) + { + if ( (retstr= LP_sendrawtransaction(symbol,txbytes)) != 0 ) + { + if ( is_hexstr(retstr,0) == 64 ) + { + decode_hex(txid.bytes,32,retstr); + sentflag = 1; + } + char str[65]; printf("[%s] %s RETSTR.(%s) %s.%s\n",txname,txbytes,retstr,symbol,bits256_str(str,txid)); + free(retstr); + } + if ( sentflag != 0 ) + break; + } + return(txid); +} + +bits256 LP_broadcast_tx(char *name,char *symbol,uint8_t *data,int32_t datalen) +{ + bits256 txid; char *signedtx; + memset(txid.bytes,0,sizeof(txid)); + if ( data != 0 && datalen != 0 ) + { + char str[65]; +#ifdef BASILISK_DISABLESENDTX + txid = bits256_doublesha256(0,data,datalen); + printf("%s <- dont sendrawtransaction (%s)\n",name,bits256_str(str,txid)); + return(txid); +#endif + signedtx = malloc(datalen*2 + 1); + init_hexbytes_noT(signedtx,data,datalen); + txid = LP_broadcast(name,symbol,signedtx); + // sent to nn_socket! + free(signedtx); + } + return(txid); +} + +uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) +{ + uint64_t value = 0; cJSON *txobj,*vouts,*utxoobj; int32_t numvouts; + if ( (txobj= LP_gettx(symbol,txid)) != 0 ) + { + if ( (vouts= jarray(&numvouts,txobj,"vout")) != 0 && vout < numvouts ) + { + utxoobj = jitem(vouts,vout); + if ( (value= jdouble(utxoobj,"amount")*SATOSHIDEN) == 0 && (value= jdouble(utxoobj,"value")*SATOSHIDEN) == 0 ) + { + char str[65]; printf("%s LP_txvalue.%s strange utxo.(%s) vout.%d/%d\n",symbol,bits256_str(str,txid),jprint(utxoobj,0),vout,numvouts); + } + } + free_json(txobj); + } + return(value); +} + +int32_t LP_numconfirms(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx) +{ + int32_t numconfirms = 100; +#ifndef BASILISK_DISABLEWAITTX + cJSON *txobj; + if ( (txobj= LP_gettx(symbol,txid)) != 0 ) + { + numconfirms = jint(txobj,"confirmations"); + free_json(txobj); + } +#endif + return(numconfirms); +} + #ifdef later int32_t iguana_msgtx_Vset(uint8_t *serialized,int32_t maxlen,struct iguana_msgtx *msgtx,struct vin_info *V) @@ -488,9 +563,8 @@ int32_t iguana_signrawtransaction(uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS *signedtxp = signedtx; return(complete); } -#endif -/*int32_t basilisk_rawtx_return(struct basilisk_rawtx *rawtx,cJSON *item,int32_t lockinputs,struct vin_info *V) +int32_t basilisk_rawtx_return(struct basilisk_rawtx *rawtx,cJSON *item,int32_t lockinputs,struct vin_info *V) { char *signedtx,*txbytes; cJSON *vins,*privkeyarray; int32_t i,n,retval = -1; if ( (txbytes= jstr(item,"rawtx")) != 0 && (vins= jobj(item,"vins")) != 0 ) @@ -676,7 +750,8 @@ int32_t _basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8 free_json(txobj); free(V); return(retval); -}*/ +} +#endif char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp) { @@ -688,7 +763,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch //printf("bobtxspend.%s redeem.[%d]\n",symbol,redeemlen); if ( redeemlen < 0 ) return(0); - if ( (utxoobj= LP_swapgettxout(symbol,utxotxid,vout)) == 0 ) + if ( (utxoobj= LP_gettxout(symbol,utxotxid,vout)) == 0 ) { printf("basilisk_swap_bobtxspend.%s utxo already spent or doesnt exist\n",name); return(0); @@ -802,9 +877,9 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub if ( strcmp(coin->symbol,"BTC") != 0 ) return(retval); len = rawtx->I.datalen; - if ( coin->estimatedfee == 0 ) - coin->estimatedfee = LP_getestimatedfee(coin->symbol); - newtxfee = coin->estimatedfee * len; + if ( coin->estimatedrate == 0 ) + coin->estimatedrate = LP_getestimatedrate(coin->symbol); + newtxfee = coin->estimatedrate * len; printf("txfee %.8f -> newtxfee %.8f\n",dstr(txfee),dstr(newtxfee)); } else break; } @@ -813,7 +888,7 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,struct basilisk_swap *swap,struct basilisk_rawtx *dest,struct basilisk_rawtx *rawtx,bits256 privkey,bits256 *privkey2,uint8_t *userdata,int32_t userdatalen,int32_t ignore_cltverr) { - char *signedtx; int64_t txfee,newtxfee=0,estimatedfee,destamount; uint32_t timestamp,locktime=0,sequenceid = 0xffffffff; int32_t iter,len,retval = -1; + char *signedtx; int64_t txfee,newtxfee=0,estimatedrate,destamount; uint32_t timestamp,locktime=0,sequenceid = 0xffffffff; int32_t iter,len,retval = -1; timestamp = swap->I.started; if ( dest == &swap->aliceclaim ) locktime = swap->bobdeposit.I.locktime + 1, sequenceid = 0; @@ -835,8 +910,8 @@ int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_ if ( strcmp(symbol,"BTC") != 0 ) return(retval); len = rawtx->I.datalen; - estimatedfee = LP_getestimatedfee(symbol); - newtxfee = estimatedfee * len; + estimatedrate = LP_getestimatedrate(symbol); + newtxfee = estimatedrate * len; } else break; } return(retval); @@ -907,7 +982,7 @@ int32_t LP_swap_getcoinaddr(char *symbol,char *coinaddr,bits256 txid,int32_t vou { cJSON *retjson; coinaddr[0] = 0; - if ( (retjson= LP_swapgettx(symbol,txid)) != 0 ) + if ( (retjson= LP_gettx(symbol,txid)) != 0 ) { LP_swap_txdestaddr(coinaddr,txid,vout,retjson); free_json(retjson); @@ -918,7 +993,7 @@ int32_t LP_swap_getcoinaddr(char *symbol,char *coinaddr,bits256 txid,int32_t vou int32_t basilisk_swap_getsigscript(char *symbol,uint8_t *script,int32_t maxlen,bits256 txid,int32_t vini) { cJSON *retjson,*vins,*item,*skey; int32_t n,scriptlen = 0; char *hexstr; - if ( (retjson= LP_swapgettx(symbol,txid)) != 0 ) + if ( (retjson= LP_gettx(symbol,txid)) != 0 ) { if ( (vins= jarray(&n,retjson,"vin")) != 0 && vini < n ) { @@ -939,7 +1014,7 @@ int64_t basilisk_txvalue(char *symbol,bits256 txid,int32_t vout) { cJSON *txobj,*vouts,*item; int32_t n; int64_t value = 0; //char str[65]; printf("%s txvalue.(%s)\n",symbol,bits256_str(str,txid)); - if ( (txobj= LP_swapgettx(symbol,txid)) != 0 ) + if ( (txobj= LP_gettx(symbol,txid)) != 0 ) { //printf("txobj.(%s)\n",jprint(txobj,0)); if ( (vouts= jarray(&n,txobj,"vout")) != 0 ) @@ -957,7 +1032,7 @@ bits256 _LP_swap_spendtxid(char *symbol,char *destaddr,char *coinaddr,bits256 ut { char *retstr,*addr; cJSON *array,*item,*array2; int32_t i,n,m; bits256 spendtxid,txid; memset(&spendtxid,0,sizeof(spendtxid)); - if ( (retstr= dex_listtransactions(symbol,coinaddr,100,0)) != 0 ) + if ( (retstr= blocktrail_listtransactions(symbol,coinaddr,100,0)) != 0 ) { if ( (array= cJSON_Parse(retstr)) != 0 ) { @@ -1014,7 +1089,7 @@ bits256 LP_swap_spendtxid(char *symbol,char *destaddr,bits256 utxotxid,int32_t v coinaddr[0] = 0; memset(&spendtxid,0,sizeof(spendtxid)); //char str[65]; printf("swap %s spendtxid.(%s)\n",symbol,bits256_str(str,utxotxid)); - if ( strcmp("BTC",symbol) == 0 ) + if ( 0 && strcmp("BTC",symbol) == 0 ) { //[{"type":"sent","confirmations":379,"height":275311,"timestamp":1492084664,"txid":"8703c5517bc57db38134058370a14e99b8e662b99ccefa2061dea311bbd02b8b","vout":0,"amount":117.50945263,"spendtxid":"cf2509e076fbb9b22514923df916b7aacb1391dce9c7e1460b74947077b12510","vin":0,"paid":{"type":"paid","txid":"cf2509e076fbb9b22514923df916b7aacb1391dce9c7e1460b74947077b12510","height":275663,"timestamp":1492106024,"vouts":[{"RUDpN6PEBsE7ZFbGjUxk1W3QVsxnjBLYw6":117.50935263}]}}] LP_swap_getcoinaddr(symbol,coinaddr,utxotxid,vout); @@ -1069,7 +1144,7 @@ bits256 LP_swap_spendtxid(char *symbol,char *destaddr,bits256 utxotxid,int32_t v if ( (catstr= jstr(item,"category")) != 0 && strcmp(catstr,"send") == 0 ) { txid = jbits256(item,"txid"); - if ( (txobj= LP_swapgettx(symbol,txid)) != 0 ) + if ( (txobj= LP_gettx(symbol,txid)) != 0 ) { if ( (vins= jarray(&m,txobj,"vin")) != 0 && m > jint(item,"vout") ) { @@ -1309,7 +1384,7 @@ int32_t basilisk_verify_bobpaid(void *ptr,uint8_t *data,int32_t datalen) memset(revAm.bytes,0,sizeof(revAm)); if ( basilisk_rawtx_spendscript(swap,swap->bobcoin.longestchain,&swap->bobpayment,0,data,datalen,0) == 0 ) { - swap->bobpayment.I.signedtxid = LP_broadcast(swap->bobpayment.name,swap->bobpayment.coin->symbol,swap->bobpayment.txbytes,swap->bobpayment.I.datalen); + swap->bobpayment.I.signedtxid = LP_broadcast_tx(swap->bobpayment.name,swap->bobpayment.coin->symbol,swap->bobpayment.txbytes,swap->bobpayment.I.datalen); if ( bits256_nonz(swap->bobpayment.I.signedtxid) != 0 ) swap->paymentunconf = 1; basilisk_dontforget_update(swap,&swap->bobpayment); @@ -1452,7 +1527,7 @@ uint32_t LP_swapdata_rawtxsend(struct basilisk_swap *swap,uint32_t msgbits,uint8 if ( bits256_nonz(rawtx->I.signedtxid) != 0 && bits256_nonz(rawtx->I.actualtxid) == 0 ) { char str[65],str2[65]; - rawtx->I.actualtxid = LP_broadcast(rawtx->name,rawtx->coin->symbol,rawtx->txbytes,rawtx->I.datalen); + rawtx->I.actualtxid = LP_broadcast_tx(rawtx->name,rawtx->coin->symbol,rawtx->txbytes,rawtx->I.datalen); if ( bits256_cmp(rawtx->I.actualtxid,rawtx->I.signedtxid) != 0 ) { printf("%s rawtxsend %s vs %s\n",rawtx->name,bits256_str(str,rawtx->I.signedtxid),bits256_str(str2,rawtx->I.actualtxid)); @@ -1536,7 +1611,7 @@ int32_t basilisk_verify_bobdeposit(void *ptr,uint8_t *data,int32_t datalen) uint8_t userdata[512]; int32_t i,retval,len = 0; static bits256 zero; struct basilisk_swap *swap = ptr; if ( basilisk_rawtx_spendscript(swap,swap->bobcoin.longestchain,&swap->bobdeposit,0,data,datalen,0) == 0 ) { - swap->bobdeposit.I.signedtxid = LP_broadcast(swap->bobdeposit.name,swap->bobcoin.symbol,swap->bobdeposit.txbytes,swap->bobdeposit.I.datalen); + swap->bobdeposit.I.signedtxid = LP_broadcast_tx(swap->bobdeposit.name,swap->bobcoin.symbol,swap->bobdeposit.txbytes,swap->bobdeposit.I.datalen); if ( bits256_nonz(swap->bobdeposit.I.signedtxid) != 0 ) swap->depositunconf = 1; basilisk_dontforget_update(swap,&swap->bobdeposit); @@ -1599,7 +1674,7 @@ int32_t basilisk_verify_alicepaid(void *ptr,uint8_t *data,int32_t datalen) struct basilisk_swap *swap = ptr; if ( basilisk_rawtx_spendscript(swap,swap->alicecoin.longestchain,&swap->alicepayment,0,data,datalen,0) == 0 ) { - swap->alicepayment.I.signedtxid = LP_broadcast(swap->alicepayment.name,swap->alicecoin.symbol,swap->alicepayment.txbytes,swap->alicepayment.I.datalen); + swap->alicepayment.I.signedtxid = LP_broadcast_tx(swap->alicepayment.name,swap->alicecoin.symbol,swap->alicepayment.txbytes,swap->alicepayment.I.datalen); if ( bits256_nonz(swap->alicepayment.I.signedtxid) != 0 ) swap->aliceunconf = 1; basilisk_dontforget_update(swap,&swap->alicepayment); From 36ba6f9ff15a9b1a95bc959a3de76433d932b8f0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:03:51 +0300 Subject: [PATCH 004/339] Test --- iguana/exchanges/LP_nativeDEX.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 4e7be2b89..fba18f5ad 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -763,6 +763,7 @@ void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin) printf("error launching stats rpcloop for port.%u\n",myport); exit(-1); } + LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); LP_privkey_init(mypeer,pubsock,"KMD",60,"test",""); printf("utxos.(%s)\n",LP_utxos(mypeer,"",10000)); while ( 1 ) From 9c49e988c00d48fd7f65cfaf4a6d002150bbbefb Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:07:09 +0300 Subject: [PATCH 005/339] Test --- iguana/exchanges/LP_nativeDEX.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index fba18f5ad..8c8ab3e7f 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -521,9 +521,16 @@ void LP_statefname(char *fname,char *symbol,char *assetname,char *str) strcat(fname,".litecoin"); else { - if ( assetname[0] == 0 ) - strcat(fname,".komodo"); - else strcat(fname,assetname); + strcat(fname,".komodo"); + if ( strcmp(symbol,"KMD") != 0 ) + { +#ifdef WIN32 + strcat(fname,"\\"); +#else + strcat(fname,"/"); +#endif + strcat(fname,assetname); + } } #ifdef WIN32 strcat(fname,"\\"); @@ -623,6 +630,7 @@ struct iguana_info *LP_coinfind(char *symbol) cdata.wiftype = 188; LP_userpass(cdata.userpass,symbol,symbol,strcmp(symbol,"KMD") == 0 ? "komodo" : symbol); } + printf("%s: (%s) (%s)\n",symbol,cdata.serverport,cdata.userpass); LP_coins = realloc(LP_coins,sizeof(*LP_coins) * (LP_numcoins+1)); coin = &LP_coins[LP_numcoins++]; *coin = cdata; @@ -763,7 +771,7 @@ void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin) printf("error launching stats rpcloop for port.%u\n",myport); exit(-1); } - LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); + LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); LP_coinfind("USD"); LP_coinfind("REVS"); LP_coinfind("JUMBLR"); LP_privkey_init(mypeer,pubsock,"KMD",60,"test",""); printf("utxos.(%s)\n",LP_utxos(mypeer,"",10000)); while ( 1 ) From 2fcc19d52af6fa5280378ba4bcc3511cd81025ea Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:11:17 +0300 Subject: [PATCH 006/339] Test --- iguana/exchanges/LP_nativeDEX.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 8c8ab3e7f..e434224df 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -637,21 +637,26 @@ struct iguana_info *LP_coinfind(char *symbol) return(coin); } -uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *coin,uint8_t addrtype,char *passphrase,char *wifstr) +uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symbol,char *passphrase,char *wifstr) { - char *retstr,coinaddr[64],*script; cJSON *array,*item; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; + char *retstr,coinaddr[64],*script; cJSON *array,*item; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; struct iguana_info *coin = LP_coinfind(symbol); + if ( coin == 0 ) + { + printf("cant add privkey for %s, coin not active\n",symbol); + return(0); + } if ( passphrase != 0 ) conv_NXTpassword(privkey.bytes,pubkey.bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); else privkey = iguana_wif2privkey(wifstr); - iguana_priv2pub(pubkey33,coinaddr,privkey,addrtype); + iguana_priv2pub(pubkey33,coinaddr,privkey,coin->pubtype); bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); LP_privkeyadd(privkey,rmd160); - retstr = iguana_listunspent(coin,coinaddr); + retstr = iguana_listunspent(symbol,coinaddr); if ( retstr != 0 && retstr[0] == '[' && retstr[1] == ']' ) free(retstr), retstr = 0; if ( retstr == 0 ) { - if ( (retstr= DEX_listunspent(coin,coinaddr)) == 0 ) + if ( (retstr= DEX_listunspent(symbol,coinaddr)) == 0 ) { printf("null listunspent\n"); return(0); @@ -693,7 +698,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *coin { value = values[i]; values[i] = 0, used++; - LP_addutxo(mypeer,mypubsock,coin,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin); + LP_addutxo(mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin); total += value; } } @@ -772,7 +777,7 @@ void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin) exit(-1); } LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); LP_coinfind("USD"); LP_coinfind("REVS"); LP_coinfind("JUMBLR"); - LP_privkey_init(mypeer,pubsock,"KMD",60,"test",""); + LP_privkey_init(mypeer,pubsock,"KMD","test",""); printf("utxos.(%s)\n",LP_utxos(mypeer,"",10000)); while ( 1 ) { From 9e7e1dda47321bafd7f9111636cf9c0108ffb01c Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:12:46 +0300 Subject: [PATCH 007/339] Test --- iguana/exchanges/LP_nativeDEX.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index e434224df..c0fe51e0c 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -649,6 +649,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb conv_NXTpassword(privkey.bytes,pubkey.bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); else privkey = iguana_wif2privkey(wifstr); iguana_priv2pub(pubkey33,coinaddr,privkey,coin->pubtype); + printf("%s coinaddr.%s %d\n",symbol,coinaddr,coin->pubtype); bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); LP_privkeyadd(privkey,rmd160); retstr = iguana_listunspent(symbol,coinaddr); From 3dc34a54bbdcd55e8b68d3dedefe6f9cd25bf16a Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:14:49 +0300 Subject: [PATCH 008/339] Test --- iguana/exchanges/LP_nativeDEX.c | 17 ++--------------- iguana/exchanges/mm.c | 4 ++-- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index c0fe51e0c..3386c7964 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -639,7 +639,7 @@ struct iguana_info *LP_coinfind(char *symbol) uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symbol,char *passphrase,char *wifstr) { - char *retstr,coinaddr[64],*script; cJSON *array,*item; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; struct iguana_info *coin = LP_coinfind(symbol); + char coinaddr[64],*script; cJSON *array,*item; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; struct iguana_info *coin = LP_coinfind(symbol); if ( coin == 0 ) { printf("cant add privkey for %s, coin not active\n",symbol); @@ -652,19 +652,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb printf("%s coinaddr.%s %d\n",symbol,coinaddr,coin->pubtype); bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); LP_privkeyadd(privkey,rmd160); - retstr = iguana_listunspent(symbol,coinaddr); - if ( retstr != 0 && retstr[0] == '[' && retstr[1] == ']' ) - free(retstr), retstr = 0; - if ( retstr == 0 ) - { - if ( (retstr= DEX_listunspent(symbol,coinaddr)) == 0 ) - { - printf("null listunspent\n"); - return(0); - } - } - printf("LP_privkey_init.(%s)\n",retstr); - if ( (array= cJSON_Parse(retstr)) != 0 ) + if ( (array= LP_listunspent(symbol,coinaddr)) != 0 ) { if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) { @@ -709,7 +697,6 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb } free_json(array); } - free(retstr); return(total); } diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index ca30cf3d2..814ada65e 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -170,13 +170,13 @@ char *iguana_walletpassphrase(char *passphrase,int32_t timeout) return(bitcoind_RPC(0,"",url,0,"walletpassphrase",postdata)); } -char *iguana_listunspent(char *coin,char *coinaddr) +/*char *iguana_listunspent(char *coin,char *coinaddr) { char url[512],postdata[1024]; sprintf(url,"%s/coin=%s&agent=bitcoinrpc&method=listunspent?",IGUANA_URL,coin); sprintf(postdata,"[\"%s\"]",coinaddr); return(bitcoind_RPC(0,"",url,0,"listunspent",postdata)); -} +}*/ /*char *issue_LP_intro(char *destip,uint16_t destport,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers) { From e71e8b482141da3ce04465518f8fb3fb55c0218b Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:16:27 +0300 Subject: [PATCH 009/339] Test --- iguana/exchanges/LP_rpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index c72a80ec4..4d07fd765 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -25,7 +25,7 @@ char *LP_getdatadir() cJSON *basilisk_nullretjson(cJSON *retjson) { - char *outstr; + /*char *outstr; if ( retjson != 0 ) { outstr = jprint(retjson,0); @@ -35,7 +35,7 @@ cJSON *basilisk_nullretjson(cJSON *retjson) retjson = 0; } free(outstr); - } + }*/ return(retjson); } From ad663744a213f5510840e7fd6061e91c61b6e2e3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:17:30 +0300 Subject: [PATCH 010/339] Test --- iguana/exchanges/LP_rpc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 4d07fd765..921720400 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -52,6 +52,7 @@ cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params) retstr = bitcoind_passthru(coin->symbol,coin->serverport,coin->userpass,method,params); if ( retstr != 0 && retstr[0] != 0 ) { + printf("%s: %s.%s -> (%s)\n",coin->symbol,method,params,retstr); retjson = cJSON_Parse(retstr); free(retstr); } From 74fbd2c268aab5f534d8964afce01286e9202b23 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:18:38 +0300 Subject: [PATCH 011/339] Test --- iguana/exchanges/LP_rpc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 921720400..a28c4d9f4 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -49,6 +49,7 @@ cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params) char *retstr; cJSON *retjson = 0; if ( coin != 0 ) { + printf("issue.(%s, %s, %s, %s, %s)\n",coin->symbol,coin->serverport,coin->userpass,method,params); retstr = bitcoind_passthru(coin->symbol,coin->serverport,coin->userpass,method,params); if ( retstr != 0 && retstr[0] != 0 ) { From 4b2b791e778a50a4aea22cc4b4d132b7d73e2300 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:21:54 +0300 Subject: [PATCH 012/339] Test --- crypto777/bitcoind_RPC.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto777/bitcoind_RPC.c b/crypto777/bitcoind_RPC.c index 2a4636801..02c8e71d7 100755 --- a/crypto777/bitcoind_RPC.c +++ b/crypto777/bitcoind_RPC.c @@ -136,7 +136,7 @@ char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char * else specialcase = 0; if ( url[0] == 0 ) strcpy(url,"http://127.0.0.1:7776"); - if ( specialcase != 0 && (0) ) + //if ( specialcase != 0 && (0) ) printf("<<<<<<<<<<< bitcoind_RPC: debug.(%s) url.(%s) command.(%s) params.(%s)\n",debugstr,url,command,params); try_again: if ( retstrp != 0 ) From cfa382c2053fd9906b8eb1d2a5fc00cb6640a413 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:27:17 +0300 Subject: [PATCH 013/339] Test --- iguana/exchanges/LP_include.h | 1 + iguana/exchanges/LP_rpc.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 5aae7da4a..ed0f4fde2 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -467,5 +467,6 @@ uint32_t basilisk_requestid(struct basilisk_request *rp); uint32_t basilisk_quoteid(struct basilisk_request *rp); char *bitcoind_passthru(char *coinstr,char *serverport,char *userpass,char *method,char *params); struct iguana_info *LP_coinfind(char *symbol); +void *curl_post(void **cHandlep,char *url,char *userpass,char *postfields,char *hdr0,char *hdr1,char *hdr2,char *hdr3); #endif diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index a28c4d9f4..21d8b8a8e 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -46,11 +46,13 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_ cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params) { + static void *cHandle; char *retstr; cJSON *retjson = 0; if ( coin != 0 ) { printf("issue.(%s, %s, %s, %s, %s)\n",coin->symbol,coin->serverport,coin->userpass,method,params); - retstr = bitcoind_passthru(coin->symbol,coin->serverport,coin->userpass,method,params); + retstr = curl_post(&cHandle,coin->serverport,coin->userpass,params,method,0,0,0); + //retstr = bitcoind_passthru(coin->symbol,coin->serverport,coin->userpass,method,params); if ( retstr != 0 && retstr[0] != 0 ) { printf("%s: %s.%s -> (%s)\n",coin->symbol,method,params,retstr); From b42dad372b11c16110ebc47d9c4114c88b058db9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:29:31 +0300 Subject: [PATCH 014/339] Test --- iguana/exchanges/LP_rpc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 21d8b8a8e..604e58aa0 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -46,13 +46,14 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_ cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params) { - static void *cHandle; + //static void *cHandle; char *retstr; cJSON *retjson = 0; if ( coin != 0 ) { printf("issue.(%s, %s, %s, %s, %s)\n",coin->symbol,coin->serverport,coin->userpass,method,params); - retstr = curl_post(&cHandle,coin->serverport,coin->userpass,params,method,0,0,0); + //retstr = curl_post(&cHandle,coin->serverport,coin->userpass,params,method,0,0,0); //retstr = bitcoind_passthru(coin->symbol,coin->serverport,coin->userpass,method,params); + retstr = bitcoind_RPC(0,"",coin->serverport,0,method,params); if ( retstr != 0 && retstr[0] != 0 ) { printf("%s: %s.%s -> (%s)\n",coin->symbol,method,params,retstr); From 849296498ef76f40a8eb9fba80644166f674fb9f Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:34:53 +0300 Subject: [PATCH 015/339] Test --- iguana/exchanges/LP_nativeDEX.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 3386c7964..3ccad5825 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -765,6 +765,7 @@ void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin) exit(-1); } LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); LP_coinfind("USD"); LP_coinfind("REVS"); LP_coinfind("JUMBLR"); + LP_privkey_init(mypeer,pubsock,"BTC","test",""); LP_privkey_init(mypeer,pubsock,"KMD","test",""); printf("utxos.(%s)\n",LP_utxos(mypeer,"",10000)); while ( 1 ) From 2049ed933f43a648d58ef5d9a4692fdacabfd93f Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:36:45 +0300 Subject: [PATCH 016/339] Test --- iguana/exchanges/LP_nativeDEX.c | 3 +++ iguana/exchanges/LP_rpc.c | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 3ccad5825..c00db99ed 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -765,6 +765,9 @@ void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin) exit(-1); } LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); LP_coinfind("USD"); LP_coinfind("REVS"); LP_coinfind("JUMBLR"); + printf("%s\n",jprint(LP_getinfo("BTC"),1)); + printf("%s\n",jprint(LP_getinfo("KMD"),1)); + LP_privkey_init(mypeer,pubsock,"BTC","test",""); LP_privkey_init(mypeer,pubsock,"KMD","test",""); printf("utxos.(%s)\n",LP_utxos(mypeer,"",10000)); diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 604e58aa0..ddb156dee 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -70,6 +70,12 @@ void LP_unspents_mark(char *symbol,cJSON *vins) printf("LOCK (%s)\n",jprint(vins,0)); } +cJSON *LP_getinfo(char *symbol) +{ + struct iguana_info *coin = LP_coinfind(symbol); + return(bitcoin_json(coin,"getinfo","[]")); +} + cJSON *LP_gettxout(char *symbol,bits256 txid,int32_t vout) { char buf[128],str[65]; struct iguana_info *coin = LP_coinfind(symbol); From 80de82f18d460f2f2f2126968cebdd35eaa80e3e Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:50:13 +0300 Subject: [PATCH 017/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index c00db99ed..249e17411 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -574,7 +574,7 @@ uint32_t LP_assetmagic(char *symbol,uint64_t supply) uint16_t LP_assetport(uint32_t magic) { if ( magic == 0x8de4eef9 ) - return(7770); + return(7771); else return(8000 + (magic % 7777)); } @@ -583,7 +583,7 @@ uint16_t LP_port(char *symbol,uint64_t supply,uint32_t *magicp) if ( symbol == 0 || symbol[0] == 0 || strcmp("KMD",symbol) == 0 ) { *magicp = 0x8de4eef9; - return(7770); + return(7771); } else if ( strcmp("BTC",symbol) == 0 ) return(8332); From 4d7e7a2b8640da5eaf838c856044ed804fd947d3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:57:22 +0300 Subject: [PATCH 018/339] Test --- crypto777/bitcoind_RPC.c | 2 +- iguana/exchanges/LP_nativeDEX.c | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/crypto777/bitcoind_RPC.c b/crypto777/bitcoind_RPC.c index 02c8e71d7..47e4e6d45 100755 --- a/crypto777/bitcoind_RPC.c +++ b/crypto777/bitcoind_RPC.c @@ -137,7 +137,7 @@ char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char * if ( url[0] == 0 ) strcpy(url,"http://127.0.0.1:7776"); //if ( specialcase != 0 && (0) ) - printf("<<<<<<<<<<< bitcoind_RPC: debug.(%s) url.(%s) command.(%s) params.(%s)\n",debugstr,url,command,params); + printf("<<<<<<<<<<< bitcoind_RPC: userpass.(%s) url.(%s) command.(%s) params.(%s)\n",userpass,url,command,params); try_again: if ( retstrp != 0 ) *retstrp = 0; diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 249e17411..ddb5eb343 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -578,7 +578,7 @@ uint16_t LP_assetport(uint32_t magic) else return(8000 + (magic % 7777)); } -uint16_t LP_port(char *symbol,uint64_t supply,uint32_t *magicp) +uint16_t LP_rpcport(char *symbol,uint64_t supply,uint32_t *magicp) { if ( symbol == 0 || symbol[0] == 0 || strcmp("KMD",symbol) == 0 ) { @@ -590,7 +590,7 @@ uint16_t LP_port(char *symbol,uint64_t supply,uint32_t *magicp) else if ( strcmp("LTC",symbol) == 0 ) return(9332); *magicp = LP_assetmagic(symbol,supply); - return(LP_assetport(*magicp)); + return(LP_assetport(*magicp)+1); } struct iguana_info *LP_coinfind(char *symbol) @@ -602,7 +602,7 @@ struct iguana_info *LP_coinfind(char *symbol) return(&LP_coins[i]); coin = &cdata; safecopy(cdata.symbol,symbol,sizeof(cdata.symbol)); - port = LP_port(symbol,10,&magic); + port = LP_rpcport(symbol,10,&magic); sprintf(cdata.serverport,"127.0.0.1:%u",port); cdata.longestchain = 100000; cdata.txfee = 10000; @@ -759,6 +759,8 @@ void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin) printf("couldnt get myipaddr or null mypeer.%p\n",mypeer); exit(-1); } +//curl --url "bitcoing54g44rpc:passgh6456h5word@127.0.0.1:8332" --data "{\"method\":\"getinfo\",\"params\":[]}" +// curl --url "bitcoinrp456454z4zhhc:passwg45hgz64g499ord@127.0.0.1:7771" --data "{\"method\":\"getinfo\",\"params\":[]}" if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&myport) != 0 ) { printf("error launching stats rpcloop for port.%u\n",myport); From 6f334dfa7bb388c600c1e4d48a94b71287a2faa5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 13:59:58 +0300 Subject: [PATCH 019/339] Test --- iguana/exchanges/LP_rpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index ddb156dee..f688c6c7a 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -52,8 +52,8 @@ cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params) { printf("issue.(%s, %s, %s, %s, %s)\n",coin->symbol,coin->serverport,coin->userpass,method,params); //retstr = curl_post(&cHandle,coin->serverport,coin->userpass,params,method,0,0,0); - //retstr = bitcoind_passthru(coin->symbol,coin->serverport,coin->userpass,method,params); - retstr = bitcoind_RPC(0,"",coin->serverport,0,method,params); + retstr = bitcoind_passthru(coin->symbol,coin->serverport,coin->userpass,method,params); + //retstr = bitcoind_RPC(0,"",coin->serverport,0,method,params); if ( retstr != 0 && retstr[0] != 0 ) { printf("%s: %s.%s -> (%s)\n",coin->symbol,method,params,retstr); From 53d31b871d477ce7edb80da2b77ef57dc57b9bb0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:00:42 +0300 Subject: [PATCH 020/339] Test --- crypto777/bitcoind_RPC.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto777/bitcoind_RPC.c b/crypto777/bitcoind_RPC.c index 47e4e6d45..975208ddd 100755 --- a/crypto777/bitcoind_RPC.c +++ b/crypto777/bitcoind_RPC.c @@ -136,7 +136,7 @@ char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char * else specialcase = 0; if ( url[0] == 0 ) strcpy(url,"http://127.0.0.1:7776"); - //if ( specialcase != 0 && (0) ) + if ( specialcase != 0 && (0) ) printf("<<<<<<<<<<< bitcoind_RPC: userpass.(%s) url.(%s) command.(%s) params.(%s)\n",userpass,url,command,params); try_again: if ( retstrp != 0 ) From d302c018de0a22cd04b9b16e8b5a13bfdc6cdf32 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:02:59 +0300 Subject: [PATCH 021/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index ddb5eb343..f033c3950 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -600,6 +600,7 @@ struct iguana_info *LP_coinfind(char *symbol) for (i=0; i Date: Mon, 29 May 2017 14:05:22 +0300 Subject: [PATCH 022/339] Test --- iguana/exchanges/LP_nativeDEX.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index f033c3950..ea342563d 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -631,7 +631,7 @@ struct iguana_info *LP_coinfind(char *symbol) cdata.wiftype = 188; LP_userpass(cdata.userpass,symbol,symbol,strcmp(symbol,"KMD") == 0 ? "komodo" : symbol); } - printf("%s: (%s) (%s)\n",symbol,cdata.serverport,cdata.userpass); + //printf("%s: (%s) (%s)\n",symbol,cdata.serverport,cdata.userpass); LP_coins = realloc(LP_coins,sizeof(*LP_coins) * (LP_numcoins+1)); coin = &LP_coins[LP_numcoins++]; *coin = cdata; @@ -650,7 +650,11 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb conv_NXTpassword(privkey.bytes,pubkey.bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); else privkey = iguana_wif2privkey(wifstr); iguana_priv2pub(pubkey33,coinaddr,privkey,coin->pubtype); - printf("%s coinaddr.%s %d\n",symbol,coinaddr,coin->pubtype); + { + char tmpstr[128]; + bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); + printf("%s coinaddr.%s %d wif.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr); + } bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); LP_privkeyadd(privkey,rmd160); if ( (array= LP_listunspent(symbol,coinaddr)) != 0 ) From 80cf618f4dec9e48721b916aadc5452f71bea44c Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:13:27 +0300 Subject: [PATCH 023/339] Test --- iguana/exchanges/LP_nativeDEX.c | 7 ++++--- iguana/exchanges/LP_rpc.c | 3 +++ iguana/exchanges/mm.c | 9 +++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index ea342563d..8a5a53aff 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -654,6 +654,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb char tmpstr[128]; bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); printf("%s coinaddr.%s %d wif.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr); + LP_importaddress(coin->symbol,tmpstr); } bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); LP_privkeyadd(privkey,rmd160); @@ -705,7 +706,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb return(total); } -void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin) +void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin,char *passphrase) { char *myipaddr=0,*retstr; long filesize,n; int32_t len,timeout,maxsize,recvsize,nonz,i,lastn,pullsock=-1,pubsock=-1; struct LP_peerinfo *peer,*tmp,*mypeer=0; char pushaddr[128],subaddr[128]; void *ptr; cJSON *argjson; portable_mutex_init(&LP_peermutex); @@ -772,8 +773,8 @@ void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin) exit(-1); } LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); LP_coinfind("USD"); LP_coinfind("REVS"); LP_coinfind("JUMBLR"); - LP_privkey_init(mypeer,pubsock,"BTC","test",""); - LP_privkey_init(mypeer,pubsock,"KMD","test",""); + LP_privkey_init(mypeer,pubsock,"BTC",passphrase,""); + LP_privkey_init(mypeer,pubsock,"KMD",passphrase,""); printf("utxos.(%s)\n",LP_utxos(mypeer,"",10000)); while ( 1 ) { diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index f688c6c7a..96432470e 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -133,7 +133,10 @@ int32_t LP_importaddress(char *symbol,char *address) return(0); // success sprintf(buf,"[\"%s\", \"%s\", false]",address,address); if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"importaddress",buf)) != 0 ) + { + printf("importaddress.(%s %s) -> (%s)\n",symbol,address,retstr); free(retstr); + } else printf("importaddress.(%s %s)\n",symbol,address); return(1); } diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 814ada65e..f75f708f6 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -798,8 +798,8 @@ void marketmaker(double minask,double maxbid,char *baseaddr,char *reladdr,double void LP_main(void *ptr) { - double profitmargin = *(double *)ptr; - LPinit(7779,7780,7781,profitmargin); + double profitmargin = 0.01; char *passphrase = ptr; + LPinit(7779,7780,7781,profitmargin,passphrase); } int main(int argc, const char * argv[]) @@ -812,7 +812,9 @@ int main(int argc, const char * argv[]) minask = jdouble(retjson,"minask"); maxbid = jdouble(retjson,"maxbid"); profitmargin = jdouble(retjson,"profitmargin"); - if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_main,(void *)&profitmargin) != 0 ) + if ( (passphrase= jstr(retjson,"passphrase")) == 0 ) + passphrase = "test"; + if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_main,(void *)passphrase) != 0 ) { printf("error launching LP_main %f\n",profitmargin); exit(-1); @@ -821,7 +823,6 @@ int main(int argc, const char * argv[]) incrratio = jdouble(retjson,"lotratio"); start_base = jdouble(retjson,"start_base"); start_rel = jdouble(retjson,"start_rel"); - passphrase = jstr(retjson,"passphrase"); apikey = jstr(retjson,"apikey"); apisecret = jstr(retjson,"apisecret"); base = jstr(retjson,"base"); From 54d449ba6b37e8a5a9e0c755d26f5ca819baae6d Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:15:18 +0300 Subject: [PATCH 024/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- iguana/exchanges/LP_rpc.c | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 8a5a53aff..7c677a6f4 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -654,7 +654,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb char tmpstr[128]; bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); printf("%s coinaddr.%s %d wif.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr); - LP_importaddress(coin->symbol,tmpstr); + LP_importprivkey(coin->symbol,tmpstr); } bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); LP_privkeyadd(privkey,rmd160); diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 96432470e..0a2b56888 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -113,6 +113,13 @@ cJSON *LP_validateaddress(char *symbol,char *address) return(bitcoin_json(coin,"validateaddress",buf)); } +cJSON *LP_importprivkey(char *symbol,char *wifstr) +{ + char buf[512]; struct iguana_info *coin = LP_coinfind(symbol); + sprintf(buf,"\"%s\"",wifstr); + return(bitcoin_json(coin,"importprivkey",buf)); +} + int32_t LP_importaddress(char *symbol,char *address) { char buf[1024],*retstr; cJSON *validatejson; int32_t isvalid=0,doneflag = 0; struct iguana_info *coin = LP_coinfind(symbol); From 205e70ed15492719d1f06a8e2ec6362c06934312 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:16:09 +0300 Subject: [PATCH 025/339] Test --- iguana/exchanges/LP_nativeDEX.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 7c677a6f4..1a53a9052 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -640,7 +640,7 @@ struct iguana_info *LP_coinfind(char *symbol) uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symbol,char *passphrase,char *wifstr) { - char coinaddr[64],*script; cJSON *array,*item; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; struct iguana_info *coin = LP_coinfind(symbol); + char coinaddr[64],*script; cJSON *array,*item,*retjson; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; struct iguana_info *coin = LP_coinfind(symbol); if ( coin == 0 ) { printf("cant add privkey for %s, coin not active\n",symbol); @@ -654,7 +654,8 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb char tmpstr[128]; bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); printf("%s coinaddr.%s %d wif.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr); - LP_importprivkey(coin->symbol,tmpstr); + if ( (retjson= LP_importprivkey(coin->symbol,tmpstr)) != 0 ) + printf("importprivkey -> (%s)\n",jprint(retjson,1)); } bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); LP_privkeyadd(privkey,rmd160); From 04dc3a8627a03b7be9b171ab5a67442b88657f8f Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:17:20 +0300 Subject: [PATCH 026/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- iguana/exchanges/LP_rpc.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 1a53a9052..687af36a1 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -654,7 +654,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb char tmpstr[128]; bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); printf("%s coinaddr.%s %d wif.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr); - if ( (retjson= LP_importprivkey(coin->symbol,tmpstr)) != 0 ) + if ( (retjson= LP_importprivkey(coin->symbol,tmpstr,-1)) != 0 ) printf("importprivkey -> (%s)\n",jprint(retjson,1)); } bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 0a2b56888..58370c2d6 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -113,10 +113,10 @@ cJSON *LP_validateaddress(char *symbol,char *address) return(bitcoin_json(coin,"validateaddress",buf)); } -cJSON *LP_importprivkey(char *symbol,char *wifstr) +cJSON *LP_importprivkey(char *symbol,char *wifstr,int32_t flag) { char buf[512]; struct iguana_info *coin = LP_coinfind(symbol); - sprintf(buf,"\"%s\"",wifstr); + sprintf(buf,"[\"%s\", %s]",wifstr,flag < 0 ? "false" : "true"); return(bitcoin_json(coin,"importprivkey",buf)); } From f75ffa9f2c572a671f245722be62572ddf3cf49a Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:22:18 +0300 Subject: [PATCH 027/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- iguana/exchanges/LP_rpc.c | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 687af36a1..6291b7e8e 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -654,7 +654,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb char tmpstr[128]; bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); printf("%s coinaddr.%s %d wif.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr); - if ( (retjson= LP_importprivkey(coin->symbol,tmpstr,-1)) != 0 ) + if ( (retjson= LP_importprivkey(coin->symbol,tmpstr,coinaddr,-1)) != 0 ) printf("importprivkey -> (%s)\n",jprint(retjson,1)); } bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 58370c2d6..6336b6a93 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -46,14 +46,11 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_ cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params) { - //static void *cHandle; char *retstr; cJSON *retjson = 0; if ( coin != 0 ) { - printf("issue.(%s, %s, %s, %s, %s)\n",coin->symbol,coin->serverport,coin->userpass,method,params); - //retstr = curl_post(&cHandle,coin->serverport,coin->userpass,params,method,0,0,0); + //printf("issue.(%s, %s, %s, %s, %s)\n",coin->symbol,coin->serverport,coin->userpass,method,params); retstr = bitcoind_passthru(coin->symbol,coin->serverport,coin->userpass,method,params); - //retstr = bitcoind_RPC(0,"",coin->serverport,0,method,params); if ( retstr != 0 && retstr[0] != 0 ) { printf("%s: %s.%s -> (%s)\n",coin->symbol,method,params,retstr); @@ -113,10 +110,10 @@ cJSON *LP_validateaddress(char *symbol,char *address) return(bitcoin_json(coin,"validateaddress",buf)); } -cJSON *LP_importprivkey(char *symbol,char *wifstr,int32_t flag) +cJSON *LP_importprivkey(char *symbol,char *wifstr,char *label,int32_t flag) { char buf[512]; struct iguana_info *coin = LP_coinfind(symbol); - sprintf(buf,"[\"%s\", %s]",wifstr,flag < 0 ? "false" : "true"); + sprintf(buf,"[\"%s\", \"%s\", %s]",wifstr,label,flag < 0 ? "false" : "true"); return(bitcoin_json(coin,"importprivkey",buf)); } From 6de34cc13ddb7c7f06f8865e901935a1d1cc8a94 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:44:52 +0300 Subject: [PATCH 028/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 6291b7e8e..e97888a76 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -653,7 +653,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb { char tmpstr[128]; bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); - printf("%s coinaddr.%s %d wif.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr); + printf("%s coinaddr.%s %d wif.(%s) passphrase.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr,passphrase); if ( (retjson= LP_importprivkey(coin->symbol,tmpstr,coinaddr,-1)) != 0 ) printf("importprivkey -> (%s)\n",jprint(retjson,1)); } From 3eedaabb82eedca191a6baf3867fed377c0041f4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:47:33 +0300 Subject: [PATCH 029/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 -- iguana/exchanges/mm.c | 5 +++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index e97888a76..a4c3502fb 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -766,8 +766,6 @@ void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin,c printf("couldnt get myipaddr or null mypeer.%p\n",mypeer); exit(-1); } -//curl --url "bitcoing54g44rpc:passgh6456h5word@127.0.0.1:8332" --data "{\"method\":\"getinfo\",\"params\":[]}" -// curl --url "bitcoinrp456454z4zhhc:passwg45hgz64g499ord@127.0.0.1:7771" --data "{\"method\":\"getinfo\",\"params\":[]}" if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&myport) != 0 ) { printf("error launching stats rpcloop for port.%u\n",myport); diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index f75f708f6..3df00e582 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -814,11 +814,12 @@ int main(int argc, const char * argv[]) profitmargin = jdouble(retjson,"profitmargin"); if ( (passphrase= jstr(retjson,"passphrase")) == 0 ) passphrase = "test"; - if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_main,(void *)passphrase) != 0 ) + if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_main,(void *)clonestr(passphrase)) != 0 ) { printf("error launching LP_main %f\n",profitmargin); exit(-1); - } getchar(); + } else printf("launched.(%s)\n",passphrase); +getchar(); maxexposure = jdouble(retjson,"maxexposure"); incrratio = jdouble(retjson,"lotratio"); start_base = jdouble(retjson,"start_base"); From 63109071181b62fe53fddc4fcb360b7b6e002bd6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:48:16 +0300 Subject: [PATCH 030/339] Test --- iguana/exchanges/mm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 3df00e582..99bdb7ada 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -818,7 +818,7 @@ int main(int argc, const char * argv[]) { printf("error launching LP_main %f\n",profitmargin); exit(-1); - } else printf("launched.(%s)\n",passphrase); + } else printf("(%s) launched.(%s)\n",argv[1],passphrase); getchar(); maxexposure = jdouble(retjson,"maxexposure"); incrratio = jdouble(retjson,"lotratio"); From 2edf6668aac848e84a88ced9896d6dd3e5eb1e21 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:52:23 +0300 Subject: [PATCH 031/339] Test --- iguana/exchanges/LP_rpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 6336b6a93..486ffce72 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -53,7 +53,7 @@ cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params) retstr = bitcoind_passthru(coin->symbol,coin->serverport,coin->userpass,method,params); if ( retstr != 0 && retstr[0] != 0 ) { - printf("%s: %s.%s -> (%s)\n",coin->symbol,method,params,retstr); + //printf("%s: %s.%s -> (%s)\n",coin->symbol,method,params,retstr); retjson = cJSON_Parse(retstr); free(retstr); } From 73b949004233995cc4466b37c005bf35c1ddb1be Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 14:58:18 +0300 Subject: [PATCH 032/339] Test --- iguana/exchanges/LP_nativeDEX.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index a4c3502fb..5e95b876d 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -653,7 +653,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb { char tmpstr[128]; bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); - printf("%s coinaddr.%s %d wif.(%s) passphrase.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr,passphrase); + printf("%s coinaddr.(%s) %d wif.(%s) passphrase.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr,passphrase); if ( (retjson= LP_importprivkey(coin->symbol,tmpstr,coinaddr,-1)) != 0 ) printf("importprivkey -> (%s)\n",jprint(retjson,1)); } @@ -710,12 +710,14 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin,char *passphrase) { char *myipaddr=0,*retstr; long filesize,n; int32_t len,timeout,maxsize,recvsize,nonz,i,lastn,pullsock=-1,pubsock=-1; struct LP_peerinfo *peer,*tmp,*mypeer=0; char pushaddr[128],subaddr[128]; void *ptr; cJSON *argjson; + OS_randombytes((void *)&n,sizeof(n)); + srand((int32_t)n); portable_mutex_init(&LP_peermutex); portable_mutex_init(&LP_utxomutex); portable_mutex_init(&LP_commandmutex); - if ( profitmargin == 0. ) + if ( profitmargin == 0. || profitmargin == 0.01 ) { - profitmargin = 0.01; + profitmargin = 0.01 + (double)(rand() % 100)/10000; printf("default profit margin %f\n",profitmargin); } if ( system("curl -s4 checkip.amazonaws.com > /tmp/myipaddr") == 0 ) From 4d8f779289c99fa0b1345041fa1f69cbf4e2515f Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 17:15:17 +0300 Subject: [PATCH 033/339] Test --- iguana/exchanges/LP_commands.c | 29 ++-- iguana/exchanges/LP_nativeDEX.c | 258 ++++++++++++++++++-------------- iguana/exchanges/mm.c | 20 ++- 3 files changed, 172 insertions(+), 135 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 7db343df6..5c52bdbd0 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -149,21 +149,24 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port { if ( (ipaddr= jstr(argjson,"ipaddr")) != 0 && (argport= juint(argjson,"port")) != 0 ) { - if ( (pushport= juint(argjson,"push")) == 0 ) - pushport = argport + 1; - if ( (subport= juint(argjson,"sub")) == 0 ) - subport = argport + 2; - if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),argport)) != 0 ) + if ( strcmp(ipaddr,"127.0.0.1") != 0 && port >= 1000 ) { - if ( (otherpeers= jint(argjson,"numpeers")) > peer->numpeers ) - peer->numpeers = otherpeers; - if ( (othernumutxos= jint(argjson,"numutxos")) > peer->numutxos ) + if ( (pushport= juint(argjson,"push")) == 0 ) + pushport = argport + 1; + if ( (subport= juint(argjson,"sub")) == 0 ) + subport = argport + 2; + if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),argport)) != 0 ) { - printf("change.(%s) numutxos.%d -> %d mynumutxos.%d\n",peer->ipaddr,peer->numutxos,othernumutxos,LP_mypeer->numutxos); - peer->numutxos = othernumutxos; - } - //printf("peer.(%s) found (%d %d) (%d %d) (%s)\n",peer->ipaddr,peer->numpeers,peer->numutxos,otherpeers,othernumutxos,jprint(argjson,0)); - } else LP_addpeer(LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos")); + if ( (otherpeers= jint(argjson,"numpeers")) > peer->numpeers ) + peer->numpeers = otherpeers; + if ( (othernumutxos= jint(argjson,"numutxos")) > peer->numutxos ) + { + printf("change.(%s) numutxos.%d -> %d mynumutxos.%d\n",peer->ipaddr,peer->numutxos,othernumutxos,LP_mypeer->numutxos); + peer->numutxos = othernumutxos; + } + //printf("peer.(%s) found (%d %d) (%d %d) (%s)\n",peer->ipaddr,peer->numpeers,peer->numutxos,otherpeers,othernumutxos,jprint(argjson,0)); + } else LP_addpeer(LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos")); + } if ( strcmp(method,"getpeers") == 0 ) retstr = LP_peers(); else if ( strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 5e95b876d..cde7e1707 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -257,12 +257,14 @@ int32_t LP_peersparse(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipa subport = argport + 2; argipbits = (uint32_t)calc_ipbits(argipaddr); if ( (peer= LP_peerfind(argipbits,argport)) == 0 ) - peer = LP_addpeer(mypeer,mypubsock,argipaddr,argport,pushport,subport,jdouble(item,"profit"),jint(item,"numpeers"),jint(item,"numutxos")); - if ( peer != 0 ) { - peer->lasttime = now; - if ( strcmp(argipaddr,destipaddr) == 0 && destport == argport && peer->numpeers < n ) - peer->numpeers = n; + peer = LP_addpeer(mypeer,mypubsock,argipaddr,argport,pushport,subport,jdouble(item,"profit"),jint(item,"numpeers"),jint(item,"numutxos")); + if ( peer != 0 ) + { + peer->lasttime = now; + if ( strcmp(argipaddr,destipaddr) == 0 && destport == argport && peer->numpeers < n ) + peer->numpeers = n; + } } } } @@ -351,24 +353,27 @@ void LP_peersquery(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr peer = LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport); if ( peer != 0 && peer->errors > 0 ) return; - if ( (retstr= issue_LP_getpeers(destipaddr,destport,myipaddr,myport,myprofit,mypeer->numpeers,mypeer->numutxos)) != 0 ) + if ( (retstr= issue_LP_getpeers(destipaddr,destport,myipaddr,myport,myprofit,mypeer!=0?mypeer->numpeers:0,mypeer!=0?mypeer->numutxos:0)) != 0 ) { //printf("got.(%s)\n",retstr); now = (uint32_t)time(NULL); LP_peersparse(mypeer,mypubsock,destipaddr,destport,retstr,now); free(retstr); - HASH_ITER(hh,LP_peerinfos,peer,tmp) + if ( mypeer != 0 ) { - if ( peer->lasttime != now ) + HASH_ITER(hh,LP_peerinfos,peer,tmp) { - printf("{%s:%u %.6f} ",peer->ipaddr,peer->port,peer->profitmargin); - flag++; - if ( (retstr= issue_LP_notify(destipaddr,destport,peer->ipaddr,peer->port,peer->profitmargin,peer->numpeers,0)) != 0 ) - free(retstr); + if ( peer->lasttime != now ) + { + printf("{%s:%u %.6f} ",peer->ipaddr,peer->port,peer->profitmargin); + flag++; + if ( (retstr= issue_LP_notify(destipaddr,destport,peer->ipaddr,peer->port,peer->profitmargin,peer->numpeers,0)) != 0 ) + free(retstr); + } } + if ( flag != 0 ) + printf(" <- missing peers\n"); } - if ( flag != 0 ) - printf(" <- missing peers\n"); } else if ( peer != 0 ) peer->errors++; } @@ -638,7 +643,7 @@ struct iguana_info *LP_coinfind(char *symbol) return(coin); } -uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symbol,char *passphrase,char *wifstr) +uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symbol,char *passphrase,char *wifstr,int32_t amclient) { char coinaddr[64],*script; cJSON *array,*item,*retjson; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; struct iguana_info *coin = LP_coinfind(symbol); if ( coin == 0 ) @@ -659,7 +664,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb } bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); LP_privkeyadd(privkey,rmd160); - if ( (array= LP_listunspent(symbol,coinaddr)) != 0 ) + if ( amclient == 0 && (array= LP_listunspent(symbol,coinaddr)) != 0 ) { if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) { @@ -707,132 +712,157 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb return(total); } -void LPinit(uint16_t myport,uint16_t mypull,uint16_t mypub,double profitmargin,char *passphrase) +void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,int32_t pullsock,uint16_t myport,int32_t amclient,char *passphrase,double profitmargin) { - char *myipaddr=0,*retstr; long filesize,n; int32_t len,timeout,maxsize,recvsize,nonz,i,lastn,pullsock=-1,pubsock=-1; struct LP_peerinfo *peer,*tmp,*mypeer=0; char pushaddr[128],subaddr[128]; void *ptr; cJSON *argjson; - OS_randombytes((void *)&n,sizeof(n)); - srand((int32_t)n); - portable_mutex_init(&LP_peermutex); - portable_mutex_init(&LP_utxomutex); - portable_mutex_init(&LP_commandmutex); - if ( profitmargin == 0. || profitmargin == 0.01 ) - { - profitmargin = 0.01 + (double)(rand() % 100)/10000; - printf("default profit margin %f\n",profitmargin); - } - if ( system("curl -s4 checkip.amazonaws.com > /tmp/myipaddr") == 0 ) + char *retstr; int32_t i,len,recvsize,nonz,lastn; struct LP_peerinfo *peer,*tmp; void *ptr; cJSON *argjson; + for (i=0; i>>>>>>>> myipaddr.%s (%s %s)\n",myipaddr,pushaddr,subaddr); - if ( (pullsock= nn_socket(AF_SP,NN_PULL)) >= 0 && (pubsock= nn_socket(AF_SP,NN_PUB)) >= 0 ) - { - if ( nn_bind(pullsock,pushaddr) >= 0 && nn_bind(pubsock,subaddr) >= 0 ) - { - timeout = 10; - nn_setsockopt(pubsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); - timeout = 1; - nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); - timeout = 1; - maxsize = 1024 * 1024; - nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVBUF,&maxsize,sizeof(maxsize)); - } - else - { - printf("error binding to (%s).%d (%s).%d\n",pushaddr,pullsock,subaddr,pubsock); - if ( pullsock >= 0 ) - nn_close(pullsock), pullsock = -1; - if ( pubsock >= 0 ) - nn_close(pubsock), pubsock = -1; - } - } else printf("error getting sockets %d %d\n",pullsock,pubsock); - LP_mypubsock = pubsock; - LP_mypeer = mypeer = LP_addpeer(mypeer,pubsock,myipaddr,myport,0,0,profitmargin,0,0); - //printf("my ipaddr.(%s) peers.(%s)\n",ipaddr,retstr!=0?retstr:""); - for (i=0; i 25 ) - continue; - LP_peersquery(mypeer,pubsock,default_LPnodes[i],myport,myipaddr,myport,profitmargin); - } - } else printf("error getting myipaddr\n"); - } else printf("error issuing curl\n"); - if ( myipaddr == 0 || mypeer == 0 ) - { - printf("couldnt get myipaddr or null mypeer.%p\n",mypeer); - exit(-1); + if ( amclient == 0 && (rand() % 100) > 25 ) + continue; + LP_peersquery(mypeer,pubsock,default_LPnodes[i],myport,mypeer!=0?mypeer->ipaddr:"127.0.0.1",myport,profitmargin); } if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&myport) != 0 ) { printf("error launching stats rpcloop for port.%u\n",myport); exit(-1); } - LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); LP_coinfind("USD"); LP_coinfind("REVS"); LP_coinfind("JUMBLR"); - LP_privkey_init(mypeer,pubsock,"BTC",passphrase,""); - LP_privkey_init(mypeer,pubsock,"KMD",passphrase,""); - printf("utxos.(%s)\n",LP_utxos(mypeer,"",10000)); - while ( 1 ) + LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); + LP_coinfind("USD"); LP_coinfind("REVS"); LP_coinfind("JUMBLR"); + if ( amclient != 0 ) { - nonz = 0; HASH_ITER(hh,LP_peerinfos,peer,tmp) { - if ( peer->numpeers != mypeer->numpeers || (rand() % 10) == 0 ) - { - if ( peer->numpeers != mypeer->numpeers ) - printf("%s num.%d vs %d\n",peer->ipaddr,peer->numpeers,mypeer->numpeers); - if ( strcmp(peer->ipaddr,myipaddr) != 0 ) - LP_peersquery(mypeer,pubsock,peer->ipaddr,peer->port,myipaddr,myport,profitmargin); - } - if ( peer->numutxos != mypeer->numutxos ) + printf("%s:%u ",peer->ipaddr,peer->port); + } + printf("peers\n"); + while ( 1 ) + { + sleep(1); + } + } + else + { + LP_privkey_init(mypeer,pubsock,"BTC",passphrase,"",amclient); + LP_privkey_init(mypeer,pubsock,"KMD",passphrase,"",amclient); + while ( 1 ) + { + nonz = 0; + HASH_ITER(hh,LP_peerinfos,peer,tmp) { - lastn = peer->numutxos - mypeer->numutxos + LP_PROPAGATION_SLACK; - if ( lastn < 0 ) - lastn = LP_PROPAGATION_SLACK * 2; - printf("%s numutxos.%d vs %d lastn.%d\n",peer->ipaddr,peer->numutxos,mypeer->numutxos,lastn); - if ( strcmp(peer->ipaddr,myipaddr) != 0 ) - LP_utxosquery(mypeer,pubsock,peer->ipaddr,peer->port,"",lastn,myipaddr,myport,profitmargin); + if ( peer->numpeers != mypeer->numpeers || (rand() % 10) == 0 ) + { + if ( peer->numpeers != mypeer->numpeers ) + printf("%s num.%d vs %d\n",peer->ipaddr,peer->numpeers,mypeer->numpeers); + if ( strcmp(peer->ipaddr,mypeer->ipaddr) != 0 ) + LP_peersquery(mypeer,pubsock,peer->ipaddr,peer->port,mypeer->ipaddr,myport,profitmargin); + } + if ( peer->numutxos != mypeer->numutxos ) + { + lastn = peer->numutxos - mypeer->numutxos + LP_PROPAGATION_SLACK; + if ( lastn < 0 ) + lastn = LP_PROPAGATION_SLACK * 2; + printf("%s numutxos.%d vs %d lastn.%d\n",peer->ipaddr,peer->numutxos,mypeer->numutxos,lastn); + if ( strcmp(peer->ipaddr,mypeer->ipaddr) != 0 ) + LP_utxosquery(mypeer,pubsock,peer->ipaddr,peer->port,"",lastn,mypeer->ipaddr,myport,profitmargin); + } + while ( peer->subsock >= 0 && (recvsize= nn_recv(peer->subsock,&ptr,NN_MSG,0)) >= 0 ) + { + nonz++; + if ( (argjson= cJSON_Parse((char *)ptr)) != 0 ) + { + portable_mutex_lock(&LP_commandmutex); + if ( (retstr= stats_JSON(argjson,"127.0.0.1",mypubport)) != 0 ) + { + printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); + free(retstr); + } + portable_mutex_unlock(&LP_commandmutex); + free_json(argjson); + } else printf("error parsing.(%s)\n",(char *)ptr); + if ( ptr != 0 ) + nn_freemsg(ptr), ptr = 0; + } } - while ( peer->subsock >= 0 && (recvsize= nn_recv(peer->subsock,&ptr,NN_MSG,0)) >= 0 ) + while ( pullsock >= 0 && (recvsize= nn_recv(pullsock,&ptr,NN_MSG,0)) >= 0 ) { nonz++; if ( (argjson= cJSON_Parse((char *)ptr)) != 0 ) { + len = (int32_t)strlen((char *)ptr) + 1; portable_mutex_lock(&LP_commandmutex); - if ( (retstr= stats_JSON(argjson,"127.0.0.1",mypub)) != 0 ) - { - printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); - free(retstr); - } + LP_command(mypeer,pubsock,argjson,&((uint8_t *)ptr)[len],recvsize - len,profitmargin); portable_mutex_unlock(&LP_commandmutex); free_json(argjson); - } else printf("error parsing.(%s)\n",(char *)ptr); + } if ( ptr != 0 ) nn_freemsg(ptr), ptr = 0; } + if ( nonz == 0 ) + sleep(mypeer->numpeers + 1); + } + } +} + +void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profitmargin,char *passphrase,int32_t amclient) +{ + char *myipaddr=0; long filesize,n; int32_t timeout,maxsize,pullsock=-1,pubsock=-1; struct LP_peerinfo *mypeer=0; char pushaddr[128],subaddr[128]; + OS_randombytes((void *)&n,sizeof(n)); + srand((int32_t)n); + portable_mutex_init(&LP_peermutex); + portable_mutex_init(&LP_utxomutex); + portable_mutex_init(&LP_commandmutex); + if ( amclient == 0 ) + { + if ( profitmargin == 0. || profitmargin == 0.01 ) + { + profitmargin = 0.01 + (double)(rand() % 100)/100000; + printf("default profit margin %f\n",profitmargin); } - while ( pullsock >= 0 && (recvsize= nn_recv(pullsock,&ptr,NN_MSG,0)) >= 0 ) + if ( system("curl -s4 checkip.amazonaws.com > /tmp/myipaddr") == 0 ) { - nonz++; - if ( (argjson= cJSON_Parse((char *)ptr)) != 0 ) + if ( (myipaddr= OS_filestr(&filesize,"/tmp/myipaddr")) != 0 && myipaddr[0] != 0 ) { - len = (int32_t)strlen((char *)ptr) + 1; - portable_mutex_lock(&LP_commandmutex); - LP_command(mypeer,mypub,argjson,&((uint8_t *)ptr)[len],recvsize - len,profitmargin); - portable_mutex_unlock(&LP_commandmutex); - free_json(argjson); - } - if ( ptr != 0 ) - nn_freemsg(ptr), ptr = 0; + n = strlen(myipaddr); + if ( myipaddr[n-1] == '\n' ) + myipaddr[--n] = 0; + pullsock = pubsock = -1; + nanomsg_tcpname(pushaddr,myipaddr,mypullport); + nanomsg_tcpname(subaddr,myipaddr,mypubport); + printf(">>>>>>>>> myipaddr.%s (%s %s)\n",myipaddr,pushaddr,subaddr); + if ( (pullsock= nn_socket(AF_SP,NN_PULL)) >= 0 && (pubsock= nn_socket(AF_SP,NN_PUB)) >= 0 ) + { + if ( nn_bind(pullsock,pushaddr) >= 0 && nn_bind(pubsock,subaddr) >= 0 ) + { + timeout = 10; + nn_setsockopt(pubsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); + timeout = 1; + nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); + timeout = 1; + maxsize = 1024 * 1024; + nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVBUF,&maxsize,sizeof(maxsize)); + } + else + { + printf("error binding to (%s).%d (%s).%d\n",pushaddr,pullsock,subaddr,pubsock); + if ( pullsock >= 0 ) + nn_close(pullsock), pullsock = -1; + if ( pubsock >= 0 ) + nn_close(pubsock), pubsock = -1; + } + } else printf("error getting sockets %d %d\n",pullsock,pubsock); + LP_mypubsock = pubsock; + LP_mypeer = mypeer = LP_addpeer(mypeer,pubsock,myipaddr,myport,0,0,profitmargin,0,0); + //printf("my ipaddr.(%s) peers.(%s)\n",ipaddr,retstr!=0?retstr:""); + } else printf("error getting myipaddr\n"); + } else printf("error issuing curl\n"); + if ( myipaddr == 0 || mypeer == 0 ) + { + printf("couldnt get myipaddr or null mypeer.%p\n",mypeer); + exit(-1); } - if ( nonz == 0 ) - sleep(mypeer->numpeers + 1); + printf("utxos.(%s)\n",LP_utxos(mypeer,"",10000)); } + LP_mainloop(mypeer,mypubport,pubsock,pullsock,myport,amclient,passphrase,profitmargin); } /*#ifdef __APPLE__ diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 99bdb7ada..1ffef2e92 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -798,8 +798,12 @@ void marketmaker(double minask,double maxbid,char *baseaddr,char *reladdr,double void LP_main(void *ptr) { - double profitmargin = 0.01; char *passphrase = ptr; - LPinit(7779,7780,7781,profitmargin,passphrase); + char *passphrase; double profitmargin; cJSON *argjson = ptr; + if ( (passphrase= jstr(argjson,"passphrase")) != 0 ) + { + profitmargin = jdouble(argjson,"profitmargin"); + LPinit(7779,7780,7781,profitmargin,passphrase,jint(argjson,"client")); + } } int main(int argc, const char * argv[]) @@ -809,17 +813,17 @@ int main(int argc, const char * argv[]) cJSON *retjson,*loginjson; int32_t i; if ( argc > 1 && (retjson= cJSON_Parse(argv[1])) != 0 ) { - minask = jdouble(retjson,"minask"); - maxbid = jdouble(retjson,"maxbid"); - profitmargin = jdouble(retjson,"profitmargin"); if ( (passphrase= jstr(retjson,"passphrase")) == 0 ) - passphrase = "test"; - if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_main,(void *)clonestr(passphrase)) != 0 ) + jaddstr(retjson,"passphrase","test"); + if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_main,(void *)retjson) != 0 ) { - printf("error launching LP_main %f\n",profitmargin); + printf("error launching LP_main (%s)\n",jprint(retjson,0)); exit(-1); } else printf("(%s) launched.(%s)\n",argv[1],passphrase); getchar(); + profitmargin = jdouble(retjson,"profitmargin"); + minask = jdouble(retjson,"minask"); + maxbid = jdouble(retjson,"maxbid"); maxexposure = jdouble(retjson,"maxexposure"); incrratio = jdouble(retjson,"lotratio"); start_base = jdouble(retjson,"start_base"); From 4df9c9dc08d5530f160489ce0280cdbc30440995 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 17:23:22 +0300 Subject: [PATCH 034/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index cde7e1707..5de30c593 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -728,6 +728,8 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i } LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); LP_coinfind("USD"); LP_coinfind("REVS"); LP_coinfind("JUMBLR"); + LP_privkey_init(mypeer,pubsock,"BTC",passphrase,"",amclient); + LP_privkey_init(mypeer,pubsock,"KMD",passphrase,"",amclient); if ( amclient != 0 ) { HASH_ITER(hh,LP_peerinfos,peer,tmp) @@ -742,8 +744,6 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i } else { - LP_privkey_init(mypeer,pubsock,"BTC",passphrase,"",amclient); - LP_privkey_init(mypeer,pubsock,"KMD",passphrase,"",amclient); while ( 1 ) { nonz = 0; From c69bcde1f32dd937b4b0a4acec8c6f248422f387 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 17:54:12 +0300 Subject: [PATCH 035/339] Test --- iguana/exchanges/LP_commands.c | 7 +++--- iguana/exchanges/LP_nativeDEX.c | 43 +++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 5c52bdbd0..21eeb0a14 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -142,7 +142,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port { - char *method,*ipaddr,*coin,*retstr = 0; uint16_t argport,pushport,subport; int32_t otherpeers,othernumutxos; struct LP_peerinfo *peer; cJSON *retjson; + char *method,*ipaddr,*coin,*retstr = 0; uint16_t argport,pushport,subport; int32_t amclient,otherpeers,othernumutxos; struct LP_peerinfo *peer; cJSON *retjson; if ( (method= jstr(argjson,"method")) == 0 ) return(clonestr("{\"error\":\"need method in request\"}")); else @@ -166,7 +166,8 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } //printf("peer.(%s) found (%d %d) (%d %d) (%s)\n",peer->ipaddr,peer->numpeers,peer->numutxos,otherpeers,othernumutxos,jprint(argjson,0)); } else LP_addpeer(LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos")); - } + amclient = 0; + } else amclient = 1; if ( strcmp(method,"getpeers") == 0 ) retstr = LP_peers(); else if ( strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) @@ -176,7 +177,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port else if ( strcmp(method,"notifyutxo") == 0 ) { printf("utxonotify.(%s)\n",jprint(argjson,0)); - LP_addutxo(LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"deposit"),jint(argjson,"dvout"),SATOSHIDEN * jdouble(argjson,"dvalue"),jstr(argjson,"script"),jstr(argjson,"address"),ipaddr,argport,jdouble(argjson,"profit")); + LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"deposit"),jint(argjson,"dvout"),SATOSHIDEN * jdouble(argjson,"dvalue"),jstr(argjson,"script"),jstr(argjson,"address"),ipaddr,argport,jdouble(argjson,"profit")); retstr = clonestr("{\"result\":\"success\",\"notifyutxo\":\"received\"}"); } } else printf("malformed request.(%s)\n",jprint(argjson,0)); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 5de30c593..ea2dd90c2 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -42,11 +42,11 @@ struct LP_peerinfo struct LP_utxoinfo { UT_hash_handle hh; - bits256 txid,deposittxid,otherpubkey; + bits256 txid,deposittxid,feetxid,otherpubkey; void *swap; - uint64_t satoshis,depositsatoshis; + uint64_t satoshis,depositsatoshis,feesatoshis; uint8_t key[sizeof(bits256) + sizeof(int32_t)]; - int32_t vout,depositvout,pair; uint32_t lasttime,errors,swappending; + int32_t vout,depositvout,feevout,pair; uint32_t lasttime,errors,swappending; double profitmargin; char ipaddr[64],coinaddr[64],spendscript[256],coin[16]; uint16_t port; @@ -188,7 +188,7 @@ struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char return(peer); } -struct LP_utxoinfo *LP_addutxo(struct LP_peerinfo *mypeer,int32_t mypubsock,char *coin,bits256 txid,int32_t vout,int64_t satoshis,bits256 deposittxid,int32_t depositvout,int64_t depositsatoshis,char *spendscript,char *coinaddr,char *ipaddr,uint16_t port,double profitmargin) +struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *coin,bits256 txid,int32_t vout,int64_t satoshis,bits256 deposittxid,int32_t depositvout,int64_t depositsatoshis,char *spendscript,char *coinaddr,char *ipaddr,uint16_t port,double profitmargin) { struct LP_utxoinfo *utxo = 0; uint8_t key[sizeof(txid) + sizeof(vout)]; if ( coin == 0 || coin[0] == 0 || spendscript == 0 || spendscript[0] == 0 || coinaddr == 0 || coinaddr[0] == 0 || bits256_nonz(txid) == 0 || bits256_nonz(deposittxid) == 0 || vout < 0 || depositvout < 0 || satoshis <= 0 || depositsatoshis <= 0 ) @@ -219,9 +219,18 @@ struct LP_utxoinfo *LP_addutxo(struct LP_peerinfo *mypeer,int32_t mypubsock,char utxo->txid = txid; utxo->vout = vout; utxo->satoshis = satoshis; - utxo->deposittxid = deposittxid; - utxo->depositvout = depositvout; - utxo->depositsatoshis = depositsatoshis; + if ( amclient == 0 ) + { + utxo->deposittxid = deposittxid; + utxo->depositvout = depositvout; + utxo->depositsatoshis = depositsatoshis; + } + else + { + utxo->feetxid = deposittxid; + utxo->feevout = depositvout; + utxo->feesatoshis = depositsatoshis; + } memcpy(key,txid.bytes,sizeof(txid)); memcpy(&key[sizeof(txid)],&vout,sizeof(vout)); memcpy(utxo->key,key,sizeof(key)); @@ -274,7 +283,7 @@ int32_t LP_peersparse(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipa return(n); } -int32_t LP_utxosparse(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *retstr,uint32_t now) +int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *retstr,uint32_t now) { struct LP_peerinfo *peer,*destpeer; uint32_t argipbits; char *argipaddr; uint16_t argport,pushport,subport; cJSON *array,*item; int32_t i,n=0; bits256 txid; struct LP_utxoinfo *utxo; if ( (array= cJSON_Parse(retstr)) != 0 ) @@ -296,7 +305,7 @@ int32_t LP_utxosparse(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipa if ( jobj(item,"txid") != 0 ) { txid = jbits256(item,"txid"); - utxo = LP_addutxo(mypeer,mypubsock,jstr(item,"coin"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"deposit"),jint(item,"dvout"),SATOSHIDEN * jdouble(item,"dvalue"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); + utxo = LP_addutxo(amclient,mypeer,mypubsock,jstr(item,"coin"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"deposit"),jint(item,"dvout"),SATOSHIDEN * jdouble(item,"dvalue"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); if ( utxo != 0 ) utxo->lasttime = now; } @@ -378,7 +387,7 @@ void LP_peersquery(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr peer->errors++; } -void LP_utxosquery(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *coin,int32_t lastn,char *myipaddr,uint16_t myport,double myprofit) +void LP_utxosquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *coin,int32_t lastn,char *myipaddr,uint16_t myport,double myprofit) { char *retstr; struct LP_utxoinfo *utxo,*tmp; struct LP_peerinfo *peer; int32_t i,firsti; uint32_t now,flag = 0; peer = LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport); @@ -389,7 +398,7 @@ void LP_utxosquery(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr if ( (retstr= issue_LP_getutxos(destipaddr,destport,coin,lastn,myipaddr,myport,myprofit,mypeer->numpeers,mypeer->numutxos)) != 0 ) { now = (uint32_t)time(NULL); - LP_utxosparse(mypeer,mypubsock,destipaddr,destport,retstr,now); + LP_utxosparse(amclient,mypeer,mypubsock,destipaddr,destport,retstr,now); free(retstr); i = 0; if ( lastn >= mypeer->numutxos ) @@ -664,7 +673,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb } bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); LP_privkeyadd(privkey,rmd160); - if ( amclient == 0 && (array= LP_listunspent(symbol,coinaddr)) != 0 ) + if ( (array= LP_listunspent(symbol,coinaddr)) != 0 ) { if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) { @@ -688,7 +697,9 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb script = jstr(item,"scriptPubKey"); depositval = values[i]; values[i] = 0, used++; - targetval = (depositval / 9) * 8; + if ( amclient != 0 ) + targetval = (depositval / 777); + else targetval = (depositval / 9) * 8; printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); if ( (i= LP_nearestvalue(values,n,targetval)) >= 0 ) { @@ -699,7 +710,9 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb { value = values[i]; values[i] = 0, used++; - LP_addutxo(mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin); + if ( amclient == 0 ) + LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin); + else LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,"127.0.0.1",0,0); total += value; } } @@ -763,7 +776,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i lastn = LP_PROPAGATION_SLACK * 2; printf("%s numutxos.%d vs %d lastn.%d\n",peer->ipaddr,peer->numutxos,mypeer->numutxos,lastn); if ( strcmp(peer->ipaddr,mypeer->ipaddr) != 0 ) - LP_utxosquery(mypeer,pubsock,peer->ipaddr,peer->port,"",lastn,mypeer->ipaddr,myport,profitmargin); + LP_utxosquery(0,mypeer,pubsock,peer->ipaddr,peer->port,"",lastn,mypeer->ipaddr,myport,profitmargin); } while ( peer->subsock >= 0 && (recvsize= nn_recv(peer->subsock,&ptr,NN_MSG,0)) >= 0 ) { From 06fd4493f8c4de86937c3674ddeabfdb1d214430 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 17:57:03 +0300 Subject: [PATCH 036/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index ea2dd90c2..aae484341 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -237,13 +237,11 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 portable_mutex_lock(&LP_utxomutex); HASH_ADD(hh,LP_utxoinfos,key,sizeof(key),utxo); if ( mypeer != 0 ) - { mypeer->numutxos++; - printf("%s:%u LP_addutxo.(%.8f %.8f) numutxos.%d\n",ipaddr,port,dstr(satoshis),dstr(depositsatoshis),mypeer->numutxos); - } portable_mutex_unlock(&LP_utxomutex); if ( mypubsock >= 0 ) LP_send(mypubsock,jprint(LP_utxojson(utxo),1),1); + printf("%s:%u LP_addutxo.(%.8f %.8f) numutxos.%d\n",ipaddr,port,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0); } return(utxo); } From 4fa2204136fe828156df7bf43a6dcfa80b62d3f6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:21:32 +0300 Subject: [PATCH 037/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index aae484341..b4c987d8c 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -372,7 +372,7 @@ void LP_peersquery(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr { if ( peer->lasttime != now ) { - printf("{%s:%u %.6f} ",peer->ipaddr,peer->port,peer->profitmargin); + printf("{%s:%u %.6f}.%d ",peer->ipaddr,peer->port,peer->profitmargin,peer->lasttime - now); flag++; if ( (retstr= issue_LP_notify(destipaddr,destport,peer->ipaddr,peer->port,peer->profitmargin,peer->numpeers,0)) != 0 ) free(retstr); From a07fe51ddc34f03a6e1a332ec167c82bc767744a Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:22:22 +0300 Subject: [PATCH 038/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index b4c987d8c..11e21c895 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -362,7 +362,7 @@ void LP_peersquery(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr return; if ( (retstr= issue_LP_getpeers(destipaddr,destport,myipaddr,myport,myprofit,mypeer!=0?mypeer->numpeers:0,mypeer!=0?mypeer->numutxos:0)) != 0 ) { - //printf("got.(%s)\n",retstr); + printf("got.(%s)\n",retstr); now = (uint32_t)time(NULL); LP_peersparse(mypeer,mypubsock,destipaddr,destport,retstr,now); free(retstr); From 33df0bc0d8f3a028672b17074c7ef32dd0cab6b1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:24:26 +0300 Subject: [PATCH 039/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 11e21c895..1d5cb459d 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -273,7 +273,7 @@ int32_t LP_peersparse(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipa peer->numpeers = n; } } - } + } } } free_json(array); From 2b3920992d9d75ae6ef476bc94314bed59ad0474 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:25:30 +0300 Subject: [PATCH 040/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 1d5cb459d..b4c987d8c 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -273,7 +273,7 @@ int32_t LP_peersparse(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipa peer->numpeers = n; } } - } + } } } free_json(array); @@ -362,7 +362,7 @@ void LP_peersquery(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr return; if ( (retstr= issue_LP_getpeers(destipaddr,destport,myipaddr,myport,myprofit,mypeer!=0?mypeer->numpeers:0,mypeer!=0?mypeer->numutxos:0)) != 0 ) { - printf("got.(%s)\n",retstr); + //printf("got.(%s)\n",retstr); now = (uint32_t)time(NULL); LP_peersparse(mypeer,mypubsock,destipaddr,destport,retstr,now); free(retstr); From ab940b986d4d44ec312e511e9b4b14c08c1b7823 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:28:04 +0300 Subject: [PATCH 041/339] Test --- iguana/exchanges/LP_nativeDEX.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index b4c987d8c..4a4d36dd2 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -325,10 +325,12 @@ int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs char *issue_LP_getpeers(char *destip,uint16_t destport,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers,int32_t numutxos) { - char url[512]; + char url[512],*retstr; sprintf(url,"http://%s:%u/api/stats/getpeers?ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,ipaddr,port,profitmargin,numpeers,numutxos); //printf("send.(%s)\n",url); - return(issue_curl(url)); + retstr = issue_curl(url); + printf("GETPEERS.(%s)\n",retstr); + return(retstr); } char *issue_LP_getutxos(char *destip,uint16_t destport,char *coin,int32_t lastn,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers,int32_t numutxos) From 6bab3468ce90629070e2601f7c62f7e14e3f19d3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:30:57 +0300 Subject: [PATCH 042/339] Test --- iguana/exchanges/LP_nativeDEX.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 4a4d36dd2..d5ae87bcc 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -329,7 +329,7 @@ char *issue_LP_getpeers(char *destip,uint16_t destport,char *ipaddr,uint16_t por sprintf(url,"http://%s:%u/api/stats/getpeers?ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,ipaddr,port,profitmargin,numpeers,numutxos); //printf("send.(%s)\n",url); retstr = issue_curl(url); - printf("GETPEERS.(%s)\n",retstr); + //printf("GETPEERS.(%s)\n",retstr); return(retstr); } @@ -340,6 +340,13 @@ char *issue_LP_getutxos(char *destip,uint16_t destport,char *coin,int32_t lastn, return(issue_curl(url)); } +char *issue_LP_clientgetutxos(char *destip,uint16_t destport,char *coin,int32_t lastn) +{ + char url[512]; + sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d",destip,destport,coin,lastn); + return(issue_curl(url)); +} + char *issue_LP_notify(char *destip,uint16_t destport,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers,int32_t numutxos) { char url[512]; @@ -727,7 +734,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,int32_t pullsock,uint16_t myport,int32_t amclient,char *passphrase,double profitmargin) { - char *retstr; int32_t i,len,recvsize,nonz,lastn; struct LP_peerinfo *peer,*tmp; void *ptr; cJSON *argjson; + char *retstr,*utxostr; int32_t i,len,recvsize,nonz,lastn; struct LP_peerinfo *peer,*tmp; void *ptr; cJSON *argjson; for (i=0; i 25 ) @@ -747,7 +754,8 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i { HASH_ITER(hh,LP_peerinfos,peer,tmp) { - printf("%s:%u ",peer->ipaddr,peer->port); + utxostr = issue_LP_clientgetutxos(peer->ipaddr,peer->port,"KMD",0); + printf("%s:%u %s\n",peer->ipaddr,peer->port,utxostr); } printf("peers\n"); while ( 1 ) From 9cb89eba9c2b5c133a62bb75ebb49a5439b2718c Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:32:54 +0300 Subject: [PATCH 043/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index d5ae87bcc..72ebdeeee 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -336,7 +336,7 @@ char *issue_LP_getpeers(char *destip,uint16_t destport,char *ipaddr,uint16_t por char *issue_LP_getutxos(char *destip,uint16_t destport,char *coin,int32_t lastn,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers,int32_t numutxos) { char url[512]; - sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,coin,lastn,ipaddr,port,profitmargin,numpeers,numutxos); + sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d&ipaddr=127.0.0.1&port=0",destip,destport,coin,lastn,ipaddr,port,profitmargin,numpeers,numutxos); return(issue_curl(url)); } From 24d59fc33c2ea854a2cc8aba53f63f6399c851eb Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:34:18 +0300 Subject: [PATCH 044/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 72ebdeeee..ce53e6842 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -336,14 +336,14 @@ char *issue_LP_getpeers(char *destip,uint16_t destport,char *ipaddr,uint16_t por char *issue_LP_getutxos(char *destip,uint16_t destport,char *coin,int32_t lastn,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers,int32_t numutxos) { char url[512]; - sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d&ipaddr=127.0.0.1&port=0",destip,destport,coin,lastn,ipaddr,port,profitmargin,numpeers,numutxos); + sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,coin,lastn,ipaddr,port,profitmargin,numpeers,numutxos); return(issue_curl(url)); } char *issue_LP_clientgetutxos(char *destip,uint16_t destport,char *coin,int32_t lastn) { char url[512]; - sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d",destip,destport,coin,lastn); + sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=127.0.0.1&port=0",destip,destport,coin,lastn); return(issue_curl(url)); } From 20c9f28d420225974b2b9d45047c0421ac737d60 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:35:20 +0300 Subject: [PATCH 045/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index ce53e6842..fbc34f07c 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -735,7 +735,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,int32_t pullsock,uint16_t myport,int32_t amclient,char *passphrase,double profitmargin) { char *retstr,*utxostr; int32_t i,len,recvsize,nonz,lastn; struct LP_peerinfo *peer,*tmp; void *ptr; cJSON *argjson; - for (i=0; i 25 ) continue; From 78d57ddf890a3be3ed350d4136137d4258cf1b00 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:37:51 +0300 Subject: [PATCH 046/339] Test --- iguana/exchanges/LP_commands.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 21eeb0a14..65a6a6ace 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -147,6 +147,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port return(clonestr("{\"error\":\"need method in request\"}")); else { + amclient = 0; if ( (ipaddr= jstr(argjson,"ipaddr")) != 0 && (argport= juint(argjson,"port")) != 0 ) { if ( strcmp(ipaddr,"127.0.0.1") != 0 && port >= 1000 ) @@ -168,19 +169,19 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } else LP_addpeer(LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos")); amclient = 0; } else amclient = 1; - if ( strcmp(method,"getpeers") == 0 ) - retstr = LP_peers(); - else if ( strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) - retstr = LP_utxos(LP_mypeer,coin,jint(argjson,"lastn")); - else if ( strcmp(method,"notify") == 0 ) - retstr = clonestr("{\"result\":\"success\",\"notify\":\"received\"}"); - else if ( strcmp(method,"notifyutxo") == 0 ) - { - printf("utxonotify.(%s)\n",jprint(argjson,0)); - LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"deposit"),jint(argjson,"dvout"),SATOSHIDEN * jdouble(argjson,"dvalue"),jstr(argjson,"script"),jstr(argjson,"address"),ipaddr,argport,jdouble(argjson,"profit")); - retstr = clonestr("{\"result\":\"success\",\"notifyutxo\":\"received\"}"); - } - } else printf("malformed request.(%s)\n",jprint(argjson,0)); + } + if ( strcmp(method,"getpeers") == 0 ) + retstr = LP_peers(); + else if ( strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) + retstr = LP_utxos(LP_mypeer,coin,jint(argjson,"lastn")); + else if ( strcmp(method,"notify") == 0 ) + retstr = clonestr("{\"result\":\"success\",\"notify\":\"received\"}"); + else if ( strcmp(method,"notifyutxo") == 0 ) + { + printf("utxonotify.(%s)\n",jprint(argjson,0)); + LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"deposit"),jint(argjson,"dvout"),SATOSHIDEN * jdouble(argjson,"dvalue"),jstr(argjson,"script"),jstr(argjson,"address"),jstr(argjson,"ipaddr"),juint(argjson,"port"),jdouble(argjson,"profit")); + retstr = clonestr("{\"result\":\"success\",\"notifyutxo\":\"received\"}"); + } } if ( retstr != 0 ) return(retstr); From b862bcf788f22f8c94dbffe11744d2adb5b0a63c Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:42:05 +0300 Subject: [PATCH 047/339] Test --- iguana/exchanges/LP_nativeDEX.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index fbc34f07c..6f46d0da9 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -264,14 +264,12 @@ int32_t LP_peersparse(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipa subport = argport + 2; argipbits = (uint32_t)calc_ipbits(argipaddr); if ( (peer= LP_peerfind(argipbits,argport)) == 0 ) - { peer = LP_addpeer(mypeer,mypubsock,argipaddr,argport,pushport,subport,jdouble(item,"profit"),jint(item,"numpeers"),jint(item,"numutxos")); - if ( peer != 0 ) - { - peer->lasttime = now; - if ( strcmp(argipaddr,destipaddr) == 0 && destport == argport && peer->numpeers < n ) - peer->numpeers = n; - } + if ( peer != 0 ) + { + peer->lasttime = now; + if ( strcmp(argipaddr,destipaddr) == 0 && destport == argport && peer->numpeers < n ) + peer->numpeers = n; } } } From af51b9bc7cb529977a9bf1c0b11618c6708cd9aa Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 18:46:36 +0300 Subject: [PATCH 048/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 6f46d0da9..396cd2481 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -268,7 +268,7 @@ int32_t LP_peersparse(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipa if ( peer != 0 ) { peer->lasttime = now; - if ( strcmp(argipaddr,destipaddr) == 0 && destport == argport && peer->numpeers < n ) + if ( strcmp(argipaddr,destipaddr) == 0 && destport == argport && peer->numpeers != n ) peer->numpeers = n; } } @@ -752,7 +752,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i { HASH_ITER(hh,LP_peerinfos,peer,tmp) { - utxostr = issue_LP_clientgetutxos(peer->ipaddr,peer->port,"KMD",0); + utxostr = issue_LP_clientgetutxos(peer->ipaddr,peer->port,"KMD",10); printf("%s:%u %s\n",peer->ipaddr,peer->port,utxostr); } printf("peers\n"); From d9fb711d4bcc4d306ef18117e53ab776f0087d14 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:13:39 +0300 Subject: [PATCH 049/339] Test --- iguana/exchanges/LP_nativeDEX.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 396cd2481..7777d1ad8 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -732,7 +732,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,int32_t pullsock,uint16_t myport,int32_t amclient,char *passphrase,double profitmargin) { - char *retstr,*utxostr; int32_t i,len,recvsize,nonz,lastn; struct LP_peerinfo *peer,*tmp; void *ptr; cJSON *argjson; + char *retstr,*utxostr; int32_t i,n,len,recvsize,nonz,lastn; struct LP_peerinfo *peer,*tmp; void *ptr; cJSON *argjson,*array,*reqjson,*item; for (i=amclient; i 25 ) @@ -750,10 +750,37 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i LP_privkey_init(mypeer,pubsock,"KMD",passphrase,"",amclient); if ( amclient != 0 ) { + nonz = 0; HASH_ITER(hh,LP_peerinfos,peer,tmp) { - utxostr = issue_LP_clientgetutxos(peer->ipaddr,peer->port,"KMD",10); - printf("%s:%u %s\n",peer->ipaddr,peer->port,utxostr); + if ( (utxostr= issue_LP_clientgetutxos(peer->ipaddr,peer->port,"KMD",10)) != 0 ) + { + printf("%s:%u %s\n",peer->ipaddr,peer->port,utxostr); + if ( (array= cJSON_Parse(utxostr)) != 0 ) + { + if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) + { + for (i=0; i<1; i++) + { + item = jitem(array,i); + reqjson = cJSON_CreateObject(); + jaddbits256(reqjson,"txid",jbits256(item,"txid")); + jaddnum(reqjson,"vout",jint(item,"vout")); + jaddstr(reqjson,"base",jstr(item,"base")); + jaddstr(reqjson,"rel","BTC"); + jaddstr(reqjson,"method","price"); + //5.9.253.196:7779 [{"ipaddr":"5.9.253.196","port":7779,"profit":0.01064000,"coin":"KMD","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","script":"76a914434009423522682bd7cc1b18a614c3096d19683188ac","txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"value":100,"deposit":"07902a65d11f0f577a0346432bcd2b6b53de5554c314209d1964693962524d69","dvout":1,"dvalue":120}] + + LP_send(peer->pushsock,jprint(reqjson,1),1); + nonz = 1; + } + } + free_json(array); + } + free(utxostr); + } + if ( nonz != 0 ) + break; } printf("peers\n"); while ( 1 ) From aa694c6f09da14ef3b220dc6d1ad4bbbc9c980f1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:15:28 +0300 Subject: [PATCH 050/339] Test --- iguana/exchanges/LP_commands.c | 1 + iguana/exchanges/LP_network.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 65a6a6ace..d9ae10154 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -38,6 +38,7 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { char *method,*base,*rel,*retstr,*pairstr; cJSON *retjson; double price; bits256 srchash,desthash,pubkey,privkey,txid,desttxid; struct LP_utxoinfo *utxo; uint32_t timestamp,quotetime; int32_t destvout,DEXselector = 0; uint64_t txfee,satoshis,desttxfee,destsatoshis,value; struct basilisk_request R; + printf("LP_command.(%s)\n",jprint(argjson,0)); if ( (method= jstr(argjson,"method")) != 0 ) { txid = jbits256(argjson,"txid"); diff --git a/iguana/exchanges/LP_network.c b/iguana/exchanges/LP_network.c index c32ded7f4..bd6855e9d 100644 --- a/iguana/exchanges/LP_network.c +++ b/iguana/exchanges/LP_network.c @@ -115,6 +115,13 @@ char *nanomsg_tcpname(char *str,char *ipaddr,uint16_t port) int32_t LP_send(int32_t sock,char *msg,int32_t freeflag) { int32_t sentbytes,len,i; struct nn_pollfd pfd; + if ( sock < 0 ) + { + printf("LP_send to illegal socket\n"); + if ( freeflag != 0 ) + free(msg); + return(-1); + } for (i=0; i<100; i++) { pfd.fd = sock; @@ -132,6 +139,8 @@ int32_t LP_send(int32_t sock,char *msg,int32_t freeflag) usleep(1000); } printf("error LP_send\n"); + if ( freeflag != 0 ) + free(msg); return(-1); } From 323f473806ca6d2e5e68fdf5b540fe388d93a2a9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:17:19 +0300 Subject: [PATCH 051/339] Test --- iguana/exchanges/LP_commands.c | 1 + iguana/exchanges/LP_nativeDEX.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index d9ae10154..7dc7b3a18 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -39,6 +39,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ { char *method,*base,*rel,*retstr,*pairstr; cJSON *retjson; double price; bits256 srchash,desthash,pubkey,privkey,txid,desttxid; struct LP_utxoinfo *utxo; uint32_t timestamp,quotetime; int32_t destvout,DEXselector = 0; uint64_t txfee,satoshis,desttxfee,destsatoshis,value; struct basilisk_request R; printf("LP_command.(%s)\n",jprint(argjson,0)); + //LP_command.({"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"rel":"BTC","method":"price"}) if ( (method= jstr(argjson,"method")) != 0 ) { txid = jbits256(argjson,"txid"); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 7777d1ad8..94d4af905 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -766,7 +766,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i reqjson = cJSON_CreateObject(); jaddbits256(reqjson,"txid",jbits256(item,"txid")); jaddnum(reqjson,"vout",jint(item,"vout")); - jaddstr(reqjson,"base",jstr(item,"base")); + jaddstr(reqjson,"base",jstr(item,"coin")); jaddstr(reqjson,"rel","BTC"); jaddstr(reqjson,"method","price"); //5.9.253.196:7779 [{"ipaddr":"5.9.253.196","port":7779,"profit":0.01064000,"coin":"KMD","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","script":"76a914434009423522682bd7cc1b18a614c3096d19683188ac","txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"value":100,"deposit":"07902a65d11f0f577a0346432bcd2b6b53de5554c314209d1964693962524d69","dvout":1,"dvalue":120}] From 0b68601ee620b715e54dfd90cf95ed3681adc7de Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:21:04 +0300 Subject: [PATCH 052/339] Test --- iguana/exchanges/LP_commands.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 7dc7b3a18..2c1e8b78e 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -38,13 +38,13 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { char *method,*base,*rel,*retstr,*pairstr; cJSON *retjson; double price; bits256 srchash,desthash,pubkey,privkey,txid,desttxid; struct LP_utxoinfo *utxo; uint32_t timestamp,quotetime; int32_t destvout,DEXselector = 0; uint64_t txfee,satoshis,desttxfee,destsatoshis,value; struct basilisk_request R; - printf("LP_command.(%s)\n",jprint(argjson,0)); - //LP_command.({"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"rel":"BTC","method":"price"}) + //LP_command.({"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"base":"KMD","rel":"BTC","method":"price"}) if ( (method= jstr(argjson,"method")) != 0 ) { txid = jbits256(argjson,"txid"); if ( (utxo= LP_utxofind(txid,jint(argjson,"vout"))) != 0 && strcmp(utxo->ipaddr,mypeer->ipaddr) == 0 && utxo->port == mypeer->port && (base= jstr(argjson,"base")) != 0 && (rel= jstr(argjson,"rel")) != 0 && strcmp(base,utxo->coin) == 0 ) { + printf("LP_command.(%s)\n",jprint(argjson,0)); if ( time(NULL) > utxo->swappending ) utxo->swappending = 0; if ( strcmp(method,"price") == 0 || strcmp(method,"request") == 0 ) @@ -77,8 +77,8 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ } retstr = jprint(retjson,1); LP_send(pubsock,retstr,1); - } - } + } else printf("null price\n"); + } else printf("swappending.%u pair.%d\n",utxo->swappending,utxo->pair); } else if ( strcmp(method,"connect") == 0 ) { From fb775092c228775c2f974bcc5f732970814fc156 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:29:41 +0300 Subject: [PATCH 053/339] Test --- iguana/exchanges/mm.c | 74 +++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 1ffef2e92..4f9d3e0b1 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -680,10 +680,43 @@ int32_t marketmaker_spread(char *exchange,char *base,char *rel,double bid,double return(n); } -void marketmaker(double minask,double maxbid,char *baseaddr,char *reladdr,double start_BASE,double start_REL,double profitmargin,double maxexposure,double ratioincr,char *exchange,char *name,char *base,char *rel) +double marketmaker_updateprice(char *name,char *base,char *rel,double theoretical,double *incrp) { static uint32_t counter; - cJSON *fiatjson; char *retstr; double bid,ask,start_DEXbase,start_DEXrel,USD_average=0.,DEX_base = 0.,DEX_rel = 0.,balance_base=0.,balance_rel=0.,mmbid,mmask,usdprice=0.,CMC_average=0.,aveprice,incr,pendingbids,pendingasks,buyvol,sellvol,bidincr,askincr,filledprice,avebid=0.,aveask=0.,val,changes[3],highbid=0.,lowask=0.,theoretical = 0.; uint32_t lasttime = 0; + cJSON *fiatjson; double USD_average=0.,usdprice=0.,CMC_average=0.,avebid=0.,aveask=0.,val,changes[3],highbid=0.,lowask=0.; + if ( (val= get_theoretical(&avebid,&aveask,&highbid,&lowask,&CMC_average,changes,name,base,rel,&USD_average)) != 0. ) + { + if ( theoretical == 0. ) + { + theoretical = val; + if ( *incrp > 2 ) + { + *incrp = (int32_t)*incrp; + *incrp += 0.777; + } + } else theoretical = (theoretical + val) * 0.5; + if ( (counter++ % 12) == 0 ) + { + if ( USD_average > SMALLVAL && CMC_average > SMALLVAL && theoretical > SMALLVAL ) + { + usdprice = USD_average * (theoretical / CMC_average); + printf("USD %.4f <- (%.6f * (%.8f / %.8f))\n",usdprice,USD_average,theoretical,CMC_average); + PAXPRICES[0] = usdprice; + if ( (fiatjson= yahoo_allcurrencies()) != 0 ) + { + marketmaker_fiatupdate(fiatjson); + free_json(fiatjson); + } + } + } + LP_priceupdate(base,rel,theoretical,avebid,aveask,highbid,lowask,PAXPRICES); + } + return(theoretical); +} + +void marketmaker(double minask,double maxbid,char *baseaddr,char *reladdr,double start_BASE,double start_REL,double profitmargin,double maxexposure,double ratioincr,char *exchange,char *name,char *base,char *rel) +{ + char *retstr; double bid,ask,start_DEXbase,start_DEXrel,DEX_base = 0.,DEX_rel = 0.,balance_base=0.,balance_rel=0.,mmbid,mmask,aveprice,incr,pendingbids,pendingasks,buyvol,sellvol,bidincr,askincr,filledprice,avebid=0.,aveask=0.,highbid=0.,lowask=0.,theoretical = 0.; uint32_t lasttime = 0; incr = maxexposure * ratioincr; buyvol = sellvol = 0.; start_DEXbase = dex_balance(base,baseaddr); @@ -692,34 +725,10 @@ void marketmaker(double minask,double maxbid,char *baseaddr,char *reladdr,double { if ( time(NULL) > lasttime+60 ) { - if ( (val= get_theoretical(&avebid,&aveask,&highbid,&lowask,&CMC_average,changes,name,base,rel,&USD_average)) != 0. ) + if ( (theoretical= marketmaker_updateprice(name,base,rel,theoretical,&incr)) != 0. ) { - if ( theoretical == 0. ) - { - theoretical = val; - incr /= theoretical; + if ( lasttime == 0 ) maxexposure /= theoretical; - if ( incr > 2 ) - { - incr = (int32_t)incr; - incr += 0.777; - } - } else theoretical = (theoretical + val) * 0.5; - if ( (counter++ % 12) == 0 ) - { - if ( USD_average > SMALLVAL && CMC_average > SMALLVAL && theoretical > SMALLVAL ) - { - usdprice = USD_average * (theoretical / CMC_average); - printf("USD %.4f <- (%.6f * (%.8f / %.8f))\n",usdprice,USD_average,theoretical,CMC_average); - PAXPRICES[0] = usdprice; - if ( (fiatjson= yahoo_allcurrencies()) != 0 ) - { - marketmaker_fiatupdate(fiatjson); - free_json(fiatjson); - } - } - } - LP_priceupdate(base,rel,theoretical,avebid,aveask,highbid,lowask,PAXPRICES); } if ( strcmp(exchange,"bittrex") == 0 ) { @@ -809,7 +818,7 @@ void LP_main(void *ptr) int main(int argc, const char * argv[]) { char *base,*rel,*name,*exchange,*apikey,*apisecret,*blocktrail,*retstr,*baseaddr,*reladdr,*passphrase; - double profitmargin,maxexposure,incrratio,start_rel,start_base,minask,maxbid; + double profitmargin,maxexposure,incrratio,start_rel,start_base,minask,maxbid,incr,theoretical = 0.; cJSON *retjson,*loginjson; int32_t i; if ( argc > 1 && (retjson= cJSON_Parse(argv[1])) != 0 ) { @@ -820,7 +829,12 @@ int main(int argc, const char * argv[]) printf("error launching LP_main (%s)\n",jprint(retjson,0)); exit(-1); } else printf("(%s) launched.(%s)\n",argv[1],passphrase); -getchar(); + incr = 100.; + while ( 1 ) + { + theoretical = marketmaker_updateprice("komodo","KMD","BTC",theoretical,&incr); + sleep(30); + } profitmargin = jdouble(retjson,"profitmargin"); minask = jdouble(retjson,"minask"); maxbid = jdouble(retjson,"maxbid"); From 34ca17cc9fdf131e6b3529546c7d4f99dd62bb43 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:37:21 +0300 Subject: [PATCH 054/339] Test --- iguana/exchanges/LP_commands.c | 11 ++++++----- iguana/exchanges/LP_include.h | 3 +++ iguana/exchanges/LP_nativeDEX.c | 3 +++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 2c1e8b78e..a8f56afb0 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -64,13 +64,14 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ jaddbits256(retjson,"txid",txid); pubkey = LP_pubkey(LP_privkey(utxo->coinaddr)); jaddbits256(retjson,"srchash",pubkey); - txfee = LP_txfee(base); + if ( (txfee= LP_getestimatedrate(base)*LP_AVETXSIZE) < 10000 ) + txfee = 10000; jadd64bits(retjson,"txfee",txfee); jadd64bits(retjson,"satoshis",utxo->satoshis - txfee); jadd64bits(retjson,"destsatoshis",price * (utxo->satoshis-txfee)); if ( strcmp(method,"request") == 0 ) { - utxo->swappending = (uint32_t)(time(NULL) + 60); + utxo->swappending = (uint32_t)(time(NULL) + LP_RESERVETIME); utxo->otherpubkey = jbits256(argjson,"pubkey"); jaddstr(retjson,"result","reserved"); jaddnum(retjson,"pending",utxo->swappending); @@ -87,9 +88,9 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ if ( (price= LP_price(base,rel)) != 0. ) { price *= (1. + profitmargin); - txfee = LP_txfee(base); + txfee = j64bits(argjson,"txfee"); satoshis = j64bits(argjson,"satoshis"); - desttxfee = LP_txfee(rel); + desttxfee = LP_getestimatedrate(rel) * LP_AVETXSIZE; desttxid = jbits256(argjson,"desttxid"); destvout = jint(argjson,"destvout"); timestamp = juint(argjson,"timestamp"); @@ -98,7 +99,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ pubkey = LP_pubkey(privkey); srchash = jbits256(argjson,"srchash"); value = j64bits(argjson,"destsatoshis"); - if ( timestamp == utxo->swappending-60 && quotetime >= timestamp && quotetime < utxo->swappending && bits256_cmp(pubkey,srchash) == 0 && (destsatoshis= LP_txvalue(rel,desttxid,destvout)) > price*(utxo->satoshis-txfee)+desttxfee && value <= destsatoshis-desttxfee ) + if ( timestamp == utxo->swappending-LP_RESERVETIME && quotetime >= timestamp && quotetime < utxo->swappending && bits256_cmp(pubkey,srchash) == 0 && (destsatoshis= LP_txvalue(rel,desttxid,destvout)) > price*(utxo->satoshis-txfee)+desttxfee && value <= destsatoshis-desttxfee ) { destsatoshis = value; if ( (utxo->pair= nn_socket(AF_SP,NN_PAIR)) < 0 ) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index ed0f4fde2..7df5192aa 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -24,6 +24,9 @@ #define BASILISK_DISABLEWAITTX #define BASILISK_DISABLESENDTX +#define LP_RESERVETIME 60 +#define LP_AVETXSIZE 500 + #define BASILISK_DEFAULT_NUMCONFIRMS 5 #define DEX_SLEEP 3 #define BASILISK_DEXDURATION 300 diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 94d4af905..1a5cb048e 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -771,6 +771,9 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i jaddstr(reqjson,"method","price"); //5.9.253.196:7779 [{"ipaddr":"5.9.253.196","port":7779,"profit":0.01064000,"coin":"KMD","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","script":"76a914434009423522682bd7cc1b18a614c3096d19683188ac","txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"value":100,"deposit":"07902a65d11f0f577a0346432bcd2b6b53de5554c314209d1964693962524d69","dvout":1,"dvalue":120}] + LP_send(peer->pushsock,jprint(reqjson,0),1); + jdelete(reqjson,"method"); + jaddstr(reqjson,"method","request"); LP_send(peer->pushsock,jprint(reqjson,1),1); nonz = 1; } From 85bb64e21908d76a0aa33bf3cdffc31255f767f6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:38:26 +0300 Subject: [PATCH 055/339] Test --- iguana/exchanges/LP_rpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 486ffce72..a32b0232a 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -149,7 +149,7 @@ uint64_t LP_getestimatedrate(char *symbol) char buf[512],*retstr; uint64_t rate = 200; struct iguana_info *coin = LP_coinfind(symbol); if ( coin != 0 ) { - sprintf(buf,"[%d]",3); + sprintf(buf,"[\"%d\"]",3); if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"importaddress",buf)) != 0 ) { rate = atof(retstr); From bc93f83ea7900426e794b7952ac754681ad3bba5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:39:39 +0300 Subject: [PATCH 056/339] Test --- iguana/exchanges/LP_rpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index a32b0232a..ec25ae6d6 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -149,8 +149,8 @@ uint64_t LP_getestimatedrate(char *symbol) char buf[512],*retstr; uint64_t rate = 200; struct iguana_info *coin = LP_coinfind(symbol); if ( coin != 0 ) { - sprintf(buf,"[\"%d\"]",3); - if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"importaddress",buf)) != 0 ) + sprintf(buf,"[%d]",3); + if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"estimatefee",buf)) != 0 ) { rate = atof(retstr); printf("estimated rate %s -> %llu\n",retstr,(long long)rate); From 88895d2e6d22b50d325833b32091a1172fd92780 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:40:44 +0300 Subject: [PATCH 057/339] Test --- iguana/exchanges/LP_rpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index ec25ae6d6..178c3ccf5 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -149,7 +149,7 @@ uint64_t LP_getestimatedrate(char *symbol) char buf[512],*retstr; uint64_t rate = 200; struct iguana_info *coin = LP_coinfind(symbol); if ( coin != 0 ) { - sprintf(buf,"[%d]",3); + sprintf(buf,"[%d]",13); if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"estimatefee",buf)) != 0 ) { rate = atof(retstr); From 3cd37204f8b641880832aa68da3dadc12c67cfeb Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:41:55 +0300 Subject: [PATCH 058/339] Test --- iguana/exchanges/LP_rpc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 178c3ccf5..3e523034c 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -152,8 +152,11 @@ uint64_t LP_getestimatedrate(char *symbol) sprintf(buf,"[%d]",13); if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"estimatefee",buf)) != 0 ) { - rate = atof(retstr); - printf("estimated rate %s -> %llu\n",retstr,(long long)rate); + if ( retstr[0] != '-' ) + { + rate = atof(retstr); + printf("estimated rate %s -> %llu\n",retstr,(long long)rate); + } free(retstr); } } @@ -164,7 +167,7 @@ uint64_t LP_txfee(char *symbol) { uint64_t txfee = 0; if ( strcmp(symbol,"BTC") != 0 ) - txfee = 50000; + txfee = 10000; return(txfee); } From 47f20b288ae013ef2686dcbfd5999a551a25597f Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:50:56 +0300 Subject: [PATCH 059/339] Test --- iguana/exchanges/LP_commands.c | 6 +++--- iguana/exchanges/LP_nativeDEX.c | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index a8f56afb0..a150ce32e 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -94,12 +94,12 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ desttxid = jbits256(argjson,"desttxid"); destvout = jint(argjson,"destvout"); timestamp = juint(argjson,"timestamp"); - quotetime = juint(argjson,"quotetime"); privkey = LP_privkey(utxo->coinaddr); pubkey = LP_pubkey(privkey); srchash = jbits256(argjson,"srchash"); value = j64bits(argjson,"destsatoshis"); - if ( timestamp == utxo->swappending-LP_RESERVETIME && quotetime >= timestamp && quotetime < utxo->swappending && bits256_cmp(pubkey,srchash) == 0 && (destsatoshis= LP_txvalue(rel,desttxid,destvout)) > price*(utxo->satoshis-txfee)+desttxfee && value <= destsatoshis-desttxfee ) + quotetime = juint(argjson,"quotetime"); + //if ( timestamp == utxo->swappending-LP_RESERVETIME && quotetime >= timestamp && quotetime < utxo->swappending && bits256_cmp(pubkey,srchash) == 0 && (destsatoshis= LP_txvalue(rel,desttxid,destvout)) > price*(utxo->satoshis-txfee)+desttxfee && value <= destsatoshis-desttxfee ) { destsatoshis = value; if ( (utxo->pair= nn_socket(AF_SP,NN_PAIR)) < 0 ) @@ -135,7 +135,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ nn_close(utxo->pair); utxo->pair = -1; } - } else printf("dest %.8f < required %.8f\n",dstr(value),dstr(price*(utxo->satoshis-txfee))); + } //else printf("dest %.8f < required %.8f\n",dstr(value),dstr(price*(utxo->satoshis-txfee))); } else printf("no price for %s/%s\n",base,rel); } else printf("utxo->pair.%d when connect came in (%s)\n",utxo->pair,jprint(argjson,0)); } diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 1a5cb048e..cac96ca08 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -774,7 +774,12 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i LP_send(peer->pushsock,jprint(reqjson,0),1); jdelete(reqjson,"method"); jaddstr(reqjson,"method","request"); - LP_send(peer->pushsock,jprint(reqjson,1),1); + LP_send(peer->pushsock,jprint(reqjson,0),1); + jdelete(reqjson,"method"); + jaddstr(reqjson,"method","connect"); + LP_send(peer->pushsock,jprint(reqjson,0),1); + + //SENT.({"base":"KMD","rel":"BTC","timestamp":1496076137,"price":0.00021791,"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","srchash":"2fe57da347cd62431528daac5fbb290730fff684afc4cfc2ed90995f58cb3b74","txfee":"100000","satoshis":"9999900000","destsatoshis":"2179101","result":"reserved","pending":1496076197} nonz = 1; } } From 3864cbf154a9355cd8341da1306de48f8ce90363 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:54:34 +0300 Subject: [PATCH 060/339] Test --- iguana/exchanges/LP_commands.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index a150ce32e..3b193af49 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -37,7 +37,7 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { - char *method,*base,*rel,*retstr,*pairstr; cJSON *retjson; double price; bits256 srchash,desthash,pubkey,privkey,txid,desttxid; struct LP_utxoinfo *utxo; uint32_t timestamp,quotetime; int32_t destvout,DEXselector = 0; uint64_t txfee,satoshis,desttxfee,destsatoshis,value; struct basilisk_request R; + char *method,*base,*rel,*retstr,pairstr[512]; cJSON *retjson; double price; bits256 srchash,desthash,pubkey,privkey,txid,desttxid; struct LP_utxoinfo *utxo; uint32_t timestamp,quotetime; int32_t destvout,DEXselector = 0; uint64_t txfee,satoshis,desttxfee,destsatoshis,value; struct basilisk_request R; //LP_command.({"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"base":"KMD","rel":"BTC","method":"price"}) if ( (method= jstr(argjson,"method")) != 0 ) { @@ -102,9 +102,10 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ //if ( timestamp == utxo->swappending-LP_RESERVETIME && quotetime >= timestamp && quotetime < utxo->swappending && bits256_cmp(pubkey,srchash) == 0 && (destsatoshis= LP_txvalue(rel,desttxid,destvout)) > price*(utxo->satoshis-txfee)+desttxfee && value <= destsatoshis-desttxfee ) { destsatoshis = value; + nanomsg_tcpname(pairstr,mypeer->ipaddr,10000+(rand() % 10000)); if ( (utxo->pair= nn_socket(AF_SP,NN_PAIR)) < 0 ) printf("error creating utxo->pair\n"); - else if ( (pairstr= jstr(argjson,"pair")) != 0 && nn_connect(utxo->pair,pairstr) >= 0 ) + else if ( nn_connect(utxo->pair,pairstr) >= 0 ) { desthash = jbits256(argjson,"desthash"); LP_requestinit(&R,srchash,desthash,base,satoshis,rel,destsatoshis,timestamp,quotetime,DEXselector); @@ -112,6 +113,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ { retjson = cJSON_CreateObject(); jaddstr(retjson,"result","connected"); + jaddstr(retjson,"pair",pairstr); jaddnum(retjson,"requestid",R.requestid); jaddnum(retjson,"quoteid",R.quoteid); retstr = jprint(retjson,1); From 17d6f615b8744d1042c2ac81056dd03bbe46d262 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:55:50 +0300 Subject: [PATCH 061/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 3b193af49..891e99d78 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -109,7 +109,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ { desthash = jbits256(argjson,"desthash"); LP_requestinit(&R,srchash,desthash,base,satoshis,rel,destsatoshis,timestamp,quotetime,DEXselector); - if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_bobloop,(void *)utxo) != 0 ) + if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_bobloop,(void *)utxo) == 0 ) { retjson = cJSON_CreateObject(); jaddstr(retjson,"result","connected"); From 429ccaf65bbfaf773cab4b1163cf59f8ee958bc3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 19:59:24 +0300 Subject: [PATCH 062/339] Test --- iguana/exchanges/LP_commands.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 891e99d78..29cd11dac 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -59,6 +59,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ retjson = cJSON_CreateObject(); jaddstr(retjson,"base",base); jaddstr(retjson,"rel",rel); + jaddstr(retjson,"address",utxo->coinaddr); jaddnum(retjson,"timestamp",time(NULL)); jaddnum(retjson,"price",price); jaddbits256(retjson,"txid",txid); @@ -131,9 +132,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ } else { - if ( pairstr != 0 ) - printf("printf error nn_connect to %s\n",pairstr); - else printf("(%s) missing pair\n",jprint(argjson,0)); + printf("printf error nn_connect to %s\n",pairstr); nn_close(utxo->pair); utxo->pair = -1; } From c876cdfcc19f5932291ce84cbcb71698ed6201b1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 20:02:57 +0300 Subject: [PATCH 063/339] Test --- iguana/exchanges/LP_bitcoin.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 79c78dc9c..db55e823d 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -26,7 +26,10 @@ bits256 LP_privkeyfind(uint8_t rmd160[20]) for (i=0; i no privkey\n"); + return(zero); } int32_t LP_privkeyadd(bits256 privkey,uint8_t rmd160[20]) @@ -37,6 +40,9 @@ int32_t LP_privkeyadd(bits256 privkey,uint8_t rmd160[20]) return(-bits256_cmp(privkey,tmpkey)); LP_privkeys[LP_numprivkeys].privkey = privkey; memcpy(LP_privkeys[LP_numprivkeys].rmd160,rmd160,20); + int32_t i; for (i=0; i<20; i++) + printf("%02x",rmd160[i]); + char str[65]; printf(" -> privkey.(%s)\n",bits256_str(str,privkey)); LP_numprivkeys++; return(LP_numprivkeys); } From 331003c1c6bbf221f39ee274464dac003c2b97b7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 29 May 2017 20:04:02 +0300 Subject: [PATCH 064/339] Test --- iguana/exchanges/LP_bitcoin.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index db55e823d..2b3a412f5 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -36,13 +36,13 @@ int32_t LP_privkeyadd(bits256 privkey,uint8_t rmd160[20]) { bits256 tmpkey; tmpkey = LP_privkeyfind(rmd160); - if ( bits256_nonz(privkey) != 0 ) + if ( bits256_nonz(tmpkey) != 0 ) return(-bits256_cmp(privkey,tmpkey)); LP_privkeys[LP_numprivkeys].privkey = privkey; memcpy(LP_privkeys[LP_numprivkeys].rmd160,rmd160,20); int32_t i; for (i=0; i<20; i++) printf("%02x",rmd160[i]); - char str[65]; printf(" -> privkey.(%s)\n",bits256_str(str,privkey)); + char str[65]; printf(" -> add privkey.(%s)\n",bits256_str(str,privkey)); LP_numprivkeys++; return(LP_numprivkeys); } From 49f9fcb3bf02ddc9dcc6f24dd27e5ac58bfeec87 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 10:30:03 +0300 Subject: [PATCH 065/339] Test --- iguana/exchanges/LP_bitcoin.c | 2 +- iguana/exchanges/LP_commands.c | 42 +++++++++++++++++++++++++ iguana/exchanges/LP_nativeDEX.c | 54 ++++++++------------------------- 3 files changed, 55 insertions(+), 43 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 2b3a412f5..1a4876b34 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -29,7 +29,7 @@ bits256 LP_privkeyfind(uint8_t rmd160[20]) for (i=0; i<20; i++) printf("%02x",rmd160[i]); printf(" -> no privkey\n"); - return(zero); + return(zero); } int32_t LP_privkeyadd(bits256 privkey,uint8_t rmd160[20]) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 29cd11dac..298c9e52d 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -35,6 +35,48 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch return(rp); } +struct LP_utxoinfo *LP_orderbook(char *base,char *rel) +{ + struct LP_peerinfo *peer,*tmp; char *utxostr; cJSON *array,*reqjson,*item; int32_t i,n; + HASH_ITER(hh,LP_peerinfos,peer,tmp) + { + if ( (utxostr= issue_LP_clientgetutxos(peer->ipaddr,peer->port,"KMD",10)) != 0 ) + { + printf("%s:%u %s\n",peer->ipaddr,peer->port,utxostr); + if ( 0 && (array= cJSON_Parse(utxostr)) != 0 ) + { + if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) + { + for (i=0; i<1; i++) + { + item = jitem(array,i); + reqjson = cJSON_CreateObject(); + jaddbits256(reqjson,"txid",jbits256(item,"txid")); + jaddnum(reqjson,"vout",jint(item,"vout")); + jaddstr(reqjson,"base",jstr(item,"coin")); + jaddstr(reqjson,"rel","BTC"); + jaddstr(reqjson,"method","price"); + //5.9.253.196:7779 [{"ipaddr":"5.9.253.196","port":7779,"profit":0.01064000,"coin":"KMD","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","script":"76a914434009423522682bd7cc1b18a614c3096d19683188ac","txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"value":100,"deposit":"07902a65d11f0f577a0346432bcd2b6b53de5554c314209d1964693962524d69","dvout":1,"dvalue":120}] + + LP_send(peer->pushsock,jprint(reqjson,0),1); + jdelete(reqjson,"method"); + jaddstr(reqjson,"method","request"); + LP_send(peer->pushsock,jprint(reqjson,0),1); + jdelete(reqjson,"method"); + jaddstr(reqjson,"method","connect"); + LP_send(peer->pushsock,jprint(reqjson,0),1); + + //SENT.({"base":"KMD","rel":"BTC","timestamp":1496076137,"price":0.00021791,"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","srchash":"2fe57da347cd62431528daac5fbb290730fff684afc4cfc2ed90995f58cb3b74","txfee":"100000","satoshis":"9999900000","destsatoshis":"2179101","result":"reserved","pending":1496076197} + } + } + free_json(array); + } + free(utxostr); + } + } + return(0); +} + void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { char *method,*base,*rel,*retstr,pairstr[512]; cJSON *retjson; double price; bits256 srchash,desthash,pubkey,privkey,txid,desttxid; struct LP_utxoinfo *utxo; uint32_t timestamp,quotetime; int32_t destvout,DEXselector = 0; uint64_t txfee,satoshis,desttxfee,destsatoshis,value; struct basilisk_request R; diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index cac96ca08..5b220285f 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -732,7 +732,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,int32_t pullsock,uint16_t myport,int32_t amclient,char *passphrase,double profitmargin) { - char *retstr,*utxostr; int32_t i,n,len,recvsize,nonz,lastn; struct LP_peerinfo *peer,*tmp; void *ptr; cJSON *argjson,*array,*reqjson,*item; + char *retstr; int32_t i,len,recvsize,counter,nonz,lastn; struct LP_peerinfo *peer,*tmp; void *ptr; cJSON *argjson; for (i=amclient; i 25 ) @@ -746,51 +746,11 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i } LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); LP_coinfind("USD"); LP_coinfind("REVS"); LP_coinfind("JUMBLR"); - LP_privkey_init(mypeer,pubsock,"BTC",passphrase,"",amclient); - LP_privkey_init(mypeer,pubsock,"KMD",passphrase,"",amclient); if ( amclient != 0 ) { nonz = 0; - HASH_ITER(hh,LP_peerinfos,peer,tmp) - { - if ( (utxostr= issue_LP_clientgetutxos(peer->ipaddr,peer->port,"KMD",10)) != 0 ) - { - printf("%s:%u %s\n",peer->ipaddr,peer->port,utxostr); - if ( (array= cJSON_Parse(utxostr)) != 0 ) - { - if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) - { - for (i=0; i<1; i++) - { - item = jitem(array,i); - reqjson = cJSON_CreateObject(); - jaddbits256(reqjson,"txid",jbits256(item,"txid")); - jaddnum(reqjson,"vout",jint(item,"vout")); - jaddstr(reqjson,"base",jstr(item,"coin")); - jaddstr(reqjson,"rel","BTC"); - jaddstr(reqjson,"method","price"); - //5.9.253.196:7779 [{"ipaddr":"5.9.253.196","port":7779,"profit":0.01064000,"coin":"KMD","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","script":"76a914434009423522682bd7cc1b18a614c3096d19683188ac","txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"value":100,"deposit":"07902a65d11f0f577a0346432bcd2b6b53de5554c314209d1964693962524d69","dvout":1,"dvalue":120}] - - LP_send(peer->pushsock,jprint(reqjson,0),1); - jdelete(reqjson,"method"); - jaddstr(reqjson,"method","request"); - LP_send(peer->pushsock,jprint(reqjson,0),1); - jdelete(reqjson,"method"); - jaddstr(reqjson,"method","connect"); - LP_send(peer->pushsock,jprint(reqjson,0),1); - - //SENT.({"base":"KMD","rel":"BTC","timestamp":1496076137,"price":0.00021791,"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","srchash":"2fe57da347cd62431528daac5fbb290730fff684afc4cfc2ed90995f58cb3b74","txfee":"100000","satoshis":"9999900000","destsatoshis":"2179101","result":"reserved","pending":1496076197} - nonz = 1; - } - } - free_json(array); - } - free(utxostr); - } - if ( nonz != 0 ) - break; - } printf("peers\n"); + LP_orderbook("KMD","BTC"); while ( 1 ) { sleep(1); @@ -798,9 +758,19 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i } else { + counter = 0; while ( 1 ) { nonz = 0; + if ( (counter++ % 60) == 0 ) + { + LP_privkey_init(mypeer,pubsock,"BTC",passphrase,"",amclient); + LP_privkey_init(mypeer,pubsock,"KMD",passphrase,"",amclient); + LP_privkey_init(mypeer,pubsock,"LTC",passphrase,"",amclient); + LP_privkey_init(mypeer,pubsock,"USD",passphrase,"",amclient); + LP_privkey_init(mypeer,pubsock,"REVS",passphrase,"",amclient); + LP_privkey_init(mypeer,pubsock,"JUMBLR",passphrase,"",amclient); + } HASH_ITER(hh,LP_peerinfos,peer,tmp) { if ( peer->numpeers != mypeer->numpeers || (rand() % 10) == 0 ) From a0a0e1b978baec703674a5ba79742169c3c79a5a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:05:09 +0300 Subject: [PATCH 066/339] Test --- iguana/exchanges/LP_commands.c | 186 ++++++++++++++++++++++----- iguana/exchanges/LP_include.h | 1 + iguana/exchanges/LP_nativeDEX.c | 221 +++++++++++++++++++++++++------- iguana/exchanges/LP_prices.c | 2 + iguana/exchanges/mm.c | 11 +- 5 files changed, 342 insertions(+), 79 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 298c9e52d..77e8c3a71 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -35,46 +35,163 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch return(rp); } -struct LP_utxoinfo *LP_orderbook(char *base,char *rel) +double LP_pricequery(uint64_t *destsatoshisp,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout) { - struct LP_peerinfo *peer,*tmp; char *utxostr; cJSON *array,*reqjson,*item; int32_t i,n; + cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,pushsock = -1; double price = 0.; + if ( ipaddr != 0 && port >= 1000 ) + { + if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),port)) == 0 ) + peer = LP_addpeer(1,0,-1,ipaddr,port,port+1,port+2,0,0,0); + if ( peer != 0 ) + { + if ( (pushsock= peer->pushsock) >= 0 ) + { + reqjson = cJSON_CreateObject(); + jaddbits256(reqjson,"txid",txid); + jaddnum(reqjson,"vout",vout); + jaddstr(reqjson,"base",base); + jaddstr(reqjson,"rel",rel); + jaddstr(reqjson,"method","price"); + LP_send(pushsock,jprint(reqjson,1),1); + for (i=0; i<10; i++) + { + if ( (price= LP_pricecache(destsatoshisp,base,rel,txid,vout)) != 0. ) + break; + usleep(100000); + } + } else printf("no pushsock for peer.%s:%u\n",ipaddr,port); + } else printf("cant find/create peer.%s:%u\n",ipaddr,port); + } + return(price); +} + +/* + //5.9.253.196:7779 [{"ipaddr":"5.9.253.196","port":7779,"profit":0.01064000,"coin":"KMD","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","script":"76a914434009423522682bd7cc1b18a614c3096d19683188ac","txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"value":100,"deposit":"07902a65d11f0f577a0346432bcd2b6b53de5554c314209d1964693962524d69","dvout":1,"dvalue":120}] + + LP_send(peer->pushsock,jprint(reqjson,0),1); + jdelete(reqjson,"method"); + jaddstr(reqjson,"method","request"); + LP_send(peer->pushsock,jprint(reqjson,0),1); + jdelete(reqjson,"method"); + jaddstr(reqjson,"method","connect"); + LP_send(peer->pushsock,jprint(reqjson,0),1); + + //SENT.({"base":"KMD","rel":"BTC","timestamp":1496076137,"price":0.00021791,"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","srchash":"2fe57da347cd62431528daac5fbb290730fff684afc4cfc2ed90995f58cb3b74","txfee":"100000","satoshis":"9999900000","destsatoshis":"2179101","result":"reserved","pending":1496076197} +*/ +int32_t LP_sizematch(uint64_t mysatoshis,uint64_t othersatoshis) +{ + if ( mysatoshis >= othersatoshis ) + return(0); + else return(-1); +} + +cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) +{ + struct LP_peerinfo *peer,*tmp; char *utxostr,coinstr[16]; cJSON *array,*icopy,*retarray=0,*item; int32_t i,n; double price; int64_t estimatedbase; uint64_t destsatoshis; + if ( (price= LP_price(base,myutxo->coin)) == .0 ) + return(0); + estimatedbase = myutxo->satoshis / price; + if ( estimatedbase <= 0 ) + return(0); + printf("%s -> %s price %.8f mysatoshis %llu estimated base %llu\n",base,myutxo->coin,price,(long long)myutxo->satoshis,(long long)estimatedbase); HASH_ITER(hh,LP_peerinfos,peer,tmp) { - if ( (utxostr= issue_LP_clientgetutxos(peer->ipaddr,peer->port,"KMD",10)) != 0 ) + if ( (utxostr= issue_LP_clientgetutxos(peer->ipaddr,peer->port,base,100)) != 0 ) { printf("%s:%u %s\n",peer->ipaddr,peer->port,utxostr); - if ( 0 && (array= cJSON_Parse(utxostr)) != 0 ) + if ( (array= cJSON_Parse(utxostr)) != 0 ) { if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) { - for (i=0; i<1; i++) + retarray = cJSON_CreateArray(); + for (i=0; ipushsock,jprint(reqjson,0),1); - jdelete(reqjson,"method"); - jaddstr(reqjson,"method","request"); - LP_send(peer->pushsock,jprint(reqjson,0),1); - jdelete(reqjson,"method"); - jaddstr(reqjson,"method","connect"); - LP_send(peer->pushsock,jprint(reqjson,0),1); - - //SENT.({"base":"KMD","rel":"BTC","timestamp":1496076137,"price":0.00021791,"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","srchash":"2fe57da347cd62431528daac5fbb290730fff684afc4cfc2ed90995f58cb3b74","txfee":"100000","satoshis":"9999900000","destsatoshis":"2179101","result":"reserved","pending":1496076197} + safecopy(coinstr,jstr(item,"coin"),sizeof(coinstr)); + if ( strcmp(coinstr,base) == 0 && LP_sizematch(estimatedbase,j64bits(item,"satoshis")) == 0 ) + { + icopy = 0; + if ( (price= LP_pricecache(&destsatoshis,base,myutxo->coin,jbits256(item,"txid"),jint(item,"vout"))) != 0. ) + { + if ( LP_sizematch(myutxo->satoshis,destsatoshis) == 0 ) + icopy = jduplicate(item); + } else icopy = jduplicate(item); + if ( icopy != 0 ) + { + jaddnum(icopy,"price",price); + jaddi(retarray,icopy); + } + } } } free_json(array); } free(utxostr); } + if ( retarray != 0 ) + break; + } + return(retarray); +} + +cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) +{ + int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; uint64_t destsatoshis[100]; + bestprice = 0.; + if ( (array= LP_tradecandidates(utxo,base)) != 0 ) + { + printf("%s %.8f -> (%s)\n",utxo->coin,dstr(utxo->satoshis),jprint(array,0)); + if ( (n= cJSON_GetArraySize(array)) > 0 ) + { + memset(prices,0,sizeof(prices)); + memset(destsatoshis,0,sizeof(destsatoshis)); + for (i=0; icoin,jbits256(item,"txid"),jint(item,"vout")); + if ( destsatoshis[i] != 0 && (double)j64bits(item,"satoshis")/destsatoshis[i] > price ) + price = (double)j64bits(item,"satoshis")/destsatoshis[i]; + } + if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) ) + bestprice = price; + } + if ( bestprice != 0. ) + { + bestmetric = 0.; + besti = -1; + for (i=0; i 0.9 ) + { + metric = destsatoshis[i] / metric * metric * metric; + if ( metric > bestmetric ) + { + besti = i; + bestmetric = metric; + } + } + } + } + if ( besti >= 0 ) + bestitem = jduplicate(jitem(array,besti)); + } + free_json(array); + } } - return(0); + return(bestitem); +} + +char *LP_quote(uint32_t reserved,char *base,char *rel,bits256 txid,int32_t vout,double price,uint64_t satoshis) +{ + struct LP_cacheinfo *ptr; + if ( (ptr= LP_cacheadd(base,rel,txid,vout,price,satoshis)) != 0 ) + return(clonestr("{\"result\":\"updated\"}")); + else return(clonestr("{\"error\":\"nullptr\"}")); } void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) @@ -110,15 +227,18 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ if ( (txfee= LP_getestimatedrate(base)*LP_AVETXSIZE) < 10000 ) txfee = 10000; jadd64bits(retjson,"txfee",txfee); + if ( (desttxfee= LP_getestimatedrate(rel) * LP_AVETXSIZE) < 10000 ) + desttxfee = 10000; + jadd64bits(retjson,"desttxfee",desttxfee); jadd64bits(retjson,"satoshis",utxo->satoshis - txfee); - jadd64bits(retjson,"destsatoshis",price * (utxo->satoshis-txfee)); + jadd64bits(retjson,"destsatoshis",price * (utxo->satoshis-txfee) + desttxfee); if ( strcmp(method,"request") == 0 ) { utxo->swappending = (uint32_t)(time(NULL) + LP_RESERVETIME); utxo->otherpubkey = jbits256(argjson,"pubkey"); - jaddstr(retjson,"result","reserved"); + jaddstr(retjson,"method","reserved"); jaddnum(retjson,"pending",utxo->swappending); - } + } else jaddstr(retjson,"method","quote"); retstr = jprint(retjson,1); LP_send(pubsock,retstr,1); } else printf("null price\n"); @@ -132,8 +252,8 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ { price *= (1. + profitmargin); txfee = j64bits(argjson,"txfee"); + desttxfee = j64bits(argjson,"desttxfee"); satoshis = j64bits(argjson,"satoshis"); - desttxfee = LP_getestimatedrate(rel) * LP_AVETXSIZE; desttxid = jbits256(argjson,"desttxid"); destvout = jint(argjson,"destvout"); timestamp = juint(argjson,"timestamp"); @@ -198,6 +318,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port { if ( strcmp(ipaddr,"127.0.0.1") != 0 && port >= 1000 ) { + amclient = 0; if ( (pushport= juint(argjson,"push")) == 0 ) pushport = argport + 1; if ( (subport= juint(argjson,"sub")) == 0 ) @@ -212,20 +333,21 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port peer->numutxos = othernumutxos; } //printf("peer.(%s) found (%d %d) (%d %d) (%s)\n",peer->ipaddr,peer->numpeers,peer->numutxos,otherpeers,othernumutxos,jprint(argjson,0)); - } else LP_addpeer(LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos")); - amclient = 0; + } else LP_addpeer(amclient,LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos")); } else amclient = 1; } - if ( strcmp(method,"getpeers") == 0 ) + if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) + retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis")); + else if ( strcmp(method,"getpeers") == 0 ) retstr = LP_peers(); - else if ( strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) + else if ( strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"base")) != 0 ) retstr = LP_utxos(LP_mypeer,coin,jint(argjson,"lastn")); else if ( strcmp(method,"notify") == 0 ) retstr = clonestr("{\"result\":\"success\",\"notify\":\"received\"}"); else if ( strcmp(method,"notifyutxo") == 0 ) { printf("utxonotify.(%s)\n",jprint(argjson,0)); - LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"deposit"),jint(argjson,"dvout"),SATOSHIDEN * jdouble(argjson,"dvalue"),jstr(argjson,"script"),jstr(argjson,"address"),jstr(argjson,"ipaddr"),juint(argjson,"port"),jdouble(argjson,"profit")); + LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"base"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"deposit"),jint(argjson,"dvout"),SATOSHIDEN * jdouble(argjson,"dvalue"),jstr(argjson,"script"),jstr(argjson,"address"),jstr(argjson,"ipaddr"),juint(argjson,"port"),jdouble(argjson,"profit")); retstr = clonestr("{\"result\":\"success\",\"notifyutxo\":\"received\"}"); } } diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 7df5192aa..3a01c34e0 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -26,6 +26,7 @@ #define LP_RESERVETIME 60 #define LP_AVETXSIZE 500 +#define LP_CACHEDURATION 60 #define BASILISK_DEFAULT_NUMCONFIRMS 5 #define DEX_SLEEP 3 diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 5b220285f..d5fbf7f27 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -24,9 +24,12 @@ #define LP_PROPAGATION_SLACK 10 // txid ordering is not enforced, so getting extra recent txid +char *activecoins[] = { "BTC", "KMD", "LTC", "USD", "REVS", "JUMBLR" }; + char *default_LPnodes[] = { "5.9.253.195", "5.9.253.196", "5.9.253.197", "5.9.253.198", "5.9.253.199", "5.9.253.200", "5.9.253.201", "5.9.253.202", "5.9.253.203", "5.9.253.204" }; -portable_mutex_t LP_peermutex,LP_utxomutex,LP_commandmutex; +portable_mutex_t LP_peermutex,LP_utxomutex,LP_commandmutex,LP_cachemutex; int32_t LP_mypubsock = -1; +int32_t Client_connections; struct LP_peerinfo { @@ -52,6 +55,78 @@ struct LP_utxoinfo uint16_t port; } *LP_utxoinfos; +struct LP_cacheinfo +{ + UT_hash_handle hh; + uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(int32_t)]; + double price; + uint64_t satoshis,destsatoshis; + uint32_t timestamp; +} *LP_cacheinfos; + +int32_t LP_cachekey(uint8_t *key,char *base,char *rel,bits256 txid,int32_t vout) +{ + uint64_t basebits,relbits; int32_t offset = 0; + basebits = stringbits(base); + relbits = stringbits(rel); + memcpy(&key[offset],&basebits,sizeof(basebits)), offset += sizeof(basebits); + memcpy(&key[offset],&relbits,sizeof(relbits)), offset += sizeof(relbits); + memcpy(&key[offset],&txid,sizeof(txid)), offset += sizeof(txid); + memcpy(&key[offset],&vout,sizeof(vout)), offset += sizeof(vout); + return(offset); +} + +struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout) +{ + struct LP_cacheinfo *ptr=0; uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(vout)]; + if ( LP_cachekey(key,base,rel,txid,vout) == sizeof(key) ) + { + portable_mutex_lock(&LP_cachemutex); + HASH_FIND(hh,LP_cacheinfos,key,sizeof(key),ptr); + portable_mutex_unlock(&LP_cachemutex); + } else printf("LP_cachefind keysize mismatch?\n"); + if ( ptr->timestamp != 0 && ptr->timestamp < time(NULL)-LP_CACHEDURATION ) + { + printf("expire price %.8f\n",ptr->price); + ptr->price = 0.; + ptr->destsatoshis = 0; + ptr->timestamp = 0; + } + return(ptr); +} + +struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout,double price,uint64_t satoshis) +{ + struct LP_cacheinfo *ptr=0; + if ( (ptr= LP_cachefind(base,rel,txid,vout)) == 0 ) + { + ptr = calloc(1,sizeof(*ptr)); + if ( LP_cachekey(ptr->key,base,rel,txid,vout) == sizeof(ptr->key) ) + { + portable_mutex_lock(&LP_cachemutex); + HASH_ADD(hh,LP_cacheinfos,key,sizeof(ptr->key),ptr); + portable_mutex_unlock(&LP_cachemutex); + } else printf("LP_cacheadd keysize mismatch?\n"); + } + ptr->price = price; + ptr->satoshis = satoshis; + ptr->destsatoshis = satoshis * price; + ptr->timestamp = (uint32_t)time(NULL); + printf("updated %s/%s %llu price %.8f\n",base,rel,(long long)satoshis,price); + return(ptr); +} + +double LP_pricecache(uint64_t *destsatoshisp,char *base,char *rel,bits256 txid,int32_t vout) +{ + struct LP_cacheinfo *ptr; + if ( (ptr= LP_cachefind(base,rel,txid,vout)) != 0 ) + { + *destsatoshisp = ptr->destsatoshis; + return(ptr->price); + } else *destsatoshisp = 0; + return(0.); +} + struct LP_peerinfo *LP_peerfind(uint32_t ipbits,uint16_t port) { struct LP_peerinfo *peer=0; uint64_t ip_port; @@ -88,7 +163,7 @@ cJSON *LP_utxojson(struct LP_utxoinfo *utxo) jaddstr(item,"ipaddr",utxo->ipaddr); jaddnum(item,"port",utxo->port); jaddnum(item,"profit",utxo->profitmargin); - jaddstr(item,"coin",utxo->coin); + jaddstr(item,"base",utxo->coin); jaddstr(item,"address",utxo->coinaddr); jaddstr(item,"script",utxo->spendscript); jaddbits256(item,"txid",utxo->txid); @@ -129,9 +204,9 @@ char *LP_utxos(struct LP_peerinfo *mypeer,char *coin,int32_t lastn) return(jprint(utxosjson,1)); } -struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char *ipaddr,uint16_t port,uint16_t pushport,uint16_t subport,double profitmargin,int32_t numpeers,int32_t numutxos) +struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *ipaddr,uint16_t port,uint16_t pushport,uint16_t subport,double profitmargin,int32_t numpeers,int32_t numutxos) { - uint32_t ipbits; int32_t pushsock,subsock,timeout; char checkip[64],pushaddr[64],subaddr[64]; struct LP_peerinfo *peer = 0; + uint32_t ipbits; int32_t pushsock,subsock,timeout,enabled; char checkip[64],pushaddr[64],subaddr[64]; struct LP_peerinfo *peer = 0; ipbits = (uint32_t)calc_ipbits(ipaddr); expand_ipbits(checkip,ipbits); if ( strcmp(checkip,ipaddr) == 0 ) @@ -151,22 +226,32 @@ struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char peer = calloc(1,sizeof(*peer)); peer->pushsock = peer->subsock = pushsock = subsock = -1; strcpy(peer->ipaddr,ipaddr); - if ( pushport != 0 && subport != 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 ) + if ( amclient == 0 ) + enabled = 1; + else enabled = (rand() % (1 << Client_connections)) == 0; + if ( enabled != 0 && pushport != 0 && subport != 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 ) { - if ( (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 ) + timeout = 1000; + nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); + nanomsg_tcpname(pushaddr,peer->ipaddr,pushport); + if ( nn_connect(peer->pushsock,pushaddr) >= 0 ) { - timeout = 1000; - nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); - timeout = 1; - nn_setsockopt(subsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); - nn_setsockopt(subsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); + printf("connected to push.(%s) %d\n",pushaddr,peer->pushsock); + peer->connected = (uint32_t)time(NULL); peer->pushsock = pushsock; - peer->subsock = subsock; - nanomsg_tcpname(pushaddr,peer->ipaddr,pushport); - nanomsg_tcpname(subaddr,peer->ipaddr,subport); - printf("adding (%s and %s) %d %d\n",pushaddr,subaddr,peer->pushsock,peer->subsock); - if ( nn_connect(peer->pushsock,pushaddr) >= 0 && nn_connect(peer->subsock,subaddr) >= 0 ) - peer->connected = (uint32_t)time(NULL); + if ( enabled != 0 && (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 ) + { + timeout = 1; + nn_setsockopt(subsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); + nn_setsockopt(subsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); + nanomsg_tcpname(subaddr,peer->ipaddr,subport); + if ( nn_connect(peer->subsock,subaddr) >= 0 ) + { + peer->subsock = subsock; + printf("connected to sub.(%s) %d\n",subaddr,peer->subsock); + Client_connections += amclient; + } else nn_close(subsock); + } } else nn_close(pushsock); } peer->profitmargin = profitmargin; @@ -246,7 +331,7 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 return(utxo); } -int32_t LP_peersparse(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *retstr,uint32_t now) +int32_t LP_peersparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *retstr,uint32_t now) { struct LP_peerinfo *peer; uint32_t argipbits; char *argipaddr; uint16_t argport,pushport,subport; cJSON *array,*item; int32_t i,n=0; if ( (array= cJSON_Parse(retstr)) != 0 ) @@ -264,7 +349,7 @@ int32_t LP_peersparse(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipa subport = argport + 2; argipbits = (uint32_t)calc_ipbits(argipaddr); if ( (peer= LP_peerfind(argipbits,argport)) == 0 ) - peer = LP_addpeer(mypeer,mypubsock,argipaddr,argport,pushport,subport,jdouble(item,"profit"),jint(item,"numpeers"),jint(item,"numutxos")); + peer = LP_addpeer(amclient,mypeer,mypubsock,argipaddr,argport,pushport,subport,jdouble(item,"profit"),jint(item,"numpeers"),jint(item,"numutxos")); if ( peer != 0 ) { peer->lasttime = now; @@ -282,6 +367,11 @@ int32_t LP_peersparse(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipa int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *retstr,uint32_t now) { struct LP_peerinfo *peer,*destpeer; uint32_t argipbits; char *argipaddr; uint16_t argport,pushport,subport; cJSON *array,*item; int32_t i,n=0; bits256 txid; struct LP_utxoinfo *utxo; + if ( amclient != 0 ) + { + printf("LP_utxosparse not for clientside\n"); + return(-1); + } if ( (array= cJSON_Parse(retstr)) != 0 ) { if ( (n= cJSON_GetArraySize(array)) > 0 ) @@ -297,11 +387,11 @@ int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs subport = argport + 2; argipbits = (uint32_t)calc_ipbits(argipaddr); if ( (peer= LP_peerfind(argipbits,argport)) == 0 ) - peer = LP_addpeer(mypeer,mypubsock,argipaddr,argport,pushport,subport,jdouble(item,"profit"),jint(item,"numpeers"),jint(item,"numutxos")); + peer = LP_addpeer(amclient,mypeer,mypubsock,argipaddr,argport,pushport,subport,jdouble(item,"profit"),jint(item,"numpeers"),jint(item,"numutxos")); if ( jobj(item,"txid") != 0 ) { txid = jbits256(item,"txid"); - utxo = LP_addutxo(amclient,mypeer,mypubsock,jstr(item,"coin"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"deposit"),jint(item,"dvout"),SATOSHIDEN * jdouble(item,"dvalue"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); + utxo = LP_addutxo(amclient,mypeer,mypubsock,jstr(item,"base"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"deposit"),jint(item,"dvout"),SATOSHIDEN * jdouble(item,"dvalue"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); if ( utxo != 0 ) utxo->lasttime = now; } @@ -361,7 +451,7 @@ char *issue_LP_notifyutxo(char *destip,uint16_t destport,struct LP_utxoinfo *utx return(issue_curl(url)); } -void LP_peersquery(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *myipaddr,uint16_t myport,double myprofit) +void LP_peersquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *myipaddr,uint16_t myport,double myprofit) { char *retstr; struct LP_peerinfo *peer,*tmp; uint32_t now,flag = 0; peer = LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport); @@ -371,7 +461,7 @@ void LP_peersquery(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr { //printf("got.(%s)\n",retstr); now = (uint32_t)time(NULL); - LP_peersparse(mypeer,mypubsock,destipaddr,destport,retstr,now); + LP_peersparse(amclient,mypeer,mypubsock,destipaddr,destport,retstr,now); free(retstr); if ( mypeer != 0 ) { @@ -395,6 +485,11 @@ void LP_peersquery(struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr void LP_utxosquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *coin,int32_t lastn,char *myipaddr,uint16_t myport,double myprofit) { char *retstr; struct LP_utxoinfo *utxo,*tmp; struct LP_peerinfo *peer; int32_t i,firsti; uint32_t now,flag = 0; + if ( amclient != 0 ) + { + printf("LP_utxosquery not for clientside\n"); + return; + } peer = LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport); if ( (peer != 0 && peer->errors > 0) || mypeer == 0 ) return; @@ -630,7 +725,7 @@ struct iguana_info *LP_coinfind(char *symbol) if ( strcmp(symbol,"BTC") == 0 ) { cdata.txfee = 50000; - cdata.estimatedrate = 200; + cdata.estimatedrate = 300; cdata.p2shtype = 5; cdata.wiftype = 128; LP_userpass(cdata.userpass,symbol,"","bitcoin"); @@ -730,47 +825,80 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb return(total); } +void LP_privkey_updates(struct LP_peerinfo *mypeer,int32_t pubsock,char *passphrase,int32_t amclient) +{ + int32_t i; + for (i=0; i 25 ) - continue; - LP_peersquery(mypeer,pubsock,default_LPnodes[i],myport,mypeer!=0?mypeer->ipaddr:"127.0.0.1",myport,profitmargin); + for (i=0; i 25 ) + continue; + LP_peersquery(amclient,mypeer,pubsock,default_LPnodes[i],myport,mypeer!=0?mypeer->ipaddr:"127.0.0.1",myport,profitmargin); + } + } + else + { + OS_randombytes((void *)&r,sizeof(r)); + for (j=0; jipaddr:"127.0.0.1",myport,profitmargin); + } } if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&myport) != 0 ) { printf("error launching stats rpcloop for port.%u\n",myport); exit(-1); } - LP_coinfind("BTC"); LP_coinfind("LTC"); LP_coinfind("KMD"); - LP_coinfind("USD"); LP_coinfind("REVS"); LP_coinfind("JUMBLR"); + for (i=0; isubsock >= 0 && (recvsize= nn_recv(peer->subsock,&ptr,NN_MSG,0)) >= 0 ) + { + nonz++; + if ( (argjson= cJSON_Parse((char *)ptr)) != 0 ) + { + portable_mutex_lock(&LP_commandmutex); + if ( (retstr= stats_JSON(argjson,"127.0.0.1",mypubport)) != 0 ) + { + printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); + free(retstr); + } + portable_mutex_unlock(&LP_commandmutex); + free_json(argjson); + } else printf("error parsing.(%s)\n",(char *)ptr); + if ( ptr != 0 ) + nn_freemsg(ptr), ptr = 0; + } + } + if ( nonz == 0 ) + sleep(n + 1); } } else { - counter = 0; while ( 1 ) { nonz = 0; if ( (counter++ % 60) == 0 ) - { - LP_privkey_init(mypeer,pubsock,"BTC",passphrase,"",amclient); - LP_privkey_init(mypeer,pubsock,"KMD",passphrase,"",amclient); - LP_privkey_init(mypeer,pubsock,"LTC",passphrase,"",amclient); - LP_privkey_init(mypeer,pubsock,"USD",passphrase,"",amclient); - LP_privkey_init(mypeer,pubsock,"REVS",passphrase,"",amclient); - LP_privkey_init(mypeer,pubsock,"JUMBLR",passphrase,"",amclient); - } + LP_privkey_updates(mypeer,pubsock,passphrase,amclient); HASH_ITER(hh,LP_peerinfos,peer,tmp) { if ( peer->numpeers != mypeer->numpeers || (rand() % 10) == 0 ) @@ -778,7 +906,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i if ( peer->numpeers != mypeer->numpeers ) printf("%s num.%d vs %d\n",peer->ipaddr,peer->numpeers,mypeer->numpeers); if ( strcmp(peer->ipaddr,mypeer->ipaddr) != 0 ) - LP_peersquery(mypeer,pubsock,peer->ipaddr,peer->port,mypeer->ipaddr,myport,profitmargin); + LP_peersquery(amclient,mypeer,pubsock,peer->ipaddr,peer->port,mypeer->ipaddr,myport,profitmargin); } if ( peer->numutxos != mypeer->numutxos ) { @@ -835,6 +963,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profit portable_mutex_init(&LP_peermutex); portable_mutex_init(&LP_utxomutex); portable_mutex_init(&LP_commandmutex); + portable_mutex_init(&LP_cachemutex); if ( amclient == 0 ) { if ( profitmargin == 0. || profitmargin == 0.01 ) @@ -875,7 +1004,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profit } } else printf("error getting sockets %d %d\n",pullsock,pubsock); LP_mypubsock = pubsock; - LP_mypeer = mypeer = LP_addpeer(mypeer,pubsock,myipaddr,myport,0,0,profitmargin,0,0); + LP_mypeer = mypeer = LP_addpeer(amclient,mypeer,pubsock,myipaddr,myport,0,0,profitmargin,0,0); //printf("my ipaddr.(%s) peers.(%s)\n",ipaddr,retstr!=0?retstr:""); } else printf("error getting myipaddr\n"); } else printf("error issuing curl\n"); diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index 691325cc4..33bc6f897 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -20,6 +20,8 @@ double LP_kmdbtc; + + // very, very simple for now void LP_priceupdate(char *base,char *rel,double price,double avebid,double aveask,double highbid,double lowask,double PAXPRICES[32]) diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 4f9d3e0b1..34312e13a 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -819,7 +819,7 @@ int main(int argc, const char * argv[]) { char *base,*rel,*name,*exchange,*apikey,*apisecret,*blocktrail,*retstr,*baseaddr,*reladdr,*passphrase; double profitmargin,maxexposure,incrratio,start_rel,start_base,minask,maxbid,incr,theoretical = 0.; - cJSON *retjson,*loginjson; int32_t i; + cJSON *retjson,*loginjson,*matchjson; int32_t i; if ( argc > 1 && (retjson= cJSON_Parse(argv[1])) != 0 ) { if ( (passphrase= jstr(retjson,"passphrase")) == 0 ) @@ -834,6 +834,15 @@ int main(int argc, const char * argv[]) { theoretical = marketmaker_updateprice("komodo","KMD","BTC",theoretical,&incr); sleep(30); + if ( jint(retjson,"client") != 0 ) + { + struct LP_utxoinfo *utxo,*utmp; + HASH_ITER(hh,LP_utxoinfos,utxo,utmp) + { + if ( (matchjson= LP_bestprice(utxo,"KMD")) != 0 ) + printf("bestprice (%s)\n",jprint(matchjson,1)); + } + } } profitmargin = jdouble(retjson,"profitmargin"); minask = jdouble(retjson,"minask"); From fb64cba72678c90adcb140b5fea3d19f256dca73 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:06:52 +0300 Subject: [PATCH 067/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index d5fbf7f27..732e90bf6 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -24,7 +24,7 @@ #define LP_PROPAGATION_SLACK 10 // txid ordering is not enforced, so getting extra recent txid -char *activecoins[] = { "BTC", "KMD", "LTC", "USD", "REVS", "JUMBLR" }; +char *activecoins[] = { "BTC", "KMD", };//"LTC", "USD", "REVS", "JUMBLR" }; char *default_LPnodes[] = { "5.9.253.195", "5.9.253.196", "5.9.253.197", "5.9.253.198", "5.9.253.199", "5.9.253.200", "5.9.253.201", "5.9.253.202", "5.9.253.203", "5.9.253.204" }; portable_mutex_t LP_peermutex,LP_utxomutex,LP_commandmutex,LP_cachemutex; From 66ec96e0fac90f366aea68794fe8b6998a163e61 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:07:54 +0300 Subject: [PATCH 068/339] Test --- iguana/m_mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/m_mm b/iguana/m_mm index 2e310704b..2a2f6c130 100755 --- a/iguana/m_mm +++ b/iguana/m_mm @@ -1,2 +1,2 @@ cd secp256k1; ./m_unix; cd .. -gcc -o marketmaker -I../crypto777 exchanges/mm.c mini-gmp.c secp256k1.o ../agents/libcrypto777.a -lnanomsg -lcurl -lpthread -lm +gcc -g -o marketmaker -I../crypto777 exchanges/mm.c mini-gmp.c secp256k1.o ../agents/libcrypto777.a -lnanomsg -lcurl -lpthread -lm From af6046a918a84d2a5fbdc6ed5c8bc3e59f1f9882 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:12:34 +0300 Subject: [PATCH 069/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 732e90bf6..dcdf2e3db 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -278,7 +278,7 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 struct LP_utxoinfo *utxo = 0; uint8_t key[sizeof(txid) + sizeof(vout)]; if ( coin == 0 || coin[0] == 0 || spendscript == 0 || spendscript[0] == 0 || coinaddr == 0 || coinaddr[0] == 0 || bits256_nonz(txid) == 0 || bits256_nonz(deposittxid) == 0 || vout < 0 || depositvout < 0 || satoshis <= 0 || depositsatoshis <= 0 ) { - printf("malformed addutxo %d %d %d %d %d %d %d %d %d %d %d %d\n", coin == 0,coin[0] == 0,spendscript == 0,spendscript[0] == 0,coinaddr == 0,coinaddr[0] == 0,bits256_nonz(txid) == 0,bits256_nonz(deposittxid) == 0,vout < 0,depositvout < 0,satoshis <= 0,depositsatoshis <= 0); + printf("malformed addutxo %d %d %d %d %d %d %d %d %d\n", coin == 0,spendscript == 0,coinaddr == 0,bits256_nonz(txid) == 0,bits256_nonz(deposittxid) == 0,vout < 0,depositvout < 0,satoshis <= 0,depositsatoshis <= 0); return(0); } if ( (utxo= LP_utxofind(txid,vout)) != 0 ) From 3025bd5e38e8d2e170835f7a90d42aef89570d22 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:16:25 +0300 Subject: [PATCH 070/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index dcdf2e3db..4ddfed183 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -85,7 +85,7 @@ struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout HASH_FIND(hh,LP_cacheinfos,key,sizeof(key),ptr); portable_mutex_unlock(&LP_cachemutex); } else printf("LP_cachefind keysize mismatch?\n"); - if ( ptr->timestamp != 0 && ptr->timestamp < time(NULL)-LP_CACHEDURATION ) + if ( ptr != 0 && ptr->timestamp != 0 && ptr->timestamp < time(NULL)-LP_CACHEDURATION ) { printf("expire price %.8f\n",ptr->price); ptr->price = 0.; From 2ba0fbe53543d86fdcbf140d4e586bff022826a8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:18:54 +0300 Subject: [PATCH 071/339] Test --- iguana/exchanges/LP_commands.c | 1 + iguana/exchanges/LP_nativeDEX.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 77e8c3a71..411a46f7b 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -355,5 +355,6 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port return(retstr); retjson = cJSON_CreateObject(); jaddstr(retjson,"error","unrecognized command"); + printf("ERROR.(%s)\n",jprint(argjson,0)); return(clonestr(jprint(retjson,1))); } diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 4ddfed183..956c35f60 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -391,7 +391,7 @@ int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs if ( jobj(item,"txid") != 0 ) { txid = jbits256(item,"txid"); - utxo = LP_addutxo(amclient,mypeer,mypubsock,jstr(item,"base"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"deposit"),jint(item,"dvout"),SATOSHIDEN * jdouble(item,"dvalue"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); + utxo = LP_addutxo(amclient,mypeer,mypubsock,jstr(item,"coin"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"deposit"),jint(item,"dvout"),SATOSHIDEN * jdouble(item,"dvalue"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); if ( utxo != 0 ) utxo->lasttime = now; } From 2be51f0f90b9856ee962b12eb41207a6d0254bb1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:21:56 +0300 Subject: [PATCH 072/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 411a46f7b..9901e531a 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -340,7 +340,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis")); else if ( strcmp(method,"getpeers") == 0 ) retstr = LP_peers(); - else if ( strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"base")) != 0 ) + else if ( strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) retstr = LP_utxos(LP_mypeer,coin,jint(argjson,"lastn")); else if ( strcmp(method,"notify") == 0 ) retstr = clonestr("{\"result\":\"success\",\"notify\":\"received\"}"); From 145ec12ab52b5e25f13d2904272102336e1e33ad Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:22:58 +0300 Subject: [PATCH 073/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 9901e531a..9610f6b0d 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -329,7 +329,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port peer->numpeers = otherpeers; if ( (othernumutxos= jint(argjson,"numutxos")) > peer->numutxos ) { - printf("change.(%s) numutxos.%d -> %d mynumutxos.%d\n",peer->ipaddr,peer->numutxos,othernumutxos,LP_mypeer->numutxos); + printf("change.(%s) numutxos.%d -> %d mynumutxos.%d\n",peer->ipaddr,peer->numutxos,othernumutxos,LP_mypeer != 0 ? LP_mypeer->numutxos:0); peer->numutxos = othernumutxos; } //printf("peer.(%s) found (%d %d) (%d %d) (%s)\n",peer->ipaddr,peer->numpeers,peer->numutxos,otherpeers,othernumutxos,jprint(argjson,0)); From 8416af8a29b43037448f94cb8b0c0bdd3f7abf24 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:30:41 +0300 Subject: [PATCH 074/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 9610f6b0d..8f35f0320 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -347,7 +347,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port else if ( strcmp(method,"notifyutxo") == 0 ) { printf("utxonotify.(%s)\n",jprint(argjson,0)); - LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"base"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"deposit"),jint(argjson,"dvout"),SATOSHIDEN * jdouble(argjson,"dvalue"),jstr(argjson,"script"),jstr(argjson,"address"),jstr(argjson,"ipaddr"),juint(argjson,"port"),jdouble(argjson,"profit")); + LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"deposit"),jint(argjson,"dvout"),SATOSHIDEN * jdouble(argjson,"dvalue"),jstr(argjson,"script"),jstr(argjson,"address"),jstr(argjson,"ipaddr"),juint(argjson,"port"),jdouble(argjson,"profit")); retstr = clonestr("{\"result\":\"success\",\"notifyutxo\":\"received\"}"); } } From 982be563cfa89115b86bc024940216e2c117f2a8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:50:14 +0300 Subject: [PATCH 075/339] Test --- iguana/exchanges/LP_commands.c | 5 ++--- iguana/exchanges/LP_nativeDEX.c | 15 +++++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 8f35f0320..cc447c97b 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -313,12 +313,11 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port return(clonestr("{\"error\":\"need method in request\"}")); else { - amclient = 0; + amclient = (LP_mypeer == 0); if ( (ipaddr= jstr(argjson,"ipaddr")) != 0 && (argport= juint(argjson,"port")) != 0 ) { if ( strcmp(ipaddr,"127.0.0.1") != 0 && port >= 1000 ) { - amclient = 0; if ( (pushport= juint(argjson,"push")) == 0 ) pushport = argport + 1; if ( (subport= juint(argjson,"sub")) == 0 ) @@ -334,7 +333,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } //printf("peer.(%s) found (%d %d) (%d %d) (%s)\n",peer->ipaddr,peer->numpeers,peer->numutxos,otherpeers,othernumutxos,jprint(argjson,0)); } else LP_addpeer(amclient,LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos")); - } else amclient = 1; + } } if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis")); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 956c35f60..38e864bb9 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -301,20 +301,23 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 safecopy(utxo->coin,coin,sizeof(utxo->coin)); safecopy(utxo->coinaddr,coinaddr,sizeof(utxo->coinaddr)); safecopy(utxo->spendscript,spendscript,sizeof(utxo->spendscript)); - utxo->txid = txid; - utxo->vout = vout; - utxo->satoshis = satoshis; if ( amclient == 0 ) { + utxo->txid = txid; + utxo->vout = vout; + utxo->satoshis = satoshis; utxo->deposittxid = deposittxid; utxo->depositvout = depositvout; utxo->depositsatoshis = depositsatoshis; } else { - utxo->feetxid = deposittxid; - utxo->feevout = depositvout; - utxo->feesatoshis = depositsatoshis; + utxo->feetxid = txid; + utxo->feevout = vout; + utxo->feesatoshis = satoshis; + utxo->txid = deposittxid; + utxo->vout = depositvout; + utxo->satoshis = depositsatoshis; } memcpy(key,txid.bytes,sizeof(txid)); memcpy(&key[sizeof(txid)],&vout,sizeof(vout)); From 9f5d590c1b83fb3a7476fbb4b9e1e101995c8cdd Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:52:34 +0300 Subject: [PATCH 076/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index cc447c97b..bac7fd116 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -107,7 +107,7 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) for (i=0; i Date: Tue, 30 May 2017 13:56:40 +0300 Subject: [PATCH 077/339] Test --- iguana/exchanges/LP_nativeDEX.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 38e864bb9..28144c8a4 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -350,6 +350,7 @@ int32_t LP_peersparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs pushport = argport + 1; if ( (subport= juint(item,"sub")) == 0 ) subport = argport + 2; + printf("(%s)\n",jprint(item,0)); argipbits = (uint32_t)calc_ipbits(argipaddr); if ( (peer= LP_peerfind(argipbits,argport)) == 0 ) peer = LP_addpeer(amclient,mypeer,mypubsock,argipaddr,argport,pushport,subport,jdouble(item,"profit"),jint(item,"numpeers"),jint(item,"numutxos")); @@ -466,7 +467,7 @@ void LP_peersquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock now = (uint32_t)time(NULL); LP_peersparse(amclient,mypeer,mypubsock,destipaddr,destport,retstr,now); free(retstr); - if ( mypeer != 0 ) + if ( amclient == 0 ) { HASH_ITER(hh,LP_peerinfos,peer,tmp) { @@ -844,7 +845,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i { if ( (rand() % 100) > 25 ) continue; - LP_peersquery(amclient,mypeer,pubsock,default_LPnodes[i],myport,mypeer!=0?mypeer->ipaddr:"127.0.0.1",myport,profitmargin); + LP_peersquery(amclient,mypeer,pubsock,default_LPnodes[i],myport,mypeer->ipaddr,myport,profitmargin); } } else @@ -853,7 +854,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i for (j=0; jipaddr:"127.0.0.1",myport,profitmargin); + LP_peersquery(amclient,mypeer,pubsock,default_LPnodes[i],myport,"127.0.0.1",myport,profitmargin); } } if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&myport) != 0 ) From d6be9dda264eaae6992515e58174a09f378b34bd Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 13:58:07 +0300 Subject: [PATCH 078/339] Test --- iguana/exchanges/LP_nativeDEX.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 28144c8a4..c4d99a2b2 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -229,7 +229,7 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 if ( amclient == 0 ) enabled = 1; else enabled = (rand() % (1 << Client_connections)) == 0; - if ( enabled != 0 && pushport != 0 && subport != 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 ) + if ( pushport != 0 && subport != 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 ) { timeout = 1000; nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); @@ -350,7 +350,6 @@ int32_t LP_peersparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs pushport = argport + 1; if ( (subport= juint(item,"sub")) == 0 ) subport = argport + 2; - printf("(%s)\n",jprint(item,0)); argipbits = (uint32_t)calc_ipbits(argipaddr); if ( (peer= LP_peerfind(argipbits,argport)) == 0 ) peer = LP_addpeer(amclient,mypeer,mypubsock,argipaddr,argport,pushport,subport,jdouble(item,"profit"),jint(item,"numpeers"),jint(item,"numutxos")); From 1a7bf25b20877a718129765b8f713be6d80c6afe Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:00:20 +0300 Subject: [PATCH 079/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index c4d99a2b2..c7b01e2f7 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -253,7 +253,7 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 } else nn_close(subsock); } } else nn_close(pushsock); - } + } else printf("%s pushport.%u subport.%u pushsock.%d\n",ipaddr,pushport,subport,pushsock); peer->profitmargin = profitmargin; peer->ipbits = ipbits; peer->port = port; From 63a7eb3b464803739442c3823c952f041c67b83b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:01:58 +0300 Subject: [PATCH 080/339] Test --- iguana/exchanges/LP_nativeDEX.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index c7b01e2f7..9640866e2 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -211,7 +211,7 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 expand_ipbits(checkip,ipbits); if ( strcmp(checkip,ipaddr) == 0 ) { - //printf("LPaddpeer %s\n",ipaddr); + printf("LPaddpeer %s\n",ipaddr); if ( (peer= LP_peerfind(ipbits,port)) != 0 ) { if ( peer->profitmargin == 0. ) @@ -269,7 +269,7 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 if ( mypubsock >= 0 ) LP_send(mypubsock,jprint(LP_peerjson(peer),1),1); } - } + } else printf("LP_addpeer: checkip.(%s) vs (%s)\n",checkip,ipaddr); return(peer); } @@ -352,7 +352,10 @@ int32_t LP_peersparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs subport = argport + 2; argipbits = (uint32_t)calc_ipbits(argipaddr); if ( (peer= LP_peerfind(argipbits,argport)) == 0 ) + { peer = LP_addpeer(amclient,mypeer,mypubsock,argipaddr,argport,pushport,subport,jdouble(item,"profit"),jint(item,"numpeers"),jint(item,"numutxos")); + printf("peer.%p after LP_addpeer\n",peer); + } else printf("have peer.%p\n",peer); if ( peer != 0 ) { peer->lasttime = now; From 9238021c86b62166402bfc08156a7544e2fc6059 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:03:03 +0300 Subject: [PATCH 081/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 9640866e2..725398a01 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -211,7 +211,6 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 expand_ipbits(checkip,ipbits); if ( strcmp(checkip,ipaddr) == 0 ) { - printf("LPaddpeer %s\n",ipaddr); if ( (peer= LP_peerfind(ipbits,port)) != 0 ) { if ( peer->profitmargin == 0. ) @@ -223,6 +222,7 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 } else { + printf("LPaddpeer %s\n",ipaddr); peer = calloc(1,sizeof(*peer)); peer->pushsock = peer->subsock = pushsock = subsock = -1; strcpy(peer->ipaddr,ipaddr); From 10638942f93ccf10d78185a164ea0be202618882 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:05:01 +0300 Subject: [PATCH 082/339] Test --- iguana/exchanges/LP_nativeDEX.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 725398a01..917322782 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -232,6 +232,7 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 if ( pushport != 0 && subport != 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 ) { timeout = 1000; + printf("pushsock.%d\n",pushsock); nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); nanomsg_tcpname(pushaddr,peer->ipaddr,pushport); if ( nn_connect(peer->pushsock,pushaddr) >= 0 ) @@ -252,7 +253,12 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 Client_connections += amclient; } else nn_close(subsock); } - } else nn_close(pushsock); + } + else + { + nn_close(pushsock); + printf("error connecting to push.(%s)\n",pushaddr); + } } else printf("%s pushport.%u subport.%u pushsock.%d\n",ipaddr,pushport,subport,pushsock); peer->profitmargin = profitmargin; peer->ipbits = ipbits; From 81babac38da2975cce83713be2a620a962faf956 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:08:06 +0300 Subject: [PATCH 083/339] Test --- iguana/exchanges/LP_nativeDEX.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 917322782..d4292a695 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -235,9 +235,9 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 printf("pushsock.%d\n",pushsock); nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); nanomsg_tcpname(pushaddr,peer->ipaddr,pushport); - if ( nn_connect(peer->pushsock,pushaddr) >= 0 ) + if ( nn_connect(pushsock,pushaddr) >= 0 ) { - printf("connected to push.(%s) %d\n",pushaddr,peer->pushsock); + printf("connected to push.(%s) %d\n",pushaddr,pushsock); peer->connected = (uint32_t)time(NULL); peer->pushsock = pushsock; if ( enabled != 0 && (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 ) @@ -246,7 +246,7 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 nn_setsockopt(subsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); nn_setsockopt(subsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); nanomsg_tcpname(subaddr,peer->ipaddr,subport); - if ( nn_connect(peer->subsock,subaddr) >= 0 ) + if ( nn_connect(subsock,subaddr) >= 0 ) { peer->subsock = subsock; printf("connected to sub.(%s) %d\n",subaddr,peer->subsock); From 02c36500907e5aca707418677234eca9a312a174 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:09:56 +0300 Subject: [PATCH 084/339] Test --- iguana/exchanges/LP_nativeDEX.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index d4292a695..f684e1486 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -222,7 +222,7 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 } else { - printf("LPaddpeer %s\n",ipaddr); + //printf("LPaddpeer %s\n",ipaddr); peer = calloc(1,sizeof(*peer)); peer->pushsock = peer->subsock = pushsock = subsock = -1; strcpy(peer->ipaddr,ipaddr); @@ -232,7 +232,6 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 if ( pushport != 0 && subport != 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 ) { timeout = 1000; - printf("pushsock.%d\n",pushsock); nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); nanomsg_tcpname(pushaddr,peer->ipaddr,pushport); if ( nn_connect(pushsock,pushaddr) >= 0 ) @@ -360,8 +359,7 @@ int32_t LP_peersparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs if ( (peer= LP_peerfind(argipbits,argport)) == 0 ) { peer = LP_addpeer(amclient,mypeer,mypubsock,argipaddr,argport,pushport,subport,jdouble(item,"profit"),jint(item,"numpeers"),jint(item,"numutxos")); - printf("peer.%p after LP_addpeer\n",peer); - } else printf("have peer.%p\n",peer); + } if ( peer != 0 ) { peer->lasttime = now; @@ -909,7 +907,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i while ( 1 ) { nonz = 0; - if ( (counter++ % 60) == 0 ) + if ( (counter++ % 300) == 0 ) LP_privkey_updates(mypeer,pubsock,passphrase,amclient); HASH_ITER(hh,LP_peerinfos,peer,tmp) { @@ -962,7 +960,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i nn_freemsg(ptr), ptr = 0; } if ( nonz == 0 ) - sleep(mypeer->numpeers + 1); + sleep(1); } } } From 503216a9706b98424c3c41eab26f3a44816dc56b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:11:55 +0300 Subject: [PATCH 085/339] Test --- iguana/exchanges/LP_nativeDEX.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index f684e1486..4af8b295d 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -892,6 +892,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i free(retstr); } portable_mutex_unlock(&LP_commandmutex); + printf("subloop.(%s)\n",jprint(argjson,0)); free_json(argjson); } else printf("error parsing.(%s)\n",(char *)ptr); if ( ptr != 0 ) @@ -899,7 +900,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i } } if ( nonz == 0 ) - sleep(n + 1); + sleep(1); } } else From 473aab4301ae9e46d2292dbd570e137a98312739 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:12:59 +0300 Subject: [PATCH 086/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 4af8b295d..54b5f17ed 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -228,7 +228,7 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 strcpy(peer->ipaddr,ipaddr); if ( amclient == 0 ) enabled = 1; - else enabled = (rand() % (1 << Client_connections)) == 0; + else enabled = 1;//(rand() % (1 << Client_connections)) == 0; if ( pushport != 0 && subport != 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 ) { timeout = 1000; From a6a750862320ee0c27d8e90c28a68143e04d8c62 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:36:50 +0300 Subject: [PATCH 087/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 54b5f17ed..a89289298 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -291,7 +291,7 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 if ( bits256_cmp(txid,utxo->txid) != 0 || bits256_cmp(deposittxid,utxo->deposittxid) != 0 || vout != utxo->vout || satoshis != utxo->satoshis || depositvout != utxo->depositvout || depositsatoshis != utxo->depositsatoshis || strcmp(coin,utxo->coin) != 0 || strcmp(spendscript,utxo->spendscript) != 0 || strcmp(coinaddr,utxo->coinaddr) != 0 || strcmp(ipaddr,utxo->ipaddr) != 0 || port != utxo->port ) { utxo->errors++; - char str[65]; printf("error on subsequent utxo add.(%s)\n",bits256_str(str,txid)); + char str[65]; printf("error on subsequent utxo add.(%s) %d %d %d %d %d %d %d %d %d %d %d\n",bits256_str(str,txid),bits256_cmp(txid,utxo->txid) != 0,bits256_cmp(deposittxid,utxo->deposittxid) != 0,vout != utxo->vout,satoshis != utxo->satoshis,depositvout != utxo->depositvout,depositsatoshis != utxo->depositsatoshis,strcmp(coin,utxo->coin) != 0,strcmp(spendscript,utxo->spendscript) != 0,strcmp(coinaddr,utxo->coinaddr) != 0,strcmp(ipaddr,utxo->ipaddr) != 0,port != utxo->port); } else if ( profitmargin != 0. ) utxo->profitmargin = profitmargin; From 123ede953ec30f4138013600cb01ea3366cb8529 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:42:02 +0300 Subject: [PATCH 088/339] Test --- iguana/exchanges/LP_include.h | 1 + iguana/exchanges/LP_nativeDEX.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 3a01c34e0..af134d939 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -38,6 +38,7 @@ #define BASILISK_KEYSIZE ((int32_t)(2*sizeof(bits256)+sizeof(uint32_t)*2)) extern char GLOBAL_DBDIR[]; +extern int32_t IAMCLIENT; void *bitcoin_ctx(); int32_t bitcoin_verify(void *ctx,uint8_t *sig,int32_t siglen,bits256 txhash2,uint8_t *pubkey,int32_t plen); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index a89289298..055fea163 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -30,6 +30,7 @@ char *default_LPnodes[] = { "5.9.253.195", "5.9.253.196", "5.9.253.197", "5.9.25 portable_mutex_t LP_peermutex,LP_utxomutex,LP_commandmutex,LP_cachemutex; int32_t LP_mypubsock = -1; int32_t Client_connections; +int32_t IAMCLIENT = 0; struct LP_peerinfo { @@ -856,6 +857,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i } else { + myport += 10; OS_randombytes((void *)&r,sizeof(r)); for (j=0; j Date: Tue, 30 May 2017 14:43:29 +0300 Subject: [PATCH 089/339] Test --- iguana/exchanges/LP_nativeDEX.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 055fea163..47da3f08c 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -857,19 +857,20 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i } else { - myport += 10; OS_randombytes((void *)&r,sizeof(r)); for (j=0; j Date: Tue, 30 May 2017 14:47:49 +0300 Subject: [PATCH 090/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 47da3f08c..e4d0e9c81 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -329,7 +329,7 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 memcpy(&key[sizeof(txid)],&vout,sizeof(vout)); memcpy(utxo->key,key,sizeof(key)); portable_mutex_lock(&LP_utxomutex); - HASH_ADD(hh,LP_utxoinfos,key,sizeof(key),utxo); + HASH_ADD_KEYPTR(hh,LP_utxoinfos,utxo->key,sizeof(key),utxo); if ( mypeer != 0 ) mypeer->numutxos++; portable_mutex_unlock(&LP_utxomutex); From 756f5162fee13fa204acaf229aa02545ecb5d54e Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:49:56 +0300 Subject: [PATCH 091/339] Test --- iguana/exchanges/LP_nativeDEX.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index e4d0e9c81..df3f186dc 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -105,10 +105,10 @@ struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout, if ( LP_cachekey(ptr->key,base,rel,txid,vout) == sizeof(ptr->key) ) { portable_mutex_lock(&LP_cachemutex); - HASH_ADD(hh,LP_cacheinfos,key,sizeof(ptr->key),ptr); + HASH_ADD_KEYPTR(hh,LP_cacheinfos,ptr->key,sizeof(ptr->key),ptr); portable_mutex_unlock(&LP_cachemutex); } else printf("LP_cacheadd keysize mismatch?\n"); - } + } else printf("CACHE hit!\n"); ptr->price = price; ptr->satoshis = satoshis; ptr->destsatoshis = satoshis * price; @@ -146,6 +146,8 @@ struct LP_utxoinfo *LP_utxofind(bits256 txid,int32_t vout) portable_mutex_lock(&LP_utxomutex); HASH_FIND(hh,LP_utxoinfos,key,sizeof(key),utxo); portable_mutex_unlock(&LP_utxomutex); + if ( utxo != 0 ) + printf("found utxo\n"); return(utxo); } From 9e6986cf273ff0a162d461d14bc4440b5d81a8ca Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:52:40 +0300 Subject: [PATCH 092/339] Test --- iguana/exchanges/LP_commands.c | 3 +++ iguana/exchanges/LP_nativeDEX.c | 1 + 2 files changed, 4 insertions(+) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index bac7fd116..c4c7ce92c 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -190,7 +190,10 @@ char *LP_quote(uint32_t reserved,char *base,char *rel,bits256 txid,int32_t vout, { struct LP_cacheinfo *ptr; if ( (ptr= LP_cacheadd(base,rel,txid,vout,price,satoshis)) != 0 ) + { +LP_cacheadd(base,rel,txid,vout,price,satoshis); return(clonestr("{\"result\":\"updated\"}")); + } else return(clonestr("{\"error\":\"nullptr\"}")); } diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index df3f186dc..d89d43726 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -405,6 +405,7 @@ int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs { txid = jbits256(item,"txid"); utxo = LP_addutxo(amclient,mypeer,mypubsock,jstr(item,"coin"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"deposit"),jint(item,"dvout"),SATOSHIDEN * jdouble(item,"dvalue"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); + utxo = LP_addutxo(amclient,mypeer,mypubsock,jstr(item,"coin"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"deposit"),jint(item,"dvout"),SATOSHIDEN * jdouble(item,"dvalue"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); if ( utxo != 0 ) utxo->lasttime = now; } From 52fbae0f201a04fe589218d921cfaa0ae5778f87 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:54:41 +0300 Subject: [PATCH 093/339] Test --- iguana/exchanges/LP_commands.c | 3 --- iguana/exchanges/LP_nativeDEX.c | 3 +-- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index c4c7ce92c..bac7fd116 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -190,10 +190,7 @@ char *LP_quote(uint32_t reserved,char *base,char *rel,bits256 txid,int32_t vout, { struct LP_cacheinfo *ptr; if ( (ptr= LP_cacheadd(base,rel,txid,vout,price,satoshis)) != 0 ) - { -LP_cacheadd(base,rel,txid,vout,price,satoshis); return(clonestr("{\"result\":\"updated\"}")); - } else return(clonestr("{\"error\":\"nullptr\"}")); } diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index d89d43726..e659471c6 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -294,7 +294,7 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 if ( bits256_cmp(txid,utxo->txid) != 0 || bits256_cmp(deposittxid,utxo->deposittxid) != 0 || vout != utxo->vout || satoshis != utxo->satoshis || depositvout != utxo->depositvout || depositsatoshis != utxo->depositsatoshis || strcmp(coin,utxo->coin) != 0 || strcmp(spendscript,utxo->spendscript) != 0 || strcmp(coinaddr,utxo->coinaddr) != 0 || strcmp(ipaddr,utxo->ipaddr) != 0 || port != utxo->port ) { utxo->errors++; - char str[65]; printf("error on subsequent utxo add.(%s) %d %d %d %d %d %d %d %d %d %d %d\n",bits256_str(str,txid),bits256_cmp(txid,utxo->txid) != 0,bits256_cmp(deposittxid,utxo->deposittxid) != 0,vout != utxo->vout,satoshis != utxo->satoshis,depositvout != utxo->depositvout,depositsatoshis != utxo->depositsatoshis,strcmp(coin,utxo->coin) != 0,strcmp(spendscript,utxo->spendscript) != 0,strcmp(coinaddr,utxo->coinaddr) != 0,strcmp(ipaddr,utxo->ipaddr) != 0,port != utxo->port); + char str[65],str2[65]; printf("error on subsequent utxo add.(%s v %s) %d %d %d %d %d %d %d %d %d %d %d\n",bits256_str(str,txid),bits256_str(str2,utxo->txid),bits256_cmp(txid,utxo->txid) != 0,bits256_cmp(deposittxid,utxo->deposittxid) != 0,vout != utxo->vout,satoshis != utxo->satoshis,depositvout != utxo->depositvout,depositsatoshis != utxo->depositsatoshis,strcmp(coin,utxo->coin) != 0,strcmp(spendscript,utxo->spendscript) != 0,strcmp(coinaddr,utxo->coinaddr) != 0,strcmp(ipaddr,utxo->ipaddr) != 0,port != utxo->port); } else if ( profitmargin != 0. ) utxo->profitmargin = profitmargin; @@ -405,7 +405,6 @@ int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs { txid = jbits256(item,"txid"); utxo = LP_addutxo(amclient,mypeer,mypubsock,jstr(item,"coin"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"deposit"),jint(item,"dvout"),SATOSHIDEN * jdouble(item,"dvalue"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); - utxo = LP_addutxo(amclient,mypeer,mypubsock,jstr(item,"coin"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"deposit"),jint(item,"dvout"),SATOSHIDEN * jdouble(item,"dvalue"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); if ( utxo != 0 ) utxo->lasttime = now; } From 62cdff4746614bfa67d8d99fa25b45edebc15160 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 14:58:16 +0300 Subject: [PATCH 094/339] Test --- iguana/exchanges/LP_nativeDEX.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index e659471c6..7cdf69448 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -309,23 +309,20 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 safecopy(utxo->coin,coin,sizeof(utxo->coin)); safecopy(utxo->coinaddr,coinaddr,sizeof(utxo->coinaddr)); safecopy(utxo->spendscript,spendscript,sizeof(utxo->spendscript)); + utxo->txid = txid; + utxo->vout = vout; + utxo->satoshis = satoshis; if ( amclient == 0 ) { - utxo->txid = txid; - utxo->vout = vout; - utxo->satoshis = satoshis; utxo->deposittxid = deposittxid; utxo->depositvout = depositvout; utxo->depositsatoshis = depositsatoshis; } else { - utxo->feetxid = txid; - utxo->feevout = vout; - utxo->feesatoshis = satoshis; - utxo->txid = deposittxid; - utxo->vout = depositvout; - utxo->satoshis = depositsatoshis; + utxo->feetxid = deposittxid; + utxo->feevout = depositvout; + utxo->feesatoshis = depositsatoshis; } memcpy(key,txid.bytes,sizeof(txid)); memcpy(&key[sizeof(txid)],&vout,sizeof(vout)); @@ -825,7 +822,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb values[i] = 0, used++; if ( amclient == 0 ) LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin); - else LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,"127.0.0.1",0,0); + else LP_addutxo(amclient,mypeer,mypubsock,symbol,deposittxid,depositvout,depositval,txid,vout,value,script,coinaddr,"127.0.0.1",0,0); total += value; } } From df5a754c030e903fcc50507ecf82e71796891ad4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 15:02:18 +0300 Subject: [PATCH 095/339] Test --- iguana/exchanges/LP_nativeDEX.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 7cdf69448..bbfb43648 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -108,12 +108,13 @@ struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout, HASH_ADD_KEYPTR(hh,LP_cacheinfos,ptr->key,sizeof(ptr->key),ptr); portable_mutex_unlock(&LP_cachemutex); } else printf("LP_cacheadd keysize mismatch?\n"); - } else printf("CACHE hit!\n"); + } //else printf("CACHE hit!\n"); + if ( price != ptr->price ) + printf("updated %s/%s %llu price %.8f\n",base,rel,(long long)satoshis,price); ptr->price = price; ptr->satoshis = satoshis; ptr->destsatoshis = satoshis * price; ptr->timestamp = (uint32_t)time(NULL); - printf("updated %s/%s %llu price %.8f\n",base,rel,(long long)satoshis,price); return(ptr); } @@ -146,8 +147,6 @@ struct LP_utxoinfo *LP_utxofind(bits256 txid,int32_t vout) portable_mutex_lock(&LP_utxomutex); HASH_FIND(hh,LP_utxoinfos,key,sizeof(key),utxo); portable_mutex_unlock(&LP_utxomutex); - if ( utxo != 0 ) - printf("found utxo\n"); return(utxo); } @@ -312,18 +311,9 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 utxo->txid = txid; utxo->vout = vout; utxo->satoshis = satoshis; - if ( amclient == 0 ) - { - utxo->deposittxid = deposittxid; - utxo->depositvout = depositvout; - utxo->depositsatoshis = depositsatoshis; - } - else - { - utxo->feetxid = deposittxid; - utxo->feevout = depositvout; - utxo->feesatoshis = depositsatoshis; - } + utxo->deposittxid = deposittxid; + utxo->depositvout = depositvout; + utxo->depositsatoshis = depositsatoshis; memcpy(key,txid.bytes,sizeof(txid)); memcpy(&key[sizeof(txid)],&vout,sizeof(vout)); memcpy(utxo->key,key,sizeof(key)); From 9cfc48cd44fbf2c515051c3c1af43b999489f8a9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 15:11:43 +0300 Subject: [PATCH 096/339] Test --- iguana/exchanges/LP_commands.c | 4 ++-- iguana/exchanges/LP_nativeDEX.c | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index bac7fd116..79fb01c98 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -93,12 +93,12 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) estimatedbase = myutxo->satoshis / price; if ( estimatedbase <= 0 ) return(0); - printf("%s -> %s price %.8f mysatoshis %llu estimated base %llu\n",base,myutxo->coin,price,(long long)myutxo->satoshis,(long long)estimatedbase); + //printf("%s -> %s price %.8f mysatoshis %llu estimated base %llu\n",base,myutxo->coin,price,(long long)myutxo->satoshis,(long long)estimatedbase); HASH_ITER(hh,LP_peerinfos,peer,tmp) { if ( (utxostr= issue_LP_clientgetutxos(peer->ipaddr,peer->port,base,100)) != 0 ) { - printf("%s:%u %s\n",peer->ipaddr,peer->port,utxostr); + //printf("%s:%u %s\n",peer->ipaddr,peer->port,utxostr); if ( (array= cJSON_Parse(utxostr)) != 0 ) { if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index bbfb43648..3f24c632c 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -767,7 +767,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb { char tmpstr[128]; bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); - printf("%s coinaddr.(%s) %d wif.(%s) passphrase.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr,passphrase); + //printf("%s coinaddr.(%s) %d wif.(%s) passphrase.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr,passphrase); if ( (retjson= LP_importprivkey(coin->symbol,tmpstr,coinaddr,-1)) != 0 ) printf("importprivkey -> (%s)\n",jprint(retjson,1)); } @@ -783,9 +783,9 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb item = jitem(array,i); satoshis = SATOSHIDEN * jdouble(item,"amount"); values[i] = satoshis; - printf("%.8f ",dstr(satoshis)); + //printf("%.8f ",dstr(satoshis)); } - printf("array.%d\n",n); + //printf("array.%d\n",n); used = 0; while ( used < n ) { @@ -800,7 +800,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb if ( amclient != 0 ) targetval = (depositval / 777); else targetval = (depositval / 9) * 8; - printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); + //printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); if ( (i= LP_nearestvalue(values,n,targetval)) >= 0 ) { item = jitem(array,i); @@ -867,7 +867,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i while ( 1 ) { nonz = n = 0; - if ( (counter++ % 60) == 0 ) + if ( (counter++ % 3600) == 0 ) LP_privkey_updates(mypeer,pubsock,passphrase,amclient); HASH_ITER(hh,LP_peerinfos,peer,tmp) { @@ -880,11 +880,11 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i portable_mutex_lock(&LP_commandmutex); if ( (retstr= stats_JSON(argjson,"127.0.0.1",mypubport)) != 0 ) { - printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); + //printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); free(retstr); } portable_mutex_unlock(&LP_commandmutex); - printf("subloop.(%s)\n",jprint(argjson,0)); + //printf("subloop.(%s)\n",jprint(argjson,0)); free_json(argjson); } else printf("error parsing.(%s)\n",(char *)ptr); if ( ptr != 0 ) From 32c88d96dc85834ed9f4dc81a348303cbb76afbf Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 15:17:55 +0300 Subject: [PATCH 097/339] Test --- iguana/exchanges/LP_bitcoin.c | 12 ++++++------ iguana/exchanges/LP_nativeDEX.c | 8 +++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 1a4876b34..319552e58 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -26,9 +26,9 @@ bits256 LP_privkeyfind(uint8_t rmd160[20]) for (i=0; i no privkey\n"); + //for (i=0; i<20; i++) + // printf("%02x",rmd160[i]); + //printf(" -> no privkey\n"); return(zero); } @@ -40,9 +40,9 @@ int32_t LP_privkeyadd(bits256 privkey,uint8_t rmd160[20]) return(-bits256_cmp(privkey,tmpkey)); LP_privkeys[LP_numprivkeys].privkey = privkey; memcpy(LP_privkeys[LP_numprivkeys].rmd160,rmd160,20); - int32_t i; for (i=0; i<20; i++) - printf("%02x",rmd160[i]); - char str[65]; printf(" -> add privkey.(%s)\n",bits256_str(str,privkey)); + //int32_t i; for (i=0; i<20; i++) + // printf("%02x",rmd160[i]); + //char str[65]; printf(" -> add privkey.(%s)\n",bits256_str(str,privkey)); LP_numprivkeys++; return(LP_numprivkeys); } diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 3f24c632c..1ed13badd 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -834,6 +834,7 @@ void LP_privkey_updates(struct LP_peerinfo *mypeer,int32_t pubsock,char *passphr void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,int32_t pullsock,uint16_t myport,int32_t amclient,char *passphrase,double profitmargin) { + static uint16_t tmpport; char *retstr; uint8_t r; int32_t i,n,j,len,recvsize,counter=0,nonz,lastn; struct LP_peerinfo *peer,*tmp; void *ptr; cJSON *argjson; if ( amclient == 0 ) { @@ -852,14 +853,15 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i i = (r + j) % (sizeof(default_LPnodes)/sizeof(*default_LPnodes)); LP_peersquery(amclient,mypeer,pubsock,default_LPnodes[i],myport,"127.0.0.1",myport,profitmargin); } - myport += 10; } - if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&myport) != 0 ) + if ( amclient != 0 ) + tmpport = myport + 10; + else tmpport = myport; + if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&tmpport) != 0 ) { printf("error launching stats rpcloop for port.%u\n",myport); exit(-1); } - myport -= 10 * amclient; for (i=0; i Date: Tue, 30 May 2017 15:23:17 +0300 Subject: [PATCH 098/339] Test --- iguana/exchanges/LP_commands.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 79fb01c98..dc8ec5b77 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -337,13 +337,13 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis")); - else if ( strcmp(method,"getpeers") == 0 ) + else if ( IAMCLIENT == 0 && strcmp(method,"getpeers") == 0 ) retstr = LP_peers(); - else if ( strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) + else if ( IAMCLIENT == 0 && strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) retstr = LP_utxos(LP_mypeer,coin,jint(argjson,"lastn")); - else if ( strcmp(method,"notify") == 0 ) + else if ( IAMCLIENT == 0 && strcmp(method,"notify") == 0 ) retstr = clonestr("{\"result\":\"success\",\"notify\":\"received\"}"); - else if ( strcmp(method,"notifyutxo") == 0 ) + else if ( IAMCLIENT == 0 && strcmp(method,"notifyutxo") == 0 ) { printf("utxonotify.(%s)\n",jprint(argjson,0)); LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"deposit"),jint(argjson,"dvout"),SATOSHIDEN * jdouble(argjson,"dvalue"),jstr(argjson,"script"),jstr(argjson,"address"),jstr(argjson,"ipaddr"),juint(argjson,"port"),jdouble(argjson,"profit")); From 255e6cd4d834225ef9b807294896f75db59fa7c7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 15:25:45 +0300 Subject: [PATCH 099/339] Test --- iguana/exchanges/LP_nativeDEX.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 1ed13badd..2a6b1f428 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -166,6 +166,7 @@ cJSON *LP_utxojson(struct LP_utxoinfo *utxo) jaddnum(item,"port",utxo->port); jaddnum(item,"profit",utxo->profitmargin); jaddstr(item,"base",utxo->coin); + jaddstr(item,"coin",utxo->coin); jaddstr(item,"address",utxo->coinaddr); jaddstr(item,"script",utxo->spendscript); jaddbits256(item,"txid",utxo->txid); From aecd6f7cd85283128126af9918887e3c3958bc83 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 15:31:50 +0300 Subject: [PATCH 100/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 2a6b1f428..1956addb1 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -418,7 +418,7 @@ char *issue_LP_getpeers(char *destip,uint16_t destport,char *ipaddr,uint16_t por sprintf(url,"http://%s:%u/api/stats/getpeers?ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,ipaddr,port,profitmargin,numpeers,numutxos); //printf("send.(%s)\n",url); retstr = issue_curl(url); - //printf("GETPEERS.(%s)\n",retstr); + printf("GETPEERS.(%s)\n",retstr); return(retstr); } From 66c011ff1c8f333b3c74aeb09c4907c6d80620a4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 15:34:30 +0300 Subject: [PATCH 101/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 1956addb1..71fbcc1b6 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -26,7 +26,7 @@ char *activecoins[] = { "BTC", "KMD", };//"LTC", "USD", "REVS", "JUMBLR" }; -char *default_LPnodes[] = { "5.9.253.195", "5.9.253.196", "5.9.253.197", "5.9.253.198", "5.9.253.199", "5.9.253.200", "5.9.253.201", "5.9.253.202", "5.9.253.203", "5.9.253.204" }; +char *default_LPnodes[] = { "5.9.253.196", "5.9.253.197", "5.9.253.198", "5.9.253.199", "5.9.253.200", "5.9.253.201", "5.9.253.202", "5.9.253.203", "5.9.253.204" }; //"5.9.253.195", portable_mutex_t LP_peermutex,LP_utxomutex,LP_commandmutex,LP_cachemutex; int32_t LP_mypubsock = -1; int32_t Client_connections; @@ -418,7 +418,7 @@ char *issue_LP_getpeers(char *destip,uint16_t destport,char *ipaddr,uint16_t por sprintf(url,"http://%s:%u/api/stats/getpeers?ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,ipaddr,port,profitmargin,numpeers,numutxos); //printf("send.(%s)\n",url); retstr = issue_curl(url); - printf("GETPEERS.(%s)\n",retstr); + //printf("GETPEERS.(%s)\n",retstr); return(retstr); } From 968420f4f5d0fbf231a7a0728afe485c50371e7a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 30 May 2017 15:41:35 +0300 Subject: [PATCH 102/339] Test --- iguana/exchanges/LP_commands.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index dc8ec5b77..27775d77b 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -118,7 +118,8 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) } else icopy = jduplicate(item); if ( icopy != 0 ) { - jaddnum(icopy,"price",price); + if ( price != 0. ) + jaddnum(icopy,"price",price); jaddi(retarray,icopy); } } @@ -178,7 +179,12 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) } } if ( besti >= 0 ) + { bestitem = jduplicate(jitem(array,besti)); + if ( jobj(bestitem,"price") != 0 ) + jdelete(bestitem,"price"); + jaddnum(bestitem,"price",prices[besti]); + } } free_json(array); } From 6591a7ae37190aa00cb5bd73528b845e152d9321 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 10:24:12 +0300 Subject: [PATCH 103/339] Test --- iguana/exchanges/LP_commands.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 27775d77b..43c7f3b2f 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -141,22 +141,23 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) bestprice = 0.; if ( (array= LP_tradecandidates(utxo,base)) != 0 ) { - printf("%s %.8f -> (%s)\n",utxo->coin,dstr(utxo->satoshis),jprint(array,0)); if ( (n= cJSON_GetArraySize(array)) > 0 ) { memset(prices,0,sizeof(prices)); memset(destsatoshis,0,sizeof(destsatoshis)); + //BTC 0.02500000 -> ([{"ipaddr":"5.9.253.196","port":7779,"profit":0.01035000,"base":"KMD","coin":"KMD","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","script":"76a914434009423522682bd7cc1b18a614c3096d19683188ac","txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"value":100,"deposit":"07902a65d11f0f577a0346432bcd2b6b53de5554c314209d1964693962524d69","dvout":1,"dvalue":120}]) for (i=0; icoin,jbits256(item,"txid"),jint(item,"vout")); - if ( destsatoshis[i] != 0 && (double)j64bits(item,"satoshis")/destsatoshis[i] > price ) + if ( destsatoshis[i] != 0 && (double)j64bits(item,"value")/destsatoshis[i] > price ) price = (double)j64bits(item,"satoshis")/destsatoshis[i]; } if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) ) bestprice = price; + printf("i.%d price %.8f bestprice %.8f\n",i,price,bestprice); } if ( bestprice != 0. ) { @@ -236,6 +237,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ if ( (desttxfee= LP_getestimatedrate(rel) * LP_AVETXSIZE) < 10000 ) desttxfee = 10000; jadd64bits(retjson,"desttxfee",desttxfee); + jadd64bits(retjson,"value",utxo->satoshis); jadd64bits(retjson,"satoshis",utxo->satoshis - txfee); jadd64bits(retjson,"destsatoshis",price * (utxo->satoshis-txfee) + desttxfee); if ( strcmp(method,"request") == 0 ) From 2a9a4cca7ae2fcb3c3b93ea8bbb33b1692269db6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 10:33:22 +0300 Subject: [PATCH 104/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 43c7f3b2f..f37c6ae76 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -157,7 +157,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) } if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) ) bestprice = price; - printf("i.%d price %.8f bestprice %.8f\n",i,price,bestprice); + printf("i.%d price %.8f bestprice %.8f: (%s)\n",i,price,bestprice,jprint(item,0)); } if ( bestprice != 0. ) { From 8b01aa3518ff53c2e0b6874016fb36162d5f2a9a Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 10:36:11 +0300 Subject: [PATCH 105/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index f37c6ae76..503bb64ee 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -53,7 +53,7 @@ double LP_pricequery(uint64_t *destsatoshisp,char *ipaddr,uint16_t port,char *ba jaddstr(reqjson,"rel",rel); jaddstr(reqjson,"method","price"); LP_send(pushsock,jprint(reqjson,1),1); - for (i=0; i<10; i++) + for (i=0; i<30; i++) { if ( (price= LP_pricecache(destsatoshisp,base,rel,txid,vout)) != 0. ) break; From 28ce67314f97977a268de2c3c79ad325a5997d69 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 10:38:27 +0300 Subject: [PATCH 106/339] Test --- iguana/exchanges/LP_nativeDEX.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 71fbcc1b6..ea9d6f7cd 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -124,6 +124,7 @@ double LP_pricecache(uint64_t *destsatoshisp,char *base,char *rel,bits256 txid,i if ( (ptr= LP_cachefind(base,rel,txid,vout)) != 0 ) { *destsatoshisp = ptr->destsatoshis; + printf("found %s/%s %.8f\n",base,rel,ptr->price); return(ptr->price); } else *destsatoshisp = 0; return(0.); From cb50f0d7505872d0ec90251301801717c5170163 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 10:42:17 +0300 Subject: [PATCH 107/339] Test --- iguana/exchanges/LP_nativeDEX.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index ea9d6f7cd..b63143f0e 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -96,6 +96,22 @@ struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout return(ptr); } +double LP_pricecache(uint64_t *destsatoshisp,char *base,char *rel,bits256 txid,int32_t vout) +{ + struct LP_cacheinfo *ptr; + if ( (ptr= LP_cachefind(base,rel,txid,vout)) != 0 ) + { + if ( destsatoshisp != 0 ) + *destsatoshisp = ptr->destsatoshis; + printf("found %s/%s %.8f\n",base,rel,ptr->price); + return(ptr->price); + } + else if ( destsatoshisp != 0 ) + *destsatoshisp = 0; + char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); + return(0.); +} + struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout,double price,uint64_t satoshis) { struct LP_cacheinfo *ptr=0; @@ -115,21 +131,10 @@ struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout, ptr->satoshis = satoshis; ptr->destsatoshis = satoshis * price; ptr->timestamp = (uint32_t)time(NULL); + LP_pricecache(0,base,rel,txid,vout); return(ptr); } -double LP_pricecache(uint64_t *destsatoshisp,char *base,char *rel,bits256 txid,int32_t vout) -{ - struct LP_cacheinfo *ptr; - if ( (ptr= LP_cachefind(base,rel,txid,vout)) != 0 ) - { - *destsatoshisp = ptr->destsatoshis; - printf("found %s/%s %.8f\n",base,rel,ptr->price); - return(ptr->price); - } else *destsatoshisp = 0; - return(0.); -} - struct LP_peerinfo *LP_peerfind(uint32_t ipbits,uint16_t port) { struct LP_peerinfo *peer=0; uint64_t ip_port; @@ -326,7 +331,7 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 portable_mutex_unlock(&LP_utxomutex); if ( mypubsock >= 0 ) LP_send(mypubsock,jprint(LP_utxojson(utxo),1),1); - printf("%s:%u LP_addutxo.(%.8f %.8f) numutxos.%d\n",ipaddr,port,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0); + printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0); } return(utxo); } From 8bd51e51a1b10d01ade34c7d21090b230b6bcdda Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 10:45:19 +0300 Subject: [PATCH 108/339] Test --- iguana/exchanges/LP_nativeDEX.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index b63143f0e..968c7bfbc 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -121,12 +121,12 @@ struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout, if ( LP_cachekey(ptr->key,base,rel,txid,vout) == sizeof(ptr->key) ) { portable_mutex_lock(&LP_cachemutex); - HASH_ADD_KEYPTR(hh,LP_cacheinfos,ptr->key,sizeof(ptr->key),ptr); + HASH_ADD(hh,LP_cacheinfos,key,sizeof(ptr->key),ptr); portable_mutex_unlock(&LP_cachemutex); } else printf("LP_cacheadd keysize mismatch?\n"); } //else printf("CACHE hit!\n"); - if ( price != ptr->price ) - printf("updated %s/%s %llu price %.8f\n",base,rel,(long long)satoshis,price); + char str[65]; if ( price != ptr->price ) + printf("updated %s/v%d %s/%s %llu price %.8f\n",bits256_str(str,txid),vout,base,rel,(long long)satoshis,price); ptr->price = price; ptr->satoshis = satoshis; ptr->destsatoshis = satoshis * price; From 5b4ac8c57769c7e7020f9f3c78a728b379f189ba Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 10:50:45 +0300 Subject: [PATCH 109/339] Test --- iguana/exchanges/LP_commands.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 503bb64ee..592ad938b 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -193,11 +193,17 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) return(bestitem); } -char *LP_quote(uint32_t reserved,char *base,char *rel,bits256 txid,int32_t vout,double price,uint64_t satoshis) +char *LP_quote(uint32_t reserved,char *base,char *rel,bits256 txid,int32_t vout,double price,uint64_t satoshis,uint64_t txfee,uint64_t destsatoshis,uint64_t desttxfee) { struct LP_cacheinfo *ptr; if ( (ptr= LP_cacheadd(base,rel,txid,vout,price,satoshis)) != 0 ) + { + //SENT.({"base":"KMD","rel":"BTC","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","timestamp":1496216835,"price":0.00021141,"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","srchash":"0bcabd875bfa724e26de5f35035ca3767c50b30960e23cbfcbd478cac9147412","txfee":"100000","desttxfee":"10000","value":"10000000000","satoshis":"9999900000","destsatoshis":"2124104","method":"quote"}) + ptr->txfee = txfee; + ptr->destsatoshis = destsatoshis; + ptr->desttxfee = desttxfee; return(clonestr("{\"result\":\"updated\"}")); + } else return(clonestr("{\"error\":\"nullptr\"}")); } @@ -229,6 +235,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ jaddnum(retjson,"timestamp",time(NULL)); jaddnum(retjson,"price",price); jaddbits256(retjson,"txid",txid); + jaddnum(retjson,"vout",utxo->vout); pubkey = LP_pubkey(LP_privkey(utxo->coinaddr)); jaddbits256(retjson,"srchash",pubkey); if ( (txfee= LP_getestimatedrate(base)*LP_AVETXSIZE) < 10000 ) @@ -344,7 +351,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } } if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) - retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis")); + retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis"),j64bits(argjson,"txfee"),j64bits(argjson,"destsatoshis"),j64bits(argjson,"desttxfee")); else if ( IAMCLIENT == 0 && strcmp(method,"getpeers") == 0 ) retstr = LP_peers(); else if ( IAMCLIENT == 0 && strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) From adac8c4d75cad0594dfb206d123a0032eefa44fb Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 10:51:33 +0300 Subject: [PATCH 110/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 968c7bfbc..834483132 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -61,7 +61,7 @@ struct LP_cacheinfo UT_hash_handle hh; uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(int32_t)]; double price; - uint64_t satoshis,destsatoshis; + uint64_t satoshis,txfee,destsatoshis,desttxfee; uint32_t timestamp; } *LP_cacheinfos; From 614c8bd979d11300b7c89ece75e900f1445eefaa Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 11:05:36 +0300 Subject: [PATCH 111/339] Test --- iguana/exchanges/LP_commands.c | 21 +++++++++++++-------- iguana/exchanges/LP_nativeDEX.c | 15 ++++++++++----- iguana/exchanges/mm.c | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 592ad938b..7f8e9ac2d 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -35,7 +35,7 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch return(rp); } -double LP_pricequery(uint64_t *destsatoshisp,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout) +double LP_pricequery(uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout) { cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,pushsock = -1; double price = 0.; if ( ipaddr != 0 && port >= 1000 ) @@ -53,11 +53,11 @@ double LP_pricequery(uint64_t *destsatoshisp,char *ipaddr,uint16_t port,char *ba jaddstr(reqjson,"rel",rel); jaddstr(reqjson,"method","price"); LP_send(pushsock,jprint(reqjson,1),1); - for (i=0; i<30; i++) + for (i=0; i<10; i++) { - if ( (price= LP_pricecache(destsatoshisp,base,rel,txid,vout)) != 0. ) + if ( (price= LP_pricecache(txfeep,destsatoshisp,desttxfeep,base,rel,txid,vout)) != 0. ) break; - usleep(100000); + usleep(250000); } } else printf("no pushsock for peer.%s:%u\n",ipaddr,port); } else printf("cant find/create peer.%s:%u\n",ipaddr,port); @@ -87,7 +87,7 @@ int32_t LP_sizematch(uint64_t mysatoshis,uint64_t othersatoshis) cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) { - struct LP_peerinfo *peer,*tmp; char *utxostr,coinstr[16]; cJSON *array,*icopy,*retarray=0,*item; int32_t i,n; double price; int64_t estimatedbase; uint64_t destsatoshis; + struct LP_peerinfo *peer,*tmp; char *utxostr,coinstr[16]; cJSON *array,*icopy,*retarray=0,*item; int32_t i,n; double price; int64_t estimatedbase; uint64_t txfee,destsatoshis,desttxfee; if ( (price= LP_price(base,myutxo->coin)) == .0 ) return(0); estimatedbase = myutxo->satoshis / price; @@ -111,7 +111,7 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) if ( strcmp(coinstr,base) == 0 && LP_sizematch(estimatedbase,j64bits(item,"satoshis")) == 0 ) { icopy = 0; - if ( (price= LP_pricecache(&destsatoshis,base,myutxo->coin,jbits256(item,"txid"),jint(item,"vout"))) != 0. ) + if ( (price= LP_pricecache(&txfee,&destsatoshis,&desttxfee,base,myutxo->coin,jbits256(item,"txid"),jint(item,"vout"))) != 0. ) { if ( LP_sizematch(myutxo->satoshis,destsatoshis) == 0 ) icopy = jduplicate(item); @@ -137,21 +137,23 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) { - int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; uint64_t destsatoshis[100]; + int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; uint64_t txfees[100],destsatoshis[100],desttxfees[100]; bestprice = 0.; if ( (array= LP_tradecandidates(utxo,base)) != 0 ) { if ( (n= cJSON_GetArraySize(array)) > 0 ) { memset(prices,0,sizeof(prices)); + memset(txfees,0,sizeof(txfees)); memset(destsatoshis,0,sizeof(destsatoshis)); + memset(desttxfees,0,sizeof(desttxfees)); //BTC 0.02500000 -> ([{"ipaddr":"5.9.253.196","port":7779,"profit":0.01035000,"base":"KMD","coin":"KMD","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","script":"76a914434009423522682bd7cc1b18a614c3096d19683188ac","txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"value":100,"deposit":"07902a65d11f0f577a0346432bcd2b6b53de5554c314209d1964693962524d69","dvout":1,"dvalue":120}]) for (i=0; icoin,jbits256(item,"txid"),jint(item,"vout")); + price = LP_pricequery(&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout")); if ( destsatoshis[i] != 0 && (double)j64bits(item,"value")/destsatoshis[i] > price ) price = (double)j64bits(item,"satoshis")/destsatoshis[i]; } @@ -185,6 +187,9 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); jaddnum(bestitem,"price",prices[besti]); + jadd64bits(bestitem,"txfee",txfees[besti]); + jadd64bits(bestitem,"desttxfee",desttxfees[besti]); + jadd64bits(bestitem,"destsatoshis",destsatoshis[besti]); } } free_json(array); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 834483132..20ac4504a 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -17,6 +17,8 @@ // LP_nativeDEX.c // marketmaker // +// jl777: fix price calcs based on specific txfees +// jl777: add change output #include #include "LP_include.h" @@ -90,25 +92,29 @@ struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout { printf("expire price %.8f\n",ptr->price); ptr->price = 0.; - ptr->destsatoshis = 0; + ptr->destsatoshis = ptr->txfee = ptr->desttxfee = 0; ptr->timestamp = 0; } return(ptr); } -double LP_pricecache(uint64_t *destsatoshisp,char *base,char *rel,bits256 txid,int32_t vout) +double LP_pricecache(uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *base,char *rel,bits256 txid,int32_t vout) { struct LP_cacheinfo *ptr; if ( (ptr= LP_cachefind(base,rel,txid,vout)) != 0 ) { if ( destsatoshisp != 0 ) + { *destsatoshisp = ptr->destsatoshis; - printf("found %s/%s %.8f\n",base,rel,ptr->price); + *txfeep = ptr->txfee; + *desttxfeep = ptr->desttxfee; + } + //printf("found %s/%s %.8f\n",base,rel,ptr->price); return(ptr->price); } else if ( destsatoshisp != 0 ) *destsatoshisp = 0; - char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); + //char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); return(0.); } @@ -131,7 +137,6 @@ struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout, ptr->satoshis = satoshis; ptr->destsatoshis = satoshis * price; ptr->timestamp = (uint32_t)time(NULL); - LP_pricecache(0,base,rel,txid,vout); return(ptr); } diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 34312e13a..08af48aac 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -840,7 +840,7 @@ int main(int argc, const char * argv[]) HASH_ITER(hh,LP_utxoinfos,utxo,utmp) { if ( (matchjson= LP_bestprice(utxo,"KMD")) != 0 ) - printf("bestprice (%s)\n",jprint(matchjson,1)); + printf("bestprice %.8f txfees %.8f %.8f destsatoshis %.8f (%s)\n",jdouble(matchjson,"price"),dstr(j64bits(matchjson,"txfee")),dstr(j64bits(matchjson,"desttxfee")),dstr(j64bits(matchjson,"destsatoshis")),jprint(matchjson,1)); } } } From 139dcd5960d7069e1987c1375fdc215283d7177a Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 11:09:53 +0300 Subject: [PATCH 112/339] Test --- iguana/exchanges/mm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 08af48aac..34312e13a 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -840,7 +840,7 @@ int main(int argc, const char * argv[]) HASH_ITER(hh,LP_utxoinfos,utxo,utmp) { if ( (matchjson= LP_bestprice(utxo,"KMD")) != 0 ) - printf("bestprice %.8f txfees %.8f %.8f destsatoshis %.8f (%s)\n",jdouble(matchjson,"price"),dstr(j64bits(matchjson,"txfee")),dstr(j64bits(matchjson,"desttxfee")),dstr(j64bits(matchjson,"destsatoshis")),jprint(matchjson,1)); + printf("bestprice (%s)\n",jprint(matchjson,1)); } } } From adbb0513cdbaf5a551b682f0f4d8ffa363476dee Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 11:12:47 +0300 Subject: [PATCH 113/339] Test --- iguana/exchanges/LP_rpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 3e523034c..57a5b6992 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -154,7 +154,7 @@ uint64_t LP_getestimatedrate(char *symbol) { if ( retstr[0] != '-' ) { - rate = atof(retstr); + rate = SATOSHIDEN * atof(retstr); printf("estimated rate %s -> %llu\n",retstr,(long long)rate); } free(retstr); From b26f751fc055ab52da6080623e31e29493e818bf Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 11:12:57 +0300 Subject: [PATCH 114/339] Test --- iguana/exchanges/LP_rpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 57a5b6992..1f027728d 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -149,7 +149,7 @@ uint64_t LP_getestimatedrate(char *symbol) char buf[512],*retstr; uint64_t rate = 200; struct iguana_info *coin = LP_coinfind(symbol); if ( coin != 0 ) { - sprintf(buf,"[%d]",13); + sprintf(buf,"[%d]",3); if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"estimatefee",buf)) != 0 ) { if ( retstr[0] != '-' ) From 51e4c17acd4fbfe0005debba0718c96b146807e7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 11:17:00 +0300 Subject: [PATCH 115/339] Test --- iguana/exchanges/LP_include.h | 2 +- iguana/exchanges/LP_nativeDEX.c | 4 ++-- iguana/exchanges/LP_rpc.c | 2 +- iguana/exchanges/LP_transaction.c | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index af134d939..a7e239f2b 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -176,7 +176,7 @@ struct basilisk_swapinfo struct iguana_info { - uint64_t txfee,estimatedrate; + uint64_t txfee; double estimatedrate; int32_t longestchain; uint8_t pubtype,p2shtype,isPoS,wiftype; char symbol[16],changeaddr[64],userpass[1024],serverport[128]; diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 20ac4504a..4acbd7645 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -131,8 +131,8 @@ struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout, portable_mutex_unlock(&LP_cachemutex); } else printf("LP_cacheadd keysize mismatch?\n"); } //else printf("CACHE hit!\n"); - char str[65]; if ( price != ptr->price ) - printf("updated %s/v%d %s/%s %llu price %.8f\n",bits256_str(str,txid),vout,base,rel,(long long)satoshis,price); + //char str[65]; if ( price != ptr->price ) + // printf("updated %s/v%d %s/%s %llu price %.8f\n",bits256_str(str,txid),vout,base,rel,(long long)satoshis,price); ptr->price = price; ptr->satoshis = satoshis; ptr->destsatoshis = satoshis * price; diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 1f027728d..a3131ac93 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -154,7 +154,7 @@ uint64_t LP_getestimatedrate(char *symbol) { if ( retstr[0] != '-' ) { - rate = SATOSHIDEN * atof(retstr); + rate = atof(retstr) / 1024.; printf("estimated rate %s -> %llu\n",retstr,(long long)rate); } free(retstr); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 74d820b5f..eea8a63fc 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -877,7 +877,7 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub if ( strcmp(coin->symbol,"BTC") != 0 ) return(retval); len = rawtx->I.datalen; - if ( coin->estimatedrate == 0 ) + if ( coin->estimatedrate == 0. ) coin->estimatedrate = LP_getestimatedrate(coin->symbol); newtxfee = coin->estimatedrate * len; printf("txfee %.8f -> newtxfee %.8f\n",dstr(txfee),dstr(newtxfee)); @@ -888,7 +888,7 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,struct basilisk_swap *swap,struct basilisk_rawtx *dest,struct basilisk_rawtx *rawtx,bits256 privkey,bits256 *privkey2,uint8_t *userdata,int32_t userdatalen,int32_t ignore_cltverr) { - char *signedtx; int64_t txfee,newtxfee=0,estimatedrate,destamount; uint32_t timestamp,locktime=0,sequenceid = 0xffffffff; int32_t iter,len,retval = -1; + char *signedtx; int64_t txfee,newtxfee=0,destamount; uint32_t timestamp,locktime=0,sequenceid = 0xffffffff; int32_t iter,len,retval = -1; double estimatedrate; timestamp = swap->I.started; if ( dest == &swap->aliceclaim ) locktime = swap->bobdeposit.I.locktime + 1, sequenceid = 0; From 5d4c6ca0d7de830202b75dfb3e7c96b8fe319f69 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 11:28:47 +0300 Subject: [PATCH 116/339] Test --- iguana/exchanges/LP_commands.c | 23 +++++++++++++++-------- iguana/exchanges/LP_nativeDEX.c | 13 +++++++++++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 7f8e9ac2d..eb9d3f16e 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -35,7 +35,7 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch return(rp); } -double LP_pricequery(uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout) +double LP_pricequery(bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout) { cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,pushsock = -1; double price = 0.; if ( ipaddr != 0 && port >= 1000 ) @@ -55,7 +55,7 @@ double LP_pricequery(uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfe LP_send(pushsock,jprint(reqjson,1),1); for (i=0; i<10; i++) { - if ( (price= LP_pricecache(txfeep,destsatoshisp,desttxfeep,base,rel,txid,vout)) != 0. ) + if ( (price= LP_pricecache(otherpubp,reservedp,txfeep,destsatoshisp,desttxfeep,base,rel,txid,vout)) != 0. ) break; usleep(250000); } @@ -87,7 +87,7 @@ int32_t LP_sizematch(uint64_t mysatoshis,uint64_t othersatoshis) cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) { - struct LP_peerinfo *peer,*tmp; char *utxostr,coinstr[16]; cJSON *array,*icopy,*retarray=0,*item; int32_t i,n; double price; int64_t estimatedbase; uint64_t txfee,destsatoshis,desttxfee; + struct LP_peerinfo *peer,*tmp; char *utxostr,coinstr[16]; cJSON *array,*icopy,*retarray=0,*item; int32_t i,n; double price; bits256 otherpub; uint32_t reserved; int64_t estimatedbase; uint64_t txfee,destsatoshis,desttxfee; if ( (price= LP_price(base,myutxo->coin)) == .0 ) return(0); estimatedbase = myutxo->satoshis / price; @@ -111,7 +111,7 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) if ( strcmp(coinstr,base) == 0 && LP_sizematch(estimatedbase,j64bits(item,"satoshis")) == 0 ) { icopy = 0; - if ( (price= LP_pricecache(&txfee,&destsatoshis,&desttxfee,base,myutxo->coin,jbits256(item,"txid"),jint(item,"vout"))) != 0. ) + if ( (price= LP_pricecache(&otherpub,&reserved,&txfee,&destsatoshis,&desttxfee,base,myutxo->coin,jbits256(item,"txid"),jint(item,"vout"))) != 0. ) { if ( LP_sizematch(myutxo->satoshis,destsatoshis) == 0 ) icopy = jduplicate(item); @@ -137,13 +137,15 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) { - int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; uint64_t txfees[100],destsatoshis[100],desttxfees[100]; + int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; bits256 otherpubs[100]; uint32_t reserved[100]; uint64_t txfees[100],destsatoshis[100],desttxfees[100]; bestprice = 0.; if ( (array= LP_tradecandidates(utxo,base)) != 0 ) { if ( (n= cJSON_GetArraySize(array)) > 0 ) { memset(prices,0,sizeof(prices)); + memset(reserved,0,sizeof(reserved)); + memset(otherpubs,0,sizeof(otherpubs)); memset(txfees,0,sizeof(txfees)); memset(destsatoshis,0,sizeof(destsatoshis)); memset(desttxfees,0,sizeof(desttxfees)); @@ -153,7 +155,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) item = jitem(array,i); if ( (price= jdouble(item,"price")) == 0. ) { - price = LP_pricequery(&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout")); + price = LP_pricequery(&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout")); if ( destsatoshis[i] != 0 && (double)j64bits(item,"value")/destsatoshis[i] > price ) price = (double)j64bits(item,"satoshis")/destsatoshis[i]; } @@ -186,10 +188,12 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) bestitem = jduplicate(jitem(array,besti)); if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); + jaddnum(bestitem,"reserved",reserved[besti]); jaddnum(bestitem,"price",prices[besti]); jadd64bits(bestitem,"txfee",txfees[besti]); jadd64bits(bestitem,"desttxfee",desttxfees[besti]); jadd64bits(bestitem,"destsatoshis",destsatoshis[besti]); + jaddbits256(bestitem,"otherpub",otherpubs[besti]); } } free_json(array); @@ -198,15 +202,17 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) return(bestitem); } -char *LP_quote(uint32_t reserved,char *base,char *rel,bits256 txid,int32_t vout,double price,uint64_t satoshis,uint64_t txfee,uint64_t destsatoshis,uint64_t desttxfee) +char *LP_quote(uint32_t reserved,char *base,char *rel,bits256 txid,int32_t vout,double price,uint64_t satoshis,uint64_t txfee,uint64_t destsatoshis,uint64_t desttxfee,bits256 otherpub) { struct LP_cacheinfo *ptr; if ( (ptr= LP_cacheadd(base,rel,txid,vout,price,satoshis)) != 0 ) { //SENT.({"base":"KMD","rel":"BTC","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","timestamp":1496216835,"price":0.00021141,"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","srchash":"0bcabd875bfa724e26de5f35035ca3767c50b30960e23cbfcbd478cac9147412","txfee":"100000","desttxfee":"10000","value":"10000000000","satoshis":"9999900000","destsatoshis":"2124104","method":"quote"}) + ptr->reserved = reserved; ptr->txfee = txfee; ptr->destsatoshis = destsatoshis; ptr->desttxfee = desttxfee; + ptr->otherpub = otherpub; return(clonestr("{\"result\":\"updated\"}")); } else return(clonestr("{\"error\":\"nullptr\"}")); @@ -256,6 +262,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ { utxo->swappending = (uint32_t)(time(NULL) + LP_RESERVETIME); utxo->otherpubkey = jbits256(argjson,"pubkey"); + jaddbits256(retjson,"otherpubkey",utxo->otherpubkey); jaddstr(retjson,"method","reserved"); jaddnum(retjson,"pending",utxo->swappending); } else jaddstr(retjson,"method","quote"); @@ -356,7 +363,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } } if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) - retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis"),j64bits(argjson,"txfee"),j64bits(argjson,"destsatoshis"),j64bits(argjson,"desttxfee")); + retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis"),j64bits(argjson,"txfee"),j64bits(argjson,"destsatoshis"),j64bits(argjson,"desttxfee"),jbits256(argjson,"otherpubkey")); else if ( IAMCLIENT == 0 && strcmp(method,"getpeers") == 0 ) retstr = LP_peers(); else if ( IAMCLIENT == 0 && strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 4acbd7645..240b0a8ca 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -61,10 +61,11 @@ struct LP_utxoinfo struct LP_cacheinfo { UT_hash_handle hh; + bits256 otherpub; uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(int32_t)]; double price; uint64_t satoshis,txfee,destsatoshis,desttxfee; - uint32_t timestamp; + uint32_t timestamp,reserved; } *LP_cacheinfos; int32_t LP_cachekey(uint8_t *key,char *base,char *rel,bits256 txid,int32_t vout) @@ -98,22 +99,30 @@ struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout return(ptr); } -double LP_pricecache(uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *base,char *rel,bits256 txid,int32_t vout) +double LP_pricecache(bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *base,char *rel,bits256 txid,int32_t vout) { struct LP_cacheinfo *ptr; if ( (ptr= LP_cachefind(base,rel,txid,vout)) != 0 ) { if ( destsatoshisp != 0 ) { + *otherpubp = ptr->otherpub; *destsatoshisp = ptr->destsatoshis; *txfeep = ptr->txfee; *desttxfeep = ptr->desttxfee; + *reservedp = ptr->reserved; } //printf("found %s/%s %.8f\n",base,rel,ptr->price); return(ptr->price); } else if ( destsatoshisp != 0 ) + { + memset(otherpubp,0,sizeof(*otherpubp)); *destsatoshisp = 0; + *txfeep = 0; + *desttxfeep = 0; + *reservedp = 0; + } //char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); return(0.); } From 005d96d4d23a8424a991044ab4a200eae890c97d Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 11:29:34 +0300 Subject: [PATCH 117/339] Test --- iguana/exchanges/LP_rpc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index a3131ac93..f912440aa 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -144,9 +144,9 @@ int32_t LP_importaddress(char *symbol,char *address) return(1); } -uint64_t LP_getestimatedrate(char *symbol) +double LP_getestimatedrate(char *symbol) { - char buf[512],*retstr; uint64_t rate = 200; struct iguana_info *coin = LP_coinfind(symbol); + char buf[512],*retstr; double rate = 200; struct iguana_info *coin = LP_coinfind(symbol); if ( coin != 0 ) { sprintf(buf,"[%d]",3); @@ -155,7 +155,7 @@ uint64_t LP_getestimatedrate(char *symbol) if ( retstr[0] != '-' ) { rate = atof(retstr) / 1024.; - printf("estimated rate %s -> %llu\n",retstr,(long long)rate); + printf("estimated rate %s -> %.8f\n",retstr,rate); } free(retstr); } From 371375141d66d0cb4c82506a74cc675464a70c2e Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 11:40:29 +0300 Subject: [PATCH 118/339] Test --- iguana/exchanges/LP_commands.c | 24 +++++++++++++++++++----- iguana/exchanges/LP_nativeDEX.c | 10 +++++++--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index eb9d3f16e..236fa8675 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -35,9 +35,9 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch return(rp); } -double LP_pricequery(bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout) +double LP_pricequery(bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout,bits256 mypub) { - cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,pushsock = -1; double price = 0.; + cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,flag = 0,pushsock = -1; double price = 0.; if ( ipaddr != 0 && port >= 1000 ) { if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),port)) == 0 ) @@ -51,12 +51,22 @@ double LP_pricequery(bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uin jaddnum(reqjson,"vout",vout); jaddstr(reqjson,"base",base); jaddstr(reqjson,"rel",rel); - jaddstr(reqjson,"method","price"); + if ( bits256_nonz(mypub) == 0 ) + jaddstr(reqjson,"method","price"); + else + { + flag = 1; + jaddstr(reqjson,"method","request"); + jaddbits256(reqjson,"mypub",mypub); + } LP_send(pushsock,jprint(reqjson,1),1); for (i=0; i<10; i++) { if ( (price= LP_pricecache(otherpubp,reservedp,txfeep,destsatoshisp,desttxfeep,base,rel,txid,vout)) != 0. ) - break; + { + if ( flag == 0 || bits256_nonz(*otherpubp) != 0 ) + break; + } usleep(250000); } } else printf("no pushsock for peer.%s:%u\n",ipaddr,port); @@ -137,6 +147,7 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) { + static bits256 zero; int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; bits256 otherpubs[100]; uint32_t reserved[100]; uint64_t txfees[100],destsatoshis[100],desttxfees[100]; bestprice = 0.; if ( (array= LP_tradecandidates(utxo,base)) != 0 ) @@ -155,7 +166,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) item = jitem(array,i); if ( (price= jdouble(item,"price")) == 0. ) { - price = LP_pricequery(&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout")); + price = LP_pricequery(&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),zero); if ( destsatoshis[i] != 0 && (double)j64bits(item,"value")/destsatoshis[i] > price ) price = (double)j64bits(item,"satoshis")/destsatoshis[i]; } @@ -186,6 +197,9 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) if ( besti >= 0 ) { bestitem = jduplicate(jitem(array,besti)); + i = besti; + item = bestitem; + price = LP_pricequery(&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),myutxo->pubkey); if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); jaddnum(bestitem,"reserved",reserved[besti]); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 240b0a8ca..b970f4dce 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -48,7 +48,7 @@ struct LP_peerinfo struct LP_utxoinfo { UT_hash_handle hh; - bits256 txid,deposittxid,feetxid,otherpubkey; + bits256 txid,deposittxid,feetxid,otherpubkey,mypub; void *swap; uint64_t satoshis,depositsatoshis,feesatoshis; uint8_t key[sizeof(bits256) + sizeof(int32_t)]; @@ -775,7 +775,7 @@ struct iguana_info *LP_coinfind(char *symbol) uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symbol,char *passphrase,char *wifstr,int32_t amclient) { - char coinaddr[64],*script; cJSON *array,*item,*retjson; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; struct iguana_info *coin = LP_coinfind(symbol); + char coinaddr[64],*script; struct LP_utxoinfo *utxo; cJSON *array,*item,*retjson; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; struct iguana_info *coin = LP_coinfind(symbol); if ( coin == 0 ) { printf("cant add privkey for %s, coin not active\n",symbol); @@ -833,7 +833,11 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb values[i] = 0, used++; if ( amclient == 0 ) LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin); - else LP_addutxo(amclient,mypeer,mypubsock,symbol,deposittxid,depositvout,depositval,txid,vout,value,script,coinaddr,"127.0.0.1",0,0); + else + { + if ( (utxo= LP_addutxo(amclient,mypeer,mypubsock,symbol,deposittxid,depositvout,depositval,txid,vout,value,script,coinaddr,"127.0.0.1",0,0)) != 0 ) + utxo->mypub = curve25519(privkey,curve25519_basepoint9()); + } total += value; } } From 9f159a1c43a0689382645749f88f48316ddffee6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 11:41:28 +0300 Subject: [PATCH 119/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 236fa8675..98a9e938a 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -199,7 +199,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) bestitem = jduplicate(jitem(array,besti)); i = besti; item = bestitem; - price = LP_pricequery(&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),myutxo->pubkey); + price = LP_pricequery(&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub); if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); jaddnum(bestitem,"reserved",reserved[besti]); From 93ddd65c94084f8641cf6d8940e339275168897f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 11:42:58 +0300 Subject: [PATCH 120/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 98a9e938a..7039cbfa8 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -275,7 +275,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ if ( strcmp(method,"request") == 0 ) { utxo->swappending = (uint32_t)(time(NULL) + LP_RESERVETIME); - utxo->otherpubkey = jbits256(argjson,"pubkey"); + utxo->otherpubkey = jbits256(argjson,"mypub"); jaddbits256(retjson,"otherpubkey",utxo->otherpubkey); jaddstr(retjson,"method","reserved"); jaddnum(retjson,"pending",utxo->swappending); From 700f42ee7e67ca90e9145c6f13378886a1ba1df9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 11:46:12 +0300 Subject: [PATCH 121/339] Test --- iguana/exchanges/LP_commands.c | 2 +- iguana/exchanges/LP_nativeDEX.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 7039cbfa8..c61238235 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -172,7 +172,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) } if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) ) bestprice = price; - printf("i.%d price %.8f bestprice %.8f: (%s)\n",i,price,bestprice,jprint(item,0)); + //printf("i.%d price %.8f bestprice %.8f: (%s)\n",i,price,bestprice,jprint(item,0)); } if ( bestprice != 0. ) { diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index b970f4dce..01a19cf07 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -980,7 +980,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i nn_freemsg(ptr), ptr = 0; } if ( nonz == 0 ) - sleep(1); + usleep(50000); } } } From e9af28aa079d764aac22660744c006dc5b089854 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 13:35:59 +0300 Subject: [PATCH 122/339] Test --- iguana/exchanges/LP_commands.c | 16 +++++++++------- iguana/exchanges/LP_nativeDEX.c | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index c61238235..c802b249a 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -35,7 +35,7 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch return(rp); } -double LP_pricequery(bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout,bits256 mypub) +double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout,bits256 mypub) { cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,flag = 0,pushsock = -1; double price = 0.; if ( ipaddr != 0 && port >= 1000 ) @@ -51,14 +51,12 @@ double LP_pricequery(bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uin jaddnum(reqjson,"vout",vout); jaddstr(reqjson,"base",base); jaddstr(reqjson,"rel",rel); - if ( bits256_nonz(mypub) == 0 ) - jaddstr(reqjson,"method","price"); - else + if ( bits256_nonz(mypub) != 0 ) { flag = 1; - jaddstr(reqjson,"method","request"); jaddbits256(reqjson,"mypub",mypub); } + jaddstr(reqjson,"method",method); LP_send(pushsock,jprint(reqjson,1),1); for (i=0; i<10; i++) { @@ -166,7 +164,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) item = jitem(array,i); if ( (price= jdouble(item,"price")) == 0. ) { - price = LP_pricequery(&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),zero); + price = LP_query("price",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),zero); if ( destsatoshis[i] != 0 && (double)j64bits(item,"value")/destsatoshis[i] > price ) price = (double)j64bits(item,"satoshis")/destsatoshis[i]; } @@ -199,7 +197,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) bestitem = jduplicate(jitem(array,besti)); i = besti; item = bestitem; - price = LP_pricequery(&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub); + price = LP_query("request",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub); if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); jaddnum(bestitem,"reserved",reserved[besti]); @@ -208,6 +206,10 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) jadd64bits(bestitem,"desttxfee",desttxfees[besti]); jadd64bits(bestitem,"destsatoshis",destsatoshis[besti]); jaddbits256(bestitem,"otherpub",otherpubs[besti]); + if ( LP_price(base,utxo->coin) > 0.975*price ) + { + price = LP_query("connect",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub); + } } } free_json(array); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 01a19cf07..d072833fd 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -436,7 +436,7 @@ char *issue_LP_getpeers(char *destip,uint16_t destport,char *ipaddr,uint16_t por { char url[512],*retstr; sprintf(url,"http://%s:%u/api/stats/getpeers?ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,ipaddr,port,profitmargin,numpeers,numutxos); - //printf("send.(%s)\n",url); + printf("send.(%s)\n",url); retstr = issue_curl(url); //printf("GETPEERS.(%s)\n",retstr); return(retstr); From 9286f78fdd6d1b65ff57b5bdda9988e340c9dc2b Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 13:37:26 +0300 Subject: [PATCH 123/339] Test --- iguana/exchanges/LP_nativeDEX.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index d072833fd..81a16abf9 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -436,7 +436,7 @@ char *issue_LP_getpeers(char *destip,uint16_t destport,char *ipaddr,uint16_t por { char url[512],*retstr; sprintf(url,"http://%s:%u/api/stats/getpeers?ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,ipaddr,port,profitmargin,numpeers,numutxos); - printf("send.(%s)\n",url); + //printf("send.(%s)\n",url); retstr = issue_curl(url); //printf("GETPEERS.(%s)\n",retstr); return(retstr); @@ -446,6 +446,7 @@ char *issue_LP_getutxos(char *destip,uint16_t destport,char *coin,int32_t lastn, { char url[512]; sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,coin,lastn,ipaddr,port,profitmargin,numpeers,numutxos); + printf("getutxos.(%s)\n",url); return(issue_curl(url)); } From c9a2dc8626518783591e6808631420e227b74942 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 13:39:29 +0300 Subject: [PATCH 124/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 81a16abf9..972f1829d 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -446,7 +446,6 @@ char *issue_LP_getutxos(char *destip,uint16_t destport,char *coin,int32_t lastn, { char url[512]; sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,coin,lastn,ipaddr,port,profitmargin,numpeers,numutxos); - printf("getutxos.(%s)\n",url); return(issue_curl(url)); } @@ -454,6 +453,7 @@ char *issue_LP_clientgetutxos(char *destip,uint16_t destport,char *coin,int32_t { char url[512]; sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=127.0.0.1&port=0",destip,destport,coin,lastn); + printf("getutxos.(%s)\n",url); return(issue_curl(url)); } From 76e37735d5e3676633ca88f0a63e0c3747c0b9de Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 13:46:22 +0300 Subject: [PATCH 125/339] Test --- iguana/exchanges/LP_commands.c | 2 ++ iguana/exchanges/LP_nativeDEX.c | 2 +- iguana/exchanges/LP_prices.c | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index c802b249a..cde261c53 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -380,6 +380,8 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis"),j64bits(argjson,"txfee"),j64bits(argjson,"destsatoshis"),j64bits(argjson,"desttxfee"),jbits256(argjson,"otherpubkey")); + else if ( IAMCLIENT == 0 && strcmp(method,"getprice") == 0 ) + retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel")); else if ( IAMCLIENT == 0 && strcmp(method,"getpeers") == 0 ) retstr = LP_peers(); else if ( IAMCLIENT == 0 && strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 972f1829d..d06aaad8b 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -453,7 +453,7 @@ char *issue_LP_clientgetutxos(char *destip,uint16_t destport,char *coin,int32_t { char url[512]; sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=127.0.0.1&port=0",destip,destport,coin,lastn); - printf("getutxos.(%s)\n",url); + //printf("getutxos.(%s)\n",url); return(issue_curl(url)); } diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index 33bc6f897..3804bc029 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -41,3 +41,24 @@ double LP_price(char *base,char *rel) } return(0.); } + +char *LP_pricestr(char *base,char *rel) +{ + double price = 0.; cJSON *retjson; + if ( base != 0 && base[0] != 0 && rel != 0 && rel[0] != 0 ) + price = LP_price(base,rel); + if ( price != 0. ) + { + retjson = cJSON_CreateObject(); + jaddstr(retjson,"result","success"); + jaddnum(retjson,"price",price); + return(jprint(retjson,1)); + } else return(clonestr("{\"error\":\"cant find baserel pair\"}")); +} + + + + + + + From 29aaa66d452341715390c0692f5022f856c2afbf Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 13:47:58 +0300 Subject: [PATCH 126/339] Test --- iguana/exchanges/LP_prices.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index 3804bc029..4d1fff738 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -51,6 +51,8 @@ char *LP_pricestr(char *base,char *rel) { retjson = cJSON_CreateObject(); jaddstr(retjson,"result","success"); + jaddstr(retjson,"base",base); + jaddstr(retjson,"rel",rel); jaddnum(retjson,"price",price); return(jprint(retjson,1)); } else return(clonestr("{\"error\":\"cant find baserel pair\"}")); From 1d7ff64c9c9cd04a51273c9b71baec95c78acfd3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 14:01:43 +0300 Subject: [PATCH 127/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index cde261c53..3a03be77d 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -30,8 +30,8 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch rp->DEXselector = DEXselector; safecopy(rp->src,src,sizeof(rp->src)); safecopy(rp->dest,dest,sizeof(rp->dest)); - rp->quoteid = basilisk_quoteid(rp); rp->requestid = basilisk_requestid(rp); + rp->quoteid = basilisk_quoteid(rp); return(rp); } From 1b18065238f01be01a5a9caa53cbd3d40c405997 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 14:05:55 +0300 Subject: [PATCH 128/339] Test --- iguana/exchanges/LP_swap.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 2a1d96bef..fe2df8a7c 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -177,6 +177,14 @@ void basilisk_swap_finished(struct basilisk_swap *swap) swap->nummessages = 0; } +uint32_t basilisk_quoteid(struct basilisk_request *rp) +{ + struct basilisk_request R; + R = *rp; + R.unused = R.requestid = R.quoteid = R.DEXselector = 0; + return(calc_crc32(0,(void *)&R,sizeof(R))); +} + uint32_t basilisk_requestid(struct basilisk_request *rp) { struct basilisk_request R; @@ -184,25 +192,17 @@ uint32_t basilisk_requestid(struct basilisk_request *rp) R.requestid = R.quoteid = R.quotetime = R.DEXselector = 0; R.destamount = R.unused = 0; memset(R.desthash.bytes,0,sizeof(R.desthash.bytes)); - if ( 0 ) + if ( 1 ) { int32_t i; for (i=0; i %s %.8f %s crc.%u\n",R.timestamp,R.requestid,R.quoteid,R.src,dstr(R.srcamount),bits256_str(str,R.srchash),R.dest,dstr(R.destamount),bits256_str(str2,R.desthash),calc_crc32(0,(void *)&R,sizeof(R))); + char str[65],str2[65]; printf("B REQUESTID: t.%u r.%u q.%u %s %.8f %s -> %s %.8f %s crc.%u q%u\n",R.timestamp,R.requestid,R.quoteid,R.src,dstr(R.srcamount),bits256_str(str,R.srchash),R.dest,dstr(R.destamount),bits256_str(str2,R.desthash),calc_crc32(0,(void *)&R,sizeof(R)),basilisk_quoteid(&R)); } return(calc_crc32(0,(void *)&R,sizeof(R))); } -uint32_t basilisk_quoteid(struct basilisk_request *rp) -{ - struct basilisk_request R; - R = *rp; - R.requestid = R.quoteid = R.unused = R.DEXselector = 0; - return(calc_crc32(0,(void *)&R,sizeof(R))); -} - int32_t LP_pubkeys_data(struct basilisk_swap *swap,uint8_t *data,int32_t maxlen) { int32_t i,datalen = 0; From d2a991f5ec624de1acaab275c32f74785ad54954 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 19:01:35 +0300 Subject: [PATCH 129/339] Test --- iguana/exchanges/LP_commands.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 3a03be77d..7d34bfe5d 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -318,7 +318,22 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_bobloop,(void *)utxo) == 0 ) { retjson = cJSON_CreateObject(); - jaddstr(retjson,"result","connected"); + jaddstr(retjson,"base",base); + jaddstr(retjson,"rel",rel); + jaddstr(retjson,"address",utxo->coinaddr); + jaddnum(retjson,"timestamp",timestamp); + jaddnum(retjson,"quotetime",quotetime); + jaddnum(retjson,"price",price); + jaddbits256(retjson,"txid",txid); + jaddnum(retjson,"vout",utxo->vout); + pubkey = LP_pubkey(LP_privkey(utxo->coinaddr)); + jaddbits256(retjson,"srchash",pubkey); + jadd64bits(retjson,"txfee",txfee); + jadd64bits(retjson,"desttxfee",desttxfee); + jadd64bits(retjson,"value",utxo->satoshis); + jadd64bits(retjson,"satoshis",utxo->satoshis - txfee); + jadd64bits(retjson,"destsatoshis",price * (utxo->satoshis-txfee) + desttxfee); + jaddstr(retjson,"method","connected"); jaddstr(retjson,"pair",pairstr); jaddnum(retjson,"requestid",R.requestid); jaddnum(retjson,"quoteid",R.quoteid); @@ -380,6 +395,8 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis"),j64bits(argjson,"txfee"),j64bits(argjson,"destsatoshis"),j64bits(argjson,"desttxfee"),jbits256(argjson,"otherpubkey")); + else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 ) + retstr = jprint(argjson,0); else if ( IAMCLIENT == 0 && strcmp(method,"getprice") == 0 ) retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel")); else if ( IAMCLIENT == 0 && strcmp(method,"getpeers") == 0 ) From cc367abf4dcd605ae9df991578dbd67571e737a3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 19:12:52 +0300 Subject: [PATCH 130/339] Test --- iguana/exchanges/LP_commands.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 7d34bfe5d..a914f0f46 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -35,7 +35,7 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch return(rp); } -double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout,bits256 mypub) +double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout,bits256 mypub,cJSON *argitem) { cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,flag = 0,pushsock = -1; double price = 0.; if ( ipaddr != 0 && port >= 1000 ) @@ -55,6 +55,13 @@ double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *tx { flag = 1; jaddbits256(reqjson,"mypub",mypub); + if ( argitem != 0 ) + { + jadd64bits(reqjson,"satoshis",j64bits(argitem,"satoshis")); + jadd64bits(reqjson,"txfee",j64bits(argitem,"txfee")); + jadd64bits(reqjson,"desttxfee",j64bits(argitem,"desttxfee")); + jadd64bits(reqjson,"destsatoshis",j64bits(argitem,"destsatoshis")); + } } jaddstr(reqjson,"method",method); LP_send(pushsock,jprint(reqjson,1),1); @@ -164,7 +171,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) item = jitem(array,i); if ( (price= jdouble(item,"price")) == 0. ) { - price = LP_query("price",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),zero); + price = LP_query("price",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),zero,0); if ( destsatoshis[i] != 0 && (double)j64bits(item,"value")/destsatoshis[i] > price ) price = (double)j64bits(item,"satoshis")/destsatoshis[i]; } @@ -192,12 +199,12 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) } } } - if ( besti >= 0 ) + if ( besti >= 0 && bits256_cmp(utxo->mypub,otherpubs[besti]) == 0 ) { bestitem = jduplicate(jitem(array,besti)); i = besti; item = bestitem; - price = LP_query("request",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub); + price = LP_query("request",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,0); if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); jaddnum(bestitem,"reserved",reserved[besti]); @@ -208,7 +215,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) jaddbits256(bestitem,"otherpub",otherpubs[besti]); if ( LP_price(base,utxo->coin) > 0.975*price ) { - price = LP_query("connect",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub); + price = LP_query("connect",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,item); } } } From 8c6aaa81eb65413769e284cf492043874c696fa0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 19:16:09 +0300 Subject: [PATCH 131/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index a914f0f46..1bc50c150 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -199,7 +199,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) } } } - if ( besti >= 0 && bits256_cmp(utxo->mypub,otherpubs[besti]) == 0 ) + if ( besti >= 0 )//&& bits256_cmp(utxo->mypub,otherpubs[besti]) == 0 ) { bestitem = jduplicate(jitem(array,besti)); i = besti; From bf1aa24b309c6d39871d5963a2927b50f5e57dc2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 19:18:31 +0300 Subject: [PATCH 132/339] Test --- iguana/exchanges/LP_commands.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 1bc50c150..ff4c4f85a 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -403,7 +403,10 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis"),j64bits(argjson,"txfee"),j64bits(argjson,"destsatoshis"),j64bits(argjson,"desttxfee"),jbits256(argjson,"otherpubkey")); else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 ) + { retstr = jprint(argjson,0); + printf("got connected! (%s)\n",retstr); + } else if ( IAMCLIENT == 0 && strcmp(method,"getprice") == 0 ) retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel")); else if ( IAMCLIENT == 0 && strcmp(method,"getpeers") == 0 ) From e0b3c1eed11de3463c33046ca2033593361decd4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 19:21:39 +0300 Subject: [PATCH 133/339] Test --- iguana/exchanges/LP_commands.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index ff4c4f85a..616e5ddd2 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -400,6 +400,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } else LP_addpeer(amclient,LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos")); } } + printf("CMD.(%s)\n",jprint(argjson,0)); if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis"),j64bits(argjson,"txfee"),j64bits(argjson,"destsatoshis"),j64bits(argjson,"desttxfee"),jbits256(argjson,"otherpubkey")); else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 ) From 5e6e8424f140dfeb08d9e55b57dcf513e2d2d607 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 19:24:03 +0300 Subject: [PATCH 134/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 616e5ddd2..c34c69a85 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -65,7 +65,7 @@ double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *tx } jaddstr(reqjson,"method",method); LP_send(pushsock,jprint(reqjson,1),1); - for (i=0; i<10; i++) + for (i=0; i<100; i++) { if ( (price= LP_pricecache(otherpubp,reservedp,txfeep,destsatoshisp,desttxfeep,base,rel,txid,vout)) != 0. ) { From a5eaae009075c465c447e43c8e9ec4f874aa3bb3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 19:58:12 +0300 Subject: [PATCH 135/339] Test --- iguana/exchanges/LP_commands.c | 8 ++++++-- iguana/exchanges/LP_nativeDEX.c | 9 ++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index c34c69a85..4406189db 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -241,9 +241,9 @@ char *LP_quote(uint32_t reserved,char *base,char *rel,bits256 txid,int32_t vout, else return(clonestr("{\"error\":\"nullptr\"}")); } -void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) +int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { - char *method,*base,*rel,*retstr,pairstr[512]; cJSON *retjson; double price; bits256 srchash,desthash,pubkey,privkey,txid,desttxid; struct LP_utxoinfo *utxo; uint32_t timestamp,quotetime; int32_t destvout,DEXselector = 0; uint64_t txfee,satoshis,desttxfee,destsatoshis,value; struct basilisk_request R; + char *method,*base,*rel,*retstr,pairstr[512]; cJSON *retjson; double price; bits256 srchash,desthash,pubkey,privkey,txid,desttxid; struct LP_utxoinfo *utxo; uint32_t timestamp,quotetime; int32_t destvout,retval = -1,DEXselector = 0; uint64_t txfee,satoshis,desttxfee,destsatoshis,value; struct basilisk_request R; //LP_command.({"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"base":"KMD","rel":"BTC","method":"price"}) if ( (method= jstr(argjson,"method")) != 0 ) { @@ -255,6 +255,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ utxo->swappending = 0; if ( strcmp(method,"price") == 0 || strcmp(method,"request") == 0 ) { + retval = 1; if ( utxo->swappending == 0 && utxo->pair < 0 ) { if ( utxo->pair >= 0 ) @@ -283,6 +284,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ jadd64bits(retjson,"destsatoshis",price * (utxo->satoshis-txfee) + desttxfee); if ( strcmp(method,"request") == 0 ) { + retval |= 2; utxo->swappending = (uint32_t)(time(NULL) + LP_RESERVETIME); utxo->otherpubkey = jbits256(argjson,"mypub"); jaddbits256(retjson,"otherpubkey",utxo->otherpubkey); @@ -296,6 +298,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ } else if ( strcmp(method,"connect") == 0 ) { + retval = 4; if ( utxo->pair < 0 ) { if ( (price= LP_price(base,rel)) != 0. ) @@ -369,6 +372,7 @@ void LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_ } } } + return(retval); } char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index d06aaad8b..a444540fa 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -973,7 +973,14 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i { len = (int32_t)strlen((char *)ptr) + 1; portable_mutex_lock(&LP_commandmutex); - LP_command(mypeer,pubsock,argjson,&((uint8_t *)ptr)[len],recvsize - len,profitmargin); + if ( LP_command(mypeer,pubsock,argjson,&((uint8_t *)ptr)[len],recvsize - len,profitmargin) == 0 ) + { + if ( (retstr= stats_JSON(argjson,"127.0.0.1",mypubport)) != 0 ) + { + printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); + free(retstr); + } + } portable_mutex_unlock(&LP_commandmutex); free_json(argjson); } From 68228f80eb3b52912a08d39e371cc66e8656b172 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 20:49:35 +0300 Subject: [PATCH 136/339] Test --- iguana/exchanges/LP_commands.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 4406189db..e45d792d6 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -256,9 +256,9 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin if ( strcmp(method,"price") == 0 || strcmp(method,"request") == 0 ) { retval = 1; - if ( utxo->swappending == 0 && utxo->pair < 0 ) + if ( utxo->swappending == 0 ) { - if ( utxo->pair >= 0 ) + if ( strcmp(method,"request") == 0 && utxo->pair >= 0 ) nn_close(utxo->pair), utxo->pair = -1; if ( (price= LP_price(base,rel)) != 0. ) { From eb2d06d9da03e937f45b2a5060d9eec28d214aa9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 21:47:02 +0300 Subject: [PATCH 137/339] Test --- iguana/exchanges/LP_commands.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index e45d792d6..0d4928eb4 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -54,7 +54,7 @@ double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *tx if ( bits256_nonz(mypub) != 0 ) { flag = 1; - jaddbits256(reqjson,"mypub",mypub); + jaddbits256(reqjson,"otherpub",mypub); if ( argitem != 0 ) { jadd64bits(reqjson,"satoshis",j64bits(argitem,"satoshis")); @@ -65,7 +65,7 @@ double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *tx } jaddstr(reqjson,"method",method); LP_send(pushsock,jprint(reqjson,1),1); - for (i=0; i<100; i++) + for (i=0; i<30; i++) { if ( (price= LP_pricecache(otherpubp,reservedp,txfeep,destsatoshisp,desttxfeep,base,rel,txid,vout)) != 0. ) { @@ -244,7 +244,6 @@ char *LP_quote(uint32_t reserved,char *base,char *rel,bits256 txid,int32_t vout, int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { char *method,*base,*rel,*retstr,pairstr[512]; cJSON *retjson; double price; bits256 srchash,desthash,pubkey,privkey,txid,desttxid; struct LP_utxoinfo *utxo; uint32_t timestamp,quotetime; int32_t destvout,retval = -1,DEXselector = 0; uint64_t txfee,satoshis,desttxfee,destsatoshis,value; struct basilisk_request R; - //LP_command.({"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"base":"KMD","rel":"BTC","method":"price"}) if ( (method= jstr(argjson,"method")) != 0 ) { txid = jbits256(argjson,"txid"); @@ -286,8 +285,8 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin { retval |= 2; utxo->swappending = (uint32_t)(time(NULL) + LP_RESERVETIME); - utxo->otherpubkey = jbits256(argjson,"mypub"); - jaddbits256(retjson,"otherpubkey",utxo->otherpubkey); + utxo->otherpubkey = jbits256(argjson,"otherpub"); + jaddbits256(retjson,"otherpub",utxo->otherpubkey); jaddstr(retjson,"method","reserved"); jaddnum(retjson,"pending",utxo->swappending); } else jaddstr(retjson,"method","quote"); @@ -301,6 +300,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin retval = 4; if ( utxo->pair < 0 ) { + //LP_command.({"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"base":"KMD","rel":"BTC","mypub":"3b8f71015d644aaa4c9cceeee289b9a50dc9ec7fafab861c4d5872a8e3844466","satoshis":"0","txfee":"100000","desttxfee":"10000","destsatoshis":"2163363","method":"connect"}) if ( (price= LP_price(base,rel)) != 0. ) { price *= (1. + profitmargin); @@ -311,8 +311,8 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin destvout = jint(argjson,"destvout"); timestamp = juint(argjson,"timestamp"); privkey = LP_privkey(utxo->coinaddr); - pubkey = LP_pubkey(privkey); - srchash = jbits256(argjson,"srchash"); + srchash = LP_pubkey(privkey); + jaddbits256(argjson,"srchash",srchash); value = j64bits(argjson,"destsatoshis"); quotetime = juint(argjson,"quotetime"); //if ( timestamp == utxo->swappending-LP_RESERVETIME && quotetime >= timestamp && quotetime < utxo->swappending && bits256_cmp(pubkey,srchash) == 0 && (destsatoshis= LP_txvalue(rel,desttxid,destvout)) > price*(utxo->satoshis-txfee)+desttxfee && value <= destsatoshis-desttxfee ) @@ -323,7 +323,8 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin printf("error creating utxo->pair\n"); else if ( nn_connect(utxo->pair,pairstr) >= 0 ) { - desthash = jbits256(argjson,"desthash"); + desthash = jbits256(argjson,"otherpub"); + char str[65],str2[65]; printf("(%s) -> %s %s %s %.8f %s %.8f %u %u %d\n",jprint(argjson,0),bits256_str(str,srchash),bits256_str(str2,desthash),base,dstr(satoshis),rel,dstr(destsatoshis),timestamp,quotetime,DEXselector); LP_requestinit(&R,srchash,desthash,base,satoshis,rel,destsatoshis,timestamp,quotetime,DEXselector); if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_bobloop,(void *)utxo) == 0 ) { @@ -406,7 +407,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } printf("CMD.(%s)\n",jprint(argjson,0)); if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) - retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis"),j64bits(argjson,"txfee"),j64bits(argjson,"destsatoshis"),j64bits(argjson,"desttxfee"),jbits256(argjson,"otherpubkey")); + retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis"),j64bits(argjson,"txfee"),j64bits(argjson,"destsatoshis"),j64bits(argjson,"desttxfee"),jbits256(argjson,"otherpub")); else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 ) { retstr = jprint(argjson,0); From 0a17042d06b01433f94ee4309356579bdb382580 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 21:54:45 +0300 Subject: [PATCH 138/339] Test --- iguana/exchanges/LP_commands.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 0d4928eb4..9ff6fff5e 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -57,6 +57,9 @@ double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *tx jaddbits256(reqjson,"otherpub",mypub); if ( argitem != 0 ) { + printf("ARGITEM.(%s)\n",jprint(argitem,0)); + jaddnum(reqjson,"timestamp",j64bits(argitem,"timestamp")); + jaddnum(reqjson,"quotetime",j64bits(argitem,"quotetime")); jadd64bits(reqjson,"satoshis",j64bits(argitem,"satoshis")); jadd64bits(reqjson,"txfee",j64bits(argitem,"txfee")); jadd64bits(reqjson,"desttxfee",j64bits(argitem,"desttxfee")); @@ -173,7 +176,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) { price = LP_query("price",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),zero,0); if ( destsatoshis[i] != 0 && (double)j64bits(item,"value")/destsatoshis[i] > price ) - price = (double)j64bits(item,"satoshis")/destsatoshis[i]; + price = (double)j64bits(item,"value")/destsatoshis[i]; } if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) ) bestprice = price; From 6219964c7cb188a3fbfe9fa078f29caea036ed8f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 22:01:11 +0300 Subject: [PATCH 139/339] Test --- iguana/exchanges/LP_commands.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 9ff6fff5e..25a09714e 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -20,6 +20,7 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srchash,bits256 desthash,char *src,uint64_t srcsatoshis,char *dest,uint64_t destsatoshis,uint32_t timestamp,uint32_t quotetime,int32_t DEXselector) { + struct basilisk_request R; memset(rp,0,sizeof(*rp)); rp->srchash = srchash; rp->desthash = desthash; @@ -30,7 +31,9 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch rp->DEXselector = DEXselector; safecopy(rp->src,src,sizeof(rp->src)); safecopy(rp->dest,dest,sizeof(rp->dest)); + R = *rp; rp->requestid = basilisk_requestid(rp); + *rp = R; rp->quoteid = basilisk_quoteid(rp); return(rp); } @@ -59,8 +62,8 @@ double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *tx { printf("ARGITEM.(%s)\n",jprint(argitem,0)); jaddnum(reqjson,"timestamp",j64bits(argitem,"timestamp")); - jaddnum(reqjson,"quotetime",j64bits(argitem,"quotetime")); - jadd64bits(reqjson,"satoshis",j64bits(argitem,"satoshis")); + jaddnum(reqjson,"quotetime",time(NULL)); + jadd64bits(reqjson,"satoshis",j64bits(argitem,"value")-j64bits(argitem,"txfee")); jadd64bits(reqjson,"txfee",j64bits(argitem,"txfee")); jadd64bits(reqjson,"desttxfee",j64bits(argitem,"desttxfee")); jadd64bits(reqjson,"destsatoshis",j64bits(argitem,"destsatoshis")); From dbce54481394febf36a7f9877cc3d7dd17a870a5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 22:05:40 +0300 Subject: [PATCH 140/339] Test --- iguana/exchanges/LP_commands.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 25a09714e..c7598222c 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -159,7 +159,7 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) { static bits256 zero; - int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; bits256 otherpubs[100]; uint32_t reserved[100]; uint64_t txfees[100],destsatoshis[100],desttxfees[100]; + int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; bits256 otherpubs[100]; uint32_t reserved[100]; uint64_t txfees[100],destsatoshis[100],desttxfees[100]; quoteinfo struct bestprice = 0.; if ( (array= LP_tradecandidates(utxo,base)) != 0 ) { @@ -177,6 +177,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) item = jitem(array,i); if ( (price= jdouble(item,"price")) == 0. ) { + horrible, pass in quoteinfo, utxo price = LP_query("price",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),zero,0); if ( destsatoshis[i] != 0 && (double)j64bits(item,"value")/destsatoshis[i] > price ) price = (double)j64bits(item,"value")/destsatoshis[i]; @@ -221,6 +222,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) jaddbits256(bestitem,"otherpub",otherpubs[besti]); if ( LP_price(base,utxo->coin) > 0.975*price ) { + the same, cleanup price = LP_query("connect",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,item); } } @@ -268,6 +270,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin if ( (price= LP_price(base,rel)) != 0. ) { price *= (1. + profitmargin); + horror show retjson = cJSON_CreateObject(); jaddstr(retjson,"base",base); jaddstr(retjson,"rel",rel); @@ -321,7 +324,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin jaddbits256(argjson,"srchash",srchash); value = j64bits(argjson,"destsatoshis"); quotetime = juint(argjson,"quotetime"); - //if ( timestamp == utxo->swappending-LP_RESERVETIME && quotetime >= timestamp && quotetime < utxo->swappending && bits256_cmp(pubkey,srchash) == 0 && (destsatoshis= LP_txvalue(rel,desttxid,destvout)) > price*(utxo->satoshis-txfee)+desttxfee && value <= destsatoshis-desttxfee ) + if ( timestamp == utxo->swappending-LP_RESERVETIME && quotetime >= timestamp && quotetime < utxo->swappending && bits256_cmp(pubkey,srchash) == 0 && (destsatoshis= LP_txvalue(rel,desttxid,destvout)) > price*(utxo->satoshis-txfee)+desttxfee && value <= destsatoshis-desttxfee ) { destsatoshis = value; nanomsg_tcpname(pairstr,mypeer->ipaddr,10000+(rand() % 10000)); @@ -373,7 +376,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin nn_close(utxo->pair); utxo->pair = -1; } - } //else printf("dest %.8f < required %.8f\n",dstr(value),dstr(price*(utxo->satoshis-txfee))); + } else printf("dest %.8f < required %.8f\n",dstr(value),dstr(price*(utxo->satoshis-txfee))); } else printf("no price for %s/%s\n",base,rel); } else printf("utxo->pair.%d when connect came in (%s)\n",utxo->pair,jprint(argjson,0)); } From dc11d5c77bde52434b7a4d6b5248a087b2d28518 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 May 2017 22:06:58 +0300 Subject: [PATCH 141/339] Test --- iguana/exchanges/LP_commands.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index c7598222c..96e9a9c1f 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -159,7 +159,7 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) { static bits256 zero; - int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; bits256 otherpubs[100]; uint32_t reserved[100]; uint64_t txfees[100],destsatoshis[100],desttxfees[100]; quoteinfo struct + int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; bits256 otherpubs[100]; uint32_t reserved[100]; uint64_t txfees[100],destsatoshis[100],desttxfees[100]; //quoteinfo struct bestprice = 0.; if ( (array= LP_tradecandidates(utxo,base)) != 0 ) { @@ -177,7 +177,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) item = jitem(array,i); if ( (price= jdouble(item,"price")) == 0. ) { - horrible, pass in quoteinfo, utxo +// horrible, pass in quoteinfo, utxo price = LP_query("price",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),zero,0); if ( destsatoshis[i] != 0 && (double)j64bits(item,"value")/destsatoshis[i] > price ) price = (double)j64bits(item,"value")/destsatoshis[i]; @@ -222,7 +222,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) jaddbits256(bestitem,"otherpub",otherpubs[besti]); if ( LP_price(base,utxo->coin) > 0.975*price ) { - the same, cleanup +// the same, cleanup price = LP_query("connect",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,item); } } @@ -270,7 +270,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin if ( (price= LP_price(base,rel)) != 0. ) { price *= (1. + profitmargin); - horror show +// horror show retjson = cJSON_CreateObject(); jaddstr(retjson,"base",base); jaddstr(retjson,"rel",rel); @@ -317,11 +317,12 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin desttxfee = j64bits(argjson,"desttxfee"); satoshis = j64bits(argjson,"satoshis"); desttxid = jbits256(argjson,"desttxid"); + srchash = jbits256(argjson,"srchash"); destvout = jint(argjson,"destvout"); timestamp = juint(argjson,"timestamp"); privkey = LP_privkey(utxo->coinaddr); - srchash = LP_pubkey(privkey); - jaddbits256(argjson,"srchash",srchash); + pubkey = LP_pubkey(privkey); + jaddbits256(argjson,"srchash",pubkey); value = j64bits(argjson,"destsatoshis"); quotetime = juint(argjson,"quotetime"); if ( timestamp == utxo->swappending-LP_RESERVETIME && quotetime >= timestamp && quotetime < utxo->swappending && bits256_cmp(pubkey,srchash) == 0 && (destsatoshis= LP_txvalue(rel,desttxid,destvout)) > price*(utxo->satoshis-txfee)+desttxfee && value <= destsatoshis-desttxfee ) From d7955ef7794c1c3dc7945f3c4f9a3b46b984c1c6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 07:43:31 +0300 Subject: [PATCH 142/339] Test --- iguana/exchanges/LP_commands.c | 266 +++++++++++++++++++------------- iguana/exchanges/LP_nativeDEX.c | 67 ++++---- 2 files changed, 192 insertions(+), 141 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 96e9a9c1f..60f201cc0 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -38,9 +38,10 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch return(rp); } -double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout,bits256 mypub,cJSON *argitem) +double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout,bits256 mypub,cJSON *argitem) { cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,flag = 0,pushsock = -1; double price = 0.; + memset(qp,0,sizeof(*qp)); if ( ipaddr != 0 && port >= 1000 ) { if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),port)) == 0 ) @@ -57,7 +58,7 @@ double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *tx if ( bits256_nonz(mypub) != 0 ) { flag = 1; - jaddbits256(reqjson,"otherpub",mypub); + jaddbits256(reqjson,"desthash",mypub); if ( argitem != 0 ) { printf("ARGITEM.(%s)\n",jprint(argitem,0)); @@ -73,9 +74,9 @@ double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *tx LP_send(pushsock,jprint(reqjson,1),1); for (i=0; i<30; i++) { - if ( (price= LP_pricecache(otherpubp,reservedp,txfeep,destsatoshisp,desttxfeep,base,rel,txid,vout)) != 0. ) + if ( (price= LP_pricecache(qp,base,rel,txid,vout)) != 0. ) { - if ( flag == 0 || bits256_nonz(*otherpubp) != 0 ) + if ( flag == 0 || bits256_nonz(qp->desthash) != 0 ) break; } usleep(250000); @@ -86,19 +87,6 @@ double LP_query(char *method,bits256 *otherpubp,uint32_t *reservedp,uint64_t *tx return(price); } -/* - //5.9.253.196:7779 [{"ipaddr":"5.9.253.196","port":7779,"profit":0.01064000,"coin":"KMD","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","script":"76a914434009423522682bd7cc1b18a614c3096d19683188ac","txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"value":100,"deposit":"07902a65d11f0f577a0346432bcd2b6b53de5554c314209d1964693962524d69","dvout":1,"dvalue":120}] - - LP_send(peer->pushsock,jprint(reqjson,0),1); - jdelete(reqjson,"method"); - jaddstr(reqjson,"method","request"); - LP_send(peer->pushsock,jprint(reqjson,0),1); - jdelete(reqjson,"method"); - jaddstr(reqjson,"method","connect"); - LP_send(peer->pushsock,jprint(reqjson,0),1); - - //SENT.({"base":"KMD","rel":"BTC","timestamp":1496076137,"price":0.00021791,"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","srchash":"2fe57da347cd62431528daac5fbb290730fff684afc4cfc2ed90995f58cb3b74","txfee":"100000","satoshis":"9999900000","destsatoshis":"2179101","result":"reserved","pending":1496076197} -*/ int32_t LP_sizematch(uint64_t mysatoshis,uint64_t othersatoshis) { if ( mysatoshis >= othersatoshis ) @@ -108,7 +96,7 @@ int32_t LP_sizematch(uint64_t mysatoshis,uint64_t othersatoshis) cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) { - struct LP_peerinfo *peer,*tmp; char *utxostr,coinstr[16]; cJSON *array,*icopy,*retarray=0,*item; int32_t i,n; double price; bits256 otherpub; uint32_t reserved; int64_t estimatedbase; uint64_t txfee,destsatoshis,desttxfee; + struct LP_peerinfo *peer,*tmp; struct LP_quoteinfo Q; char *utxostr,coinstr[16]; cJSON *array,*icopy,*retarray=0,*item; int32_t i,n; double price; int64_t estimatedbase; if ( (price= LP_price(base,myutxo->coin)) == .0 ) return(0); estimatedbase = myutxo->satoshis / price; @@ -132,9 +120,9 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) if ( strcmp(coinstr,base) == 0 && LP_sizematch(estimatedbase,j64bits(item,"satoshis")) == 0 ) { icopy = 0; - if ( (price= LP_pricecache(&otherpub,&reserved,&txfee,&destsatoshis,&desttxfee,base,myutxo->coin,jbits256(item,"txid"),jint(item,"vout"))) != 0. ) + if ( (price= LP_pricecache(&Q,base,myutxo->coin,jbits256(item,"txid"),jint(item,"vout"))) != 0. ) { - if ( LP_sizematch(myutxo->satoshis,destsatoshis) == 0 ) + if ( LP_sizematch(myutxo->satoshis,Q.destsatoshis) == 0 ) icopy = jduplicate(item); } else icopy = jduplicate(item); if ( icopy != 0 ) @@ -159,18 +147,14 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) { static bits256 zero; - int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; bits256 otherpubs[100]; uint32_t reserved[100]; uint64_t txfees[100],destsatoshis[100],desttxfees[100]; //quoteinfo struct + int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; struct LP_quoteinfo Q[sizeof(prices)/sizeof(*prices)]; bestprice = 0.; if ( (array= LP_tradecandidates(utxo,base)) != 0 ) { if ( (n= cJSON_GetArraySize(array)) > 0 ) { memset(prices,0,sizeof(prices)); - memset(reserved,0,sizeof(reserved)); - memset(otherpubs,0,sizeof(otherpubs)); - memset(txfees,0,sizeof(txfees)); - memset(destsatoshis,0,sizeof(destsatoshis)); - memset(desttxfees,0,sizeof(desttxfees)); + memset(Q,0,sizeof(Q)); //BTC 0.02500000 -> ([{"ipaddr":"5.9.253.196","port":7779,"profit":0.01035000,"base":"KMD","coin":"KMD","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","script":"76a914434009423522682bd7cc1b18a614c3096d19683188ac","txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"value":100,"deposit":"07902a65d11f0f577a0346432bcd2b6b53de5554c314209d1964693962524d69","dvout":1,"dvalue":120}]) for (i=0; icoin,jbits256(item,"txid"),jint(item,"vout"),zero,0); - if ( destsatoshis[i] != 0 && (double)j64bits(item,"value")/destsatoshis[i] > price ) - price = (double)j64bits(item,"value")/destsatoshis[i]; + price = LP_query("price",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),zero,0); + if ( Q[i].destsatoshis != 0 && (double)j64bits(item,"value")/Q[i].destsatoshis > price ) + price = (double)j64bits(item,"value")/Q[i].destsatoshis; } if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) ) bestprice = price; @@ -192,12 +176,12 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) besti = -1; for (i=0; i 0.9 ) { - metric = destsatoshis[i] / metric * metric * metric; + metric = Q[i].destsatoshis / metric * metric * metric; if ( metric > bestmetric ) { besti = i; @@ -211,19 +195,18 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) bestitem = jduplicate(jitem(array,besti)); i = besti; item = bestitem; - price = LP_query("request",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,0); + price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,0); if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); - jaddnum(bestitem,"reserved",reserved[besti]); jaddnum(bestitem,"price",prices[besti]); - jadd64bits(bestitem,"txfee",txfees[besti]); - jadd64bits(bestitem,"desttxfee",desttxfees[besti]); - jadd64bits(bestitem,"destsatoshis",destsatoshis[besti]); - jaddbits256(bestitem,"otherpub",otherpubs[besti]); + jadd64bits(bestitem,"txfee",Q[besti].txfee); + jadd64bits(bestitem,"desttxfee",Q[besti].desttxfee); + jadd64bits(bestitem,"destsatoshis",Q[besti].destsatoshis); + jaddbits256(bestitem,"desthash",Q[besti].desthash); if ( LP_price(base,utxo->coin) > 0.975*price ) { // the same, cleanup - price = LP_query("connect",&otherpubs[i],&reserved[i],&txfees[i],&destsatoshis[i],&desttxfees[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,item); + price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,item); } } } @@ -233,17 +216,130 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) return(bestitem); } -char *LP_quote(uint32_t reserved,char *base,char *rel,bits256 txid,int32_t vout,double price,uint64_t satoshis,uint64_t txfee,uint64_t destsatoshis,uint64_t desttxfee,bits256 otherpub) +cJSON *LP_quotejson(struct LP_quoteinfo *qp) { - struct LP_cacheinfo *ptr; - if ( (ptr= LP_cacheadd(base,rel,txid,vout,price,satoshis)) != 0 ) + double price; cJSON *retjson = cJSON_CreateObject(); + jaddstr(retjson,"base",qp->srccoin); + jaddstr(retjson,"rel",qp->destcoin); + jaddstr(retjson,"address",qp->coinaddr); + if ( qp->timestamp != 0 ) + jaddnum(retjson,"timestamp",qp->timestamp); + jaddbits256(retjson,"txid",qp->txid); + jaddnum(retjson,"vout",qp->vout); + jaddbits256(retjson,"srchash",qp->srchash); + jadd64bits(retjson,"txfee",qp->txfee); + if ( qp->quotetime != 0 ) + jaddnum(retjson,"quotetime",qp->quotetime); + jadd64bits(retjson,"satoshis",qp->satoshis); + if ( bits256_nonz(qp->desthash) != 0 ) + jaddbits256(retjson,"desthash",qp->desthash); + if ( bits256_nonz(qp->txid) != 0 ) + { + jaddbits256(retjson,"txid",qp->txid); + jaddnum(retjson,"vout",qp->vout); + } + if ( bits256_nonz(qp->txid2) != 0 ) + { + jaddbits256(retjson,"txid2",qp->txid2); + jaddnum(retjson,"vout2",qp->vout2); + jadd64bits(retjson,"satoshis2",qp->satoshis2); + } + if ( bits256_nonz(qp->desttxid) != 0 ) + { + jaddstr(retjson,"destaddr",qp->destaddr); + jaddbits256(retjson,"desttxid",qp->desttxid); + jaddnum(retjson,"destvout",qp->destvout); + } + jadd64bits(retjson,"destsatoshis",qp->destsatoshis); + jadd64bits(retjson,"desttxfee",qp->desttxfee); + if ( qp->change != 0 ) + jaddnum(retjson,"change",dstr(qp->change)); + price = (double)(qp->destsatoshis+qp->desttxfee) / qp->satoshis; + jaddnum(retjson,"price",price); + return(retjson); +} + +int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) +{ + safecopy(qp->srccoin,jstr(argjson,"base"),sizeof(qp->srccoin)); + safecopy(qp->coinaddr,jstr(argjson,"address"),sizeof(qp->coinaddr)); + safecopy(qp->destcoin,jstr(argjson,"rel"),sizeof(qp->destcoin)); + safecopy(qp->destaddr,jstr(argjson,"destaddr"),sizeof(qp->destaddr)); + qp->timestamp = juint(argjson,"timestamp"); + qp->quotetime = juint(argjson,"quotetime"); + qp->txid = jbits256(argjson,"txid"); + qp->txid2 = jbits256(argjson,"txid2"); + qp->vout = jint(argjson,"vout"); + qp->vout2 = jint(argjson,"vout2"); + qp->srchash = jbits256(argjson,"srchash"); + qp->desttxid = jbits256(argjson,"desttxid"); + qp->destvout = jint(argjson,"destvout"); + qp->desthash = jbits256(argjson,"desthash"); + qp->satoshis = j64bits(argjson,"satoshis"); + qp->destsatoshis = j64bits(argjson,"destsatoshis"); + qp->change = SATOSHIDEN * jdouble(argjson,"change"); + qp->satoshis2 = j64bits(argjson,"satoshis2"); + qp->txfee = j64bits(argjson,"txfee"); + qp->desttxfee = j64bits(argjson,"desttxfee"); + return(0); +} + +int32_t LP_quoteinfoinit(struct LP_quoteinfo *qp,struct LP_utxoinfo *utxo,char *destcoin,double price) +{ + memset(qp,0,sizeof(*qp)); + qp->timestamp = (uint32_t)time(NULL); + if ( (qp->txfee= LP_getestimatedrate(utxo->coin)*LP_AVETXSIZE) < 10000 ) + qp->txfee = 10000; + if ( qp->txfee >= utxo->satoshis || qp->txfee >= utxo->satoshis2 ) + return(-1); + qp->txid = utxo->txid; + qp->vout = utxo->vout; + qp->txid2 = utxo->txid2; + qp->vout2 = utxo->vout2; + qp->satoshis2 = utxo->satoshis2 - qp->txfee; + qp->satoshis = utxo->satoshis - qp->txfee; + qp->destsatoshis = qp->satoshis * price; + if ( (qp->desttxfee= LP_getestimatedrate(qp->destcoin) * LP_AVETXSIZE) < 10000 ) + qp->desttxfee = 10000; + if ( qp->desttxfee >= qp->destsatoshis ) + return(-2); + qp->destsatoshis -= qp->desttxfee; + safecopy(qp->srccoin,utxo->coin,sizeof(qp->srccoin)); + safecopy(qp->coinaddr,utxo->coinaddr,sizeof(qp->coinaddr)); + safecopy(qp->destcoin,destcoin,sizeof(qp->destcoin)); + qp->srchash = LP_pubkey(LP_privkey(utxo->coinaddr)); + return(0); +} + +int32_t LP_quoteinfoset(struct LP_quoteinfo *qp,uint32_t timestamp,uint32_t quotetime,uint64_t value,uint64_t txfee,uint64_t destsatoshis,uint64_t desttxfee,bits256 desttxid,int32_t destvout,bits256 desthash,char *destaddr) +{ + if ( txfee != qp->txfee ) + { + if ( txfee >= value ) + return(-1); + qp->txfee = txfee; + qp->satoshis = value - txfee; + } + qp->timestamp = timestamp; + qp->quotetime = quotetime; + qp->destsatoshis = destsatoshis; + qp->desttxfee = desttxfee; + qp->desttxid = desttxid; + qp->destvout = destvout; + qp->desthash = desthash; + safecopy(qp->destaddr,destaddr,sizeof(qp->destaddr)); + return(0); +} + +char *LP_quote(cJSON *argjson) +{ + struct LP_cacheinfo *ptr; double price; struct LP_quoteinfo Q; + LP_quoteparse(&Q,argjson); + price = (double)(Q.destsatoshis + Q.desttxfee) / (Q.satoshis + Q.txfee); + if ( (ptr= LP_cacheadd(Q.srccoin,Q.destcoin,Q.txid,Q.vout,price,&Q)) != 0 ) { //SENT.({"base":"KMD","rel":"BTC","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","timestamp":1496216835,"price":0.00021141,"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","srchash":"0bcabd875bfa724e26de5f35035ca3767c50b30960e23cbfcbd478cac9147412","txfee":"100000","desttxfee":"10000","value":"10000000000","satoshis":"9999900000","destsatoshis":"2124104","method":"quote"}) - ptr->reserved = reserved; - ptr->txfee = txfee; - ptr->destsatoshis = destsatoshis; - ptr->desttxfee = desttxfee; - ptr->otherpub = otherpub; + ptr->Q = Q; return(clonestr("{\"result\":\"updated\"}")); } else return(clonestr("{\"error\":\"nullptr\"}")); @@ -251,7 +347,7 @@ char *LP_quote(uint32_t reserved,char *base,char *rel,bits256 txid,int32_t vout, int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { - char *method,*base,*rel,*retstr,pairstr[512]; cJSON *retjson; double price; bits256 srchash,desthash,pubkey,privkey,txid,desttxid; struct LP_utxoinfo *utxo; uint32_t timestamp,quotetime; int32_t destvout,retval = -1,DEXselector = 0; uint64_t txfee,satoshis,desttxfee,destsatoshis,value; struct basilisk_request R; + char *method,*base,*rel,*retstr,pairstr[512]; cJSON *retjson; double price; bits256 privkey,txid; struct LP_utxoinfo *utxo; int32_t retval = -1,DEXselector = 0; uint64_t destvalue; struct basilisk_request R; struct LP_quoteinfo Q; if ( (method= jstr(argjson,"method")) != 0 ) { txid = jbits256(argjson,"txid"); @@ -270,34 +366,18 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin if ( (price= LP_price(base,rel)) != 0. ) { price *= (1. + profitmargin); -// horror show - retjson = cJSON_CreateObject(); - jaddstr(retjson,"base",base); - jaddstr(retjson,"rel",rel); - jaddstr(retjson,"address",utxo->coinaddr); - jaddnum(retjson,"timestamp",time(NULL)); - jaddnum(retjson,"price",price); - jaddbits256(retjson,"txid",txid); - jaddnum(retjson,"vout",utxo->vout); - pubkey = LP_pubkey(LP_privkey(utxo->coinaddr)); - jaddbits256(retjson,"srchash",pubkey); - if ( (txfee= LP_getestimatedrate(base)*LP_AVETXSIZE) < 10000 ) - txfee = 10000; - jadd64bits(retjson,"txfee",txfee); - if ( (desttxfee= LP_getestimatedrate(rel) * LP_AVETXSIZE) < 10000 ) - desttxfee = 10000; - jadd64bits(retjson,"desttxfee",desttxfee); - jadd64bits(retjson,"value",utxo->satoshis); - jadd64bits(retjson,"satoshis",utxo->satoshis - txfee); - jadd64bits(retjson,"destsatoshis",price * (utxo->satoshis-txfee) + desttxfee); + if ( LP_quoteinfoinit(&Q,utxo,rel,price) < 0 ) + return(-1); + retjson = LP_quotejson(&Q); if ( strcmp(method,"request") == 0 ) { retval |= 2; utxo->swappending = (uint32_t)(time(NULL) + LP_RESERVETIME); - utxo->otherpubkey = jbits256(argjson,"otherpub"); - jaddbits256(retjson,"otherpub",utxo->otherpubkey); - jaddstr(retjson,"method","reserved"); + utxo->otherpubkey = jbits256(argjson,"desthash"); + jaddnum(retjson,"quotetime",juint(argjson,"quotetime")); jaddnum(retjson,"pending",utxo->swappending); + jaddbits256(retjson,"desthash",utxo->otherpubkey); + jaddstr(retjson,"method","reserved"); } else jaddstr(retjson,"method","quote"); retstr = jprint(retjson,1); LP_send(pubsock,retstr,1); @@ -313,47 +393,23 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin if ( (price= LP_price(base,rel)) != 0. ) { price *= (1. + profitmargin); - txfee = j64bits(argjson,"txfee"); - desttxfee = j64bits(argjson,"desttxfee"); - satoshis = j64bits(argjson,"satoshis"); - desttxid = jbits256(argjson,"desttxid"); - srchash = jbits256(argjson,"srchash"); - destvout = jint(argjson,"destvout"); - timestamp = juint(argjson,"timestamp"); + if ( LP_quoteinfoinit(&Q,utxo,rel,price) < 0 ) + return(-1); + if ( LP_quoteparse(&Q,argjson) < 0 ) + return(-2); privkey = LP_privkey(utxo->coinaddr); - pubkey = LP_pubkey(privkey); - jaddbits256(argjson,"srchash",pubkey); - value = j64bits(argjson,"destsatoshis"); - quotetime = juint(argjson,"quotetime"); - if ( timestamp == utxo->swappending-LP_RESERVETIME && quotetime >= timestamp && quotetime < utxo->swappending && bits256_cmp(pubkey,srchash) == 0 && (destsatoshis= LP_txvalue(rel,desttxid,destvout)) > price*(utxo->satoshis-txfee)+desttxfee && value <= destsatoshis-desttxfee ) + if ( bits256_nonz(privkey) != 0 && Q.timestamp == utxo->swappending-LP_RESERVETIME && Q.quotetime >= Q.timestamp && Q.quotetime < utxo->swappending && bits256_cmp(utxo->mypub,Q.srchash) == 0 && (destvalue= LP_txvalue(rel,Q.desttxid,Q.destvout)) >= price*Q.satoshis+Q.desttxfee && destvalue >= Q.destsatoshis+Q.desttxfee ) { - destsatoshis = value; + Q.change = destvalue - (Q.destsatoshis+Q.desttxfee); nanomsg_tcpname(pairstr,mypeer->ipaddr,10000+(rand() % 10000)); if ( (utxo->pair= nn_socket(AF_SP,NN_PAIR)) < 0 ) printf("error creating utxo->pair\n"); else if ( nn_connect(utxo->pair,pairstr) >= 0 ) { - desthash = jbits256(argjson,"otherpub"); - char str[65],str2[65]; printf("(%s) -> %s %s %s %.8f %s %.8f %u %u %d\n",jprint(argjson,0),bits256_str(str,srchash),bits256_str(str2,desthash),base,dstr(satoshis),rel,dstr(destsatoshis),timestamp,quotetime,DEXselector); - LP_requestinit(&R,srchash,desthash,base,satoshis,rel,destsatoshis,timestamp,quotetime,DEXselector); + LP_requestinit(&R,Q.srchash,Q.desthash,base,Q.satoshis,rel,Q.destsatoshis,Q.timestamp,Q.quotetime,DEXselector); if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_bobloop,(void *)utxo) == 0 ) { - retjson = cJSON_CreateObject(); - jaddstr(retjson,"base",base); - jaddstr(retjson,"rel",rel); - jaddstr(retjson,"address",utxo->coinaddr); - jaddnum(retjson,"timestamp",timestamp); - jaddnum(retjson,"quotetime",quotetime); - jaddnum(retjson,"price",price); - jaddbits256(retjson,"txid",txid); - jaddnum(retjson,"vout",utxo->vout); - pubkey = LP_pubkey(LP_privkey(utxo->coinaddr)); - jaddbits256(retjson,"srchash",pubkey); - jadd64bits(retjson,"txfee",txfee); - jadd64bits(retjson,"desttxfee",desttxfee); - jadd64bits(retjson,"value",utxo->satoshis); - jadd64bits(retjson,"satoshis",utxo->satoshis - txfee); - jadd64bits(retjson,"destsatoshis",price * (utxo->satoshis-txfee) + desttxfee); + retjson = LP_quotejson(&Q); jaddstr(retjson,"method","connected"); jaddstr(retjson,"pair",pairstr); jaddnum(retjson,"requestid",R.requestid); @@ -377,7 +433,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin nn_close(utxo->pair); utxo->pair = -1; } - } else printf("dest %.8f < required %.8f\n",dstr(value),dstr(price*(utxo->satoshis-txfee))); + } else printf("dest %.8f < required %.8f (%d %d %d %d %d %d)\n",dstr(Q.satoshis),dstr(price*(utxo->satoshis-Q.txfee)),bits256_nonz(privkey) != 0 ,Q.timestamp == utxo->swappending-LP_RESERVETIME ,Q.quotetime >= Q.timestamp ,Q.quotetime < utxo->swappending ,bits256_cmp(utxo->mypub,Q.srchash) == 0 , LP_txvalue(rel,Q.desttxid,Q.destvout) >= price*Q.satoshis+Q.desttxfee); } else printf("no price for %s/%s\n",base,rel); } else printf("utxo->pair.%d when connect came in (%s)\n",utxo->pair,jprint(argjson,0)); } @@ -386,6 +442,8 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin return(retval); } +// add orderbook api + char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port { char *method,*ipaddr,*coin,*retstr = 0; uint16_t argport,pushport,subport; int32_t amclient,otherpeers,othernumutxos; struct LP_peerinfo *peer; cJSON *retjson; @@ -417,7 +475,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } printf("CMD.(%s)\n",jprint(argjson,0)); if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) - retstr = LP_quote(juint(argjson,"pending"),jstr(argjson,"base"),jstr(argjson,"rel"),jbits256(argjson,"txid"),jint(argjson,"vout"),jdouble(argjson,"price"),j64bits(argjson,"satoshis"),j64bits(argjson,"txfee"),j64bits(argjson,"destsatoshis"),j64bits(argjson,"desttxfee"),jbits256(argjson,"otherpub")); + retstr = LP_quote(argjson); else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 ) { retstr = jprint(argjson,0); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index a444540fa..b5668c9b6 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -48,24 +48,31 @@ struct LP_peerinfo struct LP_utxoinfo { UT_hash_handle hh; - bits256 txid,deposittxid,feetxid,otherpubkey,mypub; + bits256 txid,txid2,feetxid,otherpubkey,mypub; void *swap; - uint64_t satoshis,depositsatoshis,feesatoshis; + uint64_t satoshis,satoshis2; uint8_t key[sizeof(bits256) + sizeof(int32_t)]; - int32_t vout,depositvout,feevout,pair; uint32_t lasttime,errors,swappending; + int32_t vout,vout2,pair; uint32_t lasttime,errors,swappending; double profitmargin; char ipaddr[64],coinaddr[64],spendscript[256],coin[16]; uint16_t port; } *LP_utxoinfos; +struct LP_quoteinfo +{ + bits256 srchash,desthash,txid,txid2,desttxid; + uint64_t satoshis,satoshis2,txfee,destsatoshis,desttxfee,change; + uint32_t timestamp,quotetime; int32_t vout,vout2,destvout; + char srccoin[16],coinaddr[64],destcoin[16],destaddr[64]; +}; + struct LP_cacheinfo { UT_hash_handle hh; - bits256 otherpub; + struct LP_quoteinfo Q; uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(int32_t)]; double price; - uint64_t satoshis,txfee,destsatoshis,desttxfee; - uint32_t timestamp,reserved; + uint32_t timestamp; } *LP_cacheinfos; int32_t LP_cachekey(uint8_t *key,char *base,char *rel,bits256 txid,int32_t vout) @@ -93,41 +100,28 @@ struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout { printf("expire price %.8f\n",ptr->price); ptr->price = 0.; - ptr->destsatoshis = ptr->txfee = ptr->desttxfee = 0; - ptr->timestamp = 0; + memset(&ptr->Q,0,sizeof(ptr->Q)); } return(ptr); } -double LP_pricecache(bits256 *otherpubp,uint32_t *reservedp,uint64_t *txfeep,uint64_t *destsatoshisp,uint64_t *desttxfeep,char *base,char *rel,bits256 txid,int32_t vout) +double LP_pricecache(struct LP_quoteinfo *qp,char *base,char *rel,bits256 txid,int32_t vout) { struct LP_cacheinfo *ptr; if ( (ptr= LP_cachefind(base,rel,txid,vout)) != 0 ) { - if ( destsatoshisp != 0 ) - { - *otherpubp = ptr->otherpub; - *destsatoshisp = ptr->destsatoshis; - *txfeep = ptr->txfee; - *desttxfeep = ptr->desttxfee; - *reservedp = ptr->reserved; - } + if ( qp != 0 ) + (*qp) = ptr->Q; //printf("found %s/%s %.8f\n",base,rel,ptr->price); return(ptr->price); } - else if ( destsatoshisp != 0 ) - { - memset(otherpubp,0,sizeof(*otherpubp)); - *destsatoshisp = 0; - *txfeep = 0; - *desttxfeep = 0; - *reservedp = 0; - } + else if ( qp != 0 ) + memset(qp,0,sizeof(*qp)); //char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); return(0.); } -struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout,double price,uint64_t satoshis) +struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout,double price,struct LP_quoteinfo *qp) { struct LP_cacheinfo *ptr=0; if ( (ptr= LP_cachefind(base,rel,txid,vout)) == 0 ) @@ -143,8 +137,7 @@ struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout, //char str[65]; if ( price != ptr->price ) // printf("updated %s/v%d %s/%s %llu price %.8f\n",bits256_str(str,txid),vout,base,rel,(long long)satoshis,price); ptr->price = price; - ptr->satoshis = satoshis; - ptr->destsatoshis = satoshis * price; + ptr->Q = *qp; ptr->timestamp = (uint32_t)time(NULL); return(ptr); } @@ -192,9 +185,9 @@ cJSON *LP_utxojson(struct LP_utxoinfo *utxo) jaddbits256(item,"txid",utxo->txid); jaddnum(item,"vout",utxo->vout); jaddnum(item,"value",dstr(utxo->satoshis)); - jaddbits256(item,"deposit",utxo->deposittxid); - jaddnum(item,"dvout",utxo->depositvout); - jaddnum(item,"dvalue",dstr(utxo->depositsatoshis)); + jaddbits256(item,"txid2",utxo->txid2); + jaddnum(item,"vout2",utxo->vout2); + jaddnum(item,"value2",dstr(utxo->satoshis2)); return(item); } @@ -311,10 +304,10 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 } if ( (utxo= LP_utxofind(txid,vout)) != 0 ) { - if ( bits256_cmp(txid,utxo->txid) != 0 || bits256_cmp(deposittxid,utxo->deposittxid) != 0 || vout != utxo->vout || satoshis != utxo->satoshis || depositvout != utxo->depositvout || depositsatoshis != utxo->depositsatoshis || strcmp(coin,utxo->coin) != 0 || strcmp(spendscript,utxo->spendscript) != 0 || strcmp(coinaddr,utxo->coinaddr) != 0 || strcmp(ipaddr,utxo->ipaddr) != 0 || port != utxo->port ) + if ( bits256_cmp(txid,utxo->txid) != 0 || bits256_cmp(deposittxid,utxo->txid2) != 0 || vout != utxo->vout || satoshis != utxo->satoshis || depositvout != utxo->vout2 || depositsatoshis != utxo->satoshis2 || strcmp(coin,utxo->coin) != 0 || strcmp(spendscript,utxo->spendscript) != 0 || strcmp(coinaddr,utxo->coinaddr) != 0 || strcmp(ipaddr,utxo->ipaddr) != 0 || port != utxo->port ) { utxo->errors++; - char str[65],str2[65]; printf("error on subsequent utxo add.(%s v %s) %d %d %d %d %d %d %d %d %d %d %d\n",bits256_str(str,txid),bits256_str(str2,utxo->txid),bits256_cmp(txid,utxo->txid) != 0,bits256_cmp(deposittxid,utxo->deposittxid) != 0,vout != utxo->vout,satoshis != utxo->satoshis,depositvout != utxo->depositvout,depositsatoshis != utxo->depositsatoshis,strcmp(coin,utxo->coin) != 0,strcmp(spendscript,utxo->spendscript) != 0,strcmp(coinaddr,utxo->coinaddr) != 0,strcmp(ipaddr,utxo->ipaddr) != 0,port != utxo->port); + char str[65],str2[65]; printf("error on subsequent utxo add.(%s v %s) %d %d %d %d %d %d %d %d %d %d %d\n",bits256_str(str,txid),bits256_str(str2,utxo->txid),bits256_cmp(txid,utxo->txid) != 0,bits256_cmp(deposittxid,utxo->txid2) != 0,vout != utxo->vout,satoshis != utxo->satoshis,depositvout != utxo->vout2,depositsatoshis != utxo->satoshis2,strcmp(coin,utxo->coin) != 0,strcmp(spendscript,utxo->spendscript) != 0,strcmp(coinaddr,utxo->coinaddr) != 0,strcmp(ipaddr,utxo->ipaddr) != 0,port != utxo->port); } else if ( profitmargin != 0. ) utxo->profitmargin = profitmargin; @@ -332,9 +325,9 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 utxo->txid = txid; utxo->vout = vout; utxo->satoshis = satoshis; - utxo->deposittxid = deposittxid; - utxo->depositvout = depositvout; - utxo->depositsatoshis = depositsatoshis; + utxo->txid2 = deposittxid; + utxo->vout2 = depositvout; + utxo->satoshis2 = depositsatoshis; memcpy(key,txid.bytes,sizeof(txid)); memcpy(&key[sizeof(txid)],&vout,sizeof(vout)); memcpy(utxo->key,key,sizeof(key)); @@ -467,7 +460,7 @@ char *issue_LP_notify(char *destip,uint16_t destport,char *ipaddr,uint16_t port, char *issue_LP_notifyutxo(char *destip,uint16_t destport,struct LP_utxoinfo *utxo) { char url[4096],str[65],str2[65]; - sprintf(url,"http://%s:%u/api/stats/notifyutxo?ipaddr=%s&port=%u&profit=%.6f&coin=%s&txid=%s&vout=%d&value=%.8f&deposit=%s&dvout=%d&dvalue=%.8f&script=%s&address=%s",destip,destport,utxo->ipaddr,utxo->port,utxo->profitmargin,utxo->coin,bits256_str(str,utxo->txid),utxo->vout,dstr(utxo->satoshis),bits256_str(str2,utxo->deposittxid),utxo->depositvout,dstr(utxo->depositsatoshis),utxo->spendscript,utxo->coinaddr); + sprintf(url,"http://%s:%u/api/stats/notifyutxo?ipaddr=%s&port=%u&profit=%.6f&coin=%s&txid=%s&vout=%d&value=%.8f&deposit=%s&dvout=%d&dvalue=%.8f&script=%s&address=%s",destip,destport,utxo->ipaddr,utxo->port,utxo->profitmargin,utxo->coin,bits256_str(str,utxo->txid),utxo->vout,dstr(utxo->satoshis),bits256_str(str2,utxo->txid2),utxo->vout2,dstr(utxo->satoshis2),utxo->spendscript,utxo->coinaddr); if ( strlen(url) > 1024 ) printf("WARNING long url.(%s)\n",url); return(issue_curl(url)); From 15540f9a6f01e444f818ccb347d74ce13ea4f112 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:01:28 +0300 Subject: [PATCH 143/339] Test --- iguana/exchanges/LP_commands.c | 146 ++++++++++++++++----------------- 1 file changed, 71 insertions(+), 75 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 60f201cc0..5b97e4feb 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -38,6 +38,74 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch return(rp); } +cJSON *LP_quotejson(struct LP_quoteinfo *qp) +{ + double price; cJSON *retjson = cJSON_CreateObject(); + jaddstr(retjson,"base",qp->srccoin); + jaddstr(retjson,"rel",qp->destcoin); + jaddstr(retjson,"address",qp->coinaddr); + if ( qp->timestamp != 0 ) + jaddnum(retjson,"timestamp",qp->timestamp); + jaddbits256(retjson,"txid",qp->txid); + jaddnum(retjson,"vout",qp->vout); + jaddbits256(retjson,"srchash",qp->srchash); + jadd64bits(retjson,"txfee",qp->txfee); + if ( qp->quotetime != 0 ) + jaddnum(retjson,"quotetime",qp->quotetime); + jadd64bits(retjson,"satoshis",qp->satoshis); + if ( bits256_nonz(qp->desthash) != 0 ) + jaddbits256(retjson,"desthash",qp->desthash); + if ( bits256_nonz(qp->txid) != 0 ) + { + jaddbits256(retjson,"txid",qp->txid); + jaddnum(retjson,"vout",qp->vout); + } + if ( bits256_nonz(qp->txid2) != 0 ) + { + jaddbits256(retjson,"txid2",qp->txid2); + jaddnum(retjson,"vout2",qp->vout2); + jadd64bits(retjson,"satoshis2",qp->satoshis2); + } + if ( bits256_nonz(qp->desttxid) != 0 ) + { + jaddstr(retjson,"destaddr",qp->destaddr); + jaddbits256(retjson,"desttxid",qp->desttxid); + jaddnum(retjson,"destvout",qp->destvout); + } + jadd64bits(retjson,"destsatoshis",qp->destsatoshis); + jadd64bits(retjson,"desttxfee",qp->desttxfee); + if ( qp->change != 0 ) + jaddnum(retjson,"change",dstr(qp->change)); + price = (double)(qp->destsatoshis+qp->desttxfee) / qp->satoshis; + jaddnum(retjson,"price",price); + return(retjson); +} + +int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) +{ + safecopy(qp->srccoin,jstr(argjson,"base"),sizeof(qp->srccoin)); + safecopy(qp->coinaddr,jstr(argjson,"address"),sizeof(qp->coinaddr)); + safecopy(qp->destcoin,jstr(argjson,"rel"),sizeof(qp->destcoin)); + safecopy(qp->destaddr,jstr(argjson,"destaddr"),sizeof(qp->destaddr)); + qp->timestamp = juint(argjson,"timestamp"); + qp->quotetime = juint(argjson,"quotetime"); + qp->txid = jbits256(argjson,"txid"); + qp->txid2 = jbits256(argjson,"txid2"); + qp->vout = jint(argjson,"vout"); + qp->vout2 = jint(argjson,"vout2"); + qp->srchash = jbits256(argjson,"srchash"); + qp->desttxid = jbits256(argjson,"desttxid"); + qp->destvout = jint(argjson,"destvout"); + qp->desthash = jbits256(argjson,"desthash"); + qp->satoshis = j64bits(argjson,"satoshis"); + qp->destsatoshis = j64bits(argjson,"destsatoshis"); + qp->change = SATOSHIDEN * jdouble(argjson,"change"); + qp->satoshis2 = j64bits(argjson,"satoshis2"); + qp->txfee = j64bits(argjson,"txfee"); + qp->desttxfee = j64bits(argjson,"desttxfee"); + return(0); +} + double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout,bits256 mypub,cJSON *argitem) { cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,flag = 0,pushsock = -1; double price = 0.; @@ -192,21 +260,17 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) } if ( besti >= 0 )//&& bits256_cmp(utxo->mypub,otherpubs[besti]) == 0 ) { - bestitem = jduplicate(jitem(array,besti)); + item = jitem(array,besti); + bestitem = LP_quotejson(&Q[i]); i = besti; - item = bestitem; price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,0); if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); jaddnum(bestitem,"price",prices[besti]); - jadd64bits(bestitem,"txfee",Q[besti].txfee); - jadd64bits(bestitem,"desttxfee",Q[besti].desttxfee); - jadd64bits(bestitem,"destsatoshis",Q[besti].destsatoshis); - jaddbits256(bestitem,"desthash",Q[besti].desthash); if ( LP_price(base,utxo->coin) > 0.975*price ) { // the same, cleanup - price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,item); + price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,bestitem); } } } @@ -216,74 +280,6 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) return(bestitem); } -cJSON *LP_quotejson(struct LP_quoteinfo *qp) -{ - double price; cJSON *retjson = cJSON_CreateObject(); - jaddstr(retjson,"base",qp->srccoin); - jaddstr(retjson,"rel",qp->destcoin); - jaddstr(retjson,"address",qp->coinaddr); - if ( qp->timestamp != 0 ) - jaddnum(retjson,"timestamp",qp->timestamp); - jaddbits256(retjson,"txid",qp->txid); - jaddnum(retjson,"vout",qp->vout); - jaddbits256(retjson,"srchash",qp->srchash); - jadd64bits(retjson,"txfee",qp->txfee); - if ( qp->quotetime != 0 ) - jaddnum(retjson,"quotetime",qp->quotetime); - jadd64bits(retjson,"satoshis",qp->satoshis); - if ( bits256_nonz(qp->desthash) != 0 ) - jaddbits256(retjson,"desthash",qp->desthash); - if ( bits256_nonz(qp->txid) != 0 ) - { - jaddbits256(retjson,"txid",qp->txid); - jaddnum(retjson,"vout",qp->vout); - } - if ( bits256_nonz(qp->txid2) != 0 ) - { - jaddbits256(retjson,"txid2",qp->txid2); - jaddnum(retjson,"vout2",qp->vout2); - jadd64bits(retjson,"satoshis2",qp->satoshis2); - } - if ( bits256_nonz(qp->desttxid) != 0 ) - { - jaddstr(retjson,"destaddr",qp->destaddr); - jaddbits256(retjson,"desttxid",qp->desttxid); - jaddnum(retjson,"destvout",qp->destvout); - } - jadd64bits(retjson,"destsatoshis",qp->destsatoshis); - jadd64bits(retjson,"desttxfee",qp->desttxfee); - if ( qp->change != 0 ) - jaddnum(retjson,"change",dstr(qp->change)); - price = (double)(qp->destsatoshis+qp->desttxfee) / qp->satoshis; - jaddnum(retjson,"price",price); - return(retjson); -} - -int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) -{ - safecopy(qp->srccoin,jstr(argjson,"base"),sizeof(qp->srccoin)); - safecopy(qp->coinaddr,jstr(argjson,"address"),sizeof(qp->coinaddr)); - safecopy(qp->destcoin,jstr(argjson,"rel"),sizeof(qp->destcoin)); - safecopy(qp->destaddr,jstr(argjson,"destaddr"),sizeof(qp->destaddr)); - qp->timestamp = juint(argjson,"timestamp"); - qp->quotetime = juint(argjson,"quotetime"); - qp->txid = jbits256(argjson,"txid"); - qp->txid2 = jbits256(argjson,"txid2"); - qp->vout = jint(argjson,"vout"); - qp->vout2 = jint(argjson,"vout2"); - qp->srchash = jbits256(argjson,"srchash"); - qp->desttxid = jbits256(argjson,"desttxid"); - qp->destvout = jint(argjson,"destvout"); - qp->desthash = jbits256(argjson,"desthash"); - qp->satoshis = j64bits(argjson,"satoshis"); - qp->destsatoshis = j64bits(argjson,"destsatoshis"); - qp->change = SATOSHIDEN * jdouble(argjson,"change"); - qp->satoshis2 = j64bits(argjson,"satoshis2"); - qp->txfee = j64bits(argjson,"txfee"); - qp->desttxfee = j64bits(argjson,"desttxfee"); - return(0); -} - int32_t LP_quoteinfoinit(struct LP_quoteinfo *qp,struct LP_utxoinfo *utxo,char *destcoin,double price) { memset(qp,0,sizeof(*qp)); From b73b585916eba56327b7b1f0a40d0d48f4389b54 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:09:52 +0300 Subject: [PATCH 144/339] Test --- iguana/exchanges/LP_commands.c | 38 +++++++++++---------------------- iguana/exchanges/LP_nativeDEX.c | 2 +- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 5b97e4feb..9f25669ab 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -106,7 +106,7 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) return(0); } -double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port,char *base,char *rel,bits256 txid,int32_t vout,bits256 mypub,cJSON *argitem) +double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port,char *base,char *rel,bits256 mypub) { cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,flag = 0,pushsock = -1; double price = 0.; memset(qp,0,sizeof(*qp)); @@ -118,31 +118,17 @@ double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port, { if ( (pushsock= peer->pushsock) >= 0 ) { - reqjson = cJSON_CreateObject(); - jaddbits256(reqjson,"txid",txid); - jaddnum(reqjson,"vout",vout); - jaddstr(reqjson,"base",base); - jaddstr(reqjson,"rel",rel); - if ( bits256_nonz(mypub) != 0 ) - { + qp->desthash = mypub; + strcpy(qp->srccoin,base); + strcpy(qp->destcoin,rel); + reqjson = LP_quotejson(qp); + if ( bits256_nonz(qp->desthash) != 0 ) flag = 1; - jaddbits256(reqjson,"desthash",mypub); - if ( argitem != 0 ) - { - printf("ARGITEM.(%s)\n",jprint(argitem,0)); - jaddnum(reqjson,"timestamp",j64bits(argitem,"timestamp")); - jaddnum(reqjson,"quotetime",time(NULL)); - jadd64bits(reqjson,"satoshis",j64bits(argitem,"value")-j64bits(argitem,"txfee")); - jadd64bits(reqjson,"txfee",j64bits(argitem,"txfee")); - jadd64bits(reqjson,"desttxfee",j64bits(argitem,"desttxfee")); - jadd64bits(reqjson,"destsatoshis",j64bits(argitem,"destsatoshis")); - } - } jaddstr(reqjson,"method",method); LP_send(pushsock,jprint(reqjson,1),1); for (i=0; i<30; i++) { - if ( (price= LP_pricecache(qp,base,rel,txid,vout)) != 0. ) + if ( (price= LP_pricecache(qp,base,rel,qp->txid,qp->vout)) != 0. ) { if ( flag == 0 || bits256_nonz(qp->desthash) != 0 ) break; @@ -223,14 +209,14 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) { memset(prices,0,sizeof(prices)); memset(Q,0,sizeof(Q)); - //BTC 0.02500000 -> ([{"ipaddr":"5.9.253.196","port":7779,"profit":0.01035000,"base":"KMD","coin":"KMD","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","script":"76a914434009423522682bd7cc1b18a614c3096d19683188ac","txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"value":100,"deposit":"07902a65d11f0f577a0346432bcd2b6b53de5554c314209d1964693962524d69","dvout":1,"dvalue":120}]) for (i=0; icoin,jbits256(item,"txid"),jint(item,"vout"),zero,0); + LP_quoteparse(&Q[i],item); + price = LP_query("price",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,zero); if ( Q[i].destsatoshis != 0 && (double)j64bits(item,"value")/Q[i].destsatoshis > price ) price = (double)j64bits(item,"value")/Q[i].destsatoshis; } @@ -263,14 +249,14 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) item = jitem(array,besti); bestitem = LP_quotejson(&Q[i]); i = besti; - price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,0); + price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,utxo->mypub); if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); jaddnum(bestitem,"price",prices[besti]); if ( LP_price(base,utxo->coin) > 0.975*price ) { // the same, cleanup - price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,jbits256(item,"txid"),jint(item,"vout"),utxo->mypub,bestitem); + price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,utxo->mypub); } } } @@ -488,7 +474,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port else if ( IAMCLIENT == 0 && strcmp(method,"notifyutxo") == 0 ) { printf("utxonotify.(%s)\n",jprint(argjson,0)); - LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"deposit"),jint(argjson,"dvout"),SATOSHIDEN * jdouble(argjson,"dvalue"),jstr(argjson,"script"),jstr(argjson,"address"),jstr(argjson,"ipaddr"),juint(argjson,"port"),jdouble(argjson,"profit")); + LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"txid2"),jint(argjson,"vout2"),SATOSHIDEN * jdouble(argjson,"value2"),jstr(argjson,"script"),jstr(argjson,"address"),jstr(argjson,"ipaddr"),juint(argjson,"port"),jdouble(argjson,"profit")); retstr = clonestr("{\"result\":\"success\",\"notifyutxo\":\"received\"}"); } } diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index b5668c9b6..3d07f24cd 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -405,7 +405,7 @@ int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs if ( jobj(item,"txid") != 0 ) { txid = jbits256(item,"txid"); - utxo = LP_addutxo(amclient,mypeer,mypubsock,jstr(item,"coin"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"deposit"),jint(item,"dvout"),SATOSHIDEN * jdouble(item,"dvalue"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); + utxo = LP_addutxo(amclient,mypeer,mypubsock,jstr(item,"coin"),txid,jint(item,"vout"),SATOSHIDEN*jdouble(item,"value"),jbits256(item,"txid2"),jint(item,"vout2"),SATOSHIDEN * jdouble(item,"value2"),jstr(item,"script"),jstr(item,"address"),argipaddr,argport,jdouble(item,"profit")); if ( utxo != 0 ) utxo->lasttime = now; } From b6aaf8e6740dec508da7164b35b431fa02583f02 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:16:38 +0300 Subject: [PATCH 145/339] Test --- iguana/exchanges/LP_commands.c | 6 +++--- iguana/exchanges/LP_nativeDEX.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 9f25669ab..a3bce4b39 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -214,11 +214,11 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) item = jitem(array,i); if ( (price= jdouble(item,"price")) == 0. ) { -// horrible, pass in quoteinfo, utxo + printf("i.%d of %d: (%s)\n",i,n,jprint(item,0)); LP_quoteparse(&Q[i],item); price = LP_query("price",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,zero); - if ( Q[i].destsatoshis != 0 && (double)j64bits(item,"value")/Q[i].destsatoshis > price ) - price = (double)j64bits(item,"value")/Q[i].destsatoshis; + if ( Q[i].destsatoshis != 0 && (double)j64bits(item,"satoshis")/Q[i].destsatoshis > price ) + price = (double)j64bits(item,"satoshis")/Q[i].destsatoshis; } if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) ) bestprice = price; diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 3d07f24cd..733661f3e 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -460,7 +460,7 @@ char *issue_LP_notify(char *destip,uint16_t destport,char *ipaddr,uint16_t port, char *issue_LP_notifyutxo(char *destip,uint16_t destport,struct LP_utxoinfo *utxo) { char url[4096],str[65],str2[65]; - sprintf(url,"http://%s:%u/api/stats/notifyutxo?ipaddr=%s&port=%u&profit=%.6f&coin=%s&txid=%s&vout=%d&value=%.8f&deposit=%s&dvout=%d&dvalue=%.8f&script=%s&address=%s",destip,destport,utxo->ipaddr,utxo->port,utxo->profitmargin,utxo->coin,bits256_str(str,utxo->txid),utxo->vout,dstr(utxo->satoshis),bits256_str(str2,utxo->txid2),utxo->vout2,dstr(utxo->satoshis2),utxo->spendscript,utxo->coinaddr); + sprintf(url,"http://%s:%u/api/stats/notifyutxo?ipaddr=%s&port=%u&profit=%.6f&coin=%s&txid=%s&vout=%d&value=%.8f&txid2=%s&vout2=%d&value2=%.8f&script=%s&address=%s",destip,destport,utxo->ipaddr,utxo->port,utxo->profitmargin,utxo->coin,bits256_str(str,utxo->txid),utxo->vout,dstr(utxo->satoshis),bits256_str(str2,utxo->txid2),utxo->vout2,dstr(utxo->satoshis2),utxo->spendscript,utxo->coinaddr); if ( strlen(url) > 1024 ) printf("WARNING long url.(%s)\n",url); return(issue_curl(url)); From 36cfcd80d3d615321e243c7502046c6ce8a019fa Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:26:37 +0300 Subject: [PATCH 146/339] Test --- iguana/exchanges/LP_commands.c | 45 ++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index a3bce4b39..1a8382d3b 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -43,16 +43,27 @@ cJSON *LP_quotejson(struct LP_quoteinfo *qp) double price; cJSON *retjson = cJSON_CreateObject(); jaddstr(retjson,"base",qp->srccoin); jaddstr(retjson,"rel",qp->destcoin); - jaddstr(retjson,"address",qp->coinaddr); + if ( qp->coinaddr[0] != 0 ) + jaddstr(retjson,"address",qp->coinaddr); if ( qp->timestamp != 0 ) jaddnum(retjson,"timestamp",qp->timestamp); - jaddbits256(retjson,"txid",qp->txid); - jaddnum(retjson,"vout",qp->vout); - jaddbits256(retjson,"srchash",qp->srchash); - jadd64bits(retjson,"txfee",qp->txfee); + if ( bits256_nonz(qp->txid) != 0 ) + { + jaddbits256(retjson,"txid",qp->txid); + jaddnum(retjson,"vout",qp->vout); + } + if ( bits256_nonz(qp->srchash) != 0 ) + jaddbits256(retjson,"srchash",qp->srchash); + if ( qp->txfee != 0 ) + jadd64bits(retjson,"txfee",qp->txfee); if ( qp->quotetime != 0 ) jaddnum(retjson,"quotetime",qp->quotetime); - jadd64bits(retjson,"satoshis",qp->satoshis); + if ( qp->satoshis != 0 ) + { + jadd64bits(retjson,"satoshis",qp->satoshis); + price = (double)(qp->destsatoshis+qp->desttxfee) / qp->satoshis; + jaddnum(retjson,"price",price); + } if ( bits256_nonz(qp->desthash) != 0 ) jaddbits256(retjson,"desthash",qp->desthash); if ( bits256_nonz(qp->txid) != 0 ) @@ -64,20 +75,22 @@ cJSON *LP_quotejson(struct LP_quoteinfo *qp) { jaddbits256(retjson,"txid2",qp->txid2); jaddnum(retjson,"vout2",qp->vout2); - jadd64bits(retjson,"satoshis2",qp->satoshis2); + if ( qp->satoshis2 != 0 ) + jadd64bits(retjson,"satoshis2",qp->satoshis2); } if ( bits256_nonz(qp->desttxid) != 0 ) { - jaddstr(retjson,"destaddr",qp->destaddr); + if ( qp->destaddr[0] != 0 ) + jaddstr(retjson,"destaddr",qp->destaddr); jaddbits256(retjson,"desttxid",qp->desttxid); jaddnum(retjson,"destvout",qp->destvout); } - jadd64bits(retjson,"destsatoshis",qp->destsatoshis); - jadd64bits(retjson,"desttxfee",qp->desttxfee); + if ( qp->destsatoshis != 0 ) + jadd64bits(retjson,"destsatoshis",qp->destsatoshis); + if ( qp->desttxfee != 0 ) + jadd64bits(retjson,"desttxfee",qp->desttxfee); if ( qp->change != 0 ) jaddnum(retjson,"change",dstr(qp->change)); - price = (double)(qp->destsatoshis+qp->desttxfee) / qp->satoshis; - jaddnum(retjson,"price",price); return(retjson); } @@ -97,10 +110,12 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) qp->desttxid = jbits256(argjson,"desttxid"); qp->destvout = jint(argjson,"destvout"); qp->desthash = jbits256(argjson,"desthash"); - qp->satoshis = j64bits(argjson,"satoshis"); + if ( (qp->satoshis= j64bits(argjson,"satoshis")) == 0 ) + qp->satoshis = j64bits(argjson,"value"); + if ( (qp->satoshis2= j64bits(argjson,"satoshis2")) == 0 ) + qp->satoshis2 = j64bits(argjson,"value2"); qp->destsatoshis = j64bits(argjson,"destsatoshis"); qp->change = SATOSHIDEN * jdouble(argjson,"change"); - qp->satoshis2 = j64bits(argjson,"satoshis2"); qp->txfee = j64bits(argjson,"txfee"); qp->desttxfee = j64bits(argjson,"desttxfee"); return(0); @@ -214,8 +229,8 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) item = jitem(array,i); if ( (price= jdouble(item,"price")) == 0. ) { - printf("i.%d of %d: (%s)\n",i,n,jprint(item,0)); LP_quoteparse(&Q[i],item); + char str[65]; printf("i.%d of %d: (%s) -> txid.%s\n",i,n,jprint(item,0),bits256_str(str,Q[i].txid)); price = LP_query("price",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,zero); if ( Q[i].destsatoshis != 0 && (double)j64bits(item,"satoshis")/Q[i].destsatoshis > price ) price = (double)j64bits(item,"satoshis")/Q[i].destsatoshis; From 8d3893da90308e8f6a060d5769cf99d6abd13d9e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:31:00 +0300 Subject: [PATCH 147/339] Test --- iguana/exchanges/LP_commands.c | 1 - 1 file changed, 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 1a8382d3b..eb0de5c11 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -124,7 +124,6 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port,char *base,char *rel,bits256 mypub) { cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,flag = 0,pushsock = -1; double price = 0.; - memset(qp,0,sizeof(*qp)); if ( ipaddr != 0 && port >= 1000 ) { if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),port)) == 0 ) From 6b3b43ca651532a740bb3afbf8408303a8e55250 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:34:44 +0300 Subject: [PATCH 148/339] Test --- iguana/exchanges/LP_commands.c | 2 +- iguana/exchanges/LP_nativeDEX.c | 1 + iguana/exchanges/LP_rpc.c | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index eb0de5c11..69b7452d5 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -236,7 +236,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) } if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) ) bestprice = price; - //printf("i.%d price %.8f bestprice %.8f: (%s)\n",i,price,bestprice,jprint(item,0)); + printf("i.%d price %.8f bestprice %.8f: (%s)\n",i,price,bestprice,jprint(item,0)); } if ( bestprice != 0. ) { diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 733661f3e..44c25270b 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -100,6 +100,7 @@ struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout { printf("expire price %.8f\n",ptr->price); ptr->price = 0.; + ptr->timestamp = 0; memset(&ptr->Q,0,sizeof(ptr->Q)); } return(ptr); diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index f912440aa..b30a303a4 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -155,7 +155,7 @@ double LP_getestimatedrate(char *symbol) if ( retstr[0] != '-' ) { rate = atof(retstr) / 1024.; - printf("estimated rate %s -> %.8f\n",retstr,rate); + printf("estimated rate.%s %s -> %.8f\n",coin->symbol,retstr,rate); } free(retstr); } From 52ae8865f88477a128f85e43b3deec0ce1ca2096 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:37:19 +0300 Subject: [PATCH 149/339] test --- iguana/exchanges/LP_nativeDEX.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 44c25270b..c2305dc23 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -922,11 +922,11 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i while ( 1 ) { nonz = 0; - if ( (counter++ % 300) == 0 ) + if ( (counter++ % 2000) == 0 ) LP_privkey_updates(mypeer,pubsock,passphrase,amclient); HASH_ITER(hh,LP_peerinfos,peer,tmp) { - if ( peer->numpeers != mypeer->numpeers || (rand() % 10) == 0 ) + if ( peer->numpeers != mypeer->numpeers || (rand() % 1000) == 0 ) { if ( peer->numpeers != mypeer->numpeers ) printf("%s num.%d vs %d\n",peer->ipaddr,peer->numpeers,mypeer->numpeers); From 46e91f04f141648dd144938c7797e1563fe29934 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:43:04 +0300 Subject: [PATCH 150/339] Test --- iguana/exchanges/LP_commands.c | 2 +- iguana/exchanges/LP_nativeDEX.c | 5 +++-- iguana/exchanges/LP_rpc.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 69b7452d5..4c69954dd 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -284,6 +284,7 @@ int32_t LP_quoteinfoinit(struct LP_quoteinfo *qp,struct LP_utxoinfo *utxo,char * { memset(qp,0,sizeof(*qp)); qp->timestamp = (uint32_t)time(NULL); + safecopy(qp->destcoin,destcoin,sizeof(qp->destcoin)); if ( (qp->txfee= LP_getestimatedrate(utxo->coin)*LP_AVETXSIZE) < 10000 ) qp->txfee = 10000; if ( qp->txfee >= utxo->satoshis || qp->txfee >= utxo->satoshis2 ) @@ -302,7 +303,6 @@ int32_t LP_quoteinfoinit(struct LP_quoteinfo *qp,struct LP_utxoinfo *utxo,char * qp->destsatoshis -= qp->desttxfee; safecopy(qp->srccoin,utxo->coin,sizeof(qp->srccoin)); safecopy(qp->coinaddr,utxo->coinaddr,sizeof(qp->coinaddr)); - safecopy(qp->destcoin,destcoin,sizeof(qp->destcoin)); qp->srchash = LP_pubkey(LP_privkey(utxo->coinaddr)); return(0); } diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index c2305dc23..01b57a821 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -113,7 +113,7 @@ double LP_pricecache(struct LP_quoteinfo *qp,char *base,char *rel,bits256 txid,i { if ( qp != 0 ) (*qp) = ptr->Q; - //printf("found %s/%s %.8f\n",base,rel,ptr->price); + printf("found %s/%s %.8f\n",base,rel,ptr->price); return(ptr->price); } else if ( qp != 0 ) @@ -140,6 +140,7 @@ struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout, ptr->price = price; ptr->Q = *qp; ptr->timestamp = (uint32_t)time(NULL); + printf("cacheadd %.8f\n",price); return(ptr); } @@ -926,7 +927,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i LP_privkey_updates(mypeer,pubsock,passphrase,amclient); HASH_ITER(hh,LP_peerinfos,peer,tmp) { - if ( peer->numpeers != mypeer->numpeers || (rand() % 1000) == 0 ) + if ( peer->numpeers != mypeer->numpeers || (rand() % 10000) == 0 ) { if ( peer->numpeers != mypeer->numpeers ) printf("%s num.%d vs %d\n",peer->ipaddr,peer->numpeers,mypeer->numpeers); diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index b30a303a4..d6a3dfeab 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -155,7 +155,7 @@ double LP_getestimatedrate(char *symbol) if ( retstr[0] != '-' ) { rate = atof(retstr) / 1024.; - printf("estimated rate.%s %s -> %.8f\n",coin->symbol,retstr,rate); + printf("estimated rate.(%s) %s -> %.8f\n",symbol,retstr,rate); } free(retstr); } From 6e5fa167ea57dab2108da3e9d2e15386e049f224 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:48:19 +0300 Subject: [PATCH 151/339] Test --- iguana/exchanges/LP_nativeDEX.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 01b57a821..6a68e716e 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -113,6 +113,11 @@ double LP_pricecache(struct LP_quoteinfo *qp,char *base,char *rel,bits256 txid,i { if ( qp != 0 ) (*qp) = ptr->Q; + if ( ptr->price == 0. && ptr->Q.satoshis != 0 ) + { + printf("null ptr->price? "); + ptr->price = (double)ptr->Q.destsatoshis / ptr->Q.satoshis; + } printf("found %s/%s %.8f\n",base,rel,ptr->price); return(ptr->price); } From 800c69d0aa56636a9cb324a1dbaabe479e01efbe Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:52:23 +0300 Subject: [PATCH 152/339] Test --- iguana/exchanges/LP_commands.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 4c69954dd..57ed4622a 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -145,7 +145,10 @@ double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port, if ( (price= LP_pricecache(qp,base,rel,qp->txid,qp->vout)) != 0. ) { if ( flag == 0 || bits256_nonz(qp->desthash) != 0 ) + { + printf("break out of loop.%d price %.8f\n",i,price); break; + } } usleep(250000); } @@ -231,8 +234,12 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) LP_quoteparse(&Q[i],item); char str[65]; printf("i.%d of %d: (%s) -> txid.%s\n",i,n,jprint(item,0),bits256_str(str,Q[i].txid)); price = LP_query("price",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,zero); + printf("price %.8f\n",price); if ( Q[i].destsatoshis != 0 && (double)j64bits(item,"satoshis")/Q[i].destsatoshis > price ) + { + printf("adjustprice %.8f -> %.8f\n",price,(double)j64bits(item,"satoshis")/Q[i].destsatoshis); price = (double)j64bits(item,"satoshis")/Q[i].destsatoshis; + } } if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) ) bestprice = price; From b5c8c9b80f7fedc30aa2388bdfdf64e50377ae4f Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:54:31 +0300 Subject: [PATCH 153/339] Test --- iguana/exchanges/LP_nativeDEX.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 6a68e716e..eb07ecfe9 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -123,7 +123,7 @@ double LP_pricecache(struct LP_quoteinfo *qp,char *base,char *rel,bits256 txid,i } else if ( qp != 0 ) memset(qp,0,sizeof(*qp)); - //char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); + char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); return(0.); } @@ -140,8 +140,8 @@ struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout, portable_mutex_unlock(&LP_cachemutex); } else printf("LP_cacheadd keysize mismatch?\n"); } //else printf("CACHE hit!\n"); - //char str[65]; if ( price != ptr->price ) - // printf("updated %s/v%d %s/%s %llu price %.8f\n",bits256_str(str,txid),vout,base,rel,(long long)satoshis,price); + char str[65]; if ( price != ptr->price ) + printf("updated %s/v%d %s/%s %llu price %.8f\n",bits256_str(str,txid),vout,base,rel,(long long)qp->satoshis,price); ptr->price = price; ptr->Q = *qp; ptr->timestamp = (uint32_t)time(NULL); From 7740ae6d0e4ba05dc696481daf686e1e1f41e2a0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 08:55:57 +0300 Subject: [PATCH 154/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index eb07ecfe9..687d17212 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -121,9 +121,7 @@ double LP_pricecache(struct LP_quoteinfo *qp,char *base,char *rel,bits256 txid,i printf("found %s/%s %.8f\n",base,rel,ptr->price); return(ptr->price); } - else if ( qp != 0 ) - memset(qp,0,sizeof(*qp)); - char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); + //char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); return(0.); } From 14a7a7296a632389278f6b624182570c6e066002 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 09:05:31 +0300 Subject: [PATCH 155/339] Test --- iguana/exchanges/LP_commands.c | 135 ++++++++++++++++---------------- iguana/exchanges/LP_nativeDEX.c | 4 +- 2 files changed, 71 insertions(+), 68 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 57ed4622a..a4461d6b3 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -121,6 +121,66 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) return(0); } +int32_t LP_quoteinfoinit(struct LP_quoteinfo *qp,struct LP_utxoinfo *utxo,char *destcoin,double price) +{ + memset(qp,0,sizeof(*qp)); + qp->timestamp = (uint32_t)time(NULL); + safecopy(qp->destcoin,destcoin,sizeof(qp->destcoin)); + if ( (qp->txfee= LP_getestimatedrate(utxo->coin)*LP_AVETXSIZE) < 10000 ) + qp->txfee = 10000; + if ( qp->txfee >= utxo->satoshis || qp->txfee >= utxo->satoshis2 ) + return(-1); + qp->txid = utxo->txid; + qp->vout = utxo->vout; + qp->txid2 = utxo->txid2; + qp->vout2 = utxo->vout2; + qp->satoshis2 = utxo->satoshis2 - qp->txfee; + qp->satoshis = utxo->satoshis - qp->txfee; + qp->destsatoshis = qp->satoshis * price; + if ( (qp->desttxfee= LP_getestimatedrate(qp->destcoin) * LP_AVETXSIZE) < 10000 ) + qp->desttxfee = 10000; + if ( qp->desttxfee >= qp->destsatoshis ) + return(-2); + qp->destsatoshis -= qp->desttxfee; + safecopy(qp->srccoin,utxo->coin,sizeof(qp->srccoin)); + safecopy(qp->coinaddr,utxo->coinaddr,sizeof(qp->coinaddr)); + qp->srchash = LP_pubkey(LP_privkey(utxo->coinaddr)); + return(0); +} + +int32_t LP_quoteinfoset(struct LP_quoteinfo *qp,uint32_t timestamp,uint32_t quotetime,uint64_t value,uint64_t txfee,uint64_t destsatoshis,uint64_t desttxfee,bits256 desttxid,int32_t destvout,bits256 desthash,char *destaddr) +{ + if ( txfee != qp->txfee ) + { + if ( txfee >= value ) + return(-1); + qp->txfee = txfee; + qp->satoshis = value - txfee; + } + qp->timestamp = timestamp; + qp->quotetime = quotetime; + qp->destsatoshis = destsatoshis; + qp->desttxfee = desttxfee; + qp->desttxid = desttxid; + qp->destvout = destvout; + qp->desthash = desthash; + safecopy(qp->destaddr,destaddr,sizeof(qp->destaddr)); + return(0); +} + +char *LP_quote(cJSON *argjson) +{ + struct LP_cacheinfo *ptr; double price; struct LP_quoteinfo Q; + LP_quoteparse(&Q,argjson); + price = (double)(Q.destsatoshis + Q.desttxfee) / (Q.satoshis + Q.txfee); + if ( (ptr= LP_cacheadd(Q.srccoin,Q.destcoin,Q.txid,Q.vout,price,&Q)) != 0 ) + { + ptr->Q = Q; + return(clonestr("{\"result\":\"updated\"}")); + } + else return(clonestr("{\"error\":\"nullptr\"}")); +} + double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port,char *base,char *rel,bits256 mypub) { cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,flag = 0,pushsock = -1; double price = 0.; @@ -135,6 +195,8 @@ double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port, qp->desthash = mypub; strcpy(qp->srccoin,base); strcpy(qp->destcoin,rel); + if ( strcmp(method,"request") == 0 ) + qp->quotetime = (uint32_t)time(NULL); reqjson = LP_quotejson(qp); if ( bits256_nonz(qp->desthash) != 0 ) flag = 1; @@ -146,7 +208,7 @@ double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port, { if ( flag == 0 || bits256_nonz(qp->desthash) != 0 ) { - printf("break out of loop.%d price %.8f\n",i,price); + //printf("break out of loop.%d price %.8f\n",i,price); break; } } @@ -232,9 +294,8 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) if ( (price= jdouble(item,"price")) == 0. ) { LP_quoteparse(&Q[i],item); - char str[65]; printf("i.%d of %d: (%s) -> txid.%s\n",i,n,jprint(item,0),bits256_str(str,Q[i].txid)); + //char str[65]; printf("i.%d of %d: (%s) -> txid.%s\n",i,n,jprint(item,0),bits256_str(str,Q[i].txid)); price = LP_query("price",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,zero); - printf("price %.8f\n",price); if ( Q[i].destsatoshis != 0 && (double)j64bits(item,"satoshis")/Q[i].destsatoshis > price ) { printf("adjustprice %.8f -> %.8f\n",price,(double)j64bits(item,"satoshis")/Q[i].destsatoshis); @@ -243,7 +304,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) } if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) ) bestprice = price; - printf("i.%d price %.8f bestprice %.8f: (%s)\n",i,price,bestprice,jprint(item,0)); + //printf("i.%d price %.8f bestprice %.8f: (%s)\n",i,price,bestprice,jprint(item,0)); } if ( bestprice != 0. ) { @@ -287,67 +348,6 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) return(bestitem); } -int32_t LP_quoteinfoinit(struct LP_quoteinfo *qp,struct LP_utxoinfo *utxo,char *destcoin,double price) -{ - memset(qp,0,sizeof(*qp)); - qp->timestamp = (uint32_t)time(NULL); - safecopy(qp->destcoin,destcoin,sizeof(qp->destcoin)); - if ( (qp->txfee= LP_getestimatedrate(utxo->coin)*LP_AVETXSIZE) < 10000 ) - qp->txfee = 10000; - if ( qp->txfee >= utxo->satoshis || qp->txfee >= utxo->satoshis2 ) - return(-1); - qp->txid = utxo->txid; - qp->vout = utxo->vout; - qp->txid2 = utxo->txid2; - qp->vout2 = utxo->vout2; - qp->satoshis2 = utxo->satoshis2 - qp->txfee; - qp->satoshis = utxo->satoshis - qp->txfee; - qp->destsatoshis = qp->satoshis * price; - if ( (qp->desttxfee= LP_getestimatedrate(qp->destcoin) * LP_AVETXSIZE) < 10000 ) - qp->desttxfee = 10000; - if ( qp->desttxfee >= qp->destsatoshis ) - return(-2); - qp->destsatoshis -= qp->desttxfee; - safecopy(qp->srccoin,utxo->coin,sizeof(qp->srccoin)); - safecopy(qp->coinaddr,utxo->coinaddr,sizeof(qp->coinaddr)); - qp->srchash = LP_pubkey(LP_privkey(utxo->coinaddr)); - return(0); -} - -int32_t LP_quoteinfoset(struct LP_quoteinfo *qp,uint32_t timestamp,uint32_t quotetime,uint64_t value,uint64_t txfee,uint64_t destsatoshis,uint64_t desttxfee,bits256 desttxid,int32_t destvout,bits256 desthash,char *destaddr) -{ - if ( txfee != qp->txfee ) - { - if ( txfee >= value ) - return(-1); - qp->txfee = txfee; - qp->satoshis = value - txfee; - } - qp->timestamp = timestamp; - qp->quotetime = quotetime; - qp->destsatoshis = destsatoshis; - qp->desttxfee = desttxfee; - qp->desttxid = desttxid; - qp->destvout = destvout; - qp->desthash = desthash; - safecopy(qp->destaddr,destaddr,sizeof(qp->destaddr)); - return(0); -} - -char *LP_quote(cJSON *argjson) -{ - struct LP_cacheinfo *ptr; double price; struct LP_quoteinfo Q; - LP_quoteparse(&Q,argjson); - price = (double)(Q.destsatoshis + Q.desttxfee) / (Q.satoshis + Q.txfee); - if ( (ptr= LP_cacheadd(Q.srccoin,Q.destcoin,Q.txid,Q.vout,price,&Q)) != 0 ) - { - //SENT.({"base":"KMD","rel":"BTC","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","timestamp":1496216835,"price":0.00021141,"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","srchash":"0bcabd875bfa724e26de5f35035ca3767c50b30960e23cbfcbd478cac9147412","txfee":"100000","desttxfee":"10000","value":"10000000000","satoshis":"9999900000","destsatoshis":"2124104","method":"quote"}) - ptr->Q = Q; - return(clonestr("{\"result\":\"updated\"}")); - } - else return(clonestr("{\"error\":\"nullptr\"}")); -} - int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { char *method,*base,*rel,*retstr,pairstr[512]; cJSON *retjson; double price; bits256 privkey,txid; struct LP_utxoinfo *utxo; int32_t retval = -1,DEXselector = 0; uint64_t destvalue; struct basilisk_request R; struct LP_quoteinfo Q; @@ -371,6 +371,8 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin price *= (1. + profitmargin); if ( LP_quoteinfoinit(&Q,utxo,rel,price) < 0 ) return(-1); + if ( strcmp(method,"price") == 0 ) + Q.timestamp = (uint32_t)time(NULL); retjson = LP_quotejson(&Q); if ( strcmp(method,"request") == 0 ) { @@ -381,7 +383,8 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin jaddnum(retjson,"pending",utxo->swappending); jaddbits256(retjson,"desthash",utxo->otherpubkey); jaddstr(retjson,"method","reserved"); - } else jaddstr(retjson,"method","quote"); + } + else jaddstr(retjson,"method","quote"); retstr = jprint(retjson,1); LP_send(pubsock,retstr,1); } else printf("null price\n"); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 687d17212..c31d69ece 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -96,7 +96,7 @@ struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout HASH_FIND(hh,LP_cacheinfos,key,sizeof(key),ptr); portable_mutex_unlock(&LP_cachemutex); } else printf("LP_cachefind keysize mismatch?\n"); - if ( ptr != 0 && ptr->timestamp != 0 && ptr->timestamp < time(NULL)-LP_CACHEDURATION ) + if ( 0 && ptr != 0 && ptr->timestamp != 0 && ptr->timestamp < time(NULL)-LP_CACHEDURATION ) { printf("expire price %.8f\n",ptr->price); ptr->price = 0.; @@ -118,7 +118,7 @@ double LP_pricecache(struct LP_quoteinfo *qp,char *base,char *rel,bits256 txid,i printf("null ptr->price? "); ptr->price = (double)ptr->Q.destsatoshis / ptr->Q.satoshis; } - printf("found %s/%s %.8f\n",base,rel,ptr->price); + //printf("found %s/%s %.8f\n",base,rel,ptr->price); return(ptr->price); } //char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); From e036e7ea0022ec651022d28ffc20982a2b32f4ce Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 09:08:57 +0300 Subject: [PATCH 156/339] Test --- iguana/exchanges/LP_commands.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index a4461d6b3..1ca121b80 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -404,6 +404,8 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin if ( LP_quoteparse(&Q,argjson) < 0 ) return(-2); privkey = LP_privkey(utxo->coinaddr); + if ( bits256_nonz(utxo->mypub) == 0 ) + utxo->mypub = LP_pubkey(privkey); if ( bits256_nonz(privkey) != 0 && Q.timestamp == utxo->swappending-LP_RESERVETIME && Q.quotetime >= Q.timestamp && Q.quotetime < utxo->swappending && bits256_cmp(utxo->mypub,Q.srchash) == 0 && (destvalue= LP_txvalue(rel,Q.desttxid,Q.destvout)) >= price*Q.satoshis+Q.desttxfee && destvalue >= Q.destsatoshis+Q.desttxfee ) { Q.change = destvalue - (Q.destsatoshis+Q.desttxfee); @@ -439,7 +441,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin nn_close(utxo->pair); utxo->pair = -1; } - } else printf("dest %.8f < required %.8f (%d %d %d %d %d %d)\n",dstr(Q.satoshis),dstr(price*(utxo->satoshis-Q.txfee)),bits256_nonz(privkey) != 0 ,Q.timestamp == utxo->swappending-LP_RESERVETIME ,Q.quotetime >= Q.timestamp ,Q.quotetime < utxo->swappending ,bits256_cmp(utxo->mypub,Q.srchash) == 0 , LP_txvalue(rel,Q.desttxid,Q.destvout) >= price*Q.satoshis+Q.desttxfee); + } else printf("dest %.8f < required %.8f (%d %d %d %d %d %d) %.8f %.8f\n",dstr(Q.satoshis),dstr(price*(utxo->satoshis-Q.txfee)),bits256_nonz(privkey) != 0 ,Q.timestamp == utxo->swappending-LP_RESERVETIME ,Q.quotetime >= Q.timestamp ,Q.quotetime < utxo->swappending ,bits256_cmp(utxo->mypub,Q.srchash) == 0 , LP_txvalue(rel,Q.desttxid,Q.destvout) >= price*Q.satoshis+Q.desttxfee,dstr(LP_txvalue(rel,Q.desttxid,Q.destvout)),dstr(price*Q.satoshis+Q.desttxfee)); } else printf("no price for %s/%s\n",base,rel); } else printf("utxo->pair.%d when connect came in (%s)\n",utxo->pair,jprint(argjson,0)); } From 880f82174e619cf594cf705cf1cb16ed8ad278f7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 09:10:42 +0300 Subject: [PATCH 157/339] Test --- iguana/exchanges/LP_transaction.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index eea8a63fc..2d192ef5b 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -67,6 +67,7 @@ uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) uint64_t value = 0; cJSON *txobj,*vouts,*utxoobj; int32_t numvouts; if ( (txobj= LP_gettx(symbol,txid)) != 0 ) { + printf("%s txobj.(%s)\n",symbol,jprint(txobj,0)); if ( (vouts= jarray(&numvouts,txobj,"vout")) != 0 && vout < numvouts ) { utxoobj = jitem(vouts,vout); From 76b37edd3a9a38918ed58fb251f9992de3120077 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 09:21:36 +0300 Subject: [PATCH 158/339] Test --- iguana/exchanges/LP_commands.c | 16 +++++++++------- iguana/exchanges/LP_transaction.c | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 1ca121b80..34554aebb 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -277,12 +277,12 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) return(retarray); } -cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) +cJSON *LP_bestprice(struct LP_utxoinfo *myutxo,char *base) { static bits256 zero; int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; struct LP_quoteinfo Q[sizeof(prices)/sizeof(*prices)]; bestprice = 0.; - if ( (array= LP_tradecandidates(utxo,base)) != 0 ) + if ( (array= LP_tradecandidates(myutxo,base)) != 0 ) { if ( (n= cJSON_GetArraySize(array)) > 0 ) { @@ -295,7 +295,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) { LP_quoteparse(&Q[i],item); //char str[65]; printf("i.%d of %d: (%s) -> txid.%s\n",i,n,jprint(item,0),bits256_str(str,Q[i].txid)); - price = LP_query("price",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,zero); + price = LP_query("price",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,zero); if ( Q[i].destsatoshis != 0 && (double)j64bits(item,"satoshis")/Q[i].destsatoshis > price ) { printf("adjustprice %.8f -> %.8f\n",price,(double)j64bits(item,"satoshis")/Q[i].destsatoshis); @@ -326,19 +326,21 @@ cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base) } } } - if ( besti >= 0 )//&& bits256_cmp(utxo->mypub,otherpubs[besti]) == 0 ) + if ( besti >= 0 )//&& bits256_cmp(myutxo->mypub,otherpubs[besti]) == 0 ) { item = jitem(array,besti); bestitem = LP_quotejson(&Q[i]); i = besti; - price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,utxo->mypub); + Q[i].desttxid = myutxo->txid; + Q[i].destvout = myutxo->vout; + price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); jaddnum(bestitem,"price",prices[besti]); - if ( LP_price(base,utxo->coin) > 0.975*price ) + if ( LP_price(base,myutxo->coin) > 0.975*price ) { // the same, cleanup - price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,utxo->mypub); + price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); } } } diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 2d192ef5b..c54336e74 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -67,7 +67,7 @@ uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) uint64_t value = 0; cJSON *txobj,*vouts,*utxoobj; int32_t numvouts; if ( (txobj= LP_gettx(symbol,txid)) != 0 ) { - printf("%s txobj.(%s)\n",symbol,jprint(txobj,0)); + char str[65]; printf("%s.(%s) txobj.(%s)\n",symbol,bits256_str(str,txid),jprint(txobj,0)); if ( (vouts= jarray(&numvouts,txobj,"vout")) != 0 && vout < numvouts ) { utxoobj = jitem(vouts,vout); From 5f1ff2296736974a955bec23eeb665544a21c9b7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 09:26:21 +0300 Subject: [PATCH 159/339] Test --- iguana/exchanges/LP_commands.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 34554aebb..2d967fc8b 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -66,11 +66,6 @@ cJSON *LP_quotejson(struct LP_quoteinfo *qp) } if ( bits256_nonz(qp->desthash) != 0 ) jaddbits256(retjson,"desthash",qp->desthash); - if ( bits256_nonz(qp->txid) != 0 ) - { - jaddbits256(retjson,"txid",qp->txid); - jaddnum(retjson,"vout",qp->vout); - } if ( bits256_nonz(qp->txid2) != 0 ) { jaddbits256(retjson,"txid2",qp->txid2); @@ -339,7 +334,8 @@ cJSON *LP_bestprice(struct LP_utxoinfo *myutxo,char *base) jaddnum(bestitem,"price",prices[besti]); if ( LP_price(base,myutxo->coin) > 0.975*price ) { -// the same, cleanup + Q[i].desttxid = myutxo->txid; + Q[i].destvout = myutxo->vout; price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); } } @@ -397,7 +393,6 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin retval = 4; if ( utxo->pair < 0 ) { - //LP_command.({"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"base":"KMD","rel":"BTC","mypub":"3b8f71015d644aaa4c9cceeee289b9a50dc9ec7fafab861c4d5872a8e3844466","satoshis":"0","txfee":"100000","desttxfee":"10000","destsatoshis":"2163363","method":"connect"}) if ( (price= LP_price(base,rel)) != 0. ) { price *= (1. + profitmargin); @@ -405,6 +400,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin return(-1); if ( LP_quoteparse(&Q,argjson) < 0 ) return(-2); + printf("connect with.(%s)\n",jprint(argjson,0)); privkey = LP_privkey(utxo->coinaddr); if ( bits256_nonz(utxo->mypub) == 0 ) utxo->mypub = LP_pubkey(privkey); From c7d9eff67041bd41071ebdb5b88e71131aacb26d Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 09:31:35 +0300 Subject: [PATCH 160/339] Test --- iguana/exchanges/LP_commands.c | 7 +++---- iguana/exchanges/LP_transaction.c | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 2d967fc8b..39763fea1 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -23,17 +23,16 @@ struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srch struct basilisk_request R; memset(rp,0,sizeof(*rp)); rp->srchash = srchash; - rp->desthash = desthash; rp->srcamount = srcsatoshis; - rp->destamount = destsatoshis; rp->timestamp = timestamp; - rp->quotetime = quotetime; rp->DEXselector = DEXselector; safecopy(rp->src,src,sizeof(rp->src)); safecopy(rp->dest,dest,sizeof(rp->dest)); R = *rp; rp->requestid = basilisk_requestid(rp); - *rp = R; + rp->quotetime = quotetime; + rp->desthash = desthash; + rp->destamount = destsatoshis; rp->quoteid = basilisk_quoteid(rp); return(rp); } diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index c54336e74..3a8d63120 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -67,7 +67,7 @@ uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) uint64_t value = 0; cJSON *txobj,*vouts,*utxoobj; int32_t numvouts; if ( (txobj= LP_gettx(symbol,txid)) != 0 ) { - char str[65]; printf("%s.(%s) txobj.(%s)\n",symbol,bits256_str(str,txid),jprint(txobj,0)); + //char str[65]; printf("%s.(%s) txobj.(%s)\n",symbol,bits256_str(str,txid),jprint(txobj,0)); if ( (vouts= jarray(&numvouts,txobj,"vout")) != 0 && vout < numvouts ) { utxoobj = jitem(vouts,vout); From 45544ec6a5034c1a62703ca9124e14658dad0505 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 09:34:27 +0300 Subject: [PATCH 161/339] Test --- iguana/exchanges/LP_commands.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 39763fea1..0f5c6a257 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -411,6 +411,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin printf("error creating utxo->pair\n"); else if ( nn_connect(utxo->pair,pairstr) >= 0 ) { + char str[65]; printf("destsatoshis %.8f %s t%u\n",dstr(Q.destsatoshis),bits256_str(str,Q.desthash),Q.quotetime); LP_requestinit(&R,Q.srchash,Q.desthash,base,Q.satoshis,rel,Q.destsatoshis,Q.timestamp,Q.quotetime,DEXselector); if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_bobloop,(void *)utxo) == 0 ) { From 209c02cefee13795c504e00d5ddf1c9575555b3d Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 09:38:25 +0300 Subject: [PATCH 162/339] Test --- iguana/exchanges/LP_commands.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 0f5c6a257..af337b933 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -274,7 +274,7 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) cJSON *LP_bestprice(struct LP_utxoinfo *myutxo,char *base) { static bits256 zero; - int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; struct LP_quoteinfo Q[sizeof(prices)/sizeof(*prices)]; + int32_t i,n,besti,DEXselector=0; cJSON *array,*item,*bestitem=0; struct basilisk_request R; double bestmetric,metric,bestprice=0.,price,prices[100]; struct LP_quoteinfo Q[sizeof(prices)/sizeof(*prices)]; bestprice = 0.; if ( (array= LP_tradecandidates(myutxo,base)) != 0 ) { @@ -336,6 +336,8 @@ cJSON *LP_bestprice(struct LP_utxoinfo *myutxo,char *base) Q[i].desttxid = myutxo->txid; Q[i].destvout = myutxo->vout; price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); + LP_requestinit(&R,Q[i].srchash,Q[i].desthash,base,Q[i].satoshis,Q[i].destcoin,Q[i].destsatoshis,Q[i].timestamp,Q[i].quotetime,DEXselector); + printf("Alice r.%u q.%u\n",R.requestid,R.quoteid); } } } @@ -399,7 +401,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin return(-1); if ( LP_quoteparse(&Q,argjson) < 0 ) return(-2); - printf("connect with.(%s)\n",jprint(argjson,0)); + //printf("connect with.(%s)\n",jprint(argjson,0)); privkey = LP_privkey(utxo->coinaddr); if ( bits256_nonz(utxo->mypub) == 0 ) utxo->mypub = LP_pubkey(privkey); @@ -411,7 +413,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin printf("error creating utxo->pair\n"); else if ( nn_connect(utxo->pair,pairstr) >= 0 ) { - char str[65]; printf("destsatoshis %.8f %s t%u\n",dstr(Q.destsatoshis),bits256_str(str,Q.desthash),Q.quotetime); + //char str[65]; printf("destsatoshis %.8f %s t%u\n",dstr(Q.destsatoshis),bits256_str(str,Q.desthash),Q.quotetime); LP_requestinit(&R,Q.srchash,Q.desthash,base,Q.satoshis,rel,Q.destsatoshis,Q.timestamp,Q.quotetime,DEXselector); if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_bobloop,(void *)utxo) == 0 ) { From 79b4f515f7f20c064f4ba8fbb98f85198f230af6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 10:19:25 +0300 Subject: [PATCH 163/339] Test --- iguana/exchanges/LP_swap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index fe2df8a7c..86f1d0a0c 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -192,7 +192,7 @@ uint32_t basilisk_requestid(struct basilisk_request *rp) R.requestid = R.quoteid = R.quotetime = R.DEXselector = 0; R.destamount = R.unused = 0; memset(R.desthash.bytes,0,sizeof(R.desthash.bytes)); - if ( 1 ) + if ( 0 ) { int32_t i; for (i=0; i Date: Thu, 1 Jun 2017 10:51:25 +0300 Subject: [PATCH 164/339] Test --- iguana/exchanges/LP_commands.c | 17 +++++++++++++++-- iguana/exchanges/LP_include.h | 1 + iguana/exchanges/LP_nativeDEX.c | 5 +++-- iguana/exchanges/LP_swap.c | 24 +++++++++++------------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index af337b933..dc0b68a5c 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -486,8 +486,21 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port retstr = LP_quote(argjson); else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 ) { - retstr = jprint(argjson,0); - printf("got connected! (%s)\n",retstr); + int32_t pairsock = -1; char *pairstr; + if ( (pairstr= jstr(argjson,"pair")) == 0 || (pairsock= nn_socket(AF_SP,NN_PAIR)) < 0 ) + printf("error creating pairsock\n"); + else if ( nn_connect(pairsock,pairstr) >= 0 ) + { + struct LP_quoteinfo *qp; int32_t DEXselector = 0; + qp = calloc(1,sizeof(*qp)); + LP_quoteparse(qp,argjson); + qp->pair = pairsock; + qp->privkey = LP_privkey(qp->destaddr); + LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis,qp->destcoin,qp->destsatoshis,qp->timestamp,qp->quotetime,DEXselector); + if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_aliceloop,(void *)qp) == 0 ) + { + } + } } else if ( IAMCLIENT == 0 && strcmp(method,"getprice") == 0 ) retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel")); diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index a7e239f2b..35c38051a 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -470,6 +470,7 @@ enum opcodetype void basilisk_dontforget_update(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx); uint32_t basilisk_requestid(struct basilisk_request *rp); uint32_t basilisk_quoteid(struct basilisk_request *rp); +struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256 privkey,struct basilisk_request *rp); char *bitcoind_passthru(char *coinstr,char *serverport,char *userpass,char *method,char *params); struct iguana_info *LP_coinfind(char *symbol); void *curl_post(void **cHandlep,char *url,char *userpass,char *postfields,char *hdr0,char *hdr1,char *hdr2,char *hdr3); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index c31d69ece..29c81b18e 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -60,9 +60,10 @@ struct LP_utxoinfo struct LP_quoteinfo { - bits256 srchash,desthash,txid,txid2,desttxid; + struct basilisk_request R; + bits256 srchash,desthash,txid,txid2,desttxid,privkey; uint64_t satoshis,satoshis2,txfee,destsatoshis,desttxfee,change; - uint32_t timestamp,quotetime; int32_t vout,vout2,destvout; + uint32_t timestamp,quotetime; int32_t vout,vout2,destvout,pair; char srccoin[16],coinaddr[64],destcoin[16],destaddr[64]; }; diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 86f1d0a0c..d70b78954 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -527,22 +527,21 @@ void LP_bobloop(void *_utxo) utxo->pair = -1; } -void LP_aliceloop(void *_utxo) +void LP_aliceloop(void *_qp) { - uint8_t *data; int32_t maxlen; uint32_t expiration; struct basilisk_swap *swap; struct LP_utxoinfo *utxo = _utxo; - fprintf(stderr,"start swap iambob\n"); + uint8_t *data; int32_t maxlen; uint32_t expiration; struct basilisk_swap *swap = 0; struct LP_quoteinfo *qp = _qp; + fprintf(stderr,"start swap iamalice\n"); maxlen = 1024*1024 + sizeof(*swap); data = malloc(maxlen); expiration = (uint32_t)time(NULL) + 10; - while ( (swap= utxo->swap) == 0 && time(NULL) < expiration ) - sleep(1); - if ( (utxo->swap= swap) != 0 ) + swap = LP_swapinit(1,0,qp->privkey,&qp->R); + if ( swap != 0 ) { - if ( LP_sendwait("pubkeys",10,utxo->pair,swap,data,maxlen,LP_pubkeys_verify,LP_pubkeys_data) < 0 ) + if ( LP_sendwait("pubkeys",10,qp->pair,swap,data,maxlen,LP_pubkeys_verify,LP_pubkeys_data) < 0 ) printf("error LP_sendwait pubkeys\n"); - else if ( LP_sendwait("choosei",10,utxo->pair,swap,data,maxlen,LP_choosei_verify,LP_choosei_data) < 0 ) + else if ( LP_sendwait("choosei",10,qp->pair,swap,data,maxlen,LP_choosei_verify,LP_choosei_data) < 0 ) printf("error LP_sendwait choosei\n"); - else if ( LP_sendwait("mostprivs",10,utxo->pair,swap,data,maxlen,LP_mostprivs_verify,LP_mostprivs_data) < 0 ) + else if ( LP_sendwait("mostprivs",10,qp->pair,swap,data,maxlen,LP_mostprivs_verify,LP_mostprivs_data) < 0 ) printf("error LP_sendwait mostprivs\n"); else if ( basilisk_alicetxs(swap,data,maxlen) != 0 ) printf("basilisk_alicetxs error\n"); @@ -558,10 +557,9 @@ void LP_aliceloop(void *_utxo) } } else printf("swap timed out\n"); basilisk_swap_finished(swap); - free(utxo->swap); - utxo->swap = 0; - nn_close(utxo->pair); - utxo->pair = -1; + free(swap); + nn_close(qp->pair); + free(qp); } #ifdef old From fc1c078bb7da07d3cb9c7e23cd20722eaf3781f8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 10:53:26 +0300 Subject: [PATCH 165/339] Test --- iguana/exchanges/LP_commands.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index dc0b68a5c..611bf9aab 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -327,6 +327,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *myutxo,char *base) i = besti; Q[i].desttxid = myutxo->txid; Q[i].destvout = myutxo->vout; + strcpy(Q[i].destaddr,myutxo->coinaddr); price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); @@ -335,6 +336,7 @@ cJSON *LP_bestprice(struct LP_utxoinfo *myutxo,char *base) { Q[i].desttxid = myutxo->txid; Q[i].destvout = myutxo->vout; + strcpy(Q[i].destaddr,myutxo->coinaddr); price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); LP_requestinit(&R,Q[i].srchash,Q[i].desthash,base,Q[i].satoshis,Q[i].destcoin,Q[i].destsatoshis,Q[i].timestamp,Q[i].quotetime,DEXselector); printf("Alice r.%u q.%u\n",R.requestid,R.quoteid); From d3ad3e546fd05566f926f7950fdef4bfffa7eefa Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 10:55:25 +0300 Subject: [PATCH 166/339] Test --- iguana/exchanges/LP_swap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index d70b78954..de851683d 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -534,7 +534,7 @@ void LP_aliceloop(void *_qp) maxlen = 1024*1024 + sizeof(*swap); data = malloc(maxlen); expiration = (uint32_t)time(NULL) + 10; - swap = LP_swapinit(1,0,qp->privkey,&qp->R); + swap = LP_swapinit(0,0,qp->privkey,&qp->R); if ( swap != 0 ) { if ( LP_sendwait("pubkeys",10,qp->pair,swap,data,maxlen,LP_pubkeys_verify,LP_pubkeys_data) < 0 ) @@ -555,9 +555,9 @@ void LP_aliceloop(void *_qp) // alicespend // done } - } else printf("swap timed out\n"); - basilisk_swap_finished(swap); - free(swap); + basilisk_swap_finished(swap); + free(swap); + } nn_close(qp->pair); free(qp); } From ab8c9158afb57f3ed4e90031947b8b2177647405 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 10:56:40 +0300 Subject: [PATCH 167/339] Test --- iguana/exchanges/LP_swap.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index de851683d..618597b29 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -443,11 +443,13 @@ int32_t LP_waitsend(char *statename,int32_t timeout,int32_t pairsock,struct basi int32_t datalen,sendlen,retval = -1; if ( LP_waitfor(pairsock,swap,timeout,verify) == 0 ) { + printf("waitsend waited\n"); if ( (datalen= (*datagen)(swap,data,maxlen)) > 0 ) { if ( (sendlen= nn_send(pairsock,data,datalen,0)) == datalen ) { retval = 0; + printf("sent success\n"); } else printf("send %s error\n",statename); } } else printf("didnt get pubkeys\n"); @@ -459,10 +461,14 @@ int32_t LP_sendwait(char *statename,int32_t timeout,int32_t pairsock,struct basi int32_t datalen,sendlen,retval = -1; if ( (datalen= (*datagen)(swap,data,maxlen)) > 0 ) { + printf("sendwait.%d\n",datalen); if ( (sendlen= nn_send(pairsock,data,datalen,0)) == datalen ) { if ( LP_waitfor(pairsock,swap,timeout,verify) == 0 ) + { retval = 0; + printf("waited success\n"); + } else printf("didnt get %s\n",statename); } else printf("send pubkeys error\n"); } From 2072e3a3b2a0eae2cbaadf63396aafe7ec1e4d4f Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:06:14 +0300 Subject: [PATCH 168/339] Test --- iguana/exchanges/LP_commands.c | 1 + iguana/exchanges/LP_swap.c | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 611bf9aab..2c5c4d65c 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -499,6 +499,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port qp->pair = pairsock; qp->privkey = LP_privkey(qp->destaddr); LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis,qp->destcoin,qp->destsatoshis,qp->timestamp,qp->quotetime,DEXselector); + printf("alice pairstr.(%s)\n",pairstr); if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_aliceloop,(void *)qp) == 0 ) { } diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 618597b29..707c2d3e1 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -441,6 +441,7 @@ int32_t LP_waitfor(int32_t pairsock,struct basilisk_swap *swap,int32_t timeout,i int32_t LP_waitsend(char *statename,int32_t timeout,int32_t pairsock,struct basilisk_swap *swap,uint8_t *data,int32_t maxlen,int32_t (*verify)(struct basilisk_swap *swap,uint8_t *data,int32_t datalen),int32_t (*datagen)(struct basilisk_swap *swap,uint8_t *data,int32_t maxlen)) { int32_t datalen,sendlen,retval = -1; + printf("wait on pairsock.%d\n",pairsock); if ( LP_waitfor(pairsock,swap,timeout,verify) == 0 ) { printf("waitsend waited\n"); @@ -525,9 +526,9 @@ void LP_bobloop(void *_utxo) // bobrefund // done } + basilisk_swap_finished(swap); + free(utxo->swap); } else printf("swap timed out\n"); - basilisk_swap_finished(swap); - free(utxo->swap); utxo->swap = 0; nn_close(utxo->pair); utxo->pair = -1; @@ -536,7 +537,7 @@ void LP_bobloop(void *_utxo) void LP_aliceloop(void *_qp) { uint8_t *data; int32_t maxlen; uint32_t expiration; struct basilisk_swap *swap = 0; struct LP_quoteinfo *qp = _qp; - fprintf(stderr,"start swap iamalice\n"); + fprintf(stderr,"start swap iamalice pair.%d\n",qp->pair); maxlen = 1024*1024 + sizeof(*swap); data = malloc(maxlen); expiration = (uint32_t)time(NULL) + 10; From f32f4c3868166e0a11d1316d81b68eb7fcd365cb Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:11:46 +0300 Subject: [PATCH 169/339] Test --- iguana/exchanges/LP_swap.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 707c2d3e1..2fab0961e 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -431,9 +431,10 @@ int32_t LP_waitfor(int32_t pairsock,struct basilisk_swap *swap,int32_t timeout,i { if ( (datalen= nn_recv(pairsock,&data,NN_MSG,0)) >= 0 ) { + printf("got %d bytes\n",datalen); retval = (*verify)(swap,data,datalen); nn_freemsg(data); - } + } else printf("error nn_recv\n"); } return(retval); } @@ -453,7 +454,7 @@ int32_t LP_waitsend(char *statename,int32_t timeout,int32_t pairsock,struct basi printf("sent success\n"); } else printf("send %s error\n",statename); } - } else printf("didnt get pubkeys\n"); + } else printf("didnt get data\n"); return(retval); } From 0c238430b5de46e71ae0666316dbb15826f3f453 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:14:20 +0300 Subject: [PATCH 170/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 2c5c4d65c..8e9fdb623 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -413,7 +413,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin nanomsg_tcpname(pairstr,mypeer->ipaddr,10000+(rand() % 10000)); if ( (utxo->pair= nn_socket(AF_SP,NN_PAIR)) < 0 ) printf("error creating utxo->pair\n"); - else if ( nn_connect(utxo->pair,pairstr) >= 0 ) + else if ( nn_bind(utxo->pair,pairstr) >= 0 ) { //char str[65]; printf("destsatoshis %.8f %s t%u\n",dstr(Q.destsatoshis),bits256_str(str,Q.desthash),Q.quotetime); LP_requestinit(&R,Q.srchash,Q.desthash,base,Q.satoshis,rel,Q.destsatoshis,Q.timestamp,Q.quotetime,DEXselector); From 204bb261615a3f1f83dac20c2ee9f49a54714508 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:20:07 +0300 Subject: [PATCH 171/339] Test --- iguana/exchanges/LP_swap.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 2fab0961e..1fcef1467 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -216,10 +216,14 @@ int32_t LP_pubkeys_verify(struct basilisk_swap *swap,uint8_t *data,int32_t datal int32_t i,len = 0; if ( datalen == sizeof(swap->otherdeck) ) { + printf("extract pubkeys\n"); for (i=0; iotherdeck)/sizeof(swap->otherdeck[0][0]); i++) len += iguana_rwnum(0,&data[len],sizeof(swap->otherdeck[i>>1][i&1]),&swap->otherdeck[i>>1][i&1]); + printf("done extract pubkeys\n"); return(0); - } else return(-1); + } + printf("pubkeys verify size mismatch %d != %d\n",datalen,(int32_t)sizeof(swap->otherdeck)); + return(-1); } int32_t LP_choosei_data(struct basilisk_swap *swap,uint8_t *data,int32_t maxlen) @@ -434,6 +438,7 @@ int32_t LP_waitfor(int32_t pairsock,struct basilisk_swap *swap,int32_t timeout,i printf("got %d bytes\n",datalen); retval = (*verify)(swap,data,datalen); nn_freemsg(data); + printf("retval.%d\n",retval); } else printf("error nn_recv\n"); } return(retval); @@ -454,7 +459,7 @@ int32_t LP_waitsend(char *statename,int32_t timeout,int32_t pairsock,struct basi printf("sent success\n"); } else printf("send %s error\n",statename); } - } else printf("didnt get data\n"); + } else printf("didnt get valid data\n"); return(retval); } From a1cf1dcf3a123052bff2c2c0b1490ab446da1eaf Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:22:03 +0300 Subject: [PATCH 172/339] Test --- iguana/exchanges/LP_swap.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 1fcef1467..32af56b5e 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -439,6 +439,7 @@ int32_t LP_waitfor(int32_t pairsock,struct basilisk_swap *swap,int32_t timeout,i retval = (*verify)(swap,data,datalen); nn_freemsg(data); printf("retval.%d\n",retval); + return(retval); } else printf("error nn_recv\n"); } return(retval); From cdd9cca9e47b71940774bdbe7bcd6a2b23a6dc4b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:25:33 +0300 Subject: [PATCH 173/339] Test --- iguana/exchanges/LP_swap.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 32af56b5e..18b7eaa4f 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -435,10 +435,8 @@ int32_t LP_waitfor(int32_t pairsock,struct basilisk_swap *swap,int32_t timeout,i { if ( (datalen= nn_recv(pairsock,&data,NN_MSG,0)) >= 0 ) { - printf("got %d bytes\n",datalen); retval = (*verify)(swap,data,datalen); nn_freemsg(data); - printf("retval.%d\n",retval); return(retval); } else printf("error nn_recv\n"); } @@ -448,17 +446,13 @@ int32_t LP_waitfor(int32_t pairsock,struct basilisk_swap *swap,int32_t timeout,i int32_t LP_waitsend(char *statename,int32_t timeout,int32_t pairsock,struct basilisk_swap *swap,uint8_t *data,int32_t maxlen,int32_t (*verify)(struct basilisk_swap *swap,uint8_t *data,int32_t datalen),int32_t (*datagen)(struct basilisk_swap *swap,uint8_t *data,int32_t maxlen)) { int32_t datalen,sendlen,retval = -1; - printf("wait on pairsock.%d\n",pairsock); if ( LP_waitfor(pairsock,swap,timeout,verify) == 0 ) { - printf("waitsend waited\n"); if ( (datalen= (*datagen)(swap,data,maxlen)) > 0 ) { if ( (sendlen= nn_send(pairsock,data,datalen,0)) == datalen ) - { retval = 0; - printf("sent success\n"); - } else printf("send %s error\n",statename); + else printf("send %s error\n",statename); } } else printf("didnt get valid data\n"); return(retval); @@ -469,14 +463,10 @@ int32_t LP_sendwait(char *statename,int32_t timeout,int32_t pairsock,struct basi int32_t datalen,sendlen,retval = -1; if ( (datalen= (*datagen)(swap,data,maxlen)) > 0 ) { - printf("sendwait.%d\n",datalen); if ( (sendlen= nn_send(pairsock,data,datalen,0)) == datalen ) { if ( LP_waitfor(pairsock,swap,timeout,verify) == 0 ) - { retval = 0; - printf("waited success\n"); - } else printf("didnt get %s\n",statename); } else printf("send pubkeys error\n"); } From b3d19ef972f84733353b72315151e3763a1f44e4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:27:59 +0300 Subject: [PATCH 174/339] Test --- iguana/exchanges/LP_transaction.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 3a8d63120..99bb86bfb 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -766,7 +766,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch return(0); if ( (utxoobj= LP_gettxout(symbol,utxotxid,vout)) == 0 ) { - printf("basilisk_swap_bobtxspend.%s utxo already spent or doesnt exist\n",name); + printf("basilisk_swap_bobtxspend.%s %s utxo.(%s) already spent or doesnt exist\n",name,symbol,bits256_str(str,utxotxid)); return(0); } if ( (destamount= jdouble(utxoobj,"amount")*SATOSHIDEN) == 0 && (destamount= jdouble(utxoobj,"value")*SATOSHIDEN) == 0 ) From f996a6d4a79f8d8d53f7218ece9861fb6a5d2fca Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:33:27 +0300 Subject: [PATCH 175/339] Test --- iguana/exchanges/LP_commands.c | 2 +- iguana/exchanges/LP_include.h | 11 ++++++++++- iguana/exchanges/LP_nativeDEX.c | 9 --------- iguana/exchanges/LP_swap.c | 7 +++++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 8e9fdb623..9208a6006 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -426,7 +426,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin jaddnum(retjson,"quoteid",R.quoteid); retstr = jprint(retjson,1); LP_send(pubsock,retstr,1); - utxo->swap = LP_swapinit(1,0,privkey,&R); + utxo->swap = LP_swapinit(1,0,privkey,&R,&Q); } else { diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 35c38051a..a7ec599ed 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -199,6 +199,15 @@ struct basilisk_swap }; +struct LP_quoteinfo +{ + struct basilisk_request R; + bits256 srchash,desthash,txid,txid2,desttxid,privkey; + uint64_t satoshis,satoshis2,txfee,destsatoshis,desttxfee,change; + uint32_t timestamp,quotetime; int32_t vout,vout2,destvout,pair; + char srccoin[16],coinaddr[64],destcoin[16],destaddr[64]; +}; + static struct bitcoin_opcode { UT_hash_handle hh; uint8_t opcode,flags,stackitems; int8_t extralen; } *OPTABLE; static char *OPCODES[0x100]; static int32_t OPCODELENS[0x100]; #define SIGHASH_ALL 1 @@ -470,7 +479,7 @@ enum opcodetype void basilisk_dontforget_update(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx); uint32_t basilisk_requestid(struct basilisk_request *rp); uint32_t basilisk_quoteid(struct basilisk_request *rp); -struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256 privkey,struct basilisk_request *rp); +struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256 privkey,struct basilisk_request *rp,struct LP_quoteinfo *qp); char *bitcoind_passthru(char *coinstr,char *serverport,char *userpass,char *method,char *params); struct iguana_info *LP_coinfind(char *symbol); void *curl_post(void **cHandlep,char *url,char *userpass,char *postfields,char *hdr0,char *hdr1,char *hdr2,char *hdr3); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 29c81b18e..90e56063e 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -58,15 +58,6 @@ struct LP_utxoinfo uint16_t port; } *LP_utxoinfos; -struct LP_quoteinfo -{ - struct basilisk_request R; - bits256 srchash,desthash,txid,txid2,desttxid,privkey; - uint64_t satoshis,satoshis2,txfee,destsatoshis,desttxfee,change; - uint32_t timestamp,quotetime; int32_t vout,vout2,destvout,pair; - char srccoin[16],coinaddr[64],destcoin[16],destaddr[64]; -}; - struct LP_cacheinfo { UT_hash_handle hh; diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 18b7eaa4f..79df18dff 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -538,7 +538,7 @@ void LP_aliceloop(void *_qp) maxlen = 1024*1024 + sizeof(*swap); data = malloc(maxlen); expiration = (uint32_t)time(NULL) + 10; - swap = LP_swapinit(0,0,qp->privkey,&qp->R); + swap = LP_swapinit(0,0,qp->privkey,&qp->R,qp); if ( swap != 0 ) { if ( LP_sendwait("pubkeys",10,qp->pair,swap,data,maxlen,LP_pubkeys_verify,LP_pubkeys_data) < 0 ) @@ -977,7 +977,7 @@ struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256 return(swap); } -struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256 privkey,struct basilisk_request *rp) +struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256 privkey,struct basilisk_request *rp,struct LP_quoteinfo *qp) { struct basilisk_swap *swap; bits256 pubkey25519; uint8_t pubkey33[33]; swap = calloc(1,sizeof(*swap)); @@ -996,6 +996,9 @@ struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256 free(swap); swap = 0; } + swap->bobpayment.utxotxid = qp->txid, swap->bobpayment.utxovout = qp->vout; + swap->bobdeposit.utxotxid = qp->txid2, swap->bobdeposit.utxovout = qp->vout2; + swap->alicepayment.utxotxid = qp->desttxid, swap->alicepayment.utxovout = qp->destvout; return(swap); } From b6293004cd31315b49b7395cde059fc9fbd0f453 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:37:35 +0300 Subject: [PATCH 176/339] Test --- iguana/exchanges/LP_swap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 79df18dff..3837d1b7b 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -907,7 +907,7 @@ struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256 return(0); } if ( (coin= LP_coinfind(swap->I.req.dest)) != 0 ) - swap->bobcoin = *coin; + swap->alicecoin = *coin; else { printf("missing bobcoin.%p or missing alicecoin.%p src.%p dest.%p\n",&swap->bobcoin,&swap->alicecoin,LP_coinfind(swap->I.req.src),LP_coinfind(swap->I.req.dest)); @@ -915,7 +915,7 @@ struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256 return(0); } if ( (coin= LP_coinfind(swap->I.req.src)) != 0 ) - swap->alicecoin = *coin; + swap->bobcoin = *coin; else { printf("missing bobcoin.%p or missing alicecoin.%p src.%p dest.%p\n",&swap->bobcoin,&swap->alicecoin,LP_coinfind(swap->I.req.src),LP_coinfind(swap->I.req.dest)); From 25eee9e0b451cdf12d881e08ec1989114db0df8c Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:41:01 +0300 Subject: [PATCH 177/339] Test --- iguana/exchanges/LP_nativeDEX.c | 1 - 1 file changed, 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 90e56063e..4c8a8e28f 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -751,7 +751,6 @@ struct iguana_info *LP_coinfind(char *symbol) } else { - cdata.isPoS = 1; cdata.pubtype = 60; cdata.p2shtype = 85; cdata.wiftype = 188; From 9b7ba324ca6d531b920c7e732c099596dc479620 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:43:08 +0300 Subject: [PATCH 178/339] Test --- iguana/exchanges/LP_transaction.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 99bb86bfb..ac40faa22 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -621,10 +621,11 @@ cJSON *LP_createvins(struct basilisk_rawtx *dest,struct vin_info *V,struct basil jaddbits256(item,"txid",rawtx->I.actualtxid); else jaddbits256(item,"txid",rawtx->I.signedtxid); jaddnum(item,"vout",0); - sobj = cJSON_CreateObject(); + //sobj = cJSON_CreateObject(); init_hexbytes_noT(hexstr,rawtx->spendscript,rawtx->I.spendlen); - jaddstr(sobj,"hex",hexstr); - jadd(item,"scriptPubKey",sobj); + //jaddstr(sobj,"hex",hexstr); + //jadd(item,"scriptPubKey",sobj); + jaddstr(item,"scriptPubKey",hexstr); jaddnum(item,"suppress",dest->I.suppress_pubkeys); jaddnum(item,"sequence",sequenceid); if ( (dest->I.redeemlen= rawtx->I.redeemlen) != 0 ) From cb49fee8ae18982eaf1ad85b18d17c99ab289371 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:44:49 +0300 Subject: [PATCH 179/339] Test --- iguana/exchanges/LP_transaction.c | 7 ++++--- iguana/exchanges/mm.c | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index ac40faa22..cca131464 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -810,7 +810,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch } jaddbits256(item,"txid",utxotxid); jaddnum(item,"vout",vout); - sobj = cJSON_CreateObject(); + //sobj = cJSON_CreateObject(); bitcoin_address(destaddr,pubtype,pubkey33,33); bitcoin_addr2rmd160(&addrtype,rmd160,destaddr); /*int32_t i; @@ -826,8 +826,9 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch printf(" <- vs direct calc\n");*/ spendlen = bitcoin_standardspend(spendscript,0,rmd160); init_hexbytes_noT(hexstr,spendscript,spendlen); - jaddstr(sobj,"hex",hexstr); - jadd(item,"scriptPubKey",sobj); + //jaddstr(sobj,"hex",hexstr); + //jadd(item,"scriptPubKey",sobj); + jaddstr(item,"scriptPubKey",hexstr); jaddnum(item,"suppress",suppress_pubkeys); jaddnum(item,"sequence",sequenceid); if ( redeemlen != 0 ) diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 34312e13a..703f70dd7 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -833,7 +833,7 @@ int main(int argc, const char * argv[]) while ( 1 ) { theoretical = marketmaker_updateprice("komodo","KMD","BTC",theoretical,&incr); - sleep(30); + sleep(10); if ( jint(retjson,"client") != 0 ) { struct LP_utxoinfo *utxo,*utmp; @@ -843,6 +843,7 @@ int main(int argc, const char * argv[]) printf("bestprice (%s)\n",jprint(matchjson,1)); } } + sleep(1000); } profitmargin = jdouble(retjson,"profitmargin"); minask = jdouble(retjson,"minask"); From a69d5fc6f6c8aefed284ab6442fefc59bb48f7bc Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:46:48 +0300 Subject: [PATCH 180/339] Test --- iguana/exchanges/LP_rpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index d6a3dfeab..904fe7a2b 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -208,7 +208,7 @@ printf("%s signrawtransaction.(%s) params.(%s)\n",coin->symbol,retstr,paramstr); len = (int32_t)strlen(hexstr); signedtx = calloc(1,len+1); strcpy(signedtx,hexstr); - *completedp = jint(json,"completed"); + *completedp = is_cJSON_True(jobj(json,"complete")); data = malloc(len >> 1); decode_hex(data,len>>1,hexstr); *signedtxidp = bits256_doublesha256(0,data,len >> 1); From d4933ab98edebe76ab953125e7887e0e6a7431bf Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:56:17 +0300 Subject: [PATCH 181/339] Test --- iguana/exchanges/LP_rpc.c | 2 +- iguana/exchanges/LP_transaction.c | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 904fe7a2b..e36dc2b81 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -200,7 +200,7 @@ char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON * //printf("signrawtransaction\n"); if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"signrawtransaction",paramstr)) != 0 ) { -printf("%s signrawtransaction.(%s) params.(%s)\n",coin->symbol,retstr,paramstr); +//printf("%s signrawtransaction.(%s) params.(%s)\n",coin->symbol,retstr,paramstr); if ( (json= cJSON_Parse(retstr)) != 0 ) { if ( (hexstr= jstr(json,"hex")) != 0 ) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index cca131464..eddcd4118 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -757,7 +757,7 @@ int32_t _basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8 char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp) { - char *rawtxbytes=0,*signedtx=0,str[65],hexstr[999],wifstr[128],destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*sobj,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t destamount; + char *rawtxbytes=0,*signedtx=0,str[65],hexstr[999],wifstr[128],destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t destamount; *destamountp = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); if ( finalseqid == 0 ) @@ -844,10 +844,10 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch { //printf("locktime.%u sequenceid.%x rawtx.(%s) vins.(%s)\n",locktime,sequenceid,rawtxbytes,jprint(vins,0)); if ( (signedtx= LP_signrawtx(symbol,signedtxidp,&completed,vins,rawtxbytes,privkeys,V)) == 0 ) - printf("couldnt sign transaction\n"); + printf("couldnt sign transaction.%s\n",name); else if ( completed == 0 ) { - printf("incomplete signing\n"); + printf("incomplete signing %s\n",name); if ( signedtx != 0 ) free(signedtx), signedtx = 0; } else printf("%s -> %s\n",name,bits256_str(str,*signedtxidp)); @@ -1432,7 +1432,7 @@ int32_t basilisk_bobdeposit_refund(struct basilisk_swap *swap,int32_t delay) int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,int32_t genflag) { - int32_t i,j; //char str[65]; + int32_t j; //char str[65]; if ( genflag != 0 && swap->I.iambob == 0 ) printf("basilisk_bobscripts_set WARNING: alice generating BOB tx\n"); if ( depositflag == 0 ) @@ -1444,7 +1444,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i //printf(" <- bobpayment.%d\n",i); if ( genflag != 0 && bits256_nonz(*(bits256 *)swap->I.secretBn256) != 0 && swap->bobpayment.I.datalen == 0 ) { - for (i=0; i<3; i++) + //for (i=0; i<3; i++) { //if ( swap->bobpayment.txbytes != 0 && swap->bobpayment.I.spendlen != 0 ) // break; @@ -1477,7 +1477,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i bitcoin_address(swap->bobdeposit.p2shaddr,swap->bobcoin.p2shtype,swap->bobdeposit.redeemscript,swap->bobdeposit.I.redeemlen); if ( genflag != 0 && (swap->bobdeposit.I.datalen == 0 || swap->bobrefund.I.datalen == 0) ) { - for (i=0; i<3; i++) + //for (i=0; i<3; i++) { //if ( swap->bobdeposit.txbytes != 0 && swap->bobdeposit.I.spendlen != 0 ) // break; From 942688e9290d085d335c55e7358b75a40d195955 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 11:58:48 +0300 Subject: [PATCH 182/339] Test --- iguana/exchanges/LP_rpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index e36dc2b81..801954ceb 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -200,7 +200,6 @@ char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON * //printf("signrawtransaction\n"); if ( (retstr= bitcoind_passthru(symbol,coin->serverport,coin->userpass,"signrawtransaction",paramstr)) != 0 ) { -//printf("%s signrawtransaction.(%s) params.(%s)\n",coin->symbol,retstr,paramstr); if ( (json= cJSON_Parse(retstr)) != 0 ) { if ( (hexstr= jstr(json,"hex")) != 0 ) @@ -212,6 +211,7 @@ char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON * data = malloc(len >> 1); decode_hex(data,len>>1,hexstr); *signedtxidp = bits256_doublesha256(0,data,len >> 1); + printf("%s signrawtransaction.(%s) params.(%s) -> \nsignedtx.(%s)\n",coin->symbol,retstr,paramstr,signedtx); } free_json(json); } From d73a608326b47c8ad7260a3e86c2288409bfa60d Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 12:05:05 +0300 Subject: [PATCH 183/339] Test --- iguana/exchanges/LP_rpc.c | 3 +++ iguana/exchanges/LP_transaction.c | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 801954ceb..ee8bd29b2 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -191,7 +191,10 @@ char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON * memset(signedtxidp,0,sizeof(*signedtxidp)); *completedp = 0; if ( coin == 0 ) + { + printf("LP_signrawtx cant find coin.(%s)\n",symbol); return(0); + } array = cJSON_CreateArray(); jaddistr(array,rawtx); jaddi(array,jduplicate(vins)); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index eddcd4118..bad3640f6 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -842,9 +842,12 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch txobj = bitcoin_txoutput(txobj,spendscript,spendlen,destamount); if ( (rawtxbytes= bitcoin_json2hex(isPoS,&txid,txobj,V)) != 0 ) { + char str[65]; + completed = 0; + memset(signedtxidp,0,sizeof(*signedtxidp)); //printf("locktime.%u sequenceid.%x rawtx.(%s) vins.(%s)\n",locktime,sequenceid,rawtxbytes,jprint(vins,0)); if ( (signedtx= LP_signrawtx(symbol,signedtxidp,&completed,vins,rawtxbytes,privkeys,V)) == 0 ) - printf("couldnt sign transaction.%s\n",name); + printf("couldnt sign transaction.%s %s\n",name,bits256_str(str,*signedtxidp)); else if ( completed == 0 ) { printf("incomplete signing %s\n",name); From 2a0ba4b6762056a3eb5fd536be39ceedd0185676 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 12:08:35 +0300 Subject: [PATCH 184/339] Test --- iguana/exchanges/LP_rpc.c | 1 + iguana/exchanges/LP_transaction.c | 2 +- iguana/exchanges/mm.c | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index ee8bd29b2..c901bf1aa 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -221,5 +221,6 @@ char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON * free(retstr); } free(paramstr); + printf("return signedtx.%p\n",signedtx); return(signedtx); } diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index bad3640f6..f0a002ee7 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1500,7 +1500,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i //printf(" <- redeem.%d\n",swap->bobdeposit.redeemlen); //printf("GENERATED BOB DEPOSIT\n"); LP_unspents_mark(swap->bobcoin.symbol,swap->bobdeposit.vins); - basilisk_bobdeposit_refund(swap,swap->I.putduration); + //basilisk_bobdeposit_refund(swap,swap->I.putduration); printf("bobscripts set completed\n"); return(0); } diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 703f70dd7..ebcc50b5b 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -833,7 +833,7 @@ int main(int argc, const char * argv[]) while ( 1 ) { theoretical = marketmaker_updateprice("komodo","KMD","BTC",theoretical,&incr); - sleep(10); + sleep(3); if ( jint(retjson,"client") != 0 ) { struct LP_utxoinfo *utxo,*utmp; From 94ed5048cd1baad2c650d4d2e9f6b7634890b264 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 12:09:54 +0300 Subject: [PATCH 185/339] Test --- iguana/exchanges/mm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index ebcc50b5b..703f70dd7 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -833,7 +833,7 @@ int main(int argc, const char * argv[]) while ( 1 ) { theoretical = marketmaker_updateprice("komodo","KMD","BTC",theoretical,&incr); - sleep(3); + sleep(10); if ( jint(retjson,"client") != 0 ) { struct LP_utxoinfo *utxo,*utmp; From 29ee53b3b5be2ff95cf253df102664ed82e7fdd4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 12:21:02 +0300 Subject: [PATCH 186/339] Test --- iguana/exchanges/LP_commands.c | 11 +++++++++++ iguana/exchanges/LP_include.h | 4 ++-- iguana/exchanges/LP_rpc.c | 2 +- iguana/exchanges/LP_swap.c | 5 ++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 9208a6006..ca22231aa 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -79,6 +79,11 @@ cJSON *LP_quotejson(struct LP_quoteinfo *qp) jaddbits256(retjson,"desttxid",qp->desttxid); jaddnum(retjson,"destvout",qp->destvout); } + if ( bits256_nonz(qp->feetxid) != 0 ) + { + jaddbits256(retjson,"feetxid",qp->feetxid); + jaddnum(retjson,"feevout",qp->feevout); + } if ( qp->destsatoshis != 0 ) jadd64bits(retjson,"destsatoshis",qp->destsatoshis); if ( qp->desttxfee != 0 ) @@ -100,8 +105,10 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) qp->txid2 = jbits256(argjson,"txid2"); qp->vout = jint(argjson,"vout"); qp->vout2 = jint(argjson,"vout2"); + qp->feevout = jint(argjson,"feevout"); qp->srchash = jbits256(argjson,"srchash"); qp->desttxid = jbits256(argjson,"desttxid"); + qp->feetxid = jbits256(argjson,"feetxid"); qp->destvout = jint(argjson,"destvout"); qp->desthash = jbits256(argjson,"desthash"); if ( (qp->satoshis= j64bits(argjson,"satoshis")) == 0 ) @@ -327,6 +334,8 @@ cJSON *LP_bestprice(struct LP_utxoinfo *myutxo,char *base) i = besti; Q[i].desttxid = myutxo->txid; Q[i].destvout = myutxo->vout; + Q[i].feetxid = myutxo->txid2; + Q[i].feevout = myutxo->vout2; strcpy(Q[i].destaddr,myutxo->coinaddr); price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); if ( jobj(bestitem,"price") != 0 ) @@ -336,6 +345,8 @@ cJSON *LP_bestprice(struct LP_utxoinfo *myutxo,char *base) { Q[i].desttxid = myutxo->txid; Q[i].destvout = myutxo->vout; + Q[i].feetxid = myutxo->txid2; + Q[i].feevout = myutxo->vout2; strcpy(Q[i].destaddr,myutxo->coinaddr); price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); LP_requestinit(&R,Q[i].srchash,Q[i].desthash,base,Q[i].satoshis,Q[i].destcoin,Q[i].destsatoshis,Q[i].timestamp,Q[i].quotetime,DEXselector); diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index a7ec599ed..5ee18b1a7 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -202,9 +202,9 @@ struct basilisk_swap struct LP_quoteinfo { struct basilisk_request R; - bits256 srchash,desthash,txid,txid2,desttxid,privkey; + bits256 srchash,desthash,txid,txid2,desttxid,feetxid,privkey; uint64_t satoshis,satoshis2,txfee,destsatoshis,desttxfee,change; - uint32_t timestamp,quotetime; int32_t vout,vout2,destvout,pair; + uint32_t timestamp,quotetime; int32_t vout,vout2,destvout,feevout,pair; char srccoin[16],coinaddr[64],destcoin[16],destaddr[64]; }; diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index c901bf1aa..7000057d5 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -214,8 +214,8 @@ char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON * data = malloc(len >> 1); decode_hex(data,len>>1,hexstr); *signedtxidp = bits256_doublesha256(0,data,len >> 1); - printf("%s signrawtransaction.(%s) params.(%s) -> \nsignedtx.(%s)\n",coin->symbol,retstr,paramstr,signedtx); } + else printf("%s signrawtransaction.(%s) params.(%s)\n",coin->symbol,retstr,paramstr); free_json(json); } free(retstr); diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 3837d1b7b..92db527c8 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -410,7 +410,7 @@ int32_t LP_mostprivs_verify(struct basilisk_swap *swap,uint8_t *data,int32_t dat swap->I.secretAm[i] = data[len++]; for (i=0; i<32; i++) swap->I.secretAm256[i] = data[len++]; - basilisk_bobscripts_set(swap,1,1); + //basilisk_bobscripts_set(swap,1,1); } else { @@ -999,6 +999,9 @@ struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256 swap->bobpayment.utxotxid = qp->txid, swap->bobpayment.utxovout = qp->vout; swap->bobdeposit.utxotxid = qp->txid2, swap->bobdeposit.utxovout = qp->vout2; swap->alicepayment.utxotxid = qp->desttxid, swap->alicepayment.utxovout = qp->destvout; + if ( iambob != 0 ) + swap->otherfee.utxotxid = qp->feetxid, swap->otherfee.utxovout = qp->feevout; + else swap->myfee.utxotxid = qp->feetxid, swap->myfee.utxovout = qp->feevout; return(swap); } From c4f561430344c8818394e1ba57228a372bd871a4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 12:28:17 +0300 Subject: [PATCH 187/339] Test --- iguana/exchanges/LP_rpc.c | 1 - iguana/exchanges/LP_swap.c | 2 -- iguana/exchanges/LP_transaction.c | 6 +++--- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 7000057d5..d35568d2d 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -221,6 +221,5 @@ char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON * free(retstr); } free(paramstr); - printf("return signedtx.%p\n",signedtx); return(signedtx); } diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 92db527c8..0bf13129d 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -216,10 +216,8 @@ int32_t LP_pubkeys_verify(struct basilisk_swap *swap,uint8_t *data,int32_t datal int32_t i,len = 0; if ( datalen == sizeof(swap->otherdeck) ) { - printf("extract pubkeys\n"); for (i=0; iotherdeck)/sizeof(swap->otherdeck[0][0]); i++) len += iguana_rwnum(0,&data[len],sizeof(swap->otherdeck[i>>1][i&1]),&swap->otherdeck[i>>1][i&1]); - printf("done extract pubkeys\n"); return(0); } printf("pubkeys verify size mismatch %d != %d\n",datalen,(int32_t)sizeof(swap->otherdeck)); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index f0a002ee7..8b5e25291 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1715,15 +1715,15 @@ int32_t basilisk_alicetxs(struct basilisk_swap *swap,uint8_t *data,int32_t maxle } if ( swap->myfee.I.datalen == 0 ) { - printf("generate fee\n"); + //printf("generate fee\n"); if ( basilisk_rawtx_gen(swap->ctx,"myfee",swap->I.started,swap->persistent_pubkey33,swap->I.iambob,1,&swap->myfee,0,swap->myfee.spendscript,swap->myfee.I.spendlen,swap->myfee.coin->txfee,1,0,swap->persistent_privkey) == 0 ) { swap->I.statebits |= LP_swapdata_rawtxsend(swap,0x80,data,maxlen,&swap->myfee,0x40,0); LP_unspents_mark(swap->I.iambob!=0?swap->bobcoin.symbol:swap->alicecoin.symbol,swap->myfee.vins); //basilisk_txlog(swap,&swap->myfee,-1); - for (i=0; imyfee.I.spendlen; i++) + for (i=0; imyfee.I.datalen; i++) printf("%02x",swap->myfee.txbytes[i]); - printf(" fee %p %x\n",swap->myfee.txbytes,swap->I.statebits); + printf(" <- fee state.%x\n",swap->I.statebits); swap->I.statebits |= 0x40; } else From 0987f2de40f496f5e2efa296fcdc1933d7162b19 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 12:57:28 +0300 Subject: [PATCH 188/339] Test --- iguana/exchanges/LP_bitcoin.c | 2 +- iguana/exchanges/LP_include.h | 2 +- iguana/exchanges/LP_remember.c | 8 ++--- iguana/exchanges/LP_transaction.c | 55 +++++++++++++++++++++---------- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 319552e58..781a50930 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -1279,7 +1279,7 @@ cJSON *bitcoin_txoutput(cJSON *txobj,uint8_t *paymentscript,int32_t len,uint64_t jaddstr(skey,"hex",hexstr); //printf("addoutput.(%s %s)\n",hexstr,jprint(skey,0)); free(hexstr); - jadd(item,"scriptPubkey",skey); + jadd(item,"scriptPubKey",skey); jaddi(vouts,item); jadd(txobj,"vout",vouts); return(txobj); diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 5ee18b1a7..39567a5fe 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -195,7 +195,7 @@ struct basilisk_swap struct basilisk_swapmessage *messages; int32_t nummessages; char Bdeposit[64],Bpayment[64]; uint64_t otherdeck[INSTANTDEX_DECKSIZE][2],deck[INSTANTDEX_DECKSIZE][2]; - uint8_t persistent_pubkey33[33],pad[15],verifybuf[65536]; + uint8_t persistent_pubkey33[33],changermd160[20],pad[15],verifybuf[65536]; }; diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index d8951c9a5..efa8e8a59 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -748,7 +748,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); printf("alicespend len.%d redeemlen.%d\n",len,redeemlen); - if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND])) != 0 ) + if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0)) != 0 ) printf("alicespend.(%s)\n",txbytes[BASILISK_ALICESPEND]); } } @@ -773,7 +773,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( redeemlen > 0 ) { len = basilisk_swapuserdata(userdata,zero,1,myprivs[0],redeemscript,redeemlen); - if ( (txbytes[BASILISK_ALICECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"aliceclaim",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,pubkey33,0,expiration,&values[BASILISK_ALICECLAIM])) != 0 ) + if ( (txbytes[BASILISK_ALICECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"aliceclaim",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,pubkey33,0,expiration,&values[BASILISK_ALICECLAIM],0,0)) != 0 ) printf("privBn.(%s) aliceclaim.(%s)\n",bits256_str(str,privBn),txbytes[BASILISK_ALICECLAIM]); } } @@ -849,7 +849,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( redeemlen > 0 ) { len = basilisk_swapuserdata(userdata,zero,1,myprivs[1],redeemscript,redeemlen); - if ( (txbytes[BASILISK_BOBRECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[1],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,pubkey33,0,expiration,&values[BASILISK_BOBRECLAIM])) != 0 ) + if ( (txbytes[BASILISK_BOBRECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[1],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,pubkey33,0,expiration,&values[BASILISK_BOBRECLAIM],0,0)) != 0 ) { int32_t z; for (z=0; z<20; z++) @@ -879,7 +879,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti vcalc_sha256(0,secretBn256,privBn.bytes,sizeof(privBn)); redeemlen = basilisk_swap_bobredeemscript(1,&secretstart,redeemscript,dlocktime,pubA0,pubB0,pubB1,privAm,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,privBn,0,myprivs[0],redeemscript,redeemlen); - if ( (txbytes[BASILISK_BOBREFUND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,pubkey33,1,expiration,&values[BASILISK_BOBREFUND])) != 0 ) + if ( (txbytes[BASILISK_BOBREFUND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,pubkey33,1,expiration,&values[BASILISK_BOBREFUND],0,0)) != 0 ) printf("pubB1.(%s) bobrefund.(%s)\n",bits256_str(str,pubB1),txbytes[BASILISK_BOBREFUND]); } if ( txbytes[BASILISK_BOBREFUND] != 0 ) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 8b5e25291..f4f3ca59b 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -755,9 +755,9 @@ int32_t _basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8 } #endif -char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp) +char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr) { - char *rawtxbytes=0,*signedtx=0,str[65],hexstr[999],wifstr[128],destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t destamount; + char *rawtxbytes=0,*signedtx=0,str[65],hexstr[999],wifstr[128],destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t destamount,change = 0; *destamountp = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); if ( finalseqid == 0 ) @@ -777,6 +777,8 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch return(0); } else free_json(utxoobj); *destamountp = destamount; + if ( satoshis != 0 && destamount > satoshis+txfee ) + change = destamount - (satoshis - txfee); if ( destamount > txfee ) destamount -= txfee; timestamp = (uint32_t)time(NULL); @@ -840,6 +842,13 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch jdelete(txobj,"vin"); jadd(txobj,"vin",vins); txobj = bitcoin_txoutput(txobj,spendscript,spendlen,destamount); + if ( change != 0 ) + { + int32_t changelen; uint8_t changescript[1024],changetype,changermd160[20]; + bitcoin_addr2rmd160(&changetype,changermd160,changeaddr); + changelen = bitcoin_standardspend(changescript,0,changermd160); + txobj = bitcoin_txoutput(txobj,changescript,changelen,change); + } if ( (rawtxbytes= bitcoin_json2hex(isPoS,&txid,txobj,V)) != 0 ) { char str[65]; @@ -862,15 +871,20 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch return(signedtx); } -int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pubkey33,int32_t iambob,int32_t lockinputs,struct basilisk_rawtx *rawtx,uint32_t locktime,uint8_t *script,int32_t scriptlen,int64_t txfee,int32_t minconf,int32_t delay,bits256 privkey) +int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pubkey33,int32_t iambob,int32_t lockinputs,struct basilisk_rawtx *rawtx,uint32_t locktime,uint8_t *script,int32_t scriptlen,int64_t txfee,int32_t minconf,int32_t delay,bits256 privkey,uint8_t *changermd160) { - int32_t retval=-1,len,iter; char *signedtx; struct iguana_info *coin; int64_t newtxfee=0,destamount; + int32_t retval=-1,len,iter; char *signedtx,*changeaddr = 0,_changeaddr[64]; struct iguana_info *coin; int64_t newtxfee=0,destamount; if ( (coin= rawtx->coin) == 0 ) return(-1); //return(_basilisk_rawtx_gen(str,swapstarted,pubkey33,iambob,lockinputs,rawtx,locktime,script,scriptlen,txfee,minconf,delay,privkey)); + if ( changermd160 != 0 ) + { + changeaddr = _changeaddr; + bitcoin_address(changeaddr,coin->pubtype,changermd160,20); + } for (iter=0; iter<2; iter++) { - if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,str,coin->symbol,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,ctx,privkey,0,0,0,0,0,rawtx->utxotxid,rawtx->utxovout,pubkey33,1,0,&destamount)) != 0 ) + if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,str,coin->symbol,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,ctx,privkey,0,0,0,0,0,rawtx->utxotxid,rawtx->utxovout,pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr)) != 0 ) { rawtx->I.datalen = (int32_t)strlen(signedtx) >> 1; if ( rawtx->I.datalen <= sizeof(rawtx->txbytes) ) @@ -892,18 +906,23 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub return(retval); } -int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,struct basilisk_swap *swap,struct basilisk_rawtx *dest,struct basilisk_rawtx *rawtx,bits256 privkey,bits256 *privkey2,uint8_t *userdata,int32_t userdatalen,int32_t ignore_cltverr) +int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,struct basilisk_swap *swap,struct basilisk_rawtx *dest,struct basilisk_rawtx *rawtx,bits256 privkey,bits256 *privkey2,uint8_t *userdata,int32_t userdatalen,int32_t ignore_cltverr,uint8_t *changermd160) { - char *signedtx; int64_t txfee,newtxfee=0,destamount; uint32_t timestamp,locktime=0,sequenceid = 0xffffffff; int32_t iter,len,retval = -1; double estimatedrate; + char *signedtx,*changeaddr = 0,_changeaddr[64]; int64_t txfee,newtxfee=0,destamount; uint32_t timestamp,locktime=0,sequenceid = 0xffffffff; int32_t iter,len,retval = -1; double estimatedrate; timestamp = swap->I.started; if ( dest == &swap->aliceclaim ) locktime = swap->bobdeposit.I.locktime + 1, sequenceid = 0; else if ( dest == &swap->bobreclaim ) locktime = swap->bobpayment.I.locktime + 1, sequenceid = 0; txfee = strcmp("BTC",symbol) == 0 ? 0 : 10000; + if ( changermd160 != 0 ) + { + changeaddr = _changeaddr; + bitcoin_address(changeaddr,pubtype,changermd160,20); + } for (iter=0; iter<2; iter++) { - if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,rawtx->name,symbol,pubtype,p2shtype,isPoS,wiftype,swap->ctx,privkey,privkey2,0,0,userdata,userdatalen,rawtx->utxotxid,rawtx->utxovout,rawtx->pubkey33,1,0,&destamount)) != 0 ) + if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,rawtx->name,symbol,pubtype,p2shtype,isPoS,wiftype,swap->ctx,privkey,privkey2,0,0,userdata,userdatalen,rawtx->utxotxid,rawtx->utxovout,rawtx->pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr)) != 0 ) { rawtx->I.datalen = (int32_t)strlen(signedtx) >> 1; if ( rawtx->I.datalen <= sizeof(rawtx->txbytes) ) @@ -959,7 +978,7 @@ char *basilisk_swap_Aspend(char *name,char *symbol,uint8_t pubtype,uint8_t p2sht for (i=0; i<32; i++) privBn.bytes[i] = rev.bytes[31 - i];*/ txfee = LP_txfee(symbol); - signedtx = basilisk_swap_bobtxspend(&signedtxid,txfee,name,symbol,pubtype,p2shtype,isPoS,wiftype,ctx,privAm,&privBn,redeemscript,redeemlen,0,0,utxotxid,vout,pubkey33,1,expiration,destamountp); + signedtx = basilisk_swap_bobtxspend(&signedtxid,txfee,name,symbol,pubtype,p2shtype,isPoS,wiftype,ctx,privAm,&privBn,redeemscript,redeemlen,0,0,utxotxid,vout,pubkey33,1,expiration,destamountp,0,0); } return(signedtx); } @@ -1314,7 +1333,7 @@ int32_t basilisk_bobpayment_reclaim(struct basilisk_swap *swap,int32_t delay) len = basilisk_swapuserdata(userdata,zero,1,swap->I.myprivs[1],swap->bobpayment.redeemscript,swap->bobpayment.I.redeemlen); memcpy(swap->I.userdata_bobreclaim,userdata,len); swap->I.userdata_bobreclaimlen = len; - if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->bobreclaim,&swap->bobpayment,swap->I.myprivs[1],0,userdata,len,1)) == 0 ) + if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->bobreclaim,&swap->bobpayment,swap->I.myprivs[1],0,userdata,len,1,swap->changermd160)) == 0 ) { for (i=0; ibobreclaim.I.datalen; i++) printf("%02x",swap->bobreclaim.txbytes[i]); @@ -1400,7 +1419,7 @@ int32_t basilisk_verify_bobpaid(void *ptr,uint8_t *data,int32_t datalen) memcpy(swap->I.userdata_alicespend,userdata,len); swap->I.userdata_alicespendlen = len; char str[65],str2[65]; printf("bobpaid privAm.(%s) myprivs[0].(%s)\n",bits256_str(str,swap->I.privAm),bits256_str(str2,swap->I.myprivs[0])); - if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->alicespend,&swap->bobpayment,swap->I.myprivs[0],0,userdata,len,1)) == 0 ) + if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->alicespend,&swap->bobpayment,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) { for (i=0; ibobpayment.I.datalen; i++) printf("%02x",swap->bobpayment.txbytes[i]); @@ -1422,7 +1441,7 @@ int32_t basilisk_bobdeposit_refund(struct basilisk_swap *swap,int32_t delay) len = basilisk_swapuserdata(userdata,swap->I.privBn,0,swap->I.myprivs[0],swap->bobdeposit.redeemscript,swap->bobdeposit.I.redeemlen); memcpy(swap->I.userdata_bobrefund,userdata,len); swap->I.userdata_bobrefundlen = len; - if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->bobrefund,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,0)) == 0 ) + if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->bobrefund,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,0,swap->changermd160)) == 0 ) { for (i=0; ibobrefund.I.datalen; i++) printf("%02x",swap->bobrefund.txbytes[i]); @@ -1451,7 +1470,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i { //if ( swap->bobpayment.txbytes != 0 && swap->bobpayment.I.spendlen != 0 ) // break; - basilisk_rawtx_gen(swap->ctx,"payment",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobpayment,swap->bobpayment.I.locktime,swap->bobpayment.spendscript,swap->bobpayment.I.spendlen,swap->bobpayment.coin->txfee,1,0,swap->persistent_privkey); + basilisk_rawtx_gen(swap->ctx,"payment",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobpayment,swap->bobpayment.I.locktime,swap->bobpayment.spendscript,swap->bobpayment.I.spendlen,swap->bobpayment.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160); if ( swap->bobpayment.I.spendlen == 0 || swap->bobpayment.I.datalen == 0 ) { printf("error bob generating %p payment.%d\n",swap->bobpayment.txbytes,swap->bobpayment.I.spendlen); @@ -1484,7 +1503,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i { //if ( swap->bobdeposit.txbytes != 0 && swap->bobdeposit.I.spendlen != 0 ) // break; - basilisk_rawtx_gen(swap->ctx,"deposit",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobdeposit,swap->bobdeposit.I.locktime,swap->bobdeposit.spendscript,swap->bobdeposit.I.spendlen,swap->bobdeposit.coin->txfee,1,0,swap->persistent_privkey); + basilisk_rawtx_gen(swap->ctx,"deposit",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobdeposit,swap->bobdeposit.I.locktime,swap->bobdeposit.spendscript,swap->bobdeposit.I.spendlen,swap->bobdeposit.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160); if ( swap->bobdeposit.I.datalen == 0 || swap->bobdeposit.I.spendlen == 0 ) { printf("error bob generating %p deposit.%d\n",swap->bobdeposit.txbytes,swap->bobdeposit.I.spendlen); @@ -1624,7 +1643,7 @@ int32_t basilisk_verify_bobdeposit(void *ptr,uint8_t *data,int32_t datalen) len = basilisk_swapuserdata(userdata,zero,1,swap->I.myprivs[0],swap->bobdeposit.redeemscript,swap->bobdeposit.I.redeemlen); memcpy(swap->I.userdata_aliceclaim,userdata,len); swap->I.userdata_aliceclaimlen = len; - if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->aliceclaim,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,1)) == 0 ) + if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->aliceclaim,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) { for (i=0; ibobdeposit.I.datalen; i++) printf("%02x",swap->bobdeposit.txbytes[i]); @@ -1643,7 +1662,7 @@ int32_t basilisk_verify_bobdeposit(void *ptr,uint8_t *data,int32_t datalen) void basilisk_alicepayment(struct basilisk_swap *swap,struct iguana_info *coin,struct basilisk_rawtx *alicepayment,bits256 pubAm,bits256 pubBn) { alicepayment->I.spendlen = basilisk_alicescript(alicepayment->redeemscript,&alicepayment->I.redeemlen,alicepayment->spendscript,0,alicepayment->I.destaddr,coin->p2shtype,pubAm,pubBn); - basilisk_rawtx_gen(swap->ctx,"alicepayment",swap->I.started,swap->persistent_pubkey33,0,1,alicepayment,alicepayment->I.locktime,alicepayment->spendscript,alicepayment->I.spendlen,coin->txfee,1,0,swap->persistent_privkey); + basilisk_rawtx_gen(swap->ctx,"alicepayment",swap->I.started,swap->persistent_pubkey33,0,1,alicepayment,alicepayment->I.locktime,alicepayment->spendscript,alicepayment->I.spendlen,coin->txfee,1,0,swap->persistent_privkey,swap->changermd160); } int32_t basilisk_alicepayment_spend(struct basilisk_swap *swap,struct basilisk_rawtx *dest) @@ -1662,7 +1681,7 @@ int32_t basilisk_alicepayment_spend(struct basilisk_swap *swap,struct basilisk_r memcpy(swap->I.userdata_bobspend,swap->alicepayment.redeemscript,swap->alicepayment.I.spendlen); swap->I.userdata_bobspendlen = swap->alicepayment.I.spendlen; } - if ( (retval= basilisk_rawtx_sign(swap->alicecoin.symbol,swap->alicecoin.pubtype,swap->alicecoin.p2shtype,swap->alicecoin.isPoS,swap->alicecoin.wiftype,swap,dest,&swap->alicepayment,swap->I.privAm,&swap->I.privBn,0,0,1)) == 0 ) + if ( (retval= basilisk_rawtx_sign(swap->alicecoin.symbol,swap->alicecoin.pubtype,swap->alicecoin.p2shtype,swap->alicecoin.isPoS,swap->alicecoin.wiftype,swap,dest,&swap->alicepayment,swap->I.privAm,&swap->I.privBn,0,0,1,swap->changermd160)) == 0 ) { for (i=0; iI.datalen; i++) printf("%02x",dest->txbytes[i]); @@ -1716,7 +1735,7 @@ int32_t basilisk_alicetxs(struct basilisk_swap *swap,uint8_t *data,int32_t maxle if ( swap->myfee.I.datalen == 0 ) { //printf("generate fee\n"); - if ( basilisk_rawtx_gen(swap->ctx,"myfee",swap->I.started,swap->persistent_pubkey33,swap->I.iambob,1,&swap->myfee,0,swap->myfee.spendscript,swap->myfee.I.spendlen,swap->myfee.coin->txfee,1,0,swap->persistent_privkey) == 0 ) + if ( basilisk_rawtx_gen(swap->ctx,"myfee",swap->I.started,swap->persistent_pubkey33,swap->I.iambob,1,&swap->myfee,0,swap->myfee.spendscript,swap->myfee.I.spendlen,swap->myfee.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160) == 0 ) { swap->I.statebits |= LP_swapdata_rawtxsend(swap,0x80,data,maxlen,&swap->myfee,0x40,0); LP_unspents_mark(swap->I.iambob!=0?swap->bobcoin.symbol:swap->alicecoin.symbol,swap->myfee.vins); From 3a6220cd80bb92c6c6f8503389bb2367eb4d391b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 12:59:33 +0300 Subject: [PATCH 189/339] Test --- iguana/exchanges/LP_swap.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 0bf13129d..11b41a792 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -988,6 +988,7 @@ struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256 swap->persistent_pubkey = pubkey25519; swap->persistent_privkey = privkey; memcpy(swap->persistent_pubkey33,pubkey33,33); + calc_rmd160_sha256(swap->changermd160,pubkey33,33); if ( bitcoin_swapinit(privkey,pubkey33,pubkey25519,swap,optionduration,!iambob) == 0 ) { printf("error doing swapinit\n"); From 1d343f8bd36e1056658a5e78716b4cb7eef9e0e4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 13:08:28 +0300 Subject: [PATCH 190/339] Test --- iguana/exchanges/LP_swap.c | 149 ------------------------------ iguana/exchanges/LP_transaction.c | 13 ++- 2 files changed, 11 insertions(+), 151 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 11b41a792..2fffa34dd 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -1004,152 +1004,3 @@ struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256 return(swap); } -#ifdef notanymore -struct basilisk_swap *basilisk_thread_start(bits256 privkey,struct basilisk_request *rp,uint32_t statebits,int32_t optionduration,int32_t reinit) -{ - int32_t i,m,n,iter; uint8_t pubkey33[33]; bits256 pubkey25519; uint32_t channel,starttime; cJSON *retarray,*item,*msgobj; struct iguana_info *coin; double pending=0.; struct basilisk_swap *swap = 0; - // statebits 1 -> client, 0 -> LP - /*if ( myinfo->numswaps > 0 ) - { - if ( (coin= LP_coinfind(rp->src)) == 0 || coin->FULLNODE >= 0 ) - { - printf("dont have SRC coin.%s or not native and already swap pending\n",rp->src); - return(0); - } - if ( (coin= LP_coinfind(rp->dest)) == 0 || coin->FULLNODE >= 0 ) - { - printf("dont have DEST coin.%s or not native and already swap pending\n",rp->dest); - return(0); - } - } - portable_mutex_lock(&myinfo->DEX_swapmutex); - for (i=0; inumswaps; i++) - if ( myinfo->swaps[i]->I.req.requestid == rp->requestid ) - { - printf("basilisk_thread_start error trying to start requestid.%u which is already started\n",rp->requestid); - break; - } - if ( i == myinfo->numswaps && i < sizeof(myinfo->swaps)/sizeof(*myinfo->swaps) )*/ - { - swap = calloc(1,sizeof(*swap)); - swap->ctx = bitcoin_ctx(); - swap->subsock = swap->pushsock = -1; - vcalc_sha256(0,swap->I.orderhash.bytes,(uint8_t *)rp,sizeof(*rp)); - swap->I.req = *rp; - //swap->myinfoptr = myinfo; - printf("basilisk_thread_start request.%u statebits.%d (%s/%s) reinit.%d\n",rp->requestid,statebits,rp->src,rp->dest,reinit); - bitcoin_pubkey33(swap->ctx,pubkey33,privkey); - pubkey25519 = curve25519(privkey,curve25519_basepoint9()); - swap->persistent_pubkey = pubkey25519; - swap->persistent_privkey = privkey; - memcpy(swap->persistent_pubkey33,pubkey33,33); - m = n = 0; - if ( bitcoin_swapinit(privkey,pubkey33,pubkey25519,swap,optionduration,statebits,reinit) != 0 ) - { - for (iter=0; iter<16; iter++) - { - basilisk_psockinit(swap,statebits == 0); - sleep(3); - if ( swap->connected > 0 ) - break; - sleep(10); - /*basilisk_sendstate(swap,data,sizeof(data)); - basilisk_swapget(swap,0x80000000,data,sizeof(data),basilisk_verify_statebits); - if ( swap->connected > 0 ) - break; - printf("loopback didntwork with %d %d\n",swap->pushsock,swap->subsock);*/ - } - if ( reinit != 0 ) - { - if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)basilisk_swaploop,(void *)swap) != 0 ) - { - - } - //myinfo->swaps[myinfo->numswaps++] = swap; - } - else - { - starttime = (uint32_t)time(NULL); - printf("statebits.%x m.%d n.%d\n",statebits,m,n); - while ( statebits == 0 && m <= n/2 && time(NULL) < starttime+7*BASILISK_MSGDURATION ) - { - uint32_t msgid; uint8_t data[1024]; int32_t datalen; - m = n = 0; - sleep(DEX_SLEEP); - printf("waiting for offer to be accepted\n"); - channel = 'D' + ((uint32_t)'E' << 8) + ((uint32_t)'X' << 16); - datalen = basilisk_rwDEXquote(1,data,rp); - msgid = (uint32_t)time(NULL); - printf("other req.%d >>>>>>>>>>> send response (%llx -> %llx) r.%u quoteid.%u\n",i,(long long)rp->desthash.txid,(long long)rp->srchash.txid,rp->requestid,rp->quoteid); - LP_channelsend(rp->desthash,rp->srchash,channel,msgid,data,datalen); - if ( (retarray= basilisk_channelget(rp->srchash,rp->desthash,channel,0x4000000,30)) != 0 ) - { - if ( is_cJSON_Array(retarray) != 0 && (n= cJSON_GetArraySize(retarray)) > 0 ) - { - for (i=0; i 0 ) - { - item = jitem(msgobj,0); - if ( jobj(item,"data") != 0 && jobj(item,"key") != 0 ) - m++; - else printf("(%s)\n",jprint(item,0)); - } //else printf("msgobj.%p m.%d n.%d\n",msgobj,m,n); - } - } - } else printf("no retarray\n"); - } - printf("LAUNCH check.%d m.%d\n",statebits,m); - if ( statebits != 0 || m > 0 )//n/2 ) - { - //for (i=0; iI.req); i++) - // fprintf(stderr,"%02x",((uint8_t *)&swap->I.req)[i]); - if ( (swap->fp= basilisk_swap_save(swap,privkey,rp,statebits,optionduration,reinit)) != 0 ) - { - } - if ( reinit == 0 ) - { - static FILE *swapsfp; - if ( swapsfp == 0 ) - { - char fname[512]; - sprintf(fname,"%s/SWAPS/list",GLOBAL_DBDIR), OS_compatible_path(fname); - if ( (swapsfp= fopen(fname,"rb+")) == 0 ) - swapsfp = fopen(fname,"wb+"); - else fseek(swapsfp,0,SEEK_END); - printf("LIST fp.%p\n",swapsfp); - } - if ( swapsfp != 0 ) - { - fwrite(&rp->requestid,1,sizeof(rp->requestid),swapsfp); - fwrite(&rp->quoteid,1,sizeof(rp->quoteid),swapsfp); - fflush(swapsfp); - } - } - if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)basilisk_swaploop,(void *)swap) != 0 ) - { - - } - //myinfo->swaps[myinfo->numswaps++] = swap; - } - else - { - if ( statebits != 0 ) - { - if ( (coin= LP_coinfind(rp->src)) != 0 ) - { - - } - } - printf("%u/%u offer wasnt accepted statebits.%d m.%d n.%d pending %.8f\n",rp->requestid,rp->quoteid,statebits,m,n,pending); - } - } - } - } - //portable_mutex_unlock(&myinfo->DEX_swapmutex); - return(swap); -} -#endif - - diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index f4f3ca59b..8a27b4255 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -776,11 +776,17 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch free_json(utxoobj); return(0); } else free_json(utxoobj); + if ( satoshis != 0 ) + { + if ( destamount > satoshis+txfee ) + change = destamount - (satoshis - txfee); + printf("utxo %.8f, destamount %.8f change %.8f txfee %.8f\n",dstr(destamount),dstr(satoshis),dstr(change),dstr(txfee)); + destamount = satoshis; + } *destamountp = destamount; - if ( satoshis != 0 && destamount > satoshis+txfee ) - change = destamount - (satoshis - txfee); if ( destamount > txfee ) destamount -= txfee; + else printf("unexpected too small destamount %.8f txfee %.8f\n",dstr(destamount),dstr(txfee)); timestamp = (uint32_t)time(NULL); V = calloc(256,sizeof(*V)); privkeys = cJSON_CreateArray(); @@ -848,6 +854,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch bitcoin_addr2rmd160(&changetype,changermd160,changeaddr); changelen = bitcoin_standardspend(changescript,0,changermd160); txobj = bitcoin_txoutput(txobj,changescript,changelen,change); + printf("add change.(%s)\n",jprint(txobj,0)); } if ( (rawtxbytes= bitcoin_json2hex(isPoS,&txid,txobj,V)) != 0 ) { @@ -881,6 +888,7 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub { changeaddr = _changeaddr; bitcoin_address(changeaddr,coin->pubtype,changermd160,20); + printf("changeaddr.(%s)\n",changeaddr); } for (iter=0; iter<2; iter++) { @@ -919,6 +927,7 @@ int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_ { changeaddr = _changeaddr; bitcoin_address(changeaddr,pubtype,changermd160,20); + printf("changeaddr.(%s)\n",changeaddr); } for (iter=0; iter<2; iter++) { From 22447b12ed6caa84e9e85f4636062403f41efd4b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 13:13:06 +0300 Subject: [PATCH 191/339] Test --- iguana/exchanges/LP_swap.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 2fffa34dd..f107203d3 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -873,14 +873,14 @@ struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256 swap->I.putduration -= optionduration; else if ( optionduration > 0 ) swap->I.callduration += optionduration; - swap->I.bobsatoshis = swap->I.req.destamount; - swap->I.alicesatoshis = swap->I.req.srcamount; + swap->I.bobsatoshis = swap->I.req.srcamount; + swap->I.alicesatoshis = swap->I.req.destamount; if ( (swap->I.bobinsurance= (swap->I.bobsatoshis / INSTANTDEX_INSURANCEDIV)) < 50000 ) swap->I.bobinsurance = 50000; if ( (swap->I.aliceinsurance= (swap->I.alicesatoshis / INSTANTDEX_INSURANCEDIV)) < 50000 ) swap->I.aliceinsurance = 50000; - strcpy(swap->I.bobstr,swap->I.req.dest); - strcpy(swap->I.alicestr,swap->I.req.src); + strcpy(swap->I.bobstr,swap->I.req.src); + strcpy(swap->I.alicestr,swap->I.req.dest); swap->I.started = (uint32_t)time(NULL); swap->I.expiration = swap->I.req.timestamp + swap->I.putduration + swap->I.callduration; OS_randombytes((uint8_t *)&swap->I.choosei,sizeof(swap->I.choosei)); @@ -904,7 +904,7 @@ struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256 char str[65]; printf("couldnt generate privkeys %d %s\n",x,bits256_str(str,privkey)); return(0); } - if ( (coin= LP_coinfind(swap->I.req.dest)) != 0 ) + if ( (coin= LP_coinfind(swap->I.alicestr)) != 0 ) swap->alicecoin = *coin; else { @@ -912,7 +912,7 @@ struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256 free(swap); return(0); } - if ( (coin= LP_coinfind(swap->I.req.src)) != 0 ) + if ( (coin= LP_coinfind(swap->I.bobstr)) != 0 ) swap->bobcoin = *coin; else { From 6128ac5b4f79d446c9f0d4fc9054f194391bbc6c Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 13:15:29 +0300 Subject: [PATCH 192/339] Test --- iguana/exchanges/LP_include.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 39567a5fe..db85f0489 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -25,7 +25,7 @@ #define BASILISK_DISABLESENDTX #define LP_RESERVETIME 60 -#define LP_AVETXSIZE 500 +#define LP_AVETXSIZE 200 #define LP_CACHEDURATION 60 #define BASILISK_DEFAULT_NUMCONFIRMS 5 From cce067bc10123ea15e0bd93524acf8d2431a97e0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 13:23:33 +0300 Subject: [PATCH 193/339] Test --- iguana/exchanges/LP_nativeDEX.c | 3 +-- iguana/exchanges/LP_remember.c | 8 ++++---- iguana/exchanges/LP_swap.c | 8 ++++---- iguana/exchanges/LP_transaction.c | 17 ++++++++++------- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 4c8a8e28f..312ae6468 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -18,7 +18,6 @@ // marketmaker // // jl777: fix price calcs based on specific txfees -// jl777: add change output #include #include "LP_include.h" @@ -809,7 +808,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb depositval = values[i]; values[i] = 0, used++; if ( amclient != 0 ) - targetval = (depositval / 777); + targetval = (depositval / 776) + 50000; else targetval = (depositval / 9) * 8; //printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); if ( (i= LP_nearestvalue(values,n,targetval)) >= 0 ) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index efa8e8a59..d52a9838c 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -748,7 +748,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); printf("alicespend len.%d redeemlen.%d\n",len,redeemlen); - if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0)) != 0 ) + if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0)) != 0 ) printf("alicespend.(%s)\n",txbytes[BASILISK_ALICESPEND]); } } @@ -773,7 +773,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( redeemlen > 0 ) { len = basilisk_swapuserdata(userdata,zero,1,myprivs[0],redeemscript,redeemlen); - if ( (txbytes[BASILISK_ALICECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"aliceclaim",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,pubkey33,0,expiration,&values[BASILISK_ALICECLAIM],0,0)) != 0 ) + if ( (txbytes[BASILISK_ALICECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"aliceclaim",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,0,pubkey33,0,expiration,&values[BASILISK_ALICECLAIM],0,0)) != 0 ) printf("privBn.(%s) aliceclaim.(%s)\n",bits256_str(str,privBn),txbytes[BASILISK_ALICECLAIM]); } } @@ -849,7 +849,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( redeemlen > 0 ) { len = basilisk_swapuserdata(userdata,zero,1,myprivs[1],redeemscript,redeemlen); - if ( (txbytes[BASILISK_BOBRECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[1],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,pubkey33,0,expiration,&values[BASILISK_BOBRECLAIM],0,0)) != 0 ) + if ( (txbytes[BASILISK_BOBRECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[1],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,0,expiration,&values[BASILISK_BOBRECLAIM],0,0)) != 0 ) { int32_t z; for (z=0; z<20; z++) @@ -879,7 +879,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti vcalc_sha256(0,secretBn256,privBn.bytes,sizeof(privBn)); redeemlen = basilisk_swap_bobredeemscript(1,&secretstart,redeemscript,dlocktime,pubA0,pubB0,pubB1,privAm,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,privBn,0,myprivs[0],redeemscript,redeemlen); - if ( (txbytes[BASILISK_BOBREFUND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,pubkey33,1,expiration,&values[BASILISK_BOBREFUND],0,0)) != 0 ) + if ( (txbytes[BASILISK_BOBREFUND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,0,pubkey33,1,expiration,&values[BASILISK_BOBREFUND],0,0)) != 0 ) printf("pubB1.(%s) bobrefund.(%s)\n",bits256_str(str,pubB1),txbytes[BASILISK_BOBREFUND]); } if ( txbytes[BASILISK_BOBREFUND] != 0 ) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index f107203d3..a5b60731c 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -875,10 +875,10 @@ struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256 swap->I.callduration += optionduration; swap->I.bobsatoshis = swap->I.req.srcamount; swap->I.alicesatoshis = swap->I.req.destamount; - if ( (swap->I.bobinsurance= (swap->I.bobsatoshis / INSTANTDEX_INSURANCEDIV)) < 50000 ) - swap->I.bobinsurance = 50000; - if ( (swap->I.aliceinsurance= (swap->I.alicesatoshis / INSTANTDEX_INSURANCEDIV)) < 50000 ) - swap->I.aliceinsurance = 50000; + if ( (swap->I.bobinsurance= (swap->I.bobsatoshis / INSTANTDEX_INSURANCEDIV)) < 10000 ) + swap->I.bobinsurance = 10000; + if ( (swap->I.aliceinsurance= (swap->I.alicesatoshis / INSTANTDEX_INSURANCEDIV)) < 10000 ) + swap->I.aliceinsurance = 10000; strcpy(swap->I.bobstr,swap->I.req.src); strcpy(swap->I.alicestr,swap->I.req.dest); swap->I.started = (uint32_t)time(NULL); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 8a27b4255..11b1c96b6 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -755,9 +755,9 @@ int32_t _basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8 } #endif -char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr) +char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr) { - char *rawtxbytes=0,*signedtx=0,str[65],hexstr[999],wifstr[128],destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t destamount,change = 0; + char *rawtxbytes=0,*signedtx=0,str[65],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t destamount,change = 0; *destamountp = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); if ( finalseqid == 0 ) @@ -819,7 +819,11 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch jaddbits256(item,"txid",utxotxid); jaddnum(item,"vout",vout); //sobj = cJSON_CreateObject(); - bitcoin_address(destaddr,pubtype,pubkey33,33); + if ( destaddr == 0 ) + { + destaddr = _destaddr; + bitcoin_address(destaddr,pubtype,pubkey33,33); + } bitcoin_addr2rmd160(&addrtype,rmd160,destaddr); /*int32_t i; for (i=0; i<33; i++) @@ -854,7 +858,6 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch bitcoin_addr2rmd160(&changetype,changermd160,changeaddr); changelen = bitcoin_standardspend(changescript,0,changermd160); txobj = bitcoin_txoutput(txobj,changescript,changelen,change); - printf("add change.(%s)\n",jprint(txobj,0)); } if ( (rawtxbytes= bitcoin_json2hex(isPoS,&txid,txobj,V)) != 0 ) { @@ -892,7 +895,7 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub } for (iter=0; iter<2; iter++) { - if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,str,coin->symbol,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,ctx,privkey,0,0,0,0,0,rawtx->utxotxid,rawtx->utxovout,pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr)) != 0 ) + if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,str,coin->symbol,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,ctx,privkey,0,0,0,0,0,rawtx->utxotxid,rawtx->utxovout,rawtx->I.destaddr,pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr)) != 0 ) { rawtx->I.datalen = (int32_t)strlen(signedtx) >> 1; if ( rawtx->I.datalen <= sizeof(rawtx->txbytes) ) @@ -931,7 +934,7 @@ int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_ } for (iter=0; iter<2; iter++) { - if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,rawtx->name,symbol,pubtype,p2shtype,isPoS,wiftype,swap->ctx,privkey,privkey2,0,0,userdata,userdatalen,rawtx->utxotxid,rawtx->utxovout,rawtx->pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr)) != 0 ) + if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,rawtx->name,symbol,pubtype,p2shtype,isPoS,wiftype,swap->ctx,privkey,privkey2,0,0,userdata,userdatalen,rawtx->utxotxid,rawtx->utxovout,rawtx->I.destaddr,rawtx->pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr)) != 0 ) { rawtx->I.datalen = (int32_t)strlen(signedtx) >> 1; if ( rawtx->I.datalen <= sizeof(rawtx->txbytes) ) @@ -987,7 +990,7 @@ char *basilisk_swap_Aspend(char *name,char *symbol,uint8_t pubtype,uint8_t p2sht for (i=0; i<32; i++) privBn.bytes[i] = rev.bytes[31 - i];*/ txfee = LP_txfee(symbol); - signedtx = basilisk_swap_bobtxspend(&signedtxid,txfee,name,symbol,pubtype,p2shtype,isPoS,wiftype,ctx,privAm,&privBn,redeemscript,redeemlen,0,0,utxotxid,vout,pubkey33,1,expiration,destamountp,0,0); + signedtx = basilisk_swap_bobtxspend(&signedtxid,txfee,name,symbol,pubtype,p2shtype,isPoS,wiftype,ctx,privAm,&privBn,redeemscript,redeemlen,0,0,utxotxid,vout,0,pubkey33,1,expiration,destamountp,0,0); } return(signedtx); } From 4c4f0123d49ff4f0b65b97a66840a396285f0df0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 13:28:24 +0300 Subject: [PATCH 194/339] Test --- iguana/exchanges/LP_transaction.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 11b1c96b6..d386dffff 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -818,12 +818,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch } jaddbits256(item,"txid",utxotxid); jaddnum(item,"vout",vout); - //sobj = cJSON_CreateObject(); - if ( destaddr == 0 ) - { - destaddr = _destaddr; - bitcoin_address(destaddr,pubtype,pubkey33,33); - } + bitcoin_address(destaddr,pubtype,pubkey33,33); bitcoin_addr2rmd160(&addrtype,rmd160,destaddr); /*int32_t i; for (i=0; i<33; i++) @@ -838,6 +833,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch printf(" <- vs direct calc\n");*/ spendlen = bitcoin_standardspend(spendscript,0,rmd160); init_hexbytes_noT(hexstr,spendscript,spendlen); + //sobj = cJSON_CreateObject(); //jaddstr(sobj,"hex",hexstr); //jadd(item,"scriptPubKey",sobj); jaddstr(item,"scriptPubKey",hexstr); @@ -851,6 +847,12 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch jaddi(vins,item); jdelete(txobj,"vin"); jadd(txobj,"vin",vins); + if ( destaddr == 0 ) + { + destaddr = _destaddr; + bitcoin_address(destaddr,pubtype,pubkey33,33); + } + bitcoin_addr2rmd160(&addrtype,rmd160,destaddr); txobj = bitcoin_txoutput(txobj,spendscript,spendlen,destamount); if ( change != 0 ) { From 53433db1a9f8968158c3822b04ec158eb6ca51d7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 13:30:10 +0300 Subject: [PATCH 195/339] Test --- iguana/exchanges/LP_swap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index a5b60731c..23f4e8c7f 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -840,8 +840,8 @@ void basilisk_rawtx_setparms(char *name,uint32_t quoteid,struct basilisk_rawtx * rawtx->coin = coin; strcpy(rawtx->I.coinstr,coin->symbol); rawtx->I.numconfirms = numconfirms; - if ( (rawtx->I.amount= satoshis) < 50000 ) - rawtx->I.amount = 50000; + if ( (rawtx->I.amount= satoshis) < 10000 ) + rawtx->I.amount = 10000; rawtx->I.vintype = vintype; // 0 -> std, 2 -> 2of2, 3 -> spend bobpayment, 4 -> spend bobdeposit rawtx->I.vouttype = vouttype; // 0 -> fee, 1 -> std, 2 -> 2of2, 3 -> bobpayment, 4 -> bobdeposit if ( rawtx->I.vouttype == 0 ) From 2bf1bc43a50c5f349355ffd65108236f0ccdc0cf Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 13:38:39 +0300 Subject: [PATCH 196/339] Test --- iguana/exchanges/LP_transaction.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index d386dffff..04ae644ff 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -893,7 +893,7 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub { changeaddr = _changeaddr; bitcoin_address(changeaddr,coin->pubtype,changermd160,20); - printf("changeaddr.(%s)\n",changeaddr); + printf("changeaddr.(%s) vs destaddr.(%s)\n",changeaddr,rawtx->I.destaddr); } for (iter=0; iter<2; iter++) { @@ -936,7 +936,7 @@ int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_ } for (iter=0; iter<2; iter++) { - if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,rawtx->name,symbol,pubtype,p2shtype,isPoS,wiftype,swap->ctx,privkey,privkey2,0,0,userdata,userdatalen,rawtx->utxotxid,rawtx->utxovout,rawtx->I.destaddr,rawtx->pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr)) != 0 ) + if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,rawtx->name,symbol,pubtype,p2shtype,isPoS,wiftype,swap->ctx,privkey,privkey2,0,0,userdata,userdatalen,rawtx->utxotxid,rawtx->utxovout,dest->p2shaddr,rawtx->I.pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr)) != 0 ) { rawtx->I.datalen = (int32_t)strlen(signedtx) >> 1; if ( rawtx->I.datalen <= sizeof(rawtx->txbytes) ) From b727ce72befd45c8acf6a12e0692fb8160f8bb5e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 13:41:36 +0300 Subject: [PATCH 197/339] Test --- iguana/exchanges/LP_transaction.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 04ae644ff..65ab7ea53 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -757,7 +757,7 @@ int32_t _basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8 char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr) { - char *rawtxbytes=0,*signedtx=0,str[65],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t destamount,change = 0; + char *rawtxbytes=0,*signedtx=0,str[65],tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t destamount,change = 0; *destamountp = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); if ( finalseqid == 0 ) @@ -818,8 +818,8 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch } jaddbits256(item,"txid",utxotxid); jaddnum(item,"vout",vout); - bitcoin_address(destaddr,pubtype,pubkey33,33); - bitcoin_addr2rmd160(&addrtype,rmd160,destaddr); + bitcoin_address(tmpaddr,pubtype,pubkey33,33); + bitcoin_addr2rmd160(&addrtype,rmd160,tmpaddr); /*int32_t i; for (i=0; i<33; i++) printf("%02x",pubkey33[i]); From 39e812062f8c91092c2a80de548868db1fc331c9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 13:47:14 +0300 Subject: [PATCH 198/339] Test --- iguana/exchanges/LP_transaction.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 65ab7ea53..1340f11eb 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -757,7 +757,7 @@ int32_t _basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8 char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr) { - char *rawtxbytes=0,*signedtx=0,str[65],tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t destamount,change = 0; + char *rawtxbytes=0,*signedtx=0,str[65],tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t destamount,value,change = 0; *destamountp = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); if ( finalseqid == 0 ) @@ -770,7 +770,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch printf("basilisk_swap_bobtxspend.%s %s utxo.(%s) already spent or doesnt exist\n",name,symbol,bits256_str(str,utxotxid)); return(0); } - if ( (destamount= jdouble(utxoobj,"amount")*SATOSHIDEN) == 0 && (destamount= jdouble(utxoobj,"value")*SATOSHIDEN) == 0 ) + if ( (value= jdouble(utxoobj,"amount")*SATOSHIDEN) == 0 && (value= jdouble(utxoobj,"value")*SATOSHIDEN) == 0 ) { printf("%s %s basilisk_swap_bobtxspend.%s strange utxo.(%s)\n",symbol,bits256_str(str,utxotxid),name,jprint(utxoobj,0)); free_json(utxoobj); @@ -778,15 +778,13 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch } else free_json(utxoobj); if ( satoshis != 0 ) { - if ( destamount > satoshis+txfee ) - change = destamount - (satoshis - txfee); - printf("utxo %.8f, destamount %.8f change %.8f txfee %.8f\n",dstr(destamount),dstr(satoshis),dstr(change),dstr(txfee)); - destamount = satoshis; - } - *destamountp = destamount; - if ( destamount > txfee ) - destamount -= txfee; - else printf("unexpected too small destamount %.8f txfee %.8f\n",dstr(destamount),dstr(txfee)); + 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)); + } else if ( value > txfee ) + satoshis = value - txfee; + else printf("unexpected small value %.8f vs txfee %.8f\n",dstr(value),dstr(txfee)); + *destamountp = satoshis; timestamp = (uint32_t)time(NULL); V = calloc(256,sizeof(*V)); privkeys = cJSON_CreateArray(); @@ -853,7 +851,8 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch bitcoin_address(destaddr,pubtype,pubkey33,33); } bitcoin_addr2rmd160(&addrtype,rmd160,destaddr); - txobj = bitcoin_txoutput(txobj,spendscript,spendlen,destamount); + spendlen = bitcoin_standardspend(spendscript,0,rmd160); + txobj = bitcoin_txoutput(txobj,spendscript,spendlen,satoshis); if ( change != 0 ) { int32_t changelen; uint8_t changescript[1024],changetype,changermd160[20]; From 14f3da18f61b5176ad8000745efef45b88109e7b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 13:56:44 +0300 Subject: [PATCH 199/339] Test --- iguana/exchanges/LP_transaction.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 1340f11eb..637c14d82 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -757,7 +757,7 @@ int32_t _basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8 char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr) { - char *rawtxbytes=0,*signedtx=0,str[65],tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t destamount,value,change = 0; + char *rawtxbytes=0,*signedtx=0,str[65],tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value,change = 0; *destamountp = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); if ( finalseqid == 0 ) @@ -851,7 +851,9 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch bitcoin_address(destaddr,pubtype,pubkey33,33); } bitcoin_addr2rmd160(&addrtype,rmd160,destaddr); - spendlen = bitcoin_standardspend(spendscript,0,rmd160); + if ( addrtype == p2shtype ) + spendlen = bitcoin_p2shspend(spendscript,0,rmd160); + else spendlen = bitcoin_standardspend(spendscript,0,rmd160); txobj = bitcoin_txoutput(txobj,spendscript,spendlen,satoshis); if ( change != 0 ) { From d95040f938ec6a7fc3ef45650bffe2474cc90b78 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:00:15 +0300 Subject: [PATCH 200/339] Test --- iguana/exchanges/LP_swap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 23f4e8c7f..6a2f7ac89 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -269,7 +269,7 @@ int32_t LP_choosei_verify(struct basilisk_swap *swap,uint8_t *data,int32_t datal vcalc_sha256(0,swap->I.secretBn256,swap->I.privBn.bytes,sizeof(swap->I.privBn)); swap->I.pubBn = bitcoin_pubkey33(swap->ctx,pubkey33,swap->I.privBn); printf("set privBn.%s %s\n",bits256_str(str,swap->I.privBn),bits256_str(str2,*(bits256 *)swap->I.secretBn256)); - basilisk_bobscripts_set(swap,1,1); + //basilisk_bobscripts_set(swap,1,1); } else { @@ -408,7 +408,7 @@ int32_t LP_mostprivs_verify(struct basilisk_swap *swap,uint8_t *data,int32_t dat swap->I.secretAm[i] = data[len++]; for (i=0; i<32; i++) swap->I.secretAm256[i] = data[len++]; - //basilisk_bobscripts_set(swap,1,1); + basilisk_bobscripts_set(swap,1,1); } else { From 3411112b0a4821b5d1f8c62d4a03c71593b45dd8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:01:31 +0300 Subject: [PATCH 201/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 312ae6468..1a29d6e20 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -908,7 +908,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i } } if ( nonz == 0 ) - sleep(1); + usleep(100000); } } else From d1776cc5219c6e5c5b45c7ee216051911b058c45 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:04:13 +0300 Subject: [PATCH 202/339] Test --- iguana/exchanges/LP_transaction.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 637c14d82..4b9b46f4b 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1476,6 +1476,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i { swap->bobpayment.I.spendlen = basilisk_bobscript(swap->bobpayment.I.rmd160,swap->bobpayment.redeemscript,&swap->bobpayment.I.redeemlen,swap->bobpayment.spendscript,0,&swap->bobpayment.I.locktime,&swap->bobpayment.I.secretstart,&swap->I,0); bitcoin_address(swap->bobpayment.p2shaddr,swap->bobcoin.p2shtype,swap->bobpayment.redeemscript,swap->bobpayment.I.redeemlen); + strcpy(swap->bobpayment.I.destaddr,swap->bobpayment.p2shaddr); //for (i=0; ibobpayment.redeemlen; i++) // printf("%02x",swap->bobpayment.redeemscript[i]); //printf(" <- bobpayment.%d\n",i); @@ -1512,6 +1513,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i { swap->bobdeposit.I.spendlen = basilisk_bobscript(swap->bobdeposit.I.rmd160,swap->bobdeposit.redeemscript,&swap->bobdeposit.I.redeemlen,swap->bobdeposit.spendscript,0,&swap->bobdeposit.I.locktime,&swap->bobdeposit.I.secretstart,&swap->I,1); bitcoin_address(swap->bobdeposit.p2shaddr,swap->bobcoin.p2shtype,swap->bobdeposit.redeemscript,swap->bobdeposit.I.redeemlen); + strcpy(swap->bobdeposit.I.destaddr,swap->bobdeposit.p2shaddr); if ( genflag != 0 && (swap->bobdeposit.I.datalen == 0 || swap->bobrefund.I.datalen == 0) ) { //for (i=0; i<3; i++) From b9abdc45d2e056694bcdd74112206051ca538040 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:09:56 +0300 Subject: [PATCH 203/339] Test --- iguana/exchanges/LP_swap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 6a2f7ac89..3df11271e 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -373,8 +373,8 @@ int32_t basilisk_verify_privi(void *ptr,uint8_t *data,int32_t datalen) basilisk_dontforget_update(swap,0); char str[65]; printf("privi verified.(%s)\n",bits256_str(str,privkey)); return(0); - } - } + } else printf("pubpair doesnt verify privi\n"); + } else printf("verify privi size mismatch %d != %d\n",datalen,(int32_t)sizeof(bits256)); return(-1); } From 62dac5199f6882985823bd64c085af47eeb39c66 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:17:42 +0300 Subject: [PATCH 204/339] Test --- iguana/exchanges/LP_swap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 3df11271e..0eeb78ca2 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -408,7 +408,7 @@ int32_t LP_mostprivs_verify(struct basilisk_swap *swap,uint8_t *data,int32_t dat swap->I.secretAm[i] = data[len++]; for (i=0; i<32; i++) swap->I.secretAm256[i] = data[len++]; - basilisk_bobscripts_set(swap,1,1); + //basilisk_bobscripts_set(swap,1,1); } else { @@ -508,7 +508,7 @@ void LP_bobloop(void *_utxo) printf("error waitsend choosei\n"); else if ( LP_waitsend("mostprivs",10,utxo->pair,swap,data,maxlen,LP_mostprivs_verify,LP_mostprivs_data) < 0 ) printf("error waitsend mostprivs\n"); - else if ( basilisk_bobscripts_set(swap,1,1) < 0 ) + else if ( basilisk_bobscripts_set(swap,1,1) < 0 || basilisk_bobscripts_set(swap,0,1) < 0 ) printf("error bobscripts\n"); else { From 6d081009c99c5d57cd298abf098cb42c72251b36 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:21:01 +0300 Subject: [PATCH 205/339] Test --- iguana/exchanges/LP_transaction.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 4b9b46f4b..05133715a 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1502,8 +1502,8 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i //printf(" <- redeem.%d\n",swap->bobpayment.redeemlen); printf(" <- GENERATED BOB PAYMENT.%d\n",swap->bobpayment.I.datalen); LP_unspents_mark(swap->bobcoin.symbol,swap->bobpayment.vins); - basilisk_bobpayment_reclaim(swap,swap->I.callduration); - printf("bobscripts set completed\n"); + //basilisk_bobpayment_reclaim(swap,swap->I.callduration); + //printf("bobscripts set completed\n"); return(0); } } From bb81415e5876347e010e84af2447505568e8ae6e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:29:23 +0300 Subject: [PATCH 206/339] Test --- iguana/exchanges/LP_transaction.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 05133715a..aed68652c 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -64,7 +64,7 @@ bits256 LP_broadcast_tx(char *name,char *symbol,uint8_t *data,int32_t datalen) uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) { - uint64_t value = 0; cJSON *txobj,*vouts,*utxoobj; int32_t numvouts; + uint64_t value = 0; double interest; cJSON *txobj,*vouts,*utxoobj; int32_t numvouts; if ( (txobj= LP_gettx(symbol,txid)) != 0 ) { //char str[65]; printf("%s.(%s) txobj.(%s)\n",symbol,bits256_str(str,txid),jprint(txobj,0)); @@ -75,6 +75,11 @@ uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) { char str[65]; printf("%s LP_txvalue.%s strange utxo.(%s) vout.%d/%d\n",symbol,bits256_str(str,txid),jprint(utxoobj,0),vout,numvouts); } + else if ( (interest= jdouble(txobj,"interest")) != 0. ) + { + printf("add interest of %.8f to %.8f\n",interest,dstr(value)); + value += SATOSHIDEN * interest; + } } free_json(txobj); } @@ -779,7 +784,10 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch if ( satoshis != 0 ) { if ( value > satoshis+txfee ) - change = value - (satoshis - txfee); + { + if ( strcmp(symbol,"BTC") == 0 || value > satoshis+txfee*5 ) + change = value - (satoshis + 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; From 9be441cc169ce35c03346fe5c9f449b5f639fb9c Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:34:18 +0300 Subject: [PATCH 207/339] Test --- iguana/exchanges/LP_transaction.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index aed68652c..476e9b535 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -75,10 +75,18 @@ uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) { char str[65]; printf("%s LP_txvalue.%s strange utxo.(%s) vout.%d/%d\n",symbol,bits256_str(str,txid),jprint(utxoobj,0),vout,numvouts); } - else if ( (interest= jdouble(txobj,"interest")) != 0. ) + else if ( strcmp(symbol,"KMD") == 0 ) { - printf("add interest of %.8f to %.8f\n",interest,dstr(value)); - value += SATOSHIDEN * interest; + if ( (utxoobj= LP_gettxout(symbol,txid,vout)) != 0 ) + { + printf("gettxout.(%s)\n",jprint(utxoobj,0)); + if ( (interest= jdouble(txobj,"interest")) != 0. ) + { + printf("add interest of %.8f to %.8f\n",interest,dstr(value)); + value += SATOSHIDEN * interest; + } + free_json(utxoobj); + } } } free_json(txobj); From 6ff841c083338c19e03755c4e9431c96d33adebc Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:40:53 +0300 Subject: [PATCH 208/339] Test --- iguana/exchanges/LP_transaction.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 476e9b535..1d160e1d9 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -770,7 +770,7 @@ int32_t _basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8 char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr) { - char *rawtxbytes=0,*signedtx=0,str[65],tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *utxoobj,*txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value,change = 0; + 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,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value,change = 0; *destamountp = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); if ( finalseqid == 0 ) @@ -778,17 +778,11 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch //printf("bobtxspend.%s redeem.[%d]\n",symbol,redeemlen); if ( redeemlen < 0 ) return(0); - if ( (utxoobj= LP_gettxout(symbol,utxotxid,vout)) == 0 ) + if ( (value= LP_txvalue(symbol,utxotxid,vout)) == 0 ) { printf("basilisk_swap_bobtxspend.%s %s utxo.(%s) already spent or doesnt exist\n",name,symbol,bits256_str(str,utxotxid)); return(0); } - if ( (value= jdouble(utxoobj,"amount")*SATOSHIDEN) == 0 && (value= jdouble(utxoobj,"value")*SATOSHIDEN) == 0 ) - { - printf("%s %s basilisk_swap_bobtxspend.%s strange utxo.(%s)\n",symbol,bits256_str(str,utxotxid),name,jprint(utxoobj,0)); - free_json(utxoobj); - return(0); - } else free_json(utxoobj); if ( satoshis != 0 ) { if ( value > satoshis+txfee ) From 41fc7fbd11bdf1dfc2274bfbd52995f6b9a3a96c Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:43:21 +0300 Subject: [PATCH 209/339] Test --- iguana/exchanges/LP_transaction.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 1d160e1d9..3c4e21298 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -79,8 +79,7 @@ uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) { if ( (utxoobj= LP_gettxout(symbol,txid,vout)) != 0 ) { - printf("gettxout.(%s)\n",jprint(utxoobj,0)); - if ( (interest= jdouble(txobj,"interest")) != 0. ) + if ( (interest= jdouble(utxoobj,"interest")) != 0. ) { printf("add interest of %.8f to %.8f\n",interest,dstr(value)); value += SATOSHIDEN * interest; @@ -786,10 +785,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch if ( satoshis != 0 ) { if ( value > satoshis+txfee ) - { - if ( strcmp(symbol,"BTC") == 0 || value > satoshis+txfee*5 ) - change = 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)); } else if ( value > txfee ) satoshis = value - txfee; From 024e2a2c15d3362ad01596c38649d55675699526 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:46:20 +0300 Subject: [PATCH 210/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 1a29d6e20..e02ee5c12 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -578,7 +578,7 @@ void tradebot_pendingadd(cJSON *tradejson,char *base,double basevolume,char *rel // add to trades } -char GLOBAL_DBDIR[] = "."; +char GLOBAL_DBDIR[] = "DB"; #include "LP_secp.c" #include "LP_rpc.c" From 7c40aa18105afe34364a0ca8ab24d363989db2d3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 14:57:40 +0300 Subject: [PATCH 211/339] Test --- iguana/exchanges/mm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 703f70dd7..4fc22eda1 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -833,7 +833,8 @@ int main(int argc, const char * argv[]) while ( 1 ) { theoretical = marketmaker_updateprice("komodo","KMD","BTC",theoretical,&incr); - sleep(10); + sleep(3); + LP_privkey_updates(0,-1,passphrase,1); if ( jint(retjson,"client") != 0 ) { struct LP_utxoinfo *utxo,*utmp; From 47e03ce8d02c5e9521394b2d9d5013fd02881e8f Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 16:59:54 +0300 Subject: [PATCH 212/339] Test --- iguana/exchanges/LP_include.h | 1 + iguana/exchanges/LP_nativeDEX.c | 2 +- iguana/exchanges/LP_network.c | 10 +- iguana/exchanges/LP_remember.c | 2 +- iguana/exchanges/LP_statemachine.c | 189 +++++++++- iguana/exchanges/LP_swap.c | 104 +++++- iguana/exchanges/LP_transaction.c | 552 ++++++++--------------------- 7 files changed, 434 insertions(+), 426 deletions(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index db85f0489..77604a157 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -483,5 +483,6 @@ struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256 char *bitcoind_passthru(char *coinstr,char *serverport,char *userpass,char *method,char *params); struct iguana_info *LP_coinfind(char *symbol); void *curl_post(void **cHandlep,char *url,char *userpass,char *postfields,char *hdr0,char *hdr1,char *hdr2,char *hdr3); +uint32_t LP_swapdata_rawtxsend(int32_t pairsock,struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t maxlen,struct basilisk_rawtx *rawtx,uint32_t nextbits,int32_t suppress_swapsend); #endif diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index e02ee5c12..f651df270 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -585,7 +585,7 @@ char GLOBAL_DBDIR[] = "DB"; #include "LP_bitcoin.c" #include "LP_transaction.c" #include "LP_remember.c" -#include "LP_statemachine.c" +//#include "LP_statemachine.c" #include "LP_swap.c" #include "LP_commands.c" diff --git a/iguana/exchanges/LP_network.c b/iguana/exchanges/LP_network.c index bd6855e9d..45d25e403 100644 --- a/iguana/exchanges/LP_network.c +++ b/iguana/exchanges/LP_network.c @@ -144,7 +144,7 @@ int32_t LP_send(int32_t sock,char *msg,int32_t freeflag) return(-1); } -uint32_t LP_swapsend(struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t datalen,uint32_t nextbits,uint32_t crcs[2]) +uint32_t LP_swapsend(int32_t pairsock,struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t datalen,uint32_t nextbits,uint32_t crcs[2]) { uint8_t *buf; int32_t sentbytes,offset=0,i; buf = malloc(datalen + sizeof(msgbits) + sizeof(swap->I.req.quoteid) + sizeof(bits256)*2); @@ -156,17 +156,11 @@ uint32_t LP_swapsend(struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,i offset += iguana_rwnum(1,&buf[offset],sizeof(msgbits),&msgbits); if ( datalen > 0 ) memcpy(&buf[offset],data,datalen), offset += datalen; - if ( (sentbytes= nn_send(swap->pushsock,buf,offset,0)) != offset ) + if ( (sentbytes= nn_send(pairsock,buf,offset,0)) != offset ) { printf("sentbytes.%d vs offset.%d\n",sentbytes,offset); if ( sentbytes < 0 ) { - if ( swap->pushsock >= 0 ) - nn_close(swap->pushsock), swap->pushsock = -1; //, - if ( swap->subsock >= 0 ) // - nn_close(swap->subsock), swap->subsock = -1; - swap->connected = swap->I.iambob != 0 ? -1 : 0; - swap->aborted = (uint32_t)time(NULL); } } //else printf("send.[%d] %x offset.%d datalen.%d [%llx]\n",sentbytes,msgbits,offset,datalen,*(long long *)data); diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index d52a9838c..faea5b665 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -184,7 +184,7 @@ void basilisk_dontforget(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx fprintf(fp,"\",\"txid\":\"%s\"",bits256_str(str,bits256_doublesha256(0,rawtx->txbytes,rawtx->I.datalen))); if ( rawtx == &swap->bobdeposit || rawtx == &swap->bobpayment ) { - basilisk_swap_coinaddr(swap,&swap->bobcoin,coinaddr,rawtx->txbytes,rawtx->I.datalen); + LP_swap_coinaddr(swap,&swap->bobcoin,coinaddr,rawtx->txbytes,rawtx->I.datalen); if ( coinaddr[0] != 0 ) { LP_importaddress(swap->bobcoin.symbol,coinaddr); diff --git a/iguana/exchanges/LP_statemachine.c b/iguana/exchanges/LP_statemachine.c index 4cb663b99..950e22e88 100644 --- a/iguana/exchanges/LP_statemachine.c +++ b/iguana/exchanges/LP_statemachine.c @@ -17,6 +17,193 @@ // LP_statemachine.c // marketmaker // +int32_t basilisk_rawtx_return(struct basilisk_rawtx *rawtx,cJSON *item,int32_t lockinputs,struct vin_info *V) +{ + char *signedtx,*txbytes; cJSON *vins,*privkeyarray; int32_t i,n,retval = -1; + if ( (txbytes= jstr(item,"rawtx")) != 0 && (vins= jobj(item,"vins")) != 0 ) + { + privkeyarray = cJSON_CreateArray(); + jaddistr(privkeyarray,wifstr); + if ( (signedtx= LP_signrawtx(rawtx->coin->symbol,&rawtx->I.signedtxid,&rawtx->I.completed,vins,txbytes,privkeyarray,V)) != 0 ) + { + if ( lockinputs != 0 ) + { + //printf("lockinputs\n"); + LP_unspentslock(rawtx->coin->symbol,vins); + if ( (n= cJSON_GetArraySize(vins)) != 0 ) + { + bits256 txid; int32_t vout; + for (i=0; iI.datalen = (int32_t)strlen(signedtx) >> 1; + //rawtx->txbytes = calloc(1,rawtx->I.datalen); + decode_hex(rawtx->txbytes,rawtx->I.datalen,signedtx); + //printf("%s SIGNEDTX.(%s)\n",rawtx->name,signedtx); + free(signedtx); + retval = 0; + } else printf("error signrawtx\n"); //do a very short timeout so it finishes via local poll + free_json(privkeyarray); + } + return(retval); +} + +cJSON *LP_createvins(struct basilisk_rawtx *dest,struct vin_info *V,struct basilisk_rawtx *rawtx,uint8_t *userdata,int32_t userdatalen,uint32_t sequenceid) +{ + cJSON *vins,*item,*sobj; char hexstr[8192]; + vins = cJSON_CreateArray(); + item = cJSON_CreateObject(); + if ( userdata != 0 && userdatalen > 0 ) + { + memcpy(V[0].userdata,userdata,userdatalen); + V[0].userdatalen = userdatalen; + init_hexbytes_noT(hexstr,userdata,userdatalen); + jaddstr(item,"userdata",hexstr); +#ifdef DISABLE_CHECKSIG + needsig = 0; +#endif + } + //printf("rawtx B\n"); + if ( bits256_nonz(rawtx->I.actualtxid) != 0 ) + jaddbits256(item,"txid",rawtx->I.actualtxid); + else jaddbits256(item,"txid",rawtx->I.signedtxid); + jaddnum(item,"vout",0); + //sobj = cJSON_CreateObject(); + init_hexbytes_noT(hexstr,rawtx->spendscript,rawtx->I.spendlen); + //jaddstr(sobj,"hex",hexstr); + //jadd(item,"scriptPubKey",sobj); + jaddstr(item,"scriptPubKey",hexstr); + jaddnum(item,"suppress",dest->I.suppress_pubkeys); + jaddnum(item,"sequence",sequenceid); + if ( (dest->I.redeemlen= rawtx->I.redeemlen) != 0 ) + { + init_hexbytes_noT(hexstr,rawtx->redeemscript,rawtx->I.redeemlen); + memcpy(dest->redeemscript,rawtx->redeemscript,rawtx->I.redeemlen); + jaddstr(item,"redeemScript",hexstr); + } + jaddi(vins,item); + return(vins); +} + +int32_t _basilisk_rawtx_gen(char *str,uint32_t swapstarted,uint8_t *pubkey33,int32_t iambob,int32_t lockinputs,struct basilisk_rawtx *rawtx,uint32_t locktime,uint8_t *script,int32_t scriptlen,int64_t txfee,int32_t minconf,int32_t delay,bits256 privkey) +{ + char scriptstr[1024],wifstr[256],coinaddr[64],*signedtx,*rawtxbytes; uint32_t basilisktag; int32_t retval = -1; cJSON *vins,*privkeys,*addresses,*valsobj; struct vin_info *V; + //bitcoin_address(coinaddr,rawtx->coin->chain->pubtype,myinfo->persistent_pubkey33,33); + if ( rawtx->coin->changeaddr[0] == 0 ) + { + bitcoin_address(rawtx->coin->changeaddr,rawtx->coin->pubtype,pubkey33,33); + printf("set change address.(%s)\n",rawtx->coin->changeaddr); + } + init_hexbytes_noT(scriptstr,script,scriptlen); + basilisktag = (uint32_t)rand(); + valsobj = cJSON_CreateObject(); + jaddstr(valsobj,"coin",rawtx->coin->symbol); + jaddstr(valsobj,"spendscript",scriptstr); + jaddstr(valsobj,"changeaddr",rawtx->coin->changeaddr); + jadd64bits(valsobj,"satoshis",rawtx->I.amount); + if ( strcmp(rawtx->coin->symbol,"BTC") == 0 && txfee > 0 && txfee < 50000 ) + txfee = 50000; + jadd64bits(valsobj,"txfee",txfee); + jaddnum(valsobj,"minconf",minconf); + if ( locktime == 0 ) + locktime = (uint32_t)time(NULL) - 777; + jaddnum(valsobj,"locktime",locktime); + jaddnum(valsobj,"timeout",30000); + jaddnum(valsobj,"timestamp",swapstarted+delay); + addresses = cJSON_CreateArray(); + bitcoin_address(coinaddr,rawtx->coin->pubtype,pubkey33,33); + jaddistr(addresses,coinaddr); + jadd(valsobj,"addresses",addresses); + rawtx->I.locktime = locktime; + printf("%s locktime.%u\n",rawtx->name,locktime); + V = calloc(256,sizeof(*V)); + privkeys = cJSON_CreateArray(); + bitcoin_priv2wif(wifstr,privkey,rawtx->coin->wiftype); + jaddistr(privkeys,wifstr); + vins = LP_createvins(rawtx,V,rawtx,0,0,0xffffffff); + rawtx->vins = jduplicate(vins); + jdelete(valsobj,"vin"); + jadd(valsobj,"vin",vins); + if ( (rawtxbytes= bitcoin_json2hex(rawtx->coin->isPoS,&rawtx->I.txid,valsobj,V)) != 0 ) + { + //printf("rawtx.(%s) vins.%p\n",rawtxbytes,vins); + if ( (signedtx= LP_signrawtx(rawtx->coin->symbol,&rawtx->I.signedtxid,&rawtx->I.completed,vins,rawtxbytes,privkeys,V)) != 0 ) + { + rawtx->I.datalen = (int32_t)strlen(signedtx) >> 1; + if ( rawtx->I.datalen <= sizeof(rawtx->txbytes) ) + decode_hex(rawtx->txbytes,rawtx->I.datalen,signedtx); + else printf("DEX tx is too big %d vs %d\n",rawtx->I.datalen,(int32_t)sizeof(rawtx->txbytes)); + if ( signedtx != rawtxbytes ) + free(signedtx); + if ( rawtx->I.completed != 0 ) + retval = 0; + else printf("couldnt complete sign transaction %s\n",rawtx->name); + } else printf("error signing\n"); + free(rawtxbytes); + } else printf("error making rawtx\n"); + free_json(privkeys); + free_json(valsobj); + free(V); + return(retval); +} + +int32_t _basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,struct basilisk_swap *swap,uint32_t timestamp,uint32_t locktime,uint32_t sequenceid,struct basilisk_rawtx *dest,struct basilisk_rawtx *rawtx,bits256 privkey,bits256 *privkey2,uint8_t *userdata,int32_t userdatalen,int32_t ignore_cltverr) +{ + char *rawtxbytes=0,*signedtx=0,wifstr[128]; cJSON *txobj,*vins,*privkeys; int32_t needsig=1,retval = -1; struct vin_info *V; + V = calloc(256,sizeof(*V)); + V[0].signers[0].privkey = privkey; + bitcoin_pubkey33(swap->ctx,V[0].signers[0].pubkey,privkey); + privkeys = cJSON_CreateArray(); + bitcoin_priv2wif(wifstr,privkey,wiftype); + jaddistr(privkeys,wifstr); + if ( privkey2 != 0 ) + { + V[0].signers[1].privkey = *privkey2; + bitcoin_pubkey33(swap->ctx,V[0].signers[1].pubkey,*privkey2); + bitcoin_priv2wif(wifstr,*privkey2,wiftype); + jaddistr(privkeys,wifstr); + V[0].N = V[0].M = 2; + //char str[65]; printf("add second privkey.(%s) %s\n",jprint(privkeys,0),bits256_str(str,*privkey2)); + } else V[0].N = V[0].M = 1; + V[0].suppress_pubkeys = dest->I.suppress_pubkeys; + V[0].ignore_cltverr = ignore_cltverr; + if ( dest->I.redeemlen != 0 ) + memcpy(V[0].p2shscript,dest->redeemscript,dest->I.redeemlen), V[0].p2shlen = dest->I.redeemlen; + txobj = bitcoin_txcreate(symbol,isPoS,locktime,userdata == 0 ? 1 : 1,timestamp);//rawtx->coin->locktime_txversion); + vins = LP_createvins(dest,V,rawtx,userdata,userdatalen,sequenceid); + jdelete(txobj,"vin"); + jadd(txobj,"vin",vins); + //printf("basilisk_rawtx_sign locktime.%u/%u for %s spendscript.%s -> %s, suppress.%d\n",rawtx->I.locktime,dest->I.locktime,rawtx->name,hexstr,dest->name,dest->I.suppress_pubkeys); + txobj = bitcoin_txoutput(txobj,dest->spendscript,dest->I.spendlen,dest->I.amount); + if ( (rawtxbytes= bitcoin_json2hex(isPoS,&dest->I.txid,txobj,V)) != 0 ) + { + //printf("rawtx.(%s) vins.%p\n",rawtxbytes,vins); + if ( needsig == 0 ) + signedtx = rawtxbytes; + if ( signedtx != 0 || (signedtx= LP_signrawtx(symbol,&dest->I.signedtxid,&dest->I.completed,vins,rawtxbytes,privkeys,V)) != 0 ) + { + dest->I.datalen = (int32_t)strlen(signedtx) >> 1; + if ( dest->I.datalen <= sizeof(dest->txbytes) ) + decode_hex(dest->txbytes,dest->I.datalen,signedtx); + else printf("DEX tx is too big %d vs %d\n",dest->I.datalen,(int32_t)sizeof(dest->txbytes)); + if ( signedtx != rawtxbytes ) + free(signedtx); + if ( dest->I.completed != 0 ) + retval = 0; + else printf("couldnt complete sign transaction %s\n",rawtx->name); + } else printf("error signing\n"); + free(rawtxbytes); + } else printf("error making rawtx\n"); + free_json(privkeys); + free_json(txobj); + free(V); + return(retval); +} int32_t basilisk_process_swapverify(void *ptr,int32_t (*internal_func)(void *ptr,uint8_t *data,int32_t datalen),uint32_t channel,uint32_t msgid,uint8_t *data,int32_t datalen,uint32_t expiration,uint32_t duration) { @@ -26,8 +213,6 @@ int32_t basilisk_process_swapverify(void *ptr,int32_t (*internal_func)(void *ptr else return(0); } - - int32_t basilisk_priviextract(struct iguana_info *coin,char *name,bits256 *destp,uint8_t secret160[20],bits256 srctxid,int32_t srcvout) { /*bits256 txid; char str[65]; int32_t i,vini,scriptlen; uint8_t rmd160[20],scriptsig[IGUANA_MAXSCRIPTSIZE]; diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 0eeb78ca2..c066e3012 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -491,9 +491,62 @@ void LP_swapsfp_update(struct basilisk_request *rp) } } +struct basilisk_rawtx *LP_swapdata_rawtx(struct basilisk_swap *swap,uint8_t *data,int32_t maxlen,struct basilisk_rawtx *rawtx) +{ + if ( rawtx->I.datalen != 0 && rawtx->I.datalen <= maxlen ) + { + memcpy(data,rawtx->txbytes,rawtx->I.datalen); + return(rawtx); + } + return(0); +} + +uint32_t LP_swapdata_rawtxsend(int32_t pairsock,struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t maxlen,struct basilisk_rawtx *rawtx,uint32_t nextbits,int32_t suppress_swapsend) +{ + uint8_t sendbuf[32768]; int32_t sendlen; + if ( LP_swapdata_rawtx(swap,data,maxlen,rawtx) != 0 ) + { + if ( bits256_nonz(rawtx->I.signedtxid) != 0 && bits256_nonz(rawtx->I.actualtxid) == 0 ) + { + char str[65],str2[65]; + rawtx->I.actualtxid = LP_broadcast_tx(rawtx->name,rawtx->coin->symbol,rawtx->txbytes,rawtx->I.datalen); + if ( bits256_cmp(rawtx->I.actualtxid,rawtx->I.signedtxid) != 0 ) + { + printf("%s rawtxsend %s vs %s\n",rawtx->name,bits256_str(str,rawtx->I.signedtxid),bits256_str(str2,rawtx->I.actualtxid)); + rawtx->I.actualtxid = rawtx->I.signedtxid; + } + if ( bits256_nonz(rawtx->I.actualtxid) != 0 && msgbits != 0 ) + { + sendlen = 0; + sendbuf[sendlen++] = rawtx->I.datalen & 0xff; + sendbuf[sendlen++] = (rawtx->I.datalen >> 8) & 0xff; + sendbuf[sendlen++] = rawtx->I.redeemlen; + memcpy(&sendbuf[sendlen],rawtx->txbytes,rawtx->I.datalen), sendlen += rawtx->I.datalen; + if ( rawtx->I.redeemlen > 0 && rawtx->I.redeemlen < 0x100 ) + { + memcpy(&sendbuf[sendlen],rawtx->redeemscript,rawtx->I.redeemlen); + sendlen += rawtx->I.redeemlen; + } + basilisk_dontforget_update(swap,rawtx); + //printf("sendlen.%d datalen.%d redeemlen.%d\n",sendlen,rawtx->datalen,rawtx->redeemlen); + if ( suppress_swapsend == 0 ) + return(LP_swapsend(pairsock,swap,msgbits,sendbuf,sendlen,nextbits,rawtx->I.crcs)); + else + { + printf("suppress swapsend %x\n",msgbits); + return(0); + } + } + } + return(nextbits); + } else if ( swap->I.iambob == 0 ) + printf("error from basilisk_swapdata_rawtx.%s %p len.%d\n",rawtx->name,rawtx->txbytes,rawtx->I.datalen); + return(0); +} + void LP_bobloop(void *_utxo) { - uint8_t *data; int32_t maxlen; uint32_t expiration; struct basilisk_swap *swap; struct LP_utxoinfo *utxo = _utxo; + uint8_t *data; char *retstr; int32_t maxlen; uint32_t expiration; struct basilisk_swap *swap; struct LP_utxoinfo *utxo = _utxo; fprintf(stderr,"start swap iambob\n"); maxlen = 1024*1024 + sizeof(*swap); data = malloc(maxlen); @@ -513,13 +566,22 @@ void LP_bobloop(void *_utxo) else { LP_swapsfp_update(&swap->I.req); - // wait alicefee - // send bobdeposit - // wait alicepayment - // send bobpayment - // bobspend - // bobrefund - // done + if ( LP_waitfor(utxo->pair,swap,10,LP_verify_otherfee) < 0 ) + printf("error waiting for alicefee\n"); + else if ( LP_swapdata_rawtxsend(utxo->pair,swap,0x200,data,maxlen,&swap->bobdeposit,0x100,0) == 0 ) + printf("error sending bobdeposit\n"); + else if ( LP_waitfor(utxo->pair,swap,10,LP_verify_alicepayment) < 0 ) + printf("error waiting for alicepayment\n"); + else if ( LP_swapdata_rawtxsend(utxo->pair,swap,0x8000,data,maxlen,&swap->bobpayment,0x4000,0) == 0 ) + printf("error sending bobpayment\n"); + while ( 1 ) + { + if ( (retstr= basilisk_swaplist()) != 0 ) + { + printf("%s\n",retstr); + free(retstr); + } + } } basilisk_swap_finished(swap); free(utxo->swap); @@ -531,7 +593,7 @@ void LP_bobloop(void *_utxo) void LP_aliceloop(void *_qp) { - uint8_t *data; int32_t maxlen; uint32_t expiration; struct basilisk_swap *swap = 0; struct LP_quoteinfo *qp = _qp; + uint8_t *data; char *retstr; int32_t maxlen; uint32_t expiration; struct basilisk_swap *swap = 0; struct LP_quoteinfo *qp = _qp; fprintf(stderr,"start swap iamalice pair.%d\n",qp->pair); maxlen = 1024*1024 + sizeof(*swap); data = malloc(maxlen); @@ -545,17 +607,27 @@ void LP_aliceloop(void *_qp) printf("error LP_sendwait choosei\n"); else if ( LP_sendwait("mostprivs",10,qp->pair,swap,data,maxlen,LP_mostprivs_verify,LP_mostprivs_data) < 0 ) printf("error LP_sendwait mostprivs\n"); - else if ( basilisk_alicetxs(swap,data,maxlen) != 0 ) + else if ( basilisk_alicetxs(qp->pair,swap,data,maxlen) != 0 ) printf("basilisk_alicetxs error\n"); else { LP_swapsfp_update(&swap->I.req); - // send alicefee - // wait bobdeposit - // send alicepayment - // wait bobpayment - // alicespend - // done + if ( LP_swapdata_rawtxsend(qp->pair,swap,0x80,data,maxlen,&swap->myfee,0x40,0) == 0 ) + printf("error sending alicefee\n"); + else if ( LP_waitfor(qp->pair,swap,10,LP_verify_bobdeposit) < 0 ) + printf("error waiting for bobdeposit\n"); + else if ( LP_swapdata_rawtxsend(qp->pair,swap,0x1000,data,maxlen,&swap->alicepayment,0x800,0) == 0 ) + printf("error sending alicepayment\n"); + else if ( LP_waitfor(qp->pair,swap,10,LP_verify_bobpayment) < 0 ) + printf("error waiting for bobpayment\n"); + while ( 1 ) + { + if ( (retstr= basilisk_swaplist()) != 0 ) + { + printf("%s\n",retstr); + free(retstr); + } + } } basilisk_swap_finished(swap); free(swap); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 3c4e21298..29cda5305 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -577,194 +577,7 @@ int32_t iguana_signrawtransaction(uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS return(complete); } -int32_t basilisk_rawtx_return(struct basilisk_rawtx *rawtx,cJSON *item,int32_t lockinputs,struct vin_info *V) -{ - char *signedtx,*txbytes; cJSON *vins,*privkeyarray; int32_t i,n,retval = -1; - if ( (txbytes= jstr(item,"rawtx")) != 0 && (vins= jobj(item,"vins")) != 0 ) - { - privkeyarray = cJSON_CreateArray(); - jaddistr(privkeyarray,wifstr); - if ( (signedtx= LP_signrawtx(rawtx->coin->symbol,&rawtx->I.signedtxid,&rawtx->I.completed,vins,txbytes,privkeyarray,V)) != 0 ) - { - if ( lockinputs != 0 ) - { - //printf("lockinputs\n"); - LP_unspentslock(rawtx->coin->symbol,vins); - if ( (n= cJSON_GetArraySize(vins)) != 0 ) - { - bits256 txid; int32_t vout; - for (i=0; iI.datalen = (int32_t)strlen(signedtx) >> 1; - //rawtx->txbytes = calloc(1,rawtx->I.datalen); - decode_hex(rawtx->txbytes,rawtx->I.datalen,signedtx); - //printf("%s SIGNEDTX.(%s)\n",rawtx->name,signedtx); - free(signedtx); - retval = 0; - } else printf("error signrawtx\n"); //do a very short timeout so it finishes via local poll - free_json(privkeyarray); - } - return(retval); -} -cJSON *LP_createvins(struct basilisk_rawtx *dest,struct vin_info *V,struct basilisk_rawtx *rawtx,uint8_t *userdata,int32_t userdatalen,uint32_t sequenceid) -{ - cJSON *vins,*item,*sobj; char hexstr[8192]; - vins = cJSON_CreateArray(); - item = cJSON_CreateObject(); - if ( userdata != 0 && userdatalen > 0 ) - { - memcpy(V[0].userdata,userdata,userdatalen); - V[0].userdatalen = userdatalen; - init_hexbytes_noT(hexstr,userdata,userdatalen); - jaddstr(item,"userdata",hexstr); -#ifdef DISABLE_CHECKSIG - needsig = 0; -#endif - } - //printf("rawtx B\n"); - if ( bits256_nonz(rawtx->I.actualtxid) != 0 ) - jaddbits256(item,"txid",rawtx->I.actualtxid); - else jaddbits256(item,"txid",rawtx->I.signedtxid); - jaddnum(item,"vout",0); - //sobj = cJSON_CreateObject(); - init_hexbytes_noT(hexstr,rawtx->spendscript,rawtx->I.spendlen); - //jaddstr(sobj,"hex",hexstr); - //jadd(item,"scriptPubKey",sobj); - jaddstr(item,"scriptPubKey",hexstr); - jaddnum(item,"suppress",dest->I.suppress_pubkeys); - jaddnum(item,"sequence",sequenceid); - if ( (dest->I.redeemlen= rawtx->I.redeemlen) != 0 ) - { - init_hexbytes_noT(hexstr,rawtx->redeemscript,rawtx->I.redeemlen); - memcpy(dest->redeemscript,rawtx->redeemscript,rawtx->I.redeemlen); - jaddstr(item,"redeemScript",hexstr); - } - jaddi(vins,item); - return(vins); -} - -int32_t _basilisk_rawtx_gen(char *str,uint32_t swapstarted,uint8_t *pubkey33,int32_t iambob,int32_t lockinputs,struct basilisk_rawtx *rawtx,uint32_t locktime,uint8_t *script,int32_t scriptlen,int64_t txfee,int32_t minconf,int32_t delay,bits256 privkey) -{ - char scriptstr[1024],wifstr[256],coinaddr[64],*signedtx,*rawtxbytes; uint32_t basilisktag; int32_t retval = -1; cJSON *vins,*privkeys,*addresses,*valsobj; struct vin_info *V; - //bitcoin_address(coinaddr,rawtx->coin->chain->pubtype,myinfo->persistent_pubkey33,33); - if ( rawtx->coin->changeaddr[0] == 0 ) - { - bitcoin_address(rawtx->coin->changeaddr,rawtx->coin->pubtype,pubkey33,33); - printf("set change address.(%s)\n",rawtx->coin->changeaddr); - } - init_hexbytes_noT(scriptstr,script,scriptlen); - basilisktag = (uint32_t)rand(); - valsobj = cJSON_CreateObject(); - jaddstr(valsobj,"coin",rawtx->coin->symbol); - jaddstr(valsobj,"spendscript",scriptstr); - jaddstr(valsobj,"changeaddr",rawtx->coin->changeaddr); - jadd64bits(valsobj,"satoshis",rawtx->I.amount); - if ( strcmp(rawtx->coin->symbol,"BTC") == 0 && txfee > 0 && txfee < 50000 ) - txfee = 50000; - jadd64bits(valsobj,"txfee",txfee); - jaddnum(valsobj,"minconf",minconf); - if ( locktime == 0 ) - locktime = (uint32_t)time(NULL) - 777; - jaddnum(valsobj,"locktime",locktime); - jaddnum(valsobj,"timeout",30000); - jaddnum(valsobj,"timestamp",swapstarted+delay); - addresses = cJSON_CreateArray(); - bitcoin_address(coinaddr,rawtx->coin->pubtype,pubkey33,33); - jaddistr(addresses,coinaddr); - jadd(valsobj,"addresses",addresses); - rawtx->I.locktime = locktime; - printf("%s locktime.%u\n",rawtx->name,locktime); - V = calloc(256,sizeof(*V)); - privkeys = cJSON_CreateArray(); - bitcoin_priv2wif(wifstr,privkey,rawtx->coin->wiftype); - jaddistr(privkeys,wifstr); - vins = LP_createvins(rawtx,V,rawtx,0,0,0xffffffff); - rawtx->vins = jduplicate(vins); - jdelete(valsobj,"vin"); - jadd(valsobj,"vin",vins); - if ( (rawtxbytes= bitcoin_json2hex(rawtx->coin->isPoS,&rawtx->I.txid,valsobj,V)) != 0 ) - { - //printf("rawtx.(%s) vins.%p\n",rawtxbytes,vins); - if ( (signedtx= LP_signrawtx(rawtx->coin->symbol,&rawtx->I.signedtxid,&rawtx->I.completed,vins,rawtxbytes,privkeys,V)) != 0 ) - { - rawtx->I.datalen = (int32_t)strlen(signedtx) >> 1; - if ( rawtx->I.datalen <= sizeof(rawtx->txbytes) ) - decode_hex(rawtx->txbytes,rawtx->I.datalen,signedtx); - else printf("DEX tx is too big %d vs %d\n",rawtx->I.datalen,(int32_t)sizeof(rawtx->txbytes)); - if ( signedtx != rawtxbytes ) - free(signedtx); - if ( rawtx->I.completed != 0 ) - retval = 0; - else printf("couldnt complete sign transaction %s\n",rawtx->name); - } else printf("error signing\n"); - free(rawtxbytes); - } else printf("error making rawtx\n"); - free_json(privkeys); - free_json(valsobj); - free(V); - return(retval); -} - - -int32_t _basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,struct basilisk_swap *swap,uint32_t timestamp,uint32_t locktime,uint32_t sequenceid,struct basilisk_rawtx *dest,struct basilisk_rawtx *rawtx,bits256 privkey,bits256 *privkey2,uint8_t *userdata,int32_t userdatalen,int32_t ignore_cltverr) -{ - char *rawtxbytes=0,*signedtx=0,wifstr[128]; cJSON *txobj,*vins,*privkeys; int32_t needsig=1,retval = -1; struct vin_info *V; - V = calloc(256,sizeof(*V)); - V[0].signers[0].privkey = privkey; - bitcoin_pubkey33(swap->ctx,V[0].signers[0].pubkey,privkey); - privkeys = cJSON_CreateArray(); - bitcoin_priv2wif(wifstr,privkey,wiftype); - jaddistr(privkeys,wifstr); - if ( privkey2 != 0 ) - { - V[0].signers[1].privkey = *privkey2; - bitcoin_pubkey33(swap->ctx,V[0].signers[1].pubkey,*privkey2); - bitcoin_priv2wif(wifstr,*privkey2,wiftype); - jaddistr(privkeys,wifstr); - V[0].N = V[0].M = 2; - //char str[65]; printf("add second privkey.(%s) %s\n",jprint(privkeys,0),bits256_str(str,*privkey2)); - } else V[0].N = V[0].M = 1; - V[0].suppress_pubkeys = dest->I.suppress_pubkeys; - V[0].ignore_cltverr = ignore_cltverr; - if ( dest->I.redeemlen != 0 ) - memcpy(V[0].p2shscript,dest->redeemscript,dest->I.redeemlen), V[0].p2shlen = dest->I.redeemlen; - txobj = bitcoin_txcreate(symbol,isPoS,locktime,userdata == 0 ? 1 : 1,timestamp);//rawtx->coin->locktime_txversion); - vins = LP_createvins(dest,V,rawtx,userdata,userdatalen,sequenceid); - jdelete(txobj,"vin"); - jadd(txobj,"vin",vins); - //printf("basilisk_rawtx_sign locktime.%u/%u for %s spendscript.%s -> %s, suppress.%d\n",rawtx->I.locktime,dest->I.locktime,rawtx->name,hexstr,dest->name,dest->I.suppress_pubkeys); - txobj = bitcoin_txoutput(txobj,dest->spendscript,dest->I.spendlen,dest->I.amount); - if ( (rawtxbytes= bitcoin_json2hex(isPoS,&dest->I.txid,txobj,V)) != 0 ) - { - //printf("rawtx.(%s) vins.%p\n",rawtxbytes,vins); - if ( needsig == 0 ) - signedtx = rawtxbytes; - if ( signedtx != 0 || (signedtx= LP_signrawtx(symbol,&dest->I.signedtxid,&dest->I.completed,vins,rawtxbytes,privkeys,V)) != 0 ) - { - dest->I.datalen = (int32_t)strlen(signedtx) >> 1; - if ( dest->I.datalen <= sizeof(dest->txbytes) ) - decode_hex(dest->txbytes,dest->I.datalen,signedtx); - else printf("DEX tx is too big %d vs %d\n",dest->I.datalen,(int32_t)sizeof(dest->txbytes)); - if ( signedtx != rawtxbytes ) - free(signedtx); - if ( dest->I.completed != 0 ) - retval = 0; - else printf("couldnt complete sign transaction %s\n",rawtx->name); - } else printf("error signing\n"); - free(rawtxbytes); - } else printf("error making rawtx\n"); - free_json(privkeys); - free_json(txobj); - free(V); - return(retval); -} #endif char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr) @@ -1365,97 +1178,6 @@ int32_t basilisk_bobpayment_reclaim(struct basilisk_swap *swap,int32_t delay) return(-1); } -int32_t basilisk_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) -{ - int32_t datalen=0,retval=-1,hexlen,n; uint8_t *data; cJSON *txobj,*skey,*vouts,*vout; char *hexstr; bits256 txid; - datalen = recvbuf[0]; - datalen += (int32_t)recvbuf[1] << 8; - if ( datalen > 65536 ) - return(-1); - rawtx->I.redeemlen = recvbuf[2]; - data = &recvbuf[3]; - if ( rawtx->I.redeemlen > 0 && rawtx->I.redeemlen < 0x100 ) - memcpy(rawtx->redeemscript,&data[datalen],rawtx->I.redeemlen); - //printf("recvlen.%d datalen.%d redeemlen.%d\n",recvlen,datalen,rawtx->redeemlen); - if ( rawtx->I.datalen == 0 ) - { - //rawtx->txbytes = calloc(1,datalen); - memcpy(rawtx->txbytes,data,datalen); - rawtx->I.datalen = datalen; - } - else if ( datalen != rawtx->I.datalen || memcmp(rawtx->txbytes,data,datalen) != 0 ) - { - int32_t i; for (i=0; iI.datalen; i++) - printf("%02x",rawtx->txbytes[i]); - printf(" <- rawtx\n"); - printf("%s rawtx data compare error, len %d vs %d <<<<<<<<<< warning\n",rawtx->name,rawtx->I.datalen,datalen); - return(-1); - } - txid = bits256_doublesha256(0,data,datalen); - 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(rawtx->coin->pubtype,rawtx->coin->p2shtype,rawtx->coin->isPoS,height,&rawtx->I.signedtxid,&rawtx->msgtx,rawtx->extraspace,sizeof(rawtx->extraspace),data,datalen,0,suppress_pubkeys)) != 0 ) - { - rawtx->I.actualtxid = rawtx->I.signedtxid; - //char str[65]; printf("got txid.%s (%s)\n",bits256_str(str,rawtx->signedtxid),jprint(txobj,0)); - rawtx->I.locktime = rawtx->msgtx.lock_time; - if ( (vouts= jarray(&n,txobj,"vout")) != 0 && v < n ) - { - vout = jitem(vouts,v); - if ( j64bits(vout,"satoshis") == rawtx->I.amount && (skey= jobj(vout,"scriptPubKey")) != 0 && (hexstr= jstr(skey,"hex")) != 0 ) - { - if ( (hexlen= (int32_t)strlen(hexstr) >> 1) < sizeof(rawtx->spendscript) ) - { - decode_hex(rawtx->spendscript,hexlen,hexstr); - rawtx->I.spendlen = hexlen; - bitcoin_address(rawtx->p2shaddr,rawtx->coin->p2shtype,rawtx->spendscript,hexlen); - //if ( swap != 0 ) - // basilisk_txlog(swap->myinfoptr,swap,rawtx,-1); // bobdeposit, bobpayment or alicepayment - retval = 0; - } - } else printf("%s ERROR.(%s)\n",rawtx->name,jprint(txobj,0)); - } - free_json(txobj); - } - return(retval); -} - -int32_t basilisk_verify_bobpaid(void *ptr,uint8_t *data,int32_t datalen) -{ - uint8_t userdata[512]; int32_t i,retval,len = 0; bits256 revAm; struct basilisk_swap *swap = ptr; - memset(revAm.bytes,0,sizeof(revAm)); - if ( basilisk_rawtx_spendscript(swap,swap->bobcoin.longestchain,&swap->bobpayment,0,data,datalen,0) == 0 ) - { - swap->bobpayment.I.signedtxid = LP_broadcast_tx(swap->bobpayment.name,swap->bobpayment.coin->symbol,swap->bobpayment.txbytes,swap->bobpayment.I.datalen); - if ( bits256_nonz(swap->bobpayment.I.signedtxid) != 0 ) - swap->paymentunconf = 1; - basilisk_dontforget_update(swap,&swap->bobpayment); - for (i=0; i<32; i++) - revAm.bytes[i] = swap->I.privAm.bytes[31-i]; - len = basilisk_swapuserdata(userdata,revAm,0,swap->I.myprivs[0],swap->bobpayment.redeemscript,swap->bobpayment.I.redeemlen); - memcpy(swap->I.userdata_alicespend,userdata,len); - swap->I.userdata_alicespendlen = len; - char str[65],str2[65]; printf("bobpaid privAm.(%s) myprivs[0].(%s)\n",bits256_str(str,swap->I.privAm),bits256_str(str2,swap->I.myprivs[0])); - if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->alicespend,&swap->bobpayment,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) - { - for (i=0; ibobpayment.I.datalen; i++) - printf("%02x",swap->bobpayment.txbytes[i]); - printf(" <- bobpayment\n"); - for (i=0; ialicespend.I.datalen; i++) - printf("%02x",swap->alicespend.txbytes[i]); - printf(" <- alicespend\n\n"); - swap->I.alicespent = 1; - //basilisk_txlog(swap,&swap->alicespend,-1); - return(retval); - } - } - return(-1); -} - int32_t basilisk_bobdeposit_refund(struct basilisk_swap *swap,int32_t delay) { uint8_t userdata[512]; int32_t i,retval,len = 0; char str[65]; @@ -1557,60 +1279,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i /**/ -struct basilisk_rawtx *LP_swapdata_rawtx(struct basilisk_swap *swap,uint8_t *data,int32_t maxlen,struct basilisk_rawtx *rawtx) -{ - if ( rawtx->I.datalen != 0 && rawtx->I.datalen <= maxlen ) - { - memcpy(data,rawtx->txbytes,rawtx->I.datalen); - return(rawtx); - } - return(0); -} - -uint32_t LP_swapdata_rawtxsend(struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t maxlen,struct basilisk_rawtx *rawtx,uint32_t nextbits,int32_t suppress_swapsend) -{ - uint8_t sendbuf[32768]; int32_t sendlen; - if ( LP_swapdata_rawtx(swap,data,maxlen,rawtx) != 0 ) - { - if ( bits256_nonz(rawtx->I.signedtxid) != 0 && bits256_nonz(rawtx->I.actualtxid) == 0 ) - { - char str[65],str2[65]; - rawtx->I.actualtxid = LP_broadcast_tx(rawtx->name,rawtx->coin->symbol,rawtx->txbytes,rawtx->I.datalen); - if ( bits256_cmp(rawtx->I.actualtxid,rawtx->I.signedtxid) != 0 ) - { - printf("%s rawtxsend %s vs %s\n",rawtx->name,bits256_str(str,rawtx->I.signedtxid),bits256_str(str2,rawtx->I.actualtxid)); - rawtx->I.actualtxid = rawtx->I.signedtxid; - } - if ( bits256_nonz(rawtx->I.actualtxid) != 0 && msgbits != 0 ) - { - sendlen = 0; - sendbuf[sendlen++] = rawtx->I.datalen & 0xff; - sendbuf[sendlen++] = (rawtx->I.datalen >> 8) & 0xff; - sendbuf[sendlen++] = rawtx->I.redeemlen; - memcpy(&sendbuf[sendlen],rawtx->txbytes,rawtx->I.datalen), sendlen += rawtx->I.datalen; - if ( rawtx->I.redeemlen > 0 && rawtx->I.redeemlen < 0x100 ) - { - memcpy(&sendbuf[sendlen],rawtx->redeemscript,rawtx->I.redeemlen); - sendlen += rawtx->I.redeemlen; - } - basilisk_dontforget_update(swap,rawtx); - //printf("sendlen.%d datalen.%d redeemlen.%d\n",sendlen,rawtx->datalen,rawtx->redeemlen); - if ( suppress_swapsend == 0 ) - return(LP_swapsend(swap,msgbits,sendbuf,sendlen,nextbits,rawtx->I.crcs)); - else - { - printf("suppress swapsend %x\n",msgbits); - return(0); - } - } - } - return(nextbits); - } else if ( swap->I.iambob == 0 ) - printf("error from basilisk_swapdata_rawtx.%s %p len.%d\n",rawtx->name,rawtx->txbytes,rawtx->I.datalen); - return(0); -} - -void basilisk_swap_coinaddr(struct basilisk_swap *swap,struct iguana_info *coin,char *coinaddr,uint8_t *data,int32_t datalen) +void LP_swap_coinaddr(struct basilisk_swap *swap,struct iguana_info *coin,char *coinaddr,uint8_t *data,int32_t datalen) { cJSON *txobj,*vouts,*vout,*addresses,*item,*skey; uint8_t extraspace[8192]; bits256 signedtxid; struct iguana_msgtx msgtx; char *addr; int32_t n,m,suppress_pubkeys = 0; if ( (txobj= bitcoin_data2json(coin->pubtype,coin->p2shtype,coin->isPoS,coin->longestchain,&signedtxid,&msgtx,extraspace,sizeof(extraspace),data,datalen,0,suppress_pubkeys)) != 0 ) @@ -1635,59 +1304,6 @@ void basilisk_swap_coinaddr(struct basilisk_swap *swap,struct iguana_info *coin, } } -int32_t basilisk_verify_otherfee(void *ptr,uint8_t *data,int32_t datalen) -{ - struct basilisk_swap *swap = ptr; - // add verification and broadcast - //swap->otherfee.txbytes = calloc(1,datalen); - memcpy(swap->otherfee.txbytes,data,datalen); - swap->otherfee.I.datalen = datalen; - swap->otherfee.I.actualtxid = swap->otherfee.I.signedtxid = bits256_doublesha256(0,data,datalen); - //basilisk_txlog(swap,&swap->otherfee,-1); - return(0); -} - -/* Bob deposit: - OP_IF - OP_CLTV OP_DROP OP_CHECKSIG - OP_ELSE - OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG - OP_ENDIF*/ - -int32_t basilisk_verify_bobdeposit(void *ptr,uint8_t *data,int32_t datalen) -{ - uint8_t userdata[512]; int32_t i,retval,len = 0; static bits256 zero; struct basilisk_swap *swap = ptr; - if ( basilisk_rawtx_spendscript(swap,swap->bobcoin.longestchain,&swap->bobdeposit,0,data,datalen,0) == 0 ) - { - swap->bobdeposit.I.signedtxid = LP_broadcast_tx(swap->bobdeposit.name,swap->bobcoin.symbol,swap->bobdeposit.txbytes,swap->bobdeposit.I.datalen); - if ( bits256_nonz(swap->bobdeposit.I.signedtxid) != 0 ) - swap->depositunconf = 1; - basilisk_dontforget_update(swap,&swap->bobdeposit); - len = basilisk_swapuserdata(userdata,zero,1,swap->I.myprivs[0],swap->bobdeposit.redeemscript,swap->bobdeposit.I.redeemlen); - memcpy(swap->I.userdata_aliceclaim,userdata,len); - swap->I.userdata_aliceclaimlen = len; - if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->aliceclaim,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) - { - 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"); - //basilisk_txlog(swap,&swap->aliceclaim,swap->I.putduration+swap->I.callduration); - return(retval); - } - } - printf("error with bobdeposit\n"); - return(-1); -} - -void basilisk_alicepayment(struct basilisk_swap *swap,struct iguana_info *coin,struct basilisk_rawtx *alicepayment,bits256 pubAm,bits256 pubBn) -{ - alicepayment->I.spendlen = basilisk_alicescript(alicepayment->redeemscript,&alicepayment->I.redeemlen,alicepayment->spendscript,0,alicepayment->I.destaddr,coin->p2shtype,pubAm,pubBn); - basilisk_rawtx_gen(swap->ctx,"alicepayment",swap->I.started,swap->persistent_pubkey33,0,1,alicepayment,alicepayment->I.locktime,alicepayment->spendscript,alicepayment->I.spendlen,coin->txfee,1,0,swap->persistent_privkey,swap->changermd160); -} - int32_t basilisk_alicepayment_spend(struct basilisk_swap *swap,struct basilisk_rawtx *dest) { int32_t i,retval; @@ -1717,21 +1333,13 @@ int32_t basilisk_alicepayment_spend(struct basilisk_swap *swap,struct basilisk_r return(-1); } -int32_t basilisk_verify_alicepaid(void *ptr,uint8_t *data,int32_t datalen) +void basilisk_alicepayment(struct basilisk_swap *swap,struct iguana_info *coin,struct basilisk_rawtx *alicepayment,bits256 pubAm,bits256 pubBn) { - struct basilisk_swap *swap = ptr; - if ( basilisk_rawtx_spendscript(swap,swap->alicecoin.longestchain,&swap->alicepayment,0,data,datalen,0) == 0 ) - { - swap->alicepayment.I.signedtxid = LP_broadcast_tx(swap->alicepayment.name,swap->alicecoin.symbol,swap->alicepayment.txbytes,swap->alicepayment.I.datalen); - if ( bits256_nonz(swap->alicepayment.I.signedtxid) != 0 ) - swap->aliceunconf = 1; - basilisk_dontforget_update(swap,&swap->alicepayment); - return(0); - } - else return(-1); + alicepayment->I.spendlen = basilisk_alicescript(alicepayment->redeemscript,&alicepayment->I.redeemlen,alicepayment->spendscript,0,alicepayment->I.destaddr,coin->p2shtype,pubAm,pubBn); + basilisk_rawtx_gen(swap->ctx,"alicepayment",swap->I.started,swap->persistent_pubkey33,0,1,alicepayment,alicepayment->I.locktime,alicepayment->spendscript,alicepayment->I.spendlen,coin->txfee,1,0,swap->persistent_privkey,swap->changermd160); } -int32_t basilisk_alicetxs(struct basilisk_swap *swap,uint8_t *data,int32_t maxlen) +int32_t basilisk_alicetxs(int32_t pairsock,struct basilisk_swap *swap,uint8_t *data,int32_t maxlen) { int32_t i,retval = -1; printf("alicetxs\n"); @@ -1760,7 +1368,7 @@ int32_t basilisk_alicetxs(struct basilisk_swap *swap,uint8_t *data,int32_t maxle //printf("generate fee\n"); if ( basilisk_rawtx_gen(swap->ctx,"myfee",swap->I.started,swap->persistent_pubkey33,swap->I.iambob,1,&swap->myfee,0,swap->myfee.spendscript,swap->myfee.I.spendlen,swap->myfee.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160) == 0 ) { - swap->I.statebits |= LP_swapdata_rawtxsend(swap,0x80,data,maxlen,&swap->myfee,0x40,0); + swap->I.statebits |= LP_swapdata_rawtxsend(pairsock,swap,0x80,data,maxlen,&swap->myfee,0x40,0); LP_unspents_mark(swap->I.iambob!=0?swap->bobcoin.symbol:swap->alicecoin.symbol,swap->myfee.vins); //basilisk_txlog(swap,&swap->myfee,-1); for (i=0; imyfee.I.datalen; i++) @@ -1778,3 +1386,151 @@ int32_t basilisk_alicetxs(struct basilisk_swap *swap,uint8_t *data,int32_t maxle return(0); return(-1); } + +int32_t LP_verify_otherfee(struct basilisk_swap *swap,uint8_t *data,int32_t datalen) +{ + // add verification and broadcast + memcpy(swap->otherfee.txbytes,data,datalen); + swap->otherfee.I.datalen = datalen; + swap->otherfee.I.actualtxid = swap->otherfee.I.signedtxid = bits256_doublesha256(0,data,datalen); + return(0); +} + +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) +{ + int32_t datalen=0,retval=-1,hexlen,n; uint8_t *data; cJSON *txobj,*skey,*vouts,*vout; char *hexstr; bits256 txid; + datalen = recvbuf[0]; + datalen += (int32_t)recvbuf[1] << 8; + if ( datalen > 65536 ) + return(-1); + rawtx->I.redeemlen = recvbuf[2]; + data = &recvbuf[3]; + if ( rawtx->I.redeemlen > 0 && rawtx->I.redeemlen < 0x100 ) + memcpy(rawtx->redeemscript,&data[datalen],rawtx->I.redeemlen); + //printf("recvlen.%d datalen.%d redeemlen.%d\n",recvlen,datalen,rawtx->redeemlen); + if ( rawtx->I.datalen == 0 ) + { + //rawtx->txbytes = calloc(1,datalen); + memcpy(rawtx->txbytes,data,datalen); + rawtx->I.datalen = datalen; + } + else if ( datalen != rawtx->I.datalen || memcmp(rawtx->txbytes,data,datalen) != 0 ) + { + int32_t i; for (i=0; iI.datalen; i++) + printf("%02x",rawtx->txbytes[i]); + printf(" <- rawtx\n"); + printf("%s rawtx data compare error, len %d vs %d <<<<<<<<<< warning\n",rawtx->name,rawtx->I.datalen,datalen); + return(-1); + } + txid = bits256_doublesha256(0,data,datalen); + 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(rawtx->coin->pubtype,rawtx->coin->p2shtype,rawtx->coin->isPoS,height,&rawtx->I.signedtxid,&rawtx->msgtx,rawtx->extraspace,sizeof(rawtx->extraspace),data,datalen,0,suppress_pubkeys)) != 0 ) + { + rawtx->I.actualtxid = rawtx->I.signedtxid; + //char str[65]; printf("got txid.%s (%s)\n",bits256_str(str,rawtx->signedtxid),jprint(txobj,0)); + rawtx->I.locktime = rawtx->msgtx.lock_time; + if ( (vouts= jarray(&n,txobj,"vout")) != 0 && v < n ) + { + vout = jitem(vouts,v); + if ( j64bits(vout,"satoshis") == rawtx->I.amount && (skey= jobj(vout,"scriptPubKey")) != 0 && (hexstr= jstr(skey,"hex")) != 0 ) + { + if ( (hexlen= (int32_t)strlen(hexstr) >> 1) < sizeof(rawtx->spendscript) ) + { + decode_hex(rawtx->spendscript,hexlen,hexstr); + rawtx->I.spendlen = hexlen; + bitcoin_address(rawtx->p2shaddr,rawtx->coin->p2shtype,rawtx->spendscript,hexlen); + //if ( swap != 0 ) + // basilisk_txlog(swap->myinfoptr,swap,rawtx,-1); // bobdeposit, bobpayment or alicepayment + retval = 0; + } + } else printf("%s ERROR.(%s)\n",rawtx->name,jprint(txobj,0)); + } + free_json(txobj); + } + return(retval); +} + +/* Bob deposit: + OP_IF + OP_CLTV OP_DROP OP_CHECKSIG + OP_ELSE + OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG + OP_ENDIF*/ + +int32_t LP_verify_bobdeposit(struct basilisk_swap *swap,uint8_t *data,int32_t datalen) +{ + uint8_t userdata[512]; int32_t i,retval,len = 0; static bits256 zero; + if ( LP_rawtx_spendscript(swap,swap->bobcoin.longestchain,&swap->bobdeposit,0,data,datalen,0) == 0 ) + { + swap->bobdeposit.I.signedtxid = LP_broadcast_tx(swap->bobdeposit.name,swap->bobcoin.symbol,swap->bobdeposit.txbytes,swap->bobdeposit.I.datalen); + if ( bits256_nonz(swap->bobdeposit.I.signedtxid) != 0 ) + swap->depositunconf = 1; + basilisk_dontforget_update(swap,&swap->bobdeposit); + len = basilisk_swapuserdata(userdata,zero,1,swap->I.myprivs[0],swap->bobdeposit.redeemscript,swap->bobdeposit.I.redeemlen); + memcpy(swap->I.userdata_aliceclaim,userdata,len); + swap->I.userdata_aliceclaimlen = len; + if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->aliceclaim,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) + { + 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"); + //basilisk_txlog(swap,&swap->aliceclaim,swap->I.putduration+swap->I.callduration); + return(retval); + } + } + printf("error with bobdeposit\n"); + return(-1); +} + +int32_t LP_verify_alicepayment(struct basilisk_swap *swap,uint8_t *data,int32_t datalen) +{ + if ( LP_rawtx_spendscript(swap,swap->alicecoin.longestchain,&swap->alicepayment,0,data,datalen,0) == 0 ) + { + swap->alicepayment.I.signedtxid = LP_broadcast_tx(swap->alicepayment.name,swap->alicecoin.symbol,swap->alicepayment.txbytes,swap->alicepayment.I.datalen); + if ( bits256_nonz(swap->alicepayment.I.signedtxid) != 0 ) + swap->aliceunconf = 1; + basilisk_dontforget_update(swap,&swap->alicepayment); + return(0); + } + else return(-1); +} + +int32_t LP_verify_bobpayment(struct basilisk_swap *swap,uint8_t *data,int32_t datalen) +{ + uint8_t userdata[512]; int32_t i,retval,len = 0; bits256 revAm; + memset(revAm.bytes,0,sizeof(revAm)); + if ( LP_rawtx_spendscript(swap,swap->bobcoin.longestchain,&swap->bobpayment,0,data,datalen,0) == 0 ) + { + swap->bobpayment.I.signedtxid = LP_broadcast_tx(swap->bobpayment.name,swap->bobpayment.coin->symbol,swap->bobpayment.txbytes,swap->bobpayment.I.datalen); + if ( bits256_nonz(swap->bobpayment.I.signedtxid) != 0 ) + swap->paymentunconf = 1; + basilisk_dontforget_update(swap,&swap->bobpayment); + for (i=0; i<32; i++) + revAm.bytes[i] = swap->I.privAm.bytes[31-i]; + len = basilisk_swapuserdata(userdata,revAm,0,swap->I.myprivs[0],swap->bobpayment.redeemscript,swap->bobpayment.I.redeemlen); + memcpy(swap->I.userdata_alicespend,userdata,len); + swap->I.userdata_alicespendlen = len; + char str[65],str2[65]; printf("bobpaid privAm.(%s) myprivs[0].(%s)\n",bits256_str(str,swap->I.privAm),bits256_str(str2,swap->I.myprivs[0])); + if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->alicespend,&swap->bobpayment,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) + { + for (i=0; ibobpayment.I.datalen; i++) + printf("%02x",swap->bobpayment.txbytes[i]); + printf(" <- bobpayment\n"); + for (i=0; ialicespend.I.datalen; i++) + printf("%02x",swap->alicespend.txbytes[i]); + printf(" <- alicespend\n\n"); + swap->I.alicespent = 1; + //basilisk_txlog(swap,&swap->alicespend,-1); + return(retval); + } + } + return(-1); +} From 1b97251bc675529d248edd6b89704ba6e1737f50 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 17:05:34 +0300 Subject: [PATCH 213/339] Test --- iguana/exchanges/LP_statemachine.c | 219 ++++++++++++++++++++++++++++ iguana/exchanges/LP_swap.c | 222 +---------------------------- 2 files changed, 221 insertions(+), 220 deletions(-) diff --git a/iguana/exchanges/LP_statemachine.c b/iguana/exchanges/LP_statemachine.c index 950e22e88..9b4604c25 100644 --- a/iguana/exchanges/LP_statemachine.c +++ b/iguana/exchanges/LP_statemachine.c @@ -724,6 +724,225 @@ cJSON *basilisk_privkeyarray(struct iguana_info *coin,cJSON *vins) #endif +#ifdef old +void basilisk_swaploop(void *_utxo) +{ + uint8_t *data; uint32_t expiration,savestatebits=0,saveotherbits=0; uint32_t channel; int32_t iters,retval=0,j,datalen,maxlen; struct basilisk_swap *swap; struct LP_utxoinfo *utxo = _utxo; + swap = utxo->swap; + fprintf(stderr,"start swap iambob.%d\n",swap->I.iambob); + maxlen = 1024*1024 + sizeof(*swap); + data = malloc(maxlen); + expiration = (uint32_t)time(NULL) + 300; + //myinfo->DEXactive = expiration; + channel = 'D' + ((uint32_t)'E' << 8) + ((uint32_t)'X' << 16); + while ( swap->aborted == 0 && (swap->I.statebits & (0x08|0x02)) != (0x08|0x02) && time(NULL) < expiration ) + { + LP_channelsend(swap->I.req.srchash,swap->I.req.desthash,channel,0x4000000,(void *)&swap->I.req.requestid,sizeof(swap->I.req.requestid)); //,60); + if ( swap->connected == 0 ) + basilisk_psockinit(swap,swap->I.iambob != 0); + if ( swap->connected > 0 ) + { + printf("A r%u/q%u swapstate.%x\n",swap->I.req.requestid,swap->I.req.quoteid,swap->I.statebits); + basilisk_sendstate(swap,data,maxlen); + basilisk_sendpubkeys(swap,data,maxlen); // send pubkeys + if ( basilisk_checkdeck(swap,data,maxlen) == 0) // check for other deck 0x02 + basilisk_sendchoosei(swap,data,maxlen); + basilisk_waitchoosei(swap,data,maxlen); // wait for choosei 0x08 + if ( (swap->I.statebits & (0x08|0x02)) == (0x08|0x02) ) + break; + } + if ( swap->I.statebits == savestatebits && swap->I.otherstatebits == saveotherbits ) + sleep(DEX_SLEEP + (swap->I.iambob == 0)*1); + savestatebits = swap->I.statebits; + saveotherbits = swap->I.otherstatebits; + } + if ( swap->connected == 0 ) + { + printf("couldnt establish connection\n"); + retval = -1; + } + while ( swap->aborted == 0 && retval == 0 && (swap->I.statebits & 0x20) == 0 ) + { + if ( swap->connected == 0 ) + basilisk_psockinit(swap,swap->I.iambob != 0); + printf("B r%u/q%u swapstate.%x\n",swap->I.req.requestid,swap->I.req.quoteid,swap->I.statebits); + basilisk_sendstate(swap,data,maxlen); + basilisk_sendchoosei(swap,data,maxlen); + basilisk_sendmostprivs(swap,data,maxlen); + if ( basilisk_swapget(swap,0x20,data,maxlen,basilisk_verify_privkeys) == 0 ) + { + swap->I.statebits |= 0x20; + break; + } + if ( swap->I.statebits == savestatebits && swap->I.otherstatebits == saveotherbits ) + sleep(DEX_SLEEP + (swap->I.iambob == 0)*1); + savestatebits = swap->I.statebits; + saveotherbits = swap->I.otherstatebits; + if ( time(NULL) > expiration ) + break; + } + //myinfo->DEXactive = swap->I.expiration; + if ( time(NULL) >= expiration ) + { + retval = -1; + //myinfo->DEXactive = 0; + } + if ( swap->aborted != 0 ) + { + printf("swap aborted before tx sent\n"); + retval = -1; + } + printf("C r%u/q%u swapstate.%x retval.%d\n",swap->I.req.requestid,swap->I.req.quoteid,swap->I.statebits,retval); + iters = 0; + while ( swap->aborted == 0 && retval == 0 && (swap->I.statebits & 0x40) == 0 && iters++ < 10 ) // send fee + { + if ( swap->connected == 0 ) + basilisk_psockinit(swap,swap->I.iambob != 0); + //printf("sendstate.%x\n",swap->I.statebits); + basilisk_sendstate(swap,data,maxlen); + //printf("swapget\n"); + basilisk_swapget(swap,0x80000000,data,maxlen,basilisk_verify_otherstatebits); + //printf("after swapget\n"); + if ( swap->I.iambob != 0 && swap->bobdeposit.I.datalen == 0 ) + { + printf("bobscripts set\n"); + if ( basilisk_bobscripts_set(swap,1,1) < 0 ) + { + sleep(DEX_SLEEP); + printf("bobscripts set error\n"); + continue; + } + } + if ( swap->I.iambob == 0 ) + { + /*for (i=0; i<20; i++) + printf("%02x",swap->secretAm[i]); + printf(" <- secretAm\n"); + for (i=0; i<32; i++) + printf("%02x",swap->secretAm256[i]); + printf(" <- secretAm256\n"); + for (i=0; i<32; i++) + printf("%02x",swap->pubAm.bytes[i]); + printf(" <- pubAm\n"); + for (i=0; i<20; i++) + printf("%02x",swap->secretBn[i]); + printf(" <- secretBn\n"); + for (i=0; i<32; i++) + printf("%02x",swap->secretBn256[i]); + printf(" <- secretBn256\n"); + for (i=0; i<32; i++) + printf("%02x",swap->pubBn.bytes[i]); + printf(" <- pubBn\n"); + for (i=0; i<32; i++) + printf("%02x",swap->pubA0.bytes[i]); + printf(" <- pubA0\n"); + for (i=0; i<32; i++) + printf("%02x",swap->pubA1.bytes[i]); + printf(" <- pubA1\n"); + for (i=0; i<32; i++) + printf("%02x",swap->pubB0.bytes[i]); + printf(" <- pubB0\n"); + for (i=0; i<32; i++) + printf("%02x",swap->pubB1.bytes[i]); + printf(" <- pubB1\n");*/ + if ( (retval= basilisk_alicetxs(swap,data,maxlen)) != 0 ) + { + printf("basilisk_alicetxs error\n"); + break; + } + } + } + if ( swap->I.iambob == 0 && (swap->I.statebits & 0x40) == 0 ) + { + printf("couldnt send fee\n"); + retval = -8; + } + if ( retval == 0 ) + { + if ( swap->I.iambob == 0 && (swap->myfee.I.datalen == 0 || swap->alicepayment.I.datalen == 0 || swap->alicepayment.I.datalen == 0) ) + { + printf("ALICE's error %d %d %d\n",swap->myfee.I.datalen,swap->alicepayment.I.datalen,swap->alicepayment.I.datalen); + retval = -7; + } + else if ( swap->I.iambob != 0 && swap->bobdeposit.I.datalen == 0 ) //swap->bobpayment.I.datalen == 0 + { + printf("BOB's error %d %d %d\n",swap->myfee.I.datalen,swap->bobpayment.I.datalen,swap->bobdeposit.I.datalen); + retval = -7; + } + } + while ( swap->aborted == 0 && retval == 0 && basilisk_swapiteration(swap,data,maxlen) == 0 ) + { + if ( swap->I.statebits == savestatebits && swap->I.otherstatebits == saveotherbits ) + sleep(DEX_SLEEP + (swap->I.iambob == 0)*1); + savestatebits = swap->I.statebits; + saveotherbits = swap->I.otherstatebits; + basilisk_sendstate(swap,data,maxlen); + basilisk_swapget(swap,0x80000000,data,maxlen,basilisk_verify_otherstatebits); + basilisk_swap_saveupdate(swap); + if ( time(NULL) > swap->I.expiration ) + break; + } + if ( swap->I.iambob != 0 && swap->bobdeposit.I.datalen != 0 && bits256_nonz(swap->bobdeposit.I.actualtxid) != 0 ) + { + printf("BOB waiting for confirm state.%x\n",swap->I.statebits); + sleep(60); // wait for confirm/propagation of msig + printf("BOB reclaims refund\n"); + basilisk_bobdeposit_refund(swap,0); + if ( LP_swapdata_rawtxsend(swap,0,data,maxlen,&swap->bobrefund,0x40000000,0) == 0 ) // use secretBn + { + printf("Bob submit error getting refund of deposit\n"); + } + else + { + // maybe wait for bobrefund to be confirmed + for (j=datalen=0; j<32; j++) + data[datalen++] = swap->I.privBn.bytes[j]; + LP_swapsend(swap,0x40000000,data,datalen,0x40000000,swap->I.crcs_mypriv); + } + basilisk_swap_saveupdate(swap); + } + if ( retval != 0 ) + basilisk_swap_sendabort(swap); + printf("end of atomic swap\n"); + if ( swapcompleted(swap) > 0 ) // only if swap completed + { + if ( swap->I.iambob != 0 ) + tradebot_pendingadd(swapjson(swap),swap->I.req.src,dstr(swap->I.req.srcamount),swap->I.req.dest,dstr(swap->I.req.destamount)); + else tradebot_pendingadd(swapjson(swap),swap->I.req.dest,dstr(swap->I.req.destamount),swap->I.req.src,dstr(swap->I.req.srcamount)); + } + printf("%s swap finished statebits %x\n",swap->I.iambob!=0?"BOB":"ALICE",swap->I.statebits); + //basilisk_swap_purge(swap); + free(data); +} +#endif + +int32_t bitcoin_coinptrs(bits256 pubkey,struct iguana_info **bobcoinp,struct iguana_info **alicecoinp,char *src,char *dest,bits256 srchash,bits256 desthash) +{ + struct iguana_info *coin = LP_coinfind(src); + if ( coin == 0 || LP_coinfind(dest) == 0 ) + return(0); + *bobcoinp = *alicecoinp = 0; + *bobcoinp = LP_coinfind(dest); + *alicecoinp = LP_coinfind(src); + if ( bits256_cmp(pubkey,srchash) == 0 ) + { + if ( strcmp(src,(*bobcoinp)->symbol) == 0 ) + return(1); + else if ( strcmp(dest,(*alicecoinp)->symbol) == 0 ) + return(-1); + else return(0); + } + else if ( bits256_cmp(pubkey,desthash) == 0 ) + { + if ( strcmp(src,(*bobcoinp)->symbol) == 0 ) + return(-1); + else if ( strcmp(dest,(*alicecoinp)->symbol) == 0 ) + return(1); + else return(0); + } + return(0); +} + /*void basilisk_swap_purge(struct basilisk_swap *swap) { int32_t i,n; diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index c066e3012..55b48fe66 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -119,33 +119,6 @@ */ -int32_t bitcoin_coinptrs(bits256 pubkey,struct iguana_info **bobcoinp,struct iguana_info **alicecoinp,char *src,char *dest,bits256 srchash,bits256 desthash) -{ - struct iguana_info *coin = LP_coinfind(src); - if ( coin == 0 || LP_coinfind(dest) == 0 ) - return(0); - *bobcoinp = *alicecoinp = 0; - *bobcoinp = LP_coinfind(dest); - *alicecoinp = LP_coinfind(src); - if ( bits256_cmp(pubkey,srchash) == 0 ) - { - if ( strcmp(src,(*bobcoinp)->symbol) == 0 ) - return(1); - else if ( strcmp(dest,(*alicecoinp)->symbol) == 0 ) - return(-1); - else return(0); - } - else if ( bits256_cmp(pubkey,desthash) == 0 ) - { - if ( strcmp(src,(*bobcoinp)->symbol) == 0 ) - return(-1); - else if ( strcmp(dest,(*alicecoinp)->symbol) == 0 ) - return(1); - else return(0); - } - return(0); -} - void basilisk_rawtx_purge(struct basilisk_rawtx *rawtx) { if ( rawtx->vins != 0 ) @@ -498,6 +471,7 @@ struct basilisk_rawtx *LP_swapdata_rawtx(struct basilisk_swap *swap,uint8_t *dat memcpy(data,rawtx->txbytes,rawtx->I.datalen); return(rawtx); } + printf("swapdata rawtx has null txbytes\n"); return(0); } @@ -539,7 +513,7 @@ uint32_t LP_swapdata_rawtxsend(int32_t pairsock,struct basilisk_swap *swap,uint3 } } return(nextbits); - } else if ( swap->I.iambob == 0 ) + } //else if ( swap->I.iambob == 0 ) printf("error from basilisk_swapdata_rawtx.%s %p len.%d\n",rawtx->name,rawtx->txbytes,rawtx->I.datalen); return(0); } @@ -636,198 +610,6 @@ void LP_aliceloop(void *_qp) free(qp); } -#ifdef old -void basilisk_swaploop(void *_utxo) -{ - uint8_t *data; uint32_t expiration,savestatebits=0,saveotherbits=0; uint32_t channel; int32_t iters,retval=0,j,datalen,maxlen; struct basilisk_swap *swap; struct LP_utxoinfo *utxo = _utxo; - swap = utxo->swap; - fprintf(stderr,"start swap iambob.%d\n",swap->I.iambob); - maxlen = 1024*1024 + sizeof(*swap); - data = malloc(maxlen); - expiration = (uint32_t)time(NULL) + 300; - //myinfo->DEXactive = expiration; - channel = 'D' + ((uint32_t)'E' << 8) + ((uint32_t)'X' << 16); - while ( swap->aborted == 0 && (swap->I.statebits & (0x08|0x02)) != (0x08|0x02) && time(NULL) < expiration ) - { - LP_channelsend(swap->I.req.srchash,swap->I.req.desthash,channel,0x4000000,(void *)&swap->I.req.requestid,sizeof(swap->I.req.requestid)); //,60); - if ( swap->connected == 0 ) - basilisk_psockinit(swap,swap->I.iambob != 0); - if ( swap->connected > 0 ) - { - printf("A r%u/q%u swapstate.%x\n",swap->I.req.requestid,swap->I.req.quoteid,swap->I.statebits); - basilisk_sendstate(swap,data,maxlen); - basilisk_sendpubkeys(swap,data,maxlen); // send pubkeys - if ( basilisk_checkdeck(swap,data,maxlen) == 0) // check for other deck 0x02 - basilisk_sendchoosei(swap,data,maxlen); - basilisk_waitchoosei(swap,data,maxlen); // wait for choosei 0x08 - if ( (swap->I.statebits & (0x08|0x02)) == (0x08|0x02) ) - break; - } - if ( swap->I.statebits == savestatebits && swap->I.otherstatebits == saveotherbits ) - sleep(DEX_SLEEP + (swap->I.iambob == 0)*1); - savestatebits = swap->I.statebits; - saveotherbits = swap->I.otherstatebits; - } - if ( swap->connected == 0 ) - { - printf("couldnt establish connection\n"); - retval = -1; - } - while ( swap->aborted == 0 && retval == 0 && (swap->I.statebits & 0x20) == 0 ) - { - if ( swap->connected == 0 ) - basilisk_psockinit(swap,swap->I.iambob != 0); - printf("B r%u/q%u swapstate.%x\n",swap->I.req.requestid,swap->I.req.quoteid,swap->I.statebits); - basilisk_sendstate(swap,data,maxlen); - basilisk_sendchoosei(swap,data,maxlen); - basilisk_sendmostprivs(swap,data,maxlen); - if ( basilisk_swapget(swap,0x20,data,maxlen,basilisk_verify_privkeys) == 0 ) - { - swap->I.statebits |= 0x20; - break; - } - if ( swap->I.statebits == savestatebits && swap->I.otherstatebits == saveotherbits ) - sleep(DEX_SLEEP + (swap->I.iambob == 0)*1); - savestatebits = swap->I.statebits; - saveotherbits = swap->I.otherstatebits; - if ( time(NULL) > expiration ) - break; - } - //myinfo->DEXactive = swap->I.expiration; - if ( time(NULL) >= expiration ) - { - retval = -1; - //myinfo->DEXactive = 0; - } - if ( swap->aborted != 0 ) - { - printf("swap aborted before tx sent\n"); - retval = -1; - } - printf("C r%u/q%u swapstate.%x retval.%d\n",swap->I.req.requestid,swap->I.req.quoteid,swap->I.statebits,retval); - iters = 0; - while ( swap->aborted == 0 && retval == 0 && (swap->I.statebits & 0x40) == 0 && iters++ < 10 ) // send fee - { - if ( swap->connected == 0 ) - basilisk_psockinit(swap,swap->I.iambob != 0); - //printf("sendstate.%x\n",swap->I.statebits); - basilisk_sendstate(swap,data,maxlen); - //printf("swapget\n"); - basilisk_swapget(swap,0x80000000,data,maxlen,basilisk_verify_otherstatebits); - //printf("after swapget\n"); - if ( swap->I.iambob != 0 && swap->bobdeposit.I.datalen == 0 ) - { - printf("bobscripts set\n"); - if ( basilisk_bobscripts_set(swap,1,1) < 0 ) - { - sleep(DEX_SLEEP); - printf("bobscripts set error\n"); - continue; - } - } - if ( swap->I.iambob == 0 ) - { - /*for (i=0; i<20; i++) - printf("%02x",swap->secretAm[i]); - printf(" <- secretAm\n"); - for (i=0; i<32; i++) - printf("%02x",swap->secretAm256[i]); - printf(" <- secretAm256\n"); - for (i=0; i<32; i++) - printf("%02x",swap->pubAm.bytes[i]); - printf(" <- pubAm\n"); - for (i=0; i<20; i++) - printf("%02x",swap->secretBn[i]); - printf(" <- secretBn\n"); - for (i=0; i<32; i++) - printf("%02x",swap->secretBn256[i]); - printf(" <- secretBn256\n"); - for (i=0; i<32; i++) - printf("%02x",swap->pubBn.bytes[i]); - printf(" <- pubBn\n"); - for (i=0; i<32; i++) - printf("%02x",swap->pubA0.bytes[i]); - printf(" <- pubA0\n"); - for (i=0; i<32; i++) - printf("%02x",swap->pubA1.bytes[i]); - printf(" <- pubA1\n"); - for (i=0; i<32; i++) - printf("%02x",swap->pubB0.bytes[i]); - printf(" <- pubB0\n"); - for (i=0; i<32; i++) - printf("%02x",swap->pubB1.bytes[i]); - printf(" <- pubB1\n");*/ - if ( (retval= basilisk_alicetxs(swap,data,maxlen)) != 0 ) - { - printf("basilisk_alicetxs error\n"); - break; - } - } - } - if ( swap->I.iambob == 0 && (swap->I.statebits & 0x40) == 0 ) - { - printf("couldnt send fee\n"); - retval = -8; - } - if ( retval == 0 ) - { - if ( swap->I.iambob == 0 && (swap->myfee.I.datalen == 0 || swap->alicepayment.I.datalen == 0 || swap->alicepayment.I.datalen == 0) ) - { - printf("ALICE's error %d %d %d\n",swap->myfee.I.datalen,swap->alicepayment.I.datalen,swap->alicepayment.I.datalen); - retval = -7; - } - else if ( swap->I.iambob != 0 && swap->bobdeposit.I.datalen == 0 ) //swap->bobpayment.I.datalen == 0 - { - printf("BOB's error %d %d %d\n",swap->myfee.I.datalen,swap->bobpayment.I.datalen,swap->bobdeposit.I.datalen); - retval = -7; - } - } - while ( swap->aborted == 0 && retval == 0 && basilisk_swapiteration(swap,data,maxlen) == 0 ) - { - if ( swap->I.statebits == savestatebits && swap->I.otherstatebits == saveotherbits ) - sleep(DEX_SLEEP + (swap->I.iambob == 0)*1); - savestatebits = swap->I.statebits; - saveotherbits = swap->I.otherstatebits; - basilisk_sendstate(swap,data,maxlen); - basilisk_swapget(swap,0x80000000,data,maxlen,basilisk_verify_otherstatebits); - basilisk_swap_saveupdate(swap); - if ( time(NULL) > swap->I.expiration ) - break; - } - if ( swap->I.iambob != 0 && swap->bobdeposit.I.datalen != 0 && bits256_nonz(swap->bobdeposit.I.actualtxid) != 0 ) - { - printf("BOB waiting for confirm state.%x\n",swap->I.statebits); - sleep(60); // wait for confirm/propagation of msig - printf("BOB reclaims refund\n"); - basilisk_bobdeposit_refund(swap,0); - if ( LP_swapdata_rawtxsend(swap,0,data,maxlen,&swap->bobrefund,0x40000000,0) == 0 ) // use secretBn - { - printf("Bob submit error getting refund of deposit\n"); - } - else - { - // maybe wait for bobrefund to be confirmed - for (j=datalen=0; j<32; j++) - data[datalen++] = swap->I.privBn.bytes[j]; - LP_swapsend(swap,0x40000000,data,datalen,0x40000000,swap->I.crcs_mypriv); - } - basilisk_swap_saveupdate(swap); - } - if ( retval != 0 ) - basilisk_swap_sendabort(swap); - printf("end of atomic swap\n"); - if ( swapcompleted(swap) > 0 ) // only if swap completed - { - if ( swap->I.iambob != 0 ) - tradebot_pendingadd(swapjson(swap),swap->I.req.src,dstr(swap->I.req.srcamount),swap->I.req.dest,dstr(swap->I.req.destamount)); - else tradebot_pendingadd(swapjson(swap),swap->I.req.dest,dstr(swap->I.req.destamount),swap->I.req.src,dstr(swap->I.req.srcamount)); - } - printf("%s swap finished statebits %x\n",swap->I.iambob!=0?"BOB":"ALICE",swap->I.statebits); - //basilisk_swap_purge(swap); - free(data); -} -#endif - bits256 instantdex_derivekeypair(void *ctx,bits256 *newprivp,uint8_t pubkey[33],bits256 privkey,bits256 orderhash) { bits256 sharedsecret; From 3472fab226e87b30879dad9103bca882bd2c644b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 17:14:29 +0300 Subject: [PATCH 214/339] Test --- iguana/exchanges/LP_swap.c | 2 ++ iguana/exchanges/LP_transaction.c | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 55b48fe66..46d05fbe7 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -555,6 +555,7 @@ void LP_bobloop(void *_utxo) printf("%s\n",retstr); free(retstr); } + sleep(10); } } basilisk_swap_finished(swap); @@ -601,6 +602,7 @@ void LP_aliceloop(void *_qp) printf("%s\n",retstr); free(retstr); } + sleep(10); } } basilisk_swap_finished(swap); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 29cda5305..f51844127 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1432,7 +1432,7 @@ int32_t LP_rawtx_spendscript(struct basilisk_swap *swap,int32_t height,struct ba if ( (txobj= bitcoin_data2json(rawtx->coin->pubtype,rawtx->coin->p2shtype,rawtx->coin->isPoS,height,&rawtx->I.signedtxid,&rawtx->msgtx,rawtx->extraspace,sizeof(rawtx->extraspace),data,datalen,0,suppress_pubkeys)) != 0 ) { rawtx->I.actualtxid = rawtx->I.signedtxid; - //char str[65]; printf("got txid.%s (%s)\n",bits256_str(str,rawtx->signedtxid),jprint(txobj,0)); + char str[65]; printf("got %s txid.%s (%s)\n",rawtx->name,bits256_str(str,rawtx->I.signedtxid),jprint(txobj,0)); rawtx->I.locktime = rawtx->msgtx.lock_time; if ( (vouts= jarray(&n,txobj,"vout")) != 0 && v < n ) { @@ -1484,7 +1484,7 @@ int32_t LP_verify_bobdeposit(struct basilisk_swap *swap,uint8_t *data,int32_t da printf(" <- aliceclaim\n"); //basilisk_txlog(swap,&swap->aliceclaim,swap->I.putduration+swap->I.callduration); return(retval); - } + } else printf("error signing aliceclaim\n"); } printf("error with bobdeposit\n"); return(-1); @@ -1500,7 +1500,8 @@ int32_t LP_verify_alicepayment(struct basilisk_swap *swap,uint8_t *data,int32_t basilisk_dontforget_update(swap,&swap->alicepayment); return(0); } - else return(-1); + printf("error validating alicepayment\n"); + return(-1); } int32_t LP_verify_bobpayment(struct basilisk_swap *swap,uint8_t *data,int32_t datalen) @@ -1532,5 +1533,6 @@ int32_t LP_verify_bobpayment(struct basilisk_swap *swap,uint8_t *data,int32_t da return(retval); } } + printf("error validating bobpayment\n"); return(-1); } From 38d1b559356aea6d1f1cca5945259f4f48ae9381 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 17:16:37 +0300 Subject: [PATCH 215/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index ca22231aa..46f5cb8ee 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -418,7 +418,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin privkey = LP_privkey(utxo->coinaddr); if ( bits256_nonz(utxo->mypub) == 0 ) utxo->mypub = LP_pubkey(privkey); - if ( bits256_nonz(privkey) != 0 && Q.timestamp == utxo->swappending-LP_RESERVETIME && Q.quotetime >= Q.timestamp && Q.quotetime < utxo->swappending && bits256_cmp(utxo->mypub,Q.srchash) == 0 && (destvalue= LP_txvalue(rel,Q.desttxid,Q.destvout)) >= price*Q.satoshis+Q.desttxfee && destvalue >= Q.destsatoshis+Q.desttxfee ) + if ( bits256_nonz(privkey) != 0 && Q.timestamp == utxo->swappending-LP_RESERVETIME && Q.quotetime >= Q.timestamp-3 && Q.quotetime < utxo->swappending && bits256_cmp(utxo->mypub,Q.srchash) == 0 && (destvalue= LP_txvalue(rel,Q.desttxid,Q.destvout)) >= price*Q.satoshis+Q.desttxfee && destvalue >= Q.destsatoshis+Q.desttxfee ) { Q.change = destvalue - (Q.destsatoshis+Q.desttxfee); nanomsg_tcpname(pairstr,mypeer->ipaddr,10000+(rand() % 10000)); From 399475b7f439a24b292ad96d2b09edfb3569546e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 17:19:59 +0300 Subject: [PATCH 216/339] Test --- iguana/exchanges/LP_network.c | 1 + iguana/exchanges/LP_swap.c | 1 + 2 files changed, 2 insertions(+) diff --git a/iguana/exchanges/LP_network.c b/iguana/exchanges/LP_network.c index 45d25e403..faf6137f3 100644 --- a/iguana/exchanges/LP_network.c +++ b/iguana/exchanges/LP_network.c @@ -163,6 +163,7 @@ uint32_t LP_swapsend(int32_t pairsock,struct basilisk_swap *swap,uint32_t msgbit { } } + printf("sent %d bytes\n",sentbytes); //else printf("send.[%d] %x offset.%d datalen.%d [%llx]\n",sentbytes,msgbits,offset,datalen,*(long long *)data); free(buf); return(nextbits); diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 46d05fbe7..4bfb92b1a 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -406,6 +406,7 @@ int32_t LP_waitfor(int32_t pairsock,struct basilisk_swap *swap,int32_t timeout,i { if ( (datalen= nn_recv(pairsock,&data,NN_MSG,0)) >= 0 ) { + printf("received %d bytes\n",datalen); retval = (*verify)(swap,data,datalen); nn_freemsg(data); return(retval); From 87f3c78047258ac3496de02afc3f4612bf3b1489 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 17:25:15 +0300 Subject: [PATCH 217/339] Test --- iguana/exchanges/LP_swap.c | 2 +- iguana/exchanges/LP_transaction.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 4bfb92b1a..ed3191e13 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -487,7 +487,7 @@ uint32_t LP_swapdata_rawtxsend(int32_t pairsock,struct basilisk_swap *swap,uint3 rawtx->I.actualtxid = LP_broadcast_tx(rawtx->name,rawtx->coin->symbol,rawtx->txbytes,rawtx->I.datalen); if ( bits256_cmp(rawtx->I.actualtxid,rawtx->I.signedtxid) != 0 ) { - printf("%s rawtxsend %s vs %s\n",rawtx->name,bits256_str(str,rawtx->I.signedtxid),bits256_str(str2,rawtx->I.actualtxid)); + printf("%s rawtxsend.[%d] %s vs %s\n",rawtx->name,rawtx->I.datalen,bits256_str(str,rawtx->I.signedtxid),bits256_str(str2,rawtx->I.actualtxid)); rawtx->I.actualtxid = rawtx->I.signedtxid; } if ( bits256_nonz(rawtx->I.actualtxid) != 0 && msgbits != 0 ) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index f51844127..c6de0c949 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1401,8 +1401,11 @@ int32_t LP_rawtx_spendscript(struct basilisk_swap *swap,int32_t height,struct ba int32_t datalen=0,retval=-1,hexlen,n; uint8_t *data; cJSON *txobj,*skey,*vouts,*vout; char *hexstr; bits256 txid; datalen = recvbuf[0]; datalen += (int32_t)recvbuf[1] << 8; - if ( datalen > 65536 ) + if ( datalen > 1024 ) + { + printf("LP_rawtx_spendscript %s datalen.%d too big\n",rawtx->name,datalen); return(-1); + } rawtx->I.redeemlen = recvbuf[2]; data = &recvbuf[3]; if ( rawtx->I.redeemlen > 0 && rawtx->I.redeemlen < 0x100 ) From de4837a6e8e680b64d7d32ab8d244772fead97d1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 17:27:29 +0300 Subject: [PATCH 218/339] test --- iguana/exchanges/LP_swap.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index ed3191e13..9acbc9f67 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -496,6 +496,7 @@ uint32_t LP_swapdata_rawtxsend(int32_t pairsock,struct basilisk_swap *swap,uint3 sendbuf[sendlen++] = rawtx->I.datalen & 0xff; sendbuf[sendlen++] = (rawtx->I.datalen >> 8) & 0xff; sendbuf[sendlen++] = rawtx->I.redeemlen; + printf("datalen.%d redeemlen.%d\n",rawtx->I.datalen,rawtx->I.redeemlen); memcpy(&sendbuf[sendlen],rawtx->txbytes,rawtx->I.datalen), sendlen += rawtx->I.datalen; if ( rawtx->I.redeemlen > 0 && rawtx->I.redeemlen < 0x100 ) { From ad8e91a832b4c68906853a96bfcbadd932bc137e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 17:33:18 +0300 Subject: [PATCH 219/339] Test --- iguana/exchanges/LP_transaction.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index c6de0c949..c9f50cfda 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1398,16 +1398,22 @@ int32_t LP_verify_otherfee(struct basilisk_swap *swap,uint8_t *data,int32_t data 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) { - int32_t datalen=0,retval=-1,hexlen,n; uint8_t *data; cJSON *txobj,*skey,*vouts,*vout; char *hexstr; bits256 txid; - datalen = recvbuf[0]; - datalen += (int32_t)recvbuf[1] << 8; + bits256 otherhash,myhash,txid; int32_t i,offset=0,datalen=0,retval=-1,hexlen,n; uint8_t *data; cJSON *txobj,*skey,*vouts,*vout; char *hexstr; uint32_t quoteid,msgbits; + for (i=0; i<32; i++) + otherhash.bytes[i] = recvbuf[offset++]; + for (i=0; i<32; i++) + myhash.bytes[i] = recvbuf[offset++]; + offset += iguana_rwnum(0,&recvbuf[offset],sizeof(quoteid),"eid); + offset += iguana_rwnum(0,&recvbuf[offset],sizeof(msgbits),&msgbits); + datalen = recvbuf[offset++]; + datalen += (int32_t)recvbuf[offset++] << 8; if ( datalen > 1024 ) { printf("LP_rawtx_spendscript %s datalen.%d too big\n",rawtx->name,datalen); return(-1); } - rawtx->I.redeemlen = recvbuf[2]; - data = &recvbuf[3]; + rawtx->I.redeemlen = recvbuf[offset++]; + data = &recvbuf[offset++]; if ( rawtx->I.redeemlen > 0 && rawtx->I.redeemlen < 0x100 ) memcpy(rawtx->redeemscript,&data[datalen],rawtx->I.redeemlen); //printf("recvlen.%d datalen.%d redeemlen.%d\n",recvlen,datalen,rawtx->redeemlen); From f6fbdba6b44aeca77d92cd62cf39bd71b06ee7ba Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 17:55:30 +0300 Subject: [PATCH 220/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index f651df270..bc691ce86 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -334,7 +334,7 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 portable_mutex_unlock(&LP_utxomutex); if ( mypubsock >= 0 ) LP_send(mypubsock,jprint(LP_utxojson(utxo),1),1); - printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0); + char str[65]; printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d %s\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0,bits256_str(str,utxo->txid)); } return(utxo); } From 8d39f92ae210f0176c93a973c99d1d41fd5a7444 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 17:57:57 +0300 Subject: [PATCH 221/339] Test --- iguana/exchanges/LP_nativeDEX.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index bc691ce86..d4197a969 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -820,6 +820,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb { value = values[i]; values[i] = 0, used++; + printf("addutxo.(%s)\n",jprint(item,0)); if ( amclient == 0 ) LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin); else From 0aaea83995f9e3b214343777f003953e879eccf9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 18:00:52 +0300 Subject: [PATCH 222/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index d4197a969..ff9efa048 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -820,7 +820,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb { value = values[i]; values[i] = 0, used++; - printf("addutxo.(%s)\n",jprint(item,0)); + char str[65],str2[65]; printf("addutxo.(%s) %s %s\n",jprint(item,0),bits256_str(str,txid),bits256_str(str2,jbits256(item,"txid2"))); if ( amclient == 0 ) LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin); else From b12e3cd10d15e5c3ef9c7f1b513fe9ab7950d541 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 18:09:20 +0300 Subject: [PATCH 223/339] Test --- iguana/exchanges/LP_nativeDEX.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index ff9efa048..eadcee992 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -292,7 +292,7 @@ struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32 struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *coin,bits256 txid,int32_t vout,int64_t satoshis,bits256 deposittxid,int32_t depositvout,int64_t depositsatoshis,char *spendscript,char *coinaddr,char *ipaddr,uint16_t port,double profitmargin) { - struct LP_utxoinfo *utxo = 0; uint8_t key[sizeof(txid) + sizeof(vout)]; + struct LP_utxoinfo *utxo = 0; if ( coin == 0 || coin[0] == 0 || spendscript == 0 || spendscript[0] == 0 || coinaddr == 0 || coinaddr[0] == 0 || bits256_nonz(txid) == 0 || bits256_nonz(deposittxid) == 0 || vout < 0 || depositvout < 0 || satoshis <= 0 || depositsatoshis <= 0 ) { printf("malformed addutxo %d %d %d %d %d %d %d %d %d\n", coin == 0,spendscript == 0,coinaddr == 0,bits256_nonz(txid) == 0,bits256_nonz(deposittxid) == 0,vout < 0,depositvout < 0,satoshis <= 0,depositsatoshis <= 0); @@ -324,17 +324,16 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 utxo->txid2 = deposittxid; utxo->vout2 = depositvout; utxo->satoshis2 = depositsatoshis; - memcpy(key,txid.bytes,sizeof(txid)); - memcpy(&key[sizeof(txid)],&vout,sizeof(vout)); - memcpy(utxo->key,key,sizeof(key)); + memcpy(utxo->key,txid.bytes,sizeof(txid)); + memcpy(&utxo->key[sizeof(txid)],&vout,sizeof(vout)); portable_mutex_lock(&LP_utxomutex); - HASH_ADD_KEYPTR(hh,LP_utxoinfos,utxo->key,sizeof(key),utxo); + HASH_ADD_KEYPTR(hh,LP_utxoinfos,utxo->key,sizeof(utxo->key),utxo); if ( mypeer != 0 ) mypeer->numutxos++; portable_mutex_unlock(&LP_utxomutex); if ( mypubsock >= 0 ) LP_send(mypubsock,jprint(LP_utxojson(utxo),1),1); - char str[65]; printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d %s\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0,bits256_str(str,utxo->txid)); + char str[65],str2[65]; printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d %s %s\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0,bits256_str(str,utxo->txid),bits256_str(str2,txid)); } return(utxo); } @@ -820,9 +819,12 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb { value = values[i]; values[i] = 0, used++; - char str[65],str2[65]; printf("addutxo.(%s) %s %s\n",jprint(item,0),bits256_str(str,txid),bits256_str(str2,jbits256(item,"txid2"))); + char str[65],str2[65]; printf("addutxo.(%s) %s %s\n",jprint(item,0),bits256_str(str,txid),bits256_str(str2,deposittxid)); if ( amclient == 0 ) - LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin); + { + if ( (utxo= LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin)) != 0 ) + utxo->mypub = curve25519(privkey,curve25519_basepoint9()); + } else { if ( (utxo= LP_addutxo(amclient,mypeer,mypubsock,symbol,deposittxid,depositvout,depositval,txid,vout,value,script,coinaddr,"127.0.0.1",0,0)) != 0 ) From 2e5cd278ab85913825cf7ed6919d52a8ae0dbd6e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 18:12:33 +0300 Subject: [PATCH 224/339] Test --- iguana/exchanges/LP_nativeDEX.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index eadcee992..3c6320504 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -802,6 +802,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb { item = jitem(array,i); deposittxid = jbits256(item,"txid"); + char str[65],str2[65]; printf("addutxo.(%s) %s\n",jprint(item,0),bits256_str(str,deposittxid)); depositvout = juint(item,"vout"); script = jstr(item,"scriptPubKey"); depositval = values[i]; @@ -819,7 +820,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb { value = values[i]; values[i] = 0, used++; - char str[65],str2[65]; printf("addutxo.(%s) %s %s\n",jprint(item,0),bits256_str(str,txid),bits256_str(str2,deposittxid)); + printf("addutxo.(%s) %s %s\n",jprint(item,0),bits256_str(str,txid),bits256_str(str2,deposittxid)); if ( amclient == 0 ) { if ( (utxo= LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin)) != 0 ) From d66a5ac9a786632d0c462715cf03a4a28fd40a89 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 18:15:57 +0300 Subject: [PATCH 225/339] Test --- iguana/exchanges/LP_nativeDEX.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 3c6320504..7fc8a2733 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -802,7 +802,6 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb { item = jitem(array,i); deposittxid = jbits256(item,"txid"); - char str[65],str2[65]; printf("addutxo.(%s) %s\n",jprint(item,0),bits256_str(str,deposittxid)); depositvout = juint(item,"vout"); script = jstr(item,"scriptPubKey"); depositval = values[i]; @@ -810,17 +809,17 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb if ( amclient != 0 ) targetval = (depositval / 776) + 50000; else targetval = (depositval / 9) * 8; - //printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); + printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); if ( (i= LP_nearestvalue(values,n,targetval)) >= 0 ) { item = jitem(array,i); txid = jbits256(item,"txid"); vout = juint(item,"vout"); + printf("j.%d %.8f target %.8f\n",i,dstr(values[i]),dstr(targetval)); if ( jstr(item,"scriptPubKey") != 0 && strcmp(script,jstr(item,"scriptPubKey")) == 0 ) { value = values[i]; values[i] = 0, used++; - printf("addutxo.(%s) %s %s\n",jprint(item,0),bits256_str(str,txid),bits256_str(str2,deposittxid)); if ( amclient == 0 ) { if ( (utxo= LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin)) != 0 ) From 9ef56e6143bfc3d6e022aa6e57c21dc8fc963bdb Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 18:22:33 +0300 Subject: [PATCH 226/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 7fc8a2733..98c509d7b 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -518,7 +518,7 @@ void LP_utxosquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock { if ( i++ < firsti ) continue; - if ( utxo->lasttime != now ) + if ( utxo->lasttime != now && strcmp(utxo->ipaddr,"127.0.0.1") != 0 ) { char str[65]; printf("{%s:%u %s} ",utxo->ipaddr,utxo->port,bits256_str(str,utxo->txid)); flag++; From 1e1c4e3098be3d2c065f650dd3354ee503ee9ae7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 18:30:44 +0300 Subject: [PATCH 227/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 98c509d7b..cfedfab1e 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -410,8 +410,8 @@ int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs { if ( destpeer->numutxos < n ) { - destpeer->numutxos = n; - printf("got.(%s) from %s numutxos.%d\n",retstr,destpeer->ipaddr,destpeer->numutxos); + //destpeer->numutxos = n; + //printf("got.(%s) from %s numutxos.%d\n",retstr,destpeer->ipaddr,destpeer->numutxos); } } } From c87311d4155022e444749ea4f1c1a6b680ff321e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 18:38:19 +0300 Subject: [PATCH 228/339] Test --- iguana/exchanges/LP_nativeDEX.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index cfedfab1e..d846074f4 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -298,6 +298,11 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 printf("malformed addutxo %d %d %d %d %d %d %d %d %d\n", coin == 0,spendscript == 0,coinaddr == 0,bits256_nonz(txid) == 0,bits256_nonz(deposittxid) == 0,vout < 0,depositvout < 0,satoshis <= 0,depositsatoshis <= 0); return(0); } + if ( amclient == 0 && strcmp(ipaddr,"127.0.0.1") == 0 ) + { + printf("LP node got localhost utxo\n"); + return(0); + } if ( (utxo= LP_utxofind(txid,vout)) != 0 ) { if ( bits256_cmp(txid,utxo->txid) != 0 || bits256_cmp(deposittxid,utxo->txid2) != 0 || vout != utxo->vout || satoshis != utxo->satoshis || depositvout != utxo->vout2 || depositsatoshis != utxo->satoshis2 || strcmp(coin,utxo->coin) != 0 || strcmp(spendscript,utxo->spendscript) != 0 || strcmp(coinaddr,utxo->coinaddr) != 0 || strcmp(ipaddr,utxo->ipaddr) != 0 || port != utxo->port ) From 47d38bd91cf8a3e10ed33db0e2651cb355ecc802 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 18:43:35 +0300 Subject: [PATCH 229/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index d846074f4..c056d38d8 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -935,7 +935,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i if ( strcmp(peer->ipaddr,mypeer->ipaddr) != 0 ) LP_peersquery(amclient,mypeer,pubsock,peer->ipaddr,peer->port,mypeer->ipaddr,myport,profitmargin); } - if ( peer->numutxos != mypeer->numutxos ) + if ( peer->numutxos > mypeer->numutxos ) { lastn = peer->numutxos - mypeer->numutxos + LP_PROPAGATION_SLACK; if ( lastn < 0 ) From c28396d4b592587709eb8584f978616a1fd0c713 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 18:57:30 +0300 Subject: [PATCH 230/339] Test --- iguana/exchanges/LP_swap.c | 5 ++--- iguana/exchanges/LP_transaction.c | 26 ++++++++++++++------------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 9acbc9f67..01aa3d3e2 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -483,11 +483,10 @@ uint32_t LP_swapdata_rawtxsend(int32_t pairsock,struct basilisk_swap *swap,uint3 { if ( bits256_nonz(rawtx->I.signedtxid) != 0 && bits256_nonz(rawtx->I.actualtxid) == 0 ) { - char str[65],str2[65]; rawtx->I.actualtxid = LP_broadcast_tx(rawtx->name,rawtx->coin->symbol,rawtx->txbytes,rawtx->I.datalen); if ( bits256_cmp(rawtx->I.actualtxid,rawtx->I.signedtxid) != 0 ) { - printf("%s rawtxsend.[%d] %s vs %s\n",rawtx->name,rawtx->I.datalen,bits256_str(str,rawtx->I.signedtxid),bits256_str(str2,rawtx->I.actualtxid)); + //printf("%s rawtxsend.[%d] %s vs %s\n",rawtx->name,rawtx->I.datalen,bits256_str(str,rawtx->I.signedtxid),bits256_str(str2,rawtx->I.actualtxid)); rawtx->I.actualtxid = rawtx->I.signedtxid; } if ( bits256_nonz(rawtx->I.actualtxid) != 0 && msgbits != 0 ) @@ -496,7 +495,7 @@ uint32_t LP_swapdata_rawtxsend(int32_t pairsock,struct basilisk_swap *swap,uint3 sendbuf[sendlen++] = rawtx->I.datalen & 0xff; sendbuf[sendlen++] = (rawtx->I.datalen >> 8) & 0xff; sendbuf[sendlen++] = rawtx->I.redeemlen; - printf("datalen.%d redeemlen.%d\n",rawtx->I.datalen,rawtx->I.redeemlen); + //printf("datalen.%d redeemlen.%d\n",rawtx->I.datalen,rawtx->I.redeemlen); memcpy(&sendbuf[sendlen],rawtx->txbytes,rawtx->I.datalen), sendlen += rawtx->I.datalen; if ( rawtx->I.redeemlen > 0 && rawtx->I.redeemlen < 0x100 ) { diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index c9f50cfda..78be11aaf 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1473,7 +1473,7 @@ int32_t LP_rawtx_spendscript(struct basilisk_swap *swap,int32_t height,struct ba int32_t LP_verify_bobdeposit(struct basilisk_swap *swap,uint8_t *data,int32_t datalen) { - uint8_t userdata[512]; int32_t i,retval,len = 0; static bits256 zero; + uint8_t userdata[512]; int32_t i,retval=-1,len = 0; static bits256 zero; if ( LP_rawtx_spendscript(swap,swap->bobcoin.longestchain,&swap->bobdeposit,0,data,datalen,0) == 0 ) { swap->bobdeposit.I.signedtxid = LP_broadcast_tx(swap->bobdeposit.name,swap->bobcoin.symbol,swap->bobdeposit.txbytes,swap->bobdeposit.I.datalen); @@ -1483,20 +1483,21 @@ int32_t LP_verify_bobdeposit(struct basilisk_swap *swap,uint8_t *data,int32_t da len = basilisk_swapuserdata(userdata,zero,1,swap->I.myprivs[0],swap->bobdeposit.redeemscript,swap->bobdeposit.I.redeemlen); memcpy(swap->I.userdata_aliceclaim,userdata,len); swap->I.userdata_aliceclaimlen = len; - if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->aliceclaim,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) + retval = 0; + //if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->aliceclaim,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) { 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"); + //for (i=0; ialiceclaim.I.datalen; i++) + // printf("%02x",swap->aliceclaim.txbytes[i]); + //printf(" <- aliceclaim\n"); //basilisk_txlog(swap,&swap->aliceclaim,swap->I.putduration+swap->I.callduration); return(retval); - } else printf("error signing aliceclaim\n"); + } //else printf("error signing aliceclaim\n"); } printf("error with bobdeposit\n"); - return(-1); + return(retval); } int32_t LP_verify_alicepayment(struct basilisk_swap *swap,uint8_t *data,int32_t datalen) @@ -1515,7 +1516,7 @@ int32_t LP_verify_alicepayment(struct basilisk_swap *swap,uint8_t *data,int32_t int32_t LP_verify_bobpayment(struct basilisk_swap *swap,uint8_t *data,int32_t datalen) { - uint8_t userdata[512]; int32_t i,retval,len = 0; bits256 revAm; + uint8_t userdata[512]; int32_t i,retval=-1,len = 0; bits256 revAm; memset(revAm.bytes,0,sizeof(revAm)); if ( LP_rawtx_spendscript(swap,swap->bobcoin.longestchain,&swap->bobpayment,0,data,datalen,0) == 0 ) { @@ -1528,15 +1529,16 @@ int32_t LP_verify_bobpayment(struct basilisk_swap *swap,uint8_t *data,int32_t da len = basilisk_swapuserdata(userdata,revAm,0,swap->I.myprivs[0],swap->bobpayment.redeemscript,swap->bobpayment.I.redeemlen); memcpy(swap->I.userdata_alicespend,userdata,len); swap->I.userdata_alicespendlen = len; + retval = 0; char str[65],str2[65]; printf("bobpaid privAm.(%s) myprivs[0].(%s)\n",bits256_str(str,swap->I.privAm),bits256_str(str2,swap->I.myprivs[0])); - if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->alicespend,&swap->bobpayment,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) + //if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->alicespend,&swap->bobpayment,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) { for (i=0; ibobpayment.I.datalen; i++) printf("%02x",swap->bobpayment.txbytes[i]); printf(" <- bobpayment\n"); - for (i=0; ialicespend.I.datalen; i++) - printf("%02x",swap->alicespend.txbytes[i]); - printf(" <- alicespend\n\n"); + //for (i=0; ialicespend.I.datalen; i++) + // printf("%02x",swap->alicespend.txbytes[i]); + //printf(" <- alicespend\n\n"); swap->I.alicespent = 1; //basilisk_txlog(swap,&swap->alicespend,-1); return(retval); From c0ae01e5a507feabbf1a56c571c93e1e586b4a92 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 18:58:55 +0300 Subject: [PATCH 231/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 46f5cb8ee..8c0ee8730 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -418,7 +418,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin privkey = LP_privkey(utxo->coinaddr); if ( bits256_nonz(utxo->mypub) == 0 ) utxo->mypub = LP_pubkey(privkey); - if ( bits256_nonz(privkey) != 0 && Q.timestamp == utxo->swappending-LP_RESERVETIME && Q.quotetime >= Q.timestamp-3 && Q.quotetime < utxo->swappending && bits256_cmp(utxo->mypub,Q.srchash) == 0 && (destvalue= LP_txvalue(rel,Q.desttxid,Q.destvout)) >= price*Q.satoshis+Q.desttxfee && destvalue >= Q.destsatoshis+Q.desttxfee ) + if ( bits256_nonz(privkey) != 0 && Q.quotetime >= Q.timestamp-3 && Q.quotetime < utxo->swappending && bits256_cmp(utxo->mypub,Q.srchash) == 0 && (destvalue= LP_txvalue(rel,Q.desttxid,Q.destvout)) >= price*Q.satoshis+Q.desttxfee && destvalue >= Q.destsatoshis+Q.desttxfee ) { Q.change = destvalue - (Q.destsatoshis+Q.desttxfee); nanomsg_tcpname(pairstr,mypeer->ipaddr,10000+(rand() % 10000)); From 238e74d443ed16fe7ade22cb68b403942ab2b0b9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 19:20:25 +0300 Subject: [PATCH 232/339] Test --- iguana/exchanges/LP_swap.c | 4 ++-- iguana/exchanges/mm.c | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 01aa3d3e2..72a1804d6 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -556,7 +556,7 @@ void LP_bobloop(void *_utxo) printf("%s\n",retstr); free(retstr); } - sleep(10); + sleep(100); } } basilisk_swap_finished(swap); @@ -603,7 +603,7 @@ void LP_aliceloop(void *_qp) printf("%s\n",retstr); free(retstr); } - sleep(10); + sleep(100); } } basilisk_swap_finished(swap); diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 4fc22eda1..f2dffc4de 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -829,6 +829,12 @@ int main(int argc, const char * argv[]) printf("error launching LP_main (%s)\n",jprint(retjson,0)); exit(-1); } else printf("(%s) launched.(%s)\n",argv[1],passphrase); + if ( (retstr= basilisk_swaplist()) != 0 ) + { + printf("%s\ngetchar to continue\n",retstr); + getchar(); + free(retstr); + } incr = 100.; while ( 1 ) { From dc3ae44f6828dd905f3ca4ecd79a55780551e7b8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 19:24:15 +0300 Subject: [PATCH 233/339] Test --- iguana/exchanges/LP_remember.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index faea5b665..db1f55f9b 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -559,13 +559,13 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( bits256_nonz(privkey) != 0 ) { privAm = privkey; - //printf("set privAm <- %s\n",bits256_str(str,privAm)); + printf("set privAm <- %s\n",bits256_str(str,privAm)); } privkey = jbits256(item,"privBn"); if ( bits256_nonz(privkey) != 0 ) { privBn = privkey; - //printf("set privBn <- %s\n",bits256_str(str,privBn)); + printf("set privBn <- %s\n",bits256_str(str,privBn)); } expiration = juint(item,"expiration"); state = jint(item,"state"); From 0475f88c370f52aaf9027375b276d77e4211fb19 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 19:42:42 +0300 Subject: [PATCH 234/339] Test --- iguana/exchanges/LP_transaction.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 78be11aaf..a63f971f7 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -637,6 +637,9 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch jaddnum(item,"vout",vout); bitcoin_address(tmpaddr,pubtype,pubkey33,33); bitcoin_addr2rmd160(&addrtype,rmd160,tmpaddr); + if ( addrtype == p2shtype ) + spendlen = bitcoin_p2shspend(spendscript,0,rmd160); + else spendlen = bitcoin_standardspend(spendscript,0,rmd160); /*int32_t i; for (i=0; i<33; i++) printf("%02x",pubkey33[i]); @@ -648,11 +651,8 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch for (i=0; i<20; i++) printf("%02x",rmd160[i]); printf(" <- vs direct calc\n");*/ - spendlen = bitcoin_standardspend(spendscript,0,rmd160); + //spendlen = bitcoin_standardspend(spendscript,0,rmd160); init_hexbytes_noT(hexstr,spendscript,spendlen); - //sobj = cJSON_CreateObject(); - //jaddstr(sobj,"hex",hexstr); - //jadd(item,"scriptPubKey",sobj); jaddstr(item,"scriptPubKey",hexstr); jaddnum(item,"suppress",suppress_pubkeys); jaddnum(item,"sequence",sequenceid); From 6ede6c7e9dea6398d3a574664c7ac2c47bace4ae Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 19:45:21 +0300 Subject: [PATCH 235/339] Test --- iguana/exchanges/LP_transaction.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index a63f971f7..72ff0e45d 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -637,9 +637,6 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch jaddnum(item,"vout",vout); bitcoin_address(tmpaddr,pubtype,pubkey33,33); bitcoin_addr2rmd160(&addrtype,rmd160,tmpaddr); - if ( addrtype == p2shtype ) - spendlen = bitcoin_p2shspend(spendscript,0,rmd160); - else spendlen = bitcoin_standardspend(spendscript,0,rmd160); /*int32_t i; for (i=0; i<33; i++) printf("%02x",pubkey33[i]); @@ -652,15 +649,16 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch printf("%02x",rmd160[i]); printf(" <- vs direct calc\n");*/ //spendlen = bitcoin_standardspend(spendscript,0,rmd160); - init_hexbytes_noT(hexstr,spendscript,spendlen); - jaddstr(item,"scriptPubKey",hexstr); - jaddnum(item,"suppress",suppress_pubkeys); - jaddnum(item,"sequence",sequenceid); if ( redeemlen != 0 ) { init_hexbytes_noT(hexstr,redeemscript,redeemlen); jaddstr(item,"redeemScript",hexstr); - } + spendlen = bitcoin_p2shspend(spendscript,0,rmd160); + } else spendlen = bitcoin_standardspend(spendscript,0,rmd160); + init_hexbytes_noT(hexstr,spendscript,spendlen); + jaddstr(item,"scriptPubKey",hexstr); + jaddnum(item,"suppress",suppress_pubkeys); + jaddnum(item,"sequence",sequenceid); jaddi(vins,item); jdelete(txobj,"vin"); jadd(txobj,"vin",vins); From c27a08b8c4d7ce2fa0e1c0b58a6b305443412f2e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 19:46:44 +0300 Subject: [PATCH 236/339] Test --- iguana/exchanges/LP_transaction.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 72ff0e45d..464fa16d1 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -654,6 +654,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch init_hexbytes_noT(hexstr,redeemscript,redeemlen); jaddstr(item,"redeemScript",hexstr); spendlen = bitcoin_p2shspend(spendscript,0,rmd160); + printf("P2SH path\n"); } else spendlen = bitcoin_standardspend(spendscript,0,rmd160); init_hexbytes_noT(hexstr,spendscript,spendlen); jaddstr(item,"scriptPubKey",hexstr); From d69dcef42da623d183fc56762e7c56cd668843a1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 19:48:38 +0300 Subject: [PATCH 237/339] Test --- iguana/exchanges/LP_transaction.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 464fa16d1..0a0a59cfb 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -635,8 +635,6 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch } jaddbits256(item,"txid",utxotxid); jaddnum(item,"vout",vout); - bitcoin_address(tmpaddr,pubtype,pubkey33,33); - bitcoin_addr2rmd160(&addrtype,rmd160,tmpaddr); /*int32_t i; for (i=0; i<33; i++) printf("%02x",pubkey33[i]); @@ -653,9 +651,16 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch { init_hexbytes_noT(hexstr,redeemscript,redeemlen); jaddstr(item,"redeemScript",hexstr); + calc_rmd160_sha256(rmd160,redeemscript,redeemlen); spendlen = bitcoin_p2shspend(spendscript,0,rmd160); printf("P2SH path\n"); - } else spendlen = bitcoin_standardspend(spendscript,0,rmd160); + } + else + { + bitcoin_address(tmpaddr,pubtype,pubkey33,33); + bitcoin_addr2rmd160(&addrtype,rmd160,tmpaddr); + spendlen = bitcoin_standardspend(spendscript,0,rmd160); + } init_hexbytes_noT(hexstr,spendscript,spendlen); jaddstr(item,"scriptPubKey",hexstr); jaddnum(item,"suppress",suppress_pubkeys); From 71d82f76bdc7f7c90909da0eb4b2286eac18b15d Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 20:08:44 +0300 Subject: [PATCH 238/339] Test --- iguana/exchanges/LP_remember.c | 22 +++++++++++------ iguana/exchanges/LP_transaction.c | 39 ++++++++++++++++--------------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index db1f55f9b..c9c766449 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -501,9 +501,10 @@ int32_t basilisk_swap_isfinished(int32_t iambob,bits256 *txids,int32_t *sentflag cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requestid,uint32_t quoteid) { static void *ctx; - FILE *fp; int32_t sentflags[sizeof(txnames)/sizeof(*txnames)],i,n,j,len,needflag,secretstart,redeemlen,addflag,origfinishedflag = 0,finishedflag = 0,iambob = -1; int64_t srcamount,destamount=0,value,values[sizeof(txnames)/sizeof(*txnames)]; uint8_t secretAm[20],secretAm256[32],secretBn[20],secretBn256[32],pubkey33[33],redeemscript[1024],userdata[1024]; uint32_t plocktime,dlocktime,expiration=0,r,q,state,otherstate; char *secretstr,*srcstr,*deststr,str[65],src[64],dest[64],fname[512],*fstr,*dest33,*symbol,*txname,*Adest,*Bdest,*AAdest,*ABdest,destaddr[64],Adestaddr[64],alicecoin[64],bobcoin[64],*txbytes[sizeof(txnames)/sizeof(*txnames)]; long fsize; cJSON *txobj,*item,*sentobj,*array; bits256 checktxid,txid,pubA0,pubB0,pubB1,privAm,privBn,paymentspent,Apaymentspent,depositspent,zero,privkey,rev,myprivs[2],txids[sizeof(txnames)/sizeof(*txnames)],signedtxid; struct iguana_info *bob=0,*alice=0; uint64_t txfee = 10000; + FILE *fp; int32_t sentflags[sizeof(txnames)/sizeof(*txnames)],i,n,j,len,needflag,secretstart,redeemlen,addflag,origfinishedflag = 0,finishedflag = 0,iambob = -1; int64_t srcamount,destamount=0,value,values[sizeof(txnames)/sizeof(*txnames)]; uint8_t secretAm[20],secretAm256[32],secretBn[20],secretBn256[32],pubkey33[33],redeemscript[1024],userdata[1024]; uint32_t plocktime,dlocktime,expiration=0,r,q,state,otherstate; char *secretstr,*srcstr,*deststr,str[65],src[64],dest[64],fname[512],*fstr,*dest33,*symbol,*txname,*Adest,*Bdest,*AAdest,*ABdest,destaddr[64],Adestaddr[64],alicepaymentaddr[64],bobpaymentaddr[64],bobdepositaddr[64],alicecoin[64],bobcoin[64],*txbytes[sizeof(txnames)/sizeof(*txnames)]; long fsize; cJSON *txobj,*item,*sentobj,*array; bits256 checktxid,txid,pubA0,pubB0,pubB1,privAm,privBn,paymentspent,Apaymentspent,depositspent,zero,privkey,rev,myprivs[2],txids[sizeof(txnames)/sizeof(*txnames)],signedtxid; struct iguana_info *bob=0,*alice=0; uint64_t txfee = 10000; if ( ctx == 0 ) ctx = bitcoin_ctx(); + bobpaymentaddr[0] = bobdepositaddr[0] = alicepaymentaddr[0] = 0; memset(values,0,sizeof(values)); memset(txids,0,sizeof(txids)); memset(secretAm,0,sizeof(secretAm)); @@ -640,6 +641,12 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( bits256_nonz(txid) == 0 ) continue; txids[i] = txid; + if ( jstr(txobj,"Apayment") != 0 ) + strcpy(alicepaymentaddr,jstr(txobj,"Apayment")); + if ( jstr(txobj,"Bpayment") != 0 ) + strcpy(bobpaymentaddr,jstr(txobj,"Bpayment")); + if ( jstr(txobj,"Bdeposit") != 0 ) + strcpy(bobdepositaddr,jstr(txobj,"Bdeposit")); if ( jobj(txobj,"tx") != 0 ) { txbytes[i] = clonestr(jstr(txobj,"tx")); @@ -680,6 +687,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti } else if ( finishedflag == 0 ) printf("%s not finished\n",fname); } + printf("alicepayment.%s bobpayment.%s bobdeposit.%s\n",alicepaymentaddr,bobpaymentaddr,bobdepositaddr); //printf("iambob.%d src.%s dest.%s bob.%s alice.%s pubA0.(%s)\n",iambob,src,dest,bobcoin,alicecoin,bits256_str(str,pubA0)); Adestaddr[0] = destaddr[0] = 0; Adest = Bdest = AAdest = ABdest = 0; @@ -748,7 +756,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); printf("alicespend len.%d redeemlen.%d\n",len,redeemlen); - if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0)) != 0 ) + if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0,bobpaymentaddr)) != 0 ) printf("alicespend.(%s)\n",txbytes[BASILISK_ALICESPEND]); } } @@ -773,7 +781,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( redeemlen > 0 ) { len = basilisk_swapuserdata(userdata,zero,1,myprivs[0],redeemscript,redeemlen); - if ( (txbytes[BASILISK_ALICECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"aliceclaim",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,0,pubkey33,0,expiration,&values[BASILISK_ALICECLAIM],0,0)) != 0 ) + if ( (txbytes[BASILISK_ALICECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"aliceclaim",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,0,pubkey33,0,expiration,&values[BASILISK_ALICECLAIM],0,0,bobdepositaddr)) != 0 ) printf("privBn.(%s) aliceclaim.(%s)\n",bits256_str(str,privBn),txbytes[BASILISK_ALICECLAIM]); } } @@ -795,7 +803,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti privBn = basilisk_swap_privBn_extract(&txids[BASILISK_BOBREFUND],bobcoin,txids[BASILISK_BOBDEPOSIT],privBn); if ( bits256_nonz(txids[BASILISK_ALICEPAYMENT]) != 0 && bits256_nonz(privAm) != 0 && bits256_nonz(privBn) != 0 ) { - if ( (txbytes[BASILISK_ALICERECLAIM]= basilisk_swap_Aspend("alicereclaim",alicecoin,alice->pubtype,alice->p2shtype,alice->isPoS,alice->wiftype,ctx,privAm,privBn,txids[BASILISK_ALICEPAYMENT],0,pubkey33,expiration,&values[BASILISK_ALICERECLAIM])) != 0 ) + if ( (txbytes[BASILISK_ALICERECLAIM]= basilisk_swap_Aspend("alicereclaim",alicecoin,alice->pubtype,alice->p2shtype,alice->isPoS,alice->wiftype,ctx,privAm,privBn,txids[BASILISK_ALICEPAYMENT],0,pubkey33,expiration,&values[BASILISK_ALICERECLAIM],alicepaymentaddr)) != 0 ) printf("privBn.(%s) alicereclaim.(%s)\n",bits256_str(str,privBn),txbytes[BASILISK_ALICERECLAIM]); } } @@ -825,7 +833,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti } if ( bits256_nonz(privAm) != 0 && bits256_nonz(privBn) != 0 ) { - if ( (txbytes[BASILISK_BOBSPEND]= basilisk_swap_Aspend("bobspend",alicecoin,alice->pubtype,alice->p2shtype,alice->isPoS,alice->wiftype,ctx,privAm,privBn,txids[BASILISK_ALICEPAYMENT],0,pubkey33,expiration,&values[BASILISK_BOBSPEND])) != 0 ) + if ( (txbytes[BASILISK_BOBSPEND]= basilisk_swap_Aspend("bobspend",alicecoin,alice->pubtype,alice->p2shtype,alice->isPoS,alice->wiftype,ctx,privAm,privBn,txids[BASILISK_ALICEPAYMENT],0,pubkey33,expiration,&values[BASILISK_BOBSPEND],alicepaymentaddr)) != 0 ) printf("bobspend.(%s)\n",txbytes[BASILISK_BOBSPEND]); } } @@ -849,7 +857,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( redeemlen > 0 ) { len = basilisk_swapuserdata(userdata,zero,1,myprivs[1],redeemscript,redeemlen); - if ( (txbytes[BASILISK_BOBRECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[1],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,0,expiration,&values[BASILISK_BOBRECLAIM],0,0)) != 0 ) + if ( (txbytes[BASILISK_BOBRECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[1],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,0,expiration,&values[BASILISK_BOBRECLAIM],0,0,bobpaymentaddr)) != 0 ) { int32_t z; for (z=0; z<20; z++) @@ -879,7 +887,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti vcalc_sha256(0,secretBn256,privBn.bytes,sizeof(privBn)); redeemlen = basilisk_swap_bobredeemscript(1,&secretstart,redeemscript,dlocktime,pubA0,pubB0,pubB1,privAm,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,privBn,0,myprivs[0],redeemscript,redeemlen); - if ( (txbytes[BASILISK_BOBREFUND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,0,pubkey33,1,expiration,&values[BASILISK_BOBREFUND],0,0)) != 0 ) + if ( (txbytes[BASILISK_BOBREFUND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,0,pubkey33,1,expiration,&values[BASILISK_BOBREFUND],0,0,bobdepositaddr)) != 0 ) printf("pubB1.(%s) bobrefund.(%s)\n",bits256_str(str,pubB1),txbytes[BASILISK_BOBREFUND]); } if ( txbytes[BASILISK_BOBREFUND] != 0 ) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 0a0a59cfb..e4722fd1f 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -580,7 +580,7 @@ int32_t iguana_signrawtransaction(uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS #endif -char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr) +char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr,char *vinaddr) { 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,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value,change = 0; *destamountp = 0; @@ -647,20 +647,17 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch printf("%02x",rmd160[i]); printf(" <- vs direct calc\n");*/ //spendlen = bitcoin_standardspend(spendscript,0,rmd160); + bitcoin_address(tmpaddr,pubtype,pubkey33,33); + bitcoin_addr2rmd160(&addrtype,rmd160,tmpaddr); if ( redeemlen != 0 ) { init_hexbytes_noT(hexstr,redeemscript,redeemlen); jaddstr(item,"redeemScript",hexstr); - calc_rmd160_sha256(rmd160,redeemscript,redeemlen); + if ( vinaddr != 0 ) + bitcoin_addr2rmd160(&addrtype,rmd160,vinaddr); spendlen = bitcoin_p2shspend(spendscript,0,rmd160); printf("P2SH path\n"); - } - else - { - bitcoin_address(tmpaddr,pubtype,pubkey33,33); - bitcoin_addr2rmd160(&addrtype,rmd160,tmpaddr); - spendlen = bitcoin_standardspend(spendscript,0,rmd160); - } + } else spendlen = bitcoin_standardspend(spendscript,0,rmd160); init_hexbytes_noT(hexstr,spendscript,spendlen); jaddstr(item,"scriptPubKey",hexstr); jaddnum(item,"suppress",suppress_pubkeys); @@ -707,7 +704,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch return(signedtx); } -int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pubkey33,int32_t iambob,int32_t lockinputs,struct basilisk_rawtx *rawtx,uint32_t locktime,uint8_t *script,int32_t scriptlen,int64_t txfee,int32_t minconf,int32_t delay,bits256 privkey,uint8_t *changermd160) +int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pubkey33,int32_t iambob,int32_t lockinputs,struct basilisk_rawtx *rawtx,uint32_t locktime,uint8_t *script,int32_t scriptlen,int64_t txfee,int32_t minconf,int32_t delay,bits256 privkey,uint8_t *changermd160,char *vinaddr) { int32_t retval=-1,len,iter; char *signedtx,*changeaddr = 0,_changeaddr[64]; struct iguana_info *coin; int64_t newtxfee=0,destamount; if ( (coin= rawtx->coin) == 0 ) @@ -721,7 +718,7 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub } for (iter=0; iter<2; iter++) { - if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,str,coin->symbol,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,ctx,privkey,0,0,0,0,0,rawtx->utxotxid,rawtx->utxovout,rawtx->I.destaddr,pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr)) != 0 ) + if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,str,coin->symbol,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,ctx,privkey,0,0,0,0,0,rawtx->utxotxid,rawtx->utxovout,rawtx->I.destaddr,pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr,vinaddr)) != 0 ) { rawtx->I.datalen = (int32_t)strlen(signedtx) >> 1; if ( rawtx->I.datalen <= sizeof(rawtx->txbytes) ) @@ -743,7 +740,7 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub return(retval); } -int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,struct basilisk_swap *swap,struct basilisk_rawtx *dest,struct basilisk_rawtx *rawtx,bits256 privkey,bits256 *privkey2,uint8_t *userdata,int32_t userdatalen,int32_t ignore_cltverr,uint8_t *changermd160) +int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,struct basilisk_swap *swap,struct basilisk_rawtx *dest,struct basilisk_rawtx *rawtx,bits256 privkey,bits256 *privkey2,uint8_t *userdata,int32_t userdatalen,int32_t ignore_cltverr,uint8_t *changermd160,char *vinaddr) { char *signedtx,*changeaddr = 0,_changeaddr[64]; int64_t txfee,newtxfee=0,destamount; uint32_t timestamp,locktime=0,sequenceid = 0xffffffff; int32_t iter,len,retval = -1; double estimatedrate; timestamp = swap->I.started; @@ -760,7 +757,7 @@ int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_ } for (iter=0; iter<2; iter++) { - if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,rawtx->name,symbol,pubtype,p2shtype,isPoS,wiftype,swap->ctx,privkey,privkey2,0,0,userdata,userdatalen,rawtx->utxotxid,rawtx->utxovout,dest->p2shaddr,rawtx->I.pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr)) != 0 ) + if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,rawtx->name,symbol,pubtype,p2shtype,isPoS,wiftype,swap->ctx,privkey,privkey2,0,0,userdata,userdatalen,rawtx->utxotxid,rawtx->utxovout,dest->p2shaddr,rawtx->I.pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr,vinaddr)) != 0 ) { rawtx->I.datalen = (int32_t)strlen(signedtx) >> 1; if ( rawtx->I.datalen <= sizeof(rawtx->txbytes) ) @@ -797,7 +794,7 @@ int32_t basilisk_alicescript(uint8_t *redeemscript,int32_t *redeemlenp,uint8_t * return(n); } -char *basilisk_swap_Aspend(char *name,char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,void *ctx,bits256 privAm,bits256 privBn,bits256 utxotxid,int32_t vout,uint8_t pubkey33[33],uint32_t expiration,int64_t *destamountp) +char *basilisk_swap_Aspend(char *name,char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,void *ctx,bits256 privAm,bits256 privBn,bits256 utxotxid,int32_t vout,uint8_t pubkey33[33],uint32_t expiration,int64_t *destamountp,char *vinaddr) { char msigaddr[64],*signedtx = 0; int32_t spendlen,redeemlen; uint8_t tmp33[33],redeemscript[512],spendscript[128]; bits256 pubAm,pubBn,signedtxid; uint64_t txfee; if ( bits256_nonz(privAm) != 0 && bits256_nonz(privBn) != 0 ) @@ -816,7 +813,7 @@ char *basilisk_swap_Aspend(char *name,char *symbol,uint8_t pubtype,uint8_t p2sht for (i=0; i<32; i++) privBn.bytes[i] = rev.bytes[31 - i];*/ txfee = LP_txfee(symbol); - signedtx = basilisk_swap_bobtxspend(&signedtxid,txfee,name,symbol,pubtype,p2shtype,isPoS,wiftype,ctx,privAm,&privBn,redeemscript,redeemlen,0,0,utxotxid,vout,0,pubkey33,1,expiration,destamountp,0,0); + signedtx = basilisk_swap_bobtxspend(&signedtxid,txfee,name,symbol,pubtype,p2shtype,isPoS,wiftype,ctx,privAm,&privBn,redeemscript,redeemlen,0,0,utxotxid,vout,0,pubkey33,1,expiration,destamountp,0,0,vinaddr); } return(signedtx); } @@ -1157,6 +1154,7 @@ int32_t basilisk_swapuserdata(uint8_t *userdata,bits256 privkey,int32_t ifpath,b return(len); } +#ifdef old /*Bob paytx: OP_IF OP_CLTV OP_DROP OP_CHECKSIG @@ -1198,6 +1196,7 @@ int32_t basilisk_bobdeposit_refund(struct basilisk_swap *swap,int32_t delay) } return(-1); } +#endif int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,int32_t genflag) { @@ -1218,7 +1217,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i { //if ( swap->bobpayment.txbytes != 0 && swap->bobpayment.I.spendlen != 0 ) // break; - basilisk_rawtx_gen(swap->ctx,"payment",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobpayment,swap->bobpayment.I.locktime,swap->bobpayment.spendscript,swap->bobpayment.I.spendlen,swap->bobpayment.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160); + basilisk_rawtx_gen(swap->ctx,"payment",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobpayment,swap->bobpayment.I.locktime,swap->bobpayment.spendscript,swap->bobpayment.I.spendlen,swap->bobpayment.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0); if ( swap->bobpayment.I.spendlen == 0 || swap->bobpayment.I.datalen == 0 ) { printf("error bob generating %p payment.%d\n",swap->bobpayment.txbytes,swap->bobpayment.I.spendlen); @@ -1252,7 +1251,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i { //if ( swap->bobdeposit.txbytes != 0 && swap->bobdeposit.I.spendlen != 0 ) // break; - basilisk_rawtx_gen(swap->ctx,"deposit",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobdeposit,swap->bobdeposit.I.locktime,swap->bobdeposit.spendscript,swap->bobdeposit.I.spendlen,swap->bobdeposit.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160); + basilisk_rawtx_gen(swap->ctx,"deposit",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobdeposit,swap->bobdeposit.I.locktime,swap->bobdeposit.spendscript,swap->bobdeposit.I.spendlen,swap->bobdeposit.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0); if ( swap->bobdeposit.I.datalen == 0 || swap->bobdeposit.I.spendlen == 0 ) { printf("error bob generating %p deposit.%d\n",swap->bobdeposit.txbytes,swap->bobdeposit.I.spendlen); @@ -1308,6 +1307,7 @@ void LP_swap_coinaddr(struct basilisk_swap *swap,struct iguana_info *coin,char * } } +#ifdef old int32_t basilisk_alicepayment_spend(struct basilisk_swap *swap,struct basilisk_rawtx *dest) { int32_t i,retval; @@ -1336,11 +1336,12 @@ int32_t basilisk_alicepayment_spend(struct basilisk_swap *swap,struct basilisk_r } return(-1); } +#endif void basilisk_alicepayment(struct basilisk_swap *swap,struct iguana_info *coin,struct basilisk_rawtx *alicepayment,bits256 pubAm,bits256 pubBn) { alicepayment->I.spendlen = basilisk_alicescript(alicepayment->redeemscript,&alicepayment->I.redeemlen,alicepayment->spendscript,0,alicepayment->I.destaddr,coin->p2shtype,pubAm,pubBn); - basilisk_rawtx_gen(swap->ctx,"alicepayment",swap->I.started,swap->persistent_pubkey33,0,1,alicepayment,alicepayment->I.locktime,alicepayment->spendscript,alicepayment->I.spendlen,coin->txfee,1,0,swap->persistent_privkey,swap->changermd160); + basilisk_rawtx_gen(swap->ctx,"alicepayment",swap->I.started,swap->persistent_pubkey33,0,1,alicepayment,alicepayment->I.locktime,alicepayment->spendscript,alicepayment->I.spendlen,coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0); } int32_t basilisk_alicetxs(int32_t pairsock,struct basilisk_swap *swap,uint8_t *data,int32_t maxlen) @@ -1370,7 +1371,7 @@ int32_t basilisk_alicetxs(int32_t pairsock,struct basilisk_swap *swap,uint8_t *d if ( swap->myfee.I.datalen == 0 ) { //printf("generate fee\n"); - if ( basilisk_rawtx_gen(swap->ctx,"myfee",swap->I.started,swap->persistent_pubkey33,swap->I.iambob,1,&swap->myfee,0,swap->myfee.spendscript,swap->myfee.I.spendlen,swap->myfee.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160) == 0 ) + if ( basilisk_rawtx_gen(swap->ctx,"myfee",swap->I.started,swap->persistent_pubkey33,swap->I.iambob,1,&swap->myfee,0,swap->myfee.spendscript,swap->myfee.I.spendlen,swap->myfee.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0) == 0 ) { swap->I.statebits |= LP_swapdata_rawtxsend(pairsock,swap,0x80,data,maxlen,&swap->myfee,0x40,0); LP_unspents_mark(swap->I.iambob!=0?swap->bobcoin.symbol:swap->alicecoin.symbol,swap->myfee.vins); From a572ba12d13308aaac09f6ea2e6af6fe6f203ff4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 20:14:47 +0300 Subject: [PATCH 239/339] Test --- iguana/exchanges/LP_transaction.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index e4722fd1f..0a9250112 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -656,7 +656,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch if ( vinaddr != 0 ) bitcoin_addr2rmd160(&addrtype,rmd160,vinaddr); spendlen = bitcoin_p2shspend(spendscript,0,rmd160); - printf("P2SH path\n"); + printf("P2SH path.%s\n",vinaddr!=0?vinaddr:0); } else spendlen = bitcoin_standardspend(spendscript,0,rmd160); init_hexbytes_noT(hexstr,spendscript,spendlen); jaddstr(item,"scriptPubKey",hexstr); @@ -692,7 +692,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch printf("couldnt sign transaction.%s %s\n",name,bits256_str(str,*signedtxidp)); else if ( completed == 0 ) { - printf("incomplete signing %s\n",name); + printf("incomplete signing %s (%s)\n",name,jprint(vins,0)); if ( signedtx != 0 ) free(signedtx), signedtx = 0; } else printf("%s -> %s\n",name,bits256_str(str,*signedtxidp)); From 4f5b42c5e94682d0adf3e85b78d575092599d68a Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 20:15:59 +0300 Subject: [PATCH 240/339] Test --- iguana/exchanges/LP_rpc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index d35568d2d..d3742e5e0 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -215,7 +215,8 @@ char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON * decode_hex(data,len>>1,hexstr); *signedtxidp = bits256_doublesha256(0,data,len >> 1); } - else printf("%s signrawtransaction.(%s) params.(%s)\n",coin->symbol,retstr,paramstr); + //else + printf("%s signrawtransaction.(%s) params.(%s)\n",coin->symbol,retstr,paramstr); free_json(json); } free(retstr); From fe55e3def76c8fbff23ed0b580113fe37ab56ed0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 20:43:34 +0300 Subject: [PATCH 241/339] Test --- iguana/exchanges/LP_remember.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index c9c766449..41f425b18 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -755,7 +755,12 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); - printf("alicespend len.%d redeemlen.%d\n",len,redeemlen); + { + char privaddr[64]; uint8_t privpub33[33]; + bitcoin_pubkey33(ctx,privpub33,myprivs[0]); + bitcoin_address(privaddr,60,privpub33,33); + printf("alicespend len.%d redeemlen.%d priv0addr.(%s)\n",len,redeemlen,privaddr); + } if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0,bobpaymentaddr)) != 0 ) printf("alicespend.(%s)\n",txbytes[BASILISK_ALICESPEND]); } From 351728c71323b6e7ef83911de0e3fd8197981210 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 20:56:17 +0300 Subject: [PATCH 242/339] Test --- iguana/exchanges/LP_transaction.c | 286 +++++++++++++++--------------- 1 file changed, 142 insertions(+), 144 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 0a9250112..3da97beb8 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -107,8 +107,6 @@ int32_t LP_numconfirms(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx) return(numconfirms); } - -#ifdef later int32_t iguana_msgtx_Vset(uint8_t *serialized,int32_t maxlen,struct iguana_msgtx *msgtx,struct vin_info *V) { int32_t vini,j,scriptlen,p2shlen,userdatalen,siglen,plen,need_op0=0,len = 0; uint8_t *script,*redeemscript=0,*userdata=0; struct vin_info *vp; @@ -204,139 +202,7 @@ int32_t iguana_msgtx_Vset(uint8_t *serialized,int32_t maxlen,struct iguana_msgtx return(len); } -int32_t bitcoin_verifyvins(uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,int32_t height,bits256 *signedtxidp,char **signedtx,struct iguana_msgtx *msgtx,uint8_t *serialized,int32_t maxlen,struct vin_info *V,uint32_t sighash,int32_t signtx,int32_t suppress_pubkeys) -{ - bits256 sigtxid; uint8_t *sig,*script; struct vin_info *vp; char vpnstr[64]; int32_t scriptlen,complete=0,j,vini=0,flag=0,siglen,numvouts,numsigs; - numvouts = msgtx->tx_out; - vpnstr[0] = 0; - *signedtx = 0; - memset(signedtxidp,0,sizeof(*signedtxidp)); - for (vini=0; vinitx_in; vini++) - { - if ( V->p2shscript[0] != 0 && V->p2shlen != 0 ) - { - script = V->p2shscript; - scriptlen = V->p2shlen; - //printf("V->p2shlen.%d\n",V->p2shlen); - } - else - { - script = msgtx->vins[vini].spendscript; - scriptlen = msgtx->vins[vini].spendlen; - } - sigtxid = bitcoin_sigtxid(pubtype,p2shtype,isPoS,height,serialized,maxlen,msgtx,vini,script,scriptlen,sighash,vpnstr,suppress_pubkeys); - if ( bits256_nonz(sigtxid) != 0 ) - { - vp = &V[vini]; - vp->sigtxid = sigtxid; - for (j=numsigs=0; jN; j++) - { - sig = vp->signers[j].sig; - siglen = vp->signers[j].siglen; - if ( signtx != 0 && bits256_nonz(vp->signers[j].privkey) != 0 ) - { - siglen = bitcoin_sign(swap->ctx,sig,sigtxid,vp->signers[j].privkey,0); - //if ( (plen= bitcoin_pubkeylen(vp->signers[j].pubkey)) <= 0 ) - bitcoin_pubkey33(swap->ctx,vp->signers[j].pubkey,vp->signers[j].privkey); - sig[siglen++] = sighash; - vp->signers[j].siglen = siglen; - /*char str[65]; printf("SIGTXID.(%s) ",bits256_str(str,sigtxid)); - int32_t i; for (i=0; isigners[j].pubkey[i]); - // s2 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1; - printf(" SIGNEDTX.[%02x] siglen.%d priv.%s\n",sig[siglen-1],siglen,bits256_str(str,vp->signers[j].privkey));*/ - } - if ( sig == 0 || siglen == 0 ) - { - memset(vp->signers[j].pubkey,0,sizeof(vp->signers[j].pubkey)); - continue; - } - if ( bitcoin_verify(sig,siglen-1,sigtxid,vp->signers[j].pubkey,bitcoin_pubkeylen(vp->signers[j].pubkey)) < 0 ) - { - int32_t k; for (k=0; ksigners[j].pubkey); k++) - printf("%02x",vp->signers[j].pubkey[k]); - printf(" SIG.%d.%d ERROR siglen.%d\n",vini,j,siglen); - } - else - { - flag++; - numsigs++; - /*int32_t z; - for (z=0; zsigners[j].pubkey[z]); - printf(" <- pub, SIG.%d.%d VERIFIED numsigs.%d vs M.%d\n",vini,j,numsigs,vp->M);*/ - } - } - if ( numsigs >= vp->M ) - complete = 1; - } - } - iguana_msgtx_Vset(serialized,maxlen,msgtx,V); - cJSON *txobj = cJSON_CreateObject(); - *signedtx = iguana_rawtxbytes(pubtype,p2shtype,isPoS,height,txobj,msgtx,suppress_pubkeys); - //printf("SIGNEDTX.(%s)\n",jprint(txobj,1)); - *signedtxidp = msgtx->txid; - return(complete); -} - -int32_t iguana_vininfo_create(uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t *serialized,int32_t maxsize,struct iguana_msgtx *msgtx,cJSON *vins,int32_t numinputs,struct vin_info *V) -{ - int32_t i,plen,finalized = 1,len = 0; struct vin_info *vp; //struct iguana_waccount *wacct; struct iguana_waddress *waddr; uint32_t sigsize,pubkeysize,p2shsize,userdatalen; - msgtx->tx_in = numinputs; - maxsize -= (sizeof(struct iguana_msgvin) * msgtx->tx_in); - msgtx->vins = (struct iguana_msgvin *)&serialized[maxsize]; - memset(msgtx->vins,0,sizeof(struct iguana_msgvin) * msgtx->tx_in); - if ( msgtx->tx_in > 0 && msgtx->tx_in*sizeof(struct iguana_msgvin) < maxsize ) - { - for (i=0; itx_in; i++) - { - vp = &V[i]; - //printf("VINS.(%s)\n",jprint(jitem(vins,i),0)); - len += iguana_parsevinobj(&serialized[len],maxsize,&msgtx->vins[i],jitem(vins,i),vp); - if ( msgtx->vins[i].sequence < IGUANA_SEQUENCEID_FINAL ) - finalized = 0; - if ( msgtx->vins[i].spendscript == 0 ) - { - /*if ( iguana_RTunspentindfind(coin,&outpt,vp->coinaddr,vp->spendscript,&vp->spendlen,&vp->amount,&vp->height,msgtx->vins[i].prev_hash,msgtx->vins[i].prev_vout,coin->bundlescount-1,0) == 0 ) - { - vp->unspentind = outpt.unspentind; - msgtx->vins[i].spendscript = vp->spendscript; - msgtx->vins[i].spendlen = vp->spendlen; - vp->hashtype = iguana_vinscriptparse(coin,vp,&sigsize,&pubkeysize,&p2shsize,&userdatalen,vp->spendscript,vp->spendlen); - vp->userdatalen = userdatalen; - printf("V %.8f (%s) spendscript.[%d] userdatalen.%d\n",dstr(vp->amount),vp->coinaddr,vp->spendlen,userdatalen); - }*/ - } - else - { - memcpy(vp->spendscript,msgtx->vins[i].spendscript,msgtx->vins[i].spendlen); - vp->spendlen = msgtx->vins[i].spendlen; - _iguana_calcrmd160(pubtype,p2shtype,vp); - if ( (plen= bitcoin_pubkeylen(vp->signers[0].pubkey)) > 0 ) - bitcoin_address(vp->coinaddr,pubtype,vp->signers[0].pubkey,plen); - } - if ( vp->M == 0 && vp->N == 0 ) - vp->M = vp->N = 1; - /*if ( vp->coinaddr[i] != 0 && (waddr= iguana_waddresssearch(&wacct,vp->coinaddr)) != 0 ) - { - vp->signers[0].privkey = waddr->privkey; - if ( (plen= bitcoin_pubkeylen(waddr->pubkey)) != vp->spendscript[1] || vp->spendscript[vp->spendlen-1] != 0xac ) - { - if ( plen > 0 && plen < sizeof(vp->signers[0].pubkey) ) - memcpy(vp->signers[0].pubkey,waddr->pubkey,plen); - } - }*/ - } - } - return(finalized); -} - +#ifdef adfafds void iguana_ensure_privkey(struct iguana_info *coin,bits256 privkey) { uint8_t pubkey33[33]; struct iguana_waccount *wacct; struct iguana_waddress *waddr,addr; char coinaddr[128]; @@ -427,6 +293,7 @@ int32_t iguana_interpreter(struct iguana_info *coin,cJSON *logarray,int64_t nLoc } return(0); } +#endif bits256 iguana_str2priv(char *str) { @@ -447,7 +314,140 @@ bits256 iguana_str2priv(char *str) return(privkey); } -int32_t iguana_signrawtransaction(uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,int32_t height,struct iguana_msgtx *msgtx,char **signedtxp,bits256 *signedtxidp,struct vin_info *V,int32_t numinputs,char *rawtx,cJSON *vins,cJSON *privkeysjson) +int32_t iguana_vininfo_create(uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t *serialized,int32_t maxsize,struct iguana_msgtx *msgtx,cJSON *vins,int32_t numinputs,struct vin_info *V) +{ + int32_t i,plen,finalized = 1,len = 0; struct vin_info *vp; //struct iguana_waccount *wacct; struct iguana_waddress *waddr; uint32_t sigsize,pubkeysize,p2shsize,userdatalen; + msgtx->tx_in = numinputs; + maxsize -= (sizeof(struct iguana_msgvin) * msgtx->tx_in); + msgtx->vins = (struct iguana_msgvin *)&serialized[maxsize]; + memset(msgtx->vins,0,sizeof(struct iguana_msgvin) * msgtx->tx_in); + if ( msgtx->tx_in > 0 && msgtx->tx_in*sizeof(struct iguana_msgvin) < maxsize ) + { + for (i=0; itx_in; i++) + { + vp = &V[i]; + //printf("VINS.(%s)\n",jprint(jitem(vins,i),0)); + len += iguana_parsevinobj(&serialized[len],maxsize,&msgtx->vins[i],jitem(vins,i),vp); + if ( msgtx->vins[i].sequence < IGUANA_SEQUENCEID_FINAL ) + finalized = 0; + if ( msgtx->vins[i].spendscript == 0 ) + { + /*if ( iguana_RTunspentindfind(coin,&outpt,vp->coinaddr,vp->spendscript,&vp->spendlen,&vp->amount,&vp->height,msgtx->vins[i].prev_hash,msgtx->vins[i].prev_vout,coin->bundlescount-1,0) == 0 ) + { + vp->unspentind = outpt.unspentind; + msgtx->vins[i].spendscript = vp->spendscript; + msgtx->vins[i].spendlen = vp->spendlen; + vp->hashtype = iguana_vinscriptparse(coin,vp,&sigsize,&pubkeysize,&p2shsize,&userdatalen,vp->spendscript,vp->spendlen); + vp->userdatalen = userdatalen; + printf("V %.8f (%s) spendscript.[%d] userdatalen.%d\n",dstr(vp->amount),vp->coinaddr,vp->spendlen,userdatalen); + }*/ + } + else + { + memcpy(vp->spendscript,msgtx->vins[i].spendscript,msgtx->vins[i].spendlen); + vp->spendlen = msgtx->vins[i].spendlen; + _iguana_calcrmd160(pubtype,p2shtype,vp); + if ( (plen= bitcoin_pubkeylen(vp->signers[0].pubkey)) > 0 ) + bitcoin_address(vp->coinaddr,pubtype,vp->signers[0].pubkey,plen); + } + if ( vp->M == 0 && vp->N == 0 ) + vp->M = vp->N = 1; + /*if ( vp->coinaddr[i] != 0 && (waddr= iguana_waddresssearch(&wacct,vp->coinaddr)) != 0 ) + { + vp->signers[0].privkey = waddr->privkey; + if ( (plen= bitcoin_pubkeylen(waddr->pubkey)) != vp->spendscript[1] || vp->spendscript[vp->spendlen-1] != 0xac ) + { + if ( plen > 0 && plen < sizeof(vp->signers[0].pubkey) ) + memcpy(vp->signers[0].pubkey,waddr->pubkey,plen); + } + }*/ + } + } + return(finalized); +} + +int32_t bitcoin_verifyvins(void *ctx,char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,int32_t height,bits256 *signedtxidp,char **signedtx,struct iguana_msgtx *msgtx,uint8_t *serialized,int32_t maxlen,struct vin_info *V,uint32_t sighash,int32_t signtx,int32_t suppress_pubkeys) +{ + bits256 sigtxid; uint8_t *sig,*script; struct vin_info *vp; char vpnstr[64]; int32_t scriptlen,complete=0,j,vini=0,flag=0,siglen,numvouts,numsigs; + numvouts = msgtx->tx_out; + vpnstr[0] = 0; + *signedtx = 0; + memset(signedtxidp,0,sizeof(*signedtxidp)); + for (vini=0; vinitx_in; vini++) + { + if ( V->p2shscript[0] != 0 && V->p2shlen != 0 ) + { + script = V->p2shscript; + scriptlen = V->p2shlen; + //printf("V->p2shlen.%d\n",V->p2shlen); + } + else + { + script = msgtx->vins[vini].spendscript; + scriptlen = msgtx->vins[vini].spendlen; + } + sigtxid = bitcoin_sigtxid(pubtype,p2shtype,isPoS,height,serialized,maxlen,msgtx,vini,script,scriptlen,sighash,vpnstr,suppress_pubkeys); + if ( bits256_nonz(sigtxid) != 0 ) + { + vp = &V[vini]; + vp->sigtxid = sigtxid; + for (j=numsigs=0; jN; j++) + { + sig = vp->signers[j].sig; + siglen = vp->signers[j].siglen; + if ( signtx != 0 && bits256_nonz(vp->signers[j].privkey) != 0 ) + { + siglen = bitcoin_sign(ctx,symbol,sig,sigtxid,vp->signers[j].privkey,0); + //if ( (plen= bitcoin_pubkeylen(vp->signers[j].pubkey)) <= 0 ) + bitcoin_pubkey33(ctx,vp->signers[j].pubkey,vp->signers[j].privkey); + sig[siglen++] = sighash; + vp->signers[j].siglen = siglen; + /*char str[65]; printf("SIGTXID.(%s) ",bits256_str(str,sigtxid)); + int32_t i; for (i=0; isigners[j].pubkey[i]); + // s2 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1; + printf(" SIGNEDTX.[%02x] siglen.%d priv.%s\n",sig[siglen-1],siglen,bits256_str(str,vp->signers[j].privkey));*/ + } + if ( sig == 0 || siglen == 0 ) + { + memset(vp->signers[j].pubkey,0,sizeof(vp->signers[j].pubkey)); + continue; + } + if ( bitcoin_verify(ctx,sig,siglen-1,sigtxid,vp->signers[j].pubkey,bitcoin_pubkeylen(vp->signers[j].pubkey)) < 0 ) + { + int32_t k; for (k=0; ksigners[j].pubkey); k++) + printf("%02x",vp->signers[j].pubkey[k]); + printf(" SIG.%d.%d ERROR siglen.%d\n",vini,j,siglen); + } + else + { + flag++; + numsigs++; + /*int32_t z; + for (z=0; zsigners[j].pubkey[z]); + printf(" <- pub, SIG.%d.%d VERIFIED numsigs.%d vs M.%d\n",vini,j,numsigs,vp->M);*/ + } + } + if ( numsigs >= vp->M ) + complete = 1; + } + } + iguana_msgtx_Vset(serialized,maxlen,msgtx,V); + cJSON *txobj = cJSON_CreateObject(); + *signedtx = iguana_rawtxbytes(pubtype,p2shtype,isPoS,height,txobj,msgtx,suppress_pubkeys); + //printf("SIGNEDTX.(%s)\n",jprint(txobj,1)); + *signedtxidp = msgtx->txid; + return(complete); +} + +int32_t iguana_signrawtransaction(void *ctx,char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,int32_t height,struct iguana_msgtx *msgtx,char **signedtxp,bits256 *signedtxidp,struct vin_info *V,int32_t numinputs,char *rawtx,cJSON *vins,cJSON *privkeysjson) { uint8_t *serialized,*serialized2,*serialized3,*serialized4,*extraspace,pubkeys[64][33]; int32_t finalized,i,len,n,z,plen,maxsize,complete = 0,extralen = 65536; char *privkeystr,*signedtx = 0; bits256 privkeys[64],privkey,txid; cJSON *item; cJSON *txobj = 0; maxsize = 1000000; @@ -483,7 +483,7 @@ int32_t iguana_signrawtransaction(uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS if ( privkeystr == 0 || privkeystr[0] == 0 ) continue; privkeys[i] = privkey = iguana_str2priv(privkeystr); - bitcoin_pubkey33(swap->ctx,pubkeys[i],privkey); + bitcoin_pubkey33(ctx,pubkeys[i],privkey); //if ( bits256_nonz(privkey) != 0 ) // iguana_ensure_privkey(coin,privkey); } @@ -557,7 +557,7 @@ int32_t iguana_signrawtransaction(uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS } finalized = iguana_vininfo_create(pubtype,p2shtype,isPoS,serialized2,maxsize,msgtx,vins,numinputs,V); //printf("finalized.%d\n",finalized); - if ( (complete= bitcoin_verifyvins(pubtype,p2shtype,isPoS,height,signedtxidp,&signedtx,msgtx,serialized3,maxsize,V,SIGHASH_ALL,1,V->suppress_pubkeys)) > 0 && signedtx != 0 ) + if ( (complete= bitcoin_verifyvins(ctx,symbol,pubtype,p2shtype,isPoS,height,signedtxidp,&signedtx,msgtx,serialized3,maxsize,V,SIGHASH_ALL,1,V->suppress_pubkeys)) > 0 && signedtx != 0 ) { /*int32_t tmp; //char str[65]; if ( (tmp= iguana_interpreter(coin,0,iguana_lockval(finalized,jint(txobj,"locktime")),V,numinputs)) < 0 ) @@ -577,12 +577,9 @@ int32_t iguana_signrawtransaction(uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS return(complete); } - -#endif - char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr,char *vinaddr) { - 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,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info *V; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value,change = 0; + 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,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info V[16]; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value,change = 0; struct iguana_msgtx msgtx; *destamountp = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); if ( finalseqid == 0 ) @@ -605,7 +602,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch else printf("unexpected small value %.8f vs txfee %.8f\n",dstr(value),dstr(txfee)); *destamountp = satoshis; timestamp = (uint32_t)time(NULL); - V = calloc(256,sizeof(*V)); + memset(V,0,sizeof(V)); privkeys = cJSON_CreateArray(); if ( privkey2p != 0 ) { @@ -688,7 +685,8 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch completed = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); //printf("locktime.%u sequenceid.%x rawtx.(%s) vins.(%s)\n",locktime,sequenceid,rawtxbytes,jprint(vins,0)); - if ( (signedtx= LP_signrawtx(symbol,signedtxidp,&completed,vins,rawtxbytes,privkeys,V)) == 0 ) + if ( iguana_signrawtransaction(ctx,symbol,pubtype,p2shtype,isPoS,1000000,&msgtx,&signedtx,signedtxidp,V,1,rawtxbytes,vins,privkeys) <= 0 ) + //if ( (signedtx= LP_signrawtx(symbol,signedtxidp,&completed,vins,rawtxbytes,privkeys,V)) == 0 ) printf("couldnt sign transaction.%s %s\n",name,bits256_str(str,*signedtxidp)); else if ( completed == 0 ) { From 8a940e9b221c7795a98719a1833d21414f1b0be1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 20:59:10 +0300 Subject: [PATCH 243/339] Test --- iguana/exchanges/LP_transaction.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 3da97beb8..8de54beb1 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -426,13 +426,13 @@ int32_t bitcoin_verifyvins(void *ctx,char *symbol,uint8_t pubtype,uint8_t p2shty { flag++; numsigs++; - /*int32_t z; + int32_t z; for (z=0; zsigners[j].pubkey[z]); - printf(" <- pub, SIG.%d.%d VERIFIED numsigs.%d vs M.%d\n",vini,j,numsigs,vp->M);*/ + printf(" <- pub, SIG.%d.%d VERIFIED numsigs.%d vs M.%d\n",vini,j,numsigs,vp->M); } } if ( numsigs >= vp->M ) @@ -556,7 +556,7 @@ int32_t iguana_signrawtransaction(void *ctx,char *symbol,uint8_t pubtype,uint8_t } } finalized = iguana_vininfo_create(pubtype,p2shtype,isPoS,serialized2,maxsize,msgtx,vins,numinputs,V); - //printf("finalized.%d\n",finalized); + printf("finalized.%d\n",finalized); if ( (complete= bitcoin_verifyvins(ctx,symbol,pubtype,p2shtype,isPoS,height,signedtxidp,&signedtx,msgtx,serialized3,maxsize,V,SIGHASH_ALL,1,V->suppress_pubkeys)) > 0 && signedtx != 0 ) { /*int32_t tmp; //char str[65]; @@ -698,7 +698,6 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch } else printf("error making rawtx\n"); free_json(privkeys); free_json(txobj); - free(V); return(signedtx); } From 2462138338bfa7a5c38ad5f735396335137e8554 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:02:48 +0300 Subject: [PATCH 244/339] Test --- iguana/exchanges/LP_transaction.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 8de54beb1..8dc6c7e6b 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -685,7 +685,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch completed = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); //printf("locktime.%u sequenceid.%x rawtx.(%s) vins.(%s)\n",locktime,sequenceid,rawtxbytes,jprint(vins,0)); - if ( iguana_signrawtransaction(ctx,symbol,pubtype,p2shtype,isPoS,1000000,&msgtx,&signedtx,signedtxidp,V,1,rawtxbytes,vins,privkeys) <= 0 ) + if ( (completed= iguana_signrawtransaction(ctx,symbol,pubtype,p2shtype,isPoS,1000000,&msgtx,&signedtx,signedtxidp,V,1,rawtxbytes,vins,privkeys)) < 0 ) //if ( (signedtx= LP_signrawtx(symbol,signedtxidp,&completed,vins,rawtxbytes,privkeys,V)) == 0 ) printf("couldnt sign transaction.%s %s\n",name,bits256_str(str,*signedtxidp)); else if ( completed == 0 ) From 3dab4329c9f7b373bba8372fe16a2f997a3ed175 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:17:36 +0300 Subject: [PATCH 245/339] Test --- iguana/exchanges/LP_remember.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 41f425b18..a28f5b32f 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -751,8 +751,8 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti // alicespend for (j=0; j<32; j++) rev.bytes[j] = privAm.bytes[31 - j]; - revcalc_rmd160_sha256(secretAm,rev);//privAm); - vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); + //revcalc_rmd160_sha256(secretAm,rev);//privAm); + //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); { From bc01e248c27337b4b35d9da7dc1c086b6871af88 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:18:45 +0300 Subject: [PATCH 246/339] Test --- iguana/exchanges/LP_remember.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index a28f5b32f..5b6b7e32c 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -754,7 +754,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); + len = basilisk_swapuserdata(userdata,privAm/*rev*/,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); From 88e13effa7e90a697804be8e1e771e2811aa2479 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:21:42 +0300 Subject: [PATCH 247/339] Test --- iguana/exchanges/LP_remember.c | 2 +- iguana/exchanges/LP_transaction.c | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 5b6b7e32c..a28f5b32f 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -754,7 +754,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,privAm/*rev*/,0,myprivs[0],redeemscript,redeemlen); + len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 8dc6c7e6b..8b31806fb 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -426,13 +426,14 @@ int32_t bitcoin_verifyvins(void *ctx,char *symbol,uint8_t pubtype,uint8_t p2shty { flag++; numsigs++; - int32_t z; - for (z=0; zsigners[j].pubkey[z]); - printf(" <- pub, SIG.%d.%d VERIFIED numsigs.%d vs M.%d\n",vini,j,numsigs,vp->M); + int32_t z; char tmpaddr[64]; + for (z=0; zsigners[j].pubkey[z]); + bitcoin_address(tmpaddr,60,vp->signers[j].pubkey,33); + printf(" <- pub, SIG.%d.%d VERIFIED numsigs.%d vs M.%d %s\n",vini,j,numsigs,vp->M,tmpaddr); } } if ( numsigs >= vp->M ) From f8ee4b8762d66f1879124f286aa21ab448c38d80 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:25:02 +0300 Subject: [PATCH 248/339] Test --- iguana/exchanges/LP_remember.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index a28f5b32f..621795f6f 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -759,7 +759,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); bitcoin_address(privaddr,60,privpub33,33); - printf("alicespend len.%d redeemlen.%d priv0addr.(%s)\n",len,redeemlen,privaddr); + printf("alicespend len.%d redeemlen.%d priv0addr.(%s) priv0.(%s)\n",len,redeemlen,privaddr,bits256_str(str,myprivs[0])); } if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0,bobpaymentaddr)) != 0 ) printf("alicespend.(%s)\n",txbytes[BASILISK_ALICESPEND]); From 2992320dd9f749c1948cefbad1ef2b572b6ab24a Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:30:18 +0300 Subject: [PATCH 249/339] Test --- iguana/exchanges/LP_remember.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 621795f6f..931e7abac 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -754,7 +754,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); + len = basilisk_swapuserdata(userdata,privAm/*rev*/,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); From 6f7aaa1b49a908328cc73783f0a0c3ce9bd40c60 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:31:22 +0300 Subject: [PATCH 250/339] Test --- iguana/exchanges/LP_remember.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 931e7abac..36bc245e8 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -753,8 +753,8 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti rev.bytes[j] = privAm.bytes[31 - j]; //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); - redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,privAm/*rev*/,0,myprivs[0],redeemscript,redeemlen); + redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,privAm/*rev*/,privBn,secretAm,secretAm256,secretBn,secretBn256); + len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); From 72b53252b30d675570a331ed4149ba0b35644563 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:32:26 +0300 Subject: [PATCH 251/339] Test --- iguana/exchanges/LP_transaction.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 8b31806fb..f8384b9d5 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -686,8 +686,8 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch completed = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); //printf("locktime.%u sequenceid.%x rawtx.(%s) vins.(%s)\n",locktime,sequenceid,rawtxbytes,jprint(vins,0)); - if ( (completed= iguana_signrawtransaction(ctx,symbol,pubtype,p2shtype,isPoS,1000000,&msgtx,&signedtx,signedtxidp,V,1,rawtxbytes,vins,privkeys)) < 0 ) - //if ( (signedtx= LP_signrawtx(symbol,signedtxidp,&completed,vins,rawtxbytes,privkeys,V)) == 0 ) + //if ( (completed= iguana_signrawtransaction(ctx,symbol,pubtype,p2shtype,isPoS,1000000,&msgtx,&signedtx,signedtxidp,V,1,rawtxbytes,vins,privkeys)) < 0 ) + if ( (signedtx= LP_signrawtx(symbol,signedtxidp,&completed,vins,rawtxbytes,privkeys,V)) == 0 ) printf("couldnt sign transaction.%s %s\n",name,bits256_str(str,*signedtxidp)); else if ( completed == 0 ) { From e534091c2b48dfcf6f13c0b18de30532ecf0a3a4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:48:18 +0300 Subject: [PATCH 252/339] Test --- iguana/exchanges/LP_bitcoin.c | 1131 ++++++++++++++++++++++++++++- iguana/exchanges/LP_include.h | 16 + iguana/exchanges/LP_transaction.c | 28 +- 3 files changed, 1152 insertions(+), 23 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 781a50930..7b403117d 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -395,6 +395,19 @@ void iguana_optableinit() } } +int32_t bitcoin_pubkeylen(const uint8_t *pubkey) +{ + if ( pubkey[0] == 2 || pubkey[0] == 3 ) + return(33); + else if ( pubkey[0] == 4 ) + return(65); + else + { + //printf("illegal pubkey.[%02x] %llx\n",pubkey[0],*(long long *)pubkey); + return(-1); + } +} + int32_t iguana_expandscript(char *asmstr,int32_t maxlen,uint8_t *script,int32_t scriptlen) { int32_t len,n,j,i = 0; uint8_t opcode; uint32_t val,extraflag; @@ -470,6 +483,1111 @@ int32_t iguana_expandscript(char *asmstr,int32_t maxlen,uint8_t *script,int32_t return(len); } +static inline int32_t is_delim(int32_t c) +{ + if ( c == 0 || c == ' ' || c == '\t' || c == '\r' || c == '\n' ) + return(1); + else return(0); +} + +static struct iguana_stackdata iguana_pop(struct iguana_interpreter *stacks) +{ + struct iguana_stackdata Snum; + Snum = stacks->stack[--stacks->stackdepth]; + memset(&stacks->stack[stacks->stackdepth],0,sizeof(Snum)); + return(Snum); +} + +static int32_t iguana_altpush(struct iguana_interpreter *stacks,struct iguana_stackdata Snum) +{ + stacks->stack[2*IGUANA_MAXSTACKITEMS - ++stacks->altstackdepth] = Snum; + return(stacks->altstackdepth); +} + +static struct iguana_stackdata iguana_altpop(struct iguana_interpreter *stacks) +{ + struct iguana_stackdata Snum,*ptr; + ptr = &stacks->stack[2*IGUANA_MAXSTACKITEMS - --stacks->altstackdepth]; + Snum = *ptr; + memset(ptr,0,sizeof(Snum)); + return(Snum); +} + +static struct iguana_stackdata iguana_clone(struct iguana_stackdata Snum) +{ + struct iguana_stackdata clone; + clone = Snum; + if ( Snum.data != 0 ) + { + clone.data = malloc(Snum.size); + memcpy(clone.data,Snum.data,Snum.size); + } + return(clone); +} + +static int32_t iguana_isnonz(struct iguana_stackdata Snum) +{ + uint8_t *buf; int32_t i; + if ( Snum.size == sizeof(int32_t) ) + return(Snum.U.val != 0); + else if ( Snum.size == sizeof(int64_t) ) + return(Snum.U.val64 != 0); + else if ( Snum.size == 20 ) + buf = Snum.U.rmd160; + else if ( Snum.size == sizeof(bits256) ) + buf = Snum.U.hash2.bytes; + else if ( Snum.size == 33 ) + buf = Snum.U.pubkey; + else if ( Snum.size < 74 ) + buf = Snum.U.sig; + else buf = Snum.data; + for (i=0; ilastpath[stacks->ifdepth] < 0 ) + return(0); + //printf("PUSH.(%lld %p %d)\n",(long long)num64,numbuf,numlen); + if ( stacks->maxstackdepth > 0 ) + { + /*if ( numbuf != 0 ) + { + int32_t i; for (i=0; istackdepth < stacks->maxstackdepth ) + { + if ( stacks->logarray != 0 ) + item = cJSON_CreateObject(); + memset(&Snum,0,sizeof(Snum)); + if ( numbuf != 0 ) + { + if ( numlen <= sizeof(int32_t) ) + { + iguana_rwnum(1,(void *)&num,numlen,numbuf); + numlen = sizeof(num); + Snum.U.val = num; + } + else if ( numlen <= sizeof(int64_t) ) + { + iguana_rwnum(1,(void *)&num64,numlen,numbuf); + numlen = sizeof(num64); + Snum.U.val64 = num64; + } + else if ( numlen == 20 ) + memcpy(Snum.U.rmd160,numbuf,20); + else if ( numlen == sizeof(bits256) ) + iguana_rwbignum(1,Snum.U.hash2.bytes,sizeof(Snum.U.hash2),numbuf); + else if ( numlen == 33 ) + memcpy(Snum.U.pubkey,numbuf,numlen); + else if ( numlen < 74 ) + memcpy(Snum.U.sig,numbuf,numlen); + else + { + Snum.data = malloc(numlen); + memcpy(Snum.data,numbuf,numlen); + if ( item != 0 ) + jaddnum(item,"push",numlen); + } + Snum.size = numlen; + if ( item != 0 ) + { + init_hexbytes_noT(tmpstr,numbuf,numlen); + jaddstr(item,"push",tmpstr); + } + } + else if ( num64 <= 0xffffffff ) // what about negative numbers? + { + Snum.U.val = num, Snum.size = sizeof(num); + if ( item != 0 ) + jaddnum(item,"push",Snum.U.val); + } + else + { + Snum.U.val64 = num64, Snum.size = sizeof(num64); + if ( item != 0 ) + jaddnum(item,"push",Snum.U.val64); + } + if ( item != 0 ) + { + jaddnum(item,"depth",stacks->stackdepth); + if ( stacks->logarray != 0 ) + jaddi(stacks->logarray,item); + } + stacks->stack[stacks->stackdepth++] = Snum; + } else return(-1); + } else stacks->stackdepth++; + return(0); +} + +int32_t iguana_databuf(uint8_t *databuf,struct iguana_stackdata Snum) +{ + if ( Snum.size == 4 ) + memcpy(databuf,&Snum.U.val,4); + else if ( Snum.size == 8 ) + memcpy(databuf,&Snum.U.val64,8); + else if ( Snum.size == 20 ) + memcpy(databuf,&Snum.U.rmd160,20); + else if ( Snum.size == 32 ) + memcpy(databuf,&Snum.U.hash2.bytes,32); + else if ( Snum.size == 33 ) + memcpy(databuf,&Snum.U.pubkey,33); + else if ( Snum.size < 74 ) + memcpy(databuf,&Snum.U.sig,Snum.size); + else memcpy(databuf,&Snum.data,Snum.size); + return(Snum.size); +} + +static int32_t iguana_cmp(struct iguana_stackdata *a,struct iguana_stackdata *b) +{ + if ( a->size == b->size ) + { + if ( a->size == 4 ) + return(a->U.val != b->U.val); + else if ( a->size == 8 ) + return(a->U.val64 != b->U.val64); + else if ( a->size == 20 ) + return(memcmp(a->U.rmd160,b->U.rmd160,sizeof(a->U.rmd160))); + else if ( a->size == 32 ) + return(memcmp(a->U.hash2.bytes,b->U.hash2.bytes,sizeof(a->U.hash2))); + else if ( a->size == 33 ) + return(memcmp(a->U.pubkey,b->U.pubkey,33)); + else if ( a->size < 74 ) + return(memcmp(a->U.sig,b->U.sig,a->size)); + else return(memcmp(a->data,b->data,sizeof(a->size))); + } + return(-1); +} + +static int32_t iguana_dataparse(struct iguana_interpreter *stacks,uint8_t *script,int32_t k,char *str,int32_t *lenp) +{ + int32_t n,c,len; char tmp[4]; + *lenp = 0; + c = str[0]; + n = is_hexstr(str,0); + if ( n > 0 ) + { + if ( (n & 1) != 0 ) + len = (n+1) >> 1; + else len = n >> 1; + if ( len > 0 && len < 76 ) + { + if ( len == 1 ) + { + if ( n == 1 ) + { + tmp[0] = '0'; + tmp[1] = c; + tmp[2] = 0; + decode_hex(&script[k],1,tmp), (*lenp) = 1; + iguana_pushdata(stacks,script[k],0,0); + if ( script[k] != 0 ) + script[k++] += (IGUANA_OP_1 - 1); + return(k); + } + else if ( n == 2 && c == '1' && str[1] == '0' && is_delim(str[2]) != 0 ) + { + script[k++] = (IGUANA_OP_1 - 1) + 0x10, (*lenp) = 2; + iguana_pushdata(stacks,0x10,0,0); + return(k); + } + else if ( n == 2 && c == '8' && is_delim(str[2]) != 0 ) + { + if ( str[1] == '1' ) + { + script[k++] = IGUANA_OP_1NEGATE, (*lenp) = 2; + iguana_pushdata(stacks,-1,0,0); + return(k); + } + else if ( str[1] == '0' ) + { + script[k++] = IGUANA_OP_0, (*lenp) = 2; + iguana_pushdata(stacks,0,0,0); + return(k); + } + } + } + if ( len != 0 ) + script[k++] = len; + } + else if ( len <= 0xff ) + { + script[k++] = IGUANA_OP_PUSHDATA1; + script[k++] = len; + } + else if ( len <= 0xffff ) + { + if ( len <= MAX_SCRIPT_ELEMENT_SIZE ) + { + script[k++] = IGUANA_OP_PUSHDATA2; + script[k++] = (len & 0xff); + script[k++] = ((len >> 8) & 0xff); + } + else + { + printf("len.%d > MAX_SCRIPT_ELEMENT_SIZE.%d, offset.%d\n",len,MAX_SCRIPT_ELEMENT_SIZE,k); + return(-1); + } + } + else + { + printf("len.%d > MAX_SCRIPT_ELEMENT_SIZE.%d, offset.%d\n",len,MAX_SCRIPT_ELEMENT_SIZE,k); + return(-1); + } + if ( len != 0 ) + { + uint8_t *numstart; int32_t numlen; + numstart = &script[k], numlen = len; + if ( (n & 1) != 0 ) + { + tmp[0] = '0'; + tmp[1] = c; + tmp[2] = 0; + decode_hex(&script[k++],1,tmp), *lenp = 1; + len--; + } + if ( len != 0 ) + { + decode_hex(&script[k],len,str), (*lenp) += (len << 1); + k += len; + } + iguana_pushdata(stacks,0,numstart,numlen); + } + return(k); + } + return(0); +} + +void iguana_stack(struct iguana_interpreter *stacks,struct iguana_stackdata *args,int32_t num,char *pushstr,char *clonestr) +{ + int32_t i,c; + while ( (c= *pushstr++) != 0 ) + stacks->stack[stacks->stackdepth++] = args[c - '0']; + while ( (c= *clonestr++) != 0 ) + stacks->stack[stacks->stackdepth++] = iguana_clone(args[c - '0']); + if ( num > 0 ) + { + for (i=0; i 0 && siglen > 0 && siglen < 74 ) + { + if ( (retval= (bitcoin_verify(ctx,sig,siglen-1,sigtxid,pubkey,plen) == 0)) == 0 ) + { + } + if ( (0) ) + { + int32_t i; char str[65]; + for (i=0; i 0 && privlen == 32 ) + { + bitcoin_pubkey33(ctx,checkpub,*(bits256 *)privkey); + return(memcmp(checkpub,pubkey,33) == 0); + } + return(0); +} + +int32_t iguana_checkschnorrsig(void *ctx,int64_t M,struct iguana_stackdata pubkeyarg,struct iguana_stackdata sigarg,bits256 sigtxid) +{ + /*uint8_t combined_pub[MAX_SCRIPT_ELEMENT_SIZE],sig[MAX_SCRIPT_ELEMENT_SIZE]; int32_t plen,siglen; + plen = iguana_databuf(combined_pub,pubkeyarg); + siglen = iguana_databuf(sig,sigarg); + if ( bitcoin_pubkeylen(combined_pub) == 33 && siglen == 64 ) + return(bitcoin_schnorr_verify(ctx,sig,sigtxid,combined_pub,33) == 0);*/ + return(0); +} + +int32_t iguana_checkmultisig(void *ctx,struct iguana_interpreter *stacks,int32_t M,int32_t N,bits256 txhash2) +{ + int32_t i,j=0,len,n,m,valid=0,numsigners = 0,siglens[MAX_PUBKEYS_PER_MULTISIG]; uint8_t pubkeys[MAX_PUBKEYS_PER_MULTISIG][MAX_SCRIPT_ELEMENT_SIZE],sigs[MAX_PUBKEYS_PER_MULTISIG][MAX_SCRIPT_ELEMENT_SIZE]; + if ( M <= N && N <= MAX_PUBKEYS_PER_MULTISIG ) + { + if ( stacks->stackdepth <= 0 ) + return(0); + n = (int32_t)iguana_num(iguana_pop(stacks)); + if ( n != N ) + { + printf("iguana_checkmultisig n.%d != N.%d\n",n,N); + return(0); + } + //printf("n.%d stackdepth.%d\n",n,stacks->stackdepth); + for (i=0; istackdepth <= 0 ) + return(0); + len = iguana_databuf(pubkeys[i],iguana_pop(stacks)); + if ( len == bitcoin_pubkeylen(pubkeys[i]) ) + { + numsigners++; + //for (j=0; j<33; j++) + // printf("%02x",pubkeys[i][j]); + //printf(" <- pubkey.[%d]\n",i); + } + else + { + printf("nonpubkey on stack\n"); + return(0); + memcpy(sigs[0],pubkeys[i],len); + siglens[0] = len; + break; + } + } + if ( stacks->stackdepth <= 0 ) + return(0); + m = (int32_t)iguana_num(iguana_pop(stacks)); + //printf("m.%d stackdepth.%d\n",m,stacks->stackdepth); + + if ( m != M ) + { + printf("iguana_checkmultisig m.%d != M.%d\n",m,M); + return(0); + } + for (i=0; istackdepth <= 0 ) + return(0); + siglens[i] = iguana_databuf(sigs[i],iguana_pop(stacks)); + if ( siglens[i] <= 0 || siglens[i] > 74 ) + break; + //for (j=0; jstackdepth,bits256_str(str,txhash2)); + if ( stacks->stackdepth > 0 ) + iguana_pop(stacks); // for backward compatibility + j = numsigners-1; + for (i=numsigners-1; i>=0; i--) + { + for (; j>=0; j--) + { + if ( bitcoin_verify(ctx,sigs[i],siglens[i]-1,txhash2,pubkeys[j],bitcoin_pubkeylen(pubkeys[j])) == 0 ) + { + if ( ++valid >= M ) + return(1); + j--; + break; + } + } + } + } + } + printf("checkmultisig: valid.%d j.%d M.%d N.%d numsigners.%d\n",valid,j,M,N,numsigners); + return(0); +} + +#define LOCKTIME_THRESHOLD 500000000 +int32_t iguana_checklocktimeverify(void *ctx,int64_t tx_lockval,uint32_t nSequence,struct iguana_stackdata Snum) +{ + int64_t nLockTime = iguana_num(Snum); + if ( nLockTime < 0 || tx_lockval < 0 ) + { + printf("CLTV.0 nLockTime.%lld tx_lockval.%lld\n",(long long)nLockTime,(long long)tx_lockval); + return(-1); + } + else if ( ((tx_lockval < LOCKTIME_THRESHOLD && nLockTime < LOCKTIME_THRESHOLD) || + (tx_lockval >= LOCKTIME_THRESHOLD && nLockTime >= LOCKTIME_THRESHOLD)) == 0 ) + { + printf("CLTV.1 nLockTime.%lld tx_lockval.%lld\n",(long long)nLockTime,(long long)tx_lockval); + return(-1); + } + else if ( nLockTime > tx_lockval ) + { + printf("CLTV.2 nLockTime.%lld tx_lockval.%lld\n",(long long)nLockTime,(long long)tx_lockval); + return(-1); + } + return(0); +} + +int32_t iguana_checksequenceverify(void *ctx,int64_t nLockTime,uint32_t nSequence,struct iguana_stackdata Snum) +{ + return(0); +} + +cJSON *iguana_spendasm(uint8_t *spendscript,int32_t spendlen) +{ + char asmstr[IGUANA_MAXSCRIPTSIZE*2+1]; cJSON *spendasm = cJSON_CreateObject(); + iguana_expandscript(asmstr,sizeof(asmstr),spendscript,spendlen); + //int32_t i; for (i=0; i (%s)\n",asmstr); + jaddstr(spendasm,"interpreter",asmstr); + return(spendasm); +} + +int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCRIPTSIZE],cJSON *interpreter,int32_t interpret,int64_t nLockTime,struct vin_info *V) +{ + struct bitcoin_opcode *op; cJSON *array = 0; struct iguana_interpreter STACKS,*stacks = &STACKS; + struct iguana_stackdata args[MAX_PUBKEYS_PER_MULTISIG]; + uint8_t databuf[MAX_SCRIPT_ELEMENT_SIZE]; char *asmstr,*str,*hexstr; cJSON *item; + int32_t c,numops,dlen,plen,numvars,numused,numargs=0,i,j,k,n=0,len,datalen,errs=0; int64_t val; + iguana_optableinit(); + if ( (asmstr= jstr(interpreter,"interpreter")) == 0 || asmstr[0] == 0 ) + return(0); + if ( (numvars= juint(interpreter,"numvars")) > 0 ) + { + if ( (array= jarray(&n,interpreter,"args")) == 0 || (interpret != 0 && n != numvars) ) + return(-2); + } + str = asmstr; + if ( interpret != 0 ) + { + stacks = calloc(1,sizeof(*stacks) + sizeof(*stacks->stack)*2*IGUANA_MAXSTACKITEMS); + stacks->maxstackdepth = IGUANA_MAXSTACKITEMS; + if ( (stacks->logarray= logarray) != 0 ) + item = cJSON_CreateObject(); + else item = 0; + if ( V->M == 0 && V->N == 0 ) + V->N = V->M = 1; + for (i=0; iN; i++) + { + if ( V->signers[i].siglen != 0 ) + { + iguana_pushdata(stacks,0,V->signers[i].sig,V->signers[i].siglen); + if ( bitcoin_pubkeylen(V->signers[i].pubkey) <= 0 ) + { + printf("missing pubkey.[%d]\n",i); + free(stacks); + return(-1); + } + //printf("pushdata siglen.%d depth.%d\n",V->signers[i].siglen,stacks->stackdepth); + } + } + for (i=0; iN; i++) + { + if ( V->signers[i].siglen != 0 ) + { + plen = bitcoin_pubkeylen(V->signers[i].pubkey); + if ( V->suppress_pubkeys == 0 && (V->spendscript[0] != plen || V->spendscript[V->spendlen - 1] != IGUANA_OP_CHECKSIG || bitcoin_pubkeylen(&V->spendscript[1]) <= 0) ) + { + iguana_pushdata(stacks,0,V->signers[i].pubkey,plen); + //printf(">>>>>>>>> suppress.%d pushdata [%02x %02x] plen.%d depth.%d\n",V->suppress_pubkeys,V->signers[i].pubkey[0],V->signers[i].pubkey[1],plen,stacks->stackdepth); + } // else printf("<<<<<<<<<< skip pubkey push %d script[0].%d spendlen.%d depth.%d\n",plen,V->spendscript[0],V->spendlen,stacks->stackdepth); + } + } + if ( V->userdatalen != 0 ) + { + len = 0; + while ( len < V->userdatalen ) + { + dlen = V->userdata[len++]; + if ( dlen > 0 && dlen < 76 ) + iguana_pushdata(stacks,0,&V->userdata[len],dlen), len += dlen; + else if ( dlen >= IGUANA_OP_1 && dlen <= IGUANA_OP_16 ) + { + dlen -= (IGUANA_OP_1 - 1); + iguana_pushdata(stacks,dlen,0,0); + } + else if ( dlen == IGUANA_OP_PUSHDATA1 ) + { + iguana_pushdata(stacks,V->userdata[len++],0,0); + } + else if ( dlen == IGUANA_OP_PUSHDATA2 ) + { + iguana_pushdata(stacks,V->userdata[len] + ((int32_t)V->userdata[len+1]<<8),0,0); + len += 2; + } + else if ( dlen == IGUANA_OP_0 ) + iguana_pushdata(stacks,0,0,0); + else if ( dlen == IGUANA_OP_1NEGATE ) + iguana_pushdata(stacks,-1,0,0); + else + { + printf("invalid data opcode %02x\n",dlen); + free(stacks); + return(-1); + } + //printf("user data stackdepth.%d dlen.%d\n",stacks->stackdepth,dlen); + } + if ( len != V->userdatalen ) + { + printf("mismatched userdatalen %d vs %d\n",len,V->userdatalen); + free(stacks); + return(-1); + } + } + if ( item != 0 && stacks->logarray != 0 ) + { + jaddstr(item,"spendasm",asmstr); + jaddi(stacks->logarray,item); + } + if ( V->extras != 0 ) + { + if ( (n= cJSON_GetArraySize(V->extras)) > 0 ) + { + for (i=0; iextras,i),0)) != 0 && (len= is_hexstr(hexstr,0)) > 0 ) + { + len >>= 1; + decode_hex(databuf,len,hexstr); + iguana_pushdata(stacks,0,databuf,len); + } + } + } + } + } else memset(stacks,0,sizeof(*stacks)); + stacks->lastpath[0] = 1; + k = numops = numused = 0; + script[k] = 0; + while ( (c= *str++) != 0 ) + { + if ( is_delim(c) != 0 ) + { + //if ( c == 0 ) + // break; + continue; + } + if ( c == '/' && *str == '/' ) // support // + break; + else if ( c == '-' && *str == '1' && is_delim(str[1]) != 0 ) + { + script[k++] = IGUANA_OP_1NEGATE, str += 3; // OP_1NEGATE; + iguana_pushdata(stacks,-1,0,0); + continue; + } + else if ( c == '%' && *str == 's' ) + { + str++; + if ( numused < numvars && (hexstr= jstr(jitem(array,numused++),0)) != 0 ) + { + if ( (n= iguana_dataparse(stacks,script,k,str,&len)) > 0 ) + { + k += n; + continue; + } + } + printf("dataparse error.%d, numused.%d >= numvars.%d\n",n,numused,numvars); + errs++; + break; + } + else + { + str--; + if ( (n= iguana_dataparse(stacks,script,k,str,&len)) > 0 ) + { + k = n; + str += len; + continue; + } + else if ( n < 0 ) + { + printf("dataparse negative n.%d\n",n); + errs++; + break; + } + } + for (j=0; j<32; j++) + if ( is_delim(str[j]) != 0 ) + break; + if ( j == 32 ) + { + printf("too long opcode.%s at offset.%ld\n",str,(long)str-(long)asmstr); + errs++; + break; + } + HASH_FIND(hh,OPTABLE,str,j,op); + //printf("{%s}\n",str); + str += j; + if ( op != 0 ) + { + if ( numargs > 0 ) + { + for (i=0; iopcode; + if ( (op->flags & IGUANA_CONTROLFLAG) != 0 ) + { + //printf("control opcode depth.%d\n",stacks->stackdepth); + switch ( op->opcode ) + { + case IGUANA_OP_IF: case IGUANA_OP_NOTIF: + if ( stacks->ifdepth >= IGUANA_MAXSTACKDEPTH ) + { + printf("ifdepth.%d >= MAXSTACKDEPTH.%d\n",stacks->ifdepth,IGUANA_MAXSTACKDEPTH); + errs++; + } + else + { + if ( stacks->stackdepth <= 0 ) + { + printf("if invalid stackdepth %d\n",stacks->stackdepth); + errs++; + } + else + { + args[0] = iguana_pop(stacks); + if ( iguana_isnonz(args[0]) == (op->opcode == IGUANA_OP_IF) ) + { + val = 1; + //printf("OP_IF enabled depth.%d\n",stacks->stackdepth); + } + else + { + val = -1; + //printf("OP_IF disabled depth.%d\n",stacks->stackdepth); + } + stacks->lastpath[++stacks->ifdepth] = val; + } + } + break; + case IGUANA_OP_ELSE: + /*if ( stacks->stackdepth <= 0 ) + { + printf("else invalid stackdepth %d\n",stacks->stackdepth); + errs++; + } + else*/ + { + if ( stacks->ifdepth <= stacks->elsedepth ) + { + printf("unhandled opcode.%02x stacks->ifdepth %d <= %d stacks->elsedepth\n",op->opcode,stacks->ifdepth,stacks->elsedepth); + errs++; + } + stacks->lastpath[stacks->ifdepth] *= -1; + //printf("OP_ELSE status.%d depth.%d\n",stacks->lastpath[stacks->ifdepth],stacks->stackdepth); + } + break; + case IGUANA_OP_ENDIF: + if ( stacks->ifdepth <= 0 ) + { + printf("endif without if offset.%ld\n",(long)str-(long)asmstr); + errs++; + } + stacks->ifdepth--; + //printf("OP_ENDIF status.%d depth.%d\n",stacks->lastpath[stacks->ifdepth],stacks->stackdepth); + break; + case IGUANA_OP_VERIFY: + break; + case IGUANA_OP_RETURN: + iguana_pushdata(stacks,0,0,0); + errs++; + break; + } + if ( errs != 0 ) + break; + continue; + } + if ( stacks->lastpath[stacks->ifdepth] != 0 ) + { + if ( stacks->lastpath[stacks->ifdepth] < 0 ) + { + //printf("SKIP opcode.%02x depth.%d\n",op->opcode,stacks->stackdepth); + if ( stacks->logarray ) + jaddistr(stacks->logarray,"skip"); + continue; + } + //printf("conditional opcode.%02x stackdepth.%d\n",op->opcode,stacks->stackdepth); + } + if ( op->opcode <= IGUANA_OP_16 || ++numops <= MAX_OPS_PER_SCRIPT ) + { + if ( (op->flags & IGUANA_ALWAYSILLEGAL) != 0 ) + { + printf("disabled opcode.%s at offset.%ld\n",str,(long)str-(long)asmstr); + errs++; + break; + } + else if ( op->extralen > 0 ) + { + if ( is_delim(*str) != 0 ) + str++; + if ( is_hexstr(str,0) != (op->extralen<<1) ) + { + printf("expected extralen.%d of hex, got.(%s) at offset.%ld\n",op->extralen,str,(long)str-(long)asmstr); + errs++; + break; + } + decode_hex(&script[k],op->extralen,str), str += (op->extralen << 1); + if ( op->extralen == 1 ) + iguana_pushdata(stacks,script[k],0,0); + else if ( op->extralen == 2 ) + iguana_pushdata(stacks,script[k] + ((uint32_t)script[k]<<8),0,0); + k += op->extralen; + continue; + } + if ( interpret == 0 || V == 0 ) + continue; + if ( (op->flags & IGUANA_NOPFLAG) != 0 ) + continue; + if ( (numargs= op->stackitems) > 0 ) + { + if ( stacks->stackdepth < op->stackitems ) + { + //printf("stackdepth.%d needed.%d (%s) at offset.%ld\n",stacks->stackdepth,op->stackitems,str,(long)str-(long)asmstr); + errs++; + break; + } + for (i=0; iopcode,numargs,stacks->stackdepth); + if ( stacks->logarray != 0 ) + { + char tmpstr[1096]; + item = cJSON_CreateObject(); + array = cJSON_CreateArray(); + for (i=0; ihh.key,array); + jaddi(stacks->logarray,item); + } + if ( (op->flags & IGUANA_EXECUTIONILLEGAL) != 0 ) + { + printf("opcode not allowed to run.%s at %ld\n",(char *)op->hh.key,(long)str-(long)asmstr); + errs++; + break; + } + else if ( op->opcode == IGUANA_OP_EQUALVERIFY || op->opcode == IGUANA_OP_EQUAL ) + { + if ( iguana_cmp(&args[0],&args[1]) == 0 ) + iguana_pushdata(stacks,1,0,0); + else + { + iguana_pushdata(stacks,0,0,0); + for (i=0; iopcode,args[0].size,args[1].size); + } + } + else if ( (op->flags & IGUANA_CRYPTOFLAG) != 0 ) + { + uint8_t rmd160[20],revdatabuf[MAX_SCRIPT_ELEMENT_SIZE]; bits256 hash; + datalen = iguana_databuf(databuf,args[0]); + for (i=0; iopcode ) + { + case IGUANA_OP_RIPEMD160: + calc_rmd160(0,rmd160,databuf,datalen); + iguana_pushdata(stacks,0,rmd160,sizeof(rmd160)); + break; + case IGUANA_OP_SHA1: + calc_sha1(0,rmd160,databuf,datalen); + iguana_pushdata(stacks,0,rmd160,sizeof(rmd160)); + break; + case IGUANA_OP_HASH160: + /*if ( datalen == 32 ) + { + revcalc_rmd160_sha256(rmd160,*(bits256 *)databuf); + printf("SPECIAL CASE REVERSE\n"); + } else + for (i=0; i<32; i++) + printf("%02x",databuf[i]); + printf(" <- databuf\n"); + for (i=0; i<32; i++) + printf("%02x",revdatabuf[i]); + printf(" <- revdatabuf\n"); + calc_rmd160_sha256(rmd160,revdatabuf,datalen); + for (i=0; i<20; i++) + printf("%02x",rmd160[i]); + printf(" <- rmd160 revdatabuf\n"); + revcalc_rmd160_sha256(rmd160,*(bits256 *)databuf); + for (i=0; i<20; i++) + printf("%02x",rmd160[i]); + printf(" <- rmd160 special\n"); + calc_rmd160_sha256(rmd160,databuf,datalen); + for (i=0; i<20; i++) + printf("%02x",rmd160[i]); + printf(" <- rmd160 databuf\n");*/ + if ( datalen == 32 ) + calc_rmd160_sha256(rmd160,revdatabuf,datalen); + else calc_rmd160_sha256(rmd160,databuf,datalen); + iguana_pushdata(stacks,0,rmd160,sizeof(rmd160)); + break; + case IGUANA_OP_SHA256: + vcalc_sha256(0,hash.bytes,databuf,datalen); + for (i=0; i sha256 %s\n",bits256_str(str,hash)); + iguana_pushdata(stacks,0,hash.bytes,sizeof(hash)); + break; + case IGUANA_OP_HASH256: + hash = bits256_doublesha256(0,databuf,datalen); + iguana_pushdata(stacks,0,hash.bytes,sizeof(hash)); + break; + case IGUANA_OP_CHECKSIG: case IGUANA_OP_CHECKSIGVERIFY: + iguana_pushdata(stacks,iguana_checksig(ctx,args[1],args[0],V->sigtxid),0,0); + break; + case IGUANA_OP_CHECKMULTISIG: case IGUANA_OP_CHECKMULTISIGVERIFY: + iguana_pushdata(stacks,iguana_checkmultisig(ctx,stacks,V->M,V->N,V->sigtxid),0,0); + break; + case IGUANA_OP_CHECKSCHNORR: case IGUANA_OP_CHECKSCHNORRVERIFY: + iguana_pushdata(stacks,iguana_checkschnorrsig(ctx,iguana_num(args[2]),args[1],args[0],V->sigtxid),0,0); + break; + case IGUANA_OP_CHECKPRIVATEKEY: case IGUANA_OP_CHECKPRIVATEKEYVERIFY: + iguana_pushdata(stacks,iguana_checkprivatekey(ctx,args[1],args[0]),0,0); + break; + } + } + else if ( op->opcode == IGUANA_OP_CHECKLOCKTIMEVERIFY ) // former OP_NOP2 + { + if ( V->ignore_cltverr == 0 && iguana_checklocktimeverify(ctx,nLockTime,V->sequence,args[0]) < 0 ) + { + iguana_stack(stacks,args,1,"0",""); + errs++; + break; + } + iguana_stack(stacks,args,1,"0",""); + continue; + } + else if ( op->opcode == IGUANA_OP_CHECKSEQUENCEVERIFY ) // former OP_NOP3 + { + if ( iguana_checksequenceverify(ctx,nLockTime,V->sequence,args[0]) < 0 ) + { + iguana_stack(stacks,args,1,"0",""); + errs++; + break; + } + iguana_stack(stacks,args,1,"0",""); + continue; + } + else if ( (op->flags & IGUANA_STACKFLAG) != 0 ) + { + val = 0; + if ( op->opcode == IGUANA_OP_PICK || op->opcode == IGUANA_OP_ROLL ) + { + if ( interpret != 0 && stacks->stackdepth < (val= iguana_num(args[0])) ) + { + printf("stack not deep enough %d < %lld\n",stacks->stackdepth,(long long)iguana_num(args[0])); + errs++; + break; + } + if ( op->opcode == IGUANA_OP_PICK ) + { + stacks->stack[stacks->stackdepth] = iguana_clone(stacks->stack[stacks->stackdepth - 1 - val]); + stacks->stackdepth++; + } + else + { + args[1] = stacks->stack[stacks->stackdepth - 1 - val]; + for (i=(int32_t)(stacks->stackdepth-1-val); istackdepth-1; i++) + stacks->stack[i] = stacks->stack[i+1]; + stacks->stack[stacks->stackdepth - 1] = args[1]; + } + } + else + { + switch ( op->opcode ) + { + case IGUANA_OP_TOALTSTACK: + if ( stacks->altstackdepth < stacks->maxstackdepth ) + { + iguana_altpush(stacks,args[0]); + memset(&args[0],0,sizeof(args[0])); + } + else + { + printf("altstack overflow %d vs %d\n",stacks->altstackdepth,stacks->maxstackdepth); + errs++; + } + break; + case IGUANA_OP_FROMALTSTACK: + stacks->stack[stacks->stackdepth++] = iguana_altpop(stacks); + break; + case IGUANA_OP_DEPTH: iguana_pushdata(stacks,stacks->stackdepth,0,0); break; + case IGUANA_OP_DROP: case IGUANA_OP_2DROP: break; + case IGUANA_OP_3DUP: iguana_stack(stacks,args,3,"012","012"); break; + case IGUANA_OP_2OVER: iguana_stack(stacks,args,4,"0123","01"); break; + case IGUANA_OP_2ROT: iguana_stack(stacks,args,6,"234501",""); break; + case IGUANA_OP_2SWAP: iguana_stack(stacks,args,4,"2301",""); break; + case IGUANA_OP_IFDUP: + if ( iguana_isnonz(args[0]) != 0 ) + iguana_stack(stacks,args,0,"","0"); + iguana_stack(stacks,args,1,"0",""); + break; + case IGUANA_OP_DUP: iguana_stack(stacks,args,1,"0","0"); break; + case IGUANA_OP_2DUP: iguana_stack(stacks,args,2,"01","01"); break; + case IGUANA_OP_NIP: + if ( args[0].data != 0 ) + free(args[0].data); + iguana_stack(stacks,args,2,"1",""); + break; + case IGUANA_OP_OVER: iguana_stack(stacks,args,2,"01","0"); break; + case IGUANA_OP_ROT: iguana_stack(stacks,args,3,"120",""); break; + case IGUANA_OP_SWAP: iguana_stack(stacks,args,2,"10",""); break; + case IGUANA_OP_TUCK: iguana_stack(stacks,args,2,"10","1"); break; + } + } + } + else if ( (op->flags & IGUANA_MATHFLAG) != 0 ) + { + int64_t numA=0,numB=0,numC=0; + for (i=0; istackitems; i++) + { + if ( args[i].size != sizeof(int32_t) ) + break; + if ( i == 0 ) + numA = iguana_num(args[i]); + else if ( i == 1 ) + numB = iguana_num(args[i]); + else if ( i == 2 ) + numC = iguana_num(args[i]); + } + if ( i != op->stackitems ) + { + printf("math script non-int32_t arg[%d] of %d\n",i,op->stackitems); + errs++; + break; + } + switch ( op->opcode ) + { + case IGUANA_OP_1ADD: iguana_pushdata(stacks,numA + 1,0,0); break; + case IGUANA_OP_1SUB: iguana_pushdata(stacks,numA - 1,0,0); break; + case IGUANA_OP_NEGATE: iguana_pushdata(stacks,-numA,0,0); break; + case IGUANA_OP_ABS: iguana_pushdata(stacks,numA<0?-numA:numA,0,0); break; + case IGUANA_OP_NOT: iguana_pushdata(stacks,numA == 0,0,0); break; + case IGUANA_OP_0NOTEQUAL: iguana_pushdata(stacks,numA != 0,0,0); break; + case IGUANA_OP_ADD: iguana_pushdata(stacks,numA + numB,0,0); break; + case IGUANA_OP_SUB: iguana_pushdata(stacks,numA - numB,0,0); break; + case IGUANA_OP_BOOLAND:iguana_pushdata(stacks,numA != 0 && numB != 0,0,0); break; + case IGUANA_OP_BOOLOR: iguana_pushdata(stacks,numA != 0 || numB != 0,0,0); break; + case IGUANA_OP_NUMEQUAL: case IGUANA_OP_NUMEQUALVERIFY: + iguana_pushdata(stacks,numA == numB,0,0); break; + case IGUANA_OP_NUMNOTEQUAL:iguana_pushdata(stacks,numA != numB,0,0); break; + case IGUANA_OP_LESSTHAN: iguana_pushdata(stacks,numA < numB,0,0); break; + case IGUANA_OP_GREATERTHAN:iguana_pushdata(stacks,numA > numB,0,0); break; + case IGUANA_OP_LESSTHANOREQUAL:iguana_pushdata(stacks,numA <= numB,0,0); break; + case IGUANA_OP_GREATERTHANOREQUAL:iguana_pushdata(stacks,numA >= numB,0,0); break; + case IGUANA_OP_MIN: iguana_pushdata(stacks,numA <= numB ? numA : numB,0,0); break; + case IGUANA_OP_MAX: iguana_pushdata(stacks,numA >= numB ? numA : numB,0,0); break; + case IGUANA_OP_WITHIN: iguana_pushdata(stacks,numB <= numA && numA < numC,0,0); break; + } + } + else if ( op->opcode == IGUANA_OP_CODESEPARATOR ) + { + if ( stacks != 0 ) + stacks->codeseparator = k; + continue; + } + else + { + printf("unhandled opcode.%02x (%s)\n",op->opcode,str); + errs++; + break; + } + if ( (op->flags & IGUANA_POSTVERIFY) != 0 ) + { + if ( stacks->stackdepth < 1 ) + { + printf("empty stack at offset.%ld\n",(long)str - (long)asmstr); + errs++; + break; + } + if ( iguana_isnonz(stacks->stack[stacks->stackdepth-1]) == 0 ) + break; + iguana_pop(stacks); + } + } + else + { + printf("too many ops opcode.%s at offset.%ld\n",str,(long)str - (long)asmstr); + errs++; + break; + } + } + else + { + printf("unknown opcode.%s at offset.%ld\n",str,(long)str - (long)asmstr); + errs++; + break; + } + } + if ( stacks != &STACKS ) + { + if ( jobj(interpreter,"result") != 0 ) + jdelete(interpreter,"result"); + if ( stacks->stackdepth <= 0 ) + { + errs++; + printf("empty stack error\n"); + jaddstr(interpreter,"error","empty stack"); + jadd(interpreter,"result",jfalse()); + } + else if ( iguana_isnonz(stacks->stack[--stacks->stackdepth]) != 0 ) + { + //printf("Evaluate true, depth.%d errs.%d k.%d\n",stacks->stackdepth,errs,k); + if ( errs == 0 ) + jadd(interpreter,"result",jtrue()); + else jadd(interpreter,"result",jfalse()); + } + else + { + jadd(interpreter,"result",jfalse()); + printf("Evaluate FALSE, depth.%d errs.%d [0] size.%d num.%d\n",stacks->stackdepth,errs,stacks->stack[0].size,stacks->stack[0].U.val); + if ( stacks->logarray != 0 ) + printf("LOG.(%s)\n\n",jprint(stacks->logarray,0)); + } + if ( numargs > 0 ) + { + for (i=0; isuppress_pubkeys)) > 0 && signedtx != 0 ) { - /*int32_t tmp; //char str[65]; - if ( (tmp= iguana_interpreter(coin,0,iguana_lockval(finalized,jint(txobj,"locktime")),V,numinputs)) < 0 ) - { - printf("iguana_interpreter %d error.(%s)\n",tmp,signedtx); - complete = 0; - } */ + int32_t tmp; //char str[65]; + if ( (tmp= iguana_interpreter(ctx,0,iguana_lockval(finalized,jint(txobj,"locktime")),V,numinputs)) < 0 ) + { + printf("iguana_interpreter %d error.(%s)\n",tmp,signedtx); + complete = 0; + } else printf("interpreter passed\n"); } else printf("complete.%d\n",complete); } else printf("rwmsgtx error\n"); } else fprintf(stderr,"no inputs in vins.(%s)\n",vins!=0?jprint(vins,0):"null"); @@ -686,8 +694,8 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch completed = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); //printf("locktime.%u sequenceid.%x rawtx.(%s) vins.(%s)\n",locktime,sequenceid,rawtxbytes,jprint(vins,0)); - //if ( (completed= iguana_signrawtransaction(ctx,symbol,pubtype,p2shtype,isPoS,1000000,&msgtx,&signedtx,signedtxidp,V,1,rawtxbytes,vins,privkeys)) < 0 ) - if ( (signedtx= LP_signrawtx(symbol,signedtxidp,&completed,vins,rawtxbytes,privkeys,V)) == 0 ) + if ( (completed= iguana_signrawtransaction(ctx,symbol,pubtype,p2shtype,isPoS,1000000,&msgtx,&signedtx,signedtxidp,V,1,rawtxbytes,vins,privkeys)) < 0 ) + //if ( (signedtx= LP_signrawtx(symbol,signedtxidp,&completed,vins,rawtxbytes,privkeys,V)) == 0 ) printf("couldnt sign transaction.%s %s\n",name,bits256_str(str,*signedtxidp)); else if ( completed == 0 ) { From 5f6065a18be7e0393c42c12dca0357d1933f38d7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:49:27 +0300 Subject: [PATCH 253/339] Test --- iguana/exchanges/LP_remember.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 36bc245e8..621795f6f 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -753,7 +753,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti rev.bytes[j] = privAm.bytes[31 - j]; //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); - redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,privAm/*rev*/,privBn,secretAm,secretAm256,secretBn,secretBn256); + redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; From daee179a4e01efd7024a2a7f9603f57d47bc72d1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:52:46 +0300 Subject: [PATCH 254/339] Test --- iguana/exchanges/LP_bitcoin.c | 46 +++++++++++++++++------------------ 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 7b403117d..c7c5fe8fa 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -1321,29 +1321,29 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR iguana_pushdata(stacks,0,rmd160,sizeof(rmd160)); break; case IGUANA_OP_HASH160: - /*if ( datalen == 32 ) - { - revcalc_rmd160_sha256(rmd160,*(bits256 *)databuf); - printf("SPECIAL CASE REVERSE\n"); - } else - for (i=0; i<32; i++) - printf("%02x",databuf[i]); - printf(" <- databuf\n"); - for (i=0; i<32; i++) - printf("%02x",revdatabuf[i]); - printf(" <- revdatabuf\n"); - calc_rmd160_sha256(rmd160,revdatabuf,datalen); - for (i=0; i<20; i++) - printf("%02x",rmd160[i]); - printf(" <- rmd160 revdatabuf\n"); - revcalc_rmd160_sha256(rmd160,*(bits256 *)databuf); - for (i=0; i<20; i++) - printf("%02x",rmd160[i]); - printf(" <- rmd160 special\n"); - calc_rmd160_sha256(rmd160,databuf,datalen); - for (i=0; i<20; i++) - printf("%02x",rmd160[i]); - printf(" <- rmd160 databuf\n");*/ + if ( datalen == 32 ) + { + revcalc_rmd160_sha256(rmd160,*(bits256 *)databuf); + printf("SPECIAL CASE REVERSE\n"); + } else + for (i=0; i<32; i++) + printf("%02x",databuf[i]); + printf(" <- databuf\n"); + for (i=0; i<32; i++) + printf("%02x",revdatabuf[i]); + printf(" <- revdatabuf\n"); + calc_rmd160_sha256(rmd160,revdatabuf,datalen); + for (i=0; i<20; i++) + printf("%02x",rmd160[i]); + printf(" <- rmd160 revdatabuf\n"); + revcalc_rmd160_sha256(rmd160,*(bits256 *)databuf); + for (i=0; i<20; i++) + printf("%02x",rmd160[i]); + printf(" <- rmd160 special\n"); + calc_rmd160_sha256(rmd160,databuf,datalen); + for (i=0; i<20; i++) + printf("%02x",rmd160[i]); + printf(" <- rmd160 databuf\n"); if ( datalen == 32 ) calc_rmd160_sha256(rmd160,revdatabuf,datalen); else calc_rmd160_sha256(rmd160,databuf,datalen); From 7d99eddcf9963f717bea76a4be5a1b56c7c50113 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:55:52 +0300 Subject: [PATCH 255/339] Test --- iguana/exchanges/LP_bitcoin.c | 48 +++++++++++++++++------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index c7c5fe8fa..b8d92440d 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -1321,30 +1321,30 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR iguana_pushdata(stacks,0,rmd160,sizeof(rmd160)); break; case IGUANA_OP_HASH160: - if ( datalen == 32 ) - { - revcalc_rmd160_sha256(rmd160,*(bits256 *)databuf); - printf("SPECIAL CASE REVERSE\n"); - } else - for (i=0; i<32; i++) - printf("%02x",databuf[i]); - printf(" <- databuf\n"); - for (i=0; i<32; i++) - printf("%02x",revdatabuf[i]); - printf(" <- revdatabuf\n"); - calc_rmd160_sha256(rmd160,revdatabuf,datalen); - for (i=0; i<20; i++) - printf("%02x",rmd160[i]); - printf(" <- rmd160 revdatabuf\n"); - revcalc_rmd160_sha256(rmd160,*(bits256 *)databuf); - for (i=0; i<20; i++) - printf("%02x",rmd160[i]); - printf(" <- rmd160 special\n"); - calc_rmd160_sha256(rmd160,databuf,datalen); - for (i=0; i<20; i++) - printf("%02x",rmd160[i]); - printf(" <- rmd160 databuf\n"); - if ( datalen == 32 ) + /*if ( datalen == 32 ) + { + revcalc_rmd160_sha256(rmd160,*(bits256 *)databuf); + printf("SPECIAL CASE REVERSE\n"); + } else + for (i=0; i<32; i++) + printf("%02x",databuf[i]); + printf(" <- databuf\n"); + for (i=0; i<32; i++) + printf("%02x",revdatabuf[i]); + printf(" <- revdatabuf\n"); + calc_rmd160_sha256(rmd160,revdatabuf,datalen); + for (i=0; i<20; i++) + printf("%02x",rmd160[i]); + printf(" <- rmd160 revdatabuf\n"); + revcalc_rmd160_sha256(rmd160,*(bits256 *)databuf); + for (i=0; i<20; i++) + printf("%02x",rmd160[i]); + printf(" <- rmd160 special\n"); + calc_rmd160_sha256(rmd160,databuf,datalen); + for (i=0; i<20; i++) + printf("%02x",rmd160[i]); + printf(" <- rmd160 databuf\n");*/ + if ( 0 && datalen == 32 ) calc_rmd160_sha256(rmd160,revdatabuf,datalen); else calc_rmd160_sha256(rmd160,databuf,datalen); iguana_pushdata(stacks,0,rmd160,sizeof(rmd160)); From cd9d08c40256e1b02a9aa8898b1280050d638f6b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:56:40 +0300 Subject: [PATCH 256/339] Test --- iguana/exchanges/LP_bitcoin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index b8d92440d..7b403117d 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -1344,7 +1344,7 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR for (i=0; i<20; i++) printf("%02x",rmd160[i]); printf(" <- rmd160 databuf\n");*/ - if ( 0 && datalen == 32 ) + if ( datalen == 32 ) calc_rmd160_sha256(rmd160,revdatabuf,datalen); else calc_rmd160_sha256(rmd160,databuf,datalen); iguana_pushdata(stacks,0,rmd160,sizeof(rmd160)); From d4f93281ad587970b5671b34625927abb1161b01 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:58:15 +0300 Subject: [PATCH 257/339] Test --- iguana/exchanges/LP_remember.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 621795f6f..dd37a8573 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -753,8 +753,8 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti rev.bytes[j] = privAm.bytes[31 - j]; //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); - redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); + redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,privAm/*rev*/,privBn,secretAm,secretAm256,secretBn,secretBn256); + len = basilisk_swapuserdata(userdata,privAm/*rev*/,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); From b4f31059657f36cd399dc1c864814f7822aaf199 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 21:59:54 +0300 Subject: [PATCH 258/339] Test --- iguana/exchanges/LP_remember.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index dd37a8573..6f0c7fea0 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -751,10 +751,10 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti // alicespend for (j=0; j<32; j++) rev.bytes[j] = privAm.bytes[31 - j]; - //revcalc_rmd160_sha256(secretAm,rev);//privAm); - //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); - redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,privAm/*rev*/,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,privAm/*rev*/,0,myprivs[0],redeemscript,redeemlen); + revcalc_rmd160_sha256(secretAm,rev);//privAm); + vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); + redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); + len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); From 3aebe5264debfe9d49a1b3c48dc598d56c4fe090 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:02:16 +0300 Subject: [PATCH 259/339] Test --- iguana/exchanges/LP_bitcoin.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 7b403117d..f475d2cc1 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -1295,14 +1295,14 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR else { iguana_pushdata(stacks,0,0,0); - for (i=0; iopcode,args[0].size,args[1].size); - } + } else if ( (op->flags & IGUANA_CRYPTOFLAG) != 0 ) { From 49e72c521f432a936e482a9b74e69923e0f79905 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:03:46 +0300 Subject: [PATCH 260/339] Test --- iguana/exchanges/LP_bitcoin.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index f475d2cc1..dd6cf585b 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -797,7 +797,7 @@ int32_t iguana_checksig(void *ctx,struct iguana_stackdata pubkeyarg,struct iguan if ( (retval= (bitcoin_verify(ctx,sig,siglen-1,sigtxid,pubkey,plen) == 0)) == 0 ) { } - if ( (0) ) + if ( (1) ) { int32_t i; char str[65]; for (i=0; iopcode,args[0].size,args[1].size); - + } } else if ( (op->flags & IGUANA_CRYPTOFLAG) != 0 ) { From 43a8954ebd1d1b96d87785f723115c0ea15d200d Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:04:37 +0300 Subject: [PATCH 261/339] Test --- iguana/exchanges/LP_bitcoin.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index dd6cf585b..c6ac462d6 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -792,6 +792,7 @@ int32_t iguana_checksig(void *ctx,struct iguana_stackdata pubkeyarg,struct iguan uint8_t pubkey[MAX_SCRIPT_ELEMENT_SIZE],sig[MAX_SCRIPT_ELEMENT_SIZE]; int32_t retval,plen,siglen; plen = iguana_databuf(pubkey,pubkeyarg); siglen = iguana_databuf(sig,sigarg); + printf("checksig\n"); if ( bitcoin_pubkeylen(pubkey) == plen && plen > 0 && siglen > 0 && siglen < 74 ) { if ( (retval= (bitcoin_verify(ctx,sig,siglen-1,sigtxid,pubkey,plen) == 0)) == 0 ) From df4374fc28dea9983444bb43d723d3fa754f9710 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:07:47 +0300 Subject: [PATCH 262/339] Test --- iguana/exchanges/LP_bitcoin.c | 1 - iguana/exchanges/LP_transaction.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index c6ac462d6..dd6cf585b 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -792,7 +792,6 @@ int32_t iguana_checksig(void *ctx,struct iguana_stackdata pubkeyarg,struct iguan uint8_t pubkey[MAX_SCRIPT_ELEMENT_SIZE],sig[MAX_SCRIPT_ELEMENT_SIZE]; int32_t retval,plen,siglen; plen = iguana_databuf(pubkey,pubkeyarg); siglen = iguana_databuf(sig,sigarg); - printf("checksig\n"); if ( bitcoin_pubkeylen(pubkey) == plen && plen > 0 && siglen > 0 && siglen < 74 ) { if ( (retval= (bitcoin_verify(ctx,sig,siglen-1,sigtxid,pubkey,plen) == 0)) == 0 ) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index d7f2f3f05..e3db71fe8 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -379,7 +379,7 @@ int32_t bitcoin_verifyvins(void *ctx,char *symbol,uint8_t pubtype,uint8_t p2shty { script = V->p2shscript; scriptlen = V->p2shlen; - //printf("V->p2shlen.%d\n",V->p2shlen); + printf("V->p2shlen.%d\n",V->p2shlen); } else { From 17bb635e6b96487e3b15eac7176a254deda1f91b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:09:09 +0300 Subject: [PATCH 263/339] Test --- iguana/exchanges/LP_transaction.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index e3db71fe8..777ee155d 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -379,7 +379,9 @@ int32_t bitcoin_verifyvins(void *ctx,char *symbol,uint8_t pubtype,uint8_t p2shty { script = V->p2shscript; scriptlen = V->p2shlen; - printf("V->p2shlen.%d\n",V->p2shlen); + for (j=0; jp2shlen.%d\n",V->p2shlen); } else { From 4160744795ab2cda09b0098ce7e71c91c7b18207 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:16:26 +0300 Subject: [PATCH 264/339] Test --- iguana/exchanges/LP_remember.c | 4 ++-- iguana/exchanges/LP_transaction.c | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 6f0c7fea0..621795f6f 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -751,8 +751,8 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti // alicespend for (j=0; j<32; j++) rev.bytes[j] = privAm.bytes[31 - j]; - revcalc_rmd160_sha256(secretAm,rev);//privAm); - vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); + //revcalc_rmd160_sha256(secretAm,rev);//privAm); + //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); { diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 777ee155d..a7315d8f3 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -379,9 +379,9 @@ int32_t bitcoin_verifyvins(void *ctx,char *symbol,uint8_t pubtype,uint8_t p2shty { script = V->p2shscript; scriptlen = V->p2shlen; - for (j=0; jp2shlen.%d\n",V->p2shlen); + //for (j=0; jp2shlen.%d\n",V->p2shlen); } else { From 3bb4fbf4f57b52e3e9119ed600172b4296aa8590 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:18:08 +0300 Subject: [PATCH 265/339] Test --- iguana/exchanges/LP_bitcoin.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index dd6cf585b..95d6db4fa 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -797,7 +797,7 @@ int32_t iguana_checksig(void *ctx,struct iguana_stackdata pubkeyarg,struct iguan if ( (retval= (bitcoin_verify(ctx,sig,siglen-1,sigtxid,pubkey,plen) == 0)) == 0 ) { } - if ( (1) ) + if ( (0) ) { int32_t i; char str[65]; for (i=0; iopcode,args[0].size,args[1].size); - } + //} } else if ( (op->flags & IGUANA_CRYPTOFLAG) != 0 ) { From 94861a0c68190b6b3fbda6b110a8a167c2a45f06 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:19:01 +0300 Subject: [PATCH 266/339] Test --- iguana/exchanges/LP_bitcoin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 95d6db4fa..df28e1bce 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -1344,7 +1344,7 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR for (i=0; i<20; i++) printf("%02x",rmd160[i]); printf(" <- rmd160 databuf\n");*/ - if ( datalen == 32 ) + if ( 0 && datalen == 32 ) calc_rmd160_sha256(rmd160,revdatabuf,datalen); else calc_rmd160_sha256(rmd160,databuf,datalen); iguana_pushdata(stacks,0,rmd160,sizeof(rmd160)); From 5c3c0d559221c735a79baecb8d8038c689663397 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:20:03 +0300 Subject: [PATCH 267/339] Test --- iguana/exchanges/LP_remember.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 621795f6f..36bc245e8 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -753,7 +753,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti rev.bytes[j] = privAm.bytes[31 - j]; //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); - redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); + redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,privAm/*rev*/,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; From c01b014d13a127b7bc38de51660dc28ef524e2dd Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:21:11 +0300 Subject: [PATCH 268/339] Test --- iguana/exchanges/LP_remember.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 36bc245e8..dd37a8573 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -754,7 +754,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,privAm/*rev*/,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); + len = basilisk_swapuserdata(userdata,privAm/*rev*/,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); From 3e2709c07c310e338acbcd510bcfc213982aa1e5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:37:45 +0300 Subject: [PATCH 269/339] Test --- iguana/exchanges/LP_bitcoin.c | 2 +- iguana/exchanges/LP_remember.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index df28e1bce..95d6db4fa 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -1344,7 +1344,7 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR for (i=0; i<20; i++) printf("%02x",rmd160[i]); printf(" <- rmd160 databuf\n");*/ - if ( 0 && datalen == 32 ) + if ( datalen == 32 ) calc_rmd160_sha256(rmd160,revdatabuf,datalen); else calc_rmd160_sha256(rmd160,databuf,datalen); iguana_pushdata(stacks,0,rmd160,sizeof(rmd160)); diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index dd37a8573..621795f6f 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -753,8 +753,8 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti rev.bytes[j] = privAm.bytes[31 - j]; //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); - redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,privAm/*rev*/,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,privAm/*rev*/,0,myprivs[0],redeemscript,redeemlen); + redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); + len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); From 6be5e9404762a9388d11e3b661a9217e6113466b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:39:12 +0300 Subject: [PATCH 270/339] Test --- iguana/exchanges/LP_remember.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 621795f6f..dcf45c3db 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -753,8 +753,8 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti rev.bytes[j] = privAm.bytes[31 - j]; //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); - redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); + redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,privAm,privBn,secretAm,secretAm256,secretBn,secretBn256); + len = basilisk_swapuserdata(userdata,privAm,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); From c88de3feb5fcf7b0c75b53ae2ffa7d3fa1730d04 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:40:08 +0300 Subject: [PATCH 271/339] Test --- iguana/exchanges/LP_remember.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index dcf45c3db..0e2cba2d1 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -761,7 +761,9 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti bitcoin_address(privaddr,60,privpub33,33); printf("alicespend len.%d redeemlen.%d priv0addr.(%s) priv0.(%s)\n",len,redeemlen,privaddr,bits256_str(str,myprivs[0])); } - if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0,bobpaymentaddr)) != 0 ) + for (j=0; j<32; j++) + rev.bytes[j] = myprivs[0].bytes[31 - j]; + if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rev/*myprivs[0]*/,0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0,bobpaymentaddr)) != 0 ) printf("alicespend.(%s)\n",txbytes[BASILISK_ALICESPEND]); } } From 49847eb688a17fbba36951eee5acaf1f183d5d06 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:49:54 +0300 Subject: [PATCH 272/339] Test --- iguana/exchanges/LP_bitcoin.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 95d6db4fa..4b84e137c 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -1569,6 +1569,7 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR { jadd(interpreter,"result",jfalse()); printf("Evaluate FALSE, depth.%d errs.%d [0] size.%d num.%d\n",stacks->stackdepth,errs,stacks->stack[0].size,stacks->stack[0].U.val); + errs++; if ( stacks->logarray != 0 ) printf("LOG.(%s)\n\n",jprint(stacks->logarray,0)); } From 8b17d32b2ee189a3a8cf31ec2c7702733f324ff8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:56:48 +0300 Subject: [PATCH 273/339] Test --- iguana/exchanges/LP_bitcoin.c | 20 ++++++++++---------- iguana/exchanges/LP_transaction.c | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 4b84e137c..4b6309ac7 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -564,12 +564,12 @@ static int32_t iguana_pushdata(struct iguana_interpreter *stacks,int64_t num64,u //printf("PUSH.(%lld %p %d)\n",(long long)num64,numbuf,numlen); if ( stacks->maxstackdepth > 0 ) { - /*if ( numbuf != 0 ) - { - int32_t i; for (i=0; istackdepth < stacks->maxstackdepth ) { if ( stacks->logarray != 0 ) @@ -1127,7 +1127,7 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR break; } HASH_FIND(hh,OPTABLE,str,j,op); - //printf("{%s}\n",str); + printf("{%s}\n",str); str += j; if ( op != 0 ) { @@ -1295,14 +1295,14 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR else { iguana_pushdata(stacks,0,0,0); - } for (i=0; iopcode,args[0].size,args[1].size); - //} + } } else if ( (op->flags & IGUANA_CRYPTOFLAG) != 0 ) { @@ -1568,7 +1568,7 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR else { jadd(interpreter,"result",jfalse()); - printf("Evaluate FALSE, depth.%d errs.%d [0] size.%d num.%d\n",stacks->stackdepth,errs,stacks->stack[0].size,stacks->stack[0].U.val); + printf("Evaluate FALSE, depth.%d errs.%d [0] size.%d val.%d\n",stacks->stackdepth,errs,stacks->stack[0].size,stacks->stack[0].U.val); errs++; if ( stacks->logarray != 0 ) printf("LOG.(%s)\n\n",jprint(stacks->logarray,0)); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index a7315d8f3..2b7429640 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -571,7 +571,7 @@ int32_t iguana_signrawtransaction(void *ctx,char *symbol,uint8_t pubtype,uint8_t if ( (complete= bitcoin_verifyvins(ctx,symbol,pubtype,p2shtype,isPoS,height,signedtxidp,&signedtx,msgtx,serialized3,maxsize,V,SIGHASH_ALL,1,V->suppress_pubkeys)) > 0 && signedtx != 0 ) { int32_t tmp; //char str[65]; - if ( (tmp= iguana_interpreter(ctx,0,iguana_lockval(finalized,jint(txobj,"locktime")),V,numinputs)) < 0 ) + if ( (tmp= iguana_interpreter(ctx,cJSON_CreateArray(),iguana_lockval(finalized,jint(txobj,"locktime")),V,numinputs)) < 0 ) { printf("iguana_interpreter %d error.(%s)\n",tmp,signedtx); complete = 0; From 8f5e8b5cca2b58cc85a40000bca309a46d0b327e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 22:58:49 +0300 Subject: [PATCH 274/339] Test --- iguana/exchanges/LP_remember.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 0e2cba2d1..b93b8ee95 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -763,7 +763,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti } for (j=0; j<32; j++) rev.bytes[j] = myprivs[0].bytes[31 - j]; - if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rev/*myprivs[0]*/,0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0,bobpaymentaddr)) != 0 ) + if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0,bobpaymentaddr)) != 0 ) printf("alicespend.(%s)\n",txbytes[BASILISK_ALICESPEND]); } } From 8f29ff4babaa6545e86814e884d8cd7bb9bd7c2e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 23:01:31 +0300 Subject: [PATCH 275/339] Test --- iguana/exchanges/LP_bitcoin.c | 2 +- iguana/exchanges/LP_remember.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 4b6309ac7..fb1b2f13c 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -1344,7 +1344,7 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR for (i=0; i<20; i++) printf("%02x",rmd160[i]); printf(" <- rmd160 databuf\n");*/ - if ( datalen == 32 ) + if ( 0 && datalen == 32 ) calc_rmd160_sha256(rmd160,revdatabuf,datalen); else calc_rmd160_sha256(rmd160,databuf,datalen); iguana_pushdata(stacks,0,rmd160,sizeof(rmd160)); diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index b93b8ee95..dec78b521 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -753,8 +753,8 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti rev.bytes[j] = privAm.bytes[31 - j]; //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); - redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,privAm,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,privAm,0,myprivs[0],redeemscript,redeemlen); + redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); + len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); From 29cbde9ee20fdc5c54687755e56efcc6a0658391 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 23:03:09 +0300 Subject: [PATCH 276/339] Test --- iguana/exchanges/LP_remember.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index dec78b521..b93b8ee95 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -753,8 +753,8 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti rev.bytes[j] = privAm.bytes[31 - j]; //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); - redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); + redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,privAm,privBn,secretAm,secretAm256,secretBn,secretBn256); + len = basilisk_swapuserdata(userdata,privAm,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); From a54b729addc0a93c1124cf38e4d0fa469bd8bab2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 23:04:05 +0300 Subject: [PATCH 277/339] Test --- iguana/exchanges/LP_bitcoin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index fb1b2f13c..4b6309ac7 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -1344,7 +1344,7 @@ int32_t bitcoin_assembler(void *ctx,cJSON *logarray,uint8_t script[IGUANA_MAXSCR for (i=0; i<20; i++) printf("%02x",rmd160[i]); printf(" <- rmd160 databuf\n");*/ - if ( 0 && datalen == 32 ) + if ( datalen == 32 ) calc_rmd160_sha256(rmd160,revdatabuf,datalen); else calc_rmd160_sha256(rmd160,databuf,datalen); iguana_pushdata(stacks,0,rmd160,sizeof(rmd160)); From d92a0edcc28d86059b0e8908029b4bc275d26982 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 23:10:11 +0300 Subject: [PATCH 278/339] Test --- iguana/exchanges/LP_transaction.c | 12 ------------ iguana/exchanges/mm.c | 2 +- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 2b7429640..1990ca089 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -643,18 +643,6 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch } jaddbits256(item,"txid",utxotxid); jaddnum(item,"vout",vout); - /*int32_t i; - for (i=0; i<33; i++) - printf("%02x",pubkey33[i]); - printf(" pubkey33 ->\n"); - for (i=0; i<20; i++) - printf("%02x",rmd160[i]); - printf(" destaddr.(%s)\n",destaddr); - calc_rmd160_sha256(rmd160,pubkey33,33); - for (i=0; i<20; i++) - printf("%02x",rmd160[i]); - printf(" <- vs direct calc\n");*/ - //spendlen = bitcoin_standardspend(spendscript,0,rmd160); bitcoin_address(tmpaddr,pubtype,pubkey33,33); bitcoin_addr2rmd160(&addrtype,rmd160,tmpaddr); if ( redeemlen != 0 ) diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index f2dffc4de..060e85014 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -829,7 +829,7 @@ int main(int argc, const char * argv[]) printf("error launching LP_main (%s)\n",jprint(retjson,0)); exit(-1); } else printf("(%s) launched.(%s)\n",argv[1],passphrase); - if ( (retstr= basilisk_swaplist()) != 0 ) + if ( (0) && (retstr= basilisk_swaplist()) != 0 ) { printf("%s\ngetchar to continue\n",retstr); getchar(); From 5ecdd9bd6929a2e3dee2d40478ccd93ba22eb369 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Jun 2017 23:13:04 +0300 Subject: [PATCH 279/339] Test --- iguana/exchanges/LP_remember.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index b93b8ee95..dec78b521 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -753,8 +753,8 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti rev.bytes[j] = privAm.bytes[31 - j]; //revcalc_rmd160_sha256(secretAm,rev);//privAm); //vcalc_sha256(0,secretAm256,rev.bytes,sizeof(rev)); - redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,privAm,privBn,secretAm,secretAm256,secretBn,secretBn256); - len = basilisk_swapuserdata(userdata,privAm,0,myprivs[0],redeemscript,redeemlen); + redeemlen = basilisk_swap_bobredeemscript(0,&secretstart,redeemscript,plocktime,pubA0,pubB0,pubB1,rev,privBn,secretAm,secretAm256,secretBn,secretBn256); + len = basilisk_swapuserdata(userdata,rev,0,myprivs[0],redeemscript,redeemlen); { char privaddr[64]; uint8_t privpub33[33]; bitcoin_pubkey33(ctx,privpub33,myprivs[0]); From 326d4e99957f059666c8c13d58ad9022585464d3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 2 Jun 2017 08:53:56 +0300 Subject: [PATCH 280/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 ++-- iguana/exchanges/LP_remember.c | 6 ++++++ iguana/exchanges/LP_transaction.c | 10 +++++----- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index c056d38d8..634fca3fd 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -781,7 +781,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb { char tmpstr[128]; bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); - //printf("%s coinaddr.(%s) %d wif.(%s) passphrase.(%s)\n",symbol,coinaddr,coin->pubtype,tmpstr,passphrase); + printf("%s (%s) %d wif.(%s) (%s)\n",symbol,coinaddr,coin->pubtype,tmpstr,passphrase); if ( (retjson= LP_importprivkey(coin->symbol,tmpstr,coinaddr,-1)) != 0 ) printf("importprivkey -> (%s)\n",jprint(retjson,1)); } @@ -814,7 +814,7 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb if ( amclient != 0 ) targetval = (depositval / 776) + 50000; else targetval = (depositval / 9) * 8; - printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); + //printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); if ( (i= LP_nearestvalue(values,n,targetval)) >= 0 ) { item = jitem(array,i); diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index dec78b521..52f511e77 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -212,6 +212,12 @@ void basilisk_dontforget(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx LP_importaddress(swap->alicecoin.symbol,coinaddr); fprintf(fp,",\"Apayment\":\"%s\"",coinaddr); } + if ( rawtx->I.redeemlen > 0 ) + { + char scriptstr[2049]; + init_hexbytes_noT(scriptstr,rawtx->redeemscript,rawtx->I.redeemlen); + fprintf(fp,",\"redeem\":\"%s\"",scriptstr); + } /*basilisk_dontforget_userdata("Aclaim",fp,swap->I.userdata_aliceclaim,swap->I.userdata_aliceclaimlen); basilisk_dontforget_userdata("Areclaim",fp,swap->I.userdata_alicereclaim,swap->I.userdata_alicereclaimlen); basilisk_dontforget_userdata("Aspend",fp,swap->I.userdata_alicespend,swap->I.userdata_alicespendlen); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 1990ca089..8c8a2def2 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1223,11 +1223,11 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i { for (j=0; jbobpayment.I.datalen; j++) printf("%02x",swap->bobpayment.txbytes[j]); - //printf(" <- bobpayment.%d\n",swap->bobpayment.datalen); - //for (j=0; jbobpayment.redeemlen; j++) - // printf("%02x",swap->bobpayment.redeemscript[j]); - //printf(" <- redeem.%d\n",swap->bobpayment.redeemlen); - printf(" <- GENERATED BOB PAYMENT.%d\n",swap->bobpayment.I.datalen); + 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); LP_unspents_mark(swap->bobcoin.symbol,swap->bobpayment.vins); //basilisk_bobpayment_reclaim(swap,swap->I.callduration); //printf("bobscripts set completed\n"); From d1c3c246d7b1bfcd8594363919bea11dd77dc465 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 2 Jun 2017 09:15:14 +0300 Subject: [PATCH 281/339] Test --- iguana/exchanges/LP_nativeDEX.c | 4 +- iguana/exchanges/LP_swap.c | 6 +- iguana/exchanges/LP_transaction.c | 106 +++++++++++++----------------- 3 files changed, 52 insertions(+), 64 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 634fca3fd..ce8a3b97c 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -25,7 +25,7 @@ #define LP_PROPAGATION_SLACK 10 // txid ordering is not enforced, so getting extra recent txid -char *activecoins[] = { "BTC", "KMD", };//"LTC", "USD", "REVS", "JUMBLR" }; +char *activecoins[] = { "BTC", "KMD", "REVS", "JUMBLR" };//"LTC", "USD", }; char *default_LPnodes[] = { "5.9.253.196", "5.9.253.197", "5.9.253.198", "5.9.253.199", "5.9.253.200", "5.9.253.201", "5.9.253.202", "5.9.253.203", "5.9.253.204" }; //"5.9.253.195", portable_mutex_t LP_peermutex,LP_utxomutex,LP_commandmutex,LP_cachemutex; @@ -338,7 +338,7 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 portable_mutex_unlock(&LP_utxomutex); if ( mypubsock >= 0 ) LP_send(mypubsock,jprint(LP_utxojson(utxo),1),1); - char str[65],str2[65]; printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d %s %s\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0,bits256_str(str,utxo->txid),bits256_str(str2,txid)); + //char str[65],str2[65]; printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d %s %s\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0,bits256_str(str,utxo->txid),bits256_str(str2,txid)); } return(utxo); } diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 72a1804d6..5d4a085d8 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -536,8 +536,8 @@ void LP_bobloop(void *_utxo) printf("error waitsend choosei\n"); else if ( LP_waitsend("mostprivs",10,utxo->pair,swap,data,maxlen,LP_mostprivs_verify,LP_mostprivs_data) < 0 ) printf("error waitsend mostprivs\n"); - else if ( basilisk_bobscripts_set(swap,1,1) < 0 || basilisk_bobscripts_set(swap,0,1) < 0 ) - printf("error bobscripts\n"); + else if ( basilisk_bobscripts_set(swap,1,1) < 0 ) + printf("error bobscripts deposit\n"); else { LP_swapsfp_update(&swap->I.req); @@ -547,6 +547,8 @@ void LP_bobloop(void *_utxo) printf("error sending bobdeposit\n"); else if ( LP_waitfor(utxo->pair,swap,10,LP_verify_alicepayment) < 0 ) printf("error waiting for alicepayment\n"); + else if ( basilisk_bobscripts_set(swap,0,1) < 0 ) + printf("error bobscripts payment\n"); else if ( LP_swapdata_rawtxsend(utxo->pair,swap,0x8000,data,maxlen,&swap->bobpayment,0x4000,0) == 0 ) printf("error sending bobpayment\n"); while ( 1 ) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 8c8a2def2..6ee6499f7 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1150,7 +1150,6 @@ int32_t basilisk_swapuserdata(uint8_t *userdata,bits256 privkey,int32_t ifpath,b return(len); } -#ifdef old /*Bob paytx: OP_IF OP_CLTV OP_DROP OP_CHECKSIG @@ -1165,7 +1164,7 @@ int32_t basilisk_bobpayment_reclaim(struct basilisk_swap *swap,int32_t delay) len = basilisk_swapuserdata(userdata,zero,1,swap->I.myprivs[1],swap->bobpayment.redeemscript,swap->bobpayment.I.redeemlen); memcpy(swap->I.userdata_bobreclaim,userdata,len); swap->I.userdata_bobreclaimlen = len; - if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->bobreclaim,&swap->bobpayment,swap->I.myprivs[1],0,userdata,len,1,swap->changermd160)) == 0 ) + if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->bobreclaim,&swap->bobpayment,swap->I.myprivs[1],0,userdata,len,1,swap->changermd160,swap->bobpayment.I.destaddr)) == 0 ) { for (i=0; ibobreclaim.I.datalen; i++) printf("%02x",swap->bobreclaim.txbytes[i]); @@ -1182,7 +1181,7 @@ int32_t basilisk_bobdeposit_refund(struct basilisk_swap *swap,int32_t delay) len = basilisk_swapuserdata(userdata,swap->I.privBn,0,swap->I.myprivs[0],swap->bobdeposit.redeemscript,swap->bobdeposit.I.redeemlen); memcpy(swap->I.userdata_bobrefund,userdata,len); swap->I.userdata_bobrefundlen = len; - if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->bobrefund,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,0,swap->changermd160)) == 0 ) + if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->bobrefund,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,0,swap->changermd160,swap->bobdeposit.I.destaddr)) == 0 ) { for (i=0; ibobrefund.I.datalen; i++) printf("%02x",swap->bobrefund.txbytes[i]); @@ -1192,7 +1191,6 @@ int32_t basilisk_bobdeposit_refund(struct basilisk_swap *swap,int32_t delay) } return(-1); } -#endif int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,int32_t genflag) { @@ -1204,35 +1202,31 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i swap->bobpayment.I.spendlen = basilisk_bobscript(swap->bobpayment.I.rmd160,swap->bobpayment.redeemscript,&swap->bobpayment.I.redeemlen,swap->bobpayment.spendscript,0,&swap->bobpayment.I.locktime,&swap->bobpayment.I.secretstart,&swap->I,0); bitcoin_address(swap->bobpayment.p2shaddr,swap->bobcoin.p2shtype,swap->bobpayment.redeemscript,swap->bobpayment.I.redeemlen); strcpy(swap->bobpayment.I.destaddr,swap->bobpayment.p2shaddr); - //for (i=0; ibobpayment.redeemlen; i++) - // printf("%02x",swap->bobpayment.redeemscript[i]); - //printf(" <- bobpayment.%d\n",i); + int32_t i; for (i=0; ibobpayment.I.redeemlen; i++) + printf("%02x",swap->bobpayment.redeemscript[i]); + printf(" <- bobpayment redeem %d %s\n",i,swap->bobpayment.I.destaddr); if ( genflag != 0 && bits256_nonz(*(bits256 *)swap->I.secretBn256) != 0 && swap->bobpayment.I.datalen == 0 ) { - //for (i=0; i<3; i++) + basilisk_rawtx_gen(swap->ctx,"payment",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobpayment,swap->bobpayment.I.locktime,swap->bobpayment.spendscript,swap->bobpayment.I.spendlen,swap->bobpayment.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0); + if ( swap->bobpayment.I.spendlen == 0 || swap->bobpayment.I.datalen == 0 ) { - //if ( swap->bobpayment.txbytes != 0 && swap->bobpayment.I.spendlen != 0 ) - // break; - basilisk_rawtx_gen(swap->ctx,"payment",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobpayment,swap->bobpayment.I.locktime,swap->bobpayment.spendscript,swap->bobpayment.I.spendlen,swap->bobpayment.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0); - if ( swap->bobpayment.I.spendlen == 0 || swap->bobpayment.I.datalen == 0 ) - { - printf("error bob generating %p payment.%d\n",swap->bobpayment.txbytes,swap->bobpayment.I.spendlen); - sleep(DEX_SLEEP); - } - else - { - 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); - LP_unspents_mark(swap->bobcoin.symbol,swap->bobpayment.vins); - //basilisk_bobpayment_reclaim(swap,swap->I.callduration); - //printf("bobscripts set completed\n"); - return(0); - } + printf("error bob generating %p payment.%d\n",swap->bobpayment.txbytes,swap->bobpayment.I.spendlen); + sleep(DEX_SLEEP); + } + else + { + 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); + LP_unspents_mark(swap->bobcoin.symbol,swap->bobpayment.vins); + if ( swap->I.iambob != 0 ) + basilisk_bobpayment_reclaim(swap,swap->I.callduration); + //printf("bobscripts set completed\n"); + return(0); } } } @@ -1241,37 +1235,29 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i swap->bobdeposit.I.spendlen = basilisk_bobscript(swap->bobdeposit.I.rmd160,swap->bobdeposit.redeemscript,&swap->bobdeposit.I.redeemlen,swap->bobdeposit.spendscript,0,&swap->bobdeposit.I.locktime,&swap->bobdeposit.I.secretstart,&swap->I,1); bitcoin_address(swap->bobdeposit.p2shaddr,swap->bobcoin.p2shtype,swap->bobdeposit.redeemscript,swap->bobdeposit.I.redeemlen); strcpy(swap->bobdeposit.I.destaddr,swap->bobdeposit.p2shaddr); + int32_t i; for (i=0; ibobdeposit.I.redeemlen; i++) + printf("%02x",swap->bobdeposit.redeemscript[i]); + printf(" <- bobdeposit redeem %d %s\n",i,swap->bobdeposit.I.destaddr); if ( genflag != 0 && (swap->bobdeposit.I.datalen == 0 || swap->bobrefund.I.datalen == 0) ) { - //for (i=0; i<3; i++) + basilisk_rawtx_gen(swap->ctx,"deposit",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobdeposit,swap->bobdeposit.I.locktime,swap->bobdeposit.spendscript,swap->bobdeposit.I.spendlen,swap->bobdeposit.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0); + if ( swap->bobdeposit.I.datalen == 0 || swap->bobdeposit.I.spendlen == 0 ) { - //if ( swap->bobdeposit.txbytes != 0 && swap->bobdeposit.I.spendlen != 0 ) - // break; - basilisk_rawtx_gen(swap->ctx,"deposit",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobdeposit,swap->bobdeposit.I.locktime,swap->bobdeposit.spendscript,swap->bobdeposit.I.spendlen,swap->bobdeposit.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0); - if ( swap->bobdeposit.I.datalen == 0 || swap->bobdeposit.I.spendlen == 0 ) - { - printf("error bob generating %p deposit.%d\n",swap->bobdeposit.txbytes,swap->bobdeposit.I.spendlen); - sleep(DEX_SLEEP); - } - else - { - for (j=0; jbobdeposit.I.datalen; j++) - printf("%02x",swap->bobdeposit.txbytes[j]); - printf(" <- GENERATED BOB DEPOSIT.%d\n",swap->bobdeposit.I.datalen); - //for (j=0; jbobdeposit.redeemlen; j++) - // printf("%02x",swap->bobdeposit.redeemscript[j]); - //printf(" <- redeem.%d\n",swap->bobdeposit.redeemlen); - //printf("GENERATED BOB DEPOSIT\n"); - LP_unspents_mark(swap->bobcoin.symbol,swap->bobdeposit.vins); - //basilisk_bobdeposit_refund(swap,swap->I.putduration); - printf("bobscripts set completed\n"); - return(0); - } + printf("error bob generating %p deposit.%d\n",swap->bobdeposit.txbytes,swap->bobdeposit.I.spendlen); + sleep(DEX_SLEEP); + } + else + { + for (j=0; jbobdeposit.I.datalen; j++) + printf("%02x",swap->bobdeposit.txbytes[j]); + printf(" <- GENERATED BOB DEPOSIT.%d\n",swap->bobdeposit.I.datalen); + LP_unspents_mark(swap->bobcoin.symbol,swap->bobdeposit.vins); + if ( swap->I.iambob != 0 ) + basilisk_bobdeposit_refund(swap,swap->I.putduration); + printf("bobscripts set completed\n"); + return(0); } } - //for (i=0; ibobdeposit.redeemlen; i++) - // printf("%02x",swap->bobdeposit.redeemscript[i]); - //printf(" <- bobdeposit.%d\n",i); } return(0); } @@ -1532,14 +1518,14 @@ int32_t LP_verify_bobpayment(struct basilisk_swap *swap,uint8_t *data,int32_t da swap->I.userdata_alicespendlen = len; retval = 0; char str[65],str2[65]; printf("bobpaid privAm.(%s) myprivs[0].(%s)\n",bits256_str(str,swap->I.privAm),bits256_str(str2,swap->I.myprivs[0])); - //if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->alicespend,&swap->bobpayment,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) + if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->alicespend,&swap->bobpayment,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160,swap->alicepayment.I.destaddr)) == 0 ) { for (i=0; ibobpayment.I.datalen; i++) printf("%02x",swap->bobpayment.txbytes[i]); printf(" <- bobpayment\n"); - //for (i=0; ialicespend.I.datalen; i++) - // printf("%02x",swap->alicespend.txbytes[i]); - //printf(" <- alicespend\n\n"); + for (i=0; ialicespend.I.datalen; i++) + printf("%02x",swap->alicespend.txbytes[i]); + printf(" <- alicespend\n\n"); swap->I.alicespent = 1; //basilisk_txlog(swap,&swap->alicespend,-1); return(retval); From 71ea567ca40419a51b775f8721c86ce094b18eab Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 2 Jun 2017 11:21:12 +0300 Subject: [PATCH 282/339] batch17 --- iguana/main.c | 6 +- iguana/tests/KMD.batch17 | 433 +++++++++++++++++++++++++++ iguana/tests/KMD.batch17.listunspent | 345 +++++++++++++++++++++ iguana/tests/REVS.batch17 | 5 + 4 files changed, 787 insertions(+), 2 deletions(-) create mode 100755 iguana/tests/KMD.batch17 create mode 100755 iguana/tests/KMD.batch17.listunspent create mode 100755 iguana/tests/REVS.batch17 diff --git a/iguana/main.c b/iguana/main.c index 9a224d3d2..335d68baa 100755 --- a/iguana/main.c +++ b/iguana/main.c @@ -2056,10 +2056,12 @@ FOUR_STRINGS(SuperNET,login,handle,password,permanentfile,passphrase) void komodo_ICO_batch(cJSON *array,int32_t batchid) { int32_t i,n,iter; cJSON *item; uint64_t kmdamount,revsamount; char *coinaddr,cmd[512]; double totalKMD,totalREVS; struct supernet_info *myinfo = SuperNET_MYINFO(0); + if ( myinfo->rpcport == 0 ) + myinfo->rpcport = 7778; if ( (n= cJSON_GetArraySize(array)) > 0 ) { totalKMD = totalREVS = 0; - for (iter=3; iter<4; iter++) + for (iter=0; iter<1; iter++) for (i=0; i Date: Fri, 2 Jun 2017 11:50:16 +0300 Subject: [PATCH 283/339] sleep --- iguana/tests/KMD.batch17 | 1 + iguana/tests/KMD.batch17.listunspent | 1 + 2 files changed, 2 insertions(+) diff --git a/iguana/tests/KMD.batch17 b/iguana/tests/KMD.batch17 index 6f08faea9..2b5513271 100755 --- a/iguana/tests/KMD.batch17 +++ b/iguana/tests/KMD.batch17 @@ -1,3 +1,4 @@ +sleep 9999999 # RCJHEogA7SW6PxuctPLtaVnXwiu49PyZY8 KMD 108998.28313606 ./komodo-cli sendtoaddress RCJHEogA7SW6PxuctPLtaVnXwiu49PyZY8 108998.28313606 sleep 3 diff --git a/iguana/tests/KMD.batch17.listunspent b/iguana/tests/KMD.batch17.listunspent index a59ac7f3b..bffaab46a 100755 --- a/iguana/tests/KMD.batch17.listunspent +++ b/iguana/tests/KMD.batch17.listunspent @@ -1,3 +1,4 @@ +sleep 999999 # RCJHEogA7SW6PxuctPLtaVnXwiu49PyZY8 KMD 108998.28313606 curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"dex\",\"method\":\"listunspent\",\"address\":\"RCJHEogA7SW6PxuctPLtaVnXwiu49PyZY8\",\"symbol\":\"KMD\"}" echo "108998.28313606 <- expected amount RCJHEogA7SW6PxuctPLtaVnXwiu49PyZY8" From ac3d4780316697a99c16f719b9b5cb1c09df38e6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 2 Jun 2017 16:26:02 +0300 Subject: [PATCH 284/339] Test --- iguana/exchanges/LP_bitcoin.c | 307 +++++++++++ iguana/exchanges/LP_coins.c | 196 +++++++ iguana/exchanges/LP_commands.c | 35 +- iguana/exchanges/LP_include.h | 375 ++----------- iguana/exchanges/LP_nativeDEX.c | 839 +---------------------------- iguana/exchanges/LP_network.c | 229 -------- iguana/exchanges/LP_peers.c | 188 +++++++ iguana/exchanges/LP_prices.c | 162 +++++- iguana/exchanges/LP_remember.c | 139 ----- iguana/exchanges/LP_rpc.c | 50 +- iguana/exchanges/LP_statemachine.c | 311 +++++++++++ iguana/exchanges/LP_transaction.c | 31 -- iguana/exchanges/LP_utxos.c | 327 +++++++++++ iguana/exchanges/mm.c | 2 +- 14 files changed, 1617 insertions(+), 1574 deletions(-) create mode 100644 iguana/exchanges/LP_coins.c create mode 100644 iguana/exchanges/LP_peers.c create mode 100644 iguana/exchanges/LP_utxos.c diff --git a/iguana/exchanges/LP_bitcoin.c b/iguana/exchanges/LP_bitcoin.c index 4b6309ac7..c7ee8b218 100644 --- a/iguana/exchanges/LP_bitcoin.c +++ b/iguana/exchanges/LP_bitcoin.c @@ -18,6 +18,313 @@ // marketmaker // +union iguana_stacknum { int32_t val; int64_t val64; uint8_t rmd160[20]; bits256 hash2; uint8_t pubkey[33]; uint8_t sig[74]; }; +struct iguana_stackdata { uint8_t *data; uint16_t size; union iguana_stacknum U; }; + +char *bitcoin_base58encode(char *coinaddr,uint8_t *data,int32_t datalen); +int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr); + +#define IGUANA_MAXSCRIPTSIZE 10001 +#define IGUANA_SEQUENCEID_FINAL 0xfffffffe + +#define IGUANA_SCRIPT_NULL 0 +#define IGUANA_SCRIPT_76AC 1 +#define IGUANA_SCRIPT_76A988AC 2 +#define IGUANA_SCRIPT_P2SH 3 +#define IGUANA_SCRIPT_OPRETURN 4 +#define IGUANA_SCRIPT_3of3 5 +#define IGUANA_SCRIPT_2of3 6 +#define IGUANA_SCRIPT_1of3 7 +#define IGUANA_SCRIPT_2of2 8 +#define IGUANA_SCRIPT_1of2 9 +#define IGUANA_SCRIPT_MSIG 10 +#define IGUANA_SCRIPT_DATA 11 +#define IGUANA_SCRIPT_AC 12 +#define IGUANA_SCRIPT_1of1 13 +#define IGUANA_SCRIPT_STRANGE 15 + +#define MAX_SCRIPT_ELEMENT_SIZE 520 +#define MAX_OPS_PER_SCRIPT 201 // Maximum number of non-push operations per script +#define MAX_PUBKEYS_PER_MULTISIG 20 // Maximum number of public keys per multisig + +#define IGUANA_MAXSTACKITEMS ((int32_t)(IGUANA_MAXSCRIPTSIZE / sizeof(uint32_t))) +#define IGUANA_MAXSTACKDEPTH 128 +struct iguana_interpreter +{ + int32_t active,ifdepth,elsedepth,codeseparator,stackdepth,altstackdepth,maxstackdepth; + int8_t lastpath[IGUANA_MAXSTACKDEPTH]; + cJSON *logarray; + struct iguana_stackdata stack[]; +}; +static struct bitcoin_opcode { UT_hash_handle hh; uint8_t opcode,flags,stackitems; int8_t extralen; } *OPTABLE; static char *OPCODES[0x100]; static int32_t OPCODELENS[0x100]; + +#define SIGHASH_ALL 1 +#define SIGHASH_NONE 2 +#define SIGHASH_SINGLE 3 +#define SIGHASH_ANYONECANPAY 0x80 + +#define SCRIPT_OP_NOP 0x00 +#define SCRIPT_OP_TRUE 0x51 +#define SCRIPT_OP_2 0x52 +#define SCRIPT_OP_3 0x53 +#define SCRIPT_OP_4 0x54 +#define SCRIPT_OP_IF 0x63 +#define SCRIPT_OP_ELSE 0x67 +#define SCRIPT_OP_RETURN 0x6a +#define SCRIPT_OP_DUP 0x76 +#define SCRIPT_OP_ENDIF 0x68 +#define SCRIPT_OP_DROP 0x75 +#define SCRIPT_OP_EQUALVERIFY 0x88 +#define SCRIPT_OP_SHA256 0xa8 +#define SCRIPT_OP_HASH160 0xa9 + +#define SCRIPT_OP_EQUAL 0x87 +#define SCRIPT_OP_CHECKSIG 0xac +#define SCRIPT_OP_CHECKMULTISIG 0xae +#define SCRIPT_OP_CHECKSEQUENCEVERIFY 0xb2 +#define SCRIPT_OP_CHECKLOCKTIMEVERIFY 0xb1 +#define IGUANA_OP_0 0x00 +#define IGUANA_OP_PUSHDATA1 0x4c +#define IGUANA_OP_PUSHDATA2 0x4d +#define IGUANA_OP_PUSHDATA4 0x4e +#define IGUANA_OP_1NEGATE 0x4f +#define IGUANA_OP_1 0x51 +#define IGUANA_OP_16 0x60 +#define IGUANA_OP_NOP 0x61 +#define IGUANA_OP_IF 0x63 +#define IGUANA_OP_NOTIF 0x64 +#define IGUANA_OP_ELSE 0x67 +#define IGUANA_OP_ENDIF 0x68 +#define IGUANA_OP_VERIFY 0x69 +#define IGUANA_OP_RETURN 0x6a + +#define IGUANA_OP_TOALTSTACK 0x6b +#define IGUANA_OP_FROMALTSTACK 0x6c +#define IGUANA_OP_2DROP 0x6d +#define IGUANA_OP_2DUP 0x6e +#define IGUANA_OP_3DUP 0x6f +#define IGUANA_OP_2OVER 0x70 +#define IGUANA_OP_2ROT 0x71 +#define IGUANA_OP_2SWAP 0x72 +#define IGUANA_OP_IFDUP 0x73 +#define IGUANA_OP_DEPTH 0x74 +#define IGUANA_OP_DROP 0x75 +#define IGUANA_OP_DUP 0x76 +#define IGUANA_OP_NIP 0x77 +#define IGUANA_OP_OVER 0x78 +#define IGUANA_OP_PICK 0x79 +#define IGUANA_OP_ROLL 0x7a +#define IGUANA_OP_ROT 0x7b +#define IGUANA_OP_SWAP 0x7c +#define IGUANA_OP_TUCK 0x7d + +#define IGUANA_OP_EQUAL 0x87 +#define IGUANA_OP_EQUALVERIFY 0x88 + +#define IGUANA_OP_1ADD 0x8b +#define IGUANA_OP_1SUB 0x8c +#define IGUANA_OP_NEGATE 0x8f +#define IGUANA_OP_ABS 0x90 +#define IGUANA_OP_NOT 0x91 +#define IGUANA_OP_0NOTEQUAL 0x92 +#define IGUANA_OP_ADD 0x93 +#define IGUANA_OP_SUB 0x94 + +#define IGUANA_OP_BOOLAND 0x9a +#define IGUANA_OP_BOOLOR 0x9b +#define IGUANA_OP_NUMEQUAL 0x9c +#define IGUANA_OP_NUMEQUALVERIFY 0x9d +#define IGUANA_OP_NUMNOTEQUAL 0x9e +#define IGUANA_OP_LESSTHAN 0x9f +#define IGUANA_OP_GREATERTHAN 0xa0 +#define IGUANA_OP_LESSTHANOREQUAL 0xa1 +#define IGUANA_OP_GREATERTHANOREQUAL 0xa2 +#define IGUANA_OP_MIN 0xa3 +#define IGUANA_OP_MAX 0xa4 +#define IGUANA_OP_WITHIN 0xa5 + +#define IGUANA_OP_RIPEMD160 0xa6 +#define IGUANA_OP_SHA1 0xa7 +#define IGUANA_OP_SHA256 0xa8 +#define IGUANA_OP_HASH160 0xa9 +#define IGUANA_OP_HASH256 0xaa +#define IGUANA_OP_CODESEPARATOR 0xab +#define IGUANA_OP_CHECKSIG 0xac +#define IGUANA_OP_CHECKSIGVERIFY 0xad +#define IGUANA_OP_CHECKMULTISIG 0xae +#define IGUANA_OP_CHECKMULTISIGVERIFY 0xaf + +#define IGUANA_OP_NOP1 0xb0 +#define IGUANA_OP_CHECKLOCKTIMEVERIFY 0xb1 +#define IGUANA_OP_CHECKSEQUENCEVERIFY 0xb2 +#define IGUANA_OP_NOP10 0xb9 + +#define IGUANA_OP_COMBINEPUBKEYS 0xc0 +#define IGUANA_OP_CHECKSCHNORR 0xc1 +#define IGUANA_OP_CHECKSCHNORRVERIFY 0xc2 + +// https://github.com/TierNolan/bips/blob/cpkv/bip-cprkv.mediawiki +#define IGUANA_OP_CHECKPRIVATEKEY 0xc3 +#define IGUANA_OP_CHECKPRIVATEKEYVERIFY 0xc4 + +#define IGUANA_NOPFLAG 1 +#define IGUANA_ALWAYSILLEGAL 2 +#define IGUANA_EXECUTIONILLEGAL 4 +#define IGUANA_POSTVERIFY 8 +#define IGUANA_CRYPTOFLAG 16 +#define IGUANA_MATHFLAG 32 +#define IGUANA_CONTROLFLAG 64 +#define IGUANA_STACKFLAG 128 + +enum opcodetype +{ + // push value + OP_0 = 0x00, + OP_FALSE = OP_0, + OP_PUSHDATA1 = 0x4c, + OP_PUSHDATA2 = 0x4d, + OP_PUSHDATA4 = 0x4e, + OP_1NEGATE = 0x4f, + OP_RESERVED = 0x50, + OP_1 = 0x51, + OP_TRUE=OP_1, + OP_2 = 0x52, + OP_3 = 0x53, + OP_4 = 0x54, + OP_5 = 0x55, + OP_6 = 0x56, + OP_7 = 0x57, + OP_8 = 0x58, + OP_9 = 0x59, + OP_10 = 0x5a, + OP_11 = 0x5b, + OP_12 = 0x5c, + OP_13 = 0x5d, + OP_14 = 0x5e, + OP_15 = 0x5f, + OP_16 = 0x60, + + // control + OP_NOP = 0x61, + OP_VER = 0x62, + OP_IF = 0x63, + OP_NOTIF = 0x64, + OP_VERIF = 0x65, + OP_VERNOTIF = 0x66, + OP_ELSE = 0x67, + OP_ENDIF = 0x68, + OP_VERIFY = 0x69, + OP_RETURN = 0x6a, + + // stack ops + OP_TOALTSTACK = 0x6b, + OP_FROMALTSTACK = 0x6c, + OP_2DROP = 0x6d, + OP_2DUP = 0x6e, + OP_3DUP = 0x6f, + OP_2OVER = 0x70, + OP_2ROT = 0x71, + OP_2SWAP = 0x72, + OP_IFDUP = 0x73, + OP_DEPTH = 0x74, + OP_DROP = 0x75, + OP_DUP = 0x76, + OP_NIP = 0x77, + OP_OVER = 0x78, + OP_PICK = 0x79, + OP_ROLL = 0x7a, + OP_ROT = 0x7b, + OP_SWAP = 0x7c, + OP_TUCK = 0x7d, + + // splice ops + OP_CAT = 0x7e, + OP_SUBSTR = 0x7f, + OP_LEFT = 0x80, + OP_RIGHT = 0x81, + OP_SIZE = 0x82, + + // bit logic + OP_INVERT = 0x83, + OP_AND = 0x84, + OP_OR = 0x85, + OP_XOR = 0x86, + OP_EQUAL = 0x87, + OP_EQUALVERIFY = 0x88, + OP_RESERVED1 = 0x89, + OP_RESERVED2 = 0x8a, + + // numeric + OP_1ADD = 0x8b, + OP_1SUB = 0x8c, + OP_2MUL = 0x8d, + OP_2DIV = 0x8e, + OP_NEGATE = 0x8f, + OP_ABS = 0x90, + OP_NOT = 0x91, + OP_0NOTEQUAL = 0x92, + + OP_ADD = 0x93, + OP_SUB = 0x94, + OP_MUL = 0x95, + OP_DIV = 0x96, + OP_MOD = 0x97, + OP_LSHIFT = 0x98, + OP_RSHIFT = 0x99, + + OP_BOOLAND = 0x9a, + OP_BOOLOR = 0x9b, + OP_NUMEQUAL = 0x9c, + OP_NUMEQUALVERIFY = 0x9d, + OP_NUMNOTEQUAL = 0x9e, + OP_LESSTHAN = 0x9f, + OP_GREATERTHAN = 0xa0, + OP_LESSTHANOREQUAL = 0xa1, + OP_GREATERTHANOREQUAL = 0xa2, + OP_MIN = 0xa3, + OP_MAX = 0xa4, + + OP_WITHIN = 0xa5, + + // crypto + OP_RIPEMD160 = 0xa6, + OP_SHA1 = 0xa7, + OP_SHA256 = 0xa8, + OP_HASH160 = 0xa9, + OP_HASH256 = 0xaa, + OP_CODESEPARATOR = 0xab, + OP_CHECKSIG = 0xac, + OP_CHECKSIGVERIFY = 0xad, + OP_CHECKMULTISIG = 0xae, + OP_CHECKMULTISIGVERIFY = 0xaf, + + // expansion + OP_NOP1 = 0xb0, + OP_CHECKLOCKTIMEVERIFY = 0xb1, + OP_CHECKSEQUENCEVERIFY = 0xb2, + OP_NOP4 = 0xb3, + OP_NOP5 = 0xb4, + OP_NOP6 = 0xb5, + OP_NOP7 = 0xb6, + OP_NOP8 = 0xb7, + OP_NOP9 = 0xb8, + OP_NOP10 = 0xb9, + + OP_COMBINEPUBKEYS = 0xc0, + OP_CHECKSCHNORR = 0xc1, + OP_CHECKSCHNORRVERIFY = 0xc2, + OP_CHECKPRIVATEKEY = 0xc3, + OP_CHECKPRIVATEKEYVERIFY = 0xc4, + + // template matching params + //OP_SMALLINTEGER = 0xfa, + //OP_PUBKEYS = 0xfb, + //OP_PUBKEYHASH = 0xfd, + //OP_PUBKEY = 0xfe, + + OP_INVALIDOPCODE = 0xff, +}; + struct { bits256 privkey; uint8_t rmd160[20]; } LP_privkeys[100]; int32_t LP_numprivkeys; bits256 LP_privkeyfind(uint8_t rmd160[20]) diff --git a/iguana/exchanges/LP_coins.c b/iguana/exchanges/LP_coins.c new file mode 100644 index 000000000..d3f136c00 --- /dev/null +++ b/iguana/exchanges/LP_coins.c @@ -0,0 +1,196 @@ + +/****************************************************************************** + * Copyright © 2014-2017 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ +// +// LP_coins.c +// marketmaker +// + + +char *parse_conf_line(char *line,char *field) +{ + line += strlen(field); + for (; *line!='='&&*line!=0; line++) + break; + if ( *line == 0 ) + return(0); + if ( *line == '=' ) + line++; + while ( line[strlen(line)-1] == '\r' || line[strlen(line)-1] == '\n' || line[strlen(line)-1] == ' ' ) + line[strlen(line)-1] = 0; + //printf("LINE.(%s)\n",line); + _stripwhite(line,0); + return(clonestr(line)); +} + +void LP_userpassfp(char *username,char *password,FILE *fp) +{ + char *rpcuser,*rpcpassword,*str,line[8192]; + rpcuser = rpcpassword = 0; + username[0] = password[0] = 0; + while ( fgets(line,sizeof(line),fp) != 0 ) + { + if ( line[0] == '#' ) + continue; + //printf("line.(%s) %p %p\n",line,strstr(line,(char *)"rpcuser"),strstr(line,(char *)"rpcpassword")); + if ( (str= strstr(line,(char *)"rpcuser")) != 0 ) + rpcuser = parse_conf_line(str,(char *)"rpcuser"); + else if ( (str= strstr(line,(char *)"rpcpassword")) != 0 ) + rpcpassword = parse_conf_line(str,(char *)"rpcpassword"); + } + if ( rpcuser != 0 && rpcpassword != 0 ) + { + strcpy(username,rpcuser); + strcpy(password,rpcpassword); + } + //printf("rpcuser.(%s) rpcpassword.(%s) KMDUSERPASS.(%s) %u\n",rpcuser,rpcpassword,KMDUSERPASS,port); + if ( rpcuser != 0 ) + free(rpcuser); + if ( rpcpassword != 0 ) + free(rpcpassword); +} + +void LP_statefname(char *fname,char *symbol,char *assetname,char *str) +{ + sprintf(fname,"%s",LP_getdatadir()); +#ifdef WIN32 + strcat(fname,"\\"); +#else + strcat(fname,"/"); +#endif + if ( strcmp(symbol,"BTC") == 0 ) + strcat(fname,".bitcoin"); + else if ( strcmp(symbol,"LTC") == 0 ) + strcat(fname,".litecoin"); + else + { + strcat(fname,".komodo"); + if ( strcmp(symbol,"KMD") != 0 ) + { +#ifdef WIN32 + strcat(fname,"\\"); +#else + strcat(fname,"/"); +#endif + strcat(fname,assetname); + } + } +#ifdef WIN32 + strcat(fname,"\\"); +#else + strcat(fname,"/"); +#endif + strcat(fname,str); + printf("LP_statefname.(%s) <- %s %s %s\n",fname,symbol,assetname,str); +} + +int32_t LP_userpass(char *userpass,char *symbol,char *assetname,char *confroot) +{ + FILE *fp; char fname[512],username[512],password[512],confname[16]; + userpass[0] = 0; + sprintf(confname,"%s.conf",confroot); +#ifdef __APPLE__ + confname[0] = toupper(confname[0]); +#endif + LP_statefname(fname,symbol,assetname,confname); + if ( (fp= fopen(fname,"rb")) != 0 ) + { + LP_userpassfp(username,password,fp); + sprintf(userpass,"%s:%s",username,password); + fclose(fp); + return((int32_t)strlen(userpass)); + } + return(-1); +} + +uint32_t LP_assetmagic(char *symbol,uint64_t supply) +{ + uint8_t buf[512]; int32_t len = 0; + if ( strcmp(symbol,"KMD") == 0 ) + return(0x8de4eef9); + len = iguana_rwnum(1,&buf[len],sizeof(supply),(void *)&supply); + strcpy((char *)&buf[len],symbol); + len += strlen(symbol); + return(calc_crc32(0,buf,len)); +} + +uint16_t LP_assetport(uint32_t magic) +{ + if ( magic == 0x8de4eef9 ) + return(7771); + else return(8000 + (magic % 7777)); +} + +uint16_t LP_rpcport(char *symbol,uint64_t supply,uint32_t *magicp) +{ + if ( symbol == 0 || symbol[0] == 0 || strcmp("KMD",symbol) == 0 ) + { + *magicp = 0x8de4eef9; + return(7771); + } + else if ( strcmp("BTC",symbol) == 0 ) + return(8332); + else if ( strcmp("LTC",symbol) == 0 ) + return(9332); + *magicp = LP_assetmagic(symbol,supply); + return(LP_assetport(*magicp)+1); +} + +struct iguana_info *LP_coinfind(char *symbol) +{ + static struct iguana_info *LP_coins; static int32_t LP_numcoins; + struct iguana_info *coin,cdata; int32_t i; uint32_t magic; uint16_t port; + for (i=0; iQ = Q; return(clonestr("{\"result\":\"updated\"}")); - } - else return(clonestr("{\"error\":\"nullptr\"}")); + } else return(clonestr("{\"error\":\"nullptr\"}")); } double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port,char *base,char *rel,bits256 mypub) @@ -213,7 +212,7 @@ double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port, break; } } - usleep(250000); + usleep(100000); } } else printf("no pushsock for peer.%s:%u\n",ipaddr,port); } else printf("cant find/create peer.%s:%u\n",ipaddr,port); @@ -360,6 +359,27 @@ cJSON *LP_bestprice(struct LP_utxoinfo *myutxo,char *base) return(bestitem); } +int32_t LP_priceping(int32_t pubsock,struct LP_utxoinfo *utxo,char *rel,double profitmargin) +{ + double price; uint32_t now; cJSON *retjson; struct LP_quoteinfo Q; char *retstr; + if ( (now= (uint32_t)time(NULL)) > utxo->swappending ) + utxo->swappending = 0; + if ( now > utxo->published+60 && utxo->swappending == 0 && utxo->pair < 0 && utxo->swap == 0 && (price= LP_price(utxo->coin,rel)) != 0. ) + { + price *= (1. + profitmargin); + if ( LP_quoteinfoinit(&Q,utxo,rel,price) < 0 ) + return(-1); + Q.timestamp = (uint32_t)time(NULL); + retjson = LP_quotejson(&Q); + jaddstr(retjson,"method","quote"); + retstr = jprint(retjson,1); + LP_send(pubsock,retstr,1); + utxo->published = now; + return(0); + } + return(-1); +} + int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { char *method,*base,*rel,*retstr,pairstr[512]; cJSON *retjson; double price; bits256 privkey,txid; struct LP_utxoinfo *utxo; int32_t retval = -1,DEXselector = 0; uint64_t destvalue; struct basilisk_request R; struct LP_quoteinfo Q; @@ -368,7 +388,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin txid = jbits256(argjson,"txid"); if ( (utxo= LP_utxofind(txid,jint(argjson,"vout"))) != 0 && strcmp(utxo->ipaddr,mypeer->ipaddr) == 0 && utxo->port == mypeer->port && (base= jstr(argjson,"base")) != 0 && (rel= jstr(argjson,"rel")) != 0 && strcmp(base,utxo->coin) == 0 ) { - printf("LP_command.(%s)\n",jprint(argjson,0)); + //printf("LP_command.(%s)\n",jprint(argjson,0)); if ( time(NULL) > utxo->swappending ) utxo->swappending = 0; if ( strcmp(method,"price") == 0 || strcmp(method,"request") == 0 ) @@ -399,6 +419,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin else jaddstr(retjson,"method","quote"); retstr = jprint(retjson,1); LP_send(pubsock,retstr,1); + utxo->published = (uint32_t)time(NULL); } else printf("null price\n"); } else printf("swappending.%u pair.%d\n",utxo->swappending,utxo->pair); } @@ -496,7 +517,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } printf("CMD.(%s)\n",jprint(argjson,0)); if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) - retstr = LP_quote(argjson); + retstr = LP_quotereceived(argjson); else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 ) { int32_t pairsock = -1; char *pairstr; @@ -518,6 +539,8 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } else if ( IAMCLIENT == 0 && strcmp(method,"getprice") == 0 ) retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel")); + else if ( strcmp(method,"orderbook") == 0 ) + retstr = LP_orderbook(jstr(argjson,"base"),jstr(argjson,"rel")); else if ( IAMCLIENT == 0 && strcmp(method,"getpeers") == 0 ) retstr = LP_peers(); else if ( IAMCLIENT == 0 && strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 568a65511..4cfacff3f 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -21,70 +21,30 @@ #ifndef LP_INCLUDE_H #define LP_INCLUDE_H +#define INSTANTDEX_DECKSIZE 1000 +#define INSTANTDEX_LOCKTIME (3600*2 + 300*2) +#define INSTANTDEX_INSURANCEDIV 777 +#define INSTANTDEX_PUBKEY "03bc2c7ba671bae4a6fc835244c9762b41647b9827d4780a89a949b984a8ddcc06" +#define INSTANTDEX_RMD160 "ca1e04745e8ca0c60d8c5881531d51bec470743f" +#define JUMBLR_RMD160 "5177f8b427e5f47342a4b8ab5dac770815d4389e" +#define TIERNOLAN_RMD160 "daedddd8dbe7a2439841ced40ba9c3d375f98146" +#define INSTANTDEX_BTC "1KRhTPvoxyJmVALwHFXZdeeWFbcJSbkFPu" +#define INSTANTDEX_BTCD "RThtXup6Zo7LZAi8kRWgjAyi1s4u6U9Cpf" + #define BASILISK_DISABLEWAITTX #define BASILISK_DISABLESENDTX +#define LP_PROPAGATION_SLACK 10 // txid ordering is not enforced, so getting extra recent txid #define LP_RESERVETIME 60 #define LP_AVETXSIZE 200 #define LP_CACHEDURATION 60 - #define BASILISK_DEFAULT_NUMCONFIRMS 5 #define DEX_SLEEP 3 -#define BASILISK_DEXDURATION 300 -#define BASILISK_MSGDURATION 30 -#define BASILISK_AUCTION_DURATION 5 - -#define BASILISK_MAXFUTUREBLOCK 60 #define BASILISK_KEYSIZE ((int32_t)(2*sizeof(bits256)+sizeof(uint32_t)*2)) extern char GLOBAL_DBDIR[]; extern int32_t IAMCLIENT; -void *bitcoin_ctx(); -int32_t bitcoin_verify(void *ctx,uint8_t *sig,int32_t siglen,bits256 txhash2,uint8_t *pubkey,int32_t plen); -int32_t bitcoin_recoververify(void *ctx,char *symbol,uint8_t *sig,bits256 messagehash2,uint8_t *pubkey,size_t plen); -int32_t bitcoin_sign(void *ctx,char *symbol,uint8_t *sig,bits256 txhash2,bits256 privkey,int32_t recoverflag); -bits256 bitcoin_pubkey33(void *ctx,uint8_t *data,bits256 privkey); -bits256 bitcoin_pub256(void *ctx,bits256 *privkeyp,uint8_t odd_even); - -char *bitcoin_base58encode(char *coinaddr,uint8_t *data,int32_t datalen); -int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr); - -#define IGUANA_MAXSCRIPTSIZE 10001 -#define IGUANA_SEQUENCEID_FINAL 0xfffffffe - -#define IGUANA_SCRIPT_NULL 0 -#define IGUANA_SCRIPT_76AC 1 -#define IGUANA_SCRIPT_76A988AC 2 -#define IGUANA_SCRIPT_P2SH 3 -#define IGUANA_SCRIPT_OPRETURN 4 -#define IGUANA_SCRIPT_3of3 5 -#define IGUANA_SCRIPT_2of3 6 -#define IGUANA_SCRIPT_1of3 7 -#define IGUANA_SCRIPT_2of2 8 -#define IGUANA_SCRIPT_1of2 9 -#define IGUANA_SCRIPT_MSIG 10 -#define IGUANA_SCRIPT_DATA 11 -#define IGUANA_SCRIPT_AC 12 -#define IGUANA_SCRIPT_1of1 13 -#define IGUANA_SCRIPT_STRANGE 15 - -#define BASILISK_TIMEOUT 3000 -#define BASILISK_MINFANOUT 8 -#define BASILISK_MAXFANOUT 64 -#define BASILISK_DEFAULTDIFF 0x1effffff -#define BASILISK_HDROFFSET ((int32_t)(sizeof(bits256)+sizeof(struct iguana_msghdr)+sizeof(uint32_t))) - -#define INSTANTDEX_DECKSIZE 1000 -#define INSTANTDEX_LOCKTIME (3600*2 + 300*2) -#define INSTANTDEX_INSURANCEDIV 777 -#define INSTANTDEX_PUBKEY "03bc2c7ba671bae4a6fc835244c9762b41647b9827d4780a89a949b984a8ddcc06" -#define INSTANTDEX_RMD160 "ca1e04745e8ca0c60d8c5881531d51bec470743f" -#define JUMBLR_RMD160 "5177f8b427e5f47342a4b8ab5dac770815d4389e" -#define TIERNOLAN_RMD160 "daedddd8dbe7a2439841ced40ba9c3d375f98146" -#define INSTANTDEX_BTC "1KRhTPvoxyJmVALwHFXZdeeWFbcJSbkFPu" -#define INSTANTDEX_BTCD "RThtXup6Zo7LZAi8kRWgjAyi1s4u6U9Cpf" - struct iguana_msgvin { bits256 prev_hash; uint8_t *vinscript,*userdata,*spendscript,*redeemscript; uint32_t prev_vout,sequence; uint16_t scriptlen,p2shlen,userdatalen,spendlen; }; struct iguana_msgvout { uint64_t value; uint32_t pk_scriptlen; uint8_t *pk_script; }; @@ -107,7 +67,7 @@ struct vin_info struct iguana_msgvin vin; uint64_t amount; cJSON *extras; bits256 sigtxid; int32_t M,N,validmask,spendlen,type,p2shlen,numpubkeys,numsigs,height,hashtype,userdatalen,suppress_pubkeys,ignore_cltverr; uint32_t sequence,unspentind; struct vin_signer signers[16]; char coinaddr[65]; - uint8_t rmd160[20],spendscript[IGUANA_MAXSCRIPTSIZE],p2shscript[IGUANA_MAXSCRIPTSIZE],userdata[IGUANA_MAXSCRIPTSIZE]; + uint8_t rmd160[20],spendscript[10000],p2shscript[10000],userdata[10000]; }; struct basilisk_swapmessage @@ -199,6 +159,31 @@ struct basilisk_swap }; +struct LP_utxoinfo +{ + UT_hash_handle hh; + bits256 txid,txid2,feetxid,otherpubkey,mypub; + void *swap; + uint64_t satoshis,satoshis2; + uint8_t key[sizeof(bits256) + sizeof(int32_t)]; + int32_t vout,vout2,pair; + uint32_t lasttime,errors,swappending,published; + double profitmargin; + char ipaddr[64],coinaddr[64],spendscript[256],coin[16]; + uint16_t port; +}; + +struct LP_peerinfo +{ + UT_hash_handle hh; + uint64_t ip_port; + double profitmargin; + uint32_t ipbits,errortime,errors,numpeers,numutxos,lasttime,connected; + int32_t pushsock,subsock; + uint16_t port; + char ipaddr[64]; +}; + struct LP_quoteinfo { struct basilisk_request R; @@ -208,297 +193,11 @@ struct LP_quoteinfo char srccoin[16],coinaddr[64],destcoin[16],destaddr[64]; }; -union iguana_stacknum { int32_t val; int64_t val64; uint8_t rmd160[20]; bits256 hash2; uint8_t pubkey[33]; uint8_t sig[74]; }; -struct iguana_stackdata { uint8_t *data; uint16_t size; union iguana_stacknum U; }; - -#define MAX_SCRIPT_ELEMENT_SIZE 520 -#define MAX_OPS_PER_SCRIPT 201 // Maximum number of non-push operations per script -#define MAX_PUBKEYS_PER_MULTISIG 20 // Maximum number of public keys per multisig - -#define IGUANA_MAXSTACKITEMS ((int32_t)(IGUANA_MAXSCRIPTSIZE / sizeof(uint32_t))) -#define IGUANA_MAXSTACKDEPTH 128 -struct iguana_interpreter -{ - int32_t active,ifdepth,elsedepth,codeseparator,stackdepth,altstackdepth,maxstackdepth; - int8_t lastpath[IGUANA_MAXSTACKDEPTH]; - cJSON *logarray; - struct iguana_stackdata stack[]; -}; -static struct bitcoin_opcode { UT_hash_handle hh; uint8_t opcode,flags,stackitems; int8_t extralen; } *OPTABLE; static char *OPCODES[0x100]; static int32_t OPCODELENS[0x100]; - -#define SIGHASH_ALL 1 -#define SIGHASH_NONE 2 -#define SIGHASH_SINGLE 3 -#define SIGHASH_ANYONECANPAY 0x80 - -#define SCRIPT_OP_NOP 0x00 -#define SCRIPT_OP_TRUE 0x51 -#define SCRIPT_OP_2 0x52 -#define SCRIPT_OP_3 0x53 -#define SCRIPT_OP_4 0x54 -#define SCRIPT_OP_IF 0x63 -#define SCRIPT_OP_ELSE 0x67 -#define SCRIPT_OP_RETURN 0x6a -#define SCRIPT_OP_DUP 0x76 -#define SCRIPT_OP_ENDIF 0x68 -#define SCRIPT_OP_DROP 0x75 -#define SCRIPT_OP_EQUALVERIFY 0x88 -#define SCRIPT_OP_SHA256 0xa8 -#define SCRIPT_OP_HASH160 0xa9 - -#define SCRIPT_OP_EQUAL 0x87 -#define SCRIPT_OP_CHECKSIG 0xac -#define SCRIPT_OP_CHECKMULTISIG 0xae -#define SCRIPT_OP_CHECKSEQUENCEVERIFY 0xb2 -#define SCRIPT_OP_CHECKLOCKTIMEVERIFY 0xb1 -#define IGUANA_OP_0 0x00 -#define IGUANA_OP_PUSHDATA1 0x4c -#define IGUANA_OP_PUSHDATA2 0x4d -#define IGUANA_OP_PUSHDATA4 0x4e -#define IGUANA_OP_1NEGATE 0x4f -#define IGUANA_OP_1 0x51 -#define IGUANA_OP_16 0x60 -#define IGUANA_OP_NOP 0x61 -#define IGUANA_OP_IF 0x63 -#define IGUANA_OP_NOTIF 0x64 -#define IGUANA_OP_ELSE 0x67 -#define IGUANA_OP_ENDIF 0x68 -#define IGUANA_OP_VERIFY 0x69 -#define IGUANA_OP_RETURN 0x6a - -#define IGUANA_OP_TOALTSTACK 0x6b -#define IGUANA_OP_FROMALTSTACK 0x6c -#define IGUANA_OP_2DROP 0x6d -#define IGUANA_OP_2DUP 0x6e -#define IGUANA_OP_3DUP 0x6f -#define IGUANA_OP_2OVER 0x70 -#define IGUANA_OP_2ROT 0x71 -#define IGUANA_OP_2SWAP 0x72 -#define IGUANA_OP_IFDUP 0x73 -#define IGUANA_OP_DEPTH 0x74 -#define IGUANA_OP_DROP 0x75 -#define IGUANA_OP_DUP 0x76 -#define IGUANA_OP_NIP 0x77 -#define IGUANA_OP_OVER 0x78 -#define IGUANA_OP_PICK 0x79 -#define IGUANA_OP_ROLL 0x7a -#define IGUANA_OP_ROT 0x7b -#define IGUANA_OP_SWAP 0x7c -#define IGUANA_OP_TUCK 0x7d - -#define IGUANA_OP_EQUAL 0x87 -#define IGUANA_OP_EQUALVERIFY 0x88 - -#define IGUANA_OP_1ADD 0x8b -#define IGUANA_OP_1SUB 0x8c -#define IGUANA_OP_NEGATE 0x8f -#define IGUANA_OP_ABS 0x90 -#define IGUANA_OP_NOT 0x91 -#define IGUANA_OP_0NOTEQUAL 0x92 -#define IGUANA_OP_ADD 0x93 -#define IGUANA_OP_SUB 0x94 - -#define IGUANA_OP_BOOLAND 0x9a -#define IGUANA_OP_BOOLOR 0x9b -#define IGUANA_OP_NUMEQUAL 0x9c -#define IGUANA_OP_NUMEQUALVERIFY 0x9d -#define IGUANA_OP_NUMNOTEQUAL 0x9e -#define IGUANA_OP_LESSTHAN 0x9f -#define IGUANA_OP_GREATERTHAN 0xa0 -#define IGUANA_OP_LESSTHANOREQUAL 0xa1 -#define IGUANA_OP_GREATERTHANOREQUAL 0xa2 -#define IGUANA_OP_MIN 0xa3 -#define IGUANA_OP_MAX 0xa4 -#define IGUANA_OP_WITHIN 0xa5 - -#define IGUANA_OP_RIPEMD160 0xa6 -#define IGUANA_OP_SHA1 0xa7 -#define IGUANA_OP_SHA256 0xa8 -#define IGUANA_OP_HASH160 0xa9 -#define IGUANA_OP_HASH256 0xaa -#define IGUANA_OP_CODESEPARATOR 0xab -#define IGUANA_OP_CHECKSIG 0xac -#define IGUANA_OP_CHECKSIGVERIFY 0xad -#define IGUANA_OP_CHECKMULTISIG 0xae -#define IGUANA_OP_CHECKMULTISIGVERIFY 0xaf - -#define IGUANA_OP_NOP1 0xb0 -#define IGUANA_OP_CHECKLOCKTIMEVERIFY 0xb1 -#define IGUANA_OP_CHECKSEQUENCEVERIFY 0xb2 -#define IGUANA_OP_NOP10 0xb9 - -#define IGUANA_OP_COMBINEPUBKEYS 0xc0 -#define IGUANA_OP_CHECKSCHNORR 0xc1 -#define IGUANA_OP_CHECKSCHNORRVERIFY 0xc2 - -// https://github.com/TierNolan/bips/blob/cpkv/bip-cprkv.mediawiki -#define IGUANA_OP_CHECKPRIVATEKEY 0xc3 -#define IGUANA_OP_CHECKPRIVATEKEYVERIFY 0xc4 - -#define IGUANA_NOPFLAG 1 -#define IGUANA_ALWAYSILLEGAL 2 -#define IGUANA_EXECUTIONILLEGAL 4 -#define IGUANA_POSTVERIFY 8 -#define IGUANA_CRYPTOFLAG 16 -#define IGUANA_MATHFLAG 32 -#define IGUANA_CONTROLFLAG 64 -#define IGUANA_STACKFLAG 128 - -enum opcodetype -{ - // push value - OP_0 = 0x00, - OP_FALSE = OP_0, - OP_PUSHDATA1 = 0x4c, - OP_PUSHDATA2 = 0x4d, - OP_PUSHDATA4 = 0x4e, - OP_1NEGATE = 0x4f, - OP_RESERVED = 0x50, - OP_1 = 0x51, - OP_TRUE=OP_1, - OP_2 = 0x52, - OP_3 = 0x53, - OP_4 = 0x54, - OP_5 = 0x55, - OP_6 = 0x56, - OP_7 = 0x57, - OP_8 = 0x58, - OP_9 = 0x59, - OP_10 = 0x5a, - OP_11 = 0x5b, - OP_12 = 0x5c, - OP_13 = 0x5d, - OP_14 = 0x5e, - OP_15 = 0x5f, - OP_16 = 0x60, - - // control - OP_NOP = 0x61, - OP_VER = 0x62, - OP_IF = 0x63, - OP_NOTIF = 0x64, - OP_VERIF = 0x65, - OP_VERNOTIF = 0x66, - OP_ELSE = 0x67, - OP_ENDIF = 0x68, - OP_VERIFY = 0x69, - OP_RETURN = 0x6a, - - // stack ops - OP_TOALTSTACK = 0x6b, - OP_FROMALTSTACK = 0x6c, - OP_2DROP = 0x6d, - OP_2DUP = 0x6e, - OP_3DUP = 0x6f, - OP_2OVER = 0x70, - OP_2ROT = 0x71, - OP_2SWAP = 0x72, - OP_IFDUP = 0x73, - OP_DEPTH = 0x74, - OP_DROP = 0x75, - OP_DUP = 0x76, - OP_NIP = 0x77, - OP_OVER = 0x78, - OP_PICK = 0x79, - OP_ROLL = 0x7a, - OP_ROT = 0x7b, - OP_SWAP = 0x7c, - OP_TUCK = 0x7d, - - // splice ops - OP_CAT = 0x7e, - OP_SUBSTR = 0x7f, - OP_LEFT = 0x80, - OP_RIGHT = 0x81, - OP_SIZE = 0x82, - - // bit logic - OP_INVERT = 0x83, - OP_AND = 0x84, - OP_OR = 0x85, - OP_XOR = 0x86, - OP_EQUAL = 0x87, - OP_EQUALVERIFY = 0x88, - OP_RESERVED1 = 0x89, - OP_RESERVED2 = 0x8a, - - // numeric - OP_1ADD = 0x8b, - OP_1SUB = 0x8c, - OP_2MUL = 0x8d, - OP_2DIV = 0x8e, - OP_NEGATE = 0x8f, - OP_ABS = 0x90, - OP_NOT = 0x91, - OP_0NOTEQUAL = 0x92, - - OP_ADD = 0x93, - OP_SUB = 0x94, - OP_MUL = 0x95, - OP_DIV = 0x96, - OP_MOD = 0x97, - OP_LSHIFT = 0x98, - OP_RSHIFT = 0x99, - - OP_BOOLAND = 0x9a, - OP_BOOLOR = 0x9b, - OP_NUMEQUAL = 0x9c, - OP_NUMEQUALVERIFY = 0x9d, - OP_NUMNOTEQUAL = 0x9e, - OP_LESSTHAN = 0x9f, - OP_GREATERTHAN = 0xa0, - OP_LESSTHANOREQUAL = 0xa1, - OP_GREATERTHANOREQUAL = 0xa2, - OP_MIN = 0xa3, - OP_MAX = 0xa4, - - OP_WITHIN = 0xa5, - - // crypto - OP_RIPEMD160 = 0xa6, - OP_SHA1 = 0xa7, - OP_SHA256 = 0xa8, - OP_HASH160 = 0xa9, - OP_HASH256 = 0xaa, - OP_CODESEPARATOR = 0xab, - OP_CHECKSIG = 0xac, - OP_CHECKSIGVERIFY = 0xad, - OP_CHECKMULTISIG = 0xae, - OP_CHECKMULTISIGVERIFY = 0xaf, - - // expansion - OP_NOP1 = 0xb0, - OP_CHECKLOCKTIMEVERIFY = 0xb1, - OP_CHECKSEQUENCEVERIFY = 0xb2, - OP_NOP4 = 0xb3, - OP_NOP5 = 0xb4, - OP_NOP6 = 0xb5, - OP_NOP7 = 0xb6, - OP_NOP8 = 0xb7, - OP_NOP9 = 0xb8, - OP_NOP10 = 0xb9, - - OP_COMBINEPUBKEYS = 0xc0, - OP_CHECKSCHNORR = 0xc1, - OP_CHECKSCHNORRVERIFY = 0xc2, - OP_CHECKPRIVATEKEY = 0xc3, - OP_CHECKPRIVATEKEYVERIFY = 0xc4, - - // template matching params - //OP_SMALLINTEGER = 0xfa, - //OP_PUBKEYS = 0xfb, - //OP_PUBKEYHASH = 0xfd, - //OP_PUBKEY = 0xfe, - - OP_INVALIDOPCODE = 0xff, -}; void basilisk_dontforget_update(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx); uint32_t basilisk_requestid(struct basilisk_request *rp); uint32_t basilisk_quoteid(struct basilisk_request *rp); struct basilisk_swap *LP_swapinit(int32_t iambob,int32_t optionduration,bits256 privkey,struct basilisk_request *rp,struct LP_quoteinfo *qp); char *bitcoind_passthru(char *coinstr,char *serverport,char *userpass,char *method,char *params); -struct iguana_info *LP_coinfind(char *symbol); -void *curl_post(void **cHandlep,char *url,char *userpass,char *postfields,char *hdr0,char *hdr1,char *hdr2,char *hdr3); uint32_t LP_swapdata_rawtxsend(int32_t pairsock,struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t maxlen,struct basilisk_rawtx *rawtx,uint32_t nextbits,int32_t suppress_swapsend); #endif diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index ce8a3b97c..68915cd7a 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -23,549 +23,21 @@ #include "LP_include.h" #include "LP_network.c" -#define LP_PROPAGATION_SLACK 10 // txid ordering is not enforced, so getting extra recent txid +struct LP_utxoinfo *LP_utxoinfos; +struct LP_peerinfo *LP_peerinfos,*LP_mypeer; char *activecoins[] = { "BTC", "KMD", "REVS", "JUMBLR" };//"LTC", "USD", }; +char GLOBAL_DBDIR[] = "DB"; char *default_LPnodes[] = { "5.9.253.196", "5.9.253.197", "5.9.253.198", "5.9.253.199", "5.9.253.200", "5.9.253.201", "5.9.253.202", "5.9.253.203", "5.9.253.204" }; //"5.9.253.195", + portable_mutex_t LP_peermutex,LP_utxomutex,LP_commandmutex,LP_cachemutex; int32_t LP_mypubsock = -1; int32_t Client_connections; int32_t IAMCLIENT = 0; -struct LP_peerinfo -{ - UT_hash_handle hh; - uint64_t ip_port; - double profitmargin; - uint32_t ipbits,errortime,errors,numpeers,numutxos,lasttime,connected; - int32_t pushsock,subsock; - uint16_t port; - char ipaddr[64]; -} *LP_peerinfos,*LP_mypeer; - -struct LP_utxoinfo -{ - UT_hash_handle hh; - bits256 txid,txid2,feetxid,otherpubkey,mypub; - void *swap; - uint64_t satoshis,satoshis2; - uint8_t key[sizeof(bits256) + sizeof(int32_t)]; - int32_t vout,vout2,pair; uint32_t lasttime,errors,swappending; - double profitmargin; - char ipaddr[64],coinaddr[64],spendscript[256],coin[16]; - uint16_t port; -} *LP_utxoinfos; - -struct LP_cacheinfo -{ - UT_hash_handle hh; - struct LP_quoteinfo Q; - uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(int32_t)]; - double price; - uint32_t timestamp; -} *LP_cacheinfos; - -int32_t LP_cachekey(uint8_t *key,char *base,char *rel,bits256 txid,int32_t vout) -{ - uint64_t basebits,relbits; int32_t offset = 0; - basebits = stringbits(base); - relbits = stringbits(rel); - memcpy(&key[offset],&basebits,sizeof(basebits)), offset += sizeof(basebits); - memcpy(&key[offset],&relbits,sizeof(relbits)), offset += sizeof(relbits); - memcpy(&key[offset],&txid,sizeof(txid)), offset += sizeof(txid); - memcpy(&key[offset],&vout,sizeof(vout)), offset += sizeof(vout); - return(offset); -} - -struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout) -{ - struct LP_cacheinfo *ptr=0; uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(vout)]; - if ( LP_cachekey(key,base,rel,txid,vout) == sizeof(key) ) - { - portable_mutex_lock(&LP_cachemutex); - HASH_FIND(hh,LP_cacheinfos,key,sizeof(key),ptr); - portable_mutex_unlock(&LP_cachemutex); - } else printf("LP_cachefind keysize mismatch?\n"); - if ( 0 && ptr != 0 && ptr->timestamp != 0 && ptr->timestamp < time(NULL)-LP_CACHEDURATION ) - { - printf("expire price %.8f\n",ptr->price); - ptr->price = 0.; - ptr->timestamp = 0; - memset(&ptr->Q,0,sizeof(ptr->Q)); - } - return(ptr); -} - -double LP_pricecache(struct LP_quoteinfo *qp,char *base,char *rel,bits256 txid,int32_t vout) -{ - struct LP_cacheinfo *ptr; - if ( (ptr= LP_cachefind(base,rel,txid,vout)) != 0 ) - { - if ( qp != 0 ) - (*qp) = ptr->Q; - if ( ptr->price == 0. && ptr->Q.satoshis != 0 ) - { - printf("null ptr->price? "); - ptr->price = (double)ptr->Q.destsatoshis / ptr->Q.satoshis; - } - //printf("found %s/%s %.8f\n",base,rel,ptr->price); - return(ptr->price); - } - //char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); - return(0.); -} - -struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout,double price,struct LP_quoteinfo *qp) -{ - struct LP_cacheinfo *ptr=0; - if ( (ptr= LP_cachefind(base,rel,txid,vout)) == 0 ) - { - ptr = calloc(1,sizeof(*ptr)); - if ( LP_cachekey(ptr->key,base,rel,txid,vout) == sizeof(ptr->key) ) - { - portable_mutex_lock(&LP_cachemutex); - HASH_ADD(hh,LP_cacheinfos,key,sizeof(ptr->key),ptr); - portable_mutex_unlock(&LP_cachemutex); - } else printf("LP_cacheadd keysize mismatch?\n"); - } //else printf("CACHE hit!\n"); - char str[65]; if ( price != ptr->price ) - printf("updated %s/v%d %s/%s %llu price %.8f\n",bits256_str(str,txid),vout,base,rel,(long long)qp->satoshis,price); - ptr->price = price; - ptr->Q = *qp; - ptr->timestamp = (uint32_t)time(NULL); - printf("cacheadd %.8f\n",price); - return(ptr); -} - -struct LP_peerinfo *LP_peerfind(uint32_t ipbits,uint16_t port) -{ - struct LP_peerinfo *peer=0; uint64_t ip_port; - ip_port = ((uint64_t)port << 32) | ipbits; - portable_mutex_lock(&LP_peermutex); - HASH_FIND(hh,LP_peerinfos,&ip_port,sizeof(ip_port),peer); - portable_mutex_unlock(&LP_peermutex); - return(peer); -} - -struct LP_utxoinfo *LP_utxofind(bits256 txid,int32_t vout) -{ - struct LP_utxoinfo *utxo=0; uint8_t key[sizeof(txid) + sizeof(vout)]; - memcpy(key,txid.bytes,sizeof(txid)); - memcpy(&key[sizeof(txid)],&vout,sizeof(vout)); - portable_mutex_lock(&LP_utxomutex); - HASH_FIND(hh,LP_utxoinfos,key,sizeof(key),utxo); - portable_mutex_unlock(&LP_utxomutex); - return(utxo); -} - -cJSON *LP_peerjson(struct LP_peerinfo *peer) -{ - cJSON *item = cJSON_CreateObject(); - jaddstr(item,"ipaddr",peer->ipaddr); - jaddnum(item,"port",peer->port); - jaddnum(item,"profit",peer->profitmargin); - return(item); -} - -cJSON *LP_utxojson(struct LP_utxoinfo *utxo) -{ - cJSON *item = cJSON_CreateObject(); - jaddstr(item,"ipaddr",utxo->ipaddr); - jaddnum(item,"port",utxo->port); - jaddnum(item,"profit",utxo->profitmargin); - jaddstr(item,"base",utxo->coin); - jaddstr(item,"coin",utxo->coin); - jaddstr(item,"address",utxo->coinaddr); - jaddstr(item,"script",utxo->spendscript); - jaddbits256(item,"txid",utxo->txid); - jaddnum(item,"vout",utxo->vout); - jaddnum(item,"value",dstr(utxo->satoshis)); - jaddbits256(item,"txid2",utxo->txid2); - jaddnum(item,"vout2",utxo->vout2); - jaddnum(item,"value2",dstr(utxo->satoshis2)); - return(item); -} - -char *LP_peers() -{ - struct LP_peerinfo *peer,*tmp; cJSON *peersjson = cJSON_CreateArray(); - HASH_ITER(hh,LP_peerinfos,peer,tmp) - { - jaddi(peersjson,LP_peerjson(peer)); - } - return(jprint(peersjson,1)); -} - -char *LP_utxos(struct LP_peerinfo *mypeer,char *coin,int32_t lastn) -{ - int32_t i,firsti; struct LP_utxoinfo *utxo,*tmp; cJSON *utxosjson = cJSON_CreateArray(); - i = 0; - if ( lastn >= mypeer->numutxos ) - firsti = -1; - else firsti = (mypeer->numutxos - lastn); - HASH_ITER(hh,LP_utxoinfos,utxo,tmp) - { - if ( i++ < firsti ) - continue; - if ( coin == 0 || coin[0] == 0 || strcmp(coin,utxo->coin) == 0 ) - { - jaddi(utxosjson,LP_utxojson(utxo)); - } - } - return(jprint(utxosjson,1)); -} - -struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *ipaddr,uint16_t port,uint16_t pushport,uint16_t subport,double profitmargin,int32_t numpeers,int32_t numutxos) -{ - uint32_t ipbits; int32_t pushsock,subsock,timeout,enabled; char checkip[64],pushaddr[64],subaddr[64]; struct LP_peerinfo *peer = 0; - ipbits = (uint32_t)calc_ipbits(ipaddr); - expand_ipbits(checkip,ipbits); - if ( strcmp(checkip,ipaddr) == 0 ) - { - if ( (peer= LP_peerfind(ipbits,port)) != 0 ) - { - if ( peer->profitmargin == 0. ) - peer->profitmargin = profitmargin; - if ( numpeers > peer->numpeers ) - peer->numpeers = numpeers; - if ( numutxos > peer->numutxos ) - peer->numutxos = numutxos; - } - else - { - //printf("LPaddpeer %s\n",ipaddr); - peer = calloc(1,sizeof(*peer)); - peer->pushsock = peer->subsock = pushsock = subsock = -1; - strcpy(peer->ipaddr,ipaddr); - if ( amclient == 0 ) - enabled = 1; - else enabled = 1;//(rand() % (1 << Client_connections)) == 0; - if ( pushport != 0 && subport != 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 ) - { - timeout = 1000; - nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); - nanomsg_tcpname(pushaddr,peer->ipaddr,pushport); - if ( nn_connect(pushsock,pushaddr) >= 0 ) - { - printf("connected to push.(%s) %d\n",pushaddr,pushsock); - peer->connected = (uint32_t)time(NULL); - peer->pushsock = pushsock; - if ( enabled != 0 && (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 ) - { - timeout = 1; - nn_setsockopt(subsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); - nn_setsockopt(subsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); - nanomsg_tcpname(subaddr,peer->ipaddr,subport); - if ( nn_connect(subsock,subaddr) >= 0 ) - { - peer->subsock = subsock; - printf("connected to sub.(%s) %d\n",subaddr,peer->subsock); - Client_connections += amclient; - } else nn_close(subsock); - } - } - else - { - nn_close(pushsock); - printf("error connecting to push.(%s)\n",pushaddr); - } - } else printf("%s pushport.%u subport.%u pushsock.%d\n",ipaddr,pushport,subport,pushsock); - peer->profitmargin = profitmargin; - peer->ipbits = ipbits; - peer->port = port; - peer->ip_port = ((uint64_t)port << 32) | ipbits; - portable_mutex_lock(&LP_peermutex); - HASH_ADD(hh,LP_peerinfos,ip_port,sizeof(peer->ip_port),peer); - if ( mypeer != 0 ) - { - mypeer->numpeers++; - printf("_LPaddpeer %s -> numpeers.%d mypubsock.%d other.(%d %d)\n",ipaddr,mypeer->numpeers,mypubsock,numpeers,numutxos); - } else peer->numpeers = 1; // will become mypeer - portable_mutex_unlock(&LP_peermutex); - if ( mypubsock >= 0 ) - LP_send(mypubsock,jprint(LP_peerjson(peer),1),1); - } - } else printf("LP_addpeer: checkip.(%s) vs (%s)\n",checkip,ipaddr); - return(peer); -} - -struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *coin,bits256 txid,int32_t vout,int64_t satoshis,bits256 deposittxid,int32_t depositvout,int64_t depositsatoshis,char *spendscript,char *coinaddr,char *ipaddr,uint16_t port,double profitmargin) -{ - struct LP_utxoinfo *utxo = 0; - if ( coin == 0 || coin[0] == 0 || spendscript == 0 || spendscript[0] == 0 || coinaddr == 0 || coinaddr[0] == 0 || bits256_nonz(txid) == 0 || bits256_nonz(deposittxid) == 0 || vout < 0 || depositvout < 0 || satoshis <= 0 || depositsatoshis <= 0 ) - { - printf("malformed addutxo %d %d %d %d %d %d %d %d %d\n", coin == 0,spendscript == 0,coinaddr == 0,bits256_nonz(txid) == 0,bits256_nonz(deposittxid) == 0,vout < 0,depositvout < 0,satoshis <= 0,depositsatoshis <= 0); - return(0); - } - if ( amclient == 0 && strcmp(ipaddr,"127.0.0.1") == 0 ) - { - printf("LP node got localhost utxo\n"); - return(0); - } - if ( (utxo= LP_utxofind(txid,vout)) != 0 ) - { - if ( bits256_cmp(txid,utxo->txid) != 0 || bits256_cmp(deposittxid,utxo->txid2) != 0 || vout != utxo->vout || satoshis != utxo->satoshis || depositvout != utxo->vout2 || depositsatoshis != utxo->satoshis2 || strcmp(coin,utxo->coin) != 0 || strcmp(spendscript,utxo->spendscript) != 0 || strcmp(coinaddr,utxo->coinaddr) != 0 || strcmp(ipaddr,utxo->ipaddr) != 0 || port != utxo->port ) - { - utxo->errors++; - char str[65],str2[65]; printf("error on subsequent utxo add.(%s v %s) %d %d %d %d %d %d %d %d %d %d %d\n",bits256_str(str,txid),bits256_str(str2,utxo->txid),bits256_cmp(txid,utxo->txid) != 0,bits256_cmp(deposittxid,utxo->txid2) != 0,vout != utxo->vout,satoshis != utxo->satoshis,depositvout != utxo->vout2,depositsatoshis != utxo->satoshis2,strcmp(coin,utxo->coin) != 0,strcmp(spendscript,utxo->spendscript) != 0,strcmp(coinaddr,utxo->coinaddr) != 0,strcmp(ipaddr,utxo->ipaddr) != 0,port != utxo->port); - } - else if ( profitmargin != 0. ) - utxo->profitmargin = profitmargin; - } - else - { - utxo = calloc(1,sizeof(*utxo)); - utxo->pair = -1; - utxo->profitmargin = profitmargin; - strcpy(utxo->ipaddr,ipaddr); - utxo->port = port; - safecopy(utxo->coin,coin,sizeof(utxo->coin)); - safecopy(utxo->coinaddr,coinaddr,sizeof(utxo->coinaddr)); - safecopy(utxo->spendscript,spendscript,sizeof(utxo->spendscript)); - utxo->txid = txid; - utxo->vout = vout; - utxo->satoshis = satoshis; - utxo->txid2 = deposittxid; - utxo->vout2 = depositvout; - utxo->satoshis2 = depositsatoshis; - memcpy(utxo->key,txid.bytes,sizeof(txid)); - memcpy(&utxo->key[sizeof(txid)],&vout,sizeof(vout)); - portable_mutex_lock(&LP_utxomutex); - HASH_ADD_KEYPTR(hh,LP_utxoinfos,utxo->key,sizeof(utxo->key),utxo); - if ( mypeer != 0 ) - mypeer->numutxos++; - portable_mutex_unlock(&LP_utxomutex); - if ( mypubsock >= 0 ) - LP_send(mypubsock,jprint(LP_utxojson(utxo),1),1); - //char str[65],str2[65]; printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d %s %s\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0,bits256_str(str,utxo->txid),bits256_str(str2,txid)); - } - return(utxo); -} - -int32_t LP_peersparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *retstr,uint32_t now) -{ - struct LP_peerinfo *peer; uint32_t argipbits; char *argipaddr; uint16_t argport,pushport,subport; cJSON *array,*item; int32_t i,n=0; - if ( (array= cJSON_Parse(retstr)) != 0 ) - { - if ( (n= cJSON_GetArraySize(array)) > 0 ) - { - for (i=0; ilasttime = now; - if ( strcmp(argipaddr,destipaddr) == 0 && destport == argport && peer->numpeers != n ) - peer->numpeers = n; - } - } - } - } - free_json(array); - } - return(n); -} - -int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *retstr,uint32_t now) -{ - struct LP_peerinfo *peer,*destpeer; uint32_t argipbits; char *argipaddr; uint16_t argport,pushport,subport; cJSON *array,*item; int32_t i,n=0; bits256 txid; struct LP_utxoinfo *utxo; - if ( amclient != 0 ) - { - printf("LP_utxosparse not for clientside\n"); - return(-1); - } - if ( (array= cJSON_Parse(retstr)) != 0 ) - { - if ( (n= cJSON_GetArraySize(array)) > 0 ) - { - for (i=0; ilasttime = now; - } - } - } - if ( (destpeer= LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport)) != 0 ) - { - if ( destpeer->numutxos < n ) - { - //destpeer->numutxos = n; - //printf("got.(%s) from %s numutxos.%d\n",retstr,destpeer->ipaddr,destpeer->numutxos); - } - } - } - free_json(array); - } - return(n); -} - -char *issue_LP_getpeers(char *destip,uint16_t destport,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers,int32_t numutxos) -{ - char url[512],*retstr; - sprintf(url,"http://%s:%u/api/stats/getpeers?ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,ipaddr,port,profitmargin,numpeers,numutxos); - //printf("send.(%s)\n",url); - retstr = issue_curl(url); - //printf("GETPEERS.(%s)\n",retstr); - return(retstr); -} - -char *issue_LP_getutxos(char *destip,uint16_t destport,char *coin,int32_t lastn,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers,int32_t numutxos) -{ - char url[512]; - sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,coin,lastn,ipaddr,port,profitmargin,numpeers,numutxos); - return(issue_curl(url)); -} - -char *issue_LP_clientgetutxos(char *destip,uint16_t destport,char *coin,int32_t lastn) -{ - char url[512]; - sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=127.0.0.1&port=0",destip,destport,coin,lastn); - //printf("getutxos.(%s)\n",url); - return(issue_curl(url)); -} - -char *issue_LP_notify(char *destip,uint16_t destport,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers,int32_t numutxos) -{ - char url[512]; - sprintf(url,"http://%s:%u/api/stats/notify?ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,ipaddr,port,profitmargin,numpeers,numutxos); - return(issue_curl(url)); -} - -char *issue_LP_notifyutxo(char *destip,uint16_t destport,struct LP_utxoinfo *utxo) -{ - char url[4096],str[65],str2[65]; - sprintf(url,"http://%s:%u/api/stats/notifyutxo?ipaddr=%s&port=%u&profit=%.6f&coin=%s&txid=%s&vout=%d&value=%.8f&txid2=%s&vout2=%d&value2=%.8f&script=%s&address=%s",destip,destport,utxo->ipaddr,utxo->port,utxo->profitmargin,utxo->coin,bits256_str(str,utxo->txid),utxo->vout,dstr(utxo->satoshis),bits256_str(str2,utxo->txid2),utxo->vout2,dstr(utxo->satoshis2),utxo->spendscript,utxo->coinaddr); - if ( strlen(url) > 1024 ) - printf("WARNING long url.(%s)\n",url); - return(issue_curl(url)); -} - -void LP_peersquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *myipaddr,uint16_t myport,double myprofit) -{ - char *retstr; struct LP_peerinfo *peer,*tmp; uint32_t now,flag = 0; - peer = LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport); - if ( peer != 0 && peer->errors > 0 ) - return; - if ( (retstr= issue_LP_getpeers(destipaddr,destport,myipaddr,myport,myprofit,mypeer!=0?mypeer->numpeers:0,mypeer!=0?mypeer->numutxos:0)) != 0 ) - { - //printf("got.(%s)\n",retstr); - now = (uint32_t)time(NULL); - LP_peersparse(amclient,mypeer,mypubsock,destipaddr,destport,retstr,now); - free(retstr); - if ( amclient == 0 ) - { - HASH_ITER(hh,LP_peerinfos,peer,tmp) - { - if ( peer->lasttime != now ) - { - printf("{%s:%u %.6f}.%d ",peer->ipaddr,peer->port,peer->profitmargin,peer->lasttime - now); - flag++; - if ( (retstr= issue_LP_notify(destipaddr,destport,peer->ipaddr,peer->port,peer->profitmargin,peer->numpeers,0)) != 0 ) - free(retstr); - } - } - if ( flag != 0 ) - printf(" <- missing peers\n"); - } - } else if ( peer != 0 ) - peer->errors++; -} - -void LP_utxosquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *coin,int32_t lastn,char *myipaddr,uint16_t myport,double myprofit) -{ - char *retstr; struct LP_utxoinfo *utxo,*tmp; struct LP_peerinfo *peer; int32_t i,firsti; uint32_t now,flag = 0; - if ( amclient != 0 ) - { - printf("LP_utxosquery not for clientside\n"); - return; - } - peer = LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport); - if ( (peer != 0 && peer->errors > 0) || mypeer == 0 ) - return; - if ( coin == 0 ) - coin = ""; - if ( (retstr= issue_LP_getutxos(destipaddr,destport,coin,lastn,myipaddr,myport,myprofit,mypeer->numpeers,mypeer->numutxos)) != 0 ) - { - now = (uint32_t)time(NULL); - LP_utxosparse(amclient,mypeer,mypubsock,destipaddr,destport,retstr,now); - free(retstr); - i = 0; - if ( lastn >= mypeer->numutxos ) - firsti = -1; - else firsti = (mypeer->numutxos - lastn); - HASH_ITER(hh,LP_utxoinfos,utxo,tmp) - { - if ( i++ < firsti ) - continue; - if ( utxo->lasttime != now && strcmp(utxo->ipaddr,"127.0.0.1") != 0 ) - { - char str[65]; printf("{%s:%u %s} ",utxo->ipaddr,utxo->port,bits256_str(str,utxo->txid)); - flag++; - if ( (retstr= issue_LP_notifyutxo(destipaddr,destport,utxo)) != 0 ) - free(retstr); - } - } - if ( flag != 0 ) - printf(" <- missing utxos\n"); - } else if ( peer != 0 ) - peer->errors++; -} - -int32_t LP_maxvalue(uint64_t *values,int32_t n) -{ - int32_t i,maxi = -1; uint64_t maxval = 0; - for (i=0; i maxval ) - { - maxi = i; - maxval = values[i]; - } - return(maxi); -} - -int32_t LP_nearestvalue(uint64_t *values,int32_t n,uint64_t targetval) -{ - int32_t i,mini = -1; int64_t dist; uint64_t mindist = (1 << 31); - for (i=0; i= 0 && dist < mindist ) - { - mini = i; - mindist = dist; - } - } - return(mini); -} +// stubs int32_t basilisk_istrustedbob(struct basilisk_swap *swap) { // for BTC and if trusted LP @@ -581,283 +53,32 @@ void tradebot_pendingadd(cJSON *tradejson,char *base,double basevolume,char *rel { // add to trades } +char *LP_getdatadir() +{ + return("/root"); +} -char GLOBAL_DBDIR[] = "DB"; +char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_t skip) +{ + return(0); +} #include "LP_secp.c" -#include "LP_rpc.c" #include "LP_bitcoin.c" +#include "LP_coins.c" +#include "LP_rpc.c" +#include "LP_prices.c" #include "LP_transaction.c" #include "LP_remember.c" -//#include "LP_statemachine.c" #include "LP_swap.c" +#include "LP_peers.c" +#include "LP_utxos.c" #include "LP_commands.c" -char *parse_conf_line(char *line,char *field) -{ - line += strlen(field); - for (; *line!='='&&*line!=0; line++) - break; - if ( *line == 0 ) - return(0); - if ( *line == '=' ) - line++; - while ( line[strlen(line)-1] == '\r' || line[strlen(line)-1] == '\n' || line[strlen(line)-1] == ' ' ) - line[strlen(line)-1] = 0; - //printf("LINE.(%s)\n",line); - _stripwhite(line,0); - return(clonestr(line)); -} - -void LP_userpassfp(char *username,char *password,FILE *fp) -{ - char *rpcuser,*rpcpassword,*str,line[8192]; - rpcuser = rpcpassword = 0; - username[0] = password[0] = 0; - while ( fgets(line,sizeof(line),fp) != 0 ) - { - if ( line[0] == '#' ) - continue; - //printf("line.(%s) %p %p\n",line,strstr(line,(char *)"rpcuser"),strstr(line,(char *)"rpcpassword")); - if ( (str= strstr(line,(char *)"rpcuser")) != 0 ) - rpcuser = parse_conf_line(str,(char *)"rpcuser"); - else if ( (str= strstr(line,(char *)"rpcpassword")) != 0 ) - rpcpassword = parse_conf_line(str,(char *)"rpcpassword"); - } - if ( rpcuser != 0 && rpcpassword != 0 ) - { - strcpy(username,rpcuser); - strcpy(password,rpcpassword); - } - //printf("rpcuser.(%s) rpcpassword.(%s) KMDUSERPASS.(%s) %u\n",rpcuser,rpcpassword,KMDUSERPASS,port); - if ( rpcuser != 0 ) - free(rpcuser); - if ( rpcpassword != 0 ) - free(rpcpassword); -} - -void LP_statefname(char *fname,char *symbol,char *assetname,char *str) -{ - sprintf(fname,"%s",LP_getdatadir()); -#ifdef WIN32 - strcat(fname,"\\"); -#else - strcat(fname,"/"); -#endif - if ( strcmp(symbol,"BTC") == 0 ) - strcat(fname,".bitcoin"); - else if ( strcmp(symbol,"LTC") == 0 ) - strcat(fname,".litecoin"); - else - { - strcat(fname,".komodo"); - if ( strcmp(symbol,"KMD") != 0 ) - { -#ifdef WIN32 - strcat(fname,"\\"); -#else - strcat(fname,"/"); -#endif - strcat(fname,assetname); - } - } -#ifdef WIN32 - strcat(fname,"\\"); -#else - strcat(fname,"/"); -#endif - strcat(fname,str); - printf("LP_statefname.(%s) <- %s %s %s\n",fname,symbol,assetname,str); -} - -int32_t LP_userpass(char *userpass,char *symbol,char *assetname,char *confroot) -{ - FILE *fp; char fname[512],username[512],password[512],confname[16]; - userpass[0] = 0; - sprintf(confname,"%s.conf",confroot); -#ifdef __APPLE__ - confname[0] = toupper(confname[0]); -#endif - LP_statefname(fname,symbol,assetname,confname); - if ( (fp= fopen(fname,"rb")) != 0 ) - { - LP_userpassfp(username,password,fp); - sprintf(userpass,"%s:%s",username,password); - fclose(fp); - return((int32_t)strlen(userpass)); - } - return(-1); -} - -uint32_t LP_assetmagic(char *symbol,uint64_t supply) -{ - uint8_t buf[512]; int32_t len = 0; - if ( strcmp(symbol,"KMD") == 0 ) - return(0x8de4eef9); - len = iguana_rwnum(1,&buf[len],sizeof(supply),(void *)&supply); - strcpy((char *)&buf[len],symbol); - len += strlen(symbol); - return(calc_crc32(0,buf,len)); -} - -uint16_t LP_assetport(uint32_t magic) -{ - if ( magic == 0x8de4eef9 ) - return(7771); - else return(8000 + (magic % 7777)); -} - -uint16_t LP_rpcport(char *symbol,uint64_t supply,uint32_t *magicp) -{ - if ( symbol == 0 || symbol[0] == 0 || strcmp("KMD",symbol) == 0 ) - { - *magicp = 0x8de4eef9; - return(7771); - } - else if ( strcmp("BTC",symbol) == 0 ) - return(8332); - else if ( strcmp("LTC",symbol) == 0 ) - return(9332); - *magicp = LP_assetmagic(symbol,supply); - return(LP_assetport(*magicp)+1); -} - -struct iguana_info *LP_coinfind(char *symbol) -{ - static struct iguana_info *LP_coins; static int32_t LP_numcoins; - struct iguana_info *coin,cdata; int32_t i; uint32_t magic; uint16_t port; - for (i=0; ipubtype); - { - char tmpstr[128]; - bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); - printf("%s (%s) %d wif.(%s) (%s)\n",symbol,coinaddr,coin->pubtype,tmpstr,passphrase); - if ( (retjson= LP_importprivkey(coin->symbol,tmpstr,coinaddr,-1)) != 0 ) - printf("importprivkey -> (%s)\n",jprint(retjson,1)); - } - bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); - LP_privkeyadd(privkey,rmd160); - if ( (array= LP_listunspent(symbol,coinaddr)) != 0 ) - { - if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) - { - values = calloc(n,sizeof(*values)); - for (i=0; i= 0 ) - { - item = jitem(array,i); - deposittxid = jbits256(item,"txid"); - depositvout = juint(item,"vout"); - script = jstr(item,"scriptPubKey"); - depositval = values[i]; - values[i] = 0, used++; - if ( amclient != 0 ) - targetval = (depositval / 776) + 50000; - else targetval = (depositval / 9) * 8; - //printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); - if ( (i= LP_nearestvalue(values,n,targetval)) >= 0 ) - { - item = jitem(array,i); - txid = jbits256(item,"txid"); - vout = juint(item,"vout"); - printf("j.%d %.8f target %.8f\n",i,dstr(values[i]),dstr(targetval)); - if ( jstr(item,"scriptPubKey") != 0 && strcmp(script,jstr(item,"scriptPubKey")) == 0 ) - { - value = values[i]; - values[i] = 0, used++; - if ( amclient == 0 ) - { - if ( (utxo= LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin)) != 0 ) - utxo->mypub = curve25519(privkey,curve25519_basepoint9()); - } - else - { - if ( (utxo= LP_addutxo(amclient,mypeer,mypubsock,symbol,deposittxid,depositvout,depositval,txid,vout,value,script,coinaddr,"127.0.0.1",0,0)) != 0 ) - utxo->mypub = curve25519(privkey,curve25519_basepoint9()); - } - total += value; - } - } - } else break; - } - free(values); - } - free_json(array); - } - return(total); -} - -void LP_privkey_updates(struct LP_peerinfo *mypeer,int32_t pubsock,char *passphrase,int32_t amclient) -{ - int32_t i; - for (i=0; icoin,"KMD") == 0 ) + LP_priceping(pubsock,utxo,"BTC",profitmargin); + else LP_priceping(pubsock,utxo,"KMD",profitmargin); + } + } HASH_ITER(hh,LP_peerinfos,peer,tmp) { n++; @@ -1052,16 +282,3 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profit } LP_mainloop(mypeer,mypubport,pubsock,pullsock,myport,amclient,passphrase,profitmargin); } - -/*#ifdef __APPLE__ -int32_t nn_bind() { return(-1); } -int32_t nn_close() { return(-1); } -int32_t nn_connect() { return(-1); } -int32_t nn_freemsg() { return(-1); } -int32_t nn_poll() { return(-1); } -int32_t nn_recv() { return(-1); } -int32_t nn_send() { return(-1); } -int32_t nn_setsockopt() { return(-1); } -int32_t nn_socket() { return(-1); } - -#endif*/ diff --git a/iguana/exchanges/LP_network.c b/iguana/exchanges/LP_network.c index faf6137f3..84a7cb512 100644 --- a/iguana/exchanges/LP_network.c +++ b/iguana/exchanges/LP_network.c @@ -18,94 +18,6 @@ // marketmaker // -void basilisk_psockinit(struct basilisk_swap *swap,int32_t amlp) -{ -/* char keystr[64],databuf[1024],pubkeystr[128],*retstr,*retstr2,*datastr,*pushaddr=0,*subaddr=0; cJSON *retjson,*addrjson; uint8_t data[512]; int32_t datalen,timeout,pushsock = -1,subsock = -1; - if ( swap->connected == 1 ) - return; - if ( swap->pushsock < 0 && swap->subsock < 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 && (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 ) - { - timeout = 1000; - nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); - timeout = 1; - nn_setsockopt(subsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); - nn_setsockopt(subsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); - swap->pushsock = pushsock; - swap->subsock = subsock; - } - if ( (subsock= swap->subsock) < 0 || (pushsock= swap->pushsock) < 0 ) - { - printf("error getting nn_sockets\n"); - return; - } - sprintf(keystr,"%08x-%08x",swap->I.req.requestid,swap->I.req.quoteid); - if ( swap->connected == 0 && (retstr= _dex_kvsearch("KV",keystr)) != 0 ) - { - if ( (retjson= cJSON_Parse(retstr)) != 0 ) - { - if ( (datastr= jstr(retjson,"value")) != 0 ) - { - datalen = (int32_t)strlen(datastr) >> 1; - decode_hex((uint8_t *)databuf,datalen,datastr); - if ( (addrjson= cJSON_Parse(databuf)) != 0 ) - { - pushaddr = jstr(addrjson,"push"); - subaddr = jstr(addrjson,"sub"); - if ( pushaddr != 0 && subaddr != 0 ) - { - printf("KV decoded (%s and %s) %d %d\n",pushaddr,subaddr,swap->pushsock,swap->subsock); - if ( nn_connect(swap->pushsock,pushaddr) >= 0 && nn_connect(swap->subsock,subaddr) >= 0 ) - swap->connected = 1; - } - free_json(addrjson); - } - } - free_json(retjson); - } - printf("KVsearch.(%s) -> (%s) connected.%d socks.(%d %d) amlp.%d\n",keystr,retstr,swap->connected,swap->pushsock,swap->subsock,amlp); - free(retstr); - } - printf("connected.%d amlp.%d subsock.%d pushsock.%d\n",swap->connected,amlp,subsock,pushsock); - if ( swap->connected <= 0 && amlp != 0 && subsock >= 0 && pushsock >= 0 ) - { - if ( (retstr= _dex_psock("{}")) != 0 ) - { - printf("psock returns.(%s)\n",retstr); - // {"result":"success","pushaddr":"tcp://5.9.102.210:30002","subaddr":"tcp://5.9.102.210:30003","randipbits":3606291758,"coin":"KMD","tag":"6952562460568228137"} - if ( (retjson= cJSON_Parse(retstr)) != 0 ) - { - pushaddr = jstr(retjson,"pushaddr"); - subaddr = jstr(retjson,"subaddr"); - if ( pushaddr != 0 && subaddr != 0 ) - { - if ( nn_connect(pushsock,pushaddr) >= 0 ) - { - printf("connected to %d pushaddr.(%s)\n",pushsock,pushaddr); - if ( nn_connect(subsock,subaddr) >= 0 ) - { - swap->connected = 1; - init_hexbytes_noT(pubkeystr,myinfo->persistent_pubkey33,33); - sprintf((char *)data,"{\"push\":\"%s\",\"sub\":\"%s\",\"trade\":[\"%s\", %.8f, \"%s\", %.8f],\"pub\":\"%s\"}",pushaddr,subaddr,swap->I.req.src,dstr(swap->I.req.srcamount),swap->I.req.dest,dstr(swap->I.req.destamount),pubkeystr); - datalen = (int32_t)strlen((char *)data) + 1; - printf("datalen.%d (%s)\n",datalen,(char *)data); - init_hexbytes_noT(databuf,data,datalen); - printf("%s -> %s\n",keystr,databuf); - if ( (retstr2= _dex_kvupdate("KV",keystr,databuf,1)) != 0 ) - { - printf("KVupdate.(%s)\n",retstr2); - free(retstr2); - } - } else printf("nn_connect error to %d subaddr.(%s)\n",subsock,subaddr); - } else printf("nn_connect error to %d pushaddr.(%s)\n",pushsock,pushaddr); - } - else printf("missing addr (%p) (%p) (%s)\n",pushaddr,subaddr,jprint(retjson,0)); - free_json(retjson); - } else printf("Error parsing psock.(%s)\n",retstr); - free(retstr); - } else printf("error issuing _dex_psock\n"); - }*/ -} - char *nanomsg_tcpname(char *str,char *ipaddr,uint16_t port) { sprintf(str,"tcp://%s:%u",ipaddr,port); @@ -168,144 +80,3 @@ uint32_t LP_swapsend(int32_t pairsock,struct basilisk_swap *swap,uint32_t msgbit free(buf); return(nextbits); } - -void basilisk_swap_sendabort(struct basilisk_swap *swap) -{ - uint32_t msgbits = 0; uint8_t buf[sizeof(msgbits) + sizeof(swap->I.req.quoteid) + sizeof(bits256)*2]; int32_t sentbytes,offset=0; - memset(buf,0,sizeof(buf)); - offset += iguana_rwnum(1,&buf[offset],sizeof(swap->I.req.quoteid),&swap->I.req.quoteid); - offset += iguana_rwnum(1,&buf[offset],sizeof(msgbits),&msgbits); - if ( (sentbytes= nn_send(swap->pushsock,buf,offset,0)) != offset ) - { - if ( sentbytes < 0 ) - { - if ( swap->pushsock >= 0 ) // - nn_close(swap->pushsock), swap->pushsock = -1; - if ( swap->subsock >= 0 ) // - nn_close(swap->subsock), swap->subsock = -1; - swap->connected = 0; - } - } else printf("basilisk_swap_sendabort\n"); -} - -void basilisk_psockinit(struct basilisk_swap *swap,int32_t amlp); - -void basilisk_swapgotdata(struct basilisk_swap *swap,uint32_t crc32,bits256 srchash,bits256 desthash,uint32_t quoteid,uint32_t msgbits,uint8_t *data,int32_t datalen,int32_t reinit) -{ - int32_t i; struct basilisk_swapmessage *mp; - for (i=0; inummessages; i++) - if ( crc32 == swap->messages[i].crc32 && msgbits == swap->messages[i].msgbits && bits256_cmp(srchash,swap->messages[i].srchash) == 0 && bits256_cmp(desthash,swap->messages[i].desthash) == 0 ) - return; - //printf(" new message.[%d] datalen.%d Q.%x msg.%x [%llx]\n",swap->nummessages,datalen,quoteid,msgbits,*(long long *)data); - swap->messages = realloc(swap->messages,sizeof(*swap->messages) * (swap->nummessages + 1)); - mp = &swap->messages[swap->nummessages++]; - mp->crc32 = crc32; - mp->srchash = srchash; - mp->desthash = desthash; - mp->msgbits = msgbits; - mp->quoteid = quoteid; - mp->data = malloc(datalen); - mp->datalen = datalen; - memcpy(mp->data,data,datalen); - if ( reinit == 0 && swap->fp != 0 ) - { - fwrite(mp,1,sizeof(*mp),swap->fp); - fwrite(data,1,datalen,swap->fp); - fflush(swap->fp); - } -} - -int32_t basilisk_swapget(struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t maxlen,int32_t (*basilisk_verify_func)(void *ptr,uint8_t *data,int32_t datalen)) -{ - uint8_t *ptr; bits256 srchash,desthash; uint32_t crc32,_msgbits,quoteid; int32_t i,size,offset,retval = -1; struct basilisk_swapmessage *mp = 0; - while ( (size= nn_recv(swap->subsock,&ptr,NN_MSG,0)) >= 0 ) - { - swap->lasttime = (uint32_t)time(NULL); - memset(srchash.bytes,0,sizeof(srchash)); - memset(desthash.bytes,0,sizeof(desthash)); - //printf("gotmsg.[%d] crc.%x\n",size,crc32); - offset = 0; - for (i=0; i<32; i++) - srchash.bytes[i] = ptr[offset++]; - for (i=0; i<32; i++) - desthash.bytes[i] = ptr[offset++]; - offset += iguana_rwnum(0,&ptr[offset],sizeof(uint32_t),"eid); - offset += iguana_rwnum(0,&ptr[offset],sizeof(uint32_t),&_msgbits); - if ( size > offset ) - { - crc32 = calc_crc32(0,&ptr[offset],size-offset); - if ( size > offset ) - { - //printf("size.%d offset.%d datalen.%d\n",size,offset,size-offset); - basilisk_swapgotdata(swap,crc32,srchash,desthash,quoteid,_msgbits,&ptr[offset],size-offset,0); - } - } - else if ( bits256_nonz(srchash) == 0 && bits256_nonz(desthash) == 0 ) - { - if ( swap->aborted == 0 ) - { - swap->aborted = (uint32_t)time(NULL); - printf("got abort signal from other side\n"); - } - } else printf("basilisk_swapget: got strange packet\n"); - if ( ptr != 0 ) - nn_freemsg(ptr), ptr = 0; - } - //char str[65],str2[65]; - for (i=0; inummessages; i++) - { - //printf("%d: %s vs %s\n",i,bits256_str(str,swap->messages[i].srchash),bits256_str(str2,swap->messages[i].desthash)); - if ( bits256_cmp(swap->messages[i].desthash,swap->I.myhash) == 0 ) - { - if ( swap->messages[i].msgbits == msgbits ) - { - if ( swap->I.iambob == 0 && swap->lasttime != 0 && time(NULL) > swap->lasttime+360 ) - { - printf("nothing received for a while from Bob, try new sockets\n"); - if ( swap->pushsock >= 0 ) // - nn_close(swap->pushsock), swap->pushsock = -1; - if ( swap->subsock >= 0 ) // - nn_close(swap->subsock), swap->subsock = -1; - swap->connected = 0; - basilisk_psockinit(swap,swap->I.iambob != 0); - } - mp = &swap->messages[i]; - if ( msgbits != 0x80000000 ) - break; - } - } - } - if ( mp != 0 ) - retval = (*basilisk_verify_func)(swap,mp->data,mp->datalen); - //printf("mine/other %s vs %s\n",bits256_str(str,swap->I.myhash),bits256_str(str2,swap->I.otherhash)); - return(retval); -} - -int32_t basilisk_messagekeyread(uint8_t *key,uint32_t *channelp,uint32_t *msgidp,bits256 *srchashp,bits256 *desthashp) -{ - int32_t keylen = 0; - keylen += iguana_rwnum(0,&key[keylen],sizeof(uint32_t),channelp); - keylen += iguana_rwnum(0,&key[keylen],sizeof(uint32_t),msgidp); - keylen += iguana_rwbignum(0,&key[keylen],sizeof(*srchashp),srchashp->bytes); - keylen += iguana_rwbignum(0,&key[keylen],sizeof(*desthashp),desthashp->bytes); - return(keylen); -} - -int32_t basilisk_messagekey(uint8_t *key,uint32_t channel,uint32_t msgid,bits256 srchash,bits256 desthash) -{ - int32_t keylen = 0; - keylen += iguana_rwnum(1,&key[keylen],sizeof(uint32_t),&channel); - keylen += iguana_rwnum(1,&key[keylen],sizeof(uint32_t),&msgid); - keylen += iguana_rwbignum(1,&key[keylen],sizeof(srchash),srchash.bytes); - keylen += iguana_rwbignum(1,&key[keylen],sizeof(desthash),desthash.bytes); - return(keylen); -} - -void LP_channelsend(bits256 srchash,bits256 desthash,uint32_t channel,uint32_t msgid,uint8_t *data,int32_t datalen) -{ - int32_t keylen; uint8_t key[BASILISK_KEYSIZE]; //char *retstr; - keylen = basilisk_messagekey(key,channel,msgid,srchash,desthash); - //if ( (retstr= _dex_reqsend(myinfo,"DEX",key,keylen,data,datalen)) != 0 ) - // free(retstr); -} - diff --git a/iguana/exchanges/LP_peers.c b/iguana/exchanges/LP_peers.c new file mode 100644 index 000000000..e7d83a93e --- /dev/null +++ b/iguana/exchanges/LP_peers.c @@ -0,0 +1,188 @@ + +/****************************************************************************** + * Copyright © 2014-2017 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ +// +// LP_peers.c +// marketmaker +// + +struct LP_peerinfo *LP_peerfind(uint32_t ipbits,uint16_t port) +{ + struct LP_peerinfo *peer=0; uint64_t ip_port; + ip_port = ((uint64_t)port << 32) | ipbits; + portable_mutex_lock(&LP_peermutex); + HASH_FIND(hh,LP_peerinfos,&ip_port,sizeof(ip_port),peer); + portable_mutex_unlock(&LP_peermutex); + return(peer); +} + +cJSON *LP_peerjson(struct LP_peerinfo *peer) +{ + cJSON *item = cJSON_CreateObject(); + jaddstr(item,"ipaddr",peer->ipaddr); + jaddnum(item,"port",peer->port); + jaddnum(item,"profit",peer->profitmargin); + return(item); +} + +char *LP_peers() +{ + struct LP_peerinfo *peer,*tmp; cJSON *peersjson = cJSON_CreateArray(); + HASH_ITER(hh,LP_peerinfos,peer,tmp) + { + jaddi(peersjson,LP_peerjson(peer)); + } + return(jprint(peersjson,1)); +} + +struct LP_peerinfo *LP_addpeer(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *ipaddr,uint16_t port,uint16_t pushport,uint16_t subport,double profitmargin,int32_t numpeers,int32_t numutxos) +{ + uint32_t ipbits; int32_t pushsock,subsock,timeout,enabled; char checkip[64],pushaddr[64],subaddr[64]; struct LP_peerinfo *peer = 0; + ipbits = (uint32_t)calc_ipbits(ipaddr); + expand_ipbits(checkip,ipbits); + if ( strcmp(checkip,ipaddr) == 0 ) + { + if ( (peer= LP_peerfind(ipbits,port)) != 0 ) + { + if ( peer->profitmargin == 0. ) + peer->profitmargin = profitmargin; + if ( numpeers > peer->numpeers ) + peer->numpeers = numpeers; + if ( numutxos > peer->numutxos ) + peer->numutxos = numutxos; + } + else + { + //printf("LPaddpeer %s\n",ipaddr); + peer = calloc(1,sizeof(*peer)); + peer->pushsock = peer->subsock = pushsock = subsock = -1; + strcpy(peer->ipaddr,ipaddr); + if ( amclient == 0 ) + enabled = 1; + else enabled = 1;//(rand() % (1 << Client_connections)) == 0; + if ( pushport != 0 && subport != 0 && (pushsock= nn_socket(AF_SP,NN_PUSH)) >= 0 ) + { + timeout = 1000; + nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); + nanomsg_tcpname(pushaddr,peer->ipaddr,pushport); + if ( nn_connect(pushsock,pushaddr) >= 0 ) + { + printf("connected to push.(%s) %d\n",pushaddr,pushsock); + peer->connected = (uint32_t)time(NULL); + peer->pushsock = pushsock; + if ( enabled != 0 && (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 ) + { + timeout = 1; + nn_setsockopt(subsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); + nn_setsockopt(subsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); + nanomsg_tcpname(subaddr,peer->ipaddr,subport); + if ( nn_connect(subsock,subaddr) >= 0 ) + { + peer->subsock = subsock; + printf("connected to sub.(%s) %d\n",subaddr,peer->subsock); + Client_connections += amclient; + } else nn_close(subsock); + } + } + else + { + nn_close(pushsock); + printf("error connecting to push.(%s)\n",pushaddr); + } + } else printf("%s pushport.%u subport.%u pushsock.%d\n",ipaddr,pushport,subport,pushsock); + peer->profitmargin = profitmargin; + peer->ipbits = ipbits; + peer->port = port; + peer->ip_port = ((uint64_t)port << 32) | ipbits; + portable_mutex_lock(&LP_peermutex); + HASH_ADD(hh,LP_peerinfos,ip_port,sizeof(peer->ip_port),peer); + if ( mypeer != 0 ) + { + mypeer->numpeers++; + printf("_LPaddpeer %s -> numpeers.%d mypubsock.%d other.(%d %d)\n",ipaddr,mypeer->numpeers,mypubsock,numpeers,numutxos); + } else peer->numpeers = 1; // will become mypeer + portable_mutex_unlock(&LP_peermutex); + if ( mypubsock >= 0 ) + LP_send(mypubsock,jprint(LP_peerjson(peer),1),1); + } + } else printf("LP_addpeer: checkip.(%s) vs (%s)\n",checkip,ipaddr); + return(peer); +} + +int32_t LP_peersparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *retstr,uint32_t now) +{ + struct LP_peerinfo *peer; uint32_t argipbits; char *argipaddr; uint16_t argport,pushport,subport; cJSON *array,*item; int32_t i,n=0; + if ( (array= cJSON_Parse(retstr)) != 0 ) + { + if ( (n= cJSON_GetArraySize(array)) > 0 ) + { + for (i=0; ilasttime = now; + if ( strcmp(argipaddr,destipaddr) == 0 && destport == argport && peer->numpeers != n ) + peer->numpeers = n; + } + } + } + } + free_json(array); + } + return(n); +} + +void LP_peersquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *myipaddr,uint16_t myport,double myprofit) +{ + char *retstr; struct LP_peerinfo *peer,*tmp; uint32_t now,flag = 0; + peer = LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport); + if ( peer != 0 && peer->errors > 0 ) + return; + if ( (retstr= issue_LP_getpeers(destipaddr,destport,myipaddr,myport,myprofit,mypeer!=0?mypeer->numpeers:0,mypeer!=0?mypeer->numutxos:0)) != 0 ) + { + //printf("got.(%s)\n",retstr); + now = (uint32_t)time(NULL); + LP_peersparse(amclient,mypeer,mypubsock,destipaddr,destport,retstr,now); + free(retstr); + if ( amclient == 0 ) + { + HASH_ITER(hh,LP_peerinfos,peer,tmp) + { + if ( peer->lasttime != now ) + { + printf("{%s:%u %.6f}.%d ",peer->ipaddr,peer->port,peer->profitmargin,peer->lasttime - now); + flag++; + if ( (retstr= issue_LP_notify(destipaddr,destport,peer->ipaddr,peer->port,peer->profitmargin,peer->numpeers,0)) != 0 ) + free(retstr); + } + } + if ( flag != 0 ) + printf(" <- missing peers\n"); + } + } else if ( peer != 0 ) + peer->errors++; +} diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index 4d1fff738..37cd3275c 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -20,8 +20,168 @@ double LP_kmdbtc; +struct LP_cacheinfo +{ + UT_hash_handle hh; + struct LP_quoteinfo Q; + uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(int32_t)]; + double price; + uint32_t timestamp; +} *LP_cacheinfos; + +int32_t LP_cachekey(uint8_t *key,char *base,char *rel,bits256 txid,int32_t vout) +{ + uint64_t basebits,relbits; int32_t offset = 0; + basebits = stringbits(base); + relbits = stringbits(rel); + memcpy(&key[offset],&basebits,sizeof(basebits)), offset += sizeof(basebits); + memcpy(&key[offset],&relbits,sizeof(relbits)), offset += sizeof(relbits); + memcpy(&key[offset],&txid,sizeof(txid)), offset += sizeof(txid); + memcpy(&key[offset],&vout,sizeof(vout)), offset += sizeof(vout); + return(offset); +} + +struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout) +{ + struct LP_cacheinfo *ptr=0; uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(vout)]; + if ( LP_cachekey(key,base,rel,txid,vout) == sizeof(key) ) + { + portable_mutex_lock(&LP_cachemutex); + HASH_FIND(hh,LP_cacheinfos,key,sizeof(key),ptr); + portable_mutex_unlock(&LP_cachemutex); + } else printf("LP_cachefind keysize mismatch?\n"); + if ( 0 && ptr != 0 && ptr->timestamp != 0 && ptr->timestamp < time(NULL)-LP_CACHEDURATION ) + { + printf("expire price %.8f\n",ptr->price); + ptr->price = 0.; + ptr->timestamp = 0; + memset(&ptr->Q,0,sizeof(ptr->Q)); + } + return(ptr); +} + +double LP_pricecache(struct LP_quoteinfo *qp,char *base,char *rel,bits256 txid,int32_t vout) +{ + struct LP_cacheinfo *ptr; + if ( (ptr= LP_cachefind(base,rel,txid,vout)) != 0 ) + { + if ( qp != 0 ) + (*qp) = ptr->Q; + if ( ptr->price == 0. && ptr->Q.satoshis != 0 ) + { + printf("null ptr->price? "); + ptr->price = (double)ptr->Q.destsatoshis / ptr->Q.satoshis; + } + //printf("found %s/%s %.8f\n",base,rel,ptr->price); + return(ptr->price); + } + //char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); + return(0.); +} + +struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout,double price,struct LP_quoteinfo *qp) +{ + char str[65]; struct LP_cacheinfo *ptr=0; + if ( (ptr= LP_cachefind(base,rel,txid,vout)) == 0 ) + { + ptr = calloc(1,sizeof(*ptr)); + if ( LP_cachekey(ptr->key,base,rel,txid,vout) == sizeof(ptr->key) ) + { + portable_mutex_lock(&LP_cachemutex); + HASH_ADD(hh,LP_cacheinfos,key,sizeof(ptr->key),ptr); + portable_mutex_unlock(&LP_cachemutex); + } else printf("LP_cacheadd keysize mismatch?\n"); + } + if ( price != ptr->price ) + { + printf("updated %s/v%d %s/%s %llu price %.8f\n",bits256_str(str,txid),vout,base,rel,(long long)qp->satoshis,price); + } + ptr->price = price; + ptr->Q = *qp; + ptr->timestamp = (uint32_t)time(NULL); + return(ptr); +} + +static int _cmp_orderbook(const void *a,const void *b) +{ +#define ptr_a ((struct LP_cacheinfo *)a)->price +#define ptr_b ((struct LP_cacheinfo *)b)->price + if ( ptr_b > ptr_a ) + return(1); + else if ( ptr_b < ptr_a ) + return(-1); + else + { +#undef ptr_a +#undef ptr_b +#define ptr_a ((struct LP_cacheinfo *)a)->Q.satoshis +#define ptr_b ((struct LP_cacheinfo *)b)->Q.satoshis + if ( ptr_b > ptr_a ) + return(1); + else if ( ptr_b < ptr_a ) + return(-1); + } + return(0); +#undef ptr_a +#undef ptr_b +} + +static int _cmp_orderbookrev(const void *a,const void *b) +{ + return(-_cmp_orderbook(a,b)); +} + +cJSON *LP_orderbookjson(struct LP_cacheinfo *ptr,int32_t polarity) +{ + double price,volume; cJSON *item = cJSON_CreateObject(); + if ( (price= ptr->price) != 0. && (volume= dstr(ptr->Q.satoshis)) != 0. ) + { + jaddnum(item,"price",polarity > 0 ? price : 1. / price); + jaddnum(item,"volume",polarity > 0 ? volume : volume / price); + jaddbits256(item,"txid",ptr->Q.txid); + jaddnum(item,"vout",ptr->Q.vout); + } + return(item); +} + +char *LP_orderbook(char *base,char *rel) +{ + uint32_t now,i; struct LP_cacheinfo *ptr,*tmp,**bids = 0,**asks = 0; cJSON *retjson,*array; int32_t numbids=0,numasks=0; + now = (uint32_t)time(NULL); + HASH_ITER(hh,LP_cacheinfos,ptr,tmp) + { + if ( ptr->timestamp < now-60 || ptr->price == 0. ) + continue; + if ( strcmp(ptr->Q.srccoin,base) == 0 && strcmp(ptr->Q.destcoin,rel) == 0 ) + { + asks = realloc(asks,sizeof(*asks) * (numasks+1)); + asks[numasks++] = ptr; + } + else if ( strcmp(ptr->Q.srccoin,rel) == 0 && strcmp(ptr->Q.destcoin,base) == 0 ) + { + bids = realloc(bids,sizeof(*bids) * (numbids+1)); + bids[numbids++] = ptr; + } + } + retjson = cJSON_CreateObject(); + array = cJSON_CreateArray(); + if ( numbids > 1 ) + qsort(bids,numbids,sizeof(*bids),_cmp_orderbook); + for (i=0; i 1 ) + qsort(asks,numasks,sizeof(*asks),_cmp_orderbookrev); + for (i=0; iswapsfp= fopen(fname,"rb+")) != 0 ) - { - while ( fread(&requestid,1,sizeof(requestid),myinfo->swapsfp) == sizeof(requestid) && fread("eid,1,sizeof(quoteid),myinfo->swapsfp) == sizeof(quoteid) ) - { - sprintf(fname,"%s/SWAPS/%u-%u",GLOBAL_DBDIR,requestid,quoteid), OS_compatible_path(fname); - printf("%s\n",fname); - if ( (fp= fopen(fname,"rb+")) != 0 ) // check to see if completed - { - memset(&M,0,sizeof(M)); - swapcompleted = 1; - for (iter=0; iter<2; iter++) - { - if ( fread(privkey.bytes,1,sizeof(privkey),fp) == sizeof(privkey) && - fread(&R,1,sizeof(R),fp) == sizeof(R) && - fread(&statebits,1,sizeof(statebits),fp) == sizeof(statebits) && - fread(&optionduration,1,sizeof(optionduration),fp) == sizeof(optionduration) ) - { - while ( 0 && fread(&M,1,sizeof(M),fp) == sizeof(M) ) - { - M.data = 0; - //printf("entry iter.%d crc32.%x datalen.%d\n",iter,M.crc32,M.datalen); - if ( M.datalen < 100000 ) - { - M.data = malloc(M.datalen); - if ( fread(M.data,1,M.datalen,fp) == M.datalen ) - { - if ( calc_crc32(0,M.data,M.datalen) == M.crc32 ) - { - if ( iter == 1 ) - { - if ( swap == 0 ) - { - swap = basilisk_thread_start(privkey,&R,statebits,optionduration,1); - swap->I.choosei = swap->I.otherchoosei = -1; - } - if ( swap != 0 ) - basilisk_swapgotdata(swap,M.crc32,M.srchash,M.desthash,M.quoteid,M.msgbits,M.data,M.datalen,1); - } - } else printf("crc mismatch %x vs %x\n",calc_crc32(0,M.data,M.datalen),M.crc32); - } else printf("error reading M.datalen %d\n",M.datalen); - free(M.data), M.data = 0; - } - } - } - if ( swapcompleted != 0 ) - break; - rewind(fp); - } - } - } - } else myinfo->swapsfp = fopen(fname,"wb+"); -}*/ - -FILE *basilisk_swap_save(struct basilisk_swap *swap,bits256 privkey,struct basilisk_request *rp,uint32_t statebits,int32_t optionduration,int32_t reinit) -{ - FILE *fp=0; /*char fname[512]; - sprintf(fname,"%s/SWAPS/%u-%u",GLOBAL_DBDIR,rp->requestid,rp->quoteid), OS_compatible_path(fname); - if ( (fp= fopen(fname,"rb+")) == 0 ) - { - if ( (fp= fopen(fname,"wb+")) != 0 ) - { - fwrite(privkey.bytes,1,sizeof(privkey),fp); - fwrite(rp,1,sizeof(*rp),fp); - fwrite(&statebits,1,sizeof(statebits),fp); - fwrite(&optionduration,1,sizeof(optionduration),fp); - fflush(fp); - } - } - else if ( reinit != 0 ) - { - }*/ - return(fp); -} - -int32_t basilisk_swap_load(uint32_t requestid,uint32_t quoteid,bits256 *privkeyp,struct basilisk_request *rp,uint32_t *statebitsp,int32_t *optiondurationp) -{ - FILE *fp=0; char fname[512]; int32_t retval = -1; - sprintf(fname,"%s/SWAPS/%u-%u",GLOBAL_DBDIR,requestid,quoteid), OS_compatible_path(fname); - if ( (fp= fopen(fname,"rb+")) != 0 ) - { - if ( fread(privkeyp,1,sizeof(*privkeyp),fp) == sizeof(*privkeyp) && - fread(rp,1,sizeof(*rp),fp) == sizeof(*rp) && - fread(statebitsp,1,sizeof(*statebitsp),fp) == sizeof(*statebitsp) && - fread(optiondurationp,1,sizeof(*optiondurationp),fp) == sizeof(*optiondurationp) ) - retval = 0; - fclose(fp); - } - return(retval); -} - -void basilisk_swap_saveupdate(struct basilisk_swap *swap) -{ - FILE *fp; char fname[512]; - sprintf(fname,"%s/SWAPS/%u-%u.swap",GLOBAL_DBDIR,swap->I.req.requestid,swap->I.req.quoteid), OS_compatible_path(fname); - if ( (fp= fopen(fname,"wb")) != 0 ) - { - fwrite(&swap->I,1,sizeof(swap->I),fp); - /*fwrite(&swap->bobdeposit,1,sizeof(swap->bobdeposit),fp); - fwrite(&swap->bobpayment,1,sizeof(swap->bobpayment),fp); - fwrite(&swap->alicepayment,1,sizeof(swap->alicepayment),fp); - fwrite(&swap->myfee,1,sizeof(swap->myfee),fp); - fwrite(&swap->otherfee,1,sizeof(swap->otherfee),fp); - fwrite(&swap->aliceclaim,1,sizeof(swap->aliceclaim),fp); - fwrite(&swap->alicespend,1,sizeof(swap->alicespend),fp); - fwrite(&swap->bobreclaim,1,sizeof(swap->bobreclaim),fp); - fwrite(&swap->bobspend,1,sizeof(swap->bobspend),fp); - fwrite(&swap->bobrefund,1,sizeof(swap->bobrefund),fp); - fwrite(&swap->alicereclaim,1,sizeof(swap->alicereclaim),fp);*/ - fwrite(swap->privkeys,1,sizeof(swap->privkeys),fp); - fwrite(swap->otherdeck,1,sizeof(swap->otherdeck),fp); - fwrite(swap->deck,1,sizeof(swap->deck),fp); - fclose(fp); - } -} - -/*int32_t basilisk_swap_loadtx(struct basilisk_rawtx *rawtx,FILE *fp,char *bobcoinstr,char *alicecoinstr) -{ - if ( fread(rawtx,1,sizeof(*rawtx),fp) == sizeof(*rawtx) ) - { - rawtx->coin = 0; - rawtx->vins = 0; - if ( strcmp(rawtx->I.coinstr,bobcoinstr) == 0 || strcmp(rawtx->I.coinstr,alicecoinstr) == 0 ) - { - rawtx->coin = LP_coinfind(rawtx->I.coinstr); - if ( rawtx->vinstr[0] != 0 ) - rawtx->vins = cJSON_Parse(rawtx->vinstr); - printf("loaded.%s len.%d\n",rawtx->name,rawtx->I.datalen); - return(0); - } - } - return(-1); -}*/ - void basilisk_dontforget_userdata(char *userdataname,FILE *fp,uint8_t *script,int32_t scriptlen) { int32_t i; char scriptstr[513]; @@ -350,8 +213,6 @@ void basilisk_dontforget_update(struct basilisk_swap *swap,struct basilisk_rawtx basilisk_dontforget(swap,&swap->bobreclaim,swap->bobpayment.I.locktime,triggertxid); } - - bits256 basilisk_swap_privbob_extract(char *symbol,bits256 spendtxid,int32_t vini,int32_t revflag) { bits256 privkey; int32_t i,scriptlen,siglen; uint8_t script[1024]; // from Bob refund of Bob deposit diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index d3742e5e0..ed3784b55 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -17,31 +17,45 @@ // LP_rpc.c // marketmaker // +char *issue_LP_getpeers(char *destip,uint16_t destport,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers,int32_t numutxos) +{ + char url[512],*retstr; + sprintf(url,"http://%s:%u/api/stats/getpeers?ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,ipaddr,port,profitmargin,numpeers,numutxos); + //printf("send.(%s)\n",url); + retstr = issue_curl(url); + //printf("GETPEERS.(%s)\n",retstr); + return(retstr); +} -char *LP_getdatadir() +char *issue_LP_getutxos(char *destip,uint16_t destport,char *coin,int32_t lastn,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers,int32_t numutxos) { - return("/root"); + char url[512]; + sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,coin,lastn,ipaddr,port,profitmargin,numpeers,numutxos); + return(issue_curl(url)); } -cJSON *basilisk_nullretjson(cJSON *retjson) +char *issue_LP_clientgetutxos(char *destip,uint16_t destport,char *coin,int32_t lastn) { - /*char *outstr; - if ( retjson != 0 ) - { - outstr = jprint(retjson,0); - if ( strcmp(outstr,"{}") == 0 || strcmp(outstr,"[]") == 0 ) - { - free_json(retjson); - retjson = 0; - } - free(outstr); - }*/ - return(retjson); + char url[512]; + sprintf(url,"http://%s:%u/api/stats/getutxos?coin=%s&lastn=%d&ipaddr=127.0.0.1&port=0",destip,destport,coin,lastn); + //printf("getutxos.(%s)\n",url); + return(issue_curl(url)); } -char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_t skip) +char *issue_LP_notify(char *destip,uint16_t destport,char *ipaddr,uint16_t port,double profitmargin,int32_t numpeers,int32_t numutxos) { - return(0); + char url[512]; + sprintf(url,"http://%s:%u/api/stats/notify?ipaddr=%s&port=%u&profit=%.6f&numpeers=%d&numutxos=%d",destip,destport,ipaddr,port,profitmargin,numpeers,numutxos); + return(issue_curl(url)); +} + +char *issue_LP_notifyutxo(char *destip,uint16_t destport,struct LP_utxoinfo *utxo) +{ + char url[4096],str[65],str2[65]; + sprintf(url,"http://%s:%u/api/stats/notifyutxo?ipaddr=%s&port=%u&profit=%.6f&coin=%s&txid=%s&vout=%d&value=%.8f&txid2=%s&vout2=%d&value2=%.8f&script=%s&address=%s",destip,destport,utxo->ipaddr,utxo->port,utxo->profitmargin,utxo->coin,bits256_str(str,utxo->txid),utxo->vout,dstr(utxo->satoshis),bits256_str(str2,utxo->txid2),utxo->vout2,dstr(utxo->satoshis2),utxo->spendscript,utxo->coinaddr); + if ( strlen(url) > 1024 ) + printf("WARNING long url.(%s)\n",url); + return(issue_curl(url)); } cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params) @@ -59,7 +73,7 @@ cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params) } //printf("dpow_gettxout.(%s)\n",retstr); } - return(basilisk_nullretjson(retjson)); + return(retjson); } void LP_unspents_mark(char *symbol,cJSON *vins) diff --git a/iguana/exchanges/LP_statemachine.c b/iguana/exchanges/LP_statemachine.c index 9b4604c25..dc0240c1b 100644 --- a/iguana/exchanges/LP_statemachine.c +++ b/iguana/exchanges/LP_statemachine.c @@ -17,6 +17,317 @@ // LP_statemachine.c // marketmaker // + +/*void basilisk_swaps_init(struct supernet_info *myinfo) + { + char fname[512]; uint32_t iter,swapcompleted,requestid,quoteid,optionduration,statebits; FILE *fp; bits256 privkey;struct basilisk_request R; struct basilisk_swapmessage M; struct basilisk_swap *swap = 0; + sprintf(fname,"%s/SWAPS/list",GLOBAL_DBDIR), OS_compatible_path(fname); + if ( (myinfo->swapsfp= fopen(fname,"rb+")) != 0 ) + { + while ( fread(&requestid,1,sizeof(requestid),myinfo->swapsfp) == sizeof(requestid) && fread("eid,1,sizeof(quoteid),myinfo->swapsfp) == sizeof(quoteid) ) + { + sprintf(fname,"%s/SWAPS/%u-%u",GLOBAL_DBDIR,requestid,quoteid), OS_compatible_path(fname); + printf("%s\n",fname); + if ( (fp= fopen(fname,"rb+")) != 0 ) // check to see if completed + { + memset(&M,0,sizeof(M)); + swapcompleted = 1; + for (iter=0; iter<2; iter++) + { + if ( fread(privkey.bytes,1,sizeof(privkey),fp) == sizeof(privkey) && + fread(&R,1,sizeof(R),fp) == sizeof(R) && + fread(&statebits,1,sizeof(statebits),fp) == sizeof(statebits) && + fread(&optionduration,1,sizeof(optionduration),fp) == sizeof(optionduration) ) + { + while ( 0 && fread(&M,1,sizeof(M),fp) == sizeof(M) ) + { + M.data = 0; + //printf("entry iter.%d crc32.%x datalen.%d\n",iter,M.crc32,M.datalen); + if ( M.datalen < 100000 ) + { + M.data = malloc(M.datalen); + if ( fread(M.data,1,M.datalen,fp) == M.datalen ) + { + if ( calc_crc32(0,M.data,M.datalen) == M.crc32 ) + { + if ( iter == 1 ) + { + if ( swap == 0 ) + { + swap = basilisk_thread_start(privkey,&R,statebits,optionduration,1); + swap->I.choosei = swap->I.otherchoosei = -1; + } + if ( swap != 0 ) + basilisk_swapgotdata(swap,M.crc32,M.srchash,M.desthash,M.quoteid,M.msgbits,M.data,M.datalen,1); + } + } else printf("crc mismatch %x vs %x\n",calc_crc32(0,M.data,M.datalen),M.crc32); + } else printf("error reading M.datalen %d\n",M.datalen); + free(M.data), M.data = 0; + } + } + } + if ( swapcompleted != 0 ) + break; + rewind(fp); + } + } + } + } else myinfo->swapsfp = fopen(fname,"wb+"); + }*/ + +FILE *basilisk_swap_save(struct basilisk_swap *swap,bits256 privkey,struct basilisk_request *rp,uint32_t statebits,int32_t optionduration,int32_t reinit) +{ + FILE *fp=0; /*char fname[512]; + sprintf(fname,"%s/SWAPS/%u-%u",GLOBAL_DBDIR,rp->requestid,rp->quoteid), OS_compatible_path(fname); + if ( (fp= fopen(fname,"rb+")) == 0 ) + { + if ( (fp= fopen(fname,"wb+")) != 0 ) + { + fwrite(privkey.bytes,1,sizeof(privkey),fp); + fwrite(rp,1,sizeof(*rp),fp); + fwrite(&statebits,1,sizeof(statebits),fp); + fwrite(&optionduration,1,sizeof(optionduration),fp); + fflush(fp); + } + } + else if ( reinit != 0 ) + { + }*/ + return(fp); +} + +int32_t basilisk_swap_load(uint32_t requestid,uint32_t quoteid,bits256 *privkeyp,struct basilisk_request *rp,uint32_t *statebitsp,int32_t *optiondurationp) +{ + FILE *fp=0; char fname[512]; int32_t retval = -1; + sprintf(fname,"%s/SWAPS/%u-%u",GLOBAL_DBDIR,requestid,quoteid), OS_compatible_path(fname); + if ( (fp= fopen(fname,"rb+")) != 0 ) + { + if ( fread(privkeyp,1,sizeof(*privkeyp),fp) == sizeof(*privkeyp) && + fread(rp,1,sizeof(*rp),fp) == sizeof(*rp) && + fread(statebitsp,1,sizeof(*statebitsp),fp) == sizeof(*statebitsp) && + fread(optiondurationp,1,sizeof(*optiondurationp),fp) == sizeof(*optiondurationp) ) + retval = 0; + fclose(fp); + } + return(retval); +} + +void basilisk_swap_saveupdate(struct basilisk_swap *swap) +{ + FILE *fp; char fname[512]; + sprintf(fname,"%s/SWAPS/%u-%u.swap",GLOBAL_DBDIR,swap->I.req.requestid,swap->I.req.quoteid), OS_compatible_path(fname); + if ( (fp= fopen(fname,"wb")) != 0 ) + { + fwrite(&swap->I,1,sizeof(swap->I),fp); + /*fwrite(&swap->bobdeposit,1,sizeof(swap->bobdeposit),fp); + fwrite(&swap->bobpayment,1,sizeof(swap->bobpayment),fp); + fwrite(&swap->alicepayment,1,sizeof(swap->alicepayment),fp); + fwrite(&swap->myfee,1,sizeof(swap->myfee),fp); + fwrite(&swap->otherfee,1,sizeof(swap->otherfee),fp); + fwrite(&swap->aliceclaim,1,sizeof(swap->aliceclaim),fp); + fwrite(&swap->alicespend,1,sizeof(swap->alicespend),fp); + fwrite(&swap->bobreclaim,1,sizeof(swap->bobreclaim),fp); + fwrite(&swap->bobspend,1,sizeof(swap->bobspend),fp); + fwrite(&swap->bobrefund,1,sizeof(swap->bobrefund),fp); + fwrite(&swap->alicereclaim,1,sizeof(swap->alicereclaim),fp);*/ + fwrite(swap->privkeys,1,sizeof(swap->privkeys),fp); + fwrite(swap->otherdeck,1,sizeof(swap->otherdeck),fp); + fwrite(swap->deck,1,sizeof(swap->deck),fp); + fclose(fp); + } +} + +/*int32_t basilisk_swap_loadtx(struct basilisk_rawtx *rawtx,FILE *fp,char *bobcoinstr,char *alicecoinstr) + { + if ( fread(rawtx,1,sizeof(*rawtx),fp) == sizeof(*rawtx) ) + { + rawtx->coin = 0; + rawtx->vins = 0; + if ( strcmp(rawtx->I.coinstr,bobcoinstr) == 0 || strcmp(rawtx->I.coinstr,alicecoinstr) == 0 ) + { + rawtx->coin = LP_coinfind(rawtx->I.coinstr); + if ( rawtx->vinstr[0] != 0 ) + rawtx->vins = cJSON_Parse(rawtx->vinstr); + printf("loaded.%s len.%d\n",rawtx->name,rawtx->I.datalen); + return(0); + } + } + return(-1); + }*/ + +void basilisk_swap_sendabort(struct basilisk_swap *swap) +{ + uint32_t msgbits = 0; uint8_t buf[sizeof(msgbits) + sizeof(swap->I.req.quoteid) + sizeof(bits256)*2]; int32_t sentbytes,offset=0; + memset(buf,0,sizeof(buf)); + offset += iguana_rwnum(1,&buf[offset],sizeof(swap->I.req.quoteid),&swap->I.req.quoteid); + offset += iguana_rwnum(1,&buf[offset],sizeof(msgbits),&msgbits); + if ( (sentbytes= nn_send(swap->pushsock,buf,offset,0)) != offset ) + { + if ( sentbytes < 0 ) + { + if ( swap->pushsock >= 0 ) // + nn_close(swap->pushsock), swap->pushsock = -1; + if ( swap->subsock >= 0 ) // + nn_close(swap->subsock), swap->subsock = -1; + swap->connected = 0; + } + } else printf("basilisk_swap_sendabort\n"); +} + +void basilisk_psockinit(struct basilisk_swap *swap,int32_t amlp); + +void basilisk_swapgotdata(struct basilisk_swap *swap,uint32_t crc32,bits256 srchash,bits256 desthash,uint32_t quoteid,uint32_t msgbits,uint8_t *data,int32_t datalen,int32_t reinit) +{ + int32_t i; struct basilisk_swapmessage *mp; + for (i=0; inummessages; i++) + if ( crc32 == swap->messages[i].crc32 && msgbits == swap->messages[i].msgbits && bits256_cmp(srchash,swap->messages[i].srchash) == 0 && bits256_cmp(desthash,swap->messages[i].desthash) == 0 ) + return; + //printf(" new message.[%d] datalen.%d Q.%x msg.%x [%llx]\n",swap->nummessages,datalen,quoteid,msgbits,*(long long *)data); + swap->messages = realloc(swap->messages,sizeof(*swap->messages) * (swap->nummessages + 1)); + mp = &swap->messages[swap->nummessages++]; + mp->crc32 = crc32; + mp->srchash = srchash; + mp->desthash = desthash; + mp->msgbits = msgbits; + mp->quoteid = quoteid; + mp->data = malloc(datalen); + mp->datalen = datalen; + memcpy(mp->data,data,datalen); + if ( reinit == 0 && swap->fp != 0 ) + { + fwrite(mp,1,sizeof(*mp),swap->fp); + fwrite(data,1,datalen,swap->fp); + fflush(swap->fp); + } +} + +int32_t basilisk_swapget(struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t maxlen,int32_t (*basilisk_verify_func)(void *ptr,uint8_t *data,int32_t datalen)) +{ + uint8_t *ptr; bits256 srchash,desthash; uint32_t crc32,_msgbits,quoteid; int32_t i,size,offset,retval = -1; struct basilisk_swapmessage *mp = 0; + while ( (size= nn_recv(swap->subsock,&ptr,NN_MSG,0)) >= 0 ) + { + swap->lasttime = (uint32_t)time(NULL); + memset(srchash.bytes,0,sizeof(srchash)); + memset(desthash.bytes,0,sizeof(desthash)); + //printf("gotmsg.[%d] crc.%x\n",size,crc32); + offset = 0; + for (i=0; i<32; i++) + srchash.bytes[i] = ptr[offset++]; + for (i=0; i<32; i++) + desthash.bytes[i] = ptr[offset++]; + offset += iguana_rwnum(0,&ptr[offset],sizeof(uint32_t),"eid); + offset += iguana_rwnum(0,&ptr[offset],sizeof(uint32_t),&_msgbits); + if ( size > offset ) + { + crc32 = calc_crc32(0,&ptr[offset],size-offset); + if ( size > offset ) + { + //printf("size.%d offset.%d datalen.%d\n",size,offset,size-offset); + basilisk_swapgotdata(swap,crc32,srchash,desthash,quoteid,_msgbits,&ptr[offset],size-offset,0); + } + } + else if ( bits256_nonz(srchash) == 0 && bits256_nonz(desthash) == 0 ) + { + if ( swap->aborted == 0 ) + { + swap->aborted = (uint32_t)time(NULL); + printf("got abort signal from other side\n"); + } + } else printf("basilisk_swapget: got strange packet\n"); + if ( ptr != 0 ) + nn_freemsg(ptr), ptr = 0; + } + //char str[65],str2[65]; + for (i=0; inummessages; i++) + { + //printf("%d: %s vs %s\n",i,bits256_str(str,swap->messages[i].srchash),bits256_str(str2,swap->messages[i].desthash)); + if ( bits256_cmp(swap->messages[i].desthash,swap->I.myhash) == 0 ) + { + if ( swap->messages[i].msgbits == msgbits ) + { + if ( swap->I.iambob == 0 && swap->lasttime != 0 && time(NULL) > swap->lasttime+360 ) + { + printf("nothing received for a while from Bob, try new sockets\n"); + if ( swap->pushsock >= 0 ) // + nn_close(swap->pushsock), swap->pushsock = -1; + if ( swap->subsock >= 0 ) // + nn_close(swap->subsock), swap->subsock = -1; + swap->connected = 0; + basilisk_psockinit(swap,swap->I.iambob != 0); + } + mp = &swap->messages[i]; + if ( msgbits != 0x80000000 ) + break; + } + } + } + if ( mp != 0 ) + retval = (*basilisk_verify_func)(swap,mp->data,mp->datalen); + //printf("mine/other %s vs %s\n",bits256_str(str,swap->I.myhash),bits256_str(str2,swap->I.otherhash)); + return(retval); +} + +int32_t basilisk_messagekeyread(uint8_t *key,uint32_t *channelp,uint32_t *msgidp,bits256 *srchashp,bits256 *desthashp) +{ + int32_t keylen = 0; + keylen += iguana_rwnum(0,&key[keylen],sizeof(uint32_t),channelp); + keylen += iguana_rwnum(0,&key[keylen],sizeof(uint32_t),msgidp); + keylen += iguana_rwbignum(0,&key[keylen],sizeof(*srchashp),srchashp->bytes); + keylen += iguana_rwbignum(0,&key[keylen],sizeof(*desthashp),desthashp->bytes); + return(keylen); +} + +int32_t basilisk_messagekey(uint8_t *key,uint32_t channel,uint32_t msgid,bits256 srchash,bits256 desthash) +{ + int32_t keylen = 0; + keylen += iguana_rwnum(1,&key[keylen],sizeof(uint32_t),&channel); + keylen += iguana_rwnum(1,&key[keylen],sizeof(uint32_t),&msgid); + keylen += iguana_rwbignum(1,&key[keylen],sizeof(srchash),srchash.bytes); + keylen += iguana_rwbignum(1,&key[keylen],sizeof(desthash),desthash.bytes); + return(keylen); +} + +void LP_channelsend(bits256 srchash,bits256 desthash,uint32_t channel,uint32_t msgid,uint8_t *data,int32_t datalen) +{ + int32_t keylen; uint8_t key[BASILISK_KEYSIZE]; //char *retstr; + keylen = basilisk_messagekey(key,channel,msgid,srchash,desthash); + //if ( (retstr= _dex_reqsend(myinfo,"DEX",key,keylen,data,datalen)) != 0 ) + // free(retstr); +} + + +#ifdef adfafds +void iguana_ensure_privkey(struct iguana_info *coin,bits256 privkey) +{ + uint8_t pubkey33[33]; struct iguana_waccount *wacct; struct iguana_waddress *waddr,addr; char coinaddr[128]; + bitcoin_pubkey33(swap->ctx,pubkey33,privkey); + bitcoin_address(coinaddr,coin->pubtype,pubkey33,33); + //printf("privkey for (%s)\n",coinaddr); + if ( myinfo->expiration != 0 && ((waddr= iguana_waddresssearch(&wacct,coinaddr)) == 0 || bits256_nonz(waddr->privkey) == 0) ) + { + if ( waddr == 0 ) + { + memset(&addr,0,sizeof(addr)); + iguana_waddresscalc(coin->pubtype,coin->wiftype,&addr,privkey); + if ( (wacct= iguana_waccountfind("default")) != 0 ) + waddr = iguana_waddressadd(coin,wacct,&addr,0); + } + if ( waddr != 0 ) + { + waddr->privkey = privkey; + if ( bitcoin_priv2wif(waddr->wifstr,waddr->privkey,coin->wiftype) > 0 ) + { + if ( (0) && waddr->wiftype != coin->wiftype ) + printf("ensurepriv warning: mismatched wiftype %02x != %02x\n",waddr->wiftype,coin->wiftype); + if ( (0) && waddr->addrtype != coin->pubtype ) + printf("ensurepriv warning: mismatched addrtype %02x != %02x\n",waddr->addrtype,coin->pubtype); + } + } + } +} +#endif + + int32_t basilisk_rawtx_return(struct basilisk_rawtx *rawtx,cJSON *item,int32_t lockinputs,struct vin_info *V) { char *signedtx,*txbytes; cJSON *vins,*privkeyarray; int32_t i,n,retval = -1; diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 6ee6499f7..63ca5beee 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -202,37 +202,6 @@ int32_t iguana_msgtx_Vset(uint8_t *serialized,int32_t maxlen,struct iguana_msgtx return(len); } -#ifdef adfafds -void iguana_ensure_privkey(struct iguana_info *coin,bits256 privkey) -{ - uint8_t pubkey33[33]; struct iguana_waccount *wacct; struct iguana_waddress *waddr,addr; char coinaddr[128]; - bitcoin_pubkey33(swap->ctx,pubkey33,privkey); - bitcoin_address(coinaddr,coin->pubtype,pubkey33,33); - //printf("privkey for (%s)\n",coinaddr); - if ( myinfo->expiration != 0 && ((waddr= iguana_waddresssearch(&wacct,coinaddr)) == 0 || bits256_nonz(waddr->privkey) == 0) ) - { - if ( waddr == 0 ) - { - memset(&addr,0,sizeof(addr)); - iguana_waddresscalc(coin->pubtype,coin->wiftype,&addr,privkey); - if ( (wacct= iguana_waccountfind("default")) != 0 ) - waddr = iguana_waddressadd(coin,wacct,&addr,0); - } - if ( waddr != 0 ) - { - waddr->privkey = privkey; - if ( bitcoin_priv2wif(waddr->wifstr,waddr->privkey,coin->wiftype) > 0 ) - { - if ( (0) && waddr->wiftype != coin->wiftype ) - printf("ensurepriv warning: mismatched wiftype %02x != %02x\n",waddr->wiftype,coin->wiftype); - if ( (0) && waddr->addrtype != coin->pubtype ) - printf("ensurepriv warning: mismatched addrtype %02x != %02x\n",waddr->addrtype,coin->pubtype); - } - } - } -} -#endif - int32_t iguana_interpreter(struct iguana_info *coin,cJSON *logarray,int64_t nLockTime,struct vin_info *V,int32_t numvins) { uint8_t script[IGUANA_MAXSCRIPTSIZE],*activescript,savescript[IGUANA_MAXSCRIPTSIZE]; char str[IGUANA_MAXSCRIPTSIZE*2+1]; int32_t vini,scriptlen,activescriptlen,savelen,errs = 0; cJSON *spendscript,*item=0; diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c new file mode 100644 index 000000000..b620c65c5 --- /dev/null +++ b/iguana/exchanges/LP_utxos.c @@ -0,0 +1,327 @@ + +/****************************************************************************** + * Copyright © 2014-2017 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ +// +// LP_utxos.c +// marketmaker +// + +struct LP_utxoinfo *LP_utxofind(bits256 txid,int32_t vout) +{ + struct LP_utxoinfo *utxo=0; uint8_t key[sizeof(txid) + sizeof(vout)]; + memcpy(key,txid.bytes,sizeof(txid)); + memcpy(&key[sizeof(txid)],&vout,sizeof(vout)); + portable_mutex_lock(&LP_utxomutex); + HASH_FIND(hh,LP_utxoinfos,key,sizeof(key),utxo); + portable_mutex_unlock(&LP_utxomutex); + return(utxo); +} + +cJSON *LP_utxojson(struct LP_utxoinfo *utxo) +{ + cJSON *item = cJSON_CreateObject(); + jaddstr(item,"ipaddr",utxo->ipaddr); + jaddnum(item,"port",utxo->port); + jaddnum(item,"profit",utxo->profitmargin); + jaddstr(item,"base",utxo->coin); + jaddstr(item,"coin",utxo->coin); + jaddstr(item,"address",utxo->coinaddr); + jaddstr(item,"script",utxo->spendscript); + jaddbits256(item,"txid",utxo->txid); + jaddnum(item,"vout",utxo->vout); + jaddnum(item,"value",dstr(utxo->satoshis)); + jaddbits256(item,"txid2",utxo->txid2); + jaddnum(item,"vout2",utxo->vout2); + jaddnum(item,"value2",dstr(utxo->satoshis2)); + return(item); +} + +char *LP_utxos(struct LP_peerinfo *mypeer,char *coin,int32_t lastn) +{ + int32_t i,firsti; struct LP_utxoinfo *utxo,*tmp; cJSON *utxosjson = cJSON_CreateArray(); + i = 0; + if ( lastn >= mypeer->numutxos ) + firsti = -1; + else firsti = (mypeer->numutxos - lastn); + HASH_ITER(hh,LP_utxoinfos,utxo,tmp) + { + if ( i++ < firsti ) + continue; + if ( coin == 0 || coin[0] == 0 || strcmp(coin,utxo->coin) == 0 ) + { + jaddi(utxosjson,LP_utxojson(utxo)); + } + } + return(jprint(utxosjson,1)); +} + +struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *coin,bits256 txid,int32_t vout,int64_t satoshis,bits256 deposittxid,int32_t depositvout,int64_t depositsatoshis,char *spendscript,char *coinaddr,char *ipaddr,uint16_t port,double profitmargin) +{ + struct LP_utxoinfo *utxo = 0; + if ( coin == 0 || coin[0] == 0 || spendscript == 0 || spendscript[0] == 0 || coinaddr == 0 || coinaddr[0] == 0 || bits256_nonz(txid) == 0 || bits256_nonz(deposittxid) == 0 || vout < 0 || depositvout < 0 || satoshis <= 0 || depositsatoshis <= 0 ) + { + printf("malformed addutxo %d %d %d %d %d %d %d %d %d\n", coin == 0,spendscript == 0,coinaddr == 0,bits256_nonz(txid) == 0,bits256_nonz(deposittxid) == 0,vout < 0,depositvout < 0,satoshis <= 0,depositsatoshis <= 0); + return(0); + } + if ( amclient == 0 && strcmp(ipaddr,"127.0.0.1") == 0 ) + { + printf("LP node got localhost utxo\n"); + return(0); + } + if ( (utxo= LP_utxofind(txid,vout)) != 0 ) + { + if ( bits256_cmp(txid,utxo->txid) != 0 || bits256_cmp(deposittxid,utxo->txid2) != 0 || vout != utxo->vout || satoshis != utxo->satoshis || depositvout != utxo->vout2 || depositsatoshis != utxo->satoshis2 || strcmp(coin,utxo->coin) != 0 || strcmp(spendscript,utxo->spendscript) != 0 || strcmp(coinaddr,utxo->coinaddr) != 0 || strcmp(ipaddr,utxo->ipaddr) != 0 || port != utxo->port ) + { + utxo->errors++; + char str[65],str2[65]; printf("error on subsequent utxo add.(%s v %s) %d %d %d %d %d %d %d %d %d %d %d\n",bits256_str(str,txid),bits256_str(str2,utxo->txid),bits256_cmp(txid,utxo->txid) != 0,bits256_cmp(deposittxid,utxo->txid2) != 0,vout != utxo->vout,satoshis != utxo->satoshis,depositvout != utxo->vout2,depositsatoshis != utxo->satoshis2,strcmp(coin,utxo->coin) != 0,strcmp(spendscript,utxo->spendscript) != 0,strcmp(coinaddr,utxo->coinaddr) != 0,strcmp(ipaddr,utxo->ipaddr) != 0,port != utxo->port); + } + else if ( profitmargin != 0. ) + utxo->profitmargin = profitmargin; + } + else + { + utxo = calloc(1,sizeof(*utxo)); + utxo->pair = -1; + utxo->profitmargin = profitmargin; + strcpy(utxo->ipaddr,ipaddr); + utxo->port = port; + safecopy(utxo->coin,coin,sizeof(utxo->coin)); + safecopy(utxo->coinaddr,coinaddr,sizeof(utxo->coinaddr)); + safecopy(utxo->spendscript,spendscript,sizeof(utxo->spendscript)); + utxo->txid = txid; + utxo->vout = vout; + utxo->satoshis = satoshis; + utxo->txid2 = deposittxid; + utxo->vout2 = depositvout; + utxo->satoshis2 = depositsatoshis; + memcpy(utxo->key,txid.bytes,sizeof(txid)); + memcpy(&utxo->key[sizeof(txid)],&vout,sizeof(vout)); + portable_mutex_lock(&LP_utxomutex); + HASH_ADD_KEYPTR(hh,LP_utxoinfos,utxo->key,sizeof(utxo->key),utxo); + if ( mypeer != 0 ) + mypeer->numutxos++; + portable_mutex_unlock(&LP_utxomutex); + if ( mypubsock >= 0 ) + LP_send(mypubsock,jprint(LP_utxojson(utxo),1),1); + //char str[65],str2[65]; printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d %s %s\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0,bits256_str(str,utxo->txid),bits256_str(str2,txid)); + } + return(utxo); +} + +int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *retstr,uint32_t now) +{ + struct LP_peerinfo *peer,*destpeer; uint32_t argipbits; char *argipaddr; uint16_t argport,pushport,subport; cJSON *array,*item; int32_t i,n=0; bits256 txid; struct LP_utxoinfo *utxo; + if ( amclient != 0 ) + { + printf("LP_utxosparse not for clientside\n"); + return(-1); + } + if ( (array= cJSON_Parse(retstr)) != 0 ) + { + if ( (n= cJSON_GetArraySize(array)) > 0 ) + { + for (i=0; ilasttime = now; + } + } + } + if ( (destpeer= LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport)) != 0 ) + { + if ( destpeer->numutxos < n ) + { + //destpeer->numutxos = n; + //printf("got.(%s) from %s numutxos.%d\n",retstr,destpeer->ipaddr,destpeer->numutxos); + } + } + } + free_json(array); + } + return(n); +} + +void LP_utxosquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *coin,int32_t lastn,char *myipaddr,uint16_t myport,double myprofit) +{ + char *retstr; struct LP_utxoinfo *utxo,*tmp; struct LP_peerinfo *peer; int32_t i,firsti; uint32_t now,flag = 0; + if ( amclient != 0 ) + { + printf("LP_utxosquery not for clientside\n"); + return; + } + peer = LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport); + if ( (peer != 0 && peer->errors > 0) || mypeer == 0 ) + return; + if ( coin == 0 ) + coin = ""; + if ( (retstr= issue_LP_getutxos(destipaddr,destport,coin,lastn,myipaddr,myport,myprofit,mypeer->numpeers,mypeer->numutxos)) != 0 ) + { + now = (uint32_t)time(NULL); + LP_utxosparse(amclient,mypeer,mypubsock,destipaddr,destport,retstr,now); + free(retstr); + i = 0; + if ( lastn >= mypeer->numutxos ) + firsti = -1; + else firsti = (mypeer->numutxos - lastn); + HASH_ITER(hh,LP_utxoinfos,utxo,tmp) + { + if ( i++ < firsti ) + continue; + if ( utxo->lasttime != now && strcmp(utxo->ipaddr,"127.0.0.1") != 0 ) + { + char str[65]; printf("{%s:%u %s} ",utxo->ipaddr,utxo->port,bits256_str(str,utxo->txid)); + flag++; + if ( (retstr= issue_LP_notifyutxo(destipaddr,destport,utxo)) != 0 ) + free(retstr); + } + } + if ( flag != 0 ) + printf(" <- missing utxos\n"); + } else if ( peer != 0 ) + peer->errors++; +} + +int32_t LP_maxvalue(uint64_t *values,int32_t n) +{ + int32_t i,maxi = -1; uint64_t maxval = 0; + for (i=0; i maxval ) + { + maxi = i; + maxval = values[i]; + } + return(maxi); +} + +int32_t LP_nearestvalue(uint64_t *values,int32_t n,uint64_t targetval) +{ + int32_t i,mini = -1; int64_t dist; uint64_t mindist = (1 << 31); + for (i=0; i= 0 && dist < mindist ) + { + mini = i; + mindist = dist; + } + } + return(mini); +} + +uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symbol,char *passphrase,char *wifstr,int32_t amclient) +{ + char coinaddr[64],*script; struct LP_utxoinfo *utxo; cJSON *array,*item,*retjson; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; struct iguana_info *coin = LP_coinfind(symbol); + if ( coin == 0 ) + { + printf("cant add privkey for %s, coin not active\n",symbol); + return(0); + } + if ( passphrase != 0 ) + conv_NXTpassword(privkey.bytes,pubkey.bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); + else privkey = iguana_wif2privkey(wifstr); + iguana_priv2pub(pubkey33,coinaddr,privkey,coin->pubtype); + { + char tmpstr[128]; + bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); + printf("%s (%s) %d wif.(%s) (%s)\n",symbol,coinaddr,coin->pubtype,tmpstr,passphrase); + if ( (retjson= LP_importprivkey(coin->symbol,tmpstr,coinaddr,-1)) != 0 ) + printf("importprivkey -> (%s)\n",jprint(retjson,1)); + } + bitcoin_addr2rmd160(&tmptype,rmd160,coinaddr); + LP_privkeyadd(privkey,rmd160); + if ( (array= LP_listunspent(symbol,coinaddr)) != 0 ) + { + if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) + { + values = calloc(n,sizeof(*values)); + for (i=0; i= 0 ) + { + item = jitem(array,i); + deposittxid = jbits256(item,"txid"); + depositvout = juint(item,"vout"); + script = jstr(item,"scriptPubKey"); + depositval = values[i]; + values[i] = 0, used++; + if ( amclient != 0 ) + targetval = (depositval / 776) + 50000; + else targetval = (depositval / 9) * 8; + //printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); + if ( (i= LP_nearestvalue(values,n,targetval)) >= 0 ) + { + item = jitem(array,i); + txid = jbits256(item,"txid"); + vout = juint(item,"vout"); + printf("j.%d %.8f target %.8f\n",i,dstr(values[i]),dstr(targetval)); + if ( jstr(item,"scriptPubKey") != 0 && strcmp(script,jstr(item,"scriptPubKey")) == 0 ) + { + value = values[i]; + values[i] = 0, used++; + if ( amclient == 0 ) + { + if ( (utxo= LP_addutxo(amclient,mypeer,mypubsock,symbol,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr,LP_peerinfos[0].ipaddr,LP_peerinfos[0].port,LP_peerinfos[0].profitmargin)) != 0 ) + utxo->mypub = curve25519(privkey,curve25519_basepoint9()); + } + else + { + if ( (utxo= LP_addutxo(amclient,mypeer,mypubsock,symbol,deposittxid,depositvout,depositval,txid,vout,value,script,coinaddr,"127.0.0.1",0,0)) != 0 ) + utxo->mypub = curve25519(privkey,curve25519_basepoint9()); + } + total += value; + } + } + } else break; + } + free(values); + } + free_json(array); + } + return(total); +} + +void LP_privkey_updates(struct LP_peerinfo *mypeer,int32_t pubsock,char *passphrase,int32_t amclient) +{ + int32_t i; + for (i=0; i (b) ? (a) : (b)) char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port); #include "stats.c" -#include "LP_prices.c" +void LP_priceupdate(char *base,char *rel,double price,double avebid,double aveask,double highbid,double lowask,double PAXPRICES[32]); #if defined(__APPLE__) || defined(WIN32) || defined(USE_STATIC_NANOMSG) #include "../../crypto777/nanosrc/nn.h" From 6e580760fae7ef393245c5c46d2289b95240513f Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 2 Jun 2017 19:46:42 +0300 Subject: [PATCH 285/339] Test --- iguana/exchanges/LP_coins.c | 41 ++++++++----------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/iguana/exchanges/LP_coins.c b/iguana/exchanges/LP_coins.c index d3f136c00..3ea9332d9 100644 --- a/iguana/exchanges/LP_coins.c +++ b/iguana/exchanges/LP_coins.c @@ -18,6 +18,7 @@ // marketmaker // +char *portstrs[][2] = { { "BTC", "8332" }, { "KMD", "7771" }, { "LTC", "9332" }, { "REVS", "10196" }, { "JUMBLR", "15106" }, }; char *parse_conf_line(char *line,char *field) { @@ -115,50 +116,26 @@ int32_t LP_userpass(char *userpass,char *symbol,char *assetname,char *confroot) return(-1); } -uint32_t LP_assetmagic(char *symbol,uint64_t supply) +uint16_t LP_rpcport(char *symbol) { - uint8_t buf[512]; int32_t len = 0; - if ( strcmp(symbol,"KMD") == 0 ) - return(0x8de4eef9); - len = iguana_rwnum(1,&buf[len],sizeof(supply),(void *)&supply); - strcpy((char *)&buf[len],symbol); - len += strlen(symbol); - return(calc_crc32(0,buf,len)); -} - -uint16_t LP_assetport(uint32_t magic) -{ - if ( magic == 0x8de4eef9 ) - return(7771); - else return(8000 + (magic % 7777)); -} - -uint16_t LP_rpcport(char *symbol,uint64_t supply,uint32_t *magicp) -{ - if ( symbol == 0 || symbol[0] == 0 || strcmp("KMD",symbol) == 0 ) - { - *magicp = 0x8de4eef9; - return(7771); - } - else if ( strcmp("BTC",symbol) == 0 ) - return(8332); - else if ( strcmp("LTC",symbol) == 0 ) - return(9332); - *magicp = LP_assetmagic(symbol,supply); - return(LP_assetport(*magicp)+1); + int32_t i; + for (i=0; i Date: Sat, 3 Jun 2017 04:48:12 +0300 Subject: [PATCH 286/339] Test --- iguana/coins/jumblr | 4 + iguana/coins/revs | 3 + iguana/exchanges/LP_commands.c | 178 ++++++++++++++++++++------------ iguana/exchanges/LP_include.h | 4 +- iguana/exchanges/LP_nativeDEX.c | 5 +- iguana/exchanges/LP_utxos.c | 48 +++++++-- iguana/exchanges/mm.c | 2 +- 7 files changed, 164 insertions(+), 80 deletions(-) create mode 100755 iguana/coins/jumblr create mode 100755 iguana/coins/revs diff --git a/iguana/coins/jumblr b/iguana/coins/jumblr new file mode 100755 index 000000000..5b52ce9a6 --- /dev/null +++ b/iguana/coins/jumblr @@ -0,0 +1,4 @@ +~/komodo/src/komodod -ac_name=JUMBLR -ac_supply=999999 -addnode=78.47.196.146 & + +curl --url "http://127.0.0.1:7778" --data "{\"conf\":\"JUMBLR.conf\",\"path\":\"${HOME#"/"}/.komodo/JUMBLR\",\"unitval\":\"20\",\"zcash\":1,\"RELAY\":-1,\"VALIDATE\":0,\"prefetchlag\":-1,\"poll\":100,\"active\":1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":8,\"newcoin\":\"JUMBLR\",\"name\":\"JUMBLR\",\"hasheaders\":1,\"useaddmultisig\":0,\"netmagic\":\"7223759e\",\"p2p\":15105,\"rpc\":15106,\"pubval\":60,\"p2shval\":85,\"wifval\":188,\"txfee_satoshis\":\"10000\",\"isPoS\":0,\"minoutput\":10000,\"minconfirms\":2,\"genesishash\":\"027e3758c3a65b12aa1046462b486d0a63bfa1beae327897f56c5cfb7daaae71\",\"protover\":170002,\"genesisblock\":\"0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000000000000000000000000000000000000000000000000000000000000029ab5f490f0f0f200b00000000000000000000000000000000000000000000000000000000000000fd4005000d5ba7cda5d473947263bf194285317179d2b0d307119c2e7cc4bd8ac456f0774bd52b0cd9249be9d40718b6397a4c7bbd8f2b3272fed2823cd2af4bd1632200ba4bf796727d6347b225f670f292343274cc35099466f5fb5f0cd1c105121b28213d15db2ed7bdba490b4cedc69742a57b7c25af24485e523aadbb77a0144fc76f79ef73bd8530d42b9f3b9bed1c135ad1fe152923fafe98f95f76f1615e64c4abb1137f4c31b218ba2782bc15534788dda2cc08a0ee2987c8b27ff41bd4e31cd5fb5643dfe862c9a02ca9f90c8c51a6671d681d04ad47e4b53b1518d4befafefe8cadfb912f3d03051b1efbf1dfe37b56e93a741d8dfd80d576ca250bee55fab1311fc7b3255977558cdda6f7d6f875306e43a14413facdaed2f46093e0ef1e8f8a963e1632dcbeebd8e49fd16b57d49b08f9762de89157c65233f60c8e38a1f503a48c555f8ec45dedecd574a37601323c27be597b956343107f8bd80f3a925afaf30811df83c402116bb9c1e5231c70fff899a7c82f73c902ba54da53cc459b7bf1113db65cc8f6914d3618560ea69abd13658fa7b6af92d374d6eca9529f8bd565166e4fcbf2a8dfb3c9b69539d4d2ee2e9321b85b331925df195915f2757637c2805e1d4131e1ad9ef9bc1bb1c732d8dba4738716d351ab30c996c8657bab39567ee3b29c6d054b711495c0d52e1cd5d8e55b4f0f0325b97369280755b46a02afd54be4ddd9f77c22272b8bbb17ff5118fedbae2564524e797bd28b5f74f7079d532ccc059807989f94d267f47e724b3f1ecfe00ec9e6541c961080d8891251b84b4480bc292f6a180bea089fef5bbda56e1e41390d7c0e85ba0ef530f7177413481a226465a36ef6afe1e2bca69d2078712b3912bba1a99b1fbff0d355d6ffe726d2bb6fbc103c4ac5756e5bee6e47e17424ebcbf1b63d8cb90ce2e40198b4f4198689daea254307e52a25562f4c1455340f0ffeb10f9d8e914775e37d0edca019fb1b9c6ef81255ed86bc51c5391e0591480f66e2d88c5f4fd7277697968656a9b113ab97f874fdd5f2465e5559533e01ba13ef4a8f7a21d02c30c8ded68e8c54603ab9c8084ef6d9eb4e92c75b078539e2ae786ebab6dab73a09e0aa9ac575bcefb29e930ae656e58bcb513f7e3c17e079dce4f05b5dbc18c2a872b22509740ebe6a3903e00ad1abc55076441862643f93606e3dc35e8d9f2caef3ee6be14d513b2e062b21d0061de3bd56881713a1a5c17f5ace05e1ec09da53f99442df175a49bd154aa96e4949decd52fed79ccf7ccbce32941419c314e374e4a396ac553e17b5340336a1a25c22f9e42a243ba5404450b650acfc826a6e432971ace776e15719515e1634ceb9a4a35061b668c74998d3dfb5827f6238ec015377e6f9c94f38108768cf6e5c8b132e0303fb5a200368f845ad9d46343035a6ff94031df8d8309415bb3f6cd5ede9c135fdabcc030599858d803c0f85be7661c88984d88faa3d26fb0e9aac0056a53f1b5d0baed713c853c4a2726869a0a124a8a5bbc0fc0ef80c8ae4cb53636aa02503b86a1eb9836fcc259823e2692d921d88e1ffc1e6cb2bde43939ceb3f32a611686f539f8f7c9f0bf00381f743607d40960f06d347d1cd8ac8a51969c25e37150efdf7aa4c2037a2fd0516fb444525ab157a0ed0a7412b2fa69b217fe397263153782c0f64351fbdf2678fa0dc8569912dcd8e3ccad38f34f23bbbce14c6a26ac24911b308b82c7e43062d180baeac4ba7153858365c72c63dcf5f6a5b08070b730adb017aeae925b7d0439979e2679f45ed2f25a7edcfd2fb77a8794630285ccb0a071f5cce410b46dbf9750b0354aae8b65574501cc69efb5b6a43444074fee116641bb29da56c2b4a7f456991fc92b2\",\"debug\":0,\"seedipaddr\":\"78.47.196.146\"}" + diff --git a/iguana/coins/revs b/iguana/coins/revs new file mode 100755 index 000000000..f2cf9ef72 --- /dev/null +++ b/iguana/coins/revs @@ -0,0 +1,3 @@ +./komodod -ac_name=REVS -ac_supply=1300000 -addnode=78.47.196.146 & +curl --url "http://127.0.0.1:7778" --data "{\"conf\":\"REVS.conf\",\"path\":\"${HOME#"/"}/.komodo/REVS\",\"unitval\":\"20\",\"zcash\":1,\"RELAY\":-1,\"VALIDATE\":0,\"prefetchlag\":-1,\"poll\":100,\"active\":1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":8,\"newcoin\":\"REVS\",\"name\":\"REVS\",\"hasheaders\":1,\"useaddmultisig\":0,\"netmagic\":\"905c3498\",\"p2p\":10195,\"rpc\":10196,\"pubval\":60,\"p2shval\":85,\"wifval\":188,\"txfee_satoshis\":\"10000\",\"isPoS\":0,\"minoutput\":10000,\"minconfirms\":2,\"genesishash\":\"027e3758c3a65b12aa1046462b486d0a63bfa1beae327897f56c5cfb7daaae71\",\"protover\":170002,\"genesisblock\":\"0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000000000000000000000000000000000000000000000000000000000000029ab5f490f0f0f200b00000000000000000000000000000000000000000000000000000000000000fd4005000d5ba7cda5d473947263bf194285317179d2b0d307119c2e7cc4bd8ac456f0774bd52b0cd9249be9d40718b6397a4c7bbd8f2b3272fed2823cd2af4bd1632200ba4bf796727d6347b225f670f292343274cc35099466f5fb5f0cd1c105121b28213d15db2ed7bdba490b4cedc69742a57b7c25af24485e523aadbb77a0144fc76f79ef73bd8530d42b9f3b9bed1c135ad1fe152923fafe98f95f76f1615e64c4abb1137f4c31b218ba2782bc15534788dda2cc08a0ee2987c8b27ff41bd4e31cd5fb5643dfe862c9a02ca9f90c8c51a6671d681d04ad47e4b53b1518d4befafefe8cadfb912f3d03051b1efbf1dfe37b56e93a741d8dfd80d576ca250bee55fab1311fc7b3255977558cdda6f7d6f875306e43a14413facdaed2f46093e0ef1e8f8a963e1632dcbeebd8e49fd16b57d49b08f9762de89157c65233f60c8e38a1f503a48c555f8ec45dedecd574a37601323c27be597b956343107f8bd80f3a925afaf30811df83c402116bb9c1e5231c70fff899a7c82f73c902ba54da53cc459b7bf1113db65cc8f6914d3618560ea69abd13658fa7b6af92d374d6eca9529f8bd565166e4fcbf2a8dfb3c9b69539d4d2ee2e9321b85b331925df195915f2757637c2805e1d4131e1ad9ef9bc1bb1c732d8dba4738716d351ab30c996c8657bab39567ee3b29c6d054b711495c0d52e1cd5d8e55b4f0f0325b97369280755b46a02afd54be4ddd9f77c22272b8bbb17ff5118fedbae2564524e797bd28b5f74f7079d532ccc059807989f94d267f47e724b3f1ecfe00ec9e6541c961080d8891251b84b4480bc292f6a180bea089fef5bbda56e1e41390d7c0e85ba0ef530f7177413481a226465a36ef6afe1e2bca69d2078712b3912bba1a99b1fbff0d355d6ffe726d2bb6fbc103c4ac5756e5bee6e47e17424ebcbf1b63d8cb90ce2e40198b4f4198689daea254307e52a25562f4c1455340f0ffeb10f9d8e914775e37d0edca019fb1b9c6ef81255ed86bc51c5391e0591480f66e2d88c5f4fd7277697968656a9b113ab97f874fdd5f2465e5559533e01ba13ef4a8f7a21d02c30c8ded68e8c54603ab9c8084ef6d9eb4e92c75b078539e2ae786ebab6dab73a09e0aa9ac575bcefb29e930ae656e58bcb513f7e3c17e079dce4f05b5dbc18c2a872b22509740ebe6a3903e00ad1abc55076441862643f93606e3dc35e8d9f2caef3ee6be14d513b2e062b21d0061de3bd56881713a1a5c17f5ace05e1ec09da53f99442df175a49bd154aa96e4949decd52fed79ccf7ccbce32941419c314e374e4a396ac553e17b5340336a1a25c22f9e42a243ba5404450b650acfc826a6e432971ace776e15719515e1634ceb9a4a35061b668c74998d3dfb5827f6238ec015377e6f9c94f38108768cf6e5c8b132e0303fb5a200368f845ad9d46343035a6ff94031df8d8309415bb3f6cd5ede9c135fdabcc030599858d803c0f85be7661c88984d88faa3d26fb0e9aac0056a53f1b5d0baed713c853c4a2726869a0a124a8a5bbc0fc0ef80c8ae4cb53636aa02503b86a1eb9836fcc259823e2692d921d88e1ffc1e6cb2bde43939ceb3f32a611686f539f8f7c9f0bf00381f743607d40960f06d347d1cd8ac8a51969c25e37150efdf7aa4c2037a2fd0516fb444525ab157a0ed0a7412b2fa69b217fe397263153782c0f64351fbdf2678fa0dc8569912dcd8e3ccad38f34f23bbbce14c6a26ac24911b308b82c7e43062d180baeac4ba7153858365c72c63dcf5f6a5b08070b730adb017aeae925b7d0439979e2679f45ed2f25a7edcfd2fb77a8794630285ccb0a071f5cce410b46dbf9750b0354aae8b65574501cc69efb5b6a43444074fee116641bb29da56c2b4a7f456991fc92b2\",\"debug\":0,\"seedipaddr\":\"78.47.196.146\"}" + diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 00ca359b9..32ee01428 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -277,11 +277,13 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) return(retarray); } -cJSON *LP_bestprice(struct LP_utxoinfo *myutxo,char *base) +cJSON *LP_autotrade(struct LP_utxoinfo *myutxo,char *base,double maxprice) { static bits256 zero; int32_t i,n,besti,DEXselector=0; cJSON *array,*item,*bestitem=0; struct basilisk_request R; double bestmetric,metric,bestprice=0.,price,prices[100]; struct LP_quoteinfo Q[sizeof(prices)/sizeof(*prices)]; bestprice = 0.; + if ( maxprice == 0. ) + maxprice = LP_price(base,myutxo->coin) / 0.975; if ( (array= LP_tradecandidates(myutxo,base)) != 0 ) { if ( (n= cJSON_GetArraySize(array)) > 0 ) @@ -329,33 +331,41 @@ cJSON *LP_bestprice(struct LP_utxoinfo *myutxo,char *base) if ( besti >= 0 )//&& bits256_cmp(myutxo->mypub,otherpubs[besti]) == 0 ) { item = jitem(array,besti); - bestitem = LP_quotejson(&Q[i]); i = besti; - Q[i].desttxid = myutxo->txid; - Q[i].destvout = myutxo->vout; - Q[i].feetxid = myutxo->txid2; - Q[i].feevout = myutxo->vout2; - strcpy(Q[i].destaddr,myutxo->coinaddr); - price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); - if ( jobj(bestitem,"price") != 0 ) - jdelete(bestitem,"price"); - jaddnum(bestitem,"price",prices[besti]); - if ( LP_price(base,myutxo->coin) > 0.975*price ) + bestitem = LP_quotejson(&Q[i]); + if ( bestprice <= maxprice ) { Q[i].desttxid = myutxo->txid; Q[i].destvout = myutxo->vout; Q[i].feetxid = myutxo->txid2; Q[i].feevout = myutxo->vout2; strcpy(Q[i].destaddr,myutxo->coinaddr); - price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); - LP_requestinit(&R,Q[i].srchash,Q[i].desthash,base,Q[i].satoshis,Q[i].destcoin,Q[i].destsatoshis,Q[i].timestamp,Q[i].quotetime,DEXselector); - printf("Alice r.%u q.%u\n",R.requestid,R.quoteid); + price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); + if ( jobj(bestitem,"price") != 0 ) + jdelete(bestitem,"price"); + jaddnum(bestitem,"price",prices[besti]); + if ( price <= maxprice ) + { + Q[i].desttxid = myutxo->txid; + Q[i].destvout = myutxo->vout; + Q[i].feetxid = myutxo->txid2; + Q[i].feevout = myutxo->vout2; + strcpy(Q[i].destaddr,myutxo->coinaddr); + price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); + LP_requestinit(&R,Q[i].srchash,Q[i].desthash,base,Q[i].satoshis,Q[i].destcoin,Q[i].destsatoshis,Q[i].timestamp,Q[i].quotetime,DEXselector); + jaddstr(bestitem,"status","connected"); + jaddnum(bestitem,"requestid",R.requestid); + jaddnum(bestitem,"quoteid",R.quoteid); + printf("Alice r.%u q.%u\n",R.requestid,R.quoteid); + } else jaddstr(bestitem,"status","too expensive"); } } } free_json(array); } } + if ( bestitem == 0 ) + return(cJSON_Parse("{\"error\":\"no match found\"}")); return(bestitem); } @@ -488,72 +498,104 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port { - char *method,*ipaddr,*coin,*retstr = 0; uint16_t argport,pushport,subport; int32_t amclient,otherpeers,othernumutxos; struct LP_peerinfo *peer; cJSON *retjson; + char *method,*ipaddr,*userpass,*coin,*retstr = 0; uint16_t argport,pushport,subport; int32_t amclient,otherpeers,othernumutxos; struct LP_peerinfo *peer; cJSON *retjson; if ( (method= jstr(argjson,"method")) == 0 ) return(clonestr("{\"error\":\"need method in request\"}")); - else + if ( IAMCLIENT != 0 && USERPASS[0] != 0 && strcmp(remoteaddr,"127.0.0.1") == 0 && port != 0 ) { - amclient = (LP_mypeer == 0); - if ( (ipaddr= jstr(argjson,"ipaddr")) != 0 && (argport= juint(argjson,"port")) != 0 ) + if ( (userpass= jstr(argjson,"userpass")) == 0 || strcmp(userpass,USERPASS) != 0 ) + return(clonestr("{\"error\":\"authentication error\"}")); + if ( USERPASS_COUNTER == 0 ) { - if ( strcmp(ipaddr,"127.0.0.1") != 0 && port >= 1000 ) - { - if ( (pushport= juint(argjson,"push")) == 0 ) - pushport = argport + 1; - if ( (subport= juint(argjson,"sub")) == 0 ) - subport = argport + 2; - if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),argport)) != 0 ) - { - if ( (otherpeers= jint(argjson,"numpeers")) > peer->numpeers ) - peer->numpeers = otherpeers; - if ( (othernumutxos= jint(argjson,"numutxos")) > peer->numutxos ) - { - printf("change.(%s) numutxos.%d -> %d mynumutxos.%d\n",peer->ipaddr,peer->numutxos,othernumutxos,LP_mypeer != 0 ? LP_mypeer->numutxos:0); - peer->numutxos = othernumutxos; - } - //printf("peer.(%s) found (%d %d) (%d %d) (%s)\n",peer->ipaddr,peer->numpeers,peer->numutxos,otherpeers,othernumutxos,jprint(argjson,0)); - } else LP_addpeer(amclient,LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos")); - } + USERPASS_COUNTER = 1; + retjson = cJSON_CreateObject(); + jaddstr(retjson,"userpass",USERPASS); + return(jprint(retjson,1)); } - printf("CMD.(%s)\n",jprint(argjson,0)); - if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) - retstr = LP_quotereceived(argjson); - else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 ) + if ( (coin= jstr(argjson,"coin")) != 0 ) { - int32_t pairsock = -1; char *pairstr; - if ( (pairstr= jstr(argjson,"pair")) == 0 || (pairsock= nn_socket(AF_SP,NN_PAIR)) < 0 ) - printf("error creating pairsock\n"); - else if ( nn_connect(pairsock,pairstr) >= 0 ) + if ( strcmp(method,"inventory") == 0 ) + { + LP_privkey_init(0,-1,coin,0,USERPASS_WIFSTR,1); + return(LP_inventory(coin)); + } + else if ( strcmp(method,"candidates") == 0 || strcmp(method,"autotrade") == 0 ) { - struct LP_quoteinfo *qp; int32_t DEXselector = 0; - qp = calloc(1,sizeof(*qp)); - LP_quoteparse(qp,argjson); - qp->pair = pairsock; - qp->privkey = LP_privkey(qp->destaddr); - LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis,qp->destcoin,qp->destsatoshis,qp->timestamp,qp->quotetime,DEXselector); - printf("alice pairstr.(%s)\n",pairstr); - if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_aliceloop,(void *)qp) == 0 ) + bits256 txid; int32_t vout; struct LP_utxoinfo *utxo; + txid = jbits256(argjson,"txid"); + if ( bits256_nonz(txid) == 0 ) + return(clonestr("{\"error\":\"invalid or missing txid\"}")); + if ( jobj(argjson,"vout") == 0 ) + return(clonestr("{\"error\":\"missing vout\"}")); + vout = jint(argjson,"vout"); + if ( (utxo= LP_utxofind(txid,vout)) == 0 ) + return(clonestr("{\"error\":\"txid/vout not found\"}")); + if ( strcmp(method,"candidates") == 0 ) + return(jprint(LP_tradecandidates(utxo,coin),1)); + else return(jprint(LP_autotrade(utxo,coin,jdouble(argjson,"maxprice")),1)); + } + } else return(clonestr("{\"error\":\"no coin specified\"}")); + } + amclient = (LP_mypeer == 0); + if ( (ipaddr= jstr(argjson,"ipaddr")) != 0 && (argport= juint(argjson,"port")) != 0 ) + { + if ( strcmp(ipaddr,"127.0.0.1") != 0 && port >= 1000 ) + { + if ( (pushport= juint(argjson,"push")) == 0 ) + pushport = argport + 1; + if ( (subport= juint(argjson,"sub")) == 0 ) + subport = argport + 2; + if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),argport)) != 0 ) + { + if ( (otherpeers= jint(argjson,"numpeers")) > peer->numpeers ) + peer->numpeers = otherpeers; + if ( (othernumutxos= jint(argjson,"numutxos")) > peer->numutxos ) { + printf("change.(%s) numutxos.%d -> %d mynumutxos.%d\n",peer->ipaddr,peer->numutxos,othernumutxos,LP_mypeer != 0 ? LP_mypeer->numutxos:0); + peer->numutxos = othernumutxos; } - } + //printf("peer.(%s) found (%d %d) (%d %d) (%s)\n",peer->ipaddr,peer->numpeers,peer->numutxos,otherpeers,othernumutxos,jprint(argjson,0)); + } else LP_addpeer(amclient,LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos")); } - else if ( IAMCLIENT == 0 && strcmp(method,"getprice") == 0 ) - retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel")); - else if ( strcmp(method,"orderbook") == 0 ) - retstr = LP_orderbook(jstr(argjson,"base"),jstr(argjson,"rel")); - else if ( IAMCLIENT == 0 && strcmp(method,"getpeers") == 0 ) - retstr = LP_peers(); - else if ( IAMCLIENT == 0 && strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) - retstr = LP_utxos(LP_mypeer,coin,jint(argjson,"lastn")); - else if ( IAMCLIENT == 0 && strcmp(method,"notify") == 0 ) - retstr = clonestr("{\"result\":\"success\",\"notify\":\"received\"}"); - else if ( IAMCLIENT == 0 && strcmp(method,"notifyutxo") == 0 ) + } + printf("CMD.(%s)\n",jprint(argjson,0)); + if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) + retstr = LP_quotereceived(argjson); + else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 ) + { + int32_t pairsock = -1; char *pairstr; + if ( (pairstr= jstr(argjson,"pair")) == 0 || (pairsock= nn_socket(AF_SP,NN_PAIR)) < 0 ) + printf("error creating pairsock\n"); + else if ( nn_connect(pairsock,pairstr) >= 0 ) { - printf("utxonotify.(%s)\n",jprint(argjson,0)); - LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"txid2"),jint(argjson,"vout2"),SATOSHIDEN * jdouble(argjson,"value2"),jstr(argjson,"script"),jstr(argjson,"address"),jstr(argjson,"ipaddr"),juint(argjson,"port"),jdouble(argjson,"profit")); - retstr = clonestr("{\"result\":\"success\",\"notifyutxo\":\"received\"}"); + struct LP_quoteinfo *qp; int32_t DEXselector = 0; + qp = calloc(1,sizeof(*qp)); + LP_quoteparse(qp,argjson); + qp->pair = pairsock; + qp->privkey = LP_privkey(qp->destaddr); + LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis,qp->destcoin,qp->destsatoshis,qp->timestamp,qp->quotetime,DEXselector); + printf("alice pairstr.(%s)\n",pairstr); + if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_aliceloop,(void *)qp) == 0 ) + { + } } } + else if ( IAMCLIENT == 0 && strcmp(method,"getprice") == 0 ) + retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel")); + else if ( strcmp(method,"orderbook") == 0 ) + retstr = LP_orderbook(jstr(argjson,"base"),jstr(argjson,"rel")); + else if ( IAMCLIENT == 0 && strcmp(method,"getpeers") == 0 ) + retstr = LP_peers(); + else if ( IAMCLIENT == 0 && strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) + retstr = LP_utxos(LP_mypeer,coin,jint(argjson,"lastn")); + else if ( IAMCLIENT == 0 && strcmp(method,"notify") == 0 ) + retstr = clonestr("{\"result\":\"success\",\"notify\":\"received\"}"); + else if ( IAMCLIENT == 0 && strcmp(method,"notifyutxo") == 0 ) + { + printf("utxonotify.(%s)\n",jprint(argjson,0)); + LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"txid2"),jint(argjson,"vout2"),SATOSHIDEN * jdouble(argjson,"value2"),jstr(argjson,"script"),jstr(argjson,"address"),jstr(argjson,"ipaddr"),juint(argjson,"port"),jdouble(argjson,"profit")); + retstr = clonestr("{\"result\":\"success\",\"notifyutxo\":\"received\"}"); + } if ( retstr != 0 ) return(retstr); retjson = cJSON_CreateObject(); diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 4cfacff3f..9dc21ab9b 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -42,8 +42,8 @@ #define DEX_SLEEP 3 #define BASILISK_KEYSIZE ((int32_t)(2*sizeof(bits256)+sizeof(uint32_t)*2)) -extern char GLOBAL_DBDIR[]; -extern int32_t IAMCLIENT; +extern char GLOBAL_DBDIR[],USERPASS[],USERPASS_WIFSTR[]; +extern int32_t IAMCLIENT,USERPASS_COUNTER; struct iguana_msgvin { bits256 prev_hash; uint8_t *vinscript,*userdata,*spendscript,*redeemscript; uint32_t prev_vout,sequence; uint16_t scriptlen,p2shlen,userdatalen,spendlen; }; diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 68915cd7a..cfa6d1e09 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -28,13 +28,14 @@ struct LP_peerinfo *LP_peerinfos,*LP_mypeer; char *activecoins[] = { "BTC", "KMD", "REVS", "JUMBLR" };//"LTC", "USD", }; char GLOBAL_DBDIR[] = "DB"; +char USERPASS[65],USERPASS_WIFSTR[64]; char *default_LPnodes[] = { "5.9.253.196", "5.9.253.197", "5.9.253.198", "5.9.253.199", "5.9.253.200", "5.9.253.201", "5.9.253.202", "5.9.253.203", "5.9.253.204" }; //"5.9.253.195", portable_mutex_t LP_peermutex,LP_utxomutex,LP_commandmutex,LP_cachemutex; int32_t LP_mypubsock = -1; int32_t Client_connections; -int32_t IAMCLIENT = 0; +int32_t USERPASS_COUNTER,IAMCLIENT = 0; // stubs @@ -282,3 +283,5 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profit } LP_mainloop(mypeer,mypubport,pubsock,pullsock,myport,amclient,passphrase,profitmargin); } + + diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index b620c65c5..6ba0a819b 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -29,22 +29,36 @@ struct LP_utxoinfo *LP_utxofind(bits256 txid,int32_t vout) return(utxo); } -cJSON *LP_utxojson(struct LP_utxoinfo *utxo) +cJSON *LP_inventoryjson(cJSON *item,struct LP_utxoinfo *utxo) { - cJSON *item = cJSON_CreateObject(); - jaddstr(item,"ipaddr",utxo->ipaddr); - jaddnum(item,"port",utxo->port); - jaddnum(item,"profit",utxo->profitmargin); - jaddstr(item,"base",utxo->coin); jaddstr(item,"coin",utxo->coin); jaddstr(item,"address",utxo->coinaddr); - jaddstr(item,"script",utxo->spendscript); jaddbits256(item,"txid",utxo->txid); jaddnum(item,"vout",utxo->vout); jaddnum(item,"value",dstr(utxo->satoshis)); jaddbits256(item,"txid2",utxo->txid2); jaddnum(item,"vout2",utxo->vout2); jaddnum(item,"value2",dstr(utxo->satoshis2)); + if ( utxo->swappending != 0 ) + jaddnum(item,"pending",utxo->swappending); + if ( bits256_nonz(utxo->otherpubkey) != 0 ) + jaddbits256(item,"desthash",utxo->otherpubkey); + if ( utxo->pair >= 0 ) + jaddnum(item,"socket",utxo->pair); + if ( utxo->swap != 0 ) + jaddstr(item,"swap","in progress"); + return(item); +} + +cJSON *LP_utxojson(struct LP_utxoinfo *utxo) +{ + cJSON *item = cJSON_CreateObject(); + item = LP_inventoryjson(item,utxo); + jaddstr(item,"ipaddr",utxo->ipaddr); + jaddnum(item,"port",utxo->port); + jaddnum(item,"profit",utxo->profitmargin); + jaddstr(item,"base",utxo->coin); + jaddstr(item,"script",utxo->spendscript); return(item); } @@ -207,6 +221,17 @@ void LP_utxosquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock peer->errors++; } +char *LP_inventory(char *symbol) +{ + struct LP_utxoinfo *utxo,*tmp; cJSON *array = cJSON_CreateArray(); + HASH_ITER(hh,LP_utxoinfos,utxo,tmp) + { + if ( strcmp(symbol,utxo->coin) == 0 ) + jaddi(array,LP_inventoryjson(cJSON_CreateObject(),utxo)); + } + return(jprint(array,1)); +} + int32_t LP_maxvalue(uint64_t *values,int32_t n) { int32_t i,maxi = -1; uint64_t maxval = 0; @@ -238,7 +263,8 @@ int32_t LP_nearestvalue(uint64_t *values,int32_t n,uint64_t targetval) uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symbol,char *passphrase,char *wifstr,int32_t amclient) { - char coinaddr[64],*script; struct LP_utxoinfo *utxo; cJSON *array,*item,*retjson; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; struct iguana_info *coin = LP_coinfind(symbol); + static uint32_t counter; + char coinaddr[64],*script; struct LP_utxoinfo *utxo; cJSON *array,*item,*retjson; bits256 userpass,userpub,txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33],tmptype,rmd160[20]; struct iguana_info *coin = LP_coinfind(symbol); if ( coin == 0 ) { printf("cant add privkey for %s, coin not active\n",symbol); @@ -248,9 +274,15 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb conv_NXTpassword(privkey.bytes,pubkey.bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); else privkey = iguana_wif2privkey(wifstr); iguana_priv2pub(pubkey33,coinaddr,privkey,coin->pubtype); + if ( counter == 0 ) { char tmpstr[128]; + counter++; + bitcoin_priv2wif(USERPASS_WIFSTR,privkey,188); bitcoin_priv2wif(tmpstr,privkey,coin->wiftype); + conv_NXTpassword(userpass.bytes,pubkey.bytes,(uint8_t *)tmpstr,(int32_t)strlen(tmpstr)); + userpub = curve25519(userpass,curve25519_basepoint9()); + printf("userpass.(%s)\n",bits256_str(USERPASS,userpub)); printf("%s (%s) %d wif.(%s) (%s)\n",symbol,coinaddr,coin->pubtype,tmpstr,passphrase); if ( (retjson= LP_importprivkey(coin->symbol,tmpstr,coinaddr,-1)) != 0 ) printf("importprivkey -> (%s)\n",jprint(retjson,1)); diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 89ce9c3cd..6cfc332cf 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -846,7 +846,7 @@ int main(int argc, const char * argv[]) struct LP_utxoinfo *utxo,*utmp; HASH_ITER(hh,LP_utxoinfos,utxo,utmp) { - if ( (matchjson= LP_bestprice(utxo,"KMD")) != 0 ) + if ( (matchjson= LP_autotrade(utxo,"KMD",0.)) != 0 ) printf("bestprice (%s)\n",jprint(matchjson,1)); } } From 78d5ee5025ae12e92338b7bacac022fc62f750a0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 05:09:42 +0300 Subject: [PATCH 287/339] Test --- iguana/exchanges/LP_utxos.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 6ba0a819b..902942824 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -129,14 +129,14 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 portable_mutex_unlock(&LP_utxomutex); if ( mypubsock >= 0 ) LP_send(mypubsock,jprint(LP_utxojson(utxo),1),1); - //char str[65],str2[65]; printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d %s %s\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0,bits256_str(str,utxo->txid),bits256_str(str2,txid)); + char str[65],str2[65]; printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d %s %s\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0,bits256_str(str,utxo->txid),bits256_str(str2,txid)); } return(utxo); } int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *retstr,uint32_t now) { - struct LP_peerinfo *peer,*destpeer; uint32_t argipbits; char *argipaddr; uint16_t argport,pushport,subport; cJSON *array,*item; int32_t i,n=0; bits256 txid; struct LP_utxoinfo *utxo; + struct LP_peerinfo *peer; uint32_t argipbits; char *argipaddr; uint16_t argport,pushport,subport; cJSON *array,*item; int32_t i,n=0; bits256 txid; struct LP_utxoinfo *utxo; if ( amclient != 0 ) { printf("LP_utxosparse not for clientside\n"); @@ -167,14 +167,14 @@ int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs } } } - if ( (destpeer= LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport)) != 0 ) + /*if ( (destpeer= LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport)) != 0 ) { if ( destpeer->numutxos < n ) { //destpeer->numutxos = n; //printf("got.(%s) from %s numutxos.%d\n",retstr,destpeer->ipaddr,destpeer->numutxos); } - } + }*/ } free_json(array); } From a2b4ddd02ce7fc0312c5f53a7d3e6e6b5b65b5fa Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 05:11:58 +0300 Subject: [PATCH 288/339] Test --- iguana/exchanges/LP_utxos.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 902942824..a8e7f48be 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -89,7 +89,7 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 printf("malformed addutxo %d %d %d %d %d %d %d %d %d\n", coin == 0,spendscript == 0,coinaddr == 0,bits256_nonz(txid) == 0,bits256_nonz(deposittxid) == 0,vout < 0,depositvout < 0,satoshis <= 0,depositsatoshis <= 0); return(0); } - if ( amclient == 0 && strcmp(ipaddr,"127.0.0.1") == 0 ) + if ( IAMCLIENT == 0 && strcmp(ipaddr,"127.0.0.1") == 0 ) { printf("LP node got localhost utxo\n"); return(0); @@ -129,7 +129,7 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 portable_mutex_unlock(&LP_utxomutex); if ( mypubsock >= 0 ) LP_send(mypubsock,jprint(LP_utxojson(utxo),1),1); - char str[65],str2[65]; printf("%s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d %s %s\n",ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0,bits256_str(str,utxo->txid),bits256_str(str2,txid)); + char str[65],str2[65]; printf("amclient.%d %s:%u %s LP_addutxo.(%.8f %.8f) numutxos.%d %s %s\n",IAMCLIENT,ipaddr,port,utxo->coin,dstr(satoshis),dstr(depositsatoshis),mypeer!=0?mypeer->numutxos:0,bits256_str(str,utxo->txid),bits256_str(str2,txid)); } return(utxo); } From 76ce1d0944e109c0ccbc07affda7cfd353936564 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 06:02:04 +0300 Subject: [PATCH 289/339] Test --- iguana/exchanges/LP_commands.c | 2 +- iguana/exchanges/LP_network.c | 2 +- iguana/exchanges/mm.c | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 32ee01428..dd5044682 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -393,7 +393,7 @@ int32_t LP_priceping(int32_t pubsock,struct LP_utxoinfo *utxo,char *rel,double p int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) { char *method,*base,*rel,*retstr,pairstr[512]; cJSON *retjson; double price; bits256 privkey,txid; struct LP_utxoinfo *utxo; int32_t retval = -1,DEXselector = 0; uint64_t destvalue; struct basilisk_request R; struct LP_quoteinfo Q; - if ( (method= jstr(argjson,"method")) != 0 ) + if ( IAMCLIENT == 0 && (method= jstr(argjson,"method")) != 0 ) { txid = jbits256(argjson,"txid"); if ( (utxo= LP_utxofind(txid,jint(argjson,"vout"))) != 0 && strcmp(utxo->ipaddr,mypeer->ipaddr) == 0 && utxo->port == mypeer->port && (base= jstr(argjson,"base")) != 0 && (rel= jstr(argjson,"rel")) != 0 && strcmp(base,utxo->coin) == 0 ) diff --git a/iguana/exchanges/LP_network.c b/iguana/exchanges/LP_network.c index 84a7cb512..ff2e35c11 100644 --- a/iguana/exchanges/LP_network.c +++ b/iguana/exchanges/LP_network.c @@ -29,7 +29,7 @@ int32_t LP_send(int32_t sock,char *msg,int32_t freeflag) int32_t sentbytes,len,i; struct nn_pollfd pfd; if ( sock < 0 ) { - printf("LP_send to illegal socket\n"); + printf("LP_send.(%s) to illegal socket\n",msg); if ( freeflag != 0 ) free(msg); return(-1); diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 6cfc332cf..bf3017da3 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -836,9 +836,9 @@ int main(int argc, const char * argv[]) free(retstr); } incr = 100.; - while ( 1 ) + while ( IAMCLIENT != 0 ) { - theoretical = marketmaker_updateprice("komodo","KMD","BTC",theoretical,&incr); + theoretical = marketmaker_updateprice("komodo","REVS","KMD",theoretical,&incr); sleep(3); LP_privkey_updates(0,-1,passphrase,1); if ( jint(retjson,"client") != 0 ) @@ -846,12 +846,13 @@ int main(int argc, const char * argv[]) struct LP_utxoinfo *utxo,*utmp; HASH_ITER(hh,LP_utxoinfos,utxo,utmp) { - if ( (matchjson= LP_autotrade(utxo,"KMD",0.)) != 0 ) + if ( strcmp(utxo->coin,"REVS") == 0 && (matchjson= LP_autotrade(utxo,"KMD",0.)) != 0 ) printf("bestprice (%s)\n",jprint(matchjson,1)); } } sleep(1000); } +getchar(); profitmargin = jdouble(retjson,"profitmargin"); minask = jdouble(retjson,"minask"); maxbid = jdouble(retjson,"maxbid"); From e856ee55a499d3aab401e07aca8110f83a09b6be Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 07:43:58 +0300 Subject: [PATCH 290/339] Test --- crypto777/OS_portable.h | 1 + iguana/exchanges/LP_commands.c | 30 +++++- iguana/exchanges/LP_nativeDEX.c | 3 + iguana/exchanges/LP_prices.c | 178 ++++++++++++++++++++++++++++---- iguana/exchanges/LP_utxos.c | 1 - 5 files changed, 187 insertions(+), 26 deletions(-) diff --git a/crypto777/OS_portable.h b/crypto777/OS_portable.h index a90889279..23a051b01 100755 --- a/crypto777/OS_portable.h +++ b/crypto777/OS_portable.h @@ -359,6 +359,7 @@ void calc_base64_decodestr(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); void calc_hexstr(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); void calc_unhexstr(char *hexstr,uint8_t *buf,uint8_t *msg,int32_t len); int32_t safecopy(char *dest,char *src,long len); +double dxblend(double *destp,double val,double decay); uint64_t calc_ipbits(char *ip_port); void expand_ipbits(char *ipaddr,uint64_t ipbits); diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index dd5044682..4be342e24 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -498,10 +498,10 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port { - char *method,*ipaddr,*userpass,*coin,*retstr = 0; uint16_t argport,pushport,subport; int32_t amclient,otherpeers,othernumutxos; struct LP_peerinfo *peer; cJSON *retjson; + char *method,*ipaddr,*userpass,*base,*rel,*coin,*retstr = 0; uint16_t argport,pushport,subport; int32_t amclient,otherpeers,othernumutxos; struct LP_peerinfo *peer; cJSON *retjson; if ( (method= jstr(argjson,"method")) == 0 ) return(clonestr("{\"error\":\"need method in request\"}")); - if ( IAMCLIENT != 0 && USERPASS[0] != 0 && strcmp(remoteaddr,"127.0.0.1") == 0 && port != 0 ) + if ( USERPASS[0] != 0 && strcmp(remoteaddr,"127.0.0.1") == 0 && port != 0 ) { if ( (userpass= jstr(argjson,"userpass")) == 0 || strcmp(userpass,USERPASS) != 0 ) return(clonestr("{\"error\":\"authentication error\"}")); @@ -512,7 +512,29 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port jaddstr(retjson,"userpass",USERPASS); return(jprint(retjson,1)); } - if ( (coin= jstr(argjson,"coin")) != 0 ) + if ( (base= jstr(argjson,"base")) != 0 && (rel= jstr(argjson,"rel")) != 0 ) + { + if ( strcmp(method,"setprice") == 0 ) + { + if ( LP_mypriceset(base,rel,jdouble(argjson,"price")) < 0 ) + return(clonestr("{\"error\":\"couldnt set price\"}")); + else return(clonestr("{\"result\":\"success\"}")); + } + else if ( strcmp(method,"myprice") == 0 ) + { + double bid,ask; + if ( LP_myprice(&bid,&ask,base,rel) != 0. ) + { + retjson = cJSON_CreateObject(); + jaddstr(retjson,"base",base); + jaddstr(retjson,"rel",rel); + jaddnum(retjson,"bid",bid); + jaddnum(retjson,"ask",ask); + return(jprint(retjson,1)); + } else return(clonestr("{\"error\":\"no price set\"}")); + } + } + else if ( IAMCLIENT != 0 && (coin= jstr(argjson,"coin")) != 0 ) { if ( strcmp(method,"inventory") == 0 ) { @@ -534,7 +556,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port return(jprint(LP_tradecandidates(utxo,coin),1)); else return(jprint(LP_autotrade(utxo,coin,jdouble(argjson,"maxprice")),1)); } - } else return(clonestr("{\"error\":\"no coin specified\"}")); + } } amclient = (LP_mypeer == 0); if ( (ipaddr= jstr(argjson,"ipaddr")) != 0 && (argport= juint(argjson,"port")) != 0 ) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index cfa6d1e09..450d9bad3 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -107,7 +107,10 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i exit(-1); } for (i=0; i 0 ) + { + coinbits = stringbits(symbol); + pp = LP_priceinfos; + for (i=0; icoinbits == coinbits ) + return(pp); + } + return(0); +} + +struct LP_priceinfo *LP_priceinfoptr(int32_t *indp,char *base,char *rel) +{ + struct LP_priceinfo *basepp,*relpp; + if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) + { + *indp = relpp->ind; + return(basepp); + } + else + { + *indp = -1; + return(0); + } +} + +void LP_priceinfoupdate(char *base,char *rel,double price) +{ + struct LP_priceinfo *basepp,*relpp; + if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) + { + dxblend(&basepp->relvals[relpp->ind],price,0.9); + dxblend(&relpp->relvals[basepp->ind],1. / price,0.9); + } +} + +double LP_myprice(double *bidp,double *askp,char *base,char *rel) +{ + struct LP_priceinfo *basepp,*relpp; + if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) + { + *askp = basepp->myprices[relpp->ind]; + *bidp = relpp->myprices[basepp->ind]; + return((*askp + *bidp) * 0.5); + } else return(0.); +} + +int32_t LP_mypriceset(char *base,char *rel,double price) +{ + struct LP_priceinfo *basepp,*relpp; + if ( price != 0. && (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) + { + basepp->myprices[relpp->ind] = price; // ask + relpp->myprices[basepp->ind] = 1. / price; // bid + return(0); + } else return(-1); +} + +double LP_price(char *base,char *rel) +{ + struct LP_priceinfo *basepp; int32_t relind; double price = 0.; + if ( (basepp= LP_priceinfoptr(&relind,base,rel)) != 0 ) + { + if ( (price= basepp->myprices[relind]) == 0. ) + price = basepp->relvals[relind]; + } + return(price); +} + +cJSON *LP_priceinfomatrix(int32_t usemyprices) +{ + int32_t i,j,n,m; double total,sum,val; struct LP_priceinfo *pp; uint32_t now; struct LP_cacheinfo *ptr,*tmp; cJSON *vectorjson = cJSON_CreateObject(); + now = (uint32_t)time(NULL); + HASH_ITER(hh,LP_cacheinfos,ptr,tmp) + { + if ( ptr->timestamp < now-60 || ptr->price == 0. ) + continue; + LP_priceinfoupdate(ptr->Q.srccoin,ptr->Q.destcoin,ptr->price); + } + pp = LP_priceinfos; + total = m = 0; + for (i=0; idiagval = sum = n = 0; + for (j=0; jmyprices[j]) == 0. ) + val = pp->relvals[j]; + if ( val != 0. ) + { + sum += val; + n++; + } + } + if ( n > 0 ) + { + pp->diagval = sum / n; + total += pp->diagval, m++; + } + } + if ( m > 0 ) + { + pp = LP_priceinfos; + for (i=0; idiagval /= total; + jaddnum(vectorjson,pp->symbol,pp->diagval); + } + } + return(vectorjson); +} + +struct LP_priceinfo *LP_priceinfoadd(char *symbol) +{ + struct LP_priceinfo *pp; int32_t i,vecsize; cJSON *retjson; + LP_priceinfos = realloc(LP_priceinfos,sizeof(*LP_priceinfos) * (LP_numpriceinfos + 1)); + pp = &LP_priceinfos[LP_numpriceinfos]; + memset(pp,0,sizeof(*pp)); + safecopy(pp->symbol,symbol,sizeof(pp->symbol)); + pp->coinbits = stringbits(symbol); + pp->ind = LP_numpriceinfos; + pp->relvals = calloc(LP_numpriceinfos+1,sizeof(*pp->relvals)); + pp->myprices = calloc(LP_numpriceinfos+1,sizeof(*pp->myprices)); + vecsize = sizeof(*LP_priceinfos[i].relvals) * (LP_numpriceinfos + 1); + for (i=0; iQ = *qp; + ptr->timestamp = (uint32_t)time(NULL); if ( price != ptr->price ) { + ptr->price = price; + LP_priceinfoupdate(base,rel,price); printf("updated %s/v%d %s/%s %llu price %.8f\n",bits256_str(str,txid),vout,base,rel,(long long)qp->satoshis,price); - } - ptr->price = price; - ptr->Q = *qp; - ptr->timestamp = (uint32_t)time(NULL); + } else ptr->price = price; return(ptr); } @@ -182,24 +331,9 @@ char *LP_orderbook(char *base,char *rel) return(jprint(retjson,1)); } -// very, very simple for now - void LP_priceupdate(char *base,char *rel,double price,double avebid,double aveask,double highbid,double lowask,double PAXPRICES[32]) { - if ( avebid > SMALLVAL && aveask > SMALLVAL && strcmp(base,"KMD") == 0 && strcmp(rel,"BTC") == 0 ) - LP_kmdbtc = (avebid + aveask) * 0.5; -} - -double LP_price(char *base,char *rel) -{ - if ( LP_kmdbtc != 0. ) - { - if ( strcmp(base,"KMD") == 0 && strcmp(rel,"BTC") == 0 ) - return(LP_kmdbtc); - else if ( strcmp(rel,"KMD") == 0 && strcmp(base,"BTC") == 0 ) - return(1. / LP_kmdbtc); - } - return(0.); + LP_priceinfoupdate(base,rel,price); } char *LP_pricestr(char *base,char *rel) @@ -214,6 +348,8 @@ char *LP_pricestr(char *base,char *rel) jaddstr(retjson,"base",base); jaddstr(retjson,"rel",rel); jaddnum(retjson,"price",price); + jadd(retjson,"theoretical",LP_priceinfomatrix(0)); + jadd(retjson,"quotes",LP_priceinfomatrix(1)); return(jprint(retjson,1)); } else return(clonestr("{\"error\":\"cant find baserel pair\"}")); } diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index a8e7f48be..2560f2297 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -322,7 +322,6 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb item = jitem(array,i); txid = jbits256(item,"txid"); vout = juint(item,"vout"); - printf("j.%d %.8f target %.8f\n",i,dstr(values[i]),dstr(targetval)); if ( jstr(item,"scriptPubKey") != 0 && strcmp(script,jstr(item,"scriptPubKey")) == 0 ) { value = values[i]; From b6f09e2bead0ceff36263032f963e4b0909b811a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 07:46:23 +0300 Subject: [PATCH 291/339] Test --- iguana/exchanges/mm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index bf3017da3..24294466b 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -852,7 +852,8 @@ int main(int argc, const char * argv[]) } sleep(1000); } -getchar(); + while ( 1 ) + sleep(1); profitmargin = jdouble(retjson,"profitmargin"); minask = jdouble(retjson,"minask"); maxbid = jdouble(retjson,"maxbid"); From 5d26807a66f505e198d2eb55a10c1bb168fe7f5e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 07:53:41 +0300 Subject: [PATCH 292/339] Test --- iguana/exchanges/LP_prices.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index 1a9864c5c..a6f86cc6d 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -26,7 +26,7 @@ struct LP_priceinfo double diagval; double *relvals; double *myprices; -} *LP_priceinfos; +} LP_priceinfos[256]; int32_t LP_numpriceinfos; struct LP_cacheinfo @@ -206,7 +206,11 @@ cJSON *LP_priceinfomatrix(int32_t usemyprices) struct LP_priceinfo *LP_priceinfoadd(char *symbol) { struct LP_priceinfo *pp; int32_t i,vecsize; cJSON *retjson; - LP_priceinfos = realloc(LP_priceinfos,sizeof(*LP_priceinfos) * (LP_numpriceinfos + 1)); + if ( LP_numpriceinfos >= sizeof(LP_priceinfos)/sizeof(*LP_priceinfos) ) + { + printf("cant add any more priceinfos\n"); + return(0); + } pp = &LP_priceinfos[LP_numpriceinfos]; memset(pp,0,sizeof(*pp)); safecopy(pp->symbol,symbol,sizeof(pp->symbol)); @@ -217,6 +221,7 @@ struct LP_priceinfo *LP_priceinfoadd(char *symbol) vecsize = sizeof(*LP_priceinfos[i].relvals) * (LP_numpriceinfos + 1); for (i=0; i Date: Sat, 3 Jun 2017 07:56:50 +0300 Subject: [PATCH 293/339] Test --- iguana/exchanges/LP_prices.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index a6f86cc6d..aa3df855b 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -18,15 +18,16 @@ // marketmaker // +#define LP_MAXPRICEINFOS 64 struct LP_priceinfo { char symbol[16]; uint64_t coinbits; int32_t ind,pad; double diagval; - double *relvals; - double *myprices; -} LP_priceinfos[256]; + double relvals[LP_MAXPRICEINFOS]; + double myprices[LP_MAXPRICEINFOS]; +} LP_priceinfos[LP_MAXPRICEINFOS]; int32_t LP_numpriceinfos; struct LP_cacheinfo @@ -205,7 +206,7 @@ cJSON *LP_priceinfomatrix(int32_t usemyprices) struct LP_priceinfo *LP_priceinfoadd(char *symbol) { - struct LP_priceinfo *pp; int32_t i,vecsize; cJSON *retjson; + struct LP_priceinfo *pp; cJSON *retjson; if ( LP_numpriceinfos >= sizeof(LP_priceinfos)/sizeof(*LP_priceinfos) ) { printf("cant add any more priceinfos\n"); @@ -215,9 +216,9 @@ struct LP_priceinfo *LP_priceinfoadd(char *symbol) memset(pp,0,sizeof(*pp)); safecopy(pp->symbol,symbol,sizeof(pp->symbol)); pp->coinbits = stringbits(symbol); - pp->ind = LP_numpriceinfos; - pp->relvals = calloc(LP_numpriceinfos+1,sizeof(*pp->relvals)); - pp->myprices = calloc(LP_numpriceinfos+1,sizeof(*pp->myprices)); + pp->ind = LP_numpriceinfos++; + /*pp->relvals = calloc(LP_numpriceinfos+1,sizeof(*pp->relvals)); + //pp->myprices = calloc(LP_numpriceinfos+1,sizeof(*pp->myprices)); vecsize = sizeof(*LP_priceinfos[i].relvals) * (LP_numpriceinfos + 1); for (i=0; i Date: Sat, 3 Jun 2017 08:09:07 +0300 Subject: [PATCH 294/339] Test --- iguana/exchanges/LP_utxos.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 2560f2297..0816aa2ad 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -203,7 +203,7 @@ void LP_utxosquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock if ( lastn >= mypeer->numutxos ) firsti = -1; else firsti = (mypeer->numutxos - lastn); - HASH_ITER(hh,LP_utxoinfos,utxo,tmp) + /*HASH_ITER(hh,LP_utxoinfos,utxo,tmp) { if ( i++ < firsti ) continue; @@ -216,7 +216,7 @@ void LP_utxosquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock } } if ( flag != 0 ) - printf(" <- missing utxos\n"); + printf(" <- missing utxos\n");*/ } else if ( peer != 0 ) peer->errors++; } From c7a05f4d638fd83750c66a9b1a58780fe508da99 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 08:14:15 +0300 Subject: [PATCH 295/339] Test --- iguana/exchanges/LP_nativeDEX.c | 5 +++++ iguana/exchanges/LP_utxos.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 450d9bad3..17c25ee75 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -111,6 +111,11 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i LP_coinfind(activecoins[i]); LP_priceinfoadd(activecoins[i]); } + HASH_ITER(hh,LP_peerinfos,peer,tmp) + { + if ( strcmp(peer->ipaddr,mypeer->ipaddr) != 0 ) + LP_utxosquery(0,mypeer,pubsock,peer->ipaddr,peer->port,"",100,mypeer->ipaddr,myport,profitmargin); + } if ( amclient != 0 ) { while ( 1 ) diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 0816aa2ad..5340de818 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -183,7 +183,7 @@ int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs void LP_utxosquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *coin,int32_t lastn,char *myipaddr,uint16_t myport,double myprofit) { - char *retstr; struct LP_utxoinfo *utxo,*tmp; struct LP_peerinfo *peer; int32_t i,firsti; uint32_t now,flag = 0; + char *retstr; struct LP_peerinfo *peer; int32_t i,firsti; uint32_t now; if ( amclient != 0 ) { printf("LP_utxosquery not for clientside\n"); From 6cc19fd8c8c1ede91b0c0659ceaa17232fbeda84 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 08:18:14 +0300 Subject: [PATCH 296/339] Test --- iguana/exchanges/LP_nativeDEX.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 17c25ee75..3316a162b 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -111,6 +111,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i LP_coinfind(activecoins[i]); LP_priceinfoadd(activecoins[i]); } + LP_privkey_updates(mypeer,pubsock,passphrase,amclient); HASH_ITER(hh,LP_peerinfos,peer,tmp) { if ( strcmp(peer->ipaddr,mypeer->ipaddr) != 0 ) @@ -121,7 +122,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i while ( 1 ) { nonz = n = 0; - if ( (counter++ % 3600) == 0 ) + if ( (++counter % 3600) == 0 ) LP_privkey_updates(mypeer,pubsock,passphrase,amclient); if ( (counter % 500) == 0 ) { @@ -163,7 +164,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i while ( 1 ) { nonz = 0; - if ( (counter++ % 2000) == 0 ) + if ( (++counter % 2000) == 0 ) LP_privkey_updates(mypeer,pubsock,passphrase,amclient); HASH_ITER(hh,LP_peerinfos,peer,tmp) { @@ -174,7 +175,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i if ( strcmp(peer->ipaddr,mypeer->ipaddr) != 0 ) LP_peersquery(amclient,mypeer,pubsock,peer->ipaddr,peer->port,mypeer->ipaddr,myport,profitmargin); } - if ( peer->numutxos > mypeer->numutxos ) + if ( peer->numutxos != mypeer->numutxos ) { lastn = peer->numutxos - mypeer->numutxos + LP_PROPAGATION_SLACK; if ( lastn < 0 ) From f2666d809d35b1891e480131d4646aef884a7120 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 08:20:11 +0300 Subject: [PATCH 297/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 3316a162b..4b871c7f8 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -178,7 +178,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i if ( peer->numutxos != mypeer->numutxos ) { lastn = peer->numutxos - mypeer->numutxos + LP_PROPAGATION_SLACK; - if ( lastn < 0 ) + if ( lastn < LP_PROPAGATION_SLACK * 2 ) lastn = LP_PROPAGATION_SLACK * 2; printf("%s numutxos.%d vs %d lastn.%d\n",peer->ipaddr,peer->numutxos,mypeer->numutxos,lastn); if ( strcmp(peer->ipaddr,mypeer->ipaddr) != 0 ) From 6c3487a8eb926f767d0e567663a593a186a5f6a5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 08:25:33 +0300 Subject: [PATCH 298/339] Test --- iguana/exchanges/LP_commands.c | 4 ++-- iguana/exchanges/LP_utxos.c | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 4be342e24..e1d600b8c 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -569,9 +569,9 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port subport = argport + 2; if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),argport)) != 0 ) { - if ( (otherpeers= jint(argjson,"numpeers")) > peer->numpeers ) + if ( 0 && (otherpeers= jint(argjson,"numpeers")) > peer->numpeers ) peer->numpeers = otherpeers; - if ( (othernumutxos= jint(argjson,"numutxos")) > peer->numutxos ) + if ( 0 && (othernumutxos= jint(argjson,"numutxos")) > peer->numutxos ) { printf("change.(%s) numutxos.%d -> %d mynumutxos.%d\n",peer->ipaddr,peer->numutxos,othernumutxos,LP_mypeer != 0 ? LP_mypeer->numutxos:0); peer->numutxos = othernumutxos; diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 5340de818..1096fd338 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -136,7 +136,7 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *destipaddr,uint16_t destport,char *retstr,uint32_t now) { - struct LP_peerinfo *peer; uint32_t argipbits; char *argipaddr; uint16_t argport,pushport,subport; cJSON *array,*item; int32_t i,n=0; bits256 txid; struct LP_utxoinfo *utxo; + struct LP_peerinfo *peer,*destpeer; uint32_t argipbits; char *argipaddr; uint16_t argport,pushport,subport; cJSON *array,*item; int32_t i,n=0; bits256 txid; struct LP_utxoinfo *utxo; if ( amclient != 0 ) { printf("LP_utxosparse not for clientside\n"); @@ -167,14 +167,15 @@ int32_t LP_utxosparse(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubs } } } - /*if ( (destpeer= LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport)) != 0 ) + if ( (destpeer= LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport)) != 0 ) { + destpeer->numutxos = n; if ( destpeer->numutxos < n ) { //destpeer->numutxos = n; //printf("got.(%s) from %s numutxos.%d\n",retstr,destpeer->ipaddr,destpeer->numutxos); } - }*/ + } } free_json(array); } From 371bb08502f7eb710340f0cf541ed2dbde9ed1bb Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 08:28:30 +0300 Subject: [PATCH 299/339] Test --- iguana/exchanges/LP_utxos.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 1096fd338..7a44196b9 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -191,7 +191,7 @@ void LP_utxosquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock return; } peer = LP_peerfind((uint32_t)calc_ipbits(destipaddr),destport); - if ( (peer != 0 && peer->errors > 0) || mypeer == 0 ) + if ( mypeer == 0 ) //(peer != 0 && peer->errors > 0) || return; if ( coin == 0 ) coin = ""; From 52d4190cfd166c980e41aa391bbb44a07d041936 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 08:45:45 +0300 Subject: [PATCH 300/339] Test --- iguana/coins/mesh_7776 | 2 +- iguana/exchanges/LP_nativeDEX.c | 4 ++-- iguana/tests/dexgetbalance | 2 +- iguana/tests/dexlistunspent | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/iguana/coins/mesh_7776 b/iguana/coins/mesh_7776 index 01d8dd737..bcccf8d8d 100755 --- a/iguana/coins/mesh_7776 +++ b/iguana/coins/mesh_7776 @@ -1 +1 @@ -curl --url "http://127.0.0.1:7776" --data "{\"conf\":\"MESH.conf\",\"path\":\"${HOME#"/"}/.komodo/MESH\",\"unitval\":\"20\",\"zcash\":1,\"RELAY\":-1,\"VALIDATE\":0,\"prefetchlag\":-1,\"poll\":100,\"active\":1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":8,\"newcoin\":\"MESH\",\"name\":\"MESH\",\"hasheaders\":1,\"useaddmultisig\":0,\"netmagic\":\"4d6bbfb6\",\"p2p\":8399,\"rpc\":8400,\"pubval\":60,\"p2shval\":85,\"wifval\":188,\"txfee_satoshis\":\"10000\",\"isPoS\":0,\"minoutput\":10000,\"minconfirms\":2,\"genesishash\":\"027e3758c3a65b12aa1046462b486d0a63bfa1beae327897f56c5cfb7daaae71\",\"protover\":170002,\"genesisblock\":\"0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000000000000000000000000000000000000000000000000000000000000029ab5f490f0f0f200b00000000000000000000000000000000000000000000000000000000000000fd4005000d5ba7cda5d473947263bf194285317179d2b0d307119c2e7cc4bd8ac456f0774bd52b0cd9249be9d40718b6397a4c7bbd8f2b3272fed2823cd2af4bd1632200ba4bf796727d6347b225f670f292343274cc35099466f5fb5f0cd1c105121b28213d15db2ed7bdba490b4cedc69742a57b7c25af24485e523aadbb77a0144fc76f79ef73bd8530d42b9f3b9bed1c135ad1fe152923fafe98f95f76f1615e64c4abb1137f4c31b218ba2782bc15534788dda2cc08a0ee2987c8b27ff41bd4e31cd5fb5643dfe862c9a02ca9f90c8c51a6671d681d04ad47e4b53b1518d4befafefe8cadfb912f3d03051b1efbf1dfe37b56e93a741d8dfd80d576ca250bee55fab1311fc7b3255977558cdda6f7d6f875306e43a14413facdaed2f46093e0ef1e8f8a963e1632dcbeebd8e49fd16b57d49b08f9762de89157c65233f60c8e38a1f503a48c555f8ec45dedecd574a37601323c27be597b956343107f8bd80f3a925afaf30811df83c402116bb9c1e5231c70fff899a7c82f73c902ba54da53cc459b7bf1113db65cc8f6914d3618560ea69abd13658fa7b6af92d374d6eca9529f8bd565166e4fcbf2a8dfb3c9b69539d4d2ee2e9321b85b331925df195915f2757637c2805e1d4131e1ad9ef9bc1bb1c732d8dba4738716d351ab30c996c8657bab39567ee3b29c6d054b711495c0d52e1cd5d8e55b4f0f0325b97369280755b46a02afd54be4ddd9f77c22272b8bbb17ff5118fedbae2564524e797bd28b5f74f7079d532ccc059807989f94d267f47e724b3f1ecfe00ec9e6541c961080d8891251b84b4480bc292f6a180bea089fef5bbda56e1e41390d7c0e85ba0ef530f7177413481a226465a36ef6afe1e2bca69d2078712b3912bba1a99b1fbff0d355d6ffe726d2bb6fbc103c4ac5756e5bee6e47e17424ebcbf1b63d8cb90ce2e40198b4f4198689daea254307e52a25562f4c1455340f0ffeb10f9d8e914775e37d0edca019fb1b9c6ef81255ed86bc51c5391e0591480f66e2d88c5f4fd7277697968656a9b113ab97f874fdd5f2465e5559533e01ba13ef4a8f7a21d02c30c8ded68e8c54603ab9c8084ef6d9eb4e92c75b078539e2ae786ebab6dab73a09e0aa9ac575bcefb29e930ae656e58bcb513f7e3c17e079dce4f05b5dbc18c2a872b22509740ebe6a3903e00ad1abc55076441862643f93606e3dc35e8d9f2caef3ee6be14d513b2e062b21d0061de3bd56881713a1a5c17f5ace05e1ec09da53f99442df175a49bd154aa96e4949decd52fed79ccf7ccbce32941419c314e374e4a396ac553e17b5340336a1a25c22f9e42a243ba5404450b650acfc826a6e432971ace776e15719515e1634ceb9a4a35061b668c74998d3dfb5827f6238ec015377e6f9c94f38108768cf6e5c8b132e0303fb5a200368f845ad9d46343035a6ff94031df8d8309415bb3f6cd5ede9c135fdabcc030599858d803c0f85be7661c88984d88faa3d26fb0e9aac0056a53f1b5d0baed713c853c4a2726869a0a124a8a5bbc0fc0ef80c8ae4cb53636aa02503b86a1eb9836fcc259823e2692d921d88e1ffc1e6cb2bde43939ceb3f32a611686f539f8f7c9f0bf00381f743607d40960f06d347d1cd8ac8a51969c25e37150efdf7aa4c2037a2fd0516fb444525ab157a0ed0a7412b2fa69b217fe397263153782c0f64351fbdf2678fa0dc8569912dcd8e3ccad38f34f23bbbce14c6a26ac24911b308b82c7e43062d180baeac4ba7153858365c72c63dcf5f6a5b08070b730adb017aeae925b7d0439979e2679f45ed2f25a7edcfd2fb77a8794630285ccb0a071f5cce410b46dbf9750b0354aae8b65574501cc69efb5b6a43444074fee116641bb29da56c2b4a7f456991fc92b2\",\"debug\":0,\"seedipaddr\":\"78.47.196.146\"}" +curl --url "http://127.0.0.1:7776" --data "{\"conf\":\"MESH.conf\",\"path\":\"${HOME#"/"}/.komodo/MESH\",\"unitval\":\"20\",\"zcash\":1,\"RELAY\":-1,\"VALIDATE\":0,\"prefetchlag\":-1,\"poll\":100,\"active\":1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":8,\"newcoin\":\"MESH\",\"name\":\"MESH\",\"hasheaders\":1,\"useaddmultisig\":0,\"netmagic\":\"4d6bbfb6\",\"p2p\":9454,\"rpc\":9455,\"pubval\":60,\"p2shval\":85,\"wifval\":188,\"txfee_satoshis\":\"10000\",\"isPoS\":0,\"minoutput\":10000,\"minconfirms\":2,\"genesishash\":\"027e3758c3a65b12aa1046462b486d0a63bfa1beae327897f56c5cfb7daaae71\",\"protover\":170002,\"genesisblock\":\"0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000000000000000000000000000000000000000000000000000000000000029ab5f490f0f0f200b00000000000000000000000000000000000000000000000000000000000000fd4005000d5ba7cda5d473947263bf194285317179d2b0d307119c2e7cc4bd8ac456f0774bd52b0cd9249be9d40718b6397a4c7bbd8f2b3272fed2823cd2af4bd1632200ba4bf796727d6347b225f670f292343274cc35099466f5fb5f0cd1c105121b28213d15db2ed7bdba490b4cedc69742a57b7c25af24485e523aadbb77a0144fc76f79ef73bd8530d42b9f3b9bed1c135ad1fe152923fafe98f95f76f1615e64c4abb1137f4c31b218ba2782bc15534788dda2cc08a0ee2987c8b27ff41bd4e31cd5fb5643dfe862c9a02ca9f90c8c51a6671d681d04ad47e4b53b1518d4befafefe8cadfb912f3d03051b1efbf1dfe37b56e93a741d8dfd80d576ca250bee55fab1311fc7b3255977558cdda6f7d6f875306e43a14413facdaed2f46093e0ef1e8f8a963e1632dcbeebd8e49fd16b57d49b08f9762de89157c65233f60c8e38a1f503a48c555f8ec45dedecd574a37601323c27be597b956343107f8bd80f3a925afaf30811df83c402116bb9c1e5231c70fff899a7c82f73c902ba54da53cc459b7bf1113db65cc8f6914d3618560ea69abd13658fa7b6af92d374d6eca9529f8bd565166e4fcbf2a8dfb3c9b69539d4d2ee2e9321b85b331925df195915f2757637c2805e1d4131e1ad9ef9bc1bb1c732d8dba4738716d351ab30c996c8657bab39567ee3b29c6d054b711495c0d52e1cd5d8e55b4f0f0325b97369280755b46a02afd54be4ddd9f77c22272b8bbb17ff5118fedbae2564524e797bd28b5f74f7079d532ccc059807989f94d267f47e724b3f1ecfe00ec9e6541c961080d8891251b84b4480bc292f6a180bea089fef5bbda56e1e41390d7c0e85ba0ef530f7177413481a226465a36ef6afe1e2bca69d2078712b3912bba1a99b1fbff0d355d6ffe726d2bb6fbc103c4ac5756e5bee6e47e17424ebcbf1b63d8cb90ce2e40198b4f4198689daea254307e52a25562f4c1455340f0ffeb10f9d8e914775e37d0edca019fb1b9c6ef81255ed86bc51c5391e0591480f66e2d88c5f4fd7277697968656a9b113ab97f874fdd5f2465e5559533e01ba13ef4a8f7a21d02c30c8ded68e8c54603ab9c8084ef6d9eb4e92c75b078539e2ae786ebab6dab73a09e0aa9ac575bcefb29e930ae656e58bcb513f7e3c17e079dce4f05b5dbc18c2a872b22509740ebe6a3903e00ad1abc55076441862643f93606e3dc35e8d9f2caef3ee6be14d513b2e062b21d0061de3bd56881713a1a5c17f5ace05e1ec09da53f99442df175a49bd154aa96e4949decd52fed79ccf7ccbce32941419c314e374e4a396ac553e17b5340336a1a25c22f9e42a243ba5404450b650acfc826a6e432971ace776e15719515e1634ceb9a4a35061b668c74998d3dfb5827f6238ec015377e6f9c94f38108768cf6e5c8b132e0303fb5a200368f845ad9d46343035a6ff94031df8d8309415bb3f6cd5ede9c135fdabcc030599858d803c0f85be7661c88984d88faa3d26fb0e9aac0056a53f1b5d0baed713c853c4a2726869a0a124a8a5bbc0fc0ef80c8ae4cb53636aa02503b86a1eb9836fcc259823e2692d921d88e1ffc1e6cb2bde43939ceb3f32a611686f539f8f7c9f0bf00381f743607d40960f06d347d1cd8ac8a51969c25e37150efdf7aa4c2037a2fd0516fb444525ab157a0ed0a7412b2fa69b217fe397263153782c0f64351fbdf2678fa0dc8569912dcd8e3ccad38f34f23bbbce14c6a26ac24911b308b82c7e43062d180baeac4ba7153858365c72c63dcf5f6a5b08070b730adb017aeae925b7d0439979e2679f45ed2f25a7edcfd2fb77a8794630285ccb0a071f5cce410b46dbf9750b0354aae8b65574501cc69efb5b6a43444074fee116641bb29da56c2b4a7f456991fc92b2\",\"debug\":0,\"seedipaddr\":\"78.47.196.146\"}" diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 4b871c7f8..e02714b2e 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -114,8 +114,8 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i LP_privkey_updates(mypeer,pubsock,passphrase,amclient); HASH_ITER(hh,LP_peerinfos,peer,tmp) { - if ( strcmp(peer->ipaddr,mypeer->ipaddr) != 0 ) - LP_utxosquery(0,mypeer,pubsock,peer->ipaddr,peer->port,"",100,mypeer->ipaddr,myport,profitmargin); + if ( strcmp(peer->ipaddr,mypeer != 0 ? mypeer->ipaddr : "127.0.0.1") != 0 ) + LP_utxosquery(0,mypeer,pubsock,peer->ipaddr,peer->port,"",100,mypeer != 0 ? mypeer->ipaddr : "127.0.0.1",myport,profitmargin); } if ( amclient != 0 ) { diff --git a/iguana/tests/dexgetbalance b/iguana/tests/dexgetbalance index 77d8982af..f096618ee 100755 --- a/iguana/tests/dexgetbalance +++ b/iguana/tests/dexgetbalance @@ -1,2 +1,2 @@ #!/bin/bash -curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"dex\",\"method\":\"getbalance\",\"address\":\"RHrAStvASzi5pJB6xqgmsVGD5AxxpAFzZ6\",\"symbol\":\"KMD\"}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"dex\",\"method\":\"getbalance\",\"address\":\"RYZx1e7kVNTuQ6RTrvTkYeZAz5uJobC3pg\",\"symbol\":\"KMD\"}" diff --git a/iguana/tests/dexlistunspent b/iguana/tests/dexlistunspent index 0f6eb190f..4c06d2429 100755 --- a/iguana/tests/dexlistunspent +++ b/iguana/tests/dexlistunspent @@ -1,2 +1,2 @@ #!/bin/bash -curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"dex\",\"method\":\"listunspent\",\"address\":\"RVy1qgs2HRmXKWWawoqFSo3WGBkjC19cmJ\",\"symbol\":\"KMD\"}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"dex\",\"method\":\"listunspent\",\"address\":\"RYZx1e7kVNTuQ6RTrvTkYeZAz5uJobC3pg\",\"symbol\":\"KMD\"}" From 59f04f5404f114a469f3433250038ab5bd918bf2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 08:57:43 +0300 Subject: [PATCH 301/339] Test --- iguana/exchanges/LP_commands.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index e1d600b8c..6ee80db40 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -503,8 +503,6 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port return(clonestr("{\"error\":\"need method in request\"}")); if ( USERPASS[0] != 0 && strcmp(remoteaddr,"127.0.0.1") == 0 && port != 0 ) { - if ( (userpass= jstr(argjson,"userpass")) == 0 || strcmp(userpass,USERPASS) != 0 ) - return(clonestr("{\"error\":\"authentication error\"}")); if ( USERPASS_COUNTER == 0 ) { USERPASS_COUNTER = 1; @@ -512,6 +510,8 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port jaddstr(retjson,"userpass",USERPASS); return(jprint(retjson,1)); } + if ( (userpass= jstr(argjson,"userpass")) == 0 || strcmp(userpass,USERPASS) != 0 ) + return(clonestr("{\"error\":\"authentication error\"}")); if ( (base= jstr(argjson,"base")) != 0 && (rel= jstr(argjson,"rel")) != 0 ) { if ( strcmp(method,"setprice") == 0 ) From 1bbffbb0a90831ed48a1384acc1ce5bfd3dbc086 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 08:58:33 +0300 Subject: [PATCH 302/339] Test --- iguana/exchanges/LP_nativeDEX.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index e02714b2e..977892321 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -78,7 +78,7 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,int32_t pullsock,uint16_t myport,int32_t amclient,char *passphrase,double profitmargin) { - static uint16_t tmpport; + //static uint16_t tmpport; char *retstr; uint8_t r; int32_t i,n,j,len,recvsize,counter=0,nonz,lastn; struct LP_peerinfo *peer,*tmp; struct LP_utxoinfo *utxo,*utmp; void *ptr; cJSON *argjson; if ( amclient == 0 ) { @@ -98,10 +98,10 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i LP_peersquery(amclient,mypeer,pubsock,default_LPnodes[i],myport,"127.0.0.1",myport,profitmargin); } } - if ( amclient != 0 ) - tmpport = myport + 10; - else tmpport = myport; - if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&tmpport) != 0 ) + //if ( amclient != 0 ) + // tmpport = myport + 10; + //else tmpport = myport; + if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&myport) != 0 ) { printf("error launching stats rpcloop for port.%u\n",myport); exit(-1); From 1892d3f4bb60e874249e931572b9a0585c9f8b6f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:12:33 +0300 Subject: [PATCH 303/339] Test --- iguana/exchanges/LP_commands.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 6ee80db40..977c201b4 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -534,14 +534,14 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } else return(clonestr("{\"error\":\"no price set\"}")); } } - else if ( IAMCLIENT != 0 && (coin= jstr(argjson,"coin")) != 0 ) + else if ( (coin= jstr(argjson,"coin")) != 0 ) { if ( strcmp(method,"inventory") == 0 ) { LP_privkey_init(0,-1,coin,0,USERPASS_WIFSTR,1); return(LP_inventory(coin)); } - else if ( strcmp(method,"candidates") == 0 || strcmp(method,"autotrade") == 0 ) + else if ( IAMCLIENT != 0 && (strcmp(method,"candidates") == 0 || strcmp(method,"autotrade") == 0) ) { bits256 txid; int32_t vout; struct LP_utxoinfo *utxo; txid = jbits256(argjson,"txid"); From 0d1ee1a7a24065099ded17fe0a9b9fe102b9f400 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:14:06 +0300 Subject: [PATCH 304/339] Test --- iguana/exchanges/LP_utxos.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 7a44196b9..5f897a525 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -227,7 +227,7 @@ char *LP_inventory(char *symbol) struct LP_utxoinfo *utxo,*tmp; cJSON *array = cJSON_CreateArray(); HASH_ITER(hh,LP_utxoinfos,utxo,tmp) { - if ( strcmp(symbol,utxo->coin) == 0 ) + if ( strcmp(symbol,utxo->coin) == 0 && (IAMCLIENT != 0 || strcmp(utxo->ipaddr,"127.0.0.1") != 0) ) jaddi(array,LP_inventoryjson(cJSON_CreateObject(),utxo)); } return(jprint(array,1)); From d24769d56b03d134cb814b8fef9965fbf4f47f8c Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:15:50 +0300 Subject: [PATCH 305/339] Test --- iguana/exchanges/LP_utxos.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 5f897a525..68b9f3987 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -224,10 +224,13 @@ void LP_utxosquery(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock char *LP_inventory(char *symbol) { - struct LP_utxoinfo *utxo,*tmp; cJSON *array = cJSON_CreateArray(); + struct LP_utxoinfo *utxo,*tmp; char *myipaddr; cJSON *array = cJSON_CreateArray(); + if ( LP_mypeer != 0 ) + myipaddr = LP_mypeer->ipaddr; + else myipaddr = "127.0.0.1"; HASH_ITER(hh,LP_utxoinfos,utxo,tmp) { - if ( strcmp(symbol,utxo->coin) == 0 && (IAMCLIENT != 0 || strcmp(utxo->ipaddr,"127.0.0.1") != 0) ) + if ( strcmp(symbol,utxo->coin) == 0 && (IAMCLIENT != 0 || strcmp(utxo->ipaddr,myipaddr) != 0) ) jaddi(array,LP_inventoryjson(cJSON_CreateObject(),utxo)); } return(jprint(array,1)); From 9797734042776a2fc4e609071f024432d5c3fad9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:16:18 +0300 Subject: [PATCH 306/339] Test --- iguana/exchanges/LP_utxos.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 68b9f3987..22ba7fc2f 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -230,7 +230,7 @@ char *LP_inventory(char *symbol) else myipaddr = "127.0.0.1"; HASH_ITER(hh,LP_utxoinfos,utxo,tmp) { - if ( strcmp(symbol,utxo->coin) == 0 && (IAMCLIENT != 0 || strcmp(utxo->ipaddr,myipaddr) != 0) ) + if ( strcmp(symbol,utxo->coin) == 0 && (IAMCLIENT != 0 || strcmp(utxo->ipaddr,myipaddr) == 0) ) jaddi(array,LP_inventoryjson(cJSON_CreateObject(),utxo)); } return(jprint(array,1)); From a3a465d625b7b57d9dd3128a6e0b3686c983fe11 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:20:54 +0300 Subject: [PATCH 307/339] Test --- iguana/exchanges/inv | 2 ++ iguana/exchanges/randval | 1 + iguana/exchanges/run | 2 ++ iguana/exchanges/userpass | 1 + 4 files changed, 6 insertions(+) create mode 100755 iguana/exchanges/inv create mode 100644 iguana/exchanges/randval create mode 100755 iguana/exchanges/run create mode 100644 iguana/exchanges/userpass diff --git a/iguana/exchanges/inv b/iguana/exchanges/inv new file mode 100755 index 000000000..b0b5e52b1 --- /dev/null +++ b/iguana/exchanges/inv @@ -0,0 +1,2 @@ +source userpass +curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"inventory\",\"coin\":\"KMD\"}" diff --git a/iguana/exchanges/randval b/iguana/exchanges/randval new file mode 100644 index 000000000..ca5c33f4d --- /dev/null +++ b/iguana/exchanges/randval @@ -0,0 +1 @@ +export randval="" diff --git a/iguana/exchanges/run b/iguana/exchanges/run new file mode 100755 index 000000000..c65b97bc8 --- /dev/null +++ b/iguana/exchanges/run @@ -0,0 +1,2 @@ +source randval +pkill -15 marketmaker; git pull; ./m_mm; ./marketmaker "{\"profitmargin\":0.01,\"passphrase\":\"$randval\"}" & diff --git a/iguana/exchanges/userpass b/iguana/exchanges/userpass new file mode 100644 index 000000000..1a8a9ad5a --- /dev/null +++ b/iguana/exchanges/userpass @@ -0,0 +1 @@ +export userpass="" From c2c5c53ee44f2f7262471ce83f0ff0e7d2bf2796 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:22:32 +0300 Subject: [PATCH 308/339] Test --- iguana/exchanges/client | 2 ++ 1 file changed, 2 insertions(+) create mode 100755 iguana/exchanges/client diff --git a/iguana/exchanges/client b/iguana/exchanges/client new file mode 100755 index 000000000..3ea2eecb8 --- /dev/null +++ b/iguana/exchanges/client @@ -0,0 +1,2 @@ +source randval +pkill -15 marketmaker; git pull; ./m_mm; ./marketmaker "{\"client\":1,\"passphrase\":\"$randval\"}" & From 5e1dbe509dadc010012a3cc93098d9361757dffc Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:25:12 +0300 Subject: [PATCH 309/339] Test --- iguana/exchanges/myprice | 2 ++ iguana/exchanges/setprice | 2 ++ 2 files changed, 4 insertions(+) create mode 100755 iguana/exchanges/myprice create mode 100755 iguana/exchanges/setprice diff --git a/iguana/exchanges/myprice b/iguana/exchanges/myprice new file mode 100755 index 000000000..cdb93dabc --- /dev/null +++ b/iguana/exchanges/myprice @@ -0,0 +1,2 @@ +source userpass +curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"myprice\",\"base\":\"REVS\",\"rel\":\"KMD\"}" diff --git a/iguana/exchanges/setprice b/iguana/exchanges/setprice new file mode 100755 index 000000000..e5a9c3c9c --- /dev/null +++ b/iguana/exchanges/setprice @@ -0,0 +1,2 @@ +source userpass +curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"setprice\",\"base\":\"REVS\",\"rel\":\"KMD\",\"price\":1.234}" From 85b76c06f5d41f9057780ab86b91ce6c3b2dcf87 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:31:36 +0300 Subject: [PATCH 310/339] Test --- iguana/exchanges/LP_commands.c | 4 ++-- iguana/exchanges/getpeers | 1 + iguana/exchanges/getprice | 1 + iguana/exchanges/orderbook | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) create mode 100755 iguana/exchanges/getpeers create mode 100755 iguana/exchanges/getprice create mode 100755 iguana/exchanges/orderbook diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 977c201b4..df8233d8e 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -602,11 +602,11 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } } } - else if ( IAMCLIENT == 0 && strcmp(method,"getprice") == 0 ) + else if ( strcmp(method,"getprice") == 0 ) retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel")); else if ( strcmp(method,"orderbook") == 0 ) retstr = LP_orderbook(jstr(argjson,"base"),jstr(argjson,"rel")); - else if ( IAMCLIENT == 0 && strcmp(method,"getpeers") == 0 ) + else if ( strcmp(method,"getpeers") == 0 ) retstr = LP_peers(); else if ( IAMCLIENT == 0 && strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 ) retstr = LP_utxos(LP_mypeer,coin,jint(argjson,"lastn")); diff --git a/iguana/exchanges/getpeers b/iguana/exchanges/getpeers new file mode 100755 index 000000000..93e7b9f8d --- /dev/null +++ b/iguana/exchanges/getpeers @@ -0,0 +1 @@ +curl --url "http://127.0.0.1:7779" --data "{\"method\":\"getpeers\"}" diff --git a/iguana/exchanges/getprice b/iguana/exchanges/getprice new file mode 100755 index 000000000..88e8e6502 --- /dev/null +++ b/iguana/exchanges/getprice @@ -0,0 +1 @@ +curl --url "http://127.0.0.1:7779" --data "{\"method\":\"myprice\",\"base\":\"REVS\",\"rel\":\"KMD\"}" diff --git a/iguana/exchanges/orderbook b/iguana/exchanges/orderbook new file mode 100755 index 000000000..8ed8b537f --- /dev/null +++ b/iguana/exchanges/orderbook @@ -0,0 +1 @@ +curl --url "http://127.0.0.1:7779" --data "{\"method\":\"orderbook\",\"base\":\"REVS\",\"rel\":\"KMD\"}" From 75883011bcb720f89b3fed52d7a0779195d68182 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:36:31 +0300 Subject: [PATCH 311/339] Test --- iguana/exchanges/getpeers | 3 ++- iguana/exchanges/getpeersIP | 10 ++++++++++ iguana/exchanges/getprice | 3 ++- iguana/exchanges/orderbook | 3 ++- 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100755 iguana/exchanges/getpeersIP diff --git a/iguana/exchanges/getpeers b/iguana/exchanges/getpeers index 93e7b9f8d..32852ba6a 100755 --- a/iguana/exchanges/getpeers +++ b/iguana/exchanges/getpeers @@ -1 +1,2 @@ -curl --url "http://127.0.0.1:7779" --data "{\"method\":\"getpeers\"}" +source userpass +curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"getpeers\"}" diff --git a/iguana/exchanges/getpeersIP b/iguana/exchanges/getpeersIP new file mode 100755 index 000000000..feef2e5d8 --- /dev/null +++ b/iguana/exchanges/getpeersIP @@ -0,0 +1,10 @@ +curl --url "http://5.9.253.195:7779" --data "{\"method\":\"getpeers\"}" +curl --url "http://5.9.253.196:7779" --data "{\"method\":\"getpeers\"}" +curl --url "http://5.9.253.197:7779" --data "{\"method\":\"getpeers\"}" +curl --url "http://5.9.253.198:7779" --data "{\"method\":\"getpeers\"}" +curl --url "http://5.9.253.199:7779" --data "{\"method\":\"getpeers\"}" +curl --url "http://5.9.253.200:7779" --data "{\"method\":\"getpeers\"}" +curl --url "http://5.9.253.201:7779" --data "{\"method\":\"getpeers\"}" +curl --url "http://5.9.253.202:7779" --data "{\"method\":\"getpeers\"}" +curl --url "http://5.9.253.203:7779" --data "{\"method\":\"getpeers\"}" +curl --url "http://5.9.253.204:7779" --data "{\"method\":\"getpeers\"}" diff --git a/iguana/exchanges/getprice b/iguana/exchanges/getprice index 88e8e6502..4b9614be7 100755 --- a/iguana/exchanges/getprice +++ b/iguana/exchanges/getprice @@ -1 +1,2 @@ -curl --url "http://127.0.0.1:7779" --data "{\"method\":\"myprice\",\"base\":\"REVS\",\"rel\":\"KMD\"}" +source userpass +curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"getprice\",\"base\":\"REVS\",\"rel\":\"KMD\"}" diff --git a/iguana/exchanges/orderbook b/iguana/exchanges/orderbook index 8ed8b537f..4d2367a42 100755 --- a/iguana/exchanges/orderbook +++ b/iguana/exchanges/orderbook @@ -1 +1,2 @@ -curl --url "http://127.0.0.1:7779" --data "{\"method\":\"orderbook\",\"base\":\"REVS\",\"rel\":\"KMD\"}" +source userpass +curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"orderbook\",\"base\":\"REVS\",\"rel\":\"KMD\"}" From 058d2677dc9ee1d01466f26d2ae3a939293cdd62 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:43:49 +0300 Subject: [PATCH 312/339] Test --- iguana/exchanges/LP_nativeDEX.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 977892321..24bf9d0c6 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -124,15 +124,6 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i nonz = n = 0; if ( (++counter % 3600) == 0 ) LP_privkey_updates(mypeer,pubsock,passphrase,amclient); - if ( (counter % 500) == 0 ) - { - HASH_ITER(hh,LP_utxoinfos,utxo,utmp) - { - if ( strcmp(utxo->coin,"KMD") == 0 ) - LP_priceping(pubsock,utxo,"BTC",profitmargin); - else LP_priceping(pubsock,utxo,"KMD",profitmargin); - } - } HASH_ITER(hh,LP_peerinfos,peer,tmp) { n++; @@ -166,6 +157,15 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i nonz = 0; if ( (++counter % 2000) == 0 ) LP_privkey_updates(mypeer,pubsock,passphrase,amclient); + if ( (counter % 500) == 0 ) + { + HASH_ITER(hh,LP_utxoinfos,utxo,utmp) + { + if ( strcmp(utxo->coin,"KMD") == 0 ) + LP_priceping(pubsock,utxo,"BTC",profitmargin); + else LP_priceping(pubsock,utxo,"KMD",profitmargin); + } + } HASH_ITER(hh,LP_peerinfos,peer,tmp) { if ( peer->numpeers != mypeer->numpeers || (rand() % 10000) == 0 ) From 9599b64ccd503f2147c3a5e39dac2bd6d157a151 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:46:59 +0300 Subject: [PATCH 313/339] Test --- iguana/exchanges/client | 2 +- iguana/exchanges/run | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/client b/iguana/exchanges/client index 3ea2eecb8..fe29cc7ce 100755 --- a/iguana/exchanges/client +++ b/iguana/exchanges/client @@ -1,2 +1,2 @@ source randval -pkill -15 marketmaker; git pull; ./m_mm; ./marketmaker "{\"client\":1,\"passphrase\":\"$randval\"}" & +pkill -15 marketmaker; git pull; ../m_mm; ../marketmaker "{\"client\":1,\"passphrase\":\"$randval\"}" & diff --git a/iguana/exchanges/run b/iguana/exchanges/run index c65b97bc8..9ee0a2690 100755 --- a/iguana/exchanges/run +++ b/iguana/exchanges/run @@ -1,2 +1,2 @@ source randval -pkill -15 marketmaker; git pull; ./m_mm; ./marketmaker "{\"profitmargin\":0.01,\"passphrase\":\"$randval\"}" & +pkill -15 marketmaker; git pull; ../m_mm; ../marketmaker "{\"profitmargin\":0.01,\"passphrase\":\"$randval\"}" & From abcf1f1c125c9e9dc0d7a1dfe0a94f448d9d9433 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 09:48:06 +0300 Subject: [PATCH 314/339] Test --- iguana/exchanges/client | 2 +- iguana/exchanges/run | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/client b/iguana/exchanges/client index fe29cc7ce..261b1d36a 100755 --- a/iguana/exchanges/client +++ b/iguana/exchanges/client @@ -1,2 +1,2 @@ source randval -pkill -15 marketmaker; git pull; ../m_mm; ../marketmaker "{\"client\":1,\"passphrase\":\"$randval\"}" & +pkill -15 marketmaker; git pull; cd ..; ./m_mm; ./marketmaker "{\"client\":1,\"passphrase\":\"$randval\"}" & diff --git a/iguana/exchanges/run b/iguana/exchanges/run index 9ee0a2690..ccc62231a 100755 --- a/iguana/exchanges/run +++ b/iguana/exchanges/run @@ -1,2 +1,2 @@ source randval -pkill -15 marketmaker; git pull; ../m_mm; ../marketmaker "{\"profitmargin\":0.01,\"passphrase\":\"$randval\"}" & +pkill -15 marketmaker; git pull; cd ..; ./m_mm; ./marketmaker "{\"profitmargin\":0.01,\"passphrase\":\"$randval\"}" & From 505b7f7d8db58ab1fdf9ea0ed251bc78bdc8bc10 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 10:07:46 +0300 Subject: [PATCH 315/339] Test --- iguana/coins/jumblr_osx | 3 +++ iguana/coins/revs_osx | 3 +++ 2 files changed, 6 insertions(+) create mode 100755 iguana/coins/jumblr_osx create mode 100755 iguana/coins/revs_osx diff --git a/iguana/coins/jumblr_osx b/iguana/coins/jumblr_osx new file mode 100755 index 000000000..dd50b1842 --- /dev/null +++ b/iguana/coins/jumblr_osx @@ -0,0 +1,3 @@ +/Applications/komodoOSX.app/Contents/komodod -ac_name=JUMBLR -ac_supply=999999 -addnode=78.47.196.146 & +curl --url "http://127.0.0.1:7778" --data "{\"conf\":\"JUMBLR.conf\",\"path\":\"/${HOME#"/"}/Library/Application\ Support/Komodo/JUMBLR\",\"unitval\":\"20\",\"zcash\":1,\"RELAY\":-1,\"VALIDATE\":0,\"prefetchlag\":-1,\"poll\":100,\"active\":1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":8,\"newcoin\":\"JUMBLR\",\"name\":\"JUMBLR\",\"hasheaders\":1,\"useaddmultisig\":0,\"netmagic\":\"7223759e\",\"p2p\":15105,\"rpc\":15106,\"pubval\":60,\"p2shval\":85,\"wifval\":188,\"txfee_satoshis\":\"10000\",\"isPoS\":0,\"minoutput\":10000,\"minconfirms\":2,\"genesishash\":\"027e3758c3a65b12aa1046462b486d0a63bfa1beae327897f56c5cfb7daaae71\",\"protover\":170002,\"genesisblock\":\"0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000000000000000000000000000000000000000000000000000000000000029ab5f490f0f0f200b00000000000000000000000000000000000000000000000000000000000000fd4005000d5ba7cda5d473947263bf194285317179d2b0d307119c2e7cc4bd8ac456f0774bd52b0cd9249be9d40718b6397a4c7bbd8f2b3272fed2823cd2af4bd1632200ba4bf796727d6347b225f670f292343274cc35099466f5fb5f0cd1c105121b28213d15db2ed7bdba490b4cedc69742a57b7c25af24485e523aadbb77a0144fc76f79ef73bd8530d42b9f3b9bed1c135ad1fe152923fafe98f95f76f1615e64c4abb1137f4c31b218ba2782bc15534788dda2cc08a0ee2987c8b27ff41bd4e31cd5fb5643dfe862c9a02ca9f90c8c51a6671d681d04ad47e4b53b1518d4befafefe8cadfb912f3d03051b1efbf1dfe37b56e93a741d8dfd80d576ca250bee55fab1311fc7b3255977558cdda6f7d6f875306e43a14413facdaed2f46093e0ef1e8f8a963e1632dcbeebd8e49fd16b57d49b08f9762de89157c65233f60c8e38a1f503a48c555f8ec45dedecd574a37601323c27be597b956343107f8bd80f3a925afaf30811df83c402116bb9c1e5231c70fff899a7c82f73c902ba54da53cc459b7bf1113db65cc8f6914d3618560ea69abd13658fa7b6af92d374d6eca9529f8bd565166e4fcbf2a8dfb3c9b69539d4d2ee2e9321b85b331925df195915f2757637c2805e1d4131e1ad9ef9bc1bb1c732d8dba4738716d351ab30c996c8657bab39567ee3b29c6d054b711495c0d52e1cd5d8e55b4f0f0325b97369280755b46a02afd54be4ddd9f77c22272b8bbb17ff5118fedbae2564524e797bd28b5f74f7079d532ccc059807989f94d267f47e724b3f1ecfe00ec9e6541c961080d8891251b84b4480bc292f6a180bea089fef5bbda56e1e41390d7c0e85ba0ef530f7177413481a226465a36ef6afe1e2bca69d2078712b3912bba1a99b1fbff0d355d6ffe726d2bb6fbc103c4ac5756e5bee6e47e17424ebcbf1b63d8cb90ce2e40198b4f4198689daea254307e52a25562f4c1455340f0ffeb10f9d8e914775e37d0edca019fb1b9c6ef81255ed86bc51c5391e0591480f66e2d88c5f4fd7277697968656a9b113ab97f874fdd5f2465e5559533e01ba13ef4a8f7a21d02c30c8ded68e8c54603ab9c8084ef6d9eb4e92c75b078539e2ae786ebab6dab73a09e0aa9ac575bcefb29e930ae656e58bcb513f7e3c17e079dce4f05b5dbc18c2a872b22509740ebe6a3903e00ad1abc55076441862643f93606e3dc35e8d9f2caef3ee6be14d513b2e062b21d0061de3bd56881713a1a5c17f5ace05e1ec09da53f99442df175a49bd154aa96e4949decd52fed79ccf7ccbce32941419c314e374e4a396ac553e17b5340336a1a25c22f9e42a243ba5404450b650acfc826a6e432971ace776e15719515e1634ceb9a4a35061b668c74998d3dfb5827f6238ec015377e6f9c94f38108768cf6e5c8b132e0303fb5a200368f845ad9d46343035a6ff94031df8d8309415bb3f6cd5ede9c135fdabcc030599858d803c0f85be7661c88984d88faa3d26fb0e9aac0056a53f1b5d0baed713c853c4a2726869a0a124a8a5bbc0fc0ef80c8ae4cb53636aa02503b86a1eb9836fcc259823e2692d921d88e1ffc1e6cb2bde43939ceb3f32a611686f539f8f7c9f0bf00381f743607d40960f06d347d1cd8ac8a51969c25e37150efdf7aa4c2037a2fd0516fb444525ab157a0ed0a7412b2fa69b217fe397263153782c0f64351fbdf2678fa0dc8569912dcd8e3ccad38f34f23bbbce14c6a26ac24911b308b82c7e43062d180baeac4ba7153858365c72c63dcf5f6a5b08070b730adb017aeae925b7d0439979e2679f45ed2f25a7edcfd2fb77a8794630285ccb0a071f5cce410b46dbf9750b0354aae8b65574501cc69efb5b6a43444074fee116641bb29da56c2b4a7f456991fc92b2\",\"debug\":0,\"seedipaddr\":\"78.47.196.146\"}" + diff --git a/iguana/coins/revs_osx b/iguana/coins/revs_osx new file mode 100755 index 000000000..c98b4fabb --- /dev/null +++ b/iguana/coins/revs_osx @@ -0,0 +1,3 @@ +/Applications/komodoOSX.app/Contents/komodod -ac_name=REVS -ac_supply=1300000 -addnode=78.47.196.146 & +curl --url "http://127.0.0.1:7778" --data "{\"conf\":\"REVS.conf\",\"path\":\"/${HOME#"/"}/Library/Application\ Support/Komodo/REVS\",\"unitval\":\"20\",\"zcash\":1,\"RELAY\":-1,\"VALIDATE\":0,\"prefetchlag\":-1,\"poll\":100,\"active\":1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":8,\"newcoin\":\"REVS\",\"name\":\"REVS\",\"hasheaders\":1,\"useaddmultisig\":0,\"netmagic\":\"905c3498\",\"p2p\":10195,\"rpc\":10196,\"pubval\":60,\"p2shval\":85,\"wifval\":188,\"txfee_satoshis\":\"10000\",\"isPoS\":0,\"minoutput\":10000,\"minconfirms\":2,\"genesishash\":\"027e3758c3a65b12aa1046462b486d0a63bfa1beae327897f56c5cfb7daaae71\",\"protover\":170002,\"genesisblock\":\"0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000000000000000000000000000000000000000000000000000000000000029ab5f490f0f0f200b00000000000000000000000000000000000000000000000000000000000000fd4005000d5ba7cda5d473947263bf194285317179d2b0d307119c2e7cc4bd8ac456f0774bd52b0cd9249be9d40718b6397a4c7bbd8f2b3272fed2823cd2af4bd1632200ba4bf796727d6347b225f670f292343274cc35099466f5fb5f0cd1c105121b28213d15db2ed7bdba490b4cedc69742a57b7c25af24485e523aadbb77a0144fc76f79ef73bd8530d42b9f3b9bed1c135ad1fe152923fafe98f95f76f1615e64c4abb1137f4c31b218ba2782bc15534788dda2cc08a0ee2987c8b27ff41bd4e31cd5fb5643dfe862c9a02ca9f90c8c51a6671d681d04ad47e4b53b1518d4befafefe8cadfb912f3d03051b1efbf1dfe37b56e93a741d8dfd80d576ca250bee55fab1311fc7b3255977558cdda6f7d6f875306e43a14413facdaed2f46093e0ef1e8f8a963e1632dcbeebd8e49fd16b57d49b08f9762de89157c65233f60c8e38a1f503a48c555f8ec45dedecd574a37601323c27be597b956343107f8bd80f3a925afaf30811df83c402116bb9c1e5231c70fff899a7c82f73c902ba54da53cc459b7bf1113db65cc8f6914d3618560ea69abd13658fa7b6af92d374d6eca9529f8bd565166e4fcbf2a8dfb3c9b69539d4d2ee2e9321b85b331925df195915f2757637c2805e1d4131e1ad9ef9bc1bb1c732d8dba4738716d351ab30c996c8657bab39567ee3b29c6d054b711495c0d52e1cd5d8e55b4f0f0325b97369280755b46a02afd54be4ddd9f77c22272b8bbb17ff5118fedbae2564524e797bd28b5f74f7079d532ccc059807989f94d267f47e724b3f1ecfe00ec9e6541c961080d8891251b84b4480bc292f6a180bea089fef5bbda56e1e41390d7c0e85ba0ef530f7177413481a226465a36ef6afe1e2bca69d2078712b3912bba1a99b1fbff0d355d6ffe726d2bb6fbc103c4ac5756e5bee6e47e17424ebcbf1b63d8cb90ce2e40198b4f4198689daea254307e52a25562f4c1455340f0ffeb10f9d8e914775e37d0edca019fb1b9c6ef81255ed86bc51c5391e0591480f66e2d88c5f4fd7277697968656a9b113ab97f874fdd5f2465e5559533e01ba13ef4a8f7a21d02c30c8ded68e8c54603ab9c8084ef6d9eb4e92c75b078539e2ae786ebab6dab73a09e0aa9ac575bcefb29e930ae656e58bcb513f7e3c17e079dce4f05b5dbc18c2a872b22509740ebe6a3903e00ad1abc55076441862643f93606e3dc35e8d9f2caef3ee6be14d513b2e062b21d0061de3bd56881713a1a5c17f5ace05e1ec09da53f99442df175a49bd154aa96e4949decd52fed79ccf7ccbce32941419c314e374e4a396ac553e17b5340336a1a25c22f9e42a243ba5404450b650acfc826a6e432971ace776e15719515e1634ceb9a4a35061b668c74998d3dfb5827f6238ec015377e6f9c94f38108768cf6e5c8b132e0303fb5a200368f845ad9d46343035a6ff94031df8d8309415bb3f6cd5ede9c135fdabcc030599858d803c0f85be7661c88984d88faa3d26fb0e9aac0056a53f1b5d0baed713c853c4a2726869a0a124a8a5bbc0fc0ef80c8ae4cb53636aa02503b86a1eb9836fcc259823e2692d921d88e1ffc1e6cb2bde43939ceb3f32a611686f539f8f7c9f0bf00381f743607d40960f06d347d1cd8ac8a51969c25e37150efdf7aa4c2037a2fd0516fb444525ab157a0ed0a7412b2fa69b217fe397263153782c0f64351fbdf2678fa0dc8569912dcd8e3ccad38f34f23bbbce14c6a26ac24911b308b82c7e43062d180baeac4ba7153858365c72c63dcf5f6a5b08070b730adb017aeae925b7d0439979e2679f45ed2f25a7edcfd2fb77a8794630285ccb0a071f5cce410b46dbf9750b0354aae8b65574501cc69efb5b6a43444074fee116641bb29da56c2b4a7f456991fc92b2\",\"debug\":0,\"seedipaddr\":\"78.47.196.146\"}" + From 55188bb4753cfe4bc9866bd470fbc22aedfb3e38 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 10:18:05 +0300 Subject: [PATCH 316/339] Test --- iguana/exchanges/LP_nativeDEX.c | 10 ++++++---- iguana/exchanges/mm.c | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 24bf9d0c6..498308c9f 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -27,8 +27,8 @@ struct LP_utxoinfo *LP_utxoinfos; struct LP_peerinfo *LP_peerinfos,*LP_mypeer; char *activecoins[] = { "BTC", "KMD", "REVS", "JUMBLR" };//"LTC", "USD", }; -char GLOBAL_DBDIR[] = "DB"; -char USERPASS[65],USERPASS_WIFSTR[64]; +char GLOBAL_DBDIR[] = { "DB" }; +char USERPASS[65],USERPASS_WIFSTR[64],USERHOME[512] = { "/root" }; char *default_LPnodes[] = { "5.9.253.196", "5.9.253.197", "5.9.253.198", "5.9.253.199", "5.9.253.200", "5.9.253.201", "5.9.253.202", "5.9.253.203", "5.9.253.204" }; //"5.9.253.195", @@ -56,7 +56,7 @@ void tradebot_pendingadd(cJSON *tradejson,char *base,double basevolume,char *rel } char *LP_getdatadir() { - return("/root"); + return(USERHOME); } char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_t skip) @@ -229,12 +229,14 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i } } -void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profitmargin,char *passphrase,int32_t amclient) +void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profitmargin,char *passphrase,int32_t amclient,char *userhome) { char *myipaddr=0; long filesize,n; int32_t timeout,maxsize,pullsock=-1,pubsock=-1; struct LP_peerinfo *mypeer=0; char pushaddr[128],subaddr[128]; IAMCLIENT = amclient; OS_randombytes((void *)&n,sizeof(n)); srand((int32_t)n); + if ( userhome != 0 && userhome[0] != 0 ) + safecopy(USERHOME,userhome,sizeof(USERHOME)); portable_mutex_init(&LP_peermutex); portable_mutex_init(&LP_utxomutex); portable_mutex_init(&LP_commandmutex); diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 24294466b..b2baba05e 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -811,7 +811,7 @@ void LP_main(void *ptr) if ( (passphrase= jstr(argjson,"passphrase")) != 0 ) { profitmargin = jdouble(argjson,"profitmargin"); - LPinit(7779,7780,7781,profitmargin,passphrase,jint(argjson,"client")); + LPinit(7779,7780,7781,profitmargin,passphrase,jint(argjson,"client"),jstr(argjson,"userhome")); } } From 7a1129a3a7e463556aef057ed1d064c709268b6e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 10:26:29 +0300 Subject: [PATCH 317/339] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 498308c9f..e31d540f8 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -135,7 +135,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i portable_mutex_lock(&LP_commandmutex); if ( (retstr= stats_JSON(argjson,"127.0.0.1",mypubport)) != 0 ) { - //printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); + printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); free(retstr); } portable_mutex_unlock(&LP_commandmutex); From 725b938cdc148c1aab93daa3a55f4573095c2d7a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 10:30:41 +0300 Subject: [PATCH 318/339] Test --- iguana/exchanges/LP_nativeDEX.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index e31d540f8..aad9241a2 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -133,7 +133,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i if ( (argjson= cJSON_Parse((char *)ptr)) != 0 ) { portable_mutex_lock(&LP_commandmutex); - if ( (retstr= stats_JSON(argjson,"127.0.0.1",mypubport)) != 0 ) + if ( (retstr= stats_JSON(argjson,"127.0.0.1",0)) != 0 ) { printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); free(retstr); @@ -190,7 +190,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i if ( (argjson= cJSON_Parse((char *)ptr)) != 0 ) { portable_mutex_lock(&LP_commandmutex); - if ( (retstr= stats_JSON(argjson,"127.0.0.1",mypubport)) != 0 ) + if ( (retstr= stats_JSON(argjson,"127.0.0.1",0)) != 0 ) { printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); free(retstr); @@ -211,7 +211,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i portable_mutex_lock(&LP_commandmutex); if ( LP_command(mypeer,pubsock,argjson,&((uint8_t *)ptr)[len],recvsize - len,profitmargin) == 0 ) { - if ( (retstr= stats_JSON(argjson,"127.0.0.1",mypubport)) != 0 ) + if ( (retstr= stats_JSON(argjson,"127.0.0.1",0)) != 0 ) { printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); free(retstr); From 2b14c3f9bc6d08bdd4489c053cfddd9c46acd0c2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 10:33:37 +0300 Subject: [PATCH 319/339] Test --- iguana/exchanges/LP_nativeDEX.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index aad9241a2..8db037e1e 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -161,9 +161,12 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i { HASH_ITER(hh,LP_utxoinfos,utxo,utmp) { - if ( strcmp(utxo->coin,"KMD") == 0 ) - LP_priceping(pubsock,utxo,"BTC",profitmargin); - else LP_priceping(pubsock,utxo,"KMD",profitmargin); + if ( strcmp(utxo->ipaddr,mypeer->ipaddr) == 0 && utxo->port == mypeer->port ) + { + if ( strcmp(utxo->coin,"KMD") == 0 ) + LP_priceping(pubsock,utxo,"BTC",profitmargin); + else LP_priceping(pubsock,utxo,"KMD",profitmargin); + } } } HASH_ITER(hh,LP_peerinfos,peer,tmp) From c44cbd28df6f4e6ab5163341041782ff9b065544 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 10:39:49 +0300 Subject: [PATCH 320/339] Test --- iguana/exchanges/LP_nativeDEX.c | 6 +++--- iguana/exchanges/LP_prices.c | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 8db037e1e..95b94fdd2 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -135,7 +135,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i portable_mutex_lock(&LP_commandmutex); if ( (retstr= stats_JSON(argjson,"127.0.0.1",0)) != 0 ) { - printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); + //printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); free(retstr); } portable_mutex_unlock(&LP_commandmutex); @@ -195,7 +195,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i portable_mutex_lock(&LP_commandmutex); if ( (retstr= stats_JSON(argjson,"127.0.0.1",0)) != 0 ) { - printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); + //printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); free(retstr); } portable_mutex_unlock(&LP_commandmutex); @@ -216,7 +216,7 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i { if ( (retstr= stats_JSON(argjson,"127.0.0.1",0)) != 0 ) { - printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); + //printf("%s RECV.[%d] %s\n",peer->ipaddr,recvsize,(char *)ptr); free(retstr); } } diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index aa3df855b..b18794fe7 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -288,11 +288,11 @@ static int _cmp_orderbookrev(const void *a,const void *b) cJSON *LP_orderbookjson(struct LP_cacheinfo *ptr,int32_t polarity) { - double price,volume; cJSON *item = cJSON_CreateObject(); - if ( (price= ptr->price) != 0. && (volume= dstr(ptr->Q.satoshis)) != 0. ) + double price; cJSON *item = cJSON_CreateObject(); + if ( (price= ptr->price) != 0. ) { jaddnum(item,"price",polarity > 0 ? price : 1. / price); - jaddnum(item,"volume",polarity > 0 ? volume : volume / price); + jaddnum(item,"volume",polarity > 0 ? dstr(ptr->Q.satoshis) : dstr(ptr->Q.destsatoshis)); jaddbits256(item,"txid",ptr->Q.txid); jaddnum(item,"vout",ptr->Q.vout); } @@ -323,13 +323,13 @@ char *LP_orderbook(char *base,char *rel) if ( numbids > 1 ) qsort(bids,numbids,sizeof(*bids),_cmp_orderbook); for (i=0; i 1 ) qsort(asks,numasks,sizeof(*asks),_cmp_orderbookrev); for (i=0; i Date: Sat, 3 Jun 2017 10:47:50 +0300 Subject: [PATCH 321/339] Test --- iguana/exchanges/LP_coins.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/iguana/exchanges/LP_coins.c b/iguana/exchanges/LP_coins.c index 3ea9332d9..f397ee075 100644 --- a/iguana/exchanges/LP_coins.c +++ b/iguana/exchanges/LP_coins.c @@ -72,12 +72,28 @@ void LP_statefname(char *fname,char *symbol,char *assetname,char *str) strcat(fname,"/"); #endif if ( strcmp(symbol,"BTC") == 0 ) + { +#ifdef __APPLE__ + strcat(fname,"Bitcoin"); +#else strcat(fname,".bitcoin"); +#endif + } else if ( strcmp(symbol,"LTC") == 0 ) + { +#ifdef __APPLE__ + strcat(fname,"Litecoin"); +#else strcat(fname,".litecoin"); +#endif + } else { +#ifdef __APPLE__ + strcat(fname,"Komodo"); +#else strcat(fname,".komodo"); +#endif if ( strcmp(symbol,"KMD") != 0 ) { #ifdef WIN32 From 8ac57c2d15de2f5c1888fddf5476845c76af91bc Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 10:55:27 +0300 Subject: [PATCH 322/339] Test --- iguana/exchanges/LP_prices.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index b18794fe7..6f202c070 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -289,8 +289,9 @@ static int _cmp_orderbookrev(const void *a,const void *b) cJSON *LP_orderbookjson(struct LP_cacheinfo *ptr,int32_t polarity) { double price; cJSON *item = cJSON_CreateObject(); - if ( (price= ptr->price) != 0. ) + if ( ptr->Q.satoshis != 0 && ptr->Q.destsatoshis != 0 ) { + price = (double)ptr->Q.destsatoshis / ptr->Q.satoshis; jaddnum(item,"price",polarity > 0 ? price : 1. / price); jaddnum(item,"volume",polarity > 0 ? dstr(ptr->Q.satoshis) : dstr(ptr->Q.destsatoshis)); jaddbits256(item,"txid",ptr->Q.txid); From 64bae2be42b1e3c608afb78f7600995de013333f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 10:59:15 +0300 Subject: [PATCH 323/339] Test --- iguana/exchanges/mm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index b2baba05e..c69d170f0 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -836,7 +836,7 @@ int main(int argc, const char * argv[]) free(retstr); } incr = 100.; - while ( IAMCLIENT != 0 ) + while ( (0) && IAMCLIENT != 0 ) { theoretical = marketmaker_updateprice("komodo","REVS","KMD",theoretical,&incr); sleep(3); From 4ece3f0834db9bd2e4f043d74d2a149217d48c45 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 11:00:52 +0300 Subject: [PATCH 324/339] Test --- iguana/exchanges/LP_prices.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index 6f202c070..d7b29804b 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -322,13 +322,13 @@ char *LP_orderbook(char *base,char *rel) retjson = cJSON_CreateObject(); array = cJSON_CreateArray(); if ( numbids > 1 ) - qsort(bids,numbids,sizeof(*bids),_cmp_orderbook); + qsort(bids,numbids,sizeof(*bids),_cmp_orderbookrev); for (i=0; i 1 ) - qsort(asks,numasks,sizeof(*asks),_cmp_orderbookrev); + qsort(asks,numasks,sizeof(*asks),_cmp_orderbook); for (i=0; i Date: Sat, 3 Jun 2017 11:05:41 +0300 Subject: [PATCH 325/339] Test --- iguana/exchanges/LP_nativeDEX.c | 3 ++- iguana/exchanges/LP_prices.c | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 95b94fdd2..79be79a61 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -36,7 +36,7 @@ portable_mutex_t LP_peermutex,LP_utxomutex,LP_commandmutex,LP_cachemutex; int32_t LP_mypubsock = -1; int32_t Client_connections; int32_t USERPASS_COUNTER,IAMCLIENT = 0; - +double LP_profitratio = 1.; // stubs int32_t basilisk_istrustedbob(struct basilisk_swap *swap) @@ -236,6 +236,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profit { char *myipaddr=0; long filesize,n; int32_t timeout,maxsize,pullsock=-1,pubsock=-1; struct LP_peerinfo *mypeer=0; char pushaddr[128],subaddr[128]; IAMCLIENT = amclient; + LP_profitratio += profitmargin; OS_randombytes((void *)&n,sizeof(n)); srand((int32_t)n); if ( userhome != 0 && userhome[0] != 0 ) diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index d7b29804b..11fe76813 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -144,8 +144,8 @@ int32_t LP_mypriceset(char *base,char *rel,double price) struct LP_priceinfo *basepp,*relpp; if ( price != 0. && (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) { - basepp->myprices[relpp->ind] = price; // ask - relpp->myprices[basepp->ind] = 1. / price; // bid + basepp->myprices[relpp->ind] = price * LP_profitratio; // ask + relpp->myprices[basepp->ind] = (1. / price) * LP_profitratio; // bid return(0); } else return(-1); } From 038d1a019d74f596c3a707166c5a3d145762a433 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 11:25:31 +0300 Subject: [PATCH 326/339] Test --- iguana/exchanges/autotrade | 2 ++ iguana/exchanges/candidates | 2 ++ 2 files changed, 4 insertions(+) create mode 100755 iguana/exchanges/autotrade create mode 100755 iguana/exchanges/candidates diff --git a/iguana/exchanges/autotrade b/iguana/exchanges/autotrade new file mode 100755 index 000000000..8c68b61d0 --- /dev/null +++ b/iguana/exchanges/autotrade @@ -0,0 +1,2 @@ +source userpass +curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"candidates\",\"txid\":\"\",\"vout\":0,\"coin\":\"REVS\",\"maxprice\":1.5}" diff --git a/iguana/exchanges/candidates b/iguana/exchanges/candidates new file mode 100755 index 000000000..ae72e206a --- /dev/null +++ b/iguana/exchanges/candidates @@ -0,0 +1,2 @@ +source userpass +curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"candidates\",\"txid\":\"\",\"vout\":0,\"coin\":\"REVS\"}" From b9c86dfaeab0db5860f4c1d3cd7f816604692499 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 11:31:20 +0300 Subject: [PATCH 327/339] Test --- iguana/exchanges/LP_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index df8233d8e..bb4ea07d7 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -233,9 +233,9 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) if ( (price= LP_price(base,myutxo->coin)) == .0 ) return(0); estimatedbase = myutxo->satoshis / price; + printf("%s -> %s price %.8f mysatoshis %llu estimated base %llu\n",base,myutxo->coin,price,(long long)myutxo->satoshis,(long long)estimatedbase); if ( estimatedbase <= 0 ) return(0); - //printf("%s -> %s price %.8f mysatoshis %llu estimated base %llu\n",base,myutxo->coin,price,(long long)myutxo->satoshis,(long long)estimatedbase); HASH_ITER(hh,LP_peerinfos,peer,tmp) { if ( (utxostr= issue_LP_clientgetutxos(peer->ipaddr,peer->port,base,100)) != 0 ) From 9d5f9f2693253f8fe454a98be2923eec6396b113 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 11:33:05 +0300 Subject: [PATCH 328/339] Test --- iguana/exchanges/LP_commands.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index bb4ea07d7..bafe82548 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -231,7 +231,10 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) { struct LP_peerinfo *peer,*tmp; struct LP_quoteinfo Q; char *utxostr,coinstr[16]; cJSON *array,*icopy,*retarray=0,*item; int32_t i,n; double price; int64_t estimatedbase; if ( (price= LP_price(base,myutxo->coin)) == .0 ) + { + printf("no LP_price\n"); return(0); + } estimatedbase = myutxo->satoshis / price; printf("%s -> %s price %.8f mysatoshis %llu estimated base %llu\n",base,myutxo->coin,price,(long long)myutxo->satoshis,(long long)estimatedbase); if ( estimatedbase <= 0 ) From cde872a56fd2de4d594a91f3abe3403d05dbb8cf Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 11:37:58 +0300 Subject: [PATCH 329/339] Test --- iguana/exchanges/LP_commands.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index bafe82548..36b7c81d2 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -232,7 +232,7 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) struct LP_peerinfo *peer,*tmp; struct LP_quoteinfo Q; char *utxostr,coinstr[16]; cJSON *array,*icopy,*retarray=0,*item; int32_t i,n; double price; int64_t estimatedbase; if ( (price= LP_price(base,myutxo->coin)) == .0 ) { - printf("no LP_price\n"); + printf("no LP_price (%s -> %s)\n",base,myutxo->coin); return(0); } estimatedbase = myutxo->satoshis / price; @@ -583,7 +583,7 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port } else LP_addpeer(amclient,LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos")); } } - printf("CMD.(%s)\n",jprint(argjson,0)); + //printf("CMD.(%s)\n",jprint(argjson,0)); if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) retstr = LP_quotereceived(argjson); else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 ) From 56033e0a90b66e8c8e56b99279ac993a277ecebd Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 11:46:06 +0300 Subject: [PATCH 330/339] Test --- iguana/exchanges/LP_commands.c | 2 ++ iguana/exchanges/LP_nativeDEX.c | 2 +- iguana/exchanges/LP_utxos.c | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 36b7c81d2..8474c5e0b 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -511,6 +511,8 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port USERPASS_COUNTER = 1; retjson = cJSON_CreateObject(); jaddstr(retjson,"userpass",USERPASS); + jaddstr(retjson,"BTC",BTCADDR); + jaddstr(retjson,"KMD",KMDADDR); return(jprint(retjson,1)); } if ( (userpass= jstr(argjson,"userpass")) == 0 || strcmp(userpass,USERPASS) != 0 ) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 79be79a61..dbda8d683 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -28,7 +28,7 @@ struct LP_peerinfo *LP_peerinfos,*LP_mypeer; char *activecoins[] = { "BTC", "KMD", "REVS", "JUMBLR" };//"LTC", "USD", }; char GLOBAL_DBDIR[] = { "DB" }; -char USERPASS[65],USERPASS_WIFSTR[64],USERHOME[512] = { "/root" }; +char USERPASS[65],USERPASS_WIFSTR[64],BTCADDR[64],KMDADDR[64],USERHOME[512] = { "/root" }; char *default_LPnodes[] = { "5.9.253.196", "5.9.253.197", "5.9.253.198", "5.9.253.199", "5.9.253.200", "5.9.253.201", "5.9.253.202", "5.9.253.203", "5.9.253.204" }; //"5.9.253.195", diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 22ba7fc2f..b10a7d93f 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -278,6 +278,10 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb conv_NXTpassword(privkey.bytes,pubkey.bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); else privkey = iguana_wif2privkey(wifstr); iguana_priv2pub(pubkey33,coinaddr,privkey,coin->pubtype); + if ( strcmp("BTC",symbol) == 0 ) + strcpy(BTCADDR,coinaddr); + else if ( strcmp("KMD",symbol) == 0 ) + strcpy(KMDADDR,coinaddr); if ( counter == 0 ) { char tmpstr[128]; From 93184292f8b0c09183620bb7ec87c080b56f8835 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 3 Jun 2017 13:22:58 +0300 Subject: [PATCH 331/339] Test --- iguana/exchanges/LP_commands.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 8474c5e0b..824008714 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -252,6 +252,7 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) for (i=0; i Date: Sat, 3 Jun 2017 13:25:58 +0300 Subject: [PATCH 332/339] Test --- iguana/exchanges/LP_commands.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 824008714..5ea2e6d08 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -252,7 +252,7 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) for (i=0; i Date: Sun, 4 Jun 2017 11:32:41 +0300 Subject: [PATCH 333/339] Test --- iguana/exchanges/LP_commands.c | 5 ++--- iguana/exchanges/autotrade | 2 +- iguana/exchanges/candidates | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 5ea2e6d08..023272a96 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -300,7 +300,6 @@ cJSON *LP_autotrade(struct LP_utxoinfo *myutxo,char *base,double maxprice) if ( (price= jdouble(item,"price")) == 0. ) { LP_quoteparse(&Q[i],item); - //char str[65]; printf("i.%d of %d: (%s) -> txid.%s\n",i,n,jprint(item,0),bits256_str(str,Q[i].txid)); price = LP_query("price",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,zero); if ( Q[i].destsatoshis != 0 && (double)j64bits(item,"satoshis")/Q[i].destsatoshis > price ) { @@ -310,7 +309,7 @@ cJSON *LP_autotrade(struct LP_utxoinfo *myutxo,char *base,double maxprice) } if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) ) bestprice = price; - //printf("i.%d price %.8f bestprice %.8f: (%s)\n",i,price,bestprice,jprint(item,0)); + char str[65]; printf("i.%d of %d: (%s) -> txid.%s price %.8f best %.8f\n",i,n,jprint(item,0),bits256_str(str,Q[i].txid),price,bestprice); } if ( bestprice != 0. ) { @@ -498,7 +497,7 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin return(retval); } -// add orderbook api +// addcoin api char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port { diff --git a/iguana/exchanges/autotrade b/iguana/exchanges/autotrade index 8c68b61d0..d519ab735 100755 --- a/iguana/exchanges/autotrade +++ b/iguana/exchanges/autotrade @@ -1,2 +1,2 @@ source userpass -curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"candidates\",\"txid\":\"\",\"vout\":0,\"coin\":\"REVS\",\"maxprice\":1.5}" +curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"candidates\",\"txid\":\"$1\",\"vout\":$2,\"coin\":\"REVS\",\"maxprice\":$3}" diff --git a/iguana/exchanges/candidates b/iguana/exchanges/candidates index ae72e206a..ec3e2c1a1 100755 --- a/iguana/exchanges/candidates +++ b/iguana/exchanges/candidates @@ -1,2 +1,2 @@ source userpass -curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"candidates\",\"txid\":\"\",\"vout\":0,\"coin\":\"REVS\"}" +curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"candidates\",\"txid\":\"$1\",\"vout\":$2,\"coin\":\"REVS\"}" From f5b6f7c8e72cb91343ee4bab6946c32e242c7df3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 4 Jun 2017 11:36:31 +0300 Subject: [PATCH 334/339] Test --- iguana/exchanges/LP_commands.c | 1 + iguana/exchanges/autotrade | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 023272a96..9e084f325 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -290,6 +290,7 @@ cJSON *LP_autotrade(struct LP_utxoinfo *myutxo,char *base,double maxprice) maxprice = LP_price(base,myutxo->coin) / 0.975; if ( (array= LP_tradecandidates(myutxo,base)) != 0 ) { + printf("candidates.(%s)\n",jprint(array,0)); if ( (n= cJSON_GetArraySize(array)) > 0 ) { memset(prices,0,sizeof(prices)); diff --git a/iguana/exchanges/autotrade b/iguana/exchanges/autotrade index d519ab735..c24a0a89a 100755 --- a/iguana/exchanges/autotrade +++ b/iguana/exchanges/autotrade @@ -1,2 +1,2 @@ source userpass -curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"candidates\",\"txid\":\"$1\",\"vout\":$2,\"coin\":\"REVS\",\"maxprice\":$3}" +curl --url "http://127.0.0.1:7779" --data "{\"userpass\":\"$userpass\",\"method\":\"autotrade\",\"txid\":\"$1\",\"vout\":$2,\"coin\":\"REVS\",\"maxprice\":$3}" From bc023bc8d6660b6a358452bdda64aa2279ebf925 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 4 Jun 2017 13:06:13 +0300 Subject: [PATCH 335/339] Test --- iguana/exchanges/LP_commands.c | 49 +++++++++++++++++----------- iguana/exchanges/LP_transaction.c | 53 ++++++++++++++++--------------- 2 files changed, 58 insertions(+), 44 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 9e084f325..fba1e4444 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -498,6 +498,34 @@ int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uin return(retval); } +char *LP_connected(cJSON *argjson) +{ + cJSON *retjson; int32_t pairsock = -1; char *pairstr; struct LP_quoteinfo *qp; int32_t DEXselector = 0; + retjson = cJSON_CreateObject(); + if ( IAMCLIENT == 0 ) + jaddstr(retjson,"result","update stats"); + else + { + if ( (pairstr= jstr(argjson,"pair")) == 0 || (pairsock= nn_socket(AF_SP,NN_PAIR)) < 0 ) + jaddstr(retjson,"error","couldnt create pairsock"); + else if ( nn_connect(pairsock,pairstr) >= 0 ) + { + qp = calloc(1,sizeof(*qp)); + LP_quoteparse(qp,argjson); + qp->pair = pairsock; + qp->privkey = LP_privkey(qp->destaddr); + LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis,qp->destcoin,qp->destsatoshis,qp->timestamp,qp->quotetime,DEXselector); + printf("alice pairstr.(%s)\n",pairstr); + if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_aliceloop,(void *)qp) == 0 ) + { + jaddstr(retjson,"result","success"); + jadd(retjson,"trade",LP_quotejson(qp)); + } else jaddstr(retjson,"error","couldnt aliceloop"); + } + } + return(jprint(retjson,1)); +} + // addcoin api char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port @@ -589,25 +617,8 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port //printf("CMD.(%s)\n",jprint(argjson,0)); if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 ) retstr = LP_quotereceived(argjson); - else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 ) - { - int32_t pairsock = -1; char *pairstr; - if ( (pairstr= jstr(argjson,"pair")) == 0 || (pairsock= nn_socket(AF_SP,NN_PAIR)) < 0 ) - printf("error creating pairsock\n"); - else if ( nn_connect(pairsock,pairstr) >= 0 ) - { - struct LP_quoteinfo *qp; int32_t DEXselector = 0; - qp = calloc(1,sizeof(*qp)); - LP_quoteparse(qp,argjson); - qp->pair = pairsock; - qp->privkey = LP_privkey(qp->destaddr); - LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis,qp->destcoin,qp->destsatoshis,qp->timestamp,qp->quotetime,DEXselector); - printf("alice pairstr.(%s)\n",pairstr); - if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_aliceloop,(void *)qp) == 0 ) - { - } - } - } + else if ( strcmp(method,"connected") == 0 ) + retstr = LP_connected(argjson); else if ( strcmp(method,"getprice") == 0 ) retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel")); else if ( strcmp(method,"orderbook") == 0 ) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 63ca5beee..2e710b7af 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1163,7 +1163,8 @@ int32_t basilisk_bobdeposit_refund(struct basilisk_swap *swap,int32_t delay) int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,int32_t genflag) { - int32_t j; //char str[65]; + int32_t j; char coinaddr[64]; + bitcoin_address(coinaddr,swap->bobcoin.pubtype,swap->changermd160,20); if ( genflag != 0 && swap->I.iambob == 0 ) printf("basilisk_bobscripts_set WARNING: alice generating BOB tx\n"); if ( depositflag == 0 ) @@ -1176,7 +1177,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i printf(" <- bobpayment redeem %d %s\n",i,swap->bobpayment.I.destaddr); if ( genflag != 0 && bits256_nonz(*(bits256 *)swap->I.secretBn256) != 0 && swap->bobpayment.I.datalen == 0 ) { - basilisk_rawtx_gen(swap->ctx,"payment",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobpayment,swap->bobpayment.I.locktime,swap->bobpayment.spendscript,swap->bobpayment.I.spendlen,swap->bobpayment.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0); + basilisk_rawtx_gen(swap->ctx,"payment",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobpayment,swap->bobpayment.I.locktime,swap->bobpayment.spendscript,swap->bobpayment.I.spendlen,swap->bobpayment.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,coinaddr); if ( swap->bobpayment.I.spendlen == 0 || swap->bobpayment.I.datalen == 0 ) { printf("error bob generating %p payment.%d\n",swap->bobpayment.txbytes,swap->bobpayment.I.spendlen); @@ -1209,7 +1210,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i printf(" <- bobdeposit redeem %d %s\n",i,swap->bobdeposit.I.destaddr); if ( genflag != 0 && (swap->bobdeposit.I.datalen == 0 || swap->bobrefund.I.datalen == 0) ) { - basilisk_rawtx_gen(swap->ctx,"deposit",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobdeposit,swap->bobdeposit.I.locktime,swap->bobdeposit.spendscript,swap->bobdeposit.I.spendlen,swap->bobdeposit.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0); + basilisk_rawtx_gen(swap->ctx,"deposit",swap->I.started,swap->persistent_pubkey33,1,1,&swap->bobdeposit,swap->bobdeposit.I.locktime,swap->bobdeposit.spendscript,swap->bobdeposit.I.spendlen,swap->bobdeposit.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,coinaddr); if ( swap->bobdeposit.I.datalen == 0 || swap->bobdeposit.I.spendlen == 0 ) { printf("error bob generating %p deposit.%d\n",swap->bobdeposit.txbytes,swap->bobdeposit.I.spendlen); @@ -1291,38 +1292,40 @@ int32_t basilisk_alicepayment_spend(struct basilisk_swap *swap,struct basilisk_r void basilisk_alicepayment(struct basilisk_swap *swap,struct iguana_info *coin,struct basilisk_rawtx *alicepayment,bits256 pubAm,bits256 pubBn) { + int32_t i; char coinaddr[64]; alicepayment->I.spendlen = basilisk_alicescript(alicepayment->redeemscript,&alicepayment->I.redeemlen,alicepayment->spendscript,0,alicepayment->I.destaddr,coin->p2shtype,pubAm,pubBn); - basilisk_rawtx_gen(swap->ctx,"alicepayment",swap->I.started,swap->persistent_pubkey33,0,1,alicepayment,alicepayment->I.locktime,alicepayment->spendscript,alicepayment->I.spendlen,coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0); + for (i=0; i<33; i++) + printf("%02x",swap->persistent_pubkey33[i]); + printf(" pubkey33, "); + for (i=0; i<20; i++) + printf("%02x",swap->changermd160[i]); + printf(" rmd160, "); + bitcoin_address(coinaddr,coin->pubtype,swap->changermd160,20); + printf("%s\n",coinaddr); + basilisk_rawtx_gen(swap->ctx,"alicepayment",swap->I.started,swap->persistent_pubkey33,0,1,alicepayment,alicepayment->I.locktime,alicepayment->spendscript,alicepayment->I.spendlen,coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,coinaddr); } int32_t basilisk_alicetxs(int32_t pairsock,struct basilisk_swap *swap,uint8_t *data,int32_t maxlen) { - int32_t i,retval = -1; - printf("alicetxs\n"); - for (i=0; i<3; i++) + char coinaddr[64]; int32_t i,retval = -1; + if ( swap->alicepayment.I.datalen == 0 ) + basilisk_alicepayment(swap,swap->alicepayment.coin,&swap->alicepayment,swap->I.pubAm,swap->I.pubBn); + if ( swap->alicepayment.I.datalen == 0 || swap->alicepayment.I.spendlen == 0 ) + printf("error alice generating payment.%d\n",swap->alicepayment.I.spendlen); + else { - if ( swap->alicepayment.I.datalen == 0 ) - basilisk_alicepayment(swap,swap->alicepayment.coin,&swap->alicepayment,swap->I.pubAm,swap->I.pubBn); - if ( swap->alicepayment.I.datalen == 0 || swap->alicepayment.I.spendlen == 0 ) - { - printf("error alice generating payment.%d\n",swap->alicepayment.I.spendlen); - sleep(20); - } - else - { - retval = 0; - for (i=0; ialicepayment.I.datalen; i++) - printf("%02x",swap->alicepayment.txbytes[i]); - printf(" ALICE PAYMENT created\n"); - LP_unspents_mark(swap->alicecoin.symbol,swap->alicepayment.vins); - //basilisk_txlog(swap,&swap->alicepayment,-1); - break; - } + retval = 0; + for (i=0; ialicepayment.I.datalen; i++) + printf("%02x",swap->alicepayment.txbytes[i]); + printf(" ALICE PAYMENT created\n"); + LP_unspents_mark(swap->alicecoin.symbol,swap->alicepayment.vins); + //basilisk_txlog(swap,&swap->alicepayment,-1); } if ( swap->myfee.I.datalen == 0 ) { //printf("generate fee\n"); - if ( basilisk_rawtx_gen(swap->ctx,"myfee",swap->I.started,swap->persistent_pubkey33,swap->I.iambob,1,&swap->myfee,0,swap->myfee.spendscript,swap->myfee.I.spendlen,swap->myfee.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,0) == 0 ) + bitcoin_address(coinaddr,swap->alicecoin.pubtype,swap->changermd160,20); + if ( basilisk_rawtx_gen(swap->ctx,"myfee",swap->I.started,swap->persistent_pubkey33,swap->I.iambob,1,&swap->myfee,0,swap->myfee.spendscript,swap->myfee.I.spendlen,swap->myfee.coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,coinaddr) == 0 ) { swap->I.statebits |= LP_swapdata_rawtxsend(pairsock,swap,0x80,data,maxlen,&swap->myfee,0x40,0); LP_unspents_mark(swap->I.iambob!=0?swap->bobcoin.symbol:swap->alicecoin.symbol,swap->myfee.vins); From 9ab45e5117b4a3ab9b6206a0b16ca6070fe2fb14 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 4 Jun 2017 13:12:19 +0300 Subject: [PATCH 336/339] Test --- iguana/exchanges/LP_commands.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index fba1e4444..8b7eac1a6 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -298,9 +298,9 @@ cJSON *LP_autotrade(struct LP_utxoinfo *myutxo,char *base,double maxprice) for (i=0; icoin,zero); if ( Q[i].destsatoshis != 0 && (double)j64bits(item,"satoshis")/Q[i].destsatoshis > price ) { @@ -321,9 +321,11 @@ cJSON *LP_autotrade(struct LP_utxoinfo *myutxo,char *base,double maxprice) if ( (price= prices[i]) != 0. && Q[i].destsatoshis != 0 ) { metric = price / bestprice; + printf("%f ",metric); if ( metric > 0.9 ) { metric = Q[i].destsatoshis / metric * metric * metric; + printf("%f, ",metric); if ( metric > bestmetric ) { besti = i; @@ -332,12 +334,15 @@ cJSON *LP_autotrade(struct LP_utxoinfo *myutxo,char *base,double maxprice) } } } + printf("metrics, best %f\n",bestmetric); if ( besti >= 0 )//&& bits256_cmp(myutxo->mypub,otherpubs[besti]) == 0 ) { - item = jitem(array,besti); i = besti; + bestprice = prices[i]; + item = jitem(array,i); bestitem = LP_quotejson(&Q[i]); - if ( bestprice <= maxprice ) + printf("bestprice %f vs maxprice %f\n",bestprice,maxprice); + if ( maxprice == 0. || bestprice <= maxprice ) { Q[i].desttxid = myutxo->txid; Q[i].destvout = myutxo->vout; @@ -347,7 +352,7 @@ cJSON *LP_autotrade(struct LP_utxoinfo *myutxo,char *base,double maxprice) price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,myutxo->coin,myutxo->mypub); if ( jobj(bestitem,"price") != 0 ) jdelete(bestitem,"price"); - jaddnum(bestitem,"price",prices[besti]); + jaddnum(bestitem,"price",prices[i]); if ( price <= maxprice ) { Q[i].desttxid = myutxo->txid; From d90cd5c9a98b325dd576cc5604276ca6625b373e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 4 Jun 2017 13:27:46 +0300 Subject: [PATCH 337/339] Test --- iguana/exchanges/LP_remember.c | 8 ++++---- iguana/exchanges/LP_transaction.c | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 0871e1fdd..bf9d34b82 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -630,7 +630,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti } for (j=0; j<32; j++) rev.bytes[j] = myprivs[0].bytes[31 - j]; - if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0,bobpaymentaddr)) != 0 ) + if ( (txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"alicespend",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,1,expiration,&values[BASILISK_ALICESPEND],0,0,bobpaymentaddr,1)) != 0 ) printf("alicespend.(%s)\n",txbytes[BASILISK_ALICESPEND]); } } @@ -655,7 +655,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( redeemlen > 0 ) { len = basilisk_swapuserdata(userdata,zero,1,myprivs[0],redeemscript,redeemlen); - if ( (txbytes[BASILISK_ALICECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"aliceclaim",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,0,pubkey33,0,expiration,&values[BASILISK_ALICECLAIM],0,0,bobdepositaddr)) != 0 ) + if ( (txbytes[BASILISK_ALICECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"aliceclaim",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,0,pubkey33,0,expiration,&values[BASILISK_ALICECLAIM],0,0,bobdepositaddr,1)) != 0 ) printf("privBn.(%s) aliceclaim.(%s)\n",bits256_str(str,privBn),txbytes[BASILISK_ALICECLAIM]); } } @@ -731,7 +731,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( redeemlen > 0 ) { len = basilisk_swapuserdata(userdata,zero,1,myprivs[1],redeemscript,redeemlen); - if ( (txbytes[BASILISK_BOBRECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[1],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,0,expiration,&values[BASILISK_BOBRECLAIM],0,0,bobpaymentaddr)) != 0 ) + if ( (txbytes[BASILISK_BOBRECLAIM]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[1],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBPAYMENT],0,0,pubkey33,0,expiration,&values[BASILISK_BOBRECLAIM],0,0,bobpaymentaddr,1)) != 0 ) { int32_t z; for (z=0; z<20; z++) @@ -761,7 +761,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti vcalc_sha256(0,secretBn256,privBn.bytes,sizeof(privBn)); redeemlen = basilisk_swap_bobredeemscript(1,&secretstart,redeemscript,dlocktime,pubA0,pubB0,pubB1,privAm,privBn,secretAm,secretAm256,secretBn,secretBn256); len = basilisk_swapuserdata(userdata,privBn,0,myprivs[0],redeemscript,redeemlen); - if ( (txbytes[BASILISK_BOBREFUND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,0,pubkey33,1,expiration,&values[BASILISK_BOBREFUND],0,0,bobdepositaddr)) != 0 ) + if ( (txbytes[BASILISK_BOBREFUND]= basilisk_swap_bobtxspend(&signedtxid,txfee,"bobrefund",bobcoin,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,myprivs[0],0,redeemscript,redeemlen,userdata,len,txids[BASILISK_BOBDEPOSIT],0,0,pubkey33,1,expiration,&values[BASILISK_BOBREFUND],0,0,bobdepositaddr,1)) != 0 ) printf("pubB1.(%s) bobrefund.(%s)\n",bits256_str(str,pubB1),txbytes[BASILISK_BOBREFUND]); } if ( txbytes[BASILISK_BOBREFUND] != 0 ) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index 2e710b7af..f891f25bd 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -557,9 +557,9 @@ int32_t iguana_signrawtransaction(void *ctx,char *symbol,uint8_t pubtype,uint8_t return(complete); } -char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr,char *vinaddr) +char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,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 vout,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) { - 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,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1,suppress_pubkeys=1; struct vin_info V[16]; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value,change = 0; struct iguana_msgtx msgtx; + 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,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1; struct vin_info V[16]; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value,change = 0; struct iguana_msgtx msgtx; *destamountp = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); if ( finalseqid == 0 ) @@ -658,12 +658,12 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch printf("couldnt sign transaction.%s %s\n",name,bits256_str(str,*signedtxidp)); else if ( completed == 0 ) { - printf("incomplete signing %s (%s)\n",name,jprint(vins,0)); + printf("incomplete signing suppress.%d %s (%s)\n",suppress_pubkeys,name,jprint(vins,0)); if ( signedtx != 0 ) free(signedtx), signedtx = 0; } else printf("%s -> %s\n",name,bits256_str(str,*signedtxidp)); free(rawtxbytes); - } else printf("error making rawtx\n"); + } else printf("error making rawtx suppress.%d\n",suppress_pubkeys); free_json(privkeys); free_json(txobj); return(signedtx); @@ -683,7 +683,7 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub } for (iter=0; iter<2; iter++) { - if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,str,coin->symbol,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,ctx,privkey,0,0,0,0,0,rawtx->utxotxid,rawtx->utxovout,rawtx->I.destaddr,pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr,vinaddr)) != 0 ) + if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,str,coin->symbol,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,ctx,privkey,0,0,0,0,0,rawtx->utxotxid,rawtx->utxovout,rawtx->I.destaddr,pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr,vinaddr,rawtx->I.suppress_pubkeys)) != 0 ) { rawtx->I.datalen = (int32_t)strlen(signedtx) >> 1; if ( rawtx->I.datalen <= sizeof(rawtx->txbytes) ) @@ -722,7 +722,7 @@ int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_ } for (iter=0; iter<2; iter++) { - if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,rawtx->name,symbol,pubtype,p2shtype,isPoS,wiftype,swap->ctx,privkey,privkey2,0,0,userdata,userdatalen,rawtx->utxotxid,rawtx->utxovout,dest->p2shaddr,rawtx->I.pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr,vinaddr)) != 0 ) + if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,rawtx->name,symbol,pubtype,p2shtype,isPoS,wiftype,swap->ctx,privkey,privkey2,0,0,userdata,userdatalen,rawtx->utxotxid,rawtx->utxovout,dest->p2shaddr,rawtx->I.pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr,vinaddr,rawtx->I.suppress_pubkeys)) != 0 ) { rawtx->I.datalen = (int32_t)strlen(signedtx) >> 1; if ( rawtx->I.datalen <= sizeof(rawtx->txbytes) ) @@ -778,7 +778,7 @@ char *basilisk_swap_Aspend(char *name,char *symbol,uint8_t pubtype,uint8_t p2sht for (i=0; i<32; i++) privBn.bytes[i] = rev.bytes[31 - i];*/ txfee = LP_txfee(symbol); - signedtx = basilisk_swap_bobtxspend(&signedtxid,txfee,name,symbol,pubtype,p2shtype,isPoS,wiftype,ctx,privAm,&privBn,redeemscript,redeemlen,0,0,utxotxid,vout,0,pubkey33,1,expiration,destamountp,0,0,vinaddr); + signedtx = basilisk_swap_bobtxspend(&signedtxid,txfee,name,symbol,pubtype,p2shtype,isPoS,wiftype,ctx,privAm,&privBn,redeemscript,redeemlen,0,0,utxotxid,vout,0,pubkey33,1,expiration,destamountp,0,0,vinaddr,0); } return(signedtx); } @@ -1301,7 +1301,7 @@ void basilisk_alicepayment(struct basilisk_swap *swap,struct iguana_info *coin,s printf("%02x",swap->changermd160[i]); printf(" rmd160, "); bitcoin_address(coinaddr,coin->pubtype,swap->changermd160,20); - printf("%s\n",coinaddr); + printf("%s suppress.%d fee.%d\n",coinaddr,alicepayment->I.suppress_pubkeys,swap->myfee.I.suppress_pubkeys); basilisk_rawtx_gen(swap->ctx,"alicepayment",swap->I.started,swap->persistent_pubkey33,0,1,alicepayment,alicepayment->I.locktime,alicepayment->spendscript,alicepayment->I.spendlen,coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,coinaddr); } From 6cde3ced014d1cc8d5b0e78b1b698bb942b7f556 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 4 Jun 2017 13:45:06 +0300 Subject: [PATCH 338/339] Test --- iguana/exchanges/LP_swap.c | 5 ++++- iguana/exchanges/LP_transaction.c | 10 +++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 5d4a085d8..1fa5d4725 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -406,12 +406,13 @@ int32_t LP_waitfor(int32_t pairsock,struct basilisk_swap *swap,int32_t timeout,i { if ( (datalen= nn_recv(pairsock,&data,NN_MSG,0)) >= 0 ) { - printf("received %d bytes\n",datalen); + printf("wait for got.%d\n",datalen); retval = (*verify)(swap,data,datalen); nn_freemsg(data); return(retval); } else printf("error nn_recv\n"); } + printf("waitfor timedout\n"); return(retval); } @@ -551,6 +552,7 @@ void LP_bobloop(void *_utxo) printf("error bobscripts payment\n"); else if ( LP_swapdata_rawtxsend(utxo->pair,swap,0x8000,data,maxlen,&swap->bobpayment,0x4000,0) == 0 ) printf("error sending bobpayment\n"); + printf("looping on swaplist\n"); while ( 1 ) { if ( (retstr= basilisk_swaplist()) != 0 ) @@ -598,6 +600,7 @@ void LP_aliceloop(void *_qp) printf("error sending alicepayment\n"); else if ( LP_waitfor(qp->pair,swap,10,LP_verify_bobpayment) < 0 ) printf("error waiting for bobpayment\n"); + printf("looping on swaplist\n"); while ( 1 ) { if ( (retstr= basilisk_swaplist()) != 0 ) diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index f891f25bd..c25628476 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1443,17 +1443,17 @@ int32_t LP_verify_bobdeposit(struct basilisk_swap *swap,uint8_t *data,int32_t da memcpy(swap->I.userdata_aliceclaim,userdata,len); swap->I.userdata_aliceclaimlen = len; retval = 0; - //if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->aliceclaim,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160)) == 0 ) + if ( (retval= basilisk_rawtx_sign(swap->bobcoin.symbol,swap->bobcoin.pubtype,swap->bobcoin.p2shtype,swap->bobcoin.isPoS,swap->bobcoin.wiftype,swap,&swap->aliceclaim,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160,swap->bobdeposit.I.destaddr)) == 0 ) { 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"); + for (i=0; ialiceclaim.I.datalen; i++) + printf("%02x",swap->aliceclaim.txbytes[i]); + printf(" <- aliceclaim\n"); //basilisk_txlog(swap,&swap->aliceclaim,swap->I.putduration+swap->I.callduration); return(retval); - } //else printf("error signing aliceclaim\n"); + } else printf("error signing aliceclaim\n"); } printf("error with bobdeposit\n"); return(retval); From 477f14c313acc9f4c7a5ce2f2f57c76902f1d1d8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 4 Jun 2017 14:00:47 +0300 Subject: [PATCH 339/339] Test --- iguana/exchanges/LP_swap.c | 30 ++++++++++++++++++------------ iguana/exchanges/LP_transaction.c | 5 ++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 1fa5d4725..b6e91d625 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -552,15 +552,18 @@ void LP_bobloop(void *_utxo) printf("error bobscripts payment\n"); else if ( LP_swapdata_rawtxsend(utxo->pair,swap,0x8000,data,maxlen,&swap->bobpayment,0x4000,0) == 0 ) printf("error sending bobpayment\n"); - printf("looping on swaplist\n"); - while ( 1 ) + else { - if ( (retstr= basilisk_swaplist()) != 0 ) + printf("looping on swaplist\n"); + while ( 1 ) { - printf("%s\n",retstr); - free(retstr); + if ( (retstr= basilisk_swaplist()) != 0 ) + { + printf("%s\n",retstr); + free(retstr); + } + sleep(100); } - sleep(100); } } basilisk_swap_finished(swap); @@ -600,15 +603,18 @@ void LP_aliceloop(void *_qp) printf("error sending alicepayment\n"); else if ( LP_waitfor(qp->pair,swap,10,LP_verify_bobpayment) < 0 ) printf("error waiting for bobpayment\n"); - printf("looping on swaplist\n"); - while ( 1 ) + else { - if ( (retstr= basilisk_swaplist()) != 0 ) + printf("looping on swaplist\n"); + while ( 1 ) { - printf("%s\n",retstr); - free(retstr); + if ( (retstr= basilisk_swaplist()) != 0 ) + { + printf("%s\n",retstr); + free(retstr); + } + sleep(100); } - sleep(100); } } basilisk_swap_finished(swap); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index c25628476..193c20475 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -720,6 +720,9 @@ int32_t basilisk_rawtx_sign(char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_ bitcoin_address(changeaddr,pubtype,changermd160,20); printf("changeaddr.(%s)\n",changeaddr); } + for (iter=0; iter<33; iter++) + printf("%02x",rawtx->I.pubkey33[iter]); + printf(" pubkey33.%s, suppress.%d\n",rawtx->name,rawtx->I.suppress_pubkeys); for (iter=0; iter<2; iter++) { if ( (signedtx= basilisk_swap_bobtxspend(&rawtx->I.signedtxid,iter == 0 ? txfee : newtxfee,rawtx->name,symbol,pubtype,p2shtype,isPoS,wiftype,swap->ctx,privkey,privkey2,0,0,userdata,userdatalen,rawtx->utxotxid,rawtx->utxovout,dest->p2shaddr,rawtx->I.pubkey33,1,0,&destamount,rawtx->I.amount,changeaddr,vinaddr,rawtx->I.suppress_pubkeys)) != 0 ) @@ -1453,7 +1456,7 @@ int32_t LP_verify_bobdeposit(struct basilisk_swap *swap,uint8_t *data,int32_t da printf(" <- aliceclaim\n"); //basilisk_txlog(swap,&swap->aliceclaim,swap->I.putduration+swap->I.callduration); return(retval); - } else printf("error signing aliceclaim\n"); + } else printf("error signing aliceclaim suppress.%d vin.(%s)\n",swap->aliceclaim.I.suppress_pubkeys,swap->bobdeposit.I.destaddr); } printf("error with bobdeposit\n"); return(retval);