@@ -34,6 +34,7 @@ import (
3434 "github.com/ethereum/go-ethereum/logger"
3535 "github.com/ethereum/go-ethereum/logger/glog"
3636 "github.com/ethereum/go-ethereum/p2p"
37+ "github.com/ethereum/go-ethereum/p2p/discover"
3738 "github.com/ethereum/go-ethereum/pow"
3839 "github.com/ethereum/go-ethereum/rlp"
3940)
@@ -55,6 +56,8 @@ type hashFetcherFn func(common.Hash) error
5556type blockFetcherFn func ([]common.Hash ) error
5657
5758type ProtocolManager struct {
59+ networkId int
60+
5861 fastSync bool
5962 txpool txPool
6063 blockchain * core.BlockChain
@@ -91,6 +94,7 @@ func NewProtocolManager(fastSync bool, networkId int, mux *event.TypeMux, txpool
9194 }
9295 // Create the protocol manager with the base fields
9396 manager := & ProtocolManager {
97+ networkId : networkId ,
9498 fastSync : fastSync ,
9599 eventMux : mux ,
96100 txpool : txpool ,
@@ -111,14 +115,23 @@ func NewProtocolManager(fastSync bool, networkId int, mux *event.TypeMux, txpool
111115 // Compatible; initialise the sub-protocol
112116 version := version // Closure for the run
113117 manager .SubProtocols = append (manager .SubProtocols , p2p.Protocol {
114- Name : "eth" ,
118+ Name : ProtocolName ,
115119 Version : version ,
116120 Length : ProtocolLengths [i ],
117121 Run : func (p * p2p.Peer , rw p2p.MsgReadWriter ) error {
118- peer := manager .newPeer (int (version ), networkId , p , rw )
122+ peer := manager .newPeer (int (version ), p , rw )
119123 manager .newPeerCh <- peer
120124 return manager .handle (peer )
121125 },
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+ },
122135 })
123136 }
124137 if len (manager .SubProtocols ) == 0 {
@@ -188,8 +201,8 @@ func (pm *ProtocolManager) Stop() {
188201 glog .V (logger .Info ).Infoln ("Ethereum protocol handler stopped" )
189202}
190203
191- func (pm * ProtocolManager ) newPeer (pv , nv int , p * p2p.Peer , rw p2p.MsgReadWriter ) * peer {
192- return newPeer (pv , nv , p , newMeteredMsgWriter (rw ))
204+ func (pm * ProtocolManager ) newPeer (pv int , p * p2p.Peer , rw p2p.MsgReadWriter ) * peer {
205+ return newPeer (pv , p , newMeteredMsgWriter (rw ))
193206}
194207
195208// handle is the callback invoked to manage the life cycle of an eth peer. When
@@ -199,7 +212,7 @@ func (pm *ProtocolManager) handle(p *peer) error {
199212
200213 // Execute the Ethereum handshake
201214 td , head , genesis := pm .blockchain .Status ()
202- if err := p .Handshake (td , head , genesis ); err != nil {
215+ if err := p .Handshake (pm . networkId , td , head , genesis ); err != nil {
203216 glog .V (logger .Debug ).Infof ("%v: handshake failed: %v" , p , err )
204217 return err
205218 }
@@ -730,3 +743,22 @@ func (self *ProtocolManager) txBroadcastLoop() {
730743 self .BroadcastTx (event .Tx .Hash (), event .Tx )
731744 }
732745}
746+
747+ // EthNodeInfo represents a short summary of the Ethereum sub-protocol metadata known
748+ // about the host peer.
749+ type EthNodeInfo struct {
750+ Network int `json:"network"` // Ethereum network ID (0=Olympic, 1=Frontier, 2=Morden)
751+ Difficulty * big.Int `json:"difficulty"` // Total difficulty of the host's blockchain
752+ Genesis string `json:"genesis"` // SHA3 hash of the host's genesis block
753+ Head string `json:"head"` // SHA3 hash of the host's best owned block
754+ }
755+
756+ // NodeInfo retrieves some protocol metadata about the running host node.
757+ func (self * ProtocolManager ) NodeInfo () * EthNodeInfo {
758+ return & EthNodeInfo {
759+ Network : self .networkId ,
760+ Difficulty : self .blockchain .GetTd (self .blockchain .CurrentBlock ().Hash ()),
761+ Genesis : fmt .Sprintf ("%x" , self .blockchain .Genesis ().Hash ()),
762+ Head : fmt .Sprintf ("%x" , self .blockchain .CurrentBlock ().Hash ()),
763+ }
764+ }
0 commit comments