Browse Source

Merge branch 'master' into etomic

# Conflicts:
#	iguana/exchanges/beertest
#	iguana/exchanges/coins
#	iguana/exchanges/mm.c
etomic
jl777 7 years ago
parent
commit
2eb2ebedd5
  1. BIN
      OSlibs/android/lib/libcrypto.so
  2. BIN
      OSlibs/android/lib/libcurl.a
  3. BIN
      OSlibs/android/lib/libssl.so
  4. BIN
      OSlibs/ios/lib/libcrypto.a
  5. BIN
      OSlibs/ios/lib/libcurl.a
  6. BIN
      OSlibs/ios/lib/libssl.a
  7. BIN
      OSlibs/js/libnanomsg.a
  8. BIN
      OSlibs/js/libnanomsg.so
  9. BIN
      OSlibs/linux/x86_64/libnanomsg-static.a
  10. BIN
      OSlibs/osx/libcrypto.a
  11. BIN
      OSlibs/osx/libcurl.a
  12. BIN
      OSlibs/osx/libgmp.a
  13. BIN
      OSlibs/osx/libsecp256k1.a
  14. BIN
      OSlibs/osx/libssl.a
  15. BIN
      OSlibs/win/libcrypto-1_1.dll
  16. BIN
      OSlibs/win/libcrypto.a
  17. BIN
      OSlibs/win/libcurl.a
  18. BIN
      OSlibs/win/libcurl.dll
  19. BIN
      OSlibs/win/libcurldll.a
  20. BIN
      OSlibs/win/libpthreadGC2.a
  21. BIN
      OSlibs/win/libpthreadGC2_64.a
  22. BIN
      OSlibs/win/libsecp256k1.a
  23. BIN
      OSlibs/win/libssl-1_1.dll
  24. BIN
      OSlibs/win/libssl.a
  25. BIN
      OSlibs/win/nanomsg.dll
  26. BIN
      OSlibs/win/pthreadGC2.dll
  27. BIN
      OSlibs/win/pthreadGC2_64.dll
  28. BIN
      OSlibs/win/release/nanomsg.dll
  29. BIN
      OSlibs/win/release/pthreadvc2.dll
  30. BIN
      OSlibs/win/x64/libcurl.dll
  31. BIN
      OSlibs/win/x64/libpthreadGC2.a
  32. BIN
      OSlibs/win/x64/nanomsg.dll
  33. BIN
      OSlibs/win/x64/pthread_lib.lib
  34. BIN
      OSlibs/win/x64/release/libcurl.dll
  35. BIN
      OSlibs/win/x64/release/nanomsg.dll
  36. 5
      crypto777/inet.c
  37. BIN
      crypto777/pnacl_libs/libcrypto.a
  38. BIN
      crypto777/pnacl_libs/libcurl.a
  39. BIN
      crypto777/pnacl_libs/libnanomsg.a
  40. BIN
      crypto777/pnacl_libs/libssl.a
  41. BIN
      crypto777/pnacl_libs/libz.a
  42. 20490
      deprecated/obsolete.h
  43. BIN
      iguana/Kashi/json_extracta
  44. BIN
      iguana/dexscripts.win32/curl.exe
  45. 4
      iguana/exchanges/LP_cache.c
  46. 3
      iguana/exchanges/LP_coins.c
  47. 16
      iguana/exchanges/LP_commands.c
  48. 25
      iguana/exchanges/LP_include.h
  49. 3
      iguana/exchanges/LP_instantdex.c
  50. 2
      iguana/exchanges/LP_mmjson.c
  51. 122
      iguana/exchanges/LP_nativeDEX.c
  52. 342
      iguana/exchanges/LP_network.c
  53. 253
      iguana/exchanges/LP_ordermatch.c
  54. 103
      iguana/exchanges/LP_peers.c
  55. 3
      iguana/exchanges/LP_prices.c
  56. 3
      iguana/exchanges/LP_privkey.c
  57. 30
      iguana/exchanges/LP_remember.c
  58. 2
      iguana/exchanges/LP_rpc.c
  59. 18
      iguana/exchanges/LP_signatures.c
  60. 6
      iguana/exchanges/LP_socket.c
  61. 2
      iguana/exchanges/LP_statemachine.c
  62. 8
      iguana/exchanges/LP_stats.c
  63. 19
      iguana/exchanges/LP_swap.c
  64. 89
      iguana/exchanges/LP_transaction.c
  65. 38
      iguana/exchanges/LP_utxo.c
  66. 4
      iguana/exchanges/beertest
  67. 4
      iguana/exchanges/coins
  68. 14
      iguana/exchanges/mm.c
  69. 4
      iguana/exchanges/reset_mutex
  70. 46
      iguana/exchanges/stats.c
  71. BIN
      iguana/pthreadvc2.dll
  72. BIN
      pthreadVC2.lib
  73. BIN
      pthreadvc2.dll

BIN
OSlibs/android/lib/libcrypto.so

Binary file not shown.

BIN
OSlibs/android/lib/libcurl.a

Binary file not shown.

BIN
OSlibs/android/lib/libssl.so

Binary file not shown.

BIN
OSlibs/ios/lib/libcrypto.a

Binary file not shown.

BIN
OSlibs/ios/lib/libcurl.a

Binary file not shown.

BIN
OSlibs/ios/lib/libssl.a

Binary file not shown.

BIN
OSlibs/js/libnanomsg.a

Binary file not shown.

BIN
OSlibs/js/libnanomsg.so

Binary file not shown.

BIN
OSlibs/linux/x86_64/libnanomsg-static.a

Binary file not shown.

BIN
OSlibs/osx/libcrypto.a

Binary file not shown.

BIN
OSlibs/osx/libcurl.a

Binary file not shown.

BIN
OSlibs/osx/libgmp.a

Binary file not shown.

BIN
OSlibs/osx/libsecp256k1.a

Binary file not shown.

BIN
OSlibs/osx/libssl.a

Binary file not shown.

BIN
OSlibs/win/libcrypto-1_1.dll

Binary file not shown.

BIN
OSlibs/win/libcrypto.a

Binary file not shown.

BIN
OSlibs/win/libcurl.a

Binary file not shown.

BIN
OSlibs/win/libcurl.dll

Binary file not shown.

BIN
OSlibs/win/libcurldll.a

Binary file not shown.

BIN
OSlibs/win/libpthreadGC2.a

Binary file not shown.

BIN
OSlibs/win/libpthreadGC2_64.a

Binary file not shown.

BIN
OSlibs/win/libsecp256k1.a

Binary file not shown.

BIN
OSlibs/win/libssl-1_1.dll

Binary file not shown.

BIN
OSlibs/win/libssl.a

Binary file not shown.

BIN
OSlibs/win/nanomsg.dll

Binary file not shown.

BIN
OSlibs/win/pthreadGC2.dll

Binary file not shown.

BIN
OSlibs/win/pthreadGC2_64.dll

Binary file not shown.

BIN
OSlibs/win/release/nanomsg.dll

Binary file not shown.

BIN
OSlibs/win/release/pthreadvc2.dll

Binary file not shown.

BIN
OSlibs/win/x64/libcurl.dll

Binary file not shown.

BIN
OSlibs/win/x64/libpthreadGC2.a

Binary file not shown.

BIN
OSlibs/win/x64/nanomsg.dll

Binary file not shown.

BIN
OSlibs/win/x64/pthread_lib.lib

Binary file not shown.

BIN
OSlibs/win/x64/release/libcurl.dll

Binary file not shown.

BIN
OSlibs/win/x64/release/nanomsg.dll

Binary file not shown.

5
crypto777/inet.c

