Skip to content

Commit

Permalink
Flesh out v17 and v18
Browse files Browse the repository at this point in the history
This patch is massive, I worked on it for many days across multiple
weeks. I was lazy and did not pull unrelated fixes out - sorry.

Does the following:

- Improves stuff in `node` left over from the crate rename from
`bitcoind`.
- Totally re-writes the `integration_test` crate removing the macros,
vastly simplifying it.
- Fleshes out v17 adding impls for all blockchain and wallet methods.
- Copies the v17 to v18 to prove the process, we have an open problem
around docs on the types. Will describe as a github issue.
- Various other little things I saw while working on this.
  • Loading branch information
tcharding committed Nov 21, 2024
1 parent 6092f89 commit 9397251
Show file tree
Hide file tree
Showing 85 changed files with 6,726 additions and 3,422 deletions.
140 changes: 122 additions & 18 deletions client/src/client_sync/v17/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
//!
//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
/// Implements Bitcoin Core JSON-RPC API method `getblockchaininfo`
#[macro_export]
macro_rules! impl_client_v17__getblockchaininfo {
() => {
impl Client {
pub fn get_blockchain_info(&self) -> Result<GetBlockchainInfo> {
self.call("getblockchaininfo", &[])
}
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `getbestblockhash`
#[macro_export]
macro_rules! impl_client_v17__getbestblockhash {
Expand All @@ -34,36 +46,23 @@ macro_rules! impl_client_v17__getblock {
impl Client {
/// Gets a block by blockhash.
pub fn get_block(&self, hash: BlockHash) -> Result<Block> {
let json = self.get_block_verbosity_zero(hash)?;
let json = self.get_block_verbose_zero(hash)?;
Ok(json.block()?)
}

pub fn get_block_verbosity_zero(
&self,
hash: BlockHash,
) -> Result<GetBlockVerbosityZero> {
/// Gets a block by blockhash with verbose set to 0.
pub fn get_block_verbose_zero(&self, hash: BlockHash) -> Result<GetBlockVerbosityZero> {
self.call("getblock", &[into_json(hash)?, 0.into()])
}

pub fn get_block_verbosity_one(&self, hash: BlockHash) -> Result<GetBlockVerbosityOne> {
/// Gets a block by blockhash with verbose set to 1.
pub fn get_block_verbose_one(&self, hash: BlockHash) -> Result<GetBlockVerbosityOne> {
self.call("getblock", &[into_json(hash)?, 1.into()])
}
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `getblockchaininfo`
#[macro_export]
macro_rules! impl_client_v17__getblockchaininfo {
() => {
impl Client {
pub fn get_blockchain_info(&self) -> Result<GetBlockchainInfo> {
self.call("getblockchaininfo", &[])
}
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `getblockcount`
#[macro_export]
macro_rules! impl_client_v17__getblockcount {
Expand Down Expand Up @@ -164,8 +163,76 @@ macro_rules! impl_client_v17__getmempoolancestors {
() => {
impl Client {
pub fn get_mempool_ancestors(&self, txid: Txid) -> Result<GetMempoolAncestors> {
// Equivalent to self.call("getmempoolancestors", &[into_json(txid)?, into_json(false)?])
self.call("getmempoolancestors", &[into_json(txid)?])
}

pub fn get_mempool_ancestors_verbose(
&self,
txid: Txid,
) -> Result<GetMempoolAncestorsVerbose> {
self.call("getmempoolancestors", &[into_json(txid)?, into_json(true)?])
}
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `getmempooldescendants`
#[macro_export]
macro_rules! impl_client_v17__getmempooldescendants {
() => {
impl Client {
pub fn get_mempool_descendants(&self, txid: Txid) -> Result<GetMempoolDescendants> {
// Equivalent to self.call("getmempooldescendants", &[into_json(txid)?, into_json(false)?])
self.call("getmempooldescendants", &[into_json(txid)?])
}

pub fn get_mempool_descendants_verbose(
&self,
txid: Txid,
) -> Result<GetMempoolDescendantsVerbose> {
self.call("getmempooldescendants", &[into_json(txid)?, into_json(true)?])
}
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `getmempoolentry`
#[macro_export]
macro_rules! impl_client_v17__getmempoolentry {
() => {
impl Client {
pub fn get_mempool_entry(&self, txid: Txid) -> Result<GetMempoolEntry> {
self.call("getmempoolentry", &[into_json(txid)?])
}
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `getmempoolinfo`
#[macro_export]
macro_rules! impl_client_v17__getmempoolinfo {
() => {
impl Client {
pub fn get_mempool_info(&self) -> Result<GetMempoolInfo> {
self.call("getmempoolinfo", &[])
}
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `getrawmempool`
#[macro_export]
macro_rules! impl_client_v17__getrawmempool {
() => {
impl Client {
pub fn get_raw_mempool(&self) -> Result<GetRawMempool> {
// Equivalent to self.call("getrawmempool", &[into_json(false)?])
self.call("getrawmempool", &[])
}
pub fn get_raw_mempool_verbose(&self) -> Result<GetRawMempool> {
self.call("getrawmempool", &[into_json(true)?])
}
}
};
}
Expand All @@ -181,3 +248,40 @@ macro_rules! impl_client_v17__gettxout {
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `gettxoutproof`
#[macro_export]
macro_rules! impl_client_v17__gettxoutproof {
() => {
impl Client {
pub fn get_tx_out_proof(&self, txids: Vec<Txid>) -> Result<GetTxOut> {
self.call("gettxoutproof", &[into_json(txids)?])
}
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `gettxoutsetinfo`
#[macro_export]
macro_rules! impl_client_v17__gettxoutsetinfo {
() => {
impl Client {
pub fn get_tx_out_set_info(&self) -> Result<GetTxOut> {
self.call("gettxoutsetinfo", &[])
}
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `verifytxoutproof`
#[macro_export]
macro_rules! impl_client_v17__verifytxoutproof {
() => {
impl Client {
// `proof` is the hex-encoded proof generated by `gettxoutproof`.
pub fn verify_tx_out_proof(&self, proof: &str) -> Result<GetTxOut> {
self.call("verifytxoutproof", &[into_json(proof)?])
}
}
};
}
100 changes: 71 additions & 29 deletions client/src/client_sync/v17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,54 @@ pub mod network;
pub mod raw_transactions;
pub mod wallet;

use std::collections::BTreeMap;
use std::path::Path;

use bitcoin::address::{Address, NetworkChecked};
use bitcoin::{Amount, Block, BlockHash, Txid};
use bitcoin::{Amount, Block, BlockHash, PublicKey, Txid};
use serde::{Deserialize, Serialize};

use crate::client_sync::into_json;
use crate::types::v17::*;

/// An element in the `inputs` argument of method `walletcreatefundedpsbt`.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct WalletCreateFundedPsbtInput {
txid: Txid,
vout: u32,
}

/// Argument to the `Client::get_new_address_with_type` function.
///
/// For Core versions 0.17 through to v22. For Core v23 and onwards use `v23::AddressType`.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum AddressType {
Legacy,
P2shSegwit,
Bech32,
}

impl fmt::Display for AddressType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use AddressType::*;

let s = match *self {
Legacy => "legacy",
P2shSegwit => "p2sh-segwit",
Bech32 => "bech32",
};
fmt::Display::fmt(s, f)
}
}

crate::define_jsonrpc_minreq_client!("v17");
crate::impl_client_check_expected_server_version!({ [170100] });

// == Blockchain ==
crate::impl_client_v17__getblockchaininfo!();
crate::impl_client_v17__getbestblockhash!();
crate::impl_client_v17__getblock!();
crate::impl_client_v17__gettxout!();
crate::impl_client_v17__getblockchaininfo!();
crate::impl_client_v17__getblockcount!();
crate::impl_client_v17__getblockhash!();
crate::impl_client_v17__getblockheader!();
Expand All @@ -34,6 +67,14 @@ crate::impl_client_v17__getchaintips!();
crate::impl_client_v17__getchaintxstats!();
crate::impl_client_v17__getdifficulty!();
crate::impl_client_v17__getmempoolancestors!();
crate::impl_client_v17__getmempooldescendants!();
crate::impl_client_v17__getmempoolentry!();
crate::impl_client_v17__getmempoolinfo!();
crate::impl_client_v17__getrawmempool!();
crate::impl_client_v17__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
crate::impl_client_v17__getmemoryinfo!();
Expand All @@ -55,32 +96,33 @@ crate::impl_client_v17__getpeerinfo!();
crate::impl_client_v17__sendrawtransaction!();

// == Wallet ==
crate::impl_client_v17__addmultisigaddress!();
crate::impl_client_v17__bumpfee!();
crate::impl_client_v17__createwallet!();
crate::impl_client_v17__unloadwallet!();
crate::impl_client_v17__loadwallet!();
crate::impl_client_v17__getnewaddress!();
crate::impl_client_v17__dumpprivkey!();
crate::impl_client_v17__dumpwallet!();
crate::impl_client_v17__getaddressesbylabel!();
crate::impl_client_v17__getaddressinfo!();
crate::impl_client_v17__getbalance!();
crate::impl_client_v17__sendtoaddress!();
crate::impl_client_v17__getnewaddress!();
crate::impl_client_v17__getrawchangeaddress!();
crate::impl_client_v17__getreceivedbyaddress!();
crate::impl_client_v17__gettransaction!();

/// Argument to the `Client::get_new_address_with_type` function.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum AddressType {
Legacy,
P2shSegwit,
Bech32,
}

impl fmt::Display for AddressType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use AddressType::*;

let s = match *self {
Legacy => "legacy",
P2shSegwit => "p2sh-segwit",
Bech32 => "bech32",
};
fmt::Display::fmt(s, f)
}
}
crate::impl_client_v17__getunconfirmedbalance!();
crate::impl_client_v17__getwalletinfo!();
crate::impl_client_v17__listaddressgroupings!();
crate::impl_client_v17__listlabels!();
crate::impl_client_v17__listlockunspent!();
crate::impl_client_v17__listreceivedbyaddress!();
crate::impl_client_v17__listsinceblock!();
crate::impl_client_v17__listtransactions!();
crate::impl_client_v17__listunspent!();
crate::impl_client_v17__listwallets!();
crate::impl_client_v17__loadwallet!();
crate::impl_client_v17__rescanblockchain!();
crate::impl_client_v17__sendmany!();
crate::impl_client_v17__sendtoaddress!();
crate::impl_client_v17__signmessage!();
crate::impl_client_v17__signrawtransactionwithwallet!();
crate::impl_client_v17__walletcreatefundedpsbt!();
crate::impl_client_v17__walletprocesspsbt!();
Loading

0 comments on commit 9397251

Please sign in to comment.