From b13f65a9ab18c01ee97090c73798d01c83c6e7c6 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 4 May 2020 12:17:14 -0700 Subject: [PATCH] Add params serving --- cmd/server/main.go | 6 ++++++ common/downloadhandler.go | 41 +++++++++++++++++++++++++++++++++++++ common/prometheusmetrics.go | 20 ++++++++++++++---- 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 common/downloadhandler.go diff --git a/cmd/server/main.go b/cmd/server/main.go index 40fe0be..08f1c27 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -47,6 +47,8 @@ func init() { promRegistry.MustRegister(metrics.TotalErrors) promRegistry.MustRegister(metrics.TotalBlocksServedConter) promRegistry.MustRegister(metrics.SendTransactionsCounter) + promRegistry.MustRegister(metrics.TotalSaplingParamsCounter) + promRegistry.MustRegister(metrics.TotalSproutParamsCounter) } // TODO stream logging @@ -255,6 +257,10 @@ func main() { log.Fatal(http.ListenAndServe(":2234", nil)) }() + // Start the download params handler + log.Infof("Starting params handler") + go common.ParamsDownloadHandler(metrics) + log.Infof("Starting gRPC server on %s", opts.bindAddr) err = server.Serve(listener) diff --git a/common/downloadhandler.go b/common/downloadhandler.go new file mode 100644 index 0000000..620de9e --- /dev/null +++ b/common/downloadhandler.go @@ -0,0 +1,41 @@ +package common + +import ( + "net/http" + "strings" +) + +var ( + metrics *PrometheusMetrics +) + +// Handle http(s) downloads for zcash params +func paramsHandler(w http.ResponseWriter, req *http.Request) { + if strings.HasSuffix(req.URL.Path, "sapling-output.params") { + metrics.TotalSaplingParamsCounter.Inc() + http.Redirect(w, req, "https://z.cash/downloads/sapling-output.params", 301) + return + } + + if strings.HasSuffix(req.URL.Path, "sapling-spend.params") { + http.Redirect(w, req, "https://z.cash/downloads/sapling-spend.params", 301) + return + } + + if strings.HasSuffix(req.URL.Path, "sprout-groth16.params") { + metrics.TotalSproutParamsCounter.Inc() + http.Redirect(w, req, "https://z.cash/downloads/sprout-groth16.params", 301) + return + } + + http.Error(w, "Not Found", 404) +} + +// ParamsDownloadHandler Listens on port 8090 for download requests for params +func ParamsDownloadHandler(prommetrics *PrometheusMetrics) { + metrics = prommetrics + http.HandleFunc("/params/", paramsHandler) + + println("Handling at params") + http.ListenAndServe(":8090", nil) +} diff --git a/common/prometheusmetrics.go b/common/prometheusmetrics.go index 62989c6..5e55dcf 100644 --- a/common/prometheusmetrics.go +++ b/common/prometheusmetrics.go @@ -4,10 +4,12 @@ import "github.com/prometheus/client_golang/prometheus" // PrometheusMetrics is a list of collected Prometheus Counters and Guages that will be exported type PrometheusMetrics struct { - LatestBlockCounter prometheus.Counter - TotalBlocksServedConter prometheus.Counter - SendTransactionsCounter prometheus.Counter - TotalErrors prometheus.Counter + LatestBlockCounter prometheus.Counter + TotalBlocksServedConter prometheus.Counter + SendTransactionsCounter prometheus.Counter + TotalErrors prometheus.Counter + TotalSaplingParamsCounter prometheus.Counter + TotalSproutParamsCounter prometheus.Counter } func GetPrometheusMetrics() *PrometheusMetrics { @@ -32,5 +34,15 @@ func GetPrometheusMetrics() *PrometheusMetrics { Help: "Total number of errors seen by lightwalletd", }) + m.TotalSaplingParamsCounter = prometheus.NewCounter(prometheus.CounterOpts{ + Name: "params_sapling_total", + Help: "Total number of params downloads for sapling params", + }) + + m.TotalSproutParamsCounter = prometheus.NewCounter(prometheus.CounterOpts{ + Name: "params_sprout_total", + Help: "Total number of params downloasd for sprout params", + }) + return m }