2020-10-07 17:22:44 +02:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2022-05-24 20:39:40 +02:00
|
|
|
// This file is part of go-ethereum.
|
2020-10-07 17:22:44 +02: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-10-07 17:22:44 +02: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-10-07 17:22:44 +02: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-10-07 17:22:44 +02: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-10-07 17:22:44 +02:00
|
|
|
|
|
|
|
package ethtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-02 14:48:55 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2021-02-16 15:23:03 +01:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
2020-10-07 17:22:44 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestEthProtocolNegotiation tests whether the test suite
|
|
|
|
// can negotiate the highest eth protocol in a status message exchange
|
|
|
|
func TestEthProtocolNegotiation(t *testing.T) {
|
2023-11-20 12:52:14 +03:00
|
|
|
t.Parallel()
|
2020-10-07 17:22:44 +02:00
|
|
|
var tests = []struct {
|
|
|
|
conn *Conn
|
|
|
|
caps []p2p.Cap
|
|
|
|
expected uint32
|
|
|
|
}{
|
|
|
|
{
|
2021-03-19 14:15:39 +00:00
|
|
|
conn: &Conn{
|
|
|
|
ourHighestProtoVersion: 65,
|
|
|
|
},
|
|
|
|
caps: []p2p.Cap{
|
|
|
|
{Name: "eth", Version: 63},
|
|
|
|
{Name: "eth", Version: 64},
|
|
|
|
{Name: "eth", Version: 65},
|
|
|
|
},
|
|
|
|
expected: uint32(65),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conn: &Conn{
|
|
|
|
ourHighestProtoVersion: 65,
|
|
|
|
},
|
|
|
|
caps: []p2p.Cap{
|
|
|
|
{Name: "eth", Version: 63},
|
|
|
|
{Name: "eth", Version: 64},
|
|
|
|
{Name: "eth", Version: 65},
|
|
|
|
},
|
|
|
|
expected: uint32(65),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conn: &Conn{
|
|
|
|
ourHighestProtoVersion: 65,
|
|
|
|
},
|
2020-10-07 17:22:44 +02:00
|
|
|
caps: []p2p.Cap{
|
|
|
|
{Name: "eth", Version: 63},
|
|
|
|
{Name: "eth", Version: 64},
|
|
|
|
{Name: "eth", Version: 65},
|
|
|
|
},
|
|
|
|
expected: uint32(65),
|
|
|
|
},
|
|
|
|
{
|
2021-03-19 14:15:39 +00:00
|
|
|
conn: &Conn{
|
|
|
|
ourHighestProtoVersion: 64,
|
|
|
|
},
|
|
|
|
caps: []p2p.Cap{
|
|
|
|
{Name: "eth", Version: 63},
|
|
|
|
{Name: "eth", Version: 64},
|
|
|
|
{Name: "eth", Version: 65},
|
|
|
|
},
|
|
|
|
expected: 64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conn: &Conn{
|
|
|
|
ourHighestProtoVersion: 65,
|
|
|
|
},
|
2020-10-07 17:22:44 +02:00
|
|
|
caps: []p2p.Cap{
|
|
|
|
{Name: "eth", Version: 0},
|
|
|
|
{Name: "eth", Version: 89},
|
|
|
|
{Name: "eth", Version: 65},
|
|
|
|
},
|
|
|
|
expected: uint32(65),
|
|
|
|
},
|
|
|
|
{
|
2021-03-19 14:15:39 +00:00
|
|
|
conn: &Conn{
|
|
|
|
ourHighestProtoVersion: 64,
|
|
|
|
},
|
|
|
|
caps: []p2p.Cap{
|
|
|
|
{Name: "eth", Version: 63},
|
|
|
|
{Name: "eth", Version: 64},
|
|
|
|
{Name: "wrongProto", Version: 65},
|
|
|
|
},
|
|
|
|
expected: uint32(64),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conn: &Conn{
|
|
|
|
ourHighestProtoVersion: 65,
|
|
|
|
},
|
2020-10-07 17:22:44 +02:00
|
|
|
caps: []p2p.Cap{
|
|
|
|
{Name: "eth", Version: 63},
|
|
|
|
{Name: "eth", Version: 64},
|
|
|
|
{Name: "wrongProto", Version: 65},
|
|
|
|
},
|
|
|
|
expected: uint32(64),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
|
|
|
tt.conn.negotiateEthProtocol(tt.caps)
|
2021-03-19 14:15:39 +00:00
|
|
|
assert.Equal(t, tt.expected, uint32(tt.conn.negotiatedProtoVersion))
|
2020-10-07 17:22:44 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 09:23:48 -07:00
|
|
|
// TestChainGetHeaders tests whether the test suite can correctly
|
2020-10-07 17:22:44 +02:00
|
|
|
// respond to a GetBlockHeaders request from a node.
|
2023-12-20 09:23:48 -07:00
|
|
|
func TestChainGetHeaders(t *testing.T) {
|
2023-11-20 12:52:14 +03:00
|
|
|
t.Parallel()
|
2023-12-20 09:23:48 -07:00
|
|
|
|
|
|
|
dir, err := filepath.Abs("./testdata")
|
2020-10-07 17:22:44 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-12-20 09:23:48 -07:00
|
|
|
chain, err := NewChain(dir)
|
2020-10-07 17:22:44 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests = []struct {
|
2023-12-20 09:23:48 -07:00
|
|
|
req eth.GetBlockHeadersPacket
|
2022-08-02 14:48:55 +02:00
|
|
|
expected []*types.Header
|
2020-10-07 17:22:44 +02:00
|
|
|
}{
|
|
|
|
{
|
2023-12-20 09:23:48 -07:00
|
|
|
req: eth.GetBlockHeadersPacket{
|
2023-10-03 15:03:19 +03:00
|
|
|
GetBlockHeadersRequest: ð.GetBlockHeadersRequest{
|
2022-08-02 14:48:55 +02:00
|
|
|
Origin: eth.HashOrNumber{Number: uint64(2)},
|
|
|
|
Amount: uint64(5),
|
|
|
|
Skip: 1,
|
|
|
|
Reverse: false,
|
2020-10-07 17:22:44 +02:00
|
|
|
},
|
|
|
|
},
|
2022-08-02 14:48:55 +02:00
|
|
|
expected: []*types.Header{
|
2020-10-07 17:22:44 +02:00
|
|
|
chain.blocks[2].Header(),
|
|
|
|
chain.blocks[4].Header(),
|
|
|
|
chain.blocks[6].Header(),
|
|
|
|
chain.blocks[8].Header(),
|
|
|
|
chain.blocks[10].Header(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-12-20 09:23:48 -07:00
|
|
|
req: eth.GetBlockHeadersPacket{
|
2023-10-03 15:03:19 +03:00
|
|
|
GetBlockHeadersRequest: ð.GetBlockHeadersRequest{
|
2022-08-02 14:48:55 +02:00
|
|
|
Origin: eth.HashOrNumber{Number: uint64(chain.Len() - 1)},
|
|
|
|
Amount: uint64(3),
|
|
|
|
Skip: 0,
|
|
|
|
Reverse: true,
|
2020-10-07 17:22:44 +02:00
|
|
|
},
|
|
|
|
},
|
2022-08-02 14:48:55 +02:00
|
|
|
expected: []*types.Header{
|
2020-10-07 17:22:44 +02:00
|
|
|
chain.blocks[chain.Len()-1].Header(),
|
|
|
|
chain.blocks[chain.Len()-2].Header(),
|
|
|
|
chain.blocks[chain.Len()-3].Header(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-12-20 09:23:48 -07:00
|
|
|
req: eth.GetBlockHeadersPacket{
|
2023-10-03 15:03:19 +03:00
|
|
|
GetBlockHeadersRequest: ð.GetBlockHeadersRequest{
|
2022-08-02 14:48:55 +02:00
|
|
|
Origin: eth.HashOrNumber{Hash: chain.Head().Hash()},
|
|
|
|
Amount: uint64(1),
|
|
|
|
Skip: 0,
|
|
|
|
Reverse: false,
|
2020-10-07 17:22:44 +02:00
|
|
|
},
|
|
|
|
},
|
2022-08-02 14:48:55 +02:00
|
|
|
expected: []*types.Header{
|
2020-10-07 17:22:44 +02:00
|
|
|
chain.Head().Header(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
2022-08-02 14:48:55 +02:00
|
|
|
headers, err := chain.GetHeaders(&tt.req)
|
2020-10-07 17:22:44 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, headers, tt.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|