2020-11-30 08:23:48 -06:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2022-05-24 13:39:40 -05:00
|
|
|
// This file is part of go-ethereum.
|
2020-11-30 08:23:48 -06:00
|
|
|
//
|
2022-05-24 13:39:40 -05: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 08:23:48 -06:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2022-05-24 13:39:40 -05:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2020-11-30 08:23:48 -06:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2022-05-24 13:39:40 -05:00
|
|
|
// GNU General Public License for more details.
|
2020-11-30 08:23:48 -06:00
|
|
|
//
|
2022-05-24 13:39:40 -05: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 08:23:48 -06:00
|
|
|
|
|
|
|
package ethtest
|
|
|
|
|
|
|
|
import (
|
2023-05-24 05:21:29 -05:00
|
|
|
"errors"
|
2021-05-25 16:09:11 -05:00
|
|
|
"fmt"
|
2023-12-20 10:23:48 -06:00
|
|
|
"os"
|
2020-11-30 08:23:48 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-12-20 10:23:48 -06:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
2024-02-15 12:43:37 -06:00
|
|
|
"github.com/ethereum/go-ethereum/internal/utesting"
|
2020-11-30 08:23:48 -06:00
|
|
|
)
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// sendTxs sends the given transactions to the node and
|
|
|
|
// expects the node to accept and propagate them.
|
2024-02-15 12:43:37 -06:00
|
|
|
func (s *Suite) sendTxs(t *utesting.T, txs []*types.Transaction) error {
|
2023-12-20 10:23:48 -06:00
|
|
|
// Open sending conn.
|
|
|
|
sendConn, err := s.dial()
|
2021-05-25 16:09:11 -05: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 07:48:55 -05:00
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// Open receiving conn.
|
2022-08-02 07:48:55 -05:00
|
|
|
recvConn, err := s.dial()
|
2021-05-25 16:09:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-23 11:14:39 -05:00
|
|
|
defer recvConn.Close()
|
2021-05-25 16:09:11 -05:00
|
|
|
if err = recvConn.peer(s.chain, nil); err != nil {
|
|
|
|
return fmt.Errorf("peering failed: %v", err)
|
|
|
|
}
|
2022-08-02 07:48:55 -05:00
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
if err = sendConn.Write(ethProto, eth.TransactionsMsg, eth.TransactionsPacket(txs)); err != nil {
|
2021-05-25 16:09:11 -05:00
|
|
|
return fmt.Errorf("failed to write message to connection: %v", err)
|
2021-04-23 11:14:39 -05:00
|
|
|
}
|
2022-08-02 07:48:55 -05:00
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
var (
|
|
|
|
got = make(map[common.Hash]bool)
|
|
|
|
end = time.Now().Add(timeout)
|
|
|
|
)
|
2022-08-02 07:48:55 -05:00
|
|
|
|
2023-12-20 10:23:48 -06: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 11:14:39 -05:00
|
|
|
for _, tx := range *msg {
|
2023-12-20 10:23:48 -06:00
|
|
|
got[tx.Hash()] = true
|
2021-04-23 11:14:39 -05:00
|
|
|
}
|
2024-02-08 07:49:19 -06:00
|
|
|
case *eth.NewPooledTransactionHashesPacket:
|
2023-12-20 10:23:48 -06:00
|
|
|
for _, hash := range msg.Hashes {
|
|
|
|
got[hash] = true
|
2021-04-23 11:14:39 -05:00
|
|
|
}
|
2024-02-15 12:43:37 -06: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 10:23:48 -06:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected eth wire msg: %s", pretty.Sdump(msg))
|
2021-04-23 11:14:39 -05:00
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
|
|
|
|
// Check if all txs received.
|
|
|
|
allReceived := func() bool {
|
|
|
|
for _, tx := range txs {
|
|
|
|
if !got[tx.Hash()] {
|
|
|
|
return false
|
|
|
|
}
|
2021-04-23 11:14:39 -05:00
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
return true
|
2021-04-23 11:14:39 -05:00
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
if allReceived() {
|
|
|
|
return nil
|
2021-04-23 11:14:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 05:59:17 -05:00
|
|
|
return errors.New("timed out waiting for txs")
|
2020-11-30 08:23:48 -06:00
|
|
|
}
|
|
|
|
|
2024-02-15 12:43:37 -06:00
|
|
|
func (s *Suite) sendInvalidTxs(t *utesting.T, txs []*types.Transaction) error {
|
2023-12-20 10:23:48 -06:00
|
|
|
// Open sending conn.
|
|
|
|
sendConn, err := s.dial()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-23 11:14:39 -05:00
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
defer sendConn.Close()
|
|
|
|
if err = sendConn.peer(s.chain, nil); err != nil {
|
|
|
|
return fmt.Errorf("peering failed: %v", err)
|
2021-04-23 04:15:42 -05:00
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
sendConn.SetDeadline(time.Now().Add(timeout))
|
2021-04-23 04:15:42 -05:00
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// Open receiving conn.
|
|
|
|
recvConn, err := s.dial()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-05-25 16:09:11 -05:00
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
defer recvConn.Close()
|
|
|
|
if err = recvConn.peer(s.chain, nil); err != nil {
|
|
|
|
return fmt.Errorf("peering failed: %v", err)
|
2020-11-30 08:23:48 -06:00
|
|
|
}
|
2023-12-20 10:23:48 -06:00
|
|
|
recvConn.SetDeadline(time.Now().Add(timeout))
|
2020-11-30 08:23:48 -06:00
|
|
|
|
2023-12-20 10:23:48 -06: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 08:23:48 -06:00
|
|
|
}
|
|
|
|
|
2023-12-20 10:23:48 -06:00
|
|
|
// Make map of invalid txs.
|
|
|
|
invalids := make(map[common.Hash]struct{})
|
|
|
|
for _, tx := range txs {
|
|
|
|
invalids[tx.Hash()] = struct{}{}
|
2021-04-23 11:14:39 -05:00
|
|
|
}
|
|
|
|
|
2024-01-08 13:31:22 -06:00
|
|
|
// Get responses.
|
2023-12-20 10:23:48 -06: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 07:49:19 -06:00
|
|
|
case *eth.NewPooledTransactionHashesPacket:
|
2023-12-20 10:23:48 -06:00
|
|
|
for _, hash := range msg.Hashes {
|
|
|
|
if _, ok := invalids[hash]; ok {
|
|
|
|
return fmt.Errorf("received bad tx: %s", hash)
|
|
|
|
}
|
|
|
|
}
|
2024-02-15 12:43:37 -06: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 10:23:48 -06:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected eth message: %v", pretty.Sdump(msg))
|
2020-11-30 08:23:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|