2020-10-07 10:22:44 -05:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-09-23 08:18:17 -05:00
|
|
|
package ethtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-03-20 13:50:44 -05:00
|
|
|
"strings"
|
2020-11-04 10:36:56 -06:00
|
|
|
"time"
|
2020-09-23 08:18:17 -05:00
|
|
|
|
2020-11-04 10:36:56 -06:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2020-11-30 08:23:48 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2020-09-23 08:18:17 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-02-16 08:23:03 -06:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
2020-09-23 08:18:17 -05:00
|
|
|
"github.com/ethereum/go-ethereum/internal/utesting"
|
2020-11-24 09:09:17 -06:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2020-09-23 08:18:17 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/rlpx"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-11-04 10:36:56 -06:00
|
|
|
var pretty = spew.ConfigState{
|
|
|
|
Indent: " ",
|
|
|
|
DisableCapacities: true,
|
|
|
|
DisablePointerAddresses: true,
|
|
|
|
SortKeys: true,
|
|
|
|
}
|
|
|
|
|
2020-11-30 08:23:48 -06:00
|
|
|
var timeout = 20 * time.Second
|
|
|
|
|
2020-09-23 08:18:17 -05:00
|
|
|
// Suite represents a structure used to test the eth
|
|
|
|
// protocol of a node(s).
|
|
|
|
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 11:36:01 -06:00
|
|
|
func NewSuite(dest *enode.Node, chainfile string, genesisfile string) (*Suite, error) {
|
2020-09-23 08:18:17 -05:00
|
|
|
chain, err := loadChain(chainfile, genesisfile)
|
|
|
|
if err != nil {
|
2021-02-25 11:36:01 -06:00
|
|
|
return nil, err
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
return &Suite{
|
|
|
|
Dest: dest,
|
|
|
|
chain: chain.Shorten(1000),
|
|
|
|
fullChain: chain,
|
2021-02-25 11:36:01 -06:00
|
|
|
}, nil
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
|
2021-03-19 09:15:39 -05:00
|
|
|
func (s *Suite) AllEthTests() []utesting.Test {
|
2020-09-23 08:18:17 -05:00
|
|
|
return []utesting.Test{
|
2021-02-25 11:36:01 -06:00
|
|
|
// status
|
2021-04-23 04:15:42 -05:00
|
|
|
{Name: "TestStatus", Fn: s.TestStatus},
|
|
|
|
{Name: "TestStatus_66", Fn: s.TestStatus_66},
|
2021-02-25 11:36:01 -06:00
|
|
|
// get block headers
|
2021-04-23 04:15:42 -05:00
|
|
|
{Name: "TestGetBlockHeaders", Fn: s.TestGetBlockHeaders},
|
|
|
|
{Name: "TestGetBlockHeaders_66", Fn: s.TestGetBlockHeaders_66},
|
2021-02-25 11:36:01 -06:00
|
|
|
{Name: "TestSimultaneousRequests_66", Fn: s.TestSimultaneousRequests_66},
|
|
|
|
{Name: "TestSameRequestID_66", Fn: s.TestSameRequestID_66},
|
|
|
|
{Name: "TestZeroRequestID_66", Fn: s.TestZeroRequestID_66},
|
|
|
|
// get block bodies
|
2021-04-23 04:15:42 -05:00
|
|
|
{Name: "TestGetBlockBodies", Fn: s.TestGetBlockBodies},
|
|
|
|
{Name: "TestGetBlockBodies_66", Fn: s.TestGetBlockBodies_66},
|
2021-02-25 11:36:01 -06:00
|
|
|
// broadcast
|
2021-04-23 04:15:42 -05:00
|
|
|
{Name: "TestBroadcast", Fn: s.TestBroadcast},
|
|
|
|
{Name: "TestBroadcast_66", Fn: s.TestBroadcast_66},
|
2020-11-24 09:09:17 -06:00
|
|
|
{Name: "TestLargeAnnounce", Fn: s.TestLargeAnnounce},
|
2021-02-25 11:36:01 -06:00
|
|
|
{Name: "TestLargeAnnounce_66", Fn: s.TestLargeAnnounce_66},
|
2021-03-20 13:50:44 -05:00
|
|
|
{Name: "TestOldAnnounce", Fn: s.TestOldAnnounce},
|
|
|
|
{Name: "TestOldAnnounce_66", Fn: s.TestOldAnnounce_66},
|
2021-02-25 11:36:01 -06:00
|
|
|
// malicious handshakes + status
|
2020-11-24 09:09:17 -06:00
|
|
|
{Name: "TestMaliciousHandshake", Fn: s.TestMaliciousHandshake},
|
|
|
|
{Name: "TestMaliciousStatus", Fn: s.TestMaliciousStatus},
|
2021-02-25 11:36:01 -06:00
|
|
|
{Name: "TestMaliciousHandshake_66", Fn: s.TestMaliciousHandshake_66},
|
2021-04-23 04:15:42 -05:00
|
|
|
{Name: "TestMaliciousStatus_66", Fn: s.TestMaliciousStatus_66},
|
2021-02-25 11:36:01 -06:00
|
|
|
// test transactions
|
2021-04-23 04:15:42 -05:00
|
|
|
{Name: "TestTransaction", Fn: s.TestTransaction},
|
|
|
|
{Name: "TestTransaction_66", Fn: s.TestTransaction_66},
|
|
|
|
{Name: "TestMaliciousTx", Fn: s.TestMaliciousTx},
|
|
|
|
{Name: "TestMaliciousTx_66", Fn: s.TestMaliciousTx_66},
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:15:39 -05:00
|
|
|
func (s *Suite) EthTests() []utesting.Test {
|
|
|
|
return []utesting.Test{
|
2021-04-23 04:15:42 -05:00
|
|
|
{Name: "TestStatus", Fn: s.TestStatus},
|
|
|
|
{Name: "TestGetBlockHeaders", Fn: s.TestGetBlockHeaders},
|
|
|
|
{Name: "TestGetBlockBodies", Fn: s.TestGetBlockBodies},
|
|
|
|
{Name: "TestBroadcast", Fn: s.TestBroadcast},
|
2021-03-19 09:15:39 -05:00
|
|
|
{Name: "TestLargeAnnounce", Fn: s.TestLargeAnnounce},
|
|
|
|
{Name: "TestMaliciousHandshake", Fn: s.TestMaliciousHandshake},
|
|
|
|
{Name: "TestMaliciousStatus", Fn: s.TestMaliciousStatus},
|
2021-04-23 04:15:42 -05:00
|
|
|
{Name: "TestTransaction", Fn: s.TestTransaction},
|
|
|
|
{Name: "TestMaliciousTx", Fn: s.TestMaliciousTx},
|
2021-03-19 09:15:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) Eth66Tests() []utesting.Test {
|
|
|
|
return []utesting.Test{
|
|
|
|
// only proceed with eth66 test suite if node supports eth 66 protocol
|
2021-04-23 04:15:42 -05:00
|
|
|
{Name: "TestStatus_66", Fn: s.TestStatus_66},
|
|
|
|
{Name: "TestGetBlockHeaders_66", Fn: s.TestGetBlockHeaders_66},
|
2021-03-19 09:15:39 -05:00
|
|
|
{Name: "TestSimultaneousRequests_66", Fn: s.TestSimultaneousRequests_66},
|
|
|
|
{Name: "TestSameRequestID_66", Fn: s.TestSameRequestID_66},
|
|
|
|
{Name: "TestZeroRequestID_66", Fn: s.TestZeroRequestID_66},
|
2021-04-23 04:15:42 -05:00
|
|
|
{Name: "TestGetBlockBodies_66", Fn: s.TestGetBlockBodies_66},
|
|
|
|
{Name: "TestBroadcast_66", Fn: s.TestBroadcast_66},
|
2021-03-19 09:15:39 -05:00
|
|
|
{Name: "TestLargeAnnounce_66", Fn: s.TestLargeAnnounce_66},
|
|
|
|
{Name: "TestMaliciousHandshake_66", Fn: s.TestMaliciousHandshake_66},
|
2021-04-23 04:15:42 -05:00
|
|
|
{Name: "TestMaliciousStatus_66", Fn: s.TestMaliciousStatus_66},
|
|
|
|
{Name: "TestTransaction_66", Fn: s.TestTransaction_66},
|
|
|
|
{Name: "TestMaliciousTx_66", Fn: s.TestMaliciousTx_66},
|
2021-03-19 09:15:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 08:18:17 -05:00
|
|
|
// TestStatus attempts to connect to the given node and exchange
|
|
|
|
// a status message with it, and then check to make sure
|
|
|
|
// the chain head is correct.
|
|
|
|
func (s *Suite) TestStatus(t *utesting.T) {
|
|
|
|
conn, err := s.dial()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not dial: %v", err)
|
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
defer conn.Close()
|
2020-09-23 08:18:17 -05:00
|
|
|
// get protoHandshake
|
|
|
|
conn.handshake(t)
|
|
|
|
// get status
|
2020-11-24 09:09:17 -06:00
|
|
|
switch msg := conn.statusExchange(t, s.chain, nil).(type) {
|
2020-09-23 08:18:17 -05:00
|
|
|
case *Status:
|
2020-11-04 10:36:56 -06:00
|
|
|
t.Logf("got status message: %s", pretty.Sdump(msg))
|
2020-09-23 08:18:17 -05:00
|
|
|
default:
|
2020-11-04 10:36:56 -06:00
|
|
|
t.Fatalf("unexpected: %s", pretty.Sdump(msg))
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 09:09:17 -06:00
|
|
|
// TestMaliciousStatus sends a status package with a large total difficulty.
|
|
|
|
func (s *Suite) TestMaliciousStatus(t *utesting.T) {
|
|
|
|
conn, err := s.dial()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not dial: %v", err)
|
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
defer conn.Close()
|
2020-11-24 09:09:17 -06:00
|
|
|
// get protoHandshake
|
|
|
|
conn.handshake(t)
|
|
|
|
status := &Status{
|
2021-03-19 09:15:39 -05:00
|
|
|
ProtocolVersion: uint32(conn.negotiatedProtoVersion),
|
2020-11-24 09:09:17 -06:00
|
|
|
NetworkID: s.chain.chainConfig.ChainID.Uint64(),
|
|
|
|
TD: largeNumber(2),
|
|
|
|
Head: s.chain.blocks[s.chain.Len()-1].Hash(),
|
|
|
|
Genesis: s.chain.blocks[0].Hash(),
|
|
|
|
ForkID: s.chain.ForkID(),
|
|
|
|
}
|
|
|
|
// get status
|
|
|
|
switch msg := conn.statusExchange(t, s.chain, status).(type) {
|
|
|
|
case *Status:
|
|
|
|
t.Logf("%+v\n", msg)
|
|
|
|
default:
|
|
|
|
t.Fatalf("expected status, got: %#v ", msg)
|
|
|
|
}
|
|
|
|
// wait for disconnect
|
|
|
|
switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
|
|
|
|
case *Disconnect:
|
|
|
|
case *Error:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
t.Fatalf("expected disconnect, got: %s", pretty.Sdump(msg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 08:18:17 -05:00
|
|
|
// TestGetBlockHeaders tests whether the given node can respond to
|
|
|
|
// a `GetBlockHeaders` request and that the response is accurate.
|
|
|
|
func (s *Suite) TestGetBlockHeaders(t *utesting.T) {
|
|
|
|
conn, err := s.dial()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not dial: %v", err)
|
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
defer conn.Close()
|
2020-09-23 08:18:17 -05:00
|
|
|
|
|
|
|
conn.handshake(t)
|
2020-11-24 09:09:17 -06:00
|
|
|
conn.statusExchange(t, s.chain, nil)
|
2020-09-23 08:18:17 -05:00
|
|
|
|
|
|
|
// get block headers
|
|
|
|
req := &GetBlockHeaders{
|
2021-02-16 08:23:03 -06:00
|
|
|
Origin: eth.HashOrNumber{
|
2020-09-23 08:18:17 -05:00
|
|
|
Hash: s.chain.blocks[1].Hash(),
|
|
|
|
},
|
|
|
|
Amount: 2,
|
|
|
|
Skip: 1,
|
|
|
|
Reverse: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := conn.Write(req); err != nil {
|
|
|
|
t.Fatalf("could not write to connection: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-11-04 10:36:56 -06:00
|
|
|
switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
|
2020-09-23 08:18:17 -05:00
|
|
|
case *BlockHeaders:
|
2021-02-16 08:23:03 -06:00
|
|
|
headers := *msg
|
|
|
|
for _, header := range headers {
|
2020-09-23 08:18:17 -05:00
|
|
|
num := header.Number.Uint64()
|
2021-02-25 11:36:01 -06:00
|
|
|
t.Logf("received header (%d): %s", num, pretty.Sdump(header.Hash()))
|
2020-09-23 08:18:17 -05:00
|
|
|
assert.Equal(t, s.chain.blocks[int(num)].Header(), header)
|
|
|
|
}
|
|
|
|
default:
|
2020-11-04 10:36:56 -06:00
|
|
|
t.Fatalf("unexpected: %s", pretty.Sdump(msg))
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestGetBlockBodies tests whether the given node can respond to
|
|
|
|
// a `GetBlockBodies` request and that the response is accurate.
|
|
|
|
func (s *Suite) TestGetBlockBodies(t *utesting.T) {
|
|
|
|
conn, err := s.dial()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not dial: %v", err)
|
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
defer conn.Close()
|
2020-09-23 08:18:17 -05:00
|
|
|
|
|
|
|
conn.handshake(t)
|
2020-11-24 09:09:17 -06:00
|
|
|
conn.statusExchange(t, s.chain, nil)
|
2020-09-23 08:18:17 -05:00
|
|
|
// create block bodies request
|
2021-02-16 08:23:03 -06:00
|
|
|
req := &GetBlockBodies{
|
|
|
|
s.chain.blocks[54].Hash(),
|
|
|
|
s.chain.blocks[75].Hash(),
|
|
|
|
}
|
2020-09-23 08:18:17 -05:00
|
|
|
if err := conn.Write(req); err != nil {
|
|
|
|
t.Fatalf("could not write to connection: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-11-04 10:36:56 -06:00
|
|
|
switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
|
2020-09-23 08:18:17 -05:00
|
|
|
case *BlockBodies:
|
2020-11-04 10:36:56 -06:00
|
|
|
t.Logf("received %d block bodies", len(*msg))
|
2020-09-23 08:18:17 -05:00
|
|
|
default:
|
2020-11-04 10:36:56 -06:00
|
|
|
t.Fatalf("unexpected: %s", pretty.Sdump(msg))
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestBroadcast tests whether a block announcement is correctly
|
|
|
|
// propagated to the given node's peer(s).
|
|
|
|
func (s *Suite) TestBroadcast(t *utesting.T) {
|
2020-11-24 09:09:17 -06:00
|
|
|
sendConn, receiveConn := s.setupConnection(t), s.setupConnection(t)
|
2021-04-23 04:15:42 -05:00
|
|
|
defer sendConn.Close()
|
|
|
|
defer receiveConn.Close()
|
|
|
|
|
2020-11-24 09:09:17 -06:00
|
|
|
nextBlock := len(s.chain.blocks)
|
|
|
|
blockAnnouncement := &NewBlock{
|
|
|
|
Block: s.fullChain.blocks[nextBlock],
|
|
|
|
TD: s.fullChain.TD(nextBlock + 1),
|
|
|
|
}
|
|
|
|
s.testAnnounce(t, sendConn, receiveConn, blockAnnouncement)
|
|
|
|
// update test suite chain
|
|
|
|
s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
|
|
|
|
// wait for client to update its chain
|
|
|
|
if err := receiveConn.waitForBlock(s.chain.Head()); err != nil {
|
|
|
|
t.Fatal(err)
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
2020-11-24 09:09:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestMaliciousHandshake tries to send malicious data during the handshake.
|
|
|
|
func (s *Suite) TestMaliciousHandshake(t *utesting.T) {
|
|
|
|
conn, err := s.dial()
|
2020-09-23 08:18:17 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not dial: %v", err)
|
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
defer conn.Close()
|
2020-11-24 09:09:17 -06:00
|
|
|
// write hello to client
|
|
|
|
pub0 := crypto.FromECDSAPub(&conn.ourKey.PublicKey)[1:]
|
|
|
|
handshakes := []*Hello{
|
|
|
|
{
|
|
|
|
Version: 5,
|
|
|
|
Caps: []p2p.Cap{
|
|
|
|
{Name: largeString(2), Version: 64},
|
|
|
|
},
|
|
|
|
ID: pub0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Version: 5,
|
|
|
|
Caps: []p2p.Cap{
|
|
|
|
{Name: "eth", Version: 64},
|
|
|
|
{Name: "eth", Version: 65},
|
|
|
|
},
|
|
|
|
ID: append(pub0, byte(0)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Version: 5,
|
|
|
|
Caps: []p2p.Cap{
|
|
|
|
{Name: "eth", Version: 64},
|
|
|
|
{Name: "eth", Version: 65},
|
|
|
|
},
|
|
|
|
ID: append(pub0, pub0...),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Version: 5,
|
|
|
|
Caps: []p2p.Cap{
|
|
|
|
{Name: "eth", Version: 64},
|
|
|
|
{Name: "eth", Version: 65},
|
|
|
|
},
|
|
|
|
ID: largeBuffer(2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Version: 5,
|
|
|
|
Caps: []p2p.Cap{
|
|
|
|
{Name: largeString(2), Version: 64},
|
|
|
|
},
|
|
|
|
ID: largeBuffer(2),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, handshake := range handshakes {
|
2020-11-30 08:23:48 -06:00
|
|
|
t.Logf("Testing malicious handshake %v\n", i)
|
2020-11-24 09:09:17 -06:00
|
|
|
// Init the handshake
|
|
|
|
if err := conn.Write(handshake); err != nil {
|
|
|
|
t.Fatalf("could not write to connection: %v", err)
|
|
|
|
}
|
|
|
|
// check that the peer disconnected
|
|
|
|
timeout := 20 * time.Second
|
|
|
|
// Discard one hello
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
|
|
|
|
case *Disconnect:
|
|
|
|
case *Error:
|
|
|
|
case *Hello:
|
|
|
|
// Hello's are send concurrently, so ignore them
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected: %s", pretty.Sdump(msg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Dial for the next round
|
|
|
|
conn, err = s.dial()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not dial: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 08:18:17 -05:00
|
|
|
|
2020-11-24 09:09:17 -06:00
|
|
|
// TestLargeAnnounce tests the announcement mechanism with a large block.
|
|
|
|
func (s *Suite) TestLargeAnnounce(t *utesting.T) {
|
|
|
|
nextBlock := len(s.chain.blocks)
|
|
|
|
blocks := []*NewBlock{
|
|
|
|
{
|
|
|
|
Block: largeBlock(),
|
|
|
|
TD: s.fullChain.TD(nextBlock + 1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Block: s.fullChain.blocks[nextBlock],
|
|
|
|
TD: largeNumber(2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Block: largeBlock(),
|
|
|
|
TD: largeNumber(2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Block: s.fullChain.blocks[nextBlock],
|
|
|
|
TD: s.fullChain.TD(nextBlock + 1),
|
|
|
|
},
|
|
|
|
}
|
2020-09-23 08:18:17 -05:00
|
|
|
|
2020-11-24 09:09:17 -06:00
|
|
|
for i, blockAnnouncement := range blocks[0:3] {
|
2020-11-30 08:23:48 -06:00
|
|
|
t.Logf("Testing malicious announcement: %v\n", i)
|
2020-11-24 09:09:17 -06:00
|
|
|
sendConn := s.setupConnection(t)
|
|
|
|
if err := sendConn.Write(blockAnnouncement); err != nil {
|
|
|
|
t.Fatalf("could not write to connection: %v", err)
|
|
|
|
}
|
|
|
|
// Invalid announcement, check that peer disconnected
|
2021-04-23 04:15:42 -05:00
|
|
|
switch msg := sendConn.ReadAndServe(s.chain, time.Second*8).(type) {
|
2020-11-24 09:09:17 -06:00
|
|
|
case *Disconnect:
|
|
|
|
case *Error:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected: %s wanted disconnect", pretty.Sdump(msg))
|
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
sendConn.Close()
|
2020-11-24 09:09:17 -06:00
|
|
|
}
|
|
|
|
// Test the last block as a valid block
|
|
|
|
sendConn := s.setupConnection(t)
|
|
|
|
receiveConn := s.setupConnection(t)
|
2021-04-23 04:15:42 -05:00
|
|
|
defer sendConn.Close()
|
|
|
|
defer receiveConn.Close()
|
|
|
|
|
2020-11-24 09:09:17 -06:00
|
|
|
s.testAnnounce(t, sendConn, receiveConn, blocks[3])
|
|
|
|
// update test suite chain
|
|
|
|
s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
|
|
|
|
// wait for client to update its chain
|
|
|
|
if err := receiveConn.waitForBlock(s.fullChain.blocks[nextBlock]); err != nil {
|
|
|
|
t.Fatal(err)
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
2020-11-24 09:09:17 -06:00
|
|
|
}
|
|
|
|
|
2021-03-20 13:50:44 -05:00
|
|
|
func (s *Suite) TestOldAnnounce(t *utesting.T) {
|
2021-04-23 04:15:42 -05:00
|
|
|
sendConn, recvConn := s.setupConnection(t), s.setupConnection(t)
|
|
|
|
defer sendConn.Close()
|
|
|
|
defer recvConn.Close()
|
|
|
|
|
|
|
|
s.oldAnnounce(t, sendConn, recvConn)
|
2021-03-20 13:50:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) oldAnnounce(t *utesting.T, sendConn, receiveConn *Conn) {
|
|
|
|
oldBlockAnnounce := &NewBlock{
|
|
|
|
Block: s.chain.blocks[len(s.chain.blocks)/2],
|
|
|
|
TD: s.chain.blocks[len(s.chain.blocks)/2].Difficulty(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sendConn.Write(oldBlockAnnounce); err != nil {
|
|
|
|
t.Fatalf("could not write to connection: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-04-23 04:15:42 -05:00
|
|
|
switch msg := receiveConn.ReadAndServe(s.chain, time.Second*8).(type) {
|
2021-03-20 13:50:44 -05:00
|
|
|
case *NewBlock:
|
2021-04-23 04:15:42 -05:00
|
|
|
block := *msg
|
|
|
|
if block.Block.Hash() == oldBlockAnnounce.Block.Hash() {
|
|
|
|
t.Fatalf("unexpected: block propagated: %s", pretty.Sdump(msg))
|
|
|
|
}
|
2021-03-20 13:50:44 -05:00
|
|
|
case *NewBlockHashes:
|
2021-04-23 04:15:42 -05:00
|
|
|
hashes := *msg
|
|
|
|
for _, hash := range hashes {
|
|
|
|
if hash.Hash == oldBlockAnnounce.Block.Hash() {
|
|
|
|
t.Fatalf("unexpected: block announced: %s", pretty.Sdump(msg))
|
|
|
|
}
|
|
|
|
}
|
2021-03-20 13:50:44 -05:00
|
|
|
case *Error:
|
|
|
|
errMsg := *msg
|
|
|
|
// check to make sure error is timeout (propagation didn't come through == test successful)
|
|
|
|
if !strings.Contains(errMsg.String(), "timeout") {
|
|
|
|
t.Fatalf("unexpected error: %v", pretty.Sdump(msg))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected: %s", pretty.Sdump(msg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 09:09:17 -06:00
|
|
|
func (s *Suite) testAnnounce(t *utesting.T, sendConn, receiveConn *Conn, blockAnnouncement *NewBlock) {
|
|
|
|
// Announce the block.
|
2020-09-23 08:18:17 -05:00
|
|
|
if err := sendConn.Write(blockAnnouncement); err != nil {
|
|
|
|
t.Fatalf("could not write to connection: %v", err)
|
|
|
|
}
|
2020-11-24 09:09:17 -06:00
|
|
|
s.waitAnnounce(t, receiveConn, blockAnnouncement)
|
|
|
|
}
|
2020-09-23 08:18:17 -05:00
|
|
|
|
2020-11-24 09:09:17 -06:00
|
|
|
func (s *Suite) waitAnnounce(t *utesting.T, conn *Conn, blockAnnouncement *NewBlock) {
|
2020-11-04 10:36:56 -06:00
|
|
|
timeout := 20 * time.Second
|
2020-11-24 09:09:17 -06:00
|
|
|
switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
|
2020-09-23 08:18:17 -05:00
|
|
|
case *NewBlock:
|
2020-11-04 10:36:56 -06:00
|
|
|
t.Logf("received NewBlock message: %s", pretty.Sdump(msg.Block))
|
|
|
|
assert.Equal(t,
|
|
|
|
blockAnnouncement.Block.Header(), msg.Block.Header(),
|
|
|
|
"wrong block header in announcement",
|
|
|
|
)
|
|
|
|
assert.Equal(t,
|
|
|
|
blockAnnouncement.TD, msg.TD,
|
|
|
|
"wrong TD in announcement",
|
|
|
|
)
|
2020-09-23 08:18:17 -05:00
|
|
|
case *NewBlockHashes:
|
2021-02-16 08:23:03 -06:00
|
|
|
message := *msg
|
|
|
|
t.Logf("received NewBlockHashes message: %s", pretty.Sdump(message))
|
|
|
|
assert.Equal(t, blockAnnouncement.Block.Hash(), message[0].Hash,
|
2020-11-04 10:36:56 -06:00
|
|
|
"wrong block hash in announcement",
|
|
|
|
)
|
2020-09-23 08:18:17 -05:00
|
|
|
default:
|
2020-11-04 10:36:56 -06:00
|
|
|
t.Fatalf("unexpected: %s", pretty.Sdump(msg))
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
2020-11-24 09:09:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) setupConnection(t *utesting.T) *Conn {
|
|
|
|
// create conn
|
|
|
|
sendConn, err := s.dial()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not dial: %v", err)
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
2020-11-24 09:09:17 -06:00
|
|
|
sendConn.handshake(t)
|
|
|
|
sendConn.statusExchange(t, s.chain, nil)
|
|
|
|
return sendConn
|
2020-09-23 08:18:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// dial attempts to dial the given node and perform a handshake,
|
|
|
|
// returning the created Conn if successful.
|
|
|
|
func (s *Suite) dial() (*Conn, error) {
|
|
|
|
var conn Conn
|
2021-02-25 11:36:01 -06:00
|
|
|
// dial
|
2020-09-23 08:18:17 -05:00
|
|
|
fd, err := net.Dial("tcp", fmt.Sprintf("%v:%d", s.Dest.IP(), s.Dest.TCP()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conn.Conn = rlpx.NewConn(fd, s.Dest.Pubkey())
|
|
|
|
// do encHandshake
|
|
|
|
conn.ourKey, _ = crypto.GenerateKey()
|
|
|
|
_, err = conn.Handshake(conn.ourKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-25 11:36:01 -06:00
|
|
|
// set default p2p capabilities
|
|
|
|
conn.caps = []p2p.Cap{
|
|
|
|
{Name: "eth", Version: 64},
|
|
|
|
{Name: "eth", Version: 65},
|
|
|
|
}
|
2021-03-19 09:15:39 -05:00
|
|
|
conn.ourHighestProtoVersion = 65
|
2020-09-23 08:18:17 -05:00
|
|
|
return &conn, nil
|
|
|
|
}
|
2020-11-30 08:23:48 -06:00
|
|
|
|
|
|
|
func (s *Suite) TestTransaction(t *utesting.T) {
|
|
|
|
tests := []*types.Transaction{
|
|
|
|
getNextTxFromChain(t, s),
|
|
|
|
unknownTx(t, s),
|
|
|
|
}
|
|
|
|
for i, tx := range tests {
|
|
|
|
t.Logf("Testing tx propagation: %v\n", i)
|
|
|
|
sendSuccessfulTx(t, s, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestMaliciousTx(t *utesting.T) {
|
2021-04-23 04:15:42 -05:00
|
|
|
badTxs := []*types.Transaction{
|
2020-11-30 08:23:48 -06:00
|
|
|
getOldTxFromChain(t, s),
|
|
|
|
invalidNonceTx(t, s),
|
|
|
|
hugeAmount(t, s),
|
|
|
|
hugeGasPrice(t, s),
|
|
|
|
hugeData(t, s),
|
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
sendConn := s.setupConnection(t)
|
|
|
|
defer sendConn.Close()
|
|
|
|
// set up receiving connection before sending txs to make sure
|
|
|
|
// no announcements are missed
|
|
|
|
recvConn := s.setupConnection(t)
|
|
|
|
defer recvConn.Close()
|
|
|
|
|
|
|
|
for i, tx := range badTxs {
|
2020-11-30 08:23:48 -06:00
|
|
|
t.Logf("Testing malicious tx propagation: %v\n", i)
|
2021-04-23 04:15:42 -05:00
|
|
|
if err := sendConn.Write(&Transactions{tx}); err != nil {
|
|
|
|
t.Fatalf("could not write to connection: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-11-30 08:23:48 -06:00
|
|
|
}
|
2021-04-23 04:15:42 -05:00
|
|
|
// check to make sure bad txs aren't propagated
|
|
|
|
waitForTxPropagation(t, s, badTxs, recvConn)
|
2020-11-30 08:23:48 -06:00
|
|
|
}
|