/
host.go
154 lines (126 loc) · 3.98 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
package client
import (
"context"
"fmt"
"sync"
"github.com/oasisprotocol/oasis-core/go/common/logging"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
keymanagerClient "github.com/oasisprotocol/oasis-core/go/keymanager/client"
keymanagerClientApi "github.com/oasisprotocol/oasis-core/go/keymanager/client/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"
runtimeRegistry "github.com/oasisprotocol/oasis-core/go/runtime/registry"
scheduling "github.com/oasisprotocol/oasis-core/go/runtime/scheduling/api"
)
type clientHost struct {
sync.Mutex
*runtimeRegistry.RuntimeHostNode
runtime runtimeRegistry.Runtime
consensus consensus.Backend
keyManagerClient keymanagerClientApi.Client
stopCh chan struct{}
quitCh chan struct{}
logger *logging.Logger
}
// Start starts the client host.
func (h *clientHost) Start() error {
go h.worker()
return nil
}
// Stop asks the client host to stop.
func (h *clientHost) Stop() {
close(h.stopCh)
}
// Quit returns the channel which will signal when the client host has quit.
func (h *clientHost) Quit() <-chan struct{} {
return h.quitCh
}
// Implements runtimeRegistry.RuntimeHostHandlerFactory.
func (h *clientHost) GetRuntime() runtimeRegistry.Runtime {
return h.runtime
}
// Implements runtimeRegistry.RuntimeHostHandlerFactory.
func (h *clientHost) NewRuntimeHostHandler() protocol.Handler {
return runtimeRegistry.NewRuntimeHostHandler(h, h.runtime, h.consensus)
}
// Implements runtimeRegistry.RuntimeHostHandlerFactory.
func (h *clientHost) NewRuntimeHostNotifier(ctx context.Context, host host.Runtime) protocol.Notifier {
return runtimeRegistry.NewRuntimeHostNotifier(ctx, h.runtime, host, h.consensus)
}
// Implements runtimeRegistry.RuntimeHostHandlerEnvironment.
func (h *clientHost) GetCurrentBlock(ctx context.Context) (*block.Block, error) {
return nil, fmt.Errorf("not available")
}
// Implements runtimeRegistry.RuntimeHostHandlerEnvironment.
func (h *clientHost) GetKeyManagerClient(ctx context.Context) (keymanagerClientApi.Client, error) {
h.Lock()
defer h.Unlock()
// If any existing key manager client is available, just reuse it.
if h.keyManagerClient != nil {
return h.keyManagerClient, nil
}
// Otherwise create a fresh one on demand.
cliCtx, cancel := context.WithCancel(context.Background())
go func() {
<-h.stopCh
cancel()
}()
var err error
h.keyManagerClient, err = keymanagerClient.New(cliCtx, h.runtime, h.consensus, nil)
if err != nil {
h.logger.Error("failed to create key manager client instance",
"err", err,
)
return nil, fmt.Errorf("failed to create key manager client: %w", err)
}
return h.keyManagerClient, nil
}
func (h *clientHost) GetTxPool(ctx context.Context) (scheduling.Scheduler, error) {
return nil, fmt.Errorf("not available")
}
func (h *clientHost) worker() {
defer close(h.quitCh)
// Wait for consensus sync.
select {
case <-h.consensus.Synced():
case <-h.stopCh:
return
}
// Start hosted runtime provisioning.
hrt, _, err := h.ProvisionHostedRuntime(context.Background())
switch {
case err == nil:
default:
h.logger.Error("failed to provision hosted runtime",
"err", err,
)
return
}
// Start the runtime.
if err = hrt.Start(); err != nil {
h.logger.Error("failed to start hosted runtime",
"err", err,
)
return
}
// Wait for the stop signal.
<-h.stopCh
// Ask the runtime to stop as well.
hrt.Stop()
}
func newClientHost(runtime runtimeRegistry.Runtime, consensus consensus.Backend) (*clientHost, error) {
host := &clientHost{
runtime: runtime,
consensus: consensus,
stopCh: make(chan struct{}),
quitCh: make(chan struct{}),
logger: logging.GetLogger("runtime/client/host"),
}
var err error
host.RuntimeHostNode, err = runtimeRegistry.NewRuntimeHostNode(host)
if err != nil {
return nil, fmt.Errorf("failed to create runtime host helper: %w", err)
}
return host, nil
}