forked from onflow/flowkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
346 lines (295 loc) · 10 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Flow CLI
*
* Copyright 2019 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package flowkit
import (
"fmt"
"os"
"path/filepath"
"github.com/onflow/cadence/runtime"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/crypto"
"github.com/pkg/errors"
"golang.org/x/exp/slices"
"github.com/onflow/flowkit/v2/accounts"
"github.com/onflow/flowkit/v2/config"
"github.com/onflow/flowkit/v2/config/json"
"github.com/onflow/flowkit/v2/project"
)
// ReaderWriter defines read file and write file methods.
type ReaderWriter interface {
ReadFile(source string) ([]byte, error)
WriteFile(filename string, data []byte, perm os.FileMode) error
MkdirAll(path string, perm os.FileMode) error
Stat(path string) (os.FileInfo, error)
}
// State manages the state for a Flow project.
type State struct {
conf *config.Config
confLoader *config.Loader
readerWriter ReaderWriter
accounts *accounts.Accounts
}
func (p *State) CreateCoverageReport(network string) *runtime.CoverageReport {
coverageReport := runtime.NewCoverageReport()
contractsConfig := *p.Contracts()
locationMappings := make(map[string]string, len(contractsConfig))
for _, contract := range contractsConfig {
alias := contract.Aliases.ByNetwork(network)
if alias != nil {
locationMappings[contract.Name] = contract.Location
}
}
coverageReport.WithLocationMappings(locationMappings)
return coverageReport
}
// ReaderWriter retrieve current file reader writer.
func (p *State) ReaderWriter() ReaderWriter {
return p.readerWriter
}
// ReadFile exposes an injected file loader.
func (p *State) ReadFile(source string) ([]byte, error) {
return p.readerWriter.ReadFile(source)
}
// SaveDefault saves to default path.
func (p *State) SaveDefault() error {
return p.Save(config.DefaultPath)
}
// SaveEdited saves configuration to valid path.
func (p *State) SaveEdited(paths []string) error {
// if paths are not default only allow specifying one config
if !config.IsDefaultPath(paths) && len(paths) > 1 {
return fmt.Errorf("specifying multiple paths is not supported when updating configuration")
}
// if default paths and local config doesn't exist don't allow updating global config
if config.IsDefaultPath(paths) {
_, err := p.confLoader.Load([]string{config.DefaultPath}) // check if default is present
if err != nil {
return fmt.Errorf("default configuration not found, please initialize it first or specify another configuration file")
} else {
return p.SaveDefault()
}
}
return p.Save(paths[0])
}
// Save saves the project configuration to the given path.
func (p *State) Save(path string) error {
p.conf.Accounts = accounts.ToConfig(*p.accounts)
err := p.confLoader.Save(p.conf, path)
if err != nil {
return fmt.Errorf("failed to save project configuration to: %s", path)
}
return nil
}
// Networks get network configuration.
func (p *State) Networks() *config.Networks {
return &p.conf.Networks
}
// Deployments get deployments configuration.
func (p *State) Deployments() *config.Deployments {
return &p.conf.Deployments
}
// Contracts get contracts configuration.
func (p *State) Contracts() *config.Contracts {
return &p.conf.Contracts
}
func (p *State) Dependencies() *config.Dependencies {
return &p.conf.Dependencies
}
// Accounts get accounts.
func (p *State) Accounts() *accounts.Accounts {
return p.accounts
}
// Config get underlying configuration for advanced usage.
func (p *State) Config() *config.Config {
return p.conf
}
// EmulatorServiceAccount returns the service account for the default emulator profile.
func (p *State) EmulatorServiceAccount() (*accounts.Account, error) {
emulator := p.conf.Emulators.Default()
if emulator == nil {
return nil, fmt.Errorf("no default emulator account")
}
return p.accounts.ByName(emulator.ServiceAccount)
}
// SetEmulatorKey sets the default emulator service account private key.
func (p *State) SetEmulatorKey(privateKey crypto.PrivateKey) {
acc, _ := p.EmulatorServiceAccount()
acc.Key = accounts.NewHexKeyFromPrivateKey(acc.Key.Index(), acc.Key.HashAlgo(), privateKey)
}
// DeploymentContractsByNetwork returns all contracts for a network.
//
// Build contract slice based on the network provided, check the deployment section for that network
// and retrieve the account by name, then add the accounts address on the contract as a destination.
func (p *State) DeploymentContractsByNetwork(network config.Network) ([]*project.Contract, error) {
contracts := make([]*project.Contract, 0)
// get deployments for the specified network
for _, deploy := range p.conf.Deployments.ByNetwork(network.Name) {
account, err := p.accounts.ByName(deploy.Account)
if err != nil {
return nil, err
}
// go through each contract in this deployment
for _, deploymentContract := range deploy.Contracts {
c, err := p.conf.Contracts.ByName(deploymentContract.Name)
if err != nil {
return nil, err
}
location := c.Location
// if we loaded config from a single location, we should make the path of contracts defined in config relative to
// config path we have provided, this will make cases where we execute loading in different path than config work
if len(p.confLoader.LoadedLocations) == 1 {
location = filepath.Join(
filepath.Dir(p.confLoader.LoadedLocations[0]),
location,
)
}
code, err := p.readerWriter.ReadFile(location)
if err != nil {
return nil, errors.Wrap(err, "deployment by network failed to read contract code")
}
contract := project.NewContract(
c.Name,
filepath.Clean(location),
code,
account.Address,
account.Name,
deploymentContract.Args,
)
contracts = append(contracts, contract)
}
}
return contracts, nil
}
// AccountsForNetwork returns all accounts used on a network defined by deployments.
func (p *State) AccountsForNetwork(network config.Network) *accounts.Accounts {
exists := make(map[string]bool, 0)
accs := make(accounts.Accounts, 0)
for _, account := range *p.accounts {
if p.conf.Deployments.ByAccountAndNetwork(account.Name, network.Name) != nil {
slices.ContainsFunc(accs, func(a accounts.Account) bool {
return a.Name == account.Name
})
if !exists[account.Name] {
accs = append(accs, account)
}
}
}
return &accs
}
// AccountByContractName returns the account for a contract by contract name.
func (p *State) AccountByContractName(contractName string, network config.Network) (*accounts.Account, error) {
deployments := p.conf.Deployments.ByNetwork(network.Name)
var accountName string
for _, d := range deployments {
for _, c := range d.Contracts {
if c.Name == contractName {
accountName = d.Account
break
}
}
}
if accountName == "" {
return nil, fmt.Errorf("deployment of %s not found for network %s", contractName, network.Name)
}
accs := p.Accounts()
if accs == nil {
return nil, fmt.Errorf("no accounts found in state")
}
var account *accounts.Account
for _, a := range *p.accounts {
if accountName == a.Name {
account = &a
break
}
}
if account == nil {
return nil, fmt.Errorf("account %s not found in state", accountName)
}
return account, nil
}
// ContractAddress returns the flow address for a contract given th network
func (p *State) ContractAddress(contract *config.Contract, network config.Network) (*flow.Address, error) {
acc, err := p.AccountByContractName(contract.Name, network)
if err != nil {
return nil, err
}
return &acc.Address, nil
}
// AliasesForNetwork returns all deployment aliases for a network.
func (p *State) AliasesForNetwork(network config.Network) project.LocationAliases {
aliases := make(project.LocationAliases)
// get all contracts for selected network and if any has an address as target make it an alias
for _, contract := range p.conf.Contracts {
if contract.IsAliased() && contract.Aliases.ByNetwork(network.Name) != nil {
alias := contract.Aliases.ByNetwork(network.Name).Address.String()
aliases[filepath.Clean(contract.Location)] = alias // alias for import by file location
aliases[contract.Name] = alias // alias for import by name
}
}
return aliases
}
// Load loads a project configuration and returns the resulting project.
func Load(configFilePaths []string, readerWriter ReaderWriter) (*State, error) {
confLoader := config.NewLoader(readerWriter)
// here we add all available parsers (more to add yaml etc...)
confLoader.AddConfigParser(json.NewParser())
conf, err := confLoader.Load(configFilePaths)
if err != nil {
return nil, err
}
// only add a default emulator in the config if the emulator account is present in accounts
_, err = conf.Accounts.ByName(config.DefaultEmulator.ServiceAccount)
if err == nil && len(conf.Emulators) == 0 {
conf.Emulators.AddOrUpdate("", config.DefaultEmulator)
}
proj, err := newProject(conf, confLoader, readerWriter)
if err != nil {
return nil, fmt.Errorf("invalid project configuration: %s", err)
}
return proj, nil
}
// Init initializes a new Flow project.
func Init(
rw ReaderWriter,
) (*State, error) {
loader := config.NewLoader(rw)
loader.AddConfigParser(json.NewParser())
return &State{
confLoader: loader,
readerWriter: rw,
conf: config.Default(),
accounts: &accounts.Accounts{},
}, nil
}
// newProject creates a new project from a configuration object.
func newProject(
conf *config.Config,
loader *config.Loader,
readerWriter ReaderWriter,
) (*State, error) {
accs, err := accounts.FromConfig(conf)
if err != nil {
return nil, err
}
return &State{
conf: conf,
readerWriter: readerWriter,
confLoader: loader,
accounts: &accs,
}, nil
}