@@ -19,7 +19,6 @@ package state
19
19
20
20
import (
21
21
"fmt"
22
- "sort"
23
22
"time"
24
23
25
24
"github.com/ethereum/go-ethereum/common"
@@ -42,11 +41,6 @@ const (
42
41
storageDeleteLimit = 512 * 1024 * 1024
43
42
)
44
43
45
- type revision struct {
46
- id int
47
- journalIndex int
48
- }
49
-
50
44
// StateDB structs within the ethereum protocol are used to store anything
51
45
// within the merkle trie. StateDBs take care of caching and storing
52
46
// nested states. It's the general query interface to retrieve:
@@ -113,9 +107,7 @@ type StateDB struct {
113
107
114
108
// Journal of state modifications. This is the backbone of
115
109
// Snapshot and RevertToSnapshot.
116
- journal * journal
117
- validRevisions []revision
118
- nextRevisionId int
110
+ journal * journal
119
111
120
112
// Measurements gathered during execution for debugging purposes
121
113
AccountReads time.Duration
@@ -774,26 +766,12 @@ func (s *StateDB) Copy() *StateDB {
774
766
775
767
// Snapshot returns an identifier for the current revision of the state.
776
768
func (s * StateDB ) Snapshot () int {
777
- id := s .nextRevisionId
778
- s .nextRevisionId ++
779
- s .validRevisions = append (s .validRevisions , revision {id , s .journal .length ()})
780
- return id
769
+ return s .journal .Snapshot ()
781
770
}
782
771
783
772
// RevertToSnapshot reverts all state changes made since the given revision.
784
773
func (s * StateDB ) RevertToSnapshot (revid int ) {
785
- // Find the snapshot in the stack of valid snapshots.
786
- idx := sort .Search (len (s .validRevisions ), func (i int ) bool {
787
- return s .validRevisions [i ].id >= revid
788
- })
789
- if idx == len (s .validRevisions ) || s .validRevisions [idx ].id != revid {
790
- panic (fmt .Errorf ("revision id %v cannot be reverted" , revid ))
791
- }
792
- snapshot := s .validRevisions [idx ].journalIndex
793
-
794
- // Replay the journal to undo changes and remove invalidated snapshots
795
- s .journal .revert (s , snapshot )
796
- s .validRevisions = s .validRevisions [:idx ]
774
+ s .journal .RevertToSnapshot (revid , s )
797
775
}
798
776
799
777
// GetRefund returns the current value of the refund counter.
@@ -924,11 +902,8 @@ func (s *StateDB) SetTxContext(thash common.Hash, ti int) {
924
902
}
925
903
926
904
func (s * StateDB ) clearJournalAndRefund () {
927
- if len (s .journal .entries ) > 0 {
928
- s .journal = newJournal ()
929
- s .refund = 0
930
- }
931
- s .validRevisions = s .validRevisions [:0 ] // Snapshots can be created without journal entries
905
+ s .journal .Reset ()
906
+ s .refund = 0
932
907
}
933
908
934
909
// fastDeleteStorage is the function that efficiently deletes the storage trie
0 commit comments