Skip to content

Commit 27705ad

Browse files
fjllightclient
authored andcommitted
cmd/devp2p/internal/ethtest: some fixes for the eth test suite (ethereum#28996)
Improving two things here: On hive, where we look at these tests, the Go code comment above the test is not visible. When there is a failure, it's not obvious what the test is actually expecting. I have converted the comments in to printed log messages to explain the test more. Second, I noticed that besu is failing some tests because it happens to request a header when we want it to send transactions. Trying the minimal fix here to serve the headers. Co-authored-by: lightclient <[email protected]>
1 parent e3d18a4 commit 27705ad

File tree

2 files changed

+72
-43
lines changed

2 files changed

+72
-43
lines changed

cmd/devp2p/internal/ethtest/suite.go

+51-41
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,23 @@ func NewSuite(dest *enode.Node, chainDir, engineURL, jwt string) (*Suite, error)
6464
func (s *Suite) EthTests() []utesting.Test {
6565
return []utesting.Test{
6666
// status
67-
{Name: "TestStatus", Fn: s.TestStatus},
67+
{Name: "Status", Fn: s.TestStatus},
6868
// get block headers
69-
{Name: "TestGetBlockHeaders", Fn: s.TestGetBlockHeaders},
70-
{Name: "TestSimultaneousRequests", Fn: s.TestSimultaneousRequests},
71-
{Name: "TestSameRequestID", Fn: s.TestSameRequestID},
72-
{Name: "TestZeroRequestID", Fn: s.TestZeroRequestID},
69+
{Name: "GetBlockHeaders", Fn: s.TestGetBlockHeaders},
70+
{Name: "SimultaneousRequests", Fn: s.TestSimultaneousRequests},
71+
{Name: "SameRequestID", Fn: s.TestSameRequestID},
72+
{Name: "ZeroRequestID", Fn: s.TestZeroRequestID},
7373
// get block bodies
74-
{Name: "TestGetBlockBodies", Fn: s.TestGetBlockBodies},
74+
{Name: "GetBlockBodies", Fn: s.TestGetBlockBodies},
7575
// // malicious handshakes + status
76-
{Name: "TestMaliciousHandshake", Fn: s.TestMaliciousHandshake},
77-
{Name: "TestMaliciousStatus", Fn: s.TestMaliciousStatus},
76+
{Name: "MaliciousHandshake", Fn: s.TestMaliciousHandshake},
77+
{Name: "MaliciousStatus", Fn: s.TestMaliciousStatus},
7878
// test transactions
79-
{Name: "TestLargeTxRequest", Fn: s.TestLargeTxRequest, Slow: true},
80-
{Name: "TestTransaction", Fn: s.TestTransaction},
81-
{Name: "TestInvalidTxs", Fn: s.TestInvalidTxs},
82-
{Name: "TestNewPooledTxs", Fn: s.TestNewPooledTxs},
83-
{Name: "TestBlobViolations", Fn: s.TestBlobViolations},
79+
{Name: "LargeTxRequest", Fn: s.TestLargeTxRequest, Slow: true},
80+
{Name: "Transaction", Fn: s.TestTransaction},
81+
{Name: "InvalidTxs", Fn: s.TestInvalidTxs},
82+
{Name: "NewPooledTxs", Fn: s.TestNewPooledTxs},
83+
{Name: "BlobViolations", Fn: s.TestBlobViolations},
8484
}
8585
}
8686

@@ -94,9 +94,9 @@ func (s *Suite) SnapTests() []utesting.Test {
9494
}
9595
}
9696

