-
Notifications
You must be signed in to change notification settings - Fork 178
/
account_creator.go
305 lines (264 loc) · 6.57 KB
/
account_creator.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
package environment
import (
"fmt"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/trace"
)
const (
keyAddressState = "account_address_state"
FungibleTokenAccountIndex = 2
FlowTokenAccountIndex = 3
FlowFeesAccountIndex = 4
)
type AddressGenerator interface {
Bytes() []byte
NextAddress() (flow.Address, error)
CurrentAddress() flow.Address
AddressCount() uint64
}
type BootstrapAccountCreator interface {
CreateBootstrapAccount(
publicKeys []flow.AccountPublicKey,
) (
flow.Address,
error,
)
}
// This ensures cadence can't access unexpected operations while parsing
// programs.
type ParseRestrictedAccountCreator struct {
txnState *state.TransactionState
impl AccountCreator
}
func NewParseRestrictedAccountCreator(
txnState *state.TransactionState,
creator AccountCreator,
) AccountCreator {
return ParseRestrictedAccountCreator{
txnState: txnState,
impl: creator,
}
}
func (creator ParseRestrictedAccountCreator) CreateAccount(
payer runtime.Address,
) (
runtime.Address,
error,
) {
return parseRestrict1Arg1Ret(
creator.txnState,
"CreateAccount",
creator.impl.CreateAccount,
payer)
}
type AccountCreator interface {
CreateAccount(payer runtime.Address) (runtime.Address, error)
}
type NoAccountCreator struct {
}
func (NoAccountCreator) CreateAccount(
payer runtime.Address,
) (
runtime.Address,
error,
) {
return runtime.Address{}, errors.NewOperationNotSupportedError(
"CreateAccount")
}
// accountCreator make use of the storage state and the chain's address
// generator to create accounts.
//
// It also serves as a decorator for the chain's address generator which
// updates the state when next address is called (This secondary functionality
// is only used in utility command line).
type accountCreator struct {
txnState *state.TransactionState
chain flow.Chain
accounts Accounts
isServiceAccountEnabled bool
tracer *Tracer
meter Meter
metrics MetricsReporter
systemContracts *SystemContracts
}
func NewAddressGenerator(
txnState *state.TransactionState,
chain flow.Chain,
) AddressGenerator {
return &accountCreator{
txnState: txnState,
chain: chain,
}
}
func NewBootstrapAccountCreator(
txnState *state.TransactionState,
chain flow.Chain,
accounts Accounts,
) BootstrapAccountCreator {
return &accountCreator{
txnState: txnState,
chain: chain,
accounts: accounts,
}
}
func NewAccountCreator(
txnState *state.TransactionState,
chain flow.Chain,
accounts Accounts,
isServiceAccountEnabled bool,
tracer *Tracer,
meter Meter,
metrics MetricsReporter,
systemContracts *SystemContracts,
) AccountCreator {
return &accountCreator{
txnState: txnState,
chain: chain,
accounts: accounts,
isServiceAccountEnabled: isServiceAccountEnabled,
tracer: tracer,
meter: meter,
metrics: metrics,
systemContracts: systemContracts,
}
}
func (creator *accountCreator) bytes() ([]byte, error) {
stateBytes, err := creator.txnState.Get(
"",
keyAddressState,
creator.txnState.EnforceLimits())
if err != nil {
return nil, fmt.Errorf(
"failed to read address generator state from the state: %w",
err)
}
return stateBytes, nil
}
// TODO return error instead of a panic
// this requires changes outside of fvm since the type is defined on flow model
// and emulator and others might be dependent on that
func (creator *accountCreator) Bytes() []byte {
stateBytes, err := creator.bytes()
if err != nil {
panic(err)
}
return stateBytes
}
func (creator *accountCreator) constructAddressGen() (
flow.AddressGenerator,
error,
) {
stateBytes, err := creator.bytes()
if err != nil {
return nil, err
}
return creator.chain.BytesToAddressGenerator(stateBytes), nil
}
func (creator *accountCreator) NextAddress() (flow.Address, error) {
var address flow.Address
addressGenerator, err := creator.constructAddressGen()
if err != nil {
return address, err
}
address, err = addressGenerator.NextAddress()
if err != nil {
return address, err
}
// update the ledger state
err = creator.txnState.Set(
"",
keyAddressState,
addressGenerator.Bytes(),
creator.txnState.EnforceLimits())
if err != nil {
return address, fmt.Errorf(
"failed to update the state with address generator state: %w",
err)
}
return address, nil
}
func (creator *accountCreator) CurrentAddress() flow.Address {
var address flow.Address
addressGenerator, err := creator.constructAddressGen()
if err != nil {
// TODO update CurrentAddress to return an error if needed
panic(err)
}
address = addressGenerator.CurrentAddress()
return address
}
func (creator *accountCreator) AddressCount() uint64 {
addressGenerator, err := creator.constructAddressGen()
if err != nil {
// TODO update CurrentAddress to return an error if needed
panic(err)
}
return addressGenerator.AddressCount()
}
func (creator *accountCreator) createBasicAccount(
publicKeys []flow.AccountPublicKey,
) (
flow.Address,
error,
) {
flowAddress, err := creator.NextAddress()
if err != nil {
return flow.Address{}, err
}
err = creator.accounts.Create(publicKeys, flowAddress)
if err != nil {
return flow.Address{}, fmt.Errorf("create account failed: %w", err)
}
return flowAddress, nil
}
func (creator *accountCreator) CreateBootstrapAccount(
publicKeys []flow.AccountPublicKey,
) (
flow.Address,
error,
) {
return creator.createBasicAccount(publicKeys)
}
func (creator *accountCreator) CreateAccount(
payer runtime.Address,
) (
runtime.Address,
error,
) {
defer creator.tracer.StartSpanFromRoot(trace.FVMEnvCreateAccount).End()
err := creator.meter.MeterComputation(ComputationKindCreateAccount, 1)
if err != nil {
return common.Address{}, err
}
// don't enforce limit during account creation
var addr runtime.Address
creator.txnState.RunWithAllLimitsDisabled(func() {
addr, err = creator.createAccount(payer)
})
return addr, err
}
func (creator *accountCreator) createAccount(
payer runtime.Address,
) (
runtime.Address,
error,
) {
flowAddress, err := creator.createBasicAccount(nil)
if err != nil {
return common.Address{}, err
}
if creator.isServiceAccountEnabled {
_, invokeErr := creator.systemContracts.SetupNewAccount(
flowAddress,
payer)
if invokeErr != nil {
return common.Address{}, invokeErr
}
}
creator.metrics.RuntimeSetNumberOfAccounts(creator.AddressCount())
return runtime.Address(flowAddress), nil
}