2 unstable releases
0.2.0 | Nov 15, 2024 |
---|---|
0.1.0 | Oct 28, 2024 |
#211 in Encoding
780 downloads per month
Used in dydx
1MB
16K
SLoC
Rust crate for dYdX Chain protobufs
Usage as a dependency
Cargo.toml
[dependencies]
dydx-proto = "0.2"
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::SystemTime
prost_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 (
protoc
andbuf
are 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
protoc
for this crate development? I thoughtprost-build
crate 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 theprotoc
work 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 ofprost
crate as a compiler target which generates a ISA-specific "assembly" (in our case, Rust) as an output.prost-build
always used theprotoc
but since version 0.11 of prost-build it requiresprotoc
(the protobuf compiler) to be already installed on the system - before theprotoc
could be compiled during theprost-build
build (https://github.com/tokio-rs/prost/blob/v0.10.4/prost-build/build.rs#L77). -
Why do we use
tonic-build
crate and not justprost-build
?prost-build
generates only serialization-deserialization stubs for messages, but we also need a client implementation (generated bytonic-build
) because packages in other language implementations ofv4-chain
have 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,
buf
builds 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
~13–21MB
~319K SLoC