-
Notifications
You must be signed in to change notification settings - Fork 649
/
vm.go
56 lines (44 loc) · 1.48 KB
/
vm.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package rpcchainvm
import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/hashicorp/go-plugin"
"github.com/ava-labs/avalanchego/api/proto/vmproto"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
)
var (
// Handshake is a common handshake that is shared by plugin and host.
Handshake = plugin.HandshakeConfig{
ProtocolVersion: 10,
MagicCookieKey: "VM_PLUGIN",
MagicCookieValue: "dynamic",
}
// PluginMap is the map of plugins we can dispense.
PluginMap = map[string]plugin.Plugin{
"vm": &vmPlugin{},
}
_ plugin.Plugin = &vmPlugin{}
_ plugin.GRPCPlugin = &vmPlugin{}
)
type vmPlugin struct {
plugin.NetRPCUnsupportedPlugin
// Concrete implementation, written in Go. This is only used for plugins
// that are written in Go.
vm block.ChainVM
}
// New will be called by the server side of the plugin to pass into the server
// side PluginMap for dispatching.
func New(vm block.ChainVM) plugin.Plugin {
return &vmPlugin{vm: vm}
}
// GRPCServer registers a new GRPC server.
func (p *vmPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
vmproto.RegisterVMServer(s, NewServer(p.vm, broker))
return nil
}
// GRPCClient returns a new GRPC client
func (p *vmPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return NewClient(vmproto.NewVMClient(c), broker), nil
}