-
Notifications
You must be signed in to change notification settings - Fork 107
/
archive_api.go
429 lines (379 loc) · 14 KB
/
archive_api.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
package runtime
import (
"context"
"fmt"
"reflect"
"time"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/common/pubsub"
"github.com/oasisprotocol/oasis-core/go/config"
consensusAPI "github.com/oasisprotocol/oasis-core/go/consensus/api"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
"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"
roothash "github.com/oasisprotocol/oasis-core/go/roothash/api"
"github.com/oasisprotocol/oasis-core/go/runtime/client/api"
)
// ArchiveAPI is the scenario where archive node control, consensus and runtime APIs are tested.
var ArchiveAPI scenario.Scenario = &archiveAPI{
Scenario: *NewScenario(
"archive-api",
NewTestClient().WithScenario(InsertTransferKeyValueScenario),
),
}
type archiveAPI struct {
Scenario
}
func (sc *archiveAPI) Fixture() (*oasis.NetworkFixture, error) {
f, err := sc.Scenario.Fixture()
if err != nil {
return nil, err
}
// Add a validator node that will be turned into an archive node.
f.Validators = append(f.Validators, oasis.ValidatorFixture{Entity: 1, AllowEarlyTermination: true})
// Add a compute node that will be turned into an archive node.
f.ComputeWorkers = append(f.ComputeWorkers, oasis.ComputeWorkerFixture{Entity: 1, Runtimes: []int{1}, AllowEarlyTermination: true})
f.Runtimes[1].Executor.GroupSize++
f.Network.SetMockEpoch()
f.Network.HaltEpoch = uint64(haltEpoch)
for _, val := range f.Validators {
val.AllowEarlyTermination = true
}
for _, cw := range f.ComputeWorkers {
cw.AllowEarlyTermination = true
}
return f, nil
}
func (sc *archiveAPI) Clone() scenario.Scenario {
return &archiveAPI{
Scenario: *sc.Scenario.Clone().(*Scenario),
}
}
func (sc *archiveAPI) testArchiveAPI(ctx context.Context, archiveCtrl *oasis.Controller, runtime bool, halted bool) error { // nolint: gocyclo
sc.Logger.Info("testing IsSynced")
isSynced, err := archiveCtrl.IsSynced(ctx)
if err != nil {
return err
}
if !isSynced {
return fmt.Errorf("archive node reports as not synced")
}
sc.Logger.Info("testing IsReady")
isReady, err := archiveCtrl.IsReady(ctx)
if err != nil {
return err
}
if !isReady {
return fmt.Errorf("archive node reports as not ready")
}
sc.Logger.Info("testing GetStatus")
status, err := archiveCtrl.GetStatus(ctx)
if err != nil {
return fmt.Errorf("failed to get status for node: %w", err)
}
if status.Mode != config.ModeArchive {
return fmt.Errorf("archive node should report its mode, got: %v", status.Mode)
}
if !status.Consensus.Features.Has(consensusAPI.FeatureArchiveNode) {
return fmt.Errorf("archive node lacks archive feature, got: '%s'", status.Consensus.Features.String())
}
if status.Consensus.LatestHeight == int64(0) {
return fmt.Errorf("archive node latest height should not be 0")
}
if status.Consensus.P2P != nil {
return fmt.Errorf("archive should not be included in the P2P network")
}
if p := status.PendingUpgrades; len(p) != 0 {
return fmt.Errorf("unexpected pending upgrades: %v", p)
}
sc.Logger.Info("testing SetEpoch")
if err = archiveCtrl.SetEpoch(ctx, beacon.EpochTime(0)); err == nil {
return fmt.Errorf("archive node SetEpoch should fail")
}
sc.Logger.Info("testing GetGenesisDocument")
doc, err := archiveCtrl.Consensus.GetGenesisDocument(ctx)
if err != nil {
return fmt.Errorf("archive node GetGenesisDocument should work")
}
if doc == nil {
return fmt.Errorf("archive node GetGenesisDocument should not be nil")
}
sc.Logger.Info("testing SubmitTx")
err = archiveCtrl.Consensus.SubmitTx(ctx, &transaction.SignedTransaction{})
if err != consensusAPI.ErrUnsupported {
return fmt.Errorf("archive node SubmitTx should fail with unsupported")
}
sc.Logger.Info("testing SubmitTxNoWait")
err = archiveCtrl.Consensus.SubmitTxNoWait(ctx, &transaction.SignedTransaction{})
if err != consensusAPI.ErrUnsupported {
return fmt.Errorf("archive node SubmitTxNoWait should fail with unsupported")
}
sc.Logger.Info("testing SubmitEvidence")
err = archiveCtrl.Consensus.SubmitEvidence(ctx, &consensusAPI.Evidence{})
if err != consensusAPI.ErrUnsupported {
return fmt.Errorf("archive node SubmitEvidence should fail with unsupported")
}
sc.Logger.Info("testing StateToGenesis")
_, err = archiveCtrl.Consensus.StateToGenesis(ctx, 0)
if err != nil {
return fmt.Errorf("archive node StateToGenesis should work: %w", err)
}
sc.Logger.Info("testing EstimateGas")
_, err = archiveCtrl.Consensus.EstimateGas(ctx, &consensusAPI.EstimateGasRequest{})
if err != consensusAPI.ErrUnsupported {
return fmt.Errorf("archive node EstimateGas should fail with unsupported")
}
sc.Logger.Info("testing GetBlock")
archiveBlock, err := archiveCtrl.Consensus.GetBlock(ctx, consensusAPI.HeightLatest)
if err != nil {
return fmt.Errorf("archive node GetBlock should work: %w", err)
}
sc.Logger.Info("testing GetTransactions")
_, err = archiveCtrl.Consensus.GetTransactions(ctx, consensusAPI.HeightLatest)
if err != nil {
return fmt.Errorf("archive node GetTransactions should work: %w", err)
}
sc.Logger.Info("testing GetTransactionsWithResults")
_, err = archiveCtrl.Consensus.GetTransactionsWithResults(ctx, consensusAPI.HeightLatest)
if err != nil {
return fmt.Errorf("archive node GetTransactions should work: %w", err)
}
sc.Logger.Info("testing GetUnconfirmedTransactions")
_, err = archiveCtrl.Consensus.GetUnconfirmedTransactions(ctx)
if err != consensusAPI.ErrUnsupported {
return fmt.Errorf("archive node GetUnconfirmedTransactions should work: %w", err)
}
sc.Logger.Info("testing GetSignerNonce")
_, err = archiveCtrl.Consensus.GetSignerNonce(ctx, &consensusAPI.GetSignerNonceRequest{})
if err != consensusAPI.ErrUnsupported {
return fmt.Errorf("archive node GetSignerNonce should fail with unsupported")
}
sc.Logger.Info("testing GetLightBlock")
_, err = archiveCtrl.Consensus.GetLightBlock(ctx, consensusAPI.HeightLatest)
if err != nil {
return fmt.Errorf("archive node GetLightBlock should work: %w", err)
}
sc.Logger.Info("testing GetParameters")
_, err = archiveCtrl.Consensus.GetParameters(ctx, consensusAPI.HeightLatest)
if err != nil {
return fmt.Errorf("archive node GetParameters should work: %w", err)
}
if !halted {
var blockCh <-chan *consensusAPI.Block
var blockSub pubsub.ClosableSubscription
// Wait some blocks.
blockCh, blockSub, err = sc.Net.Controller().Consensus.WatchBlocks(ctx)
if err != nil {
return err
}
defer blockSub.Close()
sc.Logger.Info("waiting for some blocks")
var wait uint
for {
if wait > 5 {
break
}
select {
case <-blockCh:
wait++
case <-time.After(30 * time.Second):
return fmt.Errorf("timed out waiting for blocks")
}
}
// Ensure archive node is not syncing.
var validatorCtrl *oasis.Controller
validatorCtrl, err = oasis.NewController(sc.Net.Validators()[0].SocketPath())
if err != nil {
return err
}
var valBlock *consensusAPI.Block
valBlock, err = validatorCtrl.Consensus.GetBlock(ctx, consensusAPI.HeightLatest)
if err != nil {
return fmt.Errorf("client GetBlock: %w", err)
}
var archiveBlock2 *consensusAPI.Block
archiveBlock2, err = archiveCtrl.Consensus.GetBlock(ctx, consensusAPI.HeightLatest)
if err != nil {
return fmt.Errorf("archive node GetBlock should work: %w", err)
}
if archiveBlock.Height != archiveBlock2.Height {
return fmt.Errorf("archive node latest block changed from: %d to: %d", archiveBlock.Height, archiveBlock2.Height)
}
if valBlock.Height <= archiveBlock.Height {
return fmt.Errorf("client latest block height (%d) should be higher than latest archive block height (%d)", valBlock.Height, archiveBlock.Height)
}
}
if !runtime {
return nil
}
// Test runtime queries.
rtClient := archiveCtrl.RuntimeClient
sc.Logger.Info("testing runtime GetBlock")
blk, err := rtClient.GetBlock(ctx, &api.GetBlockRequest{RuntimeID: KeyValueRuntimeID, Round: api.RoundLatest})
if err != nil {
return fmt.Errorf("runtime GetBlock: %w", err)
}
if blk.Header.Round == 0 {
return fmt.Errorf("unexpected latest runtime round: %d", blk.Header.Round)
}
sc.Logger.Info("testing runtime GetEvents")
_, err = rtClient.GetEvents(ctx, &api.GetEventsRequest{RuntimeID: KeyValueRuntimeID, Round: api.RoundLatest})
if err != nil {
return fmt.Errorf("runtime GetEvents: %w", err)
}
sc.Logger.Info("testing runtime GetLastRetainBlock")
_, err = rtClient.GetLastRetainedBlock(ctx, KeyValueRuntimeID)
if err != nil {
return fmt.Errorf("runtime GetLastRetainedBlock: %w", err)
}
sc.Logger.Info("testing runtime GetTransactions")
_, err = rtClient.GetTransactions(ctx, &api.GetTransactionsRequest{RuntimeID: KeyValueRuntimeID, Round: api.RoundLatest})
if err != nil {
return fmt.Errorf("runtime GetTransactions: %w", err)
}
sc.Logger.Info("testing runtime GetTransactionsWithResults")
_, err = rtClient.GetTransactionsWithResults(ctx, &api.GetTransactionsRequest{RuntimeID: KeyValueRuntimeID, Round: api.RoundLatest})
if err != nil {
return fmt.Errorf("runtime GetTransactionsWithResults: %w", err)
}
sc.Logger.Info("testing runtime WatchBlocks")
_, sub, err := rtClient.WatchBlocks(ctx, KeyValueRuntimeID)
if err != nil {
return fmt.Errorf("runtime WatchBlocks: %w", err)
}
defer sub.Close()
// Temporary configure the archive as the client controller.
clientCtrl := sc.Net.ClientController()
sc.Net.SetClientController(archiveCtrl)
defer func() {
sc.Net.SetClientController(clientCtrl)
}()
// Test runtime client query.
sc.Logger.Info("testing runtime client query")
rsp, err := sc.submitKeyValueRuntimeGetQuery(
ctx,
KeyValueRuntimeID,
"my_key",
roothash.RoundLatest,
)
if err != nil {
return fmt.Errorf("failed to query runtime: %w", err)
}
if rsp != "my_value" {
return fmt.Errorf("response does not have expected value (got: '%v', expected: '%v')", rsp, "my_value")
}
return nil
}
func (sc *archiveAPI) Run(ctx context.Context, childEnv *env.Env) error {
if err := sc.StartNetworkAndTestClient(ctx, childEnv); err != nil {
return err
}
fixture, err := sc.Fixture()
if err != nil {
return err
}
var nextEpoch beacon.EpochTime
if nextEpoch, err = sc.initialEpochTransitions(ctx, fixture); err != nil {
return err
}
nextEpoch++ // Next, after initial transitions.
// Wait for the client to exit.
sc.Logger.Info("waiting for test client to exit")
if err = sc.WaitTestClient(); err != nil {
return err
}
// Convert a validator into an archive node.
sc.Logger.Info("converting validator 3 into an archive node")
if err = sc.Net.Validators()[3].Stop(); err != nil {
return fmt.Errorf("stopping validator: %w", err)
}
sc.Net.Validators()[3].SetArchiveMode(true)
if err = sc.Net.Validators()[3].Start(); err != nil {
return fmt.Errorf("starting validator as archive: %w", err)
}
valArchive, err := oasis.NewController(sc.Net.Validators()[3].SocketPath())
if err != nil {
return err
}
sc.Logger.Info("testing validator archive API")
if err = sc.testArchiveAPI(ctx, valArchive, false, false); err != nil {
return fmt.Errorf("validator archive api: %w", err)
}
// Convert a compute worker into an archive node.
sc.Logger.Info("converting compute worker 3 into an archive node")
if err = sc.Net.ComputeWorkers()[3].Stop(); err != nil {
return fmt.Errorf("stopping compute worker: %w", err)
}
sc.Net.ComputeWorkers()[3].SetArchiveMode(true)
if err = sc.Net.ComputeWorkers()[3].Start(); err != nil {
return fmt.Errorf("starting compute worker as archive: %w", err)
}
computeArchive, err := oasis.NewController(sc.Net.ComputeWorkers()[3].SocketPath())
if err != nil {
return err
}
sc.Logger.Info("testing compute worker archive API")
if err = sc.testArchiveAPI(ctx, computeArchive, true, false); err != nil {
return fmt.Errorf("compute archive api: %w", err)
}
// Transition to halt epoch.
sc.Logger.Info("transitioning to halt epoch",
"halt_epoch", haltEpoch,
)
for i := nextEpoch; i <= beacon.EpochTime(haltEpoch); i++ {
sc.Logger.Info("setting epoch",
"epoch", i,
)
if err = sc.Net.Controller().SetEpoch(ctx, i); err != nil && i != beacon.EpochTime(haltEpoch) {
return fmt.Errorf("failed to set epoch %d: %w", i, err)
}
}
// Wait for validators to exit.
sc.Logger.Info("wait for nodes to exit")
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("network halted, testing archive nodes on a halted network")
// Convert validator 0 into an archive node.
sc.Logger.Info("converting validator 0 into an archive node")
if err = sc.Net.Validators()[0].Stop(); err != nil {
return fmt.Errorf("stopping validator: %w", err)
}
sc.Net.Validators()[0].SetArchiveMode(true)
if err = sc.Net.Validators()[0].Start(); err != nil {
return fmt.Errorf("starting validator as archive: %w", err)
}
valArchive, err = oasis.NewController(sc.Net.Validators()[0].SocketPath())
if err != nil {
return err
}
sc.Logger.Info("testing validator archive API")
if err = sc.testArchiveAPI(ctx, valArchive, false, true); err != nil {
return fmt.Errorf("validator archive api: %w", err)
}
// Convert compute 0 into an archive node.
sc.Logger.Info("converting compute worker 0 into an archive node")
if err = sc.Net.ComputeWorkers()[0].Stop(); err != nil {
return fmt.Errorf("stopping compute worker: %w", err)
}
sc.Net.ComputeWorkers()[0].SetArchiveMode(true)
if err = sc.Net.ComputeWorkers()[0].Start(); err != nil {
return fmt.Errorf("starting compute worker as archive: %w", err)
}
computeArchive, err = oasis.NewController(sc.Net.ComputeWorkers()[0].SocketPath())
if err != nil {
return err
}
sc.Logger.Info("testing compute worker archive API")
if err = sc.testArchiveAPI(ctx, computeArchive, true, true); err != nil {
return fmt.Errorf("compute worker archive api: %w", err)
}
return nil
}