-
Notifications
You must be signed in to change notification settings - Fork 202
/
userAccount.go
146 lines (119 loc) · 3.94 KB
/
userAccount.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
//go:generate protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/ElrondNetwork/protobuf/protobuf --gogoslick_out=. userAccountData.proto
package state
import (
"bytes"
"math/big"
)
var _ UserAccountHandler = (*userAccount)(nil)
// Account is the struct used in serialization/deserialization
type userAccount struct {
*baseAccount
UserAccountData
}
var zero = big.NewInt(0)
// NewEmptyUserAccount creates new simple account wrapper for an AccountContainer (that has just been initialized)
func NewEmptyUserAccount() *userAccount {
return &userAccount{
baseAccount: &baseAccount{},
UserAccountData: UserAccountData{
DeveloperReward: big.NewInt(0),
Balance: big.NewInt(0),
},
}
}
// NewUserAccount creates new simple account wrapper for an AccountContainer (that has just been initialized)
func NewUserAccount(address []byte) (*userAccount, error) {
if len(address) == 0 {
return nil, ErrNilAddress
}
return &userAccount{
baseAccount: &baseAccount{
address: address,
dataTrieTracker: NewTrackableDataTrie(address, nil),
},
UserAccountData: UserAccountData{
DeveloperReward: big.NewInt(0),
Balance: big.NewInt(0),
Address: address,
},
}, nil
}
// SetUserName sets the users name
func (a *userAccount) SetUserName(userName []byte) {
a.UserName = make([]byte, 0, len(userName))
a.UserName = append(a.UserName, userName...)
}
// AddToBalance adds new value to balance
func (a *userAccount) AddToBalance(value *big.Int) error {
newBalance := big.NewInt(0).Add(a.Balance, value)
if newBalance.Cmp(zero) < 0 {
return ErrInsufficientFunds
}
a.Balance = newBalance
return nil
}
// SubFromBalance subtracts new value from balance
func (a *userAccount) SubFromBalance(value *big.Int) error {
newBalance := big.NewInt(0).Sub(a.Balance, value)
if newBalance.Cmp(zero) < 0 {
return ErrInsufficientFunds
}
a.Balance = newBalance
return nil
}
// GetBalance returns the actual balance from the account
func (a *userAccount) GetBalance() *big.Int {
return big.NewInt(0).Set(a.Balance)
}
// ClaimDeveloperRewards returns the accumulated developer rewards and sets it to 0 in the account
func (a *userAccount) ClaimDeveloperRewards(sndAddress []byte) (*big.Int, error) {
if !bytes.Equal(sndAddress, a.OwnerAddress) {
return nil, ErrOperationNotPermitted
}
oldValue := big.NewInt(0).Set(a.DeveloperReward)
a.DeveloperReward = big.NewInt(0)
return oldValue, nil
}
// AddToDeveloperReward adds new value to developer reward
func (a *userAccount) AddToDeveloperReward(value *big.Int) {
a.DeveloperReward = big.NewInt(0).Add(a.DeveloperReward, value)
}
// GetDeveloperReward returns the actual developer reward from the account
func (a *userAccount) GetDeveloperReward() *big.Int {
return big.NewInt(0).Set(a.DeveloperReward)
}
// ChangeOwnerAddress changes the owner account if operation is permitted
func (a *userAccount) ChangeOwnerAddress(sndAddress []byte, newAddress []byte) error {
if !bytes.Equal(sndAddress, a.OwnerAddress) {
return ErrOperationNotPermitted
}
if len(newAddress) != len(a.address) {
return ErrInvalidAddressLength
}
a.OwnerAddress = newAddress
return nil
}
// SetOwnerAddress sets the owner address of an account,
func (a *userAccount) SetOwnerAddress(address []byte) {
a.OwnerAddress = address
}
// IncreaseNonce adds the given value to the current nonce
func (a *userAccount) IncreaseNonce(value uint64) {
a.Nonce = a.Nonce + value
}
// SetCodeHash sets the code hash associated with the account
func (a *userAccount) SetCodeHash(codeHash []byte) {
a.CodeHash = codeHash
}
// SetRootHash sets the root hash associated with the account
func (a *userAccount) SetRootHash(roothash []byte) {
a.RootHash = roothash
}
// SetCodeMetadata sets the code metadata
func (a *userAccount) SetCodeMetadata(codeMetadata []byte) {
a.CodeMetadata = codeMetadata
}
// IsInterfaceNil returns true if there is no value under the interface
func (a *userAccount) IsInterfaceNil() bool {
return a == nil
}