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

Merges rust-lightning's lightning-block-sync HTTP logic #201

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
members = [
"json",
"client",
"rpc",
"integration_test",
]
5 changes: 3 additions & 2 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ path = "src/lib.rs"

[dependencies]
bitcoincore-rpc-json = { version = "0.13.0", path = "../json" }
bitcoincore-rpc-rpc = { version = "0.13.0", path = "../rpc" }

log = "0.4.5"
jsonrpc = "0.12.0"
futures = { version = "0.3" }

# Used for deserialization of JSON.
serde = "1"
serde_json = "1"
serde_json = { version = "1.0", features = ["raw_value"] }
5 changes: 2 additions & 3 deletions client/examples/retry_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
//

extern crate bitcoincore_rpc;
extern crate jsonrpc;
extern crate serde;
extern crate serde_json;

use bitcoincore_rpc::{Client, Error, Result, RpcApi};
use bitcoincore_rpc::{rpc, Client, Error, Result, RpcApi};

pub struct RetryClient {
client: Client,
Expand All @@ -31,7 +30,7 @@ impl RpcApi for RetryClient {
for _ in 0..RETRY_ATTEMPTS {
match self.client.call(cmd, args) {
Ok(ret) => return Ok(ret),
Err(Error::JsonRpc(jsonrpc::error::Error::Rpc(ref rpcerr)))
Err(Error::JsonRpc(rpc::http::Error::Rpc(ref rpcerr)))
if rpcerr.code == -28 =>
{
::std::thread::sleep(::std::time::Duration::from_millis(INTERVAL));
Expand Down
56 changes: 24 additions & 32 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::path::PathBuf;
use std::{fmt, result};

use bitcoin;
use jsonrpc;
use rpc;
use serde;
use serde_json;

Expand Down Expand Up @@ -1082,12 +1082,12 @@ pub trait RpcApi: Sized {

/// Client implements a JSON-RPC client for the Bitcoin Core daemon or compatible APIs.
pub struct Client {
client: jsonrpc::client::Client,
client: rpc::rpc::RpcClient,
}

impl fmt::Debug for Client {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "bitcoincore_rpc::Client({:?})", self.client)
write!(f, "RpcClient({:?})", self.client)
}
}

Expand All @@ -1097,73 +1097,65 @@ impl Client {
/// Can only return [Err] when using cookie authentication.
pub fn new(url: &str, auth: Auth) -> Result<Self> {
let (user, pass) = auth.get_user_pass()?;
jsonrpc::client::Client::simple_http(url, user, pass)
rpc::rpc::RpcClient::from_url(user, pass, url)
.map(|client| Client {
client,
})
.map_err(|e| super::error::Error::JsonRpc(e.into()))
.map_err(|e| super::error::Error::Io(e.into()))
}

/// Create a new Client using the given [jsonrpc::Client].
pub fn from_jsonrpc(client: jsonrpc::client::Client) -> Client {
pub fn from_jsonrpc(client: rpc::rpc::RpcClient) -> Client {
Client {
client,
}
}

/// Get the underlying JSONRPC client.
pub fn get_jsonrpc_client(&self) -> &jsonrpc::client::Client {
pub fn get_jsonrpc_client(&self) -> &rpc::rpc::RpcClient {
&self.client
}
}

// FIXME: Temporary
use futures::executor::block_on;

impl RpcApi for Client {
/// Call an `cmd` rpc with given `args` list
fn call<T: for<'a> serde::de::Deserialize<'a>>(
&self,
cmd: &str,
args: &[serde_json::Value],
) -> Result<T> {
let raw_args: Vec<_> = args
.iter()
.map(|a| {
let json_string = serde_json::to_string(a)?;
serde_json::value::RawValue::from_string(json_string) // we can't use to_raw_value here due to compat with Rust 1.29
})
.map(|a| a.map_err(|e| Error::Json(e)))
.collect::<Result<Vec<_>>>()?;
let req = self.client.build_request(&cmd, &raw_args);
if log_enabled!(Debug) {
debug!(target: "bitcoincore_rpc", "JSON-RPC request: {} {}", cmd, serde_json::Value::from(args));
}

let resp = self.client.send_request(req).map_err(Error::from);
let resp = block_on(self.client.call_method(cmd, args)).map_err(Error::from);
log_response(cmd, &resp);
Ok(resp?.result()?)
Ok(resp?)
}
}

fn log_response(cmd: &str, resp: &Result<jsonrpc::Response>) {
fn log_response<T>(cmd: &str, resp: &Result<T>) {
if log_enabled!(Warn) || log_enabled!(Debug) || log_enabled!(Trace) {
match resp {
Err(ref e) => {
if log_enabled!(Debug) {
debug!(target: "bitcoincore_rpc", "JSON-RPC failed parsing reply of {}: {:?}", cmd, e);
match e {
Error::JsonRpc(ref e) => {
debug!(target: "bitcoincore_rpc", "JSON-RPC error for {}: {:?}", cmd, e)
}
_ => {
debug!(target: "bitcoincore_rpc", "JSON-RPC failed parsing reply of {}: {:?}", cmd, e)
}
}
}
}
Ok(ref resp) => {
if let Some(ref e) = resp.error {
if log_enabled!(Debug) {
debug!(target: "bitcoincore_rpc", "JSON-RPC error for {}: {:?}", cmd, e);
}
} else if log_enabled!(Trace) {
// we can't use to_raw_value here due to compat with Rust 1.29
let def = serde_json::value::RawValue::from_string(
serde_json::Value::Null.to_string(),
)
.unwrap();
let result = resp.result.as_ref().unwrap_or(&def);
trace!(target: "bitcoincore_rpc", "JSON-RPC response for {}: {}", cmd, result);
if log_enabled!(Trace) {
// FIXME: Log this. Not sure how to make T implement Display.
//trace!(target: "bitcoincore_rpc", "JSON-RPC response for {}: {}", cmd, resp);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use std::{error, fmt, io};
use bitcoin;
use bitcoin::hashes::hex;
use bitcoin::secp256k1;
use jsonrpc;
use rpc;
use serde_json;

/// The error type for errors produced in this library.
#[derive(Debug)]
pub enum Error {
JsonRpc(jsonrpc::error::Error),
JsonRpc(rpc::http::Error),
Hex(hex::Error),
Json(serde_json::error::Error),
BitcoinSerialization(bitcoin::consensus::encode::Error),
Expand All @@ -31,8 +31,8 @@ pub enum Error {
UnexpectedStructure,
}

impl From<jsonrpc::error::Error> for Error {
fn from(e: jsonrpc::error::Error) -> Error {
impl From<rpc::http::Error> for Error {
fn from(e: rpc::http::Error) -> Error {
Error::JsonRpc(e)
}
}
Expand Down
7 changes: 5 additions & 2 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ extern crate log;
#[allow(unused)]
#[macro_use] // `macro_use` is needed for v1.24.0 compilation.
extern crate serde;
extern crate futures;
extern crate serde_json;

pub extern crate jsonrpc;

pub extern crate bitcoincore_rpc_json;
pub use bitcoincore_rpc_json as json;
pub use json::bitcoin;

pub extern crate bitcoincore_rpc_rpc;
pub use bitcoincore_rpc_rpc as rpc;

mod client;
mod error;
mod queryable;

pub use client::*;
pub use error::Error;
pub use queryable::*;
pub use rpc::http::HttpEndpoint;
2 changes: 1 addition & 1 deletion integration_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern crate log;
use std::collections::HashMap;

use bitcoincore_rpc::json;
use bitcoincore_rpc::jsonrpc::error::Error as JsonRpcError;
use bitcoincore_rpc::rpc::http::Error as JsonRpcError;
use bitcoincore_rpc::{Auth, Client, Error, RpcApi};

use bitcoin::consensus::encode::{deserialize, serialize};
Expand Down
26 changes: 26 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "bitcoincore-rpc-rpc"
version = "0.13.0"
authors = ["Jeffrey Czyz", "Matt Corallo", "Sergi Delgado"]
license = "CC0-1.0"
homepage = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc/"
repository = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc/"
keywords = [ "crypto", "bitcoin", "bitcoin-core", "rpc" ]
readme = "README.md"
edition = "2018"

[lib]
name = "bitcoincore_rpc_rpc"
path = "src/lib.rs"


[dependencies]
bitcoin = "0.26"
chunked_transfer = { version = "1.4" }
base64 = "0.13.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }


[dev-dependencies]
tokio = { version = "1.0", features = [ "macros", "rt" ] }
Loading