Skip to content

Commit 1ef06f4

Browse files
authored
Merge pull request #2 from ava-labs/listen-ip
plugin: listen on IP address
2 parents 59fd1e1 + 0a52f01 commit 1ef06f4

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
Mini key-value store VM in Rust for Avalanche
77

8+
## Rust Version
9+
10+
`mini-kvvm-rs` currently works on Rust `1.60+` and requires support for the `2021` edition.
11+
812
```bash
913
cd ${HOME}/go/src/github.com/ava-labs/subnet-cli
1014
go install -v .

src/plugin.rs

+12-28
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use std::{
2-
io::{self, Error, ErrorKind},
3-
path::Path,
4-
};
1+
use std::io::{self, Error, ErrorKind};
52

63
use crate::vm::vm_server::{Vm, VmServer};
74
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;
107
use tonic::transport::{server::NamedService, Server};
118
use tonic_health::server::health_reporter;
129

@@ -15,8 +12,6 @@ pub const PROTOCOL_VERSION: u8 = 12;
1512
pub const MAGIC_COOKIE_KEY: &str = "VM_PLUGIN";
1613
pub const MAGIC_COOKIE_VALUE: &str = "dynamic";
1714

18-
pub const UNIX_SOCKET_PATH: &str = "/var/run/mini-kvvm-rs.sock";
19-
2015
/// ref. https://github.com/ava-labs/avalanchego/blob/v1.7.10/vms/rpcchainvm/vm.go
2116
#[derive(Debug)]
2217
pub struct HandshakeConfig {
@@ -51,39 +46,28 @@ pub async fn serve<V>(vm: V, handshake_config: &HandshakeConfig) -> io::Result<(
5146
where
5247
V: Vm,
5348
{
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-
6349
// "go-plugin requires the gRPC Health Checking Service to be registered on your server"
6450
// ref. https://github.com/hashicorp/go-plugin/blob/master/docs/guide-plugin-write-non-go.md
6551
// ref. https://github.com/hyperium/tonic/blob/v0.7.1/examples/src/health/server.rs
6652
let (mut health_reporter, health_svc) = health_reporter();
6753
health_reporter.set_serving::<Plugin>().await;
6854

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+
6962
// 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);
7464
info!("handshake message: {}", handshake_msg);
7565
println!("{}", handshake_msg);
7666

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-
8367
Server::builder()
8468
.add_service(health_svc)
8569
.add_service(VmServer::new(vm))
86-
.serve_with_incoming(UnixListenerStream::new(listener))
70+
.serve_with_incoming(TcpListenerStream::new(listener))
8771
.await
8872
.map_err(|e| {
8973
Error::new(

0 commit comments

Comments
 (0)