bitcoind_request/command/
get_mining_info.rs

1/*
2getmininginfo
3
4Returns a json object containing mining-related information.
5Result:
6{                              (json object)
7  "blocks" : n,                (numeric) The current block
8  "currentblockweight" : n,    (numeric, optional) The block weight of the last assembled block (only present if a block was ever assembled)
9  "currentblocktx" : n,        (numeric, optional) The number of block transactions of the last assembled block (only present if a block was ever assembled)
10  "difficulty" : n,            (numeric) The current difficulty
11  "networkhashps" : n,         (numeric) The network hashes per second
12  "pooledtx" : n,              (numeric) The size of the mempool
13  "chain" : "str",             (string) current network name (main, test, regtest)
14  "warnings" : "str"           (string) any network and blockchain warnings
15}
16
17Examples:
18> bitcoin-cli getmininginfo
19> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getmininginfo", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
20*/
21use crate::command::CallableCommand;
22use crate::Blockhash;
23use crate::{client::Client, command::request::request};
24use serde::{Deserialize, Serialize};
25use serde_json::value::{to_raw_value, RawValue};
26
27const GET_DIFFICULTY_COMMAND: &str = "getmininginfo";
28
29pub struct GetMiningInfoCommand {}
30#[derive(Serialize, Deserialize, Debug)]
31pub struct GetMiningInfoCommandResponse {
32    pub blocks: u64,                     // The current block
33    pub currentblockweight: Option<u64>, //  The block weight of the last assembled block (only present if a block was ever assembled)
34    pub currentblocktx: Option<u64>, //  The number of block transactions of the last assembled block (only present if a block was ever assembled)
35    pub difficulty: f64,             // The current difficulty
36    pub networkhashps: f64,          // The network hashes per second
37    pub pooledtx: u64,               // The size of the mempool
38    pub chain: String,               // current network name (main, test, regtest)
39    pub warnings: String,            // any network and blockchain warnings
40}
41impl GetMiningInfoCommand {
42    pub fn new() -> Self {
43        GetMiningInfoCommand {}
44    }
45}
46
47impl CallableCommand for GetMiningInfoCommand {
48    type Response = GetMiningInfoCommandResponse;
49    fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
50        let params = vec![];
51        let r = request(client, GET_DIFFICULTY_COMMAND, params);
52        let response: GetMiningInfoCommandResponse = r.result()?;
53        Ok(response)
54    }
55}