Skip to content

Commit 4b7dc15

Browse files
MariusVanDerWijdenomerfirmak
authored andcommitted
cmd/devp2p/internal/ethtest: add support for eth/68 (ethereum#26078)
Co-authored-by: Felix Lange <[email protected]> Conflicts: cmd/devp2p/internal/ethtest/helpers.go
1 parent 9e3a22d commit 4b7dc15

File tree

4 files changed

+78
-25
lines changed

4 files changed

+78
-25
lines changed

cmd/devp2p/internal/ethtest/helpers.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ func (s *Suite) dial() (*Conn, error) {
6464
conn.caps = []p2p.Cap{
6565
{Name: "eth", Version: 66},
6666
{Name: "eth", Version: 67},
67+
{Name: "eth", Version: 68},
6768
}
68-
conn.ourHighestProtoVersion = 67
69+
conn.ourHighestProtoVersion = 68
6970
return &conn, nil
7071
}
7172

@@ -249,15 +250,6 @@ func (c *Conn) headersRequest(request *GetBlockHeaders, chain *Chain, reqID uint
249250
return headers, nil
250251
}
251252

252-
func (c *Conn) snapRequest(msg Message, id uint64, chain *Chain) (Message, error) {
253-
defer c.SetReadDeadline(time.Time{})
254-
c.SetReadDeadline(time.Now().Add(5 * time.Second))
255-
if err := c.Write(msg); err != nil {
256-
return nil, fmt.Errorf("could not write to connection: %v", err)
257-
}
258-
return c.ReadSnap(id)
259-
}
260-
261253
// headersMatch returns whether the received headers match the given request
262254
func headersMatch(expected []*types.Header, headers []*types.Header) bool {
263255
return reflect.DeepEqual(expected, headers)
@@ -337,9 +329,15 @@ func (s *Suite) waitAnnounce(conn *Conn, blockAnnouncement *NewBlock) error {
337329
return fmt.Errorf("wrong block hash in announcement: expected %v, got %v", blockAnnouncement.Block.Hash(), hashes[0].Hash)
338330
}
339331
return nil
332+
333+
// ignore tx announcements from previous tests
334+
case *NewPooledTransactionHashes66:
335+
continue
340336
case *NewPooledTransactionHashes:
341-
// ignore tx announcements from previous tests
342337
continue
338+
case *Transactions:
339+
continue
340+
343341
default:
344342
return fmt.Errorf("unexpected: %s", pretty.Sdump(msg))
345343
}

cmd/devp2p/internal/ethtest/suite.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,17 +500,18 @@ func (s *Suite) TestNewPooledTxs(t *utesting.T) {
500500
}
501501

502502
// generate 50 txs
503-
hashMap, _, err := generateTxs(s, 50)
503+
_, txs, err := generateTxs(s, 50)
504504
if err != nil {
505505
t.Fatalf("failed to generate transactions: %v", err)
506506
}
507-
508-
// create new pooled tx hashes announcement
509-
hashes := make([]common.Hash, 0)
510-
for _, hash := range hashMap {
511-
hashes = append(hashes, hash)
507+
hashes := make([]common.Hash, len(txs))
508+
types := make([]byte, len(txs))
509+
sizes := make([]uint32, len(txs))
510+
for i, tx := range txs {
511+
hashes[i] = tx.Hash()
512+
types[i] = tx.Type()
513+
sizes[i] = uint32(tx.Size())
512514
}
513-
announce := NewPooledTransactionHashes(hashes)
514515

515516
// send announcement
516517
conn, err := s.dial()
@@ -521,7 +522,13 @@ func (s *Suite) TestNewPooledTxs(t *utesting.T) {
521522
if err = conn.peer(s.chain, nil); err != nil {
522523
t.Fatalf("peering failed: %v", err)
523524
}
524-
if err = conn.Write(announce); err != nil {
525+
526+
var ann Message = NewPooledTransactionHashes{Types: types, Sizes: sizes, Hashes: hashes}
527+
if conn.negotiatedProtoVersion < eth.ETH68 {
528+
ann = NewPooledTransactionHashes66(hashes)
529+
}
530+
err = conn.Write(ann)
531+
if err != nil {
525532
t.Fatalf("failed to write to connection: %v", err)
526533
}
527534

@@ -535,6 +542,8 @@ func (s *Suite) TestNewPooledTxs(t *utesting.T) {
535542
}
536543
return
537544
// ignore propagated txs from previous tests
545+
case *NewPooledTransactionHashes66:
546+
continue
538547
case *NewPooledTransactionHashes:
539548
continue
540549
// ignore block announcements from previous tests

cmd/devp2p/internal/ethtest/transaction.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func sendSuccessfulTx(s *Suite, tx *types.Transaction, prevTx *types.Transaction
9595
}
9696
}
9797
return fmt.Errorf("missing transaction: got %v missing %v", recTxs, tx.Hash())
98-
case *NewPooledTransactionHashes:
98+
case *NewPooledTransactionHashes66:
9999
txHashes := *msg
100100
// if you receive an old tx propagation, read from connection again
101101
if len(txHashes) == 1 && prevTx != nil {
@@ -110,6 +110,34 @@ func sendSuccessfulTx(s *Suite, tx *types.Transaction, prevTx *types.Transaction
110110
}
111111
}
112112
return fmt.Errorf("missing transaction announcement: got %v missing %v", txHashes, tx.Hash())
113+
case *NewPooledTransactionHashes:
114+
txHashes := msg.Hashes
115+
if len(txHashes) != len(msg.Sizes) {
116+
return fmt.Errorf("invalid msg size lengths: hashes: %v sizes: %v", len(txHashes), len(msg.Sizes))
117+
}
118+
if len(txHashes) != len(msg.Types) {
119+
return fmt.Errorf("invalid msg type lengths: hashes: %v types: %v", len(txHashes), len(msg.Types))
120+
}
121+
// if you receive an old tx propagation, read from connection again
122+
if len(txHashes) == 1 && prevTx != nil {
123+
if txHashes[0] == prevTx.Hash() {
124+
continue
125+
}
126+
}
127+
for index, gotHash := range txHashes {
128+
if gotHash == tx.Hash() {
129+
if msg.Sizes[index] != uint32(tx.Size()) {
130+
return fmt.Errorf("invalid tx size: got %v want %v", msg.Sizes[index], tx.Size())
131+
}
132+
if msg.Types[index] != tx.Type() {
133+
return fmt.Errorf("invalid tx type: got %v want %v", msg.Types[index], tx.Type())
134+
}
135+
// Ok
136+
return nil
137+
}
138+
}
139+
return fmt.Errorf("missing transaction announcement: got %v missing %v", txHashes, tx.Hash())
140+
113141
default:
114142
return fmt.Errorf("unexpected message in sendSuccessfulTx: %s", pretty.Sdump(msg))
115143
}
@@ -201,8 +229,10 @@ func sendMultipleSuccessfulTxs(t *utesting.T, s *Suite, txs []*types.Transaction
201229
for _, tx := range *msg {
202230
recvHashes = append(recvHashes, tx.Hash())
203231
}
204-
case *NewPooledTransactionHashes:
232+
case *NewPooledTransactionHashes66:
205233
recvHashes = append(recvHashes, *msg...)
234+
case *NewPooledTransactionHashes:
235+
recvHashes = append(recvHashes, msg.Hashes...)
206236
default:
207237
if !strings.Contains(pretty.Sdump(msg), "i/o timeout") {
208238
return fmt.Errorf("unexpected message while waiting to receive txs: %s", pretty.Sdump(msg))
@@ -246,11 +276,16 @@ func checkMaliciousTxPropagation(s *Suite, txs []*types.Transaction, conn *Conn)
246276
if len(badTxs) > 0 {
247277
return fmt.Errorf("received %d bad txs: \n%v", len(badTxs), badTxs)
248278
}
249-
case *NewPooledTransactionHashes:
279+
case *NewPooledTransactionHashes66:
250280
badTxs, _ := compareReceivedTxs(*msg, txs)
251281
if len(badTxs) > 0 {
252282
return fmt.Errorf("received %d bad txs: \n%v", len(badTxs), badTxs)
253283
}
284+
case *NewPooledTransactionHashes:
285+
badTxs, _ := compareReceivedTxs(msg.Hashes, txs)
286+
if len(badTxs) > 0 {
287+
return fmt.Errorf("received %d bad txs: \n%v", len(badTxs), badTxs)
288+
}
254289
case *Error:
255290
// Transaction should not be announced -> wait for timeout
256291
return nil

cmd/devp2p/internal/ethtest/types.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,14 @@ type NewBlock eth.NewBlockPacket
125125
func (msg NewBlock) Code() int { return 23 }
126126
func (msg NewBlock) ReqID() uint64 { return 0 }
127127

128+
// NewPooledTransactionHashes66 is the network packet for the tx hash propagation message.
129+
type NewPooledTransactionHashes66 eth.NewPooledTransactionHashesPacket66
130+
131+
func (msg NewPooledTransactionHashes66) Code() int { return 24 }
132+
func (msg NewPooledTransactionHashes66) ReqID() uint64 { return 0 }
133+
128134
// NewPooledTransactionHashes is the network packet for the tx hash propagation message.
129-
type NewPooledTransactionHashes eth.NewPooledTransactionHashesPacket66
135+
type NewPooledTransactionHashes eth.NewPooledTransactionHashesPacket68
130136

131137
func (msg NewPooledTransactionHashes) Code() int { return 24 }
132138
func (msg NewPooledTransactionHashes) ReqID() uint64 { return 0 }
@@ -199,8 +205,13 @@ func (c *Conn) Read() Message {
199205
msg = new(NewBlockHashes)
200206
case (Transactions{}).Code():
201207
msg = new(Transactions)
202-
case (NewPooledTransactionHashes{}).Code():
203-
msg = new(NewPooledTransactionHashes)
208+
case (NewPooledTransactionHashes66{}).Code():
209+
// Try decoding to eth68
210+
ethMsg := new(NewPooledTransactionHashes)
211+
if err := rlp.DecodeBytes(rawData, ethMsg); err == nil {
212+
return ethMsg
213+
}
214+
msg = new(NewPooledTransactionHashes66)
204215
case (GetPooledTransactions{}.Code()):
205216
ethMsg := new(eth.GetPooledTransactionsPacket66)
206217
if err := rlp.DecodeBytes(rawData, ethMsg); err != nil {

0 commit comments

Comments
 (0)