Browse Source

Merge pull request #482 from jl777/spvdex

reduce memory usage fixed
etomic
jl777 7 years ago
committed by GitHub
parent
commit
659888e28a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      crypto777/cJSON.c
  2. 8
      iguana/exchanges/LP_include.h
  3. 6
      iguana/exchanges/LP_nativeDEX.c
  4. 33
      iguana/exchanges/LP_ordermatch.c
  5. 2
      iguana/exchanges/LP_signatures.c
  6. 6
      iguana/exchanges/LP_utxo.c

4
crypto777/cJSON.c

@ -56,8 +56,8 @@ static int32_t cJSON_strcasecmp(const char *s1,const char *s2)
void *LP_alloc(uint64_t len); void *LP_alloc(uint64_t len);
void LP_free(void *ptr); void LP_free(void *ptr);
static void *(*cJSON_malloc)(size_t sz) = (void *)LP_alloc; static void *(*cJSON_malloc)(size_t sz) = (void *)malloc;//LP_alloc;
static void (*cJSON_free)(void *ptr) = LP_free; static void (*cJSON_free)(void *ptr) = free;//LP_free;
static void *cJSON_mallocstr(int32_t len) static void *cJSON_mallocstr(int32_t len)
{ {

8
iguana/exchanges/LP_include.h

@ -23,7 +23,9 @@
#define LP_MAJOR_VERSION "0" #define LP_MAJOR_VERSION "0"
#define LP_MINOR_VERSION "1" #define LP_MINOR_VERSION "1"
#define LP_BUILD_NUMBER "15000" #define LP_BUILD_NUMBER "15096"
#define LP_BARTERDEX_VERSION 1
#define LP_MAGICBITS 8
#ifdef FROM_JS #ifdef FROM_JS
#include <emscripten.h> #include <emscripten.h>
@ -38,8 +40,6 @@ void emscripten_usleep(int32_t x); // returns immediate, no sense for sleeping
#endif #endif
//#define LP_STRICTPEERS //#define LP_STRICTPEERS
#define LP_BARTERDEX_VERSION 1
#define LP_MAGICBITS 8
#define LP_MAXVINS 64 #define LP_MAXVINS 64
#define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL) #define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL)
@ -180,7 +180,7 @@ struct basilisk_request
int64_t srcamount,unused; // 16 to 31 int64_t srcamount,unused; // 16 to 31
bits256 srchash; // 32 to 63 bits256 srchash; // 32 to 63
bits256 desthash; bits256 desthash;
char src[65],dest[65]; char src[68],dest[68];
uint64_t destamount; uint64_t destamount;
int32_t optionhours,DEXselector; int32_t optionhours,DEXselector;
}; };

6
iguana/exchanges/LP_nativeDEX.c

