@@ -22,8 +22,9 @@ func TestPendingBlocksSuite(t *testing.T) {
2222}
2323
2424func (suite * PendingBlocksSuite ) SetupTest () {
25- // Initialize with finalized view 0
26- suite .buffer = NewPendingBlocks (0 )
25+ // Initialize with finalized view 0 and no view range limitation (0 = no limit)
26+ // Individual tests that need the limitation will create their own buffers
27+ suite .buffer = NewPendingBlocks (0 , 0 )
2728}
2829
2930// block creates a new block proposal wrapped as Slashable.
@@ -41,7 +42,7 @@ func (suite *PendingBlocksSuite) blockWithParent(parent *flow.Header) flow.Slash
4142// TestAdd tests adding blocks to the buffer.
4243func (suite * PendingBlocksSuite ) TestAdd () {
4344 block := suite .block ()
44- suite .buffer .Add (block )
45+ suite .Require (). NoError ( suite . buffer .Add (block ) )
4546
4647 // Verify block can be retrieved by ID
4748 retrieved , ok := suite .buffer .ByID (block .Message .Block .ID ())
@@ -59,8 +60,8 @@ func (suite *PendingBlocksSuite) TestAdd() {
5960// TestAddDuplicate verifies that adding the same block twice is a no-op.
6061func (suite * PendingBlocksSuite ) TestAddDuplicate () {
6162 block := suite .block ()
62- suite .buffer .Add (block )
63- suite .buffer .Add (block ) // Add again
63+ suite .Require (). NoError ( suite . buffer .Add (block ) )
64+ suite .Require (). NoError ( suite . buffer .Add (block ) ) // Add again
6465
6566 // Should still only have one block
6667 suite .Assert ().Equal (uint (1 ), suite .buffer .Size ())
@@ -74,24 +75,88 @@ func (suite *PendingBlocksSuite) TestAddDuplicate() {
7475// TestAddBelowFinalizedView verifies that adding blocks below finalized view is a no-op.
7576func (suite * PendingBlocksSuite ) TestAddBelowFinalizedView () {
7677 finalizedView := uint64 (1000 )
77- buffer := NewPendingBlocks (finalizedView )
78+ buffer := NewPendingBlocks (finalizedView , 100_000 )
7879
7980 // Create a block with view below finalized
8081 block := suite .block ()
8182 block .Message .Block .ParentView = finalizedView - 10
8283 block .Message .Block .View = finalizedView - 5
8384
84- buffer .Add (block )
85+ suite . Require (). NoError ( buffer .Add (block ) )
8586
8687 _ , ok := buffer .ByID (block .Message .Block .ID ())
8788 suite .Assert ().False (ok )
8889 suite .Assert ().Equal (uint (0 ), buffer .Size ())
8990}
9091
92+ // TestAddExceedsActiveViewRangeSize verifies that adding blocks that exceed the active view range size returns an error.
93+ func (suite * PendingBlocksSuite ) TestAddExceedsActiveViewRangeSize () {
94+ finalizedView := uint64 (1000 )
95+ activeViewRangeSize := uint64 (100 )
96+ buffer := NewPendingBlocks (finalizedView , activeViewRangeSize )
97+
98+ // Create a parent header and then a block that exceeds the active view range size
99+ parentHeader := unittest .BlockHeaderFixture ()
100+ parentHeader .View = finalizedView + 50
101+ block := suite .blockWithParent (parentHeader )
102+ block .Message .Block .View = finalizedView + activeViewRangeSize + 1
103+
104+ err := buffer .Add (block )
105+ suite .Assert ().Error (err )
106+ suite .Assert ().True (mempool .IsBeyondActiveRangeError (err ))
107+
108+ // Verify block was not added
109+ _ , ok := buffer .ByID (block .Message .Block .ID ())
110+ suite .Assert ().False (ok )
111+ suite .Assert ().Equal (uint (0 ), buffer .Size ())
112+ }
113+
114+ // TestAddWithinActiveViewRangeSize verifies that adding blocks within the active view range size succeeds.
115+ func (suite * PendingBlocksSuite ) TestAddWithinActiveViewRangeSize () {
116+ finalizedView := uint64 (1000 )
117+ activeViewRangeSize := uint64 (100 )
118+ buffer := NewPendingBlocks (finalizedView , activeViewRangeSize )
119+
120+ // Create a parent header and then a block that is exactly at the limit
121+ parentHeader := unittest .BlockHeaderFixture ()
122+ parentHeader .View = finalizedView + 50
123+ block := suite .blockWithParent (parentHeader )
124+ block .Message .Block .View = finalizedView + activeViewRangeSize
125+
126+ err := buffer .Add (block )
127+ suite .Assert ().NoError (err )
128+
129+ // Verify block was added
130+ _ , ok := buffer .ByID (block .Message .Block .ID ())
131+ suite .Assert ().True (ok )
132+ suite .Assert ().Equal (uint (1 ), buffer .Size ())
133+ }
134+
135+ // TestAddWithZeroActiveViewRangeSize verifies that when activeViewRangeSize is 0, there's no limitation.
136+ func (suite * PendingBlocksSuite ) TestAddWithZeroActiveViewRangeSize () {
137+ finalizedView := uint64 (1000 )
138+ activeViewRangeSize := uint64 (0 ) // No limitation
139+ buffer := NewPendingBlocks (finalizedView , activeViewRangeSize )
140+
141+ // Create a parent header and then a block that is very far ahead
142+ parentHeader := unittest .BlockHeaderFixture ()
143+ parentHeader .View = finalizedView + 500_000
144+ block := suite .blockWithParent (parentHeader )
145+ block .Message .Block .View = finalizedView + 1_000_000
146+
147+ err := buffer .Add (block )
148+ suite .Assert ().NoError (err )
149+
150+ // Verify block was added
151+ _ , ok := buffer .ByID (block .Message .Block .ID ())
152+ suite .Assert ().True (ok )
153+ suite .Assert ().Equal (uint (1 ), buffer .Size ())
154+ }
155+
91156// TestByID tests retrieving blocks by ID.
92157func (suite * PendingBlocksSuite ) TestByID () {
93158 block := suite .block ()
94- suite .buffer .Add (block )
159+ suite .Require (). NoError ( suite . buffer .Add (block ) )
95160
96161 // Test retrieving existing block
97162 retrieved , ok := suite .buffer .ByID (block .Message .Block .ID ())
@@ -173,7 +238,7 @@ func (suite *PendingBlocksSuite) TestPruneByView() {
173238 // 10% of the time, add a new unrelated block
174239 if i % 10 == 0 {
175240 block := suite .block ()
176- suite .buffer .Add (block )
241+ suite .Require (). NoError ( suite . buffer .Add (block ) )
177242 blocks = append (blocks , block )
178243 continue
179244 }
@@ -182,7 +247,7 @@ func (suite *PendingBlocksSuite) TestPruneByView() {
182247 if i % 2 == 1 && len (blocks ) > 0 {
183248 parent := blocks [rand .Intn (len (blocks ))]
184249 block := suite .blockWithParent (parent .Message .Block .ToHeader ())
185- suite .buffer .Add (block )
250+ suite .Require (). NoError ( suite . buffer .Add (block ) )
186251 blocks = append (blocks , block )
187252 }
188253 }
@@ -210,13 +275,13 @@ func (suite *PendingBlocksSuite) TestPruneByView() {
210275// TestPruneByViewBelowFinalizedView verifies that pruning below finalized view returns an error.
211276func (suite * PendingBlocksSuite ) TestPruneByViewBelowFinalizedView () {
212277 finalizedView := uint64 (100 )
213- buffer := NewPendingBlocks (finalizedView )
278+ buffer := NewPendingBlocks (finalizedView , 100_000 )
214279
215280 // Add some blocks above finalized view
216281 parent := unittest .BlockHeaderFixture ()
217282 parent .View = finalizedView + 10
218283 block := suite .blockWithParent (parent )
219- buffer .Add (block )
284+ suite . Require (). NoError ( buffer .Add (block ) )
220285
221286 // Prune at finalized view should succeed
222287 err := buffer .PruneByView (finalizedView )
@@ -305,7 +370,7 @@ func (suite *PendingBlocksSuite) TestConcurrentAccess() {
305370 defer wg .Done ()
306371 for j := 0 ; j < blocksPerGoroutine ; j ++ {
307372 block := suite .block ()
308- suite .buffer .Add (block )
373+ suite .Require (). NoError ( suite . buffer .Add (block ) )
309374 }
310375 }()
311376 }
0 commit comments