-
Notifications
You must be signed in to change notification settings - Fork 107
/
early_query.go
152 lines (132 loc) · 4.62 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
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
package runtime
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"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/scenario/e2e"
)
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: e2e.NewScenario("early-query"),
}
// EarlyQueryInitHeight is the same as EarlyQuery scenario but with an initial height set.
EarlyQueryInitHeight scenario.Scenario = &earlyQueryImpl{
Scenario: e2e.NewScenario("early-query/init-height"),
initialHeight: 42,
}
// EarlyQueryRuntime is the early query scenario where we query a runtime node.
EarlyQueryRuntime scenario.Scenario = &earlyQueryImpl{
Scenario: NewScenario("early-query", nil),
runtime: true,
}
)
type earlyQueryImpl struct {
scenario.Scenario
runtime bool
initialHeight int64
}
func (sc *earlyQueryImpl) Clone() scenario.Scenario {
return &earlyQueryImpl{
Scenario: sc.Scenario.Clone(),
runtime: sc.runtime,
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, _ *env.Env) error {
// Start the network.
var err error
if err = sc.Network().Start(); err != nil {
return err
}
var ctrl *oasis.Controller
switch sc.runtime {
case false:
ctrl = sc.Network().Controller()
case true:
// Use the compute worker node in the runtime scenario.
ctrl, err = oasis.NewController(sc.Network().ComputeWorkers()[0].SocketPath())
if err != nil {
return fmt.Errorf("failed to create controller for compute node: %w", err)
}
}
// Perform consensus queries.
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
// StateToGenesis.
_, err = ctrl.Consensus.StateToGenesis(ctx, consensus.HeightLatest)
if !errors.Is(err, consensus.ErrNoCommittedBlocks) {
return fmt.Errorf("StateToGenesis query should fail with ErrNoCommittedBlocks (got: %s)", err)
}
// GetBlock.
_, err = ctrl.Consensus.GetBlock(ctx, consensus.HeightLatest)
if !errors.Is(err, consensus.ErrNoCommittedBlocks) {
return fmt.Errorf("GetBlock query should fail with ErrNoCommittedBlocks (got: %s)", err)
}
// GetTransactions.
_, err = ctrl.Consensus.GetTransactions(ctx, consensus.HeightLatest)
if !errors.Is(err, consensus.ErrNoCommittedBlocks) {
return fmt.Errorf("GetTransactions query should fail with ErrNoCommittedBlocks (got: %s)", err)
}
// GetTransactionsWithResults.
_, err = ctrl.Consensus.GetTransactionsWithResults(ctx, consensus.HeightLatest)
if !errors.Is(err, consensus.ErrNoCommittedBlocks) {
return fmt.Errorf("GetTransactionsWithResults query should fail with ErrNoCommittedBlocks (got: %s)", err)
}
switch sc.runtime {
case false:
// GetStatus on validator.
status, err := ctrl.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")
}
case true:
// GetStatus on a compute node.
status, err := ctrl.GetStatus(ctx)
if err != nil {
return fmt.Errorf("failed to get status for compute node: %w", err)
}
fmt.Println(status)
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("compute node does report itself to be a validator at genesis")
}
if len(status.Runtimes) < 1 {
return fmt.Errorf("compute node status does not contain any runtimes")
}
}
return nil
}