@ -687,7 +687,7 @@ func (ss *StateSync) RegisterNodeInfo() int {
}
}
// getMaxPeerHeight gets the maximum blockchain heights from peers
// getMaxPeerHeight gets the maximum blockchain heights from peers
func ( ss * StateSync ) getMaxPeerHeight ( ) uint64 {
func ( ss * StateSync ) getMaxPeerHeight ( isBeacon bool ) uint64 {
maxHeight := uint64 ( 0 )
maxHeight := uint64 ( 0 )
var wg sync . WaitGroup
var wg sync . WaitGroup
ss . syncConfig . ForEachPeer ( func ( peerConfig * SyncPeerConfig ) ( brk bool ) {
ss . syncConfig . ForEachPeer ( func ( peerConfig * SyncPeerConfig ) ( brk bool ) {
@ -695,7 +695,7 @@ func (ss *StateSync) getMaxPeerHeight() uint64 {
go func ( ) {
go func ( ) {
defer wg . Done ( )
defer wg . Done ( )
//debug
//debug
utils . Logger ( ) . Debug ( ) . Str ( "IP" , peerConfig . ip ) . Str ( "Port" , peerConfig . port ) . Msg ( "[Sync]getMaxPeerHeight" )
utils . Logger ( ) . Debug ( ) . Bool ( "isBeacon" , isBeacon ) . Str ( "IP" , peerConfig . ip ) . Str ( "Port" , peerConfig . port ) . Msg ( "[Sync]getMaxPeerHeight" )
response , err := peerConfig . client . GetBlockChainHeight ( )
response , err := peerConfig . client . GetBlockChainHeight ( )
if err != nil {
if err != nil {
utils . Logger ( ) . Warn ( ) . Err ( err ) . Str ( "IP" , peerConfig . ip ) . Str ( "Port" , peerConfig . port ) . Msg ( "[Sync]GetBlockChainHeight failed" )
utils . Logger ( ) . Warn ( ) . Err ( err ) . Str ( "IP" , peerConfig . ip ) . Str ( "Port" , peerConfig . port ) . Msg ( "[Sync]GetBlockChainHeight failed" )
@ -715,14 +715,14 @@ func (ss *StateSync) getMaxPeerHeight() uint64 {
// IsSameBlockchainHeight checks whether the node is out of sync from other peers
// IsSameBlockchainHeight checks whether the node is out of sync from other peers
func ( ss * StateSync ) IsSameBlockchainHeight ( bc * core . BlockChain ) ( uint64 , bool ) {
func ( ss * StateSync ) IsSameBlockchainHeight ( bc * core . BlockChain ) ( uint64 , bool ) {
otherHeight := ss . getMaxPeerHeight ( )
otherHeight := ss . getMaxPeerHeight ( false )
currentHeight := bc . CurrentBlock ( ) . NumberU64 ( )
currentHeight := bc . CurrentBlock ( ) . NumberU64 ( )
return otherHeight , currentHeight == otherHeight
return otherHeight , currentHeight == otherHeight
}
}
// IsOutOfSync checks whether the node is out of sync from other peers
// IsOutOfSync checks whether the node is out of sync from other peers
func ( ss * StateSync ) IsOutOfSync ( bc * core . BlockChain ) bool {
func ( ss * StateSync ) IsOutOfSync ( bc * core . BlockChain ) bool {
otherHeight := ss . getMaxPeerHeight ( )
otherHeight := ss . getMaxPeerHeight ( false )
currentHeight := bc . CurrentBlock ( ) . NumberU64 ( )
currentHeight := bc . CurrentBlock ( ) . NumberU64 ( )
utils . Logger ( ) . Debug ( ) .
utils . Logger ( ) . Debug ( ) .
Uint64 ( "OtherHeight" , otherHeight ) .
Uint64 ( "OtherHeight" , otherHeight ) .
@ -743,11 +743,13 @@ Loop:
for {
for {
select {
select {
case <- ticker . C :
case <- ticker . C :
otherHeight := ss . getMaxPeerHeight ( )
otherHeight := ss . getMaxPeerHeight ( isBeacon )
currentHeight := bc . CurrentBlock ( ) . NumberU64 ( )
currentHeight := bc . CurrentBlock ( ) . NumberU64 ( )
if currentHeight >= otherHeight {
if currentHeight >= otherHeight {
utils . Logger ( ) . Info ( ) . Msgf ( "[SYNC] Node is now IN SYNC! (ShardID: %d, otherHeight: %d, currentHeight: %d)" , bc . ShardID ( ) , otherHeight , currentHeight )
utils . Logger ( ) . Info ( ) . Msgf ( "[SYNC] Node is now IN SYNC! (isBeacon: %t, ShardID: %d, otherHeight: %d, currentHeight: %d)" , isBeacon , bc . ShardID ( ) , otherHeight , currentHeight )
break Loop
break Loop
} else {
utils . Logger ( ) . Debug ( ) . Msgf ( "[SYNC] Node is Not in Sync (isBeacon: %t, ShardID: %d, otherHeight: %d, currentHeight: %d)" , isBeacon , bc . ShardID ( ) , otherHeight , currentHeight )
}
}
startHash := bc . CurrentBlock ( ) . Hash ( )
startHash := bc . CurrentBlock ( ) . Hash ( )
size := uint32 ( otherHeight - currentHeight )
size := uint32 ( otherHeight - currentHeight )