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

zcash_client_backend: Switch from protobuf 2 to prost 0.11 #691

Merged
merged 2 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zcash_client_backend/src/proto/compact_formats.rs linguist-generated=true
11 changes: 3 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ jobs:
command: test
args: --all-features --verbose --release --all -- --ignored

- name: Verify working directory is clean
run: git diff --exit-code

build:
name: Build target ${{ matrix.target }}
runs-on: ubuntu-latest
Expand Down Expand Up @@ -200,14 +203,6 @@ jobs:
toolchain: 1.56.1
override: true

# cargo fmt does not build the code, and running it in a fresh clone of
# the codebase will fail because the protobuf code has not been generated.
- name: cargo build
uses: actions-rs/cargo@v1
with:
command: build
args: --all

# Ensure all code has been formatted with rustfmt
- run: rustup component add rustfmt
- name: Check formatting
Expand Down
2 changes: 0 additions & 2 deletions zcash_client_backend/.gitignore

This file was deleted.

11 changes: 11 additions & 0 deletions zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ and this library adheres to Rust's notion of
- `WalletWrite::remove_unmined_tx` (behind the `unstable` feature flag).
- `WalletWrite::get_next_available_address`
- `WalletWrite::put_received_transparent_utxo`
- `impl From<prost::DecodeError> for error::Error`
- `zcash_client_backend::decrypt`:
- `TransferType`
- `zcash_client_backend::proto`:
Expand Down Expand Up @@ -68,6 +69,8 @@ and this library adheres to Rust's notion of
- Bumped dependencies to `ff 0.12`, `group 0.12`, `bls12_381 0.7`, `jubjub 0.9`,
`zcash_primitives 0.8`, `orchard 0.3`.
- `zcash_client_backend::proto`:
- The Protocol Buffers bindings are now generated for `prost 0.11` instead of
`protobuf 2`.
- `compact_formats::CompactSpend` has been renamed to `CompactSaplingSpend`,
and its `epk` field (and associated `set_epk` method) has been renamed to
`ephemeralKey` (and `set_ephemeralKey`).
Expand Down Expand Up @@ -104,6 +107,8 @@ and this library adheres to Rust's notion of
`store_decrypted_tx`.
- `data_api::ReceivedTransaction` has been renamed to `DecryptedTransaction`,
and its `outputs` field has been renamed to `sapling_outputs`.
- `data_api::error::Error::Protobuf` now wraps `prost::DecodeError` instead of
`protobuf::ProtobufError`.
- `data_api::error::Error` has the following additional cases:
- `Error::MemoForbidden` to report the condition where a memo was
specified to be sent to a transparent recipient.
Expand Down Expand Up @@ -150,6 +155,12 @@ and this library adheres to Rust's notion of
`WalletRead::get_unified_full_viewing_keys` instead).
- `WalletRead::get_address` (use `WalletRead::get_current_address` or
`WalletWrite::get_next_available_address` instead.)
- `impl From<protobuf::ProtobufError> for error::Error`
- `zcash_client_backend::proto::compact_formats`:
- `Compact*::new` methods (use `Default::default` or struct instantiation
instead).
- Getters (use dedicated typed methods or direct field access instead).
- Setters (use direct field access instead).
- The hardcoded `data_api::wallet::ANCHOR_OFFSET` constant.
- `zcash_client_backend::wallet::AccountId` (moved to `zcash_primitives::zip32::AccountId`).

Expand Down
8 changes: 6 additions & 2 deletions zcash_client_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.56.1"

# Exclude proto files so crates.io consumers don't need protoc.
exclude = ["*.proto"]

[package.metadata.cargo-udeps.ignore]
development = ["zcash_proofs"]

Expand Down Expand Up @@ -40,7 +43,7 @@ memuse = "0.2"
tracing = "0.1"

# - Protobuf interfaces
protobuf = "~2.27.1" # MSRV 1.52.1
prost = "0.11"

# - Secret management
secrecy = "0.8"
Expand Down Expand Up @@ -68,7 +71,8 @@ crossbeam-channel = "0.5"
rayon = "1.5"

[build-dependencies]
protobuf-codegen-pure = "~2.27.1" # MSRV 1.52.1
prost-build = "0.11"
which = "4"

