This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
accounts.go
147 lines (133 loc) · 3.82 KB
/
accounts.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
package state
import (
"fmt"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/acm/acmstate"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/crypto"
)
// Returns nil if account does not exist with given address.
func (s *ReadState) GetAccount(address crypto.Address) (*acm.Account, error) {
tree, err := s.Forest.Reader(keys.Account.Prefix())
if err != nil {
return nil, err
}
accBytes := tree.Get(keys.Account.KeyNoPrefix(address))
if accBytes == nil {
return nil, nil
}
return acm.Decode(accBytes)
}
func (ws *writeState) statsAddAccount(acc *acm.Account) {
if acc != nil {
if len(acc.Code) > 0 {
ws.accountStats.AccountsWithCode++
} else {
ws.accountStats.AccountsWithoutCode++
}
}
}
func (ws *writeState) statsRemoveAccount(acc *acm.Account) {
if acc != nil {
if len(acc.Code) > 0 {
ws.accountStats.AccountsWithCode--
} else {
ws.accountStats.AccountsWithoutCode--
}
}
}
func (ws *writeState) UpdateAccount(account *acm.Account) error {
if account == nil {
return fmt.Errorf("UpdateAccount passed nil account in State")
}
encodedAccount, err := account.Encode()
if err != nil {
return fmt.Errorf("UpdateAccount could not encode account: %v", err)
}
tree, err := ws.forest.Writer(keys.Account.Prefix())
if err != nil {
return err
}
updated := tree.Set(keys.Account.KeyNoPrefix(account.Address), encodedAccount)
if updated {
ws.statsAddAccount(account)
}
return nil
}
func (ws *writeState) RemoveAccount(address crypto.Address) error {
tree, err := ws.forest.Writer(keys.Account.Prefix())
if err != nil {
return err
}
accBytes, deleted := tree.Delete(keys.Account.KeyNoPrefix(address))
if deleted {
acc, err := acm.Decode(accBytes)
if err != nil {
return err
}
ws.statsRemoveAccount(acc)
// Delete storage associated with account too
_, err = ws.forest.Delete(keys.Storage.Key(address))
if err != nil {
return err
}
}
return nil
}
func (s *ReadState) IterateAccounts(consumer func(*acm.Account) error) error {
tree, err := s.Forest.Reader(keys.Account.Prefix())
if err != nil {
return err
}
return tree.Iterate(nil, nil, true, func(key []byte, value []byte) error {
account, err := acm.Decode(value)
if err != nil {
return fmt.Errorf("IterateAccounts could not decode account: %v", err)
}
return consumer(account)
})
}
func (s *State) GetAccountStats() acmstate.AccountStats {
return s.writeState.accountStats
}
// Storage
func (s *ReadState) GetStorage(address crypto.Address, key binary.Word256) (binary.Word256, error) {
keyFormat := keys.Storage.Fix(address)
tree, err := s.Forest.Reader(keyFormat.Prefix())
if err != nil {
return binary.Zero256, err
}
return binary.LeftPadWord256(tree.Get(keyFormat.KeyNoPrefix(key))), nil
}
func (ws *writeState) SetStorage(address crypto.Address, key, value binary.Word256) error {
keyFormat := keys.Storage.Fix(address)
tree, err := ws.forest.Writer(keyFormat.Prefix())
if err != nil {
return err
}
if value == binary.Zero256 {
tree.Delete(keyFormat.KeyNoPrefix(key))
} else {
tree.Set(keyFormat.KeyNoPrefix(key), value.Bytes())
}
return nil
}
func (s *ReadState) IterateStorage(address crypto.Address, consumer func(key, value binary.Word256) error) error {
keyFormat := keys.Storage.Fix(address)
tree, err := s.Forest.Reader(keyFormat.Prefix())
if err != nil {
return err
}
return tree.Iterate(nil, nil, true,
func(key []byte, value []byte) error {
if len(key) != binary.Word256Length {
return fmt.Errorf("key '%X' stored for account %s is not a %v-byte word",
key, address, binary.Word256Length)
}
if len(value) != binary.Word256Length {
return fmt.Errorf("value '%X' stored for account %s is not a %v-byte word",
key, address, binary.Word256Length)
}
return consumer(binary.LeftPadWord256(key), binary.LeftPadWord256(value))
})
}