Skip to content

Commit

Permalink
Merge pull request #86 from stevenroose/upgrade-deps-0.23
Browse files Browse the repository at this point in the history
Bump rust-bitcoin dependency to v0.23
  • Loading branch information
stevenroose committed Jan 25, 2020
2 parents 9bdb2ed + 6395ee6 commit 0f49925
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 94 deletions.
2 changes: 1 addition & 1 deletion client/Cargo.toml
Expand Up @@ -18,7 +18,7 @@ name = "bitcoincore_rpc"
path = "src/lib.rs"

[dependencies]
bitcoincore-rpc-json = { version = "0.8.0-rc1", path = "../json" }
bitcoincore-rpc-json = { path = "../json" }

log = "0.4.5"
jsonrpc = "0.11"
Expand Down
70 changes: 35 additions & 35 deletions client/src/client.rs
Expand Up @@ -20,7 +20,7 @@ use jsonrpc;
use serde;
use serde_json;

use bitcoin::hashes::sha256d;
use bitcoin::hashes::hex::FromHex;
use bitcoin::secp256k1::{self, SecretKey, Signature};
use bitcoin::{Address, Amount, Block, BlockHeader, OutPoint, PrivateKey, PublicKey, Transaction};
use log::Level::Debug;
Expand All @@ -37,7 +37,7 @@ pub type Result<T> = result::Result<T, Error>;

#[derive(Clone, Debug, Serialize, Deserialize)]
struct JsonOutPoint {
pub txid: sha256d::Hash,
pub txid: bitcoin::Txid,
pub vout: u32,
}

