github.com/ethereum/go-ethereum@v1.16.1/core/genesis.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "errors" 23 "fmt" 24 "math/big" 25 "strings" 26 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/common/hexutil" 29 "github.com/ethereum/go-ethereum/common/math" 30 "github.com/ethereum/go-ethereum/core/rawdb" 31 "github.com/ethereum/go-ethereum/core/state" 32 "github.com/ethereum/go-ethereum/core/tracing" 33 "github.com/ethereum/go-ethereum/core/types" 34 "github.com/ethereum/go-ethereum/crypto" 35 "github.com/ethereum/go-ethereum/ethdb" 36 "github.com/ethereum/go-ethereum/log" 37 "github.com/ethereum/go-ethereum/params" 38 "github.com/ethereum/go-ethereum/rlp" 39 "github.com/ethereum/go-ethereum/trie" 40 "github.com/ethereum/go-ethereum/triedb" 41 "github.com/ethereum/go-ethereum/triedb/pathdb" 42 "github.com/holiman/uint256" 43 ) 44 45 //go:generate go run github.com/fjl/gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go 46 47 var errGenesisNoConfig = errors.New("genesis has no chain configuration") 48 49 // Deprecated: use types.Account instead. 50 type GenesisAccount = types.Account 51 52 // Deprecated: use types.GenesisAlloc instead. 53 type GenesisAlloc = types.GenesisAlloc 54 55 // Genesis specifies the header fields, state of a genesis block. It also defines hard 56 // fork switch-over blocks through the chain configuration. 57 type Genesis struct { 58 Config *params.ChainConfig `json:"config"` 59 Nonce uint64 `json:"nonce"` 60 Timestamp uint64 `json:"timestamp"` 61 ExtraData []byte `json:"extraData"` 62 GasLimit uint64 `json:"gasLimit" gencodec:"required"` 63 Difficulty *big.Int `json:"difficulty" gencodec:"required"` 64 Mixhash common.Hash `json:"mixHash"` 65 Coinbase common.Address `json:"coinbase"` 66 Alloc types.GenesisAlloc `json:"alloc" gencodec:"required"` 67 68 // These fields are used for consensus tests. Please don't use them 69 // in actual genesis blocks. 70 Number uint64 `json:"number"` 71 GasUsed uint64 `json:"gasUsed"` 72 ParentHash common.Hash `json:"parentHash"` 73 BaseFee *big.Int `json:"baseFeePerGas"` // EIP-1559 74 ExcessBlobGas *uint64 `json:"excessBlobGas"` // EIP-4844 75 BlobGasUsed *uint64 `json:"blobGasUsed"` // EIP-4844 76 } 77 78 // copy copies the genesis. 79 func (g *Genesis) copy() *Genesis { 80 if g != nil { 81 cpy := *g 82 if g.Config != nil { 83 conf := *g.Config 84 cpy.Config = &conf 85 } 86 return &cpy 87 } 88 return nil 89 } 90 91 func ReadGenesis(db ethdb.Database) (*Genesis, error) { 92 var genesis Genesis 93 stored := rawdb.ReadCanonicalHash(db, 0) 94 if (stored == common.Hash{}) { 95 return nil, fmt.Errorf("invalid genesis hash in database: %x", stored) 96 } 97 blob := rawdb.ReadGenesisStateSpec(db, stored) 98 if blob == nil { 99 return nil, errors.New("genesis state missing from db") 100 } 101 if len(blob) != 0 { 102 if err := genesis.Alloc.UnmarshalJSON(blob); err != nil { 103 return nil, fmt.Errorf("could not unmarshal genesis state json: %s", err) 104 } 105 } 106 genesis.Config = rawdb.ReadChainConfig(db, stored) 107 if genesis.Config == nil { 108 return nil, errors.New("genesis config missing from db") 109 } 110 genesisBlock := rawdb.ReadBlock(db, stored, 0) 111 if genesisBlock == nil { 112 return nil, errors.New("genesis block missing from db") 113 } 114 genesisHeader := genesisBlock.Header() 115 genesis.Nonce = genesisHeader.Nonce.Uint64() 116 genesis.Timestamp = genesisHeader.Time 117 genesis.ExtraData = genesisHeader.Extra 118 genesis.GasLimit = genesisHeader.GasLimit 119 genesis.Difficulty = genesisHeader.Difficulty 120 genesis.Mixhash = genesisHeader.MixDigest 121 genesis.Coinbase = genesisHeader.Coinbase 122 genesis.BaseFee = genesisHeader.BaseFee 123 genesis.ExcessBlobGas = genesisHeader.ExcessBlobGas 124 genesis.BlobGasUsed = genesisHeader.BlobGasUsed 125 126 return &genesis, nil 127 } 128 129 // hashAlloc computes the state root according to the genesis specification. 130 func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) { 131 // If a genesis-time verkle trie is requested, create a trie config 132 // with the verkle trie enabled so that the tree can be initialized 133 // as such. 134 var config *triedb.Config 135 if isVerkle { 136 config = &triedb.Config{ 137 PathDB: pathdb.Defaults, 138 IsVerkle: true, 139 } 140 } 141 // Create an ephemeral in-memory database for computing hash, 142 // all the derived states will be discarded to not pollute disk. 143 emptyRoot := types.EmptyRootHash 144 if isVerkle { 145 emptyRoot = types.EmptyVerkleHash 146 } 147 db := rawdb.NewMemoryDatabase() 148 statedb, err := state.New(emptyRoot, state.NewDatabase(triedb.NewDatabase(db, config), nil)) 149 if err != nil { 150 return common.Hash{}, err 151 } 152 for addr, account := range *ga { 153 if account.Balance != nil { 154 statedb.AddBalance(addr, uint256.MustFromBig(account.Balance), tracing.BalanceIncreaseGenesisBalance) 155 } 156 statedb.SetCode(addr, account.Code) 157 statedb.SetNonce(addr, account.Nonce, tracing.NonceChangeGenesis) 158 for key, value := range account.Storage { 159 statedb.SetState(addr, key, value) 160 } 161 } 162 return statedb.Commit(0, false, false) 163 } 164 165 // flushAlloc is very similar with hash, but the main difference is all the 166 // generated states will be persisted into the given database. 167 func flushAlloc(ga *types.GenesisAlloc, triedb *triedb.Database) (common.Hash, error) { 168 emptyRoot := types.EmptyRootHash 169 if triedb.IsVerkle() { 170 emptyRoot = types.EmptyVerkleHash 171 } 172 statedb, err := state.New(emptyRoot, state.NewDatabase(triedb, nil)) 173 if err != nil { 174 return common.Hash{}, err 175 } 176 for addr, account := range *ga { 177 if account.Balance != nil { 178 // This is not actually logged via tracer because OnGenesisBlock 179 // already captures the allocations. 180 statedb.AddBalance(addr, uint256.MustFromBig(account.Balance), tracing.BalanceIncreaseGenesisBalance) 181 } 182 statedb.SetCode(addr, account.Code) 183 statedb.SetNonce(addr, account.Nonce, tracing.NonceChangeGenesis) 184 for key, value := range account.Storage { 185 statedb.SetState(addr, key, value) 186 } 187 } 188 root, err := statedb.Commit(0, false, false) 189 if err != nil { 190 return common.Hash{}, err 191 } 192 // Commit newly generated states into disk if it's not empty. 193 if root != types.EmptyRootHash { 194 if err := triedb.Commit(root, true); err != nil { 195 return common.Hash{}, err 196 } 197 } 198 return root, nil 199 } 200 201 func getGenesisState(db ethdb.Database, blockhash common.Hash) (alloc types.GenesisAlloc, err error) { 202 blob := rawdb.ReadGenesisStateSpec(db, blockhash) 203 if len(blob) != 0 { 204 if err := alloc.UnmarshalJSON(blob); err != nil { 205 return nil, err 206 } 207 208 return alloc, nil 209 } 210 211 // Genesis allocation is missing and there are several possibilities: 212 // the node is legacy which doesn't persist the genesis allocation or 213 // the persisted allocation is just lost. 214 // - supported networks(mainnet, testnets), recover with defined allocations 215 // - private network, can't recover 216 var genesis *Genesis 217 switch blockhash { 218 case params.MainnetGenesisHash: 219 genesis = DefaultGenesisBlock() 220 case params.SepoliaGenesisHash: 221 genesis = DefaultSepoliaGenesisBlock() 222 case params.HoleskyGenesisHash: 223 genesis = DefaultHoleskyGenesisBlock() 224 case params.HoodiGenesisHash: 225 genesis = DefaultHoodiGenesisBlock() 226 } 227 if genesis != nil { 228 return genesis.Alloc, nil 229 } 230 231 return nil, nil 232 } 233 234 // field type overrides for gencodec 235 type genesisSpecMarshaling struct { 236 Nonce math.HexOrDecimal64 237 Timestamp math.HexOrDecimal64 238 ExtraData hexutil.Bytes 239 GasLimit math.HexOrDecimal64 240 GasUsed math.HexOrDecimal64 241 Number math.HexOrDecimal64 242 Difficulty *math.HexOrDecimal256 243 Alloc map[common.UnprefixedAddress]types.Account 244 BaseFee *math.HexOrDecimal256 245 ExcessBlobGas *math.HexOrDecimal64 246 BlobGasUsed *math.HexOrDecimal64 247 } 248 249 // GenesisMismatchError is raised when trying to overwrite an existing 250 // genesis block with an incompatible one. 251 type GenesisMismatchError struct { 252 Stored, New common.Hash 253 } 254 255 func (e *GenesisMismatchError) Error() string { 256 return fmt.Sprintf("database contains incompatible genesis (have %x, new %x)", e.Stored, e.New) 257 } 258 259 // ChainOverrides contains the changes to chain config. 260 type ChainOverrides struct { 261 OverrideOsaka *uint64 262 OverrideVerkle *uint64 263 } 264 265 // apply applies the chain overrides on the supplied chain config. 266 func (o *ChainOverrides) apply(cfg *params.ChainConfig) error { 267 if o == nil || cfg == nil { 268 return nil 269 } 270 if o.OverrideOsaka != nil { 271 cfg.OsakaTime = o.OverrideOsaka 272 } 273 if o.OverrideVerkle != nil { 274 cfg.VerkleTime = o.OverrideVerkle 275 } 276 return cfg.CheckConfigForkOrder() 277 } 278 279 // SetupGenesisBlock writes or updates the genesis block in db. 280 // The block that will be used is: 281 // 282 // genesis == nil genesis != nil 283 // +------------------------------------------ 284 // db has no genesis | main-net default | genesis 285 // db has genesis | from DB | genesis (if compatible) 286 // 287 // The stored chain configuration will be updated if it is compatible (i.e. does not 288 // specify a fork block below the local head block). In case of a conflict, the 289 // error is a *params.ConfigCompatError and the new, unwritten config is returned. 290 func SetupGenesisBlock(db ethdb.Database, triedb *triedb.Database, genesis *Genesis) (*params.ChainConfig, common.Hash, *params.ConfigCompatError, error) { 291 return SetupGenesisBlockWithOverride(db, triedb, genesis, nil) 292 } 293 294 func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *triedb.Database, genesis *Genesis, overrides *ChainOverrides) (*params.ChainConfig, common.Hash, *params.ConfigCompatError, error) { 295 // Copy the genesis, so we can operate on a copy. 296 genesis = genesis.copy() 297 // Sanitize the supplied genesis, ensuring it has the associated chain 298 // config attached. 299 if genesis != nil && genesis.Config == nil { 300 return nil, common.Hash{}, nil, errGenesisNoConfig 301 } 302 // Commit the genesis if the database is empty 303 ghash := rawdb.ReadCanonicalHash(db, 0) 304 if (ghash == common.Hash{}) { 305 if genesis == nil { 306 log.Info("Writing default main-net genesis block") 307 genesis = DefaultGenesisBlock() 308 } else { 309 log.Info("Writing custom genesis block") 310 } 311 if err := overrides.apply(genesis.Config); err != nil { 312 return nil, common.Hash{}, nil, err 313 } 314 315 block, err := genesis.Commit(db, triedb) 316 if err != nil { 317 return nil, common.Hash{}, nil, err 318 } 319 return genesis.Config, block.Hash(), nil, nil 320 } 321 // Commit the genesis if the genesis block exists in the ancient database 322 // but the key-value database is empty without initializing the genesis 323 // fields. This scenario can occur when the node is created from scratch 324 // with an existing ancient store. 325 storedCfg := rawdb.ReadChainConfig(db, ghash) 326 if storedCfg == nil { 327 // Ensure the stored genesis block matches with the given genesis. Private 328 // networks must explicitly specify the genesis in the config file, mainnet 329 // genesis will be used as default and the initialization will always fail. 330 if genesis == nil { 331 log.Info("Writing default main-net genesis block") 332 genesis = DefaultGenesisBlock() 333 } else { 334 log.Info("Writing custom genesis block") 335 } 336 if err := overrides.apply(genesis.Config); err != nil { 337 return nil, common.Hash{}, nil, err 338 } 339 340 if hash := genesis.ToBlock().Hash(); hash != ghash { 341 return nil, common.Hash{}, nil, &GenesisMismatchError{ghash, hash} 342 } 343 block, err := genesis.Commit(db, triedb) 344 if err != nil { 345 return nil, common.Hash{}, nil, err 346 } 347 return genesis.Config, block.Hash(), nil, nil 348 } 349 // The genesis block has already been committed previously. Verify that the 350 // provided genesis with chain overrides matches the existing one, and update 351 // the stored chain config if necessary. 352 if genesis != nil { 353 if err := overrides.apply(genesis.Config); err != nil { 354 return nil, common.Hash{}, nil, err 355 } 356 357 if hash := genesis.ToBlock().Hash(); hash != ghash { 358 return nil, common.Hash{}, nil, &GenesisMismatchError{ghash, hash} 359 } 360 } 361 // Check config compatibility and write the config. Compatibility errors 362 // are returned to the caller unless we're already at block zero. 363 head := rawdb.ReadHeadHeader(db) 364 if head == nil { 365 return nil, common.Hash{}, nil, errors.New("missing head header") 366 } 367 newCfg := genesis.chainConfigOrDefault(ghash, storedCfg) 368 if err := overrides.apply(newCfg); err != nil { 369 return nil, common.Hash{}, nil, err 370 } 371 372 // Sanity-check the new configuration. 373 if err := newCfg.CheckConfigForkOrder(); err != nil { 374 return nil, common.Hash{}, nil, err 375 } 376 377 // TODO(rjl493456442) better to define the comparator of chain config 378 // and short circuit if the chain config is not changed. 379 compatErr := storedCfg.CheckCompatible(newCfg, head.Number.Uint64(), head.Time) 380 if compatErr != nil && ((head.Number.Uint64() != 0 && compatErr.RewindToBlock != 0) || (head.Time != 0 && compatErr.RewindToTime != 0)) { 381 return newCfg, ghash, compatErr, nil 382 } 383 // Don't overwrite if the old is identical to the new. It's useful 384 // for the scenarios that database is opened in the read-only mode. 385 storedData, _ := json.Marshal(storedCfg) 386 if newData, _ := json.Marshal(newCfg); !bytes.Equal(storedData, newData) { 387 rawdb.WriteChainConfig(db, ghash, newCfg) 388 } 389 return newCfg, ghash, nil, nil 390 } 391 392 // LoadChainConfig loads the stored chain config if it is already present in 393 // database, otherwise, return the config in the provided genesis specification. 394 func LoadChainConfig(db ethdb.Database, genesis *Genesis) (cfg *params.ChainConfig, ghash common.Hash, err error) { 395 // Load the stored chain config from the database. It can be nil 396 // in case the database is empty. Notably, we only care about the 397 // chain config corresponds to the canonical chain. 398 stored := rawdb.ReadCanonicalHash(db, 0) 399 if stored != (common.Hash{}) { 400 storedcfg := rawdb.ReadChainConfig(db, stored) 401 if storedcfg != nil { 402 return storedcfg, stored, nil 403 } 404 } 405 // Load the config from the provided genesis specification 406 if genesis != nil { 407 // Reject invalid genesis spec without valid chain config 408 if genesis.Config == nil { 409 return nil, common.Hash{}, errGenesisNoConfig 410 } 411 // If the canonical genesis header is present, but the chain 412 // config is missing(initialize the empty leveldb with an 413 // external ancient chain segment), ensure the provided genesis 414 // is matched. 415 ghash := genesis.ToBlock().Hash() 416 if stored != (common.Hash{}) && ghash != stored { 417 return nil, ghash, &GenesisMismatchError{stored, ghash} 418 } 419 return genesis.Config, ghash, nil 420 } 421 // There is no stored chain config and no new config provided, 422 // In this case the default chain config(mainnet) will be used 423 return params.MainnetChainConfig, params.MainnetGenesisHash, nil 424 } 425 426 // chainConfigOrDefault retrieves the attached chain configuration. If the genesis 427 // object is null, it returns the default chain configuration based on the given 428 // genesis hash, or the locally stored config if it's not a pre-defined network. 429 func (g *Genesis) chainConfigOrDefault(ghash common.Hash, stored *params.ChainConfig) *params.ChainConfig { 430 switch { 431 case g != nil: 432 return g.Config 433 case ghash == params.MainnetGenesisHash: 434 return params.MainnetChainConfig 435 case ghash == params.HoleskyGenesisHash: 436 return params.HoleskyChainConfig 437 case ghash == params.SepoliaGenesisHash: 438 return params.SepoliaChainConfig 439 case ghash == params.HoodiGenesisHash: 440 return params.HoodiChainConfig 441 default: 442 return stored 443 } 444 } 445 446 // IsVerkle indicates whether the state is already stored in a verkle 447 // tree at genesis time. 448 func (g *Genesis) IsVerkle() bool { 449 return g.Config.IsVerkleGenesis() 450 } 451 452 // ToBlock returns the genesis block according to genesis specification. 453 func (g *Genesis) ToBlock() *types.Block { 454 root, err := hashAlloc(&g.Alloc, g.IsVerkle()) 455 if err != nil { 456 panic(err) 457 } 458 return g.toBlockWithRoot(root) 459 } 460 461 // toBlockWithRoot constructs the genesis block with the given genesis state root. 462 func (g *Genesis) toBlockWithRoot(root common.Hash) *types.Block { 463 head := &types.Header{ 464 Number: new(big.Int).SetUint64(g.Number), 465 Nonce: types.EncodeNonce(g.Nonce), 466 Time: g.Timestamp, 467 ParentHash: g.ParentHash, 468 Extra: g.ExtraData, 469 GasLimit: g.GasLimit, 470 GasUsed: g.GasUsed, 471 BaseFee: g.BaseFee, 472 Difficulty: g.Difficulty, 473 MixDigest: g.Mixhash, 474 Coinbase: g.Coinbase, 475 Root: root, 476 } 477 if g.GasLimit == 0 { 478 head.GasLimit = params.GenesisGasLimit 479 } 480 if g.Difficulty == nil { 481 if g.Config != nil && g.Config.Ethash == nil { 482 head.Difficulty = big.NewInt(0) 483 } else if g.Mixhash == (common.Hash{}) { 484 head.Difficulty = params.GenesisDifficulty 485 } 486 } 487 if g.Config != nil && g.Config.IsLondon(common.Big0) { 488 if g.BaseFee != nil { 489 head.BaseFee = g.BaseFee 490 } else { 491 head.BaseFee = new(big.Int).SetUint64(params.InitialBaseFee) 492 } 493 } 494 var ( 495 withdrawals []*types.Withdrawal 496 ) 497 if conf := g.Config; conf != nil { 498 num := big.NewInt(int64(g.Number)) 499 if conf.IsShanghai(num, g.Timestamp) { 500 head.WithdrawalsHash = &types.EmptyWithdrawalsHash 501 withdrawals = make([]*types.Withdrawal, 0) 502 } 503 if conf.IsCancun(num, g.Timestamp) { 504 // EIP-4788: The parentBeaconBlockRoot of the genesis block is always 505 // the zero hash. This is because the genesis block does not have a parent 506 // by definition. 507 head.ParentBeaconRoot = new(common.Hash) 508 // EIP-4844 fields 509 head.ExcessBlobGas = g.ExcessBlobGas 510 head.BlobGasUsed = g.BlobGasUsed 511 if head.ExcessBlobGas == nil { 512 head.ExcessBlobGas = new(uint64) 513 } 514 if head.BlobGasUsed == nil { 515 head.BlobGasUsed = new(uint64) 516 } 517 } 518 if conf.IsPrague(num, g.Timestamp) { 519 head.RequestsHash = &types.EmptyRequestsHash 520 } 521 } 522 return types.NewBlock(head, &types.Body{Withdrawals: withdrawals}, nil, trie.NewStackTrie(nil)) 523 } 524 525 // Commit writes the block and state of a genesis specification to the database. 526 // The block is committed as the canonical head block. 527 func (g *Genesis) Commit(db ethdb.Database, triedb *triedb.Database) (*types.Block, error) { 528 if g.Number != 0 { 529 return nil, errors.New("can't commit genesis block with number > 0") 530 } 531 config := g.Config 532 if config == nil { 533 return nil, errors.New("invalid genesis without chain config") 534 } 535 if err := config.CheckConfigForkOrder(); err != nil { 536 return nil, err 537 } 538 if config.Clique != nil && len(g.ExtraData) < 32+crypto.SignatureLength { 539 return nil, errors.New("can't start clique chain without signers") 540 } 541 // flush the data to disk and compute the state root 542 root, err := flushAlloc(&g.Alloc, triedb) 543 if err != nil { 544 return nil, err 545 } 546 block := g.toBlockWithRoot(root) 547 548 // Marshal the genesis state specification and persist. 549 blob, err := json.Marshal(g.Alloc) 550 if err != nil { 551 return nil, err 552 } 553 batch := db.NewBatch() 554 rawdb.WriteGenesisStateSpec(batch, block.Hash(), blob) 555 rawdb.WriteBlock(batch, block) 556 rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), nil) 557 rawdb.WriteCanonicalHash(batch, block.Hash(), block.NumberU64()) 558 rawdb.WriteHeadBlockHash(batch, block.Hash()) 559 rawdb.WriteHeadFastBlockHash(batch, block.Hash()) 560 rawdb.WriteHeadHeaderHash(batch, block.Hash()) 561 rawdb.WriteChainConfig(batch, block.Hash(), config) 562 return block, batch.Write() 563 } 564 565 // MustCommit writes the genesis block and state to db, panicking on error. 566 // The block is committed as the canonical head block. 567 func (g *Genesis) MustCommit(db ethdb.Database, triedb *triedb.Database) *types.Block { 568 block, err := g.Commit(db, triedb) 569 if err != nil { 570 panic(err) 571 } 572 return block 573 } 574 575 // EnableVerkleAtGenesis indicates whether the verkle fork should be activated 576 // at genesis. This is a temporary solution only for verkle devnet testing, where 577 // verkle fork is activated at genesis, and the configured activation date has 578 // already passed. 579 // 580 // In production networks (mainnet and public testnets), verkle activation always 581 // occurs after the genesis block, making this function irrelevant in those cases. 582 func EnableVerkleAtGenesis(db ethdb.Database, genesis *Genesis) (bool, error) { 583 if genesis != nil { 584 if genesis.Config == nil { 585 return false, errGenesisNoConfig 586 } 587 return genesis.Config.EnableVerkleAtGenesis, nil 588 } 589 if ghash := rawdb.ReadCanonicalHash(db, 0); ghash != (common.Hash{}) { 590 chainCfg := rawdb.ReadChainConfig(db, ghash) 591 if chainCfg != nil { 592 return chainCfg.EnableVerkleAtGenesis, nil 593 } 594 } 595 return false, nil 596 } 597 598 // DefaultGenesisBlock returns the Ethereum main net genesis block. 599 func DefaultGenesisBlock() *Genesis { 600 return &Genesis{ 601 Config: params.MainnetChainConfig, 602 Nonce: 66, 603 ExtraData: hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"), 604 GasLimit: 5000, 605 Difficulty: big.NewInt(17179869184), 606 Alloc: decodePrealloc(mainnetAllocData), 607 } 608 } 609 610 // DefaultSepoliaGenesisBlock returns the Sepolia network genesis block. 611 func DefaultSepoliaGenesisBlock() *Genesis { 612 return &Genesis{ 613 Config: params.SepoliaChainConfig, 614 Nonce: 0, 615 ExtraData: []byte("Sepolia, Athens, Attica, Greece!"), 616 GasLimit: 0x1c9c380, 617 Difficulty: big.NewInt(0x20000), 618 Timestamp: 1633267481, 619 Alloc: decodePrealloc(sepoliaAllocData), 620 } 621 } 622 623 // DefaultHoleskyGenesisBlock returns the Holesky network genesis block. 624 func DefaultHoleskyGenesisBlock() *Genesis { 625 return &Genesis{ 626 Config: params.HoleskyChainConfig, 627 Nonce: 0x1234, 628 GasLimit: 0x17d7840, 629 Difficulty: big.NewInt(0x01), 630 Timestamp: 1695902100, 631 Alloc: decodePrealloc(holeskyAllocData), 632 } 633 } 634 635 // DefaultHoodiGenesisBlock returns the Hoodi network genesis block. 636 func DefaultHoodiGenesisBlock() *Genesis { 637 return &Genesis{ 638 Config: params.HoodiChainConfig, 639 Nonce: 0x1234, 640 GasLimit: 0x2255100, 641 Difficulty: big.NewInt(0x01), 642 Timestamp: 1742212800, 643 Alloc: decodePrealloc(hoodiAllocData), 644 } 645 } 646 647 // DeveloperGenesisBlock returns the 'geth --dev' genesis block. 648 func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis { 649 // Override the default period to the user requested one 650 config := *params.AllDevChainProtocolChanges 651 652 // Assemble and return the genesis with the precompiles and faucet pre-funded 653 genesis := &Genesis{ 654 Config: &config, 655 GasLimit: gasLimit, 656 BaseFee: big.NewInt(params.InitialBaseFee), 657 Difficulty: big.NewInt(0), 658 Alloc: map[common.Address]types.Account{ 659 common.BytesToAddress([]byte{0x01}): {Balance: big.NewInt(1)}, // ECRecover 660 common.BytesToAddress([]byte{0x02}): {Balance: big.NewInt(1)}, // SHA256 661 common.BytesToAddress([]byte{0x03}): {Balance: big.NewInt(1)}, // RIPEMD 662 common.BytesToAddress([]byte{0x04}): {Balance: big.NewInt(1)}, // Identity 663 common.BytesToAddress([]byte{0x05}): {Balance: big.NewInt(1)}, // ModExp 664 common.BytesToAddress([]byte{0x06}): {Balance: big.NewInt(1)}, // ECAdd 665 common.BytesToAddress([]byte{0x07}): {Balance: big.NewInt(1)}, // ECScalarMul 666 common.BytesToAddress([]byte{0x08}): {Balance: big.NewInt(1)}, // ECPairing 667 common.BytesToAddress([]byte{0x09}): {Balance: big.NewInt(1)}, // BLAKE2b 668 common.BytesToAddress([]byte{0x0a}): {Balance: big.NewInt(1)}, // KZGPointEval 669 common.BytesToAddress([]byte{0x0b}): {Balance: big.NewInt(1)}, // BLSG1Add 670 common.BytesToAddress([]byte{0x0c}): {Balance: big.NewInt(1)}, // BLSG1MultiExp 671 common.BytesToAddress([]byte{0x0d}): {Balance: big.NewInt(1)}, // BLSG2Add 672 common.BytesToAddress([]byte{0x0e}): {Balance: big.NewInt(1)}, // BLSG2MultiExp 673 common.BytesToAddress([]byte{0x0f}): {Balance: big.NewInt(1)}, // BLSG1Pairing 674 common.BytesToAddress([]byte{0x10}): {Balance: big.NewInt(1)}, // BLSG1MapG1 675 common.BytesToAddress([]byte{0x11}): {Balance: big.NewInt(1)}, // BLSG2MapG2 676 // Pre-deploy system contracts 677 params.BeaconRootsAddress: {Nonce: 1, Code: params.BeaconRootsCode, Balance: common.Big0}, 678 params.HistoryStorageAddress: {Nonce: 1, Code: params.HistoryStorageCode, Balance: common.Big0}, 679 params.WithdrawalQueueAddress: {Nonce: 1, Code: params.WithdrawalQueueCode, Balance: common.Big0}, 680 params.ConsolidationQueueAddress: {Nonce: 1, Code: params.ConsolidationQueueCode, Balance: common.Big0}, 681 }, 682 } 683 if faucet != nil { 684 genesis.Alloc[*faucet] = types.Account{Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))} 685 } 686 return genesis 687 } 688 689 func decodePrealloc(data string) types.GenesisAlloc { 690 var p []struct { 691 Addr *big.Int 692 Balance *big.Int 693 Misc *struct { 694 Nonce uint64 695 Code []byte 696 Slots []struct { 697 Key common.Hash 698 Val common.Hash 699 } 700 } `rlp:"optional"` 701 } 702 if err := rlp.NewStream(strings.NewReader(data), 0).Decode(&p); err != nil { 703 panic(err) 704 } 705 ga := make(types.GenesisAlloc, len(p)) 706 for _, account := range p { 707 acc := types.Account{Balance: account.Balance} 708 if account.Misc != nil { 709 acc.Nonce = account.Misc.Nonce 710 acc.Code = account.Misc.Code 711 712 acc.Storage = make(map[common.Hash]common.Hash) 713 for _, slot := range account.Misc.Slots { 714 acc.Storage[slot.Key] = slot.Val 715 } 716 } 717 ga[common.BigToAddress(account.Addr)] = acc 718 } 719 return ga 720 }