bitcoind_request/command/
get_network_hash_ps.rs

1/*
2getnetworkhashps ( nblocks height )
3
4Returns the estimated network hashes per second based on the last n blocks.
5Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.
6Pass in [height] to estimate the network speed at the time when a certain block was found.
7
8Arguments:
91. nblocks    (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.
102. height     (numeric, optional, default=-1) To estimate at the time of the given height.
11
12Result:
13n    (numeric) Hashes per second estimated
14
15Examples:
16> bitcoin-cli getnetworkhashps
17> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getnetworkhashps", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
18*/
19use crate::{
20    client::Client,
21    command::{request::request, CallableCommand},
22};
23use serde::{Deserialize, Serialize};
24use serde_json::value::to_raw_value;
25
26const GET_NETWORK_HASH_PS_COMMAND: &str = "getnetworkhashps";
27const DEFAULT_N_BLOCKS: u64 = 120;
28const N_BLOCKS_ARGUMENT_FOR_BLOCKS_SINCE_LAST_DIFFICULT_CHANGE: i64 = -1;
29const HEIGHT_ARGUMENT_FOR_CALCULATING_BASED_ON_CURRENT_HEIGHT: i64 = -1;
30
31#[derive(Serialize, Deserialize, Debug)]
32pub struct GetNetworkHashPsCommandResponse(pub f64);
33
34pub enum BlocksToIncludeArg {
35    NBlocks(u64),
36    BlocksSinceLastDifficultyChange,
37}
38
39pub enum HeightArg {
40    Height(u64),
41    CurrentHeight,
42}
43pub struct GetNetworkHashPsCommand {
44    n_blocks: BlocksToIncludeArg, // (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.
45    height: HeightArg, //(numeric, optional, default=-1) To estimate at the time of the given height.
46}
47impl GetNetworkHashPsCommand {
48    pub fn new() -> Self {
49        GetNetworkHashPsCommand {
50            n_blocks: BlocksToIncludeArg::NBlocks(DEFAULT_N_BLOCKS), // default of 120 is from the docs: https://bitcoincore.org/en/doc/0.21.0/rpc/mining/getnetworkhashps/
51            height: HeightArg::CurrentHeight, // default of -1 (which means current height), is
52                                              // defined in the docs: https://bitcoincore.org/en/doc/0.21.0/rpc/mining/getnetworkhashps/
53        }
54    }
55    pub fn set_n_blocks(mut self, n_blocks: BlocksToIncludeArg) -> Self {
56        self.n_blocks = n_blocks;
57        self
58    }
59    pub fn set_height(mut self, height: HeightArg) -> Self {
60        self.height = height;
61        self
62    }
63}
64impl CallableCommand for GetNetworkHashPsCommand {
65    type Response = GetNetworkHashPsCommandResponse;
66    fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
67        let mut params: Vec<i64> = vec![];
68        let n_blocks_arg: i64 = match self.n_blocks {
69            BlocksToIncludeArg::NBlocks(n_blocks) => n_blocks as i64,
70            BlocksToIncludeArg::BlocksSinceLastDifficultyChange => {
71                N_BLOCKS_ARGUMENT_FOR_BLOCKS_SINCE_LAST_DIFFICULT_CHANGE
72            }
73        };
74        let height_arg = match self.height {
75            HeightArg::Height(height) => height as i64,
76            HeightArg::CurrentHeight => HEIGHT_ARGUMENT_FOR_CALCULATING_BASED_ON_CURRENT_HEIGHT,
77        };
78
79        let n_blocks_arg_raw_value = to_raw_value(&n_blocks_arg).unwrap();
80        let height_arg_raw_value = to_raw_value(&height_arg).unwrap();
81        let mut params = vec![n_blocks_arg_raw_value, height_arg_raw_value];
82        let r = request(client, GET_NETWORK_HASH_PS_COMMAND, params);
83        let response: GetNetworkHashPsCommandResponse = r.result()?;
84        Ok(response)
85    }
86}