Skip to content

Commit

Permalink
fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
EasterTheBunny committed Dec 24, 2024
1 parent 30b67db commit df0062c
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ run:
linters:
enable:
- exhaustive
- exportloopref
- copyloopvar
- revive
- goimports
- gosec
Expand Down
9 changes: 5 additions & 4 deletions pkg/solana/config_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ func (c *ConfigTracker) LatestConfigDetails(ctx context.Context) (changedInBlock
func ConfigFromState(ctx context.Context, state State) (types.ContractConfig, error) {
pubKeys := []types.OnchainPublicKey{}
accounts := []types.Account{}

oracles, err := state.Oracles.Data()
if err != nil {
return types.ContractConfig{}, err
}
for _, o := range oracles {
o := o // https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable
pubKeys = append(pubKeys, o.Signer.Key[:])
accounts = append(accounts, types.Account(o.Transmitter.String()))

for idx := range oracles {
pubKeys = append(pubKeys, oracles[idx].Signer.Key[:])
accounts = append(accounts, types.Account(oracles[idx].Transmitter.String()))
}

onchainConfigStruct := median.OnchainConfig{
Expand Down
70 changes: 35 additions & 35 deletions pkg/solana/fees/block_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestBlockHistoryEstimator_InvalidBlockHistorySize(t *testing.T) {

func TestBlockHistoryEstimator_LatestBlock(t *testing.T) {
// Helper variables for tests
min := uint64(10)
max := uint64(100_000)
minPrice := uint64(10)
maxPrice := uint64(100_000)
defaultPrice := uint64(100)
depth := uint64(1) // 1 is LatestBlockEstimator
pollPeriod := 100 * time.Millisecond
Expand All @@ -63,21 +63,21 @@ func TestBlockHistoryEstimator_LatestBlock(t *testing.T) {
t.Run("Successful Estimation", func(t *testing.T) {
// Setup
cfg := cfgmock.NewConfig(t)
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Assert the computed price matches the expected price
require.NoError(t, estimator.calculatePrice(ctx), "Failed to calculate price")
cfg.On("ComputeUnitPriceMin").Return(min)
cfg.On("ComputeUnitPriceMax").Return(max)
cfg.On("ComputeUnitPriceMin").Return(minPrice)
cfg.On("ComputeUnitPriceMax").Return(maxPrice)
assert.Equal(t, uint64(lastBlockMedianPrice), estimator.BaseComputeUnitPrice())
})

t.Run("Min Gate: Price Should Be Floored at Min", func(t *testing.T) {
// Setup
cfg := cfgmock.NewConfig(t)
tmpMin := uint64(lastBlockMedianPrice) + 100 // Set min higher than the median price
setupConfigMock(cfg, defaultPrice, tmpMin, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, tmpMin, pollPeriod, depth)
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Call calculatePrice and ensure no error
Expand All @@ -91,14 +91,14 @@ func TestBlockHistoryEstimator_LatestBlock(t *testing.T) {
// Setup
cfg := cfgmock.NewConfig(t)
tmpMax := uint64(lastBlockMedianPrice) - 100 // Set max lower than the median price
setupConfigMock(cfg, defaultPrice, min, tmpMax, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Call calculatePrice and ensure no error
// Assert the compute unit price is capped at max
require.NoError(t, estimator.calculatePrice(ctx), "Failed to calculate price with price above max")
cfg.On("ComputeUnitPriceMax").Return(tmpMax)
cfg.On("ComputeUnitPriceMin").Return(min)
cfg.On("ComputeUnitPriceMin").Return(minPrice)
assert.Equal(t, tmpMax, estimator.BaseComputeUnitPrice(), "Price should be capped at max")
})

Expand All @@ -109,13 +109,13 @@ func TestBlockHistoryEstimator_LatestBlock(t *testing.T) {
return rw, nil
})
cfg := cfgmock.NewConfig(t)
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
rw.On("GetLatestBlock", mock.Anything).Return(nil, fmt.Errorf("fail rpc call")) // Mock GetLatestBlock returning error
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Ensure the price remains unchanged
require.Error(t, estimator.calculatePrice(ctx), "Expected error when GetLatestBlock fails")
cfg.On("ComputeUnitPriceMax").Return(max)
cfg.On("ComputeUnitPriceMax").Return(maxPrice)
assert.Equal(t, uint64(100), estimator.BaseComputeUnitPrice(), "Price should not change when GetLatestBlock fails")
})

Expand All @@ -126,13 +126,13 @@ func TestBlockHistoryEstimator_LatestBlock(t *testing.T) {
return rw, nil
})
cfg := cfgmock.NewConfig(t)
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
rw.On("GetLatestBlock", mock.Anything).Return(nil, nil) // Mock GetLatestBlock returning nil
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Ensure the price remains unchanged
require.Error(t, estimator.calculatePrice(ctx), "Expected error when parsing fails")
cfg.On("ComputeUnitPriceMax").Return(max)
cfg.On("ComputeUnitPriceMax").Return(maxPrice)
assert.Equal(t, uint64(100), estimator.BaseComputeUnitPrice(), "Price should not change when parsing fails")
})

Expand All @@ -143,13 +143,13 @@ func TestBlockHistoryEstimator_LatestBlock(t *testing.T) {
return rw, nil
})
cfg := cfgmock.NewConfig(t)
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
rw.On("GetLatestBlock", mock.Anything).Return(&rpc.GetBlockResult{}, nil) // Mock GetLatestBlock returning empty block
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Ensure the price remains unchanged
require.EqualError(t, estimator.calculatePrice(ctx), errNoComputeUnitPriceCollected.Error(), "Expected error when no compute unit prices are collected")
cfg.On("ComputeUnitPriceMax").Return(max)
cfg.On("ComputeUnitPriceMax").Return(maxPrice)
assert.Equal(t, uint64(100), estimator.BaseComputeUnitPrice(), "Price should not change when median calculation fails")
})

