|
| 1 | +package block |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/rollkit/rollkit/types" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGetPendingBlocks(t *testing.T) { |
| 13 | + require := require.New(t) |
| 14 | + pb := NewPendingBlocks() |
| 15 | + for i := uint64(0); i < 5; i++ { |
| 16 | + pb.addPendingBlock(types.GetRandomBlock(i, 0)) |
| 17 | + } |
| 18 | + blocks := pb.getPendingBlocks() |
| 19 | + require.True(sort.SliceIsSorted(blocks, func(i, j int) bool { |
| 20 | + return blocks[i].Height() < blocks[j].Height() |
| 21 | + })) |
| 22 | +} |
| 23 | + |
| 24 | +func TestRemoveSubmittedBlocks(t *testing.T) { |
| 25 | + require := require.New(t) |
| 26 | + pb := NewPendingBlocks() |
| 27 | + for i := uint64(0); i < 5; i++ { |
| 28 | + pb.addPendingBlock(types.GetRandomBlock(i, 0)) |
| 29 | + } |
| 30 | + blocks := pb.getPendingBlocks() |
| 31 | + pb.removeSubmittedBlocks(blocks) |
| 32 | + require.True(pb.isEmpty()) |
| 33 | +} |
| 34 | + |
| 35 | +func TestRemoveSubsetOfBlocks(t *testing.T) { |
| 36 | + require := require.New(t) |
| 37 | + pb := NewPendingBlocks() |
| 38 | + for i := uint64(0); i < 5; i++ { |
| 39 | + pb.addPendingBlock(types.GetRandomBlock(i, 0)) |
| 40 | + } |
| 41 | + // Remove blocks with height 1 and 2 |
| 42 | + pb.removeSubmittedBlocks([]*types.Block{ |
| 43 | + types.GetRandomBlock(1, 0), |
| 44 | + types.GetRandomBlock(2, 0), |
| 45 | + }) |
| 46 | + remainingBlocks := pb.getPendingBlocks() |
| 47 | + require.Len(remainingBlocks, 3, "There should be 3 blocks remaining") |
| 48 | + for _, block := range remainingBlocks { |
| 49 | + require.Contains([]uint64{0, 3, 4}, block.Height(), "Only blocks with height 0, 3, and 4 should remain") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestRemoveAllBlocksAndVerifyEmpty(t *testing.T) { |
| 54 | + require := require.New(t) |
| 55 | + pb := NewPendingBlocks() |
| 56 | + for i := uint64(0); i < 5; i++ { |
| 57 | + pb.addPendingBlock(types.GetRandomBlock(i, 0)) |
| 58 | + } |
| 59 | + // Remove all blocks |
| 60 | + pb.removeSubmittedBlocks(pb.getPendingBlocks()) |
| 61 | + require.True(pb.isEmpty(), "PendingBlocks should be empty after removing all blocks") |
| 62 | +} |
| 63 | + |
| 64 | +func TestRemoveBlocksFromEmptyPendingBlocks(t *testing.T) { |
| 65 | + require := require.New(t) |
| 66 | + pb := NewPendingBlocks() |
| 67 | + // Attempt to remove blocks from an empty PendingBlocks |
| 68 | + require.NotPanics(func() { |
| 69 | + pb.removeSubmittedBlocks([]*types.Block{ |
| 70 | + types.GetRandomBlock(1, 0), |
| 71 | + types.GetRandomBlock(2, 0), |
| 72 | + }) |
| 73 | + }, "Removing blocks from an empty PendingBlocks should not cause a panic") |
| 74 | +} |
0 commit comments