ToBlock creates the genesis block and writes state of a genesis specification to the given database (or discards it if nil).
(db database.Database)
| 216 | // ToBlock creates the genesis block and writes state of a genesis specification |
| 217 | // to the given database (or discards it if nil). |
| 218 | func (g *Genesis) ToBlock(db database.Database) *types.Block { |
| 219 | if db == nil { |
| 220 | db = database.NewMemDatabase() |
| 221 | } |
| 222 | statedb, _ := state.New(common.Hash{}, state.NewDatabase(db)) |
| 223 | for addr, account := range g.Alloc { |
| 224 | statedb.AddBalance(addr, account.Balance) |
| 225 | statedb.SetCode(addr, account.Code) |
| 226 | statedb.SetNonce(addr, account.Nonce) |
| 227 | for key, value := range account.Storage { |
| 228 | statedb.SetState(addr, key, value) |
| 229 | } |
| 230 | } |
| 231 | root := statedb.IntermediateRoot(false) |
| 232 | head := &types.Header{ |
| 233 | Number: new(big.Int).SetUint64(g.Number), |
| 234 | Time: new(big.Int).SetUint64(g.Timestamp), |
| 235 | ParentHash: g.ParentHash, |
| 236 | Extra: g.ExtraData, |
| 237 | GasLimit: g.GasLimit, |
| 238 | GasUsed: g.GasUsed, |
| 239 | Coinbase: g.Coinbase, |
| 240 | StateRoot: root, |
| 241 | Dpor: g.Dpor, |
| 242 | } |
| 243 | if g.GasLimit == 0 { |
| 244 | head.GasLimit = configs.DefaultGasLimitPerBlock |
| 245 | } |
| 246 | if _, err := statedb.Commit(false); err != nil { |
| 247 | log.Error("Error in genesis", "error", err) |
| 248 | } |
| 249 | if err := statedb.Database().TrieDB().Commit(root, true); err != nil { |
| 250 | log.Error("Error in genesis", "error", err) |
| 251 | } |
| 252 | return types.NewBlock(head, nil, nil) |
| 253 | } |
| 254 | |
| 255 | // Commit writes the block and state of a genesis specification to the database. |
| 256 | // The block is committed as the canonical head block. |