From 4f926086eeb74192b69284ce9471945b2dd1f57a Mon Sep 17 00:00:00 2001 From: Chris Purta Date: Fri, 6 Jul 2018 10:40:43 -0700 Subject: [PATCH 1/3] Use genesis difficulty for private networks --- consensus/ethash/consensus.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 99eec82211..c24e6d2cb8 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -297,6 +297,8 @@ func (ethash *Ethash) CalcDifficulty(chain consensus.ChainReader, time uint64, p func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int { next := new(big.Int).Add(parent.Number, big1) switch { + case isPrivateNetwork(config): + return parent.Difficulty case config.IsByzantium(next): return calcDifficultyByzantium(time, parent) case config.IsHomestead(next): @@ -306,6 +308,15 @@ func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Heade } } +// isPrivateNetwork is a helper function to determine if the current +// chain is a private network or not based on the configurations +// ChainID. +func isPrivateNetwork(config *params.ChainConfig) bool { + return config.ChainID != params.MainnetChainConfig.ChainID || + config.ChainID != params.TestChainConfig.ChainID || + config.ChainID != params.RinkebyChainConfig.ChainID +} + // Some weird constants to avoid constant memory allocs for them. var ( expDiffPeriod = big.NewInt(100000) From 68e62fa804ba3c43f254cf6a20f9ac582125048e Mon Sep 17 00:00:00 2001 From: Chris Purta Date: Fri, 6 Jul 2018 16:28:51 -0700 Subject: [PATCH 2/3] Fix logic error: OR to use AND --- consensus/ethash/consensus.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index c24e6d2cb8..579b9a236f 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -312,8 +312,8 @@ func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Heade // chain is a private network or not based on the configurations // ChainID. func isPrivateNetwork(config *params.ChainConfig) bool { - return config.ChainID != params.MainnetChainConfig.ChainID || - config.ChainID != params.TestChainConfig.ChainID || + return config.ChainID != params.MainnetChainConfig.ChainID && + config.ChainID != params.TestChainConfig.ChainID && config.ChainID != params.RinkebyChainConfig.ChainID } From 5f4ef4fda20513d182260f5ebc926ea0507b7860 Mon Sep 17 00:00:00 2001 From: Chris Purta Date: Sat, 7 Jul 2018 07:46:01 -0700 Subject: [PATCH 3/3] ethash: add isPrivateNetwork test --- consensus/ethash/consensus_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/consensus/ethash/consensus_test.go b/consensus/ethash/consensus_test.go index 438a99dd66..cdf0ea0858 100644 --- a/consensus/ethash/consensus_test.go +++ b/consensus/ethash/consensus_test.go @@ -84,3 +84,21 @@ func TestCalcDifficulty(t *testing.T) { } } } + +func TestIsPrivateNetwork(t *testing.T) { + tests := make(map[*big.Int]bool) + + tests[big.NewInt(314)] = true + tests[params.MainnetChainConfig.ChainID] = false + tests[params.TestChainConfig.ChainID] = false + tests[params.RinkebyChainConfig.ChainID] = false + + for chainID, expected := range tests { + config := ¶ms.ChainConfig{ChainID: chainID} + + isPrivate := isPrivateNetwork(config) + if isPrivate != expected { + t.Error("Private chain test failed. Expected", expected, "but determined", isPrivate) + } + } +}