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
/
state.go
301 lines (266 loc) · 7.77 KB
/
state.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
package evm
import (
"fmt"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/acm/acmstate"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/execution/errors"
"github.com/hyperledger/burrow/permission"
)
type Interface interface {
Reader
Writer
// Capture any errors when accessing or writing state - will return nil if no errors have occurred so far
errors.Provider
errors.Sink
// Create a new cached state over this one inheriting any cache options
NewCache(cacheOptions ...acmstate.CacheOption) Interface
// Sync this state cache to into its originator
Sync() errors.CodedError
}
type Reader interface {
GetStorage(address crypto.Address, key binary.Word256) binary.Word256
GetBalance(address crypto.Address) uint64
GetPermissions(address crypto.Address) permission.AccountPermissions
GetCode(address crypto.Address) acm.Bytecode
GetSequence(address crypto.Address) uint64
Exists(address crypto.Address) bool
// GetBlockHash returns hash of the specific block
GetBlockHash(blockNumber uint64) (binary.Word256, error)
}
type Writer interface {
CreateAccount(address crypto.Address)
InitCode(address crypto.Address, code []byte)
RemoveAccount(address crypto.Address)
SetStorage(address crypto.Address, key, value binary.Word256)
AddToBalance(address crypto.Address, amount uint64)
SubtractFromBalance(address crypto.Address, amount uint64)
SetPermission(address crypto.Address, permFlag permission.PermFlag, value bool)
UnsetPermission(address crypto.Address, permFlag permission.PermFlag)
AddRole(address crypto.Address, role string) bool
RemoveRole(address crypto.Address, role string) bool
}
type State struct {
// Where we sync
backend acmstate.ReaderWriter
// Block chain info
blockHashGetter func(height uint64) []byte
// Cache this State wraps
cache *acmstate.Cache
// Any error that may have occurred
error errors.CodedError
// In order for nested cache to inherit any options
cacheOptions []acmstate.CacheOption
}
func NewState(st acmstate.ReaderWriter, blockHashGetter func(height uint64) []byte, cacheOptions ...acmstate.CacheOption) *State {
return &State{
backend: st,
blockHashGetter: blockHashGetter,
cache: acmstate.NewCache(st, cacheOptions...),
cacheOptions: cacheOptions,
}
}
func (st *State) NewCache(cacheOptions ...acmstate.CacheOption) Interface {
return NewState(st.cache, st.blockHashGetter, append(st.cacheOptions, cacheOptions...)...)
}
func (st *State) Sync() errors.CodedError {
// Do not sync if we have erred
if st.error != nil {
return st.error
}
err := st.cache.Sync(st.backend)
if err != nil {
return errors.AsException(err)
}
return nil
}
func (st *State) Error() errors.CodedError {
if st.error == nil {
return nil
}
return st.error
}
// Errors pushed to state may end up in TxExecutions and therefore the merkle state so it is essential that errors are
// deterministic and independent of the code path taken to execution (e.g. replay takes a different path to that of
// normal consensus reactor so stack traces may differ - as they may across architectures)
func (st *State) PushError(err error) {
if st.error == nil {
// Make sure we are not wrapping a known nil value
ex := errors.AsException(err)
if ex != nil {
st.error = ex
}
}
}
// Reader
func (st *State) GetStorage(address crypto.Address, key binary.Word256) binary.Word256 {
value, err := st.cache.GetStorage(address, key)
if err != nil {
st.PushError(err)
return binary.Zero256
}
return value
}
func (st *State) GetBalance(address crypto.Address) uint64 {
acc := st.account(address)
if acc == nil {
return 0
}
return acc.Balance
}
func (st *State) GetPermissions(address crypto.Address) permission.AccountPermissions {
acc := st.account(address)
if acc == nil {
return permission.AccountPermissions{}
}
return acc.Permissions
}
func (st *State) GetCode(address crypto.Address) acm.Bytecode {
acc := st.account(address)
if acc == nil {
return nil
}
return acc.Code
}
func (st *State) Exists(address crypto.Address) bool {
acc, err := st.cache.GetAccount(address)
if err != nil {
st.PushError(err)
return false
}
if acc == nil {
return false
}
return true
}
func (st *State) GetSequence(address crypto.Address) uint64 {
acc := st.account(address)
if acc == nil {
return 0
}
return acc.Sequence
}
// Writer
func (st *State) CreateAccount(address crypto.Address) {
if st.Exists(address) {
st.PushError(errors.ErrorCodef(errors.ErrorCodeDuplicateAddress,
"tried to create an account at an address that already exists: %v", address))
return
}
st.updateAccount(&acm.Account{Address: address})
}
func (st *State) InitCode(address crypto.Address, code []byte) {
acc := st.mustAccount(address)
if acc == nil {
st.PushError(errors.ErrorCodef(errors.ErrorCodeInvalidAddress,
"tried to initialise code for an account that does not exist: %v", address))
return
}
if acc.Code != nil {
st.PushError(errors.ErrorCodef(errors.ErrorCodeIllegalWrite,
"tried to initialise code for a contract that already exists: %v", address))
return
}
acc.Code = code
st.updateAccount(acc)
}
func (st *State) RemoveAccount(address crypto.Address) {
if !st.Exists(address) {
st.PushError(errors.ErrorCodef(errors.ErrorCodeDuplicateAddress,
"tried to remove an account at an address that does not exist: %v", address))
return
}
st.removeAccount(address)
}
func (st *State) SetStorage(address crypto.Address, key, value binary.Word256) {
err := st.cache.SetStorage(address, key, value)
if err != nil {
st.PushError(err)
}
}
func (st *State) AddToBalance(address crypto.Address, amount uint64) {
acc := st.mustAccount(address)
if acc == nil {
return
}
st.PushError(acc.AddToBalance(amount))
st.updateAccount(acc)
}
func (st *State) SubtractFromBalance(address crypto.Address, amount uint64) {
acc := st.mustAccount(address)
if acc == nil {
return
}
st.PushError(acc.SubtractFromBalance(amount))
st.updateAccount(acc)
}
func (st *State) SetPermission(address crypto.Address, permFlag permission.PermFlag, value bool) {
acc := st.mustAccount(address)
if acc == nil {
return
}
st.PushError(acc.Permissions.Base.Set(permFlag, value))
st.updateAccount(acc)
}
func (st *State) UnsetPermission(address crypto.Address, permFlag permission.PermFlag) {
acc := st.mustAccount(address)
if acc == nil {
return
}
st.PushError(acc.Permissions.Base.Unset(permFlag))
st.updateAccount(acc)
}
func (st *State) AddRole(address crypto.Address, role string) bool {
acc := st.mustAccount(address)
if acc == nil {
return false
}
added := acc.Permissions.AddRole(role)
st.updateAccount(acc)
return added
}
func (st *State) RemoveRole(address crypto.Address, role string) bool {
acc := st.mustAccount(address)
if acc == nil {
return false
}
removed := acc.Permissions.RemoveRole(role)
st.updateAccount(acc)
return removed
}
func (st *State) GetBlockHash(height uint64) (binary.Word256, error) {
hash := st.blockHashGetter(height)
if len(hash) == 0 {
st.PushError(fmt.Errorf("got empty BlockHash from blockHashGetter"))
}
return binary.LeftPadWord256(hash), nil
}
// Helpers
func (st *State) account(address crypto.Address) *acm.Account {
acc, err := st.cache.GetAccount(address)
if err != nil {
st.PushError(err)
}
return acc
}
func (st *State) mustAccount(address crypto.Address) *acm.Account {
acc := st.account(address)
if acc == nil {
st.PushError(errors.ErrorCodef(errors.ErrorCodeIllegalWrite,
"attempted to modify non-existent account: %v", address))
}
return acc
}
func (st *State) updateAccount(account *acm.Account) {
err := st.cache.UpdateAccount(account)
if err != nil {
st.PushError(err)
}
}
func (st *State) removeAccount(address crypto.Address) {
err := st.cache.RemoveAccount(address)
if err != nil {
st.PushError(err)
}
}