forked from Fantom-foundation/Substate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
71 lines (58 loc) · 1.4 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
package substate
import (
"bytes"
"math/big"
"github.com/Fantom-foundation/Substate/common"
)
// Account holds any information about account used in a transaction.
type Account struct {
Nonce uint64
Balance *big.Int
Storage map[common.Hash]common.Hash
Code []byte
}
func NewAccount(nonce uint64, balance *big.Int, code []byte) *Account {
return &Account{
Nonce: nonce,
Balance: balance,
Storage: make(map[common.Hash]common.Hash),
Code: code,
}
}
// Equal returns true if a is y or if values of a are equal to values of y.
// Otherwise, a and y are not equal hence false is returned.
func (a *Account) Equal(y *Account) bool {
if a == y {
return true
}
if (a == nil || y == nil) && a != y {
return false
}
// check values
equal := a.Nonce == y.Nonce &&
a.Balance.Cmp(y.Balance) == 0 &&
bytes.Equal(a.Code, y.Code) &&
len(a.Storage) == len(y.Storage)
if !equal {
return false
}
for aKey, aVal := range a.Storage {
yValue, exist := y.Storage[aKey]
if !(exist && aVal == yValue) {
return false
}
}
return true
}
// Copy returns a hard copy of a
func (a *Account) Copy() *Account {
cpy := NewAccount(a.Nonce, a.Balance, a.Code)
for key, value := range a.Storage {
cpy.Storage[key] = value
}
return cpy
}
// CodeHash returns hashed code
//func (a *Account) CodeHash() common.Hash { todo uncomment when eth is copied
// return crypto.Keccak256Hash(a.Code)
//}