Skip to content

Commit

Permalink
Merge pull request jl777#482 from jl777/spvdex
Browse files Browse the repository at this point in the history
reduce memory usage fixed
  • Loading branch information
jl777 authored Nov 16, 2017
2 parents 957ee8d + eb3d566 commit 659888e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
4 changes: 2 additions & 2 deletions crypto777/cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ static int32_t cJSON_strcasecmp(const char *s1,const char *s2)

void *LP_alloc(uint64_t len);
void LP_free(void *ptr);
static void *(*cJSON_malloc)(size_t sz) = (void *)LP_alloc;
static void (*cJSON_free)(void *ptr) = LP_free;
static void *(*cJSON_malloc)(size_t sz) = (void *)malloc;//LP_alloc;
static void (*cJSON_free)(void *ptr) = free;//LP_free;

static void *cJSON_mallocstr(int32_t len)
{
Expand Down
8 changes: 4 additions & 4 deletions iguana/exchanges/LP_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

#define LP_MAJOR_VERSION "0"
#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
#include <emscripten.h>
Expand All @@ -38,8 +40,6 @@ void emscripten_usleep(int32_t x); // returns immediate, no sense for sleeping
#endif
//#define LP_STRICTPEERS

#define LP_BARTERDEX_VERSION 1
#define LP_MAGICBITS 8

#define LP_MAXVINS 64
#define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL)
Expand Down Expand Up @@ -180,7 +180,7 @@ struct basilisk_request
int64_t srcamount,unused; // 16 to 31
bits256 srchash; // 32 to 63
bits256 desthash;
char src[65],dest[65];
char src[68],dest[68];
uint64_t destamount;
int32_t optionhours,DEXselector;
};
Expand Down
6 changes: 3 additions & 3 deletions iguana/exchanges/LP_nativeDEX.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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);
exit(-1);
Expand Down Expand Up @@ -1216,7 +1216,7 @@ int32_t zeroval() { return(0); }

void *LP_alloc(uint64_t len)
{
return(calloc(1,len));
//return(calloc(1,len));
LP_cjson_allocated += len;
LP_cjson_total += len;
LP_cjson_count++;
Expand All @@ -1235,7 +1235,7 @@ return(calloc(1,len));
void LP_free(void *ptr)
{
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;
if ( (now= (uint32_t)time(NULL)) > lasttime+1 )
{
Expand Down
33 changes: 19 additions & 14 deletions iguana/exchanges/LP_ordermatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
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);
}
if ( (txout= LP_gettxout(qp->srccoin,qp->coinaddr,qp->txid,qp->vout)) != 0 )
Expand Down Expand Up @@ -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 )
{
//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);
}
if ( (txout= LP_gettxout(qp->destcoin,qp->destaddr,qp->desttxid,qp->destvout)) != 0 )
Expand Down Expand Up @@ -321,7 +321,7 @@ int32_t LP_nearest_utxovalue(struct iguana_info *coin,char *coinaddr,struct LP_a
}
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);
}
}
Expand Down Expand Up @@ -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 ( 1 )
if ( 0 )
{
int32_t i;
for (i=0; i<m; i++)
Expand Down Expand Up @@ -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));
qp->txid2 = qp->txid;
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_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));
}

Expand All @@ -532,9 +533,9 @@ void LP_alicequery_clear()
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");
LP_alicequery_clear();
Expand All @@ -545,7 +546,7 @@ int32_t LP_alice_eligible()
void LP_reserved(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *qp)
{
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);
if ( LP_pricevalid(price) > 0 && maxprice > SMALLVAL && price <= maxprice )
Expand All @@ -555,7 +556,7 @@ void LP_reserved(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo
printf("send CONNECT\n");
LP_query(ctx,myipaddr,mypubsock,"connect",qp);
} 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
Expand Down Expand Up @@ -778,7 +779,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
if ( strcmp(method,"reserved") == 0 )
{
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. )
return(retval);
if ( bits256_nonz(LP_Alicedestpubkey) != 0 )
Expand All @@ -789,8 +790,9 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
return(retval);
} 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 )
{
printf("reserved quote validate error %.0f\n",qprice);
Expand All @@ -807,7 +809,6 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
return(retval);
}
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 )
free(retstr);
LP_reserved(ctx,myipaddr,pubsock,&Q);
Expand Down Expand Up @@ -904,6 +905,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
Q.txid2 = butxo->deposit.txid;
Q.vout2 = butxo->deposit.vout;
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);
}
else
Expand Down Expand Up @@ -947,9 +949,11 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
retjson = LP_quotejson(&Q);
LP_unavailableset(Q.txid,Q.vout,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,"pending",Q.timestamp + LP_RESERVETIME);
jaddbits256(retjson,"desthash",Q.desthash);
//jaddbits256(retjson,"desthash",Q.desthash);
jaddstr(retjson,"method","reserved");
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);
Expand Down Expand Up @@ -1126,6 +1130,7 @@ char *LP_autobuy(void *ctx,char *myipaddr,int32_t mypubsock,char *base,char *rel
memset(pubkeys,0,sizeof(pubkeys));
LP_txfees(&txfee,&desttxfee,base,rel);
destsatoshis = SATOSHIDEN * relvolume;
LP_address_utxo_reset(relcoin);
if ( (autxo= LP_utxo_bestfit(rel,destsatoshis + 2*desttxfee)) == 0 )
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));
Expand Down
2 changes: 1 addition & 1 deletion iguana/exchanges/LP_signatures.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_
jaddnum(reqjson,"timestamp",time(NULL));
msg = jprint(reqjson,1);
msg2 = clonestr(msg);
//printf("QUERY.(%s)\n",msg);
printf("QUERY.(%s)\n",msg);
memset(&zero,0,sizeof(zero));
portable_mutex_lock(&LP_reservedmutex);
if ( num_Reserved_msgs[1] < sizeof(Reserved_msgs[1])/sizeof(*Reserved_msgs[1])-2 )
Expand Down
6 changes: 3 additions & 3 deletions iguana/exchanges/LP_utxo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
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);
}
Expand Down Expand Up @@ -999,7 +999,7 @@ int32_t LP_iseligible(uint64_t *valp,uint64_t *val2p,int32_t iambob,char *symbol
strcpy(destaddr,destaddr2);
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 )
{
Expand All @@ -1022,7 +1022,7 @@ int32_t LP_iseligible(uint64_t *valp,uint64_t *val2p,int32_t iambob,char *symbol
return(0);
}
}
else
else*/
{
if ( (txobj= LP_gettxout(coin->symbol,destaddr,txid,vout)) == 0 )
return(0);
Expand Down

0 comments on commit 659888e

Please sign in to comment.