|
@ -1055,31 +1055,43 @@ void basilisk_swaps_init(struct supernet_info *myinfo) |
|
|
while ( fread(&requestid,1,sizeof(requestid),myinfo->swapsfp) == sizeof(requestid) && fread("eid,1,sizeof(quoteid),myinfo->swapsfp) == sizeof(quoteid) ) |
|
|
while ( fread(&requestid,1,sizeof(requestid),myinfo->swapsfp) == sizeof(requestid) && fread("eid,1,sizeof(quoteid),myinfo->swapsfp) == sizeof(quoteid) ) |
|
|
{ |
|
|
{ |
|
|
sprintf(fname,"%s/SWAPS/%u-%u",GLOBAL_DBDIR,requestid,quoteid), OS_compatible_path(fname); |
|
|
sprintf(fname,"%s/SWAPS/%u-%u",GLOBAL_DBDIR,requestid,quoteid), OS_compatible_path(fname); |
|
|
|
|
|
printf("%s\n",fname); |
|
|
if ( (fp= fopen(fname,"rb+")) != 0 ) // check to see if completed
|
|
|
if ( (fp= fopen(fname,"rb+")) != 0 ) // check to see if completed
|
|
|
{ |
|
|
{ |
|
|
memset(&M,0,sizeof(M)); |
|
|
memset(&M,0,sizeof(M)); |
|
|
swapcompleted = 0; |
|
|
swapcompleted = 0; |
|
|
for (iter=0; iter<2; iter++) |
|
|
for (iter=0; iter<2; iter++) |
|
|
{ |
|
|
{ |
|
|
while ( fread(privkey.bytes,1,sizeof(privkey),fp) == sizeof(privkey) && |
|
|
if ( fread(privkey.bytes,1,sizeof(privkey),fp) == sizeof(privkey) && |
|
|
fread(&R,1,sizeof(R),fp) == sizeof(R) && |
|
|
fread(&R,1,sizeof(R),fp) == sizeof(R) && |
|
|
fread(&statebits,1,sizeof(statebits),fp) == sizeof(statebits) && |
|
|
fread(&statebits,1,sizeof(statebits),fp) == sizeof(statebits) && |
|
|
fread(&optionduration,1,sizeof(optionduration),fp) == sizeof(optionduration) && |
|
|
fread(&optionduration,1,sizeof(optionduration),fp) == sizeof(optionduration) ) |
|
|
fread(&M,1,sizeof(M),fp) == sizeof(M) ) |
|
|
|
|
|
{ |
|
|
{ |
|
|
|
|
|
while ( fread(&M,1,sizeof(M),fp) == sizeof(M) ) |
|
|
|
|
|
{ |
|
|
|
|
|
M.data = 0; |
|
|
|
|
|
printf("entry iter.%d crc32.%x datalen.%d\n",iter,M.crc32,M.datalen); |
|
|
if ( M.datalen < 100000 ) |
|
|
if ( M.datalen < 100000 ) |
|
|
{ |
|
|
{ |
|
|
if ( M.data == 0 ) |
|
|
|
|
|
M.data = malloc(M.datalen); |
|
|
M.data = malloc(M.datalen); |
|
|
fread(M.data,1,M.datalen,fp); |
|
|
fread(M.data,1,M.datalen,fp); |
|
|
if ( calc_crc32(0,M.data,M.datalen) == M.crc32 ) |
|
|
if ( calc_crc32(0,M.data,M.datalen) == M.crc32 ) |
|
|
{ |
|
|
{ |
|
|
if ( iter == 1 && (swap= basilisk_thread_start(myinfo,privkey,&R,statebits,optionduration,1)) != 0 ) |
|
|
if ( iter == 1 ) |
|
|
|
|
|
{ |
|
|
|
|
|
if ( swap == 0 ) |
|
|
|
|
|
{ |
|
|
|
|
|
swap = basilisk_thread_start(myinfo,privkey,&R,statebits,optionduration,1); |
|
|
|
|
|
swap->I.choosei = swap->I.otherchoosei = -1; |
|
|
|
|
|
} |
|
|
|
|
|
if ( swap != 0 ) |
|
|
basilisk_swapgotdata(myinfo,swap,M.crc32,M.srchash,M.desthash,M.quoteid,M.msgbits,M.data,M.datalen,1); |
|
|
basilisk_swapgotdata(myinfo,swap,M.crc32,M.srchash,M.desthash,M.quoteid,M.msgbits,M.data,M.datalen,1); |
|
|
} |
|
|
} |
|
|
|
|
|
} else printf("crc mismatch %x vs %x\n",calc_crc32(0,M.data,M.datalen),M.crc32); |
|
|
free(M.data), M.data = 0; |
|
|
free(M.data), M.data = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
if ( swapcompleted != 0 ) |
|
|
if ( swapcompleted != 0 ) |
|
|
break; |
|
|
break; |
|
|
rewind(fp); |
|
|
rewind(fp); |
|
@ -1096,7 +1108,6 @@ int32_t basilisk_swapget(struct supernet_info *myinfo,struct basilisk_swap *swap |
|
|
{ |
|
|
{ |
|
|
memset(srchash.bytes,0,sizeof(srchash)); |
|
|
memset(srchash.bytes,0,sizeof(srchash)); |
|
|
memset(desthash.bytes,0,sizeof(desthash)); |
|
|
memset(desthash.bytes,0,sizeof(desthash)); |
|
|
crc32 = calc_crc32(0,ptr,size); |
|
|
|
|
|
//printf("gotmsg.[%d] crc.%x\n",size,crc32);
|
|
|
//printf("gotmsg.[%d] crc.%x\n",size,crc32);
|
|
|
offset = 0; |
|
|
offset = 0; |
|
|
for (i=0; i<32; i++) |
|
|
for (i=0; i<32; i++) |
|
@ -1105,6 +1116,7 @@ int32_t basilisk_swapget(struct supernet_info *myinfo,struct basilisk_swap *swap |
|
|
desthash.bytes[i] = ptr[offset++]; |
|
|
desthash.bytes[i] = ptr[offset++]; |
|
|
offset += iguana_rwnum(0,&ptr[offset],sizeof(uint32_t),"eid); |
|
|
offset += iguana_rwnum(0,&ptr[offset],sizeof(uint32_t),"eid); |
|
|
offset += iguana_rwnum(0,&ptr[offset],sizeof(uint32_t),&_msgbits); |
|
|
offset += iguana_rwnum(0,&ptr[offset],sizeof(uint32_t),&_msgbits); |
|
|
|
|
|
crc32 = calc_crc32(0,&ptr[offset],size-offset); |
|
|
if ( size > offset ) |
|
|
if ( size > offset ) |
|
|
{ |
|
|
{ |
|
|
//printf("size.%d offset.%d datalen.%d\n",size,offset,size-offset);
|
|
|
//printf("size.%d offset.%d datalen.%d\n",size,offset,size-offset);
|
|
@ -1326,26 +1338,32 @@ int32_t bitcoin_coinptrs(bits256 pubkey,struct iguana_info **bobcoinp,struct igu |
|
|
return(0); |
|
|
return(0); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
struct basilisk_swap *bitcoin_swapinit(struct supernet_info *myinfo,bits256 privkey,uint8_t *pubkey33,bits256 pubkey25519,struct basilisk_swap *swap,int32_t optionduration,uint32_t statebits) |
|
|
struct basilisk_swap *bitcoin_swapinit(struct supernet_info *myinfo,bits256 privkey,uint8_t *pubkey33,bits256 pubkey25519,struct basilisk_swap *swap,int32_t optionduration,uint32_t statebits,int32_t reinit) |
|
|
{ |
|
|
{ |
|
|
uint8_t *alicepub33=0,*bobpub33=0; int32_t jumblrflag,x = -1; |
|
|
FILE *fp; char fname[512]; uint8_t *alicepub33=0,*bobpub33=0; int32_t jumblrflag,x = -1; |
|
|
|
|
|
sprintf(fname,"%s/SWAPS/%u-%u.swap",GLOBAL_DBDIR,swap->I.req.requestid,swap->I.req.quoteid); |
|
|
|
|
|
printf("swapfile.(%s)\n",fname); |
|
|
|
|
|
if ( reinit != 0 ) |
|
|
|
|
|
{ |
|
|
|
|
|
if ( (fp= fopen(fname,"rb")) != 0 ) |
|
|
|
|
|
{ |
|
|
|
|
|
fread(&swap->I,1,sizeof(swap->I),fp); |
|
|
|
|
|
fread(swap->privkeys,1,sizeof(swap->privkeys),fp); |
|
|
|
|
|
fread(swap->deck,1,sizeof(swap->deck),fp); |
|
|
|
|
|
fclose(fp); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
swap->I.putduration = swap->I.callduration = INSTANTDEX_LOCKTIME; |
|
|
swap->I.putduration = swap->I.callduration = INSTANTDEX_LOCKTIME; |
|
|
if ( optionduration < 0 ) |
|
|
if ( optionduration < 0 ) |
|
|
swap->I.putduration -= optionduration; |
|
|
swap->I.putduration -= optionduration; |
|
|
else if ( optionduration > 0 ) |
|
|
else if ( optionduration > 0 ) |
|
|
swap->I.callduration += optionduration; |
|
|
swap->I.callduration += optionduration; |
|
|
swap->bobcoin = iguana_coinfind(swap->I.req.dest); |
|
|
|
|
|
swap->I.bobsatoshis = swap->I.req.destamount; |
|
|
swap->I.bobsatoshis = swap->I.req.destamount; |
|
|
swap->I.bobconfirms = (1*0 + sqrt(dstr(swap->I.bobsatoshis) * .1)); |
|
|
swap->I.bobconfirms = (1*0 + sqrt(dstr(swap->I.bobsatoshis) * .1)); |
|
|
swap->alicecoin = iguana_coinfind(swap->I.req.src); |
|
|
|
|
|
swap->I.alicesatoshis = swap->I.req.srcamount; |
|
|
swap->I.alicesatoshis = swap->I.req.srcamount; |
|
|
swap->I.aliceconfirms = swap->I.bobconfirms * 3; |
|
|
swap->I.aliceconfirms = swap->I.bobconfirms * 3; |
|
|
if ( swap->bobcoin == 0 || swap->alicecoin == 0 ) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("missing bobcoin.%p or missing alicecoin.%p src.%p dest.%p\n",swap->bobcoin,swap->alicecoin,iguana_coinfind(swap->I.req.src),iguana_coinfind(swap->I.req.dest)); |
|
|
|
|
|
free(swap); |
|
|
|
|
|
return(0); |
|
|
|
|
|
} |
|
|
|
|
|
if ( swap->I.bobconfirms == 0 ) |
|
|
if ( swap->I.bobconfirms == 0 ) |
|
|
swap->I.bobconfirms = swap->bobcoin->chain->minconfirms; |
|
|
swap->I.bobconfirms = swap->bobcoin->chain->minconfirms; |
|
|
if ( swap->I.aliceconfirms == 0 ) |
|
|
if ( swap->I.aliceconfirms == 0 ) |
|
@ -1379,6 +1397,23 @@ struct basilisk_swap *bitcoin_swapinit(struct supernet_info *myinfo,bits256 priv |
|
|
char str[65]; printf("couldnt generate privkeys %d %s\n",x,bits256_str(str,privkey)); |
|
|
char str[65]; printf("couldnt generate privkeys %d %s\n",x,bits256_str(str,privkey)); |
|
|
return(0); |
|
|
return(0); |
|
|
} |
|
|
} |
|
|
|
|
|
if ( (fp= fopen(fname,"wb")) != 0 ) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("save swapfile\n"); |
|
|
|
|
|
fwrite(&swap->I,1,sizeof(swap->I),fp); |
|
|
|
|
|
fwrite(swap->privkeys,1,sizeof(swap->privkeys),fp); |
|
|
|
|
|
fwrite(swap->deck,1,sizeof(swap->deck),fp); |
|
|
|
|
|
fclose(fp); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
swap->bobcoin = iguana_coinfind(swap->I.req.dest); |
|
|
|
|
|
swap->alicecoin = iguana_coinfind(swap->I.req.src); |
|
|
|
|
|
if ( swap->bobcoin == 0 || swap->alicecoin == 0 ) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("missing bobcoin.%p or missing alicecoin.%p src.%p dest.%p\n",swap->bobcoin,swap->alicecoin,iguana_coinfind(swap->I.req.src),iguana_coinfind(swap->I.req.dest)); |
|
|
|
|
|
free(swap); |
|
|
|
|
|
return(0); |
|
|
|
|
|
} |
|
|
jumblrflag = bits256_cmp(privkey,myinfo->persistent_priv) != 0; |
|
|
jumblrflag = bits256_cmp(privkey,myinfo->persistent_priv) != 0; |
|
|
if ( swap->I.iambob != 0 ) |
|
|
if ( swap->I.iambob != 0 ) |
|
|
{ |
|
|
{ |
|
@ -2207,21 +2242,21 @@ struct basilisk_swap *basilisk_thread_start(struct supernet_info *myinfo,bits256 |
|
|
} |
|
|
} |
|
|
if ( i == myinfo->numswaps && i < sizeof(myinfo->swaps)/sizeof(*myinfo->swaps) ) |
|
|
if ( i == myinfo->numswaps && i < sizeof(myinfo->swaps)/sizeof(*myinfo->swaps) ) |
|
|
{ |
|
|
{ |
|
|
printf("basilisk_thread_start request.%u statebits.%d\n",rp->requestid,statebits); |
|
|
|
|
|
swap = calloc(1,sizeof(*swap)); |
|
|
swap = calloc(1,sizeof(*swap)); |
|
|
swap->subsock = swap->pushsock = -1; |
|
|
swap->subsock = swap->pushsock = -1; |
|
|
vcalc_sha256(0,swap->I.orderhash.bytes,(uint8_t *)rp,sizeof(*rp)); |
|
|
vcalc_sha256(0,swap->I.orderhash.bytes,(uint8_t *)rp,sizeof(*rp)); |
|
|
swap->I.req = *rp; |
|
|
swap->I.req = *rp; |
|
|
swap->myinfoptr = myinfo; |
|
|
swap->myinfoptr = myinfo; |
|
|
|
|
|
printf("basilisk_thread_start request.%u statebits.%d (%s/%s) reinit.%d\n",rp->requestid,statebits,rp->src,rp->dest,reinit); |
|
|
bitcoin_pubkey33(myinfo->ctx,pubkey33,privkey); |
|
|
bitcoin_pubkey33(myinfo->ctx,pubkey33,privkey); |
|
|
pubkey25519 = curve25519(privkey,curve25519_basepoint9()); |
|
|
pubkey25519 = curve25519(privkey,curve25519_basepoint9()); |
|
|
swap->persistent_pubkey = pubkey25519; |
|
|
swap->persistent_pubkey = pubkey25519; |
|
|
swap->persistent_privkey = privkey; |
|
|
swap->persistent_privkey = privkey; |
|
|
memcpy(swap->persistent_pubkey33,pubkey33,33); |
|
|
memcpy(swap->persistent_pubkey33,pubkey33,33); |
|
|
m = n = 0; |
|
|
m = n = 0; |
|
|
if ( bitcoin_swapinit(myinfo,privkey,pubkey33,pubkey25519,swap,optionduration,statebits) != 0 ) |
|
|
if ( bitcoin_swapinit(myinfo,privkey,pubkey33,pubkey25519,swap,optionduration,statebits,reinit) != 0 ) |
|
|
{ |
|
|
{ |
|
|
if ( statebits == 0 ) |
|
|
if ( statebits == 0 )//&& (swap->subsock < 0 || swap->pushsock < 0) )
|
|
|
{ |
|
|
{ |
|
|
if ( (retstr= _dex_psock(myinfo,"{}")) != 0 ) |
|
|
if ( (retstr= _dex_psock(myinfo,"{}")) != 0 ) |
|
|
{ |
|
|
{ |
|
@ -2234,7 +2269,16 @@ struct basilisk_swap *basilisk_thread_start(struct supernet_info *myinfo,bits256 |
|
|
free(retstr); |
|
|
free(retstr); |
|
|
} |
|
|
} |
|
|
} else basilisk_psockinit(myinfo,swap,0,0); |
|
|
} else basilisk_psockinit(myinfo,swap,0,0); |
|
|
|
|
|
if ( reinit != 0 ) |
|
|
|
|
|
{ |
|
|
|
|
|
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)basilisk_swaploop,(void *)swap) != 0 ) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
myinfo->swaps[myinfo->numswaps++] = swap; |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
starttime = (uint32_t)time(NULL); |
|
|
starttime = (uint32_t)time(NULL); |
|
|
printf("statebits.%x m.%d n.%d\n",statebits,m,n); |
|
|
printf("statebits.%x m.%d n.%d\n",statebits,m,n); |
|
|
while ( statebits == 0 && m <= n/2 && time(NULL) < starttime+BASILISK_MSGDURATION ) |
|
|
while ( statebits == 0 && m <= n/2 && time(NULL) < starttime+BASILISK_MSGDURATION ) |
|
@ -2297,6 +2341,7 @@ struct basilisk_swap *basilisk_thread_start(struct supernet_info *myinfo,bits256 |
|
|
} else printf("%u/%u offer wasnt accepted statebits.%d m.%d n.%d\n",rp->requestid,rp->quoteid,statebits,m,n); |
|
|
} else printf("%u/%u offer wasnt accepted statebits.%d m.%d n.%d\n",rp->requestid,rp->quoteid,statebits,m,n); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
portable_mutex_unlock(&myinfo->DEX_swapmutex); |
|
|
portable_mutex_unlock(&myinfo->DEX_swapmutex); |
|
|
return(swap); |
|
|
return(swap); |
|
|
} |
|
|
} |
|
|