Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.

Commit a53ed44

Browse files
bors[bot]Tobin C. Harding
andauthored
Merge #1756
1756: Remove Unknown variants r=tcharding a=tcharding Currently we have `Unknown` variants in `AssetKind`, `LedgerKind`, and `SwapProtocol`. These are unnecessary if we handle the errors during de/serialization. Resolves: #1330 Co-authored-by: Tobin C. Harding <tobin.harding@coblox.tech>
2 parents 637d8c6 + 132683c commit a53ed44

File tree

7 files changed

+18
-50
lines changed

7 files changed

+18
-50
lines changed

cnd/src/comit_api/mod.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ use crate::{
1111
use bitcoin::util::amount::Denomination;
1212
use libp2p_comit::frame::Header;
1313
use serde::de::Error;
14-
use std::fmt;
15-
16-
fn fail_serialize_unknown<D: fmt::Debug>(unknown: D) -> serde_json::Error {
17-
::serde::de::Error::custom(format!("serialization of {:?} is undefined.", unknown))
18-
}
1914

2015
impl FromHeader for LedgerKind {
2116
fn from_header(mut header: Header) -> Result<Self, serde_json::Error> {
@@ -33,7 +28,12 @@ impl FromHeader for LedgerKind {
3328
},
3429
)),
3530
"ethereum" => LedgerKind::Ethereum(Ethereum::new(header.take_parameter("network")?)),
36-
other => LedgerKind::Unknown(other.to_string()),
31+
unknown => {
32+
return Err(serde_json::Error::custom(format!(
33+
"unknown ledger: {}",
34+
unknown
35+
)))
36+
}
3737
})
3838
}
3939
}
@@ -52,7 +52,6 @@ impl ToHeader for LedgerKind {
5252
LedgerKind::Ethereum(ethereum) => {
5353
Header::with_str_value("ethereum").with_parameter("network", ethereum.chain_id)?
5454
}
55-
unknown @ LedgerKind::Unknown(_) => return Err(fail_serialize_unknown(unknown)),
5655
})
5756
}
5857
}
@@ -73,7 +72,12 @@ impl FromHeader for SwapProtocol {
7372
fn from_header(mut header: Header) -> Result<Self, serde_json::Error> {
7473
Ok(match header.value::<String>()?.as_str() {
7574
"comit-rfc-003" => SwapProtocol::Rfc003(header.take_parameter("hash_function")?),
76-
other => SwapProtocol::Unknown(other.to_string()),
75+
unknown => {
76+
return Err(serde_json::Error::custom(format!(
77+
"unknown swap protocol: {}",
78+
unknown
79+
)))
80+
}
7781
})
7882
}
7983
}
@@ -83,7 +87,6 @@ impl ToHeader for SwapProtocol {
8387
Ok(match self {
8488
SwapProtocol::Rfc003(hash_function) => Header::with_str_value("comit-rfc-003")
8589
.with_parameter("hash_function", hash_function)?,
86-
unknown @ SwapProtocol::Unknown(_) => return Err(fail_serialize_unknown(unknown)),
8790
})
8891
}
8992
}
@@ -103,7 +106,12 @@ impl FromHeader for AssetKind {
103106
header.take_parameter("address")?,
104107
header.take_parameter("quantity")?,
105108
)),
106-
other => AssetKind::Unknown(other.to_string()),
109+
unknown => {
110+
return Err(serde_json::Error::custom(format!(
111+
"unknown asset: {}",
112+
unknown
113+
)))
114+
}
107115
})
108116
}
109117
}
@@ -119,7 +127,6 @@ impl ToHeader for AssetKind {
119127
AssetKind::Erc20(erc20) => Header::with_str_value("erc20")
120128
.with_parameter("address", erc20.token_contract)?
121129
.with_parameter("quantity", erc20.quantity)?,
122-
unknown @ AssetKind::Unknown(_) => return Err(fail_serialize_unknown(unknown)),
123130
})
124131
}
125132
}
@@ -151,7 +158,6 @@ mod tests {
151158
swap_protocols::{ledger::ethereum, HashFunction},
152159
};
153160
use bitcoin::Amount;
154-
use spectral::prelude::*;
155161

