Skip to content

Commit 055528a

Browse files
cmd/devp2p/internal/ethtest: add support for eth/68 (#26078)
Co-authored-by: Felix Lange <[email protected]>
1 parent 9027ee0 commit 055528a

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-15
lines changed

cmd/devp2p/internal/ethtest/helpers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ func (s *Suite) dial() (*Conn, error) {
6363
conn.caps = []p2p.Cap{
6464
{Name: "eth", Version: 66},
6565
{Name: "eth", Version: 67},
66+
{Name: "eth", Version: 68},
6667
}
67-
conn.ourHighestProtoVersion = 67
68+
conn.ourHighestProtoVersion = 68
6869
return &conn, nil
6970
}
7071

@@ -359,6 +360,8 @@ func (s *Suite) waitAnnounce(conn *Conn, blockAnnouncement *NewBlock) error {
359360
return nil
360361

361362
// ignore tx announcements from previous tests
363+
case *NewPooledTransactionHashes66:
364+
continue
362365
case *NewPooledTransactionHashes:
363366
continue
364367
case *Transactions:

cmd/devp2p/internal/ethtest/suite.go

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

512512
// generate 50 txs
513-
hashMap, _, err := generateTxs(s, 50)
513+
_, txs, err := generateTxs(s, 50)
514514
if err != nil {
515515
t.Fatalf("failed to generate transactions: %v", err)
516516
}
517-
518-
// create new pooled tx hashes announcement
519-
hashes := make([]common.Hash, 0)
520-
for _, hash := range hashMap {
521-
hashes = append(hashes, hash)
517+
hashes := make([]common.Hash, len(txs))
518+
types := make([]byte, len(txs))
519+
sizes := make([]uint32, len(txs))
520+
for i, tx := range txs {
521+
hashes[i] = tx.Hash()
522+
types[i] = tx.Type()
523+
sizes[i] = uint32(tx.Size())
522524
}
523-
announce := NewPooledTransactionHashes(hashes)
524525

525526
// send announcement
526527
conn, err := s.dial()
@@ -531,7 +532,13 @@ func (s *Suite) TestNewPooledTxs(t *utesting.T) {
531532
if err = conn.peer(s.chain, nil); err != nil {
532533
t.Fatalf("peering failed: %v", err)
533534
}
534-
if err = conn.Write(announce); err != nil {
535+
536+
var ann Message = NewPooledTransactionHashes{Types: types, Sizes: sizes, Hashes: hashes}
537+
if conn.negotiatedProtoVersion < eth.ETH68 {
538+
ann = NewPooledTransactionHashes66(hashes)
539+
}
540+
err = conn.Write(ann)
541+
if err != nil {
535542
t.Fatalf("failed to write to connection: %v", err)
536543
}
537544

@@ -546,6 +553,8 @@ func (s *Suite) TestNewPooledTxs(t *utesting.T) {
546553
return
547554

548555
// ignore propagated txs from previous tests
556+
case *NewPooledTransactionHashes66:
557+
continue
549558
case *NewPooledTransactionHashes:
550559
continue
551560
case *Transactions:

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
@@ -126,8 +126,14 @@ type NewBlock eth.NewBlockPacket
126126
func (msg NewBlock) Code() int { return 23 }
127127
func (msg NewBlock) ReqID() uint64 { return 0 }
128128

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

132138
func (msg NewPooledTransactionHashes) Code() int { return 24 }
133139
func (msg NewPooledTransactionHashes) ReqID() uint64 { return 0 }
@@ -202,8 +208,13 @@ func (c *Conn) Read() Message {
202208
msg = new(NewBlockHashes)
203209
case (Transactions{}).Code():
204210
msg = new(Transactions)
205-
case (NewPooledTransactionHashes{}).Code():
206-
msg = new(NewPooledTransactionHashes)
211+
case (NewPooledTransactionHashes66{}).Code():
212+
// Try decoding to eth68
213+
ethMsg := new(NewPooledTransactionHashes)
214+
if err := rlp.DecodeBytes(rawData, ethMsg); err == nil {
215+
return ethMsg
216+
}
217+
msg = new(NewPooledTransactionHashes66)
207218
case (GetPooledTransactions{}.Code()):
208219
ethMsg := new(eth.GetPooledTransactionsPacket66)
209220
if err := rlp.DecodeBytes(rawData, ethMsg); err != nil {

0 commit comments

Comments
 (0)