SetupGenesisBlock writes or updates the genesis block in db. The block that will be used is: genesis == nil genesis != nil +------------------------------------------ db has no genesis | main-net default | genesis db has genesis | from DB | genesis (must match) The stored c
(db database.Database, genesis *Genesis)
| 138 | // |
| 139 | // The returned chain configuration is never nil. |
| 140 | func SetupGenesisBlock(db database.Database, genesis *Genesis) (*configs.ChainConfig, common.Hash, error) { |
| 141 | if genesis != nil && genesis.Config == nil { |
| 142 | return configs.ChainConfigInfo(), common.Hash{}, errGenesisNoConfig |
| 143 | } |
| 144 | |
| 145 | stored := rawdb.ReadCanonicalHash(db, 0) |
| 146 | if (stored == common.Hash{}) { |
| 147 | // Just commit the new block if there is no stored genesis block. |
| 148 | if genesis == nil { |
| 149 | log.Info("Writing default main-net genesis block") |
| 150 | genesis = DefaultGenesisBlock() |
| 151 | } else { |
| 152 | log.Info("Writing custom genesis block") |
| 153 | } |
| 154 | block, err := genesis.Commit(db) |
| 155 | return genesis.Config, block.Hash(), err |
| 156 | } else { |
| 157 | // Get the existing chain configuration. |
| 158 | storedCfg := rawdb.ReadChainConfig(db, stored) |
| 159 | newCfg := genesis.configOrDefault(stored) |
| 160 | var finalCfg *configs.ChainConfig |
| 161 | if genesis != nil { |
| 162 | // Check whether the genesis block is already written. |
| 163 | hash := genesis.ToBlock(nil).Hash() |
| 164 | if hash != stored { |
| 165 | return genesis.Config, hash, &GenesisMismatchError{stored, hash} |
| 166 | } |
| 167 | finalCfg = updateChainConfig(storedCfg, newCfg, db, stored) |
| 168 | } else { |
| 169 | // Special case: don't change the existing config of a non-mainnet chain if no new |
| 170 | // config is supplied. These chains would get AllProtocolChanges (and a compat error) |
| 171 | // if we just continued here. |
| 172 | if stored != MainnetGenesisHash { |
| 173 | return storedCfg, stored, nil |
| 174 | } else { |
| 175 | finalCfg = updateChainConfig(storedCfg, newCfg, db, stored) |
| 176 | } |
| 177 | } |
| 178 | return finalCfg, stored, nil |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | func updateChainConfig(storedcfg *configs.ChainConfig, newcfg *configs.ChainConfig, db database.Database, stored common.Hash) *configs.ChainConfig { |
| 183 | if storedcfg == nil { |