Skip to content

Commit

Permalink
JSON serialize Vec<u8> fields as hex
Browse files Browse the repository at this point in the history
serde_json has no simple way to serialize specific field types as hex,
until specialization is merged. This commit contains a hack to serialize
all Vec<u8> fields as hex anyway. It is impossible to check the type of
a generic type parameter at runtime. So all values are first serialized,
then attempted to deserialize into a Vec<u8>. If that works, the found
vector will be serialized as a hex string, rather than an array of
numbers. This is not a performant machanism, but it gets the job done.
As long as it is only used for dev commands and diagnostic data it
should be fine. At least all those number arrays in the diagnostic data
are now gone, without having to duplicate every struct definition just
for serialization.

Note that the 'Value' serialization is a copy of the real one.
  • Loading branch information
JssDWt committed Oct 11, 2024
1 parent 00b8d4d commit ce23e32
Show file tree
Hide file tree
Showing 7 changed files with 923 additions and 21 deletions.
5 changes: 3 additions & 2 deletions libs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libs/sdk-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ miniz_oxide = "0.7.1"
tokio-stream = "0.1.14"
serde_with = "3.3.0"
regex = { workspace = true }
ryu = "1.0.18"

[dev-dependencies]
mockito = { workspace = true }
Expand Down
22 changes: 12 additions & 10 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ impl BreezServices {
Ok(dev_cmd) => match dev_cmd {
DevCommand::GenerateDiagnosticData => self.generate_diagnostic_data().await,
},
Err(_) => Ok(serde_json::to_string_pretty(
Err(_) => Ok(crate::serializer::to_string_pretty(
&self.node_api.execute_command(command).await?,
)?),
}
Expand All @@ -1176,7 +1176,7 @@ impl BreezServices {
"node": node_data,
"sdk": sdk_data
});
Ok(serde_json::to_string_pretty(&result)?)
Ok(crate::serializer::to_string_pretty(&result)?)
}

/// This method sync the local state with the remote node state.
Expand Down Expand Up @@ -2171,18 +2171,20 @@ impl BreezServices {
}

async fn generate_sdk_diagnostic_data(&self) -> SdkResult<Value> {
let state = serde_json::to_value(&self.persister.get_node_state()?)?;
let payments = serde_json::to_value(
let state = crate::serializer::value::to_value(&self.persister.get_node_state()?)?;
let payments = crate::serializer::value::to_value(
&self
.persister
.list_payments(ListPaymentsRequest::default())?,
)?;
let channels = serde_json::to_value(&self.persister.list_channels()?)?;
let settings = serde_json::to_value(&self.persister.list_settings()?)?;
let reverse_swaps =
serde_json::to_value(self.persister.list_reverse_swaps().map(sanitize_vec)?)?;
let swaps = serde_json::to_value(self.persister.list_swaps().map(sanitize_vec)?)?;
let lsp_id = serde_json::to_value(&self.persister.get_lsp_id()?)?;
let channels = crate::serializer::value::to_value(&self.persister.list_channels()?)?;
let settings = crate::serializer::value::to_value(&self.persister.list_settings()?)?;
let reverse_swaps = crate::serializer::value::to_value(
self.persister.list_reverse_swaps().map(sanitize_vec)?,
)?;
let swaps =
crate::serializer::value::to_value(self.persister.list_swaps().map(sanitize_vec)?)?;
let lsp_id = crate::serializer::value::to_value(&self.persister.get_lsp_id()?)?;

let res = json!({
"node_state": state,
Expand Down
14 changes: 7 additions & 7 deletions libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ impl NodeAPI for Greenlight {
.await?
.into_inner();

Ok(serde_json::to_value(&resp)?)
Ok(crate::serializer::value::to_value(&resp)?)
}
NodeCommand::ListPeerChannels => {
let resp = self
Expand All @@ -1774,7 +1774,7 @@ impl NodeAPI for Greenlight {
.list_peer_channels(cln::ListpeerchannelsRequest::default())
.await?
.into_inner();
Ok(serde_json::to_value(&resp)?)
Ok(crate::serializer::value::to_value(&resp)?)
}
NodeCommand::ListFunds => {
let resp = self
Expand All @@ -1783,7 +1783,7 @@ impl NodeAPI for Greenlight {
.list_funds(cln::ListfundsRequest::default())
.await?
.into_inner();
Ok(serde_json::to_value(&resp)?)
Ok(crate::serializer::value::to_value(&resp)?)
}
NodeCommand::ListPayments => {
let resp = self
Expand All @@ -1792,7 +1792,7 @@ impl NodeAPI for Greenlight {
.list_pays(cln::ListpaysRequest::default())
.await?
.into_inner();
Ok(serde_json::to_value(&resp)?)
Ok(crate::serializer::value::to_value(&resp)?)
}
NodeCommand::ListInvoices => {
let resp = self
Expand All @@ -1801,7 +1801,7 @@ impl NodeAPI for Greenlight {
.list_invoices(cln::ListinvoicesRequest::default())
.await?
.into_inner();
Ok(serde_json::to_value(&resp)?)
Ok(crate::serializer::value::to_value(&resp)?)
}
NodeCommand::CloseAllChannels => {
let peers_res = self
Expand All @@ -1823,7 +1823,7 @@ impl NodeAPI for Greenlight {
.getinfo(cln::GetinfoRequest::default())
.await?
.into_inner();
Ok(serde_json::to_value(&resp)?)
Ok(crate::serializer::value::to_value(&resp)?)
}
NodeCommand::Stop => {
let resp = self
Expand All @@ -1832,7 +1832,7 @@ impl NodeAPI for Greenlight {
.stop(cln::StopRequest::default())
.await?
.into_inner();
Ok(serde_json::to_value(&resp)?)
Ok(crate::serializer::value::to_value(&resp)?)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions libs/sdk-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ mod lsps0;
mod lsps2;
mod models;
mod persist;
mod serializer;
mod support;
mod swap_in;
mod swap_out;
Expand Down
Loading

0 comments on commit ce23e32

Please sign in to comment.