Skip to content

Commit

Permalink
Query non-signers between start and stop times. (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
mooselumph authored Apr 10, 2024
1 parent 64b311b commit e201a18
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 24 deletions.
17 changes: 15 additions & 2 deletions disperser/dataapi/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,12 @@ const docTemplate = `{
"description": "Interval to query for operators nonsigning percentage [default: 3600]",
"name": "interval",
"in": "query"
},
{
"type": "string",
"description": "End time (UTC) to query for operators nonsigning percentage",
"name": "end",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -497,6 +503,9 @@ const docTemplate = `{
}
},
"definitions": {
"big.Int": {
"type": "object"
},
"core.SecurityParam": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -645,12 +654,16 @@ const docTemplate = `{
},
"total_stake": {
"description": "deprecated: use TotalStakePerQuorum instead. Remove when the frontend is updated.",
"type": "integer"
"allOf": [
{
"$ref": "#/definitions/big.Int"
}
]
},
"total_stake_per_quorum": {
"type": "object",
"additionalProperties": {
"type": "integer"
"$ref": "#/definitions/big.Int"
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions disperser/dataapi/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@
"description": "Interval to query for operators nonsigning percentage [default: 3600]",
"name": "interval",
"in": "query"
},
{
"type": "string",
"description": "End time (UTC) to query for operators nonsigning percentage",
"name": "end",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -493,6 +499,9 @@
}
},
"definitions": {
"big.Int": {
"type": "object"
},
"core.SecurityParam": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -641,12 +650,16 @@
},
"total_stake": {
"description": "deprecated: use TotalStakePerQuorum instead. Remove when the frontend is updated.",
"type": "integer"
"allOf": [
{
"$ref": "#/definitions/big.Int"
}
]
},
"total_stake_per_quorum": {
"type": "object",
"additionalProperties": {
"type": "integer"
"$ref": "#/definitions/big.Int"
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions disperser/dataapi/docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
definitions:
big.Int:
type: object
core.SecurityParam:
properties:
adversaryThreshold:
Expand Down Expand Up @@ -103,12 +105,13 @@ definitions:
throughput:
type: number
total_stake:
allOf:
- $ref: '#/definitions/big.Int'
description: 'deprecated: use TotalStakePerQuorum instead. Remove when the
frontend is updated.'
type: integer
total_stake_per_quorum:
additionalProperties:
type: integer
$ref: '#/definitions/big.Int'
type: object
type: object
dataapi.NonSigner:
Expand Down Expand Up @@ -453,6 +456,10 @@ paths:
in: query
name: interval
type: integer
- description: End time (UTC) to query for operators nonsigning percentage
in: query
name: end
type: string
produces:
- application/json
responses:
Expand Down
4 changes: 2 additions & 2 deletions disperser/dataapi/nonsigner_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/Layr-Labs/eigenda/core/eth"
)

func (s *server) getOperatorNonsigningRate(ctx context.Context, intervalSeconds int64) (*OperatorsNonsigningPercentage, error) {
batches, err := s.subgraphClient.QueryBatchNonSigningInfoInInterval(ctx, intervalSeconds)
func (s *server) getOperatorNonsigningRate(ctx context.Context, startTime, endTime int64) (*OperatorsNonsigningPercentage, error) {
batches, err := s.subgraphClient.QueryBatchNonSigningInfoInInterval(ctx, startTime, endTime)
if err != nil {
return nil, err
}
Expand Down
19 changes: 17 additions & 2 deletions disperser/dataapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ func (s *server) FetchNonSigners(c *gin.Context) {
// @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 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"
Expand All @@ -478,11 +479,25 @@ func (s *server) FetchOperatorsNonsigningPercentageHandler(c *gin.Context) {
}))
defer timer.ObserveDuration()

endTime := time.Now()
if c.Query("end") != "" {

var err error
endTime, err = time.Parse("2006-01-02T15:04:05Z", c.Query("end"))
if err != nil {
errorResponse(c, err)
return
}
}

interval, err := strconv.ParseInt(c.DefaultQuery("interval", "3600"), 10, 64)
if err != nil || interval == 0 {
interval = 3600
}
metric, err := s.getOperatorNonsigningRate(c.Request.Context(), interval)

startTime := endTime.Add(-time.Duration(interval) * time.Second)

metric, err := s.getOperatorNonsigningRate(c.Request.Context(), startTime.Unix(), endTime.Unix())
if err != nil {
s.metrics.IncrementFailedRequestNum("FetchOperatorsNonsigningPercentageHandler")
errorResponse(c, err)
Expand Down
9 changes: 7 additions & 2 deletions disperser/dataapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ func TestFetchMetricsThroughputHandler(t *testing.T) {
func TestFetchUnsignedBatchesHandler(t *testing.T) {
r := setUpRouter()

mockSubgraphApi.On("QueryBatchNonSigningInfo").Return(batchNonSigningInfo, nil)
stopTime := time.Now()
interval := 3600
startTime := stopTime.Add(-time.Duration(interval) * time.Second)

mockSubgraphApi.On("QueryBatchNonSigningInfo", startTime.Unix(), stopTime.Unix()).Return(batchNonSigningInfo, nil)
addr1 := gethcommon.HexToAddress("0x00000000219ab540356cbb839cbe05303d7705fa")
addr2 := gethcommon.HexToAddress("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2")
mockTx.On("BatchOperatorIDToAddress").Return([]gethcommon.Address{addr1, addr2}, nil)
Expand All @@ -338,7 +342,8 @@ func TestFetchUnsignedBatchesHandler(t *testing.T) {
r.GET("/v1/metrics/operator-nonsigning-percentage", testDataApiServer.FetchOperatorsNonsigningPercentageHandler)

w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/v1/metrics/operator-nonsigning-percentage", nil)
reqStr := fmt.Sprintf("/v1/metrics/operator-nonsigning-percentage?interval=%v&end=%s", interval, stopTime.Format("2006-01-02T15:04:05Z"))
req := httptest.NewRequest(http.MethodGet, reqStr, nil)
ctxWithDeadline, cancel := context.WithTimeout(req.Context(), 500*time.Microsecond)
defer cancel()

Expand Down
9 changes: 5 additions & 4 deletions disperser/dataapi/subgraph/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type (
QueryBatchesByBlockTimestampRange(ctx context.Context, start, end uint64) ([]*Batches, error)
QueryOperators(ctx context.Context, first int) ([]*Operator, error)
QueryBatchNonSigningOperatorIdsInInterval(ctx context.Context, intervalSeconds int64) ([]*BatchNonSigningOperatorIds, error)
QueryBatchNonSigningInfo(ctx context.Context, intervalSeconds int64) ([]*BatchNonSigningInfo, error)
QueryBatchNonSigningInfo(ctx context.Context, startTime, endTime int64) ([]*BatchNonSigningInfo, error)
QueryDeregisteredOperatorsGreaterThanBlockTimestamp(ctx context.Context, blockTimestamp uint64) ([]*Operator, error)
QueryRegisteredOperatorsGreaterThanBlockTimestamp(ctx context.Context, blockTimestamp uint64) ([]*Operator, error)
QueryOperatorInfoByOperatorIdAtBlockNumber(ctx context.Context, operatorId core.OperatorID, blockNumber uint32) (*IndexedOperatorInfo, error)
Expand Down Expand Up @@ -112,10 +112,11 @@ func (a *api) QueryOperators(ctx context.Context, first int) ([]*Operator, error
return result.OperatorRegistereds, nil
}

func (a *api) QueryBatchNonSigningInfo(ctx context.Context, intervalSeconds int64) ([]*BatchNonSigningInfo, error) {
nonSigningAfter := time.Now().Add(-time.Duration(intervalSeconds) * time.Second).Unix()
func (a *api) QueryBatchNonSigningInfo(ctx context.Context, startTime, endTime int64) ([]*BatchNonSigningInfo, error) {

variables := map[string]any{
"blockTimestamp_gt": graphql.Int(nonSigningAfter),
"blockTimestamp_gt": graphql.Int(startTime),
"blockTimestamp_lt": graphql.Int(endTime),
}
skip := 0

Expand Down
4 changes: 2 additions & 2 deletions disperser/dataapi/subgraph/mock/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (m *MockSubgraphApi) QueryOperators(ctx context.Context, first int) ([]*sub
return value, args.Error(1)
}

func (m *MockSubgraphApi) QueryBatchNonSigningInfo(ctx context.Context, first int64) ([]*subgraph.BatchNonSigningInfo, error) {
args := m.Called()
func (m *MockSubgraphApi) QueryBatchNonSigningInfo(ctx context.Context, startTime, endTime int64) ([]*subgraph.BatchNonSigningInfo, error) {
args := m.Called(startTime, endTime)

var value []*subgraph.BatchNonSigningInfo
if args.Get(0) != nil {
Expand Down
2 changes: 1 addition & 1 deletion disperser/dataapi/subgraph/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type (
BatchNonSigningOperatorIds []*BatchNonSigningOperatorIds `graphql:"batches(first: $first, skip: $skip, where: {blockTimestamp_gt: $blockTimestamp_gt})"`
}
queryBatchNonSigningInfo struct {
BatchNonSigningInfo []*BatchNonSigningInfo `graphql:"batches(first: $first, skip: $skip, where: {blockTimestamp_gt: $blockTimestamp_gt})"`
BatchNonSigningInfo []*BatchNonSigningInfo `graphql:"batches(first: $first, skip: $skip, where: {blockTimestamp_gt: $blockTimestamp_gt, blockTimestamp_lt: $blockTimestamp_lt})"`
}
queryOperatorRegisteredsGTBlockTimestamp struct {
OperatorRegistereds []*Operator `graphql:"operatorRegistereds(orderBy: blockTimestamp, where: {blockTimestamp_gt: $blockTimestamp_gt})"`
Expand Down
6 changes: 3 additions & 3 deletions disperser/dataapi/subgraph_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type (
QueryBatchesWithLimit(ctx context.Context, limit, skip int) ([]*Batch, error)
QueryOperatorsWithLimit(ctx context.Context, limit int) ([]*Operator, error)
QueryBatchNonSigningOperatorIdsInInterval(ctx context.Context, intervalSeconds int64) (map[string]int, error)
QueryBatchNonSigningInfoInInterval(ctx context.Context, intervalSeconds int64) ([]*BatchNonSigningInfo, error)
QueryBatchNonSigningInfoInInterval(ctx context.Context, startTime, endTime int64) ([]*BatchNonSigningInfo, error)
QueryOperatorQuorumEvent(ctx context.Context, startBlock, endBlock uint32) (*OperatorQuorumEvents, error)
QueryIndexedDeregisteredOperatorsForTimeWindow(ctx context.Context, days int32) (*IndexedDeregisteredOperatorState, error)
}
Expand Down Expand Up @@ -126,8 +126,8 @@ func (sc *subgraphClient) QueryOperatorsWithLimit(ctx context.Context, limit int
return operators, nil
}

func (sc *subgraphClient) QueryBatchNonSigningInfoInInterval(ctx context.Context, intervalSeconds int64) ([]*BatchNonSigningInfo, error) {
batchNonSigningInfoGql, err := sc.api.QueryBatchNonSigningInfo(ctx, intervalSeconds)
func (sc *subgraphClient) QueryBatchNonSigningInfoInInterval(ctx context.Context, startTime, endTime int64) ([]*BatchNonSigningInfo, error) {
batchNonSigningInfoGql, err := sc.api.QueryBatchNonSigningInfo(ctx, startTime, endTime)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions disperser/dataapi/subgraph_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,9 @@ func TestQueryIndexedDeregisteredOperatorsForTimeWindow(t *testing.T) {

func TestQueryBatchNonSigningInfoInInterval(t *testing.T) {
mockSubgraphApi := &subgraphmock.MockSubgraphApi{}
mockSubgraphApi.On("QueryBatchNonSigningInfo").Return(batchNonSigningInfo, nil)
mockSubgraphApi.On("QueryBatchNonSigningInfo", int64(0), int64(1)).Return(batchNonSigningInfo, nil)
subgraphClient := dataapi.NewSubgraphClient(mockSubgraphApi, logging.NewNoopLogger())
result, err := subgraphClient.QueryBatchNonSigningInfoInInterval(context.Background(), int64(1))
result, err := subgraphClient.QueryBatchNonSigningInfoInInterval(context.Background(), 0, 1)
assert.NoError(t, err)
assert.Equal(t, 2, len(result))

Expand Down

0 comments on commit e201a18

Please sign in to comment.