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

Update Rust protos, dependencies enhancements #2538

Merged
merged 8 commits into from
Nov 13, 2024
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
21 changes: 13 additions & 8 deletions v4-proto-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dydx-proto"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
description = "Compiled dYdX protobuf files"
repository = "https://github.com/dydxprotocol/v4-chain/tree/main/v4-proto-rs"
Expand All @@ -9,13 +9,18 @@ license = "LicenseRef-dYdX-Custom"
[lib]
doctest = false

[features]
default = ["grpc-transport"]
grpc-transport = ["tonic/transport"]

[dependencies]
cosmos-sdk-proto = "0.21.1"
tonic = { version = "0.11", features = ["tls", "tls-roots", "transport", "channel"] }
prost = "0.12"
prost-types = "0.12"
cosmos-sdk-proto = { version = "0.26", default-features = false, features = ["grpc"] }
prost = { version = "0.13", default-features = false }
prost-types = "0.13"
tonic = { version = "0.12", default-features = false, features = ["codegen", "prost"] }

[build-dependencies]
tonic-buf-build = "0.2.1"
prost-build = "0.12" # keep the version the same as in tonic-buf-build
tonic-build = "0.11" # keep the version the same as in tonic-buf-build
regex = "1.11.1"
tonic-buf-build = "0.3.0"
prost-build = "0.13" # keep the version the same as in tonic-buf-build
tonic-build = "0.12" # keep the version the same as in tonic-buf-build
2 changes: 1 addition & 1 deletion v4-proto-rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Cargo.toml

```toml
[dependencies]
dydx-proto = "0.1"
dydx-proto = "0.2"
```

*Note:* by default, rust stub files are not rebuilt (see Q&A below)
Expand Down
75 changes: 53 additions & 22 deletions v4-proto-rs/build.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
use prost_build::Config;
use regex::Regex;
use std::env;
use std::path::PathBuf;

fn main() -> Result<(), tonic_buf_build::error::TonicBufBuildError> {
if std::env::var("V4_PROTO_REBUILD").is_ok() {
let mut config = Config::new();
config.out_dir("src");
config.include_file("_includes.rs");
config.enable_type_names();
let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").map_err(|e| {
tonic_buf_build::error::TonicBufBuildError {
message: format!("Failed to get CARGO_MANIFEST_DIR: {}", e),
cause: None,
}
})?);
path.pop();
tonic_buf_build::compile_from_buf_workspace_with_config(
tonic_build::configure().build_server(false),
Some(config),
tonic_buf_build::TonicBufConfig {
buf_dir: Some(path),
},
)?;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

const OUT_DIR: &str = "src";

fn features_patch(dir: impl AsRef<Path>) -> io::Result<()> {
let regex = "impl(.+)tonic::transport(.+)";
let replacement = "#[cfg(feature = \"grpc-transport\")]\n \
impl${1}tonic::transport${2}";
Comment on lines +11 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider using a Rust parser instead of regex for code modification

Using regular expressions to modify Rust code can be fragile and may not handle all edge cases correctly. Consider using a Rust parsing library like syn or quote to parse and manipulate the code in a more robust way.


let paths = fs::read_dir(dir)?;

for entry in paths {
let path = entry?.path();
let mut contents = fs::read_to_string(&path)?;

contents = Regex::new(regex)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
.replace_all(&contents, replacement)
.to_string();

fs::write(&path, &contents)?
}

Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
if !std::env::var("V4_PROTO_REBUILD").is_ok() {
return Ok(());
}

let mut config = Config::new();
config.out_dir(OUT_DIR);
config.include_file("_includes.rs");
config.enable_type_names();
let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").map_err(|e| {
tonic_buf_build::error::TonicBufBuildError {
message: format!("Failed to get CARGO_MANIFEST_DIR: {}", e),
cause: None,
}
})?);
Comment on lines +41 to +46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Simplify error handling when retrieving CARGO_MANIFEST_DIR

Since the main function returns Result<(), Box<dyn std::error::Error>>, you can directly use the ? operator without custom error mapping.

Apply this diff to simplify error handling:

-let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").map_err(|e| {
-    tonic_buf_build::error::TonicBufBuildError {
-        message: format!("Failed to get CARGO_MANIFEST_DIR: {}", e),
-        cause: None,
-    }
-})?);
+let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").map_err(|e| {
tonic_buf_build::error::TonicBufBuildError {
message: format!("Failed to get CARGO_MANIFEST_DIR: {}", e),
cause: None,
}
})?);
let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);

