bitcoind_request/command/
get_chain_tips.rs

1use crate::client::Client;
2/*
3getchaintips
4Return information about all known tips in the block tree, including the main chain as well as orphaned branches.
5
6Result:
7[                        (json array)
8  {                      (json object)
9    "height" : n,        (numeric) height of the chain tip
10    "hash" : "hex",      (string) block hash of the tip
11    "branchlen" : n,     (numeric) zero for main chain, otherwise length of branch connecting the tip to the main chain
12    "status" : "str"     (string) status of the chain, "active" for the main chain
13                         Possible values for status:
14                         1.  "invalid"               This branch contains at least one invalid block
15                         2.  "headers-only"          Not all blocks for this branch are available, but the headers are valid
16                         3.  "valid-headers"         All blocks are available for this branch, but they were never fully validated
17                         4.  "valid-fork"            This branch is not part of the active chain, but is fully validated
18                         5.  "active"                This is the tip of the active main chain, which is certainly valid
19  },
20  ...
21]
22
23Examples:
24> bitcoin-cli getchaintips
25> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getchaintips", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
26*/
27use crate::command::request::request;
28use crate::command::CallableCommand;
29use crate::Blockhash;
30use crate::BlockhashHexEncoded;
31use serde::Deserialize;
32use serde::Serialize;
33use serde_json::value::{to_raw_value, RawValue};
34
35pub struct GetChainTipsCommand {}
36impl GetChainTipsCommand {
37    pub fn new() -> Self {
38        GetChainTipsCommand {}
39    }
40}
41#[derive(Serialize, Deserialize, Debug)]
42pub struct Tip {
43    height: u64,    // height of the chain tip
44    hash: String,   // "hex" block hash of the tip
45    branchlen: u64, // zero for main chain, otherwise length of branch connecting the tip to the main chain
46    // TODO: Represent the 5 possible string values this can be using an enum.
47    status: String, //status of the chain, "active" for the main chain
48                    // Possible values for status:
49                    // 1.  "invalid"               This branch contains at least one invalid block
50                    // 2.  "headers-only"          Not all blocks for this branch are available, but the headers are valid
51                    // 3.  "valid-headers"         All blocks are available for this branch, but they were never fully validated
52                    // 4.  "valid-fork"            This branch is not part of the active chain, but is fully validated
53                    // 5.  "active"                This is the tip of the active main chain, which is certainly valid
54}
55#[derive(Serialize, Deserialize, Debug)]
56pub struct GetChainTipsCommandResponse(Vec<Tip>);
57
58impl CallableCommand for GetChainTipsCommand {
59    type Response = GetChainTipsCommandResponse;
60    fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
61        let command = "getchaintips";
62        let params: Vec<Box<RawValue>> = vec![];
63        let r = request(client, command, params);
64        let response: GetChainTipsCommandResponse = r.result()?;
65        Ok(response)
66    }
67}