2020-11-30 15:23:48 +01:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2022-05-24 20:39:40 +02:00
|
|
|
// This file is part of go-ethereum.
|
2020-11-30 15:23:48 +01:00
|
|
|
//
|
2022-05-24 20:39:40 +02:00
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2020-11-30 15:23:48 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2022-05-24 20:39:40 +02:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2020-11-30 15:23:48 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2022-05-24 20:39:40 +02:00
|
|
|
// GNU General Public License for more details.
|
2020-11-30 15:23:48 +01:00
|
|
|
//
|
2022-05-24 20:39:40 +02:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2020-11-30 15:23:48 +01:00
|
|
|
|
|
|
|
package ethtest
|
|
|
|
|
|
|
|
import (
|
2023-05-24 18:21:29 +08:00
|
|
|
"errors"
|
2021-05-25 23:09:11 +02:00
|
|
|
"fmt"
|
2023-12-20 09:23:48 -07:00
|
|
|
"os"
|
2020-11-30 15:23:48 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-12-20 09:23:48 -07:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
2024-02-15 19:43:37 +01:00
|
|
|
"github.com/ethereum/go-ethereum/internal/utesting"
|
2020-11-30 15:23:48 +01:00
|
|
|
)
|
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
// sendTxs sends the given transactions to the node and
|
|
|
|
// expects the node to accept and propagate them.
|
2024-02-15 19:43:37 +01:00
|
|
|
func (s *Suite) sendTxs(t *utesting.T, txs []*types.Transaction) error {
|
2023-12-20 09:23:48 -07:00
|
|
|
// Open sending conn.
|
|
|
|
sendConn, err := s.dial()
|
2021-05-25 23:09:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer sendConn.Close()
|
|
|
|
if err = sendConn.peer(s.chain, nil); err != nil {
|
|
|
|
return fmt.Errorf("peering failed: %v", err)
|
|
|
|
}
|
2022-08-02 14:48:55 +02:00
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
// Open receiving conn.
|
2022-08-02 14:48:55 +02:00
|
|
|
recvConn, err := s.dial()
|
2021-05-25 23:09:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-23 18:14:39 +02:00
|
|
|
defer recvConn.Close()
|
2021-05-25 23:09:11 +02:00
|
|
|
if err = recvConn.peer(s.chain, nil); err != nil {
|
|
|
|
return fmt.Errorf("peering failed: %v", err)
|
|
|
|
}
|
2022-08-02 14:48:55 +02:00
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
if err = sendConn.Write(ethProto, eth.TransactionsMsg, eth.TransactionsPacket(txs)); err != nil {
|
2021-05-25 23:09:11 +02:00
|
|
|
return fmt.Errorf("failed to write message to connection: %v", err)
|
2021-04-23 18:14:39 +02:00
|
|
|
}
|
2022-08-02 14:48:55 +02:00
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
var (
|
|
|
|
got = make(map[common.Hash]bool)
|
|
|
|
end = time.Now().Add(timeout)
|
|
|
|
)
|
2022-08-02 14:48:55 +02:00
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
// Wait for the transaction announcements, make sure all txs ar propagated.
|
|
|
|
for time.Now().Before(end) {
|
|
|
|
msg, err := recvConn.ReadEth()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read from connection: %w", err)
|
|
|
|
}
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *eth.TransactionsPacket:
|
2021-04-23 18:14:39 +02:00
|
|
|
for _, tx := range *msg {
|
2023-12-20 09:23:48 -07:00
|
|
|
got[tx.Hash()] = true
|
2021-04-23 18:14:39 +02:00
|
|
|
}
|
2024-02-08 15:49:19 +02:00
|
|
|
case *eth.NewPooledTransactionHashesPacket:
|
2023-12-20 09:23:48 -07:00
|
|
|
for _, hash := range msg.Hashes {
|
|
|
|
got[hash] = true
|
2021-04-23 18:14:39 +02:00
|
|
|
}
|
2024-02-15 19:43:37 +01:00
|
|
|
case *eth.GetBlockHeadersPacket:
|
|
|
|
headers, err := s.chain.GetHeaders(msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("invalid GetBlockHeaders request: %v", err)
|
|
|
|
}
|
|
|
|
recvConn.Write(ethProto, eth.BlockHeadersMsg, ð.BlockHeadersPacket{
|
|
|
|
RequestId: msg.RequestId,
|
|
|
|
BlockHeadersRequest: headers,
|
|
|
|
})
|
2023-12-20 09:23:48 -07:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected eth wire msg: %s", pretty.Sdump(msg))
|
2021-04-23 18:14:39 +02:00
|
|
|
}
|
2023-12-20 09:23:48 -07:00
|
|
|
|
|
|
|
// Check if all txs received.
|
|
|
|
allReceived := func() bool {
|
|
|
|
for _, tx := range txs {
|
|
|
|
if !got[tx.Hash()] {
|
|
|
|
return false
|
|
|
|
}
|
2021-04-23 18:14:39 +02:00
|
|
|
}
|
2023-12-20 09:23:48 -07:00
|
|
|
return true
|
2021-04-23 18:14:39 +02:00
|
|
|
}
|
2023-12-20 09:23:48 -07:00
|
|
|
if allReceived() {
|
|
|
|
return nil
|
2021-04-23 18:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
return fmt.Errorf("timed out waiting for txs")
|
2020-11-30 15:23:48 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 19:43:37 +01:00
|
|
|
func (s *Suite) sendInvalidTxs(t *utesting.T, txs []*types.Transaction) error {
|
2023-12-20 09:23:48 -07:00
|
|
|
// Open sending conn.
|
|
|
|
sendConn, err := s.dial()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-23 18:14:39 +02:00
|
|
|
}
|
2023-12-20 09:23:48 -07:00
|
|
|
defer sendConn.Close()
|
|
|
|
if err = sendConn.peer(s.chain, nil); err != nil {
|
|
|
|
return fmt.Errorf("peering failed: %v", err)
|
2021-04-23 11:15:42 +02:00
|
|
|
}
|
2023-12-20 09:23:48 -07:00
|
|
|
sendConn.SetDeadline(time.Now().Add(timeout))
|
2021-04-23 11:15:42 +02:00
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
// Open receiving conn.
|
|
|
|
recvConn, err := s.dial()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-05-25 23:09:11 +02:00
|
|
|
}
|
2023-12-20 09:23:48 -07:00
|
|
|
defer recvConn.Close()
|
|
|
|
if err = recvConn.peer(s.chain, nil); err != nil {
|
|
|
|
return fmt.Errorf("peering failed: %v", err)
|
2020-11-30 15:23:48 +01:00
|
|
|
}
|
2023-12-20 09:23:48 -07:00
|
|
|
recvConn.SetDeadline(time.Now().Add(timeout))
|
2020-11-30 15:23:48 +01:00
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
if err = sendConn.Write(ethProto, eth.TransactionsMsg, txs); err != nil {
|
|
|
|
return fmt.Errorf("failed to write message to connection: %w", err)
|
2020-11-30 15:23:48 +01:00
|
|
|
}
|
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
// Make map of invalid txs.
|
|
|
|
invalids := make(map[common.Hash]struct{})
|
|
|
|
for _, tx := range txs {
|
|
|
|
invalids[tx.Hash()] = struct{}{}
|
2021-04-23 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2024-01-09 03:31:22 +08:00
|
|
|
// Get responses.
|
2023-12-20 09:23:48 -07:00
|
|
|
recvConn.SetReadDeadline(time.Now().Add(timeout))
|
|
|
|
for {
|
|
|
|
msg, err := recvConn.ReadEth()
|
|
|
|
if errors.Is(err, os.ErrDeadlineExceeded) {
|
|
|
|
// Successful if no invalid txs are propagated before timeout.
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("failed to read from connection: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *eth.TransactionsPacket:
|
|
|
|
for _, tx := range txs {
|
|
|
|
if _, ok := invalids[tx.Hash()]; ok {
|
|
|
|
return fmt.Errorf("received bad tx: %s", tx.Hash())
|
|
|
|
}
|
|
|
|
}
|
2024-02-08 15:49:19 +02:00
|
|
|
case *eth.NewPooledTransactionHashesPacket:
|
2023-12-20 09:23:48 -07:00
|
|
|
for _, hash := range msg.Hashes {
|
|
|
|
if _, ok := invalids[hash]; ok {
|
|
|
|
return fmt.Errorf("received bad tx: %s", hash)
|
|
|
|
}
|
|
|
|
}
|
2024-02-15 19:43:37 +01:00
|
|
|
case *eth.GetBlockHeadersPacket:
|
|
|
|
headers, err := s.chain.GetHeaders(msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("invalid GetBlockHeaders request: %v", err)
|
|
|
|
}
|
|
|
|
recvConn.Write(ethProto, eth.BlockHeadersMsg, ð.BlockHeadersPacket{
|
|
|
|
RequestId: msg.RequestId,
|
|
|
|
BlockHeadersRequest: headers,
|
|
|
|
})
|
2023-12-20 09:23:48 -07:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected eth message: %v", pretty.Sdump(msg))
|
2020-11-30 15:23:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|