Skip to content

Commit c9909c9

Browse files
authored
Merge branch 'main' into minhd-vu/requests-cache
2 parents bc09c95 + 94243b2 commit c9909c9

File tree

10 files changed

+196
-121
lines changed

10 files changed

+196
-121
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
timeout-minutes: 5
3939
steps:
4040
- uses: actions/checkout@v5
41-
- uses: crate-ci/typos@0c17dabcee8b8f1957fa917d17393a23e02e1583 # v1.36.3
41+
- uses: crate-ci/typos@80c8a4945eec0f6d464eaf9e65ed98ef085283d1 # v1.38.1
4242

4343
gen-doc:
4444
name: Check gen-doc generated files

cmd/p2p/sensor/api.go

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"slices"
8+
"time"
89

910
"github.com/0xPolygon/polygon-cli/p2p"
1011
"github.com/ethereum/go-ethereum/eth/protocols/eth"
@@ -14,17 +15,32 @@ import (
1415
"github.com/rs/zerolog/log"
1516
)
1617

17-
// nodeInfo represents information about the sensor node.
18-
type nodeInfo struct {
19-
ENR string `json:"enr"`
20-
URL string `json:"enode"`
18+
// peerData represents the metrics and connection information for a peer.
19+
// It includes both message counts (items sent/received) and packet counts
20+
// (number of p2p messages), along with connection timing information.
21+
type peerData struct {
22+
Received p2p.MessageCount `json:"received"`
23+
Sent p2p.MessageCount `json:"sent"`
24+
PacketsReceived p2p.MessageCount `json:"packets_received"`
25+
PacketsSent p2p.MessageCount `json:"packets_sent"`
26+
ConnectedAt string `json:"connected_at"`
27+
DurationSeconds float64 `json:"duration_seconds"`
2128
}
2229

23-
// handleAPI sets up the API for interacting with the sensor. The `/peers`
24-
// endpoint returns a list of all peers connected to the sensor, including the
25-
// types and counts of eth packets sent by each peer.
26-
func handleAPI(server *ethp2p.Server, counter *prometheus.CounterVec) {
27-
http.HandleFunc("/peers", func(w http.ResponseWriter, r *http.Request) {
30+
// apiData represents all sensor information including node info and peer data.
31+
type apiData struct {
32+
ENR string `json:"enr"`
33+
URL string `json:"enode"`
34+
PeerCount int `json:"peer_count"`
35+
Peers map[string]peerData `json:"peers"`
36+
}
37+
38+
// handleAPI sets up the API for interacting with the sensor. All endpoints
39+
// return information about the sensor node and all connected peers, including
40+
// the types and counts of eth packets sent and received by each peer.
41+
func handleAPI(server *ethp2p.Server, counter *prometheus.CounterVec, conns *p2p.Conns) {
42+
mux := http.NewServeMux()
43+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
2844
if r.Method != http.MethodGet {
2945
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
3046
return
@@ -33,62 +49,74 @@ func handleAPI(server *ethp2p.Server, counter *prometheus.CounterVec) {
3349
w.Header().Set("Content-Type", "application/json")
3450
w.WriteHeader(http.StatusOK)
3551

36-
peers := make(map[string]p2p.MessageCount)
52+
peers := make(map[string]peerData)
3753
for _, peer := range server.Peers() {
3854
url := peer.Node().URLv4()
39-
peers[url] = getPeerMessages(url, peer.Fullname(), counter)
40-
}
55+
peerID := peer.Node().ID().String()
56+
name := peer.Fullname()
57+
connectedAt := conns.GetPeerConnectedAt(peerID)
58+
if connectedAt.IsZero() {
59+
continue
60+
}
4161

42-
if err := json.NewEncoder(w).Encode(peers); err != nil {
43-
log.Error().Err(err).Msg("Failed to encode peers")
44-
}
45-
})
62+
msgs := peerData{
63+
Received: getPeerMessages(counter, url, name, p2p.MsgReceived, false),
64+
Sent: getPeerMessages(counter, url, name, p2p.MsgSent, false),
65+
PacketsReceived: getPeerMessages(counter, url, name, p2p.MsgReceived, true),
66+
PacketsSent: getPeerMessages(counter, url, name, p2p.MsgSent, true),
67+
ConnectedAt: connectedAt.UTC().Format(time.RFC3339),
68+
DurationSeconds: time.Since(connectedAt).Seconds(),
69+
}
4670

47-
http.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
48-
if r.Method != http.MethodGet {
49-
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
50-
return
71+
peers[url] = msgs
5172
}
5273

53-
info := nodeInfo{
54-
ENR: server.NodeInfo().ENR,
55-
URL: server.Self().URLv4(),
74+
data := apiData{
75+
ENR: server.NodeInfo().ENR,
76+
URL: server.Self().URLv4(),
77+
PeerCount: len(peers),
78+
Peers: peers,
5679
}
5780

58-
if err := json.NewEncoder(w).Encode(info); err != nil {
59-
log.Error().Err(err).Msg("Failed to encode node info")
81+
if err := json.NewEncoder(w).Encode(data); err != nil {
82+
log.Error().Err(err).Msg("Failed to encode sensor data")
6083
}
6184
})
6285

6386
addr := fmt.Sprintf(":%d", inputSensorParams.APIPort)
64-
if err := http.ListenAndServe(addr, nil); err != nil {
87+
if err := http.ListenAndServe(addr, mux); err != nil {
6588
log.Error().Err(err).Msg("Failed to start API handler")
6689
}
6790
}
6891

6992
// getPeerMessages retrieves the count of various types of eth packets sent by a
7093
// peer.
71-
func getPeerMessages(url, name string, counter *prometheus.CounterVec) p2p.MessageCount {
94+
func getPeerMessages(counter *prometheus.CounterVec, url, name string, direction p2p.Direction, isPacket bool) p2p.MessageCount {
7295
return p2p.MessageCount{
73-
BlockHeaders: getCounterValue(new(eth.BlockHeadersPacket), url, name, counter),
74-
BlockBodies: getCounterValue(new(eth.BlockBodiesPacket), url, name, counter),
75-
Blocks: getCounterValue(new(eth.NewBlockPacket), url, name, counter),
76-
BlockHashes: getCounterValue(new(eth.NewBlockHashesPacket), url, name, counter),
77-
BlockHeaderRequests: getCounterValue(new(eth.GetBlockHeadersPacket), url, name, counter),
78-
BlockBodiesRequests: getCounterValue(new(eth.GetBlockBodiesPacket), url, name, counter),
79-
Transactions: getCounterValue(new(eth.TransactionsPacket), url, name, counter) +
80-
getCounterValue(new(eth.PooledTransactionsPacket), url, name, counter),
81-
TransactionHashes: getCounterValue(new(eth.NewPooledTransactionHashesPacket), url, name, counter),
82-
TransactionRequests: getCounterValue(new(eth.GetPooledTransactionsRequest), url, name, counter),
96+
BlockHeaders: getCounterValue(new(eth.BlockHeadersPacket), counter, url, name, direction, isPacket),
97+
BlockBodies: getCounterValue(new(eth.BlockBodiesPacket), counter, url, name, direction, isPacket),
98+
Blocks: getCounterValue(new(eth.NewBlockPacket), counter, url, name, direction, isPacket),
99+
BlockHashes: getCounterValue(new(eth.NewBlockHashesPacket), counter, url, name, direction, isPacket),
100+
BlockHeaderRequests: getCounterValue(new(eth.GetBlockHeadersPacket), counter, url, name, direction, isPacket),
101+
BlockBodiesRequests: getCounterValue(new(eth.GetBlockBodiesPacket), counter, url, name, direction, isPacket),
102+
Transactions: getCounterValue(new(eth.TransactionsPacket), counter, url, name, direction, isPacket) +
103+
getCounterValue(new(eth.PooledTransactionsPacket), counter, url, name, direction, isPacket),
104+
TransactionHashes: getCounterValue(new(eth.NewPooledTransactionHashesPacket), counter, url, name, direction, isPacket),
105+
TransactionRequests: getCounterValue(new(eth.GetPooledTransactionsRequest), counter, url, name, direction, isPacket),
83106
}
84107
}
85108

86109
// getCounterValue retrieves the count of packets for a specific type from the
87110
// Prometheus counter.
88-
func getCounterValue(packet eth.Packet, url, name string, counter *prometheus.CounterVec) int64 {
111+
func getCounterValue(packet eth.Packet, counter *prometheus.CounterVec, url, name string, direction p2p.Direction, isPacket bool) int64 {
89112
metric := &dto.Metric{}
90113

91-
err := counter.WithLabelValues(packet.Name(), url, name).Write(metric)
114+
messageName := packet.Name()
115+
if isPacket {
116+
messageName += p2p.PacketSuffix
117+
}
118+
119+
err := counter.WithLabelValues(messageName, url, name, string(direction)).Write(metric)
92120
if err != nil {
93121
log.Error().Err(err).Send()
94122
return 0

cmd/p2p/sensor/rpc.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func handleRPC(conns *p2p.Conns, networkID uint64) {
4545
// Use network ID as chain ID for signature validation
4646
chainID := new(big.Int).SetUint64(networkID)
4747

48-
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
48+
mux := http.NewServeMux()
49+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
4950
if r.Method != http.MethodPost {
5051
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
5152
return
@@ -85,7 +86,7 @@ func handleRPC(conns *p2p.Conns, networkID uint64) {
8586

8687
addr := fmt.Sprintf(":%d", inputSensorParams.RPCPort)
8788
log.Info().Str("addr", addr).Msg("Starting JSON-RPC server")
88-
if err := http.ListenAndServe(addr, nil); err != nil {
89+
if err := http.ListenAndServe(addr, mux); err != nil {
8990
log.Error().Err(err).Msg("Failed to start RPC server")
9091
}
9192
}

cmd/p2p/sensor/sensor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ var SensorCmd = &cobra.Command{
188188
msgCounter := promauto.NewCounterVec(prometheus.CounterOpts{
189189
Namespace: "sensor",
190190
Name: "messages",
191-
Help: "The number and type of messages the sensor has received",
192-
}, []string{"message", "url", "name"})
191+
Help: "The number and type of messages the sensor has sent and received",
192+
}, []string{"message", "url", "name", "direction"})
193193

194194
// Create peer connection manager for broadcasting transactions
195195
conns := p2p.NewConns()
@@ -262,7 +262,7 @@ var SensorCmd = &cobra.Command{
262262
go handlePrometheus()
263263
}
264264

265-
go handleAPI(&server, msgCounter)
265+
go handleAPI(&server, msgCounter, conns)
266266

267267
// Start the RPC server for receiving transactions
268268
go handleRPC(conns, inputSensorParams.NetworkID)

go.mod

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
github.com/schollz/progressbar/v3 v3.18.0
2424
github.com/spf13/cobra v1.10.1
2525
github.com/spf13/pflag v1.0.10
26-
github.com/spf13/viper v1.20.1
26+
github.com/spf13/viper v1.21.0
2727
github.com/stretchr/testify v1.11.1
2828
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d
2929
github.com/tyler-smith/go-bip32 v1.0.0
@@ -48,7 +48,7 @@ require (
4848
github.com/docker/go-units v0.5.0 // indirect
4949
github.com/ethereum/c-kzg-4844/v2 v2.1.0 // indirect
5050
github.com/gdamore/encoding v1.0.1 // indirect
51-
github.com/go-viper/mapstructure/v2 v2.3.0 // indirect
51+
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
5252
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
5353
github.com/morikuni/aec v1.0.0 // indirect
5454
github.com/opencontainers/go-digest v1.0.0 // indirect
@@ -61,6 +61,7 @@ require (
6161
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
6262
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.37.0 // indirect
6363
go.yaml.in/yaml/v2 v2.4.3 // indirect
64+
go.yaml.in/yaml/v3 v3.0.4 // indirect
6465
gotest.tools/v3 v3.5.2 // indirect
6566
)
6667

@@ -82,8 +83,8 @@ require (
8283
github.com/consensys/gnark-crypto v0.16.0 // indirect
8384
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
8485
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
85-
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
86-
github.com/fsnotify/fsnotify v1.8.0 // indirect
86+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
87+
github.com/fsnotify/fsnotify v1.9.0 // indirect
8788
github.com/getsentry/sentry-go v0.29.1 // indirect
8889
github.com/go-ole/go-ole v1.3.0 // indirect
8990
github.com/gofrs/flock v0.12.1 // indirect
@@ -123,27 +124,26 @@ require (
123124
github.com/multiformats/go-varint v0.0.7 // indirect
124125
github.com/nsf/termbox-go v1.1.1 // indirect
125126
github.com/olekukonko/tablewriter v0.0.5 // indirect
126-
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
127+
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
127128
github.com/pkg/errors v0.9.1 // indirect
128129
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
129130
github.com/prometheus/client_golang v1.23.2
130131
github.com/prometheus/procfs v0.16.1 // indirect
131132
github.com/rivo/uniseg v0.4.7 // indirect
132133
github.com/rogpeppe/go-internal v1.13.1 // indirect
133-
github.com/sagikazarmark/locafero v0.7.0 // indirect
134+
github.com/sagikazarmark/locafero v0.11.0 // indirect
134135
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
135-
github.com/sourcegraph/conc v0.3.0 // indirect
136+
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
136137
github.com/spaolacci/murmur3 v1.1.0 // indirect
137-
github.com/spf13/afero v1.12.0 // indirect
138-
github.com/spf13/cast v1.7.1 // indirect
138+
github.com/spf13/afero v1.15.0 // indirect
139+
github.com/spf13/cast v1.10.0 // indirect
139140
github.com/subosito/gotenv v1.6.0 // indirect
140141
github.com/supranational/blst v0.3.14 // indirect
141142
github.com/tklauser/go-sysconf v0.3.14 // indirect
142143
github.com/tklauser/numcpus v0.9.0 // indirect
143144
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
144145
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
145146
go.opencensus.io v0.24.0 // indirect
146-
go.uber.org/multierr v1.11.0 // indirect
147147
golang.org/x/net v0.44.0 // indirect
148148
golang.org/x/oauth2 v0.31.0 // indirect
149149
golang.org/x/sync v0.17.0 // indirect
@@ -161,8 +161,8 @@ require (
161161
require (
162162
cloud.google.com/go/kms v1.23.1
163163
github.com/0xPolygon/cdk-contracts-tooling v0.0.1
164-
github.com/cometbft/cometbft v0.38.17
165-
github.com/docker/docker v25.0.6+incompatible
164+
github.com/cometbft/cometbft v0.38.19
165+
github.com/docker/docker v25.0.13+incompatible
166166
github.com/fatih/color v1.18.0
167167
github.com/gateway-fm/vectorized-poseidon-gold v1.0.0
168168
github.com/gdamore/tcell/v2 v2.9.0

0 commit comments

Comments
 (0)