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

Method loadLastState

core/blockchain.go:329–388  ·  view source on GitHub ↗

loadLastState loads the last known chain state from the database. This method assumes that the chain manager mutex is held.

()

Source from the content-addressed store, hash-verified

327// loadLastState loads the last known chain state from the database. This method
328// assumes that the chain manager mutex is held.
329func (bc *BlockChain) loadLastState() error {
330 // Restore the last known head block
331 head := rawdb.ReadHeadBlockHash(bc.db)
332 if head == (common.Hash{}) {
333 // Corrupt or empty database, init from scratch
334 log.Warn("Empty database, resetting chain")
335 return bc.Reset()
336 }
337 // Make sure the entire head block is available
338 currentBlock := bc.GetBlockByHash(head)
339 if currentBlock == nil {
340 // Corrupt or empty database, init from scratch
341 log.Warn("Head block missing, resetting chain", "hash", head)
342 return bc.Reset()
343 }
344 // Make sure the state associated with the block is available
345 if _, err := state.New(currentBlock.StateRoot(), bc.stateCache); err != nil {
346 // Dangling block without a state associated, init from scratch
347 log.Warn("Head state missing, repairing chain", "number", currentBlock.Number(), "hash", currentBlock.Hash().Hex())
348 if err := bc.repair(&currentBlock); err != nil {
349 return err
350 }
351 }
352
353 // Make sure the private state associated with the block is available
354 if _, err := state.New(GetPrivateStateRoot(bc.db, currentBlock.StateRoot()), bc.privateStateCache); err != nil {
355 log.Warn("Head private state missing, repairing chain", "number", currentBlock.Number(), "hash", currentBlock.Hash().Hex())
356 // TODO: use repair instead.
357 return bc.Reset()
358 }
359
360 // Everything seems to be fine, set as the head block
361 bc.currentBlock.Store(currentBlock)
362
363 // Restore the last known head header
364 currentHeader := currentBlock.Header()
365 if head := rawdb.ReadHeadHeaderHash(bc.db); head != (common.Hash{}) {
366 if header := bc.GetHeaderByHash(head); header != nil {
367 currentHeader = header
368 }
369 }
370 bc.hc.SetCurrentHeader(currentHeader)
371
372 // Restore the last known head fast block
373 bc.currentFastBlock.Store(currentBlock)
374 if head := rawdb.ReadHeadFastBlockHash(bc.db); head != (common.Hash{}) {
375 if block := bc.GetBlockByHash(head); block != nil {
376 bc.currentFastBlock.Store(block)
377 }
378 }
379
380 // Issue a status log for the user
381 currentFastBlock := bc.CurrentFastBlock()
382
383 log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash().Hex())
384 log.Info("Loaded most recent local full block", "number", currentBlock.Number(), "hash", currentBlock.Hash().Hex())
385 log.Info("Loaded most recent local fast block", "number", currentFastBlock.Number(), "hash", currentFastBlock.Hash().Hex())
386

Callers 2

NewBlockChainFunction · 0.95
SetHeadMethod · 0.95

Calls 13

ResetMethod · 0.95
GetBlockByHashMethod · 0.95
repairMethod · 0.95
GetHeaderByHashMethod · 0.95
CurrentFastBlockMethod · 0.95
GetPrivateStateRootFunction · 0.85
StateRootMethod · 0.80
HeaderMethod · 0.80
SetCurrentHeaderMethod · 0.80
WarnMethod · 0.65
HashMethod · 0.65
InfoMethod · 0.65

Tested by

no test coverage detected