Skip to content

Commit df0062c

Browse files
fix linter issues
1 parent 30b67db commit df0062c

9 files changed

+65
-68
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ run:
33
linters:
44
enable:
55
- exhaustive
6-
- exportloopref
6+
- copyloopvar
77
- revive
88
- goimports
99
- gosec

pkg/solana/config_tracker.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ func (c *ConfigTracker) LatestConfigDetails(ctx context.Context) (changedInBlock
2626
func ConfigFromState(ctx context.Context, state State) (types.ContractConfig, error) {
2727
pubKeys := []types.OnchainPublicKey{}
2828
accounts := []types.Account{}
29+
2930
oracles, err := state.Oracles.Data()
3031
if err != nil {
3132
return types.ContractConfig{}, err
3233
}
33-
for _, o := range oracles {
34-
o := o // https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable
35-
pubKeys = append(pubKeys, o.Signer.Key[:])
36-
accounts = append(accounts, types.Account(o.Transmitter.String()))
34+
35+
for idx := range oracles {
36+
pubKeys = append(pubKeys, oracles[idx].Signer.Key[:])
37+
accounts = append(accounts, types.Account(oracles[idx].Transmitter.String()))
3738
}
3839

3940
onchainConfigStruct := median.OnchainConfig{

pkg/solana/fees/block_history_test.go

+35-35
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func TestBlockHistoryEstimator_InvalidBlockHistorySize(t *testing.T) {
4141

4242
func TestBlockHistoryEstimator_LatestBlock(t *testing.T) {
4343
// Helper variables for tests
44-
min := uint64(10)
45-
max := uint64(100_000)
44+
minPrice := uint64(10)
45+
maxPrice := uint64(100_000)
4646
defaultPrice := uint64(100)
4747
depth := uint64(1) // 1 is LatestBlockEstimator
4848
pollPeriod := 100 * time.Millisecond
@@ -63,21 +63,21 @@ func TestBlockHistoryEstimator_LatestBlock(t *testing.T) {
6363
t.Run("Successful Estimation", func(t *testing.T) {
6464
// Setup
6565
cfg := cfgmock.NewConfig(t)
66-
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
66+
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
6767
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))
6868

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

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

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

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

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

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

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

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

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

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

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

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

174174
func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {
175175
// helpers vars for tests
176-
min := uint64(100)
177-
max := uint64(100_000)
176+
minPrice := uint64(100)
177+
maxPrice := uint64(100_000)
178178
depth := uint64(3)
179179
defaultPrice := uint64(100)
180180
pollPeriod := 3 * time.Second
@@ -220,20 +220,20 @@ func TestBlockHistoryEstimator_MultipleBlocks(t *testing.T) {
220220
t.Run("Successful Estimation", func(t *testing.T) {
221221
// Setup
222222
cfg := cfgmock.NewConfig(t)
223-
setupConfigMock(cfg, defaultPrice, min, max, pollPeriod, depth)
223+
setupConfigMock(cfg, defaultPrice, minPrice, pollPeriod, depth)
224224
estimator := initializeEstimator(ctx, t, rwLoader, cfg, logger.Test(t))
225225

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

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

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

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

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

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

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

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

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

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

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

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

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

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

350350
// setupConfigMock configures the Config mock with necessary return values.
351-
func setupConfigMock(cfg *cfgmock.Config, defaultPrice uint64, min, max uint64, pollPeriod time.Duration, depth uint64) {
351+
func setupConfigMock(cfg *cfgmock.Config, defaultPrice uint64, minPrice uint64, pollPeriod time.Duration, depth uint64) {
352352
cfg.On("ComputeUnitPriceDefault").Return(defaultPrice).Once()
353-
cfg.On("ComputeUnitPriceMin").Return(min).Once()
353+
cfg.On("ComputeUnitPriceMin").Return(minPrice).Once()
354354
cfg.On("BlockHistoryPollPeriod").Return(pollPeriod).Once()
355355
cfg.On("BlockHistorySize").Return(depth)
356356
}

pkg/solana/fees/fixed_price.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ type fixedPriceEstimator struct {
1414
}
1515

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

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

2323
return &fixedPriceEstimator{

pkg/solana/fees/utils.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

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

1414
for i := uint(0); i < count; i++ {
@@ -18,19 +18,19 @@ func CalculateFee(base, max, min uint64, count uint) uint64 {
1818
next := amount + amount
1919
if next <= amount {
2020
// overflowed
21-
amount = max
21+
amount = maxFee
2222
break
2323
}
2424
amount = next
2525
}
2626
}
2727

2828
// respect bounds
29-
if amount < min {
30-
return min
29+
if amount < minFee {
30+
return minFee
3131
}
32-
if amount > max {
33-
return max
32+
if amount > maxFee {
33+
return maxFee
3434
}
3535
return amount
3636
}

pkg/solana/logpoller/parser.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,9 @@ func (v *pgDSLParser) Timestamp(prim primitives.Timestamp) {
9090
return
9191
}
9292

93-
var tm int64
93+
tm := int64(prim.Timestamp) //nolint:gosec // disable G115
9494
if prim.Timestamp > math.MaxInt64 {
9595
tm = 0
96-
} else {
97-
tm = int64(prim.Timestamp)
9896
}
9997

10098
v.expression = fmt.Sprintf(

pkg/solana/logpoller/worker.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ type queue[T any] struct {
300300
values []T
301301
}
302302

303-
func newQueue[T any](len uint) *queue[T] {
304-
values := make([]T, len)
303+
func newQueue[T any](maxLen uint) *queue[T] {
304+
values := make([]T, maxLen)
305305

306306
return &queue[T]{
307307
values: values,

pkg/solana/report_test.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,11 @@ func TestMedianFromReport(t *testing.T) {
142142
tt = append(tt, test)
143143
}
144144

145-
for _, tc := range tt {
146-
tc := tc
147-
t.Run(tc.name, func(t *testing.T) {
145+
for idx := range tt {
146+
t.Run(tt[idx].name, func(t *testing.T) {
148147
ctx := tests.Context(t)
149148
var pos []median.ParsedAttributedObservation
150-
for i, obs := range tc.obs {
149+
for i, obs := range tt[idx].obs {
151150
pos = append(pos, median.ParsedAttributedObservation{
152151
Value: obs,
153152
JuelsPerFeeCoin: obs,
@@ -156,15 +155,15 @@ func TestMedianFromReport(t *testing.T) {
156155
}
157156
report, err := cdc.BuildReport(ctx, pos)
158157
require.NoError(t, err)
159-
max, err := cdc.MaxReportLength(ctx, len(tc.obs))
158+
maxLen, err := cdc.MaxReportLength(ctx, len(tt[idx].obs))
160159
require.NoError(t, err)
161-
assert.Equal(t, len(report), max)
160+
assert.Equal(t, len(report), maxLen)
162161
med, err := cdc.MedianFromReport(ctx, report)
163162
require.NoError(t, err)
164-
assert.Equal(t, tc.expectedMedian.String(), med.String())
163+
assert.Equal(t, tt[idx].expectedMedian.String(), med.String())
165164
count, err := cdc.ObserversCountFromReport(report)
166165
require.NoError(t, err)
167-
assert.Equal(t, len(tc.obs), int(count))
166+
assert.Equal(t, len(tt[idx].obs), int(count))
168167
})
169168
}
170169
}

0 commit comments

Comments
 (0)