Skip to content

Commit

Permalink
zcash_client_backend: Switch from async-trait to RPIT
Browse files Browse the repository at this point in the history
RPIT (return-position `impl Trait`) was stabilized in Rust 1.75, and the
`dynosaur` crate providing dynamic dispatch support was published two
weeks ago. Now that our MSRV is 1.77, we can use both of these.
  • Loading branch information
str4d committed Oct 26, 2024
1 parent 5edaa26 commit f347dc5
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 23 deletions.
34 changes: 34 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ incrementalmerkletree-testing = "0.2"
# - `arti-client` depends on `rusqlite`, and a version mismatch there causes a compilation
# failure due to incompatible `libsqlite3-sys` versions.
arti-client = { version = "0.23", default-features = false, features = ["compression", "rustls", "tokio"] }
dynosaur = "0.1.1"
tokio = "1"
tor-rtcompat = "0.23"
tower = "0.4"
trait-variant = "0.1"

# ZIP 32
aes = "0.8"
Expand Down
12 changes: 12 additions & 0 deletions supply-chain/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@ criteria = "safe-to-deploy"
version = "1.0.17"
criteria = "safe-to-deploy"

[[exemptions.dynosaur]]
version = "0.1.1"
criteria = "safe-to-deploy"

[[exemptions.dynosaur_derive]]
version = "0.1.1"
criteria = "safe-to-deploy"

[[exemptions.ecdsa]]
version = "0.16.9"
criteria = "safe-to-deploy"
Expand Down Expand Up @@ -1406,6 +1414,10 @@ criteria = "safe-to-deploy"
version = "0.2.5"
criteria = "safe-to-deploy"

[[exemptions.trait-variant]]
version = "0.1.2"
criteria = "safe-to-deploy"

[[exemptions.typed-index-collections]]
version = "3.1.0"
criteria = "safe-to-deploy"
Expand Down
7 changes: 7 additions & 0 deletions zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ and this library adheres to Rust's notion of
- A new feature flag, `non-standard-fees`, has been added. This flag is now
required in order to make use of any types or methods that enable non-standard
fee calculation.
- `zcash_client_backend::tor::http::cryptex`:
- `LocalExchange`, a variant of the `Exchange` trait without `Send` bounds.
- `DynExchange`
- `DynLocalExchange`

### Changed
- MSRV is now 1.77.0.
Expand Down Expand Up @@ -76,6 +80,9 @@ and this library adheres to Rust's notion of
`ProposalDecodingError::FeeRuleNotSupported` has been added to replace it.
- `zcash_client_backend::data_api::fees::fixed` is now available only via the
use of the `non-standard-fees` feature flag.
- `zcash_client_backend::tor::http::cryptex`:
- The `Exchange` trait is no longer object-safe. Replace any existing uses of
`dyn Exchange` with `DynExchange`.

### Removed
- `zcash_client_backend::data_api`:
Expand Down
5 changes: 4 additions & 1 deletion zcash_client_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ nom = "7"
# `hyper::Error`, `hyper::http::Error`, `serde_json::Error`. We could avoid this with
# changes to error handling.
arti-client = { workspace = true, optional = true }
dynosaur = { workspace = true, optional = true }
hyper = { workspace = true, optional = true, features = ["client", "http1"] }
serde_json = { workspace = true, optional = true }
trait-variant = { workspace = true, optional = true }

