Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jianoaix committed Apr 26, 2024
1 parent bd13a5f commit 2c2df55
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/mock/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (t *MockTransactor) UpdateOperatorSocket(ctx context.Context, socket string
func (t *MockTransactor) EjectOperators(ctx context.Context, operatorsByQuorum [][]core.OperatorID) (*types.Receipt, error) {
args := t.Called()
result := args.Get(0)
return result.(*types.Receipt), args.Error(0)
return result.(*types.Receipt), args.Error(1)
}

func (t *MockTransactor) GetOperatorStakes(ctx context.Context, operatorId core.OperatorID, blockNumber uint32) (core.OperatorStakes, []core.QuorumID, error) {
Expand Down
11 changes: 7 additions & 4 deletions disperser/dataapi/ejector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
"github.com/Layr-Labs/eigensdk-go/logging"
)

// stakeShareToSLA returns the SLA for a given stake share in a quorum.
// The caller should ensure "stakeShare" is in range (0, 1].
func stakeShareToSLA(stakeShare float64) float64 {
switch {
case stakeShare > 0.15:
return 0.995
case stakeShare > 0.1:
return 0.975
return 0.98
case stakeShare > 0.05:
return 0.95
default:
Expand All @@ -27,13 +30,13 @@ func operatorPerfScore(stakeShare float64, nonsigningRate float64) float64 {
if nonsigningRate == 0 {
return 1.0
}
sla := stakeShareToSLA(stakeShare)
sla := stakeShareToSLA(stakeShare / 100.0)
perf := (1 - sla) / nonsigningRate
return perf / (1.0 + perf)
}

func computePerfScore(metric *OperatorNonsigningPercentageMetrics) float64 {
return operatorPerfScore(metric.StakePercentage, metric.Percentage/100.0)
return operatorPerfScore(metric.StakePercentage, metric.Percentage)
}

type ejector struct {
Expand All @@ -60,7 +63,7 @@ func (e *ejector) eject(ctx context.Context, nonsigningRate *OperatorsNonsigning
nonsigners := make([]*OperatorNonsigningPercentageMetrics, 0)
for _, metric := range nonsigningRate.Data {
// Collect only the nonsigners who violate the SLA.
if metric.Percentage/100.0 > 1-stakeShareToSLA(metric.StakePercentage) {
if metric.Percentage/100.0 > 1-stakeShareToSLA(metric.StakePercentage/100.0) {
nonsigners = append(nonsigners, metric)
}
}
Expand Down
42 changes: 41 additions & 1 deletion disperser/dataapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/consensys/gnark-crypto/ecc/bn254/fp"
"github.com/ethereum/go-ethereum/common"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/gin-gonic/gin"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
Expand All @@ -51,7 +52,7 @@ var (
mockSubgraphApi = &subgraphmock.MockSubgraphApi{}
subgraphClient = dataapi.NewSubgraphClient(mockSubgraphApi, mockLogger)

config = dataapi.Config{ServerMode: "test", SocketAddr: ":8080", AllowOrigins: []string{"*"}, DisperserHostname: "localhost:32007", ChurnerHostname: "localhost:32009"}
config = dataapi.Config{ServerMode: "test", SocketAddr: ":8080", AllowOrigins: []string{"*"}, DisperserHostname: "localhost:32007", ChurnerHostname: "localhost:32009", EjectionToken: "deadbeef"}

mockTx = &coremock.MockTransactor{}
opId0, _ = core.OperatorIDFromHex("e22dae12a0074f20b8fc96a0489376db34075e545ef60c4845d264a732568311")
Expand Down Expand Up @@ -323,6 +324,45 @@ func TestFetchMetricsThroughputHandler(t *testing.T) {
assert.Equal(t, float64(3.599722666666646e+07), totalThroughput)
}

func TestEjectOperatorHandler(t *testing.T) {
r := setUpRouter()

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)
mockTx.On("GetQuorumBitmapForOperatorsAtBlockNumber").Return([]*big.Int{big.NewInt(3), big.NewInt(0)}, nil)
mockTx.On("EjectOperators").Return(&types.Receipt{
GasUsed: uint64(10),
}, nil)
mockSubgraphApi.On("QueryOperatorAddedToQuorum").Return(operatorAddedToQuorum, nil)
mockSubgraphApi.On("QueryOperatorRemovedFromQuorum").Return(operatorRemovedFromQuorum, nil)

r.GET("/v1/ejector/operator", testDataApiServer.EjectOperatorsHandler)

w := httptest.NewRecorder()
reqStr := fmt.Sprintf("/v1/ejector/operator?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()
req = req.WithContext(ctxWithDeadline)
r.ServeHTTP(w, req)
assert.Equal(t, w.Code, http.StatusUnauthorized)

w2 := httptest.NewRecorder()
req2 := httptest.NewRequest(http.MethodGet, reqStr, nil)
req2.Header.Set("ejection_token", "deadbeef")
ctxWithDeadline2, cancel2 := context.WithTimeout(req2.Context(), 500*time.Microsecond)
defer cancel2()
req2 = req2.WithContext(ctxWithDeadline2)
r.ServeHTTP(w, req2)
assert.Equal(t, w2.Code, http.StatusOK)
}

func TestFetchUnsignedBatchesHandler(t *testing.T) {
r := setUpRouter()

Expand Down

0 comments on commit 2c2df55

Please sign in to comment.