-
Notifications
You must be signed in to change notification settings - Fork 0
/
journalEntries.go
246 lines (202 loc) · 6.36 KB
/
journalEntries.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package state
import (
"bytes"
"fmt"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/marshal"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
type journalEntryCode struct {
oldCodeEntry *CodeEntry
oldCodeHash []byte
newCodeHash []byte
trie Updater
marshalizer marshal.Marshalizer
}
// NewJournalEntryCode creates a new instance of JournalEntryCode
func NewJournalEntryCode(
oldCodeEntry *CodeEntry,
oldCodeHash []byte,
newCodeHash []byte,
trie Updater,
marshalizer marshal.Marshalizer,
) (*journalEntryCode, error) {
if check.IfNil(trie) {
return nil, ErrNilUpdater
}
if check.IfNil(marshalizer) {
return nil, ErrNilMarshalizer
}
return &journalEntryCode{
oldCodeEntry: oldCodeEntry,
oldCodeHash: oldCodeHash,
newCodeHash: newCodeHash,
trie: trie,
marshalizer: marshalizer,
}, nil
}
// Revert applies undo operation
func (jea *journalEntryCode) Revert() (vmcommon.AccountHandler, error) {
if bytes.Equal(jea.oldCodeHash, jea.newCodeHash) {
return nil, nil
}
err := jea.revertOldCodeEntry()
if err != nil {
return nil, err
}
err = jea.revertNewCodeEntry()
if err != nil {
return nil, err
}
return nil, nil
}
func (jea *journalEntryCode) revertOldCodeEntry() error {
if len(jea.oldCodeHash) == 0 {
return nil
}
err := saveCodeEntry(jea.oldCodeHash, jea.oldCodeEntry, jea.trie, jea.marshalizer)
if err != nil {
return err
}
return nil
}
func (jea *journalEntryCode) revertNewCodeEntry() error {
newCodeEntry, err := getCodeEntry(jea.newCodeHash, jea.trie, jea.marshalizer)
if err != nil {
return err
}
if newCodeEntry == nil {
return nil
}
if newCodeEntry.NumReferences <= 1 {
err = jea.trie.Update(jea.newCodeHash, nil)
if err != nil {
return err
}
return nil
}
newCodeEntry.NumReferences--
err = saveCodeEntry(jea.newCodeHash, newCodeEntry, jea.trie, jea.marshalizer)
if err != nil {
return err
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (jea *journalEntryCode) IsInterfaceNil() bool {
return jea == nil
}
// JournalEntryAccount represents a journal entry for account fields change
type journalEntryAccount struct {
account vmcommon.AccountHandler
}
// NewJournalEntryAccount creates a new instance of JournalEntryAccount
func NewJournalEntryAccount(account vmcommon.AccountHandler) (*journalEntryAccount, error) {
if check.IfNil(account) {
return nil, fmt.Errorf("%w in NewJournalEntryAccount", ErrNilAccountHandler)
}
return &journalEntryAccount{
account: account,
}, nil
}
// Revert applies undo operation
func (jea *journalEntryAccount) Revert() (vmcommon.AccountHandler, error) {
return jea.account, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (jea *journalEntryAccount) IsInterfaceNil() bool {
return jea == nil
}
// JournalEntryAccountCreation represents a journal entry for account creation
type journalEntryAccountCreation struct {
address []byte
updater Updater
}
// NewJournalEntryAccountCreation creates a new instance of JournalEntryAccountCreation
func NewJournalEntryAccountCreation(address []byte, updater Updater) (*journalEntryAccountCreation, error) {
if check.IfNil(updater) {
return nil, ErrNilUpdater
}
if len(address) == 0 {
return nil, ErrInvalidAddressLength
}
return &journalEntryAccountCreation{
address: address,
updater: updater,
}, nil
}
// Revert applies undo operation
func (jea *journalEntryAccountCreation) Revert() (vmcommon.AccountHandler, error) {
return nil, jea.updater.Update(jea.address, nil)
}
// IsInterfaceNil returns true if there is no value under the interface
func (jea *journalEntryAccountCreation) IsInterfaceNil() bool {
return jea == nil
}
// JournalEntryDataTrieUpdates stores all the updates done to the account's data trie,
// so it can be reverted in case of rollback
type journalEntryDataTrieUpdates struct {
trieUpdates map[string][]byte
account baseAccountHandler
}
// NewJournalEntryDataTrieUpdates outputs a new JournalEntryDataTrieUpdates implementation used to revert an account's data trie
func NewJournalEntryDataTrieUpdates(trieUpdates map[string][]byte, account baseAccountHandler) (*journalEntryDataTrieUpdates, error) {
if check.IfNil(account) {
return nil, fmt.Errorf("%w in NewJournalEntryDataTrieUpdates", ErrNilAccountHandler)
}
if len(trieUpdates) == 0 {
return nil, ErrNilOrEmptyDataTrieUpdates
}
return &journalEntryDataTrieUpdates{
trieUpdates: trieUpdates,
account: account,
}, nil
}
// Revert applies undo operation
func (jedtu *journalEntryDataTrieUpdates) Revert() (vmcommon.AccountHandler, error) {
for key := range jedtu.trieUpdates {
err := jedtu.account.DataTrie().Update([]byte(key), jedtu.trieUpdates[key])
if err != nil {
return nil, err
}
log.Trace("revert data trie update", "key", []byte(key), "val", jedtu.trieUpdates[key])
}
rootHash, err := jedtu.account.DataTrie().RootHash()
if err != nil {
return nil, err
}
jedtu.account.SetRootHash(rootHash)
return jedtu.account, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (jedtu *journalEntryDataTrieUpdates) IsInterfaceNil() bool {
return jedtu == nil
}
// journalEntryDataTrieRemove cancels the eviction of the hashes from the data trie with the given root hash
type journalEntryDataTrieRemove struct {
rootHash []byte
obsoleteDataTrieHashes map[string][][]byte
}
// NewJournalEntryDataTrieRemove outputs a new journalEntryDataTrieRemove implementation used to cancel
// the eviction of the hashes from the data trie with the given root hash
func NewJournalEntryDataTrieRemove(rootHash []byte, obsoleteDataTrieHashes map[string][][]byte) (*journalEntryDataTrieRemove, error) {
if obsoleteDataTrieHashes == nil {
return nil, fmt.Errorf("%w in NewJournalEntryDataTrieRemove", ErrNilMapOfHashes)
}
if len(rootHash) == 0 {
return nil, ErrInvalidRootHash
}
return &journalEntryDataTrieRemove{
rootHash: rootHash,
obsoleteDataTrieHashes: obsoleteDataTrieHashes,
}, nil
}
// Revert applies undo operation
func (jedtr *journalEntryDataTrieRemove) Revert() (vmcommon.AccountHandler, error) {
delete(jedtr.obsoleteDataTrieHashes, string(jedtr.rootHash))
return nil, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (jedtr *journalEntryDataTrieRemove) IsInterfaceNil() bool {
return jedtr == nil
}