Skip to content

Commit

Permalink
fix unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Aug 28, 2024
1 parent 2a803c0 commit fbbd3a9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
56 changes: 50 additions & 6 deletions state/model/batch_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ func TestOnSequencedBatchesOnL1HappyPath(t *testing.T) {
dbTx := mock_entities.NewTx(t)

seq := model.SequenceOfBatches{
Sequence: entities.SequencedBatches{},
Batches: []*entities.VirtualBatch{},
Sequence: entities.SequencedBatches{
FromBatchNumber: 2,
ToBatchNumber: 2,
},
Batches: []*entities.VirtualBatch{},
}
previousSeq := entities.SequencedBatches{
FromBatchNumber: 1,
ToBatchNumber: 1,
}
seq.Batches = append(seq.Batches, &entities.VirtualBatch{})
mockStorage.EXPECT().AddSequencedBatches(ctx, &seq.Sequence, dbTx).Return(nil)
mockStorage.EXPECT().AddVirtualBatch(ctx, seq.Batches[0], dbTx).Return(nil)
mockStorage.EXPECT().GetLatestSequence(ctx, dbTx).Return(&previousSeq, nil)
err := sut.OnSequencedBatchesOnL1(ctx, seq, dbTx)
require.NoError(t, err)
}
Expand All @@ -35,11 +43,19 @@ func TestOnSequencedBatchesOnFailStoreSeq(t *testing.T) {
dbTx := mock_entities.NewTx(t)

seq := model.SequenceOfBatches{
Sequence: entities.SequencedBatches{},
Batches: []*entities.VirtualBatch{},
Sequence: entities.SequencedBatches{
FromBatchNumber: 2,
ToBatchNumber: 2,
},
Batches: []*entities.VirtualBatch{},
}
previousSeq := entities.SequencedBatches{
FromBatchNumber: 1,
ToBatchNumber: 1,
}
mockStorage.EXPECT().AddSequencedBatches(ctx, &seq.Sequence, dbTx).Return(entities.ErrAlreadyExists)
mockStorage.EXPECT().GetSequenceByBatchNumber(ctx, seq.Sequence.FromBatchNumber, dbTx).Return(nil, entities.ErrNotFound)
mockStorage.EXPECT().GetLatestSequence(ctx, dbTx).Return(&previousSeq, nil)
err := sut.OnSequencedBatchesOnL1(ctx, seq, dbTx)
require.Error(t, err)
}
Expand All @@ -51,11 +67,39 @@ func TestOnSequencedBatchesOnSameDataOnDB(t *testing.T) {
dbTx := mock_entities.NewTx(t)

seq := model.SequenceOfBatches{
Sequence: entities.SequencedBatches{},
Batches: []*entities.VirtualBatch{},
Sequence: entities.SequencedBatches{
FromBatchNumber: 1,
ToBatchNumber: 1,
},
Batches: []*entities.VirtualBatch{},
}

mockStorage.EXPECT().AddSequencedBatches(ctx, &seq.Sequence, dbTx).Return(entities.ErrAlreadyExists)
mockStorage.EXPECT().GetSequenceByBatchNumber(ctx, seq.Sequence.FromBatchNumber, dbTx).Return(&seq.Sequence, nil)
mockStorage.EXPECT().GetLatestSequence(ctx, dbTx).Return(nil, nil)
err := sut.OnSequencedBatchesOnL1(ctx, seq, dbTx)
require.NoError(t, err)
}

func TestOnSequencedBatchesNotContigous(t *testing.T) {
mockStorage := mock_model.NewStorageVirtualBatchInterface(t)
sut := model.NewBatchState(mockStorage)
ctx := context.TODO()
dbTx := mock_entities.NewTx(t)

seq := model.SequenceOfBatches{
Sequence: entities.SequencedBatches{
FromBatchNumber: 3,
ToBatchNumber: 3,
},
Batches: []*entities.VirtualBatch{},
}
previousSeq := entities.SequencedBatches{
FromBatchNumber: 1,
ToBatchNumber: 1,
}
seq.Batches = append(seq.Batches, &entities.VirtualBatch{})
mockStorage.EXPECT().GetLatestSequence(ctx, dbTx).Return(&previousSeq, nil)
err := sut.OnSequencedBatchesOnL1(ctx, seq, dbTx)
require.Error(t, err)
}
26 changes: 24 additions & 2 deletions state/model/rollback_batches_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestRollbackBatchesHappyPath(t *testing.T) {
}
seqs := &model.SequencesBatchesSlice{
model.SequencedBatches{
FromBatchNumber: 1,
ToBatchNumber: 123,
FromBatchNumber: 124,
ToBatchNumber: 125,
L1BlockNumber: 50,
},
}
Expand All @@ -38,3 +38,25 @@ func TestRollbackBatchesHappyPath(t *testing.T) {
require.Equal(t, request.LastBatchNumber, res.RollbackEntry.LastBatchNumber)
require.Equal(t, uint64(50), res.RollbackEntry.UndoFirstBlockNumber)
}

func TestRollbackBatchesCantBreakSequence(t *testing.T) {
storageMock := mock_model.NewStorageRollbackBatchesInterface(t)
sut := model.NewRollbackBatchesState(storageMock, false)
request := model.RollbackBatchesRequest{
LastBatchNumber: 123,
LastBatchAccInputHash: [32]byte{},
L1BlockNumber: 60,
L1BlockTimestamp: time.Now(),
}
seqs := &model.SequencesBatchesSlice{
model.SequencedBatches{
FromBatchNumber: 125,
ToBatchNumber: 126,
L1BlockNumber: 50,
},
}
dbTxMock := mock_entities.NewTx(t)
storageMock.EXPECT().GetSequencesGreatestOrEqualBatchNumber(context.TODO(), request.LastBatchNumber+1, dbTxMock).Return(seqs, nil)
_, err := sut.ExecuteRollbackBatches(context.TODO(), request, dbTxMock)
require.Error(t, err)
}

0 comments on commit fbbd3a9

Please sign in to comment.