@@ -18,18 +18,12 @@ package state
18
18
19
19
import (
20
20
"fmt"
21
- "sort"
22
21
23
22
"github.com/ethereum/go-ethereum/common"
24
23
"github.com/ethereum/go-ethereum/core/types"
25
24
"github.com/holiman/uint256"
26
25
)
27
26
28
- type revision struct {
29
- id int
30
- journalIndex int
31
- }
32
-
33
27
// journalEntry is a modification entry in the state change journal that can be
34
28
// reverted on demand.
35
29
type journalEntry interface {
@@ -47,8 +41,8 @@ type journal struct {
47
41
entries []journalEntry // Current changes tracked by the journal
48
42
dirties map [common.Address ]int // Dirty accounts and the number of changes
49
43
50
- validRevisions [] revision
51
- nextRevisionId int
44
+ // snapshots is a list of the indexes to revert to
45
+ snapshots [] int
52
46
}
53
47
54
48
// newJournal creates a new initialized journal.
@@ -63,33 +57,27 @@ func newJournal() *journal {
63
57
// slices can be reused
64
58
func (j * journal ) Reset () {
65
59
j .entries = j .entries [:0 ]
66
- j .validRevisions = j .validRevisions [:0 ]
60
+ j .snapshots = j .snapshots [:0 ]
67
61
j .dirties = make (map [common.Address ]int )
68
- j .nextRevisionId = 0
69
62
}
70
63
71
64
// Snapshot returns an identifier for the current revision of the state.
72
65
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 ())
76
68
return id
77
69
}
78
70
79
71
// 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 ))
87
75
}
88
- snapshot := j .validRevisions [ idx ]. journalIndex
76
+ snapshot := j .snapshots [ id ]
89
77
90
78
// Replay the journal to undo changes and remove invalidated snapshots
91
79
j .revert (s , snapshot )
92
- j .validRevisions = j .validRevisions [: idx ]
80
+ j .snapshots = j .snapshots [: id ]
93
81
}
94
82
95
83
// append inserts a new modification entry to the end of the change journal.
0 commit comments