From 15b0ab37bc7afc8fb4f9aa26dfef37d612e72c11 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 2 Jul 2016 06:21:15 -0300 Subject: [PATCH] test --- basilisk/basilisk.h | 4 +- basilisk/basilisk_DEX.c | 109 +++++++++++++++++++++++++------------- iguana/iguana_exchanges.c | 6 +-- iguana/iguana_msg.c | 2 +- iguana/tests/request | 2 +- 5 files changed, 80 insertions(+), 43 deletions(-) diff --git a/basilisk/basilisk.h b/basilisk/basilisk.h index 04f8b19b8..a5077084a 100755 --- a/basilisk/basilisk.h +++ b/basilisk/basilisk.h @@ -41,12 +41,12 @@ struct basilisk_request { uint32_t requestid,timestamp,quoteid; - uint64_t srcamount; + uint64_t srcamount,minamount; bits256 hash; char src[8],dest[8]; char volatile_start,message[43]; - uint64_t destamount; uint32_t relaybits; + uint64_t destamount; bits256 desthash; } __attribute__((packed)); diff --git a/basilisk/basilisk_DEX.c b/basilisk/basilisk_DEX.c index 8cfce52a0..4237ae9fc 100755 --- a/basilisk/basilisk_DEX.c +++ b/basilisk/basilisk_DEX.c @@ -71,13 +71,14 @@ int32_t basilisk_rwDEXquote(int32_t rwflag,uint8_t *serialized,struct basilisk_r return(len); } -uint32_t basilisk_request_enqueue(struct supernet_info *myinfo,int32_t queueflag,struct basilisk_request *finalR,bits256 hash,char *src,uint64_t srcamount,bits256 desthash,char *dest,uint64_t destamount,char *message,int32_t calcquoteid) +uint32_t basilisk_request_enqueue(struct supernet_info *myinfo,int32_t queueflag,struct basilisk_request *finalR,bits256 hash,char *src,uint64_t srcamount,uint64_t minamount,bits256 desthash,char *dest,uint64_t destamount,char *message,int32_t calcquoteid) { uint8_t serialized[256]; int32_t len; struct queueitem *item; struct basilisk_request R; memset(&R,0,sizeof(R)); R.timestamp = (uint32_t)time(NULL); R.hash = hash; R.srcamount = srcamount; + R.minamount = minamount; strncpy(R.src,src,sizeof(R.src)-1); strncpy(R.dest,dest,sizeof(R.dest)-1); if ( message != 0 ) @@ -105,15 +106,63 @@ uint32_t basilisk_request_enqueue(struct supernet_info *myinfo,int32_t queueflag return(0); } +struct basilisk_request *basilisk_parsejson(struct basilisk_request *rp,cJSON *reqjson) +{ + uint32_t requestid,quoteid; char *msgstr; + memset(rp,0,sizeof(*rp)); + rp->hash = jbits256(reqjson,"hash"); + rp->desthash = jbits256(reqjson,"desthash"); + rp->srcamount = j64bits(reqjson,"srcamount"); + rp->minamount = j64bits(reqjson,"minamount"); + rp->destamount = j64bits(reqjson,"destamount"); + requestid = juint(reqjson,"requestid"); + quoteid = juint(reqjson,"quoteid"); + if ( jstr(reqjson,"relay") != 0 ) + rp->relaybits = (uint32_t)calc_ipbits(jstr(reqjson,"relay")); + rp->timestamp = juint(reqjson,"timestamp"); + safecopy(rp->src,jstr(reqjson,"src"),sizeof(rp->src)); + safecopy(rp->dest,jstr(reqjson,"dest"),sizeof(rp->dest)); + if ( jobj(reqjson,"message") != 0 ) + { + msgstr = jprint(jobj(reqjson,"message"),0); + if ( strlen(msgstr) > sizeof(rp->message)-1 ) + printf("basilisk_parsejson msgstr.(%s) too long\n",msgstr); + safecopy(rp->message,msgstr,sizeof(rp->message)); + free(msgstr); + } + if ( quoteid != 0 ) + { + rp->quoteid = basilisk_quoteid(rp); + if ( quoteid != rp->quoteid ) + printf("basilisk_parsejson quoteid.%u != %u error\n",quoteid,rp->quoteid); + } + rp->requestid = basilisk_requestid(rp); + if ( requestid != rp->requestid ) + printf("basilisk_parsejson requestid.%u != %u error\n",requestid,rp->requestid); + return(rp); +} + cJSON *basilisk_requestjson(uint32_t relaybits,struct basilisk_request *rp) { + /*uint32_t requestid,timestamp,quoteid; + uint64_t srcamount,minamount; + bits256 hash; + char src[8],dest[8]; + char volatile_start,message[43]; + uint32_t relaybits; + uint64_t destamount; + bits256 desthash;*/ char ipaddr[64]; cJSON *msgobj,*item = cJSON_CreateObject(); expand_ipbits(ipaddr,relaybits); jaddstr(item,"relay",ipaddr); jaddbits256(item,"hash",rp->hash); + if ( bits256_nonz(rp->desthash) != 0 ) + jaddbits256(item,"desthash",rp->desthash); jaddstr(item,"src",rp->src); if ( rp->srcamount != 0 ) jadd64bits(item,"srcamount",rp->srcamount); + if ( rp->minamount != 0 ) + jadd64bits(item,"min",rp->minamount); jaddstr(item,"dest",rp->dest); if ( rp->destamount != 0 ) jadd64bits(item,"destamount",rp->destamount); @@ -122,6 +171,25 @@ cJSON *basilisk_requestjson(uint32_t relaybits,struct basilisk_request *rp) jaddnum(item,"quoteid",rp->quoteid); if ( rp->message[0] != 0 && (msgobj= cJSON_Parse(rp->message)) != 0 ) jadd(item,"message",msgobj); + if ( rp->quoteid != 0 && basilisk_quoteid(rp) != rp->quoteid ) + printf("quoteid mismatch %u vs %u\n",basilisk_quoteid(rp),rp->quoteid); + if ( basilisk_requestid(rp) != rp->requestid ) + printf("requestid mismatch %u vs %u\n",basilisk_requestid(rp),rp->requestid); + { + int32_t i; struct basilisk_request R; + if ( basilisk_parsejson(&R,item) != 0 ) + { + if ( memcmp(&R,rp,sizeof(*rp)) != 0 ) + { + for (i=0; ihash,rp->src,rp->srcamount,rp->desthash,rp->dest,rp->destamount,msgjsonstr,myinfo->RELAYID < 0) == rp->requestid ) + if ( basilisk_request_enqueue(myinfo,1,&R,rp->hash,rp->src,rp->srcamount,rp->minamount,rp->desthash,rp->dest,rp->destamount,msgjsonstr,myinfo->RELAYID < 0) == rp->requestid ) { if ( myinfo->RELAYID >= 0 && (bits256_cmp(rp->hash,myinfo->myaddr.persistent) == 0 || bits256_cmp(rp->desthash,myinfo->myaddr.persistent) == 0) ) { printf("START thread to complete %u/%u for (%s %.8f) <- (%s %.8f)\n",rp->requestid,R.quoteid,rp->src,dstr(rp->srcamount),rp->dest,dstr(rp->destamount)); if ( basilisk_thread_start(myinfo,&R) != 0 ) { - basilisk_request_enqueue(myinfo,1,&R,rp->hash,rp->src,rp->srcamount,rp->desthash,rp->dest,rp->destamount,msgjsonstr,1); + basilisk_request_enqueue(myinfo,1,&R,rp->hash,rp->src,rp->srcamount,rp->minamount,rp->desthash,rp->dest,rp->destamount,msgjsonstr,1); return(clonestr("{\"result\":\"started atomic swap thread\"}")); } else return(clonestr("{\"error\":\"couldnt atomic swap thread\"}")); @@ -152,38 +220,6 @@ char *basilisk_start(struct supernet_info *myinfo,struct basilisk_request *rp,ui } // end of swap code -struct basilisk_request *basilisk_parsejson(struct basilisk_request *rp,cJSON *reqjson) -{ - uint32_t requestid,quoteid; char *msgstr; - memset(rp,0,sizeof(*rp)); - rp->hash = jbits256(reqjson,"hash"); - rp->srcamount = j64bits(reqjson,"srcamount"); - rp->destamount = j64bits(reqjson,"destamount"); - requestid = juint(reqjson,"requestid"); - quoteid = juint(reqjson,"quoteid"); - rp->timestamp = juint(reqjson,"timestamp"); - safecopy(rp->src,jstr(reqjson,"src"),sizeof(rp->src)); - safecopy(rp->dest,jstr(reqjson,"dest"),sizeof(rp->dest)); - if ( jobj(reqjson,"message") != 0 ) - { - msgstr = jprint(jobj(reqjson,"message"),0); - if ( strlen(msgstr) > sizeof(rp->message)-1 ) - printf("basilisk_parsejson msgstr.(%s) too long\n",msgstr); - safecopy(rp->message,msgstr,sizeof(rp->message)); - free(msgstr); - } - if ( quoteid != 0 ) - { - rp->quoteid = basilisk_quoteid(rp); - if ( quoteid != rp->quoteid ) - printf("basilisk_parsejson quoteid.%u != %u error\n",quoteid,rp->quoteid); - } - rp->requestid = basilisk_requestid(rp); - if ( requestid != rp->requestid ) - printf("basilisk_parsejson requestid.%u != %u error\n",requestid,rp->requestid); - return(rp); -} - struct basilisk_relay *basilisk_request_ensure(struct supernet_info *myinfo,uint32_t senderipbits,int32_t numrequests) { int32_t j; struct basilisk_relay *relay = 0; @@ -431,7 +467,7 @@ char *basilisk_respond_DEX(struct supernet_info *myinfo,char *CMD,void *addr,cha char str[65]; printf("DEX.(%s %.8f) -> %s %s\n",src,dstr(satoshis),dest,bits256_str(str,hash)); if ( jobj(valsobj,"message") != 0 ) msgstr = jprint(jobj(valsobj,"message"),0); - if ( (requestid= basilisk_request_enqueue(myinfo,1,&R,hash,src,satoshis,desthash,dest,destamount,msgstr,destamount != 0)) != 0 ) + if ( (requestid= basilisk_request_enqueue(myinfo,1,&R,hash,src,satoshis,j64bits(valsobj,"min"),desthash,dest,destamount,msgstr,destamount != 0)) != 0 ) { sprintf(buf,"{\"result\":\"DEX request added\",\"requestid\":%u}",requestid); retstr = clonestr(buf); @@ -491,6 +527,7 @@ HASH_ARRAY_STRING(InstantDEX,request,hash,vals,hexstr) jaddbits256(vals,"desthash",hash); jadd64bits(vals,"satoshis",jdouble(vals,"amount") * SATOSHIDEN); jadd64bits(vals,"destsatoshis",jdouble(vals,"destamount") * SATOSHIDEN); + printf("service.(%s)\n",jprint(vals,0)); return(basilisk_standardservice("DEX",myinfo,0,myinfo->myaddr.persistent,vals,"",1)); } diff --git a/iguana/iguana_exchanges.c b/iguana/iguana_exchanges.c index 5a2f0620e..44659d007 100755 --- a/iguana/iguana_exchanges.c +++ b/iguana/iguana_exchanges.c @@ -84,19 +84,19 @@ double instantdex_aveprice(struct supernet_info *myinfo,struct exchange_quote *s else if ( dir > 0 && active[i]->numasks > 0 ) n = instantdex_updatesources(active[i]->exchange,sortbuf,n,max,i,-1,&active[i]->bidasks[1],active[i]->numasks); } - printf("dir.%d %s/%s numX.%d n.%d\n",dir,base,rel,num,n); + //printf("dir.%d %s/%s numX.%d n.%d\n",dir,base,rel,num,n); if ( dir < 0 ) revsort64s(&sortbuf[0].satoshis,n,sizeof(*sortbuf)); else sort64s(&sortbuf[0].satoshis,n,sizeof(*sortbuf)); for (totalvol=pricesum=i=0; iexchange->name,pricesum/totalvol,totalvol); + //printf("i.%d of %d %12.8f vol %.8f %s | aveprice %.8f total vol %.8f\n",i,n,sortbuf[i].price,quote.volume,active[quote.val]->exchange->name,pricesum/totalvol,totalvol); } } if ( totalvol > 0. ) diff --git a/iguana/iguana_msg.c b/iguana/iguana_msg.c index f76290bb5..39fbb50ce 100755 --- a/iguana/iguana_msg.c +++ b/iguana/iguana_msg.c @@ -889,7 +889,7 @@ int32_t iguana_msgparser(struct iguana_info *coin,struct iguana_peer *addr,struc { struct iguana_msgblock msg; struct iguana_zblock *zblocks; uint32_t tmp,n=0; len = 0; - if ( addr != 0 ) + if ( addr != 0 && recvlen >= sizeof(bits256) ) { if ( ishost == 0 ) { diff --git a/iguana/tests/request b/iguana/tests/request index db4bf1264..154968b00 100755 --- a/iguana/tests/request +++ b/iguana/tests/request @@ -1,2 +1,2 @@ -curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"InstantDEX\",\"method\":\"request\",\"vals\":{\"source\":\"BTCD\",\"amount\":1,\"dest\":\"BTC\",\"minprice\":0.003,\"autoflag\":1}}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"InstantDEX\",\"method\":\"request\",\"vals\":{\"source\":\"BTCD\",\"amount\":1,\"dest\":\"BTC\",\"minprice\":0.0025,\"autoflag\":1}}"