/
host.go
421 lines (366 loc) · 11.7 KB
/
host.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
package registry
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/eapache/channels"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/logging"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
keymanagerApi "github.com/oasisprotocol/oasis-core/go/keymanager/api"
keymanagerClientApi "github.com/oasisprotocol/oasis-core/go/keymanager/client/api"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
"github.com/oasisprotocol/oasis-core/go/roothash/api/block"
"github.com/oasisprotocol/oasis-core/go/runtime/host"
"github.com/oasisprotocol/oasis-core/go/runtime/host/protocol"
storage "github.com/oasisprotocol/oasis-core/go/storage/api"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/syncer"
)
// notifyTimeout is the maximum time to wait for a notification to be processed by the runtime.
const notifyTimeout = 10 * time.Second
// RuntimeHostNode provides methods for nodes that need to host runtimes.
type RuntimeHostNode struct {
sync.Mutex
factory RuntimeHostHandlerFactory
notifier protocol.Notifier
runtime host.RichRuntime
runtimeNotify chan struct{}
}
// ProvisionHostedRuntime provisions the configured runtime.
//
// This method may return before the runtime is fully provisioned. The returned runtime will not be
// started automatically, you must call Start explicitly.
func (n *RuntimeHostNode) ProvisionHostedRuntime(ctx context.Context) (host.RichRuntime, protocol.Notifier, error) {
cfg, provisioner, err := n.factory.GetRuntime().Host(ctx)
if err != nil {
return nil, nil, fmt.Errorf("failed to get runtime host: %w", err)
}
cfg.MessageHandler = n.factory.NewRuntimeHostHandler()
// Provision the runtime.
prt, err := provisioner.NewRuntime(ctx, cfg)
if err != nil {
return nil, nil, fmt.Errorf("failed to provision runtime: %w", err)
}
notifier := n.factory.NewRuntimeHostNotifier(ctx, prt)
rr := host.NewRichRuntime(prt)
n.Lock()
n.runtime = rr
n.notifier = notifier
n.Unlock()
close(n.runtimeNotify)
return rr, notifier, nil
}
// GetHostedRuntime returns the provisioned hosted runtime (if any).
func (n *RuntimeHostNode) GetHostedRuntime() host.RichRuntime {
n.Lock()
rt := n.runtime
n.Unlock()
return rt
}
// WaitHostedRuntime waits for the hosted runtime to be provisioned and returns it.
func (n *RuntimeHostNode) WaitHostedRuntime(ctx context.Context) (host.RichRuntime, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-n.runtimeNotify:
}
return n.GetHostedRuntime(), nil
}
// RuntimeHostHandlerFactory is an interface that can be used to create new runtime handlers and
// notifiers when provisioning hosted runtimes.
type RuntimeHostHandlerFactory interface {
// GetRuntime returns the registered runtime for which a runtime host handler is to be created.
GetRuntime() Runtime
// NewRuntimeHostHandler creates a new runtime host handler.
NewRuntimeHostHandler() protocol.Handler
// NewRuntimeHostNotifier creates a new runtime host notifier.
NewRuntimeHostNotifier(ctx context.Context, host host.Runtime) protocol.Notifier
}
// NewRuntimeHostNode creates a new runtime host node.
func NewRuntimeHostNode(factory RuntimeHostHandlerFactory) (*RuntimeHostNode, error) {
return &RuntimeHostNode{
factory: factory,
runtimeNotify: make(chan struct{}),
}, nil
}
var (
errMethodNotSupported = errors.New("method not supported")
errEndpointNotSupported = errors.New("endpoint not supported")
)
// RuntimeHostHandlerEnvironment is the host environment interface.
type RuntimeHostHandlerEnvironment interface {
// GetCurrentBlock returns the most recent runtime block.
GetCurrentBlock(ctx context.Context) (*block.Block, error)
// GetKeyManagerClient returns the key manager client for this runtime.
GetKeyManagerClient(ctx context.Context) (keymanagerClientApi.Client, error)
}
// RuntimeHostHandler is a runtime host handler suitable for compute runtimes. It provides the
// required set of methods for interacting with the outside world.
type runtimeHostHandler struct {
env RuntimeHostHandlerEnvironment
runtime Runtime
consensus consensus.Backend
}
// Implements protocol.Handler.
func (h *runtimeHostHandler) Handle(ctx context.Context, body *protocol.Body) (*protocol.Body, error) {
// RPC.
if body.HostRPCCallRequest != nil {
switch body.HostRPCCallRequest.Endpoint {
case keymanagerApi.EnclaveRPCEndpoint:
// Call into the remote key manager.
kmCli, err := h.env.GetKeyManagerClient(ctx)
if err != nil {
return nil, err
}
res, err := kmCli.CallRemote(ctx, body.HostRPCCallRequest.Request)
if err != nil {
return nil, err
}
return &protocol.Body{HostRPCCallResponse: &protocol.HostRPCCallResponse{
Response: cbor.FixSliceForSerde(res),
}}, nil
default:
return nil, errEndpointNotSupported
}
}
// Storage.
if body.HostStorageSyncRequest != nil {
rq := body.HostStorageSyncRequest
var rs syncer.ReadSyncer
switch rq.Endpoint {
case protocol.HostStorageEndpointRuntime:
// Runtime storage.
rs = h.runtime.Storage()
// Prioritize nodes that signed the last storage receipts.
if blk, _ := h.env.GetCurrentBlock(ctx); blk != nil {
ctx = storage.WithNodePriorityHintFromSignatures(ctx, blk.Header.StorageSignatures)
}
case protocol.HostStorageEndpointConsensus:
// Consensus state storage.
rs = h.consensus.State()
default:
return nil, errEndpointNotSupported
}
var rsp *storage.ProofResponse
var err error
switch {
case rq.SyncGet != nil:
rsp, err = rs.SyncGet(ctx, rq.SyncGet)
case rq.SyncGetPrefixes != nil:
rsp, err = rs.SyncGetPrefixes(ctx, rq.SyncGetPrefixes)
case rq.SyncIterate != nil:
rsp, err = rs.SyncIterate(ctx, rq.SyncIterate)
default:
return nil, errMethodNotSupported
}
if err != nil {
return nil, err
}
return &protocol.Body{HostStorageSyncResponse: &protocol.HostStorageSyncResponse{ProofResponse: rsp}}, nil
}
// Local storage.
if body.HostLocalStorageGetRequest != nil {
value, err := h.runtime.LocalStorage().Get(body.HostLocalStorageGetRequest.Key)
if err != nil {
return nil, err
}
return &protocol.Body{HostLocalStorageGetResponse: &protocol.HostLocalStorageGetResponse{Value: value}}, nil
}
if body.HostLocalStorageSetRequest != nil {
if err := h.runtime.LocalStorage().Set(body.HostLocalStorageSetRequest.Key, body.HostLocalStorageSetRequest.Value); err != nil {
return nil, err
}
return &protocol.Body{HostLocalStorageSetResponse: &protocol.Empty{}}, nil
}
// Consensus light client.
if body.HostFetchConsensusBlockRequest != nil {
lb, err := h.consensus.GetLightBlock(ctx, int64(body.HostFetchConsensusBlockRequest.Height))
if err != nil {
return nil, err
}
return &protocol.Body{HostFetchConsensusBlockResponse: &protocol.HostFetchConsensusBlockResponse{
Block: *lb,
}}, nil
}
return nil, errMethodNotSupported
}
// runtimeHostNotifier is a runtime host notifier suitable for compute runtimes. It handles things
// like key manager policy updates.
type runtimeHostNotifier struct {
sync.Mutex
ctx context.Context
stopCh chan struct{}
started bool
runtime Runtime
host host.Runtime
consensus consensus.Backend
logger *logging.Logger
}
func (n *runtimeHostNotifier) watchPolicyUpdates() {
// Subscribe to runtime descriptor updates.
dscCh, dscSub, err := n.runtime.WatchRegistryDescriptor()
if err != nil {
n.logger.Error("failed to subscribe to registry descriptor updates",
"err", err,
)
return
}
defer dscSub.Close()
// Subscribe to key manager status updates.
stCh, stSub := n.consensus.KeyManager().WatchStatuses()
defer stSub.Close()
n.logger.Debug("watching policy updates")
var rtDsc *registry.Runtime
for {
var st *keymanagerApi.Status
select {
case <-n.ctx.Done():
n.logger.Debug("context canceled")
return
case <-n.stopCh:
n.logger.Debug("termination requested")
return
case rtDsc = <-dscCh:
n.logger.Debug("got registry descriptor update")
// Ignore updates if key manager is not needed.
if rtDsc.KeyManager == nil {
n.logger.Debug("no key manager needed for this runtime")
continue
}
// GetStatus(context.Context, *registry.NamespaceQuery) (*Status, error)
var err error
st, err = n.consensus.KeyManager().GetStatus(n.ctx, ®istry.NamespaceQuery{
Height: consensus.HeightLatest,
ID: *rtDsc.KeyManager,
})
if err != nil {
n.logger.Warn("failed to fetch key manager status",
"err", err,
)
continue
}
case st = <-stCh:
// Ignore status updates if key manager is not yet known (is nil)
// or if the status update is for a different key manager.
if rtDsc == nil || !st.ID.Equal(rtDsc.KeyManager) {
continue
}
}
// Update key manager policy.
n.logger.Debug("got policy update", "status", st)
raw := cbor.Marshal(st.Policy)
req := &protocol.Body{RuntimeKeyManagerPolicyUpdateRequest: &protocol.RuntimeKeyManagerPolicyUpdateRequest{
SignedPolicyRaw: raw,
}}
ctx, cancel := context.WithTimeout(n.ctx, notifyTimeout)
response, err := n.host.Call(ctx, req)
cancel()
if err != nil {
n.logger.Error("failed dispatching key manager policy update to runtime",
"err", err,
)
continue
}
n.logger.Debug("key manager policy updated dispatched", "response", response)
}
}
func (n *runtimeHostNotifier) watchConsensusLightBlocks() {
rawCh, sub, err := n.consensus.WatchBlocks(n.ctx)
if err != nil {
n.logger.Error("failed to subscribe to consensus block updates",
"err", err,
)
return
}
defer sub.Close()
// Create a ring channel with a capacity of one as we only care about the latest block.
blkCh := channels.NewRingChannel(channels.BufferCap(1))
go func() {
for blk := range rawCh {
blkCh.In() <- blk
}
blkCh.Close()
}()
n.logger.Debug("watching consensus layer blocks")
for {
select {
case <-n.ctx.Done():
n.logger.Debug("context canceled")
return
case <-n.stopCh:
n.logger.Debug("termination requested")
return
case rawBlk, ok := <-blkCh.Out():
if !ok {
return
}
blk := rawBlk.(*consensus.Block)
// Notify the runtime that a new consensus layer block is available.
req := &protocol.Body{RuntimeConsensusSyncRequest: &protocol.RuntimeConsensusSyncRequest{
Height: uint64(blk.Height),
}}
ctx, cancel := context.WithTimeout(n.ctx, notifyTimeout)
_, err = n.host.Call(ctx, req)
cancel()
if err != nil {
n.logger.Error("failed to notify runtime of a new consensus layer block",
"err", err,
"height", blk.Height,
)
continue
}
n.logger.Debug("runtime notified of new consensus layer block",
"height", blk.Height,
)
}
}
}
// Implements protocol.Notifier.
func (n *runtimeHostNotifier) Start() error {
n.Lock()
defer n.Unlock()
if n.started {
return nil
}
n.started = true
go n.watchPolicyUpdates()
go n.watchConsensusLightBlocks()
return nil
}
// Implements protocol.Notifier.
func (n *runtimeHostNotifier) Stop() {
close(n.stopCh)
}
// NewRuntimeHostNotifier returns a protocol notifier that handles key manager policy updates.
func NewRuntimeHostNotifier(
ctx context.Context,
runtime Runtime,
host host.Runtime,
consensus consensus.Backend,
) protocol.Notifier {
return &runtimeHostNotifier{
ctx: ctx,
stopCh: make(chan struct{}),
runtime: runtime,
host: host,
consensus: consensus,
logger: logging.GetLogger("runtime/registry/host"),
}
}
// NewRuntimeHostHandler returns a protocol handler that provides the required host methods for the
// runtime to interact with the outside world.
//
// The passed identity may be nil.
func NewRuntimeHostHandler(
env RuntimeHostHandlerEnvironment,
runtime Runtime,
consensus consensus.Backend,
) protocol.Handler {
return &runtimeHostHandler{
env: env,
runtime: runtime,
consensus: consensus,
}
}