Skip to content

Commit

Permalink
zcash_client_backend: Add gRPC bindings behind feature flag
Browse files Browse the repository at this point in the history
The lightwalletd service file is sourced from:
  Git: https://github.com/zcash/lightwalletd
  Rev: ad5ecda5fbb5e12799f926b8cd785d54fdd250c8

Closes zcash#585.
  • Loading branch information
str4d committed Nov 2, 2022
1 parent 379b703 commit 17a4830
Show file tree
Hide file tree
Showing 7 changed files with 875 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
zcash_client_backend/src/proto/compact_formats.rs linguist-generated=true
zcash_client_backend/src/proto/service.rs linguist-generated=true
2 changes: 2 additions & 0 deletions zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ and this library adheres to Rust's notion of
- `zcash_client_backend::proto`:
- `actions` field on `compact_formats::CompactTx`
- `compact_formats::CompactOrchardAction`
- gRPC bindings for the `lightwalletd` server, behind a `lightwalletd-tonic`
feature flag.
- `zcash_client_backend::zip321::TransactionRequest` methods:
- `TransactionRequest::new` for constructing a request from `Vec<Payment>`.
- `TransactionRequest::payments` for accessing the `Payments` that make up a
Expand Down
6 changes: 4 additions & 2 deletions zcash_client_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ hdwallet = { version = "0.3.1", optional = true }
memuse = "0.2"
tracing = "0.1"

# - Protobuf interfaces
# - Protobuf interfaces and gRPC bindings
prost = "0.11"
tonic = { version = "0.8", optional = true }

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

[build-dependencies]
prost-build = "0.11"
tonic-build = "0.8"
which = "4"

[dev-dependencies]
Expand All @@ -86,6 +87,7 @@ zcash_proofs = { version = "0.8", path = "../zcash_proofs" }
zcash_address = { version = "0.2", path = "../components/zcash_address", features = ["test-dependencies"] }

[features]
lightwalletd-tonic = ["tonic"]
transparent-inputs = ["hdwallet", "zcash_primitives/transparent-inputs"]
test-dependencies = [
"proptest",
Expand Down
62 changes: 48 additions & 14 deletions zcash_client_backend/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ use std::path::{Path, PathBuf};

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

#[cfg(feature = "lightwalletd-tonic")]
const SERVICE_PROTO: &str = "proto/service.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")
// - We don't include the proto files in releases so that downstreams do not need to
// regenerate the bindings even if protoc is present.
// - 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 Path::new(COMPACT_FORMATS_PROTO).exists()
&& env::var_os("PROTOC")
.map(PathBuf::from)
.or_else(|| which::which("protoc").ok())
.is_some()
{
build()?;
}
{
build()?;
}

Ok(())
Expand All @@ -31,13 +31,47 @@ fn build() -> io::Result<()> {
.expect("Cannot find OUT_DIR environment variable")
.into();

prost_build::compile_protos(&[COMPACT_FORMATS_PROTO], &["proto/"])?;
// Build the compact format types.
tonic_build::compile_protos(COMPACT_FORMATS_PROTO)?;

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

#[cfg(feature = "lightwalletd-tonic")]
{
// Build the gRPC types and client.
tonic_build::configure()
.build_server(false)
.extern_path(
".cash.z.wallet.sdk.rpc.CompactBlock",
"crate::proto::compact_formats::CompactBlock",
)
.extern_path(
".cash.z.wallet.sdk.rpc.CompactTx",
"crate::proto::compact_formats::CompactTx",
)
.extern_path(
".cash.z.wallet.sdk.rpc.CompactSaplingSpend",
"crate::proto::compact_formats::CompactSaplingSpend",
)
.extern_path(
".cash.z.wallet.sdk.rpc.CompactSaplingOutput",
"crate::proto::compact_formats::CompactSaplingOutput",
)
.extern_path(
".cash.z.wallet.sdk.rpc.CompactOrchardAction",
"crate::proto::compact_formats::CompactOrchardAction",
)
.compile(&[SERVICE_PROTO], &["proto/"])?;

// Copy the generated types into the source tree so changes can be committed. The
// file has the same name as for the compact format types because they have the
// same package, but we've set things up so this only contains the service types.
fs::copy(out.join("cash.z.wallet.sdk.rpc.rs"), "src/proto/service.rs")?;
}

Ok(())
}
187 changes: 187 additions & 0 deletions zcash_client_backend/proto/service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Copyright (c) 2019-2020 The Zcash developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php .

