Browse Source

test

release/v0.1
jl777 9 years ago
parent
commit
eeb158ebb0
  1. 2
      iguana/exchanges777.h
  2. 22
      iguana/iguana_instantdex.c
  3. 75
      iguana/swaps/iguana_BTCswap.c

2
iguana/exchanges777.h

@ -161,7 +161,7 @@ struct bitcoin_eventitem { struct queueitem DL; cJSON *argjson,*newjson; int32_t
struct bitcoin_swapinfo struct bitcoin_swapinfo
{ {
struct bitcoin_swapinfo *next,*prev; struct bitcoin_swapinfo *next,*prev; portable_mutex_t mutex;
queue_t eventsQ; struct bitcoin_eventitem *pollevent; queue_t eventsQ; struct bitcoin_eventitem *pollevent;
bits256 privkeys[INSTANTDEX_DECKSIZE+2],mypubs[2],otherpubs[2],privAm,pubAm,privBn,pubBn; bits256 privkeys[INSTANTDEX_DECKSIZE+2],mypubs[2],otherpubs[2],privAm,pubAm,privBn,pubBn;
bits256 myorderhash,otherorderhash,mypubkey,othertrader,bothorderhash; bits256 myorderhash,otherorderhash,mypubkey,othertrader,bothorderhash;

22
iguana/iguana_instantdex.c

@ -1142,10 +1142,11 @@ struct bitcoin_swapinfo *bitcoin_swapinit(struct supernet_info *myinfo,struct ex
{ {
struct bitcoin_swapinfo *swap = 0; struct iguana_info *coinbtc,*altcoin; int32_t i; struct bitcoin_swapinfo *swap = 0; struct iguana_info *coinbtc,*altcoin; int32_t i;
swap = calloc(1,sizeof(struct bitcoin_swapinfo)); swap = calloc(1,sizeof(struct bitcoin_swapinfo));
portable_mutex_init(&swap->mutex);
swap->state = instantdex_statefind(BTC_states,BTC_numstates,statename); swap->state = instantdex_statefind(BTC_states,BTC_numstates,statename);
swap->mine = *myap, swap->other = *otherap; swap->mine = *myap, swap->other = *otherap;
swap->expiration = (otherap->offer.expiration < myap->offer.expiration) ? otherap->offer.expiration : myap->offer.expiration; swap->expiration = (otherap->offer.expiration < myap->offer.expiration) ? otherap->offer.expiration : myap->offer.expiration;
swap->choosei = swap->otherchoosei = -1; swap->choosei = swap->otherchoosei = -1;
strcpy(swap->status,"pending"); strcpy(swap->status,"pending");
vcalc_sha256(0,swap->myorderhash.bytes,(void *)&swap->mine.offer,sizeof(swap->mine.offer)); vcalc_sha256(0,swap->myorderhash.bytes,(void *)&swap->mine.offer,sizeof(swap->mine.offer));
vcalc_sha256(0,swap->otherorderhash.bytes,(void *)&swap->other.offer,sizeof(swap->other.offer)); vcalc_sha256(0,swap->otherorderhash.bytes,(void *)&swap->other.offer,sizeof(swap->other.offer));
@ -1219,6 +1220,7 @@ char *instantdex_checkoffer(struct supernet_info *myinfo,int32_t *addedp,uint64_
} }
isbob = ap->offer.myside; isbob = ap->offer.myside;
swap = bitcoin_swapinit(myinfo,exchange,ap,otherap,1,argjson,isbob != 0 ? "BOB_sentoffer" : "ALICE_sentoffer"); swap = bitcoin_swapinit(myinfo,exchange,ap,otherap,1,argjson,isbob != 0 ? "BOB_sentoffer" : "ALICE_sentoffer");
portable_mutex_lock(&swap->mutex);
//printf("ISBOB.%d vs %d\n",isbob,instantdex_isbob(swap)); //printf("ISBOB.%d vs %d\n",isbob,instantdex_isbob(swap));
if ( swap != 0 ) if ( swap != 0 )
{ {
@ -1232,6 +1234,7 @@ char *instantdex_checkoffer(struct supernet_info *myinfo,int32_t *addedp,uint64_
retstr = instantdex_sendcmd(myinfo,&swap->mine.offer,newjson,"BTCoffer",GENESIS_PUBKEY,INSTANTDEX_HOPS,swap->deck,sizeof(swap->deck),0); retstr = instantdex_sendcmd(myinfo,&swap->mine.offer,newjson,"BTCoffer",GENESIS_PUBKEY,INSTANTDEX_HOPS,swap->deck,sizeof(swap->deck),0);
free_json(newjson); free_json(newjson);
} else printf("error creating statemachine\n"); } else printf("error creating statemachine\n");
portable_mutex_unlock(&swap->mutex);
} }
return(retstr); return(retstr);
} }
@ -1263,18 +1266,20 @@ char *instantdex_gotoffer(struct supernet_info *myinfo,struct exchange_info *exc
if ( myap->offer.expiration < (time(NULL) + INSTANTDEX_DURATION) || otherap->offer.expiration < (time(NULL) + INSTANTDEX_DURATION) ) if ( myap->offer.expiration < (time(NULL) + INSTANTDEX_DURATION) || otherap->offer.expiration < (time(NULL) + INSTANTDEX_DURATION) )
return(clonestr("{\"error\":\"instantdex_BTCswap offer too close to expiration\"}")); return(clonestr("{\"error\":\"instantdex_BTCswap offer too close to expiration\"}"));
isbob = myap->offer.myside; isbob = myap->offer.myside;
if ( (swap= bitcoin_swapinit(myinfo,exchange,myap,otherap,0,argjson,isbob != 0 ? "BOB_gotoffer" : "ALICE_gotoffer")) == 0 ) swap = bitcoin_swapinit(myinfo,exchange,myap,otherap,0,argjson,isbob != 0 ? "BOB_gotoffer" : "ALICE_gotoffer");
if ( swap == 0 )
{
return(clonestr("{\"error\":\"couldnt allocate statemachine\"}")); return(clonestr("{\"error\":\"couldnt allocate statemachine\"}"));
}
//printf("ISBOB.%d vs %d\n",isbob,instantdex_isbob(swap)); //printf("ISBOB.%d vs %d\n",isbob,instantdex_isbob(swap));
if ( (newjson= instantdex_parseargjson(myinfo,exchange,swap,argjson,1)) == 0 ) if ( (newjson= instantdex_parseargjson(myinfo,exchange,swap,argjson,1)) == 0 )
{ {
printf("error parsing argjson\n"); printf("error parsing argjson\n");
return(clonestr("{\"error\":\"instantdex_BTCswap offer null newjson\"}")); return(clonestr("{\"error\":\"instantdex_BTCswap offer null newjson\"}"));
} }
else //if ( (retstr= instantdex_addfeetx(myinfo,newjson,ap,swap,"BOB_gotoffer","ALICE_gotoffer")) == 0 ) else
{ {
printf("create statemachine isbob.%d\n",isbob); printf("create statemachine isbob.%d\n",isbob);
//queue_enqueue("acceptableQ",&exchange->acceptableQ,&swap->DL,0);
if ( (retstr= instantdex_choosei(swap,newjson,argjson,serdata,serdatalen)) != 0 ) if ( (retstr= instantdex_choosei(swap,newjson,argjson,serdata,serdatalen)) != 0 )
{ {
free_json(newjson); free_json(newjson);
@ -1315,7 +1320,9 @@ char *instantdex_parse(struct supernet_info *myinfo,struct instantdex_msghdr *ms
A.offer.minperc = 100; A.offer.minperc = 100;
if ( (swap= instantdex_statemachinefind(myinfo,exchange,A.orderid)) != 0 ) if ( (swap= instantdex_statemachinefind(myinfo,exchange,A.orderid)) != 0 )
{ {
printf("found existing state machine %llx\n",(long long)A.orderid); if ( swap->cutverified == 0 && swap->choosei >= 0 && serdatalen == sizeof(swap->privkeys) )
instantdex_privkeyextract(myinfo,swap,serdata,serdatalen);
printf("found existing state machine %llx choosei.%d other.%d\n",(long long)A.orderid,swap->choosei,swap->otherchoosei);
newjson = instantdex_parseargjson(myinfo,exchange,swap,argjson,0); newjson = instantdex_parseargjson(myinfo,exchange,swap,argjson,0);
if ( serdatalen == sizeof(swap->otherdeck) && swap->choosei < 0 && (retstr= instantdex_choosei(swap,newjson,argjson,serdata,serdatalen)) != 0 ) if ( serdatalen == sizeof(swap->otherdeck) && swap->choosei < 0 && (retstr= instantdex_choosei(swap,newjson,argjson,serdata,serdatalen)) != 0 )
{ {
@ -1336,7 +1343,10 @@ char *instantdex_parse(struct supernet_info *myinfo,struct instantdex_msghdr *ms
{ {
if ( instantdex_statemachinefind(myinfo,exchange,ap->orderid) == 0 && instantdex_historyfind(myinfo,exchange,ap->orderid) == 0 && instantdex_statemachinefind(myinfo,exchange,A.orderid) == 0 && instantdex_historyfind(myinfo,exchange,A.orderid) == 0 ) if ( instantdex_statemachinefind(myinfo,exchange,ap->orderid) == 0 && instantdex_historyfind(myinfo,exchange,ap->orderid) == 0 && instantdex_statemachinefind(myinfo,exchange,A.orderid) == 0 && instantdex_historyfind(myinfo,exchange,A.orderid) == 0 )
{ {
if ( (retstr= instantdex_gotoffer(myinfo,exchange,ap,&A,msg,argjson,remoteaddr,signerbits,serdata,serdatalen)) != 0 ) // adds to statemachine if no error portable_mutex_lock(&swap->mutex);
retstr = instantdex_gotoffer(myinfo,exchange,ap,&A,msg,argjson,remoteaddr,signerbits,serdata,serdatalen);
portable_mutex_unlock(&swap->mutex);
if ( retstr != 0 ) // adds to statemachine if no error
{ {
printf("from GOTOFFER.(%s)\n",retstr); printf("from GOTOFFER.(%s)\n",retstr);
return(retstr); return(retstr);

75
iguana/swaps/iguana_BTCswap.c

@ -484,7 +484,7 @@ char *instantdex_choosei(struct bitcoin_swapinfo *swap,cJSON *newjson,cJSON *arg
} }
else else
{ {
printf("invalid datalen.%d vs %d\n",datalen,(int32_t)sizeof(swap->deck)); printf("choosei.%d or null serdata.%p or invalid datalen.%d vs %d\n",swap->choosei,serdata,datalen,(int32_t)sizeof(swap->deck));
return(clonestr("{\"error\":\"instantdex_BTCswap offer no cut\"}")); return(clonestr("{\"error\":\"instantdex_BTCswap offer no cut\"}"));
} }
} }
@ -554,8 +554,11 @@ int32_t instantdex_swaptxupdate(struct bitcoin_statetx **ptrp,cJSON *argjson,cha
{ {
if ( *ptrp != 0 ) if ( *ptrp != 0 )
{ {
printf("got replacement %s?\n",txname); if ( strcmp((*ptrp)->txbytes,str) != 0 )
free(*ptrp); {
printf("got replacement %s?\n",txname);
free(*ptrp);
} else return(0);
} else printf("instantdex_swaptxupdate got (%s) %s\n",txname,str); } else printf("instantdex_swaptxupdate got (%s) %s\n",txname,str);
*ptrp = calloc(1,sizeof(**ptrp) + strlen(str) + 1); *ptrp = calloc(1,sizeof(**ptrp) + strlen(str) + 1);
strcpy((*ptrp)->txbytes,str); strcpy((*ptrp)->txbytes,str);
@ -612,10 +615,32 @@ cJSON *instantdex_parseargjson(struct supernet_info *myinfo,struct exchange_info
swap->havestate |= instantdex_swaptxupdate(&swap->otherfee,argjson,"feetx","feetxid"); swap->havestate |= instantdex_swaptxupdate(&swap->otherfee,argjson,"feetx","feetxid");
if ( swap->otherchoosei < 0 && jobj(argjson,"mychoosei") != 0 ) if ( swap->otherchoosei < 0 && jobj(argjson,"mychoosei") != 0 )
{ {
//printf("otherschoosei.%d\n",swap->otherschoosei); printf("otherschoosei.%d\n",swap->otherchoosei);
if ( (swap->otherchoosei= juint(argjson,"mychoosei")) >= sizeof(swap->otherdeck)/sizeof(*swap->otherdeck) ) if ( (swap->otherchoosei= juint(argjson,"mychoosei")) >= sizeof(swap->otherdeck)/sizeof(*swap->otherdeck) )
swap->otherchoosei = -1; swap->otherchoosei = -1;
} }
if ( swap->otherchoosei >= 0 )
{
char str[65],str2[65];
if ( instantdex_isbob(swap) != 0 )
{
if ( bits256_nonz(swap->pubAm) == 0 )
swap->pubAm = jbits256(argjson,"pubAm");
else if ( bits256_cmp(swap->pubAm,jbits256(argjson,"pubAm")) != 0 )
{
printf("mismatched pubAm %s vs %s\n",bits256_str(str,swap->pubAm),bits256_str(str2,jbits256(argjson,"pubAm")));
}
}
else
{
if ( bits256_nonz(swap->pubBn) == 0 )
swap->pubBn = jbits256(argjson,"pubBn");
else if ( bits256_cmp(swap->pubBn,jbits256(argjson,"pubBn")) != 0 )
{
printf("mismatched pubBn %s vs %s\n",bits256_str(str,swap->pubBn),bits256_str(str2,jbits256(argjson,"pubBn")));
}
}
}
if ( juint(argjson,"verified") != 0 ) if ( juint(argjson,"verified") != 0 )
swap->otherverifiedcut = 1; swap->otherverifiedcut = 1;
if ( juint(argjson,"have") != 0 ) if ( juint(argjson,"have") != 0 )
@ -661,7 +686,7 @@ cJSON *BTC_waitdeckCfunc(struct supernet_info *myinfo,struct exchange_info *exch
cJSON *BTC_waitprivCfunc(struct supernet_info *myinfo,struct exchange_info *exchange,struct bitcoin_swapinfo *swap,cJSON *argjson,cJSON *newjson,uint8_t **serdatap,int32_t *serdatalenp) cJSON *BTC_waitprivCfunc(struct supernet_info *myinfo,struct exchange_info *exchange,struct bitcoin_swapinfo *swap,cJSON *argjson,cJSON *newjson,uint8_t **serdatap,int32_t *serdatalenp)
{ {
strcmp(swap->expectedcmdstr,"BTCprivC"); strcmp(swap->expectedcmdstr,"BTCprivC");
//printf("call privkey extract from serdatalen.%d\n",*serdatalenp); printf("call privkey extract from serdatalen.%d\n",*serdatalenp);
instantdex_privkeyextract(myinfo,swap,*serdatap,*serdatalenp); instantdex_privkeyextract(myinfo,swap,*serdatap,*serdatalenp);
*serdatap = 0, *serdatalenp = 0; *serdatap = 0, *serdatalenp = 0;
return(newjson); return(newjson);
@ -719,7 +744,7 @@ cJSON *BOB_waitaltconfirmfunc(struct supernet_info *myinfo,struct exchange_info
*serdatap = 0, *serdatalenp = 0; *serdatap = 0, *serdatalenp = 0;
strcpy(swap->waitfortx,"alt"); strcpy(swap->waitfortx,"alt");
//reftime = (uint32_t)(ap->offer.expiration - INSTANTDEX_LOCKTIME*2); //reftime = (uint32_t)(ap->offer.expiration - INSTANTDEX_LOCKTIME*2);
if ( altcoin != 0 && swap->altpayment != 0 && (retstr= BTC_txconfirmed(myinfo,altcoin,swap,newjson,swap->altpayment->txid,&swap->altpayment->numconfirms,"altfound",altcoin->chain->minconfirms)) != 0 ) if ( altcoin != 0 && swap->altpayment != 0 && swap->otherchoosei >= 0 && (retstr= BTC_txconfirmed(myinfo,altcoin,swap,newjson,swap->altpayment->txid,&swap->altpayment->numconfirms,"altfound",altcoin->chain->minconfirms)) != 0 )
{ {
if ( swap->payment != 0 || (swap->payment= instantdex_bobtx(myinfo,swap,altcoin,swap->mypubs[1],swap->otherpubs[0],swap->privkeys[swap->otherchoosei],swap->reftime,swap->BTCsatoshis,0)) != 0 ) if ( swap->payment != 0 || (swap->payment= instantdex_bobtx(myinfo,swap,altcoin,swap->mypubs[1],swap->otherpubs[0],swap->privkeys[swap->otherchoosei],swap->reftime,swap->BTCsatoshis,0)) != 0 )
{ {
@ -1105,24 +1130,20 @@ char *instantdex_statemachine(struct instantdex_stateinfo *states,int32_t numsta
if ( jstr(newjson,"virtevent") != 0 ) if ( jstr(newjson,"virtevent") != 0 )
{ {
printf("VIRTEVENT.(%s)\n",jstr(newjson,"virtevent")); printf("VIRTEVENT.(%s)\n",jstr(newjson,"virtevent"));
//if ( (ptr= instantdex_event(jstr(newjson,"virtevent"),argjson,newjson,0,0)) != 0 ) for (i=0; i<state->numevents; i++)
{ if ( strcmp(jstr(newjson,"virtevent"),state->events[i].cmdstr) == 0 )
//queue_enqueue("eventQ",&swap->eventsQ,&ptr->DL,0);
for (i=0; i<state->numevents; i++)
if ( strcmp(jstr(newjson,"virtevent"),state->events[i].cmdstr) == 0 )
{
cmdstr = state->events[i].cmdstr;
break;
}
if ( i == state->numevents )
{
printf("error cant find.(%s)\n",jstr(newjson,"virtevent"));
return(clonestr("{\"error\":\"instantdex_statemachine: unexpected virtevent\"}"));
}
else
{ {
//printf("found.%d event.%s -> %s next.%d\n",i,state->events[i].cmdstr,states[state->events[i].nextstateind].name,state->events[i].nextstateind); cmdstr = state->events[i].cmdstr;
break;
} }
if ( i == state->numevents )
{
printf("error cant find.(%s)\n",jstr(newjson,"virtevent"));
return(clonestr("{\"error\":\"instantdex_statemachine: unexpected virtevent\"}"));
}
else
{
//printf("found.%d event.%s -> %s next.%d\n",i,state->events[i].cmdstr,states[state->events[i].nextstateind].name,state->events[i].nextstateind);
} }
} }
if ( state->events[i].sendcmd[0] != 0 ) if ( state->events[i].sendcmd[0] != 0 )
@ -1138,12 +1159,12 @@ char *instantdex_statemachine(struct instantdex_stateinfo *states,int32_t numsta
} }
if ( bits256_nonz(swap->pubAm) != 0 ) if ( bits256_nonz(swap->pubAm) != 0 )
jaddbits256(newjson,"pubAm",swap->pubAm); jaddbits256(newjson,"pubAm",swap->pubAm);
if ( bits256_nonz(swap->privAm) != 0 ) //if ( bits256_nonz(swap->privAm) != 0 )
jaddbits256(newjson,"privAm",swap->privAm); // jaddbits256(newjson,"privAm",swap->privAm);
if ( bits256_nonz(swap->pubBn) != 0 ) if ( bits256_nonz(swap->pubBn) != 0 )
jaddbits256(newjson,"pubBn",swap->pubBn); jaddbits256(newjson,"pubBn",swap->pubBn);
if ( bits256_nonz(swap->privBn) != 0 ) //if ( bits256_nonz(swap->privBn) != 0 )
jaddbits256(newjson,"privn",swap->privBn); // jaddbits256(newjson,"privn",swap->privBn);
if ( instantdex_isbob(swap) == 0 ) if ( instantdex_isbob(swap) == 0 )
{ {
if ( (swap->otherhavestate & INSTANTDEX_ORDERSTATE_HAVEALTPAYMENT) == 0 && swap->altpayment != 0 && jobj(newjson,"altpayment") == 0 ) if ( (swap->otherhavestate & INSTANTDEX_ORDERSTATE_HAVEALTPAYMENT) == 0 && swap->altpayment != 0 && jobj(newjson,"altpayment") == 0 )
@ -1173,7 +1194,7 @@ char *instantdex_statemachine(struct instantdex_stateinfo *states,int32_t numsta
} }
} }
jaddnum(newjson,"have",swap->havestate); jaddnum(newjson,"have",swap->havestate);
//printf("i.%d (%s) %s %s.%d -> %s.%d send.(%s) %p\n",i,jprint(newjson,0),cmdstr,swap->state->name,state->ind,states[state->events[i].nextstateind].name,state->events[i].nextstateind,state->events[i].sendcmd,&states[state->events[i].nextstateind]); printf("i.%d (%s) %s %s.%d -> %s.%d send.(%s) %p\n",i,jprint(newjson,0),cmdstr,swap->state->name,state->ind,states[state->events[i].nextstateind].name,state->events[i].nextstateind,state->events[i].sendcmd,&states[state->events[i].nextstateind]);
swap->state = &states[state->events[i].nextstateind]; swap->state = &states[state->events[i].nextstateind];
return(instantdex_sendcmd(myinfo,&swap->mine.offer,newjson,state->events[i].sendcmd,swap->othertrader,INSTANTDEX_HOPS,serdata,serdatalen,0)); return(instantdex_sendcmd(myinfo,&swap->mine.offer,newjson,state->events[i].sendcmd,swap->othertrader,INSTANTDEX_HOPS,serdata,serdatalen,0));
} else return(clonestr("{\"error\":\"instantdex_statemachine: illegal state\"}")); } else return(clonestr("{\"error\":\"instantdex_statemachine: illegal state\"}"));

Loading…
Cancel
Save