aboutsummaryrefslogtreecommitdiffstats
path: root/eth/backend.go
diff options
context:
space:
mode:
authorzelig <viktor.tron@gmail.com>2015-03-15 14:39:52 +0800
committerzelig <viktor.tron@gmail.com>2015-03-15 14:39:52 +0800
commit8ad0f1b8a30cc8411d6a7b26564e36f0db4c8e51 (patch)
treee32012f30158f73319f6e67d182720063f5661e3 /eth/backend.go
parente1508327342135fc43676f3778db70814ef82569 (diff)
downloadgo-tangerine-8ad0f1b8a30cc8411d6a7b26564e36f0db4c8e51.tar
go-tangerine-8ad0f1b8a30cc8411d6a7b26564e36f0db4c8e51.tar.gz
go-tangerine-8ad0f1b8a30cc8411d6a7b26564e36f0db4c8e51.tar.bz2
go-tangerine-8ad0f1b8a30cc8411d6a7b26564e36f0db4c8e51.tar.lz
go-tangerine-8ad0f1b8a30cc8411d6a7b26564e36f0db4c8e51.tar.xz
go-tangerine-8ad0f1b8a30cc8411d6a7b26564e36f0db4c8e51.tar.zst
go-tangerine-8ad0f1b8a30cc8411d6a7b26564e36f0db4c8e51.zip
eth:
- NodeInfo, PeerInfo types, PeersInfo(), NodeInfo() in eth/backend to support node admin IF - start net only if MaxPeers > 0, close extraDb in Stop() - defer s.extraDb.Close()
Diffstat (limited to 'eth/backend.go')
-rw-r--r--eth/backend.go68
1 files changed, 65 insertions, 3 deletions
diff --git a/eth/backend.go b/eth/backend.go
index 346fc43bc..1d637c367 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -219,6 +219,64 @@ func New(config *Config) (*Ethereum, error) {
return eth, nil
}
+type NodeInfo struct {
+ Name string
+ NodeUrl string
+ NodeID string
+ IP string
+ DiscPort int // UDP listening port for discovery protocol
+ TCPPort int // TCP listening port for RLPx
+ Td string
+ ListenAddr string
+}
+
+func (s *Ethereum) NodeInfo() *NodeInfo {
+ node := s.net.Self()
+
+ return &NodeInfo{
+ Name: s.Name(),
+ NodeUrl: node.String(),
+ NodeID: node.ID.String(),
+ IP: node.IP.String(),
+ DiscPort: node.DiscPort,
+ TCPPort: node.TCPPort,
+ ListenAddr: s.net.ListenAddr,
+ Td: s.ChainManager().Td().String(),
+ }
+}
+
+type PeerInfo struct {
+ ID string
+ Name string
+ Caps string
+ RemoteAddress string
+ LocalAddress string
+}
+
+func newPeerInfo(peer *p2p.Peer) *PeerInfo {
+ var caps []string
+ for _, cap := range peer.Caps() {
+ caps = append(caps, cap.String())
+ }
+ return &PeerInfo{
+ ID: peer.ID().String(),
+ Name: peer.Name(),
+ Caps: strings.Join(caps, ", "),
+ RemoteAddress: peer.RemoteAddr().String(),
+ LocalAddress: peer.LocalAddr().String(),
+ }
+}
+
+// PeersInfo returns an array of PeerInfo objects describing connected peers
+func (s *Ethereum) PeersInfo() (peersinfo []*PeerInfo) {
+ for _, peer := range s.net.Peers() {
+ if peer != nil {
+ peersinfo = append(peersinfo, newPeerInfo(peer))
+ }
+ }
+ return
+}
+
func (s *Ethereum) ResetWithGenesisBlock(gb *types.Block) {
s.chainManager.ResetWithGenesisBlock(gb)
s.pow.UpdateCache(true)
@@ -251,6 +309,7 @@ func (s *Ethereum) StateDb() ethutil.Database { return s.stateDb }
func (s *Ethereum) ExtraDb() ethutil.Database { return s.extraDb }
func (s *Ethereum) IsListening() bool { return true } // Always listening
func (s *Ethereum) PeerCount() int { return s.net.PeerCount() }
+func (s *Ethereum) PeerInfo() int { return s.net.PeerCount() }
func (s *Ethereum) Peers() []*p2p.Peer { return s.net.Peers() }
func (s *Ethereum) MaxPeers() int { return s.net.MaxPeers }
func (s *Ethereum) Version() string { return s.version }
@@ -262,9 +321,11 @@ func (s *Ethereum) Start() error {
ProtocolVersion: ProtocolVersion,
})
- err := s.net.Start()
- if err != nil {
- return err
+ if s.net.MaxPeers > 0 {
+ err := s.net.Start()
+ if err != nil {
+ return err
+ }
}
// Start services
@@ -311,6 +372,7 @@ func (s *Ethereum) Stop() {
// Close the database
defer s.blockDb.Close()
defer s.stateDb.Close()
+ defer s.extraDb.Close()
s.txSub.Unsubscribe() // quits txBroadcastLoop
s.blockSub.Unsubscribe() // quits blockBroadcastLoop