Skip to content

Commit 907c7df

Browse files
pthmasjulienrbrt
andauthored
test: add unit test for header submission (#2315)
<!-- Please read and fill out this form before submitting your PR. Please make sure you have reviewed our contributors guide before submitting your first PR. NOTE: PR titles should follow semantic commits: https://www.conventionalcommits.org/en/v1.0.0/ --> ## Overview - Adds simple unit tests for block header submission ref #2256 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **Tests** - Added new tests to verify successful and failed header submissions, ensuring correct handling of different data availability layer responses. - Enhanced submission tests to validate gas price adjustments on retries. - Improved cache tests with more concise and idiomatic assertions for better clarity and maintainability. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: julienrbrt <julien@rbrt.fr>
1 parent 4856cd3 commit 907c7df

2 files changed

Lines changed: 76 additions & 12 deletions

File tree

block/submitter_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ func TestSubmitDataToDA_Failure(t *testing.T) {
9999
for _, tc := range testCases {
100100
t.Run(tc.name, func(t *testing.T) {
101101
// Reset mock expectations for each error scenario
102+
var gasPriceHistory []float64
102103
da.ExpectedCalls = nil
103104
da.On("GasMultiplier", mock.Anything).Return(2.0, nil)
104105
da.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
106+
Run(func(args mock.Arguments) { gasPriceHistory = append(gasPriceHistory, args.Get(2).(float64)) }). //save the gas price to verify it later
105107
Return(nil, tc.daError)
106108

107109
pubKey, err := m.signer.GetPublic()
@@ -133,6 +135,76 @@ func TestSubmitDataToDA_Failure(t *testing.T) {
133135
// Expect an error from submitDataToDA
134136
err = m.submitDataToDA(context.Background(), &signedData)
135137
assert.Error(t, err, "expected error")
138+
139+
// Validate that gas price increased according to gas multiplier
140+
previousGasPrice := m.gasPrice
141+
assert.Equal(t, gasPriceHistory[0], m.gasPrice) // verify that the first call is done with the right price
142+
for _, gasPrice := range gasPriceHistory[1:] {
143+
assert.Equal(t, gasPrice, previousGasPrice*m.gasMultiplier)
144+
previousGasPrice = gasPrice
145+
}
146+
})
147+
}
148+
}
149+
150+
func TestSubmitHeadersToDA_Success(t *testing.T) {
151+
da := &mocks.DA{}
152+
m := newTestManagerWithDA(t, da)
153+
// Prepare a mock PendingHeaders with test data
154+
m.pendingHeaders = newPendingBlocks(t)
155+
156+
// Fill the pending headers with mock block data
157+
fillWithBlockData(context.Background(), t, m.pendingHeaders, "Test Submitting Headers")
158+
159+
// Simulate DA layer successfully accepting the header submission
160+
da.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
161+
Return([]coreda.ID{[]byte("id")}, nil)
162+
163+
// Call submitHeadersToDA and expect no error
164+
err := m.submitHeadersToDA(context.Background())
165+
assert.NoError(t, err)
166+
}
167+
168+
// TestSubmitHeadersToDA_Failure verifies that submitHeadersToDA returns an error for various DA failures.
169+
func TestSubmitHeadersToDA_Failure(t *testing.T) {
170+
da := &mocks.DA{}
171+
m := newTestManagerWithDA(t, da)
172+
// Prepare a mock PendingHeaders with test data
173+
m.pendingHeaders = newPendingBlocks(t)
174+
175+
// Table-driven test for different DA error scenarios
176+
testCases := []struct {
177+
name string
178+
daError error
179+
}{
180+
{"AlreadyInMempool", coreda.ErrTxAlreadyInMempool},
181+
{"TimedOut", coreda.ErrTxTimedOut},
182+
}
183+
184+
for _, tc := range testCases {
185+
t.Run(tc.name, func(t *testing.T) {
186+
// Fill the pending headers with mock block data for each subtest
187+
fillWithBlockData(context.Background(), t, m.pendingHeaders, "Test Submitting Headers")
188+
// Reset mock expectations for each error scenario
189+
da.ExpectedCalls = nil
190+
// Simulate DA layer returning a specific error
191+
var gasPriceHistory []float64
192+
da.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
193+
Run(func(args mock.Arguments) { gasPriceHistory = append(gasPriceHistory, args.Get(2).(float64)) }). //save the gas price to verify it later
194+
Return(nil, tc.daError)
195+
196+
// Call submitHeadersToDA and expect an error
197+
err := m.submitHeadersToDA(context.Background())
198+
assert.Error(t, err, "expected error for DA error: %v", tc.daError)
199+
assert.Contains(t, err.Error(), "failed to submit all headers to DA layer")
200+
201+
// Validate that gas price increased according to gas multiplier
202+
previousGasPrice := m.gasPrice
203+
assert.Equal(t, gasPriceHistory[0], m.gasPrice) // verify that the first call is done with the right price
204+
for _, gasPrice := range gasPriceHistory[1:] {
205+
assert.Equal(t, gasPrice, previousGasPrice*m.gasMultiplier)
206+
previousGasPrice = gasPrice
207+
}
136208
})
137209
}
138210
}

pkg/cache/cache_test.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,10 @@ import (
1515
// TestNewCache verifies that NewCache initializes correctly
1616
func TestNewCache(t *testing.T) {
1717
cache := NewCache[string]()
18-
if cache == nil {
19-
t.Fatal("NewCache returned nil")
20-
}
21-
if cache.items == nil {
22-
t.Error("items map not initialized")
23-
}
24-
if cache.hashes == nil {
25-
t.Error("hashes map not initialized")
26-
}
27-
if cache.daIncluded == nil {
28-
t.Error("daIncluded map not initialized")
29-
}
18+
require.NotNil(t, cache, "NewCache returned nil")
19+
assert.NotNil(t, cache.items, "items map not initialized")
20+
assert.NotNil(t, cache.hashes, "hashes map not initialized")
21+
assert.NotNil(t, cache.daIncluded, "daIncluded map not initialized")
3022
}
3123

3224
// TestCacheItemOperations tests the item-related operations (Get, Set, Delete)

0 commit comments

Comments
 (0)