Skip to content

Commit a9de50d

Browse files
committed
simplify
1 parent b28c122 commit a9de50d

File tree

3 files changed

+16
-114
lines changed

3 files changed

+16
-114
lines changed

block/internal/submitting/batching_strategy.go

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ type BatchingStrategy interface {
1414
ShouldSubmit(pendingCount uint64, totalSize int, maxBlobSize int, timeSinceLastSubmit time.Duration) bool
1515
}
1616

17+
// NewBatchingStrategy creates a batching strategy based on configuration
18+
func NewBatchingStrategy(cfg config.DAConfig) (BatchingStrategy, error) {
19+
switch cfg.BatchingStrategy {
20+
case "immediate":
21+
return &ImmediateStrategy{}, nil
22+
case "size":
23+
return NewSizeBasedStrategy(cfg.BatchSizeThreshold, cfg.BatchMinItems), nil
24+
case "time":
25+
return NewTimeBasedStrategy(cfg.BlockTime.Duration, cfg.BatchMaxDelay.Duration, cfg.BatchMinItems), nil
26+
case "adaptive":
27+
return NewAdaptiveStrategy(cfg.BlockTime.Duration, cfg.BatchSizeThreshold, cfg.BatchMaxDelay.Duration, cfg.BatchMinItems), nil
28+
default:
29+
return nil, fmt.Errorf("unknown batching strategy: %s", cfg.BatchingStrategy)
30+
}
31+
}
32+
1733
// ImmediateStrategy submits as soon as any items are available
1834
type ImmediateStrategy struct{}
1935

@@ -122,22 +138,6 @@ func (s *AdaptiveStrategy) ShouldSubmit(pendingCount uint64, totalSize int, maxB
122138
return false
123139
}
124140

