bitcoind_request/command/get_blockchain_info.rs
1/*
2getblockchaininfo
3
4Returns an object containing various state info regarding blockchain processing.
5
6Result:
7{ (json object)
8 "chain" : "str", (string) current network name (main, test, regtest)
9 "blocks" : n, (numeric) the height of the most-work fully-validated chain. The genesis block has height 0
10 "headers" : n, (numeric) the current number of headers we have validated
11 "bestblockhash" : "str", (string) the hash of the currently best block
12 "difficulty" : n, (numeric) the current difficulty
13 "mediantime" : n, (numeric) median time for the current best block
14 "verificationprogress" : n, (numeric) estimate of verification progress [0..1]
15 "initialblockdownload" : true|false, (boolean) (debug information) estimate of whether this node is in Initial Block Download mode
16 "chainwork" : "hex", (string) total amount of work in active chain, in hexadecimal
17 "size_on_disk" : n, (numeric) the estimated size of the block and undo files on disk
18 "pruned" : true|false, (boolean) if the blocks are subject to pruning
19 "pruneheight" : n, (numeric) lowest-height complete block stored (only present if pruning is enabled)
20 "automatic_pruning" : true|false, (boolean) whether automatic pruning is enabled (only present if pruning is enabled)
21 "prune_target_size" : n, (numeric) the target size used by pruning (only present if automatic pruning is enabled)
22 "softforks" : { (json object) status of softforks
23 "xxxx" : { (json object) name of the softfork
24 "type" : "str", (string) one of "buried", "bip9"
25 "bip9" : { (json object) status of bip9 softforks (only for "bip9" type)
26 "status" : "str", (string) one of "defined", "started", "locked_in", "active", "failed"
27 "bit" : n, (numeric) the bit (0-28) in the block version field used to signal this softfork (only for "started" status)
28 "start_time" : xxx, (numeric) the minimum median time past of a block at which the bit gains its meaning
29 "timeout" : xxx, (numeric) the median time past of a block at which the deployment is considered failed if not yet locked in
30 "since" : n, (numeric) height of the first block to which the status applies
31 "statistics" : { (json object) numeric statistics about BIP9 signalling for a softfork (only for "started" status)
32 "period" : n, (numeric) the length in blocks of the BIP9 signalling period
33 "threshold" : n, (numeric) the number of blocks with the version bit set required to activate the feature
34 "elapsed" : n, (numeric) the number of blocks elapsed since the beginning of the current period
35 "count" : n, (numeric) the number of blocks with the version bit set in the current period
36 "possible" : true|false (boolean) returns false if there are not enough blocks left in this period to pass activation threshold
37 }
38 },
39 "height" : n, (numeric) height of the first block which the rules are or will be enforced (only for "buried" type, or "bip9" type with "active" status)
40 "active" : true|false (boolean) true if the rules are enforced for the mempool and the next block
41 },
42 ...
43 },
44 "warnings" : "str" (string) any network and blockchain warnings
45}
46
47Examples:
48> bitcoin-cli getblockchaininfo
49> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockchaininfo", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
50*/
51use std::collections::HashMap;
52
53use crate::client::Client;
54use crate::command::request::request;
55use crate::command::CallableCommand;
56use serde::Deserialize;
57use serde::Serialize;
58use serde_json::value::RawValue;
59
60#[derive(Serialize, Deserialize, Debug)]
61#[serde(rename_all = "camelCase")]
62pub struct Statistics {
63 pub period: u64, // the length in blocks of the BIP9 signalling period
64 pub threshold: u64, // the number of blocks with the version bit set required to activate the feature
65 pub elapsed: u64, // the number of blocks elapsed since the beginning of the current period
66 pub count: u64, // the number of blocks with the version bit set in the current period
67 pub possible: bool, // returns false if there are not enough blocks left in this period to pass activation threshold
68}
69
70#[derive(Serialize, Deserialize, Debug)]
71#[serde(rename_all = "camelCase")]
72pub struct Bip9 {
73 // TODO: can only be a couple strings
74 pub status: String, // one of "defined", "started", "locked_in", "active", "failed"
75 pub bit: u64, // the bit (0-28) in the block version field used to signal this softfork (only for "started" status)
76 pub start_time: u64, //the minimum median time past of a block at which the bit gains its meaning
77 pub timeout: u64, // the median time past of a block at which the deployment is considered failed if not yet locked in
78 pub since: u64, // height of the first block to which the status applies
79 pub statistics: Statistics, // numeric statistics about BIP9 signalling for a softfork (only for "started" status)
80}
81
82#[derive(Serialize, Deserialize, Debug)]
83#[serde(rename_all = "camelCase")]
84pub struct SoftforkBip9Response {
85 #[serde(rename = "type")]
86 pub type_: String, // one of "buried", "bip9"
87 pub bip9: Bip9, // status of bip9 softforks (only for "bip9" type)
88 pub height: u64, // height of the first block which the rules are or will be enforced (only for "buried" type, or "bip9" type with "active" status)
89 pub active: bool, // true if the rules are enforced for the mempool and the next block
90}
91#[derive(Serialize, Deserialize, Debug)]
92#[serde(rename_all = "camelCase")]
93#[serde(untagged)]
94pub enum SoftFork {
95 Bip9(SoftforkBip9Response),
96 NonBip9(NonBip9SoftforkResponse),
97}
98#[derive(Serialize, Deserialize, Debug)]
99#[serde(rename_all = "camelCase")]
100pub struct NonBip9SoftforkResponse {
101 #[serde(rename = "type")]
102 pub type_: String, // one of "buried", "bip9"
103 pub height: u64, // height of the first block which the rules are or will be enforced (only for "buried" type, or "bip9" type with "active" status)
104 pub active: bool, // true if the rules are enforced for the mempool and the next block
105}
106
107#[derive(Serialize, Deserialize, Debug)]
108pub struct GetBlockchainInfoCommandResponse {
109 pub chain: String, // current network name (main, test, regtest)
110 pub blocks: u64, //the height of the most-work fully-validated chain. The genesis block has height 0
111 pub headers: u64, // the current number of headers we have validated
112 pub bestblockhash: String, //the hash of the currently best block
113 pub difficulty: f64, // the current difficulty
114 pub mediantime: u64, // median time for the current best block
115 // TODO: is only between 0-1
116 pub verificationprogress: f64, // estimate of verification progress [0..1]
117 pub initialblockdownload: bool, // (debug information) estimate of whether this node is in Initial Block Download mode
118 pub chainwork: String, // "hex" total amount of work in active chain, in hexadecimal
119 pub size_on_disk: u64, // the estimated size of the block and undo files on disk
120 pub pruned: bool, // if the blocks are subject to pruning
121 pub pruneheight: Option<u64>, // lowest-height complete block stored (only present if pruning is enabled)
122 pub automatic_pruning: Option<bool>, // whether automatic pruning is enabled (only present if pruning is enabled)
123 pub prune_target_size: Option<u64>, //the target size used by pruning (only present if automatic pruning is enabled)
124 pub softforks: HashMap<String, SoftFork>, // status of softforks
125 pub warnings: String, // any network and blockchain warnings
126}
127
128pub struct GetBlockchainInfoCommand {}
129
130impl GetBlockchainInfoCommand {
131 pub fn new() -> Self {
132 GetBlockchainInfoCommand {}
133 }
134}
135
136impl CallableCommand for GetBlockchainInfoCommand {
137 type Response = GetBlockchainInfoCommandResponse;
138 fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
139 let command = "getblockchaininfo";
140 let params: Vec<Box<RawValue>> = vec![];
141 let r = request(client, command, params);
142 let response: GetBlockchainInfoCommandResponse = r.result()?;
143 Ok(response)
144 }
145}