/
runtime.go
223 lines (184 loc) · 5.82 KB
/
runtime.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
package chain
import (
"context"
"os"
"path/filepath"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/nqd/flat"
"github.com/pelletier/go-toml"
chainconfig "github.com/ignite/cli/v29/ignite/config/chain"
"github.com/ignite/cli/v29/ignite/pkg/chaincmd"
chaincmdrunner "github.com/ignite/cli/v29/ignite/pkg/chaincmd/runner"
"github.com/ignite/cli/v29/ignite/pkg/errors"
"github.com/ignite/cli/v29/ignite/pkg/xurl"
)
// Gentx wraps the "testd gentx" command for generating a gentx for a validator.
// Returns path of generated gentx.
func (c Chain) Gentx(ctx context.Context, runner chaincmdrunner.Runner, v Validator) (path string, err error) {
return runner.Gentx(
ctx,
v.Name,
v.StakingAmount,
chaincmd.GentxWithMoniker(v.Moniker),
chaincmd.GentxWithCommissionRate(v.CommissionRate),
chaincmd.GentxWithCommissionMaxRate(v.CommissionMaxRate),
chaincmd.GentxWithCommissionMaxChangeRate(v.CommissionMaxChangeRate),
chaincmd.GentxWithMinSelfDelegation(v.MinSelfDelegation),
chaincmd.GentxWithGasPrices(v.GasPrices),
chaincmd.GentxWithDetails(v.Details),
chaincmd.GentxWithIdentity(v.Identity),
chaincmd.GentxWithWebsite(v.Website),
chaincmd.GentxWithSecurityContact(v.SecurityContact),
)
}
// Start wraps the "appd start" command to begin running a chain from the daemon.
func (c Chain) Start(ctx context.Context, runner chaincmdrunner.Runner, cfg *chainconfig.Config) error {
validator, err := chainconfig.FirstValidator(cfg)
if err != nil {
return err
}
servers, err := validator.GetServers()
if err != nil {
return err
}
err = runner.Start(ctx, "--pruning", "nothing", "--grpc.address", servers.GRPC.Address)
return &CannotStartAppError{runner.Cmd().Name(), err}
}
// Configure sets the runtime configurations files for a chain (app.toml, client.toml, config.toml).
func (c Chain) Configure(homePath string, cfg *chainconfig.Config) error {
if err := c.appTOML(homePath, cfg); err != nil {
return err
}
if err := c.clientTOML(homePath, cfg); err != nil {
return err
}
return c.configTOML(homePath, cfg)
}
func (c Chain) appTOML(homePath string, cfg *chainconfig.Config) error {
validator, err := chainconfig.FirstValidator(cfg)
if err != nil {
return err
}
// TODO find a better way in order to not delete comments in the toml.yml
path := filepath.Join(homePath, "config/app.toml")
appConfig, err := toml.LoadFile(path)
if err != nil {
return err
}
servers, err := validator.GetServers()
if err != nil {
return err
}
apiAddr, err := xurl.TCP(servers.API.Address)
if err != nil {
return errors.Errorf("invalid api address format %s: %w", servers.API.Address, err)
}
// Set default config values
appConfig.Set("api.enable", true)
appConfig.Set("api.enabled-unsafe-cors", true)
appConfig.Set("rpc.cors_allowed_origins", []string{"*"})
staked, err := sdktypes.ParseCoinNormalized(validator.Bonded)
if err != nil {
return err
}
gas := sdktypes.NewInt64Coin(staked.Denom, 0)
appConfig.Set("minimum-gas-prices", gas.String())
// Update config values with the validator's Cosmos SDK app config
if err := updateTomlTreeValues(appConfig, validator.App); err != nil {
return err
}
// Make sure the API address have the protocol prefix
appConfig.Set("api.address", apiAddr)
file, err := os.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer file.Close()
_, err = appConfig.WriteTo(file)
return err
}
func (c Chain) configTOML(homePath string, cfg *chainconfig.Config) error {
validator, err := chainconfig.FirstValidator(cfg)
if err != nil {
return err
}
// TODO find a better way in order to not delete comments in the toml.yml
path := filepath.Join(homePath, "config/config.toml")
tmConfig, err := toml.LoadFile(path)
if err != nil {
return err
}
servers, err := validator.GetServers()
if err != nil {
return err
}
rpcAddr, err := xurl.TCP(servers.RPC.Address)
if err != nil {
return errors.Errorf("invalid rpc address format %s: %w", servers.RPC.Address, err)
}
p2pAddr, err := xurl.TCP(servers.P2P.Address)
if err != nil {
return errors.Errorf("invalid p2p address format %s: %w", servers.P2P.Address, err)
}
// Set default config values
tmConfig.Set("mode", "validator")
tmConfig.Set("rpc.cors_allowed_origins", []string{"*"})
tmConfig.Set("consensus.timeout_commit", "1s")
tmConfig.Set("consensus.timeout_propose", "1s")
// Update config values with the validator's Tendermint config
if err := updateTomlTreeValues(tmConfig, validator.Config); err != nil {
return err
}
// Make sure the addresses have the protocol prefix
tmConfig.Set("rpc.laddr", rpcAddr)
tmConfig.Set("p2p.laddr", p2pAddr)
file, err := os.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer file.Close()
_, err = tmConfig.WriteTo(file)
return err
}
func (c Chain) clientTOML(homePath string, cfg *chainconfig.Config) error {
validator, err := chainconfig.FirstValidator(cfg)
if err != nil {
return err
}
path := filepath.Join(homePath, "config/client.toml")
tmConfig, err := toml.LoadFile(path)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
// Set default config values
tmConfig.Set("keyring-backend", "test")
tmConfig.Set("broadcast-mode", "sync")
// Update config values with the validator's client config
if err := updateTomlTreeValues(tmConfig, validator.Client); err != nil {
return err
}
file, err := os.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer file.Close()
_, err = tmConfig.WriteTo(file)
return err
}
func (c Chain) appHome() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, "."+c.app.Name)
}
func updateTomlTreeValues(t *toml.Tree, values map[string]interface{}) error {
flatValues, err := flat.Flatten(values, nil)
if err != nil {
return err
}
for name, v := range flatValues {
t.Set(name, v)
}
return nil
}