-
Notifications
You must be signed in to change notification settings - Fork 665
/
factory.go
84 lines (73 loc) · 2.1 KB
/
factory.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package rpcchainvm
import (
"errors"
"io/ioutil"
"log"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/subprocess"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
)
var errWrongVM = errors.New("wrong vm type")
type Factory struct {
Path string
}
func (f *Factory) New(ctx *snow.Context) (interface{}, error) {
// Ignore warning from launching an executable with a variable command
// because the command is a controlled and required input
config := &plugin.ClientConfig{
HandshakeConfig: Handshake,
Plugins: PluginMap,
Cmd: subprocess.New(f.Path),
AllowedProtocols: []plugin.Protocol{
plugin.ProtocolNetRPC,
plugin.ProtocolGRPC,
},
// We kill this client by calling kill() when the chain running this VM
// shuts down. However, there are some cases where the VM's Shutdown
// method is not called. Namely, if:
// 1) The node shuts down after the client is created but before the
// chain is registered with the message router.
// 2) The chain doesn't handle a shutdown message before the node times
// out on the chain's shutdown and dies, leaving the shutdown message
// unhandled.
// We set managed to true so that we can call plugin.CleanupClients on
// node shutdown to ensure every plugin subprocess is killed.
Managed: true,
}
if ctx != nil {
log.SetOutput(ctx.Log)
config.Stderr = ctx.Log
config.Logger = hclog.New(&hclog.LoggerOptions{
Output: ctx.Log,
Level: hclog.Info,
})
} else {
log.SetOutput(ioutil.Discard)
config.Stderr = ioutil.Discard
config.Logger = hclog.New(&hclog.LoggerOptions{
Output: ioutil.Discard,
})
}
client := plugin.NewClient(config)
rpcClient, err := client.Client()
if err != nil {
client.Kill()
return nil, err
}
raw, err := rpcClient.Dispense("vm")
if err != nil {
client.Kill()
return nil, err
}
vm, ok := raw.(*VMClient)
if !ok {
client.Kill()
return nil, errWrongVM
}
vm.SetProcess(client)
vm.ctx = ctx
return vm, nil
}