forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sui-network: manually define rpc service
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
Showing
13 changed files
with
280 additions
and
348 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.