Skip to content

Commit

Permalink
feat: add node info route (#358)
Browse files Browse the repository at this point in the history
* add info route

* add key flag

* update docs
  • Loading branch information
minhd-vu authored Aug 28, 2024
1 parent bee3ced commit 6e66fc7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
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

0 comments on commit 6e66fc7

Please sign in to comment.