Skip to content
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
5 changes: 4 additions & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc/"
description = "RPC client library for the Bitcoin Core JSON-RPC API."
keywords = [ "crypto", "bitcoin", "bitcoin-core", "rpc" ]
readme = "README.md"
edition = "2018"

[lib]
name = "bitcoincore_rpc"
Expand All @@ -21,7 +22,9 @@ path = "src/lib.rs"
bitcoincore-rpc-json = { version = "0.11.0", path = "../json"}

log = "0.4.5"
jsonrpc = "0.11"
reqwest = { version = "0.10", features = ["json"] }
async-trait = "0.1.40"
base64-compat = "1.0.0"

# Used for deserialization of JSON.
serde = "1"
Expand Down
19 changes: 9 additions & 10 deletions client/examples/retry_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//

extern crate bitcoincore_rpc;
extern crate jsonrpc;
extern crate serde;
extern crate serde_json;
use async_trait::async_trait;
use bitcoincore_rpc;
use serde;
use serde_json;

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

Expand All @@ -22,25 +22,24 @@ pub struct RetryClient {
const INTERVAL: u64 = 1000;
const RETRY_ATTEMPTS: u8 = 10;

#[async_trait]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to drop this dependency by just writing the functions more verbose using Future?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it may be a little bit more involved, see: https://github.com/dtolnay/async-trait#explanation

impl RpcApi for RetryClient {
fn call<T: for<'a> serde::de::Deserialize<'a>>(
async 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) {
match self.client.call(cmd, args).await {
Ok(ret) => return Ok(ret),
Err(Error::JsonRpc(jsonrpc::error::Error::Rpc(ref rpcerr)))
if rpcerr.code == -28 =>
{
Err(Error::JsonRpc(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)
self.client.call(cmd, args).await
}
}

Expand Down
22 changes: 13 additions & 9 deletions client/examples/test_against_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

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

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

fn main_result() -> Result<(), Error> {
#[tokio::main]
async fn main_result() -> Result<(), Error> {
let mut args = std::env::args();

let _exe_name = args.next().unwrap();
Expand All @@ -25,19 +26,22 @@ fn main_result() -> Result<(), Error> {

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

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

let best_block_hash = rpc.get_best_block_hash()?;
let best_block_hash = rpc.get_best_block_hash().await?;
println!("best block hash: {}", best_block_hash);
let bestblockcount = rpc.get_block_count()?;
let bestblockcount = rpc.get_block_count().await?;
println!("best block height: {}", bestblockcount);
let best_block_hash_by_height = rpc.get_block_hash(bestblockcount)?;
let best_block_hash_by_height = rpc.get_block_hash(bestblockcount).await?;
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())?;
let bitcoin_block: bitcoin::Block = rpc.get_by_id(&best_block_hash).await?;
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()).await?;
println!("tx by `get`: {}", bitcoin_tx.txid());

Ok(())
Expand Down
Loading