-
Notifications
You must be signed in to change notification settings - Fork 179
/
account_creator.go
292 lines (253 loc) · 6.51 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
package environment
import (
"fmt"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/storage/state"
"github.com/onflow/flow-go/fvm/tracing"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/trace"
)
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.NestedTransactionPreparer
impl AccountCreator
}
func NewParseRestrictedAccountCreator(
txnState state.NestedTransactionPreparer,
creator AccountCreator,
) AccountCreator {
return ParseRestrictedAccountCreator{
txnState: txnState,
impl: creator,
}
}
func (creator ParseRestrictedAccountCreator) CreateAccount(
runtimePayer common.Address,
) (
common.Address,
error,
) {
return parseRestrict1Arg1Ret(
creator.txnState,
trace.FVMEnvCreateAccount,
creator.impl.CreateAccount,
runtimePayer)
}
type AccountCreator interface {
CreateAccount(runtimePayer common.Address) (common.Address, error)
}
type NoAccountCreator struct {
}
func (NoAccountCreator) CreateAccount(
runtimePayer common.Address,
) (
common.Address,
error,
) {
return common.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.NestedTransactionPreparer
chain flow.Chain
accounts Accounts
isServiceAccountEnabled bool
tracer tracing.TracerSpan
meter Meter
metrics MetricsReporter
systemContracts *SystemContracts
}
func NewAddressGenerator(
txnState state.NestedTransactionPreparer,
chain flow.Chain,
) AddressGenerator {
return &accountCreator{
txnState: txnState,
chain: chain,
}
}
func NewBootstrapAccountCreator(
txnState state.NestedTransactionPreparer,
chain flow.Chain,
accounts Accounts,
) BootstrapAccountCreator {
return &accountCreator{
txnState: txnState,
chain: chain,
accounts: accounts,
}
}
func NewAccountCreator(
txnState state.NestedTransactionPreparer,
chain flow.Chain,
accounts Accounts,
isServiceAccountEnabled bool,
tracer tracing.TracerSpan,
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(flow.AddressStateRegisterID)
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(
flow.AddressStateRegisterID,
addressGenerator.Bytes())
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.EmptyAddress, err
}
err = creator.accounts.Create(publicKeys, flowAddress)
if err != nil {
return flow.EmptyAddress, 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(
runtimePayer common.Address,
) (
common.Address,
error,
) {
defer creator.tracer.StartChildSpan(trace.FVMEnvCreateAccount).End()
err := creator.meter.MeterComputation(ComputationKindCreateAccount, 1)
if err != nil {
return common.Address{}, err
}
// don't enforce limit during account creation
var address flow.Address
creator.txnState.RunWithAllLimitsDisabled(func() {
address, err = creator.createAccount(flow.ConvertAddress(runtimePayer))
})
return common.MustBytesToAddress(address.Bytes()), err
}
func (creator *accountCreator) createAccount(
payer flow.Address,
) (
flow.Address,
error,
) {
address, err := creator.createBasicAccount(nil)
if err != nil {
return flow.EmptyAddress, err
}
if creator.isServiceAccountEnabled {
_, invokeErr := creator.systemContracts.SetupNewAccount(
address,
payer)
if invokeErr != nil {
return flow.EmptyAddress, invokeErr
}
}
creator.metrics.RuntimeSetNumberOfAccounts(creator.AddressCount())
return address, nil
}