97-
// TestStatus attempts to connect to the given node and exchange a status
98-
// message with it on the eth protocol.
9997
func (s *Suite) TestStatus(t *utesting.T) {
98+
t.Log(`This test is just a sanity check. It performs an eth protocol handshake.`)
99+
100100
conn, err := s.dial()
101101
if err != nil {
102102
t.Fatalf("dial failed: %v", err)
@@ -112,9 +112,9 @@ func headersMatch(expected []*types.Header, headers []*types.Header) bool {
112112
return reflect.DeepEqual(expected, headers)
113113
}
114114

115-
// TestGetBlockHeaders tests whether the given node can respond to an eth
116-
// `GetBlockHeaders` request and that the response is accurate.
117115
func (s *Suite) TestGetBlockHeaders(t *utesting.T) {
116+
t.Log(`This test requests block headers from the node.`)
117+
118118
conn, err := s.dial()
119119
if err != nil {
120120
t.Fatalf("dial failed: %v", err)
@@ -154,10 +154,10 @@ func (s *Suite) TestGetBlockHeaders(t *utesting.T) {
154154
}
155155
}
156156

157-
// TestSimultaneousRequests sends two simultaneous `GetBlockHeader` requests
158-
// from the same connection with different request IDs and checks to make sure
159-
// the node responds with the correct headers per request.
160157
func (s *Suite) TestSimultaneousRequests(t *utesting.T) {
158+
t.Log(`This test requests blocks headers from the node, performing two requests
159+
concurrently, with different request IDs.`)
160+
161161
conn, err := s.dial()
162162
if err != nil {
163163
t.Fatalf("dial failed: %v", err)
@@ -228,9 +228,10 @@ func (s *Suite) TestSimultaneousRequests(t *utesting.T) {
228228
}
229229
}
230230

231-
// TestSameRequestID sends two requests with the same request ID to a single
232-
// node.
233231
func (s *Suite) TestSameRequestID(t *utesting.T) {
232+
t.Log(`This test requests block headers, performing two concurrent requests with the
233+
same request ID. The node should handle the request by responding to both requests.`)
234+
234235
conn, err := s.dial()
235236
if err != nil {
236237
t.Fatalf("dial failed: %v", err)
@@ -298,9 +299,10 @@ func (s *Suite) TestSameRequestID(t *utesting.T) {
298299
}
299300
}
300301

301-
// TestZeroRequestID checks that a message with a request ID of zero is still handled
302-
// by the node.
303302
func (s *Suite) TestZeroRequestID(t *utesting.T) {
303+
t.Log(`This test sends a GetBlockHeaders message with a request-id of zero,
304+
and expects a response.`)
305+
304306
conn, err := s.dial()
305307
if err != nil {
306308
t.Fatalf("dial failed: %v", err)
@@ -333,9 +335,9 @@ func (s *Suite) TestZeroRequestID(t *utesting.T) {
333335
}
334336
}
335337

336-
// TestGetBlockBodies tests whether the given node can respond to a
337-
// `GetBlockBodies` request and that the response is accurate.
338338
func (s *Suite) TestGetBlockBodies(t *utesting.T) {
339+
t.Log(`This test sends GetBlockBodies requests to the node for known blocks in the test chain.`)
340+
339341
conn, err := s.dial()
340342
if err != nil {
341343
t.Fatalf("dial failed: %v", err)
@@ -376,12 +378,12 @@ func randBuf(size int) []byte {
376378
return buf
377379
}
378380

379-
// TestMaliciousHandshake tries to send malicious data during the handshake.
380381
func (s *Suite) TestMaliciousHandshake(t *utesting.T) {
381-
key, _ := crypto.GenerateKey()
382+
t.Log(`This test tries to send malicious data during the devp2p handshake, in various ways.`)
382383

383384
// Write hello to client.
384385
var (
386+
key, _ = crypto.GenerateKey()
385387
pub0 = crypto.FromECDSAPub(&key.PublicKey)[1:]
386388
version = eth.ProtocolVersions[0]
387389
)
@@ -451,8 +453,9 @@ func (s *Suite) TestMaliciousHandshake(t *utesting.T) {
451453
}
452454
}
453455

454-
// TestMaliciousStatus sends a status package with a large total difficulty.
455456
func (s *Suite) TestMaliciousStatus(t *utesting.T) {
457+
t.Log(`This test sends a malicious eth Status message to the node and expects a disconnect.`)
458+
456459
conn, err := s.dial()
457460
if err != nil {
458461
t.Fatalf("dial failed: %v", err)
@@ -486,9 +489,10 @@ func (s *Suite) TestMaliciousStatus(t *utesting.T) {
486489
}
487490
}
488491

489-
// TestTransaction sends a valid transaction to the node and checks if the
490-
// transaction gets propagated.
491492
func (s *Suite) TestTransaction(t *utesting.T) {
493+
t.Log(`This test sends a valid transaction to the node and checks if the
494+
transaction gets propagated.`)
495+
492496
// Nudge client out of syncing mode to accept pending txs.
493497
if err := s.engine.sendForkchoiceUpdated(); err != nil {
494498
t.Fatalf("failed to send next block: %v", err)
@@ -507,15 +511,16 @@ func (s *Suite) TestTransaction(t *utesting.T) {
507511
if err != nil {
508512
t.Fatalf("failed to sign tx: %v", err)
509513
}
510-
if err := s.sendTxs([]*types.Transaction{tx}); err != nil {
514+
if err := s.sendTxs(t, []*types.Transaction{tx}); err != nil {
511515
t.Fatal(err)
512516
}
513517
s.chain.IncNonce(from, 1)
514518
}
515519

516-
// TestInvalidTxs sends several invalid transactions and tests whether
517-
// the node will propagate them.
518520
func (s *Suite) TestInvalidTxs(t *utesting.T) {
521+
t.Log(`This test sends several kinds of invalid transactions and checks that the node
522+
does not propagate them.`)
523+
519524
// Nudge client out of syncing mode to accept pending txs.
520525
if err := s.engine.sendForkchoiceUpdated(); err != nil {
521526
t.Fatalf("failed to send next block: %v", err)
@@ -534,7 +539,7 @@ func (s *Suite) TestInvalidTxs(t *utesting.T) {
534539
if err != nil {
535540
t.Fatalf("failed to sign tx: %v", err)
536541
}
537-
if err := s.sendTxs([]*types.Transaction{tx}); err != nil {
542+
if err := s.sendTxs(t, []*types.Transaction{tx}); err != nil {
538543
t.Fatalf("failed to send txs: %v", err)
539544
}
540545
s.chain.IncNonce(from, 1)
@@ -590,14 +595,15 @@ func (s *Suite) TestInvalidTxs(t *utesting.T) {
590595
}
591596
txs = append(txs, tx)
592597
}
593-
if err := s.sendInvalidTxs(txs); err != nil {
598+
if err := s.sendInvalidTxs(t, txs); err != nil {
594599
t.Fatalf("failed to send invalid txs: %v", err)
595600
}
596601
}
597602

598-
// TestLargeTxRequest tests whether a node can fulfill a large GetPooledTransactions
599-
// request.
600603
func (s *Suite) TestLargeTxRequest(t *utesting.T) {
604+
t.Log(`This test first send ~2000 transactions to the node, then requests them
605+
on another peer connection using GetPooledTransactions.`)
606+
601607
// Nudge client out of syncing mode to accept pending txs.
602608
if err := s.engine.sendForkchoiceUpdated(); err != nil {
603609
t.Fatalf("failed to send next block: %v", err)
@@ -630,7 +636,7 @@ func (s *Suite) TestLargeTxRequest(t *utesting.T) {
630636
s.chain.IncNonce(from, uint64(count))
631637

632638
// Send txs.
633-
if err := s.sendTxs(txs); err != nil {
639+
if err := s.sendTxs(t, txs); err != nil {
634640
t.Fatalf("failed to send txs: %v", err)
635641
}
636642

@@ -667,13 +673,15 @@ func (s *Suite) TestLargeTxRequest(t *utesting.T) {
667673
}
668674
}
669675

670-
// TestNewPooledTxs tests whether a node will do a GetPooledTransactions request
671-
// upon receiving a NewPooledTransactionHashes announcement.
672676
func (s *Suite) TestNewPooledTxs(t *utesting.T) {
677+
t.Log(`This test announces transaction hashes to the node and expects it to fetch
678+
the transactions using a GetPooledTransactions request.`)
679+
673680
// Nudge client out of syncing mode to accept pending txs.
674681
if err := s.engine.sendForkchoiceUpdated(); err != nil {
675682
t.Fatalf("failed to send next block: %v", err)
676683
}
684+
677685
var (
678686
count = 50
679687
from, nonce = s.chain.GetSender(1)
@@ -787,6 +795,8 @@ func (s *Suite) makeBlobTxs(count, blobs int, discriminator byte) (txs types.Tra
787795
}
788796

789797
func (s *Suite) TestBlobViolations(t *utesting.T) {
798+
t.Log(`This test sends some invalid blob tx announcements and expects the node to disconnect.`)
799+
790800
if err := s.engine.sendForkchoiceUpdated(); err != nil {
791801
t.Fatalf("send fcu failed: %v", err)
792802
}

cmd/devp2p/internal/ethtest/transaction.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ import (
2525
"github.com/ethereum/go-ethereum/common"
2626
"github.com/ethereum/go-ethereum/core/types"
2727
"github.com/ethereum/go-ethereum/eth/protocols/eth"
28+
"github.com/ethereum/go-ethereum/internal/utesting"
2829
)
2930

3031
// sendTxs sends the given transactions to the node and
3132
// expects the node to accept and propagate them.
32-
func (s *Suite) sendTxs(txs []*types.Transaction) error {
33+
func (s *Suite) sendTxs(t *utesting.T, txs []*types.Transaction) error {
3334
// Open sending conn.
3435
sendConn, err := s.dial()
3536
if err != nil {
@@ -74,6 +75,15 @@ func (s *Suite) sendTxs(txs []*types.Transaction) error {
7475
for _, hash := range msg.Hashes {
7576
got[hash] = true
7677
}
78+
case *eth.GetBlockHeadersPacket:
79+
headers, err := s.chain.GetHeaders(msg)
80+
if err != nil {
81+
t.Logf("invalid GetBlockHeaders request: %v", err)
82+
}
83+
recvConn.Write(ethProto, eth.BlockHeadersMsg, &eth.BlockHeadersPacket{
84+
RequestId: msg.RequestId,
85+
BlockHeadersRequest: headers,
86+
})
7787
default:
7888
return fmt.Errorf("unexpected eth wire msg: %s", pretty.Sdump(msg))
7989
}
@@ -95,7 +105,7 @@ func (s *Suite) sendTxs(txs []*types.Transaction) error {
95105
return fmt.Errorf("timed out waiting for txs")
96106
}
97107

98-
func (s *Suite) sendInvalidTxs(txs []*types.Transaction) error {
108+
func (s *Suite) sendInvalidTxs(t *utesting.T, txs []*types.Transaction) error {
99109
// Open sending conn.
100110
sendConn, err := s.dial()
101111
if err != nil {
@@ -152,6 +162,15 @@ func (s *Suite) sendInvalidTxs(txs []*types.Transaction) error {
152162
return fmt.Errorf("received bad tx: %s", hash)
153163
}
154164
}
165+
case *eth.GetBlockHeadersPacket:
166+
headers, err := s.chain.GetHeaders(msg)
167+
if err != nil {
168+
t.Logf("invalid GetBlockHeaders request: %v", err)
169+
}
170+
recvConn.Write(ethProto, eth.BlockHeadersMsg, &eth.BlockHeadersPacket{
171+
RequestId: msg.RequestId,
172+
BlockHeadersRequest: headers,
173+
})
155174
default:
156175
return fmt.Errorf("unexpected eth message: %v", pretty.Sdump(msg))
157176
}

0 commit comments

Comments
 (0)