Skip to content

Commit 5c3051e

Browse files
committed
[release/1.4.10] core: gracefully handle missing homestead block config
(cherry picked from commit 9e56811)
1 parent 3dd46bc commit 5c3051e

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

cmd/geth/genesis_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU 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+
// go-ethereum 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 General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"io/ioutil"
21+
"os"
22+
"path/filepath"
23+
"testing"
24+
)
25+
26+
var customGenesisTests = []struct {
27+
genesis string
28+
query string
29+
result string
30+
}{
31+
// Plain genesis file without anything extra
32+
{
33+
genesis: `{
34+
"alloc" : {},
35+
"coinbase" : "0x0000000000000000000000000000000000000000",
36+
"difficulty" : "0x20000",
37+
"extraData" : "",
38+
"gasLimit" : "0x2fefd8",
39+
"nonce" : "0x0000000000000042",
40+
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
41+
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
42+
"timestamp" : "0x00"
43+
}`,
44+
query: "eth.getBlock(0).nonce",
45+
result: "0x0000000000000042",
46+
},
47+
// Genesis file with an empty chain configuration (ensure missing fields work)
48+
{
49+
genesis: `{
50+
"alloc" : {},
51+
"coinbase" : "0x0000000000000000000000000000000000000000",
52+
"difficulty" : "0x20000",
53+
"extraData" : "",
54+
"gasLimit" : "0x2fefd8",
55+
"nonce" : "0x0000000000000042",
56+
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
57+
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
58+
"timestamp" : "0x00",
59+
"config" : {}
60+
}`,
61+
query: "eth.getBlock(0).nonce",
62+
result: "0x0000000000000042",
63+
},
64+
// Genesis file with specific chain configurations
65+
{
66+
genesis: `{
67+
"alloc" : {},
68+
"coinbase" : "0x0000000000000000000000000000000000000000",
69+
"difficulty" : "0x20000",
70+
"extraData" : "",
71+
"gasLimit" : "0x2fefd8",
72+
"nonce" : "0x0000000000000042",
73+
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
74+
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
75+
"timestamp" : "0x00",
76+
"config" : {
77+
"homesteadBlock" : 314,
78+
},
79+
}`,
80+
query: "eth.getBlock(0).nonce",
81+
result: "0x0000000000000042",
82+
},
83+
}
84+
85+
// Tests that initializing Geth with a custom genesis block and chain definitions
86+
// work properly.
87+
func TestCustomGenesis(t *testing.T) {
88+
for i, tt := range customGenesisTests {
89+
// Create a temporary data directory to use and inspect later
90+
datadir := tmpdir(t)
91+
defer os.RemoveAll(datadir)
92+
93+
// Initialize the data directory with the custom genesis block
94+
json := filepath.Join(datadir, "genesis.json")
95+
if err := ioutil.WriteFile(json, []byte(tt.genesis), 0600); err != nil {
96+
t.Fatalf("test %d: failed to write genesis file: %v", i, err)
97+
}
98+
runGeth(t, "--datadir", datadir, "init", json).cmd.Wait()
99+
100+
// Query the custom genesis block
101+
geth := runGeth(t, "--datadir", datadir, "--maxpeers", "0", "--nodiscover", "--nat", "none", "--ipcdisable", "--exec", tt.query, "console")
102+
geth.expectRegexp(tt.result)
103+
geth.expectExit()
104+
}
105+
}

core/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type ChainConfig struct {
3939

4040
// IsHomestead returns whether num is either equal to the homestead block or greater.
4141
func (c *ChainConfig) IsHomestead(num *big.Int) bool {
42-
if num == nil {
42+
if c.HomesteadBlock == nil || num == nil {
4343
return false
4444
}
4545
return num.Cmp(c.HomesteadBlock) >= 0

0 commit comments

Comments
 (0)