Browse Source

Make instantdex.json robust

etomic
jl777 7 years ago
parent
commit
9111b47740
  1. 4
      iguana/exchanges/LP_commands.c
  2. 123
      iguana/exchanges/LP_instantdex.c
  3. 3
      iguana/exchanges/LP_nativeDEX.c
  4. 2
      iguana/exchanges/LP_ordermatch.c
  5. 2
      iguana/exchanges/LP_signatures.c
  6. 22
      iguana/exchanges/LP_statemachine.c

4
iguana/exchanges/LP_commands.c

@ -157,7 +157,7 @@ bot_settings(botid, newprice, newvolume)\n\
bot_status(botid)\n\ bot_status(botid)\n\
bot_stop(botid)\n\ bot_stop(botid)\n\
bot_pause(botid)\n\ bot_pause(botid)\n\
instantdex_deposit(weeks, amount, broadcast=0)\n\ instantdex_deposit(weeks, amount, broadcast=1)\n\
instantdex_claim()\n\ instantdex_claim()\n\
\"}")); \"}"));
//sell(base, rel, price, basevolume, timeout=10, duration=3600)\n\ //sell(base, rel, price, basevolume, timeout=10, duration=3600)\n\
@ -208,7 +208,7 @@ instantdex_claim()\n\
{ {
if ( jint(argjson,"weeks") <= 0 || jdouble(argjson,"amount") < 10. ) if ( jint(argjson,"weeks") <= 0 || jdouble(argjson,"amount") < 10. )
return(clonestr("{\"error\":\"instantdex_deposit needs to have weeks and amount\"}")); return(clonestr("{\"error\":\"instantdex_deposit needs to have weeks and amount\"}"));
else return(LP_instantdex_deposit(ptr,juint(argjson,"weeks"),jdouble(argjson,"amount"),jint(argjson,"broadcast"))); else return(LP_instantdex_deposit(ptr,juint(argjson,"weeks"),jdouble(argjson,"amount"),jobj(argjson,"broadcast") != 0 ? jint(argjson,"broadcast") : 1));
} }
return(clonestr("{\"error\":\"cant find KMD\"}")); return(clonestr("{\"error\":\"cant find KMD\"}"));
} }

123
iguana/exchanges/LP_instantdex.c

