github.com/theQRL/go-zond@v0.1.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/hex" 22 "encoding/json" 23 "errors" 24 "fmt" 25 "math/big" 26 "strings" 27 28 "github.com/theQRL/go-zond/common" 29 "github.com/theQRL/go-zond/common/hexutil" 30 "github.com/theQRL/go-zond/common/math" 31 "github.com/theQRL/go-zond/core/rawdb" 32 "github.com/theQRL/go-zond/core/state" 33 "github.com/theQRL/go-zond/core/types" 34 "github.com/theQRL/go-zond/crypto" 35 "github.com/theQRL/go-zond/log" 36 "github.com/theQRL/go-zond/params" 37 "github.com/theQRL/go-zond/rlp" 38 "github.com/theQRL/go-zond/trie" 39 "github.com/theQRL/go-zond/zonddb" 40 ) 41 42 //go:generate go run github.com/fjl/gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go 43 //go:generate go run github.com/fjl/gencodec -type GenesisAccount -field-override genesisAccountMarshaling -out gen_genesis_account.go 44 45 var errGenesisNoConfig = errors.New("genesis has no chain configuration") 46 47 // Genesis specifies the header fields, state of a genesis block. It also defines hard 48 // fork switch-over blocks through the chain configuration. 49 type Genesis struct { 50 Config *params.ChainConfig `json:"config"` 51 Nonce uint64 `json:"nonce"` 52 Timestamp uint64 `json:"timestamp"` 53 ExtraData []byte `json:"extraData"` 54 GasLimit uint64 `json:"gasLimit" gencodec:"required"` 55 Difficulty *big.Int `json:"difficulty" gencodec:"required"` 56 Mixhash common.Hash `json:"mixHash"` 57 Coinbase common.Address `json:"coinbase"` 58 Alloc GenesisAlloc `json:"alloc" gencodec:"required"` 59 60 // These fields are used for consensus tests. Please don't use them 61 // in actual genesis blocks. 62 Number uint64 `json:"number"` 63 GasUsed uint64 `json:"gasUsed"` 64 ParentHash common.Hash `json:"parentHash"` 65 BaseFee *big.Int `json:"baseFeePerGas"` // EIP-1559 66 ExcessBlobGas *uint64 `json:"excessBlobGas"` // EIP-4844 67 BlobGasUsed *uint64 `json:"blobGasUsed"` // EIP-4844 68 } 69 70 func ReadGenesis(db zonddb.Database) (*Genesis, error) { 71 var genesis Genesis 72 stored := rawdb.ReadCanonicalHash(db, 0) 73 if (stored == common.Hash{}) { 74 return nil, fmt.Errorf("invalid genesis hash in database: %x", stored) 75 } 76 blob := rawdb.ReadGenesisStateSpec(db, stored) 77 if blob == nil { 78 return nil, errors.New("genesis state missing from db") 79 } 80 if len(blob) != 0 { 81 if err := genesis.Alloc.UnmarshalJSON(blob); err != nil { 82 return nil, fmt.Errorf("could not unmarshal genesis state json: %s", err) 83 } 84 } 85 genesis.Config = rawdb.ReadChainConfig(db, stored) 86 if genesis.Config == nil { 87 return nil, errors.New("genesis config missing from db") 88 } 89 genesisBlock := rawdb.ReadBlock(db, stored, 0) 90 if genesisBlock == nil { 91 return nil, errors.New("genesis block missing from db") 92 } 93 genesisHeader := genesisBlock.Header() 94 genesis.Nonce = genesisHeader.Nonce.Uint64() 95 genesis.Timestamp = genesisHeader.Time 96 genesis.ExtraData = genesisHeader.Extra 97 genesis.GasLimit = genesisHeader.GasLimit 98 genesis.Difficulty = genesisHeader.Difficulty 99 genesis.Mixhash = genesisHeader.MixDigest 100 genesis.Coinbase = genesisHeader.Coinbase 101 genesis.BaseFee = genesisHeader.BaseFee 102 genesis.ExcessBlobGas = genesisHeader.ExcessBlobGas 103 genesis.BlobGasUsed = genesisHeader.BlobGasUsed 104 105 return &genesis, nil 106 } 107 108 // GenesisAlloc specifies the initial state that is part of the genesis block. 109 type GenesisAlloc map[common.Address]GenesisAccount 110 111 func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error { 112 m := make(map[common.UnprefixedAddress]GenesisAccount) 113 if err := json.Unmarshal(data, &m); err != nil { 114 return err 115 } 116 *ga = make(GenesisAlloc) 117 for addr, a := range m { 118 (*ga)[common.Address(addr)] = a 119 } 120 return nil 121 } 122 123 // deriveHash computes the state root according to the genesis specification. 124 func (ga *GenesisAlloc) deriveHash() (common.Hash, error) { 125 // Create an ephemeral in-memory database for computing hash, 126 // all the derived states will be discarded to not pollute disk. 127 db := state.NewDatabase(rawdb.NewMemoryDatabase()) 128 statedb, err := state.New(types.EmptyRootHash, db, nil) 129 if err != nil { 130 return common.Hash{}, err 131 } 132 for addr, account := range *ga { 133 if account.Balance != nil { 134 statedb.AddBalance(addr, account.Balance) 135 } 136 statedb.SetCode(addr, account.Code) 137 statedb.SetNonce(addr, account.Nonce) 138 for key, value := range account.Storage { 139 statedb.SetState(addr, key, value) 140 } 141 } 142 return statedb.Commit(0, false) 143 } 144 145 // flush is very similar with deriveHash, but the main difference is 146 // all the generated states will be persisted into the given database. 147 // Also, the genesis state specification will be flushed as well. 148 func (ga *GenesisAlloc) flush(db zonddb.Database, triedb *trie.Database, blockhash common.Hash) error { 149 statedb, err := state.New(types.EmptyRootHash, state.NewDatabaseWithNodeDB(db, triedb), nil) 150 if err != nil { 151 return err 152 } 153 for addr, account := range *ga { 154 if account.Balance != nil { 155 statedb.AddBalance(addr, account.Balance) 156 } 157 statedb.SetCode(addr, account.Code) 158 statedb.SetNonce(addr, account.Nonce) 159 for key, value := range account.Storage { 160 statedb.SetState(addr, key, value) 161 } 162 } 163 root, err := statedb.Commit(0, false) 164 if err != nil { 165 return err 166 } 167 // Commit newly generated states into disk if it's not empty. 168 if root != types.EmptyRootHash { 169 if err := triedb.Commit(root, true); err != nil { 170 return err 171 } 172 } 173 // Marshal the genesis state specification and persist. 174 blob, err := json.Marshal(ga) 175 if err != nil { 176 return err 177 } 178 rawdb.WriteGenesisStateSpec(db, blockhash, blob) 179 return nil 180 } 181 182 // CommitGenesisState loads the stored genesis state with the given block 183 // hash and commits it into the provided trie database. 184 func CommitGenesisState(db zonddb.Database, triedb *trie.Database, blockhash common.Hash) error { 185 var alloc GenesisAlloc 186 blob := rawdb.ReadGenesisStateSpec(db, blockhash) 187 if len(blob) != 0 { 188 if err := alloc.UnmarshalJSON(blob); err != nil { 189 return err 190 } 191 } else { 192 // Genesis allocation is missing and there are several possibilities: 193 // the node is legacy which doesn't persist the genesis allocation or 194 // the persisted allocation is just lost. 195 // - supported networks(mainnet, testnets), recover with defined allocations 196 // - private network, can't recover 197 var genesis *Genesis 198 switch blockhash { 199 case params.MainnetGenesisHash: 200 genesis = DefaultGenesisBlock() 201 case params.GoerliGenesisHash: 202 genesis = DefaultGoerliGenesisBlock() 203 case params.SepoliaGenesisHash: 204 genesis = DefaultSepoliaGenesisBlock() 205 case params.BetaNetGenesisHash: 206 genesis = DefaultBetaNetGenesisBlock() 207 } 208 if genesis != nil { 209 alloc = genesis.Alloc 210 } else { 211 return errors.New("not found") 212 } 213 } 214 return alloc.flush(db, triedb, blockhash) 215 } 216 217 // GenesisAccount is an account in the state of the genesis block. 218 type GenesisAccount struct { 219 Code []byte `json:"code,omitempty"` 220 Storage map[common.Hash]common.Hash `json:"storage,omitempty"` 221 Balance *big.Int `json:"balance" gencodec:"required"` 222 Nonce uint64 `json:"nonce,omitempty"` 223 PrivateKey []byte `json:"secretKey,omitempty"` // for tests 224 } 225 226 // field type overrides for gencodec 227 type genesisSpecMarshaling struct { 228 Nonce math.HexOrDecimal64 229 Timestamp math.HexOrDecimal64 230 ExtraData hexutil.Bytes 231 GasLimit math.HexOrDecimal64 232 GasUsed math.HexOrDecimal64 233 Number math.HexOrDecimal64 234 Difficulty *math.HexOrDecimal256 235 Alloc map[common.UnprefixedAddress]GenesisAccount 236 BaseFee *math.HexOrDecimal256 237 ExcessBlobGas *math.HexOrDecimal64 238 BlobGasUsed *math.HexOrDecimal64 239 } 240 241 type genesisAccountMarshaling struct { 242 Code hexutil.Bytes 243 Balance *math.HexOrDecimal256 244 Nonce math.HexOrDecimal64 245 Storage map[storageJSON]storageJSON 246 PrivateKey hexutil.Bytes 247 } 248 249 // storageJSON represents a 256 bit byte array, but allows less than 256 bits when 250 // unmarshaling from hex. 251 type storageJSON common.Hash 252 253 func (h *storageJSON) UnmarshalText(text []byte) error { 254 text = bytes.TrimPrefix(text, []byte("0x")) 255 if len(text) > 64 { 256 return fmt.Errorf("too many hex characters in storage key/value %q", text) 257 } 258 offset := len(h) - len(text)/2 // pad on the left 259 if _, err := hex.Decode(h[offset:], text); err != nil { 260 return fmt.Errorf("invalid hex storage key/value %q", text) 261 } 262 return nil 263 } 264 265 func (h storageJSON) MarshalText() ([]byte, error) { 266 return hexutil.Bytes(h[:]).MarshalText() 267 } 268 269 // GenesisMismatchError is raised when trying to overwrite an existing 270 // genesis block with an incompatible one. 271 type GenesisMismatchError struct { 272 Stored, New common.Hash 273 } 274 275 func (e *GenesisMismatchError) Error() string { 276 return fmt.Sprintf("database contains incompatible genesis (have %x, new %x)", e.Stored, e.New) 277 } 278 279 // ChainOverrides contains the changes to chain config. 280 type ChainOverrides struct { 281 OverrideCancun *uint64 282 OverrideVerkle *uint64 283 } 284 285 // SetupGenesisBlock writes or updates the genesis block in db. 286 // The block that will be used is: 287 // 288 // genesis == nil genesis != nil 289 // +------------------------------------------ 290 // db has no genesis | main-net default | genesis 291 // db has genesis | from DB | genesis (if compatible) 292 // 293 // The stored chain configuration will be updated if it is compatible (i.e. does not 294 // specify a fork block below the local head block). In case of a conflict, the 295 // error is a *params.ConfigCompatError and the new, unwritten config is returned. 296 // 297 // The returned chain configuration is never nil. 298 func SetupGenesisBlock(db zonddb.Database, triedb *trie.Database, genesis *Genesis) (*params.ChainConfig, common.Hash, error) { 299 return SetupGenesisBlockWithOverride(db, triedb, genesis, nil) 300 } 301 302 func SetupGenesisBlockWithOverride(db zonddb.Database, triedb *trie.Database, genesis *Genesis, overrides *ChainOverrides) (*params.ChainConfig, common.Hash, error) { 303 if genesis != nil && genesis.Config == nil { 304 return params.AllEthashProtocolChanges, common.Hash{}, errGenesisNoConfig 305 } 306 applyOverrides := func(config *params.ChainConfig) { 307 if config != nil { 308 if overrides != nil && overrides.OverrideCancun != nil { 309 config.CancunTime = overrides.OverrideCancun 310 } 311 if overrides != nil && overrides.OverrideVerkle != nil { 312 config.VerkleTime = overrides.OverrideVerkle 313 } 314 } 315 } 316 // Just commit the new block if there is no stored genesis block. 317 stored := rawdb.ReadCanonicalHash(db, 0) 318 if (stored == common.Hash{}) { 319 if genesis == nil { 320 log.Info("Writing default main-net genesis block") 321 genesis = DefaultGenesisBlock() 322 } else { 323 log.Info("Writing custom genesis block") 324 } 325 block, err := genesis.Commit(db, triedb) 326 if err != nil { 327 return genesis.Config, common.Hash{}, err 328 } 329 applyOverrides(genesis.Config) 330 return genesis.Config, block.Hash(), nil 331 } 332 // The genesis block is present(perhaps in ancient database) while the 333 // state database is not initialized yet. It can happen that the node 334 // is initialized with an external ancient store. Commit genesis state 335 // in this case. 336 header := rawdb.ReadHeader(db, stored, 0) 337 if header.Root != types.EmptyRootHash && !triedb.Initialized(header.Root) { 338 if genesis == nil { 339 genesis = DefaultGenesisBlock() 340 } 341 // Ensure the stored genesis matches with the given one. 342 hash := genesis.ToBlock().Hash() 343 if hash != stored { 344 return genesis.Config, hash, &GenesisMismatchError{stored, hash} 345 } 346 block, err := genesis.Commit(db, triedb) 347 if err != nil { 348 return genesis.Config, hash, err 349 } 350 applyOverrides(genesis.Config) 351 return genesis.Config, block.Hash(), nil 352 } 353 // Check whether the genesis block is already written. 354 if genesis != nil { 355 hash := genesis.ToBlock().Hash() 356 if hash != stored { 357 return genesis.Config, hash, &GenesisMismatchError{stored, hash} 358 } 359 } 360 // Get the existing chain configuration. 361 newcfg := genesis.configOrDefault(stored) 362 applyOverrides(newcfg) 363 if err := newcfg.CheckConfigForkOrder(); err != nil { 364 return newcfg, common.Hash{}, err 365 } 366 storedcfg := rawdb.ReadChainConfig(db, stored) 367 if storedcfg == nil { 368 log.Warn("Found genesis block without chain config") 369 rawdb.WriteChainConfig(db, stored, newcfg) 370 return newcfg, stored, nil 371 } 372 storedData, _ := json.Marshal(storedcfg) 373 // Special case: if a private network is being used (no genesis and also no 374 // mainnet hash in the database), we must not apply the `configOrDefault` 375 // chain config as that would be AllProtocolChanges (applying any new fork 376 // on top of an existing private network genesis block). In that case, only 377 // apply the overrides. 378 if genesis == nil && stored != params.MainnetGenesisHash { 379 newcfg = storedcfg 380 applyOverrides(newcfg) 381 } 382 // Check config compatibility and write the config. Compatibility errors 383 // are returned to the caller unless we're already at block zero. 384 head := rawdb.ReadHeadHeader(db) 385 if head == nil { 386 return newcfg, stored, errors.New("missing head header") 387 } 388 compatErr := storedcfg.CheckCompatible(newcfg, head.Number.Uint64(), head.Time) 389 if compatErr != nil && ((head.Number.Uint64() != 0 && compatErr.RewindToBlock != 0) || (head.Time != 0 && compatErr.RewindToTime != 0)) { 390 return newcfg, stored, compatErr 391 } 392 // Don't overwrite if the old is identical to the new 393 if newData, _ := json.Marshal(newcfg); !bytes.Equal(storedData, newData) { 394 rawdb.WriteChainConfig(db, stored, newcfg) 395 } 396 return newcfg, stored, nil 397 } 398 399 // LoadChainConfig loads the stored chain config if it is already present in 400 // database, otherwise, return the config in the provided genesis specification. 401 func LoadChainConfig(db zonddb.Database, genesis *Genesis) (*params.ChainConfig, error) { 402 // Load the stored chain config from the database. It can be nil 403 // in case the database is empty. Notably, we only care about the 404 // chain config corresponds to the canonical chain. 405 stored := rawdb.ReadCanonicalHash(db, 0) 406 if stored != (common.Hash{}) { 407 storedcfg := rawdb.ReadChainConfig(db, stored) 408 if storedcfg != nil { 409 return storedcfg, nil 410 } 411 } 412 // Load the config from the provided genesis specification 413 if genesis != nil { 414 // Reject invalid genesis spec without valid chain config 415 if genesis.Config == nil { 416 return nil, errGenesisNoConfig 417 } 418 // If the canonical genesis header is present, but the chain 419 // config is missing(initialize the empty leveldb with an 420 // external ancient chain segment), ensure the provided genesis 421 // is matched. 422 if stored != (common.Hash{}) && genesis.ToBlock().Hash() != stored { 423 return nil, &GenesisMismatchError{stored, genesis.ToBlock().Hash()} 424 } 425 return genesis.Config, nil 426 } 427 // There is no stored chain config and no new config provided, 428 // In this case the default chain config(mainnet) will be used 429 return params.MainnetChainConfig, nil 430 } 431 432 func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig { 433 switch { 434 case g != nil: 435 return g.Config 436 case ghash == params.MainnetGenesisHash: 437 return params.MainnetChainConfig 438 case ghash == params.SepoliaGenesisHash: 439 return params.SepoliaChainConfig 440 case ghash == params.GoerliGenesisHash: 441 return params.GoerliChainConfig 442 default: 443 return params.AllEthashProtocolChanges 444 } 445 } 446 447 // ToBlock returns the genesis block according to genesis specification. 448 func (g *Genesis) ToBlock() *types.Block { 449 root, err := g.Alloc.deriveHash() 450 if err != nil { 451 panic(err) 452 } 453 head := &types.Header{ 454 Number: new(big.Int).SetUint64(g.Number), 455 Nonce: types.EncodeNonce(g.Nonce), 456 Time: g.Timestamp, 457 ParentHash: g.ParentHash, 458 Extra: g.ExtraData, 459 GasLimit: g.GasLimit, 460 GasUsed: g.GasUsed, 461 BaseFee: g.BaseFee, 462 Difficulty: g.Difficulty, 463 MixDigest: g.Mixhash, 464 Coinbase: g.Coinbase, 465 Root: root, 466 } 467 if g.GasLimit == 0 { 468 head.GasLimit = params.GenesisGasLimit 469 } 470 if g.Difficulty == nil && g.Mixhash == (common.Hash{}) { 471 head.Difficulty = params.GenesisDifficulty 472 } 473 if g.Config != nil && g.Config.IsLondon(common.Big0) { 474 if g.BaseFee != nil { 475 head.BaseFee = g.BaseFee 476 } else { 477 head.BaseFee = new(big.Int).SetUint64(params.InitialBaseFee) 478 } 479 } 480 var withdrawals []*types.Withdrawal 481 if conf := g.Config; conf != nil { 482 num := big.NewInt(int64(g.Number)) 483 if conf.IsShanghai(num, g.Timestamp) { 484 head.WithdrawalsHash = &types.EmptyWithdrawalsHash 485 withdrawals = make([]*types.Withdrawal, 0) 486 } 487 if conf.IsCancun(num, g.Timestamp) { 488 // EIP-4788: The parentBeaconBlockRoot of the genesis block is always 489 // the zero hash. This is because the genesis block does not have a parent 490 // by definition. 491 head.ParentBeaconRoot = new(common.Hash) 492 // EIP-4844 fields 493 head.ExcessBlobGas = g.ExcessBlobGas 494 head.BlobGasUsed = g.BlobGasUsed 495 if head.ExcessBlobGas == nil { 496 head.ExcessBlobGas = new(uint64) 497 } 498 if head.BlobGasUsed == nil { 499 head.BlobGasUsed = new(uint64) 500 } 501 } 502 } 503 return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil)).WithWithdrawals(withdrawals) 504 } 505 506 // Commit writes the block and state of a genesis specification to the database. 507 // The block is committed as the canonical head block. 508 func (g *Genesis) Commit(db zonddb.Database, triedb *trie.Database) (*types.Block, error) { 509 block := g.ToBlock() 510 if block.Number().Sign() != 0 { 511 return nil, errors.New("can't commit genesis block with number > 0") 512 } 513 config := g.Config 514 if config == nil { 515 config = params.AllEthashProtocolChanges 516 } 517 if err := config.CheckConfigForkOrder(); err != nil { 518 return nil, err 519 } 520 if config.Clique != nil && len(block.Extra()) < 32+crypto.SignatureLength { 521 return nil, errors.New("can't start clique chain without signers") 522 } 523 // All the checks has passed, flush the states derived from the genesis 524 // specification as well as the specification itself into the provided 525 // database. 526 if err := g.Alloc.flush(db, triedb, block.Hash()); err != nil { 527 return nil, err 528 } 529 rawdb.WriteTd(db, block.Hash(), block.NumberU64(), block.Difficulty()) 530 rawdb.WriteBlock(db, block) 531 rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), nil) 532 rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64()) 533 rawdb.WriteHeadBlockHash(db, block.Hash()) 534 rawdb.WriteHeadFastBlockHash(db, block.Hash()) 535 rawdb.WriteHeadHeaderHash(db, block.Hash()) 536 rawdb.WriteChainConfig(db, block.Hash(), config) 537 return block, nil 538 } 539 540 // MustCommit writes the genesis block and state to db, panicking on error. 541 // The block is committed as the canonical head block. 542 func (g *Genesis) MustCommit(db zonddb.Database, triedb *trie.Database) *types.Block { 543 block, err := g.Commit(db, triedb) 544 if err != nil { 545 panic(err) 546 } 547 return block 548 } 549 550 // DefaultGenesisBlock returns the Ethereum main net genesis block. 551 func DefaultGenesisBlock() *Genesis { 552 return &Genesis{ 553 Config: params.MainnetChainConfig, 554 Nonce: 66, 555 ExtraData: hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"), 556 GasLimit: 5000, 557 Difficulty: big.NewInt(17179869184), 558 Alloc: decodePrealloc(mainnetAllocData), 559 } 560 } 561 562 // DefaultGoerliGenesisBlock returns the Görli network genesis block. 563 func DefaultGoerliGenesisBlock() *Genesis { 564 return &Genesis{ 565 Config: params.GoerliChainConfig, 566 Timestamp: 1548854791, 567 ExtraData: hexutil.MustDecode("0x22466c6578692069732061207468696e6722202d204166726900000000000000e0a2bd4258d2768837baa26a28fe71dc079f84c70000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), 568 GasLimit: 10485760, 569 Difficulty: big.NewInt(1), 570 Alloc: decodePrealloc(goerliAllocData), 571 } 572 } 573 574 // DefaultSepoliaGenesisBlock returns the Sepolia network genesis block. 575 func DefaultSepoliaGenesisBlock() *Genesis { 576 return &Genesis{ 577 Config: params.SepoliaChainConfig, 578 Nonce: 0, 579 ExtraData: []byte("Sepolia, Athens, Attica, Greece!"), 580 GasLimit: 0x1c9c380, 581 Difficulty: big.NewInt(0x20000), 582 Timestamp: 1633267481, 583 Alloc: decodePrealloc(sepoliaAllocData), 584 } 585 } 586 587 // DefaultHoleskyGenesisBlock returns the Holesky network genesis block. 588 func DefaultHoleskyGenesisBlock() *Genesis { 589 return &Genesis{ 590 Config: params.HoleskyChainConfig, 591 Nonce: 0x1234, 592 ExtraData: hexutil.MustDecode("0x686f77206d7563682069732074686520666973683f"), 593 GasLimit: 0x17d7840, 594 Difficulty: big.NewInt(0x01), 595 Timestamp: 1694786100, 596 Alloc: decodePrealloc(holeskyAllocData), 597 } 598 } 599 600 // DefaultBetaNetGenesisBlock returns the BetaNet network genesis block. 601 func DefaultBetaNetGenesisBlock() *Genesis { 602 return &Genesis{ 603 Config: params.BetaNetChainConfig, 604 Nonce: 0, 605 ExtraData: []byte("BetaNet, Zond, XMSS, Dilithium!!"), 606 GasLimit: 0x1c9c380, 607 Difficulty: big.NewInt(0x1), 608 Timestamp: 1705841668, 609 Alloc: decodePreallocWithContractCode(betaNetAllocData), 610 } 611 } 612 613 // DeveloperGenesisBlock returns the 'geth --dev' genesis block. 614 func DeveloperGenesisBlock(gasLimit uint64, faucet common.Address) *Genesis { 615 // Override the default period to the user requested one 616 config := *params.AllDevChainProtocolChanges 617 618 // Assemble and return the genesis with the precompiles and faucet pre-funded 619 return &Genesis{ 620 Config: &config, 621 GasLimit: gasLimit, 622 BaseFee: big.NewInt(params.InitialBaseFee), 623 Difficulty: big.NewInt(0), 624 Alloc: map[common.Address]GenesisAccount{ 625 common.BytesToAddress([]byte{1}): {Balance: big.NewInt(1)}, // ECRecover 626 common.BytesToAddress([]byte{2}): {Balance: big.NewInt(1)}, // SHA256 627 common.BytesToAddress([]byte{3}): {Balance: big.NewInt(1)}, // RIPEMD 628 common.BytesToAddress([]byte{4}): {Balance: big.NewInt(1)}, // Identity 629 common.BytesToAddress([]byte{5}): {Balance: big.NewInt(1)}, // ModExp 630 common.BytesToAddress([]byte{6}): {Balance: big.NewInt(1)}, // ECAdd 631 common.BytesToAddress([]byte{7}): {Balance: big.NewInt(1)}, // ECScalarMul 632 common.BytesToAddress([]byte{8}): {Balance: big.NewInt(1)}, // ECPairing 633 common.BytesToAddress([]byte{9}): {Balance: big.NewInt(1)}, // BLAKE2b 634 faucet: {Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))}, 635 }, 636 } 637 } 638 639 func decodePrealloc(data string) GenesisAlloc { 640 var p []struct { 641 Addr *big.Int 642 Balance *big.Int 643 Misc *struct { 644 Nonce uint64 645 Code []byte 646 Slots []struct { 647 Key common.Hash 648 Val common.Hash 649 } 650 } `rlp:"optional"` 651 } 652 if err := rlp.NewStream(strings.NewReader(data), 0).Decode(&p); err != nil { 653 panic(err) 654 } 655 ga := make(GenesisAlloc, len(p)) 656 for _, account := range p { 657 acc := GenesisAccount{Balance: account.Balance} 658 if account.Misc != nil { 659 acc.Nonce = account.Misc.Nonce 660 acc.Code = account.Misc.Code 661 662 acc.Storage = make(map[common.Hash]common.Hash) 663 for _, slot := range account.Misc.Slots { 664 acc.Storage[slot.Key] = slot.Val 665 } 666 } 667 ga[common.BigToAddress(account.Addr)] = acc 668 } 669 return ga 670 } 671 672 func decodePreallocWithContractCode(data string) GenesisAlloc { 673 var p []struct { 674 Addr, Balance *big.Int 675 Code []byte 676 } 677 if err := rlp.NewStream(strings.NewReader(data), 0).Decode(&p); err != nil { 678 panic(err) 679 } 680 ga := make(GenesisAlloc, len(p)) 681 for _, account := range p { 682 ga[common.BigToAddress(account.Addr)] = GenesisAccount{Balance: account.Balance, Code: account.Code} 683 } 684 return ga 685 }