# - Currency conversion
rust_decimal = { workspace = true, optional = true }
Expand Down Expand Up @@ -179,7 +181,7 @@ sync = [
## operations.
tor = [
"dep:arti-client",
"dep:async-trait",
"dep:dynosaur",
"dep:futures-util",
"dep:http-body-util",
"dep:hyper",
Expand All @@ -191,6 +193,7 @@ tor = [
"dep:tokio",
"dep:tokio-rustls",
"dep:tor-rtcompat",
"dep:trait-variant",
"dep:tower",
"dep:webpki-roots",
]
Expand Down
21 changes: 11 additions & 10 deletions zcash_client_backend/src/tor/http/cryptex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Cryptocurrency exchange rate APIs.
use async_trait::async_trait;
use futures_util::{future::join_all, join};
use rand::{seq::IteratorRandom, thread_rng};
use rust_decimal::Decimal;
Expand All @@ -26,8 +25,10 @@ pub mod exchanges {
}

/// An exchange that can be queried for ZEC data.
#[async_trait]
pub trait Exchange: 'static {
#[trait_variant::make(Exchange: Send)]
#[dynosaur::dynosaur(DynExchange = dyn Exchange)]
#[dynosaur::dynosaur(DynLocalExchange = dyn LocalExchange)]
pub trait LocalExchange {
/// Queries data about the USD/ZEC pair.
///
/// The returned bid and ask data must be denominated in USD, i.e. the latest bid and
Expand Down Expand Up @@ -55,8 +56,8 @@ impl ExchangeData {

/// A set of [`Exchange`]s that can be queried for ZEC data.
pub struct Exchanges {
trusted: Box<dyn Exchange>,
others: Vec<Box<dyn Exchange>>,
trusted: Box<DynExchange<'static>>,
others: Vec<Box<DynExchange<'static>>>,
}

impl Exchanges {
Expand All @@ -78,7 +79,7 @@ impl Exchanges {
///
/// The `trusted` exchange will always have its data used, _if_ data is successfully
/// obtained via Tor (i.e. no transient failures).
pub fn builder(trusted: impl Exchange) -> ExchangesBuilder {
pub fn builder(trusted: impl Exchange + 'static) -> ExchangesBuilder {

Check warning on line 82 in zcash_client_backend/src/tor/http/cryptex.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_backend/src/tor/http/cryptex.rs#L82

Added line #L82 was not covered by tests
ExchangesBuilder::new(trusted)
}
}
Expand All @@ -105,16 +106,16 @@ impl ExchangesBuilder {
///
/// The `trusted` exchange will always have its data used, _if_ data is successfully
/// obtained via Tor (i.e. no transient failures).
pub fn new(trusted: impl Exchange) -> Self {
pub fn new(trusted: impl Exchange + 'static) -> Self {

Check warning on line 109 in zcash_client_backend/src/tor/http/cryptex.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_backend/src/tor/http/cryptex.rs#L109

Added line #L109 was not covered by tests
Self(Exchanges {
trusted: Box::new(trusted),
trusted: DynExchange::boxed(trusted),

Check warning on line 111 in zcash_client_backend/src/tor/http/cryptex.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_backend/src/tor/http/cryptex.rs#L111

Added line #L111 was not covered by tests
others: vec![],
})
}

/// Adds another [`Exchange`] as a data source.
pub fn with(mut self, other: impl Exchange) -> Self {
self.0.others.push(Box::new(other));
pub fn with(mut self, other: impl Exchange + 'static) -> Self {
self.0.others.push(DynExchange::boxed(other));

Check warning on line 118 in zcash_client_backend/src/tor/http/cryptex.rs

View check run for this annotation

Codecov / codecov/patch

zcash_client_backend/src/tor/http/cryptex.rs#L117-L118

Added lines #L117 - L118 were not covered by tests
self
}

Expand Down
2 changes: 0 additions & 2 deletions zcash_client_backend/src/tor/http/cryptex/binance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use rust_decimal::Decimal;
use serde::Deserialize;

Expand Down Expand Up @@ -44,7 +43,6 @@ struct BinanceData {
count: u32,
}

#[async_trait]
impl Exchange for Binance {
async fn query_zec_to_usd(&self, client: &Client) -> Result<ExchangeData, Error> {
// API documentation:
Expand Down
2 changes: 0 additions & 2 deletions zcash_client_backend/src/tor/http/cryptex/coinbase.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use rust_decimal::Decimal;
use serde::Deserialize;

Expand Down Expand Up @@ -31,7 +30,6 @@ struct CoinbaseData {
conversions_volume: Option<Decimal>,
}

#[async_trait]
impl Exchange for Coinbase {
#[allow(dead_code)]
async fn query_zec_to_usd(&self, client: &Client) -> Result<ExchangeData, Error> {
Expand Down
2 changes: 0 additions & 2 deletions zcash_client_backend/src/tor/http/cryptex/gate_io.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use hyper::StatusCode;
use rust_decimal::Decimal;
use serde::Deserialize;
Expand Down Expand Up @@ -32,7 +31,6 @@ struct GateIoData {
low_24h: Decimal,
}

#[async_trait]
impl Exchange for GateIo {
async fn query_zec_to_usd(&self, client: &Client) -> Result<ExchangeData, Error> {
// API documentation:
Expand Down
2 changes: 0 additions & 2 deletions zcash_client_backend/src/tor/http/cryptex/gemini.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use rust_decimal::Decimal;
use serde::Deserialize;

Expand Down Expand Up @@ -30,7 +29,6 @@ struct GeminiData {
ask: Decimal,
}

#[async_trait]
impl Exchange for Gemini {
async fn query_zec_to_usd(&self, client: &Client) -> Result<ExchangeData, Error> {
// API documentation:
Expand Down
2 changes: 0 additions & 2 deletions zcash_client_backend/src/tor/http/cryptex/ku_coin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use rust_decimal::Decimal;
use serde::Deserialize;

Expand Down Expand Up @@ -46,7 +45,6 @@ struct KuCoinResponse {
data: KuCoinData,
}

#[async_trait]
impl Exchange for KuCoin {
async fn query_zec_to_usd(&self, client: &Client) -> Result<ExchangeData, Error> {
// API documentation:
Expand Down
2 changes: 0 additions & 2 deletions zcash_client_backend/src/tor/http/cryptex/mexc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use rust_decimal::Decimal;
use serde::Deserialize;

Expand Down Expand Up @@ -39,7 +38,6 @@ struct MexcData {
closeTime: u64,
}

#[async_trait]
impl Exchange for Mexc {
async fn query_zec_to_usd(&self, client: &Client) -> Result<ExchangeData, Error> {
// API documentation:
Expand Down

0 comments on commit f347dc5

Please sign in to comment.