@ -25,16 +25,11 @@ void LP_instantdex_txidaddfname(char *fname,char *afname)
sprintf(afname,"%s/instantdex_append.json",GLOBAL_DBDIR); sprintf(afname,"%s/instantdex_append.json",GLOBAL_DBDIR);
} }
cJSON *LP_instantdex_txids() cJSON *LP_instantdex_txids(int32_t appendonly)
{ {
char *filestr,fname[1024],afname[1024]; long fsize; cJSON *retjson=0; char *filestr,fname[1024],afname[1024]; long fsize; cJSON *retjson=0;
LP_instantdex_txidaddfname(fname,afname); LP_instantdex_txidaddfname(fname,afname);
if ( (filestr= OS_filestr(&fsize,afname)) != 0 ) if ( (filestr= OS_filestr(&fsize,appendonly != 0 ? afname : fname)) != 0 )
{
retjson = cJSON_Parse(filestr);
free(filestr);
}
else if ( (filestr= OS_filestr(&fsize,fname)) != 0 )
{ {
retjson = cJSON_Parse(filestr); retjson = cJSON_Parse(filestr);
free(filestr); free(filestr);
@ -55,25 +50,89 @@ void LP_instantdex_filewrite(int32_t appendfile,cJSON *array)
} }
} }
void LP_instantdex_txidadd(bits256 txid) void LP_instantdex_deposituniq(FILE *fp,bits256 txid)
{ {
cJSON *array; int32_t i,n; int32_t i,n; bits256 prevtxid;
if ( (array= LP_instantdex_txids()) == 0 ) n = (int32_t)(ftell(fp) / sizeof(txid));
array = cJSON_CreateArray(); for (i=0; i<n; i++)
if ( (n= cJSON_GetArraySize(array)) >= 0 ) {
fseek(fp,sizeof(prevtxid) * i,SEEK_SET);
fread(&prevtxid,1,sizeof(prevtxid),fp);
if ( bits256_cmp(prevtxid,txid) == 0 )
break;
}
if ( i == n )
fwrite(&txid,1,sizeof(txid),fp);
else fseek(fp,n * sizeof(txid),SEEK_SET);
}
void LP_instantdex_filescreate()
{
char fname[512]; FILE *fp; char coinaddr[64]; bits256 txid; int32_t i,n; cJSON *array,*newarray,*txobj;
sprintf(fname,"%s/deposits",GLOBAL_DBDIR), OS_compatible_path(fname);
if ( (fp= fopen(fname,"rb")) != 0 )
{ {
array = cJSON_CreateArray();
newarray = cJSON_CreateArray();
fseek(fp,0,SEEK_END);
n = (int32_t)(ftell(fp) / sizeof(txid));
bitcoin_address(coinaddr,0,60,G.LP_myrmd160,20);
for (i=0; i<n; i++) for (i=0; i<n; i++)
if ( bits256_cmp(jbits256i(array,i),txid) == 0 )
break;
if ( i == n )
{ {
fseek(fp,sizeof(txid) * i,SEEK_SET);
fread(&txid,1,sizeof(txid),fp);
jaddibits256(array,txid); jaddibits256(array,txid);
LP_instantdex_filewrite(0,array); if ( (txobj= LP_gettxout("KMD",coinaddr,txid,2)) != 0 )
LP_instantdex_filewrite(1,array); free_json(txobj);
else continue;
jaddibits256(newarray,txid);
} }
} fclose(fp);
if ( array != 0 ) LP_instantdex_filewrite(0,newarray);
free_json(newarray);
LP_instantdex_filewrite(1,array);
free_json(array); free_json(array);
}
}
void LP_instantdex_depositadd(bits256 txid)
{
static FILE *depositsfp;
char fname[512],coinaddr[64]; cJSON *array,*txobj; int32_t i,n,iter;
if ( depositsfp == 0 )
{
sprintf(fname,"%s/deposits",GLOBAL_DBDIR), OS_compatible_path(fname);
if ( (depositsfp= fopen(fname,"rb+")) == 0 )
{
depositsfp = fopen(fname,"wb+");
bitcoin_address(coinaddr,0,60,G.LP_myrmd160,20);
for (iter=0; iter<2; iter++)
{
if ((array= LP_instantdex_txids(iter)) != 0 )
{
if ( (n= cJSON_GetArraySize(array)) > 0 )
{
for (i=0; i<n; i++)
{
txid = jbits256i(array,i);
if ( (txobj= LP_gettxout("KMD",coinaddr,txid,2)) != 0 )
free_json(txobj);
else continue;
LP_instantdex_deposituniq(depositsfp,txid);
}
}
free_json(array);
}
}
} else fseek(depositsfp,0,SEEK_END);
}
if ( depositsfp != 0 )
{
LP_instantdex_deposituniq(depositsfp,txid);
fflush(depositsfp);
}
LP_instantdex_filescreate();
} }
int32_t LP_deposit_addr(char *p2shaddr,uint8_t *script,uint8_t taddr,uint8_t p2shtype,uint32_t timestamp,uint8_t *pubsecp33) int32_t LP_deposit_addr(char *p2shaddr,uint8_t *script,uint8_t taddr,uint8_t p2shtype,uint32_t timestamp,uint8_t *pubsecp33)
@ -132,8 +191,6 @@ char *LP_instantdex_deposit(struct iguana_info *coin,int32_t weeks,double amount
if ( (obj= jobj(retjson,"complete")) != 0 && is_cJSON_True(obj) != 0 && (hexstr= jstr(retjson,"hex")) != 0 ) if ( (obj= jobj(retjson,"complete")) != 0 && is_cJSON_True(obj) != 0 && (hexstr= jstr(retjson,"hex")) != 0 )
{ {
txid = jbits256(retjson,"txid"); txid = jbits256(retjson,"txid");
if ( bits256_nonz(txid) != 0 )
LP_instantdex_txidadd(txid);
if ( broadcast != 0 ) if ( broadcast != 0 )
{ {
if (bits256_nonz(txid) != 0 ) if (bits256_nonz(txid) != 0 )
@ -150,6 +207,7 @@ char *LP_instantdex_deposit(struct iguana_info *coin,int32_t weeks,double amount
{ {
jaddstr(retjson,"result","success"); jaddstr(retjson,"result","success");
jaddbits256(retjson,"broadcast",sendtxid); jaddbits256(retjson,"broadcast",sendtxid);
LP_instantdex_depositadd(txid);
free(retstr); free(retstr);
return(jprint(retjson,1)); return(jprint(retjson,1));
} }
@ -271,7 +329,7 @@ int32_t LP_claim_submit(void *ctx,cJSON *txids,int64_t *sump,struct iguana_info
char *LP_instantdex_claim(struct iguana_info *coin) char *LP_instantdex_claim(struct iguana_info *coin)
{ {
static void *ctx; static void *ctx; static int32_t firsttime = 1;
int32_t i,n; cJSON *array,*txids,*newarray,*retjson; int64_t sum; bits256 utxotxid; int32_t i,n; cJSON *array,*txids,*newarray,*retjson; int64_t sum; bits256 utxotxid;
if ( ctx == 0 ) if ( ctx == 0 )
ctx = bitcoin_ctx(); ctx = bitcoin_ctx();
@ -280,7 +338,7 @@ char *LP_instantdex_claim(struct iguana_info *coin)
sum = 0; sum = 0;
txids = cJSON_CreateArray(); txids = cJSON_CreateArray();
newarray = cJSON_CreateArray(); newarray = cJSON_CreateArray();
if ( (array= LP_instantdex_txids()) != 0 ) if ( (array= LP_instantdex_txids(firsttime)) != 0 )
{ {
printf("claiming from.(%s)\n",jprint(array,0)); printf("claiming from.(%s)\n",jprint(array,0));
if ( (n= cJSON_GetArraySize(array)) > 0 ) if ( (n= cJSON_GetArraySize(array)) > 0 )
@ -295,6 +353,7 @@ char *LP_instantdex_claim(struct iguana_info *coin)
} }
free_json(array); free_json(array);
} }
firsttime = 0;
if ( cJSON_GetArraySize(newarray) > 0 ) if ( cJSON_GetArraySize(newarray) > 0 )
LP_instantdex_filewrite(0,newarray); LP_instantdex_filewrite(0,newarray);
free_json(newarray); free_json(newarray);
@ -315,8 +374,6 @@ int64_t LP_instantdex_credit(int32_t dispflag,char *coinaddr,int64_t satoshis,in
{ {
ap->instantdex_credits += satoshis; ap->instantdex_credits += satoshis;
ap->didinstantdex = 1; ap->didinstantdex = 1;
if ( strcmp(coinaddr,coin->smartaddr) == 0 )
LP_instantdex_txidadd(txid);
if ( dispflag != 0 ) if ( dispflag != 0 )
printf("InstantDEX credit.(%s) %.8f weeki.%d (%s) -> sum %.8f\n",coinaddr,dstr(satoshis),weeki,p2shaddr,dstr(ap->instantdex_credits)); printf("InstantDEX credit.(%s) %.8f weeki.%d (%s) -> sum %.8f\n",coinaddr,dstr(satoshis),weeki,p2shaddr,dstr(ap->instantdex_credits));
return(satoshis); return(satoshis);
@ -420,7 +477,7 @@ int64_t LP_dynamictrust(bits256 pubkey,int64_t kmdvalue)
int64_t LP_instantdex_proofcheck(char *coinaddr,cJSON *proof,int32_t num) int64_t LP_instantdex_proofcheck(char *coinaddr,cJSON *proof,int32_t num)
{ {
uint8_t rmd160[20],addrtype; int32_t i; char othersmartaddr[64]; struct iguana_info *coin; struct LP_address *ap = 0; uint8_t rmd160[20],addrtype; int32_t i,j; bits256 prevtxid,txid; char othersmartaddr[64]; struct iguana_info *coin; struct LP_address *ap = 0;
if ( (coin= LP_coinfind("KMD")) != 0 ) if ( (coin= LP_coinfind("KMD")) != 0 )
{ {
bitcoin_addr2rmd160(0,&addrtype,rmd160,coinaddr); bitcoin_addr2rmd160(0,&addrtype,rmd160,coinaddr);
@ -429,7 +486,17 @@ int64_t LP_instantdex_proofcheck(char *coinaddr,cJSON *proof,int32_t num)
{ {
ap->instantdex_credits = 0; ap->instantdex_credits = 0;
for (i=0; i<num; i++) for (i=0; i<num; i++)
LP_instantdex_creditcalc(coin,1,jbits256i(proof,i),othersmartaddr); {
txid = jbits256i(proof,i);
for (j=0; j<i; j++)
{
prevtxid = jbits256i(proof,j);
if ( bits256_cmp(prevtxid,txid) == 0 )
break;
}
if ( j == i )
LP_instantdex_creditcalc(coin,1,txid,othersmartaddr);
}
ap->didinstantdex = 1; ap->didinstantdex = 1;
if ( ap->instantdex_credits > 0 ) if ( ap->instantdex_credits > 0 )
printf("validated instantdex %s.[%d] proof.(%s) credits %.8f\n",othersmartaddr,num,jprint(proof,0),dstr(ap->instantdex_credits)); printf("validated instantdex %s.[%d] proof.(%s) credits %.8f\n",othersmartaddr,num,jprint(proof,0),dstr(ap->instantdex_credits));
@ -443,7 +510,7 @@ int64_t LP_myzcredits()
cJSON *proof; struct iguana_info *coin; int64_t zcredits; cJSON *proof; struct iguana_info *coin; int64_t zcredits;
if ( (coin= LP_coinfind("KMD")) != 0 ) if ( (coin= LP_coinfind("KMD")) != 0 )
{ {
if ( (proof= LP_instantdex_txids()) != 0 ) if ( (proof= LP_instantdex_txids(0)) != 0 )
{ {
zcredits = LP_instantdex_proofcheck(coin->smartaddr,proof,cJSON_GetArraySize(proof)); zcredits = LP_instantdex_proofcheck(coin->smartaddr,proof,cJSON_GetArraySize(proof));
free_json(proof); free_json(proof);

3
iguana/exchanges/LP_nativeDEX.c

@ -712,7 +712,10 @@ void LP_initcoins(void *ctx,int32_t pubsock,cJSON *coins)
{ {
LP_unspents_load(coin->symbol,coin->smartaddr); LP_unspents_load(coin->symbol,coin->smartaddr);
if ( strcmp(coin->symbol,"KMD") == 0 ) if ( strcmp(coin->symbol,"KMD") == 0 )
{
LP_importaddress("KMD",BOTS_BONDADDRESS); LP_importaddress("KMD",BOTS_BONDADDRESS);
LP_instantdex_filescreate();
}
} }
if ( coin->txfee == 0 && strcmp(coin->symbol,"BTC") != 0 ) if ( coin->txfee == 0 && strcmp(coin->symbol,"BTC") != 0 )
coin->txfee = LP_MIN_TXFEE; coin->txfee = LP_MIN_TXFEE;

2
iguana/exchanges/LP_ordermatch.c

@ -492,7 +492,7 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double
reqjson = LP_quotejson(qp); reqjson = LP_quotejson(qp);
jaddstr(reqjson,"method","connected"); jaddstr(reqjson,"method","connected");
jaddstr(reqjson,"pair",pairstr); jaddstr(reqjson,"pair",pairstr);
jadd(reqjson,"proof",LP_instantdex_txids()); jadd(reqjson,"proof",LP_instantdex_txids(0));
char str[65]; printf("BOB pubsock.%d binds to %d (%s)\n",pubsock,pair,bits256_str(str,qp->desthash)); char str[65]; printf("BOB pubsock.%d binds to %d (%s)\n",pubsock,pair,bits256_str(str,qp->desthash));
bits256 zero; bits256 zero;
memset(zero.bytes,0,sizeof(zero)); memset(zero.bytes,0,sizeof(zero));

2
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 ) if ( jobj(reqjson,"timestamp") == 0 )
jaddnum(reqjson,"timestamp",time(NULL)); jaddnum(reqjson,"timestamp",time(NULL));
if ( strcmp(method,"connect") == 0 ) if ( strcmp(method,"connect") == 0 )
jadd(reqjson,"proof",LP_instantdex_txids()); jadd(reqjson,"proof",LP_instantdex_txids(0));
msg = jprint(reqjson,1); msg = jprint(reqjson,1);
printf("QUERY.(%s)\n",msg); printf("QUERY.(%s)\n",msg);
//if ( bits256_nonz(qp->srchash) == 0 || strcmp(method,"request") != 0 ) //if ( bits256_nonz(qp->srchash) == 0 || strcmp(method,"request") != 0 )

22
iguana/exchanges/LP_statemachine.c

@ -447,6 +447,28 @@ void issue_LP_uitem(char *destip,uint16_t destport,char *symbol,char *coinaddr,b
portable_mutex_unlock(&LP_cJSONmutex); portable_mutex_unlock(&LP_cJSONmutex);
} //else printf("cJSON_unregister of unknown %p %u\n",item,item->cjsonid); } //else printf("cJSON_unregister of unknown %p %u\n",item,item->cjsonid);
}*/ }*/
void LP_instantdex_txidadd(bits256 txid)
{
cJSON *array; int32_t i,n;
if ( (array= LP_instantdex_txids()) == 0 )
array = cJSON_CreateArray();
if ( (n= cJSON_GetArraySize(array)) >= 0 )
{
for (i=0; i<n; i++)
if ( bits256_cmp(jbits256i(array,i),txid) == 0 )
break;
if ( i == n )
{
jaddibits256(array,txid);
LP_instantdex_filewrite(0,array);
LP_instantdex_filewrite(1,array);
}
}
if ( array != 0 )
free_json(array);
}
char *issue_LP_getprices(char *destip,uint16_t destport) char *issue_LP_getprices(char *destip,uint16_t destport)
{ {
char url[512]; char url[512];

Loading…
Cancel
Save