1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
getmininginfo

Returns a json object containing mining-related information.
Result:
{                              (json object)
  "blocks" : n,                (numeric) The current block
  "currentblockweight" : n,    (numeric, optional) The block weight of the last assembled block (only present if a block was ever assembled)
  "currentblocktx" : n,        (numeric, optional) The number of block transactions of the last assembled block (only present if a block was ever assembled)
  "difficulty" : n,            (numeric) The current difficulty
  "networkhashps" : n,         (numeric) The network hashes per second
  "pooledtx" : n,              (numeric) The size of the mempool
  "chain" : "str",             (string) current network name (main, test, regtest)
  "warnings" : "str"           (string) any network and blockchain warnings
}

Examples:
> bitcoin-cli getmininginfo
> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getmininginfo", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
*/
use crate::command::CallableCommand;
use crate::Blockhash;
use crate::{client::Client, command::request::request};
use serde::{Deserialize, Serialize};
use serde_json::value::{to_raw_value, RawValue};

const GET_DIFFICULTY_COMMAND: &str = "getmininginfo";

pub struct GetMiningInfoCommand {}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetMiningInfoCommandResponse {
    pub blocks: u64,                     // The current block
    pub currentblockweight: Option<u64>, //  The block weight of the last assembled block (only present if a block was ever assembled)
    pub currentblocktx: Option<u64>, //  The number of block transactions of the last assembled block (only present if a block was ever assembled)
    pub difficulty: f64,             // The current difficulty
    pub networkhashps: f64,          // The network hashes per second
    pub pooledtx: u64,               // The size of the mempool
    pub chain: String,               // current network name (main, test, regtest)
    pub warnings: String,            // any network and blockchain warnings
}
impl GetMiningInfoCommand {
    pub fn new() -> Self {
        GetMiningInfoCommand {}
    }
}

impl CallableCommand for GetMiningInfoCommand {
    type Response = GetMiningInfoCommandResponse;
    fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
        let params = vec![];
        let r = request(client, GET_DIFFICULTY_COMMAND, params);
        let response: GetMiningInfoCommandResponse = r.result()?;
        Ok(response)
    }
}