Skip to content

Commit

Permalink
Merge pull request fedimint#6296 from elsirion/2024-11-fix-meta
Browse files Browse the repository at this point in the history
fix: allow lenient parsing of meta values if their encoding is broken
  • Loading branch information
elsirion authored Nov 11, 2024
2 parents 2c7d93c + dfef398 commit 8826dc0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions modules/fedimint-meta-client/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ pub(crate) async fn handle_cli_command(
let value = if hex {
serde_json::to_value(value).expect("can't fail")
} else {
serde_json::from_slice(value.as_slice())
.context("deserializating consensus value as json")?
value
.to_json_lossy()
.context("deserializing consensus value as json")?
};
json!({
"revision": revision,
Expand Down
1 change: 1 addition & 0 deletions modules/fedimint-meta-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ hex = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
13 changes: 12 additions & 1 deletion modules/fedimint-meta-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use fedimint_core::plugin_types_trait_impl_common;
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use thiserror::Error;

use tracing::warn;
// Common contains types shared by both the client and server

// The client and server configuration
Expand Down Expand Up @@ -106,6 +106,17 @@ impl MetaValue {
pub fn to_json(&self) -> anyhow::Result<serde_json::Value> {
Ok(serde_json::from_slice(&self.0)?)
}

/// Converts the value to a JSON value, ignoring invalid utf-8.
pub fn to_json_lossy(&self) -> anyhow::Result<serde_json::Value> {
let maybe_lossy_str = String::from_utf8_lossy(self.as_slice());

if maybe_lossy_str.as_bytes() != self.as_slice() {
warn!("Value contains invalid utf-8, converting to lossy string");
}

Ok(serde_json::from_str(&maybe_lossy_str)?)
}
}

impl Decodable for MetaValue {
Expand Down

0 comments on commit 8826dc0

Please sign in to comment.