Skip to content

Commit d3040a8

Browse files
authored
cmd/devp2p/internal/ethtest: skip eth/66 tests when v66 not supported (#22460)
1 parent c454717 commit d3040a8

File tree

6 files changed

+116
-15
lines changed

6 files changed

+116
-15
lines changed

cmd/devp2p/internal/ethtest/chain_test.go

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,31 @@ func TestEthProtocolNegotiation(t *testing.T) {
3535
expected uint32
3636
}{
3737
{
38-
conn: &Conn{},
38+
conn: &Conn{
39+
ourHighestProtoVersion: 65,
40+
},
41+
caps: []p2p.Cap{
42+
{Name: "eth", Version: 63},
43+
{Name: "eth", Version: 64},
44+
{Name: "eth", Version: 65},
45+
},
46+
expected: uint32(65),
47+
},
48+
{
49+
conn: &Conn{
50+
ourHighestProtoVersion: 65,
51+
},
52+
caps: []p2p.Cap{
53+
{Name: "eth", Version: 63},
54+
{Name: "eth", Version: 64},
55+
{Name: "eth", Version: 65},
56+
},
57+
expected: uint32(65),
58+
},
59+
{
60+
conn: &Conn{
61+
ourHighestProtoVersion: 65,
62+
},
3963
caps: []p2p.Cap{
4064
{Name: "eth", Version: 63},
4165
{Name: "eth", Version: 64},
@@ -44,7 +68,20 @@ func TestEthProtocolNegotiation(t *testing.T) {
4468
expected: uint32(65),
4569
},
4670
{
47-
conn: &Conn{},
71+
conn: &Conn{
72+
ourHighestProtoVersion: 64,
73+
},
74+
caps: []p2p.Cap{
75+
{Name: "eth", Version: 63},
76+
{Name: "eth", Version: 64},
77+
{Name: "eth", Version: 65},
78+
},
79+
expected: 64,
80+
},
81+
{
82+
conn: &Conn{
83+
ourHighestProtoVersion: 65,
84+
},
4885
caps: []p2p.Cap{
4986
{Name: "eth", Version: 0},
5087
{Name: "eth", Version: 89},
@@ -53,7 +90,20 @@ func TestEthProtocolNegotiation(t *testing.T) {
5390
expected: uint32(65),
5491
},
5592
{
56-
conn: &Conn{},
93+
conn: &Conn{
94+
ourHighestProtoVersion: 64,
95+
},
96+
caps: []p2p.Cap{
97+
{Name: "eth", Version: 63},
98+
{Name: "eth", Version: 64},
99+
{Name: "wrongProto", Version: 65},
100+
},
101+
expected: uint32(64),
102+
},
103+
{
104+
conn: &Conn{
105+
ourHighestProtoVersion: 65,
106+
},
57107
caps: []p2p.Cap{
58108
{Name: "eth", Version: 63},
59109
{Name: "eth", Version: 64},
@@ -66,7 +116,7 @@ func TestEthProtocolNegotiation(t *testing.T) {
66116
for i, tt := range tests {
67117
t.Run(strconv.Itoa(i), func(t *testing.T) {
68118
tt.conn.negotiateEthProtocol(tt.caps)
69-
assert.Equal(t, tt.expected, uint32(tt.conn.ethProtocolVersion))
119+
assert.Equal(t, tt.expected, uint32(tt.conn.negotiatedProtoVersion))
70120
})
71121
}
72122
}

cmd/devp2p/internal/ethtest/eth66_suite.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ import (
2626
"github.com/ethereum/go-ethereum/p2p"
2727
)
2828

29+
// Is_66 checks if the node supports the eth66 protocol version,
30+
// and if not, exists the test suite
31+
func (s *Suite) Is_66(t *utesting.T) {
32+
conn := s.dial66(t)
33+
conn.handshake(t)
34+
if conn.negotiatedProtoVersion < 66 {
35+
t.Fail()
36+
}
37+
}
38+
2939
// TestStatus_66 attempts to connect to the given node and exchange
3040
// a status message with it on the eth66 protocol, and then check to
3141
// make sure the chain head is correct.

cmd/devp2p/internal/ethtest/eth66_suiteHelpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (s *Suite) dial66(t *utesting.T) *Conn {
4646
t.Fatalf("could not dial: %v", err)
4747
}
4848
conn.caps = append(conn.caps, p2p.Cap{Name: "eth", Version: 66})
49+
conn.ourHighestProtoVersion = 66
4950
return conn
5051
}
5152

cmd/devp2p/internal/ethtest/suite.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func NewSuite(dest *enode.Node, chainfile string, genesisfile string) (*Suite, e
6565
}, nil
6666
}
6767

68-
func (s *Suite) EthTests() []utesting.Test {
68+
func (s *Suite) AllEthTests() []utesting.Test {
6969
return []utesting.Test{
7070
// status
7171
{Name: "Status", Fn: s.TestStatus},
@@ -97,6 +97,38 @@ func (s *Suite) EthTests() []utesting.Test {
9797
}
9898
}
9999

100+
func (s *Suite) EthTests() []utesting.Test {
101+
return []utesting.Test{
102+
{Name: "Status", Fn: s.TestStatus},
103+
{Name: "GetBlockHeaders", Fn: s.TestGetBlockHeaders},
104+
{Name: "GetBlockBodies", Fn: s.TestGetBlockBodies},
105+
{Name: "Broadcast", Fn: s.TestBroadcast},
106+
{Name: "TestLargeAnnounce", Fn: s.TestLargeAnnounce},
107+
{Name: "TestMaliciousHandshake", Fn: s.TestMaliciousHandshake},
108+
{Name: "TestMaliciousStatus", Fn: s.TestMaliciousStatus},
109+
{Name: "TestMaliciousStatus_66", Fn: s.TestMaliciousStatus},
110+
{Name: "TestTransactions", Fn: s.TestTransaction},
111+
{Name: "TestMaliciousTransactions", Fn: s.TestMaliciousTx},
112+
}
113+
}
114+
115+
func (s *Suite) Eth66Tests() []utesting.Test {
116+
return []utesting.Test{
117+
// only proceed with eth66 test suite if node supports eth 66 protocol
118+
{Name: "Status_66", Fn: s.TestStatus_66},
119+
{Name: "GetBlockHeaders_66", Fn: s.TestGetBlockHeaders_66},
120+
{Name: "TestSimultaneousRequests_66", Fn: s.TestSimultaneousRequests_66},
121+
{Name: "TestSameRequestID_66", Fn: s.TestSameRequestID_66},
122+
{Name: "TestZeroRequestID_66", Fn: s.TestZeroRequestID_66},
123+
{Name: "GetBlockBodies_66", Fn: s.TestGetBlockBodies_66},
124+
{Name: "Broadcast_66", Fn: s.TestBroadcast_66},
125+
{Name: "TestLargeAnnounce_66", Fn: s.TestLargeAnnounce_66},
126+
{Name: "TestMaliciousHandshake_66", Fn: s.TestMaliciousHandshake_66},
127+
{Name: "TestTransactions_66", Fn: s.TestTransaction_66},
128+
{Name: "TestMaliciousTransactions_66", Fn: s.TestMaliciousTx_66},
129+
}
130+
}
131+
100132
// TestStatus attempts to connect to the given node and exchange
101133
// a status message with it, and then check to make sure
102134
// the chain head is correct.
@@ -125,7 +157,7 @@ func (s *Suite) TestMaliciousStatus(t *utesting.T) {
125157
// get protoHandshake
126158
conn.handshake(t)
127159
status := &Status{
128-
ProtocolVersion: uint32(conn.ethProtocolVersion),
160+
ProtocolVersion: uint32(conn.negotiatedProtoVersion),
129161
NetworkID: s.chain.chainConfig.ChainID.Uint64(),
130162
TD: largeNumber(2),
131163
Head: s.chain.blocks[s.chain.Len()-1].Hash(),
@@ -421,6 +453,7 @@ func (s *Suite) dial() (*Conn, error) {
421453
{Name: "eth", Version: 64},
422454
{Name: "eth", Version: 65},
423455
}
456+
conn.ourHighestProtoVersion = 65
424457
return &conn, nil
425458
}
426459

cmd/devp2p/internal/ethtest/types.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ func (nb NewPooledTransactionHashes) Code() int { return 24 }
123123
// Conn represents an individual connection with a peer
124124
type Conn struct {
125125
*rlpx.Conn
126-
ourKey *ecdsa.PrivateKey
127-
ethProtocolVersion uint
128-
caps []p2p.Cap
126+
ourKey *ecdsa.PrivateKey
127+
negotiatedProtoVersion uint
128+
ourHighestProtoVersion uint
129+
caps []p2p.Cap
129130
}
130131

131132
func (c *Conn) Read() Message {
@@ -236,7 +237,7 @@ func (c *Conn) handshake(t *utesting.T) Message {
236237
c.SetSnappy(true)
237238
}
238239
c.negotiateEthProtocol(msg.Caps)
239-
if c.ethProtocolVersion == 0 {
240+
if c.negotiatedProtoVersion == 0 {
240241
t.Fatalf("unexpected eth protocol version")
241242
}
242243
return msg
@@ -254,11 +255,11 @@ func (c *Conn) negotiateEthProtocol(caps []p2p.Cap) {
254255
if capability.Name != "eth" {
255256
continue
256257
}
257-
if capability.Version > highestEthVersion && capability.Version <= 65 {
258+
if capability.Version > highestEthVersion && capability.Version <= c.ourHighestProtoVersion {
258259
highestEthVersion = capability.Version
259260
}
260261
}
261-
c.ethProtocolVersion = highestEthVersion
262+
c.negotiatedProtoVersion = highestEthVersion
262263
}
263264

264265
// statusExchange performs a `Status` message exchange with the given
@@ -295,13 +296,13 @@ loop:
295296
}
296297
}
297298
// make sure eth protocol version is set for negotiation
298-
if c.ethProtocolVersion == 0 {
299+
if c.negotiatedProtoVersion == 0 {
299300
t.Fatalf("eth protocol version must be set in Conn")
300301
}
301302
if status == nil {
302303
// write status message to client
303304
status = &Status{
304-
ProtocolVersion: uint32(c.ethProtocolVersion),
305+
ProtocolVersion: uint32(c.negotiatedProtoVersion),
305306
NetworkID: chain.chainConfig.ChainID.Uint64(),
306307
TD: chain.TD(chain.Len()),
307308
Head: chain.blocks[chain.Len()-1].Hash(),

cmd/devp2p/rlpxcmd.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/ethereum/go-ethereum/cmd/devp2p/internal/ethtest"
2424
"github.com/ethereum/go-ethereum/crypto"
25+
"github.com/ethereum/go-ethereum/internal/utesting"
2526
"github.com/ethereum/go-ethereum/p2p"
2627
"github.com/ethereum/go-ethereum/p2p/rlpx"
2728
"github.com/ethereum/go-ethereum/rlp"
@@ -98,5 +99,10 @@ func rlpxEthTest(ctx *cli.Context) error {
9899
if err != nil {
99100
exit(err)
100101
}
101-
return runTests(ctx, suite.EthTests())
102+
// check if given node supports eth66, and if so, run eth66 protocol tests as well
103+
is66Failed, _ := utesting.Run(utesting.Test{Name: "Is_66", Fn: suite.Is_66})
104+
if is66Failed {
105+
return runTests(ctx, suite.EthTests())
106+
}
107+
return runTests(ctx, suite.AllEthTests())
102108
}

0 commit comments

Comments
 (0)