Skip to content

Commit 63d293c

Browse files
authored
Merge pull request bnb-chain#3195 from karalabe/fix-testnet-fastsync
cmd/utils, core, params: explicitly pick reprice fork for fast sync
2 parents 79789af + f0dbec0 commit 63d293c

5 files changed

Lines changed: 68 additions & 31 deletions

File tree

‎cmd/geth/dao_test.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,19 @@ func TestDAOInitOldPrivnet(t *testing.T) {
116116
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{}, nil, false)
117117
}
118118
func TestDAODefaultOldPrivnet(t *testing.T) {
119-
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{false, false}}, params.MainNetDAOForkBlock, true)
119+
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{false, false}}, nil, false)
120120
}
121121
func TestDAOSupportOldPrivnet(t *testing.T) {
122-
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{true, false}}, params.MainNetDAOForkBlock, true)
122+
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{true, false}}, nil, true)
123123
}
124124
func TestDAOOpposeOldPrivnet(t *testing.T) {
125-
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{false, true}}, params.MainNetDAOForkBlock, false)
125+
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{false, true}}, nil, false)
126126
}
127127
func TestDAOSwitchToSupportOldPrivnet(t *testing.T) {
128-
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{false, true}, {true, false}}, params.MainNetDAOForkBlock, true)
128+
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{false, true}, {true, false}}, nil, true)
129129
}
130130
func TestDAOSwitchToOpposeOldPrivnet(t *testing.T) {
131-
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{true, false}, {false, true}}, params.MainNetDAOForkBlock, false)
131+
testDAOForkBlockNewChain(t, false, daoOldGenesis, [][2]bool{{true, false}, {false, true}}, nil, false)
132132
}
133133
func TestDAOInitNoForkPrivnet(t *testing.T) {
134134
testDAOForkBlockNewChain(t, false, daoNoForkGenesis, [][2]bool{}, daoGenesisForkBlock, false)

‎cmd/utils/flags.go‎

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -781,27 +781,43 @@ func MakeChainConfigFromDb(ctx *cli.Context, db ethdb.Database) *core.ChainConfi
781781
Fatalf("Could not make chain configuration: %v", err)
782782
}
783783
}
784-
// Set any missing fields due to them being unset or system upgrade
785-
if config.HomesteadBlock == nil {
786-
if ctx.GlobalBool(TestNetFlag.Name) {
787-
config.HomesteadBlock = params.TestNetHomesteadBlock
788-
} else {
789-
config.HomesteadBlock = params.MainNetHomesteadBlock
784+
// Check whether we are allowed to set default config params or not:
785+
// - If no genesis is set, we're running either mainnet or testnet (private nets use `geth init`)
786+
// - If a genesis is already set, ensure we have a configuration for it (mainnet or testnet)
787+
defaults := genesis == nil ||
788+
(genesis.Hash() == params.MainNetGenesisHash && !ctx.GlobalBool(TestNetFlag.Name)) ||
789+
(genesis.Hash() == params.TestNetGenesisHash && ctx.GlobalBool(TestNetFlag.Name))
790+
791+
// Set any missing chainConfig fields due to them being unset or system upgrade
792+
if defaults {
793+
if config.HomesteadBlock == nil {
794+
if ctx.GlobalBool(TestNetFlag.Name) {
795+
config.HomesteadBlock = params.TestNetHomesteadBlock
796+
} else {
797+
config.HomesteadBlock = params.MainNetHomesteadBlock
798+
}
790799
}
791-
}
792-
if config.DAOForkBlock == nil {
793-
if ctx.GlobalBool(TestNetFlag.Name) {
794-
config.DAOForkBlock = params.TestNetDAOForkBlock
795-
} else {
796-
config.DAOForkBlock = params.MainNetDAOForkBlock
800+
if config.DAOForkBlock == nil {
801+
if ctx.GlobalBool(TestNetFlag.Name) {
802+
config.DAOForkBlock = params.TestNetDAOForkBlock
803+
} else {
804+
config.DAOForkBlock = params.MainNetDAOForkBlock
805+
}
806+
config.DAOForkSupport = true
797807
}
798-
config.DAOForkSupport = true
799-
}
800-
if config.HomesteadGasRepriceBlock == nil {
801-
if ctx.GlobalBool(TestNetFlag.Name) {
802-
config.HomesteadGasRepriceBlock = params.TestNetHomesteadGasRepriceBlock
803-
} else {
804-
config.HomesteadGasRepriceBlock = params.MainNetHomesteadGasRepriceBlock
808+
if config.HomesteadGasRepriceBlock == nil {
809+
if ctx.GlobalBool(TestNetFlag.Name) {
810+
config.HomesteadGasRepriceBlock = params.TestNetHomesteadGasRepriceBlock
811+
} else {
812+
config.HomesteadGasRepriceBlock = params.MainNetHomesteadGasRepriceBlock
813+
}
814+
}
815+
if config.HomesteadGasRepriceHash == (common.Hash{}) {
816+
if ctx.GlobalBool(TestNetFlag.Name) {
817+
config.HomesteadGasRepriceHash = params.TestNetHomesteadGasRepriceHash
818+
} else {
819+
config.HomesteadGasRepriceHash = params.MainNetHomesteadGasRepriceHash
820+
}
805821
}
806822
}
807823
// Force override any existing configs if explicitly requested

‎core/block_validator.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,15 @@ func ValidateHeader(config *ChainConfig, pow pow.PoW, header *types.Header, pare
248248
}
249249
}
250250
// If all checks passed, validate the extra-data field for hard forks
251-
return ValidateDAOHeaderExtraData(config, header)
251+
if err := ValidateDAOHeaderExtraData(config, header); err != nil {
252+
return err
253+
}
254+
if config.HomesteadGasRepriceBlock != nil && config.HomesteadGasRepriceBlock.Cmp(header.Number) == 0 {
255+
if config.HomesteadGasRepriceHash != (common.Hash{}) && config.HomesteadGasRepriceHash != header.Hash() {
256+
return ValidationError("Homestead gas reprice fork hash mismatch: have 0x%x, want 0x%x", header.Hash(), config.HomesteadGasRepriceHash)
257+
}
258+
}
259+
return nil
252260
}
253261

254262
// CalcDifficulty is the difficulty adjustment algorithm. It returns

‎core/config.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"math/big"
2222

23+
"github.com/ethereum/go-ethereum/common"
2324
"github.com/ethereum/go-ethereum/core/vm"
2425
"github.com/ethereum/go-ethereum/params"
2526
)
@@ -36,7 +37,8 @@ type ChainConfig struct {
3637
DAOForkBlock *big.Int `json:"daoForkBlock"` // TheDAO hard-fork switch block (nil = no fork)
3738
DAOForkSupport bool `json:"daoForkSupport"` // Whether the nodes supports or opposes the DAO hard-fork
3839

39-
HomesteadGasRepriceBlock *big.Int `json:"homesteadGasRepriceBlock"` // Homestead gas reprice switch block (nil = no fork)
40+
HomesteadGasRepriceBlock *big.Int `json:"homesteadGasRepriceBlock"` // Homestead gas reprice switch block (nil = no fork)
41+
HomesteadGasRepriceHash common.Hash `json:"homesteadGasRepriceHash"` // Homestead gas reprice switch block hash (fast sync aid)
4042

4143
VmConfig vm.Config `json:"-"`
4244
}

‎params/util.go‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,22 @@
1616

1717
package params
1818

19-
import "math/big"
19+
import (
20+
"math/big"
21+
22+
"github.com/ethereum/go-ethereum/common"
23+
)
2024

2125
var (
22-
TestNetHomesteadBlock = big.NewInt(494000) // Testnet homestead block
23-
MainNetHomesteadBlock = big.NewInt(1150000) // Mainnet homestead block
24-
TestNetHomesteadGasRepriceBlock = big.NewInt(1783000) // Test net gas reprice block
25-
MainNetHomesteadGasRepriceBlock = big.NewInt(2463000) // Main net gas reprice block
26+
TestNetGenesisHash = common.HexToHash("0x0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303") // Testnet genesis hash to enforce below configs on
27+
MainNetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3") // Mainnet genesis hash to enforce below configs on
28+
29+
TestNetHomesteadBlock = big.NewInt(494000) // Testnet homestead block
30+
MainNetHomesteadBlock = big.NewInt(1150000) // Mainnet homestead block
31+
32+
TestNetHomesteadGasRepriceBlock = big.NewInt(1783000) // Testnet gas reprice block
33+
MainNetHomesteadGasRepriceBlock = big.NewInt(2463000) // Mainnet gas reprice block
34+
35+
TestNetHomesteadGasRepriceHash = common.HexToHash("0xf376243aeff1f256d970714c3de9fd78fa4e63cf63e32a51fe1169e375d98145") // Testnet gas reprice block hash (used by fast sync)
36+
MainNetHomesteadGasRepriceHash = common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0") // Mainnet gas reprice block hash (used by fast sync)
2637
)

0 commit comments

Comments
 (0)