From 23806125586e999a48429d57d2d94a2a3cab696b Mon Sep 17 00:00:00 2001 From: Chris Guida Date: Fri, 10 Nov 2023 11:30:32 -0600 Subject: [PATCH 1/5] add scanblocks call --- client/src/client.rs | 8 ++++++++ json/src/lib.rs | 24 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/client/src/client.rs b/client/src/client.rs index 47c648c6..2faf439e 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -1255,12 +1255,20 @@ pub trait RpcApi: Sized { } } + fn scan_blocks_blocking( + &self, + descriptors: &[json::ScanBlocksRequest], + ) -> Result { + self.call("scanblocks", &["start".into(), into_json(descriptors)?]) + } + fn scan_tx_out_set_blocking( &self, descriptors: &[json::ScanTxOutRequest], ) -> Result { self.call("scantxoutset", &["start".into(), into_json(descriptors)?]) } + } /// Client implements a JSON-RPC client for the Bitcoin Core daemon or compatible APIs. diff --git a/json/src/lib.rs b/json/src/lib.rs index f6b13675..041c4e53 100644 --- a/json/src/lib.rs +++ b/json/src/lib.rs @@ -30,7 +30,7 @@ use bitcoin::block::Version; use bitcoin::consensus::encode; use bitcoin::hashes::hex::FromHex; use bitcoin::hashes::sha256; -use bitcoin::{Address, Amount, PrivateKey, PublicKey, SignedAmount, Transaction, ScriptBuf, Script, bip158, bip32, Network}; +use bitcoin::{Address, Amount, PrivateKey, PublicKey, SignedAmount, Transaction, ScriptBuf, Script, bip158, bip32, Network, BlockHash}; use serde::de::Error as SerdeError; use serde::{Deserialize, Serialize}; use std::fmt; @@ -2087,6 +2087,28 @@ pub enum PubKeyOrAddress<'a> { PubKey(&'a PublicKey), } +#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] +#[serde(untagged)] +/// Start a scan of the UTXO set for an [output descriptor](https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md). +pub enum ScanBlocksRequest { + /// Scan for a single descriptor + Single(String), + /// Scan for a descriptor with xpubs + Extended { + /// Descriptor + desc: String, + /// Range of the xpub derivations to scan + range: Option<(u64, u64)>, + }, +} + +#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] +pub struct ScanBlocksResult { + pub from_height: u64, + pub to_height: u64, + pub relevant_blocks: Vec, +} + #[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] #[serde(untagged)] /// Start a scan of the UTXO set for an [output descriptor](https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md). From db2d9809727140086a6cba645983086f4076378e Mon Sep 17 00:00:00 2001 From: Chris Guida Date: Sat, 11 Nov 2023 17:43:15 -0600 Subject: [PATCH 2/5] wip fix timeout --- client/src/client.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/client/src/client.rs b/client/src/client.rs index 2faf439e..d061a7fd 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -13,6 +13,7 @@ use std::fs::File; use std::io::{BufRead, BufReader}; use std::iter::FromIterator; use std::path::PathBuf; +use std::time::Duration; use std::{fmt, result}; use crate::{bitcoin, deserialize_hex}; @@ -1288,7 +1289,16 @@ impl Client { /// Can only return [Err] when using cookie authentication. pub fn new(url: &str, auth: Auth) -> Result { let (user, pass) = auth.get_user_pass()?; - jsonrpc::client::Client::simple_http(url, user, pass) + jsonrpc::client::Client::simple_http(url, user, pass, None) + .map(|client| Client { + client, + }) + .map_err(|e| super::error::Error::JsonRpc(e.into())) + } + + pub fn new_with_timeout(url: &str, auth: Auth, timeout: Duration) -> Result { + let (user, pass) = auth.get_user_pass()?; + jsonrpc::client::Client::simple_http(url, user, pass, Some(timeout)) .map(|client| Client { client, }) From 1e6ee5f33235b30e5b0e5c6bf582e13462e26382 Mon Sep 17 00:00:00 2001 From: Chris Guida Date: Sat, 11 Nov 2023 19:59:54 -0600 Subject: [PATCH 3/5] update jsonrpc dep --- client/Cargo.toml | 4 +++- client/src/client.rs | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/client/Cargo.toml b/client/Cargo.toml index 7b55d406..05068acd 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -22,7 +22,9 @@ path = "src/lib.rs" bitcoincore-rpc-json = { version = "0.17.0", path = "../json" } log = "0.4.5" -jsonrpc = "0.14.0" +# jsonrpc = "0.14.0" +# jsonrpc = { path = "../../rust-jsonrpc" } +jsonrpc = { git = "https://github.com/chrisguida/rust-jsonrpc", branch = "feat/simple-http-timeout" } # Used for deserialization of JSON. serde = "1" diff --git a/client/src/client.rs b/client/src/client.rs index d061a7fd..4eb32e04 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -1289,7 +1289,7 @@ impl Client { /// Can only return [Err] when using cookie authentication. pub fn new(url: &str, auth: Auth) -> Result { let (user, pass) = auth.get_user_pass()?; - jsonrpc::client::Client::simple_http(url, user, pass, None) + jsonrpc::client::Client::simple_http(url, user, pass) .map(|client| Client { client, }) @@ -1298,7 +1298,7 @@ impl Client { pub fn new_with_timeout(url: &str, auth: Auth, timeout: Duration) -> Result { let (user, pass) = auth.get_user_pass()?; - jsonrpc::client::Client::simple_http(url, user, pass, Some(timeout)) + jsonrpc::client::Client::simple_http_with_timeout(url, user, pass, Some(timeout)) .map(|client| Client { client, }) From 23927f3e8b3bd413e0bf8b7c42b9531345bdd1a3 Mon Sep 17 00:00:00 2001 From: Chris Guida Date: Sun, 12 Nov 2023 16:52:28 -0600 Subject: [PATCH 4/5] flesh out scanblocks params --- client/src/client.rs | 14 ++++++++++++-- json/src/lib.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/client/src/client.rs b/client/src/client.rs index 4eb32e04..34f8b31a 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -1258,9 +1258,19 @@ pub trait RpcApi: Sized { fn scan_blocks_blocking( &self, - descriptors: &[json::ScanBlocksRequest], + request: json::ScanBlocksRequest, ) -> Result { - self.call("scanblocks", &["start".into(), into_json(descriptors)?]) + self.call( + "scanblocks", + &[ + "start".into(), + into_json(request.scanobjects)?, + into_json(request.start_height)?, + into_json(request.stop_height)?, + into_json(request.filtertype)?, + into_json(request.options)?, + ], + ) } fn scan_tx_out_set_blocking( diff --git a/json/src/lib.rs b/json/src/lib.rs index 041c4e53..5c0832dd 100644 --- a/json/src/lib.rs +++ b/json/src/lib.rs @@ -2087,10 +2087,32 @@ pub enum PubKeyOrAddress<'a> { PubKey(&'a PublicKey), } +#[derive(Serialize, Clone, PartialEq, Eq, Debug)] +/// Start a scan of the block filter index for an [output descriptor](https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md). +pub struct ScanBlocksRequest<'a> { + /// List of descriptors to scan + pub scanobjects: &'a [ScanBlocksRequestDescriptor], + /// Height at which to start scanning + pub start_height: Option, + /// Height at which to stop scanning + pub stop_height: Option, + /// Filter type. Only "basic" is supported for now. + pub filtertype: Option, + /// Additional scan options. Only "filter_false_positives" is supported for now. + pub options: Option, +} + +#[derive(Serialize, Clone, PartialEq, Eq, Debug)] +/// Options struct for `scanblocks` rpc +pub struct ScanBlocksOptions { + /// Scan for a single descriptor + pub filter_false_positives: Option, +} + #[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] #[serde(untagged)] -/// Start a scan of the UTXO set for an [output descriptor](https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md). -pub enum ScanBlocksRequest { +/// Descriptors to scan in `scanblocks` rpc +pub enum ScanBlocksRequestDescriptor { /// Scan for a single descriptor Single(String), /// Scan for a descriptor with xpubs From 20fcea324b7a4665866148343ecc1796deb086ef Mon Sep 17 00:00:00 2001 From: Chris Guida Date: Tue, 1 Oct 2024 21:36:26 -0600 Subject: [PATCH 5/5] add status subcommand to scanblocks rpc method --- client/src/client.rs | 4 ++++ json/src/lib.rs | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/client/src/client.rs b/client/src/client.rs index 34f8b31a..c756698a 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -1273,6 +1273,10 @@ pub trait RpcApi: Sized { ) } + fn scan_blocks_status(&self) -> Result> { + opt_result(self.call("scanblocks",&["status".into(),],)?) + } + fn scan_tx_out_set_blocking( &self, descriptors: &[json::ScanTxOutRequest], diff --git a/json/src/lib.rs b/json/src/lib.rs index 5c0832dd..4926c53b 100644 --- a/json/src/lib.rs +++ b/json/src/lib.rs @@ -2131,6 +2131,12 @@ pub struct ScanBlocksResult { pub relevant_blocks: Vec, } +#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] +pub struct ScanBlocksStatusResult { + pub progress: Option, + pub current_height: Option, +} + #[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] #[serde(untagged)] /// Start a scan of the UTXO set for an [output descriptor](https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md).