From 4c3d53a54fb37ad792759c7e979cc00852cc00d8 Mon Sep 17 00:00:00 2001 From: Jian Xiao <99709935+jianoaix@users.noreply.github.com> Date: Sun, 21 Apr 2024 21:46:16 -0700 Subject: [PATCH] Add a param to return only live nonsigners (#494) --- disperser/dataapi/docs/swagger.yaml | 4 ++++ disperser/dataapi/nonsigner_handler.go | 7 ++++-- disperser/dataapi/server.go | 32 +++++++++++++++++--------- disperser/dataapi/server_test.go | 4 ++-- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/disperser/dataapi/docs/swagger.yaml b/disperser/dataapi/docs/swagger.yaml index 1681cd13c1..36232b2db0 100644 --- a/disperser/dataapi/docs/swagger.yaml +++ b/disperser/dataapi/docs/swagger.yaml @@ -437,6 +437,10 @@ paths: in: query name: end type: string + - description: 'Whether return only live nonsigners [default: true]' + in: query + name: live_only + type: string produces: - application/json responses: diff --git a/disperser/dataapi/nonsigner_handler.go b/disperser/dataapi/nonsigner_handler.go index 957b858bae..a23ebf763a 100644 --- a/disperser/dataapi/nonsigner_handler.go +++ b/disperser/dataapi/nonsigner_handler.go @@ -12,7 +12,7 @@ import ( "github.com/Layr-Labs/eigenda/core/eth" ) -func (s *server) getOperatorNonsigningRate(ctx context.Context, startTime, endTime int64) (*OperatorsNonsigningPercentage, error) { +func (s *server) getOperatorNonsigningRate(ctx context.Context, startTime, endTime int64, liveOnly bool) (*OperatorsNonsigningPercentage, error) { batches, err := s.subgraphClient.QueryBatchNonSigningInfoInInterval(ctx, startTime, endTime) if err != nil { return nil, err @@ -101,6 +101,9 @@ func (s *server) getOperatorNonsigningRate(ctx context.Context, startTime, endTi if stake, ok := state.Operators[q][opID]; ok { p, _ := new(big.Int).Div(new(big.Int).Mul(stake.Stake, big.NewInt(multipler)), state.Totals[q].Stake).Float64() stakePercentage = p / multipler + } else if liveOnly { + // Operator "opID" isn't live at "endBlock", skip it. + continue } nonsignerMetric := OperatorNonsigningPercentageMetrics{ @@ -110,7 +113,7 @@ func (s *server) getOperatorNonsigningRate(ctx context.Context, startTime, endTi TotalUnsignedBatches: unsignedCount, TotalBatches: totalCount, Percentage: pf, - StakePercentage: stakePercentage, + StakePercentage: 100 * stakePercentage, } nonsignerMetrics = append(nonsignerMetrics, &nonsignerMetric) } diff --git a/disperser/dataapi/server.go b/disperser/dataapi/server.go index 405138de21..3ff4e1533e 100644 --- a/disperser/dataapi/server.go +++ b/disperser/dataapi/server.go @@ -480,16 +480,17 @@ func (s *server) FetchNonSigners(c *gin.Context) { // FetchOperatorsNonsigningPercentageHandler godoc // -// @Summary Fetch operators non signing percentage -// @Tags Metrics -// @Produce json -// @Param interval query int false "Interval to query for operators nonsigning percentage [default: 3600]" -// @Param end query string false "End time (2006-01-02T15:04:05Z) to query for operators nonsigning percentage [default: now]" -// @Success 200 {object} OperatorsNonsigningPercentage -// @Failure 400 {object} ErrorResponse "error: Bad request" -// @Failure 404 {object} ErrorResponse "error: Not found" -// @Failure 500 {object} ErrorResponse "error: Server error" -// @Router /metrics/operator-nonsigning-percentage [get] +// @Summary Fetch operators non signing percentage +// @Tags Metrics +// @Produce json +// @Param interval query int false "Interval to query for operators nonsigning percentage [default: 3600]" +// @Param end query string false "End time (2006-01-02T15:04:05Z) to query for operators nonsigning percentage [default: now]" +// @Param live_only query string false "Whether return only live nonsigners [default: true]" +// @Success 200 {object} OperatorsNonsigningPercentage +// @Failure 400 {object} ErrorResponse "error: Bad request" +// @Failure 404 {object} ErrorResponse "error: Not found" +// @Failure 500 {object} ErrorResponse "error: Server error" +// @Router /metrics/operator-nonsigning-percentage [get] func (s *server) FetchOperatorsNonsigningPercentageHandler(c *gin.Context) { timer := prometheus.NewTimer(prometheus.ObserverFunc(func(f float64) { s.metrics.ObserveLatency("FetchOperatorsNonsigningPercentageHandler", f*1000) // make milliseconds @@ -512,9 +513,18 @@ func (s *server) FetchOperatorsNonsigningPercentageHandler(c *gin.Context) { interval = 3600 } + liveOnly := "true" + if c.Query("live_only") != "" { + liveOnly = c.Query("live_only") + if liveOnly != "true" && liveOnly != "false" { + errorResponse(c, errors.New("the live_only param must be \"true\" or \"false\"")) + return + } + } + startTime := endTime.Add(-time.Duration(interval) * time.Second) - metric, err := s.getOperatorNonsigningRate(c.Request.Context(), startTime.Unix(), endTime.Unix()) + metric, err := s.getOperatorNonsigningRate(c.Request.Context(), startTime.Unix(), endTime.Unix(), liveOnly == "true") if err != nil { s.metrics.IncrementFailedRequestNum("FetchOperatorsNonsigningPercentageHandler") errorResponse(c, err) diff --git a/disperser/dataapi/server_test.go b/disperser/dataapi/server_test.go index eab2777029..ffdf45a9f2 100644 --- a/disperser/dataapi/server_test.go +++ b/disperser/dataapi/server_test.go @@ -370,7 +370,7 @@ func TestFetchUnsignedBatchesHandler(t *testing.T) { assert.Equal(t, uint8(0), responseData.QuorumId) assert.Equal(t, float64(100), responseData.Percentage) assert.Equal(t, "0xe22dae12a0074f20b8fc96a0489376db34075e545ef60c4845d264a732568311", operatorId) - assert.Equal(t, float64(0.5), responseData.StakePercentage) + assert.Equal(t, float64(50), responseData.StakePercentage) responseData = response.Data[1] operatorId = responseData.OperatorId @@ -379,7 +379,7 @@ func TestFetchUnsignedBatchesHandler(t *testing.T) { assert.Equal(t, uint8(1), responseData.QuorumId) assert.Equal(t, float64(100), responseData.Percentage) assert.Equal(t, "0xe22dae12a0074f20b8fc96a0489376db34075e545ef60c4845d264a732568311", operatorId) - assert.Equal(t, float64(0.25), responseData.StakePercentage) + assert.Equal(t, float64(25), responseData.StakePercentage) } func TestCheckBatcherHealthExpectServing(t *testing.T) {