MCPcopy Create free account
hub / github.com/CPChain/chain / NewProtocolManager

Function NewProtocolManager

protocols/cpc/handler.go:95–158  ·  view source on GitHub ↗

NewProtocolManager returns a new sub protocol manager. The cpchain sub protocol manages peers capable with the cpchain network.

(config *configs.ChainConfig, networkID uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb database.Database, coinbase common.Address, syncMode syncer.SyncMode)

Source from the content-addressed store, hash-verified

93// NewProtocolManager returns a new sub protocol manager. The cpchain sub protocol manages peers capable
94// with the cpchain network.
95func NewProtocolManager(config *configs.ChainConfig, networkID uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb database.Database, coinbase common.Address, syncMode syncer.SyncMode) (*ProtocolManager, error) {
96 // Create the protocol manager with the base fields
97 manager := &ProtocolManager{
98 networkID: networkID,
99 eventMux: mux,
100 txpool: txpool,
101 blockchain: blockchain,
102 chainconfig: config,
103 peers: newPeerSet(),
104 newPeerCh: make(chan *peer),
105 noMorePeers: make(chan struct{}),
106 txsyncCh: make(chan *txsync),
107 quitSync: make(chan struct{}),
108
109 engine: engine,
110 coinbase: coinbase,
111 syncMode: syncMode,
112 }
113
114 // initialize a sub-protocol for every implemented version we can handle
115 manager.SubProtocols = make([]p2p.Protocol, 0, len(ProtocolVersions))
116
117 for i, version := range ProtocolVersions {
118 // compatible; initialise the sub-protocol
119 manager.SubProtocols = append(manager.SubProtocols, p2p.Protocol{
120 Name: ProtocolName,
121 Version: version,
122 Length: ProtocolLengths[i],
123 Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
124 return manager.handlePeer(p, rw, version)
125 },
126 NodeInfo: func() interface{} {
127 return manager.NodeInfo()
128 },
129 PeerInfo: func(id discover.NodeID) interface{} {
130 if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
131 return p.Info()
132 }
133 return nil
134 },
135 })
136 }
137 if len(manager.SubProtocols) == 0 {
138 return nil, errIncompatibleConfig
139 }
140
141 manager.syncer = syncer.New(blockchain, manager.removePeer, manager.eventMux)
142
143 // fetcher specific
144 // verifies the header when insert into the chain
145 validator := func(header *types.Header, refHeader *types.Header) error {
146 return engine.VerifyHeader(blockchain, header, true, refHeader)
147 }
148 heighter := func() uint64 {
149 return blockchain.CurrentBlock().NumberU64()
150 }
151 inserter := func(blocks types.Blocks) (int, error) {
152 atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import

Callers 2

NewFunction · 0.85
newTestProtocolManagerFunction · 0.85

Calls 9

handlePeerMethod · 0.95
NodeInfoMethod · 0.95
newPeerSetFunction · 0.85
PeerMethod · 0.80
NumberU64Method · 0.80
InfoMethod · 0.65
VerifyHeaderMethod · 0.65
CurrentBlockMethod · 0.65
InsertChainMethod · 0.65

Tested by 1

newTestProtocolManagerFunction · 0.68