From d5821b2cf055360827a7d3231d33feea846b842b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 28 Oct 2017 13:21:05 +0300 Subject: [PATCH] Electrum maxerrors->reconnect --- iguana/exchanges/LP_include.h | 3 ++- iguana/exchanges/LP_nativeDEX.c | 2 ++ iguana/exchanges/LP_rpc.c | 4 +-- iguana/exchanges/LP_socket.c | 41 +++++++++++++++--------------- iguana/exchanges/LP_statemachine.c | 18 +++++++++++++ 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 3a380ea78..3b5a9560a 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -36,7 +36,8 @@ void emscripten_usleep(int32_t x); #define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL) #define LP_AUTOTRADE_TIMEOUT 10 -#define ELECTRUM_TIMEOUT 15 +#define ELECTRUM_TIMEOUT 10 +#define LP_ELECTRUM_MAXERRORS 3 #define LP_MEMPOOL_TIMEINCR 10 #define LP_COMMAND_SENDSOCK NN_PUSH diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index b6ad97819..f611e35d6 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -18,6 +18,8 @@ // LP_nativeDEX.c // marketmaker // +//MERKLE DIDNT VERIFY.ZEC 96d8484efb1f96e2a692f572d2b3bbc49a59bb586ec84ad952483186b69cb29b ht.209417 ({"error":"timeout"}) +//MERKLE DIDNT VERIFY.ZEC 2520e53f9e0366f711bd924fcfc6fa5c3de7f095e8d7459a68daba2d2124b262 ht.209418 ({"error":"timeout"}) // pricearray? RT metrics // select oldest utxo first, handles <-> pubkeys, reputations, bonds etc. // diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 681511202..d82145479 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -108,7 +108,7 @@ char *LP_apicall(struct iguana_info *coin,char *method,char *params) return(0); if ( coin->electrum != 0 ) { - if ( (retjson= electrum_submit(coin->symbol,coin->electrum,&retjson,method,params,LP_HTTP_TIMEOUT)) != 0 ) + if ( (retjson= electrum_submit(coin->symbol,coin->electrum,&retjson,method,params,ELECTRUM_TIMEOUT)) != 0 ) { retstr = jprint(retjson,0); //printf("got.%p (%s)\n",retjson,retstr); @@ -142,7 +142,7 @@ cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params) } else { - if ( (retjson= electrum_submit(coin->symbol,coin->electrum,&retjson,method,params,LP_HTTP_TIMEOUT)) != 0 ) + if ( (retjson= electrum_submit(coin->symbol,coin->electrum,&retjson,method,params,ELECTRUM_TIMEOUT)) != 0 ) { if ( jobj(retjson,"error") != 0 ) { diff --git a/iguana/exchanges/LP_socket.c b/iguana/exchanges/LP_socket.c index 3bb25b924..b21633e84 100644 --- a/iguana/exchanges/LP_socket.c +++ b/iguana/exchanges/LP_socket.c @@ -243,7 +243,7 @@ struct electrum_info queue_t sendQ,pendingQ; portable_mutex_t mutex,txmutex; struct electrum_info *prev; - int32_t bufsize,sock,*heightp; + int32_t bufsize,sock,*heightp,numerrors; struct iguana_info *coin; uint32_t stratumid,lasttime,pending,*heighttimep; char ipaddr[64],symbol[16]; @@ -368,7 +368,7 @@ cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,ch ep = electrum_server(symbol,0); while ( ep != 0 ) { - if ( ep != 0 && ep->sock >= 0 && retjsonp != 0 ) + if ( ep != 0 && ep->numerrors < LP_ELECTRUM_MAXERRORS && ep->sock >= 0 && retjsonp != 0 ) { *retjsonp = 0; sprintf(stratumreq,"{ \"jsonrpc\":\"2.0\", \"id\": %u, \"method\":\"%s\", \"params\": %s }\n",ep->stratumid,method,params); @@ -382,8 +382,23 @@ cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,ch queue_enqueue("sendQ",&ep->sendQ,&sitem->DL); expiration = (uint32_t)time(NULL) + timeout + 1; while ( *retjsonp == 0 && time(NULL) <= expiration ) - usleep(10000); + usleep(5000); portable_mutex_unlock(&ep->mutex); + if ( *retjsonp == 0 || jobj(*retjsonp,"error") != 0 ) + { + if ( ++ep->numerrors >= LP_ELECTRUM_MAXERRORS ) + { + closesocket(ep->sock), ep->sock = -1; + if ( (ep->sock= LP_socket(0,ep->ipaddr,ep->port)) < 0 ) + printf("error RE-connecting to %s:%u\n",ep->ipaddr,ep->port); + else + { + printf("ep.%p %s numerrors.%d too big -> new %s:%u sock.%d\n",ep,ep->symbol,ep->numerrors,ep->ipaddr,ep->port,ep->sock); + ep->numerrors = 0; + } + } + } else if ( ep->numerrors > 0 ) + ep->numerrors++; if ( ep->prev == 0 ) { if ( *retjsonp == 0 ) @@ -395,6 +410,8 @@ cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,ch } } else printf("couldnt find electrum server for (%s %s) or no retjsonp.%p\n",method,params,retjsonp); ep = ep->prev; + if ( ep != 0 ) + printf("using prev ep.%s\n",ep->symbol); } return(0); } @@ -898,24 +915,6 @@ void LP_dedicatedloop(void *arg) if ( sitem->expiration != 0 ) sitem->expiration += (uint32_t)time(NULL); else sitem->expiration = (uint32_t)time(NULL) + ELECTRUM_TIMEOUT; - /*portable_mutex_lock(&ep->pendingQ.mutex); - if ( ep->pendingQ.list != 0 ) - { - printf("list %p\n",ep->pendingQ.list); - DL_FOREACH_SAFE(ep->pendingQ.list,item,tmp) - { - printf("item.%p\n",item); - if ( item->type == 0xffffffff ) - { - printf("%p purge %s",item,((struct stritem *)item)->str); - DL_DELETE(ep->pendingQ.list,item); - free(item); - } - } - } - DL_APPEND(ep->pendingQ.list,&sitem->DL); - portable_mutex_unlock(&ep->pendingQ.mutex);*/ - //printf("%p SENT.(%s) to %s:%u\n",sitem,sitem->str,ep->ipaddr,ep->port); queue_enqueue("pendingQ",&ep->pendingQ,&sitem->DL); flag++; } diff --git a/iguana/exchanges/LP_statemachine.c b/iguana/exchanges/LP_statemachine.c index d369f5f24..bc05ecb71 100644 --- a/iguana/exchanges/LP_statemachine.c +++ b/iguana/exchanges/LP_statemachine.c @@ -2349,6 +2349,24 @@ struct LP_utxoinfo *LP_bestutxo(double *ordermatchpricep,int64_t *bestsatoshisp, LP_mypriceset(&changed,autxo->coin,base,1. / *ordermatchpricep); return(bestutxo); } +/*portable_mutex_lock(&ep->pendingQ.mutex); + if ( ep->pendingQ.list != 0 ) + { + printf("list %p\n",ep->pendingQ.list); + DL_FOREACH_SAFE(ep->pendingQ.list,item,tmp) + { + printf("item.%p\n",item); + if ( item->type == 0xffffffff ) + { + printf("%p purge %s",item,((struct stritem *)item)->str); + DL_DELETE(ep->pendingQ.list,item); + free(item); + } + } + } + DL_APPEND(ep->pendingQ.list,&sitem->DL); + portable_mutex_unlock(&ep->pendingQ.mutex);*/ +//printf("%p SENT.(%s) to %s:%u\n",sitem,sitem->str,ep->ipaddr,ep->port); char *LP_ordermatch(char *base,int64_t txfee,double maxprice,double maxvolume,char *rel,bits256 txid,int32_t vout,bits256 feetxid,int32_t feevout,int64_t desttxfee,int32_t duration) {