From 882a9c27cc748b5961945a8b84474c26f3653c52 Mon Sep 17 00:00:00 2001 From: LLFourn Date: Thu, 25 Mar 2021 16:28:38 +1100 Subject: [PATCH] Use tagged serialization for blockchain config also make the config types Clone and PartialEq --- CHANGELOG.md | 1 + src/blockchain/any.rs | 29 ++++++++++++++++++++++++++- src/blockchain/compact_filters/mod.rs | 4 ++-- src/blockchain/electrum.rs | 2 +- src/blockchain/esplora.rs | 2 +- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee0ceef1..f12f2e03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Misc #### Changed - New minimum supported rust version is 1.46.0 +- Changed `AnyBlockchainConfig` to use serde tagged representation. ### Descriptor #### Added diff --git a/src/blockchain/any.rs b/src/blockchain/any.rs index 23a92cbe..21913c92 100644 --- a/src/blockchain/any.rs +++ b/src/blockchain/any.rs @@ -177,7 +177,34 @@ impl_from!(compact_filters::CompactFiltersBlockchain, AnyBlockchain, CompactFilt /// This allows storing a single configuration that can be loaded into an [`AnyBlockchain`] /// instance. Wallets that plan to offer users the ability to switch blockchain backend at runtime /// will find this particularly useful. -#[derive(Debug, serde::Serialize, serde::Deserialize)] +/// +/// This type can be serialized from a JSON object like: +/// +/// ``` +/// # #[cfg(feature = "electrum")] +/// # { +/// use bdk::blockchain::{electrum::ElectrumBlockchainConfig, AnyBlockchainConfig}; +/// let config: AnyBlockchainConfig = serde_json::from_str( +/// r#"{ +/// "type" : "electrum", +/// "url" : "ssl://electrum.blockstream.info:50002", +/// "retry": 2 +/// }"#, +/// ) +/// .unwrap(); +/// assert_eq!( +/// config, +/// AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig { +/// url: "ssl://electrum.blockstream.info:50002".into(), +/// retry: 2, +/// socks5: None, +/// timeout: None +/// }) +/// ); +/// # } +/// ``` +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq)] +#[serde(tag = "type", rename_all = "snake_case")] pub enum AnyBlockchainConfig { #[cfg(feature = "electrum")] #[cfg_attr(docsrs, doc(cfg(feature = "electrum")))] diff --git a/src/blockchain/compact_filters/mod.rs b/src/blockchain/compact_filters/mod.rs index 65222eaf..59a4d39c 100644 --- a/src/blockchain/compact_filters/mod.rs +++ b/src/blockchain/compact_filters/mod.rs @@ -456,7 +456,7 @@ impl Blockchain for CompactFiltersBlockchain { } /// Data to connect to a Bitcoin P2P peer -#[derive(Debug, serde::Deserialize, serde::Serialize)] +#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq)] pub struct BitcoinPeerConfig { /// Peer address such as 127.0.0.1:18333 pub address: String, @@ -467,7 +467,7 @@ pub struct BitcoinPeerConfig { } /// Configuration for a [`CompactFiltersBlockchain`] -#[derive(Debug, serde::Deserialize, serde::Serialize)] +#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq)] pub struct CompactFiltersBlockchainConfig { /// List of peers to try to connect to for asking headers and filters pub peers: Vec, diff --git a/src/blockchain/electrum.rs b/src/blockchain/electrum.rs index 80538cb8..341b5558 100644 --- a/src/blockchain/electrum.rs +++ b/src/blockchain/electrum.rs @@ -144,7 +144,7 @@ impl ElectrumLikeSync for Client { } /// Configuration for an [`ElectrumBlockchain`] -#[derive(Debug, serde::Deserialize, serde::Serialize)] +#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq)] pub struct ElectrumBlockchainConfig { /// URL of the Electrum server (such as ElectrumX, Esplora, BWT) may start with `ssl://` or `tcp://` and include a port /// diff --git a/src/blockchain/esplora.rs b/src/blockchain/esplora.rs index 1ea7cf84..4052a65c 100644 --- a/src/blockchain/esplora.rs +++ b/src/blockchain/esplora.rs @@ -361,7 +361,7 @@ struct EsploraGetHistory { } /// Configuration for an [`EsploraBlockchain`] -#[derive(Debug, serde::Deserialize, serde::Serialize)] +#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq)] pub struct EsploraBlockchainConfig { /// Base URL of the esplora service ///