ethtest conn.go¶
| Source (upstream v1.17.4) | Current | |
|---|---|---|
| File | cmd/devp2p/internal/ethtest/conn.go |
conn.go |
| Symbol | (entire file) |
(entire file) |
| Ref | v1.17.4 |
etc/v1.17.4-full-node |
ETH/68 test client: StatusPacket68 with TD+Head fields, version 68 capability. Upstream switched to ETH/69 (StatusPacket69 with LatestBlock/LatestBlockHash).
Diff — vs upstream v1.17.4 (modified in-place)¶
--- a/cmd/devp2p/internal/ethtest/conn.go
+++ b/cmd/devp2p/internal/ethtest/conn.go
@@ -66,10 +66,9 @@
return nil, err
}
conn.caps = []p2p.Cap{
- {Name: "eth", Version: 70},
- {Name: "eth", Version: 69},
+ {Name: "eth", Version: 68},
}
- conn.ourHighestProtoVersion = 70
+ conn.ourHighestProtoVersion = 68
return &conn, nil
}
@@ -169,7 +168,7 @@
var msg any
switch int(code) {
case eth.StatusMsg:
- msg = new(eth.StatusPacket)
+ msg = new(eth.StatusPacket68)
case eth.GetBlockHeadersMsg:
msg = new(eth.GetBlockHeadersPacket)
case eth.BlockHeadersMsg:
@@ -178,6 +177,10 @@
msg = new(eth.GetBlockBodiesPacket)
case eth.BlockBodiesMsg:
msg = new(eth.BlockBodiesPacket)
+ case eth.NewBlockMsg:
+ msg = new(eth.NewBlockPacket)
+ case eth.NewBlockHashesMsg:
+ msg = new(eth.NewBlockHashesPacket)
case eth.TransactionsMsg:
msg = new(eth.TransactionsPacket)
case eth.NewPooledTransactionHashesMsg:
@@ -246,7 +249,7 @@
}
// dialAndPeer creates a peer connection and runs the handshake.
-func (s *Suite) dialAndPeer(status *eth.StatusPacket) (*Conn, error) {
+func (s *Suite) dialAndPeer(status *eth.StatusPacket68) (*Conn, error) {
c, err := s.dial()
if err != nil {
return nil, err
@@ -259,7 +262,7 @@
// peer performs both the protocol handshake and the status message
// exchange with the node in order to peer with it.
-func (c *Conn) peer(chain *Chain, status *eth.StatusPacket) error {
+func (c *Conn) peer(chain *Chain, status *eth.StatusPacket68) error {
if err := c.handshake(); err != nil {
return fmt.Errorf("handshake failed: %v", err)
}
@@ -332,7 +335,7 @@
}
// statusExchange performs a `Status` message exchange with the given node.
-func (c *Conn) statusExchange(chain *Chain, status *eth.StatusPacket) error {
+func (c *Conn) statusExchange(chain *Chain, status *eth.StatusPacket68) error {
loop:
for {
code, data, err := c.Read()
@@ -341,18 +344,17 @@
}
switch code {
case eth.StatusMsg + protoOffset(ethProto):
- msg := new(eth.StatusPacket)
+ msg := new(eth.StatusPacket68)
if err := rlp.DecodeBytes(data, &msg); err != nil {
return fmt.Errorf("error decoding status packet: %w", err)
}
- if have, want := msg.LatestBlock, chain.blocks[chain.Len()-1].NumberU64(); have != want {
- return fmt.Errorf("wrong head block in status, want: %d, have %d",
- want, have)
- }
- if have, want := msg.LatestBlockHash, chain.blocks[chain.Len()-1].Hash(); have != want {
+ if have, want := msg.Head, chain.blocks[chain.Len()-1].Hash(); have != want {
return fmt.Errorf("wrong head block in status, want: %#x (block %d) have %#x",
want, chain.blocks[chain.Len()-1].NumberU64(), have)
}
+ if have, want := msg.TD.Cmp(chain.TD()), 0; have != want {
+ return fmt.Errorf("wrong TD in status: have %v want %v", have, want)
+ }
if have, want := msg.ForkID, chain.ForkID(); !reflect.DeepEqual(have, want) {
return fmt.Errorf("wrong fork ID in status: have %v, want %v", have, want)
}
@@ -382,14 +384,13 @@
}
if status == nil {
// default status message
- status = ð.StatusPacket{
+ status = ð.StatusPacket68{
ProtocolVersion: uint32(c.negotiatedProtoVersion),
NetworkID: chain.config.ChainID.Uint64(),
+ TD: chain.TD(),
+ Head: chain.blocks[chain.Len()-1].Hash(),
Genesis: chain.blocks[0].Hash(),
ForkID: chain.ForkID(),
- EarliestBlock: 0,
- LatestBlock: chain.blocks[chain.Len()-1].NumberU64(),
- LatestBlockHash: chain.blocks[chain.Len()-1].Hash(),
}
}
if err := c.Write(ethProto, eth.StatusMsg, status); err != nil {
core-geth validation — +49 -11
| | | |---|---| | File | [`conn.go`](https://github.com/etclabscore/core-geth/blob/v1.12.20/cmd/devp2p/internal/ethtest/conn.go) | | Symbol | `` | | Ref | `v1.12.20` |--- a/core-geth/cmd/devp2p/internal/ethtest/conn.go
+++ b/etc/cmd/devp2p/internal/ethtest/conn.go
@@ -66,7 +66,6 @@
return nil, err
}
conn.caps = []p2p.Cap{
- {Name: "eth", Version: 67},
{Name: "eth", Version: 68},
}
conn.ourHighestProtoVersion = 68
@@ -84,6 +83,19 @@
return conn, nil
}
+// dialSnap2 creates a connection advertising snap/2 as the only snap capability.
+// This is used by the snap/2 (EIP-8189) test suite to force the peer to
+// negotiate snap/2 rather than falling back to snap/1.
+func (s *Suite) dialSnap2() (*Conn, error) {
+ conn, err := s.dial()
+ if err != nil {
+ return nil, fmt.Errorf("dial failed: %v", err)
+ }
+ conn.caps = append(conn.caps, p2p.Cap{Name: "snap", Version: 2})
+ conn.ourHighestSnapProtoVersion = 2
+ return conn, nil
+}
+
// Conn represents an individual connection with a peer
type Conn struct {
*rlpx.Conn
@@ -130,11 +142,16 @@
return err
}
+var errDisc error = errors.New("disconnect")
+
// ReadEth reads an Eth sub-protocol wire message.
func (c *Conn) ReadEth() (any, error) {
c.SetReadDeadline(time.Now().Add(timeout))
for {
code, data, _, err := c.Conn.Read()
+ if code == discMsg {
+ return nil, errDisc
+ }
if err != nil {
return nil, err
}
@@ -151,7 +168,7 @@
var msg any
switch int(code) {
case eth.StatusMsg:
- msg = new(eth.StatusPacket)
+ msg = new(eth.StatusPacket68)
case eth.GetBlockHeadersMsg:
msg = new(eth.GetBlockHeadersPacket)
case eth.BlockHeadersMsg:
@@ -182,7 +199,10 @@
}
}
-// ReadSnap reads a snap/1 response with the given id from the connection.
+// ReadSnap reads a snap protocol response from the connection. It decodes
+// the full message catalog of both snap/1 and snap/2. The caller is
+// expected to only receive codes that were actually valid on the
+// negotiated protocol version.
func (c *Conn) ReadSnap() (any, error) {
c.SetReadDeadline(time.Now().Add(timeout))
for {
@@ -214,6 +234,10 @@
msg = new(snap.GetTrieNodesPacket)
case snap.TrieNodesMsg:
msg = new(snap.TrieNodesPacket)
+ case snap.GetAccessListsMsg:
+ msg = new(snap.GetAccessListsPacket)
+ case snap.AccessListsMsg:
+ msg = new(snap.AccessListsPacket)
default:
panic(fmt.Errorf("unhandled snap code: %d", code))
}
@@ -224,9 +248,21 @@
}
}
+// dialAndPeer creates a peer connection and runs the handshake.
+func (s *Suite) dialAndPeer(status *eth.StatusPacket68) (*Conn, error) {
+ c, err := s.dial()
+ if err != nil {
+ return nil, err
+ }
+ if err = c.peer(s.chain, status); err != nil {
+ c.Close()
+ }
+ return c, err
+}
+
// peer performs both the protocol handshake and the status message
// exchange with the node in order to peer with it.
-func (c *Conn) peer(chain *Chain, status *eth.StatusPacket) error {
+func (c *Conn) peer(chain *Chain, status *eth.StatusPacket68) error {
if err := c.handshake(); err != nil {
return fmt.Errorf("handshake failed: %v", err)
}
@@ -299,7 +335,7 @@
}
// statusExchange performs a `Status` message exchange with the given node.
-func (c *Conn) statusExchange(chain *Chain, status *eth.StatusPacket) error {
+func (c *Conn) statusExchange(chain *Chain, status *eth.StatusPacket68) error {
loop:
for {
code, data, err := c.Read()
@@ -308,7 +344,7 @@
}
switch code {
case eth.StatusMsg + protoOffset(ethProto):
- msg := new(eth.StatusPacket)
+ msg := new(eth.StatusPacket68)
if err := rlp.DecodeBytes(data, &msg); err != nil {
return fmt.Errorf("error decoding status packet: %w", err)
}
@@ -322,10 +358,12 @@
if have, want := msg.ForkID, chain.ForkID(); !reflect.DeepEqual(have, want) {
return fmt.Errorf("wrong fork ID in status: have %v, want %v", have, want)
}
- if have, want := msg.ProtocolVersion, c.ourHighestProtoVersion; have != uint32(want) {
- return fmt.Errorf("wrong protocol version: have %v, want %v", have, want)
- }
+ for _, cap := range c.caps {
+ if cap.Name == "eth" && cap.Version == uint(msg.ProtocolVersion) {
break loop
+ }
+ }
+ return fmt.Errorf("wrong protocol version: have %v, want %v", msg.ProtocolVersion, c.caps)
case discMsg:
var msg []p2p.DiscReason
if rlp.DecodeBytes(data, &msg); len(msg) == 0 {
@@ -346,9 +384,9 @@
}
if status == nil {
// default status message
- status = ð.StatusPacket{
+ status = ð.StatusPacket68{
ProtocolVersion: uint32(c.negotiatedProtoVersion),
- NetworkID: chain.config.GetChainID().Uint64(),
+ NetworkID: chain.config.ChainID.Uint64(),
TD: chain.TD(),
Head: chain.blocks[chain.Len()-1].Hash(),
Genesis: chain.blocks[0].Hash(),