@@ -34,6 +34,7 @@ import (
34
34
"github.com/ethereum/go-ethereum/logger"
35
35
"github.com/ethereum/go-ethereum/logger/glog"
36
36
"github.com/ethereum/go-ethereum/p2p"
37
+ "github.com/ethereum/go-ethereum/p2p/discover"
37
38
"github.com/ethereum/go-ethereum/pow"
38
39
"github.com/ethereum/go-ethereum/rlp"
39
40
)
@@ -55,6 +56,8 @@ type hashFetcherFn func(common.Hash) error
55
56
type blockFetcherFn func ([]common.Hash ) error
56
57
57
58
type ProtocolManager struct {
59
+ networkId int
60
+
58
61
fastSync bool
59
62
txpool txPool
60
63
blockchain * core.BlockChain
@@ -91,6 +94,7 @@ func NewProtocolManager(fastSync bool, networkId int, mux *event.TypeMux, txpool
91
94
}
92
95
// Create the protocol manager with the base fields
93
96
manager := & ProtocolManager {
97
+ networkId : networkId ,
94
98
fastSync : fastSync ,
95
99
eventMux : mux ,
96
100
txpool : txpool ,
@@ -111,14 +115,23 @@ func NewProtocolManager(fastSync bool, networkId int, mux *event.TypeMux, txpool
111
115
// Compatible; initialise the sub-protocol
112
116
version := version // Closure for the run
113
117
manager .SubProtocols = append (manager .SubProtocols , p2p.Protocol {
114
- Name : "eth" ,
118
+ Name : ProtocolName ,
115
119
Version : version ,
116
120
Length : ProtocolLengths [i ],
117
121
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 )
119
123
manager .newPeerCh <- peer
120
124
return manager .handle (peer )
121
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
+ },
122
135
})
123
136
}
124
137
if len (manager .SubProtocols ) == 0 {
@@ -188,8 +201,8 @@ func (pm *ProtocolManager) Stop() {
188
201
glog .V (logger .Info ).Infoln ("Ethereum protocol handler stopped" )
189
202
}
190
203
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 ))
193
206
}
194
207
195
208
// 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 {
199
212
200
213
// Execute the Ethereum handshake
201
214
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 {
203
216
glog .V (logger .Debug ).Infof ("%v: handshake failed: %v" , p , err )
204
217
return err
205
218
}
@@ -730,3 +743,22 @@ func (self *ProtocolManager) txBroadcastLoop() {
730
743
self .BroadcastTx (event .Tx .Hash (), event .Tx )
731
744
}
732
745
}
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