Skip to content

Commit

Permalink
eth/internal: add debug.peerStats for stats related to all connecte…
Browse files Browse the repository at this point in the history
…d peers (#1252)

* implement debug.peerStats for stats related to all connected peers

* add identifier for ipc

* display block number in peer stats

* eth/downloader: log error while terminating sync
  • Loading branch information
manav2401 authored May 28, 2024
1 parent 5db6fa7 commit e789b4e
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 1 deletion.
4 changes: 4 additions & 0 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,7 @@ func (b *EthAPIBackend) GetWhitelistedMilestone() (bool, uint64, common.Hash) {
func (b *EthAPIBackend) PurgeWhitelistedMilestone() {
b.eth.Downloader().ChainValidator.PurgeWhitelistedMilestone()
}

func (b *EthAPIBackend) PeerStats() interface{} {
return b.eth.handler.GetPeerStats()
}
2 changes: 1 addition & 1 deletion eth/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td, ttd *
}

defer func(start time.Time) {
log.Debug("Synchronisation terminated", "elapsed", common.PrettyDuration(time.Since(start)))
log.Debug("Synchronisation terminated", "elapsed", common.PrettyDuration(time.Since(start)), "err", err)
}(time.Now())

// Look up the sync boundaries: the common ancestor and the target block
Expand Down
37 changes: 37 additions & 0 deletions eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,40 @@ func (h *handler) enableSyncedFeatures() {
h.chain.TrieDB().SetBufferSize(pathdb.DefaultBufferSize)
}
}

// PeerStats represents a short summary of the information known about a connected
// peer. Specifically, it contains details about the head hash and total difficulty
// of a peer which makes it a bit different from the PeerInfo.
type PeerStats struct {
Enode string `json:"enode"` // Node URL
ID string `json:"id"` // Unique node identifier
Name string `json:"name"` // Name of the node, including client type, version, OS, custom data
Hash string `json:"hash"` // Head hash of the peer
Number uint64 `json:"number"` // Head number of the peer
Td uint64 `json:"td"` // Total difficulty of the peer
}

// PeerStats returns the current head height and td of all the connected peers
// along with few additional identifiers.
func (h *handler) GetPeerStats() []*PeerStats {
info := make([]*PeerStats, 0, len(h.peers.peers))

for _, peer := range h.peers.peers {
hash, td := peer.Head()
block := h.chain.GetBlockByHash(hash)
number := uint64(0)
if block != nil {
number = block.NumberU64()
}
info = append(info, &PeerStats{
Enode: peer.Node().URLv4(),
ID: peer.ID(),
Name: peer.Name(),
Hash: hash.String(),
Number: number,
Td: td.Uint64(),
})
}

return info
}
6 changes: 6 additions & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,12 @@ func (api *DebugAPI) GetTraceStack() string {
}
}

// PeerStats returns the current head height and td of all the connected peers
// along with few additional identifiers.
func (api *DebugAPI) PeerStats() interface{} {
return api.b.PeerStats()
}

// NetAPI offers network related RPC methods
type NetAPI struct {
net *p2p.Server
Expand Down
4 changes: 4 additions & 0 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,10 @@ func (b testBackend) SubscribeStateSyncEvent(ch chan<- core.StateSyncEvent) even
panic("implement me")
}

func (b testBackend) PeerStats() interface{} {
panic("implement me")
}

func (b testBackend) GetBorBlockLogs(ctx context.Context, hash common.Hash) ([]*types.Log, error) {
receipt, err := b.GetBorBlockReceipt(ctx, hash)
if err != nil || receipt == nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ type Backend interface {
PurgeWhitelistedCheckpoint()
GetWhitelistedMilestone() (bool, uint64, common.Hash)
PurgeWhitelistedMilestone()

// Networking related APIs
PeerStats() interface{}
}

func GetAPIs(apiBackend Backend) []rpc.API {
Expand Down
4 changes: 4 additions & 0 deletions internal/ethapi/transaction_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,7 @@ func (b *backendMock) GetWhitelistedMilestone() (bool, uint64, common.Hash) {
func (b *backendMock) PurgeWhitelistedCheckpoint() {}

func (b *backendMock) PurgeWhitelistedMilestone() {}

func (b backendMock) PeerStats() interface{} {
return nil
}
5 changes: 5 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,11 @@ web3._extend({
call: 'debug_getTrieFlushInterval',
params: 0
}),
new web3._extend.Method({
name: 'peerStats',
call: 'debug_peerStats',
params: 0
}),
],
properties: []
});
Expand Down
4 changes: 4 additions & 0 deletions les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,7 @@ func (b *LesApiBackend) GetWhitelistedMilestone() (bool, uint64, common.Hash) {

func (b *LesApiBackend) PurgeWhitelistedMilestone() {
}

func (b *LesApiBackend) PeerStats() interface{} {
return nil
}

0 comments on commit e789b4e

Please sign in to comment.