Skip to content

Commit

Permalink
Add context to GetCurrentBlockNumber() (#1268)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmanc authored Feb 13, 2025
1 parent 461318e commit 456f728
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 23 deletions.
3 changes: 1 addition & 2 deletions core/eth/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func (cs *ChainState) GetOperatorState(ctx context.Context, blockNumber uint, qu
return getOperatorState(operatorsByQuorum, uint32(blockNumber))
}

func (cs *ChainState) GetCurrentBlockNumber() (uint, error) {
ctx := context.Background()
func (cs *ChainState) GetCurrentBlockNumber(ctx context.Context) (uint, error) {
header, err := cs.Client.HeaderByNumber(ctx, nil)
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion core/indexer/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (ics *IndexedChainState) GetIndexedOperators(ctx context.Context, blockNumb
return ops, nil
}

func (ics *IndexedChainState) GetCurrentBlockNumber() (uint, error) {
func (ics *IndexedChainState) GetCurrentBlockNumber(ctx context.Context) (uint, error) {
header, err := ics.Indexer.GetLatestHeader(false)
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion core/mock/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (d *ChainDataMock) GetIndexedOperators(ctx context.Context, blockNumber uin
return state.IndexedOperatorState.IndexedOperators, nil
}

func (d *ChainDataMock) GetCurrentBlockNumber() (uint, error) {
func (d *ChainDataMock) GetCurrentBlockNumber(ctx context.Context) (uint, error) {
args := d.Called()
return args.Get(0).(uint), args.Error(1)
}
Expand Down
2 changes: 1 addition & 1 deletion core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ type IndexedOperatorState struct {

// ChainState is an interface for getting information about the current chain state.
type ChainState interface {
GetCurrentBlockNumber() (uint, error)
GetCurrentBlockNumber(ctx context.Context) (uint, error)
GetOperatorState(ctx context.Context, blockNumber uint, quorums []QuorumID) (*OperatorState, error)
GetOperatorStateByOperator(ctx context.Context, blockNumber uint, operator OperatorID) (*OperatorState, error)
GetOperatorSocket(ctx context.Context, blockNumber uint, operator OperatorID) (string, error)
Expand Down
2 changes: 1 addition & 1 deletion core/thegraph/state_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestIndexerIntegration(t *testing.T) {
err = cs.Start(context.Background())
assert.NoError(t, err)

headerNum, err := cs.GetCurrentBlockNumber()
headerNum, err := cs.GetCurrentBlockNumber(context.Background())
assert.NoError(t, err)

state, err := cs.GetIndexedOperatorState(context.Background(), headerNum, quorums)
Expand Down
8 changes: 4 additions & 4 deletions core/thegraph/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestIndexedChainState_GetIndexedOperatorState(t *testing.T) {
err = cs.Start(context.Background())
assert.NoError(t, err)

headerNum, err := cs.GetCurrentBlockNumber()
headerNum, err := cs.GetCurrentBlockNumber(context.Background())
assert.NoError(t, err)

indexedState, err := cs.GetIndexedOperatorState(context.Background(), headerNum, quorums)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestIndexedChainState_GetIndexedOperatorStateMissingOperator(t *testing.T)
err = cs.Start(context.Background())
assert.NoError(t, err)

headerNum, err := cs.GetCurrentBlockNumber()
headerNum, err := cs.GetCurrentBlockNumber(context.Background())
assert.NoError(t, err)

_, err = cs.GetIndexedOperatorState(context.Background(), headerNum, quorums)
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestIndexedChainState_GetIndexedOperatorStateExtraOperator(t *testing.T) {
err = cs.Start(context.Background())
assert.NoError(t, err)

headerNum, err := cs.GetCurrentBlockNumber()
headerNum, err := cs.GetCurrentBlockNumber(context.Background())
assert.NoError(t, err)

indexedState, err := cs.GetIndexedOperatorState(context.Background(), headerNum, quorums)
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestIndexedChainState_GetIndexedOperatorInfoByOperatorId(t *testing.T) {
err = cs.Start(context.Background())
assert.NoError(t, err)

headerNum, err := cs.GetCurrentBlockNumber()
headerNum, err := cs.GetCurrentBlockNumber(context.Background())
assert.NoError(t, err)

opID := ethcomm.HexToHash(id)
Expand Down
4 changes: 2 additions & 2 deletions disperser/batcher/encoding_streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (e *EncodingStreamer) RequestEncoding(ctx context.Context, encoderChan chan

if referenceBlockNumber == 0 {
// Update the reference block number for the next iteration
blockNumber, err := e.chainState.GetCurrentBlockNumber()
blockNumber, err := e.chainState.GetCurrentBlockNumber(ctx)
if err != nil {
return fmt.Errorf("failed to get current block number, won't request encoding: %w", err)
} else {
Expand Down Expand Up @@ -497,7 +497,7 @@ func (e *EncodingStreamer) CreateBatch(ctx context.Context) (*batch, error) {

// If there were no requested blobs between the last batch and now, there is no need to create a new batch
if e.ReferenceBlockNumber == 0 {
blockNumber, err := e.chainState.GetCurrentBlockNumber()
blockNumber, err := e.chainState.GetCurrentBlockNumber(ctx)
if err != nil {
e.logger.Error("failed to get current block number. will not clean up the encoded blob store.", "err", err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion disperser/controller/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (d *Dispatcher) HandleBatch(ctx context.Context) (chan core.SigningMessage,
d.metrics.reportHandleBatchLatency(time.Since(start))
}()

currentBlockNumber, err := d.chainState.GetCurrentBlockNumber()
currentBlockNumber, err := d.chainState.GetCurrentBlockNumber(ctx)
if err != nil {
return nil, nil, fmt.Errorf("failed to get current block number: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions disperser/dataapi/operator_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ func (oh *OperatorHandler) GetOperatorsStakeAtBlock(ctx context.Context, operato
}

func (oh *OperatorHandler) GetOperatorsStake(ctx context.Context, operatorId string) (*OperatorsStakeResponse, error) {
currentBlock, err := oh.indexedChainState.GetCurrentBlockNumber()
currentBlock, err := oh.indexedChainState.GetCurrentBlockNumber(ctx)
if err != nil {
return nil, fmt.Errorf("failed to fetch current block number: %w", err)
}
return oh.GetOperatorsStakeAtBlock(ctx, operatorId, uint32(currentBlock))
}

func (s *OperatorHandler) ScanOperatorsHostInfo(ctx context.Context) (*SemverReportResponse, error) {
currentBlock, err := s.indexedChainState.GetCurrentBlockNumber()
currentBlock, err := s.indexedChainState.GetCurrentBlockNumber(ctx)
if err != nil {
return nil, fmt.Errorf("failed to fetch current block number: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion disperser/dataapi/v2/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (s *ServerV2) FetchOperatorsStake(c *gin.Context) {
operatorId := c.DefaultQuery("operator_id", "")
s.logger.Info("getting operators stake distribution", "operatorId", operatorId)

currentBlock, err := s.indexedChainState.GetCurrentBlockNumber()
currentBlock, err := s.indexedChainState.GetCurrentBlockNumber(c.Request.Context())
if err != nil {
s.metrics.IncrementFailedRequestNum("FetchOperatorsStake")
errorResponse(c, fmt.Errorf("failed to get current block number: %w", err))
Expand Down
7 changes: 4 additions & 3 deletions relay/auth/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"errors"
"fmt"
"github.com/Layr-Labs/eigenda/api/hashing"
"sync"
"time"

"github.com/Layr-Labs/eigenda/api/hashing"

pb "github.com/Layr-Labs/eigenda/api/grpc/relay"
"github.com/Layr-Labs/eigenda/core"
"github.com/emirpasic/gods/queues"
Expand Down Expand Up @@ -85,7 +86,7 @@ func NewRequestAuthenticator(
}

func (a *requestAuthenticator) preloadCache(ctx context.Context) error {
blockNumber, err := a.ics.GetCurrentBlockNumber()
blockNumber, err := a.ics.GetCurrentBlockNumber(ctx)
if err != nil {
return fmt.Errorf("failed to get current block number: %w", err)
}
Expand Down Expand Up @@ -148,7 +149,7 @@ func (a *requestAuthenticator) getOperatorKey(ctx context.Context, operatorID co
return key, nil
}

blockNumber, err := a.ics.GetCurrentBlockNumber()
blockNumber, err := a.ics.GetCurrentBlockNumber(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get current block number: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion relay/mock/ics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mock

import (
"context"

"github.com/Layr-Labs/eigenda/core"
"github.com/stretchr/testify/mock"
)
Expand All @@ -13,7 +14,7 @@ type IndexedChainState struct {
Mock mock.Mock
}

func (m *IndexedChainState) GetCurrentBlockNumber() (uint, error) {
func (m *IndexedChainState) GetCurrentBlockNumber(ctx context.Context) (uint, error) {
args := m.Mock.Called()
return args.Get(0).(uint), args.Error(1)
}
Expand Down
2 changes: 1 addition & 1 deletion test/v2/client/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func (c *TestClient) ReadBlobFromValidators(
quorums []core.QuorumID,
expectedPayload []byte) error {

currentBlockNumber, err := c.indexedChainState.GetCurrentBlockNumber()
currentBlockNumber, err := c.indexedChainState.GetCurrentBlockNumber(ctx)
if err != nil {
return fmt.Errorf("failed to get current block number: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tools/quorumscan/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func RunScan(ctx *cli.Context) error {
if config.BlockNumber != 0 {
blockNumber = uint(config.BlockNumber)
} else {
blockNumber, err = ics.GetCurrentBlockNumber()
blockNumber, err = ics.GetCurrentBlockNumber(context.Background())
if err != nil {
return fmt.Errorf("failed to fetch current block number - %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tools/semverscan/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func RunScan(ctx *cli.Context) error {
logger.Info("Connecting to subgraph", "url", config.ChainStateConfig.Endpoint)
ics := thegraph.MakeIndexedChainState(config.ChainStateConfig, chainState, logger)

currentBlock, err := ics.GetCurrentBlockNumber()
currentBlock, err := ics.GetCurrentBlockNumber(context.Background())
if err != nil {
return fmt.Errorf("failed to fetch current block number - %s", err)
}
Expand Down

0 comments on commit 456f728

Please sign in to comment.