-
Notifications
You must be signed in to change notification settings - Fork 659
/
vm.go
74 lines (59 loc) · 1.99 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (C) 2019-2022, 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/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/grpcutils"
vmpb "github.com/ava-labs/avalanchego/proto/pb/vm"
)
// protocolVersion should be bumped anytime changes are made which require
// the plugin vm to upgrade to latest avalanchego release to be compatible.
const protocolVersion = 16
var (
// Handshake is a common handshake that is shared by plugin and host.
Handshake = plugin.HandshakeConfig{
ProtocolVersion: protocolVersion,
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(_ *plugin.GRPCBroker, s *grpc.Server) error {
vmpb.RegisterVMServer(s, NewServer(p.vm))
return nil
}
// GRPCClient returns a new GRPC client
func (p *vmPlugin) GRPCClient(ctx context.Context, _ *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return NewClient(vmpb.NewVMClient(c)), nil
}
// Serve serves a ChainVM plugin using sane gRPC server defaults.
func Serve(vm block.ChainVM) {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: Handshake,
Plugins: map[string]plugin.Plugin{
"vm": New(vm),
},
// ensure proper defaults
GRPCServer: grpcutils.NewDefaultServer,
})
}