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

sui-network: manually define rpc service #1844

Merged
merged 2 commits into from
May 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
}
}
68 changes: 68 additions & 0 deletions crates/sui-network/src/codec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

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