125-
// NewBatchingStrategy creates a batching strategy based on configuration
126-
func NewBatchingStrategy(cfg config.DAConfig) (BatchingStrategy, error) {
127-
switch cfg.BatchingStrategy {
128-
case "immediate":
129-
return &ImmediateStrategy{}, nil
130-
case "size":
131-
return NewSizeBasedStrategy(cfg.BatchSizeThreshold, cfg.BatchMinItems), nil
132-
case "time":
133-
return NewTimeBasedStrategy(cfg.BlockTime.Duration, cfg.BatchMaxDelay.Duration, cfg.BatchMinItems), nil
134-
case "adaptive":
135-
return NewAdaptiveStrategy(cfg.BlockTime.Duration, cfg.BatchSizeThreshold, cfg.BatchMaxDelay.Duration, cfg.BatchMinItems), nil
136-
default:
137-
return nil, fmt.Errorf("unknown batching strategy: %s", cfg.BatchingStrategy)
138-
}
139-
}
140-
141141
// estimateBatchSize estimates the total size of pending items
142142
// This is a helper function that can be used by the submitter
143143
func estimateBatchSize(marshaled [][]byte) int {
@@ -180,39 +180,6 @@ func optimizeBatchSize(marshaled [][]byte, maxBlobSize int, targetUtilization fl
180180
return count
181181
}
182182

183-
// BatchMetrics provides information about batch efficiency
184-
type BatchMetrics struct {
185-
ItemCount int
186-
TotalBytes int
187-
MaxBlobBytes int
188-
Utilization float64 // percentage of max blob size used
189-
EstimatedCost float64 // estimated cost relative to single full blob
190-
}
191-
192-
// calculateBatchMetrics computes metrics for a batch
193-
func calculateBatchMetrics(itemCount int, totalBytes int, maxBlobBytes int) BatchMetrics {
194-
utilization := 0.0
195-
if maxBlobBytes > 0 {
196-
utilization = float64(totalBytes) / float64(maxBlobBytes)
197-
}
198-
199-
// Rough cost estimate: each blob submission has a fixed cost
200-
// Higher utilization = better cost efficiency
201-
estimatedCost := 1.0
202-
if utilization > 0 {
203-
// If we're only using 50% of the blob, we're paying 2x per byte effectively
204-
estimatedCost = 1.0 / utilization
205-
}
206-
207-
return BatchMetrics{
208-
ItemCount: itemCount,
209-
TotalBytes: totalBytes,
210-
MaxBlobBytes: maxBlobBytes,
211-
Utilization: utilization,
212-
EstimatedCost: estimatedCost,
213-
}
214-
}
215-
216183
// ShouldWaitForMoreItems determines if we should wait for more items
217184
// to improve batch efficiency
218185
func ShouldWaitForMoreItems(
@@ -239,10 +206,3 @@ func ShouldWaitForMoreItems(
239206

240207
return currentUtilization < minUtilization-epsilon
241208
}
242-
243-
// BatchingConfig holds configuration for batch optimization
244-
type BatchingConfig struct {
245-
MaxBlobSize int
246-
Strategy BatchingStrategy
247-
TargetUtilization float64
248-
}

block/internal/submitting/batching_strategy_test.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -406,63 +406,6 @@ func TestOptimizeBatchSize(t *testing.T) {
406406
}
407407
}
408408

409-
func TestCalculateBatchMetrics(t *testing.T) {
410-
maxBlobSize := 8 * 1024 * 1024
411-
412-
tests := []struct {
413-
name string
414-
itemCount int
415-
totalBytes int
416-
expectedUtil float64
417-
expectedCostRange [2]float64 // min, max
418-
}{
419-
{
420-
name: "empty batch",
421-
itemCount: 0,
422-
totalBytes: 0,
423-
expectedUtil: 0.0,
424-
expectedCostRange: [2]float64{0, 999999}, // cost is undefined for empty
425-
},
426-
{
427-
name: "half full",
428-
itemCount: 10,
429-
totalBytes: 4 * 1024 * 1024,
430-
expectedUtil: 0.5,
431-
expectedCostRange: [2]float64{2.0, 2.0}, // 1/0.5 = 2.0x cost
432-
},
433-
{
434-
name: "80% full",
435-
itemCount: 20,
436-
totalBytes: int(float64(maxBlobSize) * 0.8),
437-
expectedUtil: 0.8,
438-
expectedCostRange: [2]float64{1.25, 1.25}, // 1/0.8 = 1.25x cost
439-
},
440-
{
441-
name: "nearly full",
442-
itemCount: 50,
443-
totalBytes: int(float64(maxBlobSize) * 0.95),
444-
expectedUtil: 0.95,
445-
expectedCostRange: [2]float64{1.05, 1.06}, // ~1.05x cost
446-
},
447-
}
448-
449-
for _, tt := range tests {
450-
t.Run(tt.name, func(t *testing.T) {
451-
metrics := calculateBatchMetrics(tt.itemCount, tt.totalBytes, maxBlobSize)
452-
453-
assert.Equal(t, tt.itemCount, metrics.ItemCount)
454-
assert.Equal(t, tt.totalBytes, metrics.TotalBytes)
455-
assert.Equal(t, maxBlobSize, metrics.MaxBlobBytes)
456-
assert.InDelta(t, tt.expectedUtil, metrics.Utilization, 0.01)
457-
458-
if tt.totalBytes > 0 {
459-
assert.InEpsilon(t, (tt.expectedCostRange[0]+tt.expectedCostRange[1])/2,
460-
metrics.EstimatedCost, 0.01, "cost should be within range")
461-
}
462-
})
463-
}
464-
}
465-
466409
func TestShouldWaitForMoreItems(t *testing.T) {
467410
maxBlobSize := 8 * 1024 * 1024
468411

block/internal/submitting/submitter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ func NewSubmitter(
8888
) *Submitter {
8989
submitterLogger := logger.With().Str("component", "submitter").Logger()
9090

91-
// Initialize batching strategy
9291
strategy, err := NewBatchingStrategy(config.DA)
9392
if err != nil {
9493
submitterLogger.Warn().Err(err).Msg("failed to create batching strategy, using time-based default")

0 commit comments

Comments
 (0)