Skip to content

Commit 729cf74

Browse files
committed
Sets chain to bitcoin::network::constants::Network
Currently, when reading a chain field from an rpc response from bitcoind the field is parsed as String. If this is happen to be compared with a `bitcoin::network::constants::Network` a custom parser would need to be created, given bitcoind uses `main` and `test` for mainnet and testnet while `rust-bitcoin` uses `bitcoin` and `testnet`. Creates a custom deserializer based on Network::from_core_arg so both can be compared seamlessly
1 parent e265d32 commit 729cf74

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

integration_test/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ fn test_get_mining_info(cl: &Client) {
234234

235235
fn test_get_blockchain_info(cl: &Client) {
236236
let info = cl.get_blockchain_info().unwrap();
237-
assert_eq!(&info.chain, "regtest");
237+
assert_eq!(info.chain, Network::Regtest);
238238
}
239239

240240
fn test_get_new_address(cl: &Client) {

json/src/lib.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ extern crate serde_json;
2424

2525
use std::collections::HashMap;
2626

27+
2728
use bitcoin::address::NetworkUnchecked;
2829
use bitcoin::block::Version;
2930
use bitcoin::consensus::encode;
3031
use bitcoin::hashes::hex::FromHex;
3132
use bitcoin::hashes::sha256;
32-
use bitcoin::{Address, Amount, PrivateKey, PublicKey, SignedAmount, Transaction, ScriptBuf, Script, bip158, bip32};
33+
use bitcoin::{Address, Amount, PrivateKey, PublicKey, SignedAmount, Transaction, ScriptBuf, Script, bip158, bip32, Network};
3334
use serde::de::Error as SerdeError;
3435
use serde::{Deserialize, Serialize};
3536
use std::fmt;
@@ -515,7 +516,8 @@ pub struct GetMiningInfoResult {
515516
pub network_hash_ps: f64,
516517
#[serde(rename = "pooledtx")]
517518
pub pooled_tx: usize,
518-
pub chain: String,
519+
#[serde(deserialize_with = "deserialize_bip70_network")]
520+
pub chain: Network,
519521
pub warnings: String,
520522
}
521523

@@ -1006,8 +1008,9 @@ pub struct GetAddressInfoResult {
10061008
/// Models the result of "getblockchaininfo"
10071009
#[derive(Clone, Debug, Deserialize, Serialize)]
10081010
pub struct GetBlockchainInfoResult {
1009-
/// Current network name as defined in BIP70 (main, test, regtest)
1010-
pub chain: String,
1011+
/// Current network name as defined in BIP70 (main, test, signet, regtest)
1012+
#[serde(deserialize_with = "deserialize_bip70_network")]
1013+
pub chain: Network,
10111014
/// The current number of blocks processed in the server
10121015
pub blocks: u64,
10131016
/// The current number of headers we have validated
@@ -2169,6 +2172,29 @@ where
21692172
Ok(Some(res))
21702173
}
21712174

2175+
/// deserialize_bip70_network deserializes a Bitcoin Core network according to BIP70
2176+
/// The accepted input variants are: {"main", "test", "signet", "regtest"}
2177+
fn deserialize_bip70_network<'de, D>(deserializer: D) -> Result<Network, D::Error>
2178+
where
2179+
D: serde::Deserializer<'de>,
2180+
{
2181+
struct NetworkVisitor;
2182+
impl<'de> serde::de::Visitor<'de> for NetworkVisitor {
2183+
type Value = Network;
2184+
2185+
fn visit_str<E: serde::de::Error>(self, s: &str) -> Result<Self::Value, E> {
2186+
Network::from_core_arg(s)
2187+
.map_err(|_| E::invalid_value(serde::de::Unexpected::Str(s), &"bitcoin network encoded as a string"))
2188+
}
2189+
2190+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
2191+
write!(formatter, "bitcoin network encoded as a string")
2192+
}
2193+
}
2194+
2195+
deserializer.deserialize_str(NetworkVisitor)
2196+
}
2197+
21722198
#[cfg(test)]
21732199
mod tests {
21742200
use super::*;

0 commit comments

Comments
 (0)