Skip to content

Commit 716864d

Browse files
authored
cmd/devp2p/internal/ethtest: improve eth test suite (#21615)
This fixes issues with the protocol handshake and status exchange and adds support for responding to GetBlockHeaders requests.
1 parent e43d827 commit 716864d

6 files changed

Lines changed: 487 additions & 147 deletions

File tree

‎cmd/devp2p/internal/ethtest/chain.go‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Copyright 2020 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
117
package ethtest
218

319
import (
@@ -68,6 +84,43 @@ func (c *Chain) Head() *types.Block {
6884
return c.blocks[c.Len()-1]
6985
}
7086

87+
func (c *Chain) GetHeaders(req GetBlockHeaders) (BlockHeaders, error) {
88+
if req.Amount < 1 {
89+
return nil, fmt.Errorf("no block headers requested")
90+
}
91+
92+
headers := make(BlockHeaders, req.Amount)
93+
var blockNumber uint64
94+
95+
// range over blocks to check if our chain has the requested header
96+
for _, block := range c.blocks {
97+
if block.Hash() == req.Origin.Hash || block.Number().Uint64() == req.Origin.Number {
98+
headers[0] = block.Header()
99+
blockNumber = block.Number().Uint64()
100+
}
101+
}
102+
if headers[0] == nil {
103+
return nil, fmt.Errorf("no headers found for given origin number %v, hash %v", req.Origin.Number, req.Origin.Hash)
104+
}
105+
106+
if req.Reverse {
107+
for i := 1; i < int(req.Amount); i++ {
108+
blockNumber -= (1 - req.Skip)
109+
headers[i] = c.blocks[blockNumber].Header()
110+
111+
}
112+
113+
return headers, nil
114+
}
115+
116+
for i := 1; i < int(req.Amount); i++ {
117+
blockNumber += (1 + req.Skip)
118+
headers[i] = c.blocks[blockNumber].Header()
119+
}
120+
121+
return headers, nil
122+
}
123+
71124
// loadChain takes the given chain.rlp file, and decodes and returns
72125
// the blocks from the file.
73126
func loadChain(chainfile string, genesis string) (*Chain, error) {
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright 2020 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package ethtest
18+
19+
import (
20+
"path/filepath"
21+
"strconv"
22+
"testing"
23+
24+
"github.com/ethereum/go-ethereum/p2p"
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
// TestEthProtocolNegotiation tests whether the test suite
29+
// can negotiate the highest eth protocol in a status message exchange
30+
func TestEthProtocolNegotiation(t *testing.T) {
31+
var tests = []struct {
32+
conn *Conn
33+
caps []p2p.Cap
34+
expected uint32
35+
}{
36+
{
37+
conn: &Conn{},
38+
caps: []p2p.Cap{
39+
{Name: "eth", Version: 63},
40+
{Name: "eth", Version: 64},
41+
{Name: "eth", Version: 65},
42+
},
43+
expected: uint32(65),
44+
},
45+
{
46+
conn: &Conn{},
47+
caps: []p2p.Cap{
48+
{Name: "eth", Version: 0},
49+
{Name: "eth", Version: 89},
50+
{Name: "eth", Version: 65},
51+
},
52+
expected: uint32(65),
53+
},
54+
{
55+
conn: &Conn{},
56+
caps: []p2p.Cap{
57+
{Name: "eth", Version: 63},
58+
{Name: "eth", Version: 64},
59+
{Name: "wrongProto", Version: 65},
60+
},
61+
expected: uint32(64),
62+
},
63+
}
64+
65+
for i, tt := range tests {
66+
t.Run(strconv.Itoa(i), func(t *testing.T) {
67+
tt.conn.negotiateEthProtocol(tt.caps)
68+
assert.Equal(t, tt.expected, uint32(tt.conn.ethProtocolVersion))
69+
})
70+
}
71+
}
72+
73+
// TestChain_GetHeaders tests whether the test suite can correctly
74+
// respond to a GetBlockHeaders request from a node.
75+
func TestChain_GetHeaders(t *testing.T) {
76+
chainFile, err := filepath.Abs("./testdata/chain.rlp.gz")
77+
if err != nil {
78+
t.Fatal(err)
79+
}
80+
genesisFile, err := filepath.Abs("./testdata/genesis.json")
81+
if err != nil {
82+
t.Fatal(err)
83+
}
84+
85+
chain, err := loadChain(chainFile, genesisFile)
86+
if err != nil {
87+
t.Fatal(err)
88+
}
89+
90+
var tests = []struct {
91+
req GetBlockHeaders
92+
expected BlockHeaders
93+
}{
94+
{
95+
req: GetBlockHeaders{
96+
Origin: hashOrNumber{
97+
Number: uint64(2),
98+
},
99+
Amount: uint64(5),
100+
Skip: 1,
101+
Reverse: false,
102+
},
103+
expected: BlockHeaders{
104+
chain.blocks[2].Header(),
105+
chain.blocks[4].Header(),
106+
chain.blocks[6].Header(),
107+
chain.blocks[8].Header(),
108+
chain.blocks[10].Header(),
109+
},
110+
},
111+
{
112+
req: GetBlockHeaders{
113+
Origin: hashOrNumber{
114+
Number: uint64(chain.Len() - 1),
115+
},
116+
Amount: uint64(3),
117+
Skip: 0,
118+
Reverse: true,
119+
},
120+
expected: BlockHeaders{
121+
chain.blocks[chain.Len()-1].Header(),
122+
chain.blocks[chain.Len()-2].Header(),
123+
chain.blocks[chain.Len()-3].Header(),
124+
},
125+
},
126+
{
127+
req: GetBlockHeaders{
128+
Origin: hashOrNumber{
129+
Hash: chain.Head().Hash(),
130+
},
131+
Amount: uint64(1),
132+
Skip: 0,
133+
Reverse: false,
134+
},
135+
expected: BlockHeaders{
136+
chain.Head().Header(),
137+
},
138+
},
139+
}
140+
141+
for i, tt := range tests {
142+
t.Run(strconv.Itoa(i), func(t *testing.T) {
143+
headers, err := chain.GetHeaders(tt.req)
144+
if err != nil {
145+
t.Fatal(err)
146+
}
147+
assert.Equal(t, headers, tt.expected)
148+
})
149+
}
150+
}

0 commit comments

Comments
 (0)