Skip to content

Commit 95f720f

Browse files
authored
cmd/devp2p/internal/ethtest: update test chain (#21742)
The old one was wrong in two ways: the first block in chain.rlp was the genesis block, and the genesis difficulty was below minimum difficulty. This also contains some other fixes to the test.
1 parent 6487c00 commit 95f720f

4 files changed

Lines changed: 37 additions & 30 deletions

File tree

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,43 +124,44 @@ func (c *Chain) GetHeaders(req GetBlockHeaders) (BlockHeaders, error) {
124124
// loadChain takes the given chain.rlp file, and decodes and returns
125125
// the blocks from the file.
126126
func loadChain(chainfile string, genesis string) (*Chain, error) {
127-
// Open the file handle and potentially unwrap the gzip stream
127+
chainConfig, err := ioutil.ReadFile(genesis)
128+
if err != nil {
129+
return nil, err
130+
}
131+
var gen core.Genesis
132+
if err := json.Unmarshal(chainConfig, &gen); err != nil {
133+
return nil, err
134+
}
135+
gblock := gen.ToBlock(nil)
136+
137+
// Load chain.rlp.
128138
fh, err := os.Open(chainfile)
129139
if err != nil {
130140
return nil, err
131141
}
132142
defer fh.Close()
133-
134143
var reader io.Reader = fh
135144
if strings.HasSuffix(chainfile, ".gz") {
136145
if reader, err = gzip.NewReader(reader); err != nil {
137146
return nil, err
138147
}
139148
}
140149
stream := rlp.NewStream(reader, 0)
141-
var blocks []*types.Block
150+
var blocks = make([]*types.Block, 1)
151+
blocks[0] = gblock
142152
for i := 0; ; i++ {
143153
var b types.Block
144154
if err := stream.Decode(&b); err == io.EOF {
145155
break
146156
} else if err != nil {
147-
return nil, fmt.Errorf("at block %d: %v", i, err)
157+
return nil, fmt.Errorf("at block index %d: %v", i, err)
158+
}
159+
if b.NumberU64() != uint64(i+1) {
160+
return nil, fmt.Errorf("block at index %d has wrong number %d", i, b.NumberU64())
148161
}
149162
blocks = append(blocks, &b)
150163
}
151164

152-
// Open the file handle and potentially unwrap the gzip stream
153-
chainConfig, err := ioutil.ReadFile(genesis)
154-
if err != nil {
155-
return nil, err
156-
}
157-
var gen core.Genesis
158-
if err := json.Unmarshal(chainConfig, &gen); err != nil {
159-
return nil, err
160-
}
161-
162-
return &Chain{
163-
blocks: blocks,
164-
chainConfig: gen.Config,
165-
}, nil
165+
c := &Chain{blocks: blocks, chainConfig: gen.Config}
166+
return c, nil
166167
}

‎cmd/devp2p/internal/ethtest/testdata/chain.rlp.gz‎

100755100644
-2.38 KB
Binary file not shown.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"config": {
3-
"chainId": 1,
3+
"chainId": 19763,
44
"homesteadBlock": 0,
55
"eip150Block": 0,
66
"eip155Block": 0,
@@ -11,16 +11,16 @@
1111
"nonce": "0xdeadbeefdeadbeef",
1212
"timestamp": "0x0",
1313
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
14-
"gasLimit": "0x8000000",
15-
"difficulty": "0x10",
14+
"gasLimit": "0x80000000",
15+
"difficulty": "0x20000",
1616
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
1717
"coinbase": "0x0000000000000000000000000000000000000000",
1818
"alloc": {
1919
"71562b71999873db5b286df957af199ec94617f7": {
20-
"balance": "0xf4240"
20+
"balance": "0xffffffff"
2121
}
2222
},
2323
"number": "0x0",
2424
"gasUsed": "0x0",
2525
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
26-
}
26+
}

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,14 @@ func (c *Conn) Write(msg Message) error {
242242
}
243243
_, err = c.Conn.Write(uint64(msg.Code()), payload)
244244
return err
245-
246245
}
247246

248247
// handshake checks to make sure a `HELLO` is received.
249248
func (c *Conn) handshake(t *utesting.T) Message {
250-
// write protoHandshake to client
249+
defer c.SetDeadline(time.Time{})
250+
c.SetDeadline(time.Now().Add(10 * time.Second))
251+
252+
// write hello to client
251253
pub0 := crypto.FromECDSAPub(&c.ourKey.PublicKey)[1:]
252254
ourHandshake := &Hello{
253255
Version: 5,
@@ -260,14 +262,13 @@ func (c *Conn) handshake(t *utesting.T) Message {
260262
if err := c.Write(ourHandshake); err != nil {
261263
t.Fatalf("could not write to connection: %v", err)
262264
}
263-
// read protoHandshake from client
265+
// read hello from client
264266
switch msg := c.Read().(type) {
265267
case *Hello:
266268
// set snappy if version is at least 5
267269
if msg.Version >= 5 {
268270
c.SetSnappy(true)
269271
}
270-
271272
c.negotiateEthProtocol(msg.Caps)
272273
if c.ethProtocolVersion == 0 {
273274
t.Fatalf("unexpected eth protocol version")
@@ -297,9 +298,11 @@ func (c *Conn) negotiateEthProtocol(caps []p2p.Cap) {
297298
// statusExchange performs a `Status` message exchange with the given
298299
// node.
299300
func (c *Conn) statusExchange(t *utesting.T, chain *Chain) Message {
301+
defer c.SetDeadline(time.Time{})
302+
c.SetDeadline(time.Now().Add(20 * time.Second))
303+
300304
// read status message from client
301305
var message Message
302-
303306
loop:
304307
for {
305308
switch msg := c.Read().(type) {
@@ -331,7 +334,7 @@ loop:
331334
// write status message to client
332335
status := Status{
333336
ProtocolVersion: uint32(c.ethProtocolVersion),
334-
NetworkID: 1,
337+
NetworkID: chain.chainConfig.ChainID.Uint64(),
335338
TD: chain.TD(chain.Len()),
336339
Head: chain.blocks[chain.Len()-1].Hash(),
337340
Genesis: chain.blocks[0].Hash(),
@@ -347,12 +350,15 @@ loop:
347350
// waitForBlock waits for confirmation from the client that it has
348351
// imported the given block.
349352
func (c *Conn) waitForBlock(block *types.Block) error {
353+
defer c.SetReadDeadline(time.Time{})
354+
355+
timeout := time.Now().Add(20 * time.Second)
356+
c.SetReadDeadline(timeout)
350357
for {
351358
req := &GetBlockHeaders{Origin: hashOrNumber{Hash: block.Hash()}, Amount: 1}
352359
if err := c.Write(req); err != nil {
353360
return err
354361
}
355-
356362
switch msg := c.Read().(type) {
357363
case *BlockHeaders:
358364
if len(*msg) > 0 {

0 commit comments

Comments
 (0)