@ -28,9 +28,44 @@ struct dpow_entry *dpow_notaryfind(struct supernet_info *myinfo,struct dpow_bloc
return ( 0 ) ;
return ( 0 ) ;
}
}
void dpow_utxo2entry ( struct dpow_block * bp , struct dpow_entry * ep , struct dpow_utxoentry * up )
{
int32_t i ;
for ( i = 0 ; i < DPOW_MAXRELAYS ; i + + )
bp - > notaries [ i ] . othermask | = up - > othermasks [ i ] ;
ep - > commit = up - > commit ;
ep - > height = up - > height ;
ep - > recvmask = up - > recvmask ;
ep - > bestk = up - > bestk ;
ep - > src . prev_hash = up - > srchash ;
ep - > dest . prev_hash = up - > desthash ;
ep - > src . prev_vout = up - > srcvout ;
ep - > dest . prev_vout = up - > destvout ;
}
void dpow_entry2utxo ( struct dpow_utxoentry * up , struct dpow_block * bp , struct dpow_entry * ep )
{
int32_t i ;
up - > commit = bp - > commit ;
up - > hashmsg = bp - > hashmsg ;
up - > height = bp - > height ;
up - > recvmask = bp - > recvmask ;
up - > bestk = bp - > bestk ;
for ( i = 0 ; i < DPOW_MAXRELAYS ; i + + )
up - > othermasks [ i ] = bp - > notaries [ i ] . recvmask ;
up - > commit = ep - > commit ;
up - > height = ep - > height ;
up - > recvmask = ep - > recvmask ;
up - > bestk = ep - > bestk ;
up - > srchash = ep - > src . prev_hash ;
up - > desthash = ep - > dest . prev_hash ;
up - > srcvout = ep - > src . prev_vout ;
up - > destvout = ep - > dest . prev_vout ;
}
void dpow_utxosync ( struct supernet_info * myinfo , struct dpow_block * bp , uint64_t recvmask , int32_t myind , bits256 srchash , uint32_t channel )
void dpow_utxosync ( struct supernet_info * myinfo , struct dpow_block * bp , uint64_t recvmask , int32_t myind , bits256 srchash , uint32_t channel )
{
{
uint32_t i , j , r ; int32_t len ; uint8_t utxodata [ sizeof ( struct dpow_entry ) + 2 ] ;
uint32_t i , j , r ; int32_t len ; struct dpow_utxoentry U ; uint8_t utxodata [ sizeof ( U ) + 2 ] ;
if ( ( bp - > recvmask ^ recvmask ) ! = 0 )
if ( ( bp - > recvmask ^ recvmask ) ! = 0 )
{
{
if ( ( ( 1LL < < myind ) & recvmask ) = = 0 )
if ( ( ( 1LL < < myind ) & recvmask ) = = 0 )
@ -45,30 +80,33 @@ void dpow_utxosync(struct supernet_info *myinfo,struct dpow_block *bp,uint64_t r
break ;
break ;
}
}
}
}
if ( ( len = dpow_rwutxobuf ( 1 , utxodata , & bp - > hashmsg , & bp - > notaries [ i ] ) ) > 0 )
memset ( & U , 0 , sizeof ( U ) ) ;
dpow_entry2utxo ( & U , bp , & bp - > notaries [ i ] ) ;
if ( ( len = dpow_rwutxobuf ( 1 , utxodata , & U , bp ) ) > 0 )
dpow_send ( myinfo , bp , srchash , bp - > hashmsg , channel , bp - > height , utxodata , len , bp - > utxocrcs ) ;
dpow_send ( myinfo , bp , srchash , bp - > hashmsg , channel , bp - > height , utxodata , len , bp - > utxocrcs ) ;
}
}
}
}
void dpow_sync ( struct supernet_info * myinfo , struct dpow_block * bp , uint64_t refmask , int32_t myind , bits256 srchash , uint32_t channel )
void dpow_sync ( struct supernet_info * myinfo , struct dpow_block * bp , uint64_t refmask , int32_t myind , bits256 srchash , uint32_t channel , int32_t src_or_dest )
{
{
int8_t lastk ; uint64_t mask ;
int8_t lastk ; uint64_t mask ;
mask = dpow_maskmin ( refmask , bp , & lastk ) ;
mask = dpow_maskmin ( refmask , bp , & lastk ) ;
dpow_utxosync ( myinfo , bp , mask , myind , srchash , channel ) ;
dpow_utxosync ( myinfo , bp , mask , myind , srchash , channel ) ;
if ( bp - > notaries [ myind ] . masks [ lastk ] = = 0 )
if ( bp - > notaries [ myind ] . masks [ lastk ] = = 0 )
dpow_signedtxgen ( myinfo , bp - > coin , bp , lastk , mask , myind , bp - > opret_symbol , bits256_nonz ( bp - > btc txid) = = 0 ? DPOW_SIGBTCCHANNEL : DPOW_SIGCHANNEL ) ;
dpow_signedtxgen ( myinfo , ( src_or_dest ! = 0 ) ? bp - > destcoin : bp - > src coin, bp , lastk , mask , myind , bp - > opret_symbol , bits256_nonz ( bp - > dest txid) = = 0 ? DPOW_SIGBTCCHANNEL : DPOW_SIGCHANNEL , src_or_dest ) ;
}
}
void dpow_datahandler ( struct supernet_info * myinfo , uint32_t channel , uint32_t height , uint8_t * data , int32_t datalen )
void dpow_datahandler ( struct supernet_info * myinfo , uint32_t channel , uint32_t height , uint8_t * data , int32_t datalen , int32_t src_or_dest )
{
{
bits256 hashmsg , txid , commit , srchash ; struct dpow_block * bp = 0 ; uint32_t flag = 0 ; int32_t senderind , i , myind = - 1 ; char str [ 65 ] , str2 [ 65 ] ; struct dpow_sigentry dsig ; struct dpow_entry * ep , E ;
bits256 hashmsg , txid , commit , srchash ; struct dpow_block * bp = 0 ; uint32_t flag = 0 ; int32_t senderind , i , 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 ( channel = = DPOW_UTXOCHANNEL | | channel = = DPOW_UTXOBTCCHANNEL )
if ( channel = = DPOW_UTXOCHANNEL | | channel = = DPOW_UTXOBTCCHANNEL )
{
{
memset ( & E , 0 , sizeof ( E ) ) ;
memset ( & U , 0 , sizeof ( U ) ) ;
if ( dpow_rwutxobuf ( 0 , data , & hashmsg , & E ) < 0 )
if ( dpow_rwutxobuf ( 0 , data , & U , bp ) < 0 )
return ;
return ;
if ( ( bp = dpow_heightfind ( myinfo , height , channel = = DPOW_UTXOBTCCHANNEL ) ) ! = 0 )
if ( ( bp = dpow_heightfind ( myinfo , height , channel = = DPOW_UTXOBTCCHANNEL ) ) ! = 0 )
{
{
coin = ( src_or_dest ! = 0 ) ? bp - > destcoin : bp - > srccoin ;
dpow_notaryfind ( myinfo , bp , & myind , myinfo - > DPOW . minerkey33 ) ;
dpow_notaryfind ( myinfo , bp , & myind , myinfo - > DPOW . minerkey33 ) ;
if ( myind < 0 )
if ( myind < 0 )
return ;
return ;
@ -77,20 +115,16 @@ void dpow_datahandler(struct supernet_info *myinfo,uint32_t channel,uint32_t hei
printf ( " unexpected mismatch hashmsg.%s vs %s \n " , bits256_str ( str , hashmsg ) , bits256_str ( str2 , bp - > hashmsg ) ) ;
printf ( " unexpected mismatch hashmsg.%s vs %s \n " , bits256_str ( str , hashmsg ) , bits256_str ( str2 , bp - > hashmsg ) ) ;
return ;
return ;
}
}
if ( ( ep = dpow_notaryfind ( myinfo , bp , & senderind , E . pubkey ) ) ! = 0 )
if ( ( ep = dpow_notaryfind ( myinfo , bp , & senderind , U . pubkey ) ) ! = 0 )
{
if ( bits256_nonz ( ep - > prev_hash ) = = 0 )
{
{
* ep = E ;
dpow_utxo2entry ( bp , ep , & U ) ;
bp - > recvmask | = ( 1LL < < senderind ) ;
bp - > recvmask | = ( 1LL < < senderind ) ;
}
dpow_sync ( myinfo , bp , ep - > recvmask , myind , srchash , channel , src_or_dest ) ;
ep - > recvmask = E . recvmask ;
dpow_sync ( myinfo , bp , ep - > recvmask , myind , srchash , channel ) ;
flag = 1 ;
flag = 1 ;
}
}
}
}
if ( 0 & & flag = = 0 & & bp ! = 0 )
if ( 0 & & flag = = 0 & & bp ! = 0 )
printf ( " UTXO.%d hashmsg.(%s) txid.(%s) v%d \n " , height , bits256_str ( str , hashmsg ) , bits256_str ( str2 , E . prev_hash ) , E . prev_ vout) ;
printf ( " UTXO.%d hashmsg.(%s) txid.(%s) v%d \n " , height , bits256_str ( str , hashmsg ) , bits256_str ( str2 , src_or_dest ! = 0 ? U . desthash : U . srchash ) , src_or_dest ! = 0 ? U . destvout : U . src vout) ;
}
}
else if ( channel = = DPOW_SIGCHANNEL | | channel = = DPOW_SIGBTCCHANNEL )
else if ( channel = = DPOW_SIGCHANNEL | | channel = = DPOW_SIGBTCCHANNEL )
{
{
@ -98,26 +132,28 @@ void dpow_datahandler(struct supernet_info *myinfo,uint32_t channel,uint32_t hei
return ;
return ;
if ( dsig . senderind > = 0 & & dsig . senderind < DPOW_MAXRELAYS & & ( bp = dpow_heightfind ( myinfo , height , channel = = DPOW_SIGBTCCHANNEL ) ) ! = 0 )
if ( dsig . senderind > = 0 & & dsig . senderind < DPOW_MAXRELAYS & & ( bp = dpow_heightfind ( myinfo , height , channel = = DPOW_SIGBTCCHANNEL ) ) ! = 0 )
{
{
coin = ( src_or_dest ! = 0 ) ? bp - > destcoin : bp - > srccoin ;
dpow_notaryfind ( myinfo , bp , & myind , myinfo - > DPOW . minerkey33 ) ;
dpow_notaryfind ( myinfo , bp , & myind , myinfo - > DPOW . minerkey33 ) ;
if ( myind < 0 )
if ( myind < 0 )
return ;
return ;
if ( dsig . lastk < bp - > numnotaries & & dsig . senderind < bp - > numnotaries & & ( ep = dpow_notaryfind ( myinfo , bp , & senderind , dsig . senderpub ) ) ! = 0 )
if ( dsig . lastk < bp - > numnotaries & & dsig . senderind < bp - > numnotaries & & ( ep = dpow_notaryfind ( myinfo , bp , & senderind , dsig . senderpub ) ) ! = 0 )
{
{
cp = ( src_or_dest ! = 0 ) ? & bp - > notaries [ myind ] . dest : & bp - > notaries [ myind ] . src ;
vcalc_sha256 ( 0 , commit . bytes , dsig . beacon . bytes , sizeof ( dsig . beacon ) ) ;
vcalc_sha256 ( 0 , commit . bytes , dsig . beacon . bytes , sizeof ( dsig . beacon ) ) ;
if ( memcmp ( dsig . senderpub , bp - > notaries [ dsig . senderind ] . pubkey , 33 ) = = 0 )
if ( memcmp ( dsig . senderpub , bp - > notaries [ dsig . senderind ] . pubkey , 33 ) = = 0 )
{
{
if ( ep - > masks [ dsig . lastk ] = = 0 )
if ( ep - > masks [ dsig . lastk ] = = 0 )
{
{
ep - > masks [ dsig . lastk ] = dsig . mask ;
ep - > masks [ dsig . lastk ] = dsig . mask ;
e p- > siglens [ dsig . lastk ] = dsig . siglen ;
c p- > siglens [ dsig . lastk ] = dsig . siglen ;
memcpy ( e p- > sigs [ dsig . lastk ] , dsig . sig , dsig . siglen ) ;
memcpy ( c p- > sigs [ dsig . lastk ] , dsig . sig , dsig . siglen ) ;
ep - > beacon = dsig . beacon ;
ep - > beacon = dsig . beacon ;
printf ( " <<<<<<<< %s from.%d got lastk.%d %llx siglen.%d >>>>>>>>> \n " , bp - > coin - > symbol , dsig . senderind , dsig . lastk , ( long long ) dsig . mask , dsig . siglen ) ;
printf ( " <<<<<<<< %s from.%d got lastk.%d %llx siglen.%d >>>>>>>>> \n " , coin - > symbol , dsig . senderind , dsig . lastk , ( long long ) dsig . mask , dsig . siglen ) ;
dpow_sync ( myinfo , bp , dsig . mask , myind , srchash , channel ) ;
dpow_sync ( myinfo , bp , dsig . mask , myind , srchash , channel , src_or_dest ) ;
flag = 1 ;
flag = 1 ;
}
}
} else printf ( " %s pubkey mismatch for senderind.%d %llx vs %llx \n " , bp - > coin - > symbol , dsig . senderind , * ( long long * ) dsig . senderpub , * ( long long * ) bp - > notaries [ dsig . senderind ] . pubkey ) ;
} else printf ( " %s pubkey mismatch for senderind.%d %llx vs %llx \n " , coin - > symbol , dsig . senderind , * ( long long * ) dsig . senderpub , * ( long long * ) bp - > notaries [ dsig . senderind ] . pubkey ) ;
} else printf ( " %s illegal lastk.%d or senderind.%d or senderpub.%llx \n " , bp - > coin - > symbol , dsig . lastk , dsig . senderind , * ( long long * ) dsig . senderpub ) ;
} else printf ( " %s illegal lastk.%d or senderind.%d or senderpub.%llx \n " , coin - > symbol , dsig . lastk , dsig . senderind , * ( long long * ) dsig . senderpub ) ;
} else printf ( " couldnt find senderind.%d height.%d channel.%x \n " , dsig . senderind , height , channel ) ;
} else printf ( " couldnt find senderind.%d height.%d channel.%x \n " , dsig . senderind , height , channel ) ;
if ( 0 & & bp ! = 0 )
if ( 0 & & bp ! = 0 )
printf ( " SIG.%d sender.%d lastk.%d mask.%llx siglen.%d recv.%llx \n " , height , dsig . senderind , dsig . lastk , ( long long ) dsig . mask , dsig . siglen , ( long long ) bp - > recvmask ) ;
printf ( " SIG.%d sender.%d lastk.%d mask.%llx siglen.%d recv.%llx \n " , height , dsig . senderind , dsig . lastk , ( long long ) dsig . mask , dsig . siglen , ( long long ) bp - > recvmask ) ;
@ -127,6 +163,7 @@ void dpow_datahandler(struct supernet_info *myinfo,uint32_t channel,uint32_t hei
printf ( " handle txid channel.%x \n " , channel ) ;
printf ( " handle txid channel.%x \n " , channel ) ;
if ( ( bp = dpow_heightfind ( myinfo , height , channel = = DPOW_BTCTXIDCHANNEL ) ) ! = 0 )
if ( ( bp = dpow_heightfind ( myinfo , height , channel = = DPOW_BTCTXIDCHANNEL ) ) ! = 0 )
{
{
coin = ( src_or_dest ! = 0 ) ? bp - > destcoin : bp - > srccoin ;
//printf("bp.%p datalen.%d\n",bp,datalen);
//printf("bp.%p datalen.%d\n",bp,datalen);
for ( i = 0 ; i < 32 ; i + + )
for ( i = 0 ; i < 32 ; i + + )
srchash . bytes [ i ] = data [ i ] ;
srchash . bytes [ i ] = data [ i ] ;
@ -134,7 +171,7 @@ void dpow_datahandler(struct supernet_info *myinfo,uint32_t channel,uint32_t hei
{
{
init_hexbytes_noT ( bp - > rawtx , & data [ 32 ] , datalen - 32 ) ;
init_hexbytes_noT ( bp - > rawtx , & data [ 32 ] , datalen - 32 ) ;
//printf("got bestk.%d %llx rawtx.(%s) set utxo\n",srchash.bytes[31],(long long)srchash.ulongs[1],bp->rawtx);
//printf("got bestk.%d %llx rawtx.(%s) set utxo\n",srchash.bytes[31],(long long)srchash.ulongs[1],bp->rawtx);
dpow_rawtxsign ( myinfo , bp - > coin , bp , bp - > rawtx , 0 , srchash . bytes [ 31 ] , srchash . ulongs [ 1 ] , myind , bits256_nonz ( bp - > btc txid) = = 0 ? DPOW_SIGBTCCHANNEL : DPOW_SIGCHANNEL ) ;
dpow_rawtxsign ( myinfo , bp - > coin , bp , bp - > rawtx , 0 , srchash . bytes [ 31 ] , srchash . ulongs [ 1 ] , myind , bits256_nonz ( bp - > dest txid) = = 0 ? DPOW_SIGBTCCHANNEL : DPOW_SIGCHANNEL ) ;
}
}
else */
else */
{
{
@ -143,7 +180,7 @@ void dpow_datahandler(struct supernet_info *myinfo,uint32_t channel,uint32_t hei
printf ( " signedtx.(%s) \n " , bp - > signedtx ) ;
printf ( " signedtx.(%s) \n " , bp - > signedtx ) ;
if ( bits256_cmp ( txid , srchash ) = = 0 )
if ( bits256_cmp ( txid , srchash ) = = 0 )
{
{
printf ( " verify (%s) it is properly signed! set ht.%d signedtxid to %s \n " , bp - > coin - > symbol , height , bits256_str ( str , txid ) ) ;
printf ( " verify (%s) it is properly signed! set ht.%d signedtxid to %s \n " , coin - > symbol , height , bits256_str ( str , txid ) ) ;
bp - > signedtxid = txid ;
bp - > signedtxid = txid ;
bp - > state = 0xffffffff ;
bp - > state = 0xffffffff ;
}
}
@ -158,9 +195,9 @@ void dpow_datahandler(struct supernet_info *myinfo,uint32_t channel,uint32_t hei
}
}
}
}
int32_t dpow_update ( struct supernet_info * myinfo , struct dpow_block * bp , uint32_t utxochannel , uint32_t sigchannel , uint32_t txidchannel , bits256 srchash , int32_t myind )
int32_t dpow_update ( struct supernet_info * myinfo , struct dpow_block * bp , uint32_t utxochannel , uint32_t sigchannel , uint32_t txidchannel , bits256 srchash , int32_t myind , int32_t src_or_dest )
{
{
struct dpow_entry * ep ; int32_t i , k , len , sendutxo = 1 ; bits256 hash ; uint8_t data [ sizeof ( struct dpow_entry ) + 2 ] ;
struct dpow_entry * ep ; int32_t i , k , len , sendutxo = 1 ; uint8_t data [ sizeof ( struct dpow_entry ) + 2 ] ; struct dpow_utxoentry U ;
ep = & bp - > notaries [ myind ] ;
ep = & bp - > notaries [ myind ] ;
if ( ( bp - > bestk = dpow_bestk ( bp , & bp - > bestmask ) ) > = 0 )
if ( ( bp - > bestk = dpow_bestk ( bp , & bp - > bestmask ) ) > = 0 )
{
{
@ -176,33 +213,33 @@ int32_t dpow_update(struct supernet_info *myinfo,struct dpow_block *bp,uint32_t
}
}
}
}
if ( ep - > masks [ bp - > bestk ] = = 0 )
if ( ep - > masks [ bp - > bestk ] = = 0 )
dpow_signedtxgen ( myinfo , bp - > coin , bp , bp - > bestk , bp - > bestmask , myind , bp - > opret_symbol , sigchannel ) ;
dpow_signedtxgen ( myinfo , ( src_or_dest ! = 0 ) ? bp - > destcoin : bp - > src coin, bp , bp - > bestk , bp - > bestmask , myind , bp - > opret_symbol , sigchannel , src_or_dest ) ;
else dpow_sigsend ( myinfo , bp , myind , bp - > bestk , bp - > bestmask , srchash , sigchannel ) ;
//else dpow_sigsend(myinfo,bp,myind,bp->bestk,bp->bestmask,srchash,sigchannel);
}
}
if ( sendutxo ! = 0 )
if ( sendutxo ! = 0 )
{
{
hash = srchash ;
memset ( & U , 0 , sizeof ( U ) ) ;
hash . uints [ 0 ] = rand ( ) ;
dpow_entry2utxo ( & U , bp , & bp - > notaries [ myind ] ) ;
if ( ( len = dpow_rwutxobuf ( 1 , data , & bp - > hashmsg , & bp - > notaries [ myind ] ) ) > 0 )
if ( ( len = dpow_rwutxobuf ( 1 , data , & U , bp ) ) > 0 )
dpow_send ( myinfo , bp , hash , bp - > hashmsg , utxochannel , bp - > height , data , len , bp - > utxocrcs ) ;
dpow_send ( myinfo , bp , src hash, bp - > hashmsg , utxochannel , bp - > height , data , len , bp - > utxocrcs ) ;
}
}
if ( bp - > state ! = 0xffffffff )
if ( bp - > state ! = 0xffffffff )
{
{
if ( ep - > masks [ bp - > bestk ] = = 0 )
if ( ep - > masks [ bp - > bestk ] = = 0 )
dpow_signedtxgen ( myinfo , bp - > coin , bp , bp - > bestk , bp - > bestmask , myind , bp - > opret_symbol , sigchannel ) ;
dpow_signedtxgen ( myinfo , ( src_or_dest ! = 0 ) ? bp - > destcoin : bp - > src coin, bp , bp - > bestk , bp - > bestmask , myind , bp - > opret_symbol , sigchannel , src_or_dest ) ;
else dpow_sigsend ( myinfo , bp , myind , bp - > bestk , bp - > bestmask , srchash , sigchannel ) ;
//else dpow_sigsend(myinfo,bp,myind,bp->bestk,bp->bestmask,srchash,sigchannel);
}
}
return ( bp - > state ) ;
return ( bp - > state ) ;
}
}
uint32_t dpow_statemachineiterate ( struct supernet_info * myinfo , struct dpow_info * dp , struct iguana_info * coin , struct dpow_block * bp , int32_t myind )
uint32_t dpow_statemachineiterate ( struct supernet_info * myinfo , struct dpow_info * dp , struct iguana_info * coin , struct dpow_block * bp , int32_t myind , int32_t src_or_dest )
{
{
int32_t j , match , sigmatch , len , vout , incr , haveutxo = 0 ; cJSON * addresses ; char * sendtx , * rawtx , * opret_symbol , coinaddr [ 64 ] ; uint32_t channel , sigchannel , txidchannel ; bits256 txid , srchash , zero ; uint8_t data [ 4096 ] ; int8_t lastk ; uint64_t sigsmask ;
int32_t j , match , sigmatch , len , incr ; char * opret_symbol , coinaddr [ 64 ] ; uint32_t channel , sigchannel , txidchannel ; bits256 srchash , zero ; uint8_t data [ 4096 ] ; int8_t lastk ; uint64_t sigsmask ; struct dpow_utxoentry U ; struct dpow_coinentry * cp ;
if ( bp - > numnotaries > 8 )
if ( bp - > numnotaries > 8 )
incr = sqrt ( bp - > numnotaries ) + 1 ;
incr = sqrt ( bp - > numnotaries ) + 1 ;
else incr = 1 ;
else incr = 1 ;
memset ( zero . bytes , 0 , sizeof ( zero ) ) ;
memset ( zero . bytes , 0 , sizeof ( zero ) ) ;
if ( bits256_nonz ( bp - > btc txid) = = 0 )
if ( bits256_nonz ( bp - > dest txid) = = 0 )
{
{
channel = DPOW_UTXOBTCCHANNEL ;
channel = DPOW_UTXOBTCCHANNEL ;
sigchannel = DPOW_SIGBTCCHANNEL ;
sigchannel = DPOW_SIGBTCCHANNEL ;
@ -231,7 +268,8 @@ uint32_t dpow_statemachineiterate(struct supernet_info *myinfo,struct dpow_info
if ( bp - > notaries [ j ] . masks [ bp - > bestk ] = = bp - > bestmask )
if ( bp - > notaries [ j ] . masks [ bp - > bestk ] = = bp - > bestmask )
{
{
match + + ;
match + + ;
if ( bp - > notaries [ j ] . siglens [ bp - > bestk ] > 0 )
cp = ( src_or_dest ! = 0 ) ? & bp - > notaries [ j ] . dest : & bp - > notaries [ j ] . src ;
if ( cp - > siglens [ bp - > bestk ] > 0 )
{
{
sigmatch + + ;
sigmatch + + ;
sigsmask | = ( 1LL < < j ) ;
sigsmask | = ( 1LL < < j ) ;
@ -240,27 +278,43 @@ uint32_t dpow_statemachineiterate(struct supernet_info *myinfo,struct dpow_info
}
}
}
}
if ( ( rand ( ) % 10 ) = = 0 )
if ( ( rand ( ) % 10 ) = = 0 )
printf ( " [%d] %s ht.%d FSM.%d %s BTC.%d masks.%llx best.(%d %llx) match.(%d sigs.%d) sigsmask.%llx \n " , myind , coin - > symbol , bp - > height , bp - > state , coinaddr , bits256_nonz ( bp - > btc txid) = = 0 , ( long long ) bp - > recvmask , bp - > bestk , ( long long ) bp - > bestmask , match , sigmatch , ( long long ) sigsmask ) ;
printf ( " [%d] %s ht.%d FSM.%d %s BTC.%d masks.%llx best.(%d %llx) match.(%d sigs.%d) sigsmask.%llx \n " , myind , coin - > symbol , bp - > height , bp - > state , coinaddr , bits256_nonz ( bp - > dest txid) = = 0 , ( long long ) bp - > recvmask , bp - > bestk , ( long long ) bp - > bestmask , match , sigmatch , ( long long ) sigsmask ) ;
if ( sigmatch = = DPOW_M ( bp ) )
if ( sigmatch = = DPOW_M ( bp ) )
{
{
printf ( " sigmatch.%d \n " , sigmatch ) ;
printf ( " sigmatch.%d \n " , sigmatch ) ;
dpow_sigscheck ( myinfo , bp , sigchannel , myind ) ;
dpow_sigscheck ( myinfo , bp , sigchannel , myind , src_or_dest ) ;
}
}
switch ( bp - > state )
switch ( bp - > state )
{
{
case 0 :
case 0 :
if ( ( haveutxo = dpow_haveutxo ( myinfo , coin , & txid , & vout , coinaddr ) ) ! = 0 & & bits256_nonz ( txid ) ! = 0 )
{
bp - > notaries [ myind ] . prev_hash = txid ;
bp - > notaries [ myind ] . prev_vout = vout ;
bp - > recvmask | = ( 1LL < < myind ) ;
bp - > state = 1 ;
bp - > state = 1 ;
break ;
case 1 :
dpow_lastk_mask ( bp , & lastk ) ;
memset ( & U , 0 , sizeof ( U ) ) ;
dpow_entry2utxo ( & U , bp , & bp - > notaries [ myind ] ) ;
if ( ( len = dpow_rwutxobuf ( 1 , data , & U , bp ) ) > 0 )
dpow_send ( myinfo , bp , srchash , bp - > hashmsg , channel , bp - > height , data , len , bp - > utxocrcs ) ;
bp - > recvmask | = ( 1LL < < myind ) ;
bp - > state = 2 ;
break ;
default :
dpow_update ( myinfo , bp , channel , sigchannel , txidchannel , srchash , myind , src_or_dest ) ;
break ;
}
}
if ( haveutxo < 10 & & time ( NULL ) > dp - > lastsplit + 600 )
if ( bits256_nonz ( bp - > signedtxid ) ! = 0 )
bp - > state = 0xffffffff ;
return ( bp - > state ) ;
}
int32_t dpow_checkutxo ( struct supernet_info * myinfo , struct dpow_block * bp , struct iguana_info * coin , bits256 * txidp , int32_t * voutp , char * coinaddr )
{
int32_t haveutxo , completed ; bits256 signedtxid ; cJSON * addresses ; char * rawtx , * sendtx ;
if ( ( haveutxo = dpow_haveutxo ( myinfo , coin , txidp , voutp , coinaddr ) ) < = 10 & & time ( NULL ) > myinfo - > DPOW . lastsplit + 300 )
{
{
addresses = cJSON_CreateArray ( ) ;
addresses = cJSON_CreateArray ( ) ;
jaddistr ( addresses , coinaddr ) ;
jaddistr ( addresses , coinaddr ) ;
if ( ( rawtx = iguana_utxoduplicates ( myinfo , coin , myinfo - > DPOW . minerkey33 , DPOW_UTXOSIZE , 10 , & bp - > completed , & bp - > signedtxid , 0 , addresses ) ) ! = 0 )
if ( ( rawtx = iguana_utxoduplicates ( myinfo , coin , myinfo - > DPOW . minerkey33 , DPOW_UTXOSIZE , 10 , & completed , & signedtxid , 0 , addresses ) ) ! = 0 )
{
{
if ( ( sendtx = dpow_sendrawtransaction ( myinfo , coin , rawtx ) ) ! = 0 )
if ( ( sendtx = dpow_sendrawtransaction ( myinfo , coin , rawtx ) ) ! = 0 )
{
{
@ -270,31 +324,17 @@ uint32_t dpow_statemachineiterate(struct supernet_info *myinfo,struct dpow_info
free ( rawtx ) ;
free ( rawtx ) ;
}
}
free_json ( addresses ) ;
free_json ( addresses ) ;
dp - > lastsplit = ( uint32_t ) time ( NULL ) ;
myinfo - > DPOW . lastsplit = ( uint32_t ) time ( NULL ) ;
}
}
break ;
if ( bits256_nonz ( * txidp ) = = 0 )
case 1 :
return ( - 1 ) ;
dpow_lastk_mask ( bp , & lastk ) ;
return ( haveutxo ) ;
if ( ( len = dpow_rwutxobuf ( 1 , data , & bp - > hashmsg , & bp - > notaries [ myind ] ) ) > 0 )
dpow_send ( myinfo , bp , srchash , bp - > hashmsg , channel , bp - > height , data , len , bp - > utxocrcs ) ;
bp - > recvmask | = ( 1LL < < myind ) ;
bp - > state = 2 ;
break ;
default :
dpow_update ( myinfo , bp , channel , sigchannel , txidchannel , srchash , myind ) ;
break ;
}
if ( bits256_nonz ( bp - > signedtxid ) ! = 0 )
{
bp - > state = 0xffffffff ;
}
return ( bp - > state ) ;
}
}
void dpow_statemachinestart ( void * ptr )
void dpow_statemachinestart ( void * ptr )
{
{
struct supernet_info * myinfo ; struct dpow_info * dp ; struct dpow_checkpoint checkpoint ; void * * ptrs = ptr ;
struct supernet_info * myinfo ; struct dpow_info * dp ; struct dpow_checkpoint checkpoint ; void * * ptrs = ptr ;
int32_t i , n , myind = - 1 ; struct iguana_info * src , * dest ; char str [ 65 ] , coin addr[ 64 ] ; bits256 zero ; struct dpow_block * srcbp , * destbp , * bp ; uint32_t starttime = ( uint32_t ) time ( NULL ) ;
int32_t i , n , myind = - 1 ; struct iguana_info * src , * dest ; char str [ 65 ] , str2 [ 65 ] , srcaddr [ 64 ] , destaddr [ 64 ] ; bits256 zero ; struct dpow_block * bp ; struct dpow_entry * ep = 0 ; uint32_t starttime = ( uint32_t ) time ( NULL ) ;
memset ( & zero , 0 , sizeof ( zero ) ) ;
memset ( & zero , 0 , sizeof ( zero ) ) ;
myinfo = ptrs [ 0 ] ;
myinfo = ptrs [ 0 ] ;
dp = ptrs [ 1 ] ;
dp = ptrs [ 1 ] ;
@ -303,62 +343,62 @@ void dpow_statemachinestart(void *ptr)
printf ( " statemachinestart %s->%s %s ht.%d \n " , dp - > symbol , dp - > dest , bits256_str ( str , checkpoint . blockhash . hash ) , checkpoint . blockhash . height ) ;
printf ( " statemachinestart %s->%s %s ht.%d \n " , dp - > symbol , dp - > dest , bits256_str ( str , checkpoint . blockhash . hash ) , checkpoint . blockhash . height ) ;
src = iguana_coinfind ( dp - > symbol ) ;
src = iguana_coinfind ( dp - > symbol ) ;
dest = iguana_coinfind ( dp - > dest ) ;
dest = iguana_coinfind ( dp - > dest ) ;
if ( ( dest bp= dp - > dest blocks[ checkpoint . blockhash . height ] ) = = 0 )
if ( ( bp = dp - > blocks [ checkpoint . blockhash . height ] ) = = 0 )
{
{
destbp = calloc ( 1 , sizeof ( * destbp ) ) ;
bp = calloc ( 1 , sizeof ( * bp ) ) ;
destbp - > coin = iguana_coinfind ( dp - > dest ) ;
bp - > srccoin = src ;
destbp - > opret_symbol = dp - > symbol ;
bp - > destcoin = dest ;
destbp - > bestk = - 1 ;
bp - > opret_symbol = dp - > symbol ;
dp - > destblocks [ checkpoint . blockhash . height ] = destbp ;
bp - > bestk = - 1 ;
destbp - > beacon = rand256 ( 0 ) ;
dp - > blocks [ checkpoint . blockhash . height ] = bp ;
vcalc_sha256 ( 0 , destbp - > commit . bytes , destbp - > beacon . bytes , sizeof ( destbp - > beacon ) ) ;
bp - > beacon = rand256 ( 0 ) ;
if ( ( bp = dp - > destblocks [ checkpoint . blockhash . height - 100 ] ) ! = 0 )
vcalc_sha256 ( 0 , bp - > commit . bytes , bp - > beacon . bytes , sizeof ( bp - > beacon ) ) ;
if ( dp - > blocks [ checkpoint . blockhash . height - 1000 ] ! = 0 )
{
{
printf ( " purge %s.%d \n " , dp - > dest , checkpoint . blockhash . height - 100 ) ;
printf ( " purge %s.%d \n " , dp - > dest , checkpoint . blockhash . height - 1000 ) ;
dp - > dest blocks[ checkpoint . blockhash . height - 100 ] = 0 ;
free ( dp - > blocks [ checkpoint . blockhash . height - 1000 ] ) ;
free ( bp ) ;
dp - > blocks [ checkpoint . blockhash . height - 1000 ] = 0 ;
}
}
}
}
if ( ( srcbp = dp - > srcblocks [ checkpoint . blockhash . height ] ) = = 0 )
n = ( int32_t ) ( sizeof ( Notaries ) / sizeof ( * Notaries ) ) ;
bp - > numnotaries = n ;
for ( i = 0 ; i < n ; i + + )
{
{
srcbp = calloc ( 1 , sizeof ( * srcbp ) ) ;
decode_hex ( bp - > notaries [ i ] . pubkey , 33 , Notaries [ i ] [ 1 ] ) ;
srcbp - > coin = iguana_coinfind ( dp - > symbol ) ;
if ( memcmp ( bp - > notaries [ i ] . pubkey , myinfo - > DPOW . minerkey33 , 33 ) = = 0 )
srcbp - > opret_symbol = dp - > symbol ;
srcbp - > bestk = - 1 ;
dp - > srcblocks [ checkpoint . blockhash . height ] = srcbp ;
srcbp - > beacon = destbp - > beacon ;
srcbp - > commit = destbp - > commit ;
printf ( " create srcbp[%d] \n " , checkpoint . blockhash . height ) ;
if ( ( bp = dp - > srcblocks [ checkpoint . blockhash . height - 1000 ] ) ! = 0 )
{
{
printf ( " purge %s.%d \n " , dp - > symbol , checkpoint . blockhash . height - 1000 ) ;
myind = i ;
dp - > srcblocks [ checkpoint . blockhash . height - 1000 ] = 0 ;
ep = & bp - > notaries [ myind ] ;
free ( bp ) ;
}
}
}
}
n = ( int32_t ) ( sizeof ( Notaries ) / sizeof ( * Notaries ) ) ;
if ( myind < 0 | | ep = = 0 )
srcbp - > numnotaries = destbp - > numnotaries = n ;
for ( i = 0 ; i < n ; i + + )
{
{
decode_hex ( srcbp - > notaries [ i ] . pubkey , 33 , Notaries [ i ] [ 1 ] ) ;
printf ( " statemachinestart this node %s %s is not official notary \n " , srcaddr , destaddr ) ;
decode_hex ( destbp - > notaries [ i ] . pubkey , 33 , Notaries [ i ] [ 1 ] ) ;
free ( ptr ) ;
if ( memcmp ( destbp - > notaries [ i ] . pubkey , myinfo - > DPOW . minerkey33 , 33 ) = = 0 )
return ;
myind = i ;
}
}
bitcoin_address ( coinaddr , src - > chain - > pubtype , myinfo - > DPOW . minerkey33 , 33 ) ;
bitcoin_address ( srcaddr , dest - > chain - > pubtype , myinfo - > DPOW . minerkey33 , 33 ) ;
printf ( " myaddr.%s \n " , coinaddr ) ;
bitcoin_address ( destaddr , dest - > chain - > pubtype , myinfo - > DPOW . minerkey33 , 33 ) ;
if ( myind < 0 )
printf ( " myaddr.(%s %s) \n " , srcaddr , destaddr ) ;
if ( dpow_checkutxo ( myinfo , bp , bp - > destcoin , & ep - > dest . prev_hash , & ep - > dest . prev_vout , destaddr ) < 0 )
{
{
printf ( " statemachinestart this node %s is not official notary \n " , coin addr) ;
printf ( " dont have %s %s utxo, please send funds \n " , dp - > dest , dest addr) ;
free ( ptr ) ;
free ( ptr ) ;
return ;
return ;
}
}
if ( dpow_checkutxo ( myinfo , bp , bp - > srccoin , & ep - > src . prev_hash , & ep - > src . prev_vout , srcaddr ) < 0 )
{
printf ( " dont have %s %s utxo, please send funds \n " , dp - > symbol , srcaddr ) ;
free ( ptr ) ;
return ;
}
bp - > recvmask | = ( 1LL < < myind ) ;
dp - > checkpoint = checkpoint ;
dp - > checkpoint = checkpoint ;
srcbp - > height = destbp - > height = checkpoint . blockhash . height ;
bp - > height = checkpoint . blockhash . height ;
srcbp - > timestamp = destbp - > timestamp = checkpoint . timestamp ;
bp - > timestamp = checkpoint . timestamp ;
srcbp - > hashmsg = destbp - > hashmsg = checkpoint . blockhash . hash ;
bp - > hashmsg = checkpoint . blockhash . hash ;
printf ( " DPOW statemachine checkpoint.%d %s \n " , checkpoint . blockhash . height , bits256_str ( str , checkpoint . blockhash . hash ) ) ;
printf ( " DPOW statemachine checkpoint.%d %s \n " , checkpoint . blockhash . height , bits256_str ( str , checkpoint . blockhash . hash ) ) ;
while ( time ( NULL ) < starttime + 300 & & src ! = 0 & & dest ! = 0 & & ( srcbp - > state ! = 0xffffffff | | destbp - > state ! = 0xffffffff ) )
while ( time ( NULL ) < starttime + 300 & & src ! = 0 & & dest ! = 0 & & bp - > state ! = 0xffffffff )
{
{
sleep ( 1 ) ;
sleep ( 1 ) ;
if ( dp - > checkpoint . blockhash . height > checkpoint . blockhash . height )
if ( dp - > checkpoint . blockhash . height > checkpoint . blockhash . height )
@ -366,29 +406,12 @@ void dpow_statemachinestart(void *ptr)
printf ( " abort ht.%d due to new checkpoint.%d \n " , checkpoint . blockhash . height , dp - > checkpoint . blockhash . height ) ;
printf ( " abort ht.%d due to new checkpoint.%d \n " , checkpoint . blockhash . height , dp - > checkpoint . blockhash . height ) ;
break ;
break ;
}
}
if ( dest bp- > state ! = 0xffffffff )
if ( bp - > state ! = 0xffffffff )
{
{
//printf("dp->ht.%d ht.%d DEST.%08x %s\n",dp->checkpoint.blockhash.height,checkpoint.blockhash.height,deststate,bits256_str(str,srchash.hash));
//printf("dp->ht.%d ht.%d DEST.%08x %s\n",dp->checkpoint.blockhash.height,checkpoint.blockhash.height,deststate,bits256_str(str,srchash.hash));
destbp - > state = dpow_statemachineiterate ( myinfo , dp , dest , destbp , myind ) ;
bp - > state = dpow_statemachineiterate ( myinfo , dp , dest , bp , myind , 1 ) ;
if ( destbp - > state = = 0xffffffff )
{
srcbp - > btctxid = destbp - > signedtxid ;
printf ( " SET BTCTXID.(%s) \n " , bits256_str ( str , srcbp - > btctxid ) ) ;
}
}
if ( destbp - > state = = 0xffffffff & & bits256_nonz ( srcbp - > btctxid ) ! = 0 )
{
if ( dp - > checkpoint . blockhash . height > checkpoint . blockhash . height )
{
printf ( " abort ht.%d due to new checkpoint.%d \n " , checkpoint . blockhash . height , dp - > checkpoint . blockhash . height ) ;
break ;
}
if ( srcbp - > state ! = 0xffffffff )
{
//printf("dp->ht.%d ht.%d SRC.%08x %s\n",dp->checkpoint.blockhash.height,checkpoint.blockhash.height,srcbp->state,bits256_str(str,srcbp->btctxid));
srcbp - > state = dpow_statemachineiterate ( myinfo , dp , src , srcbp , myind ) ;
}
}
}
}
}
printf ( " state machine ht.%d completed %s.%s %s.%s \n " , bp - > height , dp - > dest , bits256_str ( str , bp - > desttxid ) , dp - > symbol , bits256_str ( str2 , bp - > srctxid ) ) ;
free ( ptr ) ;
free ( ptr ) ;
}
}