path.pop();
tonic_buf_build::compile_from_buf_workspace_with_config(
tonic_build::configure().build_server(false),
Some(config),
tonic_buf_build::TonicBufConfig {
buf_dir: Some(path),
},
)?;

features_patch(OUT_DIR)?;

Ok(())
}
12 changes: 12 additions & 0 deletions v4-proto-rs/src/_includes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub mod cosmos_proto {
include!("cosmos_proto.rs");
}
pub mod dydxprotocol {
pub mod accountplus {
include!("dydxprotocol.accountplus.rs");
}
pub mod affiliates {
include!("dydxprotocol.affiliates.rs");
}
pub mod assets {
include!("dydxprotocol.assets.rs");
}
Expand Down Expand Up @@ -75,6 +81,9 @@ pub mod dydxprotocol {
include!("dydxprotocol.indexer.socks.rs");
}
}
pub mod listing {
include!("dydxprotocol.listing.rs");
}
pub mod perpetuals {
include!("dydxprotocol.perpetuals.rs");
}
Expand All @@ -84,6 +93,9 @@ pub mod dydxprotocol {
pub mod ratelimit {
include!("dydxprotocol.ratelimit.rs");
}
pub mod revshare {
include!("dydxprotocol.revshare.rs");
}
pub mod rewards {
include!("dydxprotocol.rewards.rs");
}
Expand Down
2 changes: 0 additions & 2 deletions v4-proto-rs/src/cosmos.base.query.v1beta1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/// Foo some_parameter = 1;
/// PageRequest pagination = 2;
/// }
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct PageRequest {
/// key is a value returned in PageResponse.next_key to begin
Expand Down Expand Up @@ -52,7 +51,6 @@ impl ::prost::Name for PageRequest {
/// repeated Bar results = 1;
/// PageResponse page = 2;
/// }
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct PageResponse {
/// next_key is the key to be passed to PageRequest.key to
Expand Down
4 changes: 0 additions & 4 deletions v4-proto-rs/src/cosmos.base.v1beta1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
///
/// NOTE: The amount field is an Int which implements the custom method
/// signatures required by gogoproto.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Coin {
#[prost(string, tag = "1")]
Expand All @@ -25,7 +24,6 @@ impl ::prost::Name for Coin {
///
/// NOTE: The amount field is an Dec which implements the custom method
/// signatures required by gogoproto.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct DecCoin {
#[prost(string, tag = "1")]
Expand All @@ -45,7 +43,6 @@ impl ::prost::Name for DecCoin {
}
/// IntProto defines a Protobuf wrapper around an Int object.
/// Deprecated: Prefer to use math.Int directly. It supports binary Marshal and Unmarshal.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct IntProto {
#[prost(string, tag = "1")]
Expand All @@ -63,7 +60,6 @@ impl ::prost::Name for IntProto {
}
/// DecProto defines a Protobuf wrapper around a Dec object.
/// Deprecated: Prefer to use math.LegacyDec directly. It supports binary Marshal and Unmarshal.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct DecProto {
#[prost(string, tag = "1")]
Expand Down
8 changes: 3 additions & 5 deletions v4-proto-rs/src/cosmos_proto.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// This file is @generated by prost-build.
/// InterfaceDescriptor describes an interface type to be used with
/// accepts_interface and implements_interface and declared by declare_interface.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct InterfaceDescriptor {
/// name is the name of the interface. It should be a short-name (without
Expand Down Expand Up @@ -32,7 +31,6 @@ impl ::prost::Name for InterfaceDescriptor {
/// Scalars should ideally define an encoding such that there is only one
/// valid syntactical representation for a given semantic meaning,
/// i.e. the encoding should be deterministic.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ScalarDescriptor {
/// name is the name of the scalar. It should be a short-name (without
Expand Down Expand Up @@ -77,9 +75,9 @@ impl ScalarType {
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
pub fn as_str_name(&self) -> &'static str {
match self {
ScalarType::Unspecified => "SCALAR_TYPE_UNSPECIFIED",
ScalarType::String => "SCALAR_TYPE_STRING",
ScalarType::Bytes => "SCALAR_TYPE_BYTES",
Self::Unspecified => "SCALAR_TYPE_UNSPECIFIED",
Self::String => "SCALAR_TYPE_STRING",
Self::Bytes => "SCALAR_TYPE_BYTES",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
Expand Down
Loading
Loading