mirror of https://github.com/poanetwork/gecko.git
71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package rpcchainvm
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/ava-labs/gecko/snow"
|
|
"github.com/hashicorp/go-hclog"
|
|
"github.com/hashicorp/go-plugin"
|
|
"io/ioutil"
|
|
"log"
|
|
"os/exec"
|
|
)
|
|
|
|
var (
|
|
errWrongVM = errors.New("wrong vm type")
|
|
)
|
|
|
|
// Factory ...
|
|
type Factory struct{ Path string }
|
|
|
|
// New ...
|
|
func (f *Factory) New(ctx *snow.Context) (interface{}, error) {
|
|
config := &plugin.ClientConfig{
|
|
HandshakeConfig: Handshake,
|
|
Plugins: PluginMap,
|
|
Cmd: exec.Command("sh", "-c", f.Path),
|
|
AllowedProtocols: []plugin.Protocol{
|
|
plugin.ProtocolNetRPC,
|
|
plugin.ProtocolGRPC,
|
|
},
|
|
}
|
|
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)
|
|
return vm, nil
|
|
}
|