@ -605,7 +605,10 @@ uint16_t parse_endpoint(int32_t *ip6flagp,char *transport,char *ipbuf,char *retb
ipbits = calc_ipbits(ipaddr); ipbits = calc_ipbits(ipaddr);
expand_ipbits(tmp,ipbits); expand_ipbits(tmp,ipbits);
if ( strcmp(tmp,ipaddr) != 0 ) if ( strcmp(tmp,ipaddr) != 0 )
ipaddr = 0, sprintf(retbuf,"{\"result\":\"illegal ipaddr\",\"endpoint\":\"%s\",\"ipaddr\":\"%s\",\"checkaddr\":\"%s\"}",endpoint,ipaddr,tmp); {
sprintf(retbuf,"{\"result\":\"illegal ipaddr\",\"endpoint\":\"%s\",\"ipaddr\":\"%s\",\"checkaddr\":\"%s\"}",endpoint,ipaddr,tmp);
ipaddr = 0;
}
} }
if ( inet != 0 && ipaddr != 0 && port != 0 ) if ( inet != 0 && ipaddr != 0 && port != 0 )
{ {

BIN
crypto777/pnacl_libs/libcrypto.a

Binary file not shown.

BIN
crypto777/pnacl_libs/libcurl.a

Binary file not shown.

BIN
crypto777/pnacl_libs/libnanomsg.a

Binary file not shown.

BIN
crypto777/pnacl_libs/libssl.a

Binary file not shown.

BIN
crypto777/pnacl_libs/libz.a

Binary file not shown.

20490
deprecated/obsolete.h

File diff suppressed because it is too large

BIN
iguana/Kashi/json_extracta

Binary file not shown.

BIN
iguana/dexscripts.win32/curl.exe

Binary file not shown.

4
iguana/exchanges/LP_cache.c

@ -57,7 +57,7 @@ struct LP_transaction *LP_create_transaction(struct iguana_info *coin,bits256 tx
tx->outpoints[i].interest = SATOSHIDEN * jdouble(vout,"interest"); tx->outpoints[i].interest = SATOSHIDEN * jdouble(vout,"interest");
LP_destaddr(tx->outpoints[i].coinaddr,vout); LP_destaddr(tx->outpoints[i].coinaddr,vout);
//printf("from transaction init %s %s %s/v%d <- %.8f\n",coin->symbol,tx->outpoints[i].coinaddr,bits256_str(str,txid),i,dstr(tx->outpoints[i].value)); //printf("from transaction init %s %s %s/v%d <- %.8f\n",coin->symbol,tx->outpoints[i].coinaddr,bits256_str(str,txid),i,dstr(tx->outpoints[i].value));
LP_address_utxoadd((uint32_t)time(NULL),"LP_create_transaction",coin,tx->outpoints[i].coinaddr,txid,i,tx->outpoints[i].value,height,-1); LP_address_utxoadd(0,(uint32_t)time(NULL),"LP_create_transaction",coin,tx->outpoints[i].coinaddr,txid,i,tx->outpoints[i].value,height,-1);
} }
for (i=0; i<numvins; i++) for (i=0; i<numvins; i++)
{ {
@ -75,7 +75,7 @@ struct LP_transaction *LP_create_transaction(struct iguana_info *coin,bits256 tx
tx->outpoints[spentvout].spendtxid = txid; tx->outpoints[spentvout].spendtxid = txid;
tx->outpoints[spentvout].spendvini = i; tx->outpoints[spentvout].spendvini = i;
tx->outpoints[spentvout].spendheight = height > 0 ? height : 1; tx->outpoints[spentvout].spendheight = height > 0 ? height : 1;
LP_address_utxoadd((uint32_t)time(NULL),"LP_transactioninit iter1",coin,tx->outpoints[spentvout].coinaddr,spenttxid,spentvout,tx->outpoints[spentvout].value,-1,height>0?height:1); LP_address_utxoadd(0,(uint32_t)time(NULL),"LP_transactioninit iter1",coin,tx->outpoints[spentvout].coinaddr,spenttxid,spentvout,tx->outpoints[spentvout].value,-1,height>0?height:1);
if ( 0 && strcmp(coin->symbol,"REVS") == 0 ) if ( 0 && strcmp(coin->symbol,"REVS") == 0 )
printf("spend %s %s/v%d at ht.%d\n",coin->symbol,bits256_str(str,tx->txid),spentvout,height); printf("spend %s %s/v%d at ht.%d\n",coin->symbol,bits256_str(str,tx->txid),spentvout,height);
} }

3
iguana/exchanges/LP_coins.c

@ -214,7 +214,7 @@ uint16_t LP_userpass(char *userpass,char *symbol,char *assetname,char *confroot,
cJSON *LP_coinjson(struct iguana_info *coin,int32_t showwif) cJSON *LP_coinjson(struct iguana_info *coin,int32_t showwif)
{ {
struct electrum_info *ep; bits256 zero; int32_t notarized; uint64_t balance; char wifstr[128],ipaddr[64]; uint8_t tmptype; bits256 checkkey; cJSON *item = cJSON_CreateObject(); struct electrum_info *ep; bits256 zero; int32_t notarized; uint64_t balance; char wifstr[128],ipaddr[72]; uint8_t tmptype; bits256 checkkey; cJSON *item = cJSON_CreateObject();
jaddstr(item,"coin",coin->symbol); jaddstr(item,"coin",coin->symbol);
if ( showwif != 0 ) if ( showwif != 0 )
{ {
@ -342,6 +342,7 @@ struct iguana_info *LP_coinadd(struct iguana_info *cdata)
*coin = *cdata; *coin = *cdata;
portable_mutex_init(&coin->txmutex); portable_mutex_init(&coin->txmutex);
portable_mutex_init(&coin->addrmutex); portable_mutex_init(&coin->addrmutex);
portable_mutex_init(&coin->addressutxo_mutex);
portable_mutex_lock(&LP_coinmutex); portable_mutex_lock(&LP_coinmutex);
HASH_ADD_KEYPTR(hh,LP_coins,coin->symbol,strlen(coin->symbol),coin); HASH_ADD_KEYPTR(hh,LP_coins,coin->symbol,strlen(coin->symbol),coin);
portable_mutex_unlock(&LP_coinmutex); portable_mutex_unlock(&LP_coinmutex);

16
iguana/exchanges/LP_commands.c

@ -171,7 +171,7 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
rel = ""; rel = "";
if ( (coin= jstr(argjson,"coin")) == 0 ) if ( (coin= jstr(argjson,"coin")) == 0 )
coin = ""; coin = "";
if ( G.USERPASS[0] != 0 && strcmp(remoteaddr,"127.0.0.1") == 0 && port != 0 ) // protected localhost if ( G.USERPASS[0] != 0 && strcmp(remoteaddr,"127.0.0.1") == 0 && port != 0 && strcmp(method,"psock") != 0 ) // protected localhost
{ {
if ( G.USERPASS_COUNTER == 0 ) if ( G.USERPASS_COUNTER == 0 )
{ {
@ -645,7 +645,10 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
if ( strcmp(method,"swapstatus") == 0 ) if ( strcmp(method,"swapstatus") == 0 )
return(LP_swapstatus_recv(argjson)); return(LP_swapstatus_recv(argjson));
else if ( strcmp(method,"gettradestatus") == 0 ) else if ( strcmp(method,"gettradestatus") == 0 )
return(LP_gettradestatus(j64bits(argjson,"aliceid"),juint(argjson,"requestid"),juint(argjson,"quoteid"))); {
retstr = clonestr("{\"error\":\"deprecated\"}");
//return(LP_gettradestatus(j64bits(argjson,"aliceid"),juint(argjson,"requestid"),juint(argjson,"quoteid")));
}
else if ( strcmp(method,"postprice") == 0 ) else if ( strcmp(method,"postprice") == 0 )
return(LP_postprice_recv(argjson)); return(LP_postprice_recv(argjson));
else if ( strcmp(method,"uitem") == 0 ) else if ( strcmp(method,"uitem") == 0 )
@ -704,7 +707,7 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
else if ( strcmp(method,"tradestatus") == 0 ) else if ( strcmp(method,"tradestatus") == 0 )
{ {
LP_tradecommand_log(argjson); LP_tradecommand_log(argjson);
printf("%-4d tradestatus | aliceid.%llu RT.%d %d\n",(uint32_t)time(NULL) % 3600,(long long)j64bits(argjson,"aliceid"),LP_RTcount,LP_swapscount); //printf("%-4d tradestatus | aliceid.%llu RT.%d %d\n",(uint32_t)time(NULL) % 3600,(long long)j64bits(argjson,"aliceid"),LP_RTcount,LP_swapscount);
retstr = clonestr("{\"result\":\"success\"}"); retstr = clonestr("{\"result\":\"success\"}");
} }
else if ( strcmp(method,"wantnotify") == 0 ) else if ( strcmp(method,"wantnotify") == 0 )
@ -751,6 +754,7 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
{ {
if ( strcmp(method,"psock") == 0 ) if ( strcmp(method,"psock") == 0 )
{ {
int32_t psock;
if ( myipaddr == 0 || myipaddr[0] == 0 || strcmp(myipaddr,"127.0.0.1") == 0 ) if ( myipaddr == 0 || myipaddr[0] == 0 || strcmp(myipaddr,"127.0.0.1") == 0 )
{ {
if ( LP_mypeer != 0 ) if ( LP_mypeer != 0 )
@ -758,7 +762,11 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
else printf("LP_psock dont have actual ipaddr?\n"); else printf("LP_psock dont have actual ipaddr?\n");
} }
if ( jint(argjson,"ispaired") != 0 ) if ( jint(argjson,"ispaired") != 0 )
return(LP_psock(myipaddr,jint(argjson,"ispaired"))); {
retstr = LP_psock(&psock,myipaddr,1,jint(argjson,"cmdchannel"),jbits256(argjson,"pubkey"));
//printf("LP_commands.(%s)\n",retstr);
return(retstr);
}
else return(clonestr("{\"error\":\"you are running an obsolete version, update\"}")); else return(clonestr("{\"error\":\"you are running an obsolete version, update\"}"));
} }
} }

25
iguana/exchanges/LP_include.h

@ -25,6 +25,8 @@
#define LP_TECHSUPPORT 0 #define LP_TECHSUPPORT 0
#endif #endif
#define LP_DONT_CMDCHANNEL
#ifdef FROMGUI #ifdef FROMGUI
#define printf dontprintf #define printf dontprintf
@ -52,10 +54,11 @@ void emscripten_usleep(int32_t x); // returns immediate, no sense for sleeping
#endif #endif
//#define LP_STRICTPEERS //#define LP_STRICTPEERS
//#define LP_DISABLE_DISTCOMBINE
#define LP_MAXVINS 64 #define LP_MAXVINS 64
#define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL) #define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL)
#define LP_AUTOTRADE_TIMEOUT 30 #define LP_AUTOTRADE_TIMEOUT 180
#define LP_RESERVETIME 600 //(LP_AUTOTRADE_TIMEOUT * 2) #define LP_RESERVETIME 600 //(LP_AUTOTRADE_TIMEOUT * 2)
#define ELECTRUM_TIMEOUT 13 #define ELECTRUM_TIMEOUT 13
#define LP_ELECTRUM_KEEPALIVE 60 #define LP_ELECTRUM_KEEPALIVE 60
@ -297,10 +300,10 @@ struct LP_transaction
struct iguana_info struct iguana_info
{ {
UT_hash_handle hh; UT_hash_handle hh;
portable_mutex_t txmutex,addrmutex; struct LP_transaction *transactions; struct LP_address *addresses; portable_mutex_t txmutex,addrmutex,addressutxo_mutex; struct LP_transaction *transactions; struct LP_address *addresses;
uint64_t txfee; uint64_t txfee;
int32_t numutxos,notarized,longestchain,firstrefht,firstscanht,lastscanht,height; uint16_t busport; int32_t numutxos,notarized,longestchain,firstrefht,firstscanht,lastscanht,height; uint16_t busport,did_addrutxo_reset;
uint32_t txversion,dPoWtime,loadedcache,electrumlist,lastunspent,importedprivkey,lastpushtime,lastutxosync,addr_listunspent_requested,lastutxos,updaterate,counter,inactive,lastmempool,lastgetinfo,ratetime,heighttime,lastmonitor,obooktime; uint32_t txversion,dPoWtime,lastresetutxo,loadedcache,electrumlist,lastunspent,importedprivkey,lastpushtime,lastutxosync,addr_listunspent_requested,lastutxos,updaterate,counter,inactive,lastmempool,lastgetinfo,ratetime,heighttime,lastmonitor,obooktime;
uint8_t pubtype,p2shtype,isPoS,wiftype,wiftaddr,taddr,noimportprivkey_flag,userconfirms,isassetchain,maxconfirms; uint8_t pubtype,p2shtype,isPoS,wiftype,wiftaddr,taddr,noimportprivkey_flag,userconfirms,isassetchain,maxconfirms;
char symbol[128],smartaddr[64],etomic[64],userpass[1024],serverport[128],instantdex_address[64]; char symbol[128],smartaddr[64],etomic[64],userpass[1024],serverport[128],instantdex_address[64];
// portfolio // portfolio
@ -360,9 +363,10 @@ struct LP_address
struct LP_peerinfo struct LP_peerinfo
{ {
UT_hash_handle hh; UT_hash_handle hh;
bits256 pubkey;
uint64_t ip_port; uint64_t ip_port;
uint32_t recvtime,numrecv,ipbits,errortime,errors,numpeers,needping,lasttime,connected,lastutxos,lastpeers,diduquery,good,sessionid; uint32_t recvtime,numrecv,ipbits,errortime,errors,numpeers,needping,lasttime,connected,lastutxos,lastpeers,diduquery,good,sessionid;
int32_t pushsock,subsock,isLP; int32_t pushsock,subsock,isLP,pairsock;
uint16_t port,netid; uint16_t port,netid;
char ipaddr[64]; char ipaddr[64];
}; };
@ -430,7 +434,7 @@ struct LP_pubkey_info
struct LP_pubswap *bobswaps,*aliceswaps; struct LP_pubswap *bobswaps,*aliceswaps;
int64_t dynamictrust,unconfcredits; int64_t dynamictrust,unconfcredits;
uint32_t timestamp,numerrors,lasttime,slowresponse; uint32_t timestamp,numerrors,lasttime,slowresponse;
int32_t istrusted; int32_t istrusted,pairsock;
uint8_t rmd160[20],sig[65],pubsecp[33],siglen; uint8_t rmd160[20],sig[65],pubsecp[33],siglen;
}; };
@ -477,7 +481,7 @@ int32_t LP_forward(void *ctx,char *myipaddr,int32_t pubsock,bits256 pubkey,char
struct LP_peerinfo *LP_peerfind(uint32_t ipbits,uint16_t port); struct LP_peerinfo *LP_peerfind(uint32_t ipbits,uint16_t port);
uint64_t LP_value_extract(cJSON *obj,int32_t addinterest); uint64_t LP_value_extract(cJSON *obj,int32_t addinterest);
int32_t LP_swap_getcoinaddr(char *symbol,char *coinaddr,bits256 txid,int32_t vout); int32_t LP_swap_getcoinaddr(char *symbol,char *coinaddr,bits256 txid,int32_t vout);
char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen); char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,int32_t stats_JSONonly);
int64_t LP_kmdvalue(char *symbol,int64_t satoshis); int64_t LP_kmdvalue(char *symbol,int64_t satoshis);
int64_t LP_komodo_interest(bits256 txid,int64_t value); int64_t LP_komodo_interest(bits256 txid,int64_t value);
void LP_availableset(bits256 txid,int32_t vout); void LP_availableset(bits256 txid,int32_t vout);
@ -485,7 +489,7 @@ int32_t LP_iseligible(uint64_t *valp,uint64_t *val2p,int32_t iambob,char *symbol
int32_t LP_pullsock_check(void *ctx,char **retstrp,char *myipaddr,int32_t pubsock,int32_t pullsock); int32_t LP_pullsock_check(void *ctx,char **retstrp,char *myipaddr,int32_t pubsock,int32_t pullsock);
int64_t LP_listunspent_parseitem(struct iguana_info *coin,bits256 *txidp,int32_t *voutp,int32_t *heightp,cJSON *item); int64_t LP_listunspent_parseitem(struct iguana_info *coin,bits256 *txidp,int32_t *voutp,int32_t *heightp,cJSON *item);
void LP_unspents_cache(char *symbol,char *addr,char *arraystr,int32_t updatedflag); void LP_unspents_cache(char *symbol,char *addr,char *arraystr,int32_t updatedflag);
uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired); uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired,int32_t cmdchannel,char *ipaddr);
//void LP_utxo_clientpublish(struct LP_utxoinfo *utxo); //void LP_utxo_clientpublish(struct LP_utxoinfo *utxo);
//int32_t LP_coinbus(uint16_t coin_busport); //int32_t LP_coinbus(uint16_t coin_busport);
int32_t LP_nanomsg_recvs(void *ctx); int32_t LP_nanomsg_recvs(void *ctx);
@ -533,7 +537,7 @@ int32_t LP_numpeers();
double LP_CMCbtcprice(double *price_usdp,char *symbol); double LP_CMCbtcprice(double *price_usdp,char *symbol);
char *basilisk_swapentry(uint32_t requestid,uint32_t quoteid,int32_t forceflag); char *basilisk_swapentry(uint32_t requestid,uint32_t quoteid,int32_t forceflag);
int64_t LP_KMDvalue(struct iguana_info *coin,int64_t balance); int64_t LP_KMDvalue(struct iguana_info *coin,int64_t balance);
int32_t LP_address_utxoadd(uint32_t timestamp,char *debug,struct iguana_info *coin,char *coinaddr,bits256 txid,int32_t vout,uint64_t value,int32_t height,int32_t spendheight); int32_t LP_address_utxoadd(int32_t skipsearch,uint32_t timestamp,char *debug,struct iguana_info *coin,char *coinaddr,bits256 txid,int32_t vout,uint64_t value,int32_t height,int32_t spendheight);
void LP_smartutxos_push(struct iguana_info *coin); void LP_smartutxos_push(struct iguana_info *coin);
void LP_cacheptrs_init(struct iguana_info *coin); void LP_cacheptrs_init(struct iguana_info *coin);
cJSON *LP_address_utxos(struct iguana_info *coin,char *coinaddr,int32_t electrumret); cJSON *LP_address_utxos(struct iguana_info *coin,char *coinaddr,int32_t electrumret);
@ -543,9 +547,10 @@ int32_t LP_listunspent_both(char *symbol,char *coinaddr,int32_t fullflag);
uint16_t LP_randpeer(char *destip); uint16_t LP_randpeer(char *destip);
void LP_tradebot_pauseall(); void LP_tradebot_pauseall();
void LP_portfolio_reset(); void LP_portfolio_reset();
struct LP_pubkey_info *LP_pubkeyadd(bits256 pubkey);
uint32_t LP_atomic_locktime(char *base,char *rel); uint32_t LP_atomic_locktime(char *base,char *rel);
struct LP_pubkey_info *LP_pubkeyfind(bits256 pubkey); struct LP_pubkey_info *LP_pubkeyfind(bits256 pubkey);
char *issue_LP_psock(char *destip,uint16_t destport,int32_t ispaired); char *issue_LP_psock(char *destip,uint16_t destport,int32_t ispaired,int32_t cmdchannel);
char *LP_unspents_filestr(char *symbol,char *addr); char *LP_unspents_filestr(char *symbol,char *addr);
cJSON *bitcoin_data2json(char *symbol,uint8_t taddr,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,int32_t height,bits256 *txidp,struct iguana_msgtx *msgtx,uint8_t *extraspace,int32_t extralen,uint8_t *serialized,int32_t len,cJSON *vins,int32_t suppress_pubkeys,int32_t zcash); cJSON *bitcoin_data2json(char *symbol,uint8_t taddr,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,int32_t height,bits256 *txidp,struct iguana_msgtx *msgtx,uint8_t *extraspace,int32_t extralen,uint8_t *serialized,int32_t len,cJSON *vins,int32_t suppress_pubkeys,int32_t zcash);
//int32_t LP_butxo_findeither(bits256 txid,int32_t vout); //int32_t LP_butxo_findeither(bits256 txid,int32_t vout);

3
iguana/exchanges/LP_instantdex.c

@ -596,12 +596,15 @@ cJSON *LP_swapstats_item(struct LP_swapstats *sp,int32_t iambob)
LP_dPoW_request(alice); LP_dPoW_request(alice);
} }
if ( flag != 0 ) if ( flag != 0 )
{
if ( 0 )
{ {
reqjson = cJSON_CreateObject(); reqjson = cJSON_CreateObject();
jaddstr(reqjson,"method","gettradestatus"); jaddstr(reqjson,"method","gettradestatus");
jadd64bits(reqjson,"aliceid",sp->aliceid); jadd64bits(reqjson,"aliceid",sp->aliceid);
memset(zero.bytes,0,sizeof(zero)); memset(zero.bytes,0,sizeof(zero));
LP_reserved_msg(0,"","",zero,jprint(reqjson,1)); LP_reserved_msg(0,"","",zero,jprint(reqjson,1));
}
if ( (swapstr= basilisk_swapentry(sp->Q.R.requestid,sp->Q.R.quoteid,0)) != 0 ) if ( (swapstr= basilisk_swapentry(sp->Q.R.requestid,sp->Q.R.quoteid,0)) != 0 )
{ {
if ( (swapjson= cJSON_Parse(swapstr)) != 0 ) if ( (swapjson= cJSON_Parse(swapstr)) != 0 )

2
iguana/exchanges/LP_mmjson.c

@ -516,6 +516,8 @@ int32_t MMJSON_encodeval(uint8_t *linebuf,int32_t k,int32_t ind,char *v,uint32_t
} }
if ( v[j] == 0 ) if ( v[j] == 0 )
{ {
static uint32_t counter;
if ( counter++ < 3 )
printf("unexpected missing string value.(%s)\n",v); printf("unexpected missing string value.(%s)\n",v);
//ind = mmadd(v); //ind = mmadd(v);
//printf("%s.<%s>.%d ",s,v,ind); //printf("%s.<%s>.%d ",s,v,ind);

122
iguana/exchanges/LP_nativeDEX.c

@ -85,7 +85,7 @@ void LP_millistats_update(struct LP_millistats *mp)
} }
#include "LP_include.h" #include "LP_include.h"
portable_mutex_t LP_peermutex,LP_UTXOmutex,LP_utxomutex,LP_commandmutex,LP_cachemutex,LP_swaplistmutex,LP_forwardmutex,LP_pubkeymutex,LP_networkmutex,LP_psockmutex,LP_coinmutex,LP_messagemutex,LP_portfoliomutex,LP_electrummutex,LP_butxomutex,LP_reservedmutex,LP_nanorecvsmutex,LP_tradebotsmutex,LP_gcmutex,LP_inusemutex,LP_cJSONmutex,LP_logmutex,LP_statslogmutex,LP_tradesmutex; portable_mutex_t LP_peermutex,LP_UTXOmutex,LP_utxomutex,LP_commandmutex,LP_cachemutex,LP_swaplistmutex,LP_forwardmutex,LP_pubkeymutex,LP_networkmutex,LP_psockmutex,LP_coinmutex,LP_messagemutex,LP_portfoliomutex,LP_electrummutex,LP_butxomutex,LP_reservedmutex,LP_nanorecvsmutex,LP_tradebotsmutex,LP_gcmutex,LP_inusemutex,LP_cJSONmutex,LP_logmutex,LP_statslogmutex,LP_tradesmutex,LP_commandQmutex;
int32_t LP_canbind; int32_t LP_canbind;
char *Broadcaststr,*Reserved_msgs[2][1000]; char *Broadcaststr,*Reserved_msgs[2][1000];
int32_t num_Reserved_msgs[2],max_Reserved_msgs[2]; int32_t num_Reserved_msgs[2],max_Reserved_msgs[2];
@ -193,21 +193,21 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_
#include "LP_messages.c" #include "LP_messages.c"
#include "LP_commands.c" #include "LP_commands.c"
char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen) char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,int32_t stats_JSONonly)
{ {
char *retstr=0; cJSON *retjson; bits256 zero; char *retstr=0; cJSON *retjson; bits256 zero;
if ( jobj(argjson,"result") != 0 || jobj(argjson,"error") != 0 ) if ( jobj(argjson,"result") != 0 || jobj(argjson,"error") != 0 )
return(0); return(0);
if ( LP_tradecommand(ctx,myipaddr,pubsock,argjson,data,datalen) <= 0 ) if ( stats_JSONonly != 0 || LP_tradecommand(ctx,myipaddr,pubsock,argjson,data,datalen) <= 0 )
{ {
if ( (retstr= stats_JSON(ctx,myipaddr,pubsock,argjson,"127.0.0.1",0)) != 0 ) if ( (retstr= stats_JSON(ctx,myipaddr,pubsock,argjson,"127.0.0.1",stats_JSONonly)) != 0 )
{ {
//printf("%s PULL.[%d]-> (%s)\n",myipaddr != 0 ? myipaddr : "127.0.0.1",datalen,retstr); //printf("%s PULL.[%d]-> (%s)\n",myipaddr != 0 ? myipaddr : "127.0.0.1",datalen,retstr);
//if ( pubsock >= 0 ) //strncmp("{\"error\":",retstr,strlen("{\"error\":")) != 0 && //if ( pubsock >= 0 ) //strncmp("{\"error\":",retstr,strlen("{\"error\":")) != 0 &&
//LP_send(pubsock,retstr,(int32_t)strlen(retstr)+1,0); //LP_send(pubsock,retstr,(int32_t)strlen(retstr)+1,0);
} }
} }
else if ( LP_statslog_parse() > 0 ) else if ( LP_statslog_parse() > 0 && 0 )
{ {
memset(zero.bytes,0,sizeof(zero)); memset(zero.bytes,0,sizeof(zero));
if ( (retjson= LP_statslog_disp(2000000000,2000000000,"",zero,0,0))) // pending swaps if ( (retjson= LP_statslog_disp(2000000000,2000000000,"",zero,0,0))) // pending swaps
@ -304,7 +304,7 @@ char *LP_process_message(void *ctx,char *typestr,char *myipaddr,int32_t pubsock,
if ( jsonstr != 0 && argjson != 0 ) if ( jsonstr != 0 && argjson != 0 )
{ {
len = (int32_t)strlen(jsonstr) + 1; len = (int32_t)strlen(jsonstr) + 1;
if ( (method= jstr(argjson,"method")) != 0 && strcmp(method,"broadcast") == 0 ) if ( (method= jstr(argjson,"method")) != 0 && strcmp(method,"gettradestatus") != 0 && strcmp(method,"psock") != 0 && strcmp(method,"broadcast") == 0 )
{ {
bits256 zero; cJSON *reqjson; char *cipherstr; int32_t cipherlen; uint8_t cipher[LP_ENCRYPTED_MAXSIZE]; bits256 zero; cJSON *reqjson; char *cipherstr; int32_t cipherlen; uint8_t cipher[LP_ENCRYPTED_MAXSIZE];
if ( (reqjson= LP_dereference(argjson,"broadcast")) != 0 ) if ( (reqjson= LP_dereference(argjson,"broadcast")) != 0 )
@ -332,9 +332,10 @@ char *LP_process_message(void *ctx,char *typestr,char *myipaddr,int32_t pubsock,
} }
else else
{ {
if ( (retstr= LP_command_process(ctx,myipaddr,pubsock,argjson,&((uint8_t *)ptr)[len],recvlen - len)) != 0 ) LP_queuecommand(0,jsonstr,pubsock,0);
{ //if ( (retstr= LP_command_process(ctx,myipaddr,pubsock,argjson,&((uint8_t *)ptr)[len],recvlen - len)) != 0 )
} //{
//}
} }
} }
if ( argjson != 0 ) if ( argjson != 0 )
@ -350,7 +351,7 @@ char *LP_process_message(void *ctx,char *typestr,char *myipaddr,int32_t pubsock,
int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int32_t sock,char *remoteaddr,int32_t maxdepth) int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int32_t sock,char *remoteaddr,int32_t maxdepth)
{ {
static char *line; static char *line;
int32_t recvlen=1,msglen,nonz = 0; cJSON *argjson,*recvjson; void *ptr,*msg; char methodstr[64],*decodestr,*retstr,*str; struct nn_pollfd pfd; int32_t recvlen=1,msglen,nonz = 0; cJSON *recvjson; void *ptr,*msg; char methodstr[64],*decodestr,*retstr,*str; struct nn_pollfd pfd;
if ( line == 0 ) if ( line == 0 )
line = calloc(1,1024*1024); line = calloc(1,1024*1024);
if ( sock >= 0 ) if ( sock >= 0 )
@ -408,12 +409,14 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int
{ {
if ( (retstr= LP_process_message(ctx,typestr,myipaddr,pubsock,msg,msglen,sock)) != 0 ) if ( (retstr= LP_process_message(ctx,typestr,myipaddr,pubsock,msg,msglen,sock)) != 0 )
free(retstr); free(retstr);
if ( Broadcaststr != 0 ) if ( Broadcaststr != 0 )
{ {
//printf("self broadcast.(%s)\n",Broadcaststr); //printf("self broadcast.(%s)\n",Broadcaststr);
str = Broadcaststr; str = Broadcaststr;
Broadcaststr = 0; Broadcaststr = 0;
if ( (argjson= cJSON_Parse(str)) != 0 ) LP_queuecommand(0,str,pubsock,0);
/*if ( (argjson= cJSON_Parse(str)) != 0 )
{ {
//portable_mutex_lock(&LP_commandmutex); //portable_mutex_lock(&LP_commandmutex);
if ( LP_tradecommand(ctx,myipaddr,pubsock,argjson,0,0) <= 0 ) if ( LP_tradecommand(ctx,myipaddr,pubsock,argjson,0,0) <= 0 )
@ -423,7 +426,7 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int
} }
//portable_mutex_unlock(&LP_commandmutex); //portable_mutex_unlock(&LP_commandmutex);
free_json(argjson); free_json(argjson);
} }*/
free(str); free(str);
} }
} }
@ -562,6 +565,11 @@ void LP_coinsloop(void *_coins)
memset(&zero,0,sizeof(zero)); memset(&zero,0,sizeof(zero));
if ( coin->inactive != 0 ) if ( coin->inactive != 0 )
continue; continue;
if ( coin->did_addrutxo_reset == 0 )
{
LP_address_utxo_reset(coin);
coin->did_addrutxo_reset = 1;
}
if ( coin->longestchain == 1 ) // special init value if ( coin->longestchain == 1 ) // special init value
coin->longestchain = LP_getheight(&notarized,coin); coin->longestchain = LP_getheight(&notarized,coin);
if ( (ep= coin->electrum) != 0 ) if ( (ep= coin->electrum) != 0 )
@ -907,7 +915,8 @@ void LP_initpeers(int32_t pubsock,struct LP_peerinfo *mypeer,char *myipaddr,uint
{ {
printf("default seed nodes for netid.%d\n",netid); printf("default seed nodes for netid.%d\n",netid);
OS_randombytes((void *)&r,sizeof(r)); OS_randombytes((void *)&r,sizeof(r));
for (j=0; j<sizeof(default_LPnodes)/sizeof(*default_LPnodes)&&j<5; j++) r = 0;
for (j=0; j<sizeof(default_LPnodes)/sizeof(*default_LPnodes); j++)
{ {
i = (r + j) % (sizeof(default_LPnodes)/sizeof(*default_LPnodes)); i = (r + j) % (sizeof(default_LPnodes)/sizeof(*default_LPnodes));
LP_addpeer(mypeer,pubsock,default_LPnodes[i],myport,pushport,subport,0,G.LP_sessionid,netid); LP_addpeer(mypeer,pubsock,default_LPnodes[i],myport,pushport,subport,0,G.LP_sessionid,netid);
@ -1134,11 +1143,24 @@ void LP_reserved_msgs(void *ignore)
} }
nonz = 0; nonz = 0;
LP_millistats_update(&LP_reserved_msgs_stats); LP_millistats_update(&LP_reserved_msgs_stats);
if ( num_Reserved_msgs[0] > 0 || num_Reserved_msgs[1] > 0 ) if ( num_Reserved_msgs[1] > 0 )
{
nonz++;
portable_mutex_lock(&LP_reservedmutex);
if ( num_Reserved_msgs[1] > 0 )
{
num_Reserved_msgs[1]--;
//printf("PRIORITY BROADCAST.(%s)\n",Reserved_msgs[1][num_Reserved_msgs[1]]);
LP_broadcast_message(LP_mypubsock,"","",zero,Reserved_msgs[1][num_Reserved_msgs[1]]);
Reserved_msgs[1][num_Reserved_msgs[1]] = 0;
}
portable_mutex_unlock(&LP_reservedmutex);
}
else if ( num_Reserved_msgs[0] > 0 )
{ {
nonz++; nonz++;
flag = 0; flag = 0;
if ( LP_mypubsock >= 0 ) if ( flag == 0 && LP_mypubsock >= 0 )
{ {
memset(&pfd,0,sizeof(pfd)); memset(&pfd,0,sizeof(pfd));
pfd.fd = LP_mypubsock; pfd.fd = LP_mypubsock;
@ -1149,36 +1171,59 @@ void LP_reserved_msgs(void *ignore)
if ( flag == 1 ) if ( flag == 1 )
{ {
portable_mutex_lock(&LP_reservedmutex); portable_mutex_lock(&LP_reservedmutex);
if ( num_Reserved_msgs[1] > 0 )
{
num_Reserved_msgs[1]--;
//printf("PRIORITY BROADCAST.(%s)\n",Reserved_msgs[1][num_Reserved_msgs[1]]);
LP_broadcast_message(LP_mypubsock,"","",zero,Reserved_msgs[1][num_Reserved_msgs[1]]);
Reserved_msgs[1][num_Reserved_msgs[1]] = 0;
}
else if ( num_Reserved_msgs[0] > 0 )
{
num_Reserved_msgs[0]--; num_Reserved_msgs[0]--;
//printf("BROADCAST.(%s)\n",Reserved_msgs[0][num_Reserved_msgs[0]]); //printf("BROADCAST.(%s)\n",Reserved_msgs[0][num_Reserved_msgs[0]]);
LP_broadcast_message(LP_mypubsock,"","",zero,Reserved_msgs[0][num_Reserved_msgs[0]]); LP_broadcast_message(LP_mypubsock,"","",zero,Reserved_msgs[0][num_Reserved_msgs[0]]);
Reserved_msgs[0][num_Reserved_msgs[0]] = 0; Reserved_msgs[0][num_Reserved_msgs[0]] = 0;
}
portable_mutex_unlock(&LP_reservedmutex); portable_mutex_unlock(&LP_reservedmutex);
} }
} }
if ( ignore == 0 ) if ( ignore == 0 )
break; break;
if ( nonz != 0 ) if ( nonz == 0 )
usleep(1000); usleep(5000);
else usleep(5000);
} }
} }
int32_t LP_reserved_msg(int32_t priority,char *base,char *rel,bits256 pubkey,char *msg) int32_t LP_reserved_msg(int32_t priority,char *base,char *rel,bits256 pubkey,char *msg)
{ {
int32_t n = 0; struct LP_pubkey_info *pubp; uint32_t timestamp; char *method; cJSON *argjson; int32_t skip,sentbytes,n = 0;
skip = 0;
if ( (argjson= cJSON_Parse(msg)) != 0 )
{
if ( (method= jstr(argjson,"method")) != 0 )
{
if ( strcmp(method,"gettradestatus") == 0 || strcmp(method,"wantnotify") == 0 || strcmp(method,"getdPoW") == 0 )
skip = 1;
}
if ( (timestamp= juint(argjson,"timestamp")) != 0 && time(NULL) > timestamp+60 )
skip = 1;
free_json(argjson);
}
if ( skip != 0 )
return(-1);
if ( strcmp(G.USERPASS,"1d8b27b21efabcd96571cd56f91a40fb9aa4cc623d273c63bf9223dc6f8cd81f") == 0 ) if ( strcmp(G.USERPASS,"1d8b27b21efabcd96571cd56f91a40fb9aa4cc623d273c63bf9223dc6f8cd81f") == 0 )
return(-1); return(-1);
if ( priority > 0 && bits256_nonz(pubkey) != 0 )
{
if ( (pubp= LP_pubkeyfind(pubkey)) != 0 )
{
if ( pubp->pairsock >= 0 )
{
if ( (sentbytes= nn_send(pubp->pairsock,msg,(int32_t)strlen(msg)+1,0)) < 0 )
{
//pubp->pairsock = -1;
//LP_peer_pairsock(pubkey);
//printf("mark cmdchannel %d closed sentbytes.%d\n",pubp->pairsock,sentbytes);
}
else
{
printf("sent %d bytes to cmdchannel.%d\n",sentbytes,pubp->pairsock);
return(sentbytes);
}
}
}
}
portable_mutex_lock(&LP_reservedmutex); portable_mutex_lock(&LP_reservedmutex);
if ( num_Reserved_msgs[priority] < sizeof(Reserved_msgs[priority])/sizeof(*Reserved_msgs[priority]) ) if ( num_Reserved_msgs[priority] < sizeof(Reserved_msgs[priority])/sizeof(*Reserved_msgs[priority]) )
{ {
@ -1188,7 +1233,7 @@ int32_t LP_reserved_msg(int32_t priority,char *base,char *rel,bits256 pubkey,cha
if ( num_Reserved_msgs[priority] > max_Reserved_msgs[priority] ) if ( num_Reserved_msgs[priority] > max_Reserved_msgs[priority] )
{ {
max_Reserved_msgs[priority] = num_Reserved_msgs[priority]; max_Reserved_msgs[priority] = num_Reserved_msgs[priority];
if ( (max_Reserved_msgs[priority] % 100) == 0 ) //if ( (max_Reserved_msgs[priority] % 100) == 0 )
printf("New priority.%d max_Reserved_msgs.%d\n",priority,max_Reserved_msgs[priority]); printf("New priority.%d max_Reserved_msgs.%d\n",priority,max_Reserved_msgs[priority]);
} }
portable_mutex_unlock(&LP_reservedmutex); portable_mutex_unlock(&LP_reservedmutex);
@ -1275,6 +1320,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
portable_mutex_init(&LP_logmutex); portable_mutex_init(&LP_logmutex);
portable_mutex_init(&LP_statslogmutex); portable_mutex_init(&LP_statslogmutex);
portable_mutex_init(&LP_tradesmutex); portable_mutex_init(&LP_tradesmutex);
portable_mutex_init(&LP_commandQmutex);
myipaddr = clonestr("127.0.0.1"); myipaddr = clonestr("127.0.0.1");
#ifndef _WIN32 #ifndef _WIN32
#ifndef FROM_JS #ifndef FROM_JS
@ -1308,8 +1354,10 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
valid++; valid++;
if ( valid > 0 ) if ( valid > 0 )
{ {
timeout = 1; timeout = 100;
nn_setsockopt(LP_mypubsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); nn_setsockopt(LP_mypubsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout));
//timeout = 10;
//nn_setsockopt(LP_mypubsock,NN_SOL_SOCKET,NN_MAXTTL,&timeout,sizeof(timeout));
} }
else else
{ {
@ -1348,7 +1396,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
printf("got %s, initpeers. LP_mypubsock.%d pullsock.%d RPC_port.%u mypullport.%d mypubport.%d pushaddr.%s\n",myipaddr,LP_mypubsock,LP_mypullsock,RPC_port,mypullport,mypubport,pushaddr); printf("got %s, initpeers. LP_mypubsock.%d pullsock.%d RPC_port.%u mypullport.%d mypubport.%d pushaddr.%s\n",myipaddr,LP_mypubsock,LP_mypullsock,RPC_port,mypullport,mypubport,pushaddr);
LP_passphrase_init(passphrase,jstr(argjson,"gui"),juint(argjson,"netid"),jstr(argjson,"seednode")); LP_passphrase_init(passphrase,jstr(argjson,"gui"),juint(argjson,"netid"),jstr(argjson,"seednode"));
#ifndef FROM_JS #ifndef FROM_JS
if ( IAMLP != 0 && OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_psockloop,(void *)myipaddr) != 0 ) if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_psockloop,(void *)myipaddr) != 0 )
{ {
printf("error launching LP_psockloop for (%s)\n",myipaddr); printf("error launching LP_psockloop for (%s)\n",myipaddr);
exit(-1); exit(-1);
@ -1413,6 +1461,11 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
printf("error launching LP_swapsloop for ctx.%p\n",ctx); printf("error launching LP_swapsloop for ctx.%p\n",ctx);
exit(-1); exit(-1);
} }
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_commandQ_loop,ctx) != 0 )
{
printf("error launching LP_commandQ_loop for ctx.%p\n",ctx);
exit(-1);
}
int32_t nonz,didremote=0; int32_t nonz,didremote=0;
LP_statslog_parse(); LP_statslog_parse();
bitcoind_RPC_inittime = 0; bitcoind_RPC_inittime = 0;
@ -1485,7 +1538,10 @@ char *barterDEX(char *argstr)
printf("barterDEX.(%s)\n",argstr); printf("barterDEX.(%s)\n",argstr);
if ( (argjson= cJSON_Parse(argstr)) != 0 ) if ( (argjson= cJSON_Parse(argstr)) != 0 )
{ {
retstr = LP_command_process(ctx,LP_myipaddr,LP_mypubsock,argjson,(uint8_t *)argstr,(int32_t)strlen(argstr)); LP_queuecommand(&retstr,argstr,LP_mypubsock);
//retstr = LP_command_process(ctx,LP_myipaddr,LP_mypubsock,argjson,(uint8_t *)argstr,(int32_t)strlen(argstr));
while ( retstr == 0 )
usleep(50000);
free_json(argjson); free_json(argjson);
} else retstr = clonestr("{\"error\":\"couldnt parse request\"}"); } else retstr = clonestr("{\"error\":\"couldnt parse request\"}");
return(retstr); return(retstr);

342
iguana/exchanges/LP_network.c

@ -21,12 +21,12 @@
struct psock struct psock
{ {
uint32_t lasttime,lastping,errors; uint32_t lasttime,lastping,errors;
int32_t publicsock,sendsock,ispaired; int32_t publicsock,sendsock,ispaired,cmdchannel;
uint16_t publicport,sendport; uint16_t publicport,sendport;
char sendaddr[128],publicaddr[128]; char sendaddr[128],publicaddr[128];
} *PSOCKS; } *PSOCKS;
uint16_t Numpsocks,Psockport = MIN_PSOCK_PORT; uint16_t Numpsocks,Psockport = MIN_PSOCK_PORT,Pcmdport = MAX_PSOCK_PORT;
#ifdef FROM_JS #ifdef FROM_JS
@ -299,16 +299,18 @@ void LP_queuesend(uint32_t crc32,int32_t pubsock,char *base,char *rel,uint8_t *m
void LP_broadcast_finish(int32_t pubsock,char *base,char *rel,uint8_t *msg,cJSON *argjson,uint32_t crc32) void LP_broadcast_finish(int32_t pubsock,char *base,char *rel,uint8_t *msg,cJSON *argjson,uint32_t crc32)
{ {
int32_t msglen; int32_t msglen; char *method;
if ( (method= jstr(argjson,"method")) == 0 )
return;
msg = (void *)jprint(argjson,0); msg = (void *)jprint(argjson,0);
msglen = (int32_t)strlen((char *)msg) + 1; msglen = (int32_t)strlen((char *)msg) + 1;
if ( crc32 == 0 ) if ( crc32 == 0 )
crc32 = calc_crc32(0,&msg[2],msglen - 2); crc32 = calc_crc32(0,&msg[2],msglen - 2);
//printf("crc32.%x IAMLP.%d pubsock.%d\n",crc32,G.LP_IAMLP,pubsock); //printf("crc32.%x IAMLP.%d pubsock.%d\n",crc32,G.LP_IAMLP,pubsock);
#ifdef FROM_MARKETMAKER #ifdef FROM_MARKETMAKER
if ( G.LP_IAMLP == 0 || pubsock < 0 ) if ( (G.LP_IAMLP == 0 || pubsock < 0) && strcmp(method,"psock") != 0 )
#else #else
if ( IAMLP == 0 || pubsock < 0 ) if ( (IAMLP == 0 || pubsock < 0 && strcmp(method,"psock") != 0 )
#endif #endif
{ {
free(msg); free(msg);
@ -412,10 +414,87 @@ uint32_t LP_swapsend(int32_t pairsock,struct basilisk_swap *swap,uint32_t msgbit
return(nextbits); return(nextbits);
} }
void LP_psockloop(void *_ptr) // printouts seem to be needed for forwarding to work struct LP_queuedcommand
{
struct LP_queuedcommand *next,*prev;
char **retstrp;
int32_t responsesock,msglen,stats_JSONonly;
char msg[];
} *LP_commandQ;
void LP_commandQ_loop(void *ctx)
{
struct LP_queuedcommand *ptr,*tmp; int32_t size,nonz; char *retstr; cJSON *argjson;
while ( LP_STOP_RECEIVED == 0 )
{
nonz = 0;
DL_FOREACH_SAFE(LP_commandQ,ptr,tmp)
{
nonz++;
portable_mutex_lock(&LP_commandQmutex);
DL_DELETE(LP_commandQ,ptr);
portable_mutex_unlock(&LP_commandQmutex);
if ( (argjson= cJSON_Parse(ptr->msg)) != 0 )
{
if ( (retstr= LP_command_process(ctx,"127.0.0.1",ptr->responsesock,argjson,(uint8_t *)ptr->msg,ptr->msglen,ptr->stats_JSONonly)) != 0 )
{
//printf("processed.(%s)\n",retstr);
if ( ptr->responsesock >= 0 && (size= nn_send(ptr->responsesock,retstr,(int32_t)strlen(retstr)+1,0)) <= 0 )
printf("error sending result\n");
if ( ptr->retstrp != 0 )
(*ptr->retstrp) = retstr;
else free(retstr);
}
else if ( ptr->retstrp != 0 )
(*ptr->retstrp) = clonestr("{\"error\":\"timeout\"}");
free_json(argjson);
}
free(ptr);
}
if ( nonz == 0 )
usleep(50000);
}
}
void LP_queuecommand(char **retstrp,char *buf,int32_t responsesock,int32_t stats_JSONonly)
{
struct LP_queuedcommand *ptr; int32_t msglen;
msglen = (int32_t)strlen(buf) + 1;
portable_mutex_lock(&LP_commandQmutex);
ptr = calloc(1,sizeof(*ptr) + msglen);
if ( (ptr->retstrp= retstrp) != 0 )
*retstrp = 0;
ptr->responsesock = responsesock;
ptr->msglen = msglen;
ptr->stats_JSONonly = stats_JSONonly;
memcpy(ptr->msg,buf,msglen);
DL_APPEND(LP_commandQ,ptr);
portable_mutex_unlock(&LP_commandQmutex);
}
void mynn_close(int32_t sock)
{
struct nn_pollfd pfd; int32_t n; void *buf;
if ( sock >= 0 )
{
while ( (n= nn_recv(sock,&buf,NN_MSG,0)) > 0 )
printf("got n.%d bytes from nn_close(%d)\n",n,sock);
pfd.fd = sock;
pfd.events = POLLOUT;
while ( nn_poll(&pfd,1,100) > 0 )
{
printf("cant send to nn_close(%d)\n",sock);
sleep(1);
}
if ( IAMLP != 0 )
nn_close(sock);
}
}
void LP_psockloop(void *_ptr)
{ {
static struct nn_pollfd *pfds; static struct nn_pollfd *pfds;
int32_t i,n,nonz,iter,retval,sentbytes,size=0,sendsock = -1; uint32_t now; struct psock *ptr=0; void *buf=0; char keepalive[512]; int32_t nexti=0,j,i,n,nonz,iter,retval,sentbytes,size=0,sendsock = -1; uint32_t now; struct psock *ptr=0; void *buf=0; char keepalive[512];
strcpy(LP_psockloop_stats.name,"LP_psockloop"); strcpy(LP_psockloop_stats.name,"LP_psockloop");
LP_psockloop_stats.threshold = 1000.; LP_psockloop_stats.threshold = 1000.;
while ( LP_STOP_RECEIVED == 0 ) while ( LP_STOP_RECEIVED == 0 )
@ -445,14 +524,16 @@ void LP_psockloop(void *_ptr) // printouts seem to be needed for forwarding to w
pfds = calloc(MAX_PSOCK_PORT,sizeof(*pfds)); pfds = calloc(MAX_PSOCK_PORT,sizeof(*pfds));
portable_mutex_lock(&LP_psockmutex); portable_mutex_lock(&LP_psockmutex);
memset(pfds,0,sizeof(*pfds) * ((Numpsocks*2 <= MAX_PSOCK_PORT) ? Numpsocks*2 : MAX_PSOCK_PORT)); memset(pfds,0,sizeof(*pfds) * ((Numpsocks*2 <= MAX_PSOCK_PORT) ? Numpsocks*2 : MAX_PSOCK_PORT));
for (iter=0; iter<2; iter++) for (iter=j=0; iter<2; iter++)
{ {
for (i=n=0; i<Numpsocks; i++) for (j=n=0; j<Numpsocks; j++)
{ {
i = (j + nexti) % Numpsocks;
ptr = &PSOCKS[i]; ptr = &PSOCKS[i];
if ( iter == 0 ) if ( iter == 0 )
{ {
pfds[n].fd = ptr->publicsock; pfds[n].fd = ptr->publicsock;
//printf("check sock.%d\n",ptr->publicsock);
pfds[n].events = POLLIN; pfds[n].events = POLLIN;
} }
else else
@ -460,16 +541,20 @@ void LP_psockloop(void *_ptr) // printouts seem to be needed for forwarding to w
if ( pfds[n].fd != ptr->publicsock ) if ( pfds[n].fd != ptr->publicsock )
{ {
printf("unexpected fd.%d mismatched publicsock.%d\n",pfds[n].fd,ptr->publicsock); printf("unexpected fd.%d mismatched publicsock.%d\n",pfds[n].fd,ptr->publicsock);
nexti = i+1;
break; break;
} }
else if ( (pfds[n].revents & POLLIN) != 0 ) else if ( (pfds[n].revents & POLLIN) != 0 )
{ {
//printf("publicsock.%d %s has pollin\n",ptr->publicsock,ptr->publicaddr); //printf("cmd.%d publicsock.%d %s has pollin\n",ptr->cmdchannel,ptr->publicsock,ptr->publicaddr);
buf = 0; buf = 0;
if ( (size= nn_recv(ptr->publicsock,&buf,NN_MSG,0)) > 0 ) if ( (size= nn_recv(ptr->publicsock,&buf,NN_MSG,0)) > 0 )
{ {
ptr->lasttime = now; ptr->lasttime = now;
if ( ptr->cmdchannel == 0 )
sendsock = ptr->sendsock; sendsock = ptr->sendsock;
else LP_queuecommand(0,(char *)buf,ptr->publicsock,0);
nexti = i+1;
break; break;
} }
else if ( buf != 0 ) else if ( buf != 0 )
@ -481,6 +566,8 @@ void LP_psockloop(void *_ptr) // printouts seem to be needed for forwarding to w
} }
} }
n++; n++;
if ( ptr->sendsock > 0 )
{
if ( iter == 0 ) if ( iter == 0 )
{ {
pfds[n].fd = ptr->sendsock; pfds[n].fd = ptr->sendsock;
@ -491,6 +578,7 @@ void LP_psockloop(void *_ptr) // printouts seem to be needed for forwarding to w
if ( pfds[n].fd != ptr->sendsock ) if ( pfds[n].fd != ptr->sendsock )
{ {
printf("unexpected fd.%d mismatched sendsock.%d\n",pfds[n].fd,ptr->sendsock); printf("unexpected fd.%d mismatched sendsock.%d\n",pfds[n].fd,ptr->sendsock);
nexti = i+1;
break; break;
} }
else if ( (pfds[n].revents & POLLIN) != 0 ) else if ( (pfds[n].revents & POLLIN) != 0 )
@ -502,6 +590,7 @@ void LP_psockloop(void *_ptr) // printouts seem to be needed for forwarding to w
if ( ptr->ispaired != 0 ) if ( ptr->ispaired != 0 )
{ {
sendsock = ptr->publicsock; sendsock = ptr->publicsock;
nexti = i+1;
break; break;
} }
} }
@ -515,71 +604,66 @@ void LP_psockloop(void *_ptr) // printouts seem to be needed for forwarding to w
} }
n++; n++;
} }
}
if ( iter == 0 ) if ( iter == 0 )
{ {
if ( (retval= nn_poll(pfds,n,1)) <= 0 ) if ( (retval= nn_poll(pfds,n,1)) <= 0 )
{ {
if ( retval != 0 ) //if ( retval != 0 )
printf("nn_poll retval.%d\n",retval); // printf("nn_poll retval.%d\n",retval);
break; break;
} else printf("num pfds.%d retval.%d\n",n,retval); } // else printf("num pfds.%d retval.%d\n",n,retval);
} }
} }
//free(pfds);
//printf("sendsock.%d Numpsocks.%d\n",sendsock,Numpsocks);
if ( sendsock < 0 ) if ( sendsock < 0 )
{ {
usleep(30000);
for (i=nonz=0; i<Numpsocks; i++) for (i=nonz=0; i<Numpsocks; i++)
{ {
if ( i < Numpsocks ) if ( i < Numpsocks )
{ {
ptr = &PSOCKS[i]; ptr = &PSOCKS[i];
if ( now > ptr->lasttime+PSOCK_KEEPALIVE ) if ( ptr->cmdchannel == 0 && now > ptr->lasttime+PSOCK_KEEPALIVE )
{ {
printf("PSOCKS[%d] of %d (%u %u) lag.%d IDLETIMEOUT\n",i,Numpsocks,ptr->publicport,ptr->sendport,now - ptr->lasttime); printf("PSOCKS[%d] of %d (%u %u) lag.%d IDLETIMEOUT\n",i,Numpsocks,ptr->publicport,ptr->sendport,now - ptr->lasttime);
if ( ptr->sendsock != ptr->publicsock && ptr->sendsock >= 0 )
mynn_close(ptr->sendsock), ptr->sendsock = -1;
if ( ptr->publicsock >= 0 ) if ( ptr->publicsock >= 0 )
nn_close(ptr->publicsock); mynn_close(ptr->publicsock), ptr->publicsock = -1;
if ( ptr->sendsock >= 0 ) nonz++;
nn_close(ptr->sendsock); }
//portable_mutex_lock(&LP_psockmutex); }
if ( Numpsocks > 1 )
{
PSOCKS[i] = PSOCKS[--Numpsocks];
memset(&PSOCKS[Numpsocks],0,sizeof(*ptr));
} else Numpsocks = 0;
//portable_mutex_unlock(&LP_psockmutex);
break;
} }
else if ( now > ptr->lastping+PSOCK_KEEPALIVE/2 && ptr->errors < 3 ) if ( nonz > 0 )
{ {
ptr->lastping = now; n = Numpsocks;
if ( 0 ) for (i=0; i<n; i++)
{ {
sendsock = ptr->sendsock; ptr = &PSOCKS[i];
sprintf(keepalive,"{\"method\":\"keepalive\",\"endpoint\":\"%s\"}",ptr->sendaddr); if ( ptr->sendsock < 0 && ptr->publicsock < 0 )
size = (int32_t)strlen(keepalive) + 1; {
buf = keepalive; for (j=i; j<n-1; j++)
printf("send keepalive.(%s)\n",keepalive); PSOCKS[j] = PSOCKS[j+1];
} n--;
break;
} }
} }
printf("PSOCKS purge nonz.%d n.%d vs Numpsocks.%d\n",nonz,n,Numpsocks);
Numpsocks = n;
} }
if ( nonz == 0 && i == Numpsocks )
usleep(100000);
} }
portable_mutex_unlock(&LP_psockmutex); portable_mutex_unlock(&LP_psockmutex);
} else usleep(100000); } else usleep(100000);
} }
} }
void LP_psockadd(int32_t ispaired,int32_t publicsock,uint16_t recvport,int32_t sendsock,uint16_t sendport,char *subaddr,char *publicaddr) void LP_psockadd(int32_t ispaired,int32_t publicsock,uint16_t recvport,int32_t sendsock,uint16_t sendport,char *subaddr,char *publicaddr,int32_t cmdchannel)
{ {
struct psock *ptr; struct psock *ptr;
portable_mutex_lock(&LP_psockmutex); portable_mutex_lock(&LP_psockmutex);
PSOCKS = realloc(PSOCKS,sizeof(*PSOCKS) * (Numpsocks + 1)); PSOCKS = realloc(PSOCKS,sizeof(*PSOCKS) * (Numpsocks + 1));
ptr = &PSOCKS[Numpsocks++]; ptr = &PSOCKS[Numpsocks++];
ptr->ispaired = ispaired; ptr->ispaired = ispaired;
ptr->cmdchannel = cmdchannel;
ptr->publicsock = publicsock; ptr->publicsock = publicsock;
ptr->publicport = recvport; ptr->publicport = recvport;
ptr->sendsock = sendsock; ptr->sendsock = sendsock;
@ -609,62 +693,126 @@ int32_t LP_psockmark(char *publicaddr)
return(retval); return(retval);
} }
char *LP_psock(char *myipaddr,int32_t ispaired) char *_LP_psock_create(int32_t *pullsockp,int32_t *pubsockp,char *ipaddr,uint16_t publicport,uint16_t subport,int32_t ispaired,int32_t cmdchannel,bits256 pubkey)
{ {
char pushaddr[128],subaddr[128]; uint16_t i,publicport,subport,maxiters=100; int32_t timeout,pullsock=-1,pubsock=-1; cJSON *retjson=0; int32_t i,pullsock,bindflag=(IAMLP != 0),pubsock,arg; struct LP_pubkey_info *pubp; char pushaddr[128],subaddr[128]; cJSON *retjson = 0;
retjson = cJSON_CreateObject();
publicport = Psockport++;
subport = Psockport++;
for (i=0; i<maxiters; i++,publicport+=2,subport+=2)
{
if ( publicport < MIN_PSOCK_PORT )
publicport = MIN_PSOCK_PORT+1;
if ( subport <= publicport )
subport = publicport + 1;
pullsock = pubsock = -1; pullsock = pubsock = -1;
nanomsg_transportname(1,pushaddr,myipaddr,publicport); *pullsockp = *pubsockp = -1;
nanomsg_transportname(1,subaddr,myipaddr,subport); if ( IAMLP != 0 && bits256_nonz(pubkey) != 0 )
if ( (pullsock= nn_socket(AF_SP,ispaired!=0?NN_PAIR:NN_PULL)) >= 0 && (pubsock= nn_socket(AF_SP,ispaired!=0?NN_PAIR:NN_PAIR)) >= 0 )
{ {
if ( nn_bind(pullsock,pushaddr) >= 0 && nn_bind(pubsock,subaddr) >= 0 ) char str[65];
if ( (pubp= LP_pubkeyadd(pubkey)) != 0 )
{ {
timeout = 1; if ( pubp->pairsock >= 0 )
nn_setsockopt(pubsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout));
nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout));
if ( ispaired != 0 )
{ {
//maxsize = 1024 * 1024; printf("%s already has pairsock.%d\n",bits256_str(str,pubkey),pubp->pairsock);
//nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVBUF,&maxsize,sizeof(maxsize)); portable_mutex_lock(&LP_psockmutex);
} for (i=0; i<Numpsocks; i++)
//if ( ispaired != 0 ) if ( PSOCKS[i].publicsock == pubp->pairsock )
{ {
nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); //PSOCKS[i].lasttime = (uint32_t)time(NULL) - PSOCK_KEEPALIVE - 1;
nn_setsockopt(pubsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); retjson = cJSON_CreateObject();
jaddstr(retjson,"result","success");
jaddstr(retjson,"LPipaddr",ipaddr);
jaddstr(retjson,"connectaddr",PSOCKS[i].sendaddr);
jaddnum(retjson,"connectport",PSOCKS[i].sendport);
jaddnum(retjson,"ispaired",PSOCKS[i].ispaired);
jaddnum(retjson,"cmdchannel",PSOCKS[i].cmdchannel);
jaddstr(retjson,"publicaddr",PSOCKS[i].publicaddr);
jaddnum(retjson,"publicport",PSOCKS[i].publicport);
//printf("cmd.%d publicaddr.(%s) for subaddr.(%s), pullsock.%d pubsock.%d\n",cmdchannel,pushaddr,subaddr,pullsock,pubsock);
*pullsockp = pullsock;
*pubsockp = pubsock;
portable_mutex_unlock(&LP_psockmutex);
return(jprint(retjson,1));
}
portable_mutex_unlock(&LP_psockmutex);
}
//printf("pairsock for %s <- %d\n",bits256_str(str,pubkey),pullsock);
//pubp->pairsock = pullsock;
} }
nanomsg_transportname(0,pushaddr,myipaddr,publicport); }
nanomsg_transportname(0,subaddr,myipaddr,subport); nanomsg_transportname(bindflag,pushaddr,ipaddr,publicport);
LP_psockadd(ispaired,pullsock,publicport,pubsock,subport,subaddr,pushaddr); nanomsg_transportname(bindflag,subaddr,ipaddr,subport);
if ( (pullsock= nn_socket(AF_SP,ispaired!=0?NN_PAIR:NN_PULL)) >= 0 && (cmdchannel != 0 ||(pubsock= nn_socket(AF_SP,ispaired!=0?NN_PAIR:NN_PAIR)) >= 0) )
{
if ( nn_bind(pullsock,pushaddr) >= 0 && (cmdchannel != 0 || nn_bind(pubsock,subaddr) >= 0) )
{
arg = 100;
nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_SNDTIMEO,&arg,sizeof(arg));
if ( pubsock >= 0 )
nn_setsockopt(pubsock,NN_SOL_SOCKET,NN_SNDTIMEO,&arg,sizeof(arg));
arg = 1;
nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVTIMEO,&arg,sizeof(arg));
if ( pubsock >= 0 )
nn_setsockopt(pubsock,NN_SOL_SOCKET,NN_RCVTIMEO,&arg,sizeof(arg));
//arg = 2;
//nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_MAXTTL,&arg,sizeof(arg));
//if ( pubsock >= 0 )
// nn_setsockopt(pubsock,NN_SOL_SOCKET,NN_MAXTTL,&arg,sizeof(arg));
nanomsg_transportname(0,pushaddr,ipaddr,publicport);
nanomsg_transportname(0,subaddr,ipaddr,subport);
LP_psockadd(ispaired,pullsock,publicport,pubsock,subport,subaddr,pushaddr,cmdchannel);
retjson = cJSON_CreateObject();
jaddstr(retjson,"result","success"); jaddstr(retjson,"result","success");
jaddstr(retjson,"LPipaddr",myipaddr); jaddstr(retjson,"LPipaddr",ipaddr);
jaddstr(retjson,"connectaddr",subaddr); jaddstr(retjson,"connectaddr",subaddr);
jaddnum(retjson,"connectport",subport); jaddnum(retjson,"connectport",subport);
jaddnum(retjson,"ispaired",ispaired); jaddnum(retjson,"ispaired",ispaired);
jaddnum(retjson,"cmdchannel",cmdchannel);
jaddstr(retjson,"publicaddr",pushaddr); jaddstr(retjson,"publicaddr",pushaddr);
jaddnum(retjson,"publicport",publicport); jaddnum(retjson,"publicport",publicport);
printf("i.%d publicaddr.(%s) for subaddr.(%s), pullsock.%d pubsock.%d\n",i,pushaddr,subaddr,pullsock,pubsock); //printf("cmd.%d publicaddr.(%s) for subaddr.(%s), pullsock.%d pubsock.%d\n",cmdchannel,pushaddr,subaddr,pullsock,pubsock);
break; *pullsockp = pullsock;
} else printf("bind error on %s or %s\n",pushaddr,subaddr); *pubsockp = pubsock;
return(jprint(retjson,1));
} //else printf("bind error on %s or %s\n",pushaddr,subaddr);
if ( pullsock >= 0 ) if ( pullsock >= 0 )
nn_close(pullsock); nn_close(pullsock);
if ( pubsock >= 0 ) if ( pubsock >= 0 )
nn_close(pubsock); nn_close(pubsock);
} }
return(0);
}
char *LP_psock(int32_t *pullsockp,char *ipaddr,int32_t ispaired,int32_t cmdchannel,bits256 pubkey)
{
char *retstr=0; uint16_t i,publicport,subport,maxport; int32_t pubsock=-1;
*pullsockp = -1;
//printf("LP_psock ipaddr.%s ispaird.%d cmdchannel.%d\n",ipaddr,ispaired,cmdchannel);
if ( cmdchannel == 0 )
{
maxport = MAX_PSOCK_PORT;
publicport = Psockport++;
subport = Psockport++;
}
else
{
if ( cmdchannel != 0 && bits256_nonz(pubkey) == 0 )
return(clonestr("{\"error\",\"cant do pairsock for null pubkey\"}"));
maxport = 65534;
publicport = subport = Pcmdport++;
}
for (i=0; i<maxport; i++)
{
if ( publicport < MIN_PSOCK_PORT )
publicport = MIN_PSOCK_PORT+1;
if ( cmdchannel == 0 && subport <= publicport )
subport = publicport + 1;
if ( (retstr= _LP_psock_create(pullsockp,&pubsock,ipaddr,publicport,subport,ispaired,cmdchannel,pubkey)) != 0 )
{
//printf("LP_psock returns.(%s)\n",retstr);
return(retstr);
}
if ( cmdchannel == 0 )
publicport+=2, subport+=2;
else publicport++, subport++;
} }
if ( Psockport > MAX_PSOCK_PORT ) if ( Psockport >= MAX_PSOCK_PORT )
Psockport = MIN_PSOCK_PORT; Psockport = MIN_PSOCK_PORT;
if ( i == maxiters ) if ( Pcmdport >= 65534 )
jaddstr(retjson,"error","cant find psock ports"); Pcmdport = MAX_PSOCK_PORT;
return(jprint(retjson,1)); return(clonestr("{\"error\",\"cant find psock ports\"}"));
} }
/* /*
@ -678,51 +826,50 @@ char *LP_psock(char *myipaddr,int32_t ispaired)
*/ */
char *issue_LP_psock(char *destip,uint16_t destport,int32_t ispaired) char *issue_LP_psock(char *destip,uint16_t destport,int32_t ispaired,int32_t cmdchannel)
{ {
char url[512],*retstr; char str[65],url[512],*retstr;
sprintf(url,"http://%s:%u/api/stats/psock?ispaired=%d",destip,destport-1,ispaired); sprintf(url,"http://%s:%u/api/stats/psock?ispaired=%d&cmdchannel=%d&pubkey=%s",destip,destport-1,ispaired,cmdchannel,bits256_str(str,G.LP_mypub25519));
//return(LP_issue_curl("psock",destip,destport,url)); //return(LP_issue_curl("psock",destip,destport,url));
retstr = issue_curlt(url,LP_HTTP_TIMEOUT*3); retstr = issue_curlt(url,LP_HTTP_TIMEOUT*10);
printf("issue_LP_psock got (%s) from %s\n",retstr,destip); printf("issue_LP_psock got (%s) from %s\n",retstr,url); // this is needed?!
return(retstr); return(retstr);
} }
uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired) uint16_t LP_psock_get(char *connectaddr,char *publicaddr,int32_t ispaired,int32_t cmdchannel,char *ipaddr)
{ {
uint16_t publicport = 0; char *retstr,*addr; cJSON *retjson; struct LP_peerinfo *peer,*tmp; uint16_t publicport = 0; char *retstr,*addr; cJSON *retjson; struct LP_peerinfo *peer,*tmp;
connectaddr[0] = publicaddr[0] = 0;
HASH_ITER(hh,LP_peerinfos,peer,tmp) HASH_ITER(hh,LP_peerinfos,peer,tmp)
{ {
if ( ipaddr != 0 && strcmp(ipaddr,peer->ipaddr) != 0 )
continue;
connectaddr[0] = publicaddr[0] = 0; connectaddr[0] = publicaddr[0] = 0;
if ( peer->errors < LP_MAXPEER_ERRORS && (retstr= issue_LP_psock(peer->ipaddr,peer->port,ispaired)) != 0 ) if ( peer->errors < LP_MAXPEER_ERRORS && (retstr= issue_LP_psock(peer->ipaddr,peer->port,ispaired,cmdchannel)) != 0 )
{ {
if ( (retjson= cJSON_Parse(retstr)) != 0 ) if ( (retjson= cJSON_Parse(retstr)) != 0 )
{ {
printf("from %s:%u (%s)\n",peer->ipaddr,peer->port,retstr); //printf("from %s:%u (%s)\n",peer->ipaddr,peer->port,retstr);
if ( (addr= jstr(retjson,"publicaddr")) != 0 ) if ( (addr= jstr(retjson,"publicaddr")) != 0 )
safecopy(publicaddr,addr,128); safecopy(publicaddr,addr,128);
if ( (addr= jstr(retjson,"connectaddr")) != 0 ) if ( (addr= jstr(retjson,"connectaddr")) != 0 )
safecopy(connectaddr,addr,128); safecopy(connectaddr,addr,128);
//if ( (addr= jstr(retjson,"connectaddr2")) != 0 )
// safecopy(connectaddr2,addr,128);
if ( publicaddr[0] != 0 && connectaddr[0] != 0 ) if ( publicaddr[0] != 0 && connectaddr[0] != 0 )
publicport = juint(retjson,"publicport"); publicport = juint(retjson,"publicport");
free_json(retjson); free_json(retjson);
} }
printf("got.(%s) connect.%s public.%s\n",retstr,connectaddr,publicaddr); //printf("got.(%s) connect.%s public.%s publicport.%u\n",retstr,connectaddr,publicaddr,publicport);
free(retstr); free(retstr);
} else printf("error psock from %s:%u\n",peer->ipaddr,peer->port);
if ( publicport != 0 )
break;
}
return(publicport); return(publicport);
} //else printf("error psock from %s:%u\n",peer->ipaddr,peer->port);
}
return(0);
} }
int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char *myipaddr,uint16_t mypullport,int32_t ispaired) int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char *myipaddr,uint16_t mypullport,int32_t ispaired)
{ {
int32_t nntype,pullsock,timeout; char bindaddr[128],connectaddr[128]; int32_t nntype,pullsock,timeout; char bindaddr[128],connectaddr[128];
*mypullportp = mypullport; *mypullportp = mypullport;
//connectaddr2[0] = 0;
if ( ispaired == 0 ) if ( ispaired == 0 )
{ {
if ( LP_canbind != 0 ) if ( LP_canbind != 0 )
@ -733,7 +880,6 @@ int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char
{ {
nanomsg_transportname(0,publicaddr,myipaddr,mypullport); nanomsg_transportname(0,publicaddr,myipaddr,mypullport);
nanomsg_transportname(1,bindaddr,myipaddr,mypullport); nanomsg_transportname(1,bindaddr,myipaddr,mypullport);
//nanomsg_transportname2(1,bindaddr2,myipaddr,mypullport);
} }
else else
{ {
@ -745,7 +891,7 @@ int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char
} }
while ( *mypullportp == 0 ) while ( *mypullportp == 0 )
{ {
if ( (*mypullportp= LP_psock_get(connectaddr,publicaddr,ispaired)) != 0 ) if ( (*mypullportp= LP_psock_get(connectaddr,publicaddr,ispaired,0,0)) != 0 )
break; break;
sleep(10); sleep(10);
printf("try to get publicaddr again\n"); printf("try to get publicaddr again\n");
@ -764,8 +910,6 @@ int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char
} }
else else
{ {
//if ( connectaddr2[0] != 0 && nn_connect(pullsock,connectaddr2) > 0 )
// printf("%s ",connectaddr2);
printf("nntype.%d NN_PAIR.%d connect to %s connectsock.%d\n",nntype,NN_PAIR,connectaddr,pullsock); printf("nntype.%d NN_PAIR.%d connect to %s connectsock.%d\n",nntype,NN_PAIR,connectaddr,pullsock);
} }
} }
@ -776,14 +920,10 @@ int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char
printf("bind to %s error for %s: %s\n",bindaddr,publicaddr,nn_strerror(nn_errno())); printf("bind to %s error for %s: %s\n",bindaddr,publicaddr,nn_strerror(nn_errno()));
exit(-1); exit(-1);
} }
//if ( nn_bind(pullsock,bindaddr2) >= 0 )
// printf("bound to %s\n",bindaddr2);
} }
timeout = 1; timeout = 100;
nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout));
nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout));
//maxsize = 2 * 1024 * 1024;
//nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVBUF,&maxsize,sizeof(maxsize));
if ( nntype == NN_SUB ) if ( nntype == NN_SUB )
nn_setsockopt(pullsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); nn_setsockopt(pullsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0);
} }

