bitcoind_request/command/
get_block_header.rs

1use crate::client::Client;
2/*
3getblockheader "blockhash" ( verbose )
4
5If verbose is false, returns a string that is serialized, hex-encoded data for blockheader 'hash'.
6If verbose is true, returns an Object with information about blockheader <hash>.
7
8Arguments:
91. blockhash    (string, required) The block hash
102. verbose      (boolean, optional, default=true) true for a json object, false for the hex-encoded data
11
12Result (for verbose = true):
13{                                 (json object)
14  "hash" : "hex",                 (string) the block hash (same as provided)
15  "confirmations" : n,            (numeric) The number of confirmations, or -1 if the block is not on the main chain
16  "height" : n,                   (numeric) The block height or index
17  "version" : n,                  (numeric) The block version
18  "versionHex" : "hex",           (string) The block version formatted in hexadecimal
19  "merkleroot" : "hex",           (string) The merkle root
20  "time" : xxx,                   (numeric) The block time expressed in UNIX epoch time
21  "mediantime" : xxx,             (numeric) The median block time expressed in UNIX epoch time
22  "nonce" : n,                    (numeric) The nonce
23  "bits" : "hex",                 (string) The bits
24  "difficulty" : n,               (numeric) The difficulty
25  "chainwork" : "hex",            (string) Expected number of hashes required to produce the current chain
26  "nTx" : n,                      (numeric) The number of transactions in the block
27  "previousblockhash" : "hex",    (string) The hash of the previous block
28  "nextblockhash" : "hex"         (string) The hash of the next block
29}
30
31Result (for verbose=false):
32"hex"    (string) A string that is serialized, hex-encoded data for block 'hash'
33
34Examples:
35> bitcoin-cli getblockheader "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"
36> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockheader", "params": ["00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
37 */
38use crate::command::{request::request, CallableCommand};
39use crate::Blockhash;
40use serde::{Deserialize, Serialize};
41use serde_json::value::to_raw_value;
42
43#[derive(Serialize, Deserialize, Debug)]
44#[serde(untagged)]
45pub enum GetBlockHeaderCommandResponse {
46    BlockHash(String),
47    BlockHeader(BlockHeader),
48}
49
50#[derive(Serialize, Deserialize, Debug)]
51#[serde(rename_all = "camelCase")]
52pub struct BlockHeader {
53    pub hash: String,        // "hex" (string) the block hash (same as provided)
54    pub confirmations: i64, // The number of confirmations, or -1 if the block is not on the main chain
55    pub height: u64,        // The block height or index
56    pub version: u64,       // (numeric) The block version
57    pub version_hex: String, // "hex" The block version formatted in hexadecimal
58    pub merkleroot: String, // "hex" The merkle root
59    pub time: u64,          // "unix epoch time" The block time expressed in UNIX epoch time
60    pub mediantime: u64,    // "unix epoch time" The median block time expressed in UNIX epoch time
61    pub nonce: u64,         // The nonce
62    pub bits: String,       // "hex" The bits
63    pub difficulty: f64,    // The difficulty
64    pub chainwork: String, // "hex" Expected number of hashes required to produce the chain up to this block (in hex)
65    pub n_tx: u64,         // The number of transactions in the block
66    pub previousblockhash: Option<String>, // The hash of the previous block
67    // TODO: Why isn't this always there?
68    pub nextblockhash: Option<String>, // The hash of the next block
69}
70
71pub struct GetBlockHeaderCommand {
72    blockhash: Blockhash,
73    verbose: bool,
74}
75impl GetBlockHeaderCommand {
76    pub fn new(blockhash: Blockhash) -> Self {
77        GetBlockHeaderCommand {
78            blockhash,
79            verbose: true,
80        }
81    }
82    pub fn verbose(&mut self, verbose: bool) -> &Self {
83        self.verbose = verbose;
84        self
85    }
86}
87impl CallableCommand for GetBlockHeaderCommand {
88    type Response = GetBlockHeaderCommandResponse;
89    fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
90        let verbose_arg = self.verbose;
91        let blockhash_arg = &self.blockhash.0;
92        let blockhash_arg_raw_value = to_raw_value(&blockhash_arg).unwrap();
93        let verbose_arg_raw_value = to_raw_value(&verbose_arg).unwrap();
94        let command = "getblockheader";
95        let params = vec![blockhash_arg_raw_value, verbose_arg_raw_value];
96        let r = request(client, command, params);
97        let response: GetBlockHeaderCommandResponse = r.result()?;
98        Ok(response)
99    }
100}