Skip to content

Commit

Permalink
chore: dapi-grpc wasm feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Dec 19, 2024
1 parent 7475b09 commit 068a6fb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .cargo/config-release.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ rustflags = ["-C", "target-feature=-crt-static"]

[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"

[target.wasm32-unknown-unknown]
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ rustflags = ["-C", "target-feature=-crt-static", "--cfg", "tokio_unstable"]
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"

[target.wasm32-unknown-unknown]
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']

[build]
rustflags = ["--cfg", "tokio_unstable"]
25 changes: 20 additions & 5 deletions packages/dapi-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,37 @@ platform = []
# Re-export Dash Platform protobuf types as `dapi_grpc::platform::proto`
# Note: client needs tls and tls-roots to connect to testnet which uses TLS.
tenderdash-proto = []

# Client support.
client = [
"tonic/channel",
"tonic/transport",
"tonic/tls",
"tonic/tls-roots",
"tonic/tls-webpki-roots",
"platform",
"tenderdash-proto/client",
]
server = ["tonic/channel", "tonic/transport", "platform"]
serde = ["dep:serde", "dep:serde_bytes"]

# Specialized features for building the client in a wasm environment.
# Conflicts with `client` and `server` feature.
wasm = ["tenderdash-proto/client"]

# Build tonic server code. Includes all client features and adds server-specific dependencies.
server = [
"tonic/channel",
"tonic/transport",
"platform",
"tenderdash-proto/server",
"client",
]

serde = ["dep:serde", "dep:serde_bytes", "tenderdash-proto/serde"]
mocks = ["serde", "dep:serde_json"]

[dependencies]
tenderdash-proto = { path = "../../../rs-tenderdash-abci/proto", default-features = false }

prost = { version = "0.13" }
futures-core = "0.3.30"
tonic = { version = "0.12.3", features = [
Expand All @@ -42,9 +60,6 @@ tonic = { version = "0.12.3", features = [
serde = { version = "1.0.197", optional = true, features = ["derive"] }
serde_bytes = { version = "0.11.12", optional = true }
serde_json = { version = "1.0", optional = true }
tenderdash-proto = { git = "https://github.com/dashpay/rs-tenderdash-abci", version = "1.2.1", tag = "v1.2.1+1.3.0", default-features = false, features = [
"grpc",
] }
dapi-grpc-macros = { path = "../rs-dapi-grpc-macros" }
platform-version = { path = "../rs-platform-version" }

Expand Down
70 changes: 52 additions & 18 deletions packages/dapi-grpc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{
collections::HashSet,
fs::{create_dir_all, remove_dir_all},
path::PathBuf,
process::exit,
};

use tonic_build::Builder;
Expand All @@ -14,9 +13,19 @@ const SERDE_WITH_STRING: &str =
r#"#[cfg_attr(feature = "serde", serde(with = "crate::deserialization::from_to_string"))]"#;

fn main() {
#[cfg(feature = "server")]
generate_code(ImplType::Server);
#[cfg(feature = "client")]
generate_code(ImplType::Client);
#[cfg(feature = "wasm")]
generate_code(ImplType::Wasm);
}

fn generate_code(typ: ImplType) {
let core = MappingConfig::new(
PathBuf::from("protos/core/v0/core.proto"),
PathBuf::from("src/core"),
&typ,
);

configure_core(core)
Expand All @@ -26,6 +35,7 @@ fn main() {
let platform = MappingConfig::new(
PathBuf::from("protos/platform/v0/platform.proto"),
PathBuf::from("src/platform"),
&typ,
);

configure_platform(platform)
Expand Down Expand Up @@ -210,6 +220,43 @@ fn configure_core(core: MappingConfig) -> MappingConfig {
core
}

#[allow(unused)]
enum ImplType {
Server,
Client,
Wasm,
}

impl ImplType {
// Configure the builder based on the implementation type.
pub fn configure(&self, builder: Builder) -> Builder {
match self {
Self::Server => builder
.build_client(true)
.build_server(true)
.build_transport(true),
Self::Client => builder
.build_client(true)
.build_server(false)
.build_transport(true),
Self::Wasm => builder
.build_client(true)
.build_server(false)
.build_transport(false),
}
}

/// Get the directory name for the implementation type.
fn dirname(&self) -> String {
match self {
Self::Server => "server",
Self::Client => "client",
Self::Wasm => "wasm",
}
.to_string()
}
}

impl MappingConfig {
/// Create a new MappingConfig instance.
///
Expand All @@ -220,31 +267,18 @@ impl MappingConfig {
///
/// Depending on the features, either `client`, `server` or `client_server` subdirectory
/// will be created inside `out_dir`.
fn new(protobuf_file: PathBuf, out_dir: PathBuf) -> Self {
fn new(protobuf_file: PathBuf, out_dir: PathBuf, typ: &ImplType) -> Self {
let protobuf_file = abs_path(&protobuf_file);

let build_server = cfg!(feature = "server");
let build_client = cfg!(feature = "client");

// Depending on the features, we need to build the server, client or both.
// We save these artifacts in separate directories to avoid overwriting the generated files
// when another crate requires different features.
let out_dir_suffix = match (build_server, build_client) {
(true, true) => "client_server",
(true, false) => "server",
(false, true) => "client",
(false, false) => {
println!("WARNING: At least one of the features 'server' or 'client' must be enabled; dapi-grpc will not generate any files.");
exit(0)
}
};
let out_dir_suffix = typ.dirname();

let out_dir = abs_path(&out_dir.join(out_dir_suffix));

let builder = tonic_build::configure()
.build_server(build_server)
.build_client(build_client)
.build_transport(build_server || build_client)
let builder = typ
.configure(tonic_build::configure())
.out_dir(out_dir.clone())
.protoc_arg("--experimental_allow_proto3_optional");

Expand Down
17 changes: 9 additions & 8 deletions packages/dapi-grpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@ pub use prost::Message;
pub mod core {
#![allow(non_camel_case_types)]
pub mod v0 {
#[cfg(all(feature = "server", not(feature = "client")))]
// Note: only one of the features can be analyzed at a time
#[cfg(feature = "server")]
include!("core/server/org.dash.platform.dapi.v0.rs");

#[cfg(all(feature = "client", not(feature = "server")))]
include!("core/client/org.dash.platform.dapi.v0.rs");

#[cfg(all(feature = "server", feature = "client"))]
include!("core/client_server/org.dash.platform.dapi.v0.rs");
#[cfg(all(feature = "wasm", not(any(feature = "server", feature = "client"))))]
include!("core/wasm/org.dash.platform.dapi.v0.rs");
}
}

#[cfg(feature = "platform")]
pub mod platform {
pub mod v0 {
#[cfg(all(feature = "server", not(feature = "client")))]
#[cfg(feature = "server")]
include!("platform/server/org.dash.platform.dapi.v0.rs");

#[cfg(all(feature = "client", not(feature = "server")))]
include!("platform/client/org.dash.platform.dapi.v0.rs");

#[cfg(all(feature = "server", feature = "client"))]
include!("platform/client_server/org.dash.platform.dapi.v0.rs");
#[cfg(all(feature = "wasm", not(any(feature = "server", feature = "client"))))]
include!("platform/wasm/org.dash.platform.dapi.v0.rs");
}

#[cfg(feature = "tenderdash-proto")]
pub use tenderdash_proto as proto;

#[cfg(any(feature = "server", feature = "client"))]
#[cfg(any(feature = "server", feature = "client", feature = "wasm"))]
mod versioning;
#[cfg(any(feature = "server", feature = "client"))]
#[cfg(any(feature = "server", feature = "client", feature = "wasm"))]
pub use versioning::{VersionedGrpcMessage, VersionedGrpcResponse};
}

Expand Down

0 comments on commit 068a6fb

Please sign in to comment.