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