Skip to content

Commit 140ee32

Browse files
committed
core/state/snapshot: verify account in range at interruption point
1 parent c10ac4f commit 140ee32

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed

core/state/snapshot/disklayer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ func (dl *diskLayer) Stale() bool {
7474
return dl.stale
7575
}
7676

77+
// markStale sets the stale flag as true.
78+
func (dl *diskLayer) markStale() {
79+
dl.lock.Lock()
80+
defer dl.lock.Unlock()
81+
82+
dl.stale = true
83+
}
84+
7785
// Account directly retrieves the account associated with a particular hash in
7886
// the snapshot slim data format.
7987
func (dl *diskLayer) Account(hash common.Hash) (*types.SlimAccount, error) {
@@ -175,3 +183,18 @@ func (dl *diskLayer) Storage(accountHash, storageHash common.Hash) ([]byte, erro
175183
func (dl *diskLayer) Update(blockHash common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer {
176184
return newDiffLayer(dl, blockHash, destructs, accounts, storage)
177185
}
186+
187+
// stopGeneration aborts the state snapshot generation if it is currently running.
188+
func (dl *diskLayer) stopGeneration() {
189+
dl.lock.RLock()
190+
generating := dl.genMarker != nil
191+
dl.lock.RUnlock()
192+
if !generating {
193+
return
194+
}
195+
if dl.genAbort != nil {
196+
abort := make(chan *generatorStats)
197+
dl.genAbort <- abort
198+
<-abort
199+
}
200+
}

core/state/snapshot/generate.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -631,16 +631,10 @@ func generateAccounts(ctx *generatorContext, dl *diskLayer, accMarker []byte) er
631631
accMarker = nil
632632
return nil
633633
}
634-
// Always reset the initial account range as 1 whenever recover from the
635-
// interruption. TODO(rjl493456442) can we remove it?
636-
var accountRange = accountCheckRange
637-
if len(accMarker) > 0 {
638-
accountRange = 1
639-
}
640634
origin := common.CopyBytes(accMarker)
641635
for {
642636
id := trie.StateTrieID(dl.root)
643-
exhausted, last, err := dl.generateRange(ctx, id, rawdb.SnapshotAccountPrefix, snapAccount, origin, accountRange, onAccount, types.FullAccountRLP)
637+
exhausted, last, err := dl.generateRange(ctx, id, rawdb.SnapshotAccountPrefix, snapAccount, origin, accountCheckRange, onAccount, types.FullAccountRLP)
644638
if err != nil {
645639
return err // The procedure it aborted, either by external signal or internal error.
646640
}
@@ -652,7 +646,6 @@ func generateAccounts(ctx *generatorContext, dl *diskLayer, accMarker []byte) er
652646
ctx.removeStorageLeft()
653647
break
654648
}
655-
accountRange = accountCheckRange
656649
}
657650
return nil
658651
}

core/state/snapshot/snapshot.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -258,24 +258,9 @@ func (t *Tree) Disable() {
258258
for _, layer := range t.layers {
259259
switch layer := layer.(type) {
260260
case *diskLayer:
261-
262-
layer.lock.RLock()
263-
generating := layer.genMarker != nil
264-
layer.lock.RUnlock()
265-
if !generating {
266-
// Generator is already aborted or finished
267-
break
268-
}
269-
// If the base layer is generating, abort it
270-
if layer.genAbort != nil {
271-
abort := make(chan *generatorStats)
272-
layer.genAbort <- abort
273-
<-abort
274-
}
275-
// Layer should be inactive now, mark it as stale
276-
layer.lock.Lock()
277-
layer.stale = true
278-
layer.lock.Unlock()
261+
layer.stopGeneration()
262+
layer.markStale()
263+
layer.Release()
279264

280265
case *diffLayer:
281266
// If the layer is a simple diff, simply mark as stale
@@ -730,16 +715,9 @@ func (t *Tree) Rebuild(root common.Hash) {
730715
for _, layer := range t.layers {
731716
switch layer := layer.(type) {
732717
case *diskLayer:
733-
// If the base layer is generating, abort it and save
734-
if layer.genAbort != nil {
735-
abort := make(chan *generatorStats)
736-
layer.genAbort <- abort
737-
<-abort
738-
}
739-
// Layer should be inactive now, mark it as stale
740-
layer.lock.Lock()
741-
layer.stale = true
742-
layer.lock.Unlock()
718+
layer.stopGeneration()
719+
layer.markStale()
720+
layer.Release()
743721

744722
case *diffLayer:
745723
// If the layer is a simple diff, simply mark as stale

0 commit comments

Comments
 (0)