Expand Down Expand Up @@ -278,7 +278,7 @@ pub trait RpcApi: Sized {
// `Address`!
fn dump_priv_key(&self, address: &Address) -> Result<SecretKey> {
let hex: String = self.call("dumpprivkey", &[address.to_string().into()])?;
let bytes = hex::decode(hex)?;
let bytes: Vec<u8> = FromHex::from_hex(&hex)?;
Ok(secp256k1::SecretKey::from_slice(&bytes)?)
}

Expand All @@ -295,28 +295,28 @@ pub trait RpcApi: Sized {
self.call("getconnectioncount", &[])
}

fn get_block(&self, hash: &sha256d::Hash) -> Result<Block> {
fn get_block(&self, hash: &bitcoin::BlockHash) -> Result<Block> {
let hex: String = self.call("getblock", &[into_json(hash)?, 0.into()])?;
let bytes = hex::decode(hex)?;
let bytes: Vec<u8> = FromHex::from_hex(&hex)?;
Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
}

fn get_block_hex(&self, hash: &sha256d::Hash) -> Result<String> {
fn get_block_hex(&self, hash: &bitcoin::BlockHash) -> Result<String> {
self.call("getblock", &[into_json(hash)?, 0.into()])
}

fn get_block_info(&self, hash: &sha256d::Hash) -> Result<json::GetBlockResult> {
fn get_block_info(&self, hash: &bitcoin::BlockHash) -> Result<json::GetBlockResult> {
self.call("getblock", &[into_json(hash)?, 1.into()])
}
//TODO(stevenroose) add getblock_txs

fn get_block_header_raw(&self, hash: &sha256d::Hash) -> Result<BlockHeader> {
fn get_block_header_raw(&self, hash: &bitcoin::BlockHash) -> Result<BlockHeader> {
let hex: String = self.call("getblockheader", &[into_json(hash)?, false.into()])?;
let bytes = hex::decode(hex)?;
let bytes: Vec<u8> = FromHex::from_hex(&hex)?;
Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
}

fn get_block_header_verbose(&self, hash: &sha256d::Hash) -> Result<json::GetBlockHeaderResult> {
fn get_block_header_verbose(&self, hash: &bitcoin::BlockHash) -> Result<json::GetBlockHeaderResult> {
self.call("getblockheader", &[into_json(hash)?, true.into()])
}

Expand All @@ -336,45 +336,45 @@ pub trait RpcApi: Sized {
}

/// Returns the hash of the best (tip) block in the longest blockchain.
fn get_best_block_hash(&self) -> Result<sha256d::Hash> {
fn get_best_block_hash(&self) -> Result<bitcoin::BlockHash> {
self.call("getbestblockhash", &[])
}

/// Get block hash at a given height
fn get_block_hash(&self, height: u64) -> Result<sha256d::Hash> {
fn get_block_hash(&self, height: u64) -> Result<bitcoin::BlockHash> {
self.call("getblockhash", &[height.into()])
}

fn get_raw_transaction(
&self,
txid: &sha256d::Hash,
block_hash: Option<&sha256d::Hash>,
txid: &bitcoin::Txid,
block_hash: Option<&bitcoin::BlockHash>,
) -> Result<Transaction> {
let mut args = [into_json(txid)?, into_json(false)?, opt_into_json(block_hash)?];
let hex: String = self.call("getrawtransaction", handle_defaults(&mut args, &[null()]))?;
let bytes = hex::decode(hex)?;
let bytes: Vec<u8> = FromHex::from_hex(&hex)?;
Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
}

fn get_raw_transaction_hex(
&self,
txid: &sha256d::Hash,
block_hash: Option<&sha256d::Hash>,
txid: &bitcoin::Txid,
block_hash: Option<&bitcoin::BlockHash>,
) -> Result<String> {
let mut args = [into_json(txid)?, into_json(false)?, opt_into_json(block_hash)?];
self.call("getrawtransaction", handle_defaults(&mut args, &[null()]))
}

fn get_raw_transaction_verbose(
&self,
txid: &sha256d::Hash,
block_hash: Option<&sha256d::Hash>,
txid: &bitcoin::Txid,
block_hash: Option<&bitcoin::BlockHash>,
) -> Result<json::GetRawTransactionResult> {
let mut args = [into_json(txid)?, into_json(true)?, opt_into_json(block_hash)?];
self.call("getrawtransaction", handle_defaults(&mut args, &[null()]))
}

fn get_block_filter(&self, block_hash: &sha256d::Hash) -> Result<json::GetBlockFilterResult> {
fn get_block_filter(&self, block_hash: &bitcoin::BlockHash) -> Result<json::GetBlockFilterResult> {
self.call("getblockfilter", &[into_json(block_hash)?])
}

Expand All @@ -398,7 +398,7 @@ pub trait RpcApi: Sized {

fn get_transaction(
&self,
txid: &sha256d::Hash,
txid: &bitcoin::Txid,
include_watchonly: Option<bool>,
) -> Result<json::GetTransactionResult> {
let mut args = [into_json(txid)?, opt_into_json(include_watchonly)?];
Expand All @@ -423,7 +423,7 @@ pub trait RpcApi: Sized {

fn get_tx_out(
&self,
txid: &sha256d::Hash,
txid: &bitcoin::Txid,
vout: u32,
include_mempool: Option<bool>,
) -> Result<Option<json::GetTxOutResult>> {
Expand All @@ -433,12 +433,12 @@ pub trait RpcApi: Sized {

fn get_tx_out_proof(
&self,
txids: &[sha256d::Hash],
block_hash: Option<&sha256d::Hash>,
txids: &[bitcoin::Txid],
block_hash: Option<&bitcoin::BlockHash>,
) -> Result<Vec<u8>> {
let mut args = [into_json(txids)?, opt_into_json(block_hash)?];
let hex: String = self.call("gettxoutproof", handle_defaults(&mut args, &[null()]))?;
Ok(hex::decode(&hex)?)
Ok(FromHex::from_hex(&hex)?)
}

fn import_public_key(
Expand Down Expand Up @@ -583,7 +583,7 @@ pub trait RpcApi: Sized {
replaceable: Option<bool>,
) -> Result<Transaction> {
let hex: String = self.create_raw_transaction_hex(utxos, outs, locktime, replaceable)?;
let bytes = hex::decode(hex)?;
let bytes: Vec<u8> = FromHex::from_hex(&hex)?;
Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
}

Expand Down Expand Up @@ -676,28 +676,28 @@ pub trait RpcApi: Sized {
/// Mine `block_num` blocks and pay coinbase to `address`
///
/// Returns hashes of the generated blocks
fn generate_to_address(&self, block_num: u64, address: &Address) -> Result<Vec<sha256d::Hash>> {
fn generate_to_address(&self, block_num: u64, address: &Address) -> Result<Vec<bitcoin::BlockHash>> {
self.call("generatetoaddress", &[block_num.into(), address.to_string().into()])
}

/// Mine up to block_num blocks immediately (before the RPC call returns)
/// to an address in the wallet.
fn generate(&self, block_num: u64, maxtries: Option<u64>) -> Result<Vec<sha256d::Hash>> {
fn generate(&self, block_num: u64, maxtries: Option<u64>) -> Result<Vec<bitcoin::BlockHash>> {
self.call("generate", &[block_num.into(), opt_into_json(maxtries)?])
}

/// Mark a block as invalid by `block_hash`
fn invalidate_block(&self, block_hash: &sha256d::Hash) -> Result<()> {
fn invalidate_block(&self, block_hash: &bitcoin::BlockHash) -> Result<()> {
self.call("invalidateblock", &[into_json(block_hash)?])
}

/// Mark a block as valid by `block_hash`
fn reconsider_block(&self, block_hash: &sha256d::Hash) -> Result<()> {
fn reconsider_block(&self, block_hash: &bitcoin::BlockHash) -> Result<()> {
self.call("reconsiderblock", &[into_json(block_hash)?])
}

/// Get txids of all transactions in a memory pool
fn get_raw_mempool(&self) -> Result<Vec<sha256d::Hash>> {
fn get_raw_mempool(&self) -> Result<Vec<bitcoin::Txid>> {
self.call("getrawmempool", &[])
}

Expand All @@ -711,7 +711,7 @@ pub trait RpcApi: Sized {
replaceable: Option<bool>,
confirmation_target: Option<u32>,
estimate_mode: Option<json::EstimateMode>,
) -> Result<sha256d::Hash> {
) -> Result<bitcoin::Txid> {
let mut args = [
address.to_string().into(),
into_json(amount.as_btc())?,
Expand Down Expand Up @@ -745,7 +745,7 @@ pub trait RpcApi: Sized {
self.call("ping", &[])
}

fn send_raw_transaction<R: RawTx>(&self, tx: R) -> Result<sha256d::Hash> {
fn send_raw_transaction<R: RawTx>(&self, tx: R) -> Result<bitcoin::Txid> {
self.call("sendrawtransaction", &[tx.raw_hex().into()])
}

Expand Down Expand Up @@ -777,7 +777,7 @@ pub trait RpcApi: Sized {
/// 1. `blockhash`: Block hash to wait for.
/// 2. `timeout`: Time in milliseconds to wait for a response. 0
/// indicates no timeout.
fn wait_for_block(&self, blockhash: &sha256d::Hash, timeout: u64) -> Result<json::BlockRef> {
fn wait_for_block(&self, blockhash: &bitcoin::BlockHash, timeout: u64) -> Result<json::BlockRef> {
let args = [into_json(blockhash)?, into_json(timeout)?];
self.call("waitforblock", &args)
}
Expand Down Expand Up @@ -853,7 +853,7 @@ mod tests {
fn test_raw_tx() {
use bitcoin::consensus::encode;
let client = Client::new("http://localhost/".into(), Auth::None).unwrap();
let tx: bitcoin::Transaction = encode::deserialize(&hex::decode("0200000001586bd02815cf5faabfec986a4e50d25dbee089bd2758621e61c5fab06c334af0000000006b483045022100e85425f6d7c589972ee061413bcf08dc8c8e589ce37b217535a42af924f0e4d602205c9ba9cb14ef15513c9d946fa1c4b797883e748e8c32171bdf6166583946e35c012103dae30a4d7870cd87b45dd53e6012f71318fdd059c1c2623b8cc73f8af287bb2dfeffffff021dc4260c010000001976a914f602e88b2b5901d8aab15ebe4a97cf92ec6e03b388ac00e1f505000000001976a914687ffeffe8cf4e4c038da46a9b1d37db385a472d88acfd211500").unwrap()).unwrap();
let tx: bitcoin::Transaction = encode::deserialize(&Vec::<u8>::from_hex("0200000001586bd02815cf5faabfec986a4e50d25dbee089bd2758621e61c5fab06c334af0000000006b483045022100e85425f6d7c589972ee061413bcf08dc8c8e589ce37b217535a42af924f0e4d602205c9ba9cb14ef15513c9d946fa1c4b797883e748e8c32171bdf6166583946e35c012103dae30a4d7870cd87b45dd53e6012f71318fdd059c1c2623b8cc73f8af287bb2dfeffffff021dc4260c010000001976a914f602e88b2b5901d8aab15ebe4a97cf92ec6e03b388ac00e1f505000000001976a914687ffeffe8cf4e4c038da46a9b1d37db385a472d88acfd211500").unwrap()).unwrap();

assert!(client.send_raw_transaction(&tx).is_err());
assert!(client.send_raw_transaction(&encode::serialize(&tx)).is_err());
Expand Down
14 changes: 7 additions & 7 deletions client/src/error.rs
Expand Up @@ -12,15 +12,15 @@ use std::{error, fmt, io};

use bitcoin;
use bitcoin::secp256k1;
use hex;
use bitcoin::hashes::hex;
use jsonrpc;
use serde_json;

/// The error type for errors produced in this library.
#[derive(Debug)]
pub enum Error {
JsonRpc(jsonrpc::error::Error),
FromHex(hex::FromHexError),
Hex(hex::Error),
Json(serde_json::error::Error),
BitcoinSerialization(bitcoin::consensus::encode::Error),
Secp256k1(secp256k1::Error),
Expand All @@ -35,9 +35,9 @@ impl From<jsonrpc::error::Error> for Error {
}
}

impl From<hex::FromHexError> for Error {
fn from(e: hex::FromHexError) -> Error {
Error::FromHex(e)
impl From<hex::Error> for Error {
fn from(e: hex::Error) -> Error {
Error::Hex(e)
}
}

Expand Down Expand Up @@ -75,7 +75,7 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::JsonRpc(ref e) => write!(f, "JSON-RPC error: {}", e),
Error::FromHex(ref e) => write!(f, "hex decode error: {}", e),
Error::Hex(ref e) => write!(f, "hex decode error: {}", e),
Error::Json(ref e) => write!(f, "JSON error: {}", e),
Error::BitcoinSerialization(ref e) => write!(f, "Bitcoin serialization error: {}", e),
Error::Secp256k1(ref e) => write!(f, "secp256k1 error: {}", e),
Expand All @@ -94,7 +94,7 @@ impl error::Error for Error {
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::JsonRpc(ref e) => Some(e),
Error::FromHex(ref e) => Some(e),
Error::Hex(ref e) => Some(e),
Error::Json(ref e) => Some(e),
Error::BitcoinSerialization(ref e) => Some(e),
Error::Secp256k1(ref e) => Some(e),
Expand Down
9 changes: 4 additions & 5 deletions client/src/queryable.rs
Expand Up @@ -11,7 +11,6 @@
use bitcoin;
use serde_json;

use bitcoin::hashes::sha256d;
use client::Result;
use client::RpcApi;

Expand All @@ -24,23 +23,23 @@ pub trait Queryable<C: RpcApi>: Sized {
}

impl<C: RpcApi> Queryable<C> for bitcoin::blockdata::block::Block {
type Id = sha256d::Hash;
type Id = bitcoin::BlockHash;

fn query(rpc: &C, id: &Self::Id) -> Result<Self> {
let rpc_name = "getblock";
let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?, 0.into()])?;
let bytes = bitcoin::util::misc::hex_bytes(&hex)?;
let bytes: Vec<u8> = bitcoin::hashes::hex::FromHex::from_hex(&hex)?;
Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
}
}

impl<C: RpcApi> Queryable<C> for bitcoin::blockdata::transaction::Transaction {
type Id = sha256d::Hash;
type Id = bitcoin::Txid;

fn query(rpc: &C, id: &Self::Id) -> Result<Self> {
let rpc_name = "getrawtransaction";
let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?])?;
let bytes = bitcoin::util::misc::hex_bytes(&hex)?;
let bytes: Vec<u8> = bitcoin::hashes::hex::FromHex::from_hex(&hex)?;
Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
}
}
Expand Down
2 changes: 1 addition & 1 deletion json/Cargo.toml
Expand Up @@ -22,5 +22,5 @@ serde = { version = "1", features = [ "derive" ] }
serde_json = "1"
hex = "0.3"

bitcoin = { version = "0.19.1", features = [ "use-serde" ] }
bitcoin = { version = "0.23", features = [ "use-serde" ] }
num-bigint = { version = "0.2", features = [ "serde" ] }

0 comments on commit 0f49925

Please sign in to comment.