/
consensus_state_sync.go
170 lines (144 loc) · 4.72 KB
/
consensus_state_sync.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
package e2e
import (
"context"
"fmt"
"time"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
control "github.com/oasisprotocol/oasis-core/go/control/api"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/env"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/log"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/oasis"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/scenario"
)
// ConsensusStateSync is the consensus state sync scenario.
var ConsensusStateSync scenario.Scenario = &consensusStateSyncImpl{
E2E: *NewE2E("consensus-state-sync"),
}
type consensusStateSyncImpl struct {
E2E
}
func (sc *consensusStateSyncImpl) Clone() scenario.Scenario {
return &consensusStateSyncImpl{
E2E: sc.E2E.Clone(),
}
}
func (sc *consensusStateSyncImpl) Fixture() (*oasis.NetworkFixture, error) {
f, err := sc.E2E.Fixture()
if err != nil {
return nil, err
}
f.Network.SetInsecureBeacon()
// Enable checkpoints.
f.Network.Consensus.Parameters.StateCheckpointInterval = 10
f.Network.Consensus.Parameters.StateCheckpointNumKept = 100
f.Network.Consensus.Parameters.StateCheckpointChunkSize = 1024 * 1024
// Add an extra validator.
f.Validators = append(f.Validators,
oasis.ValidatorFixture{
NodeFixture: oasis.NodeFixture{
NoAutoStart: true,
},
Entity: 1,
Consensus: oasis.ConsensusFixture{EnableConsensusRPCWorker: true},
LogWatcherHandlerFactories: []log.WatcherHandlerFactory{
oasis.LogEventABCIStateSyncComplete(),
},
},
)
return f, nil
}
func (sc *consensusStateSyncImpl) Run(childEnv *env.Env) error {
if err := sc.Net.Start(); err != nil {
return err
}
sc.Logger.Info("waiting for network to come up")
ctx := context.Background()
if err := sc.Net.Controller().WaitNodesRegistered(ctx, len(sc.Net.Validators())-1); err != nil {
return err
}
// Let the network run for 50 blocks. This should generate some checkpoints.
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 blk *consensus.Block
for {
select {
case blk = <-blockCh:
if blk.Height < 50 {
continue
}
case <-time.After(30 * time.Second):
return fmt.Errorf("timed out waiting for blocks")
}
break
}
sc.Logger.Info("got some blocks, starting the validator that needs to sync",
"trust_height", blk.Height,
"trust_hash", blk.Hash.Hex(),
)
// The last validator configured by the fixture is the one that is stopped and will sync.
lastValidator := len(sc.Net.Validators()) - 1
// Get the TLS public key from the validators (all except the last one).
var consensusNodes []string
for _, v := range sc.Net.Validators()[:lastValidator] {
var ctrl *oasis.Controller
ctrl, err = oasis.NewController(v.SocketPath())
if err != nil {
return fmt.Errorf("failed to create controller for validator %s: %w", v.Name, err)
}
var status *control.Status
status, err = ctrl.GetStatus(ctx)
if err != nil {
return fmt.Errorf("failed to get status for validator %s: %w", v.Name, err)
}
if status.Registration.Descriptor == nil {
return fmt.Errorf("validator %s has not registered", v.Name)
}
if len(status.Registration.Descriptor.TLS.Addresses) == 0 {
return fmt.Errorf("validator %s has no TLS addresses", v.Name)
}
var rawAddress []byte
tlsAddress := status.Registration.Descriptor.TLS.Addresses[0]
rawAddress, err = tlsAddress.MarshalText()
if err != nil {
return fmt.Errorf("failed to marshal TLS address: %w", err)
}
consensusNodes = append(consensusNodes, string(rawAddress))
}
// Configure state sync for the consensus validator.
val := sc.Net.Validators()[lastValidator]
val.SetConsensusStateSync(&oasis.ConsensusStateSyncCfg{
ConsensusNodes: consensusNodes,
TrustHeight: uint64(blk.Height),
TrustHash: blk.Hash.Hex(),
})
if err = val.Start(); err != nil {
return fmt.Errorf("failed to start validator: %w", err)
}
// Wait for the validator to finish syncing.
sc.Logger.Info("waiting for the validator to sync")
valCtrl, err := oasis.NewController(val.SocketPath())
if err != nil {
return err
}
if err = valCtrl.WaitSync(ctx); err != nil {
return err
}
// Query the validator status.
ctrl, err := oasis.NewController(val.SocketPath())
if err != nil {
return fmt.Errorf("failed to create controller for validator %s: %w", val.Name, err)
}
status, err := ctrl.GetStatus(ctx)
if err != nil {
return fmt.Errorf("failed to fetch validator status: %w", err)
}
// Make sure that the last retained height has been set correctly.
if lrh := status.Consensus.LastRetainedHeight; lrh < 20 {
return fmt.Errorf("unexpected last retained height from state synced node (got: %d)", lrh)
}
return nil
}