Skip to content

Commit

Permalink
Merge remote-tracking branch 'github/feature/improve_proto_build'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Aug 23, 2023
2 parents 9acf2d5 + f7b75b6 commit 8506aef
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 43 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ jobs:
toolchain: stable
override: true

- name: Install protobuf
run: |
wget https://github.com/protocolbuffers/protobuf/releases/download/v24.0/protoc-24.0-linux-x86_64.zip
unzip protoc-24.0-linux-x86_64.zip
sudo cp bin/protoc /usr/bin/protoc
sudo cp -r include/. /usr/include
- name: Build docs
uses: actions-rs/cargo@v1
with:
Expand Down
37 changes: 23 additions & 14 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ jobs:
with:
command: check

test:
name: Test Suite
check-protos:
name: Check protos
runs-on: ubuntu-latest
steps:
- name: Checkout sources
Expand All @@ -37,11 +37,27 @@ jobs:
override: true

- name: Install protobuf
run: |
wget https://github.com/protocolbuffers/protobuf/releases/download/v24.0/protoc-24.0-linux-x86_64.zip
unzip protoc-24.0-linux-x86_64.zip
sudo cp bin/protoc /usr/bin/protoc
sudo cp -r include/. /usr/include
run: sudo apt update && sudo apt-get -y install protobuf-compiler

- name: Generate Rust code from .proto files
run: cargo run -p gen-protos

- name: Check for uncommitted changes
run: git diff --exit-code

test:
name: Test Suite
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- name: Run cargo test for nekoton
uses: actions-rs/cargo@v1
Expand Down Expand Up @@ -88,13 +104,6 @@ jobs:
override: true
components: rustfmt, clippy

- name: Install protobuf
run: |
wget https://github.com/protocolbuffers/protobuf/releases/download/v24.0/protoc-24.0-linux-x86_64.zip
unzip protoc-24.0-linux-x86_64.zip
sudo cp bin/protoc /usr/bin/protoc
sudo cp -r include/. /usr/include
- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = "2021"

[workspace]
members = [
"gen-protos",
"nekoton-abi",
"nekoton-contracts",
"nekoton-derive",
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ cargo add nekoton

- Rust 1.62+
- `wasm-pack` 0.9.1+ (to test build for wasm target)
- protoc 3.12.4+ (to generate .rs files from .proto)

### Modifying protobuffers

Occasionally, you may need to change the `.proto` files that define request/response
data format. In this case, you will need to add a few steps to the above
workflow.

- Install the `protoc` compiler.
- Run `cargo run -p gen-protos` regularly (or after every edit to a `.proto`
file). The `gen-protos` binary will use the `prost-build` library to compile the
`.proto` files into `.rs` files.
- If you are adding a new `.proto` file, you will need to edit the list of
these files in `gen-protos/src/main.rs`.

The `.rs` files generated from `.proto` files are included in the repository,
and there is a Github CI check that will complain if they do not match.

## Contributing

Expand Down
14 changes: 14 additions & 0 deletions gen-protos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "gen-protos"
description = "Generate Protocol Buffers definitions for nekoton-proto crate"
version = "0.13.0"
authors = [
"Alexey Pashinov <pashinov93@gmail.com>",
"Vladimir Petrzhikovskiy <v.petrzhikovskiy@dexpa.io>",
"Ivan Kalinin <i.kalinin@dexpa.io>"
]
rust-version = "1.62.0"
edition = "2021"

[dependencies]
prost-build = "0.11"
26 changes: 26 additions & 0 deletions gen-protos/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::io::Result;
use std::path::Path;

fn main() -> Result<()> {
let input = ["rpc.proto"];

let root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
let protos_dir = root.join("nekoton-proto").join("src").join("protos");

prost_build::Config::new()
.out_dir(&protos_dir)
.include_file("mod.rs")
// For old protoc versions. 3.12.4 needs this, but 3.21.12 doesn't.
.protoc_arg("--experimental_allow_proto3_optional")
// Replace Vec<u8> to Bytes
.bytes(["."])
// Add EQ macro
.type_attribute("rpc.Response.GetTimings", "#[derive(Eq)]")
.compile_protos(
&input
.into_iter()
.map(|x| protos_dir.join(x))
.collect::<Vec<_>>(),
&[protos_dir],
)
}
3 changes: 0 additions & 3 deletions nekoton-proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ ton_block = { git = "https://github.com/broxus/ton-labs-block.git" }
ton_types = { git = "https://github.com/broxus/ton-labs-types" }

nekoton-abi = { path = "../nekoton-abi"}

[build-dependencies]
prost-build = "0.11"
11 changes: 0 additions & 11 deletions nekoton-proto/build.rs

This file was deleted.

5 changes: 1 addition & 4 deletions nekoton-proto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
pub mod rpc {
include!(concat!(env!("OUT_DIR"), "/rpc.rs"));
}

pub mod models;
pub mod protos;
pub mod utils;

pub use prost;
6 changes: 3 additions & 3 deletions nekoton-proto/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use prost::bytes::Bytes;
use ton_types::UInt256;

use crate::rpc::response::get_contract_state::exists::{Exact, Inexact, LastTransactionId};
use crate::rpc::response::get_contract_state::not_exist::GenTimings;
use crate::rpc::response::get_contract_state::{NotExist, Timings};
use crate::protos::rpc::response::get_contract_state::exists::{Exact, Inexact, LastTransactionId};
use crate::protos::rpc::response::get_contract_state::not_exist::GenTimings;
use crate::protos::rpc::response::get_contract_state::{NotExist, Timings};

impl From<GenTimings> for nekoton_abi::GenTimings {
fn from(t: GenTimings) -> Self {
Expand Down
3 changes: 3 additions & 0 deletions nekoton-proto/src/protos/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod rpc {
include!("rpc.rs");
}
File renamed without changes.
Loading

0 comments on commit 8506aef

Please sign in to comment.