Skip to content

Use bitcoind crate for launching bitcoind, add github actions CI #3

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

Open
wants to merge 3 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
46 changes: 46 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Test

on: [push]

env:
CARGO_TERM_COLOR: always

jobs:

test:
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
features: [ "bitcoind/0_21_1" ]


steps:
- uses: actions/checkout@v2
- uses: Swatinem/rust-cache@v1.2.0
with:
key: ${{ matrix.features }}
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: --verbose --all --features ${{ matrix.features }}

cosmetics:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: Swatinem/rust-cache@v1.2.0
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
profile: minimal
components: rustfmt, clippy
- name: fmt
run: cargo fmt -- --check
# - name: clippy
# run: cargo clippy -- -D warnings
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
members = [
"json",
"client",
"integration_test",
]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
** This is a fork of https://github.com/rust-bitcoin/rust-bitcoincore-rpc with bitcoin 0.27 **

[![Status](https://travis-ci.org/rust-bitcoin/rust-bitcoincore-rpc.png?branch=master)](https://travis-ci.org/rust-bitcoin/rust-bitcoincore-rpc)

# Rust RPC client for Bitcoin Core JSON-RPC
Expand Down
19 changes: 13 additions & 6 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
[package]
name = "bitcoincore-rpc"
version = "0.13.0"
name = "core-rpc"
version = "0.14.0"
authors = [
"Steven Roose <steven@stevenroose.org>",
"Jean Pierre Dudey <jeandudey@hotmail.com>",
"Dawid Ciężarkiewicz <dpc@dpc.pw>",
"Riccardo Casatta <riccardo@casatta.it>",
]
license = "CC0-1.0"
homepage = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc/"
repository = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc/"
homepage = "https://github.com/RCasatta/rust-bitcoincore-rpc/"
repository = "https://github.com/RCasatta/rust-bitcoincore-rpc/"
description = "RPC client library for the Bitcoin Core JSON-RPC API."
keywords = ["crypto", "bitcoin", "bitcoin-core", "rpc"]
readme = "README.md"

[lib]
name = "bitcoincore_rpc"
name = "core_rpc"
path = "src/lib.rs"

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

log = "0.4.5"
jsonrpc = "0.12.0"

# Used for deserialization of JSON.
serde = "1"
serde_json = "1"


[dev-dependencies]
bitcoin = { version = "0.27.0", features = ["rand"] }
bitcoind = { version = "0.18.0" }
lazy_static = "1.4.0"
2 changes: 1 addition & 1 deletion client/examples/retry_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//

extern crate bitcoincore_rpc;
extern crate core_rpc as bitcoincore_rpc;
extern crate jsonrpc;
extern crate serde;
extern crate serde_json;
Expand Down
2 changes: 1 addition & 1 deletion client/examples/test_against_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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

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

Expand Down
16 changes: 14 additions & 2 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,33 @@ pub trait RpcApi: Sized {
blank: Option<bool>,
passphrase: Option<&str>,
avoid_reuse: Option<bool>,
descriptors: Option<bool>,
) -> Result<json::LoadWalletResult> {
let mut args = [
wallet.into(),
opt_into_json(disable_private_keys)?,
opt_into_json(blank)?,
opt_into_json(passphrase)?,
opt_into_json(avoid_reuse)?,
opt_into_json(descriptors)?,
];
self.call(
"createwallet",
handle_defaults(&mut args, &[false.into(), false.into(), into_json("")?, false.into()]),
handle_defaults(
&mut args,
&[false.into(), false.into(), into_json("")?, false.into(), false.into()],
),
)
}

fn import_descriptors(
&self,
descriptors: Vec<json::ImportDescriptorRequest>,
) -> Result<Vec<json::ImportDescriptorResult>> {
let arg = into_json(descriptors)?;
self.call("importdescriptors", &[arg])
}

fn list_wallets(&self) -> Result<Vec<String>> {
self.call("listwallets", &[])
}
Expand Down Expand Up @@ -1136,7 +1149,6 @@ impl RpcApi for Client {
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);
log_response(cmd, &resp);
Ok(resp?.result()?)
Expand Down
6 changes: 3 additions & 3 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! This is a client library for the Bitcoin Core JSON-RPC API.
//!

#![crate_name = "bitcoincore_rpc"]
#![crate_name = "core_rpc"]
#![crate_type = "rlib"]

#[macro_use]
Expand All @@ -25,8 +25,8 @@ extern crate serde_json;

pub extern crate jsonrpc;

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

mod client;
Expand Down
92 changes: 64 additions & 28 deletions integration_test/src/main.rs → client/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
#![deny(unused)]

extern crate bitcoin;
extern crate bitcoincore_rpc;
extern crate core_rpc as bitcoincore_rpc;
#[macro_use]
extern crate lazy_static;
extern crate bitcoind;
extern crate log;

use bitcoincore_rpc::core_rpc_json as bitcoincore_rpc_json;

use std::collections::HashMap;
use std::str::FromStr;

use bitcoincore_rpc::json;
use bitcoincore_rpc::jsonrpc::error::Error as JsonRpcError;
Expand All @@ -30,9 +34,8 @@ use bitcoin::{
Address, Amount, Network, OutPoint, PrivateKey, Script, SigHashType, SignedAmount, Transaction,
TxIn, TxOut, Txid,
};
use bitcoincore_rpc::bitcoincore_rpc_json::{
GetBlockTemplateModes, GetBlockTemplateRules, ScanTxOutRequest,
};
use bitcoincore_rpc_json::{GetBlockTemplateModes, GetBlockTemplateRules, ScanTxOutRequest};
use bitcoind::{BitcoinD, P2P};

lazy_static! {
static ref SECP: secp256k1::Secp256k1<secp256k1::All> = secp256k1::Secp256k1::new();
Expand Down Expand Up @@ -107,33 +110,30 @@ fn sbtc<F: Into<f64>>(btc: F) -> SignedAmount {
SignedAmount::from_btc(btc.into()).unwrap()
}

fn get_rpc_url() -> String {
return std::env::var("RPC_URL").expect("RPC_URL must be set");
}

fn get_auth() -> bitcoincore_rpc::Auth {
if let Ok(cookie) = std::env::var("RPC_COOKIE") {
return Auth::CookieFile(cookie.into());
} else if let Ok(user) = std::env::var("RPC_USER") {
return Auth::UserPass(user, std::env::var("RPC_PASS").unwrap_or_default());
} else {
panic!("Either RPC_COOKIE or RPC_USER + RPC_PASS must be set.");
};
}

fn main() {
#[test]
fn integration_test() {
log::set_logger(&LOGGER).map(|()| log::set_max_level(log::LevelFilter::max())).unwrap();

let rpc_url = format!("{}/wallet/testwallet", get_rpc_url());
let auth = get_auth();
let mut conf = bitcoind::Conf::default();
conf.args.push("-blockfilterindex=1");
conf.p2p = P2P::Yes;
let bitcoind =
bitcoind::BitcoinD::with_conf(bitcoind::downloaded_exe_path().unwrap(), &conf).unwrap();

let mut conf2 = bitcoind::Conf::default();
conf2.p2p = bitcoind.p2p_connect(true).unwrap();
let _bitcoind2 =
bitcoind::BitcoinD::with_conf(bitcoind::downloaded_exe_path().unwrap(), &conf2).unwrap();

let cl = Client::new(&rpc_url, auth).unwrap();
let rpc_url = bitcoind.rpc_url_with_wallet("testwallet");
let auth = Auth::CookieFile(bitcoind.params.cookie_file.clone());
let cl = Client::new(&rpc_url, auth.clone()).unwrap();

test_get_network_info(&cl);
unsafe { VERSION = cl.version().unwrap() };
println!("Version: {}", version());

cl.create_wallet("testwallet", None, None, None, None).unwrap();
cl.create_wallet("testwallet", None, None, None, None, None).unwrap();

test_get_mining_info(&cl);
test_get_blockchain_info(&cl);
Expand Down Expand Up @@ -184,7 +184,7 @@ fn main() {
test_ping(&cl);
test_get_peer_info(&cl);
test_rescan_blockchain(&cl);
test_create_wallet(&cl);
test_create_wallet(&cl, &bitcoind);
test_get_tx_out_set_info(&cl);
test_get_chain_tips(&cl);
test_get_net_totals(&cl);
Expand All @@ -203,6 +203,11 @@ fn main() {
//TODO load_wallet(&self, wallet: &str) -> Result<json::LoadWalletResult> {
//TODO unload_wallet(&self, wallet: Option<&str>) -> Result<()> {
//TODO backup_wallet(&self, destination: Option<&str>) -> Result<()> {

let rpc_url = bitcoind.rpc_url_with_wallet("testdescriptorwallet");
let desc_cl = Client::new(&rpc_url, auth).unwrap();

test_descriptor_wallet(&desc_cl);
test_stop(cl);
}

Expand Down Expand Up @@ -896,7 +901,7 @@ fn test_rescan_blockchain(cl: &Client) {
assert_eq!(stop, Some(count - 1));
}

fn test_create_wallet(cl: &Client) {
fn test_create_wallet(cl: &Client, bitcoind: &BitcoinD) {
let wallet_names = vec!["alice", "bob", "carol", "denise", "emily"];

struct WalletParams<'a> {
Expand All @@ -905,6 +910,7 @@ fn test_create_wallet(cl: &Client) {
blank: Option<bool>,
passphrase: Option<&'a str>,
avoid_reuse: Option<bool>,
descriptors: Option<bool>,
}

let mut wallet_params = vec![
Expand All @@ -914,20 +920,23 @@ fn test_create_wallet(cl: &Client) {
blank: None,
passphrase: None,
avoid_reuse: None,
descriptors: None,
},
WalletParams {
name: wallet_names[1],
disable_private_keys: Some(true),
blank: None,
passphrase: None,
avoid_reuse: None,
descriptors: None,
},
WalletParams {
name: wallet_names[2],
disable_private_keys: None,
blank: Some(true),
passphrase: None,
avoid_reuse: None,
descriptors: None,
},
];

Expand All @@ -938,13 +947,15 @@ fn test_create_wallet(cl: &Client) {
blank: None,
passphrase: Some("pass"),
avoid_reuse: None,
descriptors: None,
});
wallet_params.push(WalletParams {
name: wallet_names[4],
disable_private_keys: None,
blank: None,
passphrase: None,
avoid_reuse: Some(true),
descriptors: None,
});
}

Expand All @@ -956,6 +967,7 @@ fn test_create_wallet(cl: &Client) {
wallet_param.blank,
wallet_param.passphrase,
wallet_param.avoid_reuse,
wallet_param.descriptors,
)
.unwrap();

Expand All @@ -968,8 +980,10 @@ fn test_create_wallet(cl: &Client) {
};
assert_eq!(result.warning, expected_warning);

let wallet_client_url = format!("{}{}{}", get_rpc_url(), "/wallet/", wallet_param.name);
let wallet_client = Client::new(&wallet_client_url, get_auth()).unwrap();
let wallet_client_url = bitcoind.rpc_url_with_wallet(wallet_param.name);
let auth = Auth::CookieFile(bitcoind.params.cookie_file.clone());

let wallet_client = Client::new(&wallet_client_url, auth).unwrap();
let wallet_info = wallet_client.get_wallet_info().unwrap();

assert_eq!(wallet_info.wallet_name, wallet_param.name);
Expand All @@ -995,7 +1009,7 @@ fn test_create_wallet(cl: &Client) {
wallet_list.retain(|w| w != "testwallet" && w != "");

// Created wallets
assert!(wallet_list.iter().zip(wallet_names).all(|(a, b)| a == b));
assert!(wallet_names.iter().any(|e| wallet_list.contains(&e.to_string())));
}

fn test_get_tx_out_set_info(cl: &Client) {
Expand Down Expand Up @@ -1050,3 +1064,25 @@ fn test_getblocktemplate(cl: &Client) {
fn test_stop(cl: Client) {
println!("Stopping: '{}'", cl.stop().unwrap());
}

fn test_descriptor_wallet(cl: &Client) {
cl.create_wallet(
"testdescriptorwallet",
Some(false),
Some(true),
Some(""),
Some(false),
Some(true),
)
.unwrap();

cl.import_descriptors(
vec![
json::ImportDescriptorRequest::new("wpkh(tprv8ZgxMBicQKsPeRBCAfUGsZhyHy9dwWyPqhSJmaMnMJQWWtt8L2SkTeHaiF82CUCGtiTiHAs3cMkjdKckGKiCWeYtvMPF1jDTWYTryRMicpx/86h/1h/0h/0/*)#ymr4jlz6", false),
json::ImportDescriptorRequest::new("wpkh(tprv8ZgxMBicQKsPeRBCAfUGsZhyHy9dwWyPqhSJmaMnMJQWWtt8L2SkTeHaiF82CUCGtiTiHAs3cMkjdKckGKiCWeYtvMPF1jDTWYTryRMicpx/86h/1h/0h/1/*)#40x502jz", true),
]
).unwrap();

let add = cl.get_new_address(None, Some(json::AddressType::Bech32)).unwrap();
assert_eq!(add, Address::from_str("bcrt1q7crcza94drr00skmu5x0n00rhmwnthde2frhwk").unwrap());
}
Loading