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

RFC: Total refactor to support sync & async clients #294

Open
wants to merge 5 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
13 changes: 4 additions & 9 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ jobs:
matrix:
include:
- rust: stable
env:
RUSTFMTCHK: true

Choose a reason for hiding this comment

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

Very Nit: I don't really have any history in this repo but just wanted to register a vote in favor of keeping rustfmt. We have had the discussion before elsewhere so won't repeat but IMHO formatting consistency is more important than disliking some of rustfmt behaviour

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🙃 Let's say I did this to see if CI would work without having to already deal with how rustfmt is going to decide how I should have formatted my code.

- rust: nightly
env:
RUSTFMTCHK: false
- rust: 1.41.1
env:
RUSTFMTCHK: false
- rust: 1.48.0
steps:
- name: Checkout Crate
uses: actions/checkout@v2
Expand All @@ -27,9 +21,10 @@ jobs:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- run: cargo update -p serde --precise 1.0.152
- run: cargo update --package backtrace --precise 0.3.50
- run: cargo update --package async-trait --precise 0.1.53
- run: cargo update --package serde --precise 1.0.152
- name: Running test script
env: ${{ matrix.env }}
run: ./contrib/test.sh

integrations-tests:
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" }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ The following versions are officially supported and automatically tested:
* 0.21.0

# Minimum Supported Rust Version (MSRV)
This library should always compile with any combination of features on **Rust 1.41.1**.
This library should always compile with any combination of features on **Rust 1.48.0**.
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