-
Notifications
You must be signed in to change notification settings - Fork 197
/
baseJournalEntries.go
194 lines (158 loc) · 5.15 KB
/
baseJournalEntries.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package state
import (
"github.com/ElrondNetwork/elrond-go/data"
)
//------- BaseJournalEntryCreation
// BaseJournalEntryCreation creates a new account entry in the state trie
// through updater it can revert the created changes.
type BaseJournalEntryCreation struct {
key []byte
updater Updater
}
// NewBaseJournalEntryCreation outputs a new BaseJournalEntry implementation used to revert an account creation
func NewBaseJournalEntryCreation(key []byte, updater Updater) (*BaseJournalEntryCreation, error) {
if updater == nil || updater.IsInterfaceNil() {
return nil, ErrNilUpdater
}
if len(key) == 0 {
return nil, ErrNilOrEmptyKey
}
return &BaseJournalEntryCreation{
key: key,
updater: updater,
}, nil
}
// Revert applies undo operation
func (bjec *BaseJournalEntryCreation) Revert() (AccountHandler, error) {
return nil, bjec.updater.Update(bjec.key, nil)
}
// IsInterfaceNil returns true if there is no value under the interface
func (bjec *BaseJournalEntryCreation) IsInterfaceNil() bool {
if bjec == nil {
return true
}
return false
}
//------- BaseJournalEntryCodeHash
// BaseJournalEntryCodeHash creates a code hash change in account
type BaseJournalEntryCodeHash struct {
account AccountHandler
oldCodeHash []byte
}
// NewBaseJournalEntryCodeHash outputs a new BaseJournalEntry implementation used to save and revert a code hash change
func NewBaseJournalEntryCodeHash(account AccountHandler, oldCodeHash []byte) (*BaseJournalEntryCodeHash, error) {
if account == nil || account.IsInterfaceNil() {
return nil, ErrNilAccountHandler
}
return &BaseJournalEntryCodeHash{
account: account,
oldCodeHash: oldCodeHash,
}, nil
}
// Revert applies undo operation
func (bjech *BaseJournalEntryCodeHash) Revert() (AccountHandler, error) {
bjech.account.SetCodeHash(bjech.oldCodeHash)
return bjech.account, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (bjech *BaseJournalEntryCodeHash) IsInterfaceNil() bool {
if bjech == nil {
return true
}
return false
}
//------- BaseJournalEntryRoot
// BaseJournalEntryRootHash creates an account's root hash change
type BaseJournalEntryRootHash struct {
account AccountHandler
oldRootHash []byte
oldTrie data.Trie
}
// NewBaseJournalEntryRootHash outputs a new BaseJournalEntry used to save and revert an account's root hash change
func NewBaseJournalEntryRootHash(account AccountHandler, oldRootHash []byte, oldTrie data.Trie) (*BaseJournalEntryRootHash, error) {
if account == nil || account.IsInterfaceNil() {
return nil, ErrNilAccountHandler
}
return &BaseJournalEntryRootHash{
account: account,
oldRootHash: oldRootHash,
oldTrie: oldTrie,
}, nil
}
// Revert applies undo operation
func (bjer *BaseJournalEntryRootHash) Revert() (AccountHandler, error) {
bjer.account.SetRootHash(bjer.oldRootHash)
bjer.account.SetDataTrie(bjer.oldTrie)
return bjer.account, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (bjer *BaseJournalEntryRootHash) IsInterfaceNil() bool {
if bjer == nil {
return true
}
return false
}
//------- BaseJournalEntryData
// BaseJournalEntryData is used to mark an account's data change
type BaseJournalEntryData struct {
trie data.Trie
account AccountHandler
}
// NewBaseJournalEntryData outputs a new BaseJournalEntry implementation used to keep track of data change.
// The revert will practically empty the dirty data map
func NewBaseJournalEntryData(account AccountHandler, trie data.Trie) (*BaseJournalEntryData, error) {
if account == nil || account.IsInterfaceNil() {
return nil, ErrNilAccountHandler
}
return &BaseJournalEntryData{
account: account,
trie: trie,
}, nil
}
// Revert will empty the dirtyData map from AccountState
func (bjed *BaseJournalEntryData) Revert() (AccountHandler, error) {
dataTrieTracker := bjed.account.DataTrieTracker()
if dataTrieTracker != nil {
bjed.account.DataTrieTracker().ClearDataCaches()
}
return nil, nil
}
// Trie returns the referenced PatriciaMerkelTree for committing the changes
func (bjed *BaseJournalEntryData) Trie() data.Trie {
return bjed.trie
}
// IsInterfaceNil returns true if there is no value under the interface
func (bjed *BaseJournalEntryData) IsInterfaceNil() bool {
if bjed == nil {
return true
}
return false
}
//------- BaseJournalEntryNonce
// BaseJournalEntryNonce is used to revert a nonce change
type BaseJournalEntryNonce struct {
account AccountHandler
oldNonce uint64
}
// NewBaseJournalEntryNonce outputs a new JournalEntry implementation used to revert a nonce change
func NewBaseJournalEntryNonce(account AccountHandler, oldNonce uint64) (*BaseJournalEntryNonce, error) {
if account == nil || account.IsInterfaceNil() {
return nil, ErrNilAccountHandler
}
return &BaseJournalEntryNonce{
account: account,
oldNonce: oldNonce,
}, nil
}
// Revert applies undo operation
func (bjen *BaseJournalEntryNonce) Revert() (AccountHandler, error) {
bjen.account.SetNonce(bjen.oldNonce)
return bjen.account, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (bjen *BaseJournalEntryNonce) IsInterfaceNil() bool {
if bjen == nil {
return true
}
return false
}