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