test_against_node/
test_against_node.rs

1// To the extent possible under law, the author(s) have dedicated all
2// copyright and related and neighboring rights to this software to
3// the public domain worldwide. This software is distributed without
4// any warranty.
5//
6// You should have received a copy of the CC0 Public Domain Dedication
7// along with this software.
8// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
9//
10
11//! A very simple example used as a self-test of this library against a Bitcoin
12//! Core node.
13extern crate core_rpc as bitcoincore_rpc;
14
15use bitcoincore_rpc::{bitcoin, Auth, Client, Error, RpcApi};
16
17fn main_result() -> Result<(), Error> {
18    let mut args = std::env::args();
19
20    let _exe_name = args.next().unwrap();
21
22    let url = args.next().expect("Usage: <rpc_url> <username> <password>");
23    let user = args.next().expect("no user given");
24    let pass = args.next().expect("no pass given");
25
26    let rpc = Client::new(&url, Auth::UserPass(user, pass)).unwrap();
27
28    let _blockchain_info = rpc.get_blockchain_info()?;
29
30    let best_block_hash = rpc.get_best_block_hash()?;
31    println!("best block hash: {}", best_block_hash);
32    let bestblockcount = rpc.get_block_count()?;
33    println!("best block height: {}", bestblockcount);
34    let best_block_hash_by_height = rpc.get_block_hash(bestblockcount)?;
35    println!("best block hash by height: {}", best_block_hash_by_height);
36    assert_eq!(best_block_hash_by_height, best_block_hash);
37
38    let bitcoin_block: bitcoin::Block = rpc.get_by_id(&best_block_hash)?;
39    println!("best block hash by `get`: {}", bitcoin_block.header.prev_blockhash);
40    let bitcoin_tx: bitcoin::Transaction = rpc.get_by_id(&bitcoin_block.txdata[0].txid())?;
41    println!("tx by `get`: {}", bitcoin_tx.txid());
42
43    Ok(())
44}
45
46fn main() {
47    main_result().unwrap();
48}