[dev-dependencies]
gumdrop = "0.8"
Expand Down
49 changes: 42 additions & 7 deletions zcash_client_backend/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
fn main() {
protobuf_codegen_pure::Codegen::new()
.out_dir("src/proto")
.inputs(["proto/compact_formats.proto"])
.includes(["proto"])
.run()
.expect("Protobuf codegen failed");
use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

const COMPACT_FORMATS_PROTO: &str = "proto/compact_formats.proto";

fn main() -> io::Result<()> {
// We don't include the proto files in releases so that downstreams do not need to
// regenerate the bindings even if protoc is present.
if Path::new(COMPACT_FORMATS_PROTO).exists() {
println!("cargo:rerun-if-changed={}", COMPACT_FORMATS_PROTO);

// We check for the existence of protoc in the same way as prost_build, so that people
// building from source do not need to have protoc installed. If they make changes to
// the proto files, the discrepancy will be caught by CI.
if env::var_os("PROTOC")
.map(PathBuf::from)
.or_else(|| which::which("protoc").ok())
.is_some()
{
build()?;
}
}

Ok(())
}

fn build() -> io::Result<()> {
let out: PathBuf = env::var_os("OUT_DIR")
.expect("Cannot find OUT_DIR environment variable")
.into();

prost_build::compile_protos(&[COMPACT_FORMATS_PROTO], &["proto/"])?;

// Copy the generated files into the source tree so changes can be committed.
fs::copy(
out.join("cash.z.wallet.sdk.rpc.rs"),
"src/proto/compact_formats.rs",
)?;

Ok(())
}
6 changes: 3 additions & 3 deletions zcash_client_backend/src/data_api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub enum Error<NoteId> {
Builder(builder::Error),

/// An error occurred decoding a protobuf message.
Protobuf(protobuf::ProtobufError),
Protobuf(prost::DecodeError),

/// The wallet attempted a sapling-only operation at a block
/// height when Sapling was not yet active.
Expand Down Expand Up @@ -180,8 +180,8 @@ impl<N> From<builder::Error> for Error<N> {
}
}

impl<N> From<protobuf::ProtobufError> for Error<N> {
fn from(e: protobuf::ProtobufError) -> Self {
impl<N> From<prost::DecodeError> for Error<N> {
fn from(e: prost::DecodeError) -> Self {
Error::Protobuf(e)
}
}
15 changes: 8 additions & 7 deletions zcash_client_backend/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use zcash_primitives::{

use zcash_note_encryption::{EphemeralKeyBytes, COMPACT_NOTE_SIZE};

#[rustfmt::skip]
pub mod compact_formats;

impl compact_formats::CompactBlock {
Expand Down Expand Up @@ -44,7 +45,7 @@ impl compact_formats::CompactBlock {
if let Some(header) = self.header() {
header.prev_block
} else {
BlockHash::from_slice(&self.prevHash)
BlockHash::from_slice(&self.prev_hash)
}
}

Expand Down Expand Up @@ -99,7 +100,7 @@ impl compact_formats::CompactSaplingOutput {
///
/// [`CompactOutput.epk`]: #structfield.epk
pub fn ephemeral_key(&self) -> Result<EphemeralKeyBytes, ()> {
self.ephemeralKey[..]
self.ephemeral_key[..]
.try_into()
.map(EphemeralKeyBytes)
.map_err(|_| ())
Expand All @@ -110,11 +111,11 @@ impl<A: sapling::Authorization> From<sapling::OutputDescription<A>>
for compact_formats::CompactSaplingOutput
{
fn from(out: sapling::OutputDescription<A>) -> compact_formats::CompactSaplingOutput {
let mut result = compact_formats::CompactSaplingOutput::new();
result.set_cmu(out.cmu.to_repr().to_vec());
result.set_ephemeralKey(out.ephemeral_key.as_ref().to_vec());
result.set_ciphertext(out.enc_ciphertext[..COMPACT_NOTE_SIZE].to_vec());
result
compact_formats::CompactSaplingOutput {
cmu: out.cmu.to_repr().to_vec(),
ephemeral_key: out.ephemeral_key.as_ref().to_vec(),
ciphertext: out.enc_ciphertext[..COMPACT_NOTE_SIZE].to_vec(),
}
}
}

Expand Down
Empty file.
102 changes: 102 additions & 0 deletions zcash_client_backend/src/proto/compact_formats.rs

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

50 changes: 26 additions & 24 deletions zcash_client_backend/src/welding_rig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,16 @@ mod tests {
let fake_epk = SPENDING_KEY_GENERATOR * fake_esk;
fake_epk.to_bytes().to_vec()
};
let mut cspend = CompactSaplingSpend::new();
cspend.set_nf(fake_nf);
let mut cout = CompactSaplingOutput::new();
cout.set_cmu(fake_cmu);
cout.set_ephemeralKey(fake_epk);
cout.set_ciphertext(vec![0; 52]);
let mut ctx = CompactTx::new();
let cspend = CompactSaplingSpend { nf: fake_nf };
let cout = CompactSaplingOutput {
cmu: fake_cmu,
ephemeral_key: fake_epk,
ciphertext: vec![0; 52],
};
let mut ctx = CompactTx::default();
let mut txid = vec![0; 32];
rng.fill_bytes(&mut txid);
ctx.set_hash(txid);
ctx.hash = txid;
ctx.spends.push(cspend);
ctx.outputs.push(cout);
ctx
Expand Down Expand Up @@ -503,17 +503,19 @@ mod tests {
&mut rng,
);
let cmu = note.cmu().to_repr().as_ref().to_owned();
let epk = encryptor.epk().to_bytes().to_vec();
let ephemeral_key = encryptor.epk().to_bytes().to_vec();
let enc_ciphertext = encryptor.encrypt_note_plaintext();

// Create a fake CompactBlock containing the note
let mut cb = CompactBlock::new();
cb.set_hash({
let mut hash = vec![0; 32];
rng.fill_bytes(&mut hash);
hash
});
cb.set_height(height.into());
let mut cb = CompactBlock {
hash: {
let mut hash = vec![0; 32];
rng.fill_bytes(&mut hash);
hash
},
height: height.into(),
..Default::default()
};

// Add a random Sapling tx before ours
{
Expand All @@ -522,16 +524,16 @@ mod tests {
cb.vtx.push(tx);
}

let mut cspend = CompactSaplingSpend::new();
cspend.set_nf(nf.0.to_vec());
let mut cout = CompactSaplingOutput::new();
cout.set_cmu(cmu);
cout.set_ephemeralKey(epk);
cout.set_ciphertext(enc_ciphertext.as_ref()[..52].to_vec());
let mut ctx = CompactTx::new();
let cspend = CompactSaplingSpend { nf: nf.0.to_vec() };
let cout = CompactSaplingOutput {
cmu,
ephemeral_key,
ciphertext: enc_ciphertext.as_ref()[..52].to_vec(),
};
let mut ctx = CompactTx::default();
let mut txid = vec![0; 32];
rng.fill_bytes(&mut txid);
ctx.set_hash(txid);
ctx.hash = txid;
ctx.spends.push(cspend);
ctx.outputs.push(cout);
ctx.index = cb.vtx.len() as u64;
Expand Down
Loading