Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
Farber98 committed Oct 31, 2024
1 parent ca7f840 commit 014d1e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
21 changes: 15 additions & 6 deletions pkg/solana/fees/block_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

var _ Estimator = &blockHistoryEstimator{}

var errNoComputeUnitPriceCollected = fmt.Errorf("no compute unit prices collected")

type blockHistoryEstimator struct {
starter services.StateMachine
chStop services.StopChan
Expand Down Expand Up @@ -130,6 +132,10 @@ func (bhe *blockHistoryEstimator) calculatePriceFromLatestBlock(ctx context.Cont
return fmt.Errorf("failed to parse block: %w", err)
}

if len(feeData.Prices) == 0 {
return errNoComputeUnitPriceCollected
}

// take median of returned fee values
v, err := mathutil.Median(feeData.Prices...)
if err != nil {
Expand Down Expand Up @@ -194,11 +200,13 @@ func (bhe *blockHistoryEstimator) calculatePriceFromMultipleBlocks(ctx context.C

// Fetch the block details
block, errGetBlock := c.GetBlock(ctx, s)
if errGetBlock != nil || block == nil {
// Failed to get block at slot || no block found at slot
if errGetBlock != nil {
bhe.lgr.Errorw("BlockHistoryEstimator: failed to get block", "slot", s, "error", errGetBlock)
}
if errGetBlock != nil {
bhe.lgr.Errorw("BlockHistoryEstimator: failed to get block at slot", "slot", s, "error", errGetBlock)
return
}

// No block found at slot. Not logging since not all slots may have a block.
if block == nil {
return
}

Expand All @@ -210,6 +218,7 @@ func (bhe *blockHistoryEstimator) calculatePriceFromMultipleBlocks(ctx context.C
}

// When no relevant transactions for compute unit price are found in this block, we can skip it.
// No need to log this as an error. It is expected behavior.
if len(feeData.Prices) == 0 {
return
}
Expand All @@ -233,7 +242,7 @@ func (bhe *blockHistoryEstimator) calculatePriceFromMultipleBlocks(ctx context.C
wg.Wait()

if len(blockMedians) == 0 {
return fmt.Errorf("no compute unit prices collected")
return errNoComputeUnitPriceCollected
}

// Calculate avg from medians of the blocks.
Expand Down
6 changes: 3 additions & 3 deletions pkg/solana/fees/block_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestBlockHistoryEstimator_LatestBlock(t *testing.T) {
assert.Equal(t, uint64(100), estimator.BaseComputeUnitPrice(), "Price should not change when parsing fails")
})

t.Run("Failed to Calculate Median", func(t *testing.T) {
t.Run("no compute unit prices collected", func(t *testing.T) {
// Setup
rw := clientmock.NewReaderWriter(t)
rwLoader := utils.NewLazyLoad(func() (client.ReaderWriter, error) {
Expand All @@ -147,7 +147,7 @@ func TestBlockHistoryEstimator_LatestBlock(t *testing.T) {
estimator := initializeEstimator(tests.Context(t), t, rwLoader, cfg, logger.Test(t))

// Ensure the price remains unchanged
require.Error(t, estimator.calculatePrice(tests.Context(t)), "Expected error when median calculation fails")
require.EqualError(t, estimator.calculatePrice(tests.Context(t)), errNoComputeUnitPriceCollected.Error(), "Expected error when no compute unit prices are collected")
cfg.On("ComputeUnitPriceMax").Return(max)
assert.Equal(t, uint64(100), estimator.BaseComputeUnitPrice(), "Price should not change when median calculation fails")
})
Expand Down Expand Up @@ -339,7 +339,7 @@ func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {
estimator := initializeEstimator(tests.Context(t), t, rwLoader, cfg, logger.Test(t))

// Price should remain unchanged
require.Error(t, estimator.calculatePrice(tests.Context(t)), "Expected error when no compute unit prices are collected")
require.EqualError(t, estimator.calculatePrice(tests.Context(t)), errNoComputeUnitPriceCollected.Error(), "Expected error when no compute unit prices are collected")
cfg.On("ComputeUnitPriceMax").Return(max)
assert.Equal(t, defaultPrice, estimator.BaseComputeUnitPrice())
})
Expand Down

0 comments on commit 014d1e2

Please sign in to comment.