-
Notifications
You must be signed in to change notification settings - Fork 4
/
account.go
358 lines (317 loc) · 9.53 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package account
import (
"bytes"
"errors"
"fmt"
"github.com/LemoFoundationLtd/lemochain-go/chain/types"
"github.com/LemoFoundationLtd/lemochain-go/common"
"github.com/LemoFoundationLtd/lemochain-go/common/crypto"
"github.com/LemoFoundationLtd/lemochain-go/common/log"
"github.com/LemoFoundationLtd/lemochain-go/store"
"github.com/LemoFoundationLtd/lemochain-go/store/protocol"
"github.com/LemoFoundationLtd/lemochain-go/store/trie"
"math/big"
)
var (
sha3Nil = crypto.Keccak256Hash(nil)
ErrNegativeBalance = errors.New("balance can't be negative")
ErrLoadCodeFail = errors.New("can't load contract code")
ErrTrieFail = errors.New("can't load contract storage trie")
ErrTrieChanged = errors.New("the trie has changed after Finalise")
)
type Storage map[common.Hash][]byte
func (s Storage) String() (str string) {
for key, value := range s {
str += fmt.Sprintf("%X : %X\n", key, value)
}
return
}
func (s Storage) Copy() Storage {
cpy := make(Storage)
for key, value := range s {
cpy[key] = value
}
return cpy
}
// Account is used to read and write account data. the code and dirty storage K/V would be cached till they are flushing to db
type Account struct {
data *types.AccountData
db protocol.ChainDB // used to access account data in cache or file
trie *trie.SecureTrie // contract storage trie
trieDb *store.TrieDatabase // used to access tire data in file
// trie Trie // storage trie
code types.Code // contract byte code
cachedStorage Storage // Storage entry cache to avoid duplicate reads
dirtyStorage Storage // Storage entries that need to be flushed to disk
codeIsDirty bool // true if the code was updated
suicided bool // will be delete from the trie during the "save" phase
}
// NewAccount wrap an AccountData object, or creates a new one if it's nil.
func NewAccount(db protocol.ChainDB, address common.Address, data *types.AccountData) *Account {
if data == nil {
// create new one
data = &types.AccountData{Address: address}
} else {
data = data.Copy()
}
if data.Balance == nil {
data.Balance = new(big.Int)
}
if data.NewestRecords == nil {
data.NewestRecords = make(map[types.ChangeLogType]types.VersionRecord)
}
if data.Candidate.Profile == nil {
data.Candidate.Profile = make(types.CandidateProfile)
}
if data.Candidate.Votes == nil {
data.Candidate.Votes = new(big.Int)
}
return &Account{
data: data,
db: db,
cachedStorage: make(Storage),
dirtyStorage: make(Storage),
}
}
// MarshalJSON encodes the lemoClient RPC account format.
func (a *Account) MarshalJSON() ([]byte, error) {
return a.data.MarshalJSON()
}
// UnmarshalJSON decodes the lemoClient RPC account format.
func (a *Account) UnmarshalJSON(input []byte) error {
dec := new(types.AccountData)
if err := dec.UnmarshalJSON(input); err != nil {
return err
}
// TODO a.db is always nil
*a = *NewAccount(a.db, dec.Address, dec)
return nil
}
func (a *Account) String() string {
return a.data.String()
}
func (a *Account) GetTxCount() uint32 { return a.data.TxCount }
func (a *Account) SetTxCount(count uint32) {
a.data.TxCount = count
}
func (a *Account) GetVoteFor() common.Address { return a.data.VoteFor }
func (a *Account) SetVoteFor(addr common.Address) {
a.data.VoteFor = addr
}
func (a *Account) GetVotes() *big.Int {
return a.data.Candidate.Votes
}
func (a *Account) SetVotes(votes *big.Int) {
a.data.Candidate.Votes = new(big.Int).Set(votes)
}
func (a *Account) GetCandidateProfile() types.CandidateProfile {
if a.data.Candidate.Profile == nil {
return make(types.CandidateProfile)
} else {
result := make(types.CandidateProfile)
for k, v := range a.data.Candidate.Profile {
result[k] = v
}
return result
}
}
func (a *Account) SetCandidateProfile(profile types.CandidateProfile) {
if len(profile) <= 0 {
a.data.Candidate.Profile = make(map[string]string)
} else {
a.data.Candidate.Profile = make(map[string]string)
for k, v := range profile {
a.data.Candidate.Profile[k] = v
}
}
}
// Implement AccountAccessor. Access Account without changelog
func (a *Account) GetAddress() common.Address { return a.data.Address }
func (a *Account) GetBalance() *big.Int { return new(big.Int).Set(a.data.Balance) }
// GetBaseVersion returns the version of specific change log from the base block. It is not changed by tx processing until the finalised
func (a *Account) GetBaseVersion(logType types.ChangeLogType) uint32 {
return a.data.NewestRecords[logType].Version
}
func (a *Account) SetVersion(logType types.ChangeLogType, version, blockHeight uint32) {
a.data.NewestRecords[logType] = types.VersionRecord{Version: version, Height: blockHeight}
}
func (a *Account) GetSuicide() bool { return a.suicided }
func (a *Account) GetCodeHash() common.Hash { return a.data.CodeHash }
// func (a *Account) GetTxHashList() []common.Hash { return a.data.TxHashList }
// StorageRoot wouldn't change until Account.updateTrie() is called
func (a *Account) GetStorageRoot() common.Hash { return a.data.StorageRoot }
func (a *Account) SetBalance(balance *big.Int) {
if balance.Sign() < 0 {
log.Errorf("can't set negative balance %v to account %06x", balance, a.data.Address)
panic(ErrNegativeBalance)
}
a.data.Balance.Set(balance)
}
func (a *Account) SetSuicide(suicided bool) {
if suicided {
a.SetBalance(new(big.Int))
a.SetCodeHash(common.Hash{})
a.SetStorageRoot(common.Hash{})
}
a.suicided = suicided
}
func (a *Account) SetCodeHash(codeHash common.Hash) {
a.data.CodeHash = codeHash
a.code = nil
}
func (a *Account) SetStorageRoot(root common.Hash) {
a.data.StorageRoot = root
a.dirtyStorage = make(Storage)
a.trie = nil
}
// Code returns the contract code associated with this account, if any.
func (a *Account) GetCode() (types.Code, error) {
if a.code != nil {
return a.code, nil
}
codeHash := a.data.CodeHash
if codeHash == sha3Nil || codeHash == (common.Hash{}) {
return nil, nil
}
code, err := a.db.GetContractCode(codeHash)
if err != nil {
log.Errorf("can't load code hash %x: %v", a.data.CodeHash, err)
return nil, err
} else if code == nil {
log.Errorf("can't load code hash %x", a.data.CodeHash)
return nil, ErrLoadCodeFail
} else {
a.code = code
}
return a.code, nil
}
func (a *Account) SetCode(code types.Code) {
a.code = code
oldHash := a.data.CodeHash
newHash := crypto.Keccak256Hash(code)
// hash changed and not both hash are empty
if oldHash != newHash && !(oldHash == common.Hash{} && newHash == sha3Nil) {
a.data.CodeHash = newHash
a.codeIsDirty = true
}
}
// GetState returns a value in account storage.
func (a *Account) GetStorageState(key common.Hash) ([]byte, error) {
value, exists := a.cachedStorage[key]
if exists {
return value, nil
}
// Load from DB in case it is missing.
tr, err := a.getTrie()
if err != nil {
log.Errorf("load trie by root 0x%x fail: %v", a.data.StorageRoot, err)
return nil, ErrTrieFail
}
value, err = tr.TryGet(key[:])
// ignore ErrNotExist, just return empty []byte
if err != nil && err != store.ErrNotExist {
return nil, err
}
if len(value) != 0 {
a.cachedStorage[key] = value
}
return value, nil
}
// SetState updates a value in account storage.
func (a *Account) SetStorageState(key common.Hash, value []byte) error {
// TODO the key is Hash already, but secureTrie hash it again?
a.cachedStorage[key] = value
a.dirtyStorage[key] = value
return nil
}
// IsEmpty returns whether the state object is either non-existent or empty (version = 0)
func (a *Account) IsEmpty() bool {
for _, record := range a.data.NewestRecords {
if record.Version != 0 {
return false
}
}
return true
}
func (a *Account) getTrie() (*trie.SecureTrie, error) {
if a.trie == nil {
if a.trieDb == nil {
a.trieDb = a.db.GetTrieDatabase()
}
var err error
a.trie, err = trie.NewSecure(a.data.StorageRoot, a.trieDb, MaxTrieCacheGen)
if err != nil {
return nil, err
// a.trie, _ = trie.NewSecure(common.Hash{}, trieDb, MaxTrieCacheGen)
}
}
return a.trie, nil
}
// updateTrie writes cached storage modifications into storage trie.
func (a *Account) updateTrie() error {
if a.data.StorageRoot == (common.Hash{}) && len(a.dirtyStorage) == 0 {
return nil
}
tr, err := a.getTrie()
if err != nil {
log.Errorf("load trie by root 0x%x fail: %v", a.data.StorageRoot, err)
return ErrTrieFail
}
for key, value := range a.dirtyStorage {
delete(a.dirtyStorage, key)
if len(value) == 0 {
err = tr.TryDelete(key[:])
if err != nil {
return err
}
continue
}
v := bytes.TrimLeft(value, "\x00")
err = tr.TryUpdate(key[:], v)
if err != nil {
return err
}
}
a.data.StorageRoot = tr.Hash()
return nil
}
// Finalise finalises the state, clears the change caches and update tries.
func (a *Account) Finalise() error {
// update storage trie
return a.updateTrie()
}
// Save writes dirty data into db.
func (a *Account) Save() error {
if len(a.dirtyStorage) > 0 {
return ErrTrieChanged
}
if a.data.StorageRoot != (common.Hash{}) {
tr, err := a.getTrie()
if err != nil {
log.Errorf("load trie by root 0x%x fail: %v", a.data.StorageRoot, err)
return ErrTrieFail
}
// update contract storage trie nodes' hash
root, err := tr.Commit(nil)
if err != nil {
return err
}
if root != a.data.StorageRoot {
return ErrTrieChanged
}
// save contract storage trie
err = a.trieDb.Commit(root, false)
if err != nil {
log.Error("save contract storage fail", "address", a.data.Address)
return err
}
}
// save code
if a.codeIsDirty {
if err := a.db.SetContractCode(a.data.CodeHash, a.code); err != nil {
return err
}
a.codeIsDirty = false
}
return nil
}