/
run.go
275 lines (214 loc) · 6.94 KB
/
run.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
package app
import (
"context"
"github.com/omni-network/omni/contracts/bindings"
"github.com/omni-network/omni/e2e/app/agent"
"github.com/omni-network/omni/e2e/netman/pingpong"
"github.com/omni-network/omni/e2e/types"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/k1util"
"github.com/omni-network/omni/lib/log"
e2e "github.com/cometbft/cometbft/test/e2e/pkg"
)
// defaultPingPongN defines a few days of ping pong messages after each deploy.
const defaultPingPongN = 100_000
func DefaultDeployConfig() DeployConfig {
return DeployConfig{
AgentSecrets: agent.Secrets{}, // Empty secrets
PingPongN: defaultPingPongN,
}
}
type DeployConfig struct {
AgentSecrets agent.Secrets
PingPongN uint64
// Internal use parameters (no command line flags).
testConfig bool
}
// Deploy a new e2e network. It also starts all services in order to deploy private portals.
// It also returns an optional deployed ping pong contract is enabled.
func Deploy(ctx context.Context, def Definition, cfg DeployConfig) (types.DeployInfos, *pingpong.XDapp, error) {
if err := Cleanup(ctx, def); err != nil {
return nil, nil, err
}
if def.Testnet.OnlyMonitor {
return nil, nil, deployMonitorOnly(ctx, def, cfg)
}
const genesisValSetID = uint64(1) // validator set IDs start at 1
genesisVals, err := toPortalValidators(def.Testnet.Validators)
if err != nil {
return nil, nil, err
}
if err := deployPublicCreate3(ctx, def); err != nil {
return nil, nil, err
}
// TODO: deploy public proxy admins
// Deploy public portals first so their addresses are available for setup.
if err := def.Netman.DeployPublicPortals(ctx, genesisValSetID, genesisVals); err != nil {
return nil, nil, err
}
if err := Setup(ctx, def, cfg.AgentSecrets, cfg.testConfig); err != nil {
return nil, nil, err
}
if err := StartInitial(ctx, def.Testnet.Testnet, def.Infra); err != nil {
return nil, nil, err
}
if err := fundAccounts(ctx, def); err != nil {
return nil, nil, err
}
if err := deployPrivateCreate3(ctx, def); err != nil {
return nil, nil, err
}
// TODO: deploy private proxy admins
if err := def.Netman.DeployPrivatePortals(ctx, genesisValSetID, genesisVals); err != nil {
return nil, nil, err
}
logRPCs(ctx, def)
deployInfo := make(types.DeployInfos)
if err := deployAVSWithExport(ctx, def, deployInfo); err != nil {
return nil, nil, err
}
for chain, info := range def.Netman.DeployInfo() {
deployInfo.Set(chain.ID, types.ContractPortal, info.PortalAddress, info.DeployHeight)
}
if cfg.PingPongN == 0 {
return deployInfo, nil, nil
}
pp, err := pingpong.Deploy(ctx, def.Netman, def.Backends)
if err != nil {
return nil, nil, errors.Wrap(err, "deploy pingpong")
}
err = pp.StartAllEdges(ctx, cfg.PingPongN)
if err != nil {
return nil, nil, errors.Wrap(err, "start all edges")
}
pp.ExportDeployInfo(deployInfo)
if err := FundValidatorsForTesting(ctx, def); err != nil {
return nil, nil, err
}
return deployInfo, &pp, nil
}
// E2ETestConfig is the configuration required to run a full e2e test.
type E2ETestConfig struct {
Preserve bool
}
// DefaultE2ETestConfig returns a default configuration for a e2e test.
func DefaultE2ETestConfig() E2ETestConfig {
return E2ETestConfig{}
}
// E2ETest runs a full e2e test.
func E2ETest(ctx context.Context, def Definition, cfg E2ETestConfig, secrets agent.Secrets) error {
var pingpongN = uint64(3)
if def.Manifest.PingPongN != 0 {
pingpongN = def.Manifest.PingPongN
}
depCfg := DeployConfig{
AgentSecrets: secrets,
PingPongN: pingpongN,
testConfig: true,
}
deployInfo, pingpong, err := Deploy(ctx, def, depCfg)
if err != nil {
return err
}
stopReceiptMonitor := StartMonitoringReceipts(ctx, def)
stopValidatorUpdates := StartValidatorUpdates(ctx, def)
msgBatches := []int{3, 2, 1} // Send 6 msgs from each chain to each other chain
msgsErr := StartSendingXMsgs(ctx, def.Netman, def.Backends, msgBatches...)
if err := StartRemaining(ctx, def.Testnet.Testnet, def.Infra); err != nil {
return err
}
if err := Wait(ctx, def.Testnet.Testnet, 5); err != nil { // allow some txs to go through
return err
}
if def.Testnet.HasPerturbations() {
if err := perturb(ctx, def.Testnet.Testnet); err != nil {
return err
}
}
if def.Testnet.Evidence > 0 {
return errors.New("evidence injection not supported yet")
}
// Wait for:
// - all xmsgs messages to be sent
// - all xmsgs to be submitted
// - all pingpongs to complete
// - all receipts are successful
log.Info(ctx, "Waiting for all cross chain messages to be sent")
select {
case <-ctx.Done():
return errors.Wrap(ctx.Err(), "cancel")
case err := <-msgsErr:
if err != nil {
return err
}
}
if err := WaitAllSubmissions(ctx, def.Netman.Portals(), sum(msgBatches)); err != nil {
return err
}
if err := pingpong.LogBalances(ctx); err != nil {
return err
}
if err := pingpong.WaitDone(ctx); err != nil {
return errors.Wrap(err, "wait for pingpong")
}
if err := stopReceiptMonitor(); err != nil {
return errors.Wrap(err, "stop deploy")
}
if err := stopValidatorUpdates(); err != nil {
return errors.Wrap(err, "stop validator updates")
}
// Start unit tests.
if err := Test(ctx, def, deployInfo, false); err != nil {
return err
}
if err := LogMetrics(ctx, def); err != nil {
return err
}
if cfg.Preserve {
log.Warn(ctx, "Docker containers not stopped, --preserve=true", nil)
} else if err := Cleanup(ctx, def); err != nil {
return err
}
return nil
}
// Upgrade generates all local artifacts, but only copies the docker-compose file to the VMs.
// It them calls docker-compose up.
func Upgrade(ctx context.Context, def Definition) error {
if err := Setup(ctx, def, agent.Secrets{}, false); err != nil {
return err
}
return def.Infra.Upgrade(ctx)
}
// toPortalValidators returns the provided validator set as a lice of portal validators.
func toPortalValidators(validators map[*e2e.Node]int64) ([]bindings.Validator, error) {
vals := make([]bindings.Validator, 0, len(validators))
for val, power := range validators {
addr, err := k1util.PubKeyToAddress(val.PrivvalKey.PubKey())
if err != nil {
return nil, errors.Wrap(err, "convert validator pubkey to address")
}
vals = append(vals, bindings.Validator{
Addr: addr,
Power: uint64(power),
})
}
return vals, nil
}
func logRPCs(ctx context.Context, def Definition) {
network := externalNetwork(def.Testnet, def.Netman.DeployInfo())
for _, chain := range network.EVMChains() {
log.Info(ctx, "EVM Chain RPC available", "chain_id", chain.ID,
"chain_name", chain.Name, "url", chain.RPCURL)
}
}
// deployMonitorOnly deploys the monitor service only.
// It merely sets up config files and then starts the monitor service.
func deployMonitorOnly(ctx context.Context, def Definition, cfg DeployConfig) error {
if err := Setup(ctx, def, cfg.AgentSecrets, cfg.testConfig); err != nil {
return err
}
if err := def.Infra.StartNodes(ctx); err != nil {
return errors.Wrap(err, "starting initial nodes")
}
return nil
}