core, light: write chain data in atomic way (#20287)
* core: write chain data in atomic way * core, light: address comments * core, light: fix linter * core, light: address comments
This commit is contained in:
committed by
Péter Szilágyi
parent
0af96d2556
commit
770316dc20
@ -159,7 +159,6 @@ func (lc *LightChain) loadLastState() error {
|
||||
lc.hc.SetCurrentHeader(header)
|
||||
}
|
||||
}
|
||||
|
||||
// Issue a status log and return
|
||||
header := lc.hc.CurrentHeader()
|
||||
headerTd := lc.GetTd(header.Hash(), header.Number.Uint64())
|
||||
@ -198,9 +197,13 @@ func (lc *LightChain) ResetWithGenesisBlock(genesis *types.Block) {
|
||||
defer lc.chainmu.Unlock()
|
||||
|
||||
// Prepare the genesis block and reinitialise the chain
|
||||
rawdb.WriteTd(lc.chainDb, genesis.Hash(), genesis.NumberU64(), genesis.Difficulty())
|
||||
rawdb.WriteBlock(lc.chainDb, genesis)
|
||||
|
||||
batch := lc.chainDb.NewBatch()
|
||||
rawdb.WriteTd(batch, genesis.Hash(), genesis.NumberU64(), genesis.Difficulty())
|
||||
rawdb.WriteBlock(batch, genesis)
|
||||
rawdb.WriteHeadHeaderHash(batch, genesis.Hash())
|
||||
if err := batch.Write(); err != nil {
|
||||
log.Crit("Failed to reset genesis block", "err", err)
|
||||
}
|
||||
lc.genesisBlock = genesis
|
||||
lc.hc.SetGenesis(lc.genesisBlock.Header())
|
||||
lc.hc.SetCurrentHeader(lc.genesisBlock.Header())
|
||||
@ -323,13 +326,22 @@ func (lc *LightChain) Rollback(chain []common.Hash) {
|
||||
lc.chainmu.Lock()
|
||||
defer lc.chainmu.Unlock()
|
||||
|
||||
batch := lc.chainDb.NewBatch()
|
||||
for i := len(chain) - 1; i >= 0; i-- {
|
||||
hash := chain[i]
|
||||
|
||||
// Degrade the chain markers if they are explicitly reverted.
|
||||
// In theory we should update all in-memory markers in the
|
||||
// last step, however the direction of rollback is from high
|
||||
// to low, so it's safe the update in-memory markers directly.
|
||||
if head := lc.hc.CurrentHeader(); head.Hash() == hash {
|
||||
rawdb.WriteHeadHeaderHash(batch, head.ParentHash)
|
||||
lc.hc.SetCurrentHeader(lc.GetHeader(head.ParentHash, head.Number.Uint64()-1))
|
||||
}
|
||||
}
|
||||
if err := batch.Write(); err != nil {
|
||||
log.Crit("Failed to rollback light chain", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// postChainEvents iterates over the events generated by a chain insertion and
|
||||
@ -493,6 +505,7 @@ func (lc *LightChain) SyncCheckpoint(ctx context.Context, checkpoint *params.Tru
|
||||
// Ensure the chain didn't move past the latest block while retrieving it
|
||||
if lc.hc.CurrentHeader().Number.Uint64() < header.Number.Uint64() {
|
||||
log.Info("Updated latest header based on CHT", "number", header.Number, "hash", header.Hash(), "age", common.PrettyAge(time.Unix(int64(header.Time), 0)))
|
||||
rawdb.WriteHeadHeaderHash(lc.chainDb, header.Hash())
|
||||
lc.hc.SetCurrentHeader(header)
|
||||
}
|
||||
return true
|
||||
|
Reference in New Issue
Block a user