Browse Source

Merge pull request #369 from jl777/spvdex

Spvdex
etomic
jl777 7 years ago
committed by GitHub
parent
commit
fc95826e20
  1. 8
      crypto777/OS_nonportable.c
  2. 2
      crypto777/nanosrc/utils/win.h
  3. 2
      iguana/exchanges/LP_commands.c
  4. 16
      iguana/exchanges/LP_nativeDEX.c
  5. 14
      iguana/exchanges/LP_ordermatch.c
  6. 19
      iguana/exchanges/LP_remember.c
  7. 8
      iguana/exchanges/LP_socket.c
  8. 19
      iguana/exchanges/LP_statemachine.c
  9. 1
      iguana/exchanges/LP_swap.c
  10. 78
      iguana/exchanges/LP_utxo.c
  11. 2
      iguana/exchanges/LP_utxos.c
  12. 56
      iguana/exchanges/mm.c

8
crypto777/OS_nonportable.c

@ -26,10 +26,10 @@
* not from the mingw header, so we need to include the windows header
* if we are compiling in windows 64bit
*/
#if defined(_M_X64)
#define WIN32_LEAN_AND_MEAN
#include <WinSock2.h>
#endif
//#if defined(_M_X64)
//#define WIN32_LEAN_AND_MEAN
//#include <WinSock2.h>
//#endif
#include "OS_portable.h"

2
crypto777/nanosrc/utils/win.h

@ -27,8 +27,8 @@
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <windows.h>
#include <mswsock.h>
#include <process.h>
#include <ws2tcpip.h>

2
iguana/exchanges/LP_commands.c

@ -326,7 +326,7 @@ dividends(coin, height, <args>)\n\
uint32_t requestid,quoteid;
if ( (requestid= juint(argjson,"requestid")) != 0 && (quoteid= juint(argjson,"quoteid")) != 0 )
return(basilisk_swapentry(requestid,quoteid));
else return(basilisk_swaplist());
else return(basilisk_swaplist(0,0));
}
else if ( strcmp(method,"myprices") == 0 )
return(LP_myprices());

16
iguana/exchanges/LP_nativeDEX.c

