Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin: listen on IP address #2

Merged
merged 3 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

Mini key-value store VM in Rust for Avalanche

## Rust Version

`mini-kvvm-rs` currently works on Rust `1.60+` and requires support for the `2021` edition.

```bash
cd ${HOME}/go/src/github.com/ava-labs/subnet-cli
go install -v .
Expand Down
40 changes: 12 additions & 28 deletions src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::{
io::{self, Error, ErrorKind},
path::Path,
};
use std::io::{self, Error, ErrorKind};

use crate::vm::vm_server::{Vm, VmServer};
use log::info;
use tokio::{fs::create_dir_all, net::UnixListener};
use tokio_stream::wrappers::UnixListenerStream;
use tokio::net::TcpListener;
use tokio_stream::wrappers::TcpListenerStream;
use tonic::transport::{server::NamedService, Server};
use tonic_health::server::health_reporter;

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

pub const UNIX_SOCKET_PATH: &str = "/var/run/mini-kvvm-rs.sock";

/// ref. https://github.com/ava-labs/avalanchego/blob/v1.7.10/vms/rpcchainvm/vm.go
#[derive(Debug)]
pub struct HandshakeConfig {
Expand Down Expand Up @@ -51,39 +46,28 @@ pub async fn serve<V>(vm: V, handshake_config: &HandshakeConfig) -> io::Result<(
where
V: Vm,
{
create_dir_all(Path::new(UNIX_SOCKET_PATH).parent().unwrap())
.await
.map_err(|e| {
Error::new(
ErrorKind::Other,
format!("failed tokio::fs::create_dir_all '{}'", e),
)
})?;

// "go-plugin requires the gRPC Health Checking Service to be registered on your server"
// ref. https://github.com/hashicorp/go-plugin/blob/master/docs/guide-plugin-write-non-go.md
// ref. https://github.com/hyperium/tonic/blob/v0.7.1/examples/src/health/server.rs
let (mut health_reporter, health_svc) = health_reporter();
health_reporter.set_serving::<Plugin>().await;

// TODO: Add support for abstract unix sockets once supported by tonic.
// ref. https://github.com/hyperium/tonic/issues/966
// avalanchego currently only supports plugins listening on IP address.
let listener = TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
info!("plugin listening on address {:?}", addr);

// ref. https://github.com/hashicorp/go-plugin/blob/master/docs/guide-plugin-write-non-go.md#4-output-handshake-information
let handshake_msg = format!(
"1|{}|unix|{}|grpc|",
handshake_config.protocol_version, UNIX_SOCKET_PATH,
);
let handshake_msg = format!("1|{}|tcp|{}|grpc|", handshake_config.protocol_version, addr);
info!("handshake message: {}", handshake_msg);
println!("{}", handshake_msg);

// ref. https://github.com/hyperium/tonic/blob/v0.7.1/examples/src/uds/server.rs
let listener = UnixListener::bind(UNIX_SOCKET_PATH)?;

let socket_addr = listener.local_addr()?;
info!("plugin listening on socket address {:?}", socket_addr);

Server::builder()
.add_service(health_svc)
.add_service(VmServer::new(vm))
.serve_with_incoming(UnixListenerStream::new(listener))
.serve_with_incoming(TcpListenerStream::new(listener))
.await
.map_err(|e| {
Error::new(
Expand Down