1 unstable release
| 0.1.0 | Sep 17, 2024 |
|---|
#1379 in Encoding
655KB
13K
SLoC
Rust crate for dYdX Chain protobufs
Usage as a dependency
Cargo.toml
[dependencies]
v4-proto-rs = "0.1"
Note: by default, rust stub files are not rebuilt (see Q&A below)
For more idiomatic Rust you can use conversions (try_into and into) for the following types:
prost_types::Timestamp->std::time::SystemTimeprost_types::Duration->std::time::Duration
Local development
Prerequisites
- Rust
- Buf - to resolve 3rd-party dependencies for protobuf files
- protoc - to compile protobuf files with their 3rd-party dependencies
- cargo deny - to check for security/license/sources issues
Then for a code (re-)generation run
V4_PROTO_REBUILD=1 cargo build -vv
Before publishing make sure to run (and fix all warnings and errors)
cargo fmt
cargo clippy
cargo deny check licenses advisories sources
Q&A
-
Why do we put autogenerated files to the crate (and git) and do not (re-)generate them at compilation?
For several reasons:
- reproducibility of the dependency
- to avoid external dependencies for the lib users (
protocandbufare only needed for code generation)
But if a user wants to (re-)generate at compilation time, he/she can set an environment variable
V4_PROTO_REBUILD(to any value). -
Why do I need a
protocfor this crate development? I thoughtprost-buildcrate generates everything natively with Rust?The main work (parsing, linking, etc. - have a look https://protobuf.com/docs/descriptors) is done by
protoc. The result of theprotocwork is a "file descriptor" (think of it as IR assembly language like LLVM IR) - a binary file. This file descriptor is an input for a language-specific code generator likeprost. Think ofprostcrate as a compiler target which generates a ISA-specific "assembly" (in our case, Rust) as an output.prost-buildalways used theprotocbut since version 0.11 of prost-build it requiresprotoc(the protobuf compiler) to be already installed on the system - before theprotoccould be compiled during theprost-buildbuild (https://github.com/tokio-rs/prost/blob/v0.10.4/prost-build/build.rs#L77). -
Why do we use
tonic-buildcrate and not justprost-build?prost-buildgenerates only serialization-deserialization stubs for messages, but we also need a client implementation (generated bytonic-build) because packages in other language implementations ofv4-chainhave ones. -
Why do we need
buf?Buf is a tool whose primary function is to resolve dependencies in protobuf files. Protobuf specifications can refer to 3rd-party protobuf specifications and use types declared there. Basically,
bufbuilds a list of all used protobuf files, downloads them, and allows exporting (=copying) them to a specified directory. The proto files in this repository and downloaded 3rd-party proto files (aka "includes") are an input for theprotoc.
Dependencies
~14–24MB
~378K SLoC