253
iguana/exchanges/LP_ordermatch.c

@ -230,7 +230,7 @@ int32_t LP_nanobind(void *ctx,char *pairstr)
printf("error creating utxo->pair\n"); printf("error creating utxo->pair\n");
else else
{ {
for (i=0; i<10; i++) for (i=0; i<10000; i++)
{ {
r = (10000 + (LP_rand() % 50000)) & 0xffff; r = (10000 + (LP_rand() % 50000)) & 0xffff;
if ( LP_fixed_pairport != 0 ) if ( LP_fixed_pairport != 0 )
@ -242,12 +242,15 @@ int32_t LP_nanobind(void *ctx,char *pairstr)
//timeout = 1; //timeout = 1;
//nn_setsockopt(pairsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); //nn_setsockopt(pairsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout));
//nn_setsockopt(pairsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); //nn_setsockopt(pairsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout));
printf("nanobind %s to %d\n",pairstr,pairsock); //printf("nanobind %s to %d\n",pairstr,pairsock);
return(pairsock); return(pairsock);
} else printf("error binding to %s for %s\n",bindaddr,pairstr); } // else printf("error binding to %s for %s\n",bindaddr,pairstr);
if ( LP_fixed_pairport != 0 ) if ( LP_fixed_pairport != 0 )
break; break;
} }
printf("%d ports all used\n",i);
nn_close(pairsock);
pairsock = -1;
} }
} else pairsock = LP_initpublicaddr(ctx,&mypullport,pairstr,"127.0.0.1",0,1); } else pairsock = LP_initpublicaddr(ctx,&mypullport,pairstr,"127.0.0.1",0,1);
return(pairsock); return(pairsock);
@ -411,7 +414,7 @@ struct LP_utxoinfo *LP_address_myutxopair(struct LP_utxoinfo *butxo,int32_t iamb
{ {
up = utxos[mini]; up = utxos[mini];
utxos[mini] = 0; utxos[mini] = 0;
printf("found mini.%d %.8f for targetval %.8f -> targetval2 %.8f, ratio %.2f\n",mini,dstr(up->U.value),dstr(targetval),dstr(targetval2),(double)up->U.value/targetval); //printf("found mini.%d %.8f for targetval %.8f -> targetval2 %.8f, ratio %.2f\n",mini,dstr(up->U.value),dstr(targetval),dstr(targetval2),(double)up->U.value/targetval);
if ( (double)up->U.value/targetval < ratio-1 ) if ( (double)up->U.value/targetval < ratio-1 )
{ {
@ -430,7 +433,7 @@ struct LP_utxoinfo *LP_address_myutxopair(struct LP_utxoinfo *butxo,int32_t iamb
LP_butxo_set(butxo,iambob,coin,up,up2,targetval); LP_butxo_set(butxo,iambob,coin,up,up2,targetval);
return(butxo); return(butxo);
} else printf("cant find utxos[mini %d]\n",mini); } else printf("cant find utxos[mini %d]\n",mini);
} else printf("cant find targetval2 %.8f\n",dstr(targetval2)); } //else printf("cant find targetval2 %.8f\n",dstr(targetval2));
} else printf("failed ratio test %.8f\n",(double)up->U.value/targetval); } else printf("failed ratio test %.8f\n",(double)up->U.value/targetval);
} else if ( targetval != 0 && mini >= 0 ) } else if ( targetval != 0 && mini >= 0 )
printf("targetval %.8f mini.%d\n",dstr(targetval),mini); printf("targetval %.8f mini.%d\n",dstr(targetval),mini);
@ -444,7 +447,7 @@ struct LP_utxoinfo *LP_address_myutxopair(struct LP_utxoinfo *butxo,int32_t iamb
int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double price,struct LP_quoteinfo *qp) int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double price,struct LP_quoteinfo *qp)
{ {
char pairstr[512],otheraddr[64]; cJSON *reqjson; bits256 privkey; int32_t pair=-1,retval = -1,DEXselector = 0; struct basilisk_swap *swap; struct iguana_info *coin,*kmdcoin; char pairstr[512],otheraddr[64]; cJSON *reqjson; bits256 privkey; int32_t pair=-1,retval = -1,DEXselector = 0; int64_t dtrust; struct basilisk_swap *swap; struct iguana_info *coin,*kmdcoin;
qp->quotetime = (uint32_t)time(NULL); qp->quotetime = (uint32_t)time(NULL);
if ( (coin= LP_coinfind(qp->srccoin)) == 0 ) if ( (coin= LP_coinfind(qp->srccoin)) == 0 )
{ {
@ -455,12 +458,13 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double
if ( bits256_nonz(privkey) != 0 && bits256_cmp(G.LP_mypub25519,qp->srchash) == 0 ) if ( bits256_nonz(privkey) != 0 && bits256_cmp(G.LP_mypub25519,qp->srchash) == 0 )
{ {
LP_requestinit(&qp->R,qp->srchash,qp->desthash,base,qp->satoshis-qp->txfee,rel,qp->destsatoshis-qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector); LP_requestinit(&qp->R,qp->srchash,qp->desthash,base,qp->satoshis-qp->txfee,rel,qp->destsatoshis-qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector);
if ( LP_pendingswap(qp->R.requestid,qp->R.quoteid) > 0 ) /*if ( LP_pendingswap(qp->R.requestid,qp->R.quoteid) > 0 )
{ {
printf("requestid.%u quoteid.%u is already in progres\n",qp->R.requestid,qp->R.quoteid); printf("requestid.%u quoteid.%u is already in progres\n",qp->R.requestid,qp->R.quoteid);
return(-1); return(-1);
} }*/
if ( (swap= LP_swapinit(1,0,privkey,&qp->R,qp,LP_dynamictrust(qp->othercredits,qp->desthash,LP_kmdvalue(qp->destcoin,qp->destsatoshis)) > 0)) == 0 ) dtrust = LP_dynamictrust(qp->othercredits,qp->desthash,LP_kmdvalue(qp->destcoin,qp->destsatoshis));
if ( (swap= LP_swapinit(1,0,privkey,&qp->R,qp,dtrust > 0)) == 0 )
{ {
printf("cant initialize swap\n"); printf("cant initialize swap\n");
return(-1); return(-1);
@ -468,8 +472,6 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double
if ( (pair= LP_nanobind(ctx,pairstr)) >= 0 ) if ( (pair= LP_nanobind(ctx,pairstr)) >= 0 )
{ {
swap->N.pair = pair; swap->N.pair = pair;
//utxo->S.swap = swap;
//swap->utxo = utxo;
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_bobloop,(void *)swap) == 0 ) if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_bobloop,(void *)swap) == 0 )
{ {
reqjson = LP_quotejson(qp); reqjson = LP_quotejson(qp);
@ -477,14 +479,16 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double
jaddstr(reqjson,"pair",pairstr); jaddstr(reqjson,"pair",pairstr);
if ( (kmdcoin= LP_coinfind("KMD")) != 0 ) if ( (kmdcoin= LP_coinfind("KMD")) != 0 )
jadd(reqjson,"proof",LP_instantdex_txids(0,kmdcoin->smartaddr)); jadd(reqjson,"proof",LP_instantdex_txids(0,kmdcoin->smartaddr));
char str[65]; printf("BOB pubsock.%d binds to %d (%s)\n",pubsock,pair,bits256_str(str,qp->desthash)); //char str[65]; printf("BOB pubsock.%d binds to %d (%s)\n",pubsock,pair,bits256_str(str,qp->desthash));
bits256 zero; bits256 zero;
memset(zero.bytes,0,sizeof(zero)); memset(zero.bytes,0,sizeof(zero));
LP_reserved_msg(1,base,rel,zero,jprint(reqjson,0));
sleep(1);
LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0)); LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0));
sleep(1); LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0));
if ( 0 )
{
LP_reserved_msg(1,base,rel,zero,jprint(reqjson,0));
LP_reserved_msg(0,base,rel,zero,jprint(reqjson,0)); LP_reserved_msg(0,base,rel,zero,jprint(reqjson,0));
}
free_json(reqjson); free_json(reqjson);
LP_importaddress(qp->destcoin,qp->destaddr); LP_importaddress(qp->destcoin,qp->destaddr);
LP_otheraddress(qp->srccoin,otheraddr,qp->destcoin,qp->destaddr); LP_otheraddress(qp->srccoin,otheraddr,qp->destcoin,qp->destaddr);
@ -512,6 +516,7 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q
qp->aliceid = LP_aliceid_calc(qp->desttxid,qp->destvout,qp->feetxid,qp->feevout); qp->aliceid = LP_aliceid_calc(qp->desttxid,qp->destvout,qp->feetxid,qp->feevout);
if ( (qp->tradeid= tradeid) == 0 ) if ( (qp->tradeid= tradeid) == 0 )
qp->tradeid = LP_rand(); qp->tradeid = LP_rand();
qp->srchash = destpubkey;
LP_query(ctx,myipaddr,mypubsock,"request",qp); LP_query(ctx,myipaddr,mypubsock,"request",qp);
LP_Alicequery = *qp, LP_Alicemaxprice = maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = destpubkey; LP_Alicequery = *qp, LP_Alicemaxprice = maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = destpubkey;
char str[65]; printf("LP_trade %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f\n",qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),maxprice); char str[65]; printf("LP_trade %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f\n",qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),maxprice);
@ -571,13 +576,13 @@ char *LP_connectedalice(struct LP_quoteinfo *qp,char *pairstr) // alice
printf("CONNECTED numpending.%d tradeid.%u requestid.%u quoteid.%u pairstr.%s\n",G.LP_pendingswaps,qp->tradeid,qp->R.requestid,qp->R.quoteid,pairstr!=0?pairstr:""); printf("CONNECTED numpending.%d tradeid.%u requestid.%u quoteid.%u pairstr.%s\n",G.LP_pendingswaps,qp->tradeid,qp->R.requestid,qp->R.quoteid,pairstr!=0?pairstr:"");
LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis-qp->txfee,qp->destcoin,qp->destsatoshis-qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector); LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis-qp->txfee,qp->destcoin,qp->destsatoshis-qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector);
//printf("calculated requestid.%u quoteid.%u\n",qp->R.requestid,qp->R.quoteid); //printf("calculated requestid.%u quoteid.%u\n",qp->R.requestid,qp->R.quoteid);
if ( LP_pendingswap(qp->R.requestid,qp->R.quoteid) > 0 ) /*if ( LP_pendingswap(qp->R.requestid,qp->R.quoteid) > 0 )
{ {
printf("requestid.%u quoteid.%u is already in progres\n",qp->R.requestid,qp->R.quoteid); printf("requestid.%u quoteid.%u is already in progres\n",qp->R.requestid,qp->R.quoteid);
retjson = cJSON_CreateObject(); retjson = cJSON_CreateObject();
jaddstr(retjson,"error","swap already in progress"); jaddstr(retjson,"error","swap already in progress");
return(jprint(retjson,1)); return(jprint(retjson,1));
} }*/
/*if ( LP_quotecmp(1,qp,&LP_Alicereserved) == 0 ) /*if ( LP_quotecmp(1,qp,&LP_Alicereserved) == 0 )
{ {
printf("mismatched between reserved and connected\n"); printf("mismatched between reserved and connected\n");
@ -662,7 +667,7 @@ char *LP_connectedalice(struct LP_quoteinfo *qp,char *pairstr) // alice
LP_aliceid(qp->tradeid,qp->aliceid,"error10",qp->R.requestid,qp->R.quoteid); LP_aliceid(qp->tradeid,qp->aliceid,"error10",qp->R.requestid,qp->R.quoteid);
printf("connect error %s\n",nn_strerror(nn_errno())); printf("connect error %s\n",nn_strerror(nn_errno()));
} }
printf("connected result.(%s)\n",jprint(retjson,0)); //printf("connected result.(%s)\n",jprint(retjson,0));
if ( jobj(retjson,"error") != 0 ) if ( jobj(retjson,"error") != 0 )
{ {
LP_availableset(qp->desttxid,qp->vout); LP_availableset(qp->desttxid,qp->vout);
@ -702,7 +707,7 @@ int32_t LP_validSPV(char *symbol,char *coinaddr,bits256 txid,int32_t vout)
if ( vout < tx->numvouts && tx->height > 0 ) if ( vout < tx->numvouts && tx->height > 0 )
{ {
printf("added missing utxo for SPV checking\n"); printf("added missing utxo for SPV checking\n");
LP_address_utxoadd((uint32_t)time(NULL),"LP_validSPV",coin,coinaddr,txid,vout,tx->outpoints[vout].value,tx->height,-1); LP_address_utxoadd(0,(uint32_t)time(NULL),"LP_validSPV",coin,coinaddr,txid,vout,tx->outpoints[vout].value,tx->height,-1);
} }
} }
} }
@ -767,7 +772,7 @@ void LP_reserved(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo
qp->tradeid = LP_Alicequery.tradeid; qp->tradeid = LP_Alicequery.tradeid;
LP_Alicereserved = *qp; LP_Alicereserved = *qp;
LP_alicequery_clear(); LP_alicequery_clear();
printf("send CONNECT\n"); //printf("send CONNECT\n");
LP_query(ctx,myipaddr,mypubsock,"connect",qp); LP_query(ctx,myipaddr,mypubsock,"connect",qp);
} else printf("LP_reserved %llu price %.8f vs maxprice %.8f\n",(long long)qp->aliceid,price,maxprice); } else printf("LP_reserved %llu price %.8f vs maxprice %.8f\n",(long long)qp->aliceid,price,maxprice);
} //else printf("probably a timeout, reject reserved due to not eligible.%d or mismatched quote price %.8f vs maxprice %.8f\n",LP_alice_eligible(qp->quotetime),price,maxprice); } //else printf("probably a timeout, reject reserved due to not eligible.%d or mismatched quote price %.8f vs maxprice %.8f\n",LP_alice_eligible(qp->quotetime),price,maxprice);
@ -833,10 +838,9 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
double price,qprice,myprice,bestprice,range,bid,ask; struct iguana_info *coin; struct LP_utxoinfo A,B,*autxo,*butxo; cJSON *reqjson; char str[65]; struct LP_address_utxo *utxos[1000]; int32_t r,counter,max = (int32_t)(sizeof(utxos)/sizeof(*utxos)); double price,qprice,myprice,bestprice,range,bid,ask; struct iguana_info *coin; struct LP_utxoinfo A,B,*autxo,*butxo; cJSON *reqjson; char str[65]; struct LP_address_utxo *utxos[1000]; int32_t r,counter,max = (int32_t)(sizeof(utxos)/sizeof(*utxos));
*newqp = *qp; *newqp = *qp;
qp = newqp; qp = newqp;
//printf("bob %s received REQUEST.(%llu)\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid);
if ( (coin= LP_coinfind(qp->srccoin)) == 0 ) if ( (coin= LP_coinfind(qp->srccoin)) == 0 )
return(0); return(0);
//if ( strcmp(qp->srccoin,"GRS") == 0 || strcmp(qp->destcoin,"GRS") == 0 )
// printf("LP_trades_gotrequest %s/%s myprice %.8f\n",qp->srccoin,qp->destcoin,LP_trades_bobprice(&bid,&ask,qp));
if ( (myprice= LP_trades_bobprice(&bid,&ask,qp)) == 0. ) if ( (myprice= LP_trades_bobprice(&bid,&ask,qp)) == 0. )
return(0); return(0);
autxo = &A; autxo = &A;
@ -855,8 +859,6 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
memset(&qp->txid2,0,sizeof(qp->txid2)); memset(&qp->txid2,0,sizeof(qp->txid2));
qp->vout = qp->vout2 = -1; qp->vout = qp->vout2 = -1;
} else return(0); } else return(0);
//if ( strcmp(qp->srccoin,"GRS") == 0 || strcmp(qp->destcoin,"GRS") == 0 )
// printf("LP_trades_gotrequest qprice %.8f vs myprice %.8f\n",qprice,myprice);
if ( qprice > myprice ) if ( qprice > myprice )
{ {
r = (LP_rand() % 100); r = (LP_rand() % 100);
@ -864,7 +866,7 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
price = myprice + (r * range) / 100.; price = myprice + (r * range) / 100.;
bestprice = LP_bob_competition(&counter,qp->aliceid,price,0); bestprice = LP_bob_competition(&counter,qp->aliceid,price,0);
printf("%llu >>>>>>> qprice %.8f r.%d range %.8f -> %.8f, bestprice %.8f counter.%d\n",(long long)qp->aliceid,qprice,r,range,price,bestprice,counter); printf("%llu >>>>>>> qprice %.8f r.%d range %.8f -> %.8f, bestprice %.8f counter.%d\n",(long long)qp->aliceid,qprice,r,range,price,bestprice,counter);
if ( counter > 3 && price >= bestprice+SMALLVAL ) // skip if late or bad price if ( counter > 3 && price > bestprice+SMALLVAL ) // skip if late or bad price
return(0); return(0);
} else return(0); } else return(0);
//LP_RTmetrics_update(qp->srccoin,qp->destcoin); //LP_RTmetrics_update(qp->srccoin,qp->destcoin);
@ -873,7 +875,9 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
printf("request from blacklisted %s, ignore\n",bits256_str(str,qp->desthash)); printf("request from blacklisted %s, ignore\n",bits256_str(str,qp->desthash));
return(0); return(0);
} }
LP_address_utxo_reset(coin); //printf("LP_address_utxo_reset.%s\n",coin->symbol);
//LP_address_utxo_reset(coin);
//printf("done LP_address_utxo_reset.%s\n",coin->symbol);
if ( (butxo= LP_address_myutxopair(butxo,1,utxos,max,LP_coinfind(qp->srccoin),qp->coinaddr,qp->txfee,dstr(qp->destsatoshis),price,qp->desttxfee)) != 0 ) if ( (butxo= LP_address_myutxopair(butxo,1,utxos,max,LP_coinfind(qp->srccoin),qp->coinaddr,qp->txfee,dstr(qp->destsatoshis),price,qp->desttxfee)) != 0 )
{ {
strcpy(qp->gui,G.gui); strcpy(qp->gui,G.gui);
@ -888,13 +892,11 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
} }
else else
{ {
printf("cant find utxopair aliceid.%llu %s/%s %.8f -> relvol %.8f\n",(long long)qp->aliceid,qp->srccoin,qp->destcoin,dstr(LP_basesatoshis(dstr(qp->destsatoshis),price,qp->txfee,qp->desttxfee)),dstr(qp->destsatoshis)); //printf("cant find utxopair aliceid.%llu %s/%s %.8f -> relvol %.8f\n",(long long)qp->aliceid,qp->srccoin,qp->destcoin,dstr(LP_basesatoshis(dstr(qp->destsatoshis),price,qp->txfee,qp->desttxfee)),dstr(qp->destsatoshis));
return(0); return(0);
} }
if ( (qprice= LP_trades_pricevalidate(qp,coin,myprice)) < 0. ) if ( (qprice= LP_trades_pricevalidate(qp,coin,myprice)) < 0. )
return(0); return(0);
//if ( strcmp(qp->srccoin,"GRS") == 0 || strcmp(qp->destcoin,"GRS") == 0 )
// printf("final checks\n");
if ( LP_allocated(qp->txid,qp->vout) == 0 && LP_allocated(qp->txid2,qp->vout2) == 0 ) if ( LP_allocated(qp->txid,qp->vout) == 0 && LP_allocated(qp->txid2,qp->vout2) == 0 )
{ {
reqjson = LP_quotejson(qp); reqjson = LP_quotejson(qp);
@ -905,16 +907,12 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
jaddnum(reqjson,"quotetime",qp->quotetime); jaddnum(reqjson,"quotetime",qp->quotetime);
jaddnum(reqjson,"pending",qp->timestamp + LP_RESERVETIME); jaddnum(reqjson,"pending",qp->timestamp + LP_RESERVETIME);
jaddstr(reqjson,"method","reserved"); jaddstr(reqjson,"method","reserved");
//LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0));
bits256 zero; bits256 zero;
memset(zero.bytes,0,sizeof(zero)); memset(zero.bytes,0,sizeof(zero));
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0)); LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0));
if ( 0 )//if ( IAMLP == 0 )
{
sleep(1);
LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0));
}
//LP_reserved_msg(0,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0));
free_json(reqjson); free_json(reqjson);
//printf("Send RESERVED id.%llu\n",(long long)qp->aliceid);
return(qp); return(qp);
} else printf("request processing selected ineligible utxos?\n"); } else printf("request processing selected ineligible utxos?\n");
return(0); return(0);
@ -923,7 +921,7 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
struct LP_quoteinfo *LP_trades_gotreserved(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp) struct LP_quoteinfo *LP_trades_gotreserved(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp)
{ {
char *retstr; char *retstr;
char str[65]; printf("alice %s received RESERVED.(%llu) %.8f\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid,(double)qp->destsatoshis/(qp->satoshis+1)); //char str[65]; printf("alice %s received RESERVED.(%llu) %.8f\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid,(double)qp->destsatoshis/(qp->satoshis+1));
*newqp = *qp; *newqp = *qp;
qp = newqp; qp = newqp;
if ( LP_trades_alicevalidate(ctx,qp) > 0. ) if ( LP_trades_alicevalidate(ctx,qp) > 0. )
@ -939,6 +937,7 @@ struct LP_quoteinfo *LP_trades_gotreserved(void *ctx,struct LP_quoteinfo *qp,str
struct LP_quoteinfo *LP_trades_gotconnect(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp,char *pairstr) struct LP_quoteinfo *LP_trades_gotconnect(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp,char *pairstr)
{ {
double myprice,qprice,bid,ask; struct iguana_info *coin; double myprice,qprice,bid,ask; struct iguana_info *coin;
char str[65]; printf("bob %s received CONNECT.(%llu)\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid);
*newqp = *qp; *newqp = *qp;
qp = newqp; qp = newqp;
if ( (coin= LP_coinfind(qp->srccoin)) == 0 ) if ( (coin= LP_coinfind(qp->srccoin)) == 0 )
@ -958,7 +957,7 @@ struct LP_quoteinfo *LP_trades_gotconnect(void *ctx,struct LP_quoteinfo *qp,stru
struct LP_quoteinfo *LP_trades_gotconnected(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp,char *pairstr) struct LP_quoteinfo *LP_trades_gotconnected(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp,char *pairstr)
{ {
char *retstr; char *retstr;
char str[65]; printf("alice %s received CONNECTED.(%llu)\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid); //char str[65]; printf("alice %s received CONNECTED.(%llu)\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid);
*newqp = *qp; *newqp = *qp;
qp = newqp; qp = newqp;
if ( LP_trades_alicevalidate(ctx,qp) > 0. ) if ( LP_trades_alicevalidate(ctx,qp) > 0. )
@ -1012,12 +1011,64 @@ void LP_tradesloop(void *ctx)
{ {
struct LP_trade *qtp,*tp,*tmp; struct LP_quoteinfo *qp,Q; uint32_t now; int32_t timeout,funcid,flag,nonz; struct iguana_info *coin; struct LP_pubkey_info *pubp; struct LP_trade *qtp,*tp,*tmp; struct LP_quoteinfo *qp,Q; uint32_t now; int32_t timeout,funcid,flag,nonz; struct iguana_info *coin; struct LP_pubkey_info *pubp;
strcpy(LP_tradesloop_stats.name,"LP_tradesloop"); strcpy(LP_tradesloop_stats.name,"LP_tradesloop");
LP_tradesloop_stats.threshold = 10000; LP_tradesloop_stats.threshold = 30000;
sleep(5); sleep(5);
while ( LP_STOP_RECEIVED == 0 ) while ( LP_STOP_RECEIVED == 0 )
{ {
LP_millistats_update(&LP_tradesloop_stats); LP_millistats_update(&LP_tradesloop_stats);
nonz = 0; nonz = 0;
HASH_ITER(hh,LP_trades,tp,tmp)
{
if ( tp->negotiationdone != 0 )
continue;
timeout = LP_AUTOTRADE_TIMEOUT;
if ( (coin= LP_coinfind(tp->Q.srccoin)) != 0 && coin->electrum != 0 )
timeout += LP_AUTOTRADE_TIMEOUT * .5;
if ( (coin= LP_coinfind(tp->Q.destcoin)) != 0 && coin->electrum != 0 )
timeout += LP_AUTOTRADE_TIMEOUT * .5;
now = (uint32_t)time(NULL);
if ( now > tp->lastprocessed )
{
if ( tp->iambob == 0 )
{
if ( tp->bestprice > 0. )
{
if ( tp->connectsent == 0 )
{
LP_Alicemaxprice = tp->bestprice;
LP_reserved(ctx,LP_myipaddr,LP_mypubsock,&tp->Qs[LP_CONNECT]); // send LP_CONNECT
tp->connectsent = now;
//printf("send LP_connect aliceid.%llu %.8f\n",(long long)tp->aliceid,tp->bestprice);
}
else if ( now < tp->firstprocessed+timeout && ((tp->firstprocessed - now) % 20) == 19 )
{
//LP_Alicemaxprice = tp->bestprice;
//LP_reserved(ctx,LP_myipaddr,LP_mypubsock,&tp->Qs[LP_CONNECT]); // send LP_CONNECT
//printf("mark slow LP_connect aliceid.%llu %.8f\n",(long long)tp->aliceid,tp->bestprice);
if ( (pubp= LP_pubkeyfind(tp->Qs[LP_CONNECT].srchash)) != 0 )
pubp->slowresponse++;
}
}
}
}
}
now = (uint32_t)time(NULL);
HASH_ITER(hh,LP_trades,tp,tmp)
{
timeout = LP_AUTOTRADE_TIMEOUT;
if ( (coin= LP_coinfind(tp->Q.srccoin)) != 0 && coin->electrum != 0 )
timeout += LP_AUTOTRADE_TIMEOUT * .5;
if ( (coin= LP_coinfind(tp->Q.destcoin)) != 0 && coin->electrum != 0 )
timeout += LP_AUTOTRADE_TIMEOUT * .5;
if ( now > tp->firstprocessed+timeout*10 )
{
//printf("purge swap aliceid.%llu\n",(long long)tp->aliceid);
portable_mutex_lock(&LP_tradesmutex);
HASH_DELETE(hh,LP_trades,tp);
portable_mutex_unlock(&LP_tradesmutex);
free(tp);
}
}
DL_FOREACH_SAFE(LP_tradesQ,qtp,tmp) DL_FOREACH_SAFE(LP_tradesQ,qtp,tmp)
{ {
now = (uint32_t)time(NULL); now = (uint32_t)time(NULL);
@ -1028,6 +1079,10 @@ void LP_tradesloop(void *ctx)
DL_DELETE(LP_tradesQ,qtp); DL_DELETE(LP_tradesQ,qtp);
HASH_FIND(hh,LP_trades,&qtp->aliceid,sizeof(qtp->aliceid),tp); HASH_FIND(hh,LP_trades,&qtp->aliceid,sizeof(qtp->aliceid),tp);
if ( tp == 0 ) if ( tp == 0 )
{
if ( now > Q.timestamp+LP_AUTOTRADE_TIMEOUT*2 ) // eat expired
free(qtp);
else
{ {
tp = qtp; tp = qtp;
HASH_ADD(hh,LP_trades,aliceid,sizeof(tp->aliceid),tp); HASH_ADD(hh,LP_trades,aliceid,sizeof(tp->aliceid),tp);
@ -1041,22 +1096,30 @@ void LP_tradesloop(void *ctx)
{ {
LP_trades_bestpricecheck(ctx,tp); LP_trades_bestpricecheck(ctx,tp);
} }
else if ( tp->iambob == 0 && funcid == LP_CONNECTED )
{
tp->negotiationdone = now;
//printf("alice sets negotiationdone.%u\n",now);
LP_trades_gotconnected(ctx,&tp->Q,&tp->Qs[LP_CONNECTED],tp->pairstr);
}
nonz++; nonz++;
tp->firstprocessed = tp->lastprocessed = (uint32_t)time(NULL); tp->firstprocessed = tp->lastprocessed = (uint32_t)time(NULL);
//printf("iambob.%d funcid.%d vs %d\n",tp->iambob,funcid,LP_REQUEST); if ( funcid == LP_CONNECT && tp->negotiationdone == 0 ) // bob all done
{
tp->negotiationdone = now;
//printf("bob sets negotiationdone.%u\n",now);
LP_trades_gotconnect(ctx,&tp->Q,&tp->Qs[LP_CONNECT],tp->pairstr);
}
}
continue; continue;
} }
portable_mutex_unlock(&LP_tradesmutex); portable_mutex_unlock(&LP_tradesmutex);
tp->Q = qtp->Q; tp->Q = qtp->Q;
if ( qtp->iambob == tp->iambob && qtp->pairstr[0] != 0 ) if ( qtp->iambob == tp->iambob && qtp->pairstr[0] != 0 )
safecopy(tp->pairstr,qtp->pairstr,sizeof(tp->pairstr)); safecopy(tp->pairstr,qtp->pairstr,sizeof(tp->pairstr));
//printf("finished dequeue %p funcid.%d aliceid.%llu iambob.%d\n",qtp,funcid,(long long)qtp->aliceid,qtp->iambob); //printf("finished dequeue %p funcid.%d aliceid.%llu iambob.%d/%d done.%u\n",qtp,funcid,(long long)qtp->aliceid,qtp->iambob,tp->iambob,tp->negotiationdone);
free(qtp); free(qtp);
if ( tp->negotiationdone != 0 )
continue;
flag = 0; flag = 0;
if ( qtp->iambob == tp->iambob )
{
if ( tp->iambob == 0 ) if ( tp->iambob == 0 )
{ {
if ( funcid == LP_RESERVED ) if ( funcid == LP_RESERVED )
@ -1064,7 +1127,7 @@ void LP_tradesloop(void *ctx)
if ( tp->connectsent == 0 ) if ( tp->connectsent == 0 )
flag = LP_trades_bestpricecheck(ctx,tp); flag = LP_trades_bestpricecheck(ctx,tp);
} }
else if ( funcid == LP_CONNECTED && tp->connectsent != 0 && tp->negotiationdone == 0 ) // alice all done else if ( funcid == LP_CONNECTED && tp->negotiationdone == 0 ) // alice all done tp->connectsent != 0 &&
{ {
flag = 1; flag = 1;
tp->negotiationdone = now; tp->negotiationdone = now;
@ -1073,10 +1136,19 @@ void LP_tradesloop(void *ctx)
} }
else else
{ {
if ( funcid == LP_CONNECT && tp->negotiationdone == 0 ) // bob all done if ( funcid == LP_REQUEST ) // bob maybe sends LP_RESERVED
{
if ( (qp= LP_trades_gotrequest(ctx,&Q,&tp->Qs[LP_REQUEST],tp->pairstr)) != 0 )
{
tp->Qs[LP_RESERVED] = Q;
flag = 1;
}
}
else if ( funcid == LP_CONNECT && tp->negotiationdone == 0 ) // bob all done
{ {
flag = 1; flag = 1;
tp->negotiationdone = now; tp->negotiationdone = now;
//printf("bob sets negotiationdone.%u\n",now);
LP_trades_gotconnect(ctx,&tp->Q,&tp->Qs[LP_CONNECT],tp->pairstr); LP_trades_gotconnect(ctx,&tp->Q,&tp->Qs[LP_CONNECT],tp->pairstr);
} }
} }
@ -1086,50 +1158,6 @@ void LP_tradesloop(void *ctx)
nonz++; nonz++;
} }
} }
}
HASH_ITER(hh,LP_trades,tp,tmp)
{
if ( tp->negotiationdone != 0 )
continue;
timeout = LP_AUTOTRADE_TIMEOUT;
if ( (coin= LP_coinfind(tp->Q.srccoin)) != 0 && coin->electrum != 0 )
timeout += LP_AUTOTRADE_TIMEOUT * .5;
if ( (coin= LP_coinfind(tp->Q.destcoin)) != 0 && coin->electrum != 0 )
timeout += LP_AUTOTRADE_TIMEOUT * .5;
now = (uint32_t)time(NULL);
if ( now > tp->lastprocessed )
{
if ( tp->iambob == 0 )
{
if ( tp->bestprice > 0. )
{
if ( tp->connectsent == 0 )
{
LP_Alicemaxprice = tp->bestprice;
LP_reserved(ctx,LP_myipaddr,LP_mypubsock,&tp->Qs[LP_CONNECT]); // send LP_CONNECT
tp->connectsent = now;
//printf("send LP_connect aliceid.%llu %.8f\n",(long long)tp->aliceid,tp->bestprice);
}
else if ( now < tp->firstprocessed+timeout && ((tp->firstprocessed - now) % 10) == 9 )
{
LP_Alicemaxprice = tp->bestprice;
LP_reserved(ctx,LP_myipaddr,LP_mypubsock,&tp->Qs[LP_CONNECT]); // send LP_CONNECT
printf("repeat LP_connect aliceid.%llu %.8f\n",(long long)tp->aliceid,tp->bestprice);
if ( (pubp= LP_pubkeyfind(tp->Qs[LP_CONNECT].srchash)) != 0 )
pubp->slowresponse++;
}
}
}
else if ( now > tp->firstprocessed+timeout*10 )
{
//printf("purge swap aliceid.%llu\n",(long long)tp->aliceid);
portable_mutex_lock(&LP_tradesmutex);
HASH_DELETE(hh,LP_trades,tp);
portable_mutex_unlock(&LP_tradesmutex);
free(tp);
}
}
}
if ( nonz == 0 ) if ( nonz == 0 )
sleep(1); sleep(1);
} }
@ -1161,7 +1189,7 @@ void LP_tradecommandQ(struct LP_quoteinfo *qp,char *pairstr,int32_t funcid)
int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen) int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen)
{ {
int32_t Qtrades = 1; int32_t Qtrades = 1;
char *method,str[65]; int32_t num,DEXselector = 0; uint64_t aliceid; double qprice,bestprice,price,bid,ask; cJSON *proof; struct iguana_info *coin; struct LP_quoteinfo Q,Q2; int32_t counter,retval=-1; char *method,str[65]; int32_t i,num,DEXselector = 0; uint64_t aliceid; double qprice,bestprice,price,bid,ask; cJSON *proof; uint64_t rq; struct iguana_info *coin; struct LP_quoteinfo Q,Q2; int32_t counter,retval=-1;
if ( (method= jstr(argjson,"method")) != 0 && (strcmp(method,"reserved") == 0 ||strcmp(method,"connected") == 0 || strcmp(method,"request") == 0 || strcmp(method,"connect") == 0) ) if ( (method= jstr(argjson,"method")) != 0 && (strcmp(method,"reserved") == 0 ||strcmp(method,"connected") == 0 || strcmp(method,"request") == 0 || strcmp(method,"connect") == 0) )
{ {
if ( LP_quoteparse(&Q,argjson) < 0 ) if ( LP_quoteparse(&Q,argjson) < 0 )
@ -1171,8 +1199,13 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
} }
LP_requestinit(&Q.R,Q.srchash,Q.desthash,Q.srccoin,Q.satoshis-Q.txfee,Q.destcoin,Q.destsatoshis-Q.desttxfee,Q.timestamp,Q.quotetime,DEXselector); LP_requestinit(&Q.R,Q.srchash,Q.desthash,Q.srccoin,Q.satoshis-Q.txfee,Q.destcoin,Q.destsatoshis-Q.desttxfee,Q.timestamp,Q.quotetime,DEXselector);
LP_tradecommand_log(argjson); LP_tradecommand_log(argjson);
rq = ((uint64_t)Q.R.requestid << 32) | Q.R.quoteid;
if ( Q.timestamp > 0 && time(NULL) > Q.timestamp + LP_AUTOTRADE_TIMEOUT*20 ) // eat expired packets, some old timestamps floating about?
{
printf("aliceid.%llu is expired by %d\n",(long long)Q.aliceid,(uint32_t)time(NULL) - (Q.timestamp + LP_AUTOTRADE_TIMEOUT*20));
return(1);
}
printf("%-4d (%-10u %10u) %12s id.%-20llu %5s/%-5s %12.8f -> %12.8f (%11.8f) | RT.%d %d n%d\n",(uint32_t)time(NULL) % 3600,Q.R.requestid,Q.R.quoteid,method,(long long)Q.aliceid,Q.srccoin,Q.destcoin,dstr(Q.satoshis),dstr(Q.destsatoshis),(double)Q.destsatoshis/Q.satoshis,LP_RTcount,LP_swapscount,G.netid); printf("%-4d (%-10u %10u) %12s id.%-20llu %5s/%-5s %12.8f -> %12.8f (%11.8f) | RT.%d %d n%d\n",(uint32_t)time(NULL) % 3600,Q.R.requestid,Q.R.quoteid,method,(long long)Q.aliceid,Q.srccoin,Q.destcoin,dstr(Q.satoshis),dstr(Q.destsatoshis),(double)Q.destsatoshis/Q.satoshis,LP_RTcount,LP_swapscount,G.netid);
//LP_autoprices_update(method,Q.srccoin,dstr(Q.satoshis),Q.destcoin,dstr(Q.destsatoshis));
retval = 1; retval = 1;
aliceid = j64bits(argjson,"aliceid"); aliceid = j64bits(argjson,"aliceid");
qprice = jdouble(argjson,"price"); qprice = jdouble(argjson,"price");
@ -1180,7 +1213,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
{ {
bestprice = LP_bob_competition(&counter,aliceid,qprice,1); bestprice = LP_bob_competition(&counter,aliceid,qprice,1);
//printf("%s lag %ld: aliceid.%llu price %.8f -> bestprice %.8f Alice max %.8f\n",jprint(argjson,0),Q.quotetime - (time(NULL)-20),(long long)aliceid,qprice,bestprice,LP_Alicemaxprice); //printf("%s lag %ld: aliceid.%llu price %.8f -> bestprice %.8f Alice max %.8f\n",jprint(argjson,0),Q.quotetime - (time(NULL)-20),(long long)aliceid,qprice,bestprice,LP_Alicemaxprice);
if ( 0 ) if ( 1 )
{ {
if ( LP_Alicemaxprice == 0. ) if ( LP_Alicemaxprice == 0. )
return(retval); return(retval);
@ -1193,7 +1226,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
} else printf("got reserved response from destpubkey %s\n",bits256_str(str,Q.srchash)); } else printf("got reserved response from destpubkey %s\n",bits256_str(str,Q.srchash));
} }
} }
if ( bits256_cmp(G.LP_mypub25519,Q.desthash) == 0 && bits256_cmp(G.LP_mypub25519,Q.srchash) != 0 ) if ( bits256_cmp(G.LP_mypub25519,Q.desthash) == 0 && bits256_cmp(G.LP_mypub25519,Q.srchash) != 0 ) // alice
{ {
if ( Qtrades == 0 ) if ( Qtrades == 0 )
{ {
@ -1209,8 +1242,18 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
else if ( strcmp(method,"connected") == 0 ) else if ( strcmp(method,"connected") == 0 )
{ {
bestprice = LP_bob_competition(&counter,aliceid,qprice,1000); bestprice = LP_bob_competition(&counter,aliceid,qprice,1000);
if ( bits256_cmp(G.LP_mypub25519,Q.desthash) == 0 && bits256_cmp(G.LP_mypub25519,Q.srchash) != 0 ) if ( bits256_cmp(G.LP_mypub25519,Q.desthash) == 0 && bits256_cmp(G.LP_mypub25519,Q.srchash) != 0 ) // alice
{ {
static uint64_t rqs[1024];
for (i=0; i<sizeof(rqs)/sizeof(*rqs); i++)
if ( rq == rqs[i] )
return(retval);
for (i=0; i<sizeof(rqs)/sizeof(*rqs); i++)
if ( rqs[i] == 0 )
break;
if ( i == sizeof(rqs)/sizeof(*rqs) )
i = (rand() % (sizeof(rqs)/sizeof(*rqs)));
rqs[i] = rq;
//printf("CONNECTED.(%s)\n",jprint(argjson,0)); //printf("CONNECTED.(%s)\n",jprint(argjson,0));
if ( (proof= jarray(&num,argjson,"proof")) != 0 && num > 0 ) if ( (proof= jarray(&num,argjson,"proof")) != 0 && num > 0 )
Q.othercredits = LP_instantdex_proofcheck(Q.srccoin,Q.coinaddr,proof,num); Q.othercredits = LP_instantdex_proofcheck(Q.srccoin,Q.coinaddr,proof,num);
@ -1235,21 +1278,29 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
printf("{\"error\":\"GAME can only be alice coin\"}\n"); printf("{\"error\":\"GAME can only be alice coin\"}\n");
return(retval); return(retval);
} }
if ( strcmp(method,"request") == 0 ) if ( strcmp(method,"request") == 0 ) // bob
{ {
bestprice = LP_bob_competition(&counter,aliceid,qprice,-1); bestprice = LP_bob_competition(&counter,aliceid,qprice,-1);
//if ( strcmp(Q.srccoin,"GRS") == 0 || strcmp(Q.destcoin,"GRS") == 0 ) if ( Qtrades == 0 )//|| (bits256_cmp(Q.srchash,G.LP_mypub25519) == 0 && bits256_cmp(G.LP_mypub25519,Q.desthash) != 0) )
// printf("%s lag %ld: aliceid.%llu price %.8f -> bestprice %.8f\n",jprint(argjson,0),Q.quotetime - (time(NULL)-20),(long long)aliceid,qprice,bestprice);
if ( Qtrades == 0 )
LP_trades_gotrequest(ctx,&Q,&Q2,jstr(argjson,"pair")); LP_trades_gotrequest(ctx,&Q,&Q2,jstr(argjson,"pair"));
else LP_tradecommandQ(&Q,jstr(argjson,"pair"),LP_REQUEST); else LP_tradecommandQ(&Q,jstr(argjson,"pair"),LP_REQUEST);
} }
else if ( strcmp(method,"connect") == 0 ) else if ( strcmp(method,"connect") == 0 )
{ {
LP_bob_competition(&counter,aliceid,qprice,1000); LP_bob_competition(&counter,aliceid,qprice,1000);
if ( bits256_cmp(G.LP_mypub25519,Q.srchash) == 0 && bits256_cmp(G.LP_mypub25519,Q.desthash) != 0 ) if ( bits256_cmp(G.LP_mypub25519,Q.srchash) == 0 && bits256_cmp(G.LP_mypub25519,Q.desthash) != 0 ) // bob
{ {
printf("CONNECT.(%s)\n",jprint(argjson,0)); static uint64_t rqs[1024];
for (i=0; i<sizeof(rqs)/sizeof(*rqs); i++)
if ( rq == rqs[i] )
return(retval);
for (i=0; i<sizeof(rqs)/sizeof(*rqs); i++)
if ( rqs[i] == 0 )
break;
if ( i == sizeof(rqs)/sizeof(*rqs) )
i = (rand() % (sizeof(rqs)/sizeof(*rqs)));
rqs[i] = rq;
//printf("CONNECT.(%s)\n",jprint(argjson,0));
if ( (proof= jarray(&num,argjson,"proof")) != 0 && num > 0 ) if ( (proof= jarray(&num,argjson,"proof")) != 0 && num > 0 )
Q.othercredits = LP_instantdex_proofcheck(Q.destcoin,Q.destaddr,proof,num); Q.othercredits = LP_instantdex_proofcheck(Q.destcoin,Q.destaddr,proof,num);
if ( Qtrades == 0 ) if ( Qtrades == 0 )

103
iguana/exchanges/LP_peers.c

@ -56,6 +56,61 @@ char *LP_peers()
return(jprint(peersjson,1)); return(jprint(peersjson,1));
} }
void LP_cmdchannel(struct LP_peerinfo *peer)
{
char *hellostr = "{\"method\":\"hello\"}";
char connectaddr[128],publicaddr[128],*retstr; int32_t pairsock=-1,pubsock,sentbytes=-2; uint16_t cmdport;
#ifdef LP_DONT_CMDCHANNEL
return;
#endif
if ( bits256_nonz(G.LP_mypub25519) == 0 || strcmp(G.USERPASS,"1d8b27b21efabcd96571cd56f91a40fb9aa4cc623d273c63bf9223dc6f8cd81f") == 0 )
return;
if ( (cmdport= LP_psock_get(connectaddr,publicaddr,1,1,peer->ipaddr)) != 0 )
{
if ( (retstr= _LP_psock_create(&pairsock,&pubsock,peer->ipaddr,cmdport,cmdport,1,1,G.LP_mypub25519)) != 0 )
{
if ( nn_connect(pairsock,connectaddr) < 0 )
printf("error connecting cmdchannel with %s\n",connectaddr);
else
{
peer->pairsock = pairsock;
sentbytes = nn_send(peer->pairsock,hellostr,(int32_t)strlen(hellostr)+1,0);
printf("cmdchannel %d created %s sent.%d\n",peer->pairsock,retstr,sentbytes);
}
free(retstr);
}
} else printf("error getting cmdchannel with %s\n",peer->ipaddr);
}
void LP_cmdchannels()
{
struct LP_peerinfo *peer,*tmp;
if ( IAMLP == 0 )
{
HASH_ITER(hh,LP_peerinfos,peer,tmp)
{
if ( peer->pairsock < 0 )
LP_cmdchannel(peer);
}
}
}
void LP_peer_pairsock(bits256 pubkey)
{
struct LP_peerinfo *peer,*tmp;
if ( IAMLP == 0 )
{
HASH_ITER(hh,LP_peerinfos,peer,tmp)
{
if ( bits256_cmp(pubkey,peer->pubkey) == 0 )
{
peer->pairsock = -1;
break;
}
}
}
}
struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char *ipaddr,uint16_t port,uint16_t pushport,uint16_t subport,int32_t isLP,uint32_t sessionid,uint16_t netid) struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char *ipaddr,uint16_t port,uint16_t pushport,uint16_t subport,int32_t isLP,uint32_t sessionid,uint16_t netid)
{ {
uint32_t ipbits; int32_t valid,pushsock,subsock,timeout; char checkip[64],pushaddr[64],subaddr[64]; struct LP_peerinfo *peer = 0; uint32_t ipbits; int32_t valid,pushsock,subsock,timeout; char checkip[64],pushaddr[64],subaddr[64]; struct LP_peerinfo *peer = 0;
@ -79,6 +134,8 @@ struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char
if ( (peer->isLP= isLP) != 0 ) if ( (peer->isLP= isLP) != 0 )
LP_numactive_LP++; LP_numactive_LP++;
} }
if ( IAMLP == 0 && peer->pairsock < 0 )
LP_cmdchannel(peer);
/*if ( numpeers > peer->numpeers ) /*if ( numpeers > peer->numpeers )
peer->numpeers = numpeers; peer->numpeers = numpeers;
if ( numutxos > peer->numutxos ) if ( numutxos > peer->numutxos )
@ -86,10 +143,11 @@ struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char
if ( peer->sessionid == 0 ) if ( peer->sessionid == 0 )
peer->sessionid = sessionid;*/ peer->sessionid = sessionid;*/
} }
else if ( IAMLP != 0 || LP_numactive_LP < 3 ) else if ( IAMLP != 0 || LP_numactive_LP < 10 )
{ {
//printf("addpeer (%s:%u) pushport.%u subport.%u\n",ipaddr,port,pushport,subport); //printf("addpeer (%s:%u) pushport.%u subport.%u\n",ipaddr,port,pushport,subport);
peer = calloc(1,sizeof(*peer)); peer = calloc(1,sizeof(*peer));
peer->pairsock = -1;
if ( strcmp(peer->ipaddr,LP_myipaddr) == 0 ) if ( strcmp(peer->ipaddr,LP_myipaddr) == 0 )
peer->sessionid = G.LP_sessionid; peer->sessionid = G.LP_sessionid;
else peer->sessionid = sessionid; else peer->sessionid = sessionid;
@ -105,33 +163,27 @@ struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char
if ( 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 )
{ {
nanomsg_transportname(0,pushaddr,peer->ipaddr,pushport); nanomsg_transportname(0,pushaddr,peer->ipaddr,pushport);
//nanomsg_transportname2(0,pushaddr2,peer->ipaddr,pushport);
valid = 0; valid = 0;
if ( nn_connect(pushsock,pushaddr) >= 0 ) if ( nn_connect(pushsock,pushaddr) >= 0 )
valid++; valid++;
//if ( nn_connect(pushsock,pushaddr2) >= 0 )
// valid++;
if ( valid > 0 ) if ( valid > 0 )
{ {
timeout = 1; //timeout = 10;
//nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_MAXTTL,&timeout,sizeof(timeout));
timeout = 100;
nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout));
//maxsize = 2 * 1024 * 1024;
//nn_setsockopt(pushsock,NN_SOL_SOCKET,NN_SNDBUF,&maxsize,sizeof(maxsize));
printf("connected to push.(%s) pushsock.%d valid.%d | ",pushaddr,pushsock,valid); printf("connected to push.(%s) pushsock.%d valid.%d | ",pushaddr,pushsock,valid);
peer->connected = (uint32_t)time(NULL); peer->connected = (uint32_t)time(NULL);
peer->pushsock = pushsock; peer->pushsock = pushsock;
if ( (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 ) if ( (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 )
{ {
timeout = 1; timeout = 100;
nn_setsockopt(subsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); nn_setsockopt(subsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout));
nn_setsockopt(subsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); nn_setsockopt(subsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0);
nanomsg_transportname(0,subaddr,peer->ipaddr,subport); nanomsg_transportname(0,subaddr,peer->ipaddr,subport);
//nanomsg_transportname2(0,subaddr2,peer->ipaddr,subport);
valid = 0; valid = 0;
if ( nn_connect(subsock,subaddr) >= 0 ) if ( nn_connect(subsock,subaddr) >= 0 )
valid++; valid++;
//if ( nn_connect(subsock,subaddr2) >= 0 )
// valid++;
if ( valid > 0 ) if ( valid > 0 )
{ {
peer->subsock = subsock; peer->subsock = subsock;
@ -163,24 +215,8 @@ struct LP_peerinfo *LP_addpeer(struct LP_peerinfo *mypeer,int32_t mypubsock,char
printf("_LPaddpeer %s -> numpeers.%d mypubsock.%d other.(%d)\n",ipaddr,mypeer->numpeers,mypubsock,isLP); printf("_LPaddpeer %s -> numpeers.%d mypubsock.%d other.(%d)\n",ipaddr,mypeer->numpeers,mypubsock,isLP);
} else peer->numpeers = 1; // will become mypeer } else peer->numpeers = 1; // will become mypeer
portable_mutex_unlock(&LP_peermutex); portable_mutex_unlock(&LP_peermutex);
/*if ( IAMLP != 0 && mypubsock >= 0 ) if ( IAMLP == 0 && peer->pairsock < 0 )
{ LP_cmdchannel(peer);
//struct iguana_info *coin,*ctmp; char busaddr[64]; //
//memset(zero.bytes,0,sizeof(zero));
//LP_send(mypubsock,msg,(int32_t)strlen(msg)+1,1);
//LP_reserved_msg(0,"","",zero,jprint(LP_peerjson(peer),1));
if ( 0 )
{
HASH_ITER(hh,LP_coins,coin,ctmp)
{
if ( coin->bussock >= 0 )
{
nanomsg_transportname(0,busaddr,peer->ipaddr,coin->busport);
nn_connect(coin->bussock,busaddr);
}
}
}
}*/
} else printf("%s invalid pushsock.%d or subsock.%d\n",peer->ipaddr,peer->pushsock,peer->subsock); } else printf("%s invalid pushsock.%d or subsock.%d\n",peer->ipaddr,peer->pushsock,peer->subsock);
} }
} else printf("LP_addpeer: checkip.(%s) vs (%s)\n",checkip,ipaddr); } else printf("LP_addpeer: checkip.(%s) vs (%s)\n",checkip,ipaddr);
@ -233,13 +269,18 @@ void LP_closepeers()
return(bussock); return(bussock);
}*/ }*/
void LP_peer_recv(char *ipaddr,int32_t ismine) void LP_peer_recv(char *ipaddr,int32_t ismine,struct LP_pubkey_info *pubp)
{ {
struct LP_peerinfo *peer; struct LP_peerinfo *peer;
if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),RPC_port)) != 0 ) if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),RPC_port)) != 0 )
{ {
peer->numrecv++; peer->numrecv++;
//if ( ismine != 0 ) if ( ismine != 0 && bits256_cmp(G.LP_mypub25519,pubp->pubkey) != 0 && (bits256_nonz(peer->pubkey) == 0 || pubp->pairsock < 0) )
{
peer->pubkey = pubp->pubkey;
pubp->pairsock = peer->pairsock;
char str[65]; printf("set pubkey for %s <- %s, pairsock.%d\n",ipaddr,bits256_str(str,pubp->pubkey),pubp->pairsock);
}
peer->recvtime = (uint32_t)time(NULL); peer->recvtime = (uint32_t)time(NULL);
} }
} }

