From 58df6f2f11cacc31a7e29574ce5d860e58da7e79 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 2 Jul 2016 10:53:57 -0300 Subject: [PATCH] test --- basilisk/basilisk.c | 153 ++++++++++++++++++++++++++++++++++++++- basilisk/basilisk_DEX.c | 141 +----------------------------------- basilisk/basilisk_swap.c | 2 +- 3 files changed, 154 insertions(+), 142 deletions(-) diff --git a/basilisk/basilisk.c b/basilisk/basilisk.c index b498c8534..5f4d9cbc9 100755 --- a/basilisk/basilisk.c +++ b/basilisk/basilisk.c @@ -716,6 +716,157 @@ void basilisk_p2p(void *_myinfo,void *_addr,char *senderip,uint8_t *data,int32_t free(ptr); } +double basilisk_request_listprocess(struct supernet_info *myinfo,struct basilisk_request *issueR,struct basilisk_request *list,int32_t n) +{ + int32_t i,noquoteflag=0,havequoteflag=0,myrequest=0,maxi=-1; uint64_t destamount,minamount = 0,maxamount = 0; uint32_t pendingid=0; struct basilisk_swap *active; double metric = 0.; + memset(issueR,0,sizeof(*issueR)); + minamount = list[0].minamount; + // printf("need to verify null quoteid is list[0] requestid.%u quoteid.%u\n",list[0].requestid,list[0].quoteid); + if ( (active= basilisk_request_started(myinfo,list[0].requestid)) != 0 ) + pendingid = active->req.quoteid; + if ( bits256_cmp(myinfo->myaddr.persistent,list[0].hash) == 0 ) // my request + myrequest = 1; + /*if ( list[0].message[0] != 0 && (msgobj= cJSON_Parse(list[0].message)) != 0 ) + { + autoflag = juint(msgobj,"auto"); + minamount = j64bits(msgobj,"min"); + }*/ + for (i=0; imyaddr.persistent,list[i].desthash) == 0 ) // my quoteid + myrequest |= 2; + havequoteflag++; + if ( pendingid == 0 ) + { + if ( list[i].destamount > maxamount ) + { + maxamount = list[i].destamount; + maxi = i; + } + } + else if ( active != 0 && pendingid == list[i].quoteid ) + { + /*if ( (statejson= cJSON_Parse(list[i].message)) != 0 ) + { + statebits = juint(statejson,"state"); + if ( bitweight(statebits) > bitweight(active->statebits) ) + { + // advance statemachine + //active->statebits = statebits; + printf("req statbits.%x -> %x\n",active->statebits,statebits); + } + free(statejson); + }*/ + } + } else noquoteflag++; + } + printf("myrequest.%d pendingid.%u noquoteflag.%d havequoteflag.%d maxi.%d %.8f\n",myrequest,pendingid,noquoteflag,havequoteflag,maxi,dstr(maxamount)); + if ( myrequest == 0 && pendingid == 0 && noquoteflag != 0 ) + { + double retvals[4],aveprice; + aveprice = instantdex_avehbla(myinfo,retvals,list[0].src,list[0].dest,dstr(list[0].srcamount)); + destamount = 0.99 * aveprice * list[0].srcamount; + printf("destamount %.8f aveprice %.8f minamount %.8f\n",dstr(destamount),aveprice,dstr(minamount)); + if ( destamount > 0 && destamount >= maxamount && destamount >= minamount ) + { + metric = 1.; + *issueR = list[0]; + issueR->desthash = myinfo->myaddr.persistent; + issueR->destamount = destamount; + issueR->quotetime = (uint32_t)time(NULL); + } + } + else if ( myrequest != 0 && pendingid == 0 && maxi >= 0 ) // automatch best quote + { + if ( minamount != 0 && maxamount > minamount && time(NULL) > BASILISK_DEXDURATION/2 ) + { + printf("automatch quoteid.%u triggered %.8f > %.8f\n",list[maxi].quoteid,dstr(maxamount),dstr(minamount)); + *issueR = list[maxi]; + if ( minamount > 0 ) + metric = (dstr(maxamount) / dstr(minamount)) - 1.; + else metric = 1.; + } + } + return(metric); +} + +double basilisk_process_results(struct supernet_info *myinfo,cJSON *retjson,double hwm) +{ + cJSON *array,*item; int32_t i,n,m; struct basilisk_request tmpR,R,issueR,refR,list[BASILISK_MAXRELAYS*10]; double metric=0.; + if ( (array= jarray(&n,retjson,"result")) != 0 ) + { + for (i=m=0; i hwm ) + issueR = tmpR, hwm = metric; + m = 0; + } + } + if ( m < sizeof(list)/sizeof(*list) ) + basilisk_parsejson(&list[m++],item); + } + if ( m > 0 && m < sizeof(list)/sizeof(*list) ) + if ( (metric= basilisk_request_listprocess(myinfo,&tmpR,list,m)) > hwm ) + issueR = tmpR, hwm = metric; + } + return(hwm); +} + +void basilisk_requests_poll(struct supernet_info *myinfo) +{ + char *retstr; cJSON *outerarray; int32_t i,n; struct basilisk_request issueR; double hwm = 0.; + memset(&issueR,0,sizeof(issueR)); + /*{ + double retvals[4],aveprice; uint64_t destamount; + aveprice = instantdex_avehbla(myinfo,retvals,"BTCD","BTC",1); + destamount = 0.99 * aveprice * 1 * SATOSHIDEN; + printf("destamount %.8f aveprice %.8f\n",dstr(destamount),aveprice); + }*/ + if ( (retstr= InstantDEX_incoming(myinfo,0,0,0,0)) != 0 ) + { + //printf("poll.(%s)\n",retstr); + if ( (outerarray= cJSON_Parse(retstr)) != 0 ) + { + if ( is_cJSON_Array(outerarray) != 0 ) + { + n = cJSON_GetArraySize(outerarray); + for (i=0; i 0. ) + { + printf("hwm %f\n",hwm); + if ( bits256_cmp(myinfo->myaddr.persistent,issueR.hash) == 0 ) // my request + { + if ( (retstr= InstantDEX_accept(myinfo,0,0,0,issueR.requestid,issueR.quoteid)) != 0 ) + free(retstr); + } + else //if ( issueR.quoteid == 0 ) + { + issueR.quoteid = basilisk_quoteid(&issueR); + issueR.desthash = myinfo->myaddr.persistent; + if ( (retstr= basilisk_start(myinfo,&issueR,0)) != 0 ) + free(retstr); + } //else printf("basilisk_requests_poll unexpected hwm issueR\n"); + } +} + void basilisks_loop(void *arg) { struct iguana_info *virt,*tmpcoin,*btcd; struct basilisk_message *msg,*tmpmsg; struct basilisk_item *tmp,*pending; uint32_t now; int32_t iter,maxmillis,flag=0; struct supernet_info *myinfo = arg; @@ -767,7 +918,7 @@ void basilisks_loop(void *arg) } } portable_mutex_unlock(&myinfo->messagemutex); - usleep(5000000); + usleep(1000000); } } diff --git a/basilisk/basilisk_DEX.c b/basilisk/basilisk_DEX.c index 009fbd771..ea06c05a3 100755 --- a/basilisk/basilisk_DEX.c +++ b/basilisk/basilisk_DEX.c @@ -175,7 +175,7 @@ cJSON *basilisk_requestjson(uint32_t relaybits,struct basilisk_request *rp) if ( ((uint8_t *)rp)[i] != ((uint8_t *)&R)[i] ) printf("(%02x %02x).%d ",((uint8_t *)rp)[i],((uint8_t *)&R)[i],i); printf("mismatches\n"); - } else printf("matched JSON conv %u %u\n",basilisk_requestid(&R),basilisk_requestid(rp)); + } //else printf("matched JSON conv %u %u\n",basilisk_requestid(&R),basilisk_requestid(rp)); } } return(item); @@ -634,142 +634,3 @@ int32_t basilisk_request_cmpref(struct basilisk_request *ref,struct basilisk_req return(-1); } else return(0); } - -double basilisk_request_listprocess(struct supernet_info *myinfo,struct basilisk_request *issueR,struct basilisk_request *list,int32_t n) -{ - int32_t i,noquoteflag=0,havequoteflag=0,myrequest=0,maxi=-1; uint64_t destamount,minamount = 0,maxamount = 0; uint32_t pendingid=0; struct basilisk_swap *active; double metric = 0.; - memset(issueR,0,sizeof(*issueR)); - minamount = list[0].minamount; - printf("need to verify null quoteid is list[0] requestid.%u quoteid.%u\n",list[0].requestid,list[0].quoteid); - if ( (active= basilisk_request_started(myinfo,list[0].requestid)) != 0 ) - pendingid = active->req.quoteid; - if ( bits256_cmp(myinfo->myaddr.persistent,list[0].hash) == 0 ) // my request - myrequest = 1; - /*if ( list[0].message[0] != 0 && (msgobj= cJSON_Parse(list[0].message)) != 0 ) - { - autoflag = juint(msgobj,"auto"); - minamount = j64bits(msgobj,"min"); - }*/ - for (i=0; imyaddr.persistent,list[i].desthash) == 0 ) // my quoteid - myrequest |= 2; - havequoteflag++; - if ( pendingid == 0 ) - { - if ( list[i].destamount > maxamount ) - { - maxamount = list[i].destamount; - maxi = i; - } - } - else if ( active != 0 && pendingid == list[i].quoteid ) - { - /*if ( (statejson= cJSON_Parse(list[i].message)) != 0 ) - { - statebits = juint(statejson,"state"); - if ( bitweight(statebits) > bitweight(active->statebits) ) - { - // advance statemachine - //active->statebits = statebits; - printf("req statbits.%x -> %x\n",active->statebits,statebits); - } - free(statejson); - }*/ - } - } else noquoteflag++; - } - printf("myrequest.%d pendingid.%u noquoteflag.%d havequoteflag.%d\n",myrequest,pendingid,noquoteflag,havequoteflag); - if ( myrequest == 0 && pendingid == 0 && noquoteflag != 0 ) - { - double retvals[4],aveprice; - aveprice = instantdex_avehbla(myinfo,retvals,list[0].src,list[0].dest,dstr(list[0].srcamount)); - destamount = 0.99 * aveprice * list[0].srcamount; - printf("destamount %.8f aveprice %.8f minamount %.8f\n",dstr(destamount),aveprice,dstr(minamount)); - if ( destamount > 0 && destamount >= maxamount && destamount >= minamount ) - { - metric = 1.; - *issueR = list[0]; - issueR->desthash = myinfo->myaddr.persistent; - issueR->destamount = destamount; - issueR->quotetime = (uint32_t)time(NULL); - } - } - else if ( myrequest != 0 && pendingid == 0 && maxi >= 0 ) // automatch best quote - { - if ( minamount != 0 && maxamount > minamount && time(NULL) > BASILISK_DEXDURATION/2 ) - { - printf("automatch quoteid.%u triggered %.8f > %.8f\n",list[maxi].quoteid,dstr(maxamount),dstr(minamount)); - *issueR = list[maxi]; - if ( minamount > 0 ) - metric = (dstr(maxamount) / dstr(minamount)) - 1.; - else metric = 1.; - } - } - return(metric); -} - -void basilisk_requests_poll(struct supernet_info *myinfo) -{ - char *retstr; cJSON *retjson,*array,*item; int32_t i,n,m; struct basilisk_request tmpR,R,issueR,refR,list[BASILISK_MAXRELAYS*10]; double metric=0.,hwm = 0.; - memset(&issueR,0,sizeof(issueR)); - /*{ - double retvals[4],aveprice; uint64_t destamount; - aveprice = instantdex_avehbla(myinfo,retvals,"BTCD","BTC",1); - destamount = 0.99 * aveprice * 1 * SATOSHIDEN; - printf("destamount %.8f aveprice %.8f\n",dstr(destamount),aveprice); - }*/ - if ( (retstr= InstantDEX_incoming(myinfo,0,0,0,0)) != 0 ) - { - //printf("poll.(%s)\n",retstr); - if ( (retjson= cJSON_Parse(retstr)) != 0 ) - { - if ( (array= jarray(&n,retjson,"result")) != 0 ) - { - for (i=m=0; i hwm ) - issueR = tmpR, hwm = metric; - m = 0; - } - } - if ( m < sizeof(list)/sizeof(*list) ) - basilisk_parsejson(&list[m++],item); - } - if ( m > 0 && m < sizeof(list)/sizeof(*list) ) - if ( (metric= basilisk_request_listprocess(myinfo,&tmpR,list,m)) > hwm ) - issueR = tmpR, hwm = metric; - } - free_json(retjson); - } - free(retstr); - } - printf("hwm %f\n",hwm); - if ( hwm > 0. ) - { - if ( bits256_cmp(myinfo->myaddr.persistent,issueR.hash) == 0 ) // my request - { - if ( (retstr= InstantDEX_accept(myinfo,0,0,0,issueR.requestid,issueR.quoteid)) != 0 ) - free(retstr); - } - else //if ( issueR.quoteid == 0 ) - { - issueR.quoteid = basilisk_quoteid(&issueR); - issueR.desthash = myinfo->myaddr.persistent; - if ( (retstr= basilisk_start(myinfo,&issueR,0)) != 0 ) - free(retstr); - } //else printf("basilisk_requests_poll unexpected hwm issueR\n"); - } -} diff --git a/basilisk/basilisk_swap.c b/basilisk/basilisk_swap.c index 66f12a377..bd30584c8 100755 --- a/basilisk/basilisk_swap.c +++ b/basilisk/basilisk_swap.c @@ -736,7 +736,7 @@ struct basilisk_swap *basilisk_request_started(struct supernet_info *myinfo,uint for (i=0; inumswaps; i++) if ( myinfo->swaps[i]->req.requestid == requestid ) { - printf("REQUEST STARTED.[%d] <- req.%u\n",i,requestid); + //printf("REQUEST STARTED.[%d] <- req.%u\n",i,requestid); active = myinfo->swaps[i]; break; }