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