Skip to content

Commit

Permalink
Merge branch 'main' into fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
h4sh3d authored May 30, 2024
2 parents 18bff92 + 519878d commit 7ae532a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ description = "RPC client for Monero daemon and wallet"
[dependencies]
anyhow = "1"
chrono = { version = "0.4", default-features = false, features = ["serde"] }
diqwest = { version = "1.1", optional = true }
diqwest = { version = "1.2.1", default-features = false, optional = true }
fixed-hash = "0.8"
hex = "0.4"
http = "0.2"
jsonrpc-core = "18"
monero = { version = "0.19", features = ["serde"] }
reqwest = { version = "0.11", features = ["json", "socks"] }
reqwest = { version = "0.11", default-features = false, features = ["json", "socks"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tracing = "0.1"
Expand All @@ -33,7 +33,11 @@ serde_test = "1.0"
tokio = { version = "1.12.0", features = ["full"] }

[features]
default = ["native-tls"]

rpc_authentication = ["diqwest"]
native-tls = ["reqwest/native-tls", "diqwest/default"]
rustls-tls = ["reqwest/rustls-tls-native-roots", "diqwest/rustls-tls"]

[package.metadata.docs.rs]
all-features = true
Expand Down
51 changes: 44 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub use self::{models::*, util::*};

use jsonrpc_core::types::{Id, *};
use monero::{
blockdata::block::Block,
consensus::deserialize,
cryptonote::{hash::Hash as CryptoNoteHash, subaddress},
util::{address::PaymentId, amount},
Address, Amount,
Expand All @@ -77,6 +79,7 @@ use std::{
num::NonZeroU64,
ops::{Deref, RangeInclusive},
sync::Arc,
time::Duration,
};
use tracing::*;
use uuid::Uuid;
Expand Down Expand Up @@ -248,6 +251,7 @@ struct RpcClientConfig {
#[cfg_attr(docsrs, doc(cfg(feature = "rpc_authentication")))]
rpc_auth: RpcAuthentication,
proxy_address: Option<String>,
timeout: Option<Duration>,
}

/// Builder for generating a configured [`RpcClient`].
Expand All @@ -270,6 +274,7 @@ impl RpcClientBuilder {
#[cfg(feature = "rpc_authentication")]
rpc_auth: RpcAuthentication::None,
proxy_address: None,
timeout: None,
},
}
}
Expand All @@ -288,17 +293,23 @@ impl RpcClientBuilder {
self
}

/// Configures default request timeout.
pub fn timeout(mut self, timeout: Duration) -> Self {
self.config.timeout = Some(timeout);
self
}

/// Build and return the fully configured RPC client.
pub fn build(self, addr: impl Into<String>) -> anyhow::Result<RpcClient> {
let config = self.config;
let http_client_builder = reqwest::ClientBuilder::new();
let http_client = if let Some(proxy_address) = config.proxy_address {
http_client_builder
.proxy(reqwest::Proxy::all(proxy_address)?)
.build()?
} else {
http_client_builder.build()?
let mut http_client_builder = reqwest::ClientBuilder::new();
if let Some(proxy_address) = config.proxy_address {
http_client_builder = http_client_builder.proxy(reqwest::Proxy::all(proxy_address)?);
};
if let Some(timeout) = config.timeout {
http_client_builder = http_client_builder.timeout(timeout);
};
let http_client = http_client_builder.build()?;
Ok(RpcClient {
inner: CallerWrapper(Arc::new(RemoteCaller {
http_client,
Expand Down Expand Up @@ -400,6 +411,32 @@ impl DaemonJsonRpcClient {
.count)
}

pub async fn get_block(&self, selector: GetBlockHeaderSelector) -> anyhow::Result<Block> {
let (request, params) = match selector {
GetBlockHeaderSelector::Hash(hash) => (
"get_block",
RpcParams::map(
Some(("hash", serde_json::to_value(HashString(hash)).unwrap())).into_iter(),
),
),
GetBlockHeaderSelector::Height(height) => (
"get_block",
RpcParams::map(Some(("height", height.into())).into_iter()),
),
GetBlockHeaderSelector::Last => ("get_block", RpcParams::None),
};

match self.inner.request::<Value>(request, params).await {
Ok(res) => {
let block_str = res["blob"].as_str().unwrap();
let block_hex = hex::decode(block_str).unwrap();
let block: Block = deserialize(&block_hex).unwrap();
Ok(block)
}
Err(e) => Err(anyhow::Error::msg(format!("Can not fetch block: {}", e))),
}
}

/// Look up a block's hash by its height.
pub async fn on_get_block_hash(&self, height: u64) -> anyhow::Result<BlockHash> {
let res = self
Expand Down
2 changes: 1 addition & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// TODO: remove this when clippy error goes away...
#![allow(clippy::incorrect_clone_impl_on_copy_type)]
#![allow(clippy::non_canonical_clone_impl)]

use crate::util::*;
use chrono::prelude::*;
Expand Down

0 comments on commit 7ae532a

Please sign in to comment.