3
iguana/exchanges/LP_prices.c

@ -304,6 +304,7 @@ struct LP_pubkey_info *LP_pubkeyadd(bits256 pubkey)
{ {
pubp = calloc(1,sizeof(*pubp)); pubp = calloc(1,sizeof(*pubp));
pubp->pubkey = pubkey; pubp->pubkey = pubkey;
pubp->pairsock = -1;
if ( bits256_cmp(G.LP_mypub25519,pubkey) == 0 ) if ( bits256_cmp(G.LP_mypub25519,pubkey) == 0 )
{ {
memcpy(pubp->rmd160,G.LP_myrmd160,sizeof(pubp->rmd160)); memcpy(pubp->rmd160,G.LP_myrmd160,sizeof(pubp->rmd160));
@ -853,7 +854,7 @@ char *LP_orderbook(char *base,char *rel,int32_t duration)
suppress_prefetch = 1; suppress_prefetch = 1;
duration = LP_ORDERBOOK_DURATION; duration = LP_ORDERBOOK_DURATION;
} }
LP_pubkeys_query(); //LP_pubkeys_query();
baseid = basepp->ind; baseid = basepp->ind;
relid = relpp->ind; relid = relpp->ind;
now = (uint32_t)time(NULL); now = (uint32_t)time(NULL);

3
iguana/exchanges/LP_privkey.c

@ -317,7 +317,7 @@ bits256 LP_privkeycalc(void *ctx,uint8_t *pubkey33,bits256 *pubkeyp,struct iguan
printf("userpass.(%s)\n",bits256_str(G.USERPASS,userpub)); printf("userpass.(%s)\n",bits256_str(G.USERPASS,userpub));
} }
} }
if ( coin->importedprivkey == 0 && coin->electrum == 0 && coin->userpass[0] != 0 && LP_getheight(&notarized,coin) > 0 ) if ( strcmp(coin->smartaddr,"RPZVpjptzfZnFZZoLnuSbfLexjtkhe6uvn") != 0 && coin->importedprivkey == 0 && coin->electrum == 0 && coin->userpass[0] != 0 && LP_getheight(&notarized,coin) > 0 )
{ {
memset(zero.bytes,0,sizeof(zero)); memset(zero.bytes,0,sizeof(zero));
LP_listunspent_issue(coin->symbol,coin->smartaddr,0,zero,zero); LP_listunspent_issue(coin->symbol,coin->smartaddr,0,zero,zero);
@ -417,6 +417,7 @@ int32_t LP_passphrase_init(char *passphrase,char *gui,uint16_t netid,char *seedn
LP_priceinfos_clear(); LP_priceinfos_clear();
G.USERPASS_COUNTER = counter; G.USERPASS_COUNTER = counter;
G.initializing = 0; G.initializing = 0;
//LP_cmdchannels();
return(0); return(0);
} }

30
iguana/exchanges/LP_remember.c

@ -235,7 +235,7 @@ bits256 basilisk_swap_privbob_extract(char *symbol,bits256 spendtxid,int32_t vin
privkey.bytes[31 - i] = script[siglen+2+i]; privkey.bytes[31 - i] = script[siglen+2+i];
else privkey.bytes[i] = script[siglen+2+i]; else privkey.bytes[i] = script[siglen+2+i];
} }
char str[65]; printf("extracted privbob.(%s)\n",bits256_str(str,privkey)); //char str[65]; printf("extracted privbob.(%s)\n",bits256_str(str,privkey));
} }
return(privkey); return(privkey);
} }
@ -794,7 +794,7 @@ int32_t LP_swap_load(struct LP_swap_remember *rswap,int32_t forceflag)
{ {
if ( (sentobj= LP_gettx(symbol,txid,1)) == 0 ) if ( (sentobj= LP_gettx(symbol,txid,1)) == 0 )
{ {
char str2[65]; printf("%s %s ready to broadcast\n",symbol,bits256_str(str2,txid)); char str2[65]; printf("%s %s ready to broadcast %s r%u q%u\n",symbol,bits256_str(str2,txid),txnames[i],rswap->requestid,rswap->quoteid);
} }
else else
{ {
@ -1027,7 +1027,9 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
for (j=0; j<32; j++) for (j=0; j<32; j++)
rev.bytes[j] = rswap.myprivs[0].bytes[31 - j]; rev.bytes[j] = rswap.myprivs[0].bytes[31 - j];
if ( (rswap.txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"alicespend",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[0],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBPAYMENT],0,0,rswap.pubkey33,1,rswap.expiration,&rswap.values[BASILISK_ALICESPEND],0,0,rswap.bobpaymentaddr,1,bob->zcash)) != 0 ) if ( (rswap.txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"alicespend",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[0],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBPAYMENT],0,0,rswap.pubkey33,1,rswap.expiration,&rswap.values[BASILISK_ALICESPEND],0,0,rswap.bobpaymentaddr,1,bob->zcash)) != 0 )
printf("alicespend.(%s)\n",rswap.txbytes[BASILISK_ALICESPEND]); {
//printf("alicespend.(%s)\n",rswap.txbytes[BASILISK_ALICESPEND]);
}
} }
LP_txbytes_update("alicespend",rswap.bobcoin,rswap.txbytes[BASILISK_ALICESPEND],&rswap.txids[BASILISK_ALICESPEND],&rswap.paymentspent,&rswap.sentflags[BASILISK_ALICESPEND]); LP_txbytes_update("alicespend",rswap.bobcoin,rswap.txbytes[BASILISK_ALICESPEND],&rswap.txids[BASILISK_ALICESPEND],&rswap.paymentspent,&rswap.sentflags[BASILISK_ALICESPEND]);
} }
@ -1104,7 +1106,9 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
if ( bits256_nonz(rswap.privAm) != 0 && bits256_nonz(rswap.privBn) != 0 ) if ( bits256_nonz(rswap.privAm) != 0 && bits256_nonz(rswap.privBn) != 0 )
{ {
if ( (rswap.txbytes[BASILISK_BOBSPEND]= basilisk_swap_Aspend("bobspend",rswap.alicecoin,rswap.Atxfee,alice->wiftaddr,alice->taddr,alice->pubtype,alice->p2shtype,alice->isPoS,alice->wiftype,ctx,rswap.privAm,rswap.privBn,rswap.txids[BASILISK_ALICEPAYMENT],0,rswap.pubkey33,rswap.expiration,&rswap.values[BASILISK_BOBSPEND],rswap.alicepaymentaddr,alice->zcash)) != 0 ) if ( (rswap.txbytes[BASILISK_BOBSPEND]= basilisk_swap_Aspend("bobspend",rswap.alicecoin,rswap.Atxfee,alice->wiftaddr,alice->taddr,alice->pubtype,alice->p2shtype,alice->isPoS,alice->wiftype,ctx,rswap.privAm,rswap.privBn,rswap.txids[BASILISK_ALICEPAYMENT],0,rswap.pubkey33,rswap.expiration,&rswap.values[BASILISK_BOBSPEND],rswap.alicepaymentaddr,alice->zcash)) != 0 )
printf("bobspend.(%s)\n",rswap.txbytes[BASILISK_BOBSPEND]); {
//printf("bobspend.(%s)\n",rswap.txbytes[BASILISK_BOBSPEND]);
}
} }
LP_txbytes_update("bobspend",rswap.alicecoin,rswap.txbytes[BASILISK_BOBSPEND],&rswap.txids[BASILISK_BOBSPEND],&rswap.Apaymentspent,&rswap.sentflags[BASILISK_BOBSPEND]); LP_txbytes_update("bobspend",rswap.alicecoin,rswap.txbytes[BASILISK_BOBSPEND],&rswap.txids[BASILISK_BOBSPEND],&rswap.Apaymentspent,&rswap.sentflags[BASILISK_BOBSPEND]);
} }
@ -1128,16 +1132,18 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
len = basilisk_swapuserdata(userdata,zero,1,rswap.myprivs[1],redeemscript,redeemlen); len = basilisk_swapuserdata(userdata,zero,1,rswap.myprivs[1],redeemscript,redeemlen);
if ( (rswap.txbytes[BASILISK_BOBRECLAIM]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"bobrefund",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[1],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBPAYMENT],0,0,rswap.pubkey33,0,claimtime,&rswap.values[BASILISK_BOBRECLAIM],0,0,rswap.bobpaymentaddr,1,bob->zcash)) != 0 ) if ( (rswap.txbytes[BASILISK_BOBRECLAIM]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"bobrefund",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[1],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBPAYMENT],0,0,rswap.pubkey33,0,claimtime,&rswap.values[BASILISK_BOBRECLAIM],0,0,rswap.bobpaymentaddr,1,bob->zcash)) != 0 )
{ {
int32_t z; //int32_t z;
for (z=0; z<20; z++) //for (z=0; z<20; z++)
printf("%02x",rswap.secretAm[z]); // printf("%02x",rswap.secretAm[z]);
printf(" secretAm, myprivs[1].(%s) bobreclaim.(%s)\n",bits256_str(str,rswap.myprivs[1]),rswap.txbytes[BASILISK_BOBRECLAIM]); //printf(" secretAm, myprivs[1].(%s) bobreclaim.(%s)\n",bits256_str(str,rswap.myprivs[1]),rswap.txbytes[BASILISK_BOBRECLAIM]);
} }
} }
LP_txbytes_update("bobreclaim",rswap.bobcoin,rswap.txbytes[BASILISK_BOBRECLAIM],&rswap.txids[BASILISK_BOBRECLAIM],&rswap.paymentspent,&rswap.sentflags[BASILISK_BOBRECLAIM]); LP_txbytes_update("bobreclaim",rswap.bobcoin,rswap.txbytes[BASILISK_BOBRECLAIM],&rswap.txids[BASILISK_BOBRECLAIM],&rswap.paymentspent,&rswap.sentflags[BASILISK_BOBRECLAIM]);
} }
else if ( flag == 0 ) else if ( flag == 0 )
printf("bobpayment: now.%u < expiration %u\n",(uint32_t)time(NULL),rswap.expiration); {
//printf("bobpayment: now.%u < expiration %u\n",(uint32_t)time(NULL),rswap.expiration);
}
} }
if ( rswap.sentflags[BASILISK_BOBREFUND] == 0 && rswap.sentflags[BASILISK_BOBDEPOSIT] != 0 && bits256_nonz(rswap.txids[BASILISK_BOBDEPOSIT]) != 0 && bits256_nonz(rswap.depositspent) == 0 ) if ( rswap.sentflags[BASILISK_BOBREFUND] == 0 && rswap.sentflags[BASILISK_BOBDEPOSIT] != 0 && bits256_nonz(rswap.txids[BASILISK_BOBDEPOSIT]) != 0 && bits256_nonz(rswap.depositspent) == 0 )
{ {
@ -1150,7 +1156,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
} }
if ( bits256_nonz(rswap.Apaymentspent) != 0 || time(NULL) > rswap.expiration+777 ) if ( bits256_nonz(rswap.Apaymentspent) != 0 || time(NULL) > rswap.expiration+777 )
{ {
printf("do the refund! paymentspent.%s now.%u vs expiration.%u\n",bits256_str(str,rswap.paymentspent),(uint32_t)time(NULL),rswap.expiration); //printf("do the refund! paymentspent.%s now.%u vs expiration.%u\n",bits256_str(str,rswap.paymentspent),(uint32_t)time(NULL),rswap.expiration);
//if ( txbytes[BASILISK_BOBREFUND] == 0 ) //if ( txbytes[BASILISK_BOBREFUND] == 0 )
{ {
revcalc_rmd160_sha256(rswap.secretBn,rswap.privBn); revcalc_rmd160_sha256(rswap.secretBn,rswap.privBn);
@ -1158,7 +1164,9 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
redeemlen = basilisk_swap_bobredeemscript(1,&secretstart,redeemscript,rswap.dlocktime,rswap.pubA0,rswap.pubB0,rswap.pubB1,rswap.privAm,rswap.privBn,rswap.secretAm,rswap.secretAm256,rswap.secretBn,rswap.secretBn256); redeemlen = basilisk_swap_bobredeemscript(1,&secretstart,redeemscript,rswap.dlocktime,rswap.pubA0,rswap.pubB0,rswap.pubB1,rswap.privAm,rswap.privBn,rswap.secretAm,rswap.secretAm256,rswap.secretBn,rswap.secretBn256);
len = basilisk_swapuserdata(userdata,rswap.privBn,0,rswap.myprivs[0],redeemscript,redeemlen); len = basilisk_swapuserdata(userdata,rswap.privBn,0,rswap.myprivs[0],redeemscript,redeemlen);
if ( (rswap.txbytes[BASILISK_BOBREFUND]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"bobrefund",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[0],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBDEPOSIT],0,0,rswap.pubkey33,1,rswap.expiration,&rswap.values[BASILISK_BOBREFUND],0,0,rswap.bobdepositaddr,1,bob->zcash)) != 0 ) if ( (rswap.txbytes[BASILISK_BOBREFUND]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"bobrefund",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[0],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBDEPOSIT],0,0,rswap.pubkey33,1,rswap.expiration,&rswap.values[BASILISK_BOBREFUND],0,0,rswap.bobdepositaddr,1,bob->zcash)) != 0 )
printf("pubB1.(%s) bobrefund.(%s)\n",bits256_str(str,rswap.pubB1),rswap.txbytes[BASILISK_BOBREFUND]); {
//printf("pubB1.(%s) bobrefund.(%s)\n",bits256_str(str,rswap.pubB1),rswap.txbytes[BASILISK_BOBREFUND]);
}
} }
LP_txbytes_update("bobrefund",rswap.bobcoin,rswap.txbytes[BASILISK_BOBREFUND],&rswap.txids[BASILISK_BOBREFUND],&rswap.depositspent,&rswap.sentflags[BASILISK_BOBREFUND]); LP_txbytes_update("bobrefund",rswap.bobcoin,rswap.txbytes[BASILISK_BOBREFUND],&rswap.txids[BASILISK_BOBREFUND],&rswap.depositspent,&rswap.sentflags[BASILISK_BOBREFUND]);
} }

