6 "github.com/sirupsen/logrus"
8 "github.com/bytom/bytom/config"
9 "github.com/bytom/bytom/consensus"
10 dbm "github.com/bytom/bytom/database/leveldb"
11 "github.com/bytom/bytom/event"
12 "github.com/bytom/bytom/netsync/chainmgr"
13 "github.com/bytom/bytom/netsync/consensusmgr"
14 "github.com/bytom/bytom/netsync/peers"
15 "github.com/bytom/bytom/p2p"
16 "github.com/bytom/bytom/protocol"
24 errVaultModeDialPeer = errors.New("can't dial peer in vault mode")
27 // ChainMgr is the interface for p2p chain message sync manager.
28 type ChainMgr interface {
34 // ConsensusMgr is the interface for consensus message sync manager.
35 type ConsensusMgr interface {
40 // Switch is the interface for p2p switch.
41 type Switch interface {
45 DialPeerWithAddress(addr *p2p.NetAddress) error
49 //SyncManager Sync Manager is responsible for the business layer information synchronization
50 type SyncManager struct {
54 consensusMgr ConsensusMgr
58 // NewSyncManager create sync manager and set switch.
59 func NewSyncManager(config *config.Config, chain *protocol.Chain, txPool *protocol.TxPool, dispatcher *event.Dispatcher, fastSyncDB dbm.DB) (*SyncManager, error) {
60 sw, err := p2p.NewSwitch(config)
64 peers := peers.NewPeerSet(sw)
66 chainManger, err := chainmgr.NewManager(config, sw, chain, txPool, dispatcher, peers, fastSyncDB)
70 consensusMgr := consensusmgr.NewManager(sw, chain, peers, dispatcher)
74 chainMgr: chainManger,
75 consensusMgr: consensusMgr,
80 // Start message sync manager service.
81 func (sm *SyncManager) Start() error {
82 if err := sm.sw.Start(); err != nil {
83 logrus.WithFields(logrus.Fields{"module": logModule, "err": err}).Error("failed start switch")
87 if err := sm.chainMgr.Start(); err != nil {
91 return sm.consensusMgr.Start()
94 // Stop message sync manager service.
95 func (sm *SyncManager) Stop() {
97 sm.consensusMgr.Stop()
98 if !sm.config.VaultMode {
104 // IsListening check if the bytomd service port is open?
105 func (sm *SyncManager) IsListening() bool {
106 if sm.config.VaultMode {
109 return sm.sw.IsListening()
113 //IsCaughtUp check wheather the peer finish the sync
114 func (sm *SyncManager) IsCaughtUp() bool {
115 return sm.chainMgr.IsCaughtUp()
118 // PeerCount count the number of connected peers.
119 func (sm *SyncManager) PeerCount() int {
120 if sm.config.VaultMode {
123 return len(sm.sw.Peers().List())
126 // GetNetwork get the type of network.
127 func (sm *SyncManager) GetNetwork() string {
128 return sm.config.ChainID
131 // BestPeer fine the peer with the highest height from the connected peers.
132 func (sm *SyncManager) BestPeer() *peers.PeerInfo {
133 bestPeer := sm.peers.BestPeer(consensus.SFFullNode)
135 return bestPeer.GetPeerInfo()
140 // DialPeerWithAddress dial the peer and establish a connection.
141 func (sm *SyncManager) DialPeerWithAddress(addr *p2p.NetAddress) error {
142 if sm.config.VaultMode {
143 return errVaultModeDialPeer
146 return sm.sw.DialPeerWithAddress(addr)
149 //GetPeerInfos return peer info of all connected peers.
150 func (sm *SyncManager) GetPeerInfos() []*peers.PeerInfo {
151 return sm.peers.GetPeerInfos()
154 //StopPeer try to stop peer by given ID
155 func (sm *SyncManager) StopPeer(peerID string) error {
156 if peer := sm.peers.GetPeer(peerID); peer == nil {
157 return errors.New("peerId not exist")
159 sm.peers.RemovePeer(peerID)