Skip to content

Commit

Permalink
move genesis verify to vm (#753)
Browse files Browse the repository at this point in the history
* move genesis verify to vm

* fix comment

* set a correct default from genesis

* fix log

* revert change to vm.go

---------

Co-authored-by: Darioush Jalali <[email protected]>
  • Loading branch information
ceyonur and darioush authored Jul 25, 2023
1 parent 7b6fc53 commit fba21c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
32 changes: 17 additions & 15 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,6 @@ func SetupGenesisBlock(
if genesis.Config == nil {
return nil, common.Hash{}, errGenesisNoConfig
}
// Make sure genesis gas limit is consistent in SubnetEVM fork
if genesis.Config.IsSubnetEVM(genesis.Timestamp) {
gasLimitConfig := genesis.Config.FeeConfig.GasLimit.Uint64()
if gasLimitConfig != genesis.GasLimit {
return nil, common.Hash{}, fmt.Errorf(
"gas limit in fee config (%d) does not match gas limit in header (%d)",
gasLimitConfig,
genesis.GasLimit,
)
}
// Verify config
if err := genesis.Config.Verify(); err != nil {
return nil, common.Hash{}, err
}
}

// Just commit the new block if there is no stored genesis block.
stored := rawdb.ReadCanonicalHash(db, 0)
Expand Down Expand Up @@ -396,6 +381,23 @@ func (g *Genesis) MustCommit(db ethdb.Database) *types.Block {
return block
}

func (g *Genesis) Verify() error {
// Make sure genesis gas limit is consistent
gasLimitConfig := g.Config.FeeConfig.GasLimit.Uint64()
if gasLimitConfig != g.GasLimit {
return fmt.Errorf(
"gas limit in fee config (%d) does not match gas limit in header (%d)",
gasLimitConfig,
g.GasLimit,
)
}
// Verify config
if err := g.Config.Verify(); err != nil {
return err
}
return nil
}

// GenesisBlockForTesting creates and writes a block in which addr has the given wei balance.
func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block {
g := Genesis{
Expand Down
4 changes: 4 additions & 0 deletions plugin/evm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ func (vm *VM) Initialize(
g.Config.FeeConfig = params.DefaultFeeConfig
}

if err := g.Verify(); err != nil {
return fmt.Errorf("failed to verify genesis: %w", err)
}

vm.ethConfig = ethconfig.NewDefaultConfig()
vm.ethConfig.Genesis = g
// NetworkID here is different than Avalanche's NetworkID.
Expand Down

0 comments on commit fba21c6

Please sign in to comment.