MCPcopy Create free account
hub / github.com/CPChain/chain / ToBlock

Method ToBlock

core/genesis.go:218–253  ·  view source on GitHub ↗

ToBlock creates the genesis block and writes state of a genesis specification to the given database (or discards it if nil).

(db database.Database)

Source from the content-addressed store, hash-verified

216// ToBlock creates the genesis block and writes state of a genesis specification
217// to the given database (or discards it if nil).
218func (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.

Callers 4

CommitMethod · 0.95
TestDefaultGenesisBlockFunction · 0.80
SetupGenesisBlockFunction · 0.80
RunMethod · 0.80

Calls 9

IntermediateRootMethod · 0.80
AddBalanceMethod · 0.65
SetCodeMethod · 0.65
SetNonceMethod · 0.65
SetStateMethod · 0.65
CommitMethod · 0.65
ErrorMethod · 0.65
TrieDBMethod · 0.65
DatabaseMethod · 0.65

Tested by 1

TestDefaultGenesisBlockFunction · 0.64