|
|
@ -76,14 +76,14 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_ |
|
|
|
#include "LP_forwarding.c" |
|
|
|
#include "LP_commands.c" |
|
|
|
|
|
|
|
char *LP_command_process(char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) |
|
|
|
char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin) |
|
|
|
{ |
|
|
|
char *retstr=0; |
|
|
|
if ( jobj(argjson,"result") != 0 || jobj(argjson,"error") != 0 ) |
|
|
|
return(0); |
|
|
|
if ( LP_tradecommand(myipaddr,pubsock,argjson,data,datalen,profitmargin) <= 0 ) |
|
|
|
if ( LP_tradecommand(ctx,myipaddr,pubsock,argjson,data,datalen,profitmargin) <= 0 ) |
|
|
|
{ |
|
|
|
if ( (retstr= stats_JSON(myipaddr,pubsock,profitmargin,argjson,"127.0.0.1",0)) != 0 ) |
|
|
|
if ( (retstr= stats_JSON(ctx,myipaddr,pubsock,profitmargin,argjson,"127.0.0.1",0)) != 0 ) |
|
|
|
{ |
|
|
|
//printf("%s PULL.[%d]-> (%s)\n",myipaddr != 0 ? myipaddr : "127.0.0.1",datalen,retstr);
|
|
|
|
if ( pubsock >= 0 ) //strncmp("{\"error\":",retstr,strlen("{\"error\":")) != 0 &&
|
|
|
@ -93,7 +93,7 @@ char *LP_command_process(char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t * |
|
|
|
return(retstr); |
|
|
|
} |
|
|
|
|
|
|
|
char *LP_process_message(char *typestr,char *myipaddr,int32_t pubsock,double profitmargin,void *ptr,int32_t recvlen,int32_t recvsock) |
|
|
|
char *LP_process_message(void *ctx,char *typestr,char *myipaddr,int32_t pubsock,double profitmargin,void *ptr,int32_t recvlen,int32_t recvsock) |
|
|
|
{ |
|
|
|
int32_t len,datalen=0; char *retstr=0,*jsonstr=0; cJSON *argjson,*reqjson; |
|
|
|
if ( (datalen= is_hexstr((char *)ptr,0)) > 0 ) |
|
|
@ -112,7 +112,7 @@ char *LP_process_message(char *typestr,char *myipaddr,int32_t pubsock,double pro |
|
|
|
if ( jstr(argjson,"method") != 0 && strcmp(jstr(argjson,"method"),"forwardhex") == 0 ) |
|
|
|
{ |
|
|
|
//printf("got forwardhex\n");
|
|
|
|
if ( (retstr= LP_forwardhex(pubsock,jbits256(argjson,"pubkey"),jstr(argjson,"hex"))) != 0 ) |
|
|
|
if ( (retstr= LP_forwardhex(ctx,pubsock,jbits256(argjson,"pubkey"),jstr(argjson,"hex"))) != 0 ) |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
@ -125,7 +125,7 @@ char *LP_process_message(char *typestr,char *myipaddr,int32_t pubsock,double pro |
|
|
|
if ( pubsock >= 0 && (reqjson= LP_dereference(argjson,"publish")) != 0 ) |
|
|
|
LP_send(pubsock,jprint(reqjson,1),1); |
|
|
|
} |
|
|
|
else if ( (retstr= LP_command_process(myipaddr,pubsock,argjson,&((uint8_t *)ptr)[len],recvlen - len,profitmargin)) != 0 ) |
|
|
|
else if ( (retstr= LP_command_process(ctx,myipaddr,pubsock,argjson,&((uint8_t *)ptr)[len],recvlen - len,profitmargin)) != 0 ) |
|
|
|
{ |
|
|
|
} |
|
|
|
portable_mutex_unlock(&LP_commandmutex); |
|
|
@ -154,7 +154,7 @@ char *LP_process_message(char *typestr,char *myipaddr,int32_t pubsock,double pro |
|
|
|
return(retstr); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t LP_pullsock_check(char **retstrp,char *myipaddr,int32_t pubsock,int32_t pullsock,double profitmargin) |
|
|
|
int32_t LP_pullsock_check(void *ctx,char **retstrp,char *myipaddr,int32_t pubsock,int32_t pullsock,double profitmargin) |
|
|
|
{ |
|
|
|
void *ptr; int32_t recvlen=-1,nonz = 0; |
|
|
|
*retstrp = 0; |
|
|
@ -163,13 +163,13 @@ int32_t LP_pullsock_check(char **retstrp,char *myipaddr,int32_t pubsock,int32_t |
|
|
|
while ( (recvlen= nn_recv(pullsock,&ptr,NN_MSG,0)) > 0 ) |
|
|
|
{ |
|
|
|
nonz++; |
|
|
|
*retstrp = LP_process_message("PULL",myipaddr,pubsock,profitmargin,ptr,recvlen,pullsock); |
|
|
|
*retstrp = LP_process_message(ctx,"PULL",myipaddr,pubsock,profitmargin,ptr,recvlen,pullsock); |
|
|
|
} |
|
|
|
} |
|
|
|
return(nonz); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t LP_subsock_check(char *myipaddr,int32_t pubsock,int32_t sock,double profitmargin) |
|
|
|
int32_t LP_subsock_check(void *ctx,char *myipaddr,int32_t pubsock,int32_t sock,double profitmargin) |
|
|
|
{ |
|
|
|
int32_t recvlen,nonz = 0; void *ptr; char *retstr; |
|
|
|
if ( sock >= 0 ) |
|
|
@ -177,7 +177,7 @@ int32_t LP_subsock_check(char *myipaddr,int32_t pubsock,int32_t sock,double prof |
|
|
|
while ( (recvlen= nn_recv(sock,&ptr,NN_MSG,0)) > 0 ) |
|
|
|
{ |
|
|
|
nonz++; |
|
|
|
if ( (retstr= LP_process_message("SUB",myipaddr,pubsock,profitmargin,ptr,recvlen,sock)) != 0 ) |
|
|
|
if ( (retstr= LP_process_message(ctx,"SUB",myipaddr,pubsock,profitmargin,ptr,recvlen,sock)) != 0 ) |
|
|
|
free(retstr); |
|
|
|
} |
|
|
|
} |
|
|
@ -205,10 +205,10 @@ void LP_utxo_spentcheck(int32_t pubsock,struct LP_utxoinfo *utxo,double profitma |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void LP_myutxo_updates(int32_t pubsock,char *passphrase,double profitmargin) |
|
|
|
void LP_myutxo_updates(void *ctx,int32_t pubsock,char *passphrase,double profitmargin) |
|
|
|
{ |
|
|
|
//LP_utxopurge(0); not good to disrupt existing pointers
|
|
|
|
LP_privkey_updates(pubsock,passphrase,0); |
|
|
|
LP_privkey_updates(ctx,pubsock,passphrase,0); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t LP_peer_utxosquery(struct LP_peerinfo *mypeer,uint16_t myport,int32_t pubsock,struct LP_peerinfo *peer,uint32_t now,double profitmargin,int32_t interval) |
|
|
@ -229,7 +229,7 @@ int32_t LP_peer_utxosquery(struct LP_peerinfo *mypeer,uint16_t myport,int32_t pu |
|
|
|
return(n); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t LP_mainloop_iter(char *myipaddr,struct LP_peerinfo *mypeer,int32_t pubsock,char *pushaddr,uint16_t pushport,int32_t pullsock,uint16_t myport,char *passphrase,double profitmargin) |
|
|
|
int32_t LP_mainloop_iter(void *ctx,char *myipaddr,struct LP_peerinfo *mypeer,int32_t pubsock,char *pushaddr,uint16_t pushport,int32_t pullsock,uint16_t myport,char *passphrase,double profitmargin) |
|
|
|
{ |
|
|
|
static uint32_t counter,lastforward,numpeers; |
|
|
|
struct LP_utxoinfo *utxo,*utmp; char *retstr,*origipaddr; struct LP_peerinfo *peer,*tmp; uint32_t now; int32_t nonz = 0,n=0,lastn=-1; |
|
|
@ -265,12 +265,12 @@ int32_t LP_mainloop_iter(char *myipaddr,struct LP_peerinfo *mypeer,int32_t pubso |
|
|
|
LP_peer_pricesquery(peer->ipaddr,peer->port); |
|
|
|
peer->diduquery = now; |
|
|
|
} |
|
|
|
nonz += LP_subsock_check(origipaddr,pubsock,peer->subsock,profitmargin); |
|
|
|
nonz += LP_subsock_check(ctx,origipaddr,pubsock,peer->subsock,profitmargin); |
|
|
|
} |
|
|
|
//if ( LP_canbind == 0 ) printf("counter.%d canbind.%d forwarding\n",counter,LP_canbind);
|
|
|
|
if ( (counter % 600) == 60 ) |
|
|
|
{ |
|
|
|
LP_myutxo_updates(pubsock,passphrase,profitmargin); |
|
|
|
LP_myutxo_updates(ctx,pubsock,passphrase,profitmargin); |
|
|
|
if ( lastforward < now-3600 ) |
|
|
|
{ |
|
|
|
LP_forwarding_register(LP_mypubkey,pushaddr,pushport,10); |
|
|
@ -301,7 +301,7 @@ int32_t LP_mainloop_iter(char *myipaddr,struct LP_peerinfo *mypeer,int32_t pubso |
|
|
|
} |
|
|
|
} |
|
|
|
//if ( LP_canbind == 0 ) printf("counter.%d canbind.%d pullsock check\n",counter,LP_canbind);
|
|
|
|
nonz += LP_pullsock_check(&retstr,myipaddr,pubsock,pullsock,profitmargin); |
|
|
|
nonz += LP_pullsock_check(ctx,&retstr,myipaddr,pubsock,pullsock,profitmargin); |
|
|
|
if ( retstr != 0 ) |
|
|
|
free(retstr); |
|
|
|
//if ( LP_canbind == 0 ) printf("counter.%d canbind.%d hellos\n",counter,LP_canbind);
|
|
|
@ -320,7 +320,7 @@ int32_t LP_mainloop_iter(char *myipaddr,struct LP_peerinfo *mypeer,int32_t pubso |
|
|
|
return(nonz); |
|
|
|
} |
|
|
|
|
|
|
|
void LP_initcoins(int32_t pubsock,cJSON *coins,char *passphrase) |
|
|
|
void LP_initcoins(void *ctx,int32_t pubsock,cJSON *coins,char *passphrase) |
|
|
|
{ |
|
|
|
int32_t i,n; cJSON *item; |
|
|
|
for (i=0; i<sizeof(activecoins)/sizeof(*activecoins); i++) |
|
|
@ -340,7 +340,7 @@ void LP_initcoins(int32_t pubsock,cJSON *coins,char *passphrase) |
|
|
|
} |
|
|
|
} |
|
|
|
fprintf(stderr,"privkey updates\n"); |
|
|
|
LP_privkey_updates(pubsock,passphrase,1); |
|
|
|
LP_privkey_updates(ctx,pubsock,passphrase,1); |
|
|
|
} |
|
|
|
|
|
|
|
void LP_initpeers(int32_t pubsock,struct LP_peerinfo *mypeer,char *myipaddr,uint16_t myport,char *seednode,double profitmargin) |
|
|
@ -385,7 +385,7 @@ void LP_initpeers(int32_t pubsock,struct LP_peerinfo *mypeer,char *myipaddr,uint |
|
|
|
|
|
|
|
void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profitmargin,char *passphrase,int32_t amclient,char *userhome,cJSON *argjson) |
|
|
|
{ |
|
|
|
char *myipaddr=0; long filesize,n; int32_t timeout,pullsock=-1,pubsock=-1; struct LP_peerinfo *mypeer=0; char pushaddr[128],subaddr[128],bindaddr[128]; |
|
|
|
char *myipaddr=0; long filesize,n; int32_t timeout,pullsock=-1,pubsock=-1; struct LP_peerinfo *mypeer=0; char pushaddr[128],subaddr[128],bindaddr[128]; void *ctx = bitcoin_ctx(); |
|
|
|
IAMLP = !amclient; |
|
|
|
#ifndef __linux__ |
|
|
|
if ( IAMLP != 0 ) |
|
|
@ -454,10 +454,10 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profit |
|
|
|
LP_mypubsock = pubsock; |
|
|
|
} |
|
|
|
LP_initpeers(pubsock,mypeer,myipaddr,myport,jstr(argjson,"seednode"),profitmargin); |
|
|
|
pullsock = LP_initpublicaddr(&mypullport,pushaddr,myipaddr,mypullport,0); |
|
|
|
pullsock = LP_initpublicaddr(ctx,&mypullport,pushaddr,myipaddr,mypullport,0); |
|
|
|
LP_deadman_switch = (uint32_t)time(NULL); |
|
|
|
printf("my command address is (%s) pullsock.%d pullport.%u\n",pushaddr,pullsock,mypullport); |
|
|
|
LP_initcoins(pubsock,jobj(argjson,"coins"),passphrase); |
|
|
|
LP_initcoins(ctx,pubsock,jobj(argjson,"coins"),passphrase); |
|
|
|
if ( IAMLP != 0 && OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_psockloop,(void *)&myipaddr) != 0 ) |
|
|
|
{ |
|
|
|
printf("error launching LP_psockloop for (%s)\n",myipaddr); |
|
|
@ -470,7 +470,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profit |
|
|
|
} |
|
|
|
while ( 1 ) |
|
|
|
{ |
|
|
|
if ( LP_mainloop_iter(myipaddr,mypeer,pubsock,pushaddr,mypullport,pullsock,myport,passphrase,profitmargin) == 0 ) |
|
|
|
if ( LP_mainloop_iter(ctx,myipaddr,mypeer,pubsock,pushaddr,mypullport,pullsock,myport,passphrase,profitmargin) == 0 ) |
|
|
|
usleep(100000); |
|
|
|
if ( LP_canbind == 0 ) |
|
|
|
{ |
|
|
@ -480,7 +480,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profit |
|
|
|
printf("DEAD man's switch %u activated at %u lag.%d, register forwarding again\n",LP_deadman_switch,(uint32_t)time(NULL),(uint32_t)(time(NULL) - LP_deadman_switch)); |
|
|
|
if ( pullsock >= 0 ) |
|
|
|
nn_close(pullsock); |
|
|
|
pullsock = LP_initpublicaddr(&mypullport,pushaddr,myipaddr,mypullport,0); |
|
|
|
pullsock = LP_initpublicaddr(ctx,&mypullport,pushaddr,myipaddr,mypullport,0); |
|
|
|
LP_deadman_switch = (uint32_t)time(NULL); |
|
|
|
LP_forwarding_register(LP_mypubkey,pushaddr,mypullport,100000); |
|
|
|
} |
|
|
|