Skip to content

Commit

Permalink
Get discovery table information in geth console (ethereum#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkporter authored and ashishb committed Sep 26, 2019
1 parent ae27fd3 commit cba94a1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/web3ext/web3ext.go
Expand Up @@ -206,6 +206,10 @@ web3._extend({
name: 'nodeInfo',
getter: 'admin_nodeInfo'
}),
new web3._extend.Property({
name: 'discoverTableInfo',
getter: 'admin_discoverTableInfo'
}),
new web3._extend.Property({
name: 'peers',
getter: 'admin_peers'
Expand Down
12 changes: 12 additions & 0 deletions node/api.go
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/rpc"
)
Expand Down Expand Up @@ -260,6 +261,17 @@ func (api *PrivateAdminAPI) StopWS() (bool, error) {
return true, nil
}

// DiscoverTableInfo gives the content of all buckets and ips in the p2p
// discover table
func (api *PrivateAdminAPI) DiscoverTableInfo() (*discover.TableInfo, error) {
// Make sure the server is running, fail otherwise
server := api.node.Server()
if server == nil {
return nil, ErrNodeStopped
}
return server.DiscoverTableInfo(), nil
}

// PublicAdminAPI is the collection of administrative API methods exposed over
// both secure and unsecure RPC channels.
type PublicAdminAPI struct {
Expand Down
2 changes: 2 additions & 0 deletions p2p/dial.go
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/netutil"
)
Expand Down Expand Up @@ -89,6 +90,7 @@ type discoverTable interface {
Resolve(*enode.Node) *enode.Node
LookupRandom() []*enode.Node
ReadRandomNodes([]*enode.Node) int
Info() *discover.TableInfo
}

// the dial history remembers recent dials.
Expand Down
3 changes: 3 additions & 0 deletions p2p/dial_test.go
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/p2p/netutil"
Expand Down Expand Up @@ -85,6 +86,7 @@ func (t fakeTable) Close() {}
func (t fakeTable) LookupRandom() []*enode.Node { return nil }
func (t fakeTable) Resolve(*enode.Node) *enode.Node { return nil }
func (t fakeTable) ReadRandomNodes(buf []*enode.Node) int { return copy(buf, t) }
func (t fakeTable) Info() *discover.TableInfo { return &discover.TableInfo{} }

// This test checks that dynamic dials are launched from discovery results.
func TestDialStateDynDial(t *testing.T) {
Expand Down Expand Up @@ -703,3 +705,4 @@ func (t *resolveMock) Self() *enode.Node { return new(enode.
func (t *resolveMock) Close() {}
func (t *resolveMock) LookupRandom() []*enode.Node { return nil }
func (t *resolveMock) ReadRandomNodes(buf []*enode.Node) int { return 0 }
func (t *resolveMock) Info() *discover.TableInfo { return &discover.TableInfo{} }
28 changes: 28 additions & 0 deletions p2p/discover/table.go
Expand Up @@ -83,6 +83,18 @@ type Table struct {
nodeAddedHook func(*node) // for testing
}

type bucketInfo struct {
Entries []*node `json:"entries"`
Replacements []*node `json:"replacements"`
IPs string `json:"ips"`
}

// TableInfo provides information on the discovery table
type TableInfo struct {
Buckets [nBuckets]*bucketInfo `json:"buckets"`
IPs string `json:"ips"`
}

// transport is implemented by the UDP transport.
// it is an interface so we can test without opening lots of UDP
// sockets and without generating a private key.
Expand Down Expand Up @@ -724,6 +736,22 @@ func (tab *Table) deleteInBucket(b *bucket, n *node) {
tab.removeIP(b, n.IP())
}

// Info gives information on all the buckets and IPs in the Table
func (tab *Table) Info() *TableInfo {
var buckets [nBuckets]*bucketInfo
for i := 0; i < nBuckets; i++ {
buckets[i] = &bucketInfo{
Entries: tab.buckets[i].entries,
Replacements: tab.buckets[i].replacements,
IPs: tab.buckets[i].ips.String(),
}
}
return &TableInfo{
Buckets: buckets,
IPs: tab.ips.String(),
}
}

func contains(ns []*node, id enode.ID) bool {
for _, n := range ns {
if n.ID() == id {
Expand Down
6 changes: 6 additions & 0 deletions p2p/server.go
Expand Up @@ -410,6 +410,12 @@ func (srv *Server) Self() *enode.Node {
return ln.Node()
}

// DiscoverTableInfo gets information on all the buckets in the
// discover table
func (srv *Server) DiscoverTableInfo() *discover.TableInfo {
return srv.ntab.Info()
}

// Stop terminates the server and all active peer connections.
// It blocks until all active connections have been closed.
func (srv *Server) Stop() {
Expand Down

0 comments on commit cba94a1

Please sign in to comment.