From 3cdf3f266c604740659e349cd34560af8c129915 Mon Sep 17 00:00:00 2001 From: pthmas <9058370+pthmas@users.noreply.github.com> Date: Tue, 27 May 2025 18:42:26 +0200 Subject: [PATCH 1/4] add unit test for header submission --- block/submitter_test.go | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/block/submitter_test.go b/block/submitter_test.go index 2ff760e538..f9b5ec6dca 100644 --- a/block/submitter_test.go +++ b/block/submitter_test.go @@ -78,3 +78,55 @@ func TestSubmitBatchToDA_Failure(t *testing.T) { }) } } + +func TestSubmitHeadersToDA_Success(t *testing.T) { + da := &mocks.DA{} + m := newTestManagerWithDA(t, da) + // Prepare a mock PendingHeaders with test data + m.pendingHeaders = newPendingBlocks(t) + + // Fill the pending headers with mock block data + fillWithBlockData(context.Background(), t, m.pendingHeaders, "Test Submitting Headers") + + // Simulate DA layer successfully accepting the header submission + da.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). + Return([]coreda.ID{[]byte("id")}, nil) + + // Call submitHeadersToDA and expect no error + err := m.submitHeadersToDA(context.Background()) + assert.NoError(t, err) +} + +// TestSubmitHeadersToDA_Failure verifies that submitHeadersToDA returns an error for various DA failures. +func TestSubmitHeadersToDA_Failure(t *testing.T) { + da := &mocks.DA{} + m := newTestManagerWithDA(t, da) + // Prepare a mock PendingHeaders with test data + m.pendingHeaders = newPendingBlocks(t) + + // Table-driven test for different DA error scenarios + testCases := []struct { + name string + daError error + }{ + {"AlreadyInMempool", coreda.ErrTxAlreadyInMempool}, + {"TimedOut", coreda.ErrTxTimedOut}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + // Fill the pending headers with mock block data for each subtest + fillWithBlockData(context.Background(), t, m.pendingHeaders, "Test Submitting Headers") + // Reset mock expectations for each error scenario + da.ExpectedCalls = nil + // Simulate DA layer returning a specific error + da.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). + Return(nil, tc.daError) + + // Call submitHeadersToDA and expect an error + err := m.submitHeadersToDA(context.Background()) + assert.Error(t, err, "expected error for DA error: %v", tc.daError) + assert.Contains(t, err.Error(), "failed to submit all headers to DA layer") + }) + } +} From 5a7fd4ab5aa23e170ee9f05304db6fc46cfd60bf Mon Sep 17 00:00:00 2001 From: pthmas <9058370+pthmas@users.noreply.github.com> Date: Tue, 27 May 2025 19:04:25 +0200 Subject: [PATCH 2/4] change test to pass linter --- pkg/cache/cache_test.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/pkg/cache/cache_test.go b/pkg/cache/cache_test.go index 63d02dd4b0..489ea24338 100644 --- a/pkg/cache/cache_test.go +++ b/pkg/cache/cache_test.go @@ -15,18 +15,10 @@ import ( // TestNewCache verifies that NewCache initializes correctly func TestNewCache(t *testing.T) { cache := NewCache[string]() - if cache == nil { - t.Fatal("NewCache returned nil") - } - if cache.items == nil { - t.Error("items map not initialized") - } - if cache.hashes == nil { - t.Error("hashes map not initialized") - } - if cache.daIncluded == nil { - t.Error("daIncluded map not initialized") - } + require.NotNil(t, cache, "NewCache returned nil") + assert.NotNil(t, cache.items, "items map not initialized") + assert.NotNil(t, cache.hashes, "hashes map not initialized") + assert.NotNil(t, cache.daIncluded, "daIncluded map not initialized") } // TestCacheItemOperations tests the item-related operations (Get, Set, Delete) From d75805dc171dd1161f88c20893b8db6e3692a6f3 Mon Sep 17 00:00:00 2001 From: pthmas <9058370+pthmas@users.noreply.github.com> Date: Wed, 28 May 2025 10:47:06 +0200 Subject: [PATCH 3/4] validate gas price during tests --- block/submitter_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/block/submitter_test.go b/block/submitter_test.go index f9b5ec6dca..59d576f92b 100644 --- a/block/submitter_test.go +++ b/block/submitter_test.go @@ -120,13 +120,23 @@ func TestSubmitHeadersToDA_Failure(t *testing.T) { // Reset mock expectations for each error scenario da.ExpectedCalls = nil // Simulate DA layer returning a specific error + var gasPriceHistory []float64 da.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). + Run(func(args mock.Arguments) { gasPriceHistory = append(gasPriceHistory, args.Get(2).(float64)) }). //save the gas price to verify it later Return(nil, tc.daError) // Call submitHeadersToDA and expect an error err := m.submitHeadersToDA(context.Background()) assert.Error(t, err, "expected error for DA error: %v", tc.daError) assert.Contains(t, err.Error(), "failed to submit all headers to DA layer") + + // Validate that gas price increased according to gas multiplier + previousGasPrice := m.gasPrice + assert.Equal(t, gasPriceHistory[0], m.gasPrice) // verify that the first call is done with the right price + for _, gasPrice := range gasPriceHistory[1:] { + assert.Equal(t, gasPrice, previousGasPrice*m.gasMultiplier) + previousGasPrice = gasPrice + } }) } } From 560cc0396250ebd76031216f1d80b0d091069db6 Mon Sep 17 00:00:00 2001 From: pthmas <9058370+pthmas@users.noreply.github.com> Date: Wed, 28 May 2025 15:30:50 +0200 Subject: [PATCH 4/4] add gas handling to tests for batch --- block/submitter_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/block/submitter_test.go b/block/submitter_test.go index 59d576f92b..0286a6bb93 100644 --- a/block/submitter_test.go +++ b/block/submitter_test.go @@ -67,14 +67,24 @@ func TestSubmitBatchToDA_Failure(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { // Reset mock expectations for each error scenario + var gasPriceHistory []float64 da.ExpectedCalls = nil da.On("GasMultiplier", mock.Anything).Return(2.0, nil) da.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). + Run(func(args mock.Arguments) { gasPriceHistory = append(gasPriceHistory, args.Get(2).(float64)) }). //save the gas price to verify it later Return(nil, tc.daError) // Expect an error from submitBatchToDA err := m.submitBatchToDA(context.Background(), batch) assert.Error(t, err, "expected error") + + // Validate that gas price increased according to gas multiplier + previousGasPrice := m.gasPrice + assert.Equal(t, gasPriceHistory[0], m.gasPrice) // verify that the first call is done with the right price + for _, gasPrice := range gasPriceHistory[1:] { + assert.Equal(t, gasPrice, previousGasPrice*m.gasMultiplier) + previousGasPrice = gasPrice + } }) } }