|
|
|
@ -203,55 +203,62 @@ func (node *Node) DoBeaconSyncing() { |
|
|
|
|
|
|
|
|
|
// DoSyncing keep the node in sync with other peers, willJoinConsensus means the node will try to join consensus after catch up
|
|
|
|
|
func (node *Node) DoSyncing(bc *core.BlockChain, worker *worker.Worker, willJoinConsensus bool) { |
|
|
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Duration(node.syncFreq) * time.Second) |
|
|
|
|
// TODO ek – infinite loop; add shutdown/cleanup logic
|
|
|
|
|
SyncingLoop: |
|
|
|
|
for { |
|
|
|
|
if node.stateSync == nil { |
|
|
|
|
node.stateSync = syncing.CreateStateSync(node.SelfPeer.IP, node.SelfPeer.Port, node.GetSyncID()) |
|
|
|
|
utils.Logger().Debug().Msg("[SYNC] initialized state sync") |
|
|
|
|
select { |
|
|
|
|
case <-ticker.C: |
|
|
|
|
node.doSync(bc, worker, willJoinConsensus) |
|
|
|
|
case <-node.Consensus.BlockNumLowChan: |
|
|
|
|
node.doSync(bc, worker, willJoinConsensus) |
|
|
|
|
} |
|
|
|
|
if node.stateSync.GetActivePeerNumber() < MinConnectedPeers { |
|
|
|
|
shardID := bc.ShardID() |
|
|
|
|
peers, err := node.SyncingPeerProvider.SyncingPeers(shardID) |
|
|
|
|
if err != nil { |
|
|
|
|
utils.Logger().Warn(). |
|
|
|
|
Err(err). |
|
|
|
|
Uint32("shard_id", shardID). |
|
|
|
|
Msg("cannot retrieve syncing peers") |
|
|
|
|
continue SyncingLoop |
|
|
|
|
} |
|
|
|
|
if err := node.stateSync.CreateSyncConfig(peers, false); err != nil { |
|
|
|
|
utils.Logger().Warn(). |
|
|
|
|
Err(err). |
|
|
|
|
Interface("peers", peers). |
|
|
|
|
Msg("[SYNC] create peers error") |
|
|
|
|
continue SyncingLoop |
|
|
|
|
} |
|
|
|
|
utils.Logger().Debug().Int("len", node.stateSync.GetActivePeerNumber()).Msg("[SYNC] Get Active Peers") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// doSync keep the node in sync with other peers, willJoinConsensus means the node will try to join consensus after catch up
|
|
|
|
|
func (node *Node) doSync(bc *core.BlockChain, worker *worker.Worker, willJoinConsensus bool) { |
|
|
|
|
if node.stateSync == nil { |
|
|
|
|
node.stateSync = syncing.CreateStateSync(node.SelfPeer.IP, node.SelfPeer.Port, node.GetSyncID()) |
|
|
|
|
utils.Logger().Debug().Msg("[SYNC] initialized state sync") |
|
|
|
|
} |
|
|
|
|
if node.stateSync.GetActivePeerNumber() < MinConnectedPeers { |
|
|
|
|
shardID := bc.ShardID() |
|
|
|
|
peers, err := node.SyncingPeerProvider.SyncingPeers(shardID) |
|
|
|
|
if err != nil { |
|
|
|
|
utils.Logger().Warn(). |
|
|
|
|
Err(err). |
|
|
|
|
Uint32("shard_id", shardID). |
|
|
|
|
Msg("cannot retrieve syncing peers") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
// TODO: treat fake maximum height
|
|
|
|
|
if node.stateSync.IsOutOfSync(bc) { |
|
|
|
|
node.stateMutex.Lock() |
|
|
|
|
node.State = NodeNotInSync |
|
|
|
|
node.stateMutex.Unlock() |
|
|
|
|
if willJoinConsensus { |
|
|
|
|
node.Consensus.BlocksNotSynchronized() |
|
|
|
|
} |
|
|
|
|
node.stateSync.SyncLoop(bc, worker, false, node.Consensus) |
|
|
|
|
if willJoinConsensus { |
|
|
|
|
node.stateMutex.Lock() |
|
|
|
|
node.State = NodeReadyForConsensus |
|
|
|
|
node.stateMutex.Unlock() |
|
|
|
|
node.Consensus.BlocksSynchronized() |
|
|
|
|
} |
|
|
|
|
if err := node.stateSync.CreateSyncConfig(peers, false); err != nil { |
|
|
|
|
utils.Logger().Warn(). |
|
|
|
|
Err(err). |
|
|
|
|
Interface("peers", peers). |
|
|
|
|
Msg("[SYNC] create peers error") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
utils.Logger().Debug().Int("len", node.stateSync.GetActivePeerNumber()).Msg("[SYNC] Get Active Peers") |
|
|
|
|
} |
|
|
|
|
// TODO: treat fake maximum height
|
|
|
|
|
if node.stateSync.IsOutOfSync(bc) { |
|
|
|
|
node.stateMutex.Lock() |
|
|
|
|
node.State = NodeReadyForConsensus |
|
|
|
|
node.State = NodeNotInSync |
|
|
|
|
node.stateMutex.Unlock() |
|
|
|
|
// TODO on demand syncing
|
|
|
|
|
time.Sleep(time.Duration(node.syncFreq) * time.Second) |
|
|
|
|
if willJoinConsensus { |
|
|
|
|
node.Consensus.BlocksNotSynchronized() |
|
|
|
|
} |
|
|
|
|
node.stateSync.SyncLoop(bc, worker, false, node.Consensus) |
|
|
|
|
if willJoinConsensus { |
|
|
|
|
node.stateMutex.Lock() |
|
|
|
|
node.State = NodeReadyForConsensus |
|
|
|
|
node.stateMutex.Unlock() |
|
|
|
|
node.Consensus.BlocksSynchronized() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
node.stateMutex.Lock() |
|
|
|
|
node.State = NodeReadyForConsensus |
|
|
|
|
node.stateMutex.Unlock() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// SupportBeaconSyncing sync with beacon chain for archival node in beacon chan or non-beacon node
|
|
|
|
|