|
|
@ -68,9 +68,46 @@ void dex_packet(struct supernet_info *myinfo,struct dex_nanomsghdr *dexp,int32_t |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
int32_t dex_reqsend(struct supernet_info *myinfo,char *handler,uint8_t *data,int32_t datalen) |
|
|
|
struct dex_request { bits256 txid; char name[15]; uint8_t func; }; |
|
|
|
|
|
|
|
int32_t dex_rwrequest(int32_t rwflag,uint8_t *serialized,struct dex_request *dexreq) |
|
|
|
{ |
|
|
|
int32_t len = 0; |
|
|
|
len += iguana_rwbignum(rwflag,&serialized[len],sizeof(dexreq->txid),dexreq->txid.bytes); |
|
|
|
if ( rwflag != 0 ) |
|
|
|
{ |
|
|
|
memcpy(&serialized[len],dexreq->name,sizeof(dexreq->name)), len += sizeof(dexreq->name); |
|
|
|
serialized[len++] = dexreq->func; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
memcpy(dexreq->name,&serialized[len],sizeof(dexreq->name)), len += sizeof(dexreq->name); |
|
|
|
dexreq->func = serialized[len++]; |
|
|
|
} |
|
|
|
return(len); |
|
|
|
} |
|
|
|
|
|
|
|
char *dex_response(struct supernet_info *myinfo,struct dex_nanomsghdr *dexp) |
|
|
|
{ |
|
|
|
char *retstr = 0; cJSON *retjson; struct iguana_info *coin; struct dex_request dexreq; |
|
|
|
if ( strcmp(dexp->handler,"request") == 0 ) |
|
|
|
{ |
|
|
|
dex_rwrequest(0,dexp->packet,&dexreq); |
|
|
|
if ( (coin= iguana_coinfind(dexreq.name)) != 0 ) |
|
|
|
{ |
|
|
|
if ( dexreq.func == 'T' ) |
|
|
|
{ |
|
|
|
if ( (retjson= dpow_gettransaction(myinfo,coin,dexreq.txid)) != 0 ) |
|
|
|
retstr = jprint(retjson,1); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return(retstr); |
|
|
|
} |
|
|
|
|
|
|
|
char *dex_reqsend(struct supernet_info *myinfo,char *handler,uint8_t *data,int32_t datalen) |
|
|
|
{ |
|
|
|
struct dex_nanomsghdr *dexp; char ipaddr[64],str[128]; int32_t retval=0,timeout,i,n,size,recvbytes,sentbytes = 0,reqsock,subsock; uint32_t *retptr,ipbits; |
|
|
|
struct dex_nanomsghdr *dexp; char ipaddr[64],str[128]; int32_t timeout,i,n,size,recvbytes,sentbytes = 0,reqsock,subsock; uint32_t *retptr,ipbits; char *retstr = 0; |
|
|
|
portable_mutex_lock(&myinfo->dexmutex); |
|
|
|
subsock = myinfo->subsock; |
|
|
|
reqsock = myinfo->reqsock; |
|
|
@ -130,41 +167,60 @@ int32_t dex_reqsend(struct supernet_info *myinfo,char *handler,uint8_t *data,int |
|
|
|
//printf(" sent.%d:%d datalen.%d\n",sentbytes,size,datalen);
|
|
|
|
if ( (recvbytes= nn_recv(myinfo->reqsock,&retptr,NN_MSG,0)) >= 0 ) |
|
|
|
{ |
|
|
|
ipbits = *retptr; |
|
|
|
expand_ipbits(ipaddr,ipbits); |
|
|
|
//printf("req returned.[%d] %08x %s\n",recvbytes,*retptr,ipaddr);
|
|
|
|
portable_mutex_lock(&myinfo->dexmutex); |
|
|
|
n = myinfo->numdexipbits; |
|
|
|
for (i=0; i<n; i++) |
|
|
|
if ( ipbits == myinfo->dexipbits[i] ) |
|
|
|
break; |
|
|
|
if ( i == n && n < 64 ) |
|
|
|
if ( strcmp(handler,"DEX") == 0 ) |
|
|
|
{ |
|
|
|
myinfo->dexipbits[n++] = ipbits; |
|
|
|
qsort(myinfo->dexipbits,n,sizeof(uint32_t),_increasing_ipbits); |
|
|
|
if ( (myinfo->numdexipbits= n) < 3 ) |
|
|
|
ipbits = *retptr; |
|
|
|
expand_ipbits(ipaddr,ipbits); |
|
|
|
//printf("req returned.[%d] %08x %s\n",recvbytes,*retptr,ipaddr);
|
|
|
|
n = myinfo->numdexipbits; |
|
|
|
for (i=0; i<n; i++) |
|
|
|
if ( ipbits == myinfo->dexipbits[i] ) |
|
|
|
break; |
|
|
|
if ( i == n && n < 64 ) |
|
|
|
{ |
|
|
|
if ( myinfo->IAMNOTARY == 0 && myinfo->subsock >= 0 ) |
|
|
|
myinfo->dexipbits[n++] = ipbits; |
|
|
|
qsort(myinfo->dexipbits,n,sizeof(uint32_t),_increasing_ipbits); |
|
|
|
if ( (myinfo->numdexipbits= n) < 3 ) |
|
|
|
{ |
|
|
|
nn_connect(myinfo->subsock,nanomsg_tcpname(0,str,ipaddr,PUB_SOCK)); |
|
|
|
printf("%d: subscribe connect (%s)\n",myinfo->numdexipbits,str); |
|
|
|
if ( myinfo->IAMNOTARY == 0 && myinfo->subsock >= 0 ) |
|
|
|
{ |
|
|
|
nn_connect(myinfo->subsock,nanomsg_tcpname(0,str,ipaddr,PUB_SOCK)); |
|
|
|
printf("%d: subscribe connect (%s)\n",myinfo->numdexipbits,str); |
|
|
|
} |
|
|
|
} |
|
|
|
nn_connect(myinfo->reqsock,nanomsg_tcpname(0,str,ipaddr,REP_SOCK)); |
|
|
|
printf("%d: req connect (%s)\n",myinfo->numdexipbits,str); |
|
|
|
} |
|
|
|
nn_connect(myinfo->reqsock,nanomsg_tcpname(0,str,ipaddr,REP_SOCK)); |
|
|
|
printf("%d: req connect (%s)\n",myinfo->numdexipbits,str); |
|
|
|
nn_freemsg(retptr); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
retstr = (char *)retptr; |
|
|
|
printf("REQ got (%s)\n",retstr); |
|
|
|
} |
|
|
|
portable_mutex_unlock(&myinfo->dexmutex); |
|
|
|
nn_freemsg(retptr); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
retval = -2; |
|
|
|
//retval = -2;
|
|
|
|
//printf("no rep return? recvbytes.%d\n",recvbytes);
|
|
|
|
} |
|
|
|
//printf("DEXREQ.[%d] crc32.%08x datalen.%d sent.%d recv.%d timestamp.%u\n",size,dexp->crc32,datalen,sentbytes,recvbytes,dexp->timestamp);
|
|
|
|
free(dexp); |
|
|
|
} else retval = -1; |
|
|
|
return(retval); |
|
|
|
} //else retval = -1;
|
|
|
|
return(retstr); |
|
|
|
} |
|
|
|
|
|
|
|
char *dex_getrawtransaction(struct supernet_info *myinfo,char *symbol,bits256 txid) |
|
|
|
{ |
|
|
|
struct dex_request dexreq; uint8_t packet[sizeof(dexreq)]; int32_t datalen; |
|
|
|
memset(&dexreq,0,sizeof(dexreq)); |
|
|
|
safecopy(dexreq.name,symbol,sizeof(dexreq.name)); |
|
|
|
dexreq.txid = txid; |
|
|
|
dexreq.func = 'T'; |
|
|
|
datalen = dex_rwrequest(1,packet,&dexreq); |
|
|
|
return(dex_reqsend(myinfo,"request",packet,datalen)); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t dex_crc32find(struct supernet_info *myinfo,uint32_t crc32) |
|
|
@ -937,7 +993,7 @@ void dpow_ipbitsadd(struct supernet_info *myinfo,struct dpow_info *dp,uint32_t * |
|
|
|
|
|
|
|
int32_t dpow_nanomsg_update(struct supernet_info *myinfo) |
|
|
|
{ |
|
|
|
int32_t i,n=0,num=0,size,firstz = -1; uint32_t crc32,r,m; struct dpow_nanomsghdr *np=0; struct dpow_info *dp; struct dpow_block *bp; struct dex_nanomsghdr *dexp = 0; |
|
|
|
int32_t i,n=0,num=0,size,firstz = -1; char *retstr; uint32_t crc32,r,m; struct dpow_nanomsghdr *np=0; struct dpow_info *dp; struct dpow_block *bp; struct dex_nanomsghdr *dexp = 0; |
|
|
|
if ( time(NULL) < myinfo->nanoinit+5 || (myinfo->dpowsock < 0 && myinfo->dexsock < 0 && myinfo->repsock < 0) ) |
|
|
|
return(-1); |
|
|
|
portable_mutex_lock(&myinfo->dpowmutex); |
|
|
@ -1022,18 +1078,26 @@ int32_t dpow_nanomsg_update(struct supernet_info *myinfo) |
|
|
|
if ( (size= nn_recv(myinfo->repsock,&dexp,NN_MSG,0)) >= 0 ) |
|
|
|
{ |
|
|
|
num++; |
|
|
|
if ( (m= myinfo->numdpowipbits) > 0 ) |
|
|
|
if ( (retstr= dex_response(myinfo,dexp)) != 0 ) |
|
|
|
{ |
|
|
|
r = myinfo->dpowipbits[rand() % m]; |
|
|
|
nn_send(myinfo->repsock,&r,sizeof(r),0); |
|
|
|
//printf("REP.%08x <- rand ip m.%d %x\n",dexp->crc32,m,r);
|
|
|
|
} else printf("illegal state without dpowipbits?\n"); |
|
|
|
if ( dex_packetcheck(myinfo,dexp,size) == 0 ) |
|
|
|
nn_send(myinfo->repsock,retstr,(int32_t)strlen(retstr)+1,0); |
|
|
|
free(retstr); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
nn_send(myinfo->dexsock,dexp,size,0); |
|
|
|
nn_send(myinfo->pubsock,dexp,size,0); |
|
|
|
//printf("REP.%08x -> dexbus and pub, t.%d lag.%d\n",dexp->crc32,dexp->timestamp,(int32_t)(time(NULL)-dexp->timestamp));
|
|
|
|
dex_packet(myinfo,dexp,size); |
|
|
|
if ( (m= myinfo->numdpowipbits) > 0 ) |
|
|
|
{ |
|
|
|
r = myinfo->dpowipbits[rand() % m]; |
|
|
|
nn_send(myinfo->repsock,&r,sizeof(r),0); |
|
|
|
//printf("REP.%08x <- rand ip m.%d %x\n",dexp->crc32,m,r);
|
|
|
|
} else printf("illegal state without dpowipbits?\n"); |
|
|
|
if ( dex_packetcheck(myinfo,dexp,size) == 0 ) |
|
|
|
{ |
|
|
|
nn_send(myinfo->dexsock,dexp,size,0); |
|
|
|
nn_send(myinfo->pubsock,dexp,size,0); |
|
|
|
//printf("REP.%08x -> dexbus and pub, t.%d lag.%d\n",dexp->crc32,dexp->timestamp,(int32_t)(time(NULL)-dexp->timestamp));
|
|
|
|
dex_packet(myinfo,dexp,size); |
|
|
|
} |
|
|
|
} |
|
|
|
//printf("GOT DEX rep PACKET.%d\n",size);
|
|
|
|
if ( dexp != 0 ) |
|
|
|