/
halt_restore.go
237 lines (210 loc) · 6.48 KB
/
halt_restore.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
package runtime
import (
"context"
"fmt"
"reflect"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
genesis "github.com/oasisprotocol/oasis-core/go/genesis/file"
"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/scenario"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
)
var (
// HaltRestore is the halt and restore scenario.
HaltRestore scenario.Scenario = newHaltRestoreImpl(false)
// HaltRestoreSuspended is the halt and restore scenario with a suspended runtime.
HaltRestoreSuspended scenario.Scenario = newHaltRestoreImpl(true)
)
const haltEpoch = 3
type haltRestoreImpl struct {
runtimeImpl
suspendRuntime bool
haltEpoch int
}
func newHaltRestoreImpl(suspended bool) scenario.Scenario {
name := "halt-restore"
haltEpoch := haltEpoch
if suspended {
name += "-suspended"
// Add some epochs since we're also suspending a runtime.
haltEpoch += 5
}
return &haltRestoreImpl{
runtimeImpl: *newRuntimeImpl(
name,
NewLongTermTestClient().WithMode(ModePart1),
),
haltEpoch: haltEpoch,
suspendRuntime: suspended,
}
}
func (sc *haltRestoreImpl) Clone() scenario.Scenario {
return &haltRestoreImpl{
runtimeImpl: *sc.runtimeImpl.Clone().(*runtimeImpl),
suspendRuntime: sc.suspendRuntime,
haltEpoch: sc.haltEpoch,
}
}
func (sc *haltRestoreImpl) Fixture() (*oasis.NetworkFixture, error) {
f, err := sc.runtimeImpl.Fixture()
if err != nil {
return nil, err
}
f.Network.SetMockEpoch()
f.Network.HaltEpoch = uint64(sc.haltEpoch)
for _, val := range f.Validators {
val.AllowEarlyTermination = true
}
return f, nil
}
func (sc *haltRestoreImpl) Run(childEnv *env.Env) error {
ctx := context.Background()
if err := sc.startNetworkAndTestClient(ctx, childEnv); err != nil {
return err
}
fixture, err := sc.Fixture()
if err != nil {
return err
}
if err = sc.initialEpochTransitions(fixture); err != nil {
return err
}
// We're at epoch 2 after the initial transitions
nextEpoch := beacon.EpochTime(3)
// Wait for the client to exit.
if err = sc.waitTestClientOnly(); err != nil {
return err
}
if sc.suspendRuntime {
// Stop compute nodes.
sc.Logger.Info("stopping compute nodes")
for _, n := range sc.Net.ComputeWorkers() {
if err = n.Stop(); err != nil {
return fmt.Errorf("failed to stop node: %w", err)
}
}
// Epoch transitions so nodes expire.
sc.Logger.Info("performing epoch transitions so nodes expire")
for i := 0; i < 3; i++ {
if err = sc.Net.Controller().SetEpoch(ctx, nextEpoch); err != nil {
return fmt.Errorf("failed to set epoch %d: %w", nextEpoch, err)
}
nextEpoch++
}
// Ensure that runtime got suspended.
sc.Logger.Info("checking that runtime got suspended")
_, err = sc.Net.Controller().Registry.GetRuntime(ctx, ®istry.NamespaceQuery{
Height: consensus.HeightLatest,
ID: fixture.Runtimes[1].ID,
})
switch err {
case nil:
return fmt.Errorf("runtime should be suspended but it is not")
case registry.ErrNoSuchRuntime:
// Runtime is suspended.
sc.Logger.Info("runtime is suspended")
default:
return fmt.Errorf("unexpected error while fetching runtime: %w", err)
}
}
// Transition to halt epoch.
sc.Logger.Info("transitioning to halt epoch",
"halt_epoch", sc.haltEpoch,
)
for i := nextEpoch; i <= beacon.EpochTime(sc.haltEpoch); i++ {
sc.Logger.Info("setting epoch",
"epoch", i,
)
if err = sc.Net.Controller().SetEpoch(ctx, i); err != nil {
return fmt.Errorf("failed to set epoch %d: %w", i, err)
}
}
// Wait for validators to exit so that genesis docs are dumped.
var exitChs []reflect.SelectCase
for _, val := range sc.Net.Validators() {
exitChs = append(exitChs, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(val.Exit()),
})
}
// Exit status doesn't matter, we only need one of the validators to stop existing.
_, _, _ = reflect.Select(exitChs)
sc.Logger.Info("gathering exported genesis files")
files, err := sc.GetExportedGenesisFiles(true)
if err != nil {
return fmt.Errorf("failure getting exported genesis files: %w", err)
}
// Stop the network.
sc.Logger.Info("stopping the network")
sc.Net.Stop()
if err = sc.ResetConsensusState(childEnv); err != nil {
return fmt.Errorf("failed to reset consensus state: %w", err)
}
// Start the network and the client again and check that everything
// works with restored state.
sc.Logger.Info("starting the network again")
// Update halt epoch in the exported genesis so the network doesn't
// instantly halt.
genesisFileProvider, err := genesis.NewFileProvider(files[0])
if err != nil {
sc.Logger.Error("failed getting genesis file provider",
"err", err,
"genesis_file", files[0],
)
return err
}
genesisDoc, err := genesisFileProvider.GetGenesisDocument()
if err != nil {
sc.Logger.Error("failed getting genesis document from file provider",
"err", err,
)
return err
}
genesisDoc.HaltEpoch *= 2
// Don't need mock epoch anymore.
genesisDoc.Beacon.Parameters.DebugMockBackend = false
if err = genesisDoc.WriteFileJSON(files[0]); err != nil {
sc.Logger.Error("failed to update genesis",
"err", err,
)
return err
}
// Ensure compute runtime in genesis is in expected state.
var rtList []*registry.Runtime
switch sc.suspendRuntime {
case true:
rtList = genesisDoc.Registry.SuspendedRuntimes
default:
rtList = genesisDoc.Registry.Runtimes
}
var found bool
for _, rt := range rtList {
if rt.Kind != registry.KindCompute {
continue
}
found = true
}
if !found {
sc.Logger.Error("runtime not in expected state",
"expected_suspended", sc.suspendRuntime,
"runtimes", genesisDoc.Registry.Runtimes,
"suspended_runtimes", genesisDoc.Registry.SuspendedRuntimes,
)
return fmt.Errorf("runtime not in expected state")
}
// Use the updated genesis file.
fixture.Network.GenesisFile = files[0]
// Make sure to not overwrite the entity.
fixture.Entities[1].Restore = true
if sc.Net, err = fixture.Create(childEnv); err != nil {
return err
}
// If network is used, enable shorter per-node socket paths, because some e2e test datadir exceed maximum unix
// socket path length.
sc.Net.Config().UseShortGrpcSocketPaths = true
newTestClient := sc.testClient.Clone().(*LongTermTestClient)
sc.runtimeImpl.testClient = newTestClient.WithMode(ModePart2).WithSeed("second_seed")
return sc.runtimeImpl.Run(childEnv)
}