|
| 1 | +package test_l2_shared |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/0xPolygonHermez/zkevm-node/jsonrpc/types" |
| 9 | + "github.com/0xPolygonHermez/zkevm-node/state" |
| 10 | + "github.com/0xPolygonHermez/zkevm-node/synchronizer/common" |
| 11 | + mock_syncinterfaces "github.com/0xPolygonHermez/zkevm-node/synchronizer/common/syncinterfaces/mocks" |
| 12 | + "github.com/0xPolygonHermez/zkevm-node/synchronizer/l2_sync/l2_shared" |
| 13 | + l2sharedmocks "github.com/0xPolygonHermez/zkevm-node/synchronizer/l2_sync/l2_shared/mocks" |
| 14 | + syncMocks "github.com/0xPolygonHermez/zkevm-node/synchronizer/mocks" |
| 15 | + "github.com/stretchr/testify/mock" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +type testDataTrustedBatchRetrieve struct { |
| 20 | + mockBatchProcessor *l2sharedmocks.BatchProcessor |
| 21 | + mockZkEVMClient *mock_syncinterfaces.ZKEVMClientTrustedBatchesGetter |
| 22 | + mockState *l2sharedmocks.StateInterface |
| 23 | + mockSync *mock_syncinterfaces.SynchronizerFlushIDManager |
| 24 | + mockTimer *common.MockTimerProvider |
| 25 | + mockDbTx *syncMocks.DbTxMock |
| 26 | + TrustedStateMngr *l2_shared.TrustedStateManager |
| 27 | + sut *l2_shared.TrustedBatchesRetrieve |
| 28 | + ctx context.Context |
| 29 | +} |
| 30 | + |
| 31 | +func newTestDataTrustedBatchRetrieve(t *testing.T) *testDataTrustedBatchRetrieve { |
| 32 | + mockBatchProcessor := l2sharedmocks.NewBatchProcessor(t) |
| 33 | + mockZkEVMClient := mock_syncinterfaces.NewZKEVMClientTrustedBatchesGetter(t) |
| 34 | + mockState := l2sharedmocks.NewStateInterface(t) |
| 35 | + mockSync := mock_syncinterfaces.NewSynchronizerFlushIDManager(t) |
| 36 | + mockTimer := &common.MockTimerProvider{} |
| 37 | + mockDbTx := syncMocks.NewDbTxMock(t) |
| 38 | + TrustedStateMngr := l2_shared.NewTrustedStateManager(mockTimer, 0) |
| 39 | + sut := l2_shared.NewTrustedBatchesRetrieve(mockBatchProcessor, mockZkEVMClient, mockState, mockSync, *TrustedStateMngr) |
| 40 | + ctx := context.TODO() |
| 41 | + return &testDataTrustedBatchRetrieve{ |
| 42 | + mockBatchProcessor: mockBatchProcessor, |
| 43 | + mockZkEVMClient: mockZkEVMClient, |
| 44 | + mockState: mockState, |
| 45 | + mockSync: mockSync, |
| 46 | + mockTimer: mockTimer, |
| 47 | + mockDbTx: mockDbTx, |
| 48 | + TrustedStateMngr: TrustedStateMngr, |
| 49 | + sut: sut, |
| 50 | + ctx: ctx, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +const ( |
| 55 | + closedBatch = true |
| 56 | + notClosedBatch = false |
| 57 | +) |
| 58 | + |
| 59 | +// This test must do from 100 to 104. |
| 60 | +// But the batch 100 is open on TrustedNode so it stop processing |
| 61 | +func TestSyncTrustedBatchesToFromStopAfterFirstWIPBatch(t *testing.T) { |
| 62 | + data := newTestDataTrustedBatchRetrieve(t) |
| 63 | + data.mockZkEVMClient.EXPECT().BatchNumber(data.ctx).Return(uint64(102), nil) |
| 64 | + |
| 65 | + expectationsForSyncTrustedStateIteration(t, 100, notClosedBatch, data) |
| 66 | + |
| 67 | + err := data.sut.SyncTrustedState(data.ctx, 100, 104) |
| 68 | + require.NoError(t, err) |
| 69 | +} |
| 70 | + |
| 71 | +// This must process 100 (that is closed) |
| 72 | +// and stop processing at 101 because is not yet close this batch |
| 73 | +func TestSyncTrustedBatchesToFromStopAfterFirstWIPBatchCase2(t *testing.T) { |
| 74 | + data := newTestDataTrustedBatchRetrieve(t) |
| 75 | + data.mockZkEVMClient.EXPECT().BatchNumber(data.ctx).Return(uint64(102), nil) |
| 76 | + |
| 77 | + expectationsForSyncTrustedStateIteration(t, 100, closedBatch, data) |
| 78 | + expectationsForSyncTrustedStateIteration(t, 101, notClosedBatch, data) |
| 79 | + |
| 80 | + err := data.sut.SyncTrustedState(data.ctx, 100, 104) |
| 81 | + require.NoError(t, err) |
| 82 | +} |
| 83 | + |
| 84 | +// This test must do from 100 to 102. Is for check manually that the logs |
| 85 | +// That is not tested but must not emit the log: |
| 86 | +// - Batch 101 is not closed. so we break synchronization from Trusted Node because can only have 1 WIP batch on state |
| 87 | +func TestSyncTrustedBatchesToFromStopAfterFirstWIPBatchCase3(t *testing.T) { |
| 88 | + data := newTestDataTrustedBatchRetrieve(t) |
| 89 | + data.mockZkEVMClient.EXPECT().BatchNumber(data.ctx).Return(uint64(102), nil) |
| 90 | + expectationsForSyncTrustedStateIteration(t, 100, closedBatch, data) |
| 91 | + expectationsForSyncTrustedStateIteration(t, 101, closedBatch, data) |
| 92 | + expectationsForSyncTrustedStateIteration(t, 102, notClosedBatch, data) |
| 93 | + |
| 94 | + err := data.sut.SyncTrustedState(data.ctx, 100, 102) |
| 95 | + require.NoError(t, err) |
| 96 | +} |
| 97 | + |
| 98 | +func expectationsForSyncTrustedStateIteration(t *testing.T, batchNumber uint64, closed bool, data *testDataTrustedBatchRetrieve) { |
| 99 | + batch100 := &types.Batch{ |
| 100 | + Number: types.ArgUint64(batchNumber), |
| 101 | + Closed: closed, |
| 102 | + } |
| 103 | + data.mockZkEVMClient.EXPECT().BatchByNumber(data.ctx, big.NewInt(0).SetUint64(batchNumber)).Return(batch100, nil) |
| 104 | + data.mockState.EXPECT().BeginStateTransaction(data.ctx).Return(data.mockDbTx, nil) |
| 105 | + // Get Previous Batch 99 from State |
| 106 | + stateBatch99 := &state.Batch{ |
| 107 | + BatchNumber: batchNumber - 1, |
| 108 | + } |
| 109 | + data.mockState.EXPECT().GetBatchByNumber(data.ctx, uint64(batchNumber-1), data.mockDbTx).Return(stateBatch99, nil) |
| 110 | + stateBatch100 := &state.Batch{ |
| 111 | + BatchNumber: batchNumber, |
| 112 | + } |
| 113 | + data.mockState.EXPECT().GetBatchByNumber(data.ctx, uint64(batchNumber), data.mockDbTx).Return(stateBatch100, nil) |
| 114 | + data.mockBatchProcessor.EXPECT().ProcessTrustedBatch(data.ctx, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, nil) |
| 115 | + data.mockSync.EXPECT().CheckFlushID(mock.Anything).Return(nil) |
| 116 | + data.mockDbTx.EXPECT().Commit(data.ctx).Return(nil) |
| 117 | +} |
0 commit comments