syntax = "proto3";
package cash.z.wallet.sdk.rpc;
option go_package = "lightwalletd/walletrpc";
option swift_prefix = "";
import "compact_formats.proto";

// A BlockID message contains identifiers to select a block: a height or a
// hash. Specification by hash is not implemented, but may be in the future.
message BlockID {
uint64 height = 1;
bytes hash = 2;
}

// BlockRange specifies a series of blocks from start to end inclusive.
// Both BlockIDs must be heights; specification by hash is not yet supported.
message BlockRange {
BlockID start = 1;
BlockID end = 2;
}

// A TxFilter contains the information needed to identify a particular
// transaction: either a block and an index, or a direct transaction hash.
// Currently, only specification by hash is supported.
message TxFilter {
BlockID block = 1; // block identifier, height or hash
uint64 index = 2; // index within the block
bytes hash = 3; // transaction ID (hash, txid)
}

// RawTransaction contains the complete transaction data. It also optionally includes
// the block height in which the transaction was included, or, when returned
// by GetMempoolStream(), the latest block height.
message RawTransaction {
bytes data = 1; // exact data returned by Zcash 'getrawtransaction'
uint64 height = 2; // height that the transaction was mined (or -1)
}

// A SendResponse encodes an error code and a string. It is currently used
// only by SendTransaction(). If error code is zero, the operation was
// successful; if non-zero, it and the message specify the failure.
message SendResponse {
int32 errorCode = 1;
string errorMessage = 2;
}

// Chainspec is a placeholder to allow specification of a particular chain fork.
message ChainSpec {}

// Empty is for gRPCs that take no arguments, currently only GetLightdInfo.
message Empty {}

// LightdInfo returns various information about this lightwalletd instance
// and the state of the blockchain.
message LightdInfo {
string version = 1;
string vendor = 2;
bool taddrSupport = 3; // true
string chainName = 4; // either "main" or "test"
uint64 saplingActivationHeight = 5; // depends on mainnet or testnet
string consensusBranchId = 6; // protocol identifier, see consensus/upgrades.cpp
uint64 blockHeight = 7; // latest block on the best chain
string gitCommit = 8;
string branch = 9;
string buildDate = 10;
string buildUser = 11;
uint64 estimatedHeight = 12; // less than tip height if zcashd is syncing
string zcashdBuild = 13; // example: "v4.1.1-877212414"
string zcashdSubversion = 14; // example: "/MagicBean:4.1.1/"
}

// TransparentAddressBlockFilter restricts the results to the given address
// or block range.
message TransparentAddressBlockFilter {
string address = 1; // t-address
BlockRange range = 2; // start, end heights
}

// Duration is currently used only for testing, so that the Ping rpc
// can simulate a delay, to create many simultaneous connections. Units
// are microseconds.
message Duration {
int64 intervalUs = 1;
}

// PingResponse is used to indicate concurrency, how many Ping rpcs
// are executing upon entry and upon exit (after the delay).
// This rpc is used for testing only.
message PingResponse {
int64 entry = 1;
int64 exit = 2;
}

message Address {
string address = 1;
}
message AddressList {
repeated string addresses = 1;
}
message Balance {
int64 valueZat = 1;
}

message Exclude {
repeated bytes txid = 1;
}