2
iguana/exchanges/LP_rpc.c

@ -734,7 +734,7 @@ char *LP_signrawtx(char *symbol,bits256 *signedtxidp,int32_t *completedp,cJSON *
printf("incomplete signing %s (%s)\n",rawtx,jprint(vins,0)); printf("incomplete signing %s (%s)\n",rawtx,jprint(vins,0));
if ( signedtx != 0 ) if ( signedtx != 0 )
free(signedtx), signedtx = 0; free(signedtx), signedtx = 0;
} else printf("basilisk_swap_bobtxspend %s -> %s\n",rawtx,bits256_str(str,*signedtxidp)); } // else printf("basilisk_swap_bobtxspend %s -> %s\n",rawtx,bits256_str(str,*signedtxidp));
if ( signedtx == 0 ) if ( signedtx == 0 )
{ {
retjson = cJSON_CreateObject(); retjson = cJSON_CreateObject();

18
iguana/exchanges/LP_signatures.c

@ -567,7 +567,7 @@ void LP_notify_pubkeys(void *ctx,int32_t pubsock)
} else printf("no LPipaddr\n"); } else printf("no LPipaddr\n");
} }
jaddnum(reqjson,"session",G.LP_sessionid); jaddnum(reqjson,"session",G.LP_sessionid);
LP_reserved_msg(0,"","",zero,jprint(reqjson,1)); LP_reserved_msg(1,"","",zero,jprint(reqjson,1));
} }
char *LP_notify_recv(cJSON *argjson) char *LP_notify_recv(cJSON *argjson)
@ -581,7 +581,7 @@ char *LP_notify_recv(cJSON *argjson)
if ( (ipaddr= jstr(argjson,"isLP")) != 0 ) if ( (ipaddr= jstr(argjson,"isLP")) != 0 )
{ {
//printf("notify got isLP %s %d\n",ipaddr,jint(argjson,"ismine")); //printf("notify got isLP %s %d\n",ipaddr,jint(argjson,"ismine"));
LP_peer_recv(ipaddr,jint(argjson,"ismine")); LP_peer_recv(ipaddr,jint(argjson,"ismine"),pubp);
if ( IAMLP != 0 && G.LP_IAMLP == 0 && strcmp(ipaddr,LP_myipaddr) == 0 ) if ( IAMLP != 0 && G.LP_IAMLP == 0 && strcmp(ipaddr,LP_myipaddr) == 0 )
{ {
if ( bits256_cmp(pub,G.LP_mypub25519) != 0 ) if ( bits256_cmp(pub,G.LP_mypub25519) != 0 )
@ -646,7 +646,7 @@ printf("LP_uitem_recv deprecated\n");
{ {
//char str[65]; printf("uitem %s %s %s/v%d %.8f ht.%d\n",symbol,coinaddr,bits256_str(str,txid),vout,dstr(value),height); //char str[65]; printf("uitem %s %s %s/v%d %.8f ht.%d\n",symbol,coinaddr,bits256_str(str,txid),vout,dstr(value),height);
if ( strcmp(coin->smartaddr,coinaddr) != 0 ) if ( strcmp(coin->smartaddr,coinaddr) != 0 )
LP_address_utxoadd((uint32_t)time(NULL),"LP_uitem_recv",coin,coinaddr,txid,vout,value,height,-1); LP_address_utxoadd(0,(uint32_t)time(NULL),"LP_uitem_recv",coin,coinaddr,txid,vout,value,height,-1);
//else printf("ignore external uitem %s %s\n",symbol,coin->smartaddr); //else printf("ignore external uitem %s %s\n",symbol,coin->smartaddr);
} }
return(clonestr("{\"result\":\"success\"}")); return(clonestr("{\"result\":\"success\"}"));
@ -691,19 +691,13 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_
jadd(reqjson,"proof",LP_instantdex_txids(0,coin->smartaddr)); jadd(reqjson,"proof",LP_instantdex_txids(0,coin->smartaddr));
} }
msg = jprint(reqjson,1); msg = jprint(reqjson,1);
printf("QUERY.(%s)\n",msg); //printf("QUERY.(%s)\n",msg);
//if ( bits256_nonz(qp->srchash) == 0 || strcmp(method,"request") != 0 ) //if ( bits256_nonz(qp->srchash) == 0 || strcmp(method,"request") != 0 )
{ {
memset(&zero,0,sizeof(zero)); memset(&zero,0,sizeof(zero));
if ( bits256_nonz(qp->srchash) != 0 )
LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg));
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,clonestr(msg)); LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,clonestr(msg));
//if ( strcmp(method,"request") == 0 )
if ( 0 )
{
sleep(1);
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,clonestr(msg));
sleep(1);
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,clonestr(msg));
}
free(msg); free(msg);
/*portable_mutex_lock(&LP_reservedmutex); /*portable_mutex_lock(&LP_reservedmutex);
if ( num_Reserved_msgs[1] < sizeof(Reserved_msgs[1])/sizeof(*Reserved_msgs[1])-2 ) if ( num_Reserved_msgs[1] < sizeof(Reserved_msgs[1])/sizeof(*Reserved_msgs[1])-2 )

6
iguana/exchanges/LP_socket.c

@ -303,7 +303,7 @@ int32_t electrum_process_array(struct iguana_info *coin,struct electrum_info *ep
else else
{ {
//printf("external unspent has no gettxout\n"); //printf("external unspent has no gettxout\n");
flag += LP_address_utxoadd((uint32_t)time(NULL),"electrum process",coin,coinaddr,txid,v,value,0,1); flag += LP_address_utxoadd(0,(uint32_t)time(NULL),"electrum process",coin,coinaddr,txid,v,value,0,1);
} }
} }
else else
@ -348,7 +348,7 @@ int32_t electrum_process_array(struct iguana_info *coin,struct electrum_info *ep
if ( tx->height > 0 ) if ( tx->height > 0 )
{ {
//printf("from electrum_process_array\n"); //printf("from electrum_process_array\n");
flag += LP_address_utxoadd((uint32_t)time(NULL),"electrum process2",coin,coinaddr,txid,v,value,tx->height,-1); flag += LP_address_utxoadd(0,(uint32_t)time(NULL),"electrum process2",coin,coinaddr,txid,v,value,tx->height,-1);
} }
//printf("v.%d numvouts.%d %.8f (%s)\n",v,tx->numvouts,dstr(tx->outpoints[jint(item,"tx_pos")].value),jprint(item,0)); //printf("v.%d numvouts.%d %.8f (%s)\n",v,tx->numvouts,dstr(tx->outpoints[jint(item,"tx_pos")].value),jprint(item,0));
} //else printf("cant find tx\n"); } //else printf("cant find tx\n");
@ -551,7 +551,7 @@ cJSON *electrum_address_gethistory(char *symbol,struct electrum_info *ep,cJSON *
if ( tx->height > 0 && tx->height != height ) if ( tx->height > 0 && tx->height != height )
printf("update %s height.%d <- %d\n",bits256_str(str,txid),tx->height,height); printf("update %s height.%d <- %d\n",bits256_str(str,txid),tx->height,height);
tx->height = height; tx->height = height;
LP_address_utxoadd((uint32_t)time(NULL),"electrum history",coin,addr,txid,0,0,height,-1); LP_address_utxoadd(0,(uint32_t)time(NULL),"electrum history",coin,addr,txid,0,0,height,-1);
} }
} }
} }

2
iguana/exchanges/LP_statemachine.c

@ -2491,7 +2491,7 @@ 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; 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; swap = utxo->swap;
fprintf(stderr,"start swap iambob.%d\n",swap->I.iambob); //fprintf(stderr,"start swap iambob.%d\n",swap->I.iambob);
maxlen = 1024*1024 + sizeof(*swap); maxlen = 1024*1024 + sizeof(*swap);
data = malloc(maxlen); data = malloc(maxlen);
expiration = (uint32_t)time(NULL) + 300; expiration = (uint32_t)time(NULL) + 300;

8
iguana/exchanges/LP_stats.c

@ -471,7 +471,7 @@ int32_t LP_statslog_parsequote(char *method,cJSON *lineobj)
flag = 1; flag = 1;
break; break;
} }
if ( counter++ < 10 ) if ( counter++ < 1 )
printf("error after delayed match\n"); printf("error after delayed match\n");
} }
} }
@ -727,7 +727,7 @@ int32_t LP_stats_dispiter(cJSON *array,struct LP_swapstats *sp,uint32_t starttim
cJSON *LP_statslog_disp(uint32_t starttime,uint32_t endtime,char *refgui,bits256 refpubkey,char *refbase,char *refrel) cJSON *LP_statslog_disp(uint32_t starttime,uint32_t endtime,char *refgui,bits256 refpubkey,char *refbase,char *refrel)
{ {
static int32_t rval; static int32_t rval;
cJSON *retjson,*array,*item,*reqjson; struct LP_pubkey_info *pubp,*ptmp; bits256 zero; uint32_t now; struct LP_swapstats *sp,*tmp; int32_t i,n,numtrades[LP_MAXPRICEINFOS]; uint64_t basevols[LP_MAXPRICEINFOS],relvols[LP_MAXPRICEINFOS]; cJSON *retjson,*array,*item; struct LP_pubkey_info *pubp,*ptmp; uint32_t now; struct LP_swapstats *sp,*tmp; int32_t i,n,numtrades[LP_MAXPRICEINFOS]; uint64_t basevols[LP_MAXPRICEINFOS],relvols[LP_MAXPRICEINFOS];
if ( rval == 0 ) if ( rval == 0 )
rval = (LP_rand() % 300) + 60; rval = (LP_rand() % 300) + 60;
if ( starttime > endtime ) if ( starttime > endtime )
@ -754,14 +754,14 @@ cJSON *LP_statslog_disp(uint32_t starttime,uint32_t endtime,char *refgui,bits256
else else
{ {
LP_RTcount++; LP_RTcount++;
if ( now > sp->lasttime+rval ) /*if ( now > sp->lasttime+rval )
{ {
reqjson = cJSON_CreateObject(); reqjson = cJSON_CreateObject();
jaddstr(reqjson,"method","gettradestatus"); jaddstr(reqjson,"method","gettradestatus");
jadd64bits(reqjson,"aliceid",sp->aliceid); jadd64bits(reqjson,"aliceid",sp->aliceid);
memset(zero.bytes,0,sizeof(zero)); memset(zero.bytes,0,sizeof(zero));
LP_reserved_msg(0,"","",zero,jprint(reqjson,1)); LP_reserved_msg(0,"","",zero,jprint(reqjson,1));
} }*/
} }
} }
HASH_ITER(hh,LP_swapstats,sp,tmp) HASH_ITER(hh,LP_swapstats,sp,tmp)

