diff --git a/iguana/dpow/dpow_fsm.c b/iguana/dpow/dpow_fsm.c index 768bf1eba..f961995cf 100755 --- a/iguana/dpow/dpow_fsm.c +++ b/iguana/dpow/dpow_fsm.c @@ -13,62 +13,6 @@ * * ******************************************************************************/ -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][76]) -{ - int32_t i,bestmatches = 0,matches = 0; - //char str[65],str2[65]; - //printf("senderind.%d num.%d %s %s\n",senderind,bp->numnotaries,bits256_str(str,srcutxo),bits256_str(str2,destutxo)); - if ( senderind >= 0 && senderind < bp->numnotaries && bits256_nonz(srcutxo) != 0 && bits256_nonz(destutxo) != 0 ) - { - 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; - bp->notaries[senderind].ratifybestk = bestk; - for (i=0; i<2; i++) - { - if ( (bp->notaries[senderind].ratifysiglens[i]= siglens[i]) != 0 ) - { - memcpy(bp->notaries[senderind].ratifysigs[i],sigs[i],siglens[i]); - if ( bestk == bp->pendingbestk && bestmask == bp->pendingbestmask ) - bp->ratifysigmasks[i] |= (1LL << senderind); - else bp->ratifysigmasks[i] &= ~(1LL << senderind); - } - } - bp->ratifyrecvmask |= (1LL << senderind) | (1LL << bp->myind); - bp->ratifybestmask = dpow_ratifybest(bp->ratifyrecvmask,bp,&bp->ratifybestk); - if ( bp->ratifybestk >= 0 ) - { - bp->notaries[bp->myind].ratifybestmask = bp->ratifybestmask; - bp->notaries[bp->myind].ratifyrecvmask = bp->ratifyrecvmask; - bp->notaries[bp->myind].ratifybestk = bp->ratifybestk; - for (i=0; inumnotaries; i++) - { - if ( bp->ratifybestk >= 0 && bp->notaries[i].ratifybestk == bp->ratifybestk && bp->notaries[i].ratifybestmask == bp->ratifybestmask ) - { - matches++; - if ( ((1LL << i) & bp->ratifybestmask) != 0 ) - bestmatches++; - } - } - if ( bestmatches >= bp->minsigs ) - { - if ( bp->pendingbestk != bp->ratifybestk || bp->pendingbestmask != bp->ratifybestmask ) - { - printf("new PENDING BESTK (%d %llx)\n",bp->ratifybestk,(long long)bp->ratifybestmask); - bp->pendingbestk = bp->ratifybestk; - bp->pendingbestmask = bp->ratifybestmask; - dpow_signedtxgen(myinfo,dp,bp->destcoin,bp,bp->pendingbestk,bp->pendingbestmask,bp->myind,DPOW_SIGBTCCHANNEL,1,1); - dpow_signedtxgen(myinfo,dp,bp->srccoin,bp,bp->pendingbestk,bp->pendingbestmask,bp->myind,DPOW_SIGCHANNEL,0,1); - } - } - } - printf("numips.%d RATIFY.%d matches.%d bestmatches.%d bestk.%d %llx recv.%llx sigmasks.(%llx %llx)\n",myinfo->numdpowipbits,bp->minsigs,matches,bestmatches,bp->ratifybestk,(long long)bp->ratifybestmask,(long long)bp->ratifyrecvmask,(long long)bp->ratifysigmasks[1],(long long)bp->ratifysigmasks[0]); - } -} - struct dpow_entry *dpow_notaryfind(struct supernet_info *myinfo,struct dpow_block *bp,int32_t height,int32_t *senderindp,uint8_t *senderpub) { int32_t i; @@ -170,23 +114,15 @@ void dpow_sync(struct supernet_info *myinfo,int32_t forceflag,struct dpow_info * } } -int32_t dpow_datahandler(struct supernet_info *myinfo,struct dpow_info *dp,uint8_t nn_senderind,int8_t nn_bestk,uint64_t nn_bestmask,uint64_t nn_recvmask,uint32_t channel,uint32_t height,uint8_t *data,int32_t datalen,bits256 nn_destutxo,uint16_t nn_destvout,bits256 nn_srcutxo,uint16_t nn_srcvout,uint8_t nn_siglens[2],uint8_t nn_sigs[2][76]) +int32_t dpow_datahandler(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,uint8_t nn_senderind,uint32_t channel,uint32_t height,uint8_t *data,int32_t datalen) { - bits256 txid,commit,srchash,hashmsg; struct dpow_block *bp = 0; uint32_t flag = 0; int32_t src_or_dest,senderind,i,iter,rlen,myind = -1; char str[65],str2[65]; struct dpow_sigentry dsig; struct dpow_entry *ep; struct dpow_coinentry *cp; struct dpow_utxoentry U; struct iguana_info *coin; - if ( (bp= dpow_heightfind(myinfo,dp,height)) == 0 ) - { - if ( 0 && (rand() % 100) == 0 && height > 0 ) - printf("couldnt find height.%d | if you just started notary dapp this is normal\n",height); - return(-1); - } + bits256 txid,commit,srchash,hashmsg; uint32_t flag = 0; int32_t src_or_dest,senderind,i,iter,rlen,myind = -1; char str[65],str2[65]; struct dpow_sigentry dsig; struct dpow_entry *ep; struct dpow_coinentry *cp; struct dpow_utxoentry U; struct iguana_info *coin; dpow_notaryfind(myinfo,bp,height,&myind,dp->minerkey33); if ( myind < 0 ) { printf("couldnt find myind height.%d | this means your pubkey for this node is not registered and needs to be ratified by majority vote of all notaries\n",height); return(-1); } - dpow_bestmask_update(myinfo,dp,bp,nn_senderind,nn_bestk,nn_bestmask,nn_recvmask); - dpow_ratify_update(myinfo,dp,bp,nn_senderind,nn_bestk,nn_bestmask,nn_recvmask,nn_srcutxo,nn_srcvout,nn_destutxo,nn_destvout,nn_siglens,nn_sigs); for (i=0; i<32; i++) srchash.bytes[i] = dp->minerkey33[i+1]; if ( channel == DPOW_ENTRIESCHANNEL ) diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c index b6dc15d10..b7c45e8b1 100755 --- a/iguana/dpow/dpow_network.c +++ b/iguana/dpow/dpow_network.c @@ -16,15 +16,20 @@ #if ISNOTARYNODE -struct dpow_nanomsghdr +struct dpow_nanoutxo { - bits256 srchash,desthash,srcutxo,destutxo; + bits256 srcutxo,destutxo; uint64_t bestmask,recvmask; - uint32_t channel,height,size,datalen,crc32,numipbits,ipbits[64]; uint16_t srcvout,destvout; + uint8_t sigs[2][76],siglens[2],bestk,pad; +} PACKED; + +struct dpow_nanomsghdr +{ + bits256 srchash,desthash; + struct dpow_nanopair ratify,notarize; + uint32_t channel,height,size,datalen,crc32,numipbits,ipbits[64]; char symbol[16]; - int8_t bestk; - uint8_t ratifysiglens[2],ratifysigs[2][76]; uint8_t senderind,version0,version1,packet[]; } PACKED; @@ -118,6 +123,98 @@ int32_t dpow_crc32find(struct supernet_info *myinfo,struct dpow_info *dp,uint32_ return(firstz); } +void dpow_sendutxoset(struct dpow_nanoutxo *np,struct dpow_block *bp,int32_t isratify) +{ + int32_t i; + 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; + np->bestmask = bp->ratifybestmask; + np->recvmask = bp->ratifyrecvmask; + np->bestk = bp->ratifybestk; + for (i=0; i<2; i++) + { + if ( (np->siglens[i]= bp->ratifysiglens[i]) > 0 ) + memcpy(np->sigs[i],bp->ratifysigs[i],np->siglens[i]); + } + } + else + { + 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; + np->bestmask = bp->bestmask; + np->recvmask = bp->recvmask; + if ( (np->bestk= bp->bestk) >= 0 ) + { + 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]); + } + } +} + +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][76]) +{ + int32_t i,bestmatches = 0,matches = 0; + //char str[65],str2[65]; + //printf("senderind.%d num.%d %s %s\n",senderind,bp->numnotaries,bits256_str(str,srcutxo),bits256_str(str2,destutxo)); + if ( senderind >= 0 && senderind < bp->numnotaries && bits256_nonz(srcutxo) != 0 && bits256_nonz(destutxo) != 0 ) + { + 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; + bp->notaries[senderind].ratifybestk = bestk; + for (i=0; i<2; i++) + { + if ( (bp->notaries[senderind].ratifysiglens[i]= siglens[i]) != 0 ) + { + memcpy(bp->notaries[senderind].ratifysigs[i],sigs[i],siglens[i]); + if ( bestk == bp->pendingbestk && bestmask == bp->pendingbestmask ) + bp->ratifysigmasks[i] |= (1LL << senderind); + else bp->ratifysigmasks[i] &= ~(1LL << senderind); + } + } + bp->ratifyrecvmask |= (1LL << senderind) | (1LL << bp->myind); + bp->ratifybestmask = dpow_ratifybest(bp->ratifyrecvmask,bp,&bp->ratifybestk); + if ( bp->ratifybestk >= 0 ) + { + bp->notaries[bp->myind].ratifybestmask = bp->ratifybestmask; + bp->notaries[bp->myind].ratifyrecvmask = bp->ratifyrecvmask; + bp->notaries[bp->myind].ratifybestk = bp->ratifybestk; + for (i=0; inumnotaries; i++) + { + if ( bp->ratifybestk >= 0 && bp->notaries[i].ratifybestk == bp->ratifybestk && bp->notaries[i].ratifybestmask == bp->ratifybestmask ) + { + matches++; + if ( ((1LL << i) & bp->ratifybestmask) != 0 ) + bestmatches++; + } + } + if ( bestmatches >= bp->minsigs ) + { + if ( bp->pendingbestk != bp->ratifybestk || bp->pendingbestmask != bp->ratifybestmask ) + { + printf("new PENDING BESTK (%d %llx)\n",bp->ratifybestk,(long long)bp->ratifybestmask); + bp->pendingbestk = bp->ratifybestk; + bp->pendingbestmask = bp->ratifybestmask; + dpow_signedtxgen(myinfo,dp,bp->destcoin,bp,bp->pendingbestk,bp->pendingbestmask,bp->myind,DPOW_SIGBTCCHANNEL,1,1); + dpow_signedtxgen(myinfo,dp,bp->srccoin,bp,bp->pendingbestk,bp->pendingbestmask,bp->myind,DPOW_SIGCHANNEL,0,1); + } + } + } + printf("numips.%d RATIFY.%d matches.%d bestmatches.%d bestk.%d %llx recv.%llx sigmasks.(%llx %llx)\n",myinfo->numdpowipbits,bp->minsigs,matches,bestmatches,bp->ratifybestk,(long long)bp->ratifybestmask,(long long)bp->ratifyrecvmask,(long long)bp->ratifysigmasks[1],(long long)bp->ratifysigmasks[0]); + } +} + 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) { struct dpow_nanomsghdr *np; int32_t i,size,sentbytes = 0; uint32_t crc32; @@ -128,19 +225,11 @@ void dpow_send(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_blo size = (int32_t)(sizeof(*np) + datalen); np = calloc(1,size); // endian dependent! np->numipbits = myinfo->numdpowipbits; - np->bestmask = dpow_maskmin(bp->recvmask,bp,&np->bestk); np->senderind = bp->myind; memcpy(np->ipbits,myinfo->dpowipbits,myinfo->numdpowipbits * sizeof(*myinfo->dpowipbits)); //printf("dpow_send.(%d) size.%d numipbits.%d\n",datalen,size,np->numipbits); - 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; - for (i=0; i<2; i++) - { - np->ratifysiglens[i] = bp->ratifysiglens[i]; - memcpy(np->ratifysigs[i],bp->ratifysigs[i],np->ratifysiglens[i]); - } + dpow_sendutxoset(&np->notarize,bp,0); + dpow_sendutxoset(&np->ratify,bp,1); np->size = size; np->datalen = datalen; np->crc32 = crc32; @@ -217,8 +306,16 @@ void dpow_nanomsg_update(struct supernet_info *myinfo) //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); if ( i == myinfo->numdpows ) printf("received nnpacket for (%s)\n",np->symbol); - else if ( dpow_datahandler(myinfo,dp,np->senderind,np->bestk,np->bestmask,np->recvmask,np->channel,np->height,np->packet,np->datalen,np->destutxo,np->destvout,np->srcutxo,np->srcvout,np->ratifysiglens,np->ratifysigs) >= 0 ) - dp->crcs[firstz] = crc32; + else + { + if ( (bp= dpow_heightfind(myinfo,dp,np->height)) != 0 ) + { + dpow_bestmask_update(myinfo,dp,bp,nn_senderind,nn_bestk,nn_bestmask,nn_recvmask); + dpow_ratify_update(myinfo,dp,bp,nn_senderind,nn_bestk,nn_bestmask,nn_recvmask,nn_srcutxo,nn_srcvout,nn_destutxo,nn_destvout,nn_siglens,nn_sigs); + dpow_datahandler(myinfo,dp,bp,np->senderind,np->channel,np->height,np->packet,np->datalen); + } + //dp->crcs[firstz] = crc32; + } } } //else printf("ignore np->datalen.%d %d (size %d - %ld)\n",np->datalen,(int32_t)(size-sizeof(*np)),size,sizeof(*np)); } diff --git a/iguana/iguana_notary.c b/iguana/iguana_notary.c index 4b3775a9c..807c240c9 100755 --- a/iguana/iguana_notary.c +++ b/iguana/iguana_notary.c @@ -22,7 +22,7 @@ #include "iguana777.h" #include "notaries.h" -int32_t dpow_datahandler(struct supernet_info *myinfo,struct dpow_info *dp,uint8_t nn_senderind,int8_t bestk,uint64_t bestmask,uint64_t recvmask,uint32_t channel,uint32_t height,uint8_t *data,int32_t datalen,bits256 nn_destutxo,uint16_t nn_destvout,bits256 nn_srcutxo,uint16_t nn_srcvout,uint8_t nn_siglens[2],uint8_t nn_sigs[2][76]); +int32_t dpow_datahandler(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,uint8_t nn_senderind,uint32_t channel,uint32_t height,uint8_t *data,int32_t datalen); uint64_t dpow_maskmin(uint64_t refmask,struct dpow_block *bp,int8_t *lastkp); #include "dpow/dpow_network.c"