Skip to content

Commit e332431

Browse files
fjllightclient
andauthored
core: assign default difficulty to zero for chain without ethash (#31067)
I hit this case while trying something with the simulated backend. The EVM only enables instruction set forks after the merge when 'Random' is set. In the simulated backend, the random value will be set via the engine API for all blocks after genesis. But for the genesis block itself, the random value will not be assigned in the vm.BlockContext because the genesis has a non-zero difficulty. For my case, this meant that estimateGas did not work for the first transaction sent on the simulated chain, since the contract contained a PUSH0 instruction. This could also be fixed by explicitly configuring a zero difficulty in the simulated backend. However, I think that zero difficulty is a better default these days. --------- Co-authored-by: lightclient <[email protected]>
1 parent e6f3ce7 commit e332431

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

core/genesis.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,12 @@ func (g *Genesis) toBlockWithRoot(root common.Hash) *types.Block {
464464
if g.GasLimit == 0 {
465465
head.GasLimit = params.GenesisGasLimit
466466
}
467-
if g.Difficulty == nil && g.Mixhash == (common.Hash{}) {
468-
head.Difficulty = params.GenesisDifficulty
467+
if g.Difficulty == nil {
468+
if g.Config != nil && g.Config.Ethash == nil {
469+
head.Difficulty = big.NewInt(0)
470+
} else if g.Mixhash == (common.Hash{}) {
471+
head.Difficulty = params.GenesisDifficulty
472+
}
469473
}
470474
if g.Config != nil && g.Config.IsLondon(common.Big0) {
471475
if g.BaseFee != nil {

core/genesis_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ func testSetupGenesis(t *testing.T, scheme string) {
4444
var (
4545
customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50")
4646
customg = Genesis{
47-
Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3)},
47+
Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3), Ethash: &params.EthashConfig{}},
4848
Alloc: types.GenesisAlloc{
4949
{1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
5050
},
5151
}
5252
oldcustomg = customg
5353
)
54-
oldcustomg.Config = &params.ChainConfig{HomesteadBlock: big.NewInt(2)}
54+
oldcustomg.Config = &params.ChainConfig{HomesteadBlock: big.NewInt(2), Ethash: &params.EthashConfig{}}
5555

5656
tests := []struct {
5757
name string

0 commit comments

Comments
 (0)