You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1195 lines
50 KiB

8 years ago
/******************************************************************************
* Copyright © 2014-2017 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
8 years ago
//
8 years ago
// LP_nativeDEX.c
8 years ago
// marketmaker
7 years ago
//
// alice waiting for bestprice
7 years ago
// regen inventory
7 years ago
// previously, it used to show amount, kmd equiv, perc
7 years ago
// bot safe to exit?
//
7 years ago
// BCH signing
7 years ago
// dPoW security -> 4: KMD notarized, 5: BTC notarized, after next notary elections
7 years ago
// bigendian architectures need to use little endian for sighash calcs
7 years ago
//BTC LP_transaction_fromdata mismatched txid f78c80e831c02c89c0888e7d4438287030238e09e33f88da6df4dc84b949959b vs 1390f386f5997607e646be9c93f30018aa68ea971790b92b7b06e892656f0769
//failed blockchain.transaction.get BTC f78c80e831c02c89c0888e7d4438287030238e09e33f88da6df4dc84b949959b
//02000000000101242344292f9117c8318adb84f7991239361da2ff0a15ad2106d402945072cc110a0000001716001470eb5212b558cfc10b31f0849ef88ecf17097ac1ffffffff0c4a8321000000000017a914baed1c79cc6325333d5013b44dc17e8256bf5b718704bf3c000000000017a914255cf2809a1f42569c5c47c71e6234aac35a800f873099f400000000001976a914049877b81a8d6d8592919b75b9bcb4182f7c52cd88acb907e200000000001976a9140614fc114940e867210b09cc900db14d8bc58d2b88ac40771b00000000001976a9148cadbb1d7002d22d60e38ca238678d00f04ed22988ac002d3101000000001976a9140d05714431cf478846cea1435f9eabae9ad5c65388acc0f280010000000017a914c17a70c74154fc53263652aaca26c8d71683f1ba87aa2a4e00000000001976a914feea6b867954b1072962083233796d47e4c1665e88ace0e1de02000000001976a91445b71fbb02df53be5563257b77780b9afcc55bdc88acf6fe6700000000001976a91492332f8ff3b26e3ce2666afa5fee2e45308225af88ace74c1500000000001976a914fc7580f1ce24b2c85c80b2c8e641592b9bbf974588ac081ad000000000001976a9145e61c81c9b3ac55bf90ab00240023e8ddaf43e1788ac02483045022100a74be4e00585637c6792eeff9279508f09163db525f8b16c03e57283d12b0a6a022042cc061d70e78e9260ff30635db4f4e0b317831a8cb1f30473a25b93b4d1b4fb01210238288b60e8c50785809a69517ae3ca2fe3e407cac8b5868c5fb3803c233dfbcc00000000 data2json n.219 vs len.586
7 years ago
7 years ago
//BTC LP_transaction_fromdata mismatched txid ae7af65215554964b7af01babb58fce10fc51d9812ced07ee4a62a1ffe10c0bb vs 051ef4f75898b511dd3aebd687d5a5bdb004226d811aaf13e64be741dbe81ff2
//failed blockchain.transaction.get BTC ae7af65215554964b7af01babb58fce10fc51d9812ced07ee4a62a1ffe10c0bb
//errortxobj.({"version":1})
//010000000001010c590740b0c558f75b64cc0d00b0533ff951e403d993b866a87c7c52981388a60300000017160014cd2b7646f9931c85bb9d4d353b652c76b40aff84ffffffff0240420f000000000017a914239ae54cd403a91b084d3244e7e423f05c25ec268728fe28010000000017a914b96e21dd6e11bb67479f9072df55881e7fb17f788702473044022076e4c1cfba0dab889598efdfdcbc7a1046cc0eecfb8fec379994d559143d271c02206b16d9a0e3c425434d1b793d61e1e491f269abc7e7d3af52d95f6a26c1c2aa0d0121037f88a6ccdd608645024b05f408324550cf6fa1c74e3d7c1f94a1e9b57369d10600000000 data2json n.-1 vs len.247
//ScriptSig: PUSHDATA(34)[002072d8f7debd607b94c95ced6cff38265ed99c7c1ee58b67d0a3f3ed43b6d9a977]
//Witness: 040047304402206f23bb2dd06bc70d78312e27d53954420d0defbda7ad638718b9dc2c1efb93100220270f79bfb3230194d2e625875073799f6f07d96d5bce43b19cfe6be9957d95540148304502210081842f5987009e65f1d7de6140433da541b9da52ed211e87e1ae6fc25260d7680220798de97c682541d7b26b0795b19994f045f3ef4f433c55da0b5f1506ddeec413014752210305345798069ac85285628732485a3ce7e96b85696ed7da72287f6eae3b275363210268aace9c841b8d61fe3532981c8b25e235690003916c5a90c23a708e5d8f929752ae
8 years ago
#include <stdio.h>
7 years ago
struct LP_millistats
{
double lastmilli,millisum,threshold;
uint32_t count;
char name[64];
7 years ago
} LP_psockloop_stats,LP_reserved_msgs_stats,utxosQ_loop_stats,command_rpcloop_stats,queue_loop_stats,prices_loop_stats,LP_coinsloop_stats,LP_coinsloopBTC_stats,LP_coinsloopKMD_stats,LP_pubkeysloop_stats,LP_privkeysloop_stats,LP_swapsloop_stats,LP_gcloop_stats;
7 years ago
extern int32_t IAMLP;
7 years ago
void LP_millistats_update(struct LP_millistats *mp)
{
double elapsed,millis;
7 years ago
if ( mp == 0 )
{
7 years ago
if ( IAMLP != 0 )
{
7 years ago
mp = &LP_psockloop_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
7 years ago
}
7 years ago
mp = &LP_reserved_msgs_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
mp = &utxosQ_loop_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
mp = &command_rpcloop_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
mp = &queue_loop_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
mp = &prices_loop_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
mp = &LP_coinsloop_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
mp = &LP_coinsloopBTC_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
mp = &LP_coinsloopKMD_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
mp = &LP_pubkeysloop_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
mp = &LP_privkeysloop_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
mp = &LP_swapsloop_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
7 years ago
mp = &LP_gcloop_stats, printf("%32s lag %10.2f millis, threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,OS_milliseconds() - mp->lastmilli,mp->threshold,mp->millisum/(mp->count > 0 ? mp->count: 1),mp->count);
7 years ago
}
7 years ago
else
{
7 years ago
if ( mp->lastmilli == 0. )
mp->lastmilli = OS_milliseconds();
else
7 years ago
{
7 years ago
mp->count++;
millis = OS_milliseconds();
elapsed = (millis - mp->lastmilli);
mp->millisum += elapsed;
if ( mp->threshold != 0. && elapsed > mp->threshold )
{
7 years ago
//if ( IAMLP == 0 )
7 years ago
printf("%32s elapsed %10.2f millis > threshold %10.2f, ave %10.2f millis, count.%u\n",mp->name,elapsed,mp->threshold,mp->millisum/mp->count,mp->count);
7 years ago
}
mp->lastmilli = millis;
7 years ago
}
}
}
7 years ago
#include "LP_include.h"
7 years ago
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;
7 years ago
int32_t LP_canbind;
char *Broadcaststr,*Reserved_msgs[2][1000];
int32_t num_Reserved_msgs[2],max_Reserved_msgs[2];
struct LP_peerinfo *LP_peerinfos,*LP_mypeer;
struct LP_forwardinfo *LP_forwardinfos;
struct iguana_info *LP_coins;
struct LP_pubkeyinfo *LP_pubkeyinfos;
7 years ago
struct rpcrequest_info *LP_garbage_collector;
7 years ago
8 years ago
8 years ago
//uint32_t LP_deadman_switch;
8 years ago
uint16_t LP_fixed_pairport,LP_publicport;
7 years ago
uint32_t LP_lastnonce,LP_counter;
8 years ago
int32_t LP_mybussock = -1;
8 years ago
int32_t LP_mypubsock = -1;
8 years ago
int32_t LP_mypullsock = -1;
int32_t LP_numfinished,LP_showwif,IAMLP = 0;
8 years ago
double LP_profitratio = 1.;
7 years ago
struct LP_privkey { bits256 privkey; uint8_t rmd160[20]; };
struct LP_globals
{
7 years ago
struct LP_utxoinfo *LP_utxoinfos[2],*LP_utxoinfos2[2];
7 years ago
bits256 LP_mypub25519,LP_privkey,LP_mypriv25519;
uint64_t LP_skipstatus[10000];
7 years ago
uint8_t LP_myrmd160[20],LP_pubsecp[33];
7 years ago
uint32_t LP_sessionid,counter;
7 years ago
int32_t LP_IAMLP,LP_pendingswaps,USERPASS_COUNTER,LP_numprivkeys,initializing,waiting,LP_numskips;
7 years ago
char USERPASS[65],USERPASS_WIFSTR[64],LP_myrmd160str[41],gui[16];
7 years ago
struct LP_privkey LP_privkeys[100];
} G;
8 years ago
7 years ago
#include "LP_network.c"
char *activecoins[] = { "BTC", "KMD" };
char GLOBAL_DBDIR[] = { "DB" };
char LP_myipaddr[64],LP_publicaddr[64],USERHOME[512] = { "/root" };
char LP_gui[16] = { "cli" };
7 years ago
char *default_LPnodes[] = { "5.9.253.195", "5.9.253.196", "5.9.253.197", "5.9.253.198", "5.9.253.199", "5.9.253.200", "5.9.253.201", "5.9.253.202", "5.9.253.203",
7 years ago
//"24.54.206.138", "173.212.225.176", "136.243.45.140", "107.72.162.127", "72.50.16.86", "51.15.202.191", "173.228.198.88",
7 years ago
"51.15.203.171", "51.15.86.136", "51.15.94.249", "51.15.80.18", "51.15.91.40", "51.15.54.2", "51.15.86.31", "51.15.82.29", "51.15.89.155",
7 years ago
};//"5.9.253.204" }; //
8 years ago
// stubs
8 years ago
void tradebot_swap_balancingtrade(struct basilisk_swap *swap,int32_t iambob)
{
}
void tradebot_pendingadd(cJSON *tradejson,char *base,double basevolume,char *rel,double relvolume)
{
// add to trades
}
8 years ago
8 years ago
char *LP_getdatadir()
{
8 years ago
return(USERHOME);
8 years ago
}
8 years ago
8 years ago
char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_t skip)
{
return(0);
}
8 years ago
#include "LP_socket.c"
8 years ago
#include "LP_secp.c"
#include "LP_bitcoin.c"
8 years ago
#include "LP_coins.c"
#include "LP_rpc.c"
7 years ago
#include "LP_cache.c"
7 years ago
#include "LP_RTmetrics.c"
7 years ago
#include "LP_utxo.c"
7 years ago
#include "LP_prices.c"
8 years ago
#include "LP_scan.c"
8 years ago
#include "LP_transaction.c"
7 years ago
#include "LP_stats.c"
8 years ago
#include "LP_remember.c"
#include "LP_swap.c"
8 years ago
#include "LP_peers.c"
#include "LP_utxos.c"
8 years ago
#include "LP_forwarding.c"
7 years ago
#include "LP_signatures.c"
8 years ago
#include "LP_ordermatch.c"
#include "LP_tradebots.c"
8 years ago
#include "LP_portfolio.c"
8 years ago
#include "LP_messages.c"
8 years ago
#include "LP_commands.c"
8 years ago
char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen)
8 years ago
{
char *retstr=0;
8 years ago
if ( jobj(argjson,"result") != 0 || jobj(argjson,"error") != 0 )
return(0);
7 years ago
//double millis = OS_milliseconds();
8 years ago
if ( LP_tradecommand(ctx,myipaddr,pubsock,argjson,data,datalen) <= 0 )
8 years ago
{
8 years ago
if ( (retstr= stats_JSON(ctx,myipaddr,pubsock,argjson,"127.0.0.1",0)) != 0 )
8 years ago
{
8 years ago
//printf("%s PULL.[%d]-> (%s)\n",myipaddr != 0 ? myipaddr : "127.0.0.1",datalen,retstr);
8 years ago
//if ( pubsock >= 0 ) //strncmp("{\"error\":",retstr,strlen("{\"error\":")) != 0 &&
//LP_send(pubsock,retstr,(int32_t)strlen(retstr)+1,0);
8 years ago
}
7 years ago
} //else printf("finished tradecommand (%s)\n",jprint(argjson,0));
7 years ago
//if ( OS_milliseconds()-millis > 100 )
// printf("%.3f %s\n",OS_milliseconds()-millis,jprint(argjson,0));
8 years ago
return(retstr);
}
8 years ago
char *LP_decrypt(uint8_t *ptr,int32_t *recvlenp)
{
uint8_t decoded[LP_ENCRYPTED_MAXSIZE + crypto_box_ZEROBYTES],*nonce,*cipher; int32_t recvlen,cipherlen; char *jsonstr = 0;
recvlen = *recvlenp;
nonce = &ptr[2];
cipher = &ptr[2 + crypto_box_NONCEBYTES];
cipherlen = recvlen - (2 + crypto_box_NONCEBYTES);
8 years ago
if ( cipherlen > 0 && cipherlen <= sizeof(decoded) )
8 years ago
{
7 years ago
if ( (jsonstr= (char *)_SuperNET_decipher(nonce,cipher,decoded,cipherlen,GENESIS_PUBKEY,G.LP_mypriv25519)) != 0 )
8 years ago
{
recvlen = (cipherlen - crypto_box_ZEROBYTES);
if ( strlen(jsonstr)+1 != recvlen )
{
printf("unexpected len %d vs recvlen.%d\n",(int32_t)strlen(jsonstr)+1,recvlen);
jsonstr = 0;
7 years ago
} //else printf("decrypted (%s)\n",jsonstr);
8 years ago
}
} else printf("cipher.%d too big for %d\n",cipherlen,(int32_t)sizeof(decoded));
*recvlenp = recvlen;
return(jsonstr);
}
8 years ago
char *LP_process_message(void *ctx,char *typestr,char *myipaddr,int32_t pubsock,uint8_t *ptr,int32_t recvlen,int32_t recvsock)
8 years ago
{
8 years ago
static uint32_t dup,uniq;
8 years ago
int32_t i,len,cipherlen,datalen=0,duplicate=0,encrypted=0; char *method,*method2,*tmp,*cipherstr,*retstr=0,*jsonstr=0; cJSON *argjson; uint32_t crc32;
7 years ago
//double millis = OS_milliseconds();
8 years ago
crc32 = calc_crc32(0,&ptr[2],recvlen-2);
if ( (crc32 & 0xff) == ptr[0] && ((crc32>>8) & 0xff) == ptr[1] )
encrypted = 1;
i = LP_crc32find(&duplicate,-1,crc32);
8 years ago
if ( duplicate != 0 )
dup++;
else uniq++;
7 years ago
portable_mutex_lock(&LP_commandmutex);
7 years ago
if ( (rand() % 10000) == 0 )
7 years ago
printf("%s dup.%d (%u / %u) %.1f%% encrypted.%d recv.%u [%02x %02x] vs %02x %02x\n",typestr,duplicate,dup,dup+uniq,(double)100*dup/(dup+uniq),encrypted,crc32,ptr[0],ptr[1],crc32&0xff,(crc32>>8)&0xff);
8 years ago
if ( duplicate == 0 )
{
8 years ago
if ( i >= 0 )
LP_crc32find(&duplicate,i,crc32);
8 years ago
if ( encrypted != 0 )
8 years ago
jsonstr = LP_decrypt(ptr,&recvlen);
8 years ago
else if ( (datalen= is_hexstr((char *)ptr,0)) > 0 )
{
datalen >>= 1;
jsonstr = malloc(datalen + 1);
decode_hex((void *)jsonstr,datalen,(char *)ptr);
jsonstr[datalen] = 0;
} else jsonstr = (char *)ptr;
if ( jsonstr != 0 && (argjson= cJSON_Parse(jsonstr)) != 0 )
{
8 years ago
uint8_t decoded[LP_ENCRYPTED_MAXSIZE + crypto_box_ZEROBYTES];
8 years ago
//printf("[%s]\n",jsonstr);
8 years ago
cipherlen = 0;
8 years ago
if ( (cipherstr= jstr(argjson,"cipher")) != 0 && (cipherlen= is_hexstr(cipherstr,0)) > 32 && cipherlen <= sizeof(decoded)*2 )
{
8 years ago
method2 = jstr(argjson,"method2");
if ( (method= jstr(argjson,"method")) != 0 && (strcmp(method,"encrypted") == 0 ||(method2 != 0 && strcmp(method2,"encrypted") == 0)) )
8 years ago
{
cipherlen >>= 1;
decode_hex(decoded,cipherlen,cipherstr);
crc32 = calc_crc32(0,&decoded[2],cipherlen-2);
8 years ago
if ( (tmp= LP_decrypt(decoded,&cipherlen)) != 0 )
8 years ago
{
8 years ago
jsonstr = tmp;
free_json(argjson);
8 years ago
argjson = cJSON_Parse(jsonstr);
8 years ago
recvlen = cipherlen;
8 years ago
encrypted = 1;
if ( (crc32 & 0xff) == decoded[0] && ((crc32>>8) & 0xff) == decoded[1] )
{
i = LP_crc32find(&duplicate,-1,crc32);
if ( duplicate == 0 && i >= 0 )
LP_crc32find(&duplicate,i,crc32);
}
printf("%02x %02x %08x duplicate.%d decrypted.(%s)\n",decoded[0],decoded[1],crc32,duplicate,jsonstr);
8 years ago
}
else
{
7 years ago
//printf("packet not for this node %u\n",crc32);
8 years ago
}
8 years ago
} else printf("error (%s) method is %s\n",jsonstr,method);
8 years ago
}
8 years ago
if ( jsonstr != 0 && argjson != 0 )
8 years ago
{
8 years ago
len = (int32_t)strlen(jsonstr) + 1;
7 years ago
if ( (method= jstr(argjson,"method")) != 0 && strcmp(method,"broadcast") == 0 )
8 years ago
{
7 years ago
bits256 zero; cJSON *reqjson; char *cipherstr; int32_t cipherlen; uint8_t cipher[LP_ENCRYPTED_MAXSIZE];
if ( (reqjson= LP_dereference(argjson,"broadcast")) != 0 )
{
Broadcaststr = jprint(reqjson,0);
if ( (cipherstr= jstr(reqjson,"cipher")) != 0 )
{
cipherlen = (int32_t)strlen(cipherstr) >> 1;
if ( cipherlen <= sizeof(cipher) )
{
decode_hex(cipher,cipherlen,cipherstr);
LP_queuesend(calc_crc32(0,&cipher[2],cipherlen-2),LP_mypubsock,"","",cipher,cipherlen);
} else retstr = clonestr("{\"error\":\"cipher too big\"}");
}
else
{
memset(zero.bytes,0,sizeof(zero));
7 years ago
/*if ( (method= jstr(reqjson,"method")) != 0 && (strcmp(method,"request") == 0 || strcmp(method,"requested") == 0 || strcmp(method,"connect") == 0 || strcmp(method,"connected") == 0) )
printf("broadcast.(%s)\n",Broadcaststr);*/
7 years ago
LP_reserved_msg(0,"","",zero,jprint(reqjson,0));
7 years ago
}
retstr = clonestr("{\"result\":\"success\"}");
7 years ago
free_json(reqjson);
7 years ago
} else retstr = clonestr("{\"error\":\"couldnt dereference sendmessage\"}");
}
else
{
if ( (retstr= LP_command_process(ctx,myipaddr,pubsock,argjson,&((uint8_t *)ptr)[len],recvlen - len)) != 0 )
{
}
//printf("%.3f %s LP_command_process\n",OS_milliseconds()-millis,jstr(argjson,"method"));
8 years ago
}
free_json(argjson);
8 years ago
}
}
8 years ago
} //else printf("DUPLICATE.(%s)\n",(char *)ptr);
7 years ago
portable_mutex_unlock(&LP_commandmutex);
8 years ago
if ( jsonstr != 0 && (void *)jsonstr != (void *)ptr && encrypted == 0 )
8 years ago
free(jsonstr);
8 years ago
return(retstr);
8 years ago
}
7 years ago
int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int32_t sock,char *remoteaddr,int32_t maxdepth)
8 years ago
{
7 years ago
int32_t recvlen=1,nonz = 0; cJSON *argjson; void *ptr; char methodstr[64],*retstr,*str; struct nn_pollfd pfd;
8 years ago
if ( sock >= 0 )
{
7 years ago
while ( nonz < maxdepth && recvlen > 0 )
8 years ago
{
7 years ago
nonz++;
8 years ago
memset(&pfd,0,sizeof(pfd));
8 years ago
pfd.fd = sock;
pfd.events = NN_POLLIN;
7 years ago
if ( nn_poll(&pfd,1,1) != 1 )
8 years ago
break;
8 years ago
if ( (recvlen= nn_recv(sock,&ptr,NN_MSG,0)) > 0 )
{
7 years ago
methodstr[0] = 0;
7 years ago
//printf("%s.(%s)\n",typestr,(char *)ptr);
7 years ago
if ( 0 )
7 years ago
{
7 years ago
cJSON *recvjson; char *mstr;//,*cstr;
7 years ago
if ( (recvjson= cJSON_Parse((char *)ptr)) != 0 )
{
7 years ago
if ( (mstr= jstr(recvjson,"method")) != 0 )//&& strcmp(mstr,"uitem") == 0 && (cstr= jstr(recvjson,"coin")) != 0 && strcmp(cstr,"REVS") == 0 )
7 years ago
{
7 years ago
//printf("%s RECV.(%s)\n",typestr,(char *)ptr);
7 years ago
}
7 years ago
safecopy(methodstr,jstr(recvjson,"method"),sizeof(methodstr));
7 years ago
free_json(recvjson);
}
}
7 years ago
int32_t validreq = 0;
7 years ago
if ( strlen((char *)ptr)+sizeof(bits256) <= recvlen )
7 years ago
{
7 years ago
if ( LP_magic_check(ptr,recvlen,remoteaddr) <= 0 )
7 years ago
{
//printf("magic check error\n");
7 years ago
} else validreq = 1;
7 years ago
recvlen -= sizeof(bits256);
}
7 years ago
if ( validreq != 0 )
7 years ago
{
7 years ago
if ( (retstr= LP_process_message(ctx,typestr,myipaddr,pubsock,ptr,recvlen,sock)) != 0 )
free(retstr);
if ( Broadcaststr != 0 )
7 years ago
{
7 years ago
//printf("self broadcast.(%s)\n",Broadcaststr);
str = Broadcaststr;
Broadcaststr = 0;
if ( (argjson= cJSON_Parse(str)) != 0 )
7 years ago
{
7 years ago
portable_mutex_lock(&LP_commandmutex);
7 years ago
if ( LP_tradecommand(ctx,myipaddr,pubsock,argjson,0,0) <= 0 )
{
if ( (retstr= stats_JSON(ctx,myipaddr,pubsock,argjson,remoteaddr,0)) != 0 )
free(retstr);
}
7 years ago
portable_mutex_unlock(&LP_commandmutex);
7 years ago
free_json(argjson);
7 years ago
}
7 years ago
free(str);
7 years ago
}
7 years ago
}
7 years ago
if ( ptr != 0 )
nn_freemsg(ptr), ptr = 0;
8 years ago
}
}
}
return(nonz);
}
7 years ago
int32_t LP_nanomsg_recvs(void *ctx)
8 years ago
{
7 years ago
int32_t nonz = 0; char *origipaddr; struct LP_peerinfo *peer,*tmp;
7 years ago
if ( (origipaddr= LP_myipaddr) == 0 )
8 years ago
origipaddr = "127.0.0.1";
7 years ago
//portable_mutex_lock(&LP_nanorecvsmutex);
7 years ago
HASH_ITER(hh,LP_peerinfos,peer,tmp)
8 years ago
{
7 years ago
if ( peer->errors >= LP_MAXPEER_ERRORS )
8 years ago
{
7 years ago
if ( (rand() % 10000) == 0 )
peer->errors--;
else
8 years ago
{
7 years ago
//printf("skip %s\n",peer->ipaddr);
continue;
8 years ago
}
}
7 years ago
//printf("check %s pubsock.%d\n",peer->ipaddr,peer->subsock);
7 years ago
nonz += LP_sock_check("PULL",ctx,origipaddr,LP_mypubsock,peer->subsock,peer->ipaddr,1);
7 years ago
}
/*HASH_ITER(hh,LP_coins,coin,ctmp) // firstrefht,firstscanht,lastscanht
{
if ( coin->inactive != 0 )
continue;
if ( coin->bussock >= 0 )
nonz += LP_sock_check(coin->symbol,ctx,origipaddr,-1,coin->bussock,LP_profitratio - 1.);
}*/
if ( LP_mypullsock >= 0 )
7 years ago
{
7 years ago
nonz += LP_sock_check("SUB",ctx,origipaddr,-1,LP_mypullsock,"127.0.0.1",1);
7 years ago
}
7 years ago
//portable_mutex_unlock(&LP_nanorecvsmutex);
7 years ago
return(nonz);
}
void command_rpcloop(void *myipaddr)
{
int32_t nonz = 0; void *ctx;
ctx = bitcoin_ctx();
7 years ago
strcpy(command_rpcloop_stats.name,"command_rpcloop");
7 years ago
command_rpcloop_stats.threshold = 1000.;
7 years ago
while ( 1 )
{
7 years ago
LP_millistats_update(&command_rpcloop_stats);
7 years ago
nonz = LP_nanomsg_recvs(ctx);
8 years ago
//if ( LP_mybussock >= 0 )
// nonz += LP_sock_check("BUS",ctx,origipaddr,-1,LP_mybussock);
8 years ago
if ( nonz == 0 )
7 years ago
{
if ( IAMLP != 0 )
7 years ago
usleep(10000);
else usleep(50000);
7 years ago
}
7 years ago
else if ( IAMLP == 0 )
7 years ago
usleep(1000);
8 years ago
}
}
7 years ago
void utxosQ_loop(void *myipaddr)
{
7 years ago
strcpy(utxosQ_loop_stats.name,"utxosQ_loop");
7 years ago
utxosQ_loop_stats.threshold = 5000.;
7 years ago
while ( 1 )
{
7 years ago
LP_millistats_update(&utxosQ_loop_stats);
7 years ago
if ( LP_utxosQ_process() == 0 )
7 years ago
usleep(50000);
7 years ago
}
}
7 years ago
void LP_coinsloop(void *_coins)
7 years ago
{
7 years ago
struct LP_address *ap=0,*atmp; struct LP_transaction *tx; cJSON *retjson; struct LP_address_utxo *up,*tmp; struct iguana_info *coin,*ctmp; char str[65]; struct electrum_info *ep,*backupep=0; bits256 zero; int32_t oldht,j,nonz; char *coins = _coins;
7 years ago
if ( strcmp("BTC",coins) == 0 )
{
strcpy(LP_coinsloopBTC_stats.name,"BTC coin loop");
7 years ago
LP_coinsloopBTC_stats.threshold = 20000.;
7 years ago
}
else if ( strcmp("KMD",coins) == 0 )
{
strcpy(LP_coinsloopKMD_stats.name,"KMD coin loop");
7 years ago
LP_coinsloopKMD_stats.threshold = 10000.;
7 years ago
}
else
{
strcpy(LP_coinsloop_stats.name,"other coins loop");
7 years ago
LP_coinsloop_stats.threshold = 5000.;
7 years ago
}
7 years ago
while ( 1 )
{
7 years ago
if ( strcmp("BTC",coins) == 0 )
LP_millistats_update(&LP_coinsloopBTC_stats);
else if ( strcmp("KMD",coins) == 0 )
LP_millistats_update(&LP_coinsloopKMD_stats);
else LP_millistats_update(&LP_coinsloop_stats);
7 years ago
nonz = 0;
HASH_ITER(hh,LP_coins,coin,ctmp) // firstrefht,firstscanht,lastscanht
{
if ( coins != 0 )
7 years ago
{
if ( coins[0] != 0 )
{
if ( strcmp(coins,coin->symbol) != 0 )
continue;
}
else // avoid hardcode special case LP_coinsloop
{
if ( strcmp("BTC",coin->symbol) == 0 || strcmp("KMD",coin->symbol) == 0 )
continue;
}
7 years ago
}
7 years ago
memset(&zero,0,sizeof(zero));
if ( coin->inactive != 0 )
continue;
7 years ago
if ( coin->longestchain == 1 ) // special init value
coin->longestchain = LP_getheight(coin);
7 years ago
if ( (ep= coin->electrum) != 0 )
{
if ( (backupep= ep->prev) == 0 )
backupep = ep;
7 years ago
// skip cLP_address MNZ bXcSsYBiVKtTzYErqxvma4UsojZTEf5L6H
//printf("electrum %s\n",coin->symbol);
7 years ago
if ( (ap= LP_addressfind(coin,coin->smartaddr)) != 0 )
7 years ago
{
7 years ago
if ( (retjson= electrum_address_listunspent(coin->symbol,ep,&retjson,ap->coinaddr,1)) != 0 )
free_json(retjson);
7 years ago
}
HASH_ITER(hh,coin->addresses,ap,atmp)
{
7 years ago
//printf("call unspent %s\n",ap->coinaddr);
7 years ago
if ( (retjson= electrum_address_listunspent(coin->symbol,ep,&retjson,ap->coinaddr,1)) != 0 )
free_json(retjson);
}
7 years ago
if ( (ap= LP_addressfind(coin,coin->smartaddr)) != 0 )
7 years ago
{
7 years ago
DL_FOREACH_SAFE(ap->utxos,up,tmp)
{
7 years ago
if ( up->U.height > 0 && up->spendheight < 0 )
7 years ago
{
7 years ago
if ( up->SPV == 0 )
{
nonz++;
7 years ago
up->SPV = LP_merkleproof(coin,coin->smartaddr,backupep,up->U.txid,up->U.height);
7 years ago
if ( up->SPV > 0 )
{
if ( (tx= LP_transactionfind(coin,up->U.txid)) != 0 && tx->SPV == 0 )
{
tx->SPV = up->SPV;
7 years ago
//printf("%s %s: SPV.%d\n",coin->symbol,bits256_str(str,up->U.txid),up->SPV);
7 years ago
}
}
7 years ago
}
else if ( up->SPV == -1 )
{
nonz++;
printf("SPV failure for %s %s\n",coin->symbol,bits256_str(str,up->U.txid));
oldht = up->U.height;
LP_txheight_check(coin,ap->coinaddr,up);
if ( oldht != up->U.height )
7 years ago
up->SPV = LP_merkleproof(coin,coin->smartaddr,backupep,up->U.txid,up->U.height);
7 years ago
if ( up->SPV <= 0 )
up->SPV = -2;
else printf("%s %s: corrected SPV.%d\n",coin->symbol,bits256_str(str,up->U.txid),up->SPV);
}
7 years ago
}
}
}
7 years ago
while ( ep != 0 )
{
if ( time(NULL) > ep->keepalive+LP_ELECTRUM_KEEPALIVE )
{
7 years ago
//printf("%s electrum.%p needs a keepalive: lag.%d\n",ep->symbol,ep,(int32_t)(time(NULL) - ep->keepalive));
7 years ago
if ( (retjson= electrum_address_listunspent(coin->symbol,ep,&retjson,coin->smartaddr,1)) != 0 )
7 years ago
free_json(retjson);
}
ep = ep->prev;
}
7 years ago
continue;
7 years ago
}
7 years ago
if ( coin->firstrefht == 0 )
continue;
else if ( coin->firstscanht == 0 )
coin->lastscanht = coin->firstscanht = coin->firstrefht;
else if ( coin->firstrefht < coin->firstscanht )
{
printf("detected %s firstrefht.%d < firstscanht.%d\n",coin->symbol,coin->firstrefht,coin->firstscanht);
coin->lastscanht = coin->firstscanht = coin->firstrefht;
}
if ( coin->lastscanht == coin->longestchain+1 )
{
7 years ago
//printf("%s lastscanht.%d is longest.%d + 1\n",coin->symbol,coin->lastscanht,coin->longestchain);
7 years ago
continue;
}
else if ( coin->lastscanht > coin->longestchain+1 )
{
printf("detected chain rewind lastscanht.%d vs longestchain.%d, first.%d ref.%d\n",coin->lastscanht,coin->longestchain,coin->firstscanht,coin->firstrefht);
LP_undospends(coin,coin->longestchain-1);
//LP_mempoolscan(coin->symbol,zero);
coin->lastscanht = coin->longestchain - 1;
if ( coin->firstscanht < coin->lastscanht )
coin->lastscanht = coin->firstscanht;
continue;
}
7 years ago
nonz++;
7 years ago
if ( 0 && coin->lastscanht < coin->longestchain-3 )
7 years ago
printf("[%s]: %s ref.%d scan.%d to %d, longest.%d\n",coins,coin->symbol,coin->firstrefht,coin->firstscanht,coin->lastscanht,coin->longestchain);
7 years ago
for (j=0; j<100; j++)
7 years ago
{
if ( LP_blockinit(coin,coin->lastscanht) < 0 )
{
printf("blockinit.%s %d error\n",coin->symbol,coin->lastscanht);
break;
}
coin->lastscanht++;
if ( coin->lastscanht == coin->longestchain+1 )
break;
}
}
if ( coins == 0 )
return;
7 years ago
if ( nonz == 0 )
7 years ago
usleep(100000);
7 years ago
}
}
7 years ago
int32_t LP_mainloop_iter(void *ctx,char *myipaddr,struct LP_peerinfo *mypeer,int32_t pubsock,char *pushaddr,uint16_t myport)
8 years ago
{
7 years ago
static uint32_t counter;
struct iguana_info *coin,*ctmp; char *origipaddr; uint32_t now; int32_t height,nonz = 0;
8 years ago
if ( (origipaddr= myipaddr) == 0 )
origipaddr = "127.0.0.1";
if ( mypeer == 0 )
8 years ago
myipaddr = "127.0.0.1";
HASH_ITER(hh,LP_coins,coin,ctmp) // firstrefht,firstscanht,lastscanht
{
7 years ago
now = (uint32_t)time(NULL);
if ( (coin->addr_listunspent_requested != 0 && now > coin->lastpushtime+LP_ORDERBOOK_DURATION*.5) || now > coin->lastpushtime+LP_ORDERBOOK_DURATION*5 )
7 years ago
{
7 years ago
//printf("PUSH addr_listunspent_requested %u\n",coin->addr_listunspent_requested);
7 years ago
coin->lastpushtime = (uint32_t)now;
7 years ago
LP_smartutxos_push(coin);
7 years ago
coin->addr_listunspent_requested = 0;
}
7 years ago
if ( coin->electrum == 0 && coin->inactive == 0 && now > coin->lastgetinfo+LP_GETINFO_INCR )
{
7 years ago
nonz++;
7 years ago
if ( (height= LP_getheight(coin)) > coin->longestchain )
{
coin->longestchain = height;
7 years ago
if ( 0 && coin->firstrefht != 0 )
7 years ago
printf(">>>>>>>>>> set %s longestchain %d (ref.%d [%d, %d])\n",coin->symbol,height,coin->firstrefht,coin->firstscanht,coin->lastscanht);
7 years ago
} //else LP_mempoolscan(coin->symbol,zero);
7 years ago
coin->lastgetinfo = (uint32_t)now;
}
}
8 years ago
counter++;
return(nonz);
}
7 years ago
void LP_initcoins(void *ctx,int32_t pubsock,cJSON *coins)
8 years ago
{
7 years ago
int32_t i,n; cJSON *item; char *symbol; struct iguana_info *coin;
8 years ago
for (i=0; i<sizeof(activecoins)/sizeof(*activecoins); i++)
8 years ago
{
7 years ago
printf("%s ",activecoins[i]);
8 years ago
LP_coinfind(activecoins[i]);
8 years ago
LP_priceinfoadd(activecoins[i]);
7 years ago
if ( (coin= LP_coinfind(activecoins[i])) != 0 )
{
if ( LP_getheight(coin) <= 0 )
coin->inactive = (uint32_t)time(NULL);
7 years ago
else LP_unspents_load(coin->symbol,coin->smartaddr);
7 years ago
if ( coin->txfee == 0 && strcmp(coin->symbol,"BTC") != 0 )
coin->txfee = LP_MIN_TXFEE;
7 years ago
}
8 years ago
}
8 years ago
if ( (n= cJSON_GetArraySize(coins)) > 0 )
{
for (i=0; i<n; i++)
8 years ago
{
item = jitem(coins,i);
7 years ago
if ( (symbol= jstr(item,"coin")) != 0 )
{
printf("%s ",jstr(item,"coin"));
LP_coincreate(item);
LP_priceinfoadd(jstr(item,"coin"));
7 years ago
if ( (coin= LP_coinfind(symbol)) != 0 )
{
if ( LP_getheight(coin) <= 0 )
coin->inactive = (uint32_t)time(NULL);
7 years ago
else LP_unspents_load(coin->symbol,coin->smartaddr);
7 years ago
if ( coin->txfee == 0 && strcmp(coin->symbol,"BTC") != 0 )
coin->txfee = LP_MIN_TXFEE;
7 years ago
}
7 years ago
}
8 years ago
}
8 years ago
}
7 years ago
printf("privkey updates\n");
8 years ago
}
7 years ago
void LP_initpeers(int32_t pubsock,struct LP_peerinfo *mypeer,char *myipaddr,uint16_t myport,char *seednode,uint16_t pushport,uint16_t subport)
8 years ago
{
8 years ago
int32_t i,j; uint32_t r;
if ( IAMLP != 0 )
8 years ago
{
7 years ago
LP_mypeer = mypeer = LP_addpeer(mypeer,pubsock,myipaddr,myport,pushport,subport,1,G.LP_sessionid);
8 years ago
if ( myipaddr == 0 || mypeer == 0 )
8 years ago
{
8 years ago
printf("couldnt get myipaddr or null mypeer.%p\n",mypeer);
exit(-1);
8 years ago
}
8 years ago
if ( seednode == 0 || seednode[0] == 0 )
{
for (i=0; i<sizeof(default_LPnodes)/sizeof(*default_LPnodes); i++)
{
7 years ago
LP_addpeer(mypeer,pubsock,default_LPnodes[i],myport,pushport,subport,0,G.LP_sessionid);
8 years ago
}
7 years ago
} else LP_addpeer(mypeer,pubsock,seednode,myport,pushport,subport,0,G.LP_sessionid);
8 years ago
}
else
{
if ( myipaddr == 0 )
{
printf("couldnt get myipaddr\n");
exit(-1);
}
if ( seednode == 0 || seednode[0] == 0 )
{
7 years ago
//LP_addpeer(mypeer,pubsock,"51.15.86.136",myport,pushport,subport,0,G.LP_sessionid);
8 years ago
OS_randombytes((void *)&r,sizeof(r));
7 years ago
//r = 0;
8 years ago
for (j=0; j<sizeof(default_LPnodes)/sizeof(*default_LPnodes); j++)
{
i = (r + j) % (sizeof(default_LPnodes)/sizeof(*default_LPnodes));
7 years ago
LP_addpeer(mypeer,pubsock,default_LPnodes[i],myport,pushport,subport,0,G.LP_sessionid);
7 years ago
//issue_LP_getpeers(default_LPnodes[i],myport);
7 years ago
//LP_peersquery(mypeer,pubsock,default_LPnodes[i],myport,"127.0.0.1",myport);
8 years ago
}
7 years ago
} else LP_addpeer(mypeer,pubsock,seednode,myport,pushport,subport,0,G.LP_sessionid);
8 years ago
}
8 years ago
}
7 years ago
void LP_pubkeysloop(void *ctx)
{
7 years ago
static uint32_t lasttime; cJSON *retjson; struct iguana_info *coin,*tmp;
7 years ago
strcpy(LP_pubkeysloop_stats.name,"LP_pubkeysloop");
7 years ago
LP_pubkeysloop_stats.threshold = 15000.;
7 years ago
sleep(10);
while ( 1 )
{
7 years ago
LP_millistats_update(&LP_pubkeysloop_stats);
7 years ago
HASH_ITER(hh,LP_coins,coin,tmp) // firstrefht,firstscanht,lastscanht
{
if ( coin->electrum != 0 && time(NULL) > coin->lastunspent+30 )
{
//printf("call electrum listunspent.%s\n",coin->symbol);
if ( (retjson= electrum_address_listunspent(coin->symbol,coin->electrum,&retjson,coin->smartaddr,2)) != 0 )
free_json(retjson);
coin->lastunspent = (uint32_t)time(NULL);
}
}
7 years ago
if ( time(NULL) > lasttime+LP_ORDERBOOK_DURATION*0.5 )
7 years ago
{
7 years ago
//printf("LP_pubkeysloop %u\n",(uint32_t)time(NULL));
7 years ago
LP_notify_pubkeys(ctx,LP_mypubsock);
lasttime = (uint32_t)time(NULL);
}
sleep(3);
7 years ago
}
}
void LP_privkeysloop(void *ctx)
{
7 years ago
strcpy(LP_privkeysloop_stats.name,"LP_privkeysloop");
7 years ago
LP_privkeysloop_stats.threshold = (LP_ORDERBOOK_DURATION * .8 * 1000) + 10000;
7 years ago
sleep(20);
while ( 1 )
{
7 years ago
LP_millistats_update(&LP_privkeysloop_stats);
7 years ago
LP_counter += 1000;
7 years ago
//printf("LP_privkeysloop %u\n",LP_counter);
7 years ago
LP_privkey_updates(ctx,LP_mypubsock,0);
7 years ago
sleep(LP_ORDERBOOK_DURATION * .777);
7 years ago
}
}
void LP_swapsloop(void *ignore)
{
char *retstr;
7 years ago
strcpy(LP_swapsloop_stats.name,"LP_swapsloop");
LP_swapsloop_stats.threshold = 605000.;
7 years ago
sleep(50);
while ( 1 )
{
7 years ago
LP_millistats_update(&LP_swapsloop_stats);
7 years ago
LP_counter += 10000;
7 years ago
//printf("LP_swapsloop %u\n",LP_counter);
7 years ago
if ( (retstr= basilisk_swapentry(0,0)) != 0 )
free(retstr);
7 years ago
//LP_millistats_update(0);
7 years ago
sleep(600);
}
}
7 years ago
void LP_reserved_msgs(void *ignore)
7 years ago
{
7 years ago
bits256 zero; int32_t flag,nonz; struct nn_pollfd pfd;
7 years ago
memset(zero.bytes,0,sizeof(zero));
7 years ago
strcpy(LP_reserved_msgs_stats.name,"LP_reserved_msgs");
7 years ago
LP_reserved_msgs_stats.threshold = 150.;
7 years ago
while ( 1 )
7 years ago
{
7 years ago
nonz = 0;
7 years ago
LP_millistats_update(&LP_reserved_msgs_stats);
7 years ago
if ( num_Reserved_msgs[0] > 0 || num_Reserved_msgs[1] > 0 )
7 years ago
{
7 years ago
nonz++;
7 years ago
flag = 0;
if ( LP_mypubsock >= 0 )
{
memset(&pfd,0,sizeof(pfd));
pfd.fd = LP_mypubsock;
pfd.events = NN_POLLOUT;
if ( nn_poll(&pfd,1,1) == 1 )
flag = 1;
} else flag = 1;
if ( flag == 1 )
7 years ago
{
portable_mutex_lock(&LP_reservedmutex);
7 years ago
if ( num_Reserved_msgs[1] > 0 )
{
num_Reserved_msgs[1]--;
7 years ago
//printf("PRIORITY BROADCAST.(%s)\n",Reserved_msgs[1][num_Reserved_msgs[1]]);
7 years ago
LP_broadcast_message(LP_mypubsock,"","",zero,Reserved_msgs[1][num_Reserved_msgs[1]]);
Reserved_msgs[1][num_Reserved_msgs[1]] = 0;
}
else if ( num_Reserved_msgs[0] > 0 )
{
num_Reserved_msgs[0]--;
7 years ago
//printf("BROADCAST.(%s)\n",Reserved_msgs[0][num_Reserved_msgs[0]]);
7 years ago
LP_broadcast_message(LP_mypubsock,"","",zero,Reserved_msgs[0][num_Reserved_msgs[0]]);
Reserved_msgs[0][num_Reserved_msgs[0]] = 0;
}
7 years ago
portable_mutex_unlock(&LP_reservedmutex);
7 years ago
}
}
7 years ago
if ( ignore == 0 )
break;
7 years ago
if ( nonz != 0 )
7 years ago
usleep(1000);
else usleep(5000);
7 years ago
}
}
7 years ago
int32_t LP_reserved_msg(int32_t priority,char *base,char *rel,bits256 pubkey,char *msg)
7 years ago
{
int32_t n = 0;
portable_mutex_lock(&LP_reservedmutex);
7 years ago
if ( num_Reserved_msgs[priority] < sizeof(Reserved_msgs[priority])/sizeof(*Reserved_msgs[priority]) )
7 years ago
{
7 years ago
Reserved_msgs[priority][num_Reserved_msgs[priority]++] = msg;
n = num_Reserved_msgs[priority];
7 years ago
} else LP_broadcast_message(LP_mypubsock,base,rel,pubkey,msg);
7 years ago
if ( num_Reserved_msgs[priority] > max_Reserved_msgs[priority] )
7 years ago
{
7 years ago
max_Reserved_msgs[priority] = num_Reserved_msgs[priority];
7 years ago
if ( (max_Reserved_msgs[priority] % 100) == 0 )
7 years ago
printf("New priority.%d max_Reserved_msgs.%d\n",priority,max_Reserved_msgs[priority]);
7 years ago
}
7 years ago
portable_mutex_unlock(&LP_reservedmutex);
7 years ago
return(n);
}
8 years ago
void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybusport,char *passphrase,int32_t amclient,char *userhome,cJSON *argjson)
8 years ago
{
7 years ago
char *myipaddr=0; long filesize,n; int32_t valid,timeout,pubsock=-1; struct LP_peerinfo *mypeer=0; char pushaddr[128],subaddr[128],bindaddr[128],*coins_str=0; cJSON *coinsjson=0; void *ctx = bitcoin_ctx();
7 years ago
printf("Marketmaker %s.%s %s\n",LP_MAJOR_VERSION,LP_MINOR_VERSION,LP_BUILD_NUMBER);
8 years ago
LP_showwif = juint(argjson,"wif");
8 years ago
if ( passphrase == 0 || passphrase[0] == 0 )
{
printf("jeezy says we cant use the nullstring as passphrase and I agree\n");
exit(-1);
}
8 years ago
IAMLP = !amclient;
8 years ago
#ifndef __linux__
if ( IAMLP != 0 )
{
printf("must run a unix node for LP node\n");
exit(-1);
}
#endif
8 years ago
OS_randombytes((void *)&n,sizeof(n));
8 years ago
if ( jobj(argjson,"gui") != 0 )
safecopy(LP_gui,jstr(argjson,"gui"),sizeof(LP_gui));
8 years ago
if ( jobj(argjson,"canbind") == 0 )
8 years ago
{
#ifndef __linux__
8 years ago
LP_canbind = IAMLP;
8 years ago
#else
8 years ago
LP_canbind = IAMLP;
8 years ago
#endif
8 years ago
}
else
{
LP_canbind = jint(argjson,"canbind");
printf(">>>>>>>>>>> set LP_canbind.%d\n",LP_canbind);
}
8 years ago
if ( LP_canbind > 1000 && LP_canbind < 65536 )
LP_fixed_pairport = LP_canbind;
if ( LP_canbind != 0 )
LP_canbind = 1;
8 years ago
srand((int32_t)n);
8 years ago
if ( userhome != 0 && userhome[0] != 0 )
8 years ago
{
8 years ago
safecopy(USERHOME,userhome,sizeof(USERHOME));
8 years ago
#ifdef __APPLE__
strcat(USERHOME,"/Library/Application Support");
#endif
}
8 years ago
portable_mutex_init(&LP_peermutex);
portable_mutex_init(&LP_utxomutex);
8 years ago
portable_mutex_init(&LP_UTXOmutex);
8 years ago
portable_mutex_init(&LP_commandmutex);
8 years ago
portable_mutex_init(&LP_swaplistmutex);
8 years ago
portable_mutex_init(&LP_cachemutex);
8 years ago
portable_mutex_init(&LP_networkmutex);
7 years ago
portable_mutex_init(&LP_gcmutex);
8 years ago
portable_mutex_init(&LP_forwardmutex);
8 years ago
portable_mutex_init(&LP_psockmutex);
portable_mutex_init(&LP_coinmutex);
8 years ago
portable_mutex_init(&LP_pubkeymutex);
7 years ago
portable_mutex_init(&LP_electrummutex);
8 years ago
portable_mutex_init(&LP_messagemutex);
8 years ago
portable_mutex_init(&LP_portfoliomutex);
7 years ago
portable_mutex_init(&LP_butxomutex);
7 years ago
portable_mutex_init(&LP_reservedmutex);
7 years ago
portable_mutex_init(&LP_nanorecvsmutex);
portable_mutex_init(&LP_tradebotsmutex);
7 years ago
myipaddr = clonestr("127.0.0.1");
#ifndef _WIN32
7 years ago
#ifndef FROM_JS
if ( system("curl -s4 checkip.amazonaws.com > myipaddr") == 0 )
8 years ago
{
char ipfname[64];
strcpy(ipfname,"myipaddr");
if ( (myipaddr= OS_filestr(&filesize,ipfname)) != 0 && myipaddr[0] != 0 )
8 years ago
{
n = strlen(myipaddr);
if ( myipaddr[n-1] == '\n' )
myipaddr[--n] = 0;
8 years ago
strcpy(LP_myipaddr,myipaddr);
8 years ago
} else printf("error getting myipaddr\n");
} else printf("error issuing curl\n");
#else
7 years ago
IAMLP = 0;
#endif
#endif
8 years ago
if ( IAMLP != 0 )
{
8 years ago
pubsock = -1;
nanomsg_transportname(0,subaddr,myipaddr,mypubport);
nanomsg_transportname(1,bindaddr,myipaddr,mypubport);
7 years ago
//nanomsg_transportname2(1,bindaddr2,myipaddr,mypubport);
7 years ago
valid = 0;
8 years ago
if ( (pubsock= nn_socket(AF_SP,NN_PUB)) >= 0 )
8 years ago
{
7 years ago
valid = 0;
8 years ago
if ( nn_bind(pubsock,bindaddr) >= 0 )
7 years ago
valid++;
7 years ago
//if ( nn_bind(pubsock,bindaddr2) >= 0 )
// valid++;
7 years ago
if ( valid > 0 )
8 years ago
{
7 years ago
timeout = 1;
8 years ago
nn_setsockopt(pubsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout));
}
else
{
printf("error binding to (%s).%d\n",subaddr,pubsock);
if ( pubsock >= 0 )
nn_close(pubsock), pubsock = -1;
}
} else printf("error getting pubsock %d\n",pubsock);
7 years ago
printf(">>>>>>>>> myipaddr.(%s) (%s) pullsock.%d valid.%d\n",bindaddr,subaddr,pubsock,valid);
8 years ago
LP_mypubsock = pubsock;
8 years ago
}
8 years ago
printf("got %s, initpeers\n",myipaddr);
7 years ago
LP_initpeers(pubsock,mypeer,myipaddr,myport,jstr(argjson,"seednode"),mypullport,mypubport);
7 years ago
RPC_port = myport;
8 years ago
printf("get public socket\n");
8 years ago
LP_mypullsock = LP_initpublicaddr(ctx,&mypullport,pushaddr,myipaddr,mypullport,0);
8 years ago
strcpy(LP_publicaddr,pushaddr);
LP_publicport = mypullport;
8 years ago
LP_mybussock = LP_coinbus(mybusport);
//LP_deadman_switch = (uint32_t)time(NULL);
printf("canbind.%d my command address is (%s) pullsock.%d pullport.%u\n",LP_canbind,pushaddr,LP_mypullsock,mypullport);
7 years ago
if ( (coinsjson= jobj(argjson,"coins")) == 0 )
{
7 years ago
if ( (coins_str= OS_filestr(&filesize,"coins.json")) != 0 || (coins_str= OS_filestr(&filesize,"exchanges/coins.json")) != 0 )
7 years ago
{
unstringify(coins_str);
printf("UNSTRINGIFIED.(%s)\n",coins_str);
coinsjson = cJSON_Parse(coins_str);
free(coins_str);
// yes I know this coinsjson is not freed, not sure about if it is referenced
}
}
if ( coinsjson == 0 )
{
7 years ago
printf("no coins object or coins.json file, must abort\n");
7 years ago
exit(-1);
}
LP_initcoins(ctx,pubsock,coinsjson);
7 years ago
G.waiting = 1;
LP_passphrase_init(passphrase,jstr(argjson,"gui"));
7 years ago
#ifndef FROM_JS
7 years ago
if ( IAMLP != 0 && OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_psockloop,(void *)myipaddr) != 0 )
8 years ago
{
8 years ago
printf("error launching LP_psockloop for (%s)\n",myipaddr);
8 years ago
exit(-1);
}
7 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_reserved_msgs,(void *)myipaddr) != 0 )
{
printf("error launching LP_reserved_msgs for (%s)\n",myipaddr);
exit(-1);
}
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);
}
8 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&myport) != 0 )
{
printf("error launching stats rpcloop for port.%u\n",myport);
exit(-1);
7 years ago
}
uint16_t myport2 = myport-1;
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&myport2) != 0 )
{
printf("error launching stats rpcloop for port.%u\n",myport);
exit(-1);
8 years ago
}
7 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)command_rpcloop,(void *)myipaddr) != 0 )
8 years ago
{
7 years ago
printf("error launching command_rpcloop for port.%u\n",myport);
8 years ago
exit(-1);
}
7 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)queue_loop,(void *)myipaddr) != 0 )
8 years ago
{
7 years ago
printf("error launching queue_loop for port.%u\n",myport);
8 years ago
exit(-1);
}
7 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)gc_loop,(void *)myipaddr) != 0 )
{
printf("error launching gc_loop for port.%u\n",myport);
exit(-1);
}
7 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)prices_loop,ctx) != 0 )
8 years ago
{
7 years ago
printf("error launching prices_loop for port.%u\n",myport);
exit(-1);
}
7 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_coinsloop,(void *)"") != 0 )
7 years ago
{
printf("error launching LP_coinsloop for port.%u\n",myport);
8 years ago
exit(-1);
}
7 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_coinsloop,(void *)"BTC") != 0 )
{
printf("error launching BTC LP_coinsloop for port.%u\n",myport);
exit(-1);
}
7 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_coinsloop,(void *)"KMD") != 0 )
{
printf("error launching KMD LP_coinsloop for port.%u\n",myport);
exit(-1);
}
7 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_pubkeysloop,(void *)myipaddr) != 0 )
7 years ago
{
printf("error launching LP_pubkeysloop for ctx.%p\n",ctx);
exit(-1);
}
7 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_privkeysloop,(void *)myipaddr) != 0 )
7 years ago
{
printf("error launching LP_privkeysloop for ctx.%p\n",ctx);
exit(-1);
}
7 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_swapsloop,(void *)myipaddr) != 0 )
{
printf("error launching LP_swapsloop for port.%u\n",myport);
exit(-1);
}
7 years ago
int32_t nonz;
8 years ago
while ( 1 )
{
7 years ago
nonz = 0;
7 years ago
G.waiting = 1;
while ( G.initializing != 0 )
{
7 years ago
//fprintf(stderr,".");
7 years ago
sleep(3);
}
if ( LP_mainloop_iter(ctx,myipaddr,mypeer,pubsock,pushaddr,myport) != 0 )
7 years ago
nonz++;
if ( nonz == 0 )
7 years ago
usleep(1000);
else if ( IAMLP == 0 )
usleep(1000);
8 years ago
}
7 years ago
#endif
8 years ago
}
8 years ago
7 years ago
#ifdef FROM_JS
7 years ago
extern void *Nanomsg_threadarg;
void *nn_thread_main_routine(void *arg);
7 years ago
7 years ago
void emscripten_usleep(int32_t x)
{
}
7 years ago
7 years ago
char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char *command,char *params,int32_t timeout)
{
static uint32_t counter; char fname[512],*retstr; long fsize;
if ( strncmp("http://",url,strlen("http://")) != 0 )
return(clonestr("{\"error\":\"only http allowed\"}"));
7 years ago
sprintf(fname,"bitcoind_RPC/request.%d",counter % 10);
7 years ago
counter++;
//printf("issue.(%s)\n",url);
emscripten_wget(url,fname);
retstr = OS_filestr(&fsize,fname);
//printf("bitcoind_RPC(%s) -> fname.(%s) %s\n",url,fname,retstr);
return(retstr);
}
7 years ago
char *barterDEX(char *argstr)
{
7 years ago
static void *ctx;
7 years ago
cJSON *argjson; char *retstr;
7 years ago
if ( ctx == 0 )
ctx = bitcoin_ctx();
7 years ago
printf("barterDEX.(%s)\n",argstr);
if ( (argjson= cJSON_Parse(argstr)) != 0 )
{
7 years ago
retstr = LP_command_process(ctx,LP_myipaddr,LP_mypubsock,argjson,(uint8_t *)argstr,(int32_t)strlen(argstr));
7 years ago
free_json(argjson);
} else retstr = clonestr("{\"error\":\"couldnt parse request\"}");
return(retstr);
}
7 years ago
void LP_fromjs_iter()
7 years ago
{
static void *ctx; char *retstr;
7 years ago
if ( G.initializing != 0 )
{
printf("LP_fromjs_iter during G.initializing, skip\n");
return;
}
if ( ctx == 0 )
ctx = bitcoin_ctx();
7 years ago
if ( 0 && (LP_counter % 100) == 0 )
7 years ago
printf("LP_fromjs_iter got called LP_counter.%d userpass.(%s) ctx.%p\n",LP_counter,G.USERPASS,ctx);
7 years ago
//if ( Nanomsg_threadarg != 0 )
// nn_thread_main_routine(Nanomsg_threadarg);
//LP_pubkeys_query();
7 years ago
//LP_utxosQ_process();
7 years ago
//LP_nanomsg_recvs(ctx);
LP_mainloop_iter(ctx,LP_myipaddr,0,LP_mypubsock,LP_publicaddr,LP_RPCPORT);
7 years ago
//queue_loop(0);
if ( 0 && (LP_counter % 10) == 0 ) // 10 seconds
{
LP_coinsloop(0);
7 years ago
if ( (LP_counter % 100) == 0 ) // 100 seconds
{
LP_notify_pubkeys(ctx,LP_mypubsock);
LP_privkey_updates(ctx,LP_mypubsock,0);
if ( (retstr= basilisk_swapentry(0,0)) != 0 )
free(retstr);
}
}
7 years ago
LP_counter++;
7 years ago
}
7 years ago
7 years ago
#endif
8 years ago