Skip to content
Merged
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
3 changes: 3 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub trait ElectrumApi {
self.transaction_broadcast_raw(&buffer)
}

/// Executes the requested API call returning the raw answer.
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this leads to calling method like raw_call(&("method_name".to_owned(), vec![])), which is very non-rust ideomatic. Thus I propose a follow-up PR with changing function signature to raw_call(&self, method_name: &str, params: impl IntoIterator<Item=Param>).

This, however, will be a breaking change and will require a new minor version bump.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #84 to implement your proposal


/// Execute a queue of calls stored in a [`Batch`](../batch/struct.Batch.html) struct. Returns
/// `Ok()` **only if** all of the calls are successful. The order of the JSON `Value`s returned
/// reflects the order in which the calls were made on the `Batch` struct.
Expand Down
4 changes: 2 additions & 2 deletions src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use bitcoin::hashes::hex::ToHex;
use bitcoin::{Script, Txid};

use types::{Param, ToElectrumScriptHash};
use types::{Call, Param, ToElectrumScriptHash};

/// Helper structure that caches all the requests before they are actually sent to the server.
///
Expand All @@ -18,7 +18,7 @@ use types::{Param, ToElectrumScriptHash};
/// [`batch_script_get_balance`](../client/struct.Client.html#method.batch_script_get_balance) to ask the
/// server for the balance of multiple scripts with a single request.
pub struct Batch {
calls: Vec<(String, Vec<Param>)>,
calls: Vec<Call>,
}

impl Batch {
Expand Down
5 changes: 5 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ impl Client {
}

impl ElectrumApi for Client {
#[inline]
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error> {
impl_inner_call!(self, raw_call, call)
}

#[inline]
fn batch_call(&self, batch: &Batch) -> Result<Vec<serde_json::Value>, Error> {
impl_inner_call!(self, batch_call, batch)
Expand Down
42 changes: 42 additions & 0 deletions src/raw_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,17 @@ impl<S: Read + Write> RawClient<S> {
}

impl<T: Read + Write> ElectrumApi for RawClient<T> {
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error> {
let req = Request::new_id(
self.last_id.fetch_add(1, Ordering::SeqCst),
&call.0,
call.1.to_vec(),
);
let result = self.call(req)?;

Ok(result)
}

fn batch_call(&self, batch: &Batch) -> Result<Vec<serde_json::Value>, Error> {
let mut raw = Vec::new();

Expand Down Expand Up @@ -1283,4 +1294,35 @@ mod test {
assert!(client.transaction_broadcast_raw(&[0x00]).is_err());
assert!(client.server_features().is_ok());
}

#[test]
fn test_raw_call() {
use types::Param;

let client = RawClient::new(get_test_server(), None).unwrap();

let params = vec![
Param::String(
"cc2ca076fd04c2aeed6d02151c447ced3d09be6fb4d4ef36cb5ed4e7a3260566".to_string(),
),
Param::Bool(false),
];
let call = ("blockchain.transaction.get".to_string(), params);

let resp = client.raw_call(&call).unwrap();

assert_eq!(
resp,
"01000000000101000000000000000000000000000000000000000000000000000\
0000000000000ffffffff5403f09c091b4d696e656420627920416e74506f6f6c3\
13139ae006f20074d6528fabe6d6d2ab1948d50b3d991e2a0821df74358ed9c255\
3af00c7a61f97771ca0acee106e0400000000000000cbec00802461f905fffffff\
f0354ceac2a000000001976a91411dbe48cc6b617f9c6adaf4d9ed5f625b1c7cb5\
988ac0000000000000000266a24aa21a9ed2e578bce2ca6c6bc9359377345d8e98\
5dd5f90c78421ffa6efa5eb60428e698c0000000000000000266a24b9e11b6d2f6\
21d7ec3f45a5eca89d3ea6a294cdf3a042e973009584470a12916111e2caa01200\
000000000000000000000000000000000000000000000000000000000000000000\
00000"
)
}
}
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use serde::{de, Deserialize, Serialize};

static JSONRPC_2_0: &str = "2.0";

pub type Call = (String, Vec<Param>);

#[derive(Serialize, Clone)]
#[serde(untagged)]
/// A single parameter of a [`Request`](struct.Request.html)
Expand Down