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

feat: Distribute protobuf files through a new crate #391

Merged
merged 14 commits into from
Jun 25, 2024
Merged
4 changes: 4 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"bin/faucet",
"crates/block-producer",
"crates/proto",
"crates/rpc-proto",
"crates/rpc",
"crates/store",
"crates/utils",
Expand Down
2 changes: 1 addition & 1 deletion crates/proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "miden-node-proto"
version = "0.3.0"
description = "Miden RPC message definitions"
description = "Miden node message definitions (Store, Block Producer and RPC)"
readme = "README.md"
keywords = ["miden", "node", "protobuf", "rpc"]
edition.workspace = true
Expand Down
6 changes: 6 additions & 0 deletions crates/proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ use prost::Message;
fn main() -> miette::Result<()> {
// Compute the directory of the `proto` definitions
let cwd: PathBuf = env::current_dir().into_diagnostic()?;

let cwd = cwd
.parent()
.and_then(|p| p.parent())
.ok_or_else(|| miette::miette!("Failed to navigate two directories up"))?;

let proto_dir: PathBuf = cwd.join("proto");

// Compute the compiler's target file path.
Expand Down
17 changes: 17 additions & 0 deletions crates/rpc-proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "miden-rpc-proto"
version = "0.3.0"
description = "Miden node RPC message definitions"
readme = "README.md"
keywords = ["miden", "node", "protobuf", "rpc"]
bobbinth marked this conversation as resolved.
Show resolved Hide resolved
categories = ["no-std::no-alloc"]
edition.workspace = true
rust-version.workspace = true
license.workspace = true
authors.workspace = true
homepage.workspace = true
repository.workspace = true

[features]
default = ["std"]
std = []
9 changes: 9 additions & 0 deletions crates/rpc-proto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Miden RPC proto

This crate contains protobuf message definitions of the RPC component of the Miden node.
It consists of a map of `(filename, file contents)` where each entry refers to a protobuf file.

Additionally, the crate exposes a `write_proto(target_dir)` function that writes the files into `target_dir`.

## License
This project is [MIT licensed](../../LICENSE).
41 changes: 41 additions & 0 deletions crates/rpc-proto/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::{
env,
fs::File,
io::{self, Read, Write},
path::Path,
};

const DOC_COMMENT: &str =
"A list of tuples containing the names and contents of various protobuf files.";

fn main() -> io::Result<()> {
let out_dir = env::current_dir().expect("Error getting cwd");
let dest_path = Path::new(&out_dir).join("./src/proto_files.rs");
let mut file = File::create(dest_path)?;

let proto_dir = Path::new("../../proto");

writeln!(file, "/// {DOC_COMMENT}")?;
writeln!(file, "pub const PROTO_FILES: &[(&str, &str)] = &[")?;

for entry in std::fs::read_dir(proto_dir)? {
let entry = entry?;
let path = entry.path();
if path.is_file() {
let mut file_content = String::new();
let file_name =
path.file_name().and_then(|f| f.to_str()).expect("Could not get file name");

File::open(&path)?.read_to_string(&mut file_content)?;
writeln!(
file,
" (\"{}\", include_str!(\"../../../proto/{}\")),",
file_name, file_name
)?;
}
}

writeln!(file, "];")?;

Ok(())
}
36 changes: 36 additions & 0 deletions crates/rpc-proto/src/lib.rs
bobbinth marked this conversation as resolved.
Show resolved Hide resolved
bobbinth marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![no_std]

#[cfg(feature = "std")]
extern crate std;

mod proto_files;
pub use proto_files::PROTO_FILES;

/// Writes the RPC protobuf file into `target_dir`.
#[cfg(feature = "std")]
pub fn write_proto(target_dir: &std::path::Path) -> Result<(), std::string::String> {
use std::{
format,
fs::{self, File},
io::Write,
string::ToString,
};

if !target_dir.exists() {
fs::create_dir_all(target_dir)
.map_err(|err| format!("Error creating directory: {}", err))?;
} else if !target_dir.is_dir() {
return Err("The target path exists but is not a directory".to_string());
}

for (file_name, file_content) in PROTO_FILES {
let mut file_path = target_dir.to_path_buf();
file_path.push(file_name);
let mut file = File::create(&file_path)
.map_err(|err| format!("Error creating {}: {}", file_name, err))?;
file.write_all(file_content.as_bytes())
.map_err(|err| format!("Error writing {}: {}", file_name, err))?;
}

Ok(())
}
16 changes: 16 additions & 0 deletions crates/rpc-proto/src/proto_files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// A list of tuples containing the names and contents of various protobuf files.
pub const PROTO_FILES: &[(&str, &str)] = &[
("note.proto", include_str!("../../../proto/note.proto")),
("smt.proto", include_str!("../../../proto/smt.proto")),
("responses.proto", include_str!("../../../proto/responses.proto")),
("rpc.proto", include_str!("../../../proto/rpc.proto")),
("store.proto", include_str!("../../../proto/store.proto")),
("transaction.proto", include_str!("../../../proto/transaction.proto")),
("mmr.proto", include_str!("../../../proto/mmr.proto")),
("account.proto", include_str!("../../../proto/account.proto")),
("block_header.proto", include_str!("../../../proto/block_header.proto")),
("digest.proto", include_str!("../../../proto/digest.proto")),
("block_producer.proto", include_str!("../../../proto/block_producer.proto")),
("merkle.proto", include_str!("../../../proto/merkle.proto")),
("requests.proto", include_str!("../../../proto/requests.proto")),
];
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading