Skip to content

Commit

Permalink
sui-network: manually define rpc service
Browse files Browse the repository at this point in the history
Manually define our Validator rpc service instead of relying on a .proto
file. This has the advantange that for now we can use types already
defined in Rust for wire messages instead of requiring proto definitions
or using the awkward double serialization we were currently using.
  • Loading branch information
bmwill committed May 6, 2022
1 parent 0d990f8 commit a0d21bf
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 348 deletions.
66 changes: 3 additions & 63 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/sui-network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tonic = "0.7"
prost = "0.10"
bincode = "1.3.3"
serde = "1.0.136"
sui-types = { path = "../../sui_types" }

[dev-dependencies]
tonic-build = { version = "0.7", features = [ "prost", "transport" ] }
prost-build = "0.10.1"
tonic-build = { git = "https://github.com/hyperium/tonic.git", rev = "de2e4ac077c076736dc451f3415ea7da1a61a560", default-features = false, features = [ "transport" ] }
2 changes: 1 addition & 1 deletion crates/sui-network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Changing an RPC service

The general process for changing an RPC service is as follows:
1. Change the corresponding `.proto` file in the `proto` directory
1. Change the service definition in the `tests/bootstrap.rs` file.
2. Run `cargo test --test bootstrap` to re-run the code generation.
Generated rust files are in the `src/generated` directory.
3. Update any other corresponding logic that would have been affected by
Expand Down
12 changes: 0 additions & 12 deletions crates/sui-network/proto/common.proto

This file was deleted.

19 changes: 0 additions & 19 deletions crates/sui-network/proto/validator.proto

This file was deleted.

24 changes: 1 addition & 23 deletions crates/sui-network/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#[path = "generated/sui.validator.rs"]
#[path = "generated/sui.validator.Validator.rs"]
#[rustfmt::skip]
mod validator;

#[path = "generated/sui.common.rs"]
#[rustfmt::skip]
mod common;

pub use common::BincodeEncodedPayload;
pub use validator::{
validator_client::ValidatorClient,
validator_server::{Validator, ValidatorServer},
};

impl BincodeEncodedPayload {
pub fn deserialize<T: serde::de::DeserializeOwned>(&self) -> Result<T, bincode::Error> {
bincode::deserialize(self.payload.as_ref())
}

pub fn try_from<T: serde::Serialize>(value: &T) -> Result<Self, bincode::Error> {
let payload = bincode::serialize(value)?.into();
Ok(Self { payload })
}
}

impl From<bytes::Bytes> for BincodeEncodedPayload {
fn from(payload: bytes::Bytes) -> Self {
Self { payload }
}
}
65 changes: 65 additions & 0 deletions crates/sui-network/src/codec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use bytes::{Buf, BufMut};
use std::marker::PhantomData;
use tonic::{
codec::{Codec, DecodeBuf, Decoder, EncodeBuf, Encoder},
Status,
};

#[derive(Debug)]
pub struct BincodeEncoder<T>(PhantomData<T>);

impl<T: serde::Serialize> Encoder for BincodeEncoder<T> {
type Item = T;
type Error = Status;

fn encode(&mut self, item: Self::Item, buf: &mut EncodeBuf<'_>) -> Result<(), Self::Error> {
bincode::serialize_into(buf.writer(), &item).map_err(|e| Status::internal(e.to_string()))
}
}

#[derive(Debug)]
pub struct BincodeDecoder<U>(PhantomData<U>);

impl<U: serde::de::DeserializeOwned> Decoder for BincodeDecoder<U> {
type Item = U;
type Error = Status;

fn decode(&mut self, buf: &mut DecodeBuf<'_>) -> Result<Option<Self::Item>, Self::Error> {
if !buf.has_remaining() {
return Ok(None);
}

let item: Self::Item =
bincode::deserialize_from(buf.reader()).map_err(|e| Status::internal(e.to_string()))?;
Ok(Some(item))
}
}

/// A [`Codec`] that implements `application/grpc+bincode` via the serde library.
#[derive(Debug, Clone)]
pub struct BincodeCodec<T, U>(PhantomData<(T, U)>);

impl<T, U> Default for BincodeCodec<T, U> {
fn default() -> Self {
Self(PhantomData)
}
}

impl<T, U> Codec for BincodeCodec<T, U>
where
T: serde::Serialize + Send + 'static,
U: serde::de::DeserializeOwned + Send + 'static,
{
type Encode = T;
type Decode = U;
type Encoder = BincodeEncoder<T>;
type Decoder = BincodeDecoder<U>;

fn encoder(&mut self) -> Self::Encoder {
BincodeEncoder(PhantomData)
}

fn decoder(&mut self) -> Self::Decoder {
BincodeDecoder(PhantomData)
}
}
9 changes: 0 additions & 9 deletions crates/sui-network/src/generated/sui.common.rs

This file was deleted.

Loading

0 comments on commit a0d21bf

Please sign in to comment.