Skip to content

Commit

Permalink
bugfix: Fixed the regeneration of state in StateBlock independent of …
Browse files Browse the repository at this point in the history
…SetCurrentHeader
  • Loading branch information
gameofpointers authored and jdowning100 committed Aug 29, 2023
1 parent bbaf56f commit 9bffde1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 7 additions & 9 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,11 @@ func (p *StateProcessor) StateAtBlock(block *types.Block, reexec uint64, base *s
}
}

var newHeads []*types.Block
if base != nil {
// The optional base statedb is given, mark the start point as parent block
statedb, database, report = base, base.Database(), false
current = p.hc.GetBlock(block.ParentHash(), block.NumberU64()-1)
current = p.hc.GetBlockOrCandidate(block.ParentHash(), block.NumberU64()-1)
} else {
// Otherwise try to reexec blocks until we find a state or reach our limit
current = block
Expand All @@ -651,12 +652,13 @@ func (p *StateProcessor) StateAtBlock(block *types.Block, reexec uint64, base *s
return statedb, nil
}
}
newHeads = append(newHeads, block)
// Database does not have the state for the given block, try to regenerate
for i := uint64(0); i < reexec; i++ {
if current.NumberU64() == 0 {
return nil, errors.New("genesis state is missing")
}
parent := p.hc.GetBlock(current.ParentHash(), current.NumberU64()-1)
parent := p.hc.GetBlockOrCandidate(current.ParentHash(), current.NumberU64()-1)
if parent == nil {
return nil, fmt.Errorf("missing block %v %d", current.ParentHash(), current.NumberU64()-1)
}
Expand All @@ -666,6 +668,7 @@ func (p *StateProcessor) StateAtBlock(block *types.Block, reexec uint64, base *s
if err == nil {
break
}
newHeads = append(newHeads, current)
}
if err != nil {
switch err.(type) {
Expand All @@ -682,19 +685,14 @@ func (p *StateProcessor) StateAtBlock(block *types.Block, reexec uint64, base *s
logged time.Time
parent common.Hash
)
for current.NumberU64() < origin {
for i := len(newHeads) - 1; i >= 0; i-- {
current := newHeads[i]
// Print progress logs if long enough time elapsed
if time.Since(logged) > 8*time.Second && report {
log.Info("Regenerating historical state", "block", current.NumberU64()+1, "target", origin, "remaining", origin-current.NumberU64()-1, "elapsed", time.Since(start))
logged = time.Now()
}

// Retrieve the next block to regenerate and process it
next := current.NumberU64() + 1
if current = p.hc.GetBlockByNumber(next); current == nil {
return nil, fmt.Errorf("block #%d not found", next)
}

etxSet := rawdb.ReadEtxSet(p.hc.bc.db, current.ParentHash(), current.NumberU64()-1)
inboundEtxs := rawdb.ReadInboundEtxs(p.hc.bc.db, current.Hash())
etxSet.Update(inboundEtxs, current.NumberU64())
Expand Down
2 changes: 1 addition & 1 deletion core/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ func (w *worker) commit(env *environment, interval func(), update bool, start ti
}
// Create a local environment copy, avoid the data race with snapshot state.
env := env.copy(w.hc.ProcessingState())
parent := w.hc.GetBlock(env.header.ParentHash(), env.header.NumberU64()-1)
parent := w.hc.GetBlockOrCandidate(env.header.ParentHash(), env.header.NumberU64()-1)
block, err := w.FinalizeAssemble(w.hc, env.header, parent, env.state, env.txs, env.unclelist(), env.etxs, env.subManifest, env.receipts)
if err != nil {
return err
Expand Down

0 comments on commit 9bffde1

Please sign in to comment.