Browse Source

Test

etomic
jl777 8 years ago
parent
commit
52f2659276
  1. 2
      iguana/exchanges/LP_prices.c
  2. 20
      iguana/exchanges/LP_rpc.c
  3. 90
      iguana/exchanges/LP_socket.c

2
iguana/exchanges/LP_prices.c

@ -109,7 +109,7 @@ void LP_address_monitor(struct LP_pubkeyinfo *pubp)
portable_mutex_unlock(&coin->txmutex); portable_mutex_unlock(&coin->txmutex);
if ( coin->electrum != 0 ) if ( coin->electrum != 0 )
{ {
if ( (retjson= electrum_address_subscribe(coin->symbol,coin->electrum,0,coinaddr)) != 0 ) if ( (retjson= electrum_address_subscribe(coin->symbol,coin->electrum,&retjson,coinaddr)) != 0 )
{ {
printf("%s MONITOR.(%s)\n",coin->symbol,coinaddr); printf("%s MONITOR.(%s)\n",coin->symbol,coinaddr);
free_json(retjson); free_json(retjson);

20
iguana/exchanges/LP_rpc.c

@ -108,7 +108,7 @@ char *LP_apicall(struct iguana_info *coin,char *method,char *params)
cJSON *retjson,*resultjson; cJSON *retjson,*resultjson;
if ( coin->electrum != 0 ) if ( coin->electrum != 0 )
{ {
if ( (retjson= electrum_submit(coin->symbol,coin->electrum,0,method,params,LP_HTTP_TIMEOUT)) != 0 ) if ( (retjson= electrum_submit(coin->symbol,coin->electrum,&retjson,method,params,LP_HTTP_TIMEOUT)) != 0 )
{ {
if ( (resultjson= jobj(retjson,"result")) != 0 ) if ( (resultjson= jobj(retjson,"result")) != 0 )
{ {
@ -142,13 +142,15 @@ cJSON *bitcoin_json(struct iguana_info *coin,char *method,char *params)
} }
else else
{ {
retjson = electrum_submit(coin->symbol,coin->electrum,0,method,params,LP_HTTP_TIMEOUT); if ( (retjson= electrum_submit(coin->symbol,coin->electrum,&retjson,method,params,LP_HTTP_TIMEOUT)) != 0 )
//printf("electrum %s.%s -> (%s)\n",method,params,jprint(retjson,0));
if ( (resultjson= jobj(retjson,"result")) != 0 )
{ {
resultjson = jduplicate(resultjson); //printf("electrum %s.%s -> (%s)\n",method,params,jprint(retjson,0));
free_json(retjson); if ( (resultjson= jobj(retjson,"result")) != 0 )
retjson = resultjson; {
resultjson = jduplicate(resultjson);
free_json(retjson);
retjson = resultjson;
}
} }
} }
} else retjson = cJSON_Parse("{\"result\":\"disabled\"}"); } else retjson = cJSON_Parse("{\"result\":\"disabled\"}");
@ -295,7 +297,7 @@ cJSON *LP_gettxout(char *symbol,bits256 txid,int32_t vout)
decode_hex(serialized,len,hexstr+1); decode_hex(serialized,len,hexstr+1);
LP_swap_coinaddr(coin,coinaddr,&value,serialized,len,vout); LP_swap_coinaddr(coin,coinaddr,&value,serialized,len,vout);
//printf("HEX.(%s) len.%d %s %.8f\n",hexstr+1,len,coinaddr,dstr(value)); //printf("HEX.(%s) len.%d %s %.8f\n",hexstr+1,len,coinaddr,dstr(value));
if ( (listjson= electrum_address_listunspent(coin->symbol,0,0,coinaddr)) != 0 ) if ( (listjson= electrum_address_listunspent(coin->symbol,0,&listjson,coinaddr)) != 0 )
{ {
if ( (array= jarray(&n,listjson,"result")) != 0 ) if ( (array= jarray(&n,listjson,"result")) != 0 )
{ {
@ -493,7 +495,7 @@ int32_t LP_importaddress(char *symbol,char *address)
return(-2); return(-2);
if ( coin->electrum != 0 ) if ( coin->electrum != 0 )
{ {
if ( (retjson= electrum_address_subscribe(symbol,0,0,address)) != 0 ) if ( (retjson= electrum_address_subscribe(symbol,0,&retjson,address)) != 0 )
{ {
printf("importaddress.(%s) -> %s\n",address,jprint(retjson,0)); printf("importaddress.(%s) -> %s\n",address,jprint(retjson,0));
free_json(retjson); free_json(retjson);

90
iguana/exchanges/LP_socket.c

@ -26,7 +26,7 @@
#include <WinSock2.h> #include <WinSock2.h>
#endif #endif
#define ELECTRUM_TIMEOUT 5 #define ELECTRUM_TIMEOUT 3
int32_t LP_socket(int32_t bindflag,char *hostname,uint16_t port) int32_t LP_socket(int32_t bindflag,char *hostname,uint16_t port)
{ {
@ -243,7 +243,6 @@ int32_t LP_socketrecv(int32_t sock,uint8_t *recvbuf,int32_t maxlen)
struct electrum_info struct electrum_info
{ {
queue_t sendQ,pendingQ; queue_t sendQ,pendingQ;
cJSON *retjson;
int32_t bufsize,sock,*heightp; int32_t bufsize,sock,*heightp;
uint32_t stratumid,lasttime,pending,*heighttimep; uint32_t stratumid,lasttime,pending,*heighttimep;
char ipaddr[64],symbol[16]; char ipaddr[64],symbol[16];
@ -254,7 +253,7 @@ int32_t Num_electrums;
// purge timedout // purge timedout
/* /*
if ( (retjson= electrum_address_listunspent(symbol,ep,0,addr)) != 0 ) if ( (retjson= electrum_address_listunspent(symbol,ep,&retjson,addr)) != 0 )
you can call it like the above, where symbol is the coin, ep is the electrum server info pointer, the 0 is a callback ptr where 0 means to block till it is done you can call it like the above, where symbol is the coin, ep is the electrum server info pointer, the 0 is a callback ptr where 0 means to block till it is done
all the API calls have the same three args all the API calls have the same three args
if the callback ptr is &retjson, then on completion it will put the cJSON *ptr into it, so to spawn a bunch of calls you need to call with symbol,ep,&retjsons[i],... if the callback ptr is &retjson, then on completion it will put the cJSON *ptr into it, so to spawn a bunch of calls you need to call with symbol,ep,&retjsons[i],...
@ -304,56 +303,54 @@ struct electrum_info *electrum_server(char *symbol,struct electrum_info *ep)
return(ep); return(ep);
} }
// overlapped execution not debugged
cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,char *params,int32_t timeout) cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,char *params,int32_t timeout)
{ {
// queue id and string and callback // queue id and string and callback
char stratumreq[16384]; uint32_t expiration; struct stritem *sitem; char stratumreq[16384]; uint32_t expiration; struct stritem *sitem;
if ( ep == 0 ) if ( ep == 0 )
ep = electrum_server(symbol,0); ep = electrum_server(symbol,0);
if ( ep != 0 ) if ( ep != 0 && retjsonp != 0 )
{ {
if ( strcmp(method,"getrawmempool") == 0 ) if ( strcmp(method,"getrawmempool") == 0 )
{ {
ep->retjson = cJSON_Parse("{\"error\":\"unsupported method\"}"); *retjsonp = cJSON_Parse("{\"error\":\"unsupported method\"}");
if ( retjsonp != 0 ) return(*retjsonp);
*retjsonp = ep->retjson;
return(ep->retjson);
} }
sprintf(stratumreq,"{ \"jsonrpc\":\"2.0\", \"id\": %u, \"method\":\"%s\", \"params\": %s }\n",ep->stratumid,method,params); sprintf(stratumreq,"{ \"jsonrpc\":\"2.0\", \"id\": %u, \"method\":\"%s\", \"params\": %s }\n",ep->stratumid,method,params);
ep->buf[0] = 0; ep->buf[0] = 0;
sitem = (struct stritem *)queueitem(stratumreq); sitem = (struct stritem *)queueitem(stratumreq);
sitem->expiration = timeout; sitem->expiration = timeout;
sitem->DL.type = ep->stratumid++; sitem->DL.type = ep->stratumid++;
if ( retjsonp != 0 ) sitem->retptrp = (void **)retjsonp;
sitem->retptrp = (void **)retjsonp;
else sitem->retptrp = (void **)&ep->retjson;
queue_enqueue("sendQ",&ep->sendQ,&sitem->DL); queue_enqueue("sendQ",&ep->sendQ,&sitem->DL);
if ( sitem->retptrp == (void **)&ep->retjson ) expiration = (uint32_t)time(NULL) + timeout + 1;
while ( *retjsonp == 0 && time(NULL) <= expiration )
usleep(10000);
if ( *retjsonp == 0 )
{ {
expiration = (uint32_t)time(NULL) + timeout + 1; printf("unexpected timeout with null retjson: %s %s\n",method,params);
while ( ep->retjson == 0 && time(NULL) <= expiration ) *retjsonp = cJSON_Parse("{\"error\":\"timeout\"}");
usleep(10000);
if ( ep->retjson == 0 )
{
printf("unexpected timeout with null retjson: %s %s\n",method,params);
ep->retjson = cJSON_Parse("{\"error\":\"timeout\"}");
}
} }
} else printf("couldnt find electrum server for (%s %s)\n",method,params); return(*retjsonp);
return(ep->retjson); } else printf("couldnt find electrum server for (%s %s) or no retjsonp.%p\n",method,params,retjsonp);
return(0);
} }
cJSON *electrum_noargs(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,int32_t timeout) cJSON *electrum_noargs(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,int32_t timeout)
{ {
cJSON *retjson;
if ( retjsonp == 0 )
retjsonp = &retjson;
return(electrum_submit(symbol,ep,retjsonp,method,"[]",timeout)); return(electrum_submit(symbol,ep,retjsonp,method,"[]",timeout));
} }
cJSON *electrum_strarg(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,char *arg,int32_t timeout) cJSON *electrum_strarg(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,char *arg,int32_t timeout)
{ {
char params[16384]; char params[16384]; cJSON *retjson;
if ( strlen(arg) < sizeof(params) ) if ( strlen(arg) < sizeof(params) )
{ {
if ( retjsonp == 0 )
retjsonp = &retjson;
sprintf(params,"[\"%s\"]",arg); sprintf(params,"[\"%s\"]",arg);
return(electrum_submit(symbol,ep,retjsonp,method,params,timeout)); return(electrum_submit(symbol,ep,retjsonp,method,params,timeout));
} else return(0); } else return(0);
@ -361,14 +358,18 @@ cJSON *electrum_strarg(char *symbol,struct electrum_info *ep,cJSON **retjsonp,ch
cJSON *electrum_intarg(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,int32_t arg,int32_t timeout) cJSON *electrum_intarg(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,int32_t arg,int32_t timeout)
{ {
char params[64]; char params[64]; cJSON *retjson;
if ( retjsonp == 0 )
retjsonp = &retjson;
sprintf(params,"[\"%d\"]",arg); sprintf(params,"[\"%d\"]",arg);
return(electrum_submit(symbol,ep,retjsonp,method,params,timeout)); return(electrum_submit(symbol,ep,retjsonp,method,params,timeout));
} }
cJSON *electrum_hasharg(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,bits256 arg,int32_t timeout) cJSON *electrum_hasharg(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,bits256 arg,int32_t timeout)
{ {
char params[128],str[65]; char params[128],str[65]; cJSON *retjson;
if ( retjsonp == 0 )
retjsonp = &retjson;
sprintf(params,"[\"%s\"]",bits256_str(str,arg)); sprintf(params,"[\"%s\"]",bits256_str(str,arg));
return(electrum_submit(symbol,ep,retjsonp,method,params,timeout)); return(electrum_submit(symbol,ep,retjsonp,method,params,timeout));
} }
@ -549,33 +550,25 @@ int32_t LP_recvfunc(struct electrum_info *ep,char *str,int32_t len)
{ {
DL_FOREACH(ep->pendingQ.list,item) DL_FOREACH(ep->pendingQ.list,item)
{ {
if ( item->type == 0xffffffff )
continue;
stritem = (struct stritem *)item; stritem = (struct stritem *)item;
if ( item->type == idnum ) if ( item->type == idnum )
{ {
DL_DELETE(ep->pendingQ.list,item);
//printf("matched idnum.%d\n",idnum); //printf("matched idnum.%d\n",idnum);
item->type = 0xffffffff; *((cJSON **)stritem->retptrp) = strjson;
if ( stritem->retptrp != 0 ) strjson = 0;
{ free(item);
*((cJSON **)stritem->retptrp) = strjson;
strjson = 0;
}
//DL_DELETE(ep->pendingQ.list,item);
break; break;
} }
if ( stritem->expiration < ep->lasttime ) if ( stritem->expiration < ep->lasttime )
{ {
DL_DELETE(ep->pendingQ.list,item);
printf("expired (%s)\n",stritem->str); printf("expired (%s)\n",stritem->str);
item->type = 0xffffffff; errjson = cJSON_CreateObject();
//DL_DELETE(ep->pendingQ.list,item); jaddnum(errjson,"id",item->type);
if ( stritem->retptrp != 0 ) jaddstr(errjson,"error","timeout");
{ *((cJSON **)stritem->retptrp) = errjson;
errjson = cJSON_CreateObject(); free(item);
jaddnum(errjson,"id",item->type);
jaddstr(errjson,"error","timeout");
*((cJSON **)stritem->retptrp) = errjson;
};
} }
} }
} }
@ -588,7 +581,7 @@ int32_t LP_recvfunc(struct electrum_info *ep,char *str,int32_t len)
void LP_dedicatedloop(void *arg) void LP_dedicatedloop(void *arg)
{ {
struct pollfd fds; int32_t i,len,flag,timeout = 10; struct iguana_info *coin; cJSON *retjson; struct stritem *sitem; struct queueitem *item = 0; struct electrum_info *ep = arg; struct pollfd fds; int32_t i,len,flag,timeout = 10; struct iguana_info *coin; cJSON *retjson; struct stritem *sitem; struct electrum_info *ep = arg;
if ( (coin= LP_coinfind(ep->symbol)) != 0 ) if ( (coin= LP_coinfind(ep->symbol)) != 0 )
ep->heightp = &coin->height, ep->heighttimep = &coin->heighttime; ep->heightp = &coin->height, ep->heighttimep = &coin->heighttime;
if ( (retjson= electrum_headers_subscribe(ep->symbol,ep,0)) != 0 ) if ( (retjson= electrum_headers_subscribe(ep->symbol,ep,0)) != 0 )
@ -613,7 +606,7 @@ void LP_dedicatedloop(void *arg)
if ( sitem->expiration != 0 ) if ( sitem->expiration != 0 )
sitem->expiration += (uint32_t)time(NULL); sitem->expiration += (uint32_t)time(NULL);
else sitem->expiration = (uint32_t)time(NULL) + ELECTRUM_TIMEOUT; else sitem->expiration = (uint32_t)time(NULL) + ELECTRUM_TIMEOUT;
portable_mutex_lock(&ep->pendingQ.mutex); /*portable_mutex_lock(&ep->pendingQ.mutex);
if ( ep->pendingQ.list != 0 ) if ( ep->pendingQ.list != 0 )
{ {
printf("list %p\n",ep->pendingQ.list); printf("list %p\n",ep->pendingQ.list);
@ -628,9 +621,10 @@ void LP_dedicatedloop(void *arg)
} }
} }
} }
printf("%p SEND.(%s) to %s:%u\n",sitem,sitem->str,ep->ipaddr,ep->port);
DL_APPEND(ep->pendingQ.list,&sitem->DL); DL_APPEND(ep->pendingQ.list,&sitem->DL);
portable_mutex_unlock(&ep->pendingQ.mutex); portable_mutex_unlock(&ep->pendingQ.mutex);*/
printf("%p SEND.(%s) to %s:%u\n",sitem,sitem->str,ep->ipaddr,ep->port);
queue_enqueue("pendingQ",&ep->pendingQ,&sitem->DL);
flag++; flag++;
} }
if ( flag == 0 ) if ( flag == 0 )

Loading…
Cancel
Save