|
| 1 | +package block |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "cosmossdk.io/log" |
| 8 | + ds "github.com/ipfs/go-datastore" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/mock" |
| 11 | + |
| 12 | + coreda "github.com/rollkit/rollkit/core/da" |
| 13 | + coresequencer "github.com/rollkit/rollkit/core/sequencer" |
| 14 | + "github.com/rollkit/rollkit/pkg/cache" |
| 15 | + "github.com/rollkit/rollkit/pkg/config" |
| 16 | + "github.com/rollkit/rollkit/test/mocks" |
| 17 | + "github.com/rollkit/rollkit/types" |
| 18 | +) |
| 19 | + |
| 20 | +func newTestManagerWithDA(t *testing.T, da *mocks.DA) (m *Manager, mockStore *mocks.Store) { |
| 21 | + logger := log.NewNopLogger() |
| 22 | + nodeConf := config.DefaultConfig |
| 23 | + mockStore = mocks.NewStore(t) |
| 24 | + |
| 25 | + // Mock initial metadata reads during manager creation if necessary |
| 26 | + mockStore.On("GetMetadata", mock.Anything, DAIncludedHeightKey).Return(nil, ds.ErrNotFound).Maybe() |
| 27 | + mockStore.On("GetMetadata", mock.Anything, LastBatchDataKey).Return(nil, ds.ErrNotFound).Maybe() |
| 28 | + mockStore.On("GetMetadata", mock.Anything, LastSubmittedHeightKey).Return(nil, ds.ErrNotFound).Maybe() |
| 29 | + |
| 30 | + return &Manager{ |
| 31 | + store: mockStore, |
| 32 | + da: da, |
| 33 | + logger: logger, |
| 34 | + config: nodeConf, |
| 35 | + gasPrice: 1.0, |
| 36 | + gasMultiplier: 2.0, |
| 37 | + headerCache: cache.NewCache[types.SignedHeader](), |
| 38 | + dataCache: cache.NewCache[types.Data](), |
| 39 | + }, mockStore |
| 40 | +} |
| 41 | + |
| 42 | +func TestSubmitBatchToDA_Success(t *testing.T) { |
| 43 | + da := &mocks.DA{} |
| 44 | + m, _ := newTestManagerWithDA(t, da) |
| 45 | + |
| 46 | + batch := coresequencer.Batch{Transactions: [][]byte{[]byte("tx1"), []byte("tx2")}} |
| 47 | + |
| 48 | + // Simulate DA success |
| 49 | + da.On("GasMultiplier", mock.Anything).Return(2.0, nil) |
| 50 | + da.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). |
| 51 | + Return([]coreda.ID{[]byte("id")}, nil) |
| 52 | + |
| 53 | + err := m.submitBatchToDA(context.Background(), batch) |
| 54 | + assert.NoError(t, err) |
| 55 | +} |
| 56 | + |
| 57 | +func TestSubmitBatchToDA_AlreadyInMempool(t *testing.T) { |
| 58 | + da := &mocks.DA{} |
| 59 | + m, _ := newTestManagerWithDA(t, da) |
| 60 | + |
| 61 | + batch := coresequencer.Batch{Transactions: [][]byte{[]byte("tx1"), []byte("tx2")}} |
| 62 | + |
| 63 | + // Simulate DA success |
| 64 | + da.On("GasMultiplier", mock.Anything).Return(2.0, nil) |
| 65 | + da.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). |
| 66 | + Return(nil, coreda.ErrTxAlreadyInMempool) |
| 67 | + |
| 68 | + err := m.submitBatchToDA(context.Background(), batch) |
| 69 | + assert.Error(t, err) |
| 70 | + assert.EqualError(t, err, "failed to submit all transactions to DA layer, submitted 0 txs (2 left) after 30 attempts") |
| 71 | +} |
| 72 | + |
| 73 | +func TestSubmitHeadersToDA_Success(t *testing.T) { |
| 74 | + da := &mocks.DA{} |
| 75 | + m, mockStore := newTestManagerWithDA(t, da) |
| 76 | + |
| 77 | + // Prepare a mock header |
| 78 | + pendingHeaders, _ := NewPendingHeaders(m.store, m.logger) |
| 79 | + m.pendingHeaders = pendingHeaders |
| 80 | + |
| 81 | + // Simulate DA success |
| 82 | + da.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). |
| 83 | + Return([]coreda.ID{[]byte("id")}, nil) |
| 84 | + da.On("GasMultiplier", mock.Anything).Return(2.0, nil) |
| 85 | + mockStore.On("Height", mock.Anything).Return(uint64(10)) |
| 86 | + |
| 87 | + // Call submitHeadersToDA |
| 88 | + err := m.submitHeadersToDA(context.Background()) |
| 89 | + assert.NoError(t, err) |
| 90 | +} |
0 commit comments