Skip to content

[POC] generalized requests to support sync and async #212

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

Closed
wants to merge 2 commits into from
Closed
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: 0 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ jobs:
include:
- rust: stable
env:
RUSTFMTCHK: true
- rust: nightly
env:
RUSTFMTCHK: false
- rust: 1.41.1
env:
RUSTFMTCHK: false
steps:
- name: Checkout Crate
uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ members = [
"client",
"integration_test",
]

[patch."crates-io"]
bitcoin = { git = "https://github.com/stevenroose/rust-bitcoin", branch = "dev" }
bitcoin_hashes = { git = "https://github.com/stevenroose/rust-bitcoin", branch = "dev" }
13 changes: 9 additions & 4 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bitcoincore-rpc"
version = "0.17.0"
version = "1.0.0-rc0"
authors = [
"Steven Roose <[email protected]>",
"Jean Pierre Dudey <[email protected]>",
Expand All @@ -18,13 +18,18 @@ edition = "2018"
name = "bitcoincore_rpc"
path = "src/lib.rs"

[features]
default = [ "async" ]
async = [ "async-trait" ]

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

log = "0.4.5"
jsonrpc = "0.14.0"
jsonrpc = { git = "https://github.com/stevenroose/rust-jsonrpc", branch = "request" }

# Used for deserialization of JSON.
serde = "1"
serde_json = "1"
bitcoin-private = "0.1.0"

async-trait = { version = "0.1.53", optional = true }
80 changes: 40 additions & 40 deletions client/examples/retry_client.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//
//// To the extent possible under law, the author(s) have dedicated all
//// copyright and related and neighboring rights to this software to
//// the public domain worldwide. This software is distributed without
//// any warranty.
////
//// You should have received a copy of the CC0 Public Domain Dedication
//// along with this software.
//// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
////

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

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

pub struct RetryClient {
client: Client,
}
//pub struct RetryClient {
// client: Client,
//}

const INTERVAL: u64 = 1000;
const RETRY_ATTEMPTS: u8 = 10;
//const INTERVAL: u64 = 1000;
//const RETRY_ATTEMPTS: u8 = 10;

impl RpcApi for RetryClient {
fn call<T: for<'a> serde::de::Deserialize<'a>>(
&self,
cmd: &str,
args: &[serde_json::Value],
) -> Result<T> {
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)))
if rpcerr.code == -28 =>
{
::std::thread::sleep(::std::time::Duration::from_millis(INTERVAL));
continue;
}
Err(e) => return Err(e),
}
}
self.client.call(cmd, args)
}
}
//impl RpcApi for RetryClient {
// fn call<T: for<'a> serde::de::Deserialize<'a>>(
// &self,
// cmd: &str,
// args: &[serde_json::Value],
// ) -> Result<T> {
// 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)))
// if rpcerr.code == -28 =>
// {
// ::std::thread::sleep(::std::time::Duration::from_millis(INTERVAL));
// continue;
// }
// Err(e) => return Err(e),
// }
// }
// self.client.call(cmd, args)
// }
//}

fn main() {}
98 changes: 50 additions & 48 deletions client/examples/test_against_node.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//

//! A very simple example used as a self-test of this library against a Bitcoin
//! Core node.
extern crate bitcoincore_rpc;

use bitcoincore_rpc::{bitcoin, Auth, Client, Error, RpcApi};

fn main_result() -> Result<(), Error> {
let mut args = std::env::args();

let _exe_name = args.next().unwrap();

let url = args.next().expect("Usage: <rpc_url> <username> <password>");
let user = args.next().expect("no user given");
let pass = args.next().expect("no pass given");

let rpc = Client::new(&url, Auth::UserPass(user, pass)).unwrap();

let _blockchain_info = rpc.get_blockchain_info()?;

let best_block_hash = rpc.get_best_block_hash()?;
println!("best block hash: {}", best_block_hash);
let bestblockcount = rpc.get_block_count()?;
println!("best block height: {}", bestblockcount);
let best_block_hash_by_height = rpc.get_block_hash(bestblockcount)?;
println!("best block hash by height: {}", best_block_hash_by_height);
assert_eq!(best_block_hash_by_height, best_block_hash);

let bitcoin_block: bitcoin::Block = rpc.get_by_id(&best_block_hash)?;
println!("best block hash by `get`: {}", bitcoin_block.header.prev_blockhash);
let bitcoin_tx: bitcoin::Transaction = rpc.get_by_id(&bitcoin_block.txdata[0].txid())?;
println!("tx by `get`: {}", bitcoin_tx.txid());

Ok(())
}

fn main() {
main_result().unwrap();
}
//// To the extent possible under law, the author(s) have dedicated all
//// copyright and related and neighboring rights to this software to
//// the public domain worldwide. This software is distributed without
//// any warranty.
////
//// You should have received a copy of the CC0 Public Domain Dedication
//// along with this software.
//// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
////

////! A very simple example used as a self-test of this library against a Bitcoin
////! Core node.
//extern crate bitcoincore_rpc;

//use bitcoincore_rpc::{bitcoin, Auth, Client, Error, RpcApi};

//fn main_result() -> Result<(), Error> {
// let mut args = std::env::args();

// let _exe_name = args.next().unwrap();

// let url = args.next().expect("Usage: <rpc_url> <username> <password>");
// let user = args.next().expect("no user given");
// let pass = args.next().expect("no pass given");

// let rpc = Client::new(&url, Auth::UserPass(user, pass)).unwrap();

// let _blockchain_info = rpc.get_blockchain_info()?;

// let best_block_hash = rpc.get_best_block_hash()?;
// println!("best block hash: {}", best_block_hash);
// let bestblockcount = rpc.get_block_count()?;
// println!("best block height: {}", bestblockcount);
// let best_block_hash_by_height = rpc.get_block_hash(bestblockcount)?;
// println!("best block hash by height: {}", best_block_hash_by_height);
// assert_eq!(best_block_hash_by_height, best_block_hash);

// let bitcoin_block: bitcoin::Block = rpc.get_by_id(&best_block_hash)?;
// println!("best block hash by `get`: {}", bitcoin_block.header.prev_blockhash);
// let bitcoin_tx: bitcoin::Transaction = rpc.get_by_id(&bitcoin_block.txdata[0].txid())?;
// println!("tx by `get`: {}", bitcoin_tx.txid());

// Ok(())
//}

//fn main() {
// main_result().unwrap();
//}

fn main() {}
Loading