Skip to content

Commit

Permalink
Disable stake calculation (#48) (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
jianoaix authored Nov 17, 2023
1 parent 99a4223 commit d116907
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 62 deletions.
118 changes: 57 additions & 61 deletions disperser/dataapi/metrics_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ package dataapi
import (
"context"
"errors"
"fmt"
"math/big"
"time"

"github.com/gammazero/workerpool"
)

const (
Expand All @@ -17,19 +13,19 @@ const (
)

func (s *server) getMetric(ctx context.Context, startTime int64, endTime int64, limit int) (*Metric, error) {
operators, err := s.subgraphClient.QueryOperatorsWithLimit(ctx, limit)
if err != nil {
return nil, err
}

blockNumber, err := s.transactor.GetCurrentBlockNumber(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get current block number: %w", err)
}
totalStake, err := s.calculateTotalStake(operators, blockNumber)
if err != nil {
return nil, err
}
// operators, err := s.subgraphClient.QueryOperatorsWithLimit(ctx, limit)
// if err != nil {
// return nil, err
// }

// blockNumber, err := s.transactor.GetCurrentBlockNumber(ctx)
// if err != nil {
// return nil, fmt.Errorf("failed to get current block number: %w", err)
// }
// totalStake, err := s.calculateTotalStake(operators, blockNumber)
// if err != nil {
// return nil, err
// }

result, err := s.promClient.QueryDisperserBlobSizeBytesPerSecond(ctx, time.Unix(startTime, 0), time.Unix(endTime, 0))
if err != nil {
Expand All @@ -56,7 +52,7 @@ func (s *server) getMetric(ctx context.Context, startTime int64, endTime int64,
return &Metric{
Throughput: troughput,
CostInWei: costInWei,
TotalStake: uint64(totalStake),
TotalStake: 0,
}, nil
}

Expand All @@ -73,49 +69,49 @@ func (s *server) getThroughput(ctx context.Context, start int64, end int64) ([]*
return calculateAverageThroughput(result.Values, avgThroughputWindowSize), nil
}

func (s *server) calculateTotalStake(operators []*Operator, blockNumber uint32) (int64, error) {
var (
totalStakeByOperatorChan = make(chan *big.Int, len(operators))
pool = workerpool.New(maxWorkersGetOperatorState)
)

s.logger.Debug("Number of operators to calculate stake:", "numOperators", len(operators), "blockNumber", blockNumber)
for _, o := range operators {
operatorId, err := ConvertHexadecimalToBytes(o.OperatorId)
if err != nil {
s.logger.Error("Failed to convert operator id to hex string: ", "operatorId", operatorId, "err", err)
return 0, err
}

pool.Submit(func() {
operatorState, err := s.chainState.GetOperatorStateByOperator(context.Background(), uint(blockNumber), operatorId)
if err != nil {
s.logger.Error("Failed to get operator state: ", "operatorId", operatorId, "blockNumber", blockNumber, "err", err)
totalStakeByOperatorChan <- big.NewInt(-1)
return
}
totalStake := big.NewInt(0)
s.logger.Debug("Operator state:", "operatorId", operatorId, "num quorums", len(operatorState.Totals))
for quorumId, total := range operatorState.Totals {
s.logger.Debug("Operator stake:", "operatorId", operatorId, "quorum", quorumId, "stake", (*total.Stake).Int64())
totalStake.Add(totalStake, total.Stake)
}
totalStakeByOperatorChan <- totalStake
})
}

pool.StopWait()
close(totalStakeByOperatorChan)

totalStake := big.NewInt(0)
for total := range totalStakeByOperatorChan {
if total.Int64() == -1 {
return 0, errors.New("error getting operator state")
}
totalStake.Add(totalStake, total)
}
return totalStake.Int64(), nil
}
// func (s *server) calculateTotalStake(operators []*Operator, blockNumber uint32) (int64, error) {
// var (
// totalStakeByOperatorChan = make(chan *big.Int, len(operators))
// pool = workerpool.New(maxWorkersGetOperatorState)
// )
//
// s.logger.Debug("Number of operators to calculate stake:", "numOperators", len(operators), "blockNumber", blockNumber)
// for _, o := range operators {
// operatorId, err := ConvertHexadecimalToBytes(o.OperatorId)
// if err != nil {
// s.logger.Error("Failed to convert operator id to hex string: ", "operatorId", operatorId, "err", err)
// return 0, err
// }
//
// pool.Submit(func() {
// operatorState, err := s.chainState.GetOperatorStateByOperator(context.Background(), uint(blockNumber), operatorId)
// if err != nil {
// s.logger.Error("Failed to get operator state: ", "operatorId", operatorId, "blockNumber", blockNumber, "err", err)
// totalStakeByOperatorChan <- big.NewInt(-1)
// return
// }
// totalStake := big.NewInt(0)
// s.logger.Debug("Operator state:", "operatorId", operatorId, "num quorums", len(operatorState.Totals))
// for quorumId, total := range operatorState.Totals {
// s.logger.Debug("Operator stake:", "operatorId", operatorId, "quorum", quorumId, "stake", (*total.Stake).Int64())
// totalStake.Add(totalStake, total.Stake)
// }
// totalStakeByOperatorChan <- totalStake
// })
// }
//
// pool.StopWait()
// close(totalStakeByOperatorChan)
//
// totalStake := big.NewInt(0)
// for total := range totalStakeByOperatorChan {
// if total.Int64() == -1 {
// return 0, errors.New("error getting operator state")
// }
// totalStake.Add(totalStake, total)
// }
// return totalStake.Int64(), nil
// }

func (s *server) calculateTotalCostGasUsedInWei(ctx context.Context) (uint64, error) {
batches, err := s.subgraphClient.QueryBatchesWithLimit(ctx, 1, 0)
Expand Down
2 changes: 1 addition & 1 deletion disperser/dataapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestFetchMetricsHandler(t *testing.T) {
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, 16555.555555555555, response.Throughput)
assert.Equal(t, uint64(85144853442), response.CostInWei)
assert.Equal(t, uint64(6), response.TotalStake)
// assert.Equal(t, uint64(6), response.TotalStake)
}

func TestFetchMetricsTroughputHandler(t *testing.T) {
Expand Down

0 comments on commit d116907

Please sign in to comment.