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.

1248 lines
54 KiB

8 years ago
/******************************************************************************
* Copyright © 2014-2016 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
// 1. add rpc hooks, debug
// 2. sig validate in fsm
8 years ago
struct dex_nanomsghdr
{
8 years ago
uint32_t crc32,size,datalen,timestamp;
8 years ago
uint8_t version0,version1,packet[];
} PACKED;
8 years ago
void dex_init(struct supernet_info *myinfo)
{
strcpy(myinfo->dexseed_ipaddr,"78.47.196.146");
myinfo->dexipbits[0] = (uint32_t)calc_ipbits(myinfo->dexseed_ipaddr);
myinfo->numdexipbits = 1;
portable_mutex_init(&myinfo->dexmutex);
}
8 years ago
char *nanomsg_tcpname(struct supernet_info *myinfo,char *str,char *ipaddr,uint16_t port)
8 years ago
{
8 years ago
if ( myinfo != 0 ) // bind path)
{
if ( myinfo->bindaddr[0] != 0 && strcmp(ipaddr,myinfo->ipaddr) == 0 )
ipaddr = myinfo->bindaddr;
}
8 years ago
sprintf(str,"tcp://%s:%u",ipaddr,port);
return(str);
}
8 years ago
static int _increasing_ipbits(const void *a,const void *b)
{
#define uint32_a (*(uint32_t *)a)
#define uint32_b (*(uint32_t *)b)
if ( uint32_b > uint32_a )
return(-1);
else if ( uint32_b < uint32_a )
return(1);
return(0);
#undef uint32_a
#undef uint32_b
}
8 years ago
void dex_packet(struct supernet_info *myinfo,struct dex_nanomsghdr *dexp,int32_t size)
{
8 years ago
printf("DEX_PACKET.[%d] crc.%x lag.%d\n",size,calc_crc32(0,(void *)((long)dexp+sizeof(dexp->crc32)),(int32_t)(size-sizeof(dexp->crc32))),(int32_t)(time(NULL)-dexp->timestamp));
8 years ago
}
8 years ago
int32_t dex_reqsend(struct supernet_info *myinfo,uint8_t *data,int32_t datalen)
8 years ago
{
8 years ago
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;
portable_mutex_lock(&myinfo->dexmutex);
subsock = myinfo->subsock;
reqsock = myinfo->reqsock;
if ( reqsock < 0 && (reqsock= nn_socket(AF_SP,NN_REQ)) >= 0 )
8 years ago
{
8 years ago
if ( nn_connect(reqsock,nanomsg_tcpname(0,str,myinfo->dexseed_ipaddr,REP_SOCK)) < 0 )
8 years ago
{
8 years ago
nn_close(reqsock);
reqsock = -1;
8 years ago
}
else
{
8 years ago
timeout = 500;
8 years ago
nn_setsockopt(reqsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout));
8 years ago
timeout = 2000;
8 years ago
nn_setsockopt(reqsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout));
8 years ago
//nn_setsockopt(reqsock,NN_TCP,NN_RECONNECT_IVL,&timeout,sizeof(timeout));
8 years ago
if ( myinfo->IAMNOTARY == 0 && subsock < 0 && (subsock= nn_socket(AF_SP,NN_SUB)) >= 0 )
8 years ago
{
8 years ago
if ( nn_connect(subsock,nanomsg_tcpname(0,str,myinfo->dexseed_ipaddr,PUB_SOCK)) < 0 )
8 years ago
{
8 years ago
nn_close(reqsock);
reqsock = -1;
nn_close(subsock);
subsock = -1;
8 years ago
}
else
{
8 years ago
timeout = 100;
8 years ago
nn_setsockopt(subsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout));
nn_setsockopt(subsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0);
printf("CLIENT sockets req.%d sub.%d\n",reqsock,subsock);
8 years ago
}
}
}
}
8 years ago
if ( myinfo->subsock != subsock )
myinfo->subsock = subsock;
if ( myinfo->reqsock != reqsock )
myinfo->reqsock = reqsock;
portable_mutex_unlock(&myinfo->dexmutex);
8 years ago
if ( myinfo->reqsock >= 0 )
8 years ago
{
8 years ago
size = (int32_t)(sizeof(*dexp) + datalen);
dexp = calloc(1,size); // endian dependent!
dexp->size = size;
dexp->datalen = datalen;
8 years ago
dexp->timestamp = (uint32_t)time(NULL);
8 years ago
dexp->version0 = DEX_VERSION & 0xff;
dexp->version1 = (DEX_VERSION >> 8) & 0xff;
memcpy(dexp->packet,data,datalen);
8 years ago
dexp->crc32 = calc_crc32(0,(void *)((long)dexp+sizeof(dexp->crc32)),(int32_t)(size-sizeof(dexp->crc32)));
8 years ago
sentbytes = nn_send(myinfo->reqsock,dexp,size,0);
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);
8 years ago
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 )
8 years ago
{
8 years ago
myinfo->dexipbits[n++] = ipbits;
qsort(myinfo->dexipbits,n,sizeof(uint32_t),_increasing_ipbits);
if ( (myinfo->numdexipbits= n) < 3 )
8 years ago
{
8 years ago
if ( myinfo->IAMNOTARY == 0 && myinfo->subsock >= 0 )
8 years ago
{
8 years ago
nn_connect(myinfo->subsock,nanomsg_tcpname(0,str,ipaddr,PUB_SOCK));
printf("%d: subscribe connect (%s)\n",myinfo->numdexipbits,str);
8 years ago
}
8 years ago
}
8 years ago
nn_connect(myinfo->reqsock,nanomsg_tcpname(0,str,ipaddr,REP_SOCK));
printf("%d: req connect (%s)\n",myinfo->numdexipbits,str);
8 years ago
}
8 years ago
portable_mutex_unlock(&myinfo->dexmutex);
8 years ago
nn_freemsg(retptr);
8 years ago
}
else
{
retval = -2;
8 years ago
//printf("no rep return? recvbytes.%d\n",recvbytes);
8 years ago
}
printf("DEXREQ.[%d] crc32.%08x datalen.%d sent.%d recv.%d timestamp.%u\n",size,dexp->crc32,datalen,sentbytes,recvbytes,dexp->timestamp);
8 years ago
free(dexp);
8 years ago
} else retval = -1;
return(retval);
8 years ago
}
int32_t dex_crc32find(struct supernet_info *myinfo,uint32_t crc32)
{
int32_t i,firstz = -1;
for (i=0; i<sizeof(myinfo->dexcrcs)/sizeof(*myinfo->dexcrcs); i++)
{
if ( myinfo->dexcrcs[i] == crc32 )
{
//printf("NANODUPLICATE.%08x\n",crc32);
return(-1);
}
else if ( firstz < 0 && myinfo->dexcrcs[i] == 0 )
firstz = i;
}
if ( firstz < 0 )
firstz = (rand() % (sizeof(myinfo->dexcrcs)/sizeof(*myinfo->dexcrcs)));
myinfo->dexcrcs[firstz] = crc32;
return(firstz);
}
8 years ago
int32_t dex_packetcheck(struct supernet_info *myinfo,struct dex_nanomsghdr *dexp,int32_t size)
8 years ago
{
int32_t firstz; uint32_t crc32;
if ( dexp->version0 == (DEX_VERSION & 0xff) && dexp->version1 == ((DEX_VERSION >> 8) & 0xff) )
{
if ( dexp->datalen == (size - sizeof(*dexp)) )
{
8 years ago
crc32 = calc_crc32(0,(void *)((long)dexp + sizeof(dexp->crc32)),(int32_t)(size - sizeof(dexp->crc32)));
8 years ago
if ( dexp->crc32 == crc32 && (firstz= dex_crc32find(myinfo,crc32)) >= 0 )
return(0);
}
}
return(-1);
}
8 years ago
8 years ago
int32_t dex_subsock_poll(struct supernet_info *myinfo)
8 years ago
{
8 years ago
int32_t size= -1; struct dex_nanomsghdr *dexp;
if ( myinfo->subsock >= 0 && (size= nn_recv(myinfo->subsock,&dexp,NN_MSG,0)) >= 0 )
8 years ago
{
if ( dex_packetcheck(myinfo,dexp,size) == 0 )
{
printf("SUBSOCK.%08x",dexp->crc32);
dex_packet(myinfo,dexp,size);
}
if ( dexp != 0 )
nn_freemsg(dexp), dexp = 0;
}
8 years ago
return(size);
8 years ago
}
8 years ago
8 years ago
void dex_updateclient(struct supernet_info *myinfo)
8 years ago
{
int32_t i;
8 years ago
if ( myinfo->IAMNOTARY == 0 )
{
for (i=0; i<100; i++)
if ( dex_subsock_poll(myinfo) <= 0 )
break;
}
8 years ago
}
8 years ago
8 years ago
#if ISNOTARYNODE
8 years ago
struct dpow_nanoutxo
8 years ago
{
8 years ago
bits256 srcutxo,destutxo;
8 years ago
uint64_t bestmask,recvmask;
8 years ago
uint32_t pendingcrcs[2],paxwdcrc;
8 years ago
uint16_t srcvout,destvout;
8 years ago
uint8_t sigs[2][DPOW_MAXSIGLEN],siglens[2],pad,bestk;
8 years ago
} PACKED;
struct dpow_nanomsghdr
{
bits256 srchash,desthash;
8 years ago
struct dpow_nanoutxo ratify,notarize;
8 years ago
uint32_t channel,height,size,datalen,crc32,myipbits,numipbits,ipbits[64];
8 years ago
char symbol[16];
8 years ago
uint8_t senderind,version0,version1,packet[];
8 years ago
} PACKED;
8 years ago
8 years ago
8 years ago
uint64_t dpow_ratifybest(uint64_t refmask,struct dpow_block *bp,int8_t *lastkp);
struct dpow_block *dpow_heightfind(struct supernet_info *myinfo,struct dpow_info *dp,int32_t height);
8 years ago
int32_t dpow_signedtxgen(struct supernet_info *myinfo,struct dpow_info *dp,struct iguana_info *coin,struct dpow_block *bp,int8_t bestk,uint64_t bestmask,int32_t myind,uint32_t deprec,int32_t src_or_dest,int32_t useratified);
void dpow_sigscheck(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,int32_t myind,int32_t src_or_dest,int8_t bestk,uint64_t bestmask,uint8_t pubkeys[64][33],int32_t numratified);
8 years ago
8 years ago
int32_t dpow_addnotary(struct supernet_info *myinfo,struct dpow_info *dp,char *ipaddr)
8 years ago
{
8 years ago
char str[512]; uint32_t ipbits,*ptr; int32_t i,iter,n,retval = -1;
8 years ago
if ( myinfo->IAMNOTARY == 0 )
return(-1);
8 years ago
portable_mutex_lock(&myinfo->notarymutex);
8 years ago
if ( myinfo->dpowsock >= 0 && myinfo->dexsock >= 0 )
8 years ago
{
ipbits = (uint32_t)calc_ipbits(ipaddr);
8 years ago
for (iter=0; iter<2; iter++)
8 years ago
{
8 years ago
if ( iter == 0 )
{
n = myinfo->numdpowipbits;
ptr = myinfo->dpowipbits;
}
else
{
n = dp->numipbits;
ptr = dp->ipbits;
}
for (i=0; i<n; i++)
if ( ipbits == ptr[i] )
break;
if ( i == n && n < 64 )
{
ptr[n] = ipbits;
8 years ago
if ( iter == 0 && strcmp(ipaddr,myinfo->ipaddr) != 0 )
8 years ago
{
8 years ago
retval = nn_connect(myinfo->dpowsock,nanomsg_tcpname(0,str,ipaddr,DPOW_SOCK));
8 years ago
printf("NN_CONNECT to (%s)\n",str);
8 years ago
retval = nn_connect(myinfo->dexsock,nanomsg_tcpname(0,str,ipaddr,DEX_SOCK));
8 years ago
}
8 years ago
n++;
qsort(ptr,n,sizeof(uint32_t),_increasing_ipbits);
if ( iter == 0 )
myinfo->numdpowipbits = n;
else dp->numipbits = n;
8 years ago
//for (i=0; i<n; i++)
// printf("%08x ",ptr[i]);
8 years ago
//printf("addnotary.[%d] (%s) retval.%d (total %d %d) iter.%d\n",n,ipaddr,retval,myinfo->numdpowipbits,dp!=0?dp->numipbits:-1,iter);
8 years ago
}
8 years ago
if ( dp == 0 )
break;
8 years ago
}
}
8 years ago
portable_mutex_unlock(&myinfo->notarymutex);
8 years ago
return(retval);
}
8 years ago
void dpow_nanomsginit(struct supernet_info *myinfo,char *ipaddr)
{
8 years ago
char str[512]; int32_t timeout,retval,maxsize,dpowsock,dexsock,repsock,pubsock;
8 years ago
if ( myinfo->ipaddr[0] == 0 )
{
printf("need to set ipaddr before nanomsg\n");
return;
}
8 years ago
if ( myinfo->IAMNOTARY == 0 )
return;
8 years ago
portable_mutex_lock(&myinfo->notarymutex);
8 years ago
dpowsock = myinfo->dpowsock;
dexsock = myinfo->dexsock;
repsock = myinfo->repsock;
pubsock = myinfo->pubsock;
if ( dpowsock < 0 && (dpowsock= nn_socket(AF_SP,NN_BUS)) >= 0 )
8 years ago
{
8 years ago
if ( nn_bind(dpowsock,nanomsg_tcpname(myinfo,str,myinfo->ipaddr,DPOW_SOCK)) < 0 )
8 years ago
{
8 years ago
printf("error binding to dpowsock (%s)\n",nanomsg_tcpname(myinfo,str,myinfo->ipaddr,DPOW_SOCK));
8 years ago
nn_close(dpowsock);
dpowsock = -1;
8 years ago
}
8 years ago
else
{
8 years ago
printf("NN_BIND to %s\n",str);
8 years ago
if ( dexsock < 0 && (dexsock= nn_socket(AF_SP,NN_BUS)) >= 0 )
8 years ago
{
8 years ago
if ( nn_bind(dexsock,nanomsg_tcpname(myinfo,str,myinfo->ipaddr,DEX_SOCK)) < 0 )
8 years ago
{
8 years ago
printf("error binding to dexsock (%s)\n",nanomsg_tcpname(myinfo,str,myinfo->ipaddr,DEX_SOCK));
8 years ago
nn_close(dexsock);
dexsock = -1;
nn_close(dpowsock);
dpowsock = -1;
8 years ago
}
else
{
8 years ago
if ( pubsock < 0 && (pubsock= nn_socket(AF_SP,NN_PUB)) >= 0 )
8 years ago
{
8 years ago
if ( nn_bind(pubsock,nanomsg_tcpname(myinfo,str,myinfo->ipaddr,PUB_SOCK)) < 0 )
8 years ago
{
8 years ago
printf("error binding to pubsock (%s)\n",nanomsg_tcpname(myinfo,str,myinfo->ipaddr,PUB_SOCK));
8 years ago
nn_close(pubsock);
pubsock = -1;
nn_close(dexsock);
dexsock = -1;
nn_close(dpowsock);
dpowsock = -1;
8 years ago
}
else
{
8 years ago
if ( repsock < 0 && (repsock= nn_socket(AF_SP,NN_REP)) >= 0 )
8 years ago
{
8 years ago
if ( nn_bind(repsock,nanomsg_tcpname(myinfo,str,myinfo->ipaddr,REP_SOCK)) < 0 )
8 years ago
{
8 years ago
printf("error binding to repsock (%s)\n",nanomsg_tcpname(myinfo,str,myinfo->ipaddr,REP_SOCK));
8 years ago
nn_close(repsock);
repsock = -1;
nn_close(pubsock);
pubsock = -1;
nn_close(dexsock);
dexsock = -1;
nn_close(dpowsock);
dpowsock = -1;
8 years ago
}
else
{
8 years ago
timeout = 100;
8 years ago
nn_setsockopt(dexsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout));
nn_setsockopt(repsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout));
nn_setsockopt(dexsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout));
8 years ago
timeout = 200;
8 years ago
nn_setsockopt(repsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout));
8 years ago
maxsize = 1024 * 1024;
8 years ago
printf("RCVBUF.%d\n",nn_setsockopt(dexsock,NN_SOL_SOCKET,NN_RCVBUF,&maxsize,sizeof(maxsize)));
printf("RCVBUF.%d\n",nn_setsockopt(repsock,NN_SOL_SOCKET,NN_RCVBUF,&maxsize,sizeof(maxsize)));
printf("DEXINIT dpow.%d dex.%d rep.%d\n",dpowsock,myinfo->dexsock,myinfo->repsock);
8 years ago
}
}
}
}
}
8 years ago
}
8 years ago
myinfo->dpowipbits[0] = (uint32_t)calc_ipbits(myinfo->ipaddr);
myinfo->numdpowipbits = 1;
8 years ago
timeout = 300;
8 years ago
nn_setsockopt(dpowsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout));
8 years ago
maxsize = 1024 * 1024;
8 years ago
printf("RCVBUF.%d\n",nn_setsockopt(dpowsock,NN_SOL_SOCKET,NN_RCVBUF,&maxsize,sizeof(maxsize)));
8 years ago
myinfo->nanoinit = (uint32_t)time(NULL);
8 years ago
}
8 years ago
} //else printf("error creating nanosocket\n");
8 years ago
if ( myinfo->dpowsock != dpowsock )
myinfo->dpowsock = dpowsock;
if ( myinfo->dexsock != dexsock )
myinfo->dexsock = dexsock;
if ( myinfo->repsock != repsock )
myinfo->repsock = repsock;
if ( myinfo->pubsock != pubsock )
myinfo->pubsock = pubsock;
8 years ago
portable_mutex_unlock(&myinfo->notarymutex);
8 years ago
dpow_addnotary(myinfo,0,ipaddr);
8 years ago
}
8 years ago
void dpow_bestconsensus(struct dpow_block *bp)
{
int8_t bestks[64]; int32_t counts[64],i,j,numcrcs=0,numdiff,besti,best,bestmatches = 0,matches = 0; uint64_t masks[64],matchesmask,recvmask; uint32_t crcval=0; char srcaddr[64],destaddr[64];
memset(masks,0,sizeof(masks));
memset(bestks,0xff,sizeof(bestks));
memset(counts,0,sizeof(counts));
recvmask = 0;
for (numdiff=i=0; i<bp->numnotaries; i++)
{
if ( bits256_nonz(bp->notaries[i].src.prev_hash) != 0 && bits256_nonz(bp->notaries[i].dest.prev_hash) != 0 )
recvmask |= (1LL << i);
if ( bp->notaries[i].bestk < 0 || bp->notaries[i].bestmask == 0 )
continue;
//if ( bp->require0 != 0 && (bp->notaries[i].bestmask & 1) == 0 )
// continue;
for (j=0; j<numdiff; j++)
if ( bp->notaries[i].bestk == bestks[j] && bp->notaries[i].bestmask == masks[j] )
{
counts[j]++;
break;
}
if ( j == numdiff && bp->notaries[i].bestk >= 0 && bp->notaries[i].bestmask != 0 )
{
masks[numdiff] = bp->notaries[i].bestmask;
bestks[numdiff] = bp->notaries[i].bestk;
counts[numdiff]++;
//printf("j.%d numdiff.%d (%d %llx).%d\n",j,numdiff,bp->notaries[i].bestk,(long long)bp->notaries[i].bestmask,counts[numdiff]);
numdiff++;
}
}
besti = -1, best = 0;
for (i=0; i<numdiff; i++)
{
//printf("(%d %llx).%d ",bestks[i],(long long)masks[i],counts[i]);
if ( counts[i] > best )
{
best = counts[i];
besti = i;
}
}
if ( besti >= 0 && bestks[besti] >= 0 && masks[besti] != 0 && (recvmask & masks[besti]) == masks[besti] )
{
8 years ago
bp->notaries[bp->myind].bestmask = bp->bestmask = masks[besti];
bp->notaries[bp->myind].bestk = bp->bestk = bestks[besti];
//printf("set best.%d to (%d %llx) recv.%llx\n",best,bp->bestk,(long long)bp->bestmask,(long long)recvmask);
8 years ago
}
bp->recvmask = recvmask;
if ( bp->bestmask == 0 )//|| (time(NULL) / 180) != bp->lastepoch )
{
bp->bestmask = dpow_notarybestk(bp->recvmask,bp,&bp->bestk);
if ( 0 && (time(NULL) / 180) != bp->lastepoch )
{
bp->lastepoch = (uint32_t)(time(NULL) / 180);
printf("epoch %u\n",bp->lastepoch % bp->numnotaries);
sleep(1 + (rand() % 3));
}
}
}
8 years ago
void dpow_nanoutxoset(struct dpow_nanoutxo *np,struct dpow_block *bp,int32_t isratify)
8 years ago
{
int32_t i;
8 years ago
if ( bp->myind < 0 )
return;
8 years ago
if ( isratify != 0 )
{
np->srcutxo = bp->notaries[bp->myind].ratifysrcutxo;
np->srcvout = bp->notaries[bp->myind].ratifysrcvout;
np->destutxo = bp->notaries[bp->myind].ratifydestutxo;
np->destvout = bp->notaries[bp->myind].ratifydestvout;
8 years ago
np->bestmask = bp->ratifybestmask;
8 years ago
np->recvmask = bp->ratifyrecvmask;
8 years ago
//printf("send ratify best.(%d %llx) siglens.(%d %d)\n", bp->ratifybestk,(long long)bp->ratifybestmask,bp->ratifysiglens[0],bp->ratifysiglens[1]);
8 years ago
if ( (np->bestk= bp->ratifybestk) >= 0 )
8 years ago
{
8 years ago
for (i=0; i<2; i++)
{
8 years ago
if ( (np->siglens[i]= bp->ratifysiglens[i]) > 0 )
memcpy(np->sigs[i],bp->ratifysigs[i],np->siglens[i]);
8 years ago
}
8 years ago
}
}
else
{
dpow_bestconsensus(bp);
8 years ago
np->srcutxo = bp->notaries[bp->myind].src.prev_hash;
np->srcvout = bp->notaries[bp->myind].src.prev_vout;
np->destutxo = bp->notaries[bp->myind].dest.prev_hash;
np->destvout = bp->notaries[bp->myind].dest.prev_vout;
8 years ago
if ( (np->recvmask= bp->recvmask) == 0 )
np->recvmask = bp->notaries[bp->myind].recvmask;
if ( (np->bestmask= bp->pendingbestmask) == 0 )
{
if ( (np->bestmask= bp->notaries[bp->myind].bestmask) == 0 )
np->bestmask = bp->bestmask, np->bestk = bp->bestk;
else np->bestk = bp->notaries[bp->myind].bestk;
} else np->bestk = bp->pendingbestk;
8 years ago
if ( (int8_t)np->bestk >= 0 )
8 years ago
{
if ( (np->siglens[0]= bp->notaries[bp->myind].src.siglens[bp->bestk]) > 0 )
memcpy(np->sigs[0],bp->notaries[bp->myind].src.sigs[bp->bestk],np->siglens[0]);
if ( (np->siglens[1]= bp->notaries[bp->myind].dest.siglens[bp->bestk]) > 0 )
memcpy(np->sigs[1],bp->notaries[bp->myind].dest.sigs[bp->bestk],np->siglens[1]);
}
}
}
8 years ago
void dpow_ratify_update(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,uint8_t senderind,int8_t bestk,uint64_t bestmask,uint64_t recvmask,bits256 srcutxo,uint16_t srcvout,bits256 destutxo,uint16_t destvout,uint8_t siglens[2],uint8_t sigs[2][DPOW_MAXSIGLEN],uint32_t pendingcrcs[2])
8 years ago
{
8 years ago
int8_t bestks[64]; int32_t counts[64],i,j,numcrcs=0,numdiff,besti,best,bestmatches = 0,matches = 0; uint64_t masks[64],matchesmask; uint32_t crcval=0; char srcaddr[64],destaddr[64];
8 years ago
//char str[65],str2[65];
//printf("senderind.%d num.%d %s %s\n",senderind,bp->numnotaries,bits256_str(str,srcutxo),bits256_str(str2,destutxo));
8 years ago
if ( bp->myind < 0 )
return;
8 years ago
if ( bp->isratify != 0 && senderind >= 0 && senderind < bp->numnotaries && bits256_nonz(srcutxo) != 0 && bits256_nonz(destutxo) != 0 )
8 years ago
{
8 years ago
memset(masks,0,sizeof(masks));
memset(bestks,0xff,sizeof(bestks));
memset(counts,0,sizeof(counts));
8 years ago
for (i=0; i<2; i++)
8 years ago
bp->notaries[senderind].pendingcrcs[i] = pendingcrcs[i];
8 years ago
bp->notaries[senderind].ratifysrcutxo = srcutxo;
bp->notaries[senderind].ratifysrcvout = srcvout;
bp->notaries[senderind].ratifydestutxo = destutxo;
bp->notaries[senderind].ratifydestvout = destvout;
bp->notaries[senderind].ratifybestmask = bestmask;
bp->notaries[senderind].ratifyrecvmask = recvmask;
8 years ago
if ( (bp->notaries[senderind].ratifybestk= bestk) >= 0 )
8 years ago
{
8 years ago
for (i=0; i<2; i++)
8 years ago
{
8 years ago
if ( (bp->notaries[senderind].ratifysiglens[i]= siglens[i]) != 0 )
8 years ago
{
8 years ago
memcpy(bp->notaries[senderind].ratifysigs[i],sigs[i],siglens[i]);
if ( bestk == bp->pendingratifybestk && bestmask == bp->pendingratifybestmask )
8 years ago
{
if ( ((1LL << senderind) & bestmask) != 0 )
bp->ratifysigmasks[i] |= (1LL << senderind);
} else bp->ratifysigmasks[i] &= ~(1LL << senderind);
8 years ago
}
8 years ago
}
}
8 years ago
//printf("RECV from %d best.(%d %llx) sigs.(%d %d) %llx %llx\n",senderind,bestk,(long long)bestmask,siglens[0],siglens[1],(long long)bp->ratifysigmasks[0],(long long)bp->ratifysigmasks[1]);
8 years ago
bp->ratifyrecvmask = 0;
8 years ago
bp->ratifybestmask = 0;
bp->ratifybestk = -1;
for (numdiff=i=0; i<bp->numnotaries; i++)
{
8 years ago
if ( bits256_nonz(bp->notaries[i].ratifysrcutxo) != 0 && bits256_nonz(bp->notaries[i].ratifydestutxo) != 0 )
8 years ago
bp->ratifyrecvmask |= (1LL << i);
8 years ago
if ( bp->notaries[i].ratifybestk < 0 || bp->notaries[i].ratifybestmask == 0 )
continue;
if ( bp->require0 != 0 && (bp->notaries[i].ratifybestmask & 1) == 0 )
continue;
8 years ago
for (j=0; j<numdiff; j++)
8 years ago
if ( bp->notaries[i].ratifybestk == bestks[j] && bp->notaries[i].ratifybestmask == masks[j] )
8 years ago
{
counts[j]++;
break;
}
8 years ago
if ( j == numdiff && bp->notaries[i].ratifybestk >= 0 && bp->notaries[i].ratifybestmask != 0 )
8 years ago
{
8 years ago
masks[numdiff] = bp->notaries[i].ratifybestmask;
bestks[numdiff] = bp->notaries[i].ratifybestk;
8 years ago
counts[numdiff]++;
8 years ago
//printf("j.%d numdiff.%d (%d %llx).%d\n",j,numdiff,bp->notaries[i].ratifybestk,(long long)bp->notaries[i].ratifybestmask,counts[numdiff]);
8 years ago
numdiff++;
}
}
8 years ago
besti = -1, best = 0;
8 years ago
for (i=0; i<numdiff; i++)
{
8 years ago
//printf("(%d %llx).%d ",bestks[i],(long long)masks[i],counts[i]);
8 years ago
if ( counts[i] > best )
{
best = counts[i];
besti = i;
}
}
8 years ago
if ( besti >= 0 && bestks[besti] >= 0 && masks[besti] != 0 && (bp->ratifyrecvmask & masks[besti]) == masks[besti] )
bp->ratifybestmask = masks[besti], bp->ratifybestk = bestks[besti];
8 years ago
//printf("numdiff.%d besti.%d numbest.%d (%d %llx) vs (%d %llx)\n",numdiff,besti,best,besti>=0?bestks[besti]:-1,(long long)(besti>=0?masks[besti]:0),bestk,(long long)bestmask);
8 years ago
if ( bp->ratifybestmask == 0 || (time(NULL) / 100) != bp->lastepoch )
{
8 years ago
bp->ratifybestmask = dpow_ratifybest(bp->ratifyrecvmask,bp,&bp->ratifybestk);
8 years ago
if ( (time(NULL) / 100) != bp->lastepoch )
{
bp->lastepoch = (uint32_t)(time(NULL) / 100);
printf("epoch %u\n",bp->lastepoch % bp->numnotaries);
8 years ago
sleep(2 + (rand() % 7));
8 years ago
}
8 years ago
}
8 years ago
bp->notaries[bp->myind].ratifybestk = bp->ratifybestk;
bp->notaries[bp->myind].ratifybestmask = bp->ratifybestmask;
bp->notaries[bp->myind].ratifyrecvmask = bp->ratifyrecvmask;
8 years ago
if ( bp->ratifybestk >= 0 )
{
8 years ago
for (matchesmask=i=0; i<bp->numnotaries; i++)
8 years ago
{
if ( bp->ratifybestk >= 0 && bp->notaries[i].ratifybestk == bp->ratifybestk && bp->notaries[i].ratifybestmask == bp->ratifybestmask )
{
8 years ago
matches++;
8 years ago
if ( ((1LL << i) & bp->ratifybestmask) != 0 )
8 years ago
{
matchesmask |= (1LL << i);
8 years ago
bestmatches++;
8 years ago
}
}
}
crcval = 0;
numcrcs = 0;
for (i=0; i<bp->numnotaries; i++)
{
if ( ((1LL << i) & matchesmask) != 0 )
{
if ( numcrcs == 0 )
numcrcs++, crcval = bp->notaries[i].pendingcrcs[bp->state < 1000];
else if ( numcrcs > 0 && crcval == bp->notaries[i].pendingcrcs[bp->state < 1000] )
numcrcs++;
8 years ago
}
}
8 years ago
//printf("crcval.%x numcrcs.%d bestmatches.%d matchesmask.%llx\n",crcval,numcrcs,bestmatches,(long long)matchesmask);
8 years ago
if ( bestmatches >= bp->minsigs )
8 years ago
{
8 years ago
if ( bp->pendingratifybestk != bp->ratifybestk || bp->pendingratifybestmask != bp->ratifybestmask )
8 years ago
{
8 years ago
printf("new PENDING RATIFY BESTK (%d %llx)\n",bp->ratifybestk,(long long)bp->ratifybestmask);
bp->pendingratifybestk = bp->ratifybestk;
bp->pendingratifybestmask = bp->ratifybestmask;
8 years ago
memset(bp->notaries[bp->myind].ratifysigs,0,sizeof(bp->notaries[bp->myind].ratifysigs));
memset(bp->notaries[bp->myind].ratifysiglens,0,sizeof(bp->notaries[bp->myind].ratifysiglens));
8 years ago
memset(bp->ratifysigmasks,0,sizeof(bp->ratifysigmasks));
8 years ago
dpow_signedtxgen(myinfo,dp,bp->destcoin,bp,bp->ratifybestk,bp->ratifybestmask,bp->myind,DPOW_SIGBTCCHANNEL,1,1);
8 years ago
for (i=0; i<bp->numnotaries; i++)
{
if ( i != bp->myind )
{
memset(&bp->notaries[i].ratifysrcutxo,0,sizeof(bp->notaries[i].ratifysrcutxo));
memset(&bp->notaries[i].ratifydestutxo,0,sizeof(bp->notaries[i].ratifydestutxo));
bp->notaries[i].ratifybestmask = bp->notaries[i].ratifyrecvmask = 0;
}
else if ( bp->require0 == 0 )
{
bitcoin_address(srcaddr,bp->srccoin->chain->pubtype,dp->minerkey33,33);
bitcoin_address(destaddr,bp->destcoin->chain->pubtype,dp->minerkey33,33);
if ( dpow_checkutxo(myinfo,dp,bp,bp->destcoin,&bp->notaries[i].dest.prev_hash,&bp->notaries[i].dest.prev_vout,destaddr) < 0 )
{
printf("dont have %s %s utxo, please send funds\n",dp->dest,destaddr);
}
if ( dpow_checkutxo(myinfo,dp,bp,bp->srccoin,&bp->notaries[i].src.prev_hash,&bp->notaries[i].src.prev_vout,srcaddr) < 0 )
{
printf("dont have %s %s utxo, please send funds\n",dp->symbol,srcaddr);
}
}
}
8 years ago
}
8 years ago
if ( bp->ratifysigmasks[1] == bp->pendingratifybestmask ) // have all sigs
8 years ago
{
if ( bp->state < 1000 )
8 years ago
{
8 years ago
dpow_sigscheck(myinfo,dp,bp,bp->myind,1,bp->pendingratifybestk,bp->pendingratifybestmask,bp->ratified_pubkeys,bp->numratified);
8 years ago
}
8 years ago
if ( bp->ratifysigmasks[0] == bp->pendingratifybestmask ) // have all sigs
8 years ago
{
if ( bp->state != 0xffffffff )
8 years ago
dpow_sigscheck(myinfo,dp,bp,bp->myind,0,bp->pendingratifybestk,bp->pendingratifybestmask,bp->ratified_pubkeys,bp->numratified);
8 years ago
}
else if ( ((1LL << bp->myind) & bp->ratifybestmask) != 0 && (rand() % 100) == 0 )
{
dpow_signedtxgen(myinfo,dp,bp->srccoin,bp,bp->ratifybestk,bp->ratifybestmask,bp->myind,DPOW_SIGCHANNEL,0,1);
}
//else printf("ratify srcmask.%llx != bestmask.%llx\n",(long long)bp->ratifysigmasks[0],(long long)bp->bestmask);
8 years ago
}
8 years ago
else if ( ((1LL << bp->myind) & bp->ratifybestmask) != 0 && (rand() % 100) == 0 )
8 years ago
{
dpow_signedtxgen(myinfo,dp,bp->destcoin,bp,bp->ratifybestk,bp->ratifybestmask,bp->myind,DPOW_SIGBTCCHANNEL,1,1);
}
//else printf("ratify destmask.%llx != bestmask.%llx\n",(long long)bp->ratifysigmasks[1],(long long)bp->bestmask);
8 years ago
}
}
8 years ago
if ( (rand() % 100) == 0 )
8 years ago
printf("[%d] numips.%d %s RATIFY.%d matches.%d bestmatches.%d bestk.%d %llx recv.%llx %llx sigmasks.(%llx %llx) crcval.%x num.%d\n",bp->myind,dp->numipbits,dp->symbol,bp->minsigs,matches,bestmatches,bp->ratifybestk,(long long)bp->ratifybestmask,(long long)bp->ratifyrecvmask,(long long)matchesmask,(long long)bp->ratifysigmasks[1],(long long)bp->ratifysigmasks[0],crcval,numcrcs);
8 years ago
}
}
8 years ago
void dpow_notarize_update(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,uint8_t senderind,int8_t bestk,uint64_t bestmask,uint64_t recvmask,bits256 srcutxo,uint16_t srcvout,bits256 destutxo,uint16_t destvout,uint8_t siglens[2],uint8_t sigs[2][DPOW_MAXSIGLEN],uint32_t paxwdcrc)
8 years ago
{
8 years ago
int32_t i,bestmatches = 0,matches = 0,paxmatches = 0,paxbestmatches = 0;
8 years ago
if ( bp->myind < 0 )
return;
8 years ago
if ( bp->isratify == 0 && bp->state != 0xffffffff && senderind >= 0 && senderind < bp->numnotaries && bits256_nonz(srcutxo) != 0 && bits256_nonz(destutxo) != 0 )
8 years ago
{
bp->notaries[senderind].src.prev_hash = srcutxo;
bp->notaries[senderind].src.prev_vout = srcvout;
bp->notaries[senderind].dest.prev_hash = destutxo;
bp->notaries[senderind].dest.prev_vout = destvout;
8 years ago
if ( bestmask != 0 )
8 years ago
bp->notaries[senderind].bestmask = bestmask;
8 years ago
if ( recvmask != 0 )
8 years ago
bp->notaries[senderind].recvmask = recvmask;
8 years ago
if ( (bp->notaries[senderind].paxwdcrc= paxwdcrc) != 0 )
8 years ago
{
//fprintf(stderr,"{%d %x} ",senderind,paxwdcrc);
}
8 years ago
if ( bestk >= 0 && (bp->notaries[senderind].bestk= bestk) >= 0 )
8 years ago
{
if ( (bp->notaries[senderind].src.siglens[bestk]= siglens[0]) != 0 )
{
memcpy(bp->notaries[senderind].src.sigs[bestk],sigs[0],siglens[0]);
if ( bestk == bp->bestk && bestmask == bp->bestmask )
8 years ago
bp->srcsigsmasks[bestk] |= (1LL << senderind);
else bp->srcsigsmasks[bestk] &= ~(1LL << senderind);
8 years ago
}
if ( (bp->notaries[senderind].dest.siglens[bestk]= siglens[1]) != 0 )
{
memcpy(bp->notaries[senderind].dest.sigs[bestk],sigs[1],siglens[1]);
if ( bestk == bp->bestk && bestmask == bp->bestmask )
8 years ago
bp->destsigsmasks[bestk] |= (1LL << senderind);
else bp->destsigsmasks[bestk] &= ~(1LL << senderind);
8 years ago
}
}
8 years ago
dpow_bestconsensus(bp);
8 years ago
//bp->recvmask |= (1LL << senderind) | (1LL << bp->myind);
//bp->bestmask = dpow_maskmin(bp->recvmask,bp,&bp->bestk);
8 years ago
//if ( bp->paxwdcrc != 0 )
8 years ago
bp->notaries[bp->myind].paxwdcrc = bp->paxwdcrc;
if ( bp->bestk >= 0 )
bp->notaries[bp->myind].bestk = bp->bestk;
if ( bp->bestmask != 0 )
bp->notaries[bp->myind].bestmask = bp->bestmask;
if ( bp->recvmask != 0 )
bp->notaries[bp->myind].recvmask = bp->recvmask;
8 years ago
if ( bp->bestk >= 0 )
{
8 years ago
for (i=0; i<bp->numnotaries; i++)
8 years ago
{
8 years ago
if ( bp->paxwdcrc == bp->notaries[i].paxwdcrc )
paxmatches++;
8 years ago
if ( bp->bestk >= 0 && bp->notaries[i].bestk == bp->bestk && bp->notaries[i].bestmask == bp->bestmask )
{
matches++;
8 years ago
if ( ((1LL << i) & bp->bestmask) != 0 )
8 years ago
{
if ( bp->paxwdcrc == bp->notaries[i].paxwdcrc )
{
bestmatches++;
paxbestmatches++;
8 years ago
} //else printf("?%x ",bp->notaries[i].paxwdcrc);
8 years ago
}
8 years ago
}
8 years ago
if ( 0 && bp->myind <= 1 && bp->notaries[i].paxwdcrc != 0 )
8 years ago
printf("%d.(%x %d %llx r%llx) ",i,bp->notaries[i].paxwdcrc,bp->notaries[i].bestk,(long long)bp->notaries[i].bestmask,(long long)bp->notaries[i].recvmask);
8 years ago
}
8 years ago
if ( 0 && bp->myind <= 1 )
8 years ago
printf("recv.%llx best.(%d %llx) m.%d p.%d:%d b.%d\n",(long long)bp->recvmask,bp->bestk,(long long)bp->bestmask,matches,paxmatches,paxbestmatches,bestmatches);
8 years ago
if ( bestmatches >= bp->minsigs && paxbestmatches >= bp->minsigs )
8 years ago
{
if ( bp->pendingbestk != bp->bestk || bp->pendingbestmask != bp->bestmask )
{
8 years ago
printf("new PENDING BESTK (%d %llx) state.%d\n",bp->bestk,(long long)bp->bestmask,bp->state);
8 years ago
bp->pendingbestk = bp->bestk;
bp->pendingbestmask = bp->bestmask;
dpow_signedtxgen(myinfo,dp,bp->destcoin,bp,bp->bestk,bp->bestmask,bp->myind,DPOW_SIGBTCCHANNEL,1,0);
8 years ago
printf("finished signing\n");
8 years ago
}
8 years ago
if ( bp->destsigsmasks[bp->bestk] == bp->bestmask ) // have all sigs
8 years ago
{
if ( bp->state < 1000 )
8 years ago
dpow_sigscheck(myinfo,dp,bp,bp->myind,1,bp->bestk,bp->bestmask,0,0);
8 years ago
if ( bp->srcsigsmasks[bp->bestk] == bp->bestmask ) // have all sigs
8 years ago
{
if ( bp->state != 0xffffffff )
8 years ago
dpow_sigscheck(myinfo,dp,bp,bp->myind,0,bp->bestk,bp->bestmask,0,0);
8 years ago
} //else printf("srcmask.%llx != bestmask.%llx\n",(long long)bp->srcsigsmasks[bp->bestk],(long long)bp->bestmask);
} //else printf("destmask.%llx != bestmask.%llx\n",(long long)bp->destsigsmasks[bp->bestk],(long long)bp->bestmask);
8 years ago
}
}
8 years ago
if ( (rand() % 30) == 0 )
8 years ago
printf("[%d] ips.%d %s NOTARIZE.%d matches.%d paxmatches.%d bestmatches.%d bestk.%d %llx recv.%llx sigmasks.(%llx %llx) senderind.%d state.%x (%x %x %x) pax.%x\n",bp->myind,dp->numipbits,dp->symbol,bp->minsigs,matches,paxmatches,bestmatches,bp->bestk,(long long)bp->bestmask,(long long)bp->recvmask,(long long)(bp->bestk>=0?bp->destsigsmasks[bp->bestk]:0),(long long)(bp->bestk>=0?bp->srcsigsmasks[bp->bestk]:0),senderind,bp->state,bp->hashmsg.uints[0],bp->desttxid.uints[0],bp->srctxid.uints[0],bp->paxwdcrc);
8 years ago
}
}
8 years ago
void dpow_nanoutxoget(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,struct dpow_nanoutxo *np,int32_t isratify,int8_t senderind,uint32_t channel)
8 years ago
{
if ( isratify != 0 )
{
8 years ago
dpow_ratify_update(myinfo,dp,bp,senderind,(int8_t)np->bestk,np->bestmask,np->recvmask,np->srcutxo,np->srcvout,np->destutxo,np->destvout,np->siglens,np->sigs,np->pendingcrcs);
8 years ago
}
else
{
8 years ago
dpow_notarize_update(myinfo,dp,bp,senderind,(int8_t)np->bestk,np->bestmask,np->recvmask,np->srcutxo,np->srcvout,np->destutxo,np->destvout,np->siglens,np->sigs,np->paxwdcrc);
8 years ago
if ( 0 && bp->myind <= 2 )
8 years ago
printf("lag.[%d] RECV.%d r%llx (%d %llx) %llx/%llx\n",(int32_t)(time(NULL)-channel),senderind,(long long)np->recvmask,(int8_t)np->bestk,(long long)np->bestmask,(long long)np->srcutxo.txid,(long long)np->destutxo.txid);
8 years ago
}
//dpow_bestmask_update(myinfo,dp,bp,nn_senderind,nn_bestk,nn_bestmask,nn_recvmask);
}
8 years ago
void dpow_send(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,bits256 srchash,bits256 desthash,uint32_t channel,uint32_t msgbits,uint8_t *data,int32_t datalen)
8 years ago
{
8 years ago
struct dpow_nanomsghdr *np; int32_t i,size,extralen=0,sentbytes = 0; uint32_t crc32,paxwdcrc; uint8_t extras[10000];
8 years ago
if ( bp->myind < 0 )
return;
8 years ago
if ( time(NULL) < myinfo->nanoinit+5 )
return;
8 years ago
crc32 = calc_crc32(0,data,datalen);
8 years ago
//dp->crcs[firstz] = crc32;
size = (int32_t)(sizeof(*np) + datalen);
np = calloc(1,size); // endian dependent!
if ( (np->numipbits= dp->numipbits) == 0 )
8 years ago
{
8 years ago
dp->ipbits[0] = myinfo->myaddr.myipbits;
np->numipbits = dp->numipbits = 1;
8 years ago
}
8 years ago
np->senderind = bp->myind;
memcpy(np->ipbits,dp->ipbits,dp->numipbits * sizeof(*dp->ipbits));
//for (i=0; i<np->numipbits; i++)
// printf("%08x ",np->ipbits[i]);
//printf(" dpow_send.(%d) size.%d numipbits.%d myind.%d\n",datalen,size,np->numipbits,bp->myind);
if ( bp->isratify == 0 )
8 years ago
{
8 years ago
extralen = dpow_paxpending(extras,&paxwdcrc);
bp->paxwdcrc = bp->notaries[bp->myind].paxwdcrc = np->notarize.paxwdcrc = paxwdcrc;
8 years ago
//dpow_bestconsensus(bp);
8 years ago
dpow_nanoutxoset(&np->notarize,bp,0);
8 years ago
}
else
{
bp->paxwdcrc = bp->notaries[bp->myind].paxwdcrc = np->notarize.paxwdcrc = 0;
dpow_nanoutxoset(&np->ratify,bp,1);
}
8 years ago
np->size = size;
np->datalen = datalen;
np->crc32 = crc32;
for (i=0; i<2; i++)
np->ratify.pendingcrcs[i] = bp->pendingcrcs[i];
for (i=0; i<32; i++)
np->srchash.bytes[i] = dp->minerkey33[i+1];
np->desthash = desthash;
8 years ago
np->channel = channel;
8 years ago
np->height = msgbits;
np->myipbits = myinfo->myaddr.myipbits;
strcpy(np->symbol,dp->symbol);
np->version0 = DPOW_VERSION & 0xff;
np->version1 = (DPOW_VERSION >> 8) & 0xff;
memcpy(np->packet,data,datalen);
8 years ago
sentbytes = -1;
8 years ago
// deadlocks! portable_mutex_lock(&myinfo->dpowmutex);
8 years ago
for (i=0; i<100; i++)
8 years ago
{
struct nn_pollfd pfd;
pfd.fd = myinfo->dpowsock;
pfd.events = NN_POLLOUT;
if ( nn_poll(&pfd,1,100) > 0 )
{
sentbytes = nn_send(myinfo->dpowsock,np,size,0);
break;
}
8 years ago
usleep(1000);
8 years ago
}
8 years ago
//portable_mutex_unlock(&myinfo->dpowmutex);
8 years ago
free(np);
8 years ago
if ( 0 && bp->myind <= 2 )
8 years ago
printf("%d NANOSEND.%d ht.%d channel.%08x (%d) pax.%08x datalen.%d (%d %llx) (%d %llx) recv.%llx\n",i,sentbytes,np->height,np->channel,size,np->notarize.paxwdcrc,datalen,(int8_t)np->notarize.bestk,(long long)np->notarize.bestmask,bp->notaries[bp->myind].bestk,(long long)bp->notaries[bp->myind].bestmask,(long long)bp->recvmask);
8 years ago
}
8 years ago
void dpow_ipbitsadd(struct supernet_info *myinfo,struct dpow_info *dp,uint32_t *ipbits,int32_t numipbits,int32_t fromid,uint32_t senderipbits)
8 years ago
{
8 years ago
int32_t i,j,matched,missing,n; char ipaddr[64];
8 years ago
if ( numipbits >= 64 )
8 years ago
{
printf("dpow_ipbitsadd reject from.%d numipbits.%d\n",fromid,numipbits);
8 years ago
return;
8 years ago
}
8 years ago
n = dp->numipbits;
8 years ago
matched = missing = 0;
8 years ago
//for (i=0; i<numipbits; i++)
// printf("%08x ",ipbits[i]);
8 years ago
for (i=0; i<numipbits; i++)
{
8 years ago
for (j=0; j<n; j++)
8 years ago
if ( ipbits[i] == dp->ipbits[j] )
8 years ago
{
matched++;
ipbits[i] = 0;
break;
}
8 years ago
if ( j == n )
8 years ago
missing++;
}
8 years ago
if ( (numipbits == 1 || missing < matched || matched > 0) && missing > 0 )
8 years ago
{
for (i=0; i<numipbits; i++)
if ( ipbits[i] != 0 )
{
expand_ipbits(ipaddr,ipbits[i]);
8 years ago
//printf("ADD NOTARY.(%s) %08x\n",ipaddr,ipbits[i]);
8 years ago
dpow_addnotary(myinfo,dp,ipaddr);
8 years ago
}
8 years ago
} else if ( missing > 0 )
printf("IGNORE from.%d RECV numips.%d numipbits.%d matched.%d missing.%d\n",fromid,numipbits,n,matched,missing);
8 years ago
expand_ipbits(ipaddr,senderipbits);
dpow_addnotary(myinfo,dp,ipaddr);
8 years ago
expand_ipbits(ipaddr,myinfo->myaddr.myipbits);
dpow_addnotary(myinfo,dp,ipaddr);
8 years ago
//printf("recv numips.(%d %d)\n",myinfo->numdpowipbits,dp->numipbits);
8 years ago
}
8 years ago
int32_t dpow_nanomsg_update(struct supernet_info *myinfo)
8 years ago
{
8 years ago
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;
8 years ago
if ( time(NULL) < myinfo->nanoinit+5 || myinfo->dpowsock < 0 )
8 years ago
return(-1);
8 years ago
portable_mutex_lock(&myinfo->dpowmutex);
8 years ago
for (i=0; i<100; i++)
{
struct nn_pollfd pfd;
pfd.fd = myinfo->dpowsock;
pfd.events = NN_POLLIN;
if ( nn_poll(&pfd,1,100) > 0 )
break;
8 years ago
usleep(1000);
8 years ago
}
8 years ago
if ( i < 100 && (size= nn_recv(myinfo->dpowsock,&np,NN_MSG,0)) >= 0 )
8 years ago
{
8 years ago
num++;
8 years ago
if ( size >= 0 )
{
8 years ago
if ( np->version0 == (DPOW_VERSION & 0xff) && np->version1 == ((DPOW_VERSION >> 8) & 0xff) )
8 years ago
{
8 years ago
//printf("v.%02x %02x datalen.%d size.%d %d vs %d\n",np->version0,np->version1,np->datalen,size,np->datalen,(int32_t)(size - sizeof(*np)));
8 years ago
if ( np->datalen == (size - sizeof(*np)) )
8 years ago
{
8 years ago
crc32 = calc_crc32(0,np->packet,np->datalen);
8 years ago
dp = 0;
for (i=0; i<myinfo->numdpows; i++)
8 years ago
{
8 years ago
if ( strcmp(np->symbol,myinfo->DPOWS[i].symbol) == 0 )
8 years ago
{
8 years ago
dp = &myinfo->DPOWS[i];
break;
8 years ago
}
8 years ago
}
8 years ago
if ( dp != 0 && crc32 == np->crc32 )
8 years ago
{
8 years ago
//char str[65]; printf("%s RECV ht.%d ch.%08x (%d) crc32.%08x:%08x datalen.%d:%d firstz.%d\n",bits256_str(str,np->srchash),np->height,np->channel,size,np->crc32,crc32,np->datalen,(int32_t)(size - sizeof(*np)),firstz);
8 years ago
if ( i == myinfo->numdpows )
8 years ago
printf("received nnpacket for (%s)\n",np->symbol);
8 years ago
else
{
8 years ago
dpow_ipbitsadd(myinfo,dp,np->ipbits,np->numipbits,np->senderind,np->myipbits);
8 years ago
if ( (bp= dpow_heightfind(myinfo,dp,np->height)) != 0 && bp->state != 0xffffffff && bp->myind >= 0 )
8 years ago
{
8 years ago
if ( np->senderind >= 0 && np->senderind < bp->numnotaries )
8 years ago
{
8 years ago
if ( memcmp(bp->notaries[np->senderind].pubkey+1,np->srchash.bytes,32) == 0 && bits256_nonz(np->srchash) != 0 )
{
if ( bp->isratify == 0 )
8 years ago
dpow_nanoutxoget(myinfo,dp,bp,&np->notarize,0,np->senderind,np->channel);
else dpow_nanoutxoget(myinfo,dp,bp,&np->ratify,1,np->senderind,np->channel);
8 years ago
dpow_datahandler(myinfo,dp,bp,np->senderind,np->channel,np->height,np->packet,np->datalen);
} else printf("wrong senderind.%d\n",np->senderind);
8 years ago
}
8 years ago
}
//dp->crcs[firstz] = crc32;
}
8 years ago
} //else printf("crc error from.%d %x vs %x or no dp.%p [%s]\n",np->senderind,crc32,np->crc32,dp,np->symbol);
} //else printf("ignore.%d np->datalen.%d %d (size %d - %ld) [%s]\n",np->senderind,np->datalen,(int32_t)(size-sizeof(*np)),size,sizeof(*np),np->symbol);
} //else printf("wrong version from.%d %02x %02x size.%d [%s]\n",np->senderind,np->version0,np->version1,size,np->symbol);
8 years ago
} else printf("illegal size.%d\n",size);
8 years ago
if ( np != 0 )
nn_freemsg(np), np = 0;
8 years ago
} //else printf("no packets\n");
8 years ago
n = 0;
8 years ago
if ( myinfo->dexsock >= 0 ) // from servers
8 years ago
{
8 years ago
if ( (size= nn_recv(myinfo->dexsock,&dexp,NN_MSG,0)) >= 0 )
8 years ago
{
8 years ago
num++;
8 years ago
if ( dex_packetcheck(myinfo,dexp,size) == 0 )
8 years ago
{
printf("FROM BUS.%08x -> pub\n",dexp->crc32);
nn_send(myinfo->pubsock,dexp,size,0);
dex_packet(myinfo,dexp,size);
8 years ago
}
8 years ago
//printf("GOT DEX bus PACKET.%d\n",size);
8 years ago
if ( dexp != 0 )
nn_freemsg(dexp), dexp = 0;
8 years ago
}
8 years ago
}
8 years ago
if ( myinfo->repsock >= 0 ) // from clients
8 years ago
{
8 years ago
if ( (size= nn_recv(myinfo->repsock,&dexp,NN_MSG,0)) >= 0 )
8 years ago
{
8 years ago
num++;
8 years ago
if ( (m= myinfo->numdpowipbits) > 0 )
{
r = myinfo->dpowipbits[rand() % m];
nn_send(myinfo->repsock,&r,sizeof(r),0);
8 years ago
printf("REP.%08x <- rand ip m.%d %x\n",dexp->crc32,m,r);
8 years ago
} else printf("illegal state without dpowipbits?\n");
8 years ago
if ( dex_packetcheck(myinfo,dexp,size) == 0 )
8 years ago
{
8 years ago
nn_send(myinfo->dexsock,dexp,size,0);
8 years ago
nn_send(myinfo->pubsock,dexp,size,0);
8 years ago
printf("REP.%08x -> dexbus and pub, t.%d lag.%d\n",dexp->crc32,dexp->timestamp,(int32_t)(time(NULL)-dexp->timestamp));
8 years ago
dex_packet(myinfo,dexp,size);
8 years ago
}
8 years ago
//printf("GOT DEX rep PACKET.%d\n",size);
8 years ago
if ( dexp != 0 )
nn_freemsg(dexp), dexp = 0;
8 years ago
}
}
8 years ago
portable_mutex_unlock(&myinfo->dpowmutex);
8 years ago
return(num);
8 years ago
}
#else
void dpow_nanomsginit(struct supernet_info *myinfo,char *ipaddr) { }
8 years ago
uint32_t dpow_send(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,bits256 srchash,bits256 desthash,uint32_t channel,uint32_t msgbits,uint8_t *data,int32_t datalen)
8 years ago
{
return(0);
}
8 years ago
int32_t dpow_nanomsg_update(struct supernet_info *myinfo) { return(0); }
8 years ago
#endif
int32_t dpow_opreturnscript(uint8_t *script,uint8_t *opret,int32_t opretlen)
{
int32_t offset = 0;
script[offset++] = 0x6a;
if ( opretlen >= 0x4c )
{
if ( opretlen > 0xff )
{
script[offset++] = 0x4d;
script[offset++] = opretlen & 0xff;
script[offset++] = (opretlen >> 8) & 0xff;
}
else
{
script[offset++] = 0x4c;
script[offset++] = opretlen;
}
} else script[offset++] = opretlen;
memcpy(&script[offset],opret,opretlen);
return(opretlen + offset);
}
8 years ago
int32_t dpow_rwopret(int32_t rwflag,uint8_t *opret,bits256 *hashmsg,int32_t *heightmsgp,char *src,uint8_t *extras,int32_t extralen,struct dpow_block *bp,int32_t src_or_dest)
8 years ago
{
8 years ago
int32_t i,opretlen = 0; //bits256 beacon,beacons[DPOW_MAXRELAYS];
8 years ago
opretlen += iguana_rwbignum(rwflag,&opret[opretlen],sizeof(*hashmsg),hashmsg->bytes);
opretlen += iguana_rwnum(rwflag,&opret[opretlen],sizeof(*heightmsgp),(uint32_t *)heightmsgp);
8 years ago
if ( src_or_dest == 0 )
8 years ago
{
8 years ago
//char str[65]; printf("src_or_dest.%d opreturn add %s\n",src_or_dest,bits256_str(str,bp->desttxid));
8 years ago
if ( bits256_nonz(bp->desttxid) == 0 )
8 years ago
{
printf("no desttxid\n");
8 years ago
return(-1);
8 years ago
}
8 years ago
opretlen += iguana_rwbignum(rwflag,&opret[opretlen],sizeof(bp->desttxid),bp->desttxid.bytes);
8 years ago
}
8 years ago
/*else if ( 0 )
8 years ago
{
memset(beacons,0,sizeof(beacons));
for (i=0; i<bp->numnotaries; i++)
{
if ( ((1LL << i) & bp->bestmask) != 0 )
beacons[i] = bp->notaries[i].beacon;
}
vcalc_sha256(0,beacon.bytes,beacons[0].bytes,sizeof(*beacons) * bp->numnotaries);
opretlen += iguana_rwbignum(rwflag,&opret[opretlen],sizeof(beacon),beacon.bytes);
8 years ago
}*/
if ( rwflag != 0 )
{
if ( src != 0 )
{
for (i=0; src[i]!=0; i++)
opret[opretlen++] = src[i];
}
opret[opretlen++] = 0;
8 years ago
if ( extras != 0 && extralen > 0 )
{
memcpy(&opret[opretlen],extras,extralen);
opretlen += extralen;
8 years ago
printf("added extra.%d opreturn for withdraws paxwdcrc.%08x\n",extralen,calc_crc32(0,extras,extralen));
8 years ago
}
8 years ago
}
else
{
if ( src != 0 )
{
for (i=0; opret[opretlen]!=0; i++)
src[i] = opret[opretlen++];
src[i] = 0;
}
opretlen++;
8 years ago
}
8 years ago
return(opretlen);
}
int32_t dpow_rwsigentry(int32_t rwflag,uint8_t *data,struct dpow_sigentry *dsig)
{
int32_t i,len = 0;
if ( rwflag != 0 )
{
data[len++] = dsig->senderind;
data[len++] = dsig->lastk;
len += iguana_rwnum(rwflag,&data[len],sizeof(dsig->mask),(uint8_t *)&dsig->mask);
data[len++] = dsig->siglen;
memcpy(&data[len],dsig->sig,dsig->siglen), len += dsig->siglen;
for (i=0; i<sizeof(dsig->beacon); i++)
data[len++] = dsig->beacon.bytes[i];
for (i=0; i<33; i++)
data[len++] = dsig->senderpub[i];
}
else
{
memset(dsig,0,sizeof(*dsig));
dsig->senderind = data[len++];
if ( dsig->senderind < 0 || dsig->senderind >= DPOW_MAXRELAYS )
return(-1);
dsig->lastk = data[len++];
len += iguana_rwnum(rwflag,&data[len],sizeof(dsig->mask),(uint8_t *)&dsig->mask);
dsig->siglen = data[len++];
memcpy(dsig->sig,&data[len],dsig->siglen), len += dsig->siglen;
for (i=0; i<sizeof(dsig->beacon); i++)
dsig->beacon.bytes[i] = data[len++];
for (i=0; i<33; i++)
dsig->senderpub[i] = data[len++];
}
return(len);
}
8 years ago
void dpow_sigsend(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,int32_t myind,int8_t bestk,uint64_t bestmask,bits256 srchash,uint32_t sigchannel)
8 years ago
{
struct dpow_sigentry dsig; int32_t i,len; uint8_t data[4096]; struct dpow_entry *ep;
8 years ago
if ( bp->myind < 0 )
return;
8 years ago
if ( ((1LL << myind) & bestmask) == 0 )
return;
8 years ago
ep = &bp->notaries[myind];
8 years ago
if ( bestk >= 0 )
{
if ( sigchannel == DPOW_SIGCHANNEL )
bp->srcsigsmasks[bestk] |= (1LL << myind);
else bp->destsigsmasks[bestk] |= (1LL << myind);
}
8 years ago
//printf("ht.%d sigsend.%s: myind.%d bestk.%d %llx >>>>>> best.(%d %llx) recv.%llx sigs.%llx\n",bp->height,sigchannel == DPOW_SIGCHANNEL ? bp->srccoin->symbol : bp->destcoin->symbol,myind,bestk,(long long)bestmask,bestk,(long long)(bestk>=0?bestmask:0),(long long)bp->recvmask,(long long)(bestk>=0?bp->destsigsmasks[bestk]:0));
8 years ago
memset(&dsig,0,sizeof(dsig));
for (i=0; i<33; i++)
8 years ago
dsig.senderpub[i] = dp->minerkey33[i];
8 years ago
dsig.lastk = bestk;
dsig.mask = bestmask;
dsig.senderind = myind;
dsig.beacon = bp->beacon;
8 years ago
if ( sigchannel == DPOW_SIGBTCCHANNEL )
{
dsig.siglen = ep->dest.siglens[bestk];
memcpy(dsig.sig,ep->dest.sigs[bestk],ep->dest.siglens[bestk]);
}
else
{
dsig.siglen = ep->src.siglens[bestk];
memcpy(dsig.sig,ep->src.sigs[bestk],ep->src.siglens[bestk]);
}
8 years ago
memcpy(dsig.senderpub,dp->minerkey33,33);
8 years ago
len = dpow_rwsigentry(1,data,&dsig);
8 years ago
dpow_send(myinfo,dp,bp,srchash,bp->hashmsg,sigchannel,bp->height,data,len);
8 years ago
}
8 years ago
uint32_t komodo_assetmagic(char *symbol,uint64_t supply)
{
uint8_t buf[512]; int32_t len = 0;
len = iguana_rwnum(1,&buf[len],sizeof(supply),(void *)&supply);
strcpy((char *)&buf[len],symbol);
len += strlen(symbol);
return(calc_crc32(0,buf,len));
}
8 years ago
/*int32_t komodo_shortflag(char *symbol)
8 years ago
{
int32_t i,shortflag = 0;
if ( symbol[0] == '-' )
{
shortflag = 1;
for (i=0; symbol[i+1]!=0; i++)
symbol[i] = symbol[i+1];
symbol[i] = 0;
}
return(shortflag);
8 years ago
}*/
8 years ago
8 years ago
uint16_t komodo_assetport(uint32_t magic)
8 years ago
{
8 years ago
return(8000 + (magic % 7777));
8 years ago
}
8 years ago
uint16_t komodo_port(char *symbol,uint64_t supply,uint32_t *magicp)
8 years ago
{
*magicp = komodo_assetmagic(symbol,supply);
8 years ago
return(komodo_assetport(*magicp));
8 years ago
}
#define MAX_CURRENCIES 32
extern char CURRENCIES[][8];
void komodo_assetcoins()
{
8 years ago
uint16_t extract_userpass(char *serverport,char *userpass,char *coinstr,char *userhome,char *coindir,char *confname);
8 years ago
int32_t i,j; uint32_t magic; cJSON *json; uint16_t port; long filesize; char *userhome,confstr[16],jsonstr[512],magicstr[9],path[512]; struct iguana_info *coin;
8 years ago
if ( (userhome= OS_filestr(&filesize,"userhome.txt")) == 0 )
userhome = "root";
else
{
while ( userhome[strlen(userhome)-1] == '\r' || userhome[strlen(userhome)-1] == '\n' )
userhome[strlen(userhome)-1] = 0;
}
8 years ago
for (i=0; i<MAX_CURRENCIES; i++)
{
8 years ago
port = komodo_port(CURRENCIES[i],10,&magic);
8 years ago
for (j=0; j<4; j++)
sprintf(&magicstr[j*2],"%02x",((uint8_t *)&magic)[j]);
magicstr[j*2] = 0;
8 years ago
sprintf(jsonstr,"{\"newcoin\":\"%s\",\"RELAY\":-1,\"VALIDATE\":0,\"portp2p\":%u,\"rpcport\":%u,\"netmagic\":\"%s\"}",CURRENCIES[i],port,port+1,magicstr);
8 years ago
if ( (json= cJSON_Parse(jsonstr)) != 0 )
{
if ( (coin= iguana_coinadd(CURRENCIES[i],CURRENCIES[i],json,0)) == 0 )
{
printf("Cant create (%s)\n",CURRENCIES[i]);
return;
}
free_json(json);
8 years ago
coin->FULLNODE = -1;
coin->chain->rpcport = port + 1;
coin->chain->pubtype = 60;
coin->chain->p2shtype = 85;
coin->chain->wiftype = 188;
sprintf(confstr,"%s.conf",CURRENCIES[i]);
sprintf(path,"%s/.komodo/%s",userhome,CURRENCIES[i]);
extract_userpass(coin->chain->serverport,coin->chain->userpass,CURRENCIES[i],coin->chain->userhome,path,confstr);
8 years ago
}
printf("(%s %u) ",CURRENCIES[i],port);
}
printf("ports\n");
}