-
Notifications
You must be signed in to change notification settings - Fork 198
/
account.go
154 lines (122 loc) · 3.84 KB
/
account.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
package state
import (
"math/big"
"github.com/ElrondNetwork/elrond-go/data"
)
// Account is the struct used in serialization/deserialization
type Account struct {
Nonce uint64
Balance *big.Int
CodeHash []byte
RootHash []byte
Address []byte
addressContainer AddressContainer
code []byte
accountTracker AccountTracker
dataTrieTracker DataTrieTracker
}
// NewAccount creates new simple account wrapper for an AccountContainer (that has just been initialized)
func NewAccount(addressContainer AddressContainer, tracker AccountTracker) (*Account, error) {
if addressContainer == nil || addressContainer.IsInterfaceNil() {
return nil, ErrNilAddressContainer
}
if tracker == nil || tracker.IsInterfaceNil() {
return nil, ErrNilAccountTracker
}
addressBytes := addressContainer.Bytes()
return &Account{
Balance: big.NewInt(0),
addressContainer: addressContainer,
Address: addressBytes,
accountTracker: tracker,
dataTrieTracker: NewTrackableDataTrie(addressBytes, nil),
}, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (a *Account) IsInterfaceNil() bool {
if a == nil {
return true
}
return false
}
// AddressContainer returns the address associated with the account
func (a *Account) AddressContainer() AddressContainer {
return a.addressContainer
}
// SetNonceWithJournal sets the account's nonce, saving the old nonce before changing
func (a *Account) SetNonceWithJournal(nonce uint64) error {
entry, err := NewBaseJournalEntryNonce(a, a.Nonce)
if err != nil {
return err
}
a.accountTracker.Journalize(entry)
a.Nonce = nonce
return a.accountTracker.SaveAccount(a)
}
//SetNonce saves the nonce to the account
func (a *Account) SetNonce(nonce uint64) {
a.Nonce = nonce
}
// GetNonce gets the nonce of the account
func (a *Account) GetNonce() uint64 {
return a.Nonce
}
// SetBalanceWithJournal sets the account's balance, saving the old balance before changing
func (a *Account) SetBalanceWithJournal(balance *big.Int) error {
entry, err := NewJournalEntryBalance(a, a.Balance)
if err != nil {
return err
}
a.accountTracker.Journalize(entry)
a.Balance = balance
return a.accountTracker.SaveAccount(a)
}
//------- code / code hash
// GetCodeHash returns the code hash associated with this account
func (a *Account) GetCodeHash() []byte {
return a.CodeHash
}
// SetCodeHash sets the code hash associated with the account
func (a *Account) SetCodeHash(codeHash []byte) {
a.CodeHash = codeHash
}
// SetCodeHashWithJournal sets the account's code hash, saving the old code hash before changing
func (a *Account) SetCodeHashWithJournal(codeHash []byte) error {
entry, err := NewBaseJournalEntryCodeHash(a, a.CodeHash)
if err != nil {
return err
}
a.accountTracker.Journalize(entry)
a.CodeHash = codeHash
return a.accountTracker.SaveAccount(a)
}
// GetCode gets the actual code that needs to be run in the VM
func (a *Account) GetCode() []byte {
return a.code
}
// SetCode sets the actual code that needs to be run in the VM
func (a *Account) SetCode(code []byte) {
a.code = code
}
//------- data trie / root hash
// GetRootHash returns the root hash associated with this account
func (a *Account) GetRootHash() []byte {
return a.RootHash
}
// SetRootHash sets the root hash associated with the account
func (a *Account) SetRootHash(roothash []byte) {
a.RootHash = roothash
}
// DataTrie returns the trie that holds the current account's data
func (a *Account) DataTrie() data.Trie {
return a.dataTrieTracker.DataTrie()
}
// SetDataTrie sets the trie that holds the current account's data
func (a *Account) SetDataTrie(trie data.Trie) {
a.dataTrieTracker.SetDataTrie(trie)
}
// DataTrieTracker returns the trie wrapper used in managing the SC data
func (a *Account) DataTrieTracker() DataTrieTracker {
return a.dataTrieTracker
}
//TODO add Cap'N'Proto converter funcs