19
iguana/exchanges/LP_swap.c

@ -479,7 +479,7 @@ int32_t LP_waitfor(int32_t pairsock,struct basilisk_swap *swap,int32_t timeout,i
} // else printf("error nn_recv\n"); } // else printf("error nn_recv\n");
} }
} }
printf("waitfor timedout\n"); printf("waitfor timedout aliceid.%llu requestid.%u quoteid.%u\n",(long long)swap->aliceid,swap->I.req.requestid,swap->I.req.quoteid);
return(retval); return(retval);
} }
@ -625,7 +625,7 @@ int32_t LP_rawtx_spendscript(struct basilisk_swap *swap,int32_t height,struct ba
if ( recvlen != datalen+rawtx->I.redeemlen+75 ) if ( recvlen != datalen+rawtx->I.redeemlen+75 )
printf("RECVLEN %d != %d + %d\n",recvlen,datalen,rawtx->I.redeemlen); printf("RECVLEN %d != %d + %d\n",recvlen,datalen,rawtx->I.redeemlen);
txid = bits256_calctxid(coin->symbol,data,datalen); txid = bits256_calctxid(coin->symbol,data,datalen);
char str[65]; printf("rawtx.%s txid %s\n",rawtx->name,bits256_str(str,txid)); //char str[65]; printf("rawtx.%s txid %s\n",rawtx->name,bits256_str(str,txid));
if ( bits256_cmp(txid,rawtx->I.actualtxid) != 0 && bits256_nonz(rawtx->I.actualtxid) == 0 ) if ( bits256_cmp(txid,rawtx->I.actualtxid) != 0 && bits256_nonz(rawtx->I.actualtxid) == 0 )
rawtx->I.actualtxid = txid; rawtx->I.actualtxid = txid;
if ( (txobj= bitcoin_data2json(coin->symbol,coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,height,&rawtx->I.signedtxid,&rawtx->msgtx,rawtx->extraspace,sizeof(rawtx->extraspace),data,datalen,0,suppress_pubkeys,coin->zcash)) != 0 ) if ( (txobj= bitcoin_data2json(coin->symbol,coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,height,&rawtx->I.signedtxid,&rawtx->msgtx,rawtx->extraspace,sizeof(rawtx->extraspace),data,datalen,0,suppress_pubkeys,coin->zcash)) != 0 )
@ -659,9 +659,8 @@ int32_t LP_rawtx_spendscript(struct basilisk_swap *swap,int32_t height,struct ba
retval = 0; retval = 0;
if ( rawtx == &swap->otherfee ) if ( rawtx == &swap->otherfee )
{ {
char str[65];
LP_swap_coinaddr(coin,rawtx->p2shaddr,0,data,datalen,0); LP_swap_coinaddr(coin,rawtx->p2shaddr,0,data,datalen,0);
printf("got %s txid.%s (%s) -> %s\n",rawtx->name,bits256_str(str,rawtx->I.signedtxid),jprint(txobj,0),rawtx->p2shaddr); //printf("got %s txid.%s (%s) -> %s\n",rawtx->name,bits256_str(str,rawtx->I.signedtxid),jprint(txobj,0),rawtx->p2shaddr);
} else bitcoin_address(coin->symbol,rawtx->p2shaddr,coin->taddr,coin->p2shtype,rawtx->spendscript,hexlen); } else bitcoin_address(coin->symbol,rawtx->p2shaddr,coin->taddr,coin->p2shtype,rawtx->spendscript,hexlen);
} }
} else printf("%s satoshis %.8f ERROR.(%s) txfees.[%.8f %.8f: %.8f] amount.%.8f -> %.8f\n",rawtx->name,dstr(j64bits(vout,"satoshis")),jprint(txobj,0),dstr(swap->I.Atxfee),dstr(swap->I.Btxfee),dstr(txfee),dstr(rawtx->I.amount),dstr(rawtx->I.amount)-dstr(txfee)); } else printf("%s satoshis %.8f ERROR.(%s) txfees.[%.8f %.8f: %.8f] amount.%.8f -> %.8f\n",rawtx->name,dstr(j64bits(vout,"satoshis")),jprint(txobj,0),dstr(swap->I.Atxfee),dstr(swap->I.Btxfee),dstr(txfee),dstr(rawtx->I.amount),dstr(rawtx->I.amount)-dstr(txfee));
@ -1002,8 +1001,8 @@ int32_t instantdex_pubkeyargs(struct basilisk_swap *swap,int32_t numpubs,bits256
} }
n++; n++;
} }
if ( n > 2 || m > 2 ) //if ( n > 2 || m > 2 )
printf("n.%d m.%d len.%d numpubs.%d\n",n,m,len,swap->I.numpubs); // printf("n.%d m.%d len.%d numpubs.%d\n",n,m,len,swap->I.numpubs);
return(n); return(n);
} }
@ -1013,7 +1012,7 @@ void basilisk_rawtx_setparms(char *name,uint32_t quoteid,struct basilisk_rawtx *
numconfirms = 0; numconfirms = 0;
#endif #endif
strcpy(rawtx->name,name); strcpy(rawtx->name,name);
printf("set coin.%s %s -> %s\n",coin->symbol,coin->smartaddr,name); //printf("set coin.%s %s -> %s\n",coin->symbol,coin->smartaddr,name);
strcpy(rawtx->symbol,coin->symbol); strcpy(rawtx->symbol,coin->symbol);
rawtx->I.numconfirms = numconfirms; rawtx->I.numconfirms = numconfirms;
if ( (rawtx->I.amount= satoshis) < LP_MIN_TXFEE ) if ( (rawtx->I.amount= satoshis) < LP_MIN_TXFEE )
@ -1036,8 +1035,8 @@ void basilisk_rawtx_setparms(char *name,uint32_t quoteid,struct basilisk_rawtx *
if ( rawtx->I.vouttype <= 1 && rawtx->I.destaddr[0] != 0 ) if ( rawtx->I.vouttype <= 1 && rawtx->I.destaddr[0] != 0 )
{ {
rawtx->I.spendlen = bitcoin_standardspend(rawtx->spendscript,0,rawtx->I.rmd160); rawtx->I.spendlen = bitcoin_standardspend(rawtx->spendscript,0,rawtx->I.rmd160);
printf("%s spendlen.%d %s <- %.8f\n",name,rawtx->I.spendlen,rawtx->I.destaddr,dstr(rawtx->I.amount)); //printf("%s spendlen.%d %s <- %.8f\n",name,rawtx->I.spendlen,rawtx->I.destaddr,dstr(rawtx->I.amount));
} else printf("%s vouttype.%d destaddr.(%s)\n",name,rawtx->I.vouttype,rawtx->I.destaddr); } //else printf("%s vouttype.%d destaddr.(%s)\n",name,rawtx->I.vouttype,rawtx->I.destaddr);
} }
struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256 pubkey25519,struct basilisk_swap *swap,int32_t optionduration,uint32_t statebits,struct LP_quoteinfo *qp,int32_t dynamictrust) struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256 pubkey25519,struct basilisk_swap *swap,int32_t optionduration,uint32_t statebits,struct LP_quoteinfo *qp,int32_t dynamictrust)
@ -1199,7 +1198,7 @@ struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256
swap->myfee.utxotxid = qp->feetxid, swap->myfee.utxovout = qp->feevout; swap->myfee.utxotxid = qp->feetxid, swap->myfee.utxovout = qp->feevout;
LP_mark_spent(swap->I.alicestr,qp->feetxid,qp->feevout); LP_mark_spent(swap->I.alicestr,qp->feetxid,qp->feevout);
} }
char str[65],str2[65],str3[65]; printf("IAMBOB.%d %s %s %s [%s %s]\n",swap->I.iambob,bits256_str(str,qp->txid),bits256_str(str2,qp->txid2),bits256_str(str3,qp->feetxid),swap->I.bobstr,swap->I.alicestr); //char str[65],str2[65],str3[65]; printf("IAMBOB.%d %s %s %s [%s %s]\n",swap->I.iambob,bits256_str(str,qp->txid),bits256_str(str2,qp->txid2),bits256_str(str3,qp->feetxid),swap->I.bobstr,swap->I.alicestr);
return(swap); return(swap);
} }

