Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/jl777/SuperNET into dev-deck…
Browse files Browse the repository at this point in the history
…er-dev
  • Loading branch information
DeckerSU committed Nov 15, 2017
2 parents e61d0a0 + 15c8c5b commit 3cbd438
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 26 deletions.
2 changes: 1 addition & 1 deletion crypto777/bitcoind_RPC.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ char *post_process_bitcoind_RPC(char *debugstr,char *command,char *rpcstr,char *
char *retstr = 0;
cJSON *json,*result,*error;
#ifdef FROM_MARKETMAKER
usleep(5000);
usleep(3000);
#endif
//printf("<<<<<<<<<<< bitcoind_RPC: %s post_process_bitcoind_RPC.%s.[%s]\n",debugstr,command,rpcstr);
if ( command == 0 || rpcstr == 0 || rpcstr[0] == 0 )
Expand Down
33 changes: 30 additions & 3 deletions crypto777/cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,6 @@ cJSON *cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->t
cJSON *cJSON_CreateBool(int32_t b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;}
cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int64_t)num;}return item;}
cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;}
cJSON *cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;}
cJSON *cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;}

/* Create Arrays: */
cJSON *cJSON_CreateIntArray(int64_t *numbers,int32_t count) {int32_t i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber((double)numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
Expand Down Expand Up @@ -1129,4 +1127,33 @@ cJSON *addrs_jsonarray(uint64_t *addrs,int32_t num)
return(array);
}

void free_json(cJSON *json) { if ( json != 0 ) cJSON_Delete(json); }
cJSON *cJSON_CreateArray(void)
{
cJSON *item = cJSON_New_Item();
if ( item )
item->type = cJSON_Array;
//#ifdef CJSON_GARBAGECOLLECTION
// cJSON_register(item);
//#endif
return(item);
}

cJSON *cJSON_CreateObject(void)
{
cJSON *item = cJSON_New_Item();
if ( item )
item->type = cJSON_Object;
//#ifdef CJSON_GARBAGECOLLECTION
// cJSON_register(item);
//#endif
return item;
}

void free_json(cJSON *item)
{
//#ifdef CJSON_GARBAGECOLLECTION
// cJSON_unregister(item);
//#endif
if ( item != 0 )
cJSON_Delete(item);
}
6 changes: 3 additions & 3 deletions iguana/exchanges/LP_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ struct LP_transaction *LP_create_transaction(struct iguana_info *coin,bits256 tx
vins = jarray(&numvins,txobj,"vin");
vouts = jarray(&numvouts,txobj,"vout");
tx = LP_transactionadd(coin,txid,height,numvouts,numvins);
tx->serialized = serialized;
//free(serialized);
tx->serialized = 0;//serialized;
free(serialized);
tx->fpos = fpos;
tx->len = tx->len;
tx->len = 0;//tx->len;
tx->SPV = tx->height = height;
//printf("tx.%s numvins.%d numvouts.%d\n",bits256_str(str,txid),numvins,numvouts);
for (i=0; i<numvouts; i++)
Expand Down
6 changes: 5 additions & 1 deletion iguana/exchanges/LP_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ char *LP_numutxos()
char *stats_JSON(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port
{
char *method,*userpass,*base,*rel,*coin,*retstr = 0; int32_t changed,flag = 0; cJSON *retjson,*reqjson = 0; struct iguana_info *ptr;
//printf("stats_JSON(%s)\n",jprint(argjson,0));
method = jstr(argjson,"method");
/*if ( //strcmp(method,"uitem") == 0 || strcmp(method,"postutxos") == 0 ||
strcmp(method,"notify") == 0
|| strcmp(method,"postprice") == 0
)
return(clonestr("{}"));*/
/*if ( (ipaddr= jstr(argjson,"ipaddr")) != 0 && (argport= juint(argjson,"port")) != 0 && (method == 0 || strcmp(method,"electrum") != 0) )
{
if ( strcmp(ipaddr,"127.0.0.1") != 0 && argport >= 1000 )
Expand Down
6 changes: 3 additions & 3 deletions iguana/exchanges/LP_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#define LP_MAJOR_VERSION "0"
#define LP_MINOR_VERSION "1"
#define LP_BUILD_NUMBER "14414"
#define LP_BUILD_NUMBER "15000"

#ifdef FROM_JS
#include <emscripten.h>
Expand Down Expand Up @@ -53,8 +53,8 @@ void emscripten_usleep(int32_t x); // returns immediate, no sense for sleeping
#define LP_MIN_PEERS 8
#define LP_MAX_PEERS 32

#define LP_MAXDESIRED_UTXOS 128
#define LP_MINDESIRED_UTXOS 32
#define LP_MAXDESIRED_UTXOS (IAMLP != 0 ? 128 : 64)
#define LP_MINDESIRED_UTXOS (IAMLP != 0 ? 32 : 16)
#define LP_DUSTCOMBINE_THRESHOLD 1000000

// RTmetrics
Expand Down
25 changes: 15 additions & 10 deletions iguana/exchanges/LP_nativeDEX.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
// LP_nativeDEX.c
// marketmaker
//
// single utxo allocations alice
// gc cJSON
// more retries for swap sendrawtransaction
// more retries for swap sendrawtransaction?
// pbca26 unfinished swaps
// alice waiting for bestprice
//if ( G.LP_pendingswaps != 0 )
//return(-1);
// if ( G.LP_pendingswaps != 0 ) return(-1);
// bot safe to exit?
//
// BCH signing
// single utxo allocations alice
// alice waiting for bestprice
// previously, it used to show amount, kmd equiv, perc
// dPoW security -> 4: KMD notarized, 5: BTC notarized, after next notary elections
// bigendian architectures need to use little endian for sighash calcs
Expand Down Expand Up @@ -83,7 +81,7 @@ void LP_millistats_update(struct LP_millistats *mp)
}

#include "LP_include.h"
portable_mutex_t LP_peermutex,LP_UTXOmutex,LP_utxomutex,LP_commandmutex,LP_cachemutex,LP_swaplistmutex,LP_forwardmutex,LP_pubkeymutex,LP_networkmutex,LP_psockmutex,LP_coinmutex,LP_messagemutex,LP_portfoliomutex,LP_electrummutex,LP_butxomutex,LP_reservedmutex,LP_nanorecvsmutex,LP_tradebotsmutex,LP_gcmutex,LP_inusemutex;
portable_mutex_t LP_peermutex,LP_UTXOmutex,LP_utxomutex,LP_commandmutex,LP_cachemutex,LP_swaplistmutex,LP_forwardmutex,LP_pubkeymutex,LP_networkmutex,LP_psockmutex,LP_coinmutex,LP_messagemutex,LP_portfoliomutex,LP_electrummutex,LP_butxomutex,LP_reservedmutex,LP_nanorecvsmutex,LP_tradebotsmutex,LP_gcmutex,LP_inusemutex,LP_cJSONmutex;
int32_t LP_canbind;
char *Broadcaststr,*Reserved_msgs[2][1000];
int32_t num_Reserved_msgs[2],max_Reserved_msgs[2];
Expand Down Expand Up @@ -328,7 +326,7 @@ char *LP_process_message(void *ctx,char *typestr,char *myipaddr,int32_t pubsock,

int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int32_t sock,char *remoteaddr,int32_t maxdepth)
{
int32_t recvlen=1,nonz = 0; cJSON *argjson; void *ptr; char methodstr[64],*retstr,*str; struct nn_pollfd pfd;
int32_t recvlen=1,nonz = 0; cJSON *argjson; void *ptr,*buf; char methodstr[64],*retstr,*str; struct nn_pollfd pfd;
if ( sock >= 0 )
{
while ( nonz < maxdepth && recvlen > 0 )
Expand All @@ -340,8 +338,11 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int
if ( nn_poll(&pfd,1,1) != 1 )
break;
ptr = 0;
if ( (recvlen= nn_recv(sock,&ptr,NN_MSG,0)) > 0 )
buf = malloc(1000000);
if ( (recvlen= nn_recv(sock,buf,1000000,0)) > 0 )
//if ( (recvlen= nn_recv(sock,&ptr,NN_MSG,0)) > 0 )
{
ptr = buf;
methodstr[0] = 0;
//printf("%s.(%s)\n",typestr,(char *)ptr);
if ( 0 )
Expand Down Expand Up @@ -391,7 +392,10 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int
}
}
if ( ptr != 0 )
nn_freemsg(ptr), ptr = 0;
{
//nn_freemsg(ptr), ptr = 0;
free(buf);
}
}
}
return(nonz);
Expand Down Expand Up @@ -939,6 +943,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
portable_mutex_init(&LP_reservedmutex);
portable_mutex_init(&LP_nanorecvsmutex);
portable_mutex_init(&LP_tradebotsmutex);
portable_mutex_init(&LP_cJSONmutex);
myipaddr = clonestr("127.0.0.1");
#ifndef _WIN32
#ifndef FROM_JS
Expand Down
11 changes: 11 additions & 0 deletions iguana/exchanges/LP_ordermatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,cJSON *argjson,char *base,c
if ( bits256_nonz(privkey) != 0 && bits256_cmp(G.LP_mypub25519,qp->srchash) == 0 )
{
LP_requestinit(&qp->R,qp->srchash,qp->desthash,base,qp->satoshis-qp->txfee,rel,qp->destsatoshis-qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector);
if ( LP_pendingswap(qp->R.requestid,qp->R.quoteid) > 0 )
{
printf("requestid.%u quoteid.%u is already in progres\n",qp->R.requestid,qp->R.quoteid);
return(-1);
}
if ( (swap= LP_swapinit(1,0,privkey,&qp->R,qp)) == 0 )
{
printf("cant initialize swap\n");
Expand Down Expand Up @@ -611,6 +616,12 @@ char *LP_connectedalice(cJSON *argjson) // alice
{
retjson = cJSON_CreateObject();
LP_requestinit(&Q.R,Q.srchash,Q.desthash,Q.srccoin,Q.satoshis-Q.txfee,Q.destcoin,Q.destsatoshis-Q.desttxfee,Q.timestamp,Q.quotetime,DEXselector);
if ( LP_pendingswap(Q.R.requestid,Q.R.quoteid) > 0 )
{
printf("requestid.%u quoteid.%u is already in progres\n",Q.R.requestid,Q.R.quoteid);
jaddstr(retjson,"error","swap already in progress");
return(jprint(retjson,1));
}
if ( (swap= LP_swapinit(0,0,Q.privkey,&Q.R,&Q)) == 0 )
{
jaddstr(retjson,"error","couldnt swapinit");
Expand Down
37 changes: 37 additions & 0 deletions iguana/exchanges/LP_remember.c
Original file line number Diff line number Diff line change
Expand Up @@ -1441,3 +1441,40 @@ char *basilisk_swapentries(char *refbase,char *refrel,int32_t limit)
return(jprint(retarray,1));
}

int32_t LP_pendingswap(uint32_t requestid,uint32_t quoteid)
{
cJSON *retjson,*array,*pending,*item; uint32_t r,q; char *retstr; int32_t i,n,retval = 0;
if ( (retstr= LP_recent_swaps(1000)) != 0 )
{
if ( (retjson= cJSON_Parse(retstr)) != 0 )
{
if ( (array= jarray(&n,retjson,"swaps")) != 0 )
{
for (i=0; i<n; i++)
{
item = jitem(array,i);
r = juint(jitem(item,0),0);
q = juint(jitem(item,1),0);
if ( r == requestid && q == quoteid )
{
retval = 1;
break;
}
}
}
if ( retval == 0 )
{
if ( (pending= jobj(retjson,"pending")) != 0 )
{
r = juint(pending,"requestid");
q = juint(pending,"quoteid");
if ( r == requestid && q == quoteid )
retval = 1;
}
}
free_json(retjson);
}
free(retstr);
}
return(retval);
}
18 changes: 14 additions & 4 deletions iguana/exchanges/LP_swap.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ uint32_t basilisk_requestid(struct basilisk_request *rp)
int32_t LP_pubkeys_data(struct basilisk_swap *swap,uint8_t *data,int32_t maxlen)
{
int32_t i,datalen = 0;
datalen += iguana_rwnum(1,&data[datalen],sizeof(swap->I.req.requestid),&swap->I.req.requestid);
datalen += iguana_rwnum(1,&data[datalen],sizeof(swap->I.req.quoteid),&swap->I.req.quoteid);
data[datalen++] = swap->I.aliceconfirms;
data[datalen++] = swap->I.bobconfirms;
data[datalen++] = swap->I.alicemaxconfirms;
Expand All @@ -196,14 +198,22 @@ int32_t LP_pubkeys_data(struct basilisk_swap *swap,uint8_t *data,int32_t maxlen)
data[datalen++] = swap->persistent_pubkey33[i];
for (i=0; i<sizeof(swap->deck)/sizeof(swap->deck[0][0]); i++)
datalen += iguana_rwnum(1,&data[datalen],sizeof(swap->deck[i>>1][i&1]),&swap->deck[i>>1][i&1]);
printf("send >>>>>>>>> r.%u q.%u datalen.%d\n",swap->I.req.requestid,swap->I.req.quoteid,datalen);
return(datalen);
}

int32_t LP_pubkeys_verify(struct basilisk_swap *swap,uint8_t *data,int32_t datalen)
{
int32_t i,nonz=0,alicemaxconfirms,bobmaxconfirms,aliceconfirms,bobconfirms,len = 0; uint8_t other33[33];
if ( datalen == sizeof(swap->otherdeck)+38 )
uint32_t requestid,quoteid; int32_t i,nonz=0,alicemaxconfirms,bobmaxconfirms,aliceconfirms,bobconfirms,len = 0; uint8_t other33[33];
if ( datalen == sizeof(swap->otherdeck)+38+sizeof(uint32_t)*2 )
{
len += iguana_rwnum(0,&data[len],sizeof(requestid),&requestid);
len += iguana_rwnum(0,&data[len],sizeof(quoteid),&quoteid);
if ( requestid != swap->I.req.requestid || quoteid != swap->I.req.quoteid )
{
printf("SWAP requestid.%u quoteid.%u mismatch received r.%u q.%u\n",swap->I.req.requestid,swap->I.req.quoteid,requestid,quoteid);
return(-1);
}
aliceconfirms = data[len++];
bobconfirms = data[len++];
alicemaxconfirms = data[len++];
Expand Down Expand Up @@ -243,7 +253,7 @@ int32_t LP_pubkeys_verify(struct basilisk_swap *swap,uint8_t *data,int32_t datal
len += iguana_rwnum(0,&data[len],sizeof(swap->otherdeck[i>>1][i&1]),&swap->otherdeck[i>>1][i&1]);
return(0);
}
printf("pubkeys verify size mismatch %d != %d\n",datalen,(int32_t)sizeof(swap->otherdeck)+36);
printf("pubkeys verify size mismatch %d != %d\n",datalen,(int32_t)(sizeof(swap->otherdeck)+38+sizeof(uint32_t)*2));
return(-1);
}

Expand Down Expand Up @@ -895,7 +905,7 @@ void LP_aliceloop(void *_swap)
}
basilisk_swap_finished(swap);
printf("finish swap.%p\n",swap);
//free(swap);
free(swap);
G.LP_pendingswaps--;
}

Expand Down
67 changes: 67 additions & 0 deletions iguana/exchanges/LP_utxo.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,73 @@ struct LP_inuse_info
} LP_inuse[1024];
int32_t LP_numinuse;

struct cJSON_list
{
struct cJSON_list *next,*prev;
cJSON *item;
uint32_t timestamp,cjsonid;
} *LP_cJSONlist;

void cJSON_register(cJSON *item)
{
struct cJSON_list *ptr;
ptr = calloc(1,sizeof(*ptr));
ptr->timestamp = (uint32_t)time(NULL);
ptr->item = item;
item->cjsonid = rand();
ptr->cjsonid = item->cjsonid;
portable_mutex_lock(&LP_cJSONmutex);
DL_APPEND(LP_cJSONlist,ptr);
portable_mutex_unlock(&LP_cJSONmutex);
}

char *mbstr(char *str,double n);
void cJSON_unregister(cJSON *item)
{
static uint32_t lasttime;
int32_t n; char *tmpstr; uint64_t total = 0; struct cJSON_list *ptr,*tmp; uint32_t now;
if ( (now= (uint32_t)time(NULL)) > lasttime+6 )
{
n = 0;
DL_FOREACH_SAFE(LP_cJSONlist,ptr,tmp)
{
if ( ptr->item != 0 && ptr->item->child != 0 && ptr->cjsonid != 0 )
{
if ( (tmpstr= jprint(ptr->item,0)) != 0 )
{
total += strlen(tmpstr);
free(tmpstr);
}
}
n++;
}
printf("total %d cJSON pending\n",n);
lasttime = (uint32_t)time(NULL);
}
DL_FOREACH_SAFE(LP_cJSONlist,ptr,tmp)
{
if ( ptr->cjsonid == item->cjsonid )
break;
else if ( now > ptr->timestamp+60 && item->cjsonid != 0 )
{
portable_mutex_lock(&LP_cJSONmutex);
DL_DELETE(LP_cJSONlist,ptr);
portable_mutex_unlock(&LP_cJSONmutex);
printf("free expired\n");
cJSON_Delete(ptr->item);
free(ptr);
}
ptr = 0;
}
if ( ptr != 0 )
{
portable_mutex_lock(&LP_cJSONmutex);
DL_DELETE(LP_cJSONlist,ptr);
free(ptr);
portable_mutex_unlock(&LP_cJSONmutex);
} //else printf("cJSON_unregister of unknown %p %u\n",item,item->cjsonid);
}

struct LP_inuse_info *_LP_inuse_find(bits256 txid,int32_t vout)
{
int32_t i;
Expand Down
1 change: 1 addition & 0 deletions iguana/exchanges/invreset
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
source userpass
source passphrase
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"inventory\",\"coin\":\"KMD\",\"reset\":1,\"passphrase\":\"$passphrase\"}"
10 changes: 10 additions & 0 deletions iguana/exchanges/mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,21 @@
// Copyright © 2017 SuperNET. All rights reserved.
//

/*
#define malloc(n) LP_alloc(n)
#define calloc(a,b) LP_alloc((uint64_t)(a) * (b))
#define free(ptr) LP_free(ptr)*/

void PNACL_message(char *arg,...)
{

}
#define FROM_MARKETMAKER

#ifndef CJSON_GARBAGECOLLECTION
#define CJSON_GARBAGECOLLECTION
#endif

#include <stdio.h>
#include <stdint.h>
#ifndef NATIVE_WINDOWS
Expand Down
2 changes: 1 addition & 1 deletion iguana/m_mm
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cd secp256k1; ./m_unix; cd ..
cd ../crypto777; ./m_LP; cd ../iguana
gcc -g -o marketmaker -I../crypto777 exchanges/mm.c mini-gmp.c secp256k1.o ../agents/libcrypto777.a -lnanomsg -lcurl -lpthread -lm
gcc -g -o marketmaker -I../crypto777 exchanges/mm.c ../crypto777/cJSON.c mini-gmp.c secp256k1.o ../agents/libcrypto777.a -lnanomsg -lcurl -lpthread -lm
Loading

0 comments on commit 3cbd438

Please sign in to comment.