156162
#[test]
157163
fn erc20_quantity_to_header() -> Result<(), serde_json::Error> {
@@ -171,15 +177,6 @@ mod tests {
171177
Ok(())
172178
}
173179

174-
#[test]
175-
fn serializing_unknown_ledgerkind_doesnt_panic() {
176-
let ledger_kind = LedgerKind::Unknown("USD".to_string());
177-
178-
let header = ledger_kind.to_header();
179-
180-
assert_that(&header).is_err();
181-
}
182-
183180
#[test]
184181
fn swap_protocol_to_header() {
185182
// From comit-network/RFCs/RFC-003-SWAP-Basic.md SWAP REQUEST example.

cnd/src/db/swap_types.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ impl From<ledger::LedgerKind> for LedgerKind {
130130
match ledger {
131131
ledger::LedgerKind::Bitcoin(_) => LedgerKind::Bitcoin,
132132
ledger::LedgerKind::Ethereum(_) => LedgerKind::Ethereum,
133-
// In order to remove this ledger::LedgerKind::Unknown should be removed.
134-
// Doing so requires handling unknown ledger during deserialization.
135-
_ => unreachable!(),
136133
}
137134
}
138135
}
@@ -150,9 +147,6 @@ impl From<asset::AssetKind> for AssetKind {
150147
asset::AssetKind::Bitcoin(_) => AssetKind::Bitcoin,
151148
asset::AssetKind::Ether(_) => AssetKind::Ether,
152149
asset::AssetKind::Erc20(_) => AssetKind::Erc20,
153-
// In order to remove this ledger::AssetKind::Unknown should be removed.
154-
// Doing so requires handling unknown asset during deserialization.
155-
_ => unreachable!(),
156150
}
157151
}
158152
}

cnd/src/http_api/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ impl Serialize for Http<SwapProtocol> {
108108
match &self.0 {
109109
// Currently we do not expose the hash_function protocol parameter via REST.
110110
SwapProtocol::Rfc003(_hash_function) => serializer.serialize_str("rfc003"),
111-
SwapProtocol::Unknown(name) => serializer.serialize_str(name.as_str()),
112111
}
113112
}
114113
}

cnd/src/network/mod.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -292,25 +292,6 @@ async fn handle_request(
292292
}
293293
}
294294
}
295-
SwapProtocol::Unknown(protocol) => {
296-
log::warn!("the swap protocol {} is currently not supported", protocol);
297-
298-
let decline_body = DeclineResponseBody {
299-
reason: Some(SwapDeclineReason::UnsupportedProtocol),
300-
};
301-
Err(Response::empty()
302-
.with_header(
303-
"decision",
304-
Decision::Declined
305-
.to_header()
306-
.expect("Decision should not fail to serialize"),
307-
)
308-
.with_body(
309-
serde_json::to_value(decline_body).expect(
310-
"decline body should always serialize into serde_json::Value",
311-
),
312-
))
313-
}
314295
}
315296
}
316297

cnd/src/swap_protocols/asset.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub enum AssetKind {
3434
Bitcoin(Amount),
3535
Ether(EtherQuantity),
3636
Erc20(Erc20Token),
37-
Unknown(String),
3837
}
3938

4039
impl From<Amount> for AssetKind {

cnd/src/swap_protocols/ledger/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,4 @@ pub trait Ledger:
3636
pub enum LedgerKind {
3737
Bitcoin(Bitcoin),
3838
Ethereum(Ethereum),
39-
Unknown(String),
4039
}

cnd/src/swap_protocols/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ pub enum HashFunction {
3636
#[derive(Debug)]
3737
pub enum SwapProtocol {
3838
Rfc003(HashFunction),
39-
Unknown(String),
4039
}
4140

4241
#[derive(Clone, Copy, Debug, Display, EnumString, PartialEq)]

0 commit comments

Comments
 (0)