-
Notifications
You must be signed in to change notification settings - Fork 107
/
e2e.go
497 lines (445 loc) · 13.6 KB
/
e2e.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Package e2e implements the Oasis e2e test scenarios.
package e2e
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
goHash "hash"
"io"
"os"
"path/filepath"
flag "github.com/spf13/pflag"
"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
consensusGenesis "github.com/oasisprotocol/oasis-core/go/consensus/genesis"
genesis "github.com/oasisprotocol/oasis-core/go/genesis/api"
genesisFile "github.com/oasisprotocol/oasis-core/go/genesis/file"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/cmd"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/env"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/oasis"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/oasis/cli"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/scenario"
)
const (
// cfgNodeBinary is the path to oasis-node executable.
cfgNodeBinary = "node.binary"
)
// ParamsDummyScenario is a dummy instance of E2E scenario used to register global E2E flags.
var ParamsDummyScenario = NewScenario("")
// Scenario is a base scenario for oasis-node end-to-end tests.
type Scenario struct {
Net *oasis.Network
Flags *env.ParameterFlagSet
Logger *logging.Logger
name string
}
// NewScenario creates a new base scenario for oasis-node end-to-end tests.
func NewScenario(name string) *Scenario {
// Empty scenario name is used for registering global parameters only.
fullName := "e2e"
if name != "" {
fullName += "/" + name
}
sc := &Scenario{
name: fullName,
Logger: logging.GetLogger("scenario/" + fullName),
Flags: env.NewParameterFlagSet(fullName, flag.ContinueOnError),
}
sc.Flags.String(cfgNodeBinary, "oasis-node", "path to the node binary")
return sc
}
// Clone implements scenario.Scenario.
func (sc *Scenario) Clone() Scenario {
return Scenario{
Net: sc.Net,
Flags: sc.Flags.Clone(),
Logger: sc.Logger,
name: sc.name,
}
}
// Name implements scenario.Scenario.
func (sc *Scenario) Name() string {
return sc.name
}
// Parameters implements scenario.Scenario.
func (sc *Scenario) Parameters() *env.ParameterFlagSet {
return sc.Flags
}
// PreInit implements scenario.Scenario.
func (sc *Scenario) PreInit(childEnv *env.Env) error {
return nil
}
// Fixture implements scenario.Scenario.
func (sc *Scenario) Fixture() (*oasis.NetworkFixture, error) {
nodeBinary, _ := sc.Flags.GetString(cfgNodeBinary)
return &oasis.NetworkFixture{
Network: oasis.NetworkCfg{
NodeBinary: nodeBinary,
Consensus: consensusGenesis.Genesis{
Parameters: consensusGenesis.Parameters{
GasCosts: transaction.Costs{
consensusGenesis.GasOpTxByte: 1,
},
},
},
},
Entities: []oasis.EntityCfg{
{IsDebugTestEntity: true},
{},
},
Validators: []oasis.ValidatorFixture{
{Entity: 1, Consensus: oasis.ConsensusFixture{EnableConsensusRPCWorker: true, SupplementarySanityInterval: 1}},
{Entity: 1, Consensus: oasis.ConsensusFixture{EnableConsensusRPCWorker: true}},
{Entity: 1, Consensus: oasis.ConsensusFixture{EnableConsensusRPCWorker: true}},
},
Seeds: []oasis.SeedFixture{{}},
}, nil
}
// Init implements scenario.Scenario.
func (sc *Scenario) Init(childEnv *env.Env, net *oasis.Network) error {
sc.Net = net
return nil
}
// GetExportedGenesisFiles gathers exported genesis files and ensures
// all exported genesis files match.
func (sc *Scenario) GetExportedGenesisFiles(skipCompute bool) ([]string, error) {
dumpGlob := "genesis-*.json"
// Gather all nodes.
var nodes []interface {
ExportsPath() string
}
for _, v := range sc.Net.Validators() {
nodes = append(nodes, v)
}
if !skipCompute {
for _, n := range sc.Net.ComputeWorkers() {
nodes = append(nodes, n)
}
}
for _, n := range sc.Net.Keymanagers() {
nodes = append(nodes, n)
}
// Gather all genesis files.
var files []string
for _, node := range nodes {
dumpGlobPath := filepath.Join(node.ExportsPath(), dumpGlob)
globMatch, err := filepath.Glob(dumpGlobPath)
if err != nil {
return nil, fmt.Errorf("glob failed: %s: %w", dumpGlobPath, err)
}
if len(globMatch) == 0 {
return nil, fmt.Errorf("genesis file not found in: %s", dumpGlobPath)
}
if len(globMatch) > 1 {
return nil, fmt.Errorf("more than one genesis file found in: %s", dumpGlobPath)
}
files = append(files, globMatch[0])
}
// Assert all exported files match.
var firstHash goHash.Hash
for _, file := range files {
// Compute hash.
f, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("failed to open file: %s: %w", file, err)
}
defer f.Close()
hnew := sha256.New()
if _, err := io.Copy(hnew, f); err != nil {
return nil, fmt.Errorf("sha256 failed on: %s: %w", file, err)
}
if firstHash == nil {
firstHash = hnew
}
// Compare hash with first hash.
if !bytes.Equal(firstHash.Sum(nil), hnew.Sum(nil)) {
return nil, fmt.Errorf("exported genesis files do not match %s, %s", files[0], file)
}
}
return files, nil
}
// Flag for consensus state reset.
const (
PreserveValidatorRuntimeStorage uint8 = iota
PreserveValidatorLocalStorage
ForceValidatorReset
PreserveComputeWorkerRuntimeStorage
PreserveComputeWorkerLocalStorage
ForceComputeWorkerReset
PreserveClientRuntimeStorage
PreserveClientLocalStorage
ForceClientReset
PreserveByzantineRuntimeStorage
PreserveByzantineLocalStorage
ForceByzantineReset
PreserveSentryRuntimeStorage
PreserveSentryLocalStorage
ForceSentryReset
PreserveKeymanagerRuntimeStorage
PreserveKeymanagerLocalStorage
ForceKeymanagerReset
)
// ResetConsensusState removes all consensus state, preserving runtime storage and node-local
// storage databases unless specified with flags otherwise.
func (sc *Scenario) ResetConsensusState(childEnv *env.Env, flags map[uint8]bool) error {
if flags == nil {
flags = map[uint8]bool{
PreserveComputeWorkerRuntimeStorage: true,
PreserveKeymanagerLocalStorage: true,
}
}
cli := cli.New(childEnv, sc.Net, sc.Logger)
for _, val := range sc.Net.Validators() {
if err := cli.UnsafeReset(
val.DataDir(),
flags[PreserveValidatorRuntimeStorage],
flags[PreserveValidatorLocalStorage],
flags[ForceValidatorReset],
); err != nil {
return err
}
}
for _, cw := range sc.Net.ComputeWorkers() {
if err := cli.UnsafeReset(cw.DataDir(),
flags[PreserveComputeWorkerRuntimeStorage],
flags[PreserveComputeWorkerLocalStorage],
flags[ForceComputeWorkerReset],
); err != nil {
return err
}
}
for _, cl := range sc.Net.Clients() {
if err := cli.UnsafeReset(cl.DataDir(),
flags[PreserveClientRuntimeStorage],
flags[PreserveClientLocalStorage],
flags[ForceClientReset],
); err != nil {
return err
}
}
for _, bz := range sc.Net.Byzantine() {
if err := cli.UnsafeReset(bz.DataDir(),
flags[PreserveByzantineRuntimeStorage],
flags[PreserveByzantineLocalStorage],
flags[ForceByzantineReset],
); err != nil {
return err
}
}
for _, se := range sc.Net.Sentries() {
if err := cli.UnsafeReset(se.DataDir(),
flags[PreserveSentryRuntimeStorage],
flags[PreserveSentryLocalStorage],
flags[ForceSentryReset],
); err != nil {
return err
}
}
for _, kw := range sc.Net.Keymanagers() {
if err := cli.UnsafeReset(kw.DataDir(),
flags[PreserveKeymanagerRuntimeStorage],
flags[PreserveKeymanagerLocalStorage],
flags[ForceKeymanagerReset],
); err != nil {
return err
}
}
return nil
}
// DumpRestoreNetwork first dumps the current network state and then attempts to restore it.
func (sc *Scenario) DumpRestoreNetwork(
childEnv *env.Env,
fixture *oasis.NetworkFixture,
doDbDump bool,
genesisMapFn func(*genesis.Document),
resetFlags map[uint8]bool,
) error {
// Dump-restore network.
sc.Logger.Info("dumping network state",
"child", childEnv,
)
dumpPath := filepath.Join(childEnv.Dir(), "genesis_dump.json")
args := []string{
"genesis", "dump",
"--height", "0",
"--genesis.file", dumpPath,
"--address", "unix:" + sc.Net.Validators()[0].SocketPath(),
}
if err := cli.RunSubCommand(childEnv, sc.Logger, "genesis-dump", sc.Net.Config().NodeBinary, args); err != nil {
return fmt.Errorf("scenario/e2e/dump_restore: failed to dump state: %w", err)
}
// Stop the network.
sc.Logger.Info("stopping the network")
sc.Net.Stop()
if doDbDump {
if err := sc.dumpDatabase(childEnv, fixture, dumpPath); err != nil {
return fmt.Errorf("scenario/e2e/dump_restore: failed to dump database: %w", err)
}
}
if len(sc.Net.ComputeWorkers()) > 0 {
// Dump storage.
args = []string{
"debug", "storage", "export",
"--genesis.file", dumpPath,
"--datadir", sc.Net.ComputeWorkers()[0].DataDir(),
"--storage.export.dir", filepath.Join(childEnv.Dir(), "storage_dumps"),
"--debug.dont_blame_oasis",
"--debug.allow_test_keys",
}
if err := cli.RunSubCommand(childEnv, sc.Logger, "storage-dump", sc.Net.Config().NodeBinary, args); err != nil {
return fmt.Errorf("scenario/e2e/dump_restore: failed to dump storage: %w", err)
}
}
// Reset all the state back to the vanilla state.
if err := sc.ResetConsensusState(childEnv, resetFlags); err != nil {
return fmt.Errorf("scenario/e2e/dump_restore: failed to clean tendermint storage: %w", err)
}
// Apply optional mapping function to the genesis document.
if genesisMapFn != nil {
// Load the existing export.
fp, err := genesisFile.NewFileProvider(dumpPath)
if err != nil {
return fmt.Errorf("failed to instantiate genesis document file provider: %w", err)
}
doc, err := fp.GetGenesisDocument()
if err != nil {
return fmt.Errorf("failed to get genesis document: %w", err)
}
genesisMapFn(doc)
// Write back the updated document.
buf, err := json.Marshal(doc)
if err != nil {
return fmt.Errorf("failed to marshal updated genesis document: %w", err)
}
if err = os.WriteFile(dumpPath, buf, 0o600); err != nil {
return fmt.Errorf("failed to write updated genesis document: %w", err)
}
}
// Start the network and the client again.
sc.Logger.Info("starting the network again")
fixture.Network.GenesisFile = dumpPath
// Make sure to not overwrite entities.
for i, entity := range fixture.Entities {
if !entity.IsDebugTestEntity {
fixture.Entities[i].Restore = true
}
}
// If network is used, enable shorter per-node socket paths, because some e2e test datadir
// exceed maximum unix socket path length.
fixture.Network.UseShortGrpcSocketPaths = true
var err error
if sc.Net, err = fixture.Create(childEnv); err != nil {
return err
}
return nil
}
func (sc *Scenario) dumpDatabase(childEnv *env.Env, fixture *oasis.NetworkFixture, exportPath string) error {
// Load the existing export.
eFp, err := genesisFile.NewFileProvider(exportPath)
if err != nil {
return fmt.Errorf("failed to instantiate file provider (export): %w", err)
}
exportedDoc, err := eFp.GetGenesisDocument()
if err != nil {
return fmt.Errorf("failed to get genesis doc (export): %w", err)
}
sc.Logger.Info("dumping via debug dumpdb")
// Dump the state with the debug command off one of the validators.
dbDumpPath := filepath.Join(childEnv.Dir(), "debug_dump.json")
args := []string{
"debug", "dumpdb",
"--datadir", sc.Net.Validators()[0].DataDir(),
"-g", sc.Net.GenesisPath(),
"--dump.version", fmt.Sprintf("%d", exportedDoc.Height),
"--dump.output", dbDumpPath,
"--debug.dont_blame_oasis",
"--debug.allow_test_keys",
}
if err = cli.RunSubCommand(childEnv, sc.Logger, "debug-dump", sc.Net.Config().NodeBinary, args); err != nil {
return fmt.Errorf("failed to dump database: %w", err)
}
// Load the dumped state.
fp, err := genesisFile.NewFileProvider(dbDumpPath)
if err != nil {
return fmt.Errorf("failed to instantiate file provider (db): %w", err)
}
dbDoc, err := fp.GetGenesisDocument()
if err != nil {
return fmt.Errorf("failed to get genesis doc (dump): %w", err)
}
// Compare the two documents for approximate equality. Note: Certain
// fields will be different, so those are fixed up before the comparison.
dbDoc.Beacon.Base = exportedDoc.Beacon.Base
dbDoc.Time = exportedDoc.Time
dbRaw, err := json.Marshal(dbDoc)
if err != nil {
return fmt.Errorf("failed to marshal fixed up dump: %w", err)
}
expRaw, err := json.Marshal(exportedDoc)
if err != nil {
return fmt.Errorf("failed to re-marshal export doc: %w", err)
}
if !bytes.Equal(expRaw, dbRaw) {
return fmt.Errorf("dump does not match state export")
}
return nil
}
func (sc *Scenario) finishWithoutChild() error {
var err error
select {
case err = <-sc.Net.Errors():
return err
default:
return sc.Net.CheckLogWatchers()
}
}
// RegisterScenarios registers all end-to-end scenarios.
func RegisterScenarios() error {
// Register non-scenario-specific parameters.
cmd.RegisterScenarioParams(ParamsDummyScenario.Name(), ParamsDummyScenario.Parameters())
// Register default scenarios which are executed, if no test names provided.
for _, s := range []scenario.Scenario{
// Registry CLI test.
RegistryCLI,
// Stake CLI test.
StakeCLI,
// Gas fees tests.
GasFeesStaking,
GasFeesStakingDumpRestore,
// Identity CLI test.
IdentityCLI,
// Genesis file test.
GenesisFile,
// Node upgrade tests.
NodeUpgradeDummy,
NodeUpgradeMaxAllowances,
NodeUpgradeV62,
NodeUpgradeEmpty,
NodeUpgradeCancel,
// Debonding entries from genesis test.
Debond,
// Early query test.
EarlyQuery,
EarlyQueryInitHeight,
// Consensus state sync.
ConsensusStateSync,
// Multiple seeds test.
MultipleSeeds,
// Seed API test.
SeedAPI,
// ValidatorEquivocation test.
ValidatorEquivocation,
// Byzantine VRF beacon tests.
ByzantineVRFBeaconHonest,
ByzantineVRFBeaconEarly,
ByzantineVRFBeaconMissing,
// Minimum transact balance test.
MinTransactBalance,
} {
if err := cmd.Register(s); err != nil {
return err
}
}
return nil
}