1
- use std:: {
2
- io:: { self , Error , ErrorKind } ,
3
- path:: Path ,
4
- } ;
1
+ use std:: io:: { self , Error , ErrorKind } ;
5
2
6
3
use crate :: vm:: vm_server:: { Vm , VmServer } ;
7
4
use log:: info;
8
- use tokio:: { fs :: create_dir_all , net:: UnixListener } ;
9
- use tokio_stream:: wrappers:: UnixListenerStream ;
5
+ use tokio:: net:: TcpListener ;
6
+ use tokio_stream:: wrappers:: TcpListenerStream ;
10
7
use tonic:: transport:: { server:: NamedService , Server } ;
11
8
use tonic_health:: server:: health_reporter;
12
9
@@ -15,8 +12,6 @@ pub const PROTOCOL_VERSION: u8 = 12;
15
12
pub const MAGIC_COOKIE_KEY : & str = "VM_PLUGIN" ;
16
13
pub const MAGIC_COOKIE_VALUE : & str = "dynamic" ;
17
14
18
- pub const UNIX_SOCKET_PATH : & str = "/var/run/mini-kvvm-rs.sock" ;
19
-
20
15
/// ref. https://github.com/ava-labs/avalanchego/blob/v1.7.10/vms/rpcchainvm/vm.go
21
16
#[ derive( Debug ) ]
22
17
pub struct HandshakeConfig {
@@ -51,39 +46,28 @@ pub async fn serve<V>(vm: V, handshake_config: &HandshakeConfig) -> io::Result<(
51
46
where
52
47
V : Vm ,
53
48
{
54
- create_dir_all ( Path :: new ( UNIX_SOCKET_PATH ) . parent ( ) . unwrap ( ) )
55
- . await
56
- . map_err ( |e| {
57
- Error :: new (
58
- ErrorKind :: Other ,
59
- format ! ( "failed tokio::fs::create_dir_all '{}'" , e) ,
60
- )
61
- } ) ?;
62
-
63
49
// "go-plugin requires the gRPC Health Checking Service to be registered on your server"
64
50
// ref. https://github.com/hashicorp/go-plugin/blob/master/docs/guide-plugin-write-non-go.md
65
51
// ref. https://github.com/hyperium/tonic/blob/v0.7.1/examples/src/health/server.rs
66
52
let ( mut health_reporter, health_svc) = health_reporter ( ) ;
67
53
health_reporter. set_serving :: < Plugin > ( ) . await ;
68
54
55
+ // TODO: Add support for abstract unix sockets once supported by tonic.
56
+ // ref. https://github.com/hyperium/tonic/issues/966
57
+ // avalanchego currently only supports plugins listening on IP address.
58
+ let listener = TcpListener :: bind ( "127.0.0.1:0" ) . await ?;
59
+ let addr = listener. local_addr ( ) ?;
60
+ info ! ( "plugin listening on address {:?}" , addr) ;
61
+
69
62
// ref. https://github.com/hashicorp/go-plugin/blob/master/docs/guide-plugin-write-non-go.md#4-output-handshake-information
70
- let handshake_msg = format ! (
71
- "1|{}|unix|{}|grpc|" ,
72
- handshake_config. protocol_version, UNIX_SOCKET_PATH ,
73
- ) ;
63
+ let handshake_msg = format ! ( "1|{}|tcp|{}|grpc|" , handshake_config. protocol_version, addr) ;
74
64
info ! ( "handshake message: {}" , handshake_msg) ;
75
65
println ! ( "{}" , handshake_msg) ;
76
66
77
- // ref. https://github.com/hyperium/tonic/blob/v0.7.1/examples/src/uds/server.rs
78
- let listener = UnixListener :: bind ( UNIX_SOCKET_PATH ) ?;
79
-
80
- let socket_addr = listener. local_addr ( ) ?;
81
- info ! ( "plugin listening on socket address {:?}" , socket_addr) ;
82
-
83
67
Server :: builder ( )
84
68
. add_service ( health_svc)
85
69
. add_service ( VmServer :: new ( vm) )
86
- . serve_with_incoming ( UnixListenerStream :: new ( listener) )
70
+ . serve_with_incoming ( TcpListenerStream :: new ( listener) )
87
71
. await
88
72
. map_err ( |e| {
89
73
Error :: new (
0 commit comments