Expand All @@ -160,21 +160,21 @@ func TestBlockHistoryEstimator_LatestBlock(t *testing.T) {
return nil, fmt.Errorf("fail client load")
})
cfg := cfgmock.NewConfig(t)
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
estimator := initializeEstimator(ctx, t, rwFailLoader, cfg, logger.Test(t))

// Call calculatePrice and expect an error
// Ensure the price remains unchanged
require.Error(t, estimator.calculatePrice(ctx), "Expected error when getting client fails")
cfg.On("ComputeUnitPriceMax").Return(max)
cfg.On("ComputeUnitPriceMax").Return(maxPrice)
assert.Equal(t, defaultPrice, estimator.BaseComputeUnitPrice(), "Price should remain at default when client fails")
})
}

func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {
// helpers vars for tests
min := uint64(100)
max := uint64(100_000)
minPrice := uint64(100)
maxPrice := uint64(100_000)
depth := uint64(3)
defaultPrice := uint64(100)
pollPeriod := 3 * time.Second
Expand Down Expand Up @@ -220,20 +220,20 @@ func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {
t.Run("Successful Estimation", func(t *testing.T) {
// Setup
cfg := cfgmock.NewConfig(t)
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Calculated avg price should be equal to the one extracted manually from the blocks.
require.NoError(t, estimator.calculatePrice(ctx))
cfg.On("ComputeUnitPriceMax").Return(max)
cfg.On("ComputeUnitPriceMax").Return(maxPrice)
assert.Equal(t, uint64(multipleBlocksAvg), estimator.BaseComputeUnitPrice())
})

t.Run("Min Gate: Price Should Be Floored at Min", func(t *testing.T) {
// Setup
cfg := cfgmock.NewConfig(t)
tmpMin := uint64(multipleBlocksAvg) + 100 // Set min higher than the avg price
setupConfigMock(cfg, defaultPrice, tmpMin, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, tmpMin, pollPeriod, depth)
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Compute unit price should be floored at min
Expand All @@ -246,13 +246,13 @@ func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {
// Setup
cfg := cfgmock.NewConfig(t)
tmpMax := uint64(multipleBlocksAvg) - 100 // Set tmpMax lower than the avg price
setupConfigMock(cfg, defaultPrice, min, tmpMax, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Compute unit price should be capped at max
require.NoError(t, estimator.calculatePrice(ctx), "Failed to calculate price with price above max")
cfg.On("ComputeUnitPriceMax").Return(tmpMax)
cfg.On("ComputeUnitPriceMin").Return(min)
cfg.On("ComputeUnitPriceMin").Return(minPrice)
assert.Equal(t, tmpMax, estimator.BaseComputeUnitPrice(), "Price should be capped at max")
})

Expand All @@ -264,12 +264,12 @@ func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {
return nil, fmt.Errorf("fail client load")
})
cfg := cfgmock.NewConfig(t)
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
estimator := initializeEstimator(ctx, t, rwFailLoader, cfg, logger.Test(t))

// Price should remain unchanged
require.Error(t, estimator.calculatePrice(ctx), "Expected error when getting client fails")
cfg.On("ComputeUnitPriceMax").Return(max)
cfg.On("ComputeUnitPriceMax").Return(maxPrice)
assert.Equal(t, defaultPrice, estimator.BaseComputeUnitPrice())
})

Expand All @@ -280,13 +280,13 @@ func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {
return rw, nil
})
cfg := cfgmock.NewConfig(t)
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
rw.On("SlotHeight", mock.Anything).Return(uint64(0), fmt.Errorf("failed to get current slot")) // Mock SlotHeight returning error
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Price should remain unchanged
require.Error(t, estimator.calculatePrice(ctx), "Expected error when getting current slot fails")
cfg.On("ComputeUnitPriceMax").Return(max)
cfg.On("ComputeUnitPriceMax").Return(maxPrice)
assert.Equal(t, defaultPrice, estimator.BaseComputeUnitPrice())
})

