github.com/klaytn/klaytn@v1.12.1/cmd/homi/genesis/genesis.go (about) 1 // Copyright 2018 The klaytn Authors 2 // Copyright 2017 AMIS Technologies 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 18 package genesis 19 20 import ( 21 "encoding/json" 22 "math/big" 23 "os" 24 "path/filepath" 25 "time" 26 27 "github.com/klaytn/klaytn/blockchain" 28 istcommon "github.com/klaytn/klaytn/cmd/homi/common" 29 "github.com/klaytn/klaytn/params" 30 ) 31 32 const ( 33 FileName = "genesis.json" 34 InitBlockScore = 1 35 ) 36 37 func New(options ...Option) *blockchain.Genesis { 38 genesis := &blockchain.Genesis{ 39 Timestamp: uint64(time.Now().Unix()), 40 BlockScore: big.NewInt(InitBlockScore), 41 Alloc: make(blockchain.GenesisAlloc), 42 Config: ¶ms.ChainConfig{ 43 ChainID: big.NewInt(2018), 44 UnitPrice: 0, 45 DeriveShaImpl: 0, 46 }, 47 } 48 49 for _, opt := range options { 50 opt(genesis) 51 } 52 53 return genesis 54 } 55 56 func NewClique(options ...Option) *blockchain.Genesis { 57 genesis := &blockchain.Genesis{ 58 Timestamp: uint64(time.Now().Unix()), 59 BlockScore: big.NewInt(InitBlockScore), 60 Alloc: make(blockchain.GenesisAlloc), 61 Config: ¶ms.ChainConfig{ 62 ChainID: big.NewInt(3000), // TODO-Klaytn Needs Optional chainID 63 }, 64 } 65 66 for _, opt := range options { 67 opt(genesis) 68 } 69 70 return genesis 71 } 72 73 func NewFileAt(dir string, options ...Option) string { 74 genesis := New(options...) 75 if err := Save(dir, genesis); err != nil { 76 logger.Error("Failed to save genesis", "dir", dir, "err", err) 77 return "" 78 } 79 80 return filepath.Join(dir, FileName) 81 } 82 83 func NewFile(options ...Option) string { 84 dir, _ := istcommon.GenerateRandomDir() 85 return NewFileAt(dir, options...) 86 } 87 88 func Save(dataDir string, genesis *blockchain.Genesis) error { 89 filePath := filepath.Join(dataDir, FileName) 90 91 var raw []byte 92 var err error 93 raw, err = json.Marshal(genesis) 94 if err != nil { 95 return err 96 } 97 return os.WriteFile(filePath, raw, 0o600) 98 }