Skip to content

Commit bb7d9fa

Browse files
committed
added getblockverbose lvl 2
1 parent 06a6c15 commit bb7d9fa

File tree

2 files changed

+97
-16
lines changed

2 files changed

+97
-16
lines changed

client/src/client.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use jsonrpc;
2222
use serde;
2323
use serde_json;
2424

25-
use crate::bitcoin::address::{NetworkUnchecked, NetworkChecked};
25+
use crate::bitcoin::address::{NetworkChecked, NetworkUnchecked};
2626
use crate::bitcoin::hashes::hex::FromHex;
2727
use crate::bitcoin::secp256k1::ecdsa::Signature;
2828
use crate::bitcoin::{
@@ -344,7 +344,10 @@ pub trait RpcApi: Sized {
344344
fn get_block_info(&self, hash: &bitcoin::BlockHash) -> Result<json::GetBlockResult> {
345345
self.call("getblock", &[into_json(hash)?, 1.into()])
346346
}
347-
//TODO(stevenroose) add getblock_txs
347+
348+
fn get_block_verbose(&self, hash: &bitcoin::BlockHash) -> Result<json::GetBlockVerboseResult> {
349+
self.call("getblock", &[into_json(hash)?, 2.into()])
350+
}
348351

349352
fn get_block_header(&self, hash: &bitcoin::BlockHash) -> Result<bitcoin::block::Header> {
350353
let hex: String = self.call("getblockheader", &[into_json(hash)?, false.into()])?;
@@ -892,7 +895,10 @@ pub trait RpcApi: Sized {
892895
}
893896

894897
/// Generate new address for receiving change
895-
fn get_raw_change_address(&self, address_type: Option<json::AddressType>) -> Result<Address<NetworkUnchecked>> {
898+
fn get_raw_change_address(
899+
&self,
900+
address_type: Option<json::AddressType>,
901+
) -> Result<Address<NetworkUnchecked>> {
896902
self.call("getrawchangeaddress", &[opt_into_json(address_type)?])
897903
}
898904

@@ -1183,7 +1189,11 @@ pub trait RpcApi: Sized {
11831189
self.call("finalizepsbt", handle_defaults(&mut args, &[true.into()]))
11841190
}
11851191

1186-
fn derive_addresses(&self, descriptor: &str, range: Option<[u32; 2]>) -> Result<Vec<Address<NetworkUnchecked>>> {
1192+
fn derive_addresses(
1193+
&self,
1194+
descriptor: &str,
1195+
range: Option<[u32; 2]>,
1196+
) -> Result<Vec<Address<NetworkUnchecked>>> {
11871197
let mut args = [into_json(descriptor)?, opt_into_json(range)?];
11881198
self.call("deriveaddresses", handle_defaults(&mut args, &[null()]))
11891199
}

json/src/lib.rs

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
#![crate_name = "bitcoincore_rpc_json"]
1717
#![crate_type = "rlib"]
18-
#![allow(deprecated)] // Because of `GetPeerInfoResultNetwork::Unroutable`.
18+
#![allow(deprecated)] // Because of `GetPeerInfoResultNetwork::Unroutable`.
1919

2020
pub extern crate bitcoin;
2121
#[allow(unused)]
@@ -25,13 +25,15 @@ extern crate serde_json;
2525

2626
use std::collections::HashMap;
2727

28-
2928
use bitcoin::address::NetworkUnchecked;
3029
use bitcoin::block::Version;
3130
use bitcoin::consensus::encode;
3231
use bitcoin::hashes::hex::FromHex;
3332
use bitcoin::hashes::sha256;
34-
use bitcoin::{Address, Amount, PrivateKey, PublicKey, SignedAmount, Transaction, ScriptBuf, Script, bip158, bip32, Network};
33+
use bitcoin::{
34+
bip158, bip32, Address, Amount, Network, PrivateKey, PublicKey, Script, ScriptBuf,
35+
SignedAmount, Transaction,
36+
};
3537
use serde::de::Error as SerdeError;
3638
use serde::{Deserialize, Serialize};
3739
use std::fmt;
@@ -1008,8 +1010,8 @@ pub struct GetAddressInfoResult {
10081010
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
10091011
#[serde(untagged)]
10101012
pub enum StringOrStringArray {
1011-
String(String),
1012-
StringArray(Vec<String>),
1013+
String(String),
1014+
StringArray(Vec<String>),
10131015
}
10141016

10151017
/// Models the result of "getblockchaininfo"
@@ -1556,6 +1558,52 @@ pub enum GetBlockTemplateModes {
15561558
// side.
15571559
}
15581560

1561+
/// Models the result of "getblock "blockhash" [verbosity=3]"
1562+
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
1563+
pub struct GetBlockVerboseResult {
1564+
/// The current block hash
1565+
pub hash: bitcoin::BlockHash,
1566+
/// The number of confirmations, or -1 if the block is not on the main chain
1567+
pub confirmations: i64,
1568+
/// The block size
1569+
pub size: u64,
1570+
/// The block size excluding witness data
1571+
pub strippedsize: u64,
1572+
/// The block weight as defined in BIP 141
1573+
pub weight: u64,
1574+
/// The block height or index
1575+
pub height: u64,
1576+
/// The block version
1577+
pub version: u32,
1578+
/// The block version formatted in hexadecimal
1579+
#[serde(rename = "versionHex", with = "crate::serde_hex")]
1580+
pub version_hex: Vec<u8>,
1581+
/// The merkle root
1582+
pub merkleroot: bitcoin::hash_types::TxMerkleNode,
1583+
/// The transaction ids
1584+
pub tx: Vec<GetBlockVerboseTransactionResult>,
1585+
/// The block time
1586+
pub time: usize,
1587+
/// The median block time expressed in UNIX epoch time
1588+
pub mediantime: Option<usize>,
1589+
/// The nonce
1590+
pub nonce: u32,
1591+
/// The bits
1592+
pub bits: String,
1593+
/// The difficulty
1594+
pub difficulty: f64,
1595+
/// Expected number of hashes required to produce the chain up to this block
1596+
#[serde(with = "crate::serde_hex")]
1597+
pub chainwork: Vec<u8>,
1598+
/// The number of transactions in the block
1599+
#[serde(rename = "nTx")]
1600+
pub n_tx: usize,
1601+
/// The hash of the previous block (if available)
1602+
pub previousblockhash: Option<bitcoin::BlockHash>,
1603+
/// The hash of the next block (if available)
1604+
pub nextblockhash: Option<bitcoin::BlockHash>,
1605+
}
1606+
15591607
/// Models the result of "getblocktemplate"
15601608
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
15611609
pub struct GetBlockTemplateResult {
@@ -1782,6 +1830,28 @@ pub struct DecodeRawTransactionResult {
17821830
pub vout: Vec<GetRawTransactionResultVout>,
17831831
}
17841832

1833+
/// Model for Getblock tx verbosity 2
1834+
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
1835+
pub struct GetBlockVerboseTransactionResult {
1836+
pub txid: bitcoin::Txid,
1837+
pub hash: bitcoin::Wtxid,
1838+
pub size: u32,
1839+
pub vsize: u32,
1840+
pub weight: u32,
1841+
pub version: u32,
1842+
pub locktime: u32,
1843+
pub vin: Vec<GetRawTransactionResultVin>,
1844+
pub vout: Vec<GetRawTransactionResultVout>,
1845+
#[serde(
1846+
default,
1847+
with = "bitcoin::amount::serde::as_btc::opt",
1848+
skip_serializing_if = "Option::is_none"
1849+
)]
1850+
pub fee: Option<Amount>,
1851+
#[serde(default, with = "crate::serde_hex")]
1852+
pub hex: Vec<u8>,
1853+
}
1854+
17851855
/// Models the result of "getchaintips"
17861856
pub type GetChainTipsResult = Vec<GetChainTipsResultTip>;
17871857

@@ -1889,10 +1959,7 @@ pub struct FundRawTransactionOptions {
18891959
/// The fee rate to pay per kvB. NB. This field is converted to camelCase
18901960
/// when serialized, so it is receeived by fundrawtransaction as `feeRate`,
18911961
/// which fee rate per kvB, and *not* `fee_rate`, which is per vB.
1892-
#[serde(
1893-
with = "bitcoin::amount::serde::as_btc::opt",
1894-
skip_serializing_if = "Option::is_none"
1895-
)]
1962+
#[serde(with = "bitcoin::amount::serde::as_btc::opt", skip_serializing_if = "Option::is_none")]
18961963
pub fee_rate: Option<Amount>,
18971964
#[serde(skip_serializing_if = "Option::is_none")]
18981965
pub subtract_fee_from_outputs: Option<Vec<u32>>,
@@ -2192,7 +2259,7 @@ where
21922259

21932260
/// deserialize_bip70_network deserializes a Bitcoin Core network according to BIP70
21942261
/// The accepted input variants are: {"main", "test", "signet", "regtest"}
2195-
fn deserialize_bip70_network<'de, D>(deserializer: D) -> Result<Network, D::Error>
2262+
fn deserialize_bip70_network<'de, D>(deserializer: D) -> Result<Network, D::Error>
21962263
where
21972264
D: serde::Deserializer<'de>,
21982265
{
@@ -2201,8 +2268,12 @@ where
22012268
type Value = Network;
22022269

22032270
fn visit_str<E: serde::de::Error>(self, s: &str) -> Result<Self::Value, E> {
2204-
Network::from_core_arg(s)
2205-
.map_err(|_| E::invalid_value(serde::de::Unexpected::Str(s), &"bitcoin network encoded as a string"))
2271+
Network::from_core_arg(s).map_err(|_| {
2272+
E::invalid_value(
2273+
serde::de::Unexpected::Str(s),
2274+
&"bitcoin network encoded as a string",
2275+
)
2276+
})
22062277
}
22072278

22082279
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

0 commit comments

Comments
 (0)