@ -19,17 +19,15 @@
// LP_nativeDEX.c
// marketmaker
//
// new features:
// better error message in ordermatch
// withdraw
// SPV at tx level
// stats, fix pricearray
// sign packets
// spv check
// dPoW security
// stats, fix pricearray
// electrum peers
// withdraw
// verify portfolio
// bittrex balancing
// -check for completed one being spent
#include <stdio.h>
#include "LP_include.h"
@ -509,7 +507,7 @@ int32_t LP_mainloop_iter(void *ctx,char *myipaddr,struct LP_peerinfo *mypeer,int
coin->lastscanht = coin->firstscanht;
continue;
}
//printf("%s ref.%d scan.%d to %d, longest.%d\n",coin->symbol,coin->firstrefht,coin->firstscanht,coin->lastscanht,coin->longestchain);
printf("%s ref.%d scan.%d to %d, longest.%d\n",coin->symbol,coin->firstrefht,coin->firstscanht,coin->lastscanht,coin->longestchain);
if ( LP_blockinit(coin,coin->lastscanht) < 0 )
{
printf("blockinit.%s %d error\n",coin->symbol,coin->lastscanht);
@ -655,7 +653,9 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
portable_mutex_init(&LP_butxomutex);
if ( system("curl -s4 checkip.amazonaws.com > DB/myipaddr") == 0 )
{
if ( (myipaddr= OS_filestr(&filesize,"DB/myipaddr")) != 0 && myipaddr[0] != 0 )
char ipfname[64];
strcpy(ipfname,"DB/myipaddr");
if ( (myipaddr= OS_filestr(&filesize,ipfname)) != 0 && myipaddr[0] != 0 )
{
n = strlen(myipaddr);
if ( myipaddr[n-1] == '\n' )

14
iguana/exchanges/LP_ordermatch.c

@ -394,7 +394,7 @@ double LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct L
sleep(3);
LP_broadcast_message(LP_mypubsock,qp->srccoin,qp->destcoin,zero,msg);
} else LP_broadcast_message(LP_mypubsock,qp->srccoin,qp->destcoin,qp->srchash,msg);
for (i=0; i<30; i++)
for (i=0; i<20; i++)
{
if ( (price= LP_pricecache(qp,qp->srccoin,qp->destcoin,qp->txid,qp->vout)) > SMALLVAL )
{
@ -404,7 +404,7 @@ double LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct L
break;
}
}
usleep(250000);
sleep(1);
}
return(price);
}
@ -441,12 +441,12 @@ int32_t LP_nanobind(void *ctx,char *pairstr)
return(pairsock);
}
int32_t LP_nearest_utxovalue(struct LP_address_utxo **utxos,int32_t n,uint64_t targetval)
int32_t LP_nearest_utxovalue(int32_t noSPV,struct LP_address_utxo **utxos,int32_t n,uint64_t targetval)
{
int32_t i,mini = -1; int64_t dist; uint64_t mindist = (1LL << 60);
for (i=0; i<n; i++)
{
if ( utxos[i] != 0 )
if ( utxos[i] != 0 && (noSPV != 0 || (utxos[i]->spendheight == 0 && utxos[i]->SPV > 0)) )
{
dist = (utxos[i]->U.value - targetval);
if ( dist >= 0 && dist < mindist )
@ -484,12 +484,12 @@ struct LP_utxoinfo *LP_address_utxopair(int32_t iambob,struct LP_address_utxo **
printf("targetval %.8f vol %.8f price %.8f txfee %.8f %s\n",dstr(targetval),relvolume,price,dstr(txfee),coinaddr);
}
mini = -1;
if ( targetval != 0 && (mini= LP_nearest_utxovalue(utxos,m,targetval)) >= 0 && (double)utxos[mini]->U.value/targetval < LP_MINVOL-1 )
if ( targetval != 0 && (mini= LP_nearest_utxovalue(coin->electrum == 0,utxos,m,targetval)) >= 0 && (double)utxos[mini]->U.value/targetval < LP_MINVOL-1 )
{
up = utxos[mini];
utxos[mini] = 0;
targetval2 = (up->U.value / 8) * 9 + 2*txfee;
if ( (mini= LP_nearest_utxovalue(utxos,m,targetval2)) >= 0 )
targetval2 = (targetval / 8) * 9 + 2*txfee;
if ( (mini= LP_nearest_utxovalue(coin->electrum == 0,utxos,m,targetval2)) >= 0 )
{
if ( up != 0 && (up2= utxos[mini]) != 0 )
{

19
iguana/exchanges/LP_remember.c

@ -1035,7 +1035,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
return(item);
}
char *basilisk_swaplist()
char *basilisk_swaplist(uint32_t origrequestid,uint32_t origquoteid)
{
char fname[512]; FILE *fp; cJSON *item,*retjson,*array,*totalsobj; uint32_t r,q,quoteid,requestid; int64_t KMDtotals[16],BTCtotals[16],Btotal,Ktotal; int32_t i;
portable_mutex_lock(&LP_swaplistmutex);
@ -1058,12 +1058,15 @@ char *basilisk_swaplist()
q = (uint32_t)G.LP_skipstatus[i];
if ( r == requestid && q == quoteid )
{
item = cJSON_CreateObject();
jaddstr(item,"status","realtime");
jaddnum(item,"requestid",r);
jaddnum(item,"quoteid",q);
jaddi(array,item);
flag = 1;
if ( r != origrequestid || q != origquoteid )
{
item = cJSON_CreateObject();
jaddstr(item,"status","realtime");
jaddnum(item,"requestid",r);
jaddnum(item,"quoteid",q);
jaddi(array,item);
flag = 1;
}
break;
}
}
@ -1114,7 +1117,7 @@ char *basilisk_swaplist()
char *basilisk_swapentry(uint32_t requestid,uint32_t quoteid)
{
char *liststr,*retstr = 0; cJSON *retjson,*array,*item; int32_t i,n;
if ( (liststr= basilisk_swaplist()) != 0 )
if ( (liststr= basilisk_swaplist(requestid,quoteid)) != 0 )
{
if ( (retjson= cJSON_Parse(liststr)) != 0 )
{

8
iguana/exchanges/LP_socket.c

@ -21,10 +21,10 @@
* @remarks - #if (defined(_M_X64) || defined(__amd64__)) && defined(WIN32)
* is equivalent to #if defined(_M_X64) as _M_X64 is defined for MSVC only
*/
#if defined(_M_X64)
#define WIN32_LEAN_AND_MEAN
#include <WinSock2.h>
#endif
//#if defined(_M_X64)
//#define WIN32_LEAN_AND_MEAN
//#include <WinSock2.h>
//#endif
#define ELECTRUM_TIMEOUT 5

19
iguana/exchanges/LP_statemachine.c

@ -17,6 +17,25 @@
// LP_statemachine.c
// marketmaker
//
else
{
CTransaction tx; uint256 hashBlock; int32_t numvouts,len; uint8_t *ptr;
if ( GetTransaction(NOTARIZED_DESTTXID,tx,hashBlock,true) == 0 )
{
fprintf(stderr,"error finding")
return(-1);
}
if ( (numvouts= tx.vout.size()) > 0 )
{
ptr = (uint8_t *)tx.vout[numvouts - 1].scriptPubKey.data();
len = tx.vout[numvouts - 1].scriptPubKey.size();
retval = komodo_verifynotarizedscript(height,ptr,len,NOTARIZED_HASH);
printf("direct verify ht.%d -> %d\n",height,retval);
return(retval);
}
}
/*struct LP_cacheinfo *ptr,*tmp;
HASH_ITER(hh,LP_cacheinfos,ptr,tmp)
{

1
iguana/exchanges/LP_swap.c

@ -677,6 +677,7 @@ int32_t LP_swapwait(uint32_t requestid,uint32_t quoteid,int32_t duration,int32_t
{
if ( jstr(retjson,"status") != 0 && strcmp(jstr(retjson,"status"),"finished") == 0 )
break;
else printf("NOT FINISHED.(%s)\n",jprint(retjson,0));
free_json(retjson);
retjson = 0;
}

78
iguana/exchanges/LP_utxo.c

@ -252,21 +252,95 @@ cJSON *LP_address_item(struct iguana_info *coin,struct LP_address_utxo *up,int32
uint64_t _LP_unspents_metric(uint64_t total,int32_t n) { return((total<<16) | (n & 0xffff)); }
bits256 iguana_merkle(bits256 *tree,int32_t txn_count)
{
int32_t i,n=0,prev; uint8_t serialized[sizeof(bits256) * 2];
if ( txn_count == 1 )
return(tree[0]);
prev = 0;
while ( txn_count > 1 )
{
if ( (txn_count & 1) != 0 )
tree[prev + txn_count] = tree[prev + txn_count-1], txn_count++;
n += txn_count;
for (i=0; i<txn_count; i+=2)
{
iguana_rwbignum(1,serialized,sizeof(*tree),tree[prev + i].bytes);
iguana_rwbignum(1,&serialized[sizeof(*tree)],sizeof(*tree),tree[prev + i + 1].bytes);
tree[n + (i >> 1)] = bits256_doublesha256(0,serialized,sizeof(serialized));
}
prev = n;
txn_count >>= 1;
}
return(tree[n]);
}
bits256 validate_merkle(int32_t pos,bits256 txid,cJSON *proofarray,int32_t proofsize)
{
int32_t i; uint8_t serialized[sizeof(bits256) * 2]; bits256 hash,proof;
hash = txid;
for (i=0; i<proofsize; i++)
{
proof = jbits256i(proofarray,i);
if ( (pos & 1) == 0 )
{
iguana_rwbignum(1,&serialized[0],sizeof(hash),hash.bytes);
iguana_rwbignum(1,&serialized[sizeof(hash)],sizeof(proof),proof.bytes);
}
else
{
iguana_rwbignum(1,&serialized[0],sizeof(proof),proof.bytes);
iguana_rwbignum(1,&serialized[sizeof(hash)],sizeof(hash),hash.bytes);
}
hash = bits256_doublesha256(0,serialized,sizeof(serialized));
pos >>= 1;
}
return(hash);
}
cJSON *LP_address_utxos(struct iguana_info *coin,char *coinaddr,int32_t electrumret)
{
cJSON *array,*item; int32_t n; uint64_t total; struct LP_address *ap=0,*atmp; struct LP_address_utxo *up,*tmp;
cJSON *array,*item,*merkobj,*merkles,*hdrobj; int32_t n,m; uint64_t total; struct LP_address *ap=0,*atmp; struct LP_address_utxo *up,*tmp; bits256 merkleroot,roothash; struct electrum_info *ep,*backupep=0;
array = cJSON_CreateArray();
if ( coinaddr != 0 && coinaddr[0] != 0 )
{
if ( (ep= coin->electrum) != 0 )
{
if ( (backupep= ep->prev) == 0 )
backupep = ep;
}
//portable_mutex_lock(&coin->addrmutex);
if ( (ap= _LP_addressfind(coin,coinaddr)) != 0 )
{
total = n = 0;
DL_FOREACH_SAFE(ap->utxos,up,tmp)
{
//char str[65]; printf("LP_address_utxos %s/v%d %.8f ht.%d spend.%d\n",bits256_str(str,up->U.txid),up->U.vout,dstr(up->U.value),up->U.height,up->spendheight);
if ( up->spendheight <= 0 && up->U.height > 0 )
{
if ( up->SPV == 0 && up->U.height > 0 )
{
if ( (merkobj= electrum_getmerkle(coin->symbol,backupep,&merkobj,up->U.txid,up->U.height)) != 0 )
{
char str[65],str2[65],str3[65];
memset(roothash.bytes,0,sizeof(roothash));
if ( (merkles= jarray(&m,merkobj,"merkle")) != 0 )
{
roothash = validate_merkle(jint(merkobj,"pos"),up->U.txid,merkles,m);
if ( (hdrobj= electrum_getheader(coin->symbol,backupep,&hdrobj,up->U.height)) != 0 )
{
merkleroot = jbits256(hdrobj,"merkle_root");
if ( bits256_cmp(merkleroot,roothash) == 0 )
{
up->SPV = up->U.height;
//printf("validated MERK %s ht.%d -> %s root.(%s)\n",bits256_str(str,up->U.txid),up->U.height,jprint(merkobj,0),bits256_str(str2,roothash));
}
else printf("ERROR MERK %s ht.%d -> %s root.(%s) vs %s\n",bits256_str(str,up->U.txid),up->U.height,jprint(merkobj,0),bits256_str(str2,roothash),bits256_str(str3,merkleroot));
free_json(hdrobj);
}
}
free_json(merkobj);
}
}
jaddi(array,LP_address_item(coin,up,electrumret));
n++;
total += up->U.value;

2
iguana/exchanges/LP_utxos.c

@ -619,7 +619,7 @@ int32_t LP_privkey_init(int32_t mypubsock,struct iguana_info *coin,bits256 mypri
}
portable_mutex_unlock(&LP_UTXOmutex);
total += value;
} else printf("scriptmismatch.(%s) vs %s\n",script,jprint(item,0));
} // else printf("scriptmismatch.(%s) vs %s\n",script,jprint(item,0));
} //else printf("nothing near i.%d\n",i);
} else break;
}

56
iguana/exchanges/mm.c

@ -784,6 +784,62 @@ void marketmaker(double minask,double maxbid,char *baseaddr,char *reladdr,double
#include "LP_nativeDEX.c"
/*MERK d6071f9b03d1428b648d51ae1268f1605d97f44422ed55ad0335b13fa655f61a ht.518777 -> {"pos":1,"merkle":["526f8be81718beccc16a541a2c550b612123218d80fa884d9f080f18284e2bd8", "f68b03a7b6e418c9b306d8d8b21917ae5a584696f9b0b8cb0741733d7097fdfd"],"block_height":518777} root.(0000000000000000000000000000000000000000000000000000000000000000)
MERK c007e9c1881a83be453cb6ed3d1bd3bda85efd3b5ce60532c2e20ae3f8a82543 ht.518777 -> {"pos":2,"merkle":["fdff0962fb95120a86a07ddf1ec784fcc5554a2d0a3791a8db2083d593920501", "8c116e974c842ad3ad8b3ddbd71da3debb150e3fe692f5bd628381bc167311a7"],"block_height":518777} root.(0000000000000000000000000000000000000000000000000000000000000000)*/
/*526f8be81718beccc16a541a2c550b612123218d80fa884d9f080f18284e2bd8
d6071f9b03d1428b648d51ae1268f1605d97f44422ed55ad0335b13fa655f61a
c007e9c1881a83be453cb6ed3d1bd3bda85efd3b5ce60532c2e20ae3f8a82543
fdff0962fb95120a86a07ddf1ec784fcc5554a2d0a3791a8db2083d593920501*/
/*0: 526f8be81718beccc16a541a2c550b612123218d80fa884d9f080f18284e2bd8
1: d6071f9b03d1428b648d51ae1268f1605d97f44422ed55ad0335b13fa655f61a
2: c007e9c1881a83be453cb6ed3d1bd3bda85efd3b5ce60532c2e20ae3f8a82543
3: fdff0962fb95120a86a07ddf1ec784fcc5554a2d0a3791a8db2083d593920501
4: 8c116e974c842ad3ad8b3ddbd71da3debb150e3fe692f5bd628381bc167311a7
5: f68b03a7b6e418c9b306d8d8b21917ae5a584696f9b0b8cb0741733d7097fdfd
6: a87ee259560f20b20182760c0e7cc7896d44381f0ad58a2e755a2b6b895b01ec*/
/*
0 1 2 3
4 5
6
1 -> [0, 5]
2 -> [3, 4]
if odd -> right, else left
then /= 2
*/
/*void testmerk()
{
bits256 tree[256],roothash,txid; int32_t i; char str[65];
memset(tree,0,sizeof(tree));
decode_hex(tree[0].bytes,32,"526f8be81718beccc16a541a2c550b612123218d80fa884d9f080f18284e2bd8");
decode_hex(tree[1].bytes,32,"d6071f9b03d1428b648d51ae1268f1605d97f44422ed55ad0335b13fa655f61a");
decode_hex(tree[2].bytes,32,"c007e9c1881a83be453cb6ed3d1bd3bda85efd3b5ce60532c2e20ae3f8a82543");
decode_hex(tree[3].bytes,32,"fdff0962fb95120a86a07ddf1ec784fcc5554a2d0a3791a8db2083d593920501");
roothash = iguana_merkle(tree,4);
for (i=0; i<256; i++)
{
if ( bits256_nonz(tree[i]) == 0 )
break;
printf("%d: %s\n",i,bits256_str(str,tree[i]));
}
memset(tree,0,sizeof(tree));
decode_hex(tree[0].bytes,32,"526f8be81718beccc16a541a2c550b612123218d80fa884d9f080f18284e2bd8");
decode_hex(tree[1].bytes,32,"f68b03a7b6e418c9b306d8d8b21917ae5a584696f9b0b8cb0741733d7097fdfd");
decode_hex(txid.bytes,32,"d6071f9b03d1428b648d51ae1268f1605d97f44422ed55ad0335b13fa655f61a");
roothash = validate_merkle(1,txid,tree,2);
printf("validate 1: %s\n",bits256_str(str,roothash));
memset(tree,0,sizeof(tree));
decode_hex(tree[0].bytes,32,"fdff0962fb95120a86a07ddf1ec784fcc5554a2d0a3791a8db2083d593920501");
decode_hex(tree[1].bytes,32,"8c116e974c842ad3ad8b3ddbd71da3debb150e3fe692f5bd628381bc167311a7");
decode_hex(txid.bytes,32,"c007e9c1881a83be453cb6ed3d1bd3bda85efd3b5ce60532c2e20ae3f8a82543");
roothash = validate_merkle(2,txid,tree,2);
printf("validate 2: %s\n",bits256_str(str,roothash));
}*/
void LP_main(void *ptr)
{
char *passphrase; double profitmargin; uint16_t port; cJSON *argjson = ptr;

Loading…
Cancel
Save