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

feat/fix: add new Client builder method with an arbitrary timeout value & add new method for scantxoutset status #365

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::io::{BufRead, BufReader};
use std::iter::FromIterator;
use std::path::PathBuf;
use std::{fmt, result};
use std::time::Duration;

use crate::bitcoin;
use crate::bitcoin::consensus::encode;
Expand Down Expand Up @@ -1256,6 +1257,27 @@ pub trait RpcApi: Sized {
}
}

// Return the status of a UTXO set scan
fn scan_tx_out_set_status(&self) -> Result<Option<json::ScanTxOutStatusResult>> {
match self.call("scantxoutset", &["status".into()]) {
Ok(response) => {
let response: serde_json::Value = response;
if response.is_null() {
Ok(None)
} else {
let result: json::ScanTxOutStatusResult = serde_json::from_value(response)?;
Ok(Some(result))
}
}
Err(e) => Err(e),
}
}

// Abort a UTXO set scan
fn scan_tx_out_set_abort(&self) -> Result<bool> {
self.call("scantxoutset", &["abort".into()])
}

fn scan_tx_out_set_blocking(
&self,
descriptors: &[json::ScanTxOutRequest],
Expand Down Expand Up @@ -1293,6 +1315,28 @@ impl Client {
.map_err(|e| super::error::Error::JsonRpc(e.into()))
}

/// Creates a client to a bitcoind JSON-RPC server with a custom timeout value, in seconds.
/// Useful when making an RPC that can take a long time e.g. scantxoutset
pub fn new_with_custom_timeout(url: &str, auth: Auth, timeout: u64) -> result::Result<Self, Error> {
let (user, pass) = auth.get_user_pass()?;

let user = user.unwrap();
let pass = pass.unwrap();

let transport =
jsonrpc::simple_http::Builder::new()
.timeout(Duration::from_secs(timeout))
.url(url)
.unwrap()
.auth(user, Some(pass))
.build();

let client = jsonrpc::client::Client::with_transport(transport);

Ok(Client{ client })

}

/// Create a new Client using the given [jsonrpc::Client].
pub fn from_jsonrpc(client: jsonrpc::client::Client) -> Client {
Client {
Expand Down
6 changes: 6 additions & 0 deletions json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,12 @@ pub enum PubKeyOrAddress<'a> {
PubKey(&'a PublicKey),
}

/// Used to represent the status of a UTXO set scan.
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
pub struct ScanTxOutStatusResult {
pub progress: u8,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
#[serde(untagged)]
/// Start a scan of the UTXO set for an [output descriptor](https:/bitcoin/bitcoin/blob/master/doc/descriptors.md).
Expand Down
Loading