Expand All @@ -297,13 +297,13 @@ func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {
return rw, nil
})
cfg := cfgmock.NewConfig(t)
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
rw.On("SlotHeight", mock.Anything).Return(depth-1, nil) // Mock SlotHeight returning less than desiredBlockCount
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Price should remain unchanged
require.Error(t, estimator.calculatePrice(ctx), "Expected error when current slot is less than desired block count")
cfg.On("ComputeUnitPriceMax").Return(max)
cfg.On("ComputeUnitPriceMax").Return(maxPrice)
assert.Equal(t, defaultPrice, estimator.BaseComputeUnitPrice())
})

Expand All @@ -314,15 +314,15 @@ func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {
return rw, nil
})
cfg := cfgmock.NewConfig(t)
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
rw.On("SlotHeight", mock.Anything).Return(testSlots[len(testSlots)-1], nil)
rw.On("GetBlocksWithLimit", mock.Anything, mock.Anything, mock.Anything).
Return(nil, fmt.Errorf("failed to get blocks with limit")) // Mock GetBlocksWithLimit returning error
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))

// Price should remain unchanged
require.Error(t, estimator.calculatePrice(ctx), "Expected error when getting blocks with limit fails")
cfg.On("ComputeUnitPriceMax").Return(max)
cfg.On("ComputeUnitPriceMax").Return(maxPrice)
assert.Equal(t, defaultPrice, estimator.BaseComputeUnitPrice())
})