@ -1043,7 +1043,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
printf("error launching LP_reserved_msgs for (%s)\n",myipaddr); printf("error launching LP_reserved_msgs for (%s)\n",myipaddr);
exit(-1); exit(-1);
} }
if ( 0 && OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)utxosQ_loop,(void *)myipaddr) != 0 ) if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)utxosQ_loop,(void *)myipaddr) != 0 )
{ {
printf("error launching utxosQ_loop for (%s)\n",myipaddr); printf("error launching utxosQ_loop for (%s)\n",myipaddr);
exit(-1); exit(-1);
@ -1216,7 +1216,7 @@ int32_t zeroval() { return(0); }
void *LP_alloc(uint64_t len) void *LP_alloc(uint64_t len)
{ {
return(calloc(1,len)); //return(calloc(1,len));
LP_cjson_allocated += len; LP_cjson_allocated += len;
LP_cjson_total += len; LP_cjson_total += len;
LP_cjson_count++; LP_cjson_count++;
@ -1235,7 +1235,7 @@ return(calloc(1,len));
void LP_free(void *ptr) void LP_free(void *ptr)
{ {
static uint32_t lasttime,unknown; static int64_t lasttotal; static uint32_t lasttime,unknown; static int64_t lasttotal;
free(ptr); return; //free(ptr); return;
uint32_t now; char str[65]; int32_t n,lagging; uint64_t total = 0; struct LP_memory_list *mp,*tmp; uint32_t now; char str[65]; int32_t n,lagging; uint64_t total = 0; struct LP_memory_list *mp,*tmp;
if ( (now= (uint32_t)time(NULL)) > lasttime+1 ) if ( (now= (uint32_t)time(NULL)) > lasttime+1 )
{ {

33
iguana/exchanges/LP_ordermatch.c

@ -126,7 +126,7 @@ double LP_quote_validate(struct LP_utxoinfo *autxo,struct LP_utxoinfo *butxo,str
{ {
if ( LP_iseligible(&srcvalue,&srcvalue2,1,qp->srccoin,qp->txid,qp->vout,qp->satoshis,qp->txid2,qp->vout2) == 0 ) if ( LP_iseligible(&srcvalue,&srcvalue2,1,qp->srccoin,qp->txid,qp->vout,qp->satoshis,qp->txid2,qp->vout2) == 0 )
{ {
printf("bob not eligible %s (%.8f %.8f)\n",jprint(LP_quotejson(qp),1),dstr(srcvalue),dstr(srcvalue2)); //printf("bob not eligible %s (%.8f %.8f)\n",jprint(LP_quotejson(qp),1),dstr(srcvalue),dstr(srcvalue2));
return(-2); return(-2);
} }
if ( (txout= LP_gettxout(qp->srccoin,qp->coinaddr,qp->txid,qp->vout)) != 0 ) if ( (txout= LP_gettxout(qp->srccoin,qp->coinaddr,qp->txid,qp->vout)) != 0 )
@ -159,7 +159,7 @@ double LP_quote_validate(struct LP_utxoinfo *autxo,struct LP_utxoinfo *butxo,str
if ( LP_iseligible(&destvalue,&destvalue2,0,qp->destcoin,qp->desttxid,qp->destvout,qp->destsatoshis,qp->feetxid,qp->feevout) == 0 ) if ( LP_iseligible(&destvalue,&destvalue2,0,qp->destcoin,qp->desttxid,qp->destvout,qp->destsatoshis,qp->feetxid,qp->feevout) == 0 )
{ {
//alice not eligible 0.36893923 -> dest 0.55020000 1.49130251 (0.61732249 0.00104324) 14b8b74808d2d34a70e5eddd1cad47d855858f8b23cac802576d4d37b5f8af8f/v1 abec6e76169bcb738235ca67fab02cc55390f39e422aa71f1badf8747c290cc4/v1 //alice not eligible 0.36893923 -> dest 0.55020000 1.49130251 (0.61732249 0.00104324) 14b8b74808d2d34a70e5eddd1cad47d855858f8b23cac802576d4d37b5f8af8f/v1 abec6e76169bcb738235ca67fab02cc55390f39e422aa71f1badf8747c290cc4/v1
char str[65],str2[65]; printf("alice not eligible %.8f -> dest %.8f %.8f (%.8f %.8f) %s/v%d %s/v%d\n",dstr(qp->satoshis),dstr(qp->destsatoshis),(double)qp->destsatoshis/qp->satoshis,dstr(destvalue),dstr(destvalue2),bits256_str(str,qp->desttxid),qp->destvout,bits256_str(str2,qp->feetxid),qp->feevout); //char str[65],str2[65]; printf("alice not eligible %.8f -> dest %.8f %.8f (%.8f %.8f) %s/v%d %s/v%d\n",dstr(qp->satoshis),dstr(qp->destsatoshis),(double)qp->destsatoshis/qp->satoshis,dstr(destvalue),dstr(destvalue2),bits256_str(str,qp->desttxid),qp->destvout,bits256_str(str2,qp->feetxid),qp->feevout);
return(-3); return(-3);
} }
if ( (txout= LP_gettxout(qp->destcoin,qp->destaddr,qp->desttxid,qp->destvout)) != 0 ) if ( (txout= LP_gettxout(qp->destcoin,qp->destaddr,qp->desttxid,qp->destvout)) != 0 )
@ -321,7 +321,7 @@ int32_t LP_nearest_utxovalue(struct iguana_info *coin,char *coinaddr,struct LP_a
} }
if ( replacei >= 0 ) if ( replacei >= 0 )
{ {
printf("REPLACE bestdist %.8f height %d with dist %.8f height %d\n",dstr(bestdist),bestup->U.height,dstr(utxos[replacei]->U.value - targetval),utxos[replacei]->U.height); //printf("REPLACE bestdist %.8f height %d with dist %.8f height %d\n",dstr(bestdist),bestup->U.height,dstr(utxos[replacei]->U.value - targetval),utxos[replacei]->U.height);
return(replacei); return(replacei);
} }
} }
@ -396,7 +396,7 @@ struct LP_utxoinfo *LP_address_myutxopair(struct LP_utxoinfo *butxo,int32_t iamb
{ {
if ( (m= LP_address_utxo_ptrs(coin,iambob,utxos,max,ap,coinaddr)) > 1 ) if ( (m= LP_address_utxo_ptrs(coin,iambob,utxos,max,ap,coinaddr)) > 1 )
{ {
if ( 1 ) if ( 0 )
{ {
int32_t i; int32_t i;
for (i=0; i<m; i++) for (i=0; i<m; i++)
@ -502,10 +502,11 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q
memset(qp->txid.bytes,0,sizeof(qp->txid)); memset(qp->txid.bytes,0,sizeof(qp->txid));
qp->txid2 = qp->txid; qp->txid2 = qp->txid;
qp->aliceid = LP_aliceid_calc(qp->desttxid,qp->destvout,qp->feetxid,qp->feevout); qp->aliceid = LP_aliceid_calc(qp->desttxid,qp->destvout,qp->feetxid,qp->feevout);
qp->tradeid = tradeid; if ( (qp->tradeid= tradeid) == 0 )
qp->tradeid = LP_rand();
LP_query(ctx,myipaddr,mypubsock,"request",qp); LP_query(ctx,myipaddr,mypubsock,"request",qp);
LP_Alicequery = *qp, LP_Alicemaxprice = maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = destpubkey; LP_Alicequery = *qp, LP_Alicemaxprice = maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = destpubkey;
char str[65]; printf("LP_trade %s/%s %.8f vol %.8f dest.(%s)\n",qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey)); char str[65]; printf("LP_trade %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f\n",qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),maxprice);
return(LP_recent_swaps(0)); return(LP_recent_swaps(0));
} }
@ -532,9 +533,9 @@ void LP_alicequery_clear()
Alice_expiration = 0; Alice_expiration = 0;
} }
int32_t LP_alice_eligible() int32_t LP_alice_eligible(uint32_t quotetime)
{ {
if ( Alice_expiration != 0 && time(NULL) > Alice_expiration ) if ( Alice_expiration != 0 && quotetime > Alice_expiration )
{ {
printf("time expired for Alice_request\n"); printf("time expired for Alice_request\n");
LP_alicequery_clear(); LP_alicequery_clear();
@ -545,7 +546,7 @@ int32_t LP_alice_eligible()
void LP_reserved(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *qp) void LP_reserved(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *qp)
{ {
double price=0.,maxprice = LP_Alicemaxprice; double price=0.,maxprice = LP_Alicemaxprice;
if ( LP_alice_eligible() > 0 && LP_quotecmp(qp,&LP_Alicequery) == 0 ) if ( LP_quotecmp(qp,&LP_Alicequery) == 0 )
{ {
price = LP_pricecache(qp,qp->srccoin,qp->destcoin,qp->txid,qp->vout); price = LP_pricecache(qp,qp->srccoin,qp->destcoin,qp->txid,qp->vout);
if ( LP_pricevalid(price) > 0 && maxprice > SMALLVAL && price <= maxprice ) if ( LP_pricevalid(price) > 0 && maxprice > SMALLVAL && price <= maxprice )
@ -555,7 +556,7 @@ void LP_reserved(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo
printf("send CONNECT\n"); printf("send CONNECT\n");
LP_query(ctx,myipaddr,mypubsock,"connect",qp); LP_query(ctx,myipaddr,mypubsock,"connect",qp);
} else printf("LP_reserved price %.8f vs maxprice %.8f\n",price,maxprice*1.005); } else printf("LP_reserved price %.8f vs maxprice %.8f\n",price,maxprice*1.005);
} else printf("probably a timeout, reject reserved due to not eligible.%d or mismatched quote price %.8f vs maxprice %.8f\n",LP_alice_eligible(),price,maxprice); } else printf("probably a timeout, reject reserved due to not eligible.%d or mismatched quote price %.8f vs maxprice %.8f\n",LP_alice_eligible(qp->quotetime),price,maxprice);
} }
char *LP_connectedalice(cJSON *argjson) // alice char *LP_connectedalice(cJSON *argjson) // alice
@ -778,7 +779,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
if ( strcmp(method,"reserved") == 0 ) if ( strcmp(method,"reserved") == 0 )
{ {
bestprice = LP_bob_competition(&counter,aliceid,qprice,1); bestprice = LP_bob_competition(&counter,aliceid,qprice,1);
//printf("aliceid.%llu price %.8f -> bestprice %.8f\n",(long long)aliceid,qprice,bestprice); //printf("%s lag %ld: aliceid.%llu price %.8f -> bestprice %.8f Alice max %.8f\n",jprint(argjson,0),Q.quotetime - (time(NULL)-20),(long long)aliceid,qprice,bestprice,LP_Alicemaxprice);
if ( LP_Alicemaxprice == 0. ) if ( LP_Alicemaxprice == 0. )
return(retval); return(retval);
if ( bits256_nonz(LP_Alicedestpubkey) != 0 ) if ( bits256_nonz(LP_Alicedestpubkey) != 0 )
@ -789,8 +790,9 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
return(retval); return(retval);
} else printf("got reserved response from destpubkey %s\n",bits256_str(str,Q.srchash)); } else printf("got reserved response from destpubkey %s\n",bits256_str(str,Q.srchash));
} }
if ( bits256_cmp(G.LP_mypub25519,Q.desthash) == 0 && bits256_cmp(G.LP_mypub25519,Q.srchash) != 0 && LP_alice_eligible() > 0 ) if ( bits256_cmp(G.LP_mypub25519,Q.desthash) == 0 && bits256_cmp(G.LP_mypub25519,Q.srchash) != 0 && Q.quotetime > time(NULL)-20 && LP_alice_eligible(Q.quotetime) > 0 )
{ {
printf("alice %s received RESERVED.(%s)\n",bits256_str(str,G.LP_mypub25519),jprint(argjson,0));
if ( (qprice= LP_quote_validate(autxo,butxo,&Q,0)) <= SMALLVAL ) if ( (qprice= LP_quote_validate(autxo,butxo,&Q,0)) <= SMALLVAL )
{ {
printf("reserved quote validate error %.0f\n",qprice); printf("reserved quote validate error %.0f\n",qprice);
@ -807,7 +809,6 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
return(retval); return(retval);
} }
LP_aliceid(Q.tradeid,Q.aliceid,"reserved",0,0); LP_aliceid(Q.tradeid,Q.aliceid,"reserved",0,0);
//printf("alice %s received RESERVED.(%s)\n",bits256_str(str,G.LP_mypub25519),jprint(argjson,0));
if ( (retstr= LP_quotereceived(argjson)) != 0 ) if ( (retstr= LP_quotereceived(argjson)) != 0 )
free(retstr); free(retstr);
LP_reserved(ctx,myipaddr,pubsock,&Q); LP_reserved(ctx,myipaddr,pubsock,&Q);
@ -904,6 +905,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
Q.txid2 = butxo->deposit.txid; Q.txid2 = butxo->deposit.txid;
Q.vout2 = butxo->deposit.vout; Q.vout2 = butxo->deposit.vout;
Q.satoshis = butxo->S.satoshis; Q.satoshis = butxo->S.satoshis;
Q.quotetime = (uint32_t)time(NULL);
printf("found %.8f -> %.8f newprice %.8f vs ask %.8f += %.8f qprice %.8f\n",dstr(Q.satoshis),dstr(Q.destsatoshis),(double)Q.destsatoshis/Q.satoshis,ask,price,qprice); printf("found %.8f -> %.8f newprice %.8f vs ask %.8f += %.8f qprice %.8f\n",dstr(Q.satoshis),dstr(Q.destsatoshis),(double)Q.destsatoshis/Q.satoshis,ask,price,qprice);
} }
else else
@ -947,9 +949,11 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
retjson = LP_quotejson(&Q); retjson = LP_quotejson(&Q);
LP_unavailableset(Q.txid,Q.vout,Q.timestamp + LP_RESERVETIME,Q.desthash); LP_unavailableset(Q.txid,Q.vout,Q.timestamp + LP_RESERVETIME,Q.desthash);
LP_unavailableset(Q.txid2,Q.vout2,Q.timestamp + LP_RESERVETIME,Q.desthash); LP_unavailableset(Q.txid2,Q.vout2,Q.timestamp + LP_RESERVETIME,Q.desthash);
if ( Q.quotetime == 0 )
Q.quotetime = (uint32_t)time(NULL);
jaddnum(retjson,"quotetime",Q.quotetime); jaddnum(retjson,"quotetime",Q.quotetime);
jaddnum(retjson,"pending",Q.timestamp + LP_RESERVETIME); jaddnum(retjson,"pending",Q.timestamp + LP_RESERVETIME);
jaddbits256(retjson,"desthash",Q.desthash); //jaddbits256(retjson,"desthash",Q.desthash);
jaddstr(retjson,"method","reserved"); jaddstr(retjson,"method","reserved");
msg = jprint(retjson,0); msg = jprint(retjson,0);
printf("return after queued RESERVED: set swappending.%u accept qprice %.8f, min %.8f\n(%s)\n",Q.timestamp + LP_RESERVETIME,qprice,ask,msg); printf("return after queued RESERVED: set swappending.%u accept qprice %.8f, min %.8f\n(%s)\n",Q.timestamp + LP_RESERVETIME,qprice,ask,msg);
@ -1126,6 +1130,7 @@ char *LP_autobuy(void *ctx,char *myipaddr,int32_t mypubsock,char *base,char *rel
memset(pubkeys,0,sizeof(pubkeys)); memset(pubkeys,0,sizeof(pubkeys));
LP_txfees(&txfee,&desttxfee,base,rel); LP_txfees(&txfee,&desttxfee,base,rel);
destsatoshis = SATOSHIDEN * relvolume; destsatoshis = SATOSHIDEN * relvolume;
LP_address_utxo_reset(relcoin);
if ( (autxo= LP_utxo_bestfit(rel,destsatoshis + 2*desttxfee)) == 0 ) if ( (autxo= LP_utxo_bestfit(rel,destsatoshis + 2*desttxfee)) == 0 )
return(clonestr("{\"error\":\"cant find alice utxo that is big enough\"}")); return(clonestr("{\"error\":\"cant find alice utxo that is big enough\"}"));
//printf("bestfit selected alice (%.8f %.8f) for %.8f sats %.8f\n",dstr(autxo->payment.value),dstr(autxo->fee.value),dstr(destsatoshis),dstr(autxo->S.satoshis)); //printf("bestfit selected alice (%.8f %.8f) for %.8f sats %.8f\n",dstr(autxo->payment.value),dstr(autxo->fee.value),dstr(destsatoshis),dstr(autxo->S.satoshis));

2
iguana/exchanges/LP_signatures.c

@ -704,7 +704,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_
jaddnum(reqjson,"timestamp",time(NULL)); jaddnum(reqjson,"timestamp",time(NULL));
msg = jprint(reqjson,1); msg = jprint(reqjson,1);
msg2 = clonestr(msg); msg2 = clonestr(msg);
//printf("QUERY.(%s)\n",msg); printf("QUERY.(%s)\n",msg);
memset(&zero,0,sizeof(zero)); memset(&zero,0,sizeof(zero));
portable_mutex_lock(&LP_reservedmutex); portable_mutex_lock(&LP_reservedmutex);
if ( num_Reserved_msgs[1] < sizeof(Reserved_msgs[1])/sizeof(*Reserved_msgs[1])-2 ) if ( num_Reserved_msgs[1] < sizeof(Reserved_msgs[1])/sizeof(*Reserved_msgs[1])-2 )

6
iguana/exchanges/LP_utxo.c

@ -464,7 +464,7 @@ struct LP_address *LP_address_utxo_reset(struct iguana_info *coin)
if ( (up= LP_address_utxofind(coin,coin->smartaddr,txid,vout)) == 0 ) if ( (up= LP_address_utxofind(coin,coin->smartaddr,txid,vout)) == 0 )
printf("couldnt find just added %s/%d ht.%d %.8f\n",bits256_str(str,txid),vout,height,dstr(value)); printf("couldnt find just added %s/%d ht.%d %.8f\n",bits256_str(str,txid),vout,height,dstr(value));
} }
//printf("added %d from listunspents\n",n); printf("added %d from listunspents\n",n);
} }
free_json(array); free_json(array);
} }
@ -999,7 +999,7 @@ int32_t LP_iseligible(uint64_t *valp,uint64_t *val2p,int32_t iambob,char *symbol
strcpy(destaddr,destaddr2); strcpy(destaddr,destaddr2);
if ( coin != 0 ) if ( coin != 0 )
{ {
if ( coin->electrum != 0 ) /*if ( coin->electrum != 0 )
{ {
if ( (tx= LP_transactionfind(coin,txid)) != 0 && vout < tx->numvouts && tx->outpoints[vout].spendheight > 0 ) if ( (tx= LP_transactionfind(coin,txid)) != 0 && vout < tx->numvouts && tx->outpoints[vout].spendheight > 0 )
{ {
@ -1022,7 +1022,7 @@ int32_t LP_iseligible(uint64_t *valp,uint64_t *val2p,int32_t iambob,char *symbol
return(0); return(0);
} }
} }
else else*/
{ {
if ( (txobj= LP_gettxout(coin->symbol,destaddr,txid,vout)) == 0 ) if ( (txobj= LP_gettxout(coin->symbol,destaddr,txid,vout)) == 0 )
return(0); return(0);

Loading…
Cancel
Save