-
Notifications
You must be signed in to change notification settings - Fork 199
/
journalEntries.go
88 lines (71 loc) · 2.23 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
package state
import (
"math/big"
)
//------- JournalEntryBalance
// JournalEntryBalance is used to revert a balance change
type JournalEntryBalance struct {
account *Account
oldBalance *big.Int
}
// NewJournalEntryBalance outputs a new JournalEntry implementation used to revert a balance change
func NewJournalEntryBalance(account *Account, oldBalance *big.Int) (*JournalEntryBalance, error) {
if account == nil {
return nil, ErrNilAccountHandler
}
return &JournalEntryBalance{
account: account,
oldBalance: oldBalance,
}, nil
}
// Revert applies undo operation
func (jeb *JournalEntryBalance) Revert() (AccountHandler, error) {
jeb.account.Balance = jeb.oldBalance
return jeb.account, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (jeb *JournalEntryBalance) IsInterfaceNil() bool {
if jeb == nil {
return true
}
return false
}
//------- JournalEntryDataTrieUpdates
// 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 AccountHandler
}
// NewJournalEntryDataTrieUpdates outputs a new JournalEntryDataTrieUpdates implementation used to revert an account's data trie
func NewJournalEntryDataTrieUpdates(trieUpdates map[string][]byte, account AccountHandler) (*JournalEntryDataTrieUpdates, error) {
if account == nil || account.IsInterfaceNil() {
return nil, ErrNilUpdater
}
if len(trieUpdates) == 0 {
return nil, ErrNilOrEmptyDataTrieUpdates
}
return &JournalEntryDataTrieUpdates{
trieUpdates: trieUpdates,
account: account,
}, nil
}
// Revert applies undo operation
func (jedtu *JournalEntryDataTrieUpdates) Revert() (AccountHandler, error) {
for key := range jedtu.trieUpdates {
err := jedtu.account.DataTrie().Update([]byte(key), jedtu.trieUpdates[key])
if err != nil {
return nil, err
}
}
rootHash, err := jedtu.account.DataTrie().Root()
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
}