Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions cmd/p2p/sensor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"slices"
"strings"
"time"

"github.com/0xPolygon/polygon-cli/p2p"
Expand Down Expand Up @@ -165,30 +166,22 @@ func removePeerMessages(counter *prometheus.CounterVec, urls []string) error {
return err
}

var family *dto.MetricFamily
for _, f := range families {
if f.GetName() == "sensor_messages" {
family = f
break
// Find all matching metric families
for _, family := range families {
// Check for any sensor_messages metric (received, sent, etc.)
if !strings.Contains(family.GetName(), "sensor_messages") {
continue
}
}

// During DNS-discovery or when the server is taking a while to discover
// peers and has yet to receive a message, the sensor_messages prometheus
// metric may not exist yet.
if family == nil {
log.Trace().Msg("Could not find sensor_messages metric family")
return nil
}
for _, metric := range family.GetMetric() {
for _, label := range metric.GetLabel() {
url := label.GetValue()
if label.GetName() != "url" || slices.Contains(urls, url) {
continue
}

for _, metric := range family.GetMetric() {
for _, label := range metric.GetLabel() {
url := label.GetValue()
if label.GetName() != "url" || slices.Contains(urls, url) {
continue
counter.DeletePartialMatch(prometheus.Labels{"url": url})
}

counter.DeletePartialMatch(prometheus.Labels{"url": url})
}
}

Expand Down
54 changes: 41 additions & 13 deletions cmd/p2p/sensor/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ type (
ShouldWriteTransactions bool
ShouldWriteTransactionEvents bool
ShouldWritePeers bool
ShouldBroadcastTx bool
ShouldBroadcastTxHashes bool
ShouldBroadcastBlocks bool
ShouldBroadcastBlockHashes bool
ShouldRunPprof bool
PprofPort uint
ShouldRunPrometheus bool
Expand All @@ -74,6 +78,9 @@ type (
RequestsCache p2p.CacheOptions
ParentsCache p2p.CacheOptions
BlocksCache p2p.CacheOptions
TxsCache p2p.CacheOptions
KnownTxsCache p2p.CacheOptions
KnownBlocksCache p2p.CacheOptions

bootnodes []*enode.Node
staticNodes []*enode.Node
Expand Down Expand Up @@ -201,22 +208,33 @@ var SensorCmd = &cobra.Command{
// Create peer connection manager for broadcasting transactions
// and managing the global blocks cache
conns := p2p.NewConns(p2p.ConnsOptions{
BlocksCache: inputSensorParams.BlocksCache,
Head: head,
BlocksCache: inputSensorParams.BlocksCache,
TxsCache: inputSensorParams.TxsCache,
KnownTxsCache: inputSensorParams.KnownTxsCache,
KnownBlocksCache: inputSensorParams.KnownBlocksCache,
Head: head,
ShouldBroadcastTx: inputSensorParams.ShouldBroadcastTx,
ShouldBroadcastTxHashes: inputSensorParams.ShouldBroadcastTxHashes,
ShouldBroadcastBlocks: inputSensorParams.ShouldBroadcastBlocks,
ShouldBroadcastBlockHashes: inputSensorParams.ShouldBroadcastBlockHashes,
})

opts := p2p.EthProtocolOptions{
Context: cmd.Context(),
Database: db,
GenesisHash: common.HexToHash(inputSensorParams.GenesisHash),
RPC: inputSensorParams.RPC,
SensorID: inputSensorParams.SensorID,
NetworkID: inputSensorParams.NetworkID,
Conns: conns,
ForkID: forkid.ID{Hash: [4]byte(inputSensorParams.ForkID)},
MsgCounter: msgCounter,
RequestsCache: inputSensorParams.RequestsCache,
ParentsCache: inputSensorParams.ParentsCache,
Context: cmd.Context(),
Database: db,
GenesisHash: common.HexToHash(inputSensorParams.GenesisHash),
RPC: inputSensorParams.RPC,
SensorID: inputSensorParams.SensorID,
NetworkID: inputSensorParams.NetworkID,
Conns: conns,
ForkID: forkid.ID{Hash: [4]byte(inputSensorParams.ForkID)},
MsgCounter: msgCounter,
RequestsCache: inputSensorParams.RequestsCache,
ParentsCache: inputSensorParams.ParentsCache,
ShouldBroadcastTx: inputSensorParams.ShouldBroadcastTx,
ShouldBroadcastTxHashes: inputSensorParams.ShouldBroadcastTxHashes,
ShouldBroadcastBlocks: inputSensorParams.ShouldBroadcastBlocks,
ShouldBroadcastBlockHashes: inputSensorParams.ShouldBroadcastBlockHashes,
}

config := ethp2p.Config{
Expand Down Expand Up @@ -460,6 +478,10 @@ will result in less chance of missing data but can significantly increase memory
f.BoolVar(&inputSensorParams.ShouldWriteTransactionEvents, "write-tx-events", true,
`write transaction events to database (this option can significantly increase CPU and memory usage)`)
f.BoolVar(&inputSensorParams.ShouldWritePeers, "write-peers", true, "write peers to database")
f.BoolVar(&inputSensorParams.ShouldBroadcastTx, "broadcast-tx", false, "broadcast full transactions to peers")
f.BoolVar(&inputSensorParams.ShouldBroadcastTxHashes, "broadcast-tx-hashes", false, "broadcast transaction hashes to peers")
f.BoolVar(&inputSensorParams.ShouldBroadcastBlocks, "broadcast-blocks", false, "broadcast full blocks to peers")
f.BoolVar(&inputSensorParams.ShouldBroadcastBlockHashes, "broadcast-block-hashes", false, "broadcast block hashes to peers")
f.BoolVar(&inputSensorParams.ShouldRunPprof, "pprof", false, "run pprof server")
f.UintVar(&inputSensorParams.PprofPort, "pprof-port", 6060, "port pprof runs on")
f.BoolVar(&inputSensorParams.ShouldRunPrometheus, "prom", true, "run Prometheus server")
Expand Down Expand Up @@ -493,4 +515,10 @@ will result in less chance of missing data but can significantly increase memory
f.DurationVar(&inputSensorParams.ParentsCache.TTL, "parents-cache-ttl", 5*time.Minute, "time to live for parent hash cache entries (0 for no expiration)")
f.IntVar(&inputSensorParams.BlocksCache.MaxSize, "max-blocks", 1024, "maximum blocks to track across all peers (0 for no limit)")
f.DurationVar(&inputSensorParams.BlocksCache.TTL, "blocks-cache-ttl", 10*time.Minute, "time to live for block cache entries (0 for no expiration)")
f.IntVar(&inputSensorParams.TxsCache.MaxSize, "max-txs", 8192, "maximum transactions to cache for serving to peers (0 for no limit)")
f.DurationVar(&inputSensorParams.TxsCache.TTL, "txs-cache-ttl", 10*time.Minute, "time to live for transaction cache entries (0 for no expiration)")
f.IntVar(&inputSensorParams.KnownTxsCache.MaxSize, "max-known-txs", 8192, "maximum transaction hashes to track per peer (0 for no limit)")
f.DurationVar(&inputSensorParams.KnownTxsCache.TTL, "known-txs-cache-ttl", 5*time.Minute, "time to live for known transaction cache entries (0 for no expiration)")
f.IntVar(&inputSensorParams.KnownBlocksCache.MaxSize, "max-known-blocks", 1024, "maximum block hashes to track per peer (0 for no limit)")
f.DurationVar(&inputSensorParams.KnownBlocksCache.TTL, "known-blocks-cache-ttl", 5*time.Minute, "time to live for known block cache entries (0 for no expiration)")
}
98 changes: 54 additions & 44 deletions doc/polycli_p2p_sensor.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,50 +91,60 @@ polycli p2p sensor amoy-nodes.json \
## Flags

```bash
--api-port uint port API server will listen on (default 8080)
--blocks-cache-ttl duration time to live for block cache entries (0 for no expiration) (default 10m0s)
-b, --bootnodes string comma separated nodes used for bootstrapping
--database string which database to persist data to, options are:
- datastore (GCP Datastore)
- json (output to stdout)
- none (no persistence) (default "none")
-d, --database-id string datastore database ID
--dial-ratio int ratio of inbound to dialed connections (dial ratio of 2 allows 1/2 of connections to be dialed, setting to 0 defaults to 3)
--discovery-dns string DNS discovery ENR tree URL
--discovery-port int UDP P2P discovery port (default 30303)
--fork-id bytesHex hex encoded fork ID (omit 0x) (default F097BC13)
--genesis-hash string genesis block hash (default "0xa9c28ce2141b56c474f1dc504bee9b01eb1bd7d1a507580d5519d4437a97de1b")
-h, --help help for sensor
--key string hex-encoded private key (cannot be set with --key-file)
-k, --key-file string private key file (cannot be set with --key)
--max-blocks int maximum blocks to track across all peers (0 for no limit) (default 1024)
-D, --max-db-concurrency int maximum number of concurrent database operations to perform (increasing this
will result in less chance of missing data but can significantly increase memory usage) (default 10000)
--max-parents int maximum parent block hashes to track per peer (0 for no limit) (default 1024)
-m, --max-peers int maximum number of peers to connect to (default 2000)
--max-requests int maximum request IDs to track per peer (0 for no limit) (default 2048)
--nat string NAT port mapping mechanism (any|none|upnp|pmp|pmp:<IP>|extip:<IP>) (default "any")
-n, --network-id uint filter discovered nodes by this network ID
--no-discovery disable P2P peer discovery
--parents-cache-ttl duration time to live for parent hash cache entries (0 for no expiration) (default 5m0s)
--port int TCP network listening port (default 30303)
--pprof run pprof server
--pprof-port uint port pprof runs on (default 6060)
-p, --project-id string GCP project ID
--prom run Prometheus server (default true)
--prom-port uint port Prometheus runs on (default 2112)
--requests-cache-ttl duration time to live for requests cache entries (0 for no expiration) (default 5m0s)
--rpc string RPC endpoint used to fetch latest block (default "https://polygon-rpc.com")
--rpc-port uint port for JSON-RPC server to receive transactions (default 8545)
-s, --sensor-id string sensor ID when writing block/tx events
--static-nodes string static nodes file
--trusted-nodes string trusted nodes file
--ttl duration time to live (default 336h0m0s)
--write-block-events write block events to database (default true)
-B, --write-blocks write blocks to database (default true)
--write-peers write peers to database (default true)
--write-tx-events write transaction events to database (this option can significantly increase CPU and memory usage) (default true)
-t, --write-txs write transactions to database (this option can significantly increase CPU and memory usage) (default true)
--api-port uint port API server will listen on (default 8080)
--blocks-cache-ttl duration time to live for block cache entries (0 for no expiration) (default 10m0s)
-b, --bootnodes string comma separated nodes used for bootstrapping
--broadcast-block-hashes broadcast block hashes to peers
--broadcast-blocks broadcast full blocks to peers
--broadcast-tx broadcast full transactions to peers
--broadcast-tx-hashes broadcast transaction hashes to peers
--database string which database to persist data to, options are:
- datastore (GCP Datastore)
- json (output to stdout)
- none (no persistence) (default "none")
-d, --database-id string datastore database ID
--dial-ratio int ratio of inbound to dialed connections (dial ratio of 2 allows 1/2 of connections to be dialed, setting to 0 defaults to 3)
--discovery-dns string DNS discovery ENR tree URL
--discovery-port int UDP P2P discovery port (default 30303)
--fork-id bytesHex hex encoded fork ID (omit 0x) (default F097BC13)
--genesis-hash string genesis block hash (default "0xa9c28ce2141b56c474f1dc504bee9b01eb1bd7d1a507580d5519d4437a97de1b")
-h, --help help for sensor
--key string hex-encoded private key (cannot be set with --key-file)
-k, --key-file string private key file (cannot be set with --key)
--known-blocks-cache-ttl duration time to live for known block cache entries (0 for no expiration) (default 5m0s)
--known-txs-cache-ttl duration time to live for known transaction cache entries (0 for no expiration) (default 5m0s)
--max-blocks int maximum blocks to track across all peers (0 for no limit) (default 1024)
-D, --max-db-concurrency int maximum number of concurrent database operations to perform (increasing this
will result in less chance of missing data but can significantly increase memory usage) (default 10000)
--max-known-blocks int maximum block hashes to track per peer (0 for no limit) (default 1024)
--max-known-txs int maximum transaction hashes to track per peer (0 for no limit) (default 8192)
--max-parents int maximum parent block hashes to track per peer (0 for no limit) (default 1024)
-m, --max-peers int maximum number of peers to connect to (default 2000)
--max-requests int maximum request IDs to track per peer (0 for no limit) (default 2048)
--max-txs int maximum transactions to cache for serving to peers (0 for no limit) (default 8192)
--nat string NAT port mapping mechanism (any|none|upnp|pmp|pmp:<IP>|extip:<IP>) (default "any")
-n, --network-id uint filter discovered nodes by this network ID
--no-discovery disable P2P peer discovery
--parents-cache-ttl duration time to live for parent hash cache entries (0 for no expiration) (default 5m0s)
--port int TCP network listening port (default 30303)
--pprof run pprof server
--pprof-port uint port pprof runs on (default 6060)
-p, --project-id string GCP project ID
--prom run Prometheus server (default true)
--prom-port uint port Prometheus runs on (default 2112)
--requests-cache-ttl duration time to live for requests cache entries (0 for no expiration) (default 5m0s)
--rpc string RPC endpoint used to fetch latest block (default "https://polygon-rpc.com")
--rpc-port uint port for JSON-RPC server to receive transactions (default 8545)
-s, --sensor-id string sensor ID when writing block/tx events
--static-nodes string static nodes file
--trusted-nodes string trusted nodes file
--ttl duration time to live (default 336h0m0s)
--txs-cache-ttl duration time to live for transaction cache entries (0 for no expiration) (default 10m0s)
--write-block-events write block events to database (default true)
-B, --write-blocks write blocks to database (default true)
--write-peers write peers to database (default true)
--write-tx-events write transaction events to database (this option can significantly increase CPU and memory usage) (default true)
-t, --write-txs write transactions to database (this option can significantly increase CPU and memory usage) (default true)
```

The command also inherits flags from parent commands.
Expand Down
Loading