From 68ca1cc0b54bfa6c97eba98b24f30299a2d0cb06 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 5 Mar 2017 16:45:39 +0200 Subject: [PATCH] Test --- basilisk/basilisk_MSG.c | 4 +-- basilisk/basilisk_swap.c | 70 ++++++++++++++++++++++++++++++++++++-- iguana/dpow/dpow_network.c | 2 +- iguana/iguana777.h | 8 +++++ 4 files changed, 78 insertions(+), 6 deletions(-) diff --git a/basilisk/basilisk_MSG.c b/basilisk/basilisk_MSG.c index e6d55208d..a3ea7b343 100755 --- a/basilisk/basilisk_MSG.c +++ b/basilisk/basilisk_MSG.c @@ -448,9 +448,9 @@ cJSON *basilisk_channelget(struct supernet_info *myinfo,bits256 srchash,bits256 retstr = basilisk_getmessage(myinfo,0,0,0,desthash,valsobj,0); else { - char str[65],str2[65]; + //char str[65],str2[65]; retstr = _dex_getmessage(myinfo,jprint(valsobj,0)); - printf("channel.%u msgid.%u gotmessage.(%d) %s %s %s\n",channel,msgid,(int32_t)strlen(retstr),strlen(retstr) < 100 ? retstr : "(too long)",bits256_str(str,srchash),bits256_str(str2,desthash)); + //printf("channel.%u msgid.%u gotmessage.(%d) %s %s %s\n",channel,msgid,(int32_t)strlen(retstr),strlen(retstr) < 100 ? retstr : "(too long)",bits256_str(str,srchash),bits256_str(str2,desthash)); } if ( retstr != 0 ) { diff --git a/basilisk/basilisk_swap.c b/basilisk/basilisk_swap.c index 5843af951..47eecd497 100755 --- a/basilisk/basilisk_swap.c +++ b/basilisk/basilisk_swap.c @@ -879,9 +879,29 @@ int32_t basilisk_process_swapverify(struct supernet_info *myinfo,void *ptr,int32 else return(0); } +void basilisk_swapgotdata(struct supernet_info *myinfo,struct basilisk_swap *swap,uint32_t crc32,bits256 srchash,bits256 desthash,uint32_t quoteid,uint32_t msgbits,uint8_t *data,int32_t datalen) +{ + int32_t i; struct basilisk_swapmessage *mp; + for (i=0; inummessages; i++) + if ( crc32 == swap->messages[i].crc32 ) + return; + printf("new message.[%d] datalen.%d Q.%x msg.%x\n",swap->nummessages,datalen,quoteid,msgbits); + swap->messages = realloc(swap->messages,sizeof(*swap->messages) * (swap->nummessages + 1)); + mp = &swap->messages[swap->nummessages++]; + mp->crc32 = crc32; + mp->srchash = srchash; + mp->desthash = desthash; + mp->msgbits = msgbits; + mp->quoteid = quoteid; + mp->data = malloc(datalen); + memcpy(mp->data,data,datalen); + mp->datalen = datalen; +} + int32_t basilisk_swapget(struct supernet_info *myinfo,struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t maxlen,int32_t (*basilisk_verify_func)(struct supernet_info *myinfo,void *ptr,uint8_t *data,int32_t datalen)) { - cJSON *valsobj,*array,*item; int32_t i,n,datalen,retval = -1; char *retstr,*hexstr=0; + uint8_t *ptr; bits256 srchash,desthash; uint32_t crc32,_msgbits,quoteid; int32_t i,size,offset,retval = -1; + /*cJSON *valsobj,*array,*item; int32_t i,n,datalen,retval = -1; char *retstr,*hexstr=0; //int32_t datalen; uint32_t crc; //if ( (crc= basilisk_crcrecv(myinfo,0,swap->verifybuf,sizeof(swap->verifybuf),&datalen,swap->I.otherhash,swap->I.myhash,swap->I.req.quoteid,msgbits)) != 0 ) valsobj = cJSON_CreateObject(); @@ -916,17 +936,54 @@ int32_t basilisk_swapget(struct supernet_info *myinfo,struct basilisk_swap *swap free_json(array); } free(retstr); + }*/ + while ( (size= nn_recv(swap->subsock,&ptr,NN_MSG,0)) >= 0 ) + { + memset(srchash.bytes,0,sizeof(srchash)); + memset(desthash.bytes,0,sizeof(desthash)); + crc32 = calc_crc32(0,ptr,size); + offset = 0; + for (i=0; i<32; i++) + srchash.bytes[i] = ptr[offset++]; + for (i=0; i<32; i++) + desthash.bytes[i] = ptr[offset++]; + offset += iguana_rwnum(0,&ptr[offset],sizeof(uint32_t),"eid); + offset += iguana_rwnum(0,&ptr[offset],sizeof(uint32_t),&_msgbits); + basilisk_swapgotdata(myinfo,swap,crc32,srchash,desthash,quoteid,_msgbits,&ptr[offset],size-offset); + if ( ptr != 0 ) + nn_freemsg(ptr), ptr = 0; + } + for (i=0; inummessages; i++) + { + if ( swap->messages[i].msgbits == msgbits ) + { + printf("matched %x datalen.%d\n",msgbits,swap->messages[i].datalen); + retval = (*basilisk_verify_func)(myinfo,swap,swap->messages[i].data,swap->messages[i].datalen); + break; + } } return(retval); } uint32_t basilisk_swapsend(struct supernet_info *myinfo,struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t datalen,uint32_t nextbits,uint32_t crcs[2]) { + uint8_t *buf; int32_t sentbytes,offset=0,i; //if ( (rand() % 10) == 0 ) // basilisk_channelsend(myinfo,swap->I.myhash,swap->I.otherhash,swap->I.req.quoteid,msgbits,data,datalen,INSTANTDEX_LOCKTIME*2); //if ( basilisk_crcsend(myinfo,0,swap->verifybuf,sizeof(swap->verifybuf),swap->I.myhash,swap->I.otherhash,swap->I.req.quoteid,msgbits,data,datalen,crcs) != 0 ) //return(nextbits); - dex_channelsend(myinfo,swap->I.myhash,swap->I.otherhash,swap->I.req.quoteid,msgbits,data,datalen); //INSTANTDEX_LOCKTIME*2 + //dex_channelsend(myinfo,swap->I.myhash,swap->I.otherhash,swap->I.req.quoteid,msgbits,data,datalen); //INSTANTDEX_LOCKTIME*2 + buf = malloc(datalen) + sizeof(msgbits) + sizeof(swap->I.req.quoteid) + sizeof(bits256)*2; + for (i=0; i<32; i++) + buf[offset++] = swap->I.myhash.bytes[i]; + for (i=0; i<32; i++) + buf[offset++] = swap->I.otherhash.bytes[i]; + offset += iguana_rwnum(1,&buf[offset],sizeof(swap->I.req.quoteid),&swap->I.req.quoteid); + offset += iguana_rwnum(1,&buf[offset],sizeof(msgbits),&msgbits); + memcpy(&buf[offset],data,datalen), offset += datalen; + if ( (sentbytes= nn_send(swap->pushsock,buf,offset,0)) != offset ) + printf("sentbytes.%d vs offset.%d\n",sentbytes,offset); + free(buf); return(0); } @@ -1127,7 +1184,8 @@ int32_t bitcoin_coinptrs(bits256 pubkey,struct iguana_info **bobcoinp,struct igu struct basilisk_swap *bitcoin_swapinit(void *ctx,bits256 privkey,uint8_t *pubkey33,bits256 pubkey25519,struct basilisk_swap *swap,int32_t optionduration,uint32_t statebits) { - struct iguana_info *bobcoin,*alicecoin; uint8_t *alicepub33=0,*bobpub33=0; int32_t x = -1; + //struct iguana_info *bobcoin,*alicecoin; + uint8_t *alicepub33=0,*bobpub33=0; int32_t x = -1; swap->I.putduration = swap->I.callduration = INSTANTDEX_LOCKTIME; if ( optionduration < 0 ) swap->I.putduration -= optionduration; @@ -1285,6 +1343,7 @@ void basilisk_rawtx_purge(struct basilisk_rawtx *rawtx) void basilisk_swap_finished(struct supernet_info *myinfo,struct basilisk_swap *swap) { + int32_t i; swap->I.finished = (uint32_t)time(NULL); // save to permanent storage basilisk_rawtx_purge(&swap->bobdeposit); @@ -1297,6 +1356,11 @@ void basilisk_swap_finished(struct supernet_info *myinfo,struct basilisk_swap *s basilisk_rawtx_purge(&swap->bobreclaim); basilisk_rawtx_purge(&swap->bobspend); basilisk_rawtx_purge(&swap->bobrefund); + for (i=0; inummessages; i++) + if ( swap->messages[i].data != 0 ) + free(swap->messages[i].data), swap->messages[i].data = 0; + free(swap->messages), swap->messages = 0; + swap->nummessages = 0; } void basilisk_swap_purge(struct supernet_info *myinfo,struct basilisk_swap *swap) diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c index a0b99d84a..6f17c611a 100755 --- a/iguana/dpow/dpow_network.c +++ b/iguana/dpow/dpow_network.c @@ -467,7 +467,7 @@ char *_dex_reqsend(struct supernet_info *myinfo,char *handler,uint8_t *key,int32 if ( nn_poll(&pfd,1,100) > 0 ) { sentbytes = nn_send(myinfo->reqsock,dexp,size,0); - printf(" sent.%d:%d datalen.%d crc.%08x\n",sentbytes,size,datalen,calc_crc32(0,(void *)dexp,size)); + //printf(" sent.%d:%d datalen.%d crc.%08x\n",sentbytes,size,datalen,calc_crc32(0,(void *)dexp,size)); break; } usleep(1000); diff --git a/iguana/iguana777.h b/iguana/iguana777.h index e9b6a5f23..cf733a81b 100755 --- a/iguana/iguana777.h +++ b/iguana/iguana777.h @@ -158,6 +158,13 @@ struct supernet_info uint8_t notaries[64][33]; int32_t numnotaries,DEXEXPLORER; }; +struct basilisk_swapmessage +{ + bits256 srchash,desthash; + uint32_t crc32,msgbits,quoteid,datalen; + uint8_t *data; +}; + struct basilisk_swap { struct supernet_info *myinfoptr; struct iguana_info *bobcoin,*alicecoin; @@ -167,6 +174,7 @@ struct basilisk_swap struct basilisk_rawtx bobdeposit,bobpayment,alicepayment,myfee,otherfee,aliceclaim,alicespend,bobreclaim,bobspend,bobrefund,alicereclaim; bits256 privkeys[INSTANTDEX_DECKSIZE]; int32_t subsock,pushsock,connected; + struct basilisk_swapmessage *messages; int32_t nummessages; uint64_t otherdeck[INSTANTDEX_DECKSIZE][2],deck[INSTANTDEX_DECKSIZE][2]; uint8_t persistent_pubkey33[33],pad[15],verifybuf[65536];