[explorer][sync] make updateConsensusInformation only happens for explorer node

pull/3476/head
Jacky Wang 4 years ago committed by Leo Chen
parent 089f33d1dc
commit a7a248e323
  1. 11
      api/service/syncing/syncing.go
  2. 2
      api/service/syncing/syncing_test.go
  3. 11
      node/node_syncing.go

@ -152,13 +152,14 @@ func (sc *SyncConfig) RemovePeer(peer *SyncPeerConfig) {
}
// CreateStateSync returns the implementation of StateSyncInterface interface.
func CreateStateSync(ip string, port string, peerHash [20]byte) *StateSync {
func CreateStateSync(ip string, port string, peerHash [20]byte, isExplorer bool) *StateSync {
stateSync := &StateSync{}
stateSync.selfip = ip
stateSync.selfport = port
stateSync.selfPeerHash = peerHash
stateSync.commonBlocks = make(map[int]*types.Block)
stateSync.lastMileBlocks = []*types.Block{}
stateSync.isExplorer = isExplorer
stateSync.syncConfig = &SyncConfig{}
return stateSync
}
@ -171,6 +172,7 @@ type StateSync struct {
commonBlocks map[int]*types.Block
lastMileBlocks []*types.Block // last mile blocks to catch up with the consensus
syncConfig *SyncConfig
isExplorer bool
stateSyncTaskQueue *queue.Queue
syncMux sync.Mutex
lastMileMux sync.Mutex
@ -1025,9 +1027,10 @@ func (ss *StateSync) SyncLoop(bc *core.BlockChain, worker *worker.Worker, isBeac
if err := ss.addConsensusLastMile(bc, consensus); err != nil {
utils.Logger().Error().Err(err).Msg("[SYNC] Add consensus last mile")
}
// This logic is only needed for explorer node.
// TODO: refactor this.
consensus.UpdateConsensusInformation()
// TODO: move this to explorer handler code.
if ss.isExplorer {
consensus.UpdateConsensusInformation()
}
}
ss.purgeAllBlocksFromCache()
}

@ -95,7 +95,7 @@ func TestCompareSyncPeerConfigByblockHashes(t *testing.T) {
}
func TestCreateStateSync(t *testing.T) {
stateSync := CreateStateSync("127.0.0.1", "8000", [20]byte{})
stateSync := CreateStateSync("127.0.0.1", "8000", [20]byte{}, false)
if stateSync == nil {
t.Error("Unable to create stateSync")

@ -64,11 +64,16 @@ func (node *Node) DoSyncWithoutConsensus() {
// IsSameHeight tells whether node is at same bc height as a peer
func (node *Node) IsSameHeight() (uint64, bool) {
if node.stateSync == nil {
node.stateSync = syncing.CreateStateSync(node.SelfPeer.IP, node.SelfPeer.Port, node.GetSyncID())
node.stateSync = node.getStateSync()
}
return node.stateSync.IsSameBlockchainHeight(node.Blockchain())
}
func (node *Node) getStateSync() *syncing.StateSync {
return syncing.CreateStateSync(node.SelfPeer.IP, node.SelfPeer.Port,
node.GetSyncID(), node.NodeConfig.Role() == nodeconfig.ExplorerNode)
}
// SyncingPeerProvider is an interface for getting the peers in the given shard.
type SyncingPeerProvider interface {
SyncingPeers(shardID uint32) (peers []p2p.Peer, err error)
@ -198,7 +203,7 @@ func (node *Node) DoBeaconSyncing() {
for {
if node.beaconSync == nil {
utils.Logger().Info().Msg("initializing beacon sync")
node.beaconSync = syncing.CreateStateSync(node.SelfPeer.IP, node.SelfPeer.Port, node.GetSyncID())
node.beaconSync = node.getStateSync()
}
if node.beaconSync.GetActivePeerNumber() == 0 {
utils.Logger().Info().Msg("no peers; bootstrapping beacon sync config")
@ -298,7 +303,7 @@ func (node *Node) SupportSyncing() {
}
if node.stateSync == nil {
node.stateSync = syncing.CreateStateSync(node.SelfPeer.IP, node.SelfPeer.Port, node.GetSyncID())
node.stateSync = node.getStateSync()
utils.Logger().Debug().Msg("[SYNC] initialized state sync")
}

Loading…
Cancel
Save