This commit is contained in:
Christopher Purta 2018-07-09 08:41:43 +00:00 committed by GitHub
commit 4738034b49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View file

@ -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 { func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {
next := new(big.Int).Add(parent.Number, big1) next := new(big.Int).Add(parent.Number, big1)
switch { switch {
case isPrivateNetwork(config):
return parent.Difficulty
case config.IsByzantium(next): case config.IsByzantium(next):
return calcDifficultyByzantium(time, parent) return calcDifficultyByzantium(time, parent)
case config.IsHomestead(next): 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. // Some weird constants to avoid constant memory allocs for them.
var ( var (
expDiffPeriod = big.NewInt(100000) expDiffPeriod = big.NewInt(100000)

View file

@ -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 := &params.ChainConfig{ChainID: chainID}
isPrivate := isPrivateNetwork(config)
if isPrivate != expected {
t.Error("Private chain test failed. Expected", expected, "but determined", isPrivate)
}
}
}