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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use bitcoin::network::constants::Network;
use types::*;

#[derive(Deserialize, Debug, PartialEq)]
pub struct SoftFork {
    id: String,
    version: u32,
    reject: Reject,
}

#[derive(Deserialize, Debug, PartialEq)]
pub struct Reject {
    status: bool,
}

#[derive(Deserialize, Debug, PartialEq)]
pub struct Bip9SoftFork {
    csv: Bip9SoftForkDetails,
    segwit: Bip9SoftForkDetails,
}

#[derive(Deserialize, Debug, PartialEq)]
pub struct Bip9SoftForkDetails {
    status: String,
    bit: Option<u32>,
    #[serde(rename = "startTime")]
    // In regtest, startTime is -1
    start_time: i64,
    timeout: u64,
    since: u64,
    // TODO: implement before new BIP9
    /*
    "statistics": {         (object) numeric statistics about BIP9 signalling for a softfork (only for "started" status)
    "period": xx,        (numeric) the length in blocks of the BIP9 signalling period
    "threshold": xx,     (numeric) the number of blocks with the version bit set required to activate the feature
    "elapsed": xx,       (numeric) the number of blocks elapsed since the beginning of the current period
    "count": xx,         (numeric) the number of blocks with the version bit set in the current period
    "possible": xx       (boolean) returns false if there are not enough blocks left in this period to pass activation threshold
    */
}

#[derive(Deserialize, Debug, PartialEq)]
pub struct BlockchainInfo {
    #[serde(with = "self::serde::network")]
    chain: Network,
    blocks: u64,
    headers: u64,
    bestblockhash: String,
    //TODO: Cannot trust serde - it is not able to deserialise “4.656542373906925e-10"
    difficulty: f64,
    mediantime: u64,
    verificationprogress: f64,
    initialblockdownload: bool,
    chainwork: String,
    size_on_disk: u64,
    pruned: bool,
    pruneheight: Option<u64>,
    automatic_pruning: Option<bool>,
    prune_target_size: Option<u64>,
    softforks: Vec<SoftFork>,
    bip9_softforks: Bip9SoftFork,
    warnings: String,
}
#[cfg(test)]
mod tests {
    use super::*;
    use serde_json;

    #[test]
    fn can_deserialize_blockchain_response() {
        let json = r#"{
        "chain": "regtest",
        "blocks": 0,
        "headers": 0,
        "bestblockhash": "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206",
        "difficulty": 4.65654237390692e-10,
        "mediantime": 1296688602,
        "verificationprogress": 1,
        "initialblockdownload": true,
        "chainwork": "0000000000000000000000000000000000000000000000000000000000000002",
        "size_on_disk": 293,
        "pruned": false,
        "softforks": [
            {
                "id": "bip34",
                "version": 2,
                "reject": {
                    "status": false
                }
            },
            {
                "id": "bip66",
                "version": 3,
                "reject": {
                    "status": false
                }
            },
            {
                "id": "bip65",
                "version": 4,
                "reject": {
                    "status": false
                }
            }
        ],
        "bip9_softforks": {
            "csv": {
                "status": "defined",
                "startTime": 0,
                "timeout": 9223372036854775807,
                "since": 0
            },
            "segwit": {
                "status": "active",
                "startTime": -1,
                "timeout": 9223372036854775807,
                "since": 0
            }
        },
        "warnings": ""
}"#;
        let blockchain: BlockchainInfo = serde_json::from_str(json).unwrap();

        assert_eq!(
            blockchain,
            BlockchainInfo {
                chain: Network::Regtest,
                blocks: 0,
                headers: 0,
                bestblockhash: String::from(
                    "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"
                ),
                difficulty: 4.65654237390692e-10,
                mediantime: 1296688602,
                verificationprogress: 1.0,
                initialblockdownload: true,
                chainwork: String::from(
                    "0000000000000000000000000000000000000000000000000000000000000002"
                ),
                size_on_disk: 293,
                pruned: false,
                pruneheight: None,
                automatic_pruning: None,
                prune_target_size: None,
                softforks: vec![
                    SoftFork {
                        id: String::from("bip34"),
                        version: 2,
                        reject: Reject { status: false },
                    },
                    SoftFork {
                        id: String::from("bip66"),
                        version: 3,
                        reject: Reject { status: false },
                    },
                    SoftFork {
                        id: String::from("bip65"),
                        version: 4,
                        reject: Reject { status: false },
                    },
                ],
                bip9_softforks: Bip9SoftFork {
                    csv: Bip9SoftForkDetails {
                        status: String::from("defined"),
                        bit: None,
                        start_time: 0,
                        timeout: 9223372036854775807,
                        since: 0,
                    },
                    segwit: Bip9SoftForkDetails {
                        status: String::from("active"),
                        bit: None,
                        start_time: -1,
                        timeout: 9223372036854775807,
                        since: 0,
                    },
                },
                warnings: String::new(),
            },
        )
    }
}