Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions block/submitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ func TestSubmitDataToDA_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)

pubKey, err := m.signer.GetPublic()
Expand Down Expand Up @@ -133,6 +135,76 @@ func TestSubmitDataToDA_Failure(t *testing.T) {
// Expect an error from submitDataToDA
err = m.submitDataToDA(context.Background(), &signedData)
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
}
})
}
}

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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add a test case for gas handling.

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
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
}
})
}
}
Expand Down
16 changes: 4 additions & 12 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading