-
Notifications
You must be signed in to change notification settings - Fork 107
/
early_query.go
109 lines (92 loc) · 3.19 KB
/
early_query.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
package e2e
import (
"context"
"errors"
"fmt"
"time"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
"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"
)
var (
// EarlyQuery is the early query scenario where we query a validator node before the network
// has started and there are no committed blocks.
EarlyQuery scenario.Scenario = &earlyQueryImpl{
Scenario: *NewScenario("early-query"),
}
// EarlyQueryInitHeight is the same as EarlyQuery scenario but with an initial height set.
EarlyQueryInitHeight scenario.Scenario = &earlyQueryImpl{
Scenario: *NewScenario("early-query/init-height"),
initialHeight: 42,
}
)
type earlyQueryImpl struct {
Scenario
initialHeight int64
}
func (sc *earlyQueryImpl) Clone() scenario.Scenario {
return &earlyQueryImpl{
Scenario: sc.Scenario.Clone(),
initialHeight: sc.initialHeight,
}
}
func (sc *earlyQueryImpl) Fixture() (*oasis.NetworkFixture, error) {
f, err := sc.Scenario.Fixture()
if err != nil {
return nil, err
}
f.Network.SetInsecureBeacon()
// Set initial height.
f.Network.InitialHeight = sc.initialHeight
// Only one validator should actually start to prevent the network from committing any blocks.
f.Validators[1].NoAutoStart = true
f.Validators[2].NoAutoStart = true
return f, nil
}
func (sc *earlyQueryImpl) Run(ctx context.Context, childEnv *env.Env) error {
// Start the network.
var err error
if err = sc.Net.Start(); err != nil {
return err
}
// Perform some queries.
cs := sc.Net.Controller().Consensus
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
// StateToGenesis.
_, err = cs.StateToGenesis(ctx, consensus.HeightLatest)
if !errors.Is(err, consensus.ErrNoCommittedBlocks) {
return fmt.Errorf("StateToGenesis query should fail with ErrNoCommittedBlocks (got: %s)", err)
}
// GetBlock.
_, err = cs.GetBlock(ctx, consensus.HeightLatest)
if !errors.Is(err, consensus.ErrNoCommittedBlocks) {
return fmt.Errorf("GetBlock query should fail with ErrNoCommittedBlocks (got: %s)", err)
}
// GetTransactions.
_, err = cs.GetTransactions(ctx, consensus.HeightLatest)
if !errors.Is(err, consensus.ErrNoCommittedBlocks) {
return fmt.Errorf("GetTransactions query should fail with ErrNoCommittedBlocks (got: %s)", err)
}
// GetTransactionsWithResults.
_, err = cs.GetTransactionsWithResults(ctx, consensus.HeightLatest)
if !errors.Is(err, consensus.ErrNoCommittedBlocks) {
return fmt.Errorf("GetTransactionsWithResults query should fail with ErrNoCommittedBlocks (got: %s)", err)
}
// GetStatus.
status, err := sc.Net.Controller().GetStatus(ctx)
if err != nil {
return fmt.Errorf("failed to get status for node: %w", err)
}
if status.Consensus.Status != consensus.StatusStateSyncing {
return fmt.Errorf("node reports as ready before chain is initialized")
}
if status.Consensus.LatestHeight != 0 {
return fmt.Errorf("node reports non-zero latest height before chain is initialized")
}
if !status.Consensus.IsValidator {
return fmt.Errorf("node does not report itself to be a validator at genesis")
}
return nil
}