From 463bd334132e7f8846d4aee667883d85ebef1f4f Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 2 Sep 2024 15:33:40 +1000 Subject: [PATCH] Move code for getblock Seems that we added the `getblock` stuff in the order we implemented it instead of in some sane order. Move the code for `getblock` so it is in the same order in code as it is listed in the `rpc-api.txt` file. Code move only, no other changes. --- client/src/client_sync/v17/blockchain.rs | 24 +- integration_test/src/v17/blockchain.rs | 26 +- integration_test/tests/v17_api.rs | 2 +- integration_test/tests/v18_api.rs | 2 +- integration_test/tests/v19_api.rs | 2 +- integration_test/tests/v20_api.rs | 2 +- integration_test/tests/v21_api.rs | 2 +- integration_test/tests/v22_api.rs | 2 +- integration_test/tests/v23_api.rs | 2 +- integration_test/tests/v24_api.rs | 2 +- integration_test/tests/v25_api.rs | 2 +- integration_test/tests/v26_api.rs | 2 +- integration_test/tests/v27_api.rs | 2 +- json/src/model/blockchain.rs | 94 +++---- json/src/v17/blockchain.rs | 344 +++++++++++------------ 15 files changed, 255 insertions(+), 255 deletions(-) diff --git a/client/src/client_sync/v17/blockchain.rs b/client/src/client_sync/v17/blockchain.rs index 0ab48bb..0d3de1d 100644 --- a/client/src/client_sync/v17/blockchain.rs +++ b/client/src/client_sync/v17/blockchain.rs @@ -9,18 +9,6 @@ //! //! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`. -/// Implements bitcoind JSON-RPC API method `getblockchaininfo` -#[macro_export] -macro_rules! impl_client_v17__getblockchaininfo { - () => { - impl Client { - pub fn get_blockchain_info(&self) -> Result { - self.call("getblockchaininfo", &[]) - } - } - }; -} - /// Implements bitcoind JSON-RPC API method `getbestblockhash` #[macro_export] macro_rules! impl_client_v17__getbestblockhash { @@ -70,6 +58,18 @@ macro_rules! impl_client_v17__getblock { }; } +/// Implements bitcoind JSON-RPC API method `getblockchaininfo` +#[macro_export] +macro_rules! impl_client_v17__getblockchaininfo { + () => { + impl Client { + pub fn get_blockchain_info(&self) -> Result { + self.call("getblockchaininfo", &[]) + } + } + }; +} + /// Implements bitcoind JSON-RPC API method `gettxout` #[macro_export] macro_rules! impl_client_v17__gettxout { diff --git a/integration_test/src/v17/blockchain.rs b/integration_test/src/v17/blockchain.rs index 6fc418b..4a8c56a 100644 --- a/integration_test/src/v17/blockchain.rs +++ b/integration_test/src/v17/blockchain.rs @@ -5,19 +5,6 @@ //! Specifically this is methods found under the `== Blockchain ==` section of the //! API docs of `bitcoind v0.17.1`. -/// Requires `Client` to be in scope and to implement `get_blockchain_info`. -#[macro_export] -macro_rules! impl_test_v17__getblockchaininfo { - () => { - #[test] - fn get_blockchain_info() { - let bitcoind = $crate::bitcoind_no_wallet(); - let json = bitcoind.client.get_blockchain_info().expect("getblockchaininfo"); - assert!(json.into_model().is_ok()); - } - }; -} - /// Requires `Client` to be in scope and to implement `get_best_block_hash`. #[macro_export] macro_rules! impl_test_v17__getbestblockhash { @@ -81,6 +68,19 @@ macro_rules! impl_test_v17__getblock_verbosity_2 { }; } +/// Requires `Client` to be in scope and to implement `get_blockchain_info`. +#[macro_export] +macro_rules! impl_test_v17__getblockchaininfo { + () => { + #[test] + fn get_blockchain_info() { + let bitcoind = $crate::bitcoind_no_wallet(); + let json = bitcoind.client.get_blockchain_info().expect("getblockchaininfo"); + assert!(json.into_model().is_ok()); + } + }; +} + /// Requires `Client` to be in scope and to implement `get_tx_out`. #[macro_export] macro_rules! impl_test_v17__gettxout { diff --git a/integration_test/tests/v17_api.rs b/integration_test/tests/v17_api.rs index 0ae968b..2ee2106 100644 --- a/integration_test/tests/v17_api.rs +++ b/integration_test/tests/v17_api.rs @@ -8,10 +8,10 @@ use integration_test::*; mod blockchain { use super::*; - impl_test_v17__getblockchaininfo!(); impl_test_v17__getbestblockhash!(); impl_test_v17__getblock_verbosity_0!(); impl_test_v17__getblock_verbosity_1!(); + impl_test_v17__getblockchaininfo!(); } // == Control == diff --git a/integration_test/tests/v18_api.rs b/integration_test/tests/v18_api.rs index 0e85c9c..97f4f4f 100644 --- a/integration_test/tests/v18_api.rs +++ b/integration_test/tests/v18_api.rs @@ -8,10 +8,10 @@ use integration_test::*; mod blockchain { use super::*; - impl_test_v17__getblockchaininfo!(); impl_test_v17__getbestblockhash!(); impl_test_v17__getblock_verbosity_0!(); impl_test_v17__getblock_verbosity_1!(); + impl_test_v17__getblockchaininfo!(); } // == Control == diff --git a/integration_test/tests/v19_api.rs b/integration_test/tests/v19_api.rs index dbc3b73..62cc351 100644 --- a/integration_test/tests/v19_api.rs +++ b/integration_test/tests/v19_api.rs @@ -8,10 +8,10 @@ use integration_test::*; mod blockchain { use super::*; - impl_test_v17__getblockchaininfo!(); impl_test_v17__getbestblockhash!(); impl_test_v17__getblock_verbosity_0!(); impl_test_v17__getblock_verbosity_1!(); + impl_test_v17__getblockchaininfo!(); } // == Control == diff --git a/integration_test/tests/v20_api.rs b/integration_test/tests/v20_api.rs index 926aa88..791b10d 100644 --- a/integration_test/tests/v20_api.rs +++ b/integration_test/tests/v20_api.rs @@ -8,10 +8,10 @@ use integration_test::*; mod blockchain { use super::*; - impl_test_v17__getblockchaininfo!(); impl_test_v17__getbestblockhash!(); impl_test_v17__getblock_verbosity_0!(); impl_test_v17__getblock_verbosity_1!(); + impl_test_v17__getblockchaininfo!(); } // == Control == diff --git a/integration_test/tests/v21_api.rs b/integration_test/tests/v21_api.rs index f71c07d..79470c0 100644 --- a/integration_test/tests/v21_api.rs +++ b/integration_test/tests/v21_api.rs @@ -8,10 +8,10 @@ use integration_test::*; mod blockchain { use super::*; - impl_test_v17__getblockchaininfo!(); impl_test_v17__getbestblockhash!(); impl_test_v17__getblock_verbosity_0!(); impl_test_v17__getblock_verbosity_1!(); + impl_test_v17__getblockchaininfo!(); } // == Control == diff --git a/integration_test/tests/v22_api.rs b/integration_test/tests/v22_api.rs index 280a7c5..11a316b 100644 --- a/integration_test/tests/v22_api.rs +++ b/integration_test/tests/v22_api.rs @@ -8,10 +8,10 @@ use integration_test::*; mod blockchain { use super::*; - impl_test_v17__getblockchaininfo!(); impl_test_v17__getbestblockhash!(); impl_test_v17__getblock_verbosity_0!(); impl_test_v17__getblock_verbosity_1!(); + impl_test_v17__getblockchaininfo!(); } // == Control == diff --git a/integration_test/tests/v23_api.rs b/integration_test/tests/v23_api.rs index 44c9218..daa124e 100644 --- a/integration_test/tests/v23_api.rs +++ b/integration_test/tests/v23_api.rs @@ -8,10 +8,10 @@ use integration_test::*; mod blockchain { use super::*; - impl_test_v17__getblockchaininfo!(); impl_test_v17__getbestblockhash!(); impl_test_v17__getblock_verbosity_0!(); impl_test_v17__getblock_verbosity_1!(); + impl_test_v17__getblockchaininfo!(); } // == Control == diff --git a/integration_test/tests/v24_api.rs b/integration_test/tests/v24_api.rs index 998f3f3..e233688 100644 --- a/integration_test/tests/v24_api.rs +++ b/integration_test/tests/v24_api.rs @@ -8,10 +8,10 @@ use integration_test::*; mod blockchain { use super::*; - impl_test_v17__getblockchaininfo!(); impl_test_v17__getbestblockhash!(); impl_test_v17__getblock_verbosity_0!(); impl_test_v17__getblock_verbosity_1!(); + impl_test_v17__getblockchaininfo!(); } // == Control == diff --git a/integration_test/tests/v25_api.rs b/integration_test/tests/v25_api.rs index 17ee034..6dddb91 100644 --- a/integration_test/tests/v25_api.rs +++ b/integration_test/tests/v25_api.rs @@ -8,10 +8,10 @@ use integration_test::*; mod blockchain { use super::*; - impl_test_v17__getblockchaininfo!(); impl_test_v17__getbestblockhash!(); impl_test_v17__getblock_verbosity_0!(); impl_test_v17__getblock_verbosity_1!(); + impl_test_v17__getblockchaininfo!(); } // == Control == diff --git a/integration_test/tests/v26_api.rs b/integration_test/tests/v26_api.rs index e81196e..ef15746 100644 --- a/integration_test/tests/v26_api.rs +++ b/integration_test/tests/v26_api.rs @@ -8,10 +8,10 @@ use integration_test::*; mod blockchain { use super::*; - impl_test_v17__getblockchaininfo!(); impl_test_v17__getbestblockhash!(); impl_test_v17__getblock_verbosity_0!(); impl_test_v17__getblock_verbosity_1!(); + impl_test_v17__getblockchaininfo!(); } // == Control == diff --git a/integration_test/tests/v27_api.rs b/integration_test/tests/v27_api.rs index bc4f2a4..5fa4e1f 100644 --- a/integration_test/tests/v27_api.rs +++ b/integration_test/tests/v27_api.rs @@ -8,10 +8,10 @@ use integration_test::*; mod blockchain { use super::*; - impl_test_v17__getblockchaininfo!(); impl_test_v17__getbestblockhash!(); impl_test_v17__getblock_verbosity_0!(); impl_test_v17__getblock_verbosity_1!(); + impl_test_v17__getblockchaininfo!(); } // == Control == diff --git a/json/src/model/blockchain.rs b/json/src/model/blockchain.rs index 26e1918..d841cfb 100644 --- a/json/src/model/blockchain.rs +++ b/json/src/model/blockchain.rs @@ -17,6 +17,53 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub struct GetBestBlockHash(pub BlockHash); +/// Models the result of JSON-RPC method `getblock` with verbosity set to 0. +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct GetBlockVerbosityZero(pub Block); + +/// Models the result of JSON-RPC method `getblock` with verbosity set to 1. +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct GetBlockVerbosityOne { + /// The block hash (same as provided) in RPC call. + pub hash: BlockHash, + /// The number of confirmations, or -1 if the block is not on the main chain. + pub confirmations: i32, + /// The block size. + pub size: usize, + /// The block size excluding witness data. + pub stripped_size: Option, // Weight? + /// The block weight as defined in BIP-141. + pub weight: Weight, + /// The block height or index. + pub height: usize, + /// The block version. + pub version: block::Version, + /// The block version formatted in hexadecimal. + pub version_hex: String, + /// The merkle root. + pub merkle_root: String, + /// The transaction ids. + pub tx: Vec, + /// The block time expressed in UNIX epoch time. + pub time: usize, + /// The median block time expressed in UNIX epoch time. + pub median_time: Option, + /// The nonce. + pub nonce: u32, + /// The bits. + pub bits: CompactTarget, + /// The difficulty. + pub difficulty: f64, + /// Expected number of hashes required to produce the chain up to this block (in hex). + pub chain_work: Work, + /// The number of transactions in the block. + pub n_tx: u32, + /// The hash of the previous block (if available). + pub previous_block_hash: Option, + /// The hash of the next block (if available). + pub next_block_hash: Option, +} + /// Models the result of JSON-RPC method `getblockchaininfo`. #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub struct GetBlockchainInfo { @@ -129,53 +176,6 @@ pub struct Bip9SoftforkStatistics { pub possible: Option, } -/// Models the result of JSON-RPC method `getblock` with verbosity set to 0. -#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] -pub struct GetBlockVerbosityZero(pub Block); - -/// Models the result of JSON-RPC method `getblock` with verbosity set to 1. -#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] -pub struct GetBlockVerbosityOne { - /// The block hash (same as provided) in RPC call. - pub hash: BlockHash, - /// The number of confirmations, or -1 if the block is not on the main chain. - pub confirmations: i32, - /// The block size. - pub size: usize, - /// The block size excluding witness data. - pub stripped_size: Option, // Weight? - /// The block weight as defined in BIP-141. - pub weight: Weight, - /// The block height or index. - pub height: usize, - /// The block version. - pub version: block::Version, - /// The block version formatted in hexadecimal. - pub version_hex: String, - /// The merkle root. - pub merkle_root: String, - /// The transaction ids. - pub tx: Vec, - /// The block time expressed in UNIX epoch time. - pub time: usize, - /// The median block time expressed in UNIX epoch time. - pub median_time: Option, - /// The nonce. - pub nonce: u32, - /// The bits. - pub bits: CompactTarget, - /// The difficulty. - pub difficulty: f64, - /// Expected number of hashes required to produce the chain up to this block (in hex). - pub chain_work: Work, - /// The number of transactions in the block. - pub n_tx: u32, - /// The hash of the previous block (if available). - pub previous_block_hash: Option, - /// The hash of the next block (if available). - pub next_block_hash: Option, -} - /// Models the result of JSON-RPC method `gettxout`. #[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] pub struct GetTxOut { diff --git a/json/src/v17/blockchain.rs b/json/src/v17/blockchain.rs index b086597..46ee53e 100644 --- a/json/src/v17/blockchain.rs +++ b/json/src/v17/blockchain.rs @@ -38,6 +38,178 @@ impl GetBestBlockHash { pub fn block_hash(self) -> Result { Ok(self.into_model()?.0) } } +/// Result of JSON-RPC method `getblock` with verbosity set to 0. +/// +/// A string that is serialized, hex-encoded data for block 'hash'. +/// +/// Method call: `getblock "blockhash" ( verbosity )` +#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] +pub struct GetBlockVerbosityZero(pub String); + +impl GetBlockVerbosityZero { + /// Converts version specific type to a version in-specific, more strongly typed type. + pub fn into_model(self) -> Result { + let block = encode::deserialize_hex(&self.0)?; + Ok(model::GetBlockVerbosityZero(block)) + } + + /// Converts json straight to a `bitcoin::Block`. + pub fn block(self) -> Result { Ok(self.into_model()?.0) } +} + +/// Result of JSON-RPC method `getblock` with verbosity set to 1. +#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] +pub struct GetBlockVerbosityOne { + /// The block hash (same as provided) in RPC call. + pub hash: String, + /// The number of confirmations, or -1 if the block is not on the main chain. + pub confirmations: i32, + /// The block size. + pub size: usize, + /// The block size excluding witness data. + #[serde(rename = "strippedsize")] + pub stripped_size: Option, + /// The block weight as defined in BIP-141. + pub weight: u64, + /// The block height or index. + pub height: usize, + /// The block version. + pub version: i32, + /// The block version formatted in hexadecimal. + #[serde(rename = "versionHex")] + pub version_hex: String, + /// The merkle root + #[serde(rename = "merkleroot")] + pub merkle_root: String, + /// The transaction ids + pub tx: Vec, + /// The block time expressed in UNIX epoch time. + pub time: usize, + /// The median block time expressed in UNIX epoch time. + #[serde(rename = "mediantime")] + pub median_time: Option, + /// The nonce + pub nonce: u32, + /// The bits. + pub bits: String, + /// The difficulty. + pub difficulty: f64, + /// Expected number of hashes required to produce the chain up to this block (in hex). + #[serde(rename = "chainwork")] + pub chain_work: String, + /// The number of transactions in the block. + #[serde(rename = "nTx")] + pub n_tx: u32, + /// The hash of the previous block (if available). + #[serde(rename = "previousblockhash")] + pub previous_block_hash: Option, + /// The hash of the next block (if available). + #[serde(rename = "nextblockhash")] + pub next_block_hash: Option, +} + +impl GetBlockVerbosityOne { + /// Converts version specific type to a version in-specific, more strongly typed type. + pub fn into_model(self) -> Result { + use GetBlockVerbosityOneError as E; + + let hash = self.hash.parse::().map_err(E::Hash)?; + let weight = Weight::from_wu(self.weight); // TODO: Confirm this uses weight units. + let version = block::Version::from_consensus(self.version); + + // FIXME: Is there a better way to handle the error without type annotations on `collect`? + let tx = self + .tx + .iter() + .map(|t| encode::deserialize_hex::(t).map_err(E::Tx)) + .collect::, _>>()?; + + // FIXME: Is unprefixed correct? + let bits = CompactTarget::from_unprefixed_hex(&self.bits).map_err(E::Bits)?; + let chain_work = Work::from_unprefixed_hex(&self.chain_work).map_err(E::ChainWork)?; + + let previous_block_hash = match self.previous_block_hash { + Some(hash) => Some(hash.parse::().map_err(E::PreviousBlockHash)?), + None => None, + }; + let next_block_hash = match self.next_block_hash { + Some(hash) => Some(hash.parse::().map_err(E::NextBlockHash)?), + None => None, + }; + + Ok(model::GetBlockVerbosityOne { + hash, + confirmations: self.confirmations, + size: self.size, + stripped_size: self.stripped_size, + weight, + height: self.height, + version, + version_hex: self.version_hex, + merkle_root: self.merkle_root, // TODO: Use hash, which one depends on segwit or not + tx, + time: self.time, // TODO: Use stronger type. + median_time: self.median_time, + nonce: self.nonce, + bits, + difficulty: self.difficulty, + chain_work, + n_tx: self.n_tx, + previous_block_hash, + next_block_hash, + }) + } +} + +/// Error when converting a `GetBlockVerbasityOne` type into the model type. +#[derive(Debug)] +pub enum GetBlockVerbosityOneError { + /// Conversion of the transaction `hash` field failed. + Hash(hex::HexToArrayError), + /// Conversion of the transaction `hex` field failed. + Tx(encode::FromHexError), + /// Conversion of the transaction `bits` field failed. + Bits(UnprefixedHexError), + /// Conversion of the transaction `chain_work` field failed. + ChainWork(UnprefixedHexError), + /// Conversion of the transaction `previous_block_hash` field failed. + PreviousBlockHash(hex::HexToArrayError), + /// Conversion of the transaction `next_block_hash` field failed. + NextBlockHash(hex::HexToArrayError), +} + +impl fmt::Display for GetBlockVerbosityOneError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use GetBlockVerbosityOneError::*; + + match *self { + Hash(ref e) => write_err!(f, "conversion of the `hash` field failed"; e), + Tx(ref e) => write_err!(f, "conversion of the `tx` field failed"; e), + Bits(ref e) => write_err!(f, "conversion of the `bits` field failed"; e), + ChainWork(ref e) => write_err!(f, "conversion of the `chain_ork` field failed"; e), + PreviousBlockHash(ref e) => + write_err!(f, "conversion of the `previous_block_hash` field failed"; e), + NextBlockHash(ref e) => + write_err!(f, "conversion of the `next_block_hash` field failed"; e), + } + } +} + +impl std::error::Error for GetBlockVerbosityOneError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + use GetBlockVerbosityOneError::*; + + match *self { + Hash(ref e) => Some(e), + Tx(ref e) => Some(e), + Bits(ref e) => Some(e), + ChainWork(ref e) => Some(e), + PreviousBlockHash(ref e) => Some(e), + NextBlockHash(ref e) => Some(e), + } + } +} + /// Result of JSON-RPC method `getblockchaininfo`. /// /// Method call: `getblockchaininfo` @@ -220,178 +392,6 @@ impl std::error::Error for GetBlockchainInfoError { } } -/// Result of JSON-RPC method `getblock` with verbosity set to 0. -/// -/// A string that is serialized, hex-encoded data for block 'hash'. -/// -/// Method call: `getblock "blockhash" ( verbosity )` -#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] -pub struct GetBlockVerbosityZero(pub String); - -impl GetBlockVerbosityZero { - /// Converts version specific type to a version in-specific, more strongly typed type. - pub fn into_model(self) -> Result { - let block = encode::deserialize_hex(&self.0)?; - Ok(model::GetBlockVerbosityZero(block)) - } - - /// Converts json straight to a `bitcoin::Block`. - pub fn block(self) -> Result { Ok(self.into_model()?.0) } -} - -/// Result of JSON-RPC method `getblock` with verbosity set to 1. -#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] -pub struct GetBlockVerbosityOne { - /// The block hash (same as provided) in RPC call. - pub hash: String, - /// The number of confirmations, or -1 if the block is not on the main chain. - pub confirmations: i32, - /// The block size. - pub size: usize, - /// The block size excluding witness data. - #[serde(rename = "strippedsize")] - pub stripped_size: Option, - /// The block weight as defined in BIP-141. - pub weight: u64, - /// The block height or index. - pub height: usize, - /// The block version. - pub version: i32, - /// The block version formatted in hexadecimal. - #[serde(rename = "versionHex")] - pub version_hex: String, - /// The merkle root - #[serde(rename = "merkleroot")] - pub merkle_root: String, - /// The transaction ids - pub tx: Vec, - /// The block time expressed in UNIX epoch time. - pub time: usize, - /// The median block time expressed in UNIX epoch time. - #[serde(rename = "mediantime")] - pub median_time: Option, - /// The nonce - pub nonce: u32, - /// The bits. - pub bits: String, - /// The difficulty. - pub difficulty: f64, - /// Expected number of hashes required to produce the chain up to this block (in hex). - #[serde(rename = "chainwork")] - pub chain_work: String, - /// The number of transactions in the block. - #[serde(rename = "nTx")] - pub n_tx: u32, - /// The hash of the previous block (if available). - #[serde(rename = "previousblockhash")] - pub previous_block_hash: Option, - /// The hash of the next block (if available). - #[serde(rename = "nextblockhash")] - pub next_block_hash: Option, -} - -impl GetBlockVerbosityOne { - /// Converts version specific type to a version in-specific, more strongly typed type. - pub fn into_model(self) -> Result { - use GetBlockVerbosityOneError as E; - - let hash = self.hash.parse::().map_err(E::Hash)?; - let weight = Weight::from_wu(self.weight); // TODO: Confirm this uses weight units. - let version = block::Version::from_consensus(self.version); - - // FIXME: Is there a better way to handle the error without type annotations on `collect`? - let tx = self - .tx - .iter() - .map(|t| encode::deserialize_hex::(t).map_err(E::Tx)) - .collect::, _>>()?; - - // FIXME: Is unprefixed correct? - let bits = CompactTarget::from_unprefixed_hex(&self.bits).map_err(E::Bits)?; - let chain_work = Work::from_unprefixed_hex(&self.chain_work).map_err(E::ChainWork)?; - - let previous_block_hash = match self.previous_block_hash { - Some(hash) => Some(hash.parse::().map_err(E::PreviousBlockHash)?), - None => None, - }; - let next_block_hash = match self.next_block_hash { - Some(hash) => Some(hash.parse::().map_err(E::NextBlockHash)?), - None => None, - }; - - Ok(model::GetBlockVerbosityOne { - hash, - confirmations: self.confirmations, - size: self.size, - stripped_size: self.stripped_size, - weight, - height: self.height, - version, - version_hex: self.version_hex, - merkle_root: self.merkle_root, // TODO: Use hash, which one depends on segwit or not - tx, - time: self.time, // TODO: Use stronger type. - median_time: self.median_time, - nonce: self.nonce, - bits, - difficulty: self.difficulty, - chain_work, - n_tx: self.n_tx, - previous_block_hash, - next_block_hash, - }) - } -} - -/// Error when converting a `GetBlockVerbasityOne` type into the model type. -#[derive(Debug)] -pub enum GetBlockVerbosityOneError { - /// Conversion of the transaction `hash` field failed. - Hash(hex::HexToArrayError), - /// Conversion of the transaction `hex` field failed. - Tx(encode::FromHexError), - /// Conversion of the transaction `bits` field failed. - Bits(UnprefixedHexError), - /// Conversion of the transaction `chain_work` field failed. - ChainWork(UnprefixedHexError), - /// Conversion of the transaction `previous_block_hash` field failed. - PreviousBlockHash(hex::HexToArrayError), - /// Conversion of the transaction `next_block_hash` field failed. - NextBlockHash(hex::HexToArrayError), -} - -impl fmt::Display for GetBlockVerbosityOneError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use GetBlockVerbosityOneError::*; - - match *self { - Hash(ref e) => write_err!(f, "conversion of the `hash` field failed"; e), - Tx(ref e) => write_err!(f, "conversion of the `tx` field failed"; e), - Bits(ref e) => write_err!(f, "conversion of the `bits` field failed"; e), - ChainWork(ref e) => write_err!(f, "conversion of the `chain_ork` field failed"; e), - PreviousBlockHash(ref e) => - write_err!(f, "conversion of the `previous_block_hash` field failed"; e), - NextBlockHash(ref e) => - write_err!(f, "conversion of the `next_block_hash` field failed"; e), - } - } -} - -impl std::error::Error for GetBlockVerbosityOneError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - use GetBlockVerbosityOneError::*; - - match *self { - Hash(ref e) => Some(e), - Tx(ref e) => Some(e), - Bits(ref e) => Some(e), - ChainWork(ref e) => Some(e), - PreviousBlockHash(ref e) => Some(e), - NextBlockHash(ref e) => Some(e), - } - } -} - /// Result of JSON-RPC method `gettxout`. /// /// > gettxout "txid" n ( include_mempool )