From 6e66fc739d1bb13b7bf306f3c44d17c612fcf75e Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Wed, 28 Aug 2024 08:41:31 -0700 Subject: [PATCH] feat: add node info route (#358) * add info route * add key flag * update docs --- cmd/p2p/sensor/sensor.go | 35 ++++++++++++++++++++++++++++++++++- doc/polycli_p2p_sensor.md | 3 ++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/cmd/p2p/sensor/sensor.go b/cmd/p2p/sensor/sensor.go index d8a63e1e..fe6e43da 100644 --- a/cmd/p2p/sensor/sensor.go +++ b/cmd/p2p/sensor/sensor.go @@ -56,6 +56,7 @@ type ( PrometheusPort uint APIPort uint KeyFile string + PrivateKey string Port int DiscoveryPort int RPC string @@ -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") @@ -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") @@ -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") diff --git a/doc/polycli_p2p_sensor.md b/doc/polycli_p2p_sensor.md index 97ea6218..9fae2611 100644 --- a/doc/polycli_p2p_sensor.md +++ b/doc/polycli_p2p_sensor.md @@ -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)