Skip to content

Commit

Permalink
Merge pull request jl777#531 from jl777/spvdex
Browse files Browse the repository at this point in the history
bugfixes
  • Loading branch information
jl777 authored Dec 10, 2017
2 parents 7ee2bab + 5de6f43 commit d9458cd
Show file tree
Hide file tree
Showing 17 changed files with 368 additions and 162 deletions.
8 changes: 7 additions & 1 deletion iguana/exchanges/LP_coins.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ uint16_t LP_userpass(char *userpass,char *symbol,char *assetname,char *confroot,

cJSON *LP_coinjson(struct iguana_info *coin,int32_t showwif)
{
struct electrum_info *ep; int32_t notarized; uint64_t balance; char wifstr[128],ipaddr[64]; uint8_t tmptype; bits256 checkkey; cJSON *item = cJSON_CreateObject();
struct electrum_info *ep; bits256 zero; int32_t notarized; uint64_t balance; char wifstr[128],ipaddr[64]; uint8_t tmptype; bits256 checkkey; cJSON *item = cJSON_CreateObject();
jaddstr(item,"coin",coin->symbol);
if ( showwif != 0 )
{
Expand Down Expand Up @@ -261,6 +261,12 @@ cJSON *LP_coinjson(struct iguana_info *coin,int32_t showwif)
jaddnum(item,"txfee",strcmp(coin->symbol,"BTC") != 0 ? coin->txfee : LP_txfeecalc(coin,0,0));
if ( strcmp(coin->symbol,"KMD") == 0 )
{
memset(zero.bytes,0,sizeof(zero));
if ( strcmp(coin->smartaddr,coin->instantdex_address) != 0 )
{
LP_instantdex_depositadd(coin->smartaddr,zero);
strcpy(coin->instantdex_address,coin->smartaddr);
}
jaddnum(item,"zcredits",dstr(LP_myzcredits()));
jadd(item,"zdebits",LP_myzdebits());
}
Expand Down
2 changes: 1 addition & 1 deletion iguana/exchanges/LP_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ instantdex_claim()\n\
double price,bid,ask;
if ( strcmp(method,"autoprice") == 0 )
{
if ( LP_autoprice(base,rel,argjson) < 0 )
if ( LP_autoprice(ctx,base,rel,argjson) < 0 )
return(clonestr("{\"error\":\"couldnt set autoprice\"}"));
else return(clonestr("{\"result\":\"success\"}"));
}
Expand Down
7 changes: 6 additions & 1 deletion iguana/exchanges/LP_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ struct iguana_info
int32_t numutxos,notarized,longestchain,firstrefht,firstscanht,lastscanht,bussock,height; uint16_t busport;
uint32_t dPoWtime,loadedcache,electrumlist,lastunspent,importedprivkey,lastpushtime,lastutxosync,addr_listunspent_requested,lastutxos,updaterate,counter,inactive,lastmempool,lastgetinfo,ratetime,heighttime,lastmonitor,obooktime;
uint8_t pubtype,p2shtype,isPoS,wiftype,wiftaddr,taddr,noimportprivkey_flag,userconfirms,isassetchain,maxconfirms;
char symbol[128],smartaddr[64],userpass[1024],serverport[128];
char symbol[128],smartaddr[64],userpass[1024],serverport[128],instantdex_address[64];
// portfolio
double price_kmd,force,perc,goal,goalperc,relvolume,rate;
void *electrum; void *ctx;
Expand Down Expand Up @@ -506,6 +506,8 @@ int32_t iguana_signrawtransaction(void *ctx,char *symbol,uint8_t wiftaddr,uint8_
//void LP_butxo_swapfields_set(struct LP_utxoinfo *butxo);
struct LP_address_utxo *LP_address_utxofind(struct iguana_info *coin,char *coinaddr,bits256 txid,int32_t vout);
int64_t LP_myzcredits();
void LP_instantdex_depositadd(char *coinaddr,bits256 txid);
int64_t LP_instantdex_creditcalc(struct iguana_info *coin,int32_t dispflag,bits256 txid,char *refaddr);
int32_t LP_destaddr(char *destaddr,cJSON *item);
int32_t LP_waitmempool(char *symbol,char *coinaddr,bits256 txid,int32_t vout,int32_t duration);
cJSON *LP_statslog_disp(uint32_t starttime,uint32_t endtime,char *refgui,bits256 refpubkey,char *refbase,char *refrel);
Expand All @@ -517,6 +519,7 @@ cJSON *LP_transactioninit(struct iguana_info *coin,bits256 txid,int32_t iter,cJS
int32_t LP_mempoolscan(char *symbol,bits256 searchtxid);
int32_t LP_txheight(struct iguana_info *coin,bits256 txid);
int32_t LP_numpeers();
double LP_CMCbtcprice(double *price_usdp,char *symbol);
char *basilisk_swapentry(uint32_t requestid,uint32_t quoteid,int32_t forceflag);
int64_t LP_KMDvalue(struct iguana_info *coin,int64_t balance);
int32_t LP_address_utxoadd(uint32_t timestamp,char *debug,struct iguana_info *coin,char *coinaddr,bits256 txid,int32_t vout,uint64_t value,int32_t height,int32_t spendheight);
Expand All @@ -527,6 +530,8 @@ cJSON *LP_gettxout(char *symbol,char *coinaddr,bits256 txid,int32_t vout);
void LP_postutxos(char *symbol,char *coinaddr);
int32_t LP_listunspent_both(char *symbol,char *coinaddr,int32_t fullflag);
uint16_t LP_randpeer(char *destip);
void LP_tradebot_pauseall();
void LP_portfolio_reset();
uint32_t LP_atomic_locktime(char *base,char *rel);
struct LP_pubkey_info *LP_pubkeyfind(bits256 pubkey);
char *issue_LP_psock(char *destip,uint16_t destport,int32_t ispaired);
Expand Down
94 changes: 52 additions & 42 deletions iguana/exchanges/LP_instantdex.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@
// marketmaker
//

void LP_instantdex_txidaddfname(char *fname,char *afname)
void LP_instantdex_txidaddfname(char *fname,char *afname,char *coinaddr)
{
sprintf(fname,"%s/instantdex.json",GLOBAL_DBDIR);
sprintf(afname,"%s/instantdex_append.json",GLOBAL_DBDIR);
if ( coinaddr == 0 || coinaddr[0] == 0 )
{
sprintf(fname,"%s/instantdex.json",GLOBAL_DBDIR);
sprintf(afname,"%s/instantdex_append.json",GLOBAL_DBDIR);
}
else
{
sprintf(fname,"%s/instantdex_%s.json",GLOBAL_DBDIR,coinaddr);
sprintf(afname,"%s/instantdex_%s_append.json",GLOBAL_DBDIR,coinaddr);
}
}

cJSON *LP_instantdex_txids(int32_t appendonly)
cJSON *LP_instantdex_txids(int32_t appendonly,char *coinaddr)
{
char *filestr,fname[1024],afname[1024]; long fsize; cJSON *retjson=0;
LP_instantdex_txidaddfname(fname,afname);
LP_instantdex_txidaddfname(fname,afname,coinaddr);
if ( (filestr= OS_filestr(&fsize,appendonly != 0 ? afname : fname)) != 0 )
{
retjson = cJSON_Parse(filestr);
Expand All @@ -37,10 +45,10 @@ cJSON *LP_instantdex_txids(int32_t appendonly)
return(retjson);
}

void LP_instantdex_filewrite(int32_t appendfile,cJSON *array)
void LP_instantdex_filewrite(int32_t appendfile,cJSON *array,char *coinaddr)
{
FILE *fp; char *filestr,fname[1024],afname[1024];
LP_instantdex_txidaddfname(fname,afname);
LP_instantdex_txidaddfname(fname,afname,coinaddr);
if ( (fp= fopen(appendfile == 0 ? fname : afname,"wb")) != 0 )
{
filestr = jprint(array,0);
Expand All @@ -60,7 +68,7 @@ void LP_instantdex_deposituniq(FILE *fp,bits256 txid)
fread(&prevtxid,1,sizeof(prevtxid),fp);
if ( bits256_cmp(prevtxid,txid) == 0 )
{
printf("%s duplicate of deposits[%d]\n",bits256_str(str,prevtxid),i);
//printf("%s duplicate of deposits[%d]\n",bits256_str(str,prevtxid),i);
break;
}
}
Expand Down Expand Up @@ -96,53 +104,51 @@ void LP_instantdex_filescreate(char *coinaddr)
jaddibits256(newarray,txid);
}
fclose(fp);
LP_instantdex_filewrite(0,newarray);
LP_instantdex_filewrite(0,newarray,coinaddr);
free_json(newarray);
LP_instantdex_filewrite(1,array);
LP_instantdex_filewrite(1,array,coinaddr);
free_json(array);
}
}

void LP_instantdex_depositadd(char *coinaddr,bits256 txid)
{
static FILE *depositsfp;
char fname[512],str[65]; bits256 prevtxid; cJSON *array,*txobj; int32_t i,n,iter;
if ( depositsfp == 0 )
FILE *fp; struct iguana_info *coin; char fname[512],*addr; bits256 prevtxid; cJSON *array; int32_t i,n,iter;
coin = LP_coinfind("KMD");
sprintf(fname,"%s/deposits.%s",GLOBAL_DBDIR,coinaddr), OS_compatible_path(fname);
if ( (fp= fopen(fname,"rb+")) == 0 )
{
sprintf(fname,"%s/deposits.%s",GLOBAL_DBDIR,coinaddr), OS_compatible_path(fname);
if ( (depositsfp= fopen(fname,"rb+")) == 0 )
if ( (fp= fopen(fname,"wb+")) != 0 )
{
depositsfp = fopen(fname,"wb+");
for (iter=0; iter<2; iter++)
for (iter=0; iter<4; iter++)
{
if ( (array= LP_instantdex_txids(iter)) != 0 )
if ( iter < 2 )
addr = coinaddr;
else addr = "";
if ( coin != 0 && (array= LP_instantdex_txids(iter&1,addr)) != 0 )
{
if ( (n= cJSON_GetArraySize(array)) > 0 )
{
for (i=0; i<n; i++)
{
prevtxid = jbits256i(array,i);
printf("instantdex iter.%d i.%d check %s\n",iter,i,bits256_str(str,prevtxid));
if ( (txobj= LP_gettxout("KMD",coinaddr,prevtxid,0)) != 0 )
free_json(txobj);
else
//char str[65]; printf("instantdex iter.%d i.%d check %s\n",iter,i,bits256_str(str,prevtxid));
if ( LP_instantdex_creditcalc(coin,0,prevtxid,coinaddr) > 0 )
{
printf("null gettxout %s %s\n",coinaddr,bits256_str(str,prevtxid));
continue;
LP_instantdex_deposituniq(fp,prevtxid);
fflush(fp);
}
LP_instantdex_deposituniq(depositsfp,prevtxid);
fflush(depositsfp);
}
}
free_json(array);
}
}
} else fseek(depositsfp,0,SEEK_END);
}
if ( depositsfp != 0 && bits256_nonz(txid) != 0 )
}
} else fseek(fp,0,SEEK_END);
if ( fp != 0 && bits256_nonz(txid) != 0 )
{
LP_instantdex_deposituniq(depositsfp,txid);
fflush(depositsfp);
LP_instantdex_deposituniq(fp,txid);
fclose(fp);
}
LP_instantdex_filescreate(coinaddr);
}
Expand Down Expand Up @@ -350,7 +356,7 @@ char *LP_instantdex_claim(struct iguana_info *coin)
sum = 0;
txids = cJSON_CreateArray();
newarray = cJSON_CreateArray();
if ( (array= LP_instantdex_txids(firsttime)) != 0 )
if ( (array= LP_instantdex_txids(firsttime,coin->smartaddr)) != 0 )
{
printf("claiming from.(%s)\n",jprint(array,0));
if ( (n= cJSON_GetArraySize(array)) > 0 )
Expand All @@ -367,7 +373,7 @@ char *LP_instantdex_claim(struct iguana_info *coin)
}
firsttime = 0;
if ( cJSON_GetArraySize(newarray) > 0 )
LP_instantdex_filewrite(0,newarray);
LP_instantdex_filewrite(0,newarray,coin->smartaddr);
free_json(newarray);
retjson = cJSON_CreateObject();
jaddstr(retjson,"result","success");
Expand Down Expand Up @@ -480,15 +486,18 @@ int64_t LP_dynamictrust(int64_t credits,bits256 pubkey,int64_t kmdvalue)
credits = ap->instantdex_credits;
if ( credits != 0 && (swaps_kmdvalue+kmdvalue) > credits )
{
DL_FOREACH_SAFE(pubp->bobswaps,ptr,tmp)
if ( 0 )
{
if ( (sp= ptr->swap) != 0 && LP_swap_finished(sp,1) == 0 )
printf("unfinished bob %llu r%u-r%u src.%s %.8f dest.%s %.8f -> price %.8f value %.8f\n",(long long)sp->aliceid,sp->Q.R.requestid,sp->Q.R.quoteid,sp->Q.srccoin,dstr(sp->Q.satoshis),sp->Q.destcoin,dstr(sp->Q.destsatoshis),(double)sp->Q.destsatoshis/(sp->Q.satoshis+1),dstr(LP_kmdvalue(sp->Q.destcoin,sp->Q.destsatoshis)));
}
DL_FOREACH_SAFE(pubp->aliceswaps,ptr,tmp)
{
if ( (sp= ptr->swap) != 0 && LP_swap_finished(sp,1) == 0 )
printf("unfinished alice %llu r%u-r%u src.%s %.8f dest.%s %.8f -> price %.8f value %.8f\n",(long long)sp->aliceid,sp->Q.R.requestid,sp->Q.R.quoteid,sp->Q.srccoin,dstr(sp->Q.satoshis),sp->Q.destcoin,dstr(sp->Q.destsatoshis),(double)sp->Q.destsatoshis/(sp->Q.satoshis+1),dstr(LP_kmdvalue(sp->Q.destcoin,sp->Q.destsatoshis)));
DL_FOREACH_SAFE(pubp->bobswaps,ptr,tmp)
{
if ( (sp= ptr->swap) != 0 && LP_swap_finished(sp,1) == 0 )
printf("unfinished bob %llu r%u-r%u src.%s %.8f dest.%s %.8f -> price %.8f value %.8f\n",(long long)sp->aliceid,sp->Q.R.requestid,sp->Q.R.quoteid,sp->Q.srccoin,dstr(sp->Q.satoshis),sp->Q.destcoin,dstr(sp->Q.destsatoshis),(double)sp->Q.destsatoshis/(sp->Q.satoshis+1),dstr(LP_kmdvalue(sp->Q.destcoin,sp->Q.destsatoshis)));
}
DL_FOREACH_SAFE(pubp->aliceswaps,ptr,tmp)
{
if ( (sp= ptr->swap) != 0 && LP_swap_finished(sp,1) == 0 )
printf("unfinished alice %llu r%u-r%u src.%s %.8f dest.%s %.8f -> price %.8f value %.8f\n",(long long)sp->aliceid,sp->Q.R.requestid,sp->Q.R.quoteid,sp->Q.srccoin,dstr(sp->Q.satoshis),sp->Q.destcoin,dstr(sp->Q.destsatoshis),(double)sp->Q.destsatoshis/(sp->Q.satoshis+1),dstr(LP_kmdvalue(sp->Q.destcoin,sp->Q.destsatoshis)));
}
}
printf("REJECT: %s instantdex_credits %.8f vs (%.8f + current %.8f)\n",coinaddr,dstr(credits),dstr(swaps_kmdvalue),dstr(kmdvalue));
}
Expand All @@ -506,6 +515,7 @@ int64_t LP_instantdex_proofcheck(char *coinaddr,cJSON *proof,int32_t num)
{
bitcoin_addr2rmd160(0,&addrtype,rmd160,coinaddr);
bitcoin_address(othersmartaddr,0,60,rmd160,20);
printf("proofcheck addrtype.%d (%s) -> %s\n",addrtype,coinaddr,othersmartaddr);
if ((ap= LP_address(coin,othersmartaddr)) != 0 )
{
ap->instantdex_credits = 0;
Expand Down Expand Up @@ -535,7 +545,7 @@ int64_t LP_myzcredits()
cJSON *proof; struct iguana_info *coin; int64_t zcredits;
if ( (coin= LP_coinfind("KMD")) != 0 )
{
if ( (proof= LP_instantdex_txids(0)) != 0 )
if ( (proof= LP_instantdex_txids(0,coin->smartaddr)) != 0 )
{
zcredits = LP_instantdex_proofcheck(coin->smartaddr,proof,cJSON_GetArraySize(proof));
free_json(proof);
Expand Down
17 changes: 10 additions & 7 deletions iguana/exchanges/LP_nativeDEX.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
// LP_nativeDEX.c
// marketmaker
//
// fundvalue -> autoprice, cmc +margin -> autoprice, signals -> autoprice
// ordermatch pricing error
// https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki for signing BCH/BTG
// there is an issue about waiting for notarization for a swap that never starts
// use electrum in case of addr change in swap
//
// compress packets
// portfolio to set prices from historical
// portfolio value based on ask?
//
// else claim path
// swap memleak?
//
// WONTFIX:
// dPoW security -> 4: KMD notarized, 5: BTC notarized, after next notary elections
// bigendian architectures need to use little endian for sighash calcs
// improve critical section detection when parallel trades
// use electrum in case of addr change in swap
// locktime claiming on sporadic assetchains
// there is an issue about waiting for notarization for a swap that never starts (expiration ok)

#include <stdio.h>

Expand Down Expand Up @@ -1158,9 +1158,12 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
exit(-1);
}
LP_initcoins(ctx,pubsock,coinsjson);

G.waiting = 1;
LP_passphrase_init(passphrase,jstr(argjson,"gui"));
//char coinaddr[64]; bits256 zero;
//bitcoin_address(coinaddr,0,60,G.LP_myrmd160,20);
//memset(zero.bytes,0,sizeof(zero));
//LP_instantdex_depositadd(coinaddr,zero);
#ifndef FROM_JS
if ( IAMLP != 0 && OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_psockloop,(void *)myipaddr) != 0 )
{
Expand Down
5 changes: 3 additions & 2 deletions iguana/exchanges/LP_ordermatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ struct LP_utxoinfo *LP_address_myutxopair(struct LP_utxoinfo *butxo,int32_t iamb

int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double price,struct LP_quoteinfo *qp)
{
char pairstr[512],otheraddr[64]; cJSON *reqjson; bits256 privkey; int32_t pair=-1,retval = -1,DEXselector = 0; struct basilisk_swap *swap; struct iguana_info *coin;
char pairstr[512],otheraddr[64]; cJSON *reqjson; bits256 privkey; int32_t pair=-1,retval = -1,DEXselector = 0; struct basilisk_swap *swap; struct iguana_info *coin,*kmdcoin;
qp->quotetime = (uint32_t)time(NULL);
if ( (coin= LP_coinfind(qp->srccoin)) == 0 )
{
Expand Down Expand Up @@ -475,7 +475,8 @@ 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_txids(0));
if ( (kmdcoin= LP_coinfind("KMD")) != 0 )
jadd(reqjson,"proof",LP_instantdex_txids(0,kmdcoin->smartaddr));
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));
Expand Down
Loading

0 comments on commit d9458cd

Please sign in to comment.