Skip to content

Commit 1925ff7

Browse files
committed
core/state: simplify journal revision system
1 parent 2ccd040 commit 1925ff7

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

core/state/journal.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@ package state
1818

1919
import (
2020
"fmt"
21-
"sort"
2221

2322
"github.com/ethereum/go-ethereum/common"
2423
"github.com/ethereum/go-ethereum/core/types"
2524
"github.com/holiman/uint256"
2625
)
2726

28-
type revision struct {
29-
id int
30-
journalIndex int
31-
}
32-
3327
// journalEntry is a modification entry in the state change journal that can be
3428
// reverted on demand.
3529
type journalEntry interface {
@@ -47,8 +41,8 @@ type journal struct {
4741
entries []journalEntry // Current changes tracked by the journal
4842
dirties map[common.Address]int // Dirty accounts and the number of changes
4943

50-
validRevisions []revision
51-
nextRevisionId int
44+
// snapshots is a list of the indexes to revert to
45+
snapshots []int
5246
}
5347

5448
// newJournal creates a new initialized journal.
@@ -63,33 +57,27 @@ func newJournal() *journal {
6357
// slices can be reused
6458
func (j *journal) Reset() {
6559
j.entries = j.entries[:0]
66-
j.validRevisions = j.validRevisions[:0]
60+
j.snapshots = j.snapshots[:0]
6761
j.dirties = make(map[common.Address]int)
68-
j.nextRevisionId = 0
6962
}
7063

7164
// Snapshot returns an identifier for the current revision of the state.
7265
func (j *journal) Snapshot() int {
73-
id := j.nextRevisionId
74-
j.nextRevisionId++
75-
j.validRevisions = append(j.validRevisions, revision{id, j.length()})
66+
id := len(j.snapshots)
67+
j.snapshots = append(j.snapshots, j.length())
7668
return id
7769
}
7870

7971
// RevertToSnapshot reverts all state changes made since the given revision.
80-
func (j *journal) RevertToSnapshot(revid int, s *StateDB) {
81-
// Find the snapshot in the stack of valid snapshots.
82-
idx := sort.Search(len(j.validRevisions), func(i int) bool {
83-
return j.validRevisions[i].id >= revid
84-
})
85-
if idx == len(j.validRevisions) || j.validRevisions[idx].id != revid {
86-
panic(fmt.Errorf("revision id %v cannot be reverted", revid))
72+
func (j *journal) RevertToSnapshot(id int, s *StateDB) {
73+
if id >= len(j.snapshots) {
74+
panic(fmt.Errorf("revision id %v cannot be reverted", id))
8775
}
88-
snapshot := j.validRevisions[idx].journalIndex
76+
snapshot := j.snapshots[id]
8977

9078
// Replay the journal to undo changes and remove invalidated snapshots
9179
j.revert(s, snapshot)
92-
j.validRevisions = j.validRevisions[:idx]
80+
j.snapshots = j.snapshots[:id]
9381
}
9482

9583
// append inserts a new modification entry to the end of the change journal.

0 commit comments

Comments
 (0)