// The TreeState is derived from the Zcash z_gettreestate rpc.
message TreeState {
string network = 1; // "main" or "test"
uint64 height = 2; // block height
string hash = 3; // block id
uint32 time = 4; // Unix epoch time when the block was mined
string saplingTree = 5; // sapling commitment tree state
string orchardTree = 6; // orchard commitment tree state
}

// Results are sorted by height, which makes it easy to issue another
// request that picks up from where the previous left off.
message GetAddressUtxosArg {
repeated string addresses = 1;
uint64 startHeight = 2;
uint32 maxEntries = 3; // zero means unlimited
}
message GetAddressUtxosReply {
string address = 6;
bytes txid = 1;
int32 index = 2;
bytes script = 3;
int64 valueZat = 4;
uint64 height = 5;
}
message GetAddressUtxosReplyList {
repeated GetAddressUtxosReply addressUtxos = 1;
}

service CompactTxStreamer {
// Return the height of the tip of the best chain
rpc GetLatestBlock(ChainSpec) returns (BlockID) {}
// Return the compact block corresponding to the given block identifier
rpc GetBlock(BlockID) returns (CompactBlock) {}
// Return a list of consecutive compact blocks
rpc GetBlockRange(BlockRange) returns (stream CompactBlock) {}

// Return the requested full (not compact) transaction (as from zcashd)
rpc GetTransaction(TxFilter) returns (RawTransaction) {}
// Submit the given transaction to the Zcash network
rpc SendTransaction(RawTransaction) returns (SendResponse) {}

// Return the txids corresponding to the given t-address within the given block range
rpc GetTaddressTxids(TransparentAddressBlockFilter) returns (stream RawTransaction) {}
rpc GetTaddressBalance(AddressList) returns (Balance) {}
rpc GetTaddressBalanceStream(stream Address) returns (Balance) {}

// Return the compact transactions currently in the mempool; the results
// can be a few seconds out of date. If the Exclude list is empty, return
// all transactions; otherwise return all *except* those in the Exclude list
// (if any); this allows the client to avoid receiving transactions that it
// already has (from an earlier call to this rpc). The transaction IDs in the
// Exclude list can be shortened to any number of bytes to make the request
// more bandwidth-efficient; if two or more transactions in the mempool
// match a shortened txid, they are all sent (none is excluded). Transactions
// in the exclude list that don't exist in the mempool are ignored.
rpc GetMempoolTx(Exclude) returns (stream CompactTx) {}

// Return a stream of current Mempool transactions. This will keep the output stream open while
// there are mempool transactions. It will close the returned stream when a new block is mined.
rpc GetMempoolStream(Empty) returns (stream RawTransaction) {}

// GetTreeState returns the note commitment tree state corresponding to the given block.
// See section 3.7 of the Zcash protocol specification. It returns several other useful
// values also (even though they can be obtained using GetBlock).
// The block can be specified by either height or hash.
rpc GetTreeState(BlockID) returns (TreeState) {}
rpc GetLatestTreeState(Empty) returns (TreeState) {}

rpc GetAddressUtxos(GetAddressUtxosArg) returns (GetAddressUtxosReplyList) {}
rpc GetAddressUtxosStream(GetAddressUtxosArg) returns (stream GetAddressUtxosReply) {}

// Return information about this lightwalletd instance and the blockchain
rpc GetLightdInfo(Empty) returns (LightdInfo) {}
// Testing-only, requires lightwalletd --ping-very-insecure (do not enable in production)
rpc Ping(Duration) returns (PingResponse) {}
}
4 changes: 4 additions & 0 deletions zcash_client_backend/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ use zcash_note_encryption::{EphemeralKeyBytes, COMPACT_NOTE_SIZE};
#[rustfmt::skip]
pub mod compact_formats;

#[cfg(feature = "lightwalletd-tonic")]
#[rustfmt::skip]
pub mod service;

impl compact_formats::CompactBlock {
/// Returns the [`BlockHash`] for this block.
///
Expand Down
Loading

0 comments on commit 17a4830

Please sign in to comment.