diff --git a/crypto777/OS_time.c b/crypto777/OS_time.c index 03489f03e..b5d2efc09 100755 --- a/crypto777/OS_time.c +++ b/crypto777/OS_time.c @@ -357,7 +357,7 @@ struct tai tai_now() #ifndef DISABLE_LEAPS UTC_ADJUST = -36; #endif - printf("TAINOW.%llu %03.3f UTC.%u vs %u [diff %d]\n",(long long)t.x,t.millis,First_utc,tai2utc(t),UTC_ADJUST); + //printf("TAINOW.%llu %03.3f UTC.%u vs %u [diff %d]\n",(long long)t.x,t.millis,First_utc,tai2utc(t),UTC_ADJUST); } return(t); } diff --git a/iguana/exchanges/LP_RTmetrics.c b/iguana/exchanges/LP_RTmetrics.c index f0b813a9f..ad23b851e 100644 --- a/iguana/exchanges/LP_RTmetrics.c +++ b/iguana/exchanges/LP_RTmetrics.c @@ -123,7 +123,7 @@ void LP_RTmetrics_swapsinfo(char *refbase,char *refrel,cJSON *swaps,int32_t nums quoteid = juint(item,"quoteid"); LP_RTmetrics_pendingswap(srcpub); LP_RTmetrics_pendingswap(destpub); - if ( (retstr= basilisk_swapentry(requestid,quoteid)) != 0 ) + if ( 0 && (retstr= basilisk_swapentry(requestid,quoteid)) != 0 ) // no need for this { if ( (swapjson= cJSON_Parse(retstr)) != 0 ) { diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index ade9ae58c..a9b217906 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -503,11 +503,12 @@ stop()\n\ return(jprint(LP_coinsjson(0),1)); else if ( strcmp(method,"wantnotify") == 0 ) { - bits256 pub; + bits256 pub; static uint32_t lastnotify; pub = jbits256(argjson,"pub"); //char str[65]; printf("got wantnotify.(%s) vs %s\n",jprint(argjson,0),bits256_str(str,G.LP_mypub25519)); - if ( bits256_cmp(pub,G.LP_mypub25519) == 0 ) + if ( bits256_cmp(pub,G.LP_mypub25519) == 0 && time(NULL) > lastnotify+30 ) { + lastnotify = (uint32_t)time(NULL); printf("wantnotify for me!\n"); LP_notify_pubkeys(ctx,LP_mypubsock); } @@ -560,44 +561,14 @@ stop()\n\ } retstr = clonestr("{\"result\":\"success\"}"); } - //else if ( strcmp(method,"checktxid") == 0 ) - // retstr = LP_spentcheck(argjson); - //else if ( IAMLP == 0 && LP_isdisabled(base,rel) != 0 ) - // return(clonestr("{\"result\":\"at least one of coins disabled\"}")); - //else if ( IAMLP == 0 && LP_isdisabled(jstr(argjson,"coin"),0) != 0 ) - // retstr = clonestr("{\"result\":\"coin is disabled\"}"); else if ( strcmp(method,"encrypted") == 0 ) retstr = clonestr("{\"result\":\"success\"}"); else // psock requests/response { if ( IAMLP != 0 ) { - /*if ( strcmp(method,"broadcast") == 0 ) - { - bits256 zero; char *cipherstr; int32_t cipherlen; uint8_t cipher[LP_ENCRYPTED_MAXSIZE]; - if ( (reqjson= LP_dereference(argjson,"broadcast")) != 0 ) - { - Broadcaststr = jprint(reqjson,0); - if ( (cipherstr= jstr(reqjson,"cipher")) != 0 ) - { - cipherlen = (int32_t)strlen(cipherstr) >> 1; - if ( cipherlen <= sizeof(cipher) ) - { - decode_hex(cipher,cipherlen,cipherstr); - LP_queuesend(calc_crc32(0,&cipher[2],cipherlen-2),LP_mypubsock,base,rel,cipher,cipherlen); - } else retstr = clonestr("{\"error\":\"cipher too big\"}"); - } - else - { - memset(zero.bytes,0,sizeof(zero)); - //printf("broadcast.(%s)\n",Broadcaststr); - LP_reserved_msg(base!=0?base:jstr(argjson,"coin"),rel,zero,jprint(reqjson,0)); - } - retstr = clonestr("{\"result\":\"success\"}"); - } else retstr = clonestr("{\"error\":\"couldnt dereference sendmessage\"}"); - } - else*/ if ( strcmp(method,"psock") == 0 ) - { + if ( strcmp(method,"psock") == 0 ) + { if ( myipaddr == 0 || myipaddr[0] == 0 || strcmp(myipaddr,"127.0.0.1") == 0 ) { if ( LP_mypeer != 0 ) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 48d38e15a..626c5b456 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -34,6 +34,9 @@ void emscripten_usleep(int32_t x); #endif //#define LP_STRICTPEERS +#define LP_BARTERDEX_VERSION 0 +#define LP_MAGICBITS 10 + #define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL) #define LP_AUTOTRADE_TIMEOUT 10 #define ELECTRUM_TIMEOUT 10 @@ -311,13 +314,13 @@ struct basilisk_swap }; -#define LP_MAXPRICEINFOS 1024 +#define LP_MAXPRICEINFOS 256 struct LP_pubkeyinfo { UT_hash_handle hh; bits256 pubkey; - double matrix[LP_MAXPRICEINFOS][LP_MAXPRICEINFOS]; - uint32_t timestamp,numerrors; + float matrix[LP_MAXPRICEINFOS][LP_MAXPRICEINFOS]; + uint32_t timestamps[LP_MAXPRICEINFOS][LP_MAXPRICEINFOS],timestamp,numerrors; int32_t istrusted; uint8_t rmd160[20],sig[65],pubsecp[33],siglen; }; diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 513b1b93b..1e07c1aac 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -18,7 +18,7 @@ // LP_nativeDEX.c // marketmaker // -// verify portfolio, interest to KMD withdraw, pricebroadcast loop, trade to pubkey +// verify portfolio, interest to KMD withdraw, pricebroadcast loop // dPoW security -> 4: KMD notarized, 5: BTC notarized, after next notary elections // bigendian architectures need to use little endian for sighash calcs @@ -279,19 +279,18 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int if ( (recvlen= nn_recv(sock,&ptr,NN_MSG,0)) > 0 ) { methodstr[0] = 0; - if ( 1 ) + if ( 0 ) { #ifdef FROM_JS printf("%s RECV.(%s)\n",typestr,(char *)ptr); #endif - cJSON *recvjson; //char *mstr,*cstr; + cJSON *recvjson; char *mstr;//,*cstr; if ( (recvjson= cJSON_Parse((char *)ptr)) != 0 ) { - /*if ( (mstr= jstr(recvjson,"method")) != 0 && strcmp(mstr,"uitem") == 0 && - (cstr= jstr(recvjson,"coin")) != 0 && strcmp(cstr,"REVS") == 0 ) + if ( (mstr= jstr(recvjson,"method")) != 0 )//&& strcmp(mstr,"uitem") == 0 && (cstr= jstr(recvjson,"coin")) != 0 && strcmp(cstr,"REVS") == 0 ) { printf("%s RECV.(%s)\n",typestr,(char *)ptr); - }*/ + } safecopy(methodstr,jstr(recvjson,"method"),sizeof(methodstr)); free_json(recvjson); } @@ -300,6 +299,9 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int else printf("%s got recv.%d\n",typestr,recvlen); #endif double millis = OS_milliseconds(); + if ( strlen((char *)ptr)+sizeof(bits256) <= recvlen ) + if ( LP_magic_check(ptr,recvlen,remoteaddr) <= 0 ) + printf("magic check error\n"); if ( (retstr= LP_process_message(ctx,typestr,myipaddr,pubsock,ptr,recvlen,sock)) != 0 ) free(retstr); if ( Broadcaststr != 0 ) @@ -731,33 +733,6 @@ void LP_pubkeysloop(void *ctx) } } -void LP_price_broadcastloop(void *ctx) -{ - struct LP_priceinfo *basepp,*relpp; double price; int32_t baseind,relind; - sleep(30); - while ( 1 ) - { - for (baseind=0; baseindsymbol[0] == 0 ) - continue; - for (relind=0; relindsymbol[0] == 0 ) - continue; - if ( basepp != 0 && relpp != 0 && (price= relpp->myprices[basepp->ind]) > SMALLVAL) - { - //printf("automated price broadcast %s/%s %.8f\n",relpp->symbol,basepp->symbol,price); - LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,relpp->symbol,basepp->symbol,price); - } - } - } - sleep(LP_ORDERBOOK_DURATION * .9); - } -} - void LP_privkeysloop(void *ctx) { sleep(20); @@ -1038,11 +1013,6 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu printf("error launching LP_swapsloop for port.%u\n",myport); exit(-1); } - if ( 0 && OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_price_broadcastloop,(void *)ctx) != 0 ) - { - printf("error launching LP_swapsloop for port.%u\n",myport); - exit(-1); - } int32_t nonz; while ( 1 ) { diff --git a/iguana/exchanges/LP_network.c b/iguana/exchanges/LP_network.c index a27fa7fc3..040efab2a 100644 --- a/iguana/exchanges/LP_network.c +++ b/iguana/exchanges/LP_network.c @@ -107,7 +107,7 @@ char *nanomsg_transportname2(int32_t bindflag,char *str,char *ipaddr,uint16_t po return(str); } -int32_t _LP_send(int32_t sock,void *msg,int32_t sendlen,int32_t freeflag) +/*int32_t _LP_send(int32_t sock,void *msg,int32_t sendlen,int32_t freeflag) { int32_t sentbytes; if ( sock < 0 ) @@ -123,7 +123,7 @@ int32_t _LP_send(int32_t sock,void *msg,int32_t sendlen,int32_t freeflag) if ( freeflag != 0 ) free(msg); return(sentbytes); -} +}*/ int32_t LP_sockcheck(int32_t sock) { @@ -147,17 +147,69 @@ int32_t LP_Qenqueued,LP_Qerrors,LP_Qfound; void _LP_sendqueueadd(uint32_t crc32,int32_t sock,uint8_t *msg,int32_t msglen,int32_t peerind) { struct LP_queue *ptr; - ptr = calloc(1,sizeof(*ptr) + msglen); + ptr = calloc(1,sizeof(*ptr) + msglen + sizeof(bits256)); ptr->crc32 = crc32; ptr->sock = sock; ptr->peerind = peerind; - ptr->msglen = msglen; - memcpy(ptr->msg,msg,msglen); + ptr->msglen = (int32_t)(msglen + sizeof(bits256)); + memcpy(ptr->msg,msg,msglen); // sizeof(bits256) at the end all zeroes DL_APPEND(LP_Q,ptr); LP_Qenqueued++; //printf("Q.%p: peerind.%d msglen.%d\n",ptr,peerind,msglen); } +uint32_t _LP_magic_check(bits256 hash,bits256 magic) +{ + bits256 pubkey,shared; + pubkey = curve25519(magic,curve25519_basepoint9()); + shared = curve25519(hash,pubkey); + return(shared.uints[1] & ((1 << LP_MAGICBITS)-1)); +} + +bits256 LP_calc_magic(uint8_t *msg,int32_t len) +{ + static uint32_t maxn,counter,nsum; static double sum; + bits256 magic,hash; int32_t n = 0; double millis; + vcalc_sha256(0,hash.bytes,msg,len); + millis = OS_milliseconds(); + while ( 1 ) + { + magic = rand256(1); + if ( _LP_magic_check(hash,magic) == LP_BARTERDEX_VERSION ) + break; + n++; + } + sum += (OS_milliseconds() - millis); + nsum += n; + counter++; + if ( n > maxn || (rand() % 100) == 0 ) + { + if ( n > maxn ) + { + printf("LP_calc_magic maxn.%d <- %d\n",maxn,n); + maxn = n; + } + printf("millis %.3f ave %.3f, aveiters %.1f\n",OS_milliseconds() - millis,sum/counter,(double)nsum/counter); + } + return(magic); +} + +int32_t LP_magic_check(uint8_t *msg,int32_t recvlen,char *remoteaddr) +{ + bits256 magic,hash; uint32_t val; + recvlen -= sizeof(bits256); + if ( recvlen > 0 ) + { + vcalc_sha256(0,hash.bytes,msg,recvlen); + memcpy(magic.bytes,&msg[recvlen],sizeof(magic)); + val = _LP_magic_check(hash,magic); + if ( val != LP_BARTERDEX_VERSION ) + printf("magicval = %x from %s\n",val,remoteaddr); + return(val == LP_BARTERDEX_VERSION); + } + return(-1); +} + int32_t LP_crc32find(int32_t *duplicatep,int32_t ind,uint32_t crc32) { static uint32_t crcs[1024]; static unsigned long dup,total; @@ -228,6 +280,9 @@ void queue_loop(void *arg) { if ( LP_sockcheck(ptr->sock) > 0 ) { + bits256 magic; + magic = LP_calc_magic(ptr->msg,(int32_t)(ptr->msglen - sizeof(bits256))); + memcpy(&ptr->msg[ptr->msglen - sizeof(bits256)],&magic,sizeof(magic)); if ( (sentbytes= nn_send(ptr->sock,ptr->msg,ptr->msglen,0)) != ptr->msglen ) printf("%d LP_send sent %d instead of %d\n",n,sentbytes,ptr->msglen); ptr->sock = -1; @@ -282,32 +337,7 @@ void queue_loop(void *arg) void _LP_queuesend(uint32_t crc32,int32_t sock0,int32_t sock1,uint8_t *msg,int32_t msglen,int32_t needack) { int32_t maxind,peerind = 0; //sentbytes, - if ( sock0 >= 0 || sock1 >= 0 ) - { -/* if ( sock0 >= 0 && LP_sockcheck(sock0) > 0 ) - { - if ( (sentbytes= nn_send(sock0,msg,msglen,0)) != msglen ) - printf("_LP_queuesend0 sent %d instead of %d\n",sentbytes,msglen); - else - { -printf("Q sent %u msglen.%d (%s)\n",crc32,msglen,msg); - sock0 = -1; - } - } - if ( sock1 >= 0 && LP_sockcheck(sock1) > 0 ) - { - if ( (sentbytes= nn_send(sock1,msg,msglen,0)) != msglen ) - printf("_LP_queuesend1 sent %d instead of %d\n",sentbytes,msglen); - else - { -printf("Q sent1 %u msglen.%d (%s)\n",crc32,msglen,msg); - sock1 = -1; - } - } - if ( sock0 < 0 && sock1 < 0 ) - return;*/ - } - else + if ( sock0 < 0 && sock1 < 0 ) { if ( (maxind= LP_numpeers()) > 0 ) peerind = (rand() % maxind) + 1; @@ -326,19 +356,10 @@ printf("Q sent1 %u msglen.%d (%s)\n",crc32,msglen,msg); void LP_queuesend(uint32_t crc32,int32_t pubsock,char *base,char *rel,uint8_t *msg,int32_t msglen) { - //struct iguana_info *coin; int32_t flag=0,socks[2]; portable_mutex_lock(&LP_networkmutex); if ( pubsock >= 0 ) - { - //socks[0] = socks[1] = -1; - //if ( rel != 0 && rel[0] != 0 && (coin= LP_coinfind(rel)) != 0 && coin->bussock >= 0 ) - // socks[flag++] = coin->bussock; - //if ( base != 0 && base[0] != 0 && (coin= LP_coinfind(base)) != 0 && coin->bussock >= 0 ) - // socks[flag++] = coin->bussock; - //if ( flag == 0 && pubsock >= 0 ) - _LP_queuesend(crc32,pubsock,-1,msg,msglen,0); - //else _LP_queuesend(socks[0],socks[1],msg,msglen,0); - } else _LP_queuesend(crc32,-1,-1,msg,msglen,1); + _LP_queuesend(crc32,pubsock,-1,msg,msglen,0); + else _LP_queuesend(crc32,-1,-1,msg,msglen,1); portable_mutex_unlock(&LP_networkmutex); } @@ -362,21 +383,7 @@ void LP_broadcast_finish(int32_t pubsock,char *base,char *rel,uint8_t *msg,cJSON // add signature here msg = (void *)jprint(argjson,0); msglen = (int32_t)strlen((char *)msg) + 1; -#ifdef FROM_JS - int32_t sentbytes,sock,peerind,maxind; - if ( (maxind= LP_numpeers()) > 0 ) - peerind = (rand() % maxind) + 1; - else peerind = 1; - sock = LP_peerindsock(&peerind); - if ( sock >= 0 ) - { - if ( (sentbytes= nn_send(sock,msg,msglen,0)) != msglen ) - printf("LP_send sent %d instead of %d\n",sentbytes,msglen); - else printf("sent %d bytes of %d to sock.%d\n",sentbytes,msglen,sock); - } else printf("couldnt get valid sock\n"); -#else LP_queuesend(crc32,-1,base,rel,msg,msglen); -#endif } else LP_queuesend(crc32,pubsock,base,rel,msg,msglen); free(msg); } diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 31558980d..1ed257db0 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -852,8 +852,8 @@ struct LP_utxoinfo *LP_buyutxo(double *ordermatchpricep,int64_t *bestsatoshisp,i free(utxos); if ( *ordermatchpricep == 0. || *bestdestsatoshisp == 0 ) return(0); - int32_t changed; - LP_mypriceset(&changed,autxo->coin,base,1. / *ordermatchpricep); + //int32_t changed; + //LP_mypriceset(&changed,autxo->coin,base,1. / *ordermatchpricep); return(bestutxo); } diff --git a/iguana/exchanges/LP_portfolio.c b/iguana/exchanges/LP_portfolio.c index 16f1d65fe..c67cda50a 100644 --- a/iguana/exchanges/LP_portfolio.c +++ b/iguana/exchanges/LP_portfolio.c @@ -266,6 +266,7 @@ int32_t LP_autoprice(char *base,char *rel,cJSON *argjson) void LP_autopriceset(void *ctx,int32_t dir,struct LP_priceinfo *basepp,struct LP_priceinfo *relpp,double price,char *refbase,char *refrel) { + static uint32_t lasttime; double margin,minprice,newprice,oppomargin,factor,offset; double bid,ask; int32_t changed; margin = basepp->margins[relpp->ind]; oppomargin = relpp->margins[basepp->ind]; @@ -297,9 +298,12 @@ void LP_autopriceset(void *ctx,int32_t dir,struct LP_priceinfo *basepp,struct LP if ( (minprice= basepp->minprices[relpp->ind]) == 0. || price >= minprice ) { LP_mypriceset(&changed,relpp->symbol,basepp->symbol,newprice); - //printf("changed.%d %s/%s <- %.8f\n",changed,basepp->symbol,relpp->symbol,price); - if ( changed != 0 ) + //printf("autoprice changed.%d %s/%s <- %.8f\n",changed,basepp->symbol,relpp->symbol,price); + if ( changed != 0 || time(NULL) > lasttime+LP_ORDERBOOK_DURATION*.777) + { + lasttime = (uint32_t)time(NULL); LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,relpp->symbol,basepp->symbol,newprice); + } } } } @@ -398,20 +402,6 @@ double LP_pricesparse(void *ctx,int32_t trexflag,char *retstr,struct LP_priceinf return(nxtkmd); } -static char *assetids[][3] = -{ - { "12071612744977229797", "UNITY", "10000" }, - { "15344649963748848799", "DEX", "1" }, - { "6883271355794806507", "PANGEA", "10000" }, - { "17911762572811467637", "JUMBLR", "10000" }, - { "17083334802666450484", "BET", "10000" }, - { "13476425053110940554", "CRYPTO", "1000" }, - { "6932037131189568014", "HODL", "1" }, - { "3006420581923704757", "SHARK", "10000" }, - { "17571711292785902558", "BOTS", "1" }, - { "10524562908394749924", "MGW", "1" }, -}; - void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp) { char *retstr; cJSON *retjson,*bid,*ask; uint64_t bidsatoshis,asksatoshis; int32_t i; double nxtkmd,price; struct LP_priceinfo *kmdpp,*fiatpp,*nxtpp,*basepp,*relpp; diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index e556c1281..90159811e 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -333,7 +333,6 @@ void LP_prices_parse(struct LP_peerinfo *peer,cJSON *obj) timestamp = now; if ( timestamp >= pubp->timestamp && (asks= jarray(&n,obj,"asks")) != 0 ) { - pubp->timestamp = timestamp; for (i=0; iind,relid,askprice); pubp->matrix[basepp->ind][relid] = askprice; + pubp->timestamps[basepp->ind][relid] = timestamp; if ( (relpp= LP_priceinfofind(rel)) != 0 ) { dxblend(&basepp->relvals[relpp->ind],askprice,0.9); @@ -488,16 +488,17 @@ int32_t LP_mypriceset(int32_t *changedp,char *base,char *rel,double price) if ( base != 0 && rel != 0 && (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) { - if ( fabs(basepp->myprices[relpp->ind] - price) > SMALLVAL ) + if ( fabs(basepp->myprices[relpp->ind] - price)/price > 0.001 ) *changedp = 1; basepp->myprices[relpp->ind] = price; // ask //printf("LP_mypriceset base.%s rel.%s <- price %.8f\n",base,rel,price); //relpp->myprices[basepp->ind] = (1. / price); // bid if ( (pubp= LP_pubkeyadd(G.LP_mypub25519)) != 0 ) { + pubp->timestamp = (uint32_t)time(NULL); pubp->matrix[basepp->ind][relpp->ind] = price; + pubp->timestamps[basepp->ind][relpp->ind] = pubp->timestamp; //pubp->matrix[relpp->ind][basepp->ind] = (1. / price); - pubp->timestamp = (uint32_t)time(NULL); } return(0); } else return(-1); @@ -699,13 +700,15 @@ struct LP_orderbookentry *LP_orderbookentry(char *address,char *base,char *rel,d void LP_pubkeys_query() { + static uint32_t lasttime; uint8_t zeroes[20]; bits256 zero; cJSON *reqjson; struct LP_pubkeyinfo *pubp=0,*tmp; memset(zero.bytes,0,sizeof(zero)); memset(zeroes,0,sizeof(zeroes)); HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp) { - if ( memcmp(zeroes,pubp->rmd160,sizeof(pubp->rmd160)) == 0 ) + if ( memcmp(zeroes,pubp->rmd160,sizeof(pubp->rmd160)) == 0 && time(NULL) > lasttime+30 ) { + lasttime = (uint32_t)time(NULL); reqjson = cJSON_CreateObject(); jaddstr(reqjson,"method","wantnotify"); jaddbits256(reqjson,"pub",pubp->pubkey); @@ -738,7 +741,7 @@ int32_t LP_orderbook_utxoentries(uint32_t now,int32_t polarity,char *base,char * bitcoin_address(coinaddr,basecoin->taddr,basecoin->pubtype,pubp->rmd160,sizeof(pubp->rmd160)); minsatoshis = maxsatoshis = n = 0; ap = 0; - if ( (price= pubp->matrix[baseid][relid]) > SMALLVAL ) + if ( (price= pubp->matrix[baseid][relid]) > SMALLVAL && pubp->timestamps[baseid][relid] >= oldest ) { balance = 0; if ( (ap= LP_addressfind(basecoin,coinaddr)) != 0 ) @@ -752,7 +755,7 @@ int32_t LP_orderbook_utxoentries(uint32_t now,int32_t polarity,char *base,char * } //printf("%s/%s %s n.%d ap->n.%d %.8f\n",base,rel,coinaddr,n,ap->n,dstr(ap->total)); } - if ( (op= LP_orderbookentry(coinaddr,base,rel,polarity > 0 ? price : 1./price,n,minsatoshis,maxsatoshis,pubp->pubkey,pubp->timestamp,balance)) != 0 ) + if ( (op= LP_orderbookentry(coinaddr,base,rel,polarity > 0 ? price : 1./price,n,minsatoshis,maxsatoshis,pubp->pubkey,pubp->timestamps[baseid][relid],balance)) != 0 ) { *arrayp = realloc(*arrayp,sizeof(*(*arrayp)) * (num+1)); (*arrayp)[num++] = op; @@ -1061,15 +1064,16 @@ void LP_pricefeedupdate(bits256 pubkey,char *base,char *rel,double price) } if ( (pubp= LP_pubkeyadd(pubkey)) != 0 ) { + //if ( (rand() % 100) == 0 ) + printf("PRICEFEED UPDATE.(%-6s/%6s) %12.8f %s %12.8f\n",base,rel,price,bits256_str(str,pubkey),1./price); + pubp->timestamp = (uint32_t)time(NULL); if ( fabs(pubp->matrix[basepp->ind][relpp->ind] - price) > SMALLVAL ) { - if ( (rand() % 5000) == 0 ) - printf("PRICEFEED UPDATE.(%-6s/%6s) %12.8f %s %12.8f\n",base,rel,price,bits256_str(str,pubkey),1./price); pubp->matrix[basepp->ind][relpp->ind] = price; + pubp->timestamps[basepp->ind][relpp->ind] = pubp->timestamp; dxblend(&basepp->relvals[relpp->ind],price,0.9); dxblend(&relpp->relvals[basepp->ind],1. / price,0.9); } - pubp->timestamp = (uint32_t)time(NULL); } else printf("error finding pubkey entry %s, ok if rare\n",bits256_str(str,pubkey)); } //else if ( (rand() % 100) == 0 ) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index d82145479..eb0dab1e2 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -170,17 +170,135 @@ void LP_unspents_mark(char *symbol,cJSON *vins) char *NXTnodes[] = { "62.75.159.113", "91.44.203.238", "82.114.88.225", "78.63.207.76", "188.174.110.224", "91.235.72.49", "213.144.130.91", "209.222.98.250", "216.155.128.10", "178.33.203.157", "162.243.122.251", "69.163.47.173", "193.151.106.129", "78.94.2.74", "192.3.196.10", "173.33.112.87", "104.198.173.28", "35.184.154.126", "174.140.167.239", "23.88.113.131", "198.71.84.173", "178.150.207.53", "23.88.61.53", "192.157.233.106", "192.157.241.212", "23.89.192.88", "23.89.200.27", "192.157.241.139", "23.89.200.63", "23.89.192.98", "163.172.214.102", "176.9.85.5", "80.150.243.88", "80.150.243.92", "80.150.243.98", "109.70.186.198", "146.148.84.237", "104.155.56.82", "104.197.157.140", "37.48.73.249", "146.148.77.226", "84.57.170.200", "107.161.145.131", "80.150.243.97", "80.150.243.93", "80.150.243.100", "80.150.243.95", "80.150.243.91", "80.150.243.99", "80.150.243.96", "93.231.187.177", "212.237.23.85", "35.158.179.254", "46.36.66.41", "185.170.113.79", "163.172.68.112", "78.47.35.210", "77.90.90.75", "94.177.196.134", "212.237.22.215", "94.177.234.11", "167.160.180.199", "54.68.189.9", "94.159.62.14", "195.181.221.89", "185.33.145.94", "195.181.209.245", "195.181.221.38", "195.181.221.162", "185.33.145.12", "185.33.145.176", "178.79.128.235", "94.177.214.120", "94.177.199.41", "94.177.214.200", "94.177.213.201", "212.237.13.162", "195.181.221.236", "195.181.221.185", "185.28.103.187", "185.33.146.244", "217.61.123.71", "195.181.214.45", "195.181.212.99", "195.181.214.46", "195.181.214.215", "195.181.214.68", "217.61.123.118", "195.181.214.79", "217.61.123.14", "217.61.124.100", "195.181.214.111", "85.255.0.176", "81.2.254.116", "217.61.123.184", "195.181.212.231", "94.177.214.110", "195.181.209.164", "104.129.56.238", "85.255.13.64", "167.160.180.206", "217.61.123.226", "167.160.180.208", "93.186.253.127", "212.237.6.208", "94.177.207.190", "217.61.123.119", "85.255.1.245", "217.61.124.157", "37.59.57.141", "167.160.180.58", "104.223.53.14", "217.61.124.69", "195.181.212.103", "85.255.13.141", "104.207.133.204", "71.90.7.107", "107.150.18.108", "23.94.134.161", "80.150.243.13", "80.150.243.11", "185.81.165.52", "80.150.243.8" }; +static char *assetids[][4] = +{ + { "13502152099823770958", "SUPERNETx2", "10000", "10000" }, + { "12071612744977229797", "SUPERNET", "10000", "10000" }, + { "12071612744977229797", "UNITY", "10000", "10000" }, + { "15344649963748848799", "DEX", "1", "100000000" }, + { "6883271355794806507", "PANGEA", "10000", "10000" }, + { "17911762572811467637", "JUMBLR", "10000", "10000" }, + { "17083334802666450484", "BET", "10000", "10000" }, + { "13476425053110940554", "CRYPTO", "1000", "100000" }, + { "6932037131189568014", "HODL", "1", "100000000" }, + { "3006420581923704757", "SHARK", "10000", "10000" }, + { "17571711292785902558", "BOTS", "1", "100000000" }, + { "10524562908394749924", "MGW", "1", "100000000" }, +}; + +uint64_t LP_assetid_mult(int32_t *assetindp,char *name,uint64_t assetid) +{ + int32_t i; uint64_t mult = 0; + name[0] = 0; + *assetindp = -1; + for (i=0; i= 0 ) + totals[ind] += qty * mult; + if ( msgstr != 0 && assetname[0] != 0 && qty != 0 ) + printf("%-4d: (%35s) <- %13.5f %10s tx.%llu\n",i,msgstr!=0?msgstr:jprint(item,0),dstr(qty * mult),assetname,(long long)txnum); + if ( msgjson != 0 ) + free_json(msgjson); + } + if ( txnum == calc_nxt64bits("7256847492742571143") ) + break; + } + } + free_json(retjson); + } + free(retstr); + } + printf("\nTotal redeemed\n"); + for (i=0; irmd160,20) != 0 ) + //if ( memcmp(rmd160,pubp->rmd160,20) != 0 ) { if ( (pubsecpstr= jstr(item,"pubsecp")) != 0 && is_hexstr(pubsecpstr,0) == 66 ) { @@ -523,15 +523,18 @@ int32_t LP_pubkey_sigcheck(struct LP_pubkeyinfo *pubp,cJSON *item) decode_hex(sig,siglen,sigstr); if ( _LP_pubkey_sigcheck(sig,siglen,juint(item,"timestamp"),pubp->pubkey,rmd160,pubsecp) == 0 ) { - for (i=0; i<20; i++) - printf("%02x",pubp->rmd160[i]); - memcpy(pubp->rmd160,rmd160,sizeof(pubp->rmd160)); - memcpy(pubp->pubsecp,pubsecp,sizeof(pubp->pubsecp)); - memcpy(pubp->sig,sig,sizeof(pubp->sig)); - pubp->siglen = siglen; - char str[65]; printf(" -> rmd160.(%s) for %s (%s) sig.%s\n",hexstr,bits256_str(str,pubp->pubkey),pubsecpstr,sigstr); + if ( memcmp(rmd160,pubp->rmd160,20) != 0 ) + { + for (i=0; i<20; i++) + printf("%02x",pubp->rmd160[i]); + memcpy(pubp->rmd160,rmd160,sizeof(pubp->rmd160)); + memcpy(pubp->pubsecp,pubsecp,sizeof(pubp->pubsecp)); + memcpy(pubp->sig,sig,sizeof(pubp->sig)); + pubp->siglen = siglen; + char str[65]; printf(" -> rmd160.(%s) for %s (%s) sig.%s\n",hexstr,bits256_str(str,pubp->pubkey),pubsecpstr,sigstr); + pubp->timestamp = (uint32_t)time(NULL); + } retval = 0; - pubp->timestamp = (uint32_t)time(NULL); } else pubp->numerrors++; } } @@ -545,7 +548,7 @@ int32_t LP_pubkey_sigcheck(struct LP_pubkeyinfo *pubp,cJSON *item) printf(" for %s\n",pubsecpstr); } } - } else pubp->timestamp = (uint32_t)time(NULL); + }// else pubp->timestamp = (uint32_t)time(NULL); } } return(retval); @@ -584,7 +587,7 @@ void LP_smartutxos_push(struct iguana_info *coin) struct LP_peerinfo *peer,*tmp; uint64_t value; bits256 zero,txid; int32_t i,vout,height,n; char *retstr; cJSON *array,*item,*req; if ( coin->smartaddr[0] == 0 ) return; - LP_notify_pubkeys(coin->ctx,LP_mypubsock); + //LP_notify_pubkeys(coin->ctx,LP_mypubsock); if ( (array= LP_address_utxos(coin,coin->smartaddr,1)) != 0 ) { memset(zero.bytes,0,sizeof(zero)); diff --git a/iguana/exchanges/LP_statemachine.c b/iguana/exchanges/LP_statemachine.c index c3cd25927..c14c557ee 100644 --- a/iguana/exchanges/LP_statemachine.c +++ b/iguana/exchanges/LP_statemachine.c @@ -2219,7 +2219,143 @@ if ( 0 && (retstr= issue_LP_listunspent(peer->ipaddr,peer->port,coin->symbol,"") LP_utxo_clientpublish(utxo); } }*/ +void LP_price_broadcastloop(void *ctx) +{ + struct LP_priceinfo *basepp,*relpp; double price; int32_t baseind,relind; + sleep(30); + while ( 1 ) + { + for (baseind=0; baseindsymbol[0] == 0 ) + continue; + for (relind=0; relindsymbol[0] == 0 ) + continue; + if ( basepp != 0 && relpp != 0 && (price= relpp->myprices[basepp->ind]) > SMALLVAL) + { + //printf("automated price broadcast %s/%s %.8f\n",relpp->symbol,basepp->symbol,price); + LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,relpp->symbol,basepp->symbol,price); + } + } + } + sleep(LP_ORDERBOOK_DURATION * .9); + } +} +//else if ( strcmp(method,"checktxid") == 0 ) +// retstr = LP_spentcheck(argjson); +//else if ( IAMLP == 0 && LP_isdisabled(base,rel) != 0 ) +// return(clonestr("{\"result\":\"at least one of coins disabled\"}")); +//else if ( IAMLP == 0 && LP_isdisabled(jstr(argjson,"coin"),0) != 0 ) +// retstr = clonestr("{\"result\":\"coin is disabled\"}"); +/*if ( strcmp(method,"broadcast") == 0 ) + { + bits256 zero; char *cipherstr; int32_t cipherlen; uint8_t cipher[LP_ENCRYPTED_MAXSIZE]; + if ( (reqjson= LP_dereference(argjson,"broadcast")) != 0 ) + { + Broadcaststr = jprint(reqjson,0); + if ( (cipherstr= jstr(reqjson,"cipher")) != 0 ) + { + cipherlen = (int32_t)strlen(cipherstr) >> 1; + if ( cipherlen <= sizeof(cipher) ) + { + decode_hex(cipher,cipherlen,cipherstr); + LP_queuesend(calc_crc32(0,&cipher[2],cipherlen-2),LP_mypubsock,base,rel,cipher,cipherlen); + } else retstr = clonestr("{\"error\":\"cipher too big\"}"); + } + else + { + memset(zero.bytes,0,sizeof(zero)); + //printf("broadcast.(%s)\n",Broadcaststr); + LP_reserved_msg(base!=0?base:jstr(argjson,"coin"),rel,zero,jprint(reqjson,0)); + } + retstr = clonestr("{\"result\":\"success\"}"); + } else retstr = clonestr("{\"error\":\"couldnt dereference sendmessage\"}"); + } + else*/ #ifdef FROM_JS +int32_t sentbytes,sock,peerind,maxind; +if ( (maxind= LP_numpeers()) > 0 ) +peerind = (rand() % maxind) + 1; +else peerind = 1; +sock = LP_peerindsock(&peerind); +if ( sock >= 0 ) +{ + if ( (sentbytes= nn_send(sock,msg,msglen,0)) != msglen ) + printf("LP_send sent %d instead of %d\n",sentbytes,msglen); + else printf("sent %d bytes of %d to sock.%d\n",sentbytes,msglen,sock); + } else printf("couldnt get valid sock\n"); +#else + +void _LP_queuesend(uint32_t crc32,int32_t sock0,int32_t sock1,uint8_t *msg,int32_t msglen,int32_t needack) +{ + int32_t maxind,peerind = 0; //sentbytes, + if ( sock0 >= 0 || sock1 >= 0 ) + { + /* if ( sock0 >= 0 && LP_sockcheck(sock0) > 0 ) + { + if ( (sentbytes= nn_send(sock0,msg,msglen,0)) != msglen ) + printf("_LP_queuesend0 sent %d instead of %d\n",sentbytes,msglen); + else + { + printf("Q sent %u msglen.%d (%s)\n",crc32,msglen,msg); + sock0 = -1; + } + } + if ( sock1 >= 0 && LP_sockcheck(sock1) > 0 ) + { + if ( (sentbytes= nn_send(sock1,msg,msglen,0)) != msglen ) + printf("_LP_queuesend1 sent %d instead of %d\n",sentbytes,msglen); + else + { + printf("Q sent1 %u msglen.%d (%s)\n",crc32,msglen,msg); + sock1 = -1; + } + } + if ( sock0 < 0 && sock1 < 0 ) + return;*/ + } + else + { + if ( (maxind= LP_numpeers()) > 0 ) + peerind = (rand() % maxind) + 1; + else peerind = 1; + sock0 = LP_peerindsock(&peerind); + if ( (maxind= LP_numpeers()) > 0 ) + peerind = (rand() % maxind) + 1; + else peerind = 1; + sock1 = LP_peerindsock(&peerind); + } + if ( sock0 >= 0 ) + _LP_sendqueueadd(crc32,sock0,msg,msglen,needack * peerind); + if ( sock1 >= 0 ) + _LP_sendqueueadd(crc32,sock1,msg,msglen,needack); +} +if ( 0 && OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_price_broadcastloop,(void *)ctx) != 0 ) +{ + printf("error launching LP_swapsloop for port.%u\n",myport); + exit(-1); +} +void LP_queuesend(uint32_t crc32,int32_t pubsock,char *base,char *rel,uint8_t *msg,int32_t msglen) +{ + //struct iguana_info *coin; int32_t flag=0,socks[2]; + portable_mutex_lock(&LP_networkmutex); + if ( pubsock >= 0 ) + { + //socks[0] = socks[1] = -1; + //if ( rel != 0 && rel[0] != 0 && (coin= LP_coinfind(rel)) != 0 && coin->bussock >= 0 ) + // socks[flag++] = coin->bussock; + //if ( base != 0 && base[0] != 0 && (coin= LP_coinfind(base)) != 0 && coin->bussock >= 0 ) + // socks[flag++] = coin->bussock; + //if ( flag == 0 && pubsock >= 0 ) + _LP_queuesend(crc32,pubsock,-1,msg,msglen,0); + //else _LP_queuesend(socks[0],socks[1],msg,msglen,0); + } else _LP_queuesend(crc32,-1,-1,msg,msglen,1); + portable_mutex_unlock(&LP_networkmutex); +} #ifdef oldway struct LP_utxoinfo *LP_bestutxo(double *ordermatchpricep,int64_t *bestsatoshisp,int64_t *bestdestsatoshisp,struct LP_utxoinfo *autxo,char *base,double maxprice,int32_t duration,uint64_t txfee,uint64_t desttxfee,uint64_t maxdestsatoshis) { @@ -2349,6 +2485,16 @@ struct LP_utxoinfo *LP_bestutxo(double *ordermatchpricep,int64_t *bestsatoshisp, LP_mypriceset(&changed,autxo->coin,base,1. / *ordermatchpricep); return(bestutxo); } +if ( (0) ) +{ + ep = LP_electrum_info(&already,"BTC","88.198.241.196",50001,IGUANA_MAXPACKETSIZE * 10); + if ( ep != 0 && OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_dedicatedloop,(void *)ep) != 0 ) + { + printf("error launching LP_dedicatedloop (%s:%u)\n",ep->ipaddr,ep->port); + exit(-1); + } else printf("launched.(%s:%u)\n",ep->ipaddr,ep->port); + electrum_test(); + } /*static int _LP_metric_eval(const void *a,const void *b) { diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 19307baf3..7477b00ab 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -875,20 +875,16 @@ void LP_main(void *ptr) int main(int argc, const char * argv[]) { - char dirname[512],*base,*rel,*name,*exchange,*apikey,*apisecret,*blocktrail,*retstr,*baseaddr,*reladdr,*passphrase; struct electrum_info *ep; + char dirname[512],*base,*rel,*name,*exchange,*apikey,*apisecret,*blocktrail,*retstr,*baseaddr,*reladdr,*passphrase; double profitmargin,maxexposure,incrratio,start_rel,start_base,minask,maxbid,incr; - cJSON *retjson,*loginjson; int32_t i,already; - OS_init(); - if ( (0) ) + cJSON *retjson,*loginjson; int32_t i; + if ( argc == 1 ) { - ep = LP_electrum_info(&already,"BTC","88.198.241.196",50001,IGUANA_MAXPACKETSIZE * 10); - if ( ep != 0 && OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_dedicatedloop,(void *)ep) != 0 ) - { - printf("error launching LP_dedicatedloop (%s:%u)\n",ep->ipaddr,ep->port); - exit(-1); - } else printf("launched.(%s:%u)\n",ep->ipaddr,ep->port); - electrum_test(); + LP_NXT_redeems(); + sleep(3); + return(0); } + OS_init(); sprintf(dirname,"%s",GLOBAL_DBDIR), OS_ensure_directory(dirname); sprintf(dirname,"%s/SWAPS",GLOBAL_DBDIR), OS_ensure_directory(dirname); sprintf(dirname,"%s/PRICES",GLOBAL_DBDIR), OS_ensure_directory(dirname); diff --git a/iguana/exchanges/stats.c b/iguana/exchanges/stats.c index 3a76f3e48..2ff62895d 100644 --- a/iguana/exchanges/stats.c +++ b/iguana/exchanges/stats.c @@ -30,6 +30,24 @@ #include "DEXstats.h" char *stats_JSON(void *ctx,char *myipaddr,int32_t mypubsock,cJSON *argjson,char *remoteaddr,uint16_t port); +char *stats_validmethods[] = +{ + "getprices", "listunspent", "notify", "getpeers", "uitem", // from issue_ + "orderbook", "help", "getcoins", "pricearray", "balance" +}; + +int32_t LP_valid_remotemethod(cJSON *argjson) +{ + char *method; int32_t i; + if ( (method= jstr(argjson,"method")) != 0 ) + { + for (i=0; i 0 ) + { + if ( (retstr= stats_JSON(ctx,myipaddr,-1,argjson,remoteaddr,port)) != 0 ) + { + if ( (retitem= cJSON_Parse(retstr)) != 0 ) + jaddi(retarray,retitem); + free(retstr); + } + } else retstr = clonestr("{\"error\":\"invalid remote method\"}"); +#else if ( (retstr= stats_JSON(ctx,myipaddr,-1,argjson,remoteaddr,port)) != 0 ) { if ( (retitem= cJSON_Parse(retstr)) != 0 ) jaddi(retarray,retitem); free(retstr); } +#endif //printf("(%s) {%s} -> (%s) postflag.%d (%s)\n",urlstr,jprint(argjson,0),cJSON_Print(json),*postflagp,retstr); } free_json(origargjson); @@ -516,7 +546,13 @@ char *stats_rpcparse(char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *po //printf("ARGJSON.(%s)\n",jprint(arg,0)); if ( userpass != 0 && jstr(arg,"userpass") == 0 ) jaddstr(arg,"userpass",userpass); +#ifdef FROM_MARKETMAKER + if ( strcmp(remoteaddr,"127.0.0.1") == 0 || LP_valid_remotemethod(arg) > 0 ) + retstr = stats_JSON(ctx,myipaddr,-1,arg,remoteaddr,port); + else retstr = clonestr("{\"error\":\"invalid remote method\"}"); +#else retstr = stats_JSON(ctx,myipaddr,-1,arg,remoteaddr,port); +#endif } free_json(argjson); free_json(json);