Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add node info route #358

Merged
merged 3 commits into from
Aug 28, 2024
Merged
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
35 changes: 34 additions & 1 deletion cmd/p2p/sensor/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type (
PrometheusPort uint
APIPort uint
KeyFile string
PrivateKey string
Port int
DiscoveryPort int
RPC string
Expand Down Expand Up @@ -139,6 +140,14 @@ var SensorCmd = &cobra.Command{
}
}

if len(inputSensorParams.PrivateKey) > 0 {
inputSensorParams.privateKey, err = crypto.HexToECDSA(inputSensorParams.PrivateKey)
if err != nil {
log.Error().Err(err).Msg("Failed to parse PrivateKey")
return err
}
}

inputSensorParams.nat, err = nat.Parse(inputSensorParams.NAT)
if err != nil {
log.Error().Err(err).Msg("Failed to parse NAT")
Expand Down Expand Up @@ -331,6 +340,28 @@ func handleAPI(server *ethp2p.Server, counter *prometheus.CounterVec) {
}
})

http.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}

type NodeInfo struct {
ENR string `json:"enr"`
URL string `json:"enode"`
}

info := NodeInfo{
ENR: server.NodeInfo().ENR,
URL: server.Self().URLv4(),
}

err := json.NewEncoder(w).Encode(info)
if err != nil {
log.Error().Err(err).Msg("Failed to encode node info")
}
})

addr := fmt.Sprintf(":%d", inputSensorParams.APIPort)
if err := http.ListenAndServe(addr, nil); err != nil {
log.Error().Err(err).Msg("Failed to start API handler")
Expand Down Expand Up @@ -455,7 +486,9 @@ significantly increase CPU and memory usage.`)
SensorCmd.Flags().BoolVar(&inputSensorParams.ShouldRunPrometheus, "prom", true, "Whether to run Prometheus")
SensorCmd.Flags().UintVar(&inputSensorParams.PrometheusPort, "prom-port", 2112, "Port Prometheus runs on")
SensorCmd.Flags().UintVar(&inputSensorParams.APIPort, "api-port", 8080, "Port the API server will listen on")
SensorCmd.Flags().StringVarP(&inputSensorParams.KeyFile, "key-file", "k", "", "Private key file")
SensorCmd.Flags().StringVarP(&inputSensorParams.KeyFile, "key-file", "k", "", "Private key file (cannot be set with --key)")
SensorCmd.Flags().StringVar(&inputSensorParams.PrivateKey, "key", "", "Hex-encoded private key (cannot be set with --key-file)")
SensorCmd.MarkFlagsMutuallyExclusive("key-file", "key")
SensorCmd.Flags().IntVar(&inputSensorParams.Port, "port", 30303, "TCP network listening port")
SensorCmd.Flags().IntVar(&inputSensorParams.DiscoveryPort, "discovery-port", 30303, "UDP P2P discovery port")
SensorCmd.Flags().StringVar(&inputSensorParams.RPC, "rpc", "https://polygon-rpc.com", "RPC endpoint used to fetch the latest block")
Expand Down
3 changes: 2 additions & 1 deletion doc/polycli_p2p_sensor.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ If no nodes.json file exists, it will be created.
--fork-id bytesHex The hex encoded fork id (omit the 0x) (default F097BC13)
--genesis-hash string The genesis block hash (default "0xa9c28ce2141b56c474f1dc504bee9b01eb1bd7d1a507580d5519d4437a97de1b")
-h, --help help for sensor
-k, --key-file string Private key file
--key string Hex-encoded private key (cannot be set with --key-file)
-k, --key-file string Private key file (cannot be set with --key)
-D, --max-db-concurrency int Maximum number of concurrent database operations to perform. Increasing this
will result in less chance of missing data (i.e. broken pipes) but can
significantly increase memory usage. (default 10000)
Expand Down