2020-10-07 18:22:44 +03:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2022-05-24 21:39:40 +03:00
|
|
|
// This file is part of go-ethereum.
|
2020-10-07 18:22:44 +03:00
|
|
|
//
|
2022-05-24 21:39:40 +03: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-10-07 18:22:44 +03:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2022-05-24 21:39:40 +03:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2020-10-07 18:22:44 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2022-05-24 21:39:40 +03:00
|
|
|
// GNU General Public License for more details.
|
2020-10-07 18:22:44 +03:00
|
|
|
//
|
2022-05-24 21:39:40 +03: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-10-07 18:22:44 +03:00
|
|
|
|
2020-09-23 16:18:17 +03:00
|
|
|
package ethtest
|
|
|
|
|
|
|
|
import (
|
2020-11-04 19:36:56 +03:00
|
|
|
"time"
|
2020-09-23 16:18:17 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-02-16 17:23:03 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
2020-09-23 16:18:17 +03:00
|
|
|
"github.com/ethereum/go-ethereum/internal/utesting"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
)
|
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// Suite represents a structure used to test a node's conformance
|
|
|
|
// to the eth protocol.
|
2020-09-23 16:18:17 +03:00
|
|
|
type Suite struct {
|
|
|
|
Dest *enode.Node
|
|
|
|
|
|
|
|
chain *Chain
|
|
|
|
fullChain *Chain
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSuite creates and returns a new eth-test suite that can
|
|
|
|
// be used to test the given node against the given blockchain
|
|
|
|
// data.
|
2021-02-25 20:36:01 +03:00
|
|
|
func NewSuite(dest *enode.Node, chainfile string, genesisfile string) (*Suite, error) {
|
2020-09-23 16:18:17 +03:00
|
|
|
chain, err := loadChain(chainfile, genesisfile)
|
|
|
|
if err != nil {
|
2021-02-25 20:36:01 +03:00
|
|
|
return nil, err
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
|
|
|
return &Suite{
|
|
|
|
Dest: dest,
|
|
|
|
chain: chain.Shorten(1000),
|
|
|
|
fullChain: chain,
|
2021-02-25 20:36:01 +03:00
|
|
|
}, nil
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
func (s *Suite) EthTests() []utesting.Test {
|
2020-09-23 16:18:17 +03:00
|
|
|
return []utesting.Test{
|
2021-02-25 20:36:01 +03:00
|
|
|
// status
|
2022-08-02 15:48:55 +03:00
|
|
|
{Name: "TestStatus", Fn: s.TestStatus},
|
2021-02-25 20:36:01 +03:00
|
|
|
// get block headers
|
2022-08-02 15:48:55 +03:00
|
|
|
{Name: "TestGetBlockHeaders", Fn: s.TestGetBlockHeaders},
|
|
|
|
{Name: "TestSimultaneousRequests", Fn: s.TestSimultaneousRequests},
|
|
|
|
{Name: "TestSameRequestID", Fn: s.TestSameRequestID},
|
|
|
|
{Name: "TestZeroRequestID", Fn: s.TestZeroRequestID},
|
2021-02-25 20:36:01 +03:00
|
|
|
// get block bodies
|
2022-08-02 15:48:55 +03:00
|
|
|
{Name: "TestGetBlockBodies", Fn: s.TestGetBlockBodies},
|
2021-02-25 20:36:01 +03:00
|
|
|
// broadcast
|
2022-08-02 15:48:55 +03:00
|
|
|
{Name: "TestBroadcast", Fn: s.TestBroadcast},
|
|
|
|
{Name: "TestLargeAnnounce", Fn: s.TestLargeAnnounce},
|
|
|
|
{Name: "TestOldAnnounce", Fn: s.TestOldAnnounce},
|
|
|
|
{Name: "TestBlockHashAnnounce", Fn: s.TestBlockHashAnnounce},
|
2021-02-25 20:36:01 +03:00
|
|
|
// malicious handshakes + status
|
2022-08-02 15:48:55 +03:00
|
|
|
{Name: "TestMaliciousHandshake", Fn: s.TestMaliciousHandshake},
|
|
|
|
{Name: "TestMaliciousStatus", Fn: s.TestMaliciousStatus},
|
2021-02-25 20:36:01 +03:00
|
|
|
// test transactions
|
2022-08-02 15:48:55 +03:00
|
|
|
{Name: "TestTransaction", Fn: s.TestTransaction},
|
|
|
|
{Name: "TestMaliciousTx", Fn: s.TestMaliciousTx},
|
|
|
|
{Name: "TestLargeTxRequest", Fn: s.TestLargeTxRequest},
|
|
|
|
{Name: "TestNewPooledTxs", Fn: s.TestNewPooledTxs},
|
2021-03-19 17:15:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 17:24:32 +03:00
|
|
|
func (s *Suite) SnapTests() []utesting.Test {
|
|
|
|
return []utesting.Test{
|
|
|
|
{Name: "TestSnapStatus", Fn: s.TestSnapStatus},
|
|
|
|
{Name: "TestSnapAccountRange", Fn: s.TestSnapGetAccountRange},
|
|
|
|
{Name: "TestSnapGetByteCodes", Fn: s.TestSnapGetByteCodes},
|
|
|
|
{Name: "TestSnapGetTrieNodes", Fn: s.TestSnapTrieNodes},
|
|
|
|
{Name: "TestSnapGetStorageRanges", Fn: s.TestSnapGetStorageRanges},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestStatus attempts to connect to the given node and exchange
|
|
|
|
// a status message with it on the eth protocol.
|
|
|
|
func (s *Suite) TestStatus(t *utesting.T) {
|
2020-09-23 16:18:17 +03:00
|
|
|
conn, err := s.dial()
|
|
|
|
if err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("dial failed: %v", err)
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
2021-04-23 12:15:42 +03:00
|
|
|
defer conn.Close()
|
2021-05-26 00:09:11 +03:00
|
|
|
if err := conn.peer(s.chain, nil); err != nil {
|
|
|
|
t.Fatalf("peering failed: %v", err)
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestGetBlockHeaders tests whether the given node can respond to
|
|
|
|
// an eth `GetBlockHeaders` request and that the response is accurate.
|
|
|
|
func (s *Suite) TestGetBlockHeaders(t *utesting.T) {
|
2020-09-23 16:18:17 +03:00
|
|
|
conn, err := s.dial()
|
|
|
|
if err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("dial failed: %v", err)
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
2021-04-23 12:15:42 +03:00
|
|
|
defer conn.Close()
|
2021-05-26 00:09:11 +03:00
|
|
|
if err = conn.peer(s.chain, nil); err != nil {
|
|
|
|
t.Fatalf("peering failed: %v", err)
|
|
|
|
}
|
|
|
|
// write request
|
2020-09-23 16:18:17 +03:00
|
|
|
req := &GetBlockHeaders{
|
2022-08-02 15:48:55 +03:00
|
|
|
GetBlockHeadersPacket: ð.GetBlockHeadersPacket{
|
|
|
|
Origin: eth.HashOrNumber{Hash: s.chain.blocks[1].Hash()},
|
|
|
|
Amount: 2,
|
|
|
|
Skip: 1,
|
|
|
|
Reverse: false,
|
2020-09-23 16:18:17 +03:00
|
|
|
},
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
headers, err := conn.headersRequest(req, s.chain, 33)
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not get block headers: %v", err)
|
|
|
|
}
|
|
|
|
// check for correct headers
|
2022-08-02 15:48:55 +03:00
|
|
|
expected, err := s.chain.GetHeaders(req)
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get headers for given request: %v", err)
|
|
|
|
}
|
|
|
|
if !headersMatch(expected, headers) {
|
|
|
|
t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected, headers)
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 16:18:17 +03:00
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestSimultaneousRequests sends two simultaneous `GetBlockHeader` requests from
|
2021-05-26 00:09:11 +03:00
|
|
|
// the same connection with different request IDs and checks to make sure the node
|
|
|
|
// responds with the correct headers per request.
|
2022-08-02 15:48:55 +03:00
|
|
|
func (s *Suite) TestSimultaneousRequests(t *utesting.T) {
|
2021-05-26 00:09:11 +03:00
|
|
|
// create a connection
|
2022-08-02 15:48:55 +03:00
|
|
|
conn, err := s.dial()
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dial failed: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
if err := conn.peer(s.chain, nil); err != nil {
|
|
|
|
t.Fatalf("peering failed: %v", err)
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// create two requests
|
2022-08-02 15:48:55 +03:00
|
|
|
req1 := &GetBlockHeaders{
|
2021-05-26 00:09:11 +03:00
|
|
|
RequestId: uint64(111),
|
|
|
|
GetBlockHeadersPacket: ð.GetBlockHeadersPacket{
|
|
|
|
Origin: eth.HashOrNumber{
|
|
|
|
Hash: s.chain.blocks[1].Hash(),
|
|
|
|
},
|
|
|
|
Amount: 2,
|
|
|
|
Skip: 1,
|
|
|
|
Reverse: false,
|
|
|
|
},
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
req2 := &GetBlockHeaders{
|
2021-05-26 00:09:11 +03:00
|
|
|
RequestId: uint64(222),
|
|
|
|
GetBlockHeadersPacket: ð.GetBlockHeadersPacket{
|
|
|
|
Origin: eth.HashOrNumber{
|
|
|
|
Hash: s.chain.blocks[1].Hash(),
|
|
|
|
},
|
|
|
|
Amount: 4,
|
|
|
|
Skip: 1,
|
|
|
|
Reverse: false,
|
|
|
|
},
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// write the first request
|
2022-08-02 15:48:55 +03:00
|
|
|
if err := conn.Write(req1); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("failed to write to connection: %v", err)
|
|
|
|
}
|
|
|
|
// write the second request
|
2022-08-02 15:48:55 +03:00
|
|
|
if err := conn.Write(req2); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("failed to write to connection: %v", err)
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// wait for responses
|
|
|
|
msg := conn.waitForResponse(s.chain, timeout, req1.RequestId)
|
2022-08-02 15:48:55 +03:00
|
|
|
headers1, ok := msg.(*BlockHeaders)
|
2021-05-26 00:09:11 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected %s", pretty.Sdump(msg))
|
|
|
|
}
|
|
|
|
msg = conn.waitForResponse(s.chain, timeout, req2.RequestId)
|
2022-08-02 15:48:55 +03:00
|
|
|
headers2, ok := msg.(*BlockHeaders)
|
2021-05-26 00:09:11 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected %s", pretty.Sdump(msg))
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// check received headers for accuracy
|
2022-08-02 15:48:55 +03:00
|
|
|
expected1, err := s.chain.GetHeaders(req1)
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get expected headers for request 1: %v", err)
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
expected2, err := s.chain.GetHeaders(req2)
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get expected headers for request 2: %v", err)
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
if !headersMatch(expected1, headers1.BlockHeadersPacket) {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected1, headers1)
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
if !headersMatch(expected2, headers2.BlockHeadersPacket) {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected2, headers2)
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 16:18:17 +03:00
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestSameRequestID sends two requests with the same request ID to a
|
2021-05-26 00:09:11 +03:00
|
|
|
// single node.
|
2022-08-02 15:48:55 +03:00
|
|
|
func (s *Suite) TestSameRequestID(t *utesting.T) {
|
|
|
|
conn, err := s.dial()
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dial failed: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
if err := conn.peer(s.chain, nil); err != nil {
|
|
|
|
t.Fatalf("peering failed: %v", err)
|
|
|
|
}
|
|
|
|
// create requests
|
|
|
|
reqID := uint64(1234)
|
2022-08-02 15:48:55 +03:00
|
|
|
request1 := &GetBlockHeaders{
|
2021-05-26 00:09:11 +03:00
|
|
|
RequestId: reqID,
|
|
|
|
GetBlockHeadersPacket: ð.GetBlockHeadersPacket{
|
|
|
|
Origin: eth.HashOrNumber{
|
|
|
|
Number: 1,
|
|
|
|
},
|
|
|
|
Amount: 2,
|
|
|
|
},
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
request2 := &GetBlockHeaders{
|
2021-05-26 00:09:11 +03:00
|
|
|
RequestId: reqID,
|
|
|
|
GetBlockHeadersPacket: ð.GetBlockHeadersPacket{
|
|
|
|
Origin: eth.HashOrNumber{
|
|
|
|
Number: 33,
|
|
|
|
},
|
|
|
|
Amount: 2,
|
|
|
|
},
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// write the requests
|
2022-08-02 15:48:55 +03:00
|
|
|
if err = conn.Write(request1); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("failed to write to connection: %v", err)
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
if err = conn.Write(request2); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("failed to write to connection: %v", err)
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// wait for responses
|
|
|
|
msg := conn.waitForResponse(s.chain, timeout, reqID)
|
2022-08-02 15:48:55 +03:00
|
|
|
headers1, ok := msg.(*BlockHeaders)
|
2021-05-26 00:09:11 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected %s", pretty.Sdump(msg))
|
|
|
|
}
|
|
|
|
msg = conn.waitForResponse(s.chain, timeout, reqID)
|
2022-08-02 15:48:55 +03:00
|
|
|
headers2, ok := msg.(*BlockHeaders)
|
2021-05-26 00:09:11 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected %s", pretty.Sdump(msg))
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// check if headers match
|
2022-08-02 15:48:55 +03:00
|
|
|
expected1, err := s.chain.GetHeaders(request1)
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get expected block headers: %v", err)
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
expected2, err := s.chain.GetHeaders(request2)
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get expected block headers: %v", err)
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
if !headersMatch(expected1, headers1.BlockHeadersPacket) {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected1, headers1)
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
if !headersMatch(expected2, headers2.BlockHeadersPacket) {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected2, headers2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestZeroRequestID checks that a message with a request ID of zero is still handled
|
2021-05-26 00:09:11 +03:00
|
|
|
// by the node.
|
2022-08-02 15:48:55 +03:00
|
|
|
func (s *Suite) TestZeroRequestID(t *utesting.T) {
|
|
|
|
conn, err := s.dial()
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dial failed: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
if err := conn.peer(s.chain, nil); err != nil {
|
|
|
|
t.Fatalf("peering failed: %v", err)
|
|
|
|
}
|
|
|
|
req := &GetBlockHeaders{
|
2022-08-02 15:48:55 +03:00
|
|
|
GetBlockHeadersPacket: ð.GetBlockHeadersPacket{
|
|
|
|
Origin: eth.HashOrNumber{Number: 0},
|
|
|
|
Amount: 2,
|
2021-05-26 00:09:11 +03:00
|
|
|
},
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
headers, err := conn.headersRequest(req, s.chain, 0)
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get block headers: %v", err)
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
expected, err := s.chain.GetHeaders(req)
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get expected block headers: %v", err)
|
|
|
|
}
|
|
|
|
if !headersMatch(expected, headers) {
|
|
|
|
t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected, headers)
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestGetBlockBodies tests whether the given node can respond to
|
2020-09-23 16:18:17 +03:00
|
|
|
// a `GetBlockBodies` request and that the response is accurate.
|
2022-08-02 15:48:55 +03:00
|
|
|
func (s *Suite) TestGetBlockBodies(t *utesting.T) {
|
2020-09-23 16:18:17 +03:00
|
|
|
conn, err := s.dial()
|
|
|
|
if err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("dial failed: %v", err)
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
2021-04-23 12:15:42 +03:00
|
|
|
defer conn.Close()
|
2021-05-26 00:09:11 +03:00
|
|
|
if err := conn.peer(s.chain, nil); err != nil {
|
|
|
|
t.Fatalf("peering failed: %v", err)
|
|
|
|
}
|
2020-09-23 16:18:17 +03:00
|
|
|
// create block bodies request
|
2021-02-16 17:23:03 +03:00
|
|
|
req := &GetBlockBodies{
|
2021-05-26 00:09:11 +03:00
|
|
|
RequestId: uint64(55),
|
|
|
|
GetBlockBodiesPacket: eth.GetBlockBodiesPacket{
|
|
|
|
s.chain.blocks[54].Hash(),
|
|
|
|
s.chain.blocks[75].Hash(),
|
|
|
|
},
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
if err := conn.Write(req); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("could not write to connection: %v", err)
|
|
|
|
}
|
|
|
|
// wait for block bodies response
|
|
|
|
msg := conn.waitForResponse(s.chain, timeout, req.RequestId)
|
2022-08-02 15:48:55 +03:00
|
|
|
resp, ok := msg.(*BlockBodies)
|
2021-05-26 00:09:11 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected: %s", pretty.Sdump(msg))
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
bodies := resp.BlockBodiesPacket
|
|
|
|
t.Logf("received %d block bodies", len(bodies))
|
|
|
|
if len(bodies) != len(req.GetBlockBodiesPacket) {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("wrong bodies in response: expected %d bodies, "+
|
2022-08-02 15:48:55 +03:00
|
|
|
"got %d", len(req.GetBlockBodiesPacket), len(bodies))
|
2021-05-26 00:09:11 +03:00
|
|
|
}
|
2021-04-30 23:47:36 +03:00
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestBroadcast tests whether a block announcement is correctly
|
|
|
|
// propagated to the node's peers.
|
|
|
|
func (s *Suite) TestBroadcast(t *utesting.T) {
|
|
|
|
if err := s.sendNextBlock(); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("block broadcast failed: %v", err)
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
2020-11-24 18:09:17 +03:00
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestLargeAnnounce tests the announcement mechanism with a large block.
|
|
|
|
func (s *Suite) TestLargeAnnounce(t *utesting.T) {
|
2020-11-24 18:09:17 +03:00
|
|
|
nextBlock := len(s.chain.blocks)
|
|
|
|
blocks := []*NewBlock{
|
|
|
|
{
|
|
|
|
Block: largeBlock(),
|
2021-05-26 00:09:11 +03:00
|
|
|
TD: s.fullChain.TotalDifficultyAt(nextBlock),
|
2020-11-24 18:09:17 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Block: s.fullChain.blocks[nextBlock],
|
|
|
|
TD: largeNumber(2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Block: largeBlock(),
|
|
|
|
TD: largeNumber(2),
|
|
|
|
},
|
|
|
|
}
|
2020-09-23 16:18:17 +03:00
|
|
|
|
2020-11-24 18:09:17 +03:00
|
|
|
for i, blockAnnouncement := range blocks[0:3] {
|
2020-11-30 17:23:48 +03:00
|
|
|
t.Logf("Testing malicious announcement: %v\n", i)
|
2022-08-02 15:48:55 +03:00
|
|
|
conn, err := s.dial()
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dial failed: %v", err)
|
|
|
|
}
|
|
|
|
if err := conn.peer(s.chain, nil); err != nil {
|
|
|
|
t.Fatalf("peering failed: %v", err)
|
|
|
|
}
|
|
|
|
if err := conn.Write(blockAnnouncement); err != nil {
|
2020-11-24 18:09:17 +03:00
|
|
|
t.Fatalf("could not write to connection: %v", err)
|
|
|
|
}
|
|
|
|
// Invalid announcement, check that peer disconnected
|
2022-08-02 15:48:55 +03:00
|
|
|
switch msg := conn.readAndServe(s.chain, 8*time.Second).(type) {
|
2020-11-24 18:09:17 +03:00
|
|
|
case *Disconnect:
|
|
|
|
case *Error:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected: %s wanted disconnect", pretty.Sdump(msg))
|
|
|
|
}
|
2021-05-26 00:09:11 +03:00
|
|
|
conn.Close()
|
2020-11-24 18:09:17 +03:00
|
|
|
}
|
|
|
|
// Test the last block as a valid block
|
2022-08-02 15:48:55 +03:00
|
|
|
if err := s.sendNextBlock(); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("failed to broadcast next block: %v", err)
|
|
|
|
}
|
2020-11-24 18:09:17 +03:00
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestOldAnnounce tests the announcement mechanism with an old block.
|
|
|
|
func (s *Suite) TestOldAnnounce(t *utesting.T) {
|
|
|
|
if err := s.oldAnnounce(); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestBlockHashAnnounce sends a new block hash announcement and expects
|
2021-05-27 12:57:49 +03:00
|
|
|
// the node to perform a `GetBlockHeaders` request.
|
2022-08-02 15:48:55 +03:00
|
|
|
func (s *Suite) TestBlockHashAnnounce(t *utesting.T) {
|
|
|
|
if err := s.hashAnnounce(); err != nil {
|
2021-05-27 12:57:49 +03:00
|
|
|
t.Fatalf("block hash announcement failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestMaliciousHandshake tries to send malicious data during the handshake.
|
|
|
|
func (s *Suite) TestMaliciousHandshake(t *utesting.T) {
|
|
|
|
if err := s.maliciousHandshakes(t); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-03-20 21:50:44 +03:00
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestMaliciousStatus sends a status package with a large total difficulty.
|
|
|
|
func (s *Suite) TestMaliciousStatus(t *utesting.T) {
|
2021-05-26 00:09:11 +03:00
|
|
|
conn, err := s.dial()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dial failed: %v", err)
|
2021-03-20 21:50:44 +03:00
|
|
|
}
|
2021-05-26 00:09:11 +03:00
|
|
|
defer conn.Close()
|
2021-03-20 21:50:44 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
if err := s.maliciousStatus(conn); err != nil {
|
|
|
|
t.Fatal(err)
|
2021-03-20 21:50:44 +03:00
|
|
|
}
|
2021-05-26 00:09:11 +03:00
|
|
|
}
|
2021-03-20 21:50:44 +03:00
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestTransaction sends a valid transaction to the node and
|
2021-05-26 00:09:11 +03:00
|
|
|
// checks if the transaction gets propagated.
|
2022-08-02 15:48:55 +03:00
|
|
|
func (s *Suite) TestTransaction(t *utesting.T) {
|
|
|
|
if err := s.sendSuccessfulTxs(t); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatal(err)
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
2020-11-24 18:09:17 +03:00
|
|
|
}
|
2020-09-23 16:18:17 +03:00
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestMaliciousTx sends several invalid transactions and tests whether
|
2021-05-26 00:09:11 +03:00
|
|
|
// the node will propagate them.
|
2022-08-02 15:48:55 +03:00
|
|
|
func (s *Suite) TestMaliciousTx(t *utesting.T) {
|
|
|
|
if err := s.sendMaliciousTxs(t); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestLargeTxRequest tests whether a node can fulfill a large GetPooledTransactions
|
2021-05-26 00:09:11 +03:00
|
|
|
// request.
|
2022-08-02 15:48:55 +03:00
|
|
|
func (s *Suite) TestLargeTxRequest(t *utesting.T) {
|
2021-05-26 00:09:11 +03:00
|
|
|
// send the next block to ensure the node is no longer syncing and
|
|
|
|
// is able to accept txs
|
2022-08-02 15:48:55 +03:00
|
|
|
if err := s.sendNextBlock(); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("failed to send next block: %v", err)
|
|
|
|
}
|
|
|
|
// send 2000 transactions to the node
|
|
|
|
hashMap, txs, err := generateTxs(s, 2000)
|
2020-09-23 16:18:17 +03:00
|
|
|
if err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("failed to generate transactions: %v", err)
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
2021-05-26 00:09:11 +03:00
|
|
|
if err = sendMultipleSuccessfulTxs(t, s, txs); err != nil {
|
|
|
|
t.Fatalf("failed to send multiple txs: %v", err)
|
|
|
|
}
|
|
|
|
// set up connection to receive to ensure node is peered with the receiving connection
|
|
|
|
// before tx request is sent
|
2022-08-02 15:48:55 +03:00
|
|
|
conn, err := s.dial()
|
2020-09-23 16:18:17 +03:00
|
|
|
if err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("dial failed: %v", err)
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
2021-05-26 00:09:11 +03:00
|
|
|
defer conn.Close()
|
|
|
|
if err = conn.peer(s.chain, nil); err != nil {
|
|
|
|
t.Fatalf("peering failed: %v", err)
|
|
|
|
}
|
|
|
|
// create and send pooled tx request
|
|
|
|
hashes := make([]common.Hash, 0)
|
|
|
|
for _, hash := range hashMap {
|
|
|
|
hashes = append(hashes, hash)
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
getTxReq := &GetPooledTransactions{
|
2021-05-26 00:09:11 +03:00
|
|
|
RequestId: 1234,
|
|
|
|
GetPooledTransactionsPacket: hashes,
|
|
|
|
}
|
2022-08-02 15:48:55 +03:00
|
|
|
if err = conn.Write(getTxReq); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("could not write to conn: %v", err)
|
|
|
|
}
|
|
|
|
// check that all received transactions match those that were sent to node
|
|
|
|
switch msg := conn.waitForResponse(s.chain, timeout, getTxReq.RequestId).(type) {
|
2022-08-02 15:48:55 +03:00
|
|
|
case *PooledTransactions:
|
|
|
|
for _, gotTx := range msg.PooledTransactionsPacket {
|
2021-05-26 00:09:11 +03:00
|
|
|
if _, exists := hashMap[gotTx.Hash()]; !exists {
|
|
|
|
t.Fatalf("unexpected tx received: %v", gotTx.Hash())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected %s", pretty.Sdump(msg))
|
2021-02-25 20:36:01 +03:00
|
|
|
}
|
2020-09-23 16:18:17 +03:00
|
|
|
}
|
2020-11-30 17:23:48 +03:00
|
|
|
|
2022-08-02 15:48:55 +03:00
|
|
|
// TestNewPooledTxs tests whether a node will do a GetPooledTransactions
|
2021-05-26 00:09:11 +03:00
|
|
|
// request upon receiving a NewPooledTransactionHashes announcement.
|
2022-08-02 15:48:55 +03:00
|
|
|
func (s *Suite) TestNewPooledTxs(t *utesting.T) {
|
2021-05-26 00:09:11 +03:00
|
|
|
// send the next block to ensure the node is no longer syncing and
|
|
|
|
// is able to accept txs
|
2022-08-02 15:48:55 +03:00
|
|
|
if err := s.sendNextBlock(); err != nil {
|
2021-05-26 00:09:11 +03:00
|
|
|
t.Fatalf("failed to send next block: %v", err)
|
2020-11-30 17:23:48 +03:00
|
|
|
}
|
2021-05-27 21:53:33 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// generate 50 txs
|
|
|
|
hashMap, _, err := generateTxs(s, 50)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to generate transactions: %v", err)
|
2020-11-30 17:23:48 +03:00
|
|
|
}
|
2021-05-27 21:53:33 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// create new pooled tx hashes announcement
|
|
|
|
hashes := make([]common.Hash, 0)
|
|
|
|
for _, hash := range hashMap {
|
|
|
|
hashes = append(hashes, hash)
|
|
|
|
}
|
|
|
|
announce := NewPooledTransactionHashes(hashes)
|
2021-05-27 21:53:33 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// send announcement
|
2022-08-02 15:48:55 +03:00
|
|
|
conn, err := s.dial()
|
2021-05-26 00:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("dial failed: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
if err = conn.peer(s.chain, nil); err != nil {
|
|
|
|
t.Fatalf("peering failed: %v", err)
|
|
|
|
}
|
|
|
|
if err = conn.Write(announce); err != nil {
|
|
|
|
t.Fatalf("failed to write to connection: %v", err)
|
|
|
|
}
|
2021-05-27 21:53:33 +03:00
|
|
|
|
2021-05-26 00:09:11 +03:00
|
|
|
// wait for GetPooledTxs request
|
|
|
|
for {
|
2022-08-02 15:48:55 +03:00
|
|
|
msg := conn.readAndServe(s.chain, timeout)
|
2021-05-26 00:09:11 +03:00
|
|
|
switch msg := msg.(type) {
|
2022-08-02 15:48:55 +03:00
|
|
|
case *GetPooledTransactions:
|
|
|
|
if len(msg.GetPooledTransactionsPacket) != len(hashes) {
|
|
|
|
t.Fatalf("unexpected number of txs requested: wanted %d, got %d", len(hashes), len(msg.GetPooledTransactionsPacket))
|
2021-05-26 00:09:11 +03:00
|
|
|
}
|
|
|
|
return
|
2021-05-27 21:53:33 +03:00
|
|
|
// ignore propagated txs from previous tests
|
2021-05-26 00:09:11 +03:00
|
|
|
case *NewPooledTransactionHashes:
|
2021-05-27 21:53:33 +03:00
|
|
|
continue
|
|
|
|
// ignore block announcements from previous tests
|
|
|
|
case *NewBlockHashes:
|
|
|
|
continue
|
|
|
|
case *NewBlock:
|
2021-05-26 00:09:11 +03:00
|
|
|
continue
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected %s", pretty.Sdump(msg))
|
2021-04-23 12:15:42 +03:00
|
|
|
}
|
2020-11-30 17:23:48 +03:00
|
|
|
}
|
|
|
|
}
|