From f48ac00635dd371f86e4cabd76139b4693de91c9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 1 Dec 2017 14:52:19 +0400 Subject: [PATCH] Test --- iguana/exchanges/LP_commands.c | 4 +- iguana/exchanges/LP_instantdex.c | 156 ++++++++++++++++++----------- iguana/exchanges/LP_ordermatch.c | 2 +- iguana/exchanges/LP_signatures.c | 2 +- iguana/exchanges/LP_statemachine.c | 59 +++++++++++ iguana/exchanges/claim | 2 +- 6 files changed, 158 insertions(+), 67 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index e7a6fa96b..776a50dbf 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -212,9 +212,7 @@ zeroconf_claim(address, expiration=0)\n\ { if ( (ptr= LP_coinsearch("KMD")) != 0 ) { - if ( jstr(argjson,"address") == 0 ) - return(clonestr("{\"error\":\"deposit_claim needs to have address\"}")); - else return(LP_instantdex_claim(ptr,jstr(argjson,"address"),juint(argjson,"expiration"))); + return(LP_instantdex_claim(ptr,jstr(argjson,"address"),juint(argjson,"expiration"))); } return(clonestr("{\"error\":\"cant find KMD\"}")); } diff --git a/iguana/exchanges/LP_instantdex.c b/iguana/exchanges/LP_instantdex.c index ba8cb6c4a..bd95eca1a 100644 --- a/iguana/exchanges/LP_instantdex.c +++ b/iguana/exchanges/LP_instantdex.c @@ -19,15 +19,16 @@ // marketmaker // -void LP_instantdex_txidaddfname(char *fname) +void LP_instantdex_txidaddfname(char *fname,char *afname) { sprintf(fname,"%s/instantdex.json",GLOBAL_DBDIR); + sprintf(fname,"%s/instantdex_append.json",GLOBAL_DBDIR); } -cJSON *LP_instantdex_txidaddjson() +cJSON *LP_instantdex_txids() { - char *filestr,fname[1024]; long fsize; cJSON *retjson=0; - LP_instantdex_txidaddfname(fname); + char *filestr,fname[1024],afname[1024]; long fsize; cJSON *retjson=0; + LP_instantdex_txidaddfname(fname,afname); if ( (filestr= OS_filestr(&fsize,fname)) != 0 ) { retjson = cJSON_Parse(filestr); @@ -36,10 +37,23 @@ cJSON *LP_instantdex_txidaddjson() return(retjson); } +void LP_instantdex_filewrite(int32_t appendfile,cJSON *array) +{ + FILE *fp; char *filestr,fname[1024],afname[1024]; + LP_instantdex_txidaddfname(fname,afname); + if ( (fp= fopen(appendfile == 0 ? fname : afname,"wb")) != 0 ) + { + filestr = jprint(array,0); + fwrite(filestr,1,strlen(filestr)+1,fp); + fclose(fp); + free(filestr); + } +} + void LP_instantdex_txidadd(bits256 txid) { - cJSON *array; int32_t i,n; char fname[1024],*filestr; FILE *fp; - if ( (array= LP_instantdex_txidaddjson()) == 0 ) + cJSON *array; int32_t i,n; + if ( (array= LP_instantdex_txids()) == 0 ) array = cJSON_CreateArray(); if ( (n= cJSON_GetArraySize(array)) >= 0 ) { @@ -48,16 +62,9 @@ void LP_instantdex_txidadd(bits256 txid) break; if ( i == n ) { - LP_instantdex_txidaddfname(fname); - char str[65]; printf("add %s -> %s\n",bits256_str(str,txid),fname); jaddibits256(array,txid); - if ( (fp= fopen(fname,"wb")) != 0 ) - { - filestr = jprint(array,0); - fwrite(filestr,1,strlen(filestr)+1,fp); - fclose(fp); - free(filestr); - } + LP_instantdex_filewrite(0,array); + LP_instantdex_filewrite(1,array); } } if ( array != 0 ) @@ -80,8 +87,8 @@ char *LP_instantdex_deposit(struct iguana_info *coin,int32_t weeks,double amount return(clonestr("{\"error\":\"instantdex deposit must be in KMD\"}")); if ( amount < 10.0 ) return(clonestr("{\"error\":\"minimum instantdex deposit is 10 KMD\"}")); - if ( weeks < 0 || weeks > 52 ) - return(clonestr("{\"error\":\"weeks must be between 0 and 52\"}")); + if ( weeks <= 0 || weeks > 52 ) + return(clonestr("{\"error\":\"weeks must be between 1 and 52\"}")); if ( weeks > 0 ) { timestamp = (uint32_t)time(NULL); @@ -169,10 +176,32 @@ char *LP_instantdex_deposit(struct iguana_info *coin,int32_t weeks,double amount return(clonestr("{\"error\":\"error with LP_withdraw for instantdex deposit\"}")); } +int64_t LP_claimtx(void *ctx,struct iguana_info *coin,cJSON *txids,bits256 utxotxid,int32_t utxovout,uint64_t satoshis,char *vinaddr,uint32_t claimtime,uint8_t *redeemscript,int32_t redeemlen) +{ + uint8_t userdata[2]; char *signedtx; bits256 signedtxid,sendtxid; int32_t userdatalen; int64_t destamount,sum = 0; + userdata[0] = 0x51; + userdatalen = 1; + utxovout = 0; + char str[65]; printf("satoshis %.8f %s/v%d\n",dstr(satoshis),bits256_str(str,utxotxid),utxovout); + if ( (signedtx= basilisk_swap_bobtxspend(&signedtxid,10000,"instantdexclaim",coin->symbol,coin->wiftaddr,coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,ctx,G.LP_privkey,0,redeemscript,redeemlen,userdata,userdatalen,utxotxid,utxovout,coin->smartaddr,G.LP_pubsecp,0,claimtime,&destamount,0,0,vinaddr,1,coin->zcash)) != 0 ) + { + printf("signedtx.(%s)\n",signedtx); + sendtxid = LP_broadcast("claim","KMD",signedtx,signedtxid); + if ( bits256_cmp(sendtxid,signedtxid) == 0 ) + { + jaddibits256(txids,sendtxid); + sum += (satoshis - coin->txfee); + } + else printf("error sending %s\n",bits256_str(str,signedtxid)); + free(signedtx); + } else printf("error claiming instantdex deposit %s/v%d %.8f\n",bits256_str(str,utxotxid),utxovout,dstr(satoshis)); + return(sum); +} + char *LP_instantdex_claim(struct iguana_info *coin,char *depositaddr,uint32_t expiration) { static void *ctx; - uint8_t redeemscript[512],userdata[64]; char vinaddr[64],str[65],*signedtx=0; uint32_t timestamp,now,redeemlen,claimtime; int32_t i,n,height,utxovout,userdatalen; bits256 signedtxid,utxotxid,sendtxid,zero; int64_t sum,destamount,satoshis; cJSON *array,*item,*txids,*retjson; + uint8_t redeemscript[512]; char vinaddr[64],checkaddr[64],destaddr[64]; uint32_t now,redeemlen,claimtime; int32_t i,n,flagi,flag,weeki,numvouts,utxovout; bits256 utxotxid; int64_t sum,satoshis,weeksatoshis; cJSON *array,*txids,*retjson,*newarray,*txjson,*vouts,*vout0,*vout1,*vout2; if ( ctx == 0 ) ctx = bitcoin_ctx(); if ( strcmp(coin->symbol,"KMD") != 0 ) @@ -180,64 +209,69 @@ char *LP_instantdex_claim(struct iguana_info *coin,char *depositaddr,uint32_t ex now = (uint32_t)time(NULL); sum = 0; txids = cJSON_CreateArray(); - timestamp = (now / LP_WEEKMULT) * LP_WEEKMULT + LP_WEEKMULT; - while ( timestamp > LP_FIRSTWEEKTIME ) + newarray = cJSON_CreateArray(); + if ( (array= LP_instantdex_txids()) != 0 ) { - if ( expiration != 0 ) - timestamp = expiration; - else timestamp -= LP_WEEKMULT; - redeemlen = LP_deposit_addr(vinaddr,redeemscript,coin->taddr,coin->p2shtype,timestamp,G.LP_pubsecp); - if ( strcmp(depositaddr,vinaddr) == 0 ) + if ( (n= cJSON_GetArraySize(array)) > 0 ) { - claimtime = (uint32_t)time(NULL)-777; - if ( claimtime <= timestamp ) - { - printf("claimtime.%u vs locktime.%u, need to wait %d seconds\n",claimtime,timestamp,(int32_t)timestamp-claimtime); - } - else + flag = 0; + for (i=0; isymbol,vinaddr,zero,zero)) != 0 ) + utxotxid = jbits256i(array,i); + if ( flag == 1 ) { - userdata[0] = 0x51; - userdatalen = 1; - utxovout = 0; - //printf("unspents.(%s)\n",jprint(array,0)); - if ( (n= cJSON_GetArraySize(array)) > 0 ) + jaddibits256(newarray,utxotxid); + continue; + } + flagi = 0; + utxovout = 0; + if ( (txjson= LP_gettx(coin->symbol,utxotxid,1)) != 0 ) + { + if ( (vouts= jarray(&numvouts,txjson,"vout")) != 0 && numvouts == 3 ) { - for (i=0; ismartaddr) == 0 ) { - item = jitem(array,i); - satoshis = LP_listunspent_parseitem(coin,&utxotxid,&utxovout,&height,item); - printf("satoshis %.8f %s/v%d\n",dstr(satoshis),bits256_str(str,utxotxid),utxovout); - if ( (signedtx= basilisk_swap_bobtxspend(&signedtxid,10000,"instantdexclaim",coin->symbol,coin->wiftaddr,coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,coin->wiftype,ctx,G.LP_privkey,0,redeemscript,redeemlen,userdata,userdatalen,utxotxid,utxovout,coin->smartaddr,G.LP_pubsecp,0,claimtime,&destamount,0,0,vinaddr,1,coin->zcash)) != 0 ) + redeemlen = LP_deposit_addr(checkaddr,redeemscript,coin->taddr,coin->p2shtype,expiration,G.LP_pubsecp); + if ( strcmp(checkaddr,vinaddr) == 0 ) { - printf("signedtx.(%s)\n",signedtx); - sendtxid = LP_broadcast("claim","KMD",signedtx,signedtxid); - if ( bits256_cmp(sendtxid,signedtxid) == 0 ) + claimtime = (uint32_t)time(NULL)-777; + if ( claimtime <= expiration ) + { + printf("claimtime.%u vs locktime.%u, need to wait %d seconds\n",claimtime,expiration,(int32_t)expiration-claimtime); + } + else { - jaddibits256(txids,sendtxid); - sum += (satoshis-coin->txfee); + flagi = 1; + sum += LP_claimtx(ctx,coin,txids,utxotxid,utxovout,satoshis,vinaddr,claimtime,redeemscript,redeemlen); } - else printf("error sending %s\n",bits256_str(str,signedtxid)); - free(signedtx); - } else printf("error claiming instantdex deposit %s/v%d %.8f\n",bits256_str(str,utxotxid),utxovout,dstr(satoshis)); + } } } - free_json(array); - retjson = cJSON_CreateObject(); - jaddstr(retjson,"result","success"); - jaddnum(retjson,"claimed",dstr(sum)); - jadd(retjson,"txids",txids); - return(jprint(retjson,1)); } + if ( flagi == 0 ) + jaddibits256(newarray,utxotxid); + else flag++; } } - if ( expiration != 0 ) - break; + free_json(array); } - return(clonestr("{\"error\":\"no instantdex deposits to claim\"}")); + if ( cJSON_GetArraySize(newarray) > 0 ) + LP_instantdex_filewrite(0,newarray); + free_json(newarray); + retjson = cJSON_CreateObject(); + jaddstr(retjson,"result","success"); + jaddnum(retjson,"claimed",dstr(sum)); + jadd(retjson,"txids",txids); + return(jprint(retjson,1)); } int64_t LP_instantdex_credit(int32_t dispflag,char *coinaddr,int64_t satoshis,int32_t weeki,char *p2shaddr,bits256 txid) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index a2019c925..15d8f959d 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -487,7 +487,7 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double reqjson = LP_quotejson(qp); jaddstr(reqjson,"method","connected"); jaddstr(reqjson,"pair",pairstr); - jadd(reqjson,"proof",LP_instantdex_txidaddjson()); + jadd(reqjson,"proof",LP_instantdex_txids()); char str[65]; printf("BOB pubsock.%d binds to %d (%s)\n",pubsock,pair,bits256_str(str,qp->desthash)); bits256 zero; memset(zero.bytes,0,sizeof(zero)); diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index c73986f46..4fce8ca7f 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -665,7 +665,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ if ( jobj(reqjson,"timestamp") == 0 ) jaddnum(reqjson,"timestamp",time(NULL)); if ( strcmp(method,"connect") == 0 ) - jadd(reqjson,"proof",LP_instantdex_txidaddjson()); + jadd(reqjson,"proof",LP_instantdex_txids()); msg = jprint(reqjson,1); printf("QUERY.(%s)\n",msg); //if ( bits256_nonz(qp->srchash) == 0 || strcmp(method,"request") != 0 ) diff --git a/iguana/exchanges/LP_statemachine.c b/iguana/exchanges/LP_statemachine.c index 21b8eeaf7..34549ebf7 100644 --- a/iguana/exchanges/LP_statemachine.c +++ b/iguana/exchanges/LP_statemachine.c @@ -3540,6 +3540,65 @@ void LP_price_broadcastloop(void *ctx) sleep(LP_ORDERBOOK_DURATION * .9); } } +/*if ( expiration != 0 ) + { + redeemlen = LP_deposit_addr(vinaddr,redeemscript,coin->taddr,coin->p2shtype,expiration,G.LP_pubsecp); + if ( strcmp(depositaddr,vinaddr) == 0 ) + { + claimtime = (uint32_t)time(NULL)-777; + if ( claimtime <= expiration ) + { + printf("claimtime.%u vs locktime.%u, need to wait %d seconds\n",claimtime,timestamp,(int32_t)timestamp-claimtime); + return(clonestr("{\"error\":\"need to wait to claim\"}")); + } + sum += LP_claimtx(ctx,coin,txids,utxotxid,utxovout,satoshis,vinaddr,claimtime,redeemscript,redeemlen); + + }*/ +/*timestamp = (now / LP_WEEKMULT) * LP_WEEKMULT + LP_WEEKMULT; + while ( timestamp > LP_FIRSTWEEKTIME ) + { + if ( expiration != 0 ) + timestamp = expiration; + else timestamp -= LP_WEEKMULT; + redeemlen = LP_deposit_addr(vinaddr,redeemscript,coin->taddr,coin->p2shtype,timestamp,G.LP_pubsecp); + if ( strcmp(depositaddr,vinaddr) == 0 ) + { + claimtime = (uint32_t)time(NULL)-777; + if ( claimtime <= timestamp ) + { + printf("claimtime.%u vs locktime.%u, need to wait %d seconds\n",claimtime,timestamp,(int32_t)timestamp-claimtime); + } + else + { + printf("found %s at timestamp.%u\n",vinaddr,timestamp); + memset(zero.bytes,0,sizeof(zero)); + if ( (array= LP_listunspent(coin->symbol,vinaddr,zero,zero)) != 0 ) + { + //printf("unspents.(%s)\n",jprint(array,0)); + if ( (n= cJSON_GetArraySize(array)) > 0 ) + { + for (i=0; i