89
iguana/exchanges/LP_transaction.c

@ -744,7 +744,7 @@ void test_validate(struct iguana_info *coin,char *signedtx)
char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,uint8_t wiftaddr,uint8_t taddr,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,void *ctx,bits256 privkey,bits256 *privkey2p,uint8_t *redeemscript,int32_t redeemlen,uint8_t *userdata,int32_t userdatalen,bits256 utxotxid,int32_t utxovout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr,char *vinaddr,int32_t suppress_pubkeys,int32_t zcash) char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,uint8_t wiftaddr,uint8_t taddr,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,void *ctx,bits256 privkey,bits256 *privkey2p,uint8_t *redeemscript,int32_t redeemlen,uint8_t *userdata,int32_t userdatalen,bits256 utxotxid,int32_t utxovout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr,char *vinaddr,int32_t suppress_pubkeys,int32_t zcash)
{ {
char *rawtxbytes=0,*signedtx=0,str[65],tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *txobj,*vins,*obj,*vouts,*item,*privkeys; int32_t completed,spendlen,n,ignore_cltverr=1; struct vin_info V[8]; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value=0,change = 0; struct iguana_msgtx msgtx; struct iguana_info *coin; char *rawtxbytes=0,*signedtx=0,tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *txobj,*vins,*obj,*vouts,*item,*privkeys; int32_t completed,spendlen,n,ignore_cltverr=1; struct vin_info V[8]; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value=0,change = 0; struct iguana_msgtx msgtx; struct iguana_info *coin;
LP_mark_spent(symbol,utxotxid,utxovout); LP_mark_spent(symbol,utxotxid,utxovout);
*destamountp = 0; *destamountp = 0;
memset(signedtxidp,0,sizeof(*signedtxidp)); memset(signedtxidp,0,sizeof(*signedtxidp));
@ -771,7 +771,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch
} else printf("cant gettx\n"); } else printf("cant gettx\n");
if ( value == 0 ) if ( value == 0 )
{ {
printf("basilisk_swap_bobtxspend.%s %s utxo.(%s).v%d already spent or doesnt exist\n",name,symbol,bits256_str(str,utxotxid),utxovout); //printf("basilisk_swap_bobtxspend.%s %s utxo.(%s).v%d already spent or doesnt exist\n",name,symbol,bits256_str(str,utxotxid),utxovout);
return(0); return(0);
} }
#endif #endif
@ -790,7 +790,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch
else if ( value == satoshis && (double)txfee/value < 0.25 ) else if ( value == satoshis && (double)txfee/value < 0.25 )
{ {
satoshis = value - txfee; satoshis = value - txfee;
printf("txfee allocation from value %.8f identical to satoshis: %.8f txfee %.8f\n",dstr(value),dstr(satoshis),dstr(txfee)); //printf("txfee allocation from value %.8f identical to satoshis: %.8f txfee %.8f\n",dstr(value),dstr(satoshis),dstr(txfee));
} }
else else
{ {
@ -800,7 +800,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch
} }
if ( value > satoshis+txfee ) if ( value > satoshis+txfee )
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)); //printf("utxo %.8f, destamount %.8f change %.8f txfee %.8f\n",dstr(value),dstr(satoshis),dstr(change),dstr(txfee));
} else if ( value > txfee ) } else if ( value > txfee )
satoshis = value - txfee; satoshis = value - txfee;
else else
@ -904,7 +904,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch
printf("incomplete signing suppress.%d %s (%s)\n",suppress_pubkeys,name,jprint(vins,0)); printf("incomplete signing suppress.%d %s (%s)\n",suppress_pubkeys,name,jprint(vins,0));
if ( signedtx != 0 ) if ( signedtx != 0 )
free(signedtx), signedtx = 0; free(signedtx), signedtx = 0;
} else printf("basilisk_swap_bobtxspend %s -> %s\n",name,bits256_str(str,*signedtxidp)); } // else printf("basilisk_swap_bobtxspend %s -> %s\n",name,bits256_str(str,*signedtxidp));
free(rawtxbytes); free(rawtxbytes);
} else printf("error making rawtx suppress.%d\n",suppress_pubkeys); } else printf("error making rawtx suppress.%d\n",suppress_pubkeys);
free_json(privkeys); free_json(privkeys);
@ -1077,7 +1077,7 @@ int32_t LP_vins_select(void *ctx,struct iguana_info *coin,int64_t *totalp,int64_
if ( up == 0 ) if ( up == 0 )
{ {
value = LP_txvalue(0,coin->symbol,utxotxid,utxovout); value = LP_txvalue(0,coin->symbol,utxotxid,utxovout);
LP_address_utxoadd((uint32_t)time(NULL),"withdraw",coin,coin->smartaddr,utxotxid,utxovout,value,1,-1); LP_address_utxoadd(0,(uint32_t)time(NULL),"withdraw",coin,coin->smartaddr,utxotxid,utxovout,value,1,-1);
//printf("added after not finding\n"); //printf("added after not finding\n");
} }
if ( (up= LP_address_utxofind(coin,coin->smartaddr,utxotxid,utxovout)) != 0 ) if ( (up= LP_address_utxofind(coin,coin->smartaddr,utxotxid,utxovout)) != 0 )
@ -1085,7 +1085,13 @@ int32_t LP_vins_select(void *ctx,struct iguana_info *coin,int64_t *totalp,int64_
else else
{ {
printf("couldnt add address_utxo after not finding\n"); printf("couldnt add address_utxo after not finding\n");
return(0); sleep(1);
value = LP_txvalue(0,coin->symbol,utxotxid,utxovout);
LP_address_utxoadd(0,(uint32_t)time(NULL),"withdraw",coin,coin->smartaddr,utxotxid,utxovout,value,1,-1);
if ( (up= LP_address_utxofind(coin,coin->smartaddr,utxotxid,utxovout)) != 0 )
preselected[numpre++] = up;
else printf("second couldnt add address_utxo after not finding\n");
//return(0);
} }
} }
if ( dustcombine >= 1 && min0 != 0 && min0->U.value < LP_DUSTCOMBINE_THRESHOLD && (coin->electrum == 0 || min0->SPV > 0) ) if ( dustcombine >= 1 && min0 != 0 && min0->U.value < LP_DUSTCOMBINE_THRESHOLD && (coin->electrum == 0 || min0->SPV > 0) )
@ -1211,6 +1217,9 @@ char *LP_createrawtransaction(cJSON **txobjp,int32_t *numvinsp,struct iguana_inf
else if ( coin->numutxos >= LP_MAXDESIRED_UTXOS ) else if ( coin->numutxos >= LP_MAXDESIRED_UTXOS )
dustcombine = 2; dustcombine = 2;
else dustcombine = 1; else dustcombine = 1;
#ifdef LP_DISABLE_DISTCOMBINE
dustcombine = 0;
#endif
amount = txfee; amount = txfee;
for (i=0; i<numvouts; i++) for (i=0; i<numvouts; i++)
{ {
@ -1228,7 +1237,7 @@ char *LP_createrawtransaction(cJSON **txobjp,int32_t *numvinsp,struct iguana_inf
return(0); return(0);
} }
amount += value; amount += value;
printf("vout.%d %.8f -> total %.8f\n",i,dstr(value),dstr(amount)); //printf("vout.%d %.8f -> total %.8f\n",i,dstr(value),dstr(amount));
} }
else else
{ {
@ -1271,7 +1280,7 @@ char *LP_createrawtransaction(cJSON **txobjp,int32_t *numvinsp,struct iguana_inf
txobj = bitcoin_txcreate(coin->symbol,coin->isPoS,locktime,coin->txversion,timestamp); txobj = bitcoin_txcreate(coin->symbol,coin->isPoS,locktime,coin->txversion,timestamp);
jdelete(txobj,"vin"); jdelete(txobj,"vin");
jadd(txobj,"vin",jduplicate(vins)); jadd(txobj,"vin",jduplicate(vins));
printf("change %.8f = total %.8f - amount %.8f, adjust %.8f numvouts.%d\n",dstr(change),dstr(total),dstr(amount),dstr(adjust),numvouts); //printf("change %.8f = total %.8f - amount %.8f, adjust %.8f numvouts.%d\n",dstr(change),dstr(total),dstr(amount),dstr(adjust),numvouts);
for (i=0; i<numvouts; i++) for (i=0; i<numvouts; i++)
{ {
item = jitem(outputs,i); item = jitem(outputs,i);
@ -1371,7 +1380,7 @@ char *LP_withdraw(struct iguana_info *coin,cJSON *argjson)
printf("incomplete signing withdraw (%s)\n",jprint(vins,0)); printf("incomplete signing withdraw (%s)\n",jprint(vins,0));
if ( signedtx != 0 ) if ( signedtx != 0 )
free(signedtx), signedtx = 0; free(signedtx), signedtx = 0;
} else printf("LP_withdraw.%s %s -> %s\n",coin->symbol,jprint(argjson,0),bits256_str(str,signedtxid)); } //else printf("LP_withdraw.%s %s -> %s\n",coin->symbol,jprint(argjson,0),bits256_str(str,signedtxid));
if ( signedtx == 0 ) if ( signedtx == 0 )
break; break;
datalen = (int32_t)strlen(signedtx) / 2; datalen = (int32_t)strlen(signedtx) / 2;
@ -1455,7 +1464,7 @@ int32_t basilisk_rawtx_gen(void *ctx,char *str,uint32_t swapstarted,uint8_t *pub
rawtx->I.completed = 1; rawtx->I.completed = 1;
rawtx->I.signedtxid = jbits256(retjson,"txid"); rawtx->I.signedtxid = jbits256(retjson,"txid");
retval = 0; retval = 0;
} else printf("rawtx withdraw error? (%s)\n",retstr); } else printf("rawtx withdraw error? (%s)\n",jprint(argjson,0));
free_json(retjson); free_json(retjson);
} }
free(retstr); free(retstr);
@ -1807,9 +1816,9 @@ int32_t basilisk_bobscript(uint8_t *rmd160,uint8_t *redeemscript,int32_t *redeem
{ {
calc_rmd160_sha256(rmd160,redeemscript,n); calc_rmd160_sha256(rmd160,redeemscript,n);
n = bitcoin_p2shspend(script,0,rmd160); n = bitcoin_p2shspend(script,0,rmd160);
int32_t i; for (i=0; i<n; i++) //int32_t i; for (i=0; i<n; i++)
printf("%02x",script[i]); // printf("%02x",script[i]);
printf(" <- redeem.%d bobtx dflag.%d spendscript.[%d]\n",*redeemlenp,depositflag,n); //printf(" <- redeem.%d bobtx dflag.%d spendscript.[%d]\n",*redeemlenp,depositflag,n);
} }
return(n); return(n);
} }
@ -1842,7 +1851,7 @@ int32_t basilisk_swapuserdata(uint8_t *userdata,bits256 privkey,int32_t ifpath,b
int32_t basilisk_bobpayment_reclaim(struct basilisk_swap *swap,int32_t delay) int32_t basilisk_bobpayment_reclaim(struct basilisk_swap *swap,int32_t delay)
{ {
static bits256 zero; static bits256 zero;
uint8_t userdata[512]; int32_t retval,i,len = 0; struct iguana_info *coin; uint8_t userdata[512]; int32_t retval,len = 0; struct iguana_info *coin;
if ( (coin= LP_coinfind(swap->I.bobstr)) != 0 ) if ( (coin= LP_coinfind(swap->I.bobstr)) != 0 )
{ {
//printf("basilisk_bobpayment_reclaim\n"); //printf("basilisk_bobpayment_reclaim\n");
@ -1851,9 +1860,9 @@ int32_t basilisk_bobpayment_reclaim(struct basilisk_swap *swap,int32_t delay)
swap->I.userdata_bobreclaimlen = len; swap->I.userdata_bobreclaimlen = len;
if ( (retval= basilisk_rawtx_sign(coin->symbol,coin->wiftaddr,coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,swap,&swap->bobreclaim,&swap->bobpayment,swap->I.myprivs[1],0,userdata,len,1,swap->changermd160,swap->bobpayment.I.destaddr,coin->zcash)) == 0 ) if ( (retval= basilisk_rawtx_sign(coin->symbol,coin->wiftaddr,coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,swap,&swap->bobreclaim,&swap->bobpayment,swap->I.myprivs[1],0,userdata,len,1,swap->changermd160,swap->bobpayment.I.destaddr,coin->zcash)) == 0 )
{ {
for (i=0; i<swap->bobreclaim.I.datalen; i++) //for (i=0; i<swap->bobreclaim.I.datalen; i++)
printf("%02x",swap->bobreclaim.txbytes[i]); // printf("%02x",swap->bobreclaim.txbytes[i]);
printf(" <- bobreclaim\n"); //printf(" <- bobreclaim\n");
//basilisk_txlog(swap,&swap->bobreclaim,delay); //basilisk_txlog(swap,&swap->bobreclaim,delay);
return(retval); return(retval);
} }
@ -1903,7 +1912,7 @@ void LP_swap_coinaddr(struct iguana_info *coin,char *coinaddr,uint64_t *valuep,u
int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,int32_t genflag) int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,int32_t genflag)
{ {
int32_t j; char coinaddr[64],checkaddr[64]; struct iguana_info *coin; char coinaddr[64],checkaddr[64]; struct iguana_info *coin;
if ( (coin= LP_coinfind(swap->I.bobstr)) != 0 ) if ( (coin= LP_coinfind(swap->I.bobstr)) != 0 )
{ {
bitcoin_address(coin->symbol,coinaddr,coin->taddr,coin->pubtype,swap->changermd160,20); bitcoin_address(coin->symbol,coinaddr,coin->taddr,coin->pubtype,swap->changermd160,20);
@ -1928,13 +1937,13 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i
} }
else else
{ {
for (j=0; j<swap->bobpayment.I.datalen; j++) /*for (j=0; j<swap->bobpayment.I.datalen; j++)
printf("%02x",swap->bobpayment.txbytes[j]); printf("%02x",swap->bobpayment.txbytes[j]);
printf(" <- bobpayment.%d\n",swap->bobpayment.I.datalen); printf(" <- bobpayment.%d\n",swap->bobpayment.I.datalen);
for (j=0; j<swap->bobpayment.I.redeemlen; j++) for (j=0; j<swap->bobpayment.I.redeemlen; j++)
printf("%02x",swap->bobpayment.redeemscript[j]); printf("%02x",swap->bobpayment.redeemscript[j]);
printf(" <- redeem.%d\n",swap->bobpayment.I.redeemlen); printf(" <- redeem.%d\n",swap->bobpayment.I.redeemlen);
printf(" <- GENERATED BOB PAYMENT.%d destaddr.(%s)\n",swap->bobpayment.I.datalen,swap->bobpayment.I.destaddr); printf(" <- GENERATED BOB PAYMENT.%d destaddr.(%s)\n",swap->bobpayment.I.datalen,swap->bobpayment.I.destaddr);*/
LP_swap_coinaddr(coin,checkaddr,0,swap->bobpayment.txbytes,swap->bobpayment.I.datalen,0); LP_swap_coinaddr(coin,checkaddr,0,swap->bobpayment.txbytes,swap->bobpayment.I.datalen,0);
if ( strcmp(swap->bobpayment.I.destaddr,checkaddr) != 0 ) if ( strcmp(swap->bobpayment.I.destaddr,checkaddr) != 0 )
{ {
@ -1965,9 +1974,9 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i
} }
else else
{ {
for (j=0; j<swap->bobdeposit.I.datalen; j++) //for (j=0; j<swap->bobdeposit.I.datalen; j++)
printf("%02x",swap->bobdeposit.txbytes[j]); // printf("%02x",swap->bobdeposit.txbytes[j]);
printf(" <- GENERATED BOB DEPOSIT.%d (%s)\n",swap->bobdeposit.I.datalen,swap->bobdeposit.I.destaddr); //printf(" <- GENERATED BOB DEPOSIT.%d (%s)\n",swap->bobdeposit.I.datalen,swap->bobdeposit.I.destaddr);
LP_swap_coinaddr(coin,checkaddr,0,swap->bobdeposit.txbytes,swap->bobdeposit.I.datalen,0); LP_swap_coinaddr(coin,checkaddr,0,swap->bobdeposit.txbytes,swap->bobdeposit.I.datalen,0);
if ( strcmp(swap->bobdeposit.I.destaddr,checkaddr) != 0 ) if ( strcmp(swap->bobdeposit.I.destaddr,checkaddr) != 0 )
{ {
@ -1975,7 +1984,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i
return(-1); return(-1);
} }
LP_unspents_mark(coin->symbol,swap->bobdeposit.vins); LP_unspents_mark(coin->symbol,swap->bobdeposit.vins);
printf("bobscripts set completed\n"); //printf("bobscripts set completed\n");
return(0); return(0);
} }
} }
@ -2028,7 +2037,7 @@ void basilisk_alicepayment(struct basilisk_swap *swap,struct iguana_info *coin,s
int32_t basilisk_alicetxs(int32_t pairsock,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)
{ {
char coinaddr[64]; int32_t i,retval = -1; struct iguana_info *coin; char coinaddr[64]; int32_t retval = -1; struct iguana_info *coin;
if ( (coin= LP_coinfind(swap->I.alicestr)) != 0 ) if ( (coin= LP_coinfind(swap->I.alicestr)) != 0 )
{ {
if ( swap->alicepayment.I.datalen == 0 ) if ( swap->alicepayment.I.datalen == 0 )
@ -2041,26 +2050,26 @@ int32_t basilisk_alicetxs(int32_t pairsock,struct basilisk_swap *swap,uint8_t *d
//LP_importaddress(coin->symbol,swap->alicepayment.I.destaddr); //LP_importaddress(coin->symbol,swap->alicepayment.I.destaddr);
strcpy(swap->alicepayment.p2shaddr,swap->alicepayment.I.destaddr); strcpy(swap->alicepayment.p2shaddr,swap->alicepayment.I.destaddr);
retval = 0; retval = 0;
for (i=0; i<swap->alicepayment.I.datalen; i++) //for (i=0; i<swap->alicepayment.I.datalen; i++)
printf("%02x",swap->alicepayment.txbytes[i]); // printf("%02x",swap->alicepayment.txbytes[i]);
printf(" ALICE PAYMENT created.(%s)\n",swap->alicepayment.I.destaddr); //printf(" ALICE PAYMENT created.(%s)\n",swap->alicepayment.I.destaddr);
LP_unspents_mark(coin->symbol,swap->alicepayment.vins); LP_unspents_mark(coin->symbol,swap->alicepayment.vins);
//LP_importaddress(coin->symbol,swap->alicepayment.I.destaddr); //LP_importaddress(coin->symbol,swap->alicepayment.I.destaddr);
//basilisk_txlog(swap,&swap->alicepayment,-1); //basilisk_txlog(swap,&swap->alicepayment,-1);
} }
if ( swap->myfee.I.datalen == 0 ) if ( swap->myfee.I.datalen == 0 )
{ {
printf("%s generate fee %.8f from.%s\n",coin->symbol,dstr(strcmp(coin->symbol,"BTC") == 0 ? LP_MIN_TXFEE : coin->txfee),coin->smartaddr); //printf("%s generate fee %.8f from.%s\n",coin->symbol,dstr(strcmp(coin->symbol,"BTC") == 0 ? LP_MIN_TXFEE : coin->txfee),coin->smartaddr);
bitcoin_address(coin->symbol,coinaddr,coin->taddr,coin->pubtype,swap->changermd160,20); bitcoin_address(coin->symbol,coinaddr,coin->taddr,coin->pubtype,swap->changermd160,20);
if ( basilisk_rawtx_gen(swap->ctx,"myfee",swap->I.started,swap->persistent_pubkey33,swap->I.iambob,1,&swap->myfee,swap->myfee.I.locktime,swap->myfee.spendscript,swap->myfee.I.spendlen,strcmp(coin->symbol,"BTC") == 0 ? LP_MIN_TXFEE : coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,coinaddr) == 0 ) if ( basilisk_rawtx_gen(swap->ctx,"myfee",swap->I.started,swap->persistent_pubkey33,swap->I.iambob,1,&swap->myfee,swap->myfee.I.locktime,swap->myfee.spendscript,swap->myfee.I.spendlen,strcmp(coin->symbol,"BTC") == 0 ? LP_MIN_TXFEE : coin->txfee,1,0,swap->persistent_privkey,swap->changermd160,coinaddr) == 0 )
{ {
printf("rawtxsend %s %.8f\n",coin->symbol,dstr(strcmp(coin->symbol,"BTC") == 0 ? LP_MIN_TXFEE : coin->txfee)); //printf("rawtxsend %s %.8f\n",coin->symbol,dstr(strcmp(coin->symbol,"BTC") == 0 ? LP_MIN_TXFEE : coin->txfee));
swap->I.statebits |= LP_swapdata_rawtxsend(pairsock,swap,0x80,data,maxlen,&swap->myfee,0x40,0); swap->I.statebits |= LP_swapdata_rawtxsend(pairsock,swap,0x80,data,maxlen,&swap->myfee,0x40,0);
LP_unspents_mark(swap->I.iambob!=0?coin->symbol:coin->symbol,swap->myfee.vins); LP_unspents_mark(swap->I.iambob!=0?coin->symbol:coin->symbol,swap->myfee.vins);
//basilisk_txlog(swap,&swap->myfee,-1); //basilisk_txlog(swap,&swap->myfee,-1);
for (i=0; i<swap->myfee.I.datalen; i++) //for (i=0; i<swap->myfee.I.datalen; i++)
printf("%02x",swap->myfee.txbytes[i]); // printf("%02x",swap->myfee.txbytes[i]);
printf(" <- fee state.%x\n",swap->I.statebits); //printf(" <- fee state.%x\n",swap->I.statebits);
swap->I.statebits |= 0x40; swap->I.statebits |= 0x40;
} }
else else
@ -2071,7 +2080,7 @@ int32_t basilisk_alicetxs(int32_t pairsock,struct basilisk_swap *swap,uint8_t *d
} }
if ( swap->alicepayment.I.datalen != 0 && swap->alicepayment.I.spendlen > 0 && swap->myfee.I.datalen != 0 && swap->myfee.I.spendlen > 0 ) if ( swap->alicepayment.I.datalen != 0 && swap->alicepayment.I.spendlen > 0 && swap->myfee.I.datalen != 0 && swap->myfee.I.spendlen > 0 )
{ {
printf("fee sent\n"); //printf("fee sent\n");
return(0); return(0);
} }
} else printf("basilisk alicetx cant find (%s)\n",swap->I.alicestr); } else printf("basilisk alicetx cant find (%s)\n",swap->I.alicestr);
@ -2085,14 +2094,16 @@ int32_t LP_verify_otherfee(struct basilisk_swap *swap,uint8_t *data,int32_t data
{ {
if ( LP_rawtx_spendscript(swap,coin->longestchain,&swap->otherfee,0,data,datalen,0) == 0 ) if ( LP_rawtx_spendscript(swap,coin->longestchain,&swap->otherfee,0,data,datalen,0) == 0 )
{ {
printf("otherfee amount %.8f -> %s vs %s locktime %u vs %u\n",dstr(swap->otherfee.I.amount),swap->otherfee.p2shaddr,swap->otherfee.I.destaddr,swap->otherfee.I.locktime,swap->I.started+1); //printf("otherfee amount %.8f -> %s vs %s locktime %u vs %u\n",dstr(swap->otherfee.I.amount),swap->otherfee.p2shaddr,swap->otherfee.I.destaddr,swap->otherfee.I.locktime,swap->I.started+1);
if ( strcmp(swap->otherfee.I.destaddr,swap->otherfee.p2shaddr) == 0 ) if ( strcmp(swap->otherfee.I.destaddr,swap->otherfee.p2shaddr) == 0 )
{ {
diff = swap->otherfee.I.locktime - (swap->I.started+1); diff = swap->otherfee.I.locktime - (swap->I.started+1);
if ( diff < 0 ) if ( diff < 0 )
diff = -diff; diff = -diff;
if ( diff < LP_AUTOTRADE_TIMEOUT ) if ( diff < LP_AUTOTRADE_TIMEOUT )
printf("dexfee verified\n"); {
//printf("dexfee verified\n");
}
else printf("locktime mismatch in otherfee, reject %u vs %u\n",swap->otherfee.I.locktime,swap->I.started+1); else printf("locktime mismatch in otherfee, reject %u vs %u\n",swap->otherfee.I.locktime,swap->I.started+1);
return(0); return(0);
} else printf("destaddress mismatch in other fee, reject (%s) vs (%s)\n",swap->otherfee.I.destaddr,swap->otherfee.p2shaddr); } else printf("destaddress mismatch in other fee, reject (%s) vs (%s)\n",swap->otherfee.I.destaddr,swap->otherfee.p2shaddr);
@ -2159,12 +2170,12 @@ int32_t LP_verify_bobdeposit(struct basilisk_swap *swap,uint8_t *data,int32_t da
retval = 0; retval = 0;
if ( (retval= basilisk_rawtx_sign(coin->symbol,coin->wiftaddr,coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,swap,&swap->aliceclaim,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160,swap->bobdeposit.I.destaddr,coin->zcash)) == 0 ) if ( (retval= basilisk_rawtx_sign(coin->symbol,coin->wiftaddr,coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,swap,&swap->aliceclaim,&swap->bobdeposit,swap->I.myprivs[0],0,userdata,len,1,swap->changermd160,swap->bobdeposit.I.destaddr,coin->zcash)) == 0 )
{ {
int32_t i; for (i=0; i<swap->bobdeposit.I.datalen; i++) /*int32_t i; for (i=0; i<swap->bobdeposit.I.datalen; i++)
printf("%02x",swap->bobdeposit.txbytes[i]); printf("%02x",swap->bobdeposit.txbytes[i]);
printf(" <- bobdeposit\n"); printf(" <- bobdeposit\n");
for (i=0; i<swap->aliceclaim.I.datalen; i++) for (i=0; i<swap->aliceclaim.I.datalen; i++)
printf("%02x",swap->aliceclaim.txbytes[i]); printf("%02x",swap->aliceclaim.txbytes[i]);
printf(" <- aliceclaim\n"); printf(" <- aliceclaim\n");*/
//basilisk_txlog(swap,&swap->aliceclaim,swap->I.putduration+swap->I.callduration); //basilisk_txlog(swap,&swap->aliceclaim,swap->I.putduration+swap->I.callduration);
return(LP_waitmempool(coin->symbol,swap->bobdeposit.I.destaddr,swap->bobdeposit.I.signedtxid,0,60)); return(LP_waitmempool(coin->symbol,swap->bobdeposit.I.destaddr,swap->bobdeposit.I.signedtxid,0,60));
} else printf("error signing aliceclaim suppress.%d vin.(%s)\n",swap->aliceclaim.I.suppress_pubkeys,swap->bobdeposit.I.destaddr); } else printf("error signing aliceclaim suppress.%d vin.(%s)\n",swap->aliceclaim.I.suppress_pubkeys,swap->bobdeposit.I.destaddr);

38
iguana/exchanges/LP_utxo.c

@ -381,7 +381,7 @@ void LP_mark_spent(char *symbol,bits256 txid,int32_t vout)
} }
} }
int32_t LP_address_utxoadd(uint32_t timestamp,char *debug,struct iguana_info *coin,char *coinaddr,bits256 txid,int32_t vout,uint64_t value,int32_t height,int32_t spendheight) int32_t LP_address_utxoadd(int32_t skipsearch,uint32_t timestamp,char *debug,struct iguana_info *coin,char *coinaddr,bits256 txid,int32_t vout,uint64_t value,int32_t height,int32_t spendheight)
{ {
struct LP_address *ap; cJSON *txobj; struct LP_transaction *tx; struct LP_address_utxo *up,*tmp; int32_t flag,retval = 0; //char str[65]; struct LP_address *ap; cJSON *txobj; struct LP_transaction *tx; struct LP_address_utxo *up,*tmp; int32_t flag,retval = 0; //char str[65];
if ( coin == 0 ) if ( coin == 0 )
@ -393,6 +393,8 @@ int32_t LP_address_utxoadd(uint32_t timestamp,char *debug,struct iguana_info *co
if ( ap != 0 ) if ( ap != 0 )
{ {
flag = 0; flag = 0;
if ( skipsearch == 0 )
{
DL_FOREACH_SAFE(ap->utxos,up,tmp) DL_FOREACH_SAFE(ap->utxos,up,tmp)
{ {
if ( vout == up->U.vout && bits256_cmp(up->U.txid,txid) == 0 ) if ( vout == up->U.vout && bits256_cmp(up->U.txid,txid) == 0 )
@ -409,6 +411,7 @@ int32_t LP_address_utxoadd(uint32_t timestamp,char *debug,struct iguana_info *co
break; break;
} }
} }
}
if ( flag == 0 && value != 0 ) if ( flag == 0 && value != 0 )
{ {
if ( coin->electrum == 0 ) if ( coin->electrum == 0 )
@ -447,7 +450,6 @@ struct LP_address *LP_address_utxo_reset(struct iguana_info *coin)
{ {
struct LP_address *ap; struct LP_address_utxo *up,*tmp; int32_t i,n,m,vout,height; cJSON *array,*item,*txobj; bits256 zero; int64_t value; bits256 txid; uint32_t now; struct LP_address *ap; struct LP_address_utxo *up,*tmp; int32_t i,n,m,vout,height; cJSON *array,*item,*txobj; bits256 zero; int64_t value; bits256 txid; uint32_t now;
LP_address(coin,coin->smartaddr); LP_address(coin,coin->smartaddr);
//printf("call listunspent issue %s (%s)\n",coin->symbol,coin->smartaddr);
memset(zero.bytes,0,sizeof(zero)); memset(zero.bytes,0,sizeof(zero));
LP_listunspent_issue(coin->symbol,coin->smartaddr,2,zero,zero); LP_listunspent_issue(coin->symbol,coin->smartaddr,2,zero,zero);
if ( (ap= LP_addressfind(coin,coin->smartaddr)) == 0 ) if ( (ap= LP_addressfind(coin,coin->smartaddr)) == 0 )
@ -455,18 +457,23 @@ struct LP_address *LP_address_utxo_reset(struct iguana_info *coin)
printf("LP_address_utxo_reset: cant find address data\n"); printf("LP_address_utxo_reset: cant find address data\n");
return(0); return(0);
} }
if ( IAMLP != 0 && time(NULL) < coin->lastresetutxo+10 )
return(ap);
coin->lastresetutxo = (uint32_t)time(NULL);
portable_mutex_lock(&coin->addressutxo_mutex);
if ( (array= LP_listunspent(coin->symbol,coin->smartaddr,zero,zero)) != 0 ) if ( (array= LP_listunspent(coin->symbol,coin->smartaddr,zero,zero)) != 0 )
{ {
printf("reset %s ap->utxos\n",coin->symbol);
portable_mutex_lock(&coin->addrmutex);
portable_mutex_lock(&LP_gcmutex);
DL_FOREACH_SAFE(ap->utxos,up,tmp) DL_FOREACH_SAFE(ap->utxos,up,tmp)
{ {
portable_mutex_lock(&coin->addrmutex);
DL_DELETE(ap->utxos,up); DL_DELETE(ap->utxos,up);
portable_mutex_unlock(&coin->addrmutex);
portable_mutex_lock(&LP_gcmutex);
up->spendheight = (int32_t)time(NULL); up->spendheight = (int32_t)time(NULL);
DL_APPEND(LP_garbage_collector2,up); DL_APPEND(LP_garbage_collector2,up);
portable_mutex_unlock(&LP_gcmutex);
} }
portable_mutex_unlock(&coin->addrmutex);
portable_mutex_unlock(&LP_gcmutex);
now = (uint32_t)time(NULL); now = (uint32_t)time(NULL);
if ( (n= cJSON_GetArraySize(array)) > 0 ) if ( (n= cJSON_GetArraySize(array)) > 0 )
{ {
@ -476,12 +483,15 @@ struct LP_address *LP_address_utxo_reset(struct iguana_info *coin)
//{"tx_hash":"38d1b7c73015e1b1d6cb7fc314cae402a635b7d7ea294970ab857df8777a66f4","tx_pos":0,"height":577975,"value":238700} //{"tx_hash":"38d1b7c73015e1b1d6cb7fc314cae402a635b7d7ea294970ab857df8777a66f4","tx_pos":0,"height":577975,"value":238700}
item = jitem(array,i); item = jitem(array,i);
value = LP_listunspent_parseitem(coin,&txid,&vout,&height,item); value = LP_listunspent_parseitem(coin,&txid,&vout,&height,item);
if ( 1 )
{
if ( (txobj= LP_gettxout(coin->symbol,coin->smartaddr,txid,vout)) == 0 ) if ( (txobj= LP_gettxout(coin->symbol,coin->smartaddr,txid,vout)) == 0 )
continue; continue;
else free_json(txobj); else free_json(txobj);
if ( LP_numconfirms(coin->symbol,coin->smartaddr,txid,vout,0) <= 0 ) if ( LP_numconfirms(coin->symbol,coin->smartaddr,txid,vout,0) <= 0 )
continue; continue;
LP_address_utxoadd(now,"withdraw",coin,coin->smartaddr,txid,vout,value,height,-1); }
LP_address_utxoadd(1,now,"withdraw",coin,coin->smartaddr,txid,vout,value,height,-1);
if ( (up= LP_address_utxofind(coin,coin->smartaddr,txid,vout)) == 0 ) if ( (up= LP_address_utxofind(coin,coin->smartaddr,txid,vout)) == 0 )
printf("couldnt find just added %s/%d ht.%d %.8f\n",bits256_str(str,txid),vout,height,dstr(value)); printf("couldnt find just added %s/%d ht.%d %.8f\n",bits256_str(str,txid),vout,height,dstr(value));
else else
@ -490,10 +500,11 @@ struct LP_address *LP_address_utxo_reset(struct iguana_info *coin)
//printf("%.8f ",dstr(value)); //printf("%.8f ",dstr(value));
} }
} }
//printf("added %d from listunspents\n",m); printf("added %d from %s listunspents\n",m,coin->symbol);
} }
free_json(array); free_json(array);
} }
portable_mutex_unlock(&coin->addressutxo_mutex);
return(ap); return(ap);
} }
@ -545,9 +556,10 @@ cJSON *LP_address_utxos(struct iguana_info *coin,char *coinaddr,int32_t electrum
} }
if ( up->spendheight <= 0 && up->U.value != 0 ) if ( up->spendheight <= 0 && up->U.value != 0 )
{ {
char str[65];
if ( LP_allocated(up->U.txid,up->U.vout) != 0 ) if ( LP_allocated(up->U.txid,up->U.vout) != 0 )
printf("%s %s/v%d allocated\n",coin->symbol,bits256_str(str,up->U.txid),up->U.vout); {
//printf("%s %s/v%d allocated\n",coin->symbol,bits256_str(str,up->U.txid),up->U.vout);
}
else if ( coin->electrum == 0 || up->SPV > 0 ) else if ( coin->electrum == 0 || up->SPV > 0 )
{ {
jaddi(array,LP_address_item(coin,up,electrumret)); jaddi(array,LP_address_item(coin,up,electrumret));
@ -728,7 +740,7 @@ int32_t LP_unspents_array(struct iguana_info *coin,char *coinaddr,cJSON *array)
if ( errs == 0 ) if ( errs == 0 )
{ {
//printf("from LP_unspents_array\n"); //printf("from LP_unspents_array\n");
LP_address_utxoadd((uint32_t)time(NULL),"LP_unspents_array",coin,coinaddr,txid,v,val,height,-1); LP_address_utxoadd(0,(uint32_t)time(NULL),"LP_unspents_array",coin,coinaddr,txid,v,val,height,-1);
count++; count++;
} }
} }
@ -792,7 +804,7 @@ cJSON *LP_transactioninit(struct iguana_info *coin,bits256 txid,int32_t iter,cJS
tx->outpoints[i].interest = SATOSHIDEN * jdouble(vout,"interest"); tx->outpoints[i].interest = SATOSHIDEN * jdouble(vout,"interest");
LP_destaddr(tx->outpoints[i].coinaddr,vout); LP_destaddr(tx->outpoints[i].coinaddr,vout);
//printf("from transaction init %s %s %s/v%d <- %.8f\n",coin->symbol,tx->outpoints[i].coinaddr,bits256_str(str,txid),i,dstr(tx->outpoints[i].value)); //printf("from transaction init %s %s %s/v%d <- %.8f\n",coin->symbol,tx->outpoints[i].coinaddr,bits256_str(str,txid),i,dstr(tx->outpoints[i].value));
LP_address_utxoadd((uint32_t)time(NULL),"LP_transactioninit iter0",coin,tx->outpoints[i].coinaddr,txid,i,tx->outpoints[i].value,height,-1); LP_address_utxoadd(0,(uint32_t)time(NULL),"LP_transactioninit iter0",coin,tx->outpoints[i].coinaddr,txid,i,tx->outpoints[i].value,height,-1);
} }
//printf("numvouts.%d\n",numvouts); //printf("numvouts.%d\n",numvouts);
} }
@ -814,7 +826,7 @@ cJSON *LP_transactioninit(struct iguana_info *coin,bits256 txid,int32_t iter,cJS
tx->outpoints[spentvout].spendtxid = txid; tx->outpoints[spentvout].spendtxid = txid;
tx->outpoints[spentvout].spendvini = i; tx->outpoints[spentvout].spendvini = i;
tx->outpoints[spentvout].spendheight = height > 0 ? height : 1; tx->outpoints[spentvout].spendheight = height > 0 ? height : 1;
LP_address_utxoadd((uint32_t)time(NULL),"LP_transactioninit iter1",coin,tx->outpoints[spentvout].coinaddr,spenttxid,spentvout,tx->outpoints[spentvout].value,-1,height>0?height:1); LP_address_utxoadd(0,(uint32_t)time(NULL),"LP_transactioninit iter1",coin,tx->outpoints[spentvout].coinaddr,spenttxid,spentvout,tx->outpoints[spentvout].value,-1,height>0?height:1);
if ( 0 && strcmp(coin->symbol,"REVS") == 0 ) if ( 0 && strcmp(coin->symbol,"REVS") == 0 )
printf("spend %s %s/v%d at ht.%d\n",coin->symbol,bits256_str(str,tx->txid),spentvout,height); printf("spend %s %s/v%d at ht.%d\n",coin->symbol,bits256_str(str,tx->txid),spentvout,height);
} }

4
iguana/exchanges/beertest

@ -1,3 +1,7 @@
source userpass source userpass
<<<<<<< HEAD
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"base\":\"BEER\",\"rel\":\"KMD\",\"fixed\":0.777,\"margin\":0.00001}" curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"base\":\"BEER\",\"rel\":\"KMD\",\"fixed\":0.777,\"margin\":0.00001}"
=======
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"base\":\"BEER\",\"rel\":\"KMD\",\"fixed\":1.28700,\"margin\":0.00001}"
>>>>>>> master

4
iguana/exchanges/coins

File diff suppressed because one or more lines are too long

14
iguana/exchanges/mm.c

@ -249,10 +249,17 @@ int main(int argc, const char * argv[])
sprintf(manystrs[num++],"\\\"%s\\\":%0.8f",coinaddr,val); sprintf(manystrs[num++],"\\\"%s\\\":%0.8f",coinaddr,val);
if ( num >= sizeof(manystrs)/sizeof(*manystrs) ) if ( num >= sizeof(manystrs)/sizeof(*manystrs) )
{ {
<<<<<<< HEAD
sprintf(cmd,"fiat/btch sendmany \"\" \"{ "); sprintf(cmd,"fiat/btch sendmany \"\" \"{ ");
for (i=0; i<num; i++) for (i=0; i<num; i++)
sprintf(cmd + strlen(cmd),"%s%c",manystrs[i],i<num-1?',':' '); sprintf(cmd + strlen(cmd),"%s%c",manystrs[i],i<num-1?',':' ');
strcat(cmd,"}\""); strcat(cmd,"}\"");
=======
sprintf(cmd,"fiat/btch sendmany \\\"\\\" \"{");
for (i=0; i<num; i++)
sprintf(cmd + strlen(cmd),"%s%s",manystrs[i],i<num-1?",":"");
strcat(cmd,"}\" 0");
>>>>>>> master
printf("%s\n",cmd); printf("%s\n",cmd);
num = 0; num = 0;
memset(manystrs,0,sizeof(manystrs)); memset(manystrs,0,sizeof(manystrs));
@ -264,10 +271,17 @@ int main(int argc, const char * argv[])
} }
if ( num > 0 ) if ( num > 0 )
{ {
<<<<<<< HEAD
sprintf(cmd,"fiat/btch sendmany \"\" \"{ "); sprintf(cmd,"fiat/btch sendmany \"\" \"{ ");
for (i=0; i<num; i++) for (i=0; i<num; i++)
sprintf(cmd + strlen(cmd),"%s%c",manystrs[i],i<num-1?',':' '); sprintf(cmd + strlen(cmd),"%s%c",manystrs[i],i<num-1?',':' ');
strcat(cmd,"}\""); strcat(cmd,"}\"");
=======
sprintf(cmd,"fiat/btch sendmany \\\"\\\" \"{");
for (i=0; i<num; i++)
sprintf(cmd + strlen(cmd),"%s%s",manystrs[i],i<num-1?",":"");
strcat(cmd,"}\" 0");
>>>>>>> master
printf("%s\n",cmd); printf("%s\n",cmd);
num = 0; num = 0;
memset(manystrs,0,sizeof(manystrs)); memset(manystrs,0,sizeof(manystrs));

4
iguana/exchanges/reset_mutex

@ -0,0 +1,4 @@
#!/bin/sh
strace -f -o $HOME/strace.out -p `pidof marketmaker` &
sleep 2
killall strace

46
iguana/exchanges/stats.c

@ -29,6 +29,7 @@
#define STATS_DEST "/var/www/html/DEXstats.json" #define STATS_DEST "/var/www/html/DEXstats.json"
#include "DEXstats.h" #include "DEXstats.h"
char *stats_JSON(void *ctx,char *myipaddr,int32_t mypubsock,cJSON *argjson,char *remoteaddr,uint16_t port); char *stats_JSON(void *ctx,char *myipaddr,int32_t mypubsock,cJSON *argjson,char *remoteaddr,uint16_t port);
void LP_queuecommand(char **retstrp,char *buf,int32_t responsesock,int32_t stats_JSONonly);
extern uint32_t DOCKERFLAG; extern uint32_t DOCKERFLAG;
char *stats_validmethods[] = char *stats_validmethods[] =
@ -223,7 +224,7 @@ int32_t iguana_socket(int32_t bindflag,char *hostname,uint16_t port)
return(-1); return(-1);
} }
} }
if ( listen(sock,4096) != 0 ) if ( listen(sock,512) != 0 )
{ {
printf("listen(%s) port.%d failed: %s sock.%d. errno.%d\n",hostname,port,strerror(errno),sock,errno); printf("listen(%s) port.%d failed: %s sock.%d. errno.%d\n",hostname,port,strerror(errno),sock,errno);
if ( sock >= 0 ) if ( sock >= 0 )
@ -333,7 +334,7 @@ extern void *bitcoin_ctx();
char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *postflagp,char *urlstr,char *remoteaddr,char *filetype,uint16_t port) char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *postflagp,char *urlstr,char *remoteaddr,char *filetype,uint16_t port)
{ {
static void *ctx; static void *ctx;
cJSON *tokens,*argjson,*origargjson,*tmpjson=0,*json = 0; long filesize; char *myipaddr="127.0.0.1",symbol[64],buf[4096],*userpass=0,urlmethod[16],*data,url[8192],furl[8192],*retstr=0,*filestr,*token = 0; int32_t i,j,n,num=0; cJSON *tokens,*argjson,*origargjson,*tmpjson=0,*json = 0; long filesize; char symbol[64],buf[4096],*userpass=0,urlmethod[16],*data,url[8192],furl[8192],*retstr=0,*filestr,*token = 0; int32_t i,j,n,num=0;
if ( ctx == 0 ) if ( ctx == 0 )
ctx = bitcoin_ctx(); ctx = bitcoin_ctx();
for (i=0; i<sizeof(urlmethod)-1&&urlstr[i]!=0&&urlstr[i]!=' '; i++) for (i=0; i<sizeof(urlmethod)-1&&urlstr[i]!=0&&urlstr[i]!=' '; i++)
@ -523,7 +524,12 @@ char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *po
#ifdef FROM_MARKETMAKER #ifdef FROM_MARKETMAKER
if ( strcmp(remoteaddr,"127.0.0.1") == 0 || LP_valid_remotemethod(argjson) > 0 ) if ( strcmp(remoteaddr,"127.0.0.1") == 0 || LP_valid_remotemethod(argjson) > 0 )
{ {
if ( (retstr= stats_JSON(ctx,myipaddr,-1,argjson,remoteaddr,port)) != 0 ) //buf = jprint(argjson,0);
//LP_queuecommand(&retstr,buf,-1,1);
//free(buf);
//while ( retstr == 0 )
// usleep(10000);
if ( (retstr= stats_JSON(ctx,"127.0.0.1",-1,argjson,remoteaddr,port)) != 0 )
{ {
if ( (retitem= cJSON_Parse(retstr)) != 0 ) if ( (retitem= cJSON_Parse(retstr)) != 0 )
jaddi(retarray,retitem); jaddi(retarray,retitem);
@ -531,6 +537,11 @@ char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *po
} }
} else retstr = clonestr("{\"error\":\"invalid remote method\"}"); } else retstr = clonestr("{\"error\":\"invalid remote method\"}");
#else #else
//buf = jprint(argjson,0);
//LP_queuecommand(&retstr,buf,-1,1);
//free(buf);
//while ( retstr == 0 )
// usleep(10000);
if ( (retstr= stats_JSON(ctx,myipaddr,-1,argjson,remoteaddr,port)) != 0 ) if ( (retstr= stats_JSON(ctx,myipaddr,-1,argjson,remoteaddr,port)) != 0 )
{ {
if ( (retitem= cJSON_Parse(retstr)) != 0 ) if ( (retitem= cJSON_Parse(retstr)) != 0 )
@ -545,7 +556,7 @@ char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *po
} }
else else
{ {
cJSON *arg; cJSON *arg; //char *buf;
if ( jstr(argjson,"agent") != 0 && strcmp(jstr(argjson,"agent"),"bitcoinrpc") != 0 && jobj(argjson,"params") != 0 ) if ( jstr(argjson,"agent") != 0 && strcmp(jstr(argjson,"agent"),"bitcoinrpc") != 0 && jobj(argjson,"params") != 0 )
{ {
arg = jobj(argjson,"params"); arg = jobj(argjson,"params");
@ -557,9 +568,20 @@ char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *po
jaddstr(arg,"userpass",userpass); jaddstr(arg,"userpass",userpass);
#ifdef FROM_MARKETMAKER #ifdef FROM_MARKETMAKER
if ( strcmp(remoteaddr,"127.0.0.1") == 0 || LP_valid_remotemethod(arg) > 0 ) if ( strcmp(remoteaddr,"127.0.0.1") == 0 || LP_valid_remotemethod(arg) > 0 )
retstr = stats_JSON(ctx,myipaddr,-1,arg,remoteaddr,port); {
else retstr = clonestr("{\"error\":\"invalid remote method\"}"); //buf = jprint(arg,0);
//LP_queuecommand(&retstr,buf,-1,1);
//free(buf);
//while ( retstr == 0 )
// usleep(10000);
retstr = stats_JSON(ctx,"127.0.0.1",-1,arg,remoteaddr,port);
} else retstr = clonestr("{\"error\":\"invalid remote method\"}");
#else #else
//buf = jprint(arg,0);
//LP_queuecommand(&retstr,buf,-1,1);
//free(buf);
//while ( retstr == 0 )
// usleep(10000);
retstr = stats_JSON(ctx,myipaddr,-1,arg,remoteaddr,port); retstr = stats_JSON(ctx,myipaddr,-1,arg,remoteaddr,port);
#endif #endif
} }
@ -568,6 +590,7 @@ char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *po
free_json(json); free_json(json);
if ( tmpjson != 0 ) if ( tmpjson != 0 )
free(tmpjson); free(tmpjson);
//printf("stats_JSON rpc return.(%s)\n",retstr);
return(retstr); return(retstr);
} }
free_json(argjson); free_json(argjson);
@ -778,7 +801,7 @@ extern int32_t IAMLP,LP_STOP_RECEIVED;
void stats_rpcloop(void *args) void stats_rpcloop(void *args)
{ {
uint16_t port; int32_t retval,sock=-1,bindsock=-1; socklen_t clilen; struct sockaddr_in cli_addr; uint32_t ipbits,localhostbits; struct rpcrequest_info *req,*req2,*rtmp; uint16_t port; int32_t retval,sock=-1,bindsock=-1; socklen_t clilen; struct sockaddr_in cli_addr; uint32_t ipbits,localhostbits; struct rpcrequest_info *req;
if ( (port= *(uint16_t *)args) == 0 ) if ( (port= *(uint16_t *)args) == 0 )
port = 7779; port = 7779;
printf("Start stats_rpcloop.%u\n",port); printf("Start stats_rpcloop.%u\n",port);
@ -835,13 +858,12 @@ void stats_rpcloop(void *args)
req->port = port; req->port = port;
LP_rpc_processreq(req); LP_rpc_processreq(req);
continue; continue;
// this leads to cant open file errors // this might lead to "cant open file errors"
if ( (retval= OS_thread_create(&req->T,NULL,(void *)LP_rpc_processreq,req)) != 0 ) if ( (retval= OS_thread_create(&req->T,NULL,(void *)LP_rpc_processreq,req)) != 0 )
{ {
printf("error launching rpc handler on port %d, retval.%d\n",port,retval); printf("error launching rpc handler on port %d, retval.%d\n",port,retval);
closesocket(sock); LP_rpc_processreq(req);
sock = -1; /*portable_mutex_lock(&LP_gcmutex);
portable_mutex_lock(&LP_gcmutex);
DL_FOREACH_SAFE(LP_garbage_collector,req2,rtmp) DL_FOREACH_SAFE(LP_garbage_collector,req2,rtmp)
{ {
DL_DELETE(LP_garbage_collector,req2); DL_DELETE(LP_garbage_collector,req2);
@ -852,7 +874,7 @@ continue;
{ {
printf("error2 launching rpc handler on port %d, retval.%d\n",port,retval); printf("error2 launching rpc handler on port %d, retval.%d\n",port,retval);
LP_rpc_processreq(req); LP_rpc_processreq(req);
} }*/
} }
} }
printf("i got killed\n"); printf("i got killed\n");

BIN
iguana/pthreadvc2.dll

Binary file not shown.

BIN
pthreadVC2.lib

Binary file not shown.

BIN
pthreadvc2.dll

Binary file not shown.
Loading…
Cancel
Save