Skip to content

Commit 8ff9810

Browse files
authored
cmd/devp2p: fix flakey tests in CI (#22757)
This PR fixes a couple of issues in the eth test suite that caused flakiness when run in the CI.
1 parent afc1abd commit 8ff9810

4 files changed

Lines changed: 32 additions & 37 deletions

File tree

‎cmd/devp2p/internal/ethtest/eth66_suite.go‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,7 @@ func (s *Suite) TestLargeAnnounce_66(t *utesting.T) {
217217
sendConn.Close()
218218
}
219219
// Test the last block as a valid block
220-
sendConn, receiveConn := s.setupConnection66(t), s.setupConnection66(t)
221-
defer sendConn.Close()
222-
defer receiveConn.Close()
223-
224-
s.testAnnounce66(t, sendConn, receiveConn, blocks[3])
225-
// update test suite chain
226-
s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
227-
// wait for client to update its chain
228-
if err := receiveConn.waitForBlock66(s.fullChain.blocks[nextBlock]); err != nil {
229-
t.Fatal(err)
230-
}
220+
s.sendNextBlock66(t)
231221
}
232222

233223
func (s *Suite) TestOldAnnounce_66(t *utesting.T) {

‎cmd/devp2p/internal/ethtest/eth66_suiteHelpers.go‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,11 @@ func (s *Suite) waitAnnounce66(t *utesting.T, conn *Conn, blockAnnouncement *New
229229
func (c *Conn) waitForBlock66(block *types.Block) error {
230230
defer c.SetReadDeadline(time.Time{})
231231

232-
timeout := time.Now().Add(20 * time.Second)
233-
c.SetReadDeadline(timeout)
232+
c.SetReadDeadline(time.Now().Add(20 * time.Second))
233+
// note: if the node has not yet imported the block, it will respond
234+
// to the GetBlockHeaders request with an empty BlockHeaders response,
235+
// so the GetBlockHeaders request must be sent again until the BlockHeaders
236+
// response contains the desired header.
234237
for {
235238
req := eth.GetBlockHeadersPacket66{
236239
RequestId: 54,
@@ -253,8 +256,10 @@ func (c *Conn) waitForBlock66(block *types.Block) error {
253256
if reqID != req.RequestId {
254257
return fmt.Errorf("request ID mismatch: wanted %d, got %d", req.RequestId, reqID)
255258
}
256-
if len(msg) > 0 {
257-
return nil
259+
for _, header := range msg {
260+
if header.Number.Uint64() == block.NumberU64() {
261+
return nil
262+
}
258263
}
259264
time.Sleep(100 * time.Millisecond)
260265
case *NewPooledTransactionHashes:
@@ -319,10 +324,10 @@ func (s *Suite) sendNextBlock66(t *utesting.T) {
319324
}
320325
// send announcement and wait for node to request the header
321326
s.testAnnounce66(t, sendConn, receiveConn, blockAnnouncement)
322-
// update test suite chain
323-
s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
324327
// wait for client to update its chain
325-
if err := receiveConn.waitForBlock66(s.chain.Head()); err != nil {
328+
if err := receiveConn.waitForBlock66(s.fullChain.blocks[nextBlock]); err != nil {
326329
t.Fatal(err)
327330
}
331+
// update test suite chain
332+
s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
328333
}

‎cmd/devp2p/internal/ethtest/suite.go‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -260,22 +260,28 @@ func (s *Suite) TestGetBlockBodies(t *utesting.T) {
260260
// TestBroadcast tests whether a block announcement is correctly
261261
// propagated to the given node's peer(s).
262262
func (s *Suite) TestBroadcast(t *utesting.T) {
263+
s.sendNextBlock(t)
264+
}
265+
266+
func (s *Suite) sendNextBlock(t *utesting.T) {
263267
sendConn, receiveConn := s.setupConnection(t), s.setupConnection(t)
264268
defer sendConn.Close()
265269
defer receiveConn.Close()
266270

271+
// create new block announcement
267272
nextBlock := len(s.chain.blocks)
268273
blockAnnouncement := &NewBlock{
269274
Block: s.fullChain.blocks[nextBlock],
270275
TD: s.fullChain.TD(nextBlock + 1),
271276
}
277+
// send announcement and wait for node to request the header
272278
s.testAnnounce(t, sendConn, receiveConn, blockAnnouncement)
273-
// update test suite chain
274-
s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
275279
// wait for client to update its chain
276-
if err := receiveConn.waitForBlock(s.chain.Head()); err != nil {
280+
if err := receiveConn.waitForBlock(s.fullChain.blocks[nextBlock]); err != nil {
277281
t.Fatal(err)
278282
}
283+
// update test suite chain
284+
s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
279285
}
280286

281287
// TestMaliciousHandshake tries to send malicious data during the handshake.
@@ -394,18 +400,7 @@ func (s *Suite) TestLargeAnnounce(t *utesting.T) {
394400
sendConn.Close()
395401
}
396402
// Test the last block as a valid block
397-
sendConn := s.setupConnection(t)
398-
receiveConn := s.setupConnection(t)
399-
defer sendConn.Close()
400-
defer receiveConn.Close()
401-
402-
s.testAnnounce(t, sendConn, receiveConn, blocks[3])
403-
// update test suite chain
404-
s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
405-
// wait for client to update its chain
406-
if err := receiveConn.waitForBlock(s.fullChain.blocks[nextBlock]); err != nil {
407-
t.Fatal(err)
408-
}
403+
s.sendNextBlock(t)
409404
}
410405

411406
func (s *Suite) TestOldAnnounce(t *utesting.T) {

‎cmd/devp2p/internal/ethtest/types.go‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,22 @@ loop:
334334
func (c *Conn) waitForBlock(block *types.Block) error {
335335
defer c.SetReadDeadline(time.Time{})
336336

337-
timeout := time.Now().Add(20 * time.Second)
338-
c.SetReadDeadline(timeout)
337+
c.SetReadDeadline(time.Now().Add(20 * time.Second))
338+
// note: if the node has not yet imported the block, it will respond
339+
// to the GetBlockHeaders request with an empty BlockHeaders response,
340+
// so the GetBlockHeaders request must be sent again until the BlockHeaders
341+
// response contains the desired header.
339342
for {
340343
req := &GetBlockHeaders{Origin: eth.HashOrNumber{Hash: block.Hash()}, Amount: 1}
341344
if err := c.Write(req); err != nil {
342345
return err
343346
}
344347
switch msg := c.Read().(type) {
345348
case *BlockHeaders:
346-
if len(*msg) > 0 {
347-
return nil
349+
for _, header := range *msg {
350+
if header.Number.Uint64() == block.NumberU64() {
351+
return nil
352+
}
348353
}
349354
time.Sleep(100 * time.Millisecond)
350355
default:

0 commit comments

Comments
 (0)