Expand All @@ -333,7 +333,7 @@ func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {
return rw, nil
})
cfg := cfgmock.NewConfig(t)
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
rw.On("SlotHeight", mock.Anything).Return(testSlots[len(testSlots)-1], nil)
emptyBlocks := rpc.BlocksResult{} // No blocks with compute unit prices
rw.On("GetBlocksWithLimit", mock.Anything, mock.Anything, mock.Anything).
Expand All @@ -342,15 +342,15 @@ func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {

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

// setupConfigMock configures the Config mock with necessary return values.
func setupConfigMock(cfg *cfgmock.Config, defaultPrice uint64, min, max uint64, pollPeriod time.Duration, depth uint64) {
func setupConfigMock(cfg *cfgmock.Config, defaultPrice uint64, minPrice uint64, pollPeriod time.Duration, depth uint64) {
cfg.On("ComputeUnitPriceDefault").Return(defaultPrice).Once()
cfg.On("ComputeUnitPriceMin").Return(min).Once()
cfg.On("ComputeUnitPriceMin").Return(minPrice).Once()
cfg.On("BlockHistoryPollPeriod").Return(pollPeriod).Once()
cfg.On("BlockHistorySize").Return(depth)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/solana/fees/fixed_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type fixedPriceEstimator struct {
}

func NewFixedPriceEstimator(cfg config.Config) (Estimator, error) {
defaultPrice, min, max := cfg.ComputeUnitPriceDefault(), cfg.ComputeUnitPriceMin(), cfg.ComputeUnitPriceMax()
defaultPrice, minPrice, maxPrice := cfg.ComputeUnitPriceDefault(), cfg.ComputeUnitPriceMin(), cfg.ComputeUnitPriceMax()

if defaultPrice < min || defaultPrice > max {
return nil, fmt.Errorf("default price (%d) is not within the min (%d) and max (%d) price bounds", defaultPrice, min, max)
if defaultPrice < minPrice || defaultPrice > maxPrice {
return nil, fmt.Errorf("default price (%d) is not within the min (%d) and max (%d) price bounds", defaultPrice, minPrice, maxPrice)
}

return &fixedPriceEstimator{
Expand Down
12 changes: 6 additions & 6 deletions pkg/solana/fees/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// returns new fee based on number of times bumped
func CalculateFee(base, max, min uint64, count uint) uint64 {
func CalculateFee(base, maxFee, minFee uint64, count uint) uint64 {
amount := base

for i := uint(0); i < count; i++ {
Expand All @@ -18,19 +18,19 @@ func CalculateFee(base, max, min uint64, count uint) uint64 {
next := amount + amount
if next <= amount {
// overflowed
amount = max
amount = maxFee
break
}
amount = next
}
}

// respect bounds
if amount < min {
return min
if amount < minFee {
return minFee
}
if amount > max {
return max
if amount > maxFee {
return maxFee
}
return amount
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/solana/logpoller/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,9 @@ func (v *pgDSLParser) Timestamp(prim primitives.Timestamp) {
return
}

var tm int64
tm := int64(prim.Timestamp) //nolint:gosec // disable G115
if prim.Timestamp > math.MaxInt64 {
tm = 0
} else {
tm = int64(prim.Timestamp)
}

v.expression = fmt.Sprintf(
Expand Down
4 changes: 2 additions & 2 deletions pkg/solana/logpoller/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ type queue[T any] struct {
values []T
}

func newQueue[T any](len uint) *queue[T] {
values := make([]T, len)
func newQueue[T any](maxLen uint) *queue[T] {
values := make([]T, maxLen)

return &queue[T]{
values: values,
Expand Down
15 changes: 7 additions & 8 deletions pkg/solana/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,11 @@ func TestMedianFromReport(t *testing.T) {
tt = append(tt, test)
}

for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
for idx := range tt {
t.Run(tt[idx].name, func(t *testing.T) {
ctx := tests.Context(t)
var pos []median.ParsedAttributedObservation
for i, obs := range tc.obs {
for i, obs := range tt[idx].obs {
pos = append(pos, median.ParsedAttributedObservation{
Value: obs,
JuelsPerFeeCoin: obs,
Expand All @@ -156,15 +155,15 @@ func TestMedianFromReport(t *testing.T) {
}
report, err := cdc.BuildReport(ctx, pos)
require.NoError(t, err)
max, err := cdc.MaxReportLength(ctx, len(tc.obs))
maxLen, err := cdc.MaxReportLength(ctx, len(tt[idx].obs))
require.NoError(t, err)
assert.Equal(t, len(report), max)
assert.Equal(t, len(report), maxLen)
med, err := cdc.MedianFromReport(ctx, report)
require.NoError(t, err)
assert.Equal(t, tc.expectedMedian.String(), med.String())
assert.Equal(t, tt[idx].expectedMedian.String(), med.String())
count, err := cdc.ObserversCountFromReport(report)
require.NoError(t, err)
assert.Equal(t, len(tc.obs), int(count))
assert.Equal(t, len(tt[idx].obs), int(count))
})
}
}
Expand Down
Loading

0 comments on commit df0062c

Please sign in to comment.