Skip to content

Commit

Permalink
Revert "Go on a dependency diet"
Browse files Browse the repository at this point in the history
This reverts commit 6a42fea.
  • Loading branch information
sug0 committed Nov 19, 2024
1 parent 4af21cf commit b6cbd09
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 91 deletions.
9 changes: 1 addition & 8 deletions Cargo.lock

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

31 changes: 3 additions & 28 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,9 @@ readme = "README.md"
repository = "https://github.com/heliaxdev/ibc-middleware"
version = "0.1.0"

[workspace.dependencies.zjson]
version = "0.1.5"
default-features = false

[workspace.dependencies.dur]
version = "0.5.3"
default-features = false

[workspace.dependencies.serde_json]
package = "serde-json-wasm"
version = "1.0"
default-features = false
[workspace.dependencies]
dur = "0.5.3"
serde_json = "1.0"

[workspace.dependencies.either]
version = "1.3"
Expand All @@ -38,47 +29,31 @@ features = ["alloc", "derive"]
[workspace.dependencies.ibc-core-router]
git = "https://github.com/heliaxdev/cosmos-ibc-rs"
rev = "42826d74f47959a4e9a000f90ff6ae569415ea48"
default-features = false
features = ["serde"]

[workspace.dependencies.ibc-core-router-types]
git = "https://github.com/heliaxdev/cosmos-ibc-rs"
rev = "42826d74f47959a4e9a000f90ff6ae569415ea48"
default-features = false
features = ["serde"]

[workspace.dependencies.ibc-core-host-types]
git = "https://github.com/heliaxdev/cosmos-ibc-rs"
rev = "42826d74f47959a4e9a000f90ff6ae569415ea48"
default-features = false
features = ["serde"]

[workspace.dependencies.ibc-core-channel-types]
git = "https://github.com/heliaxdev/cosmos-ibc-rs"
rev = "42826d74f47959a4e9a000f90ff6ae569415ea48"
default-features = false
features = ["serde"]

[workspace.dependencies.ibc-core-channel]
git = "https://github.com/heliaxdev/cosmos-ibc-rs"
rev = "42826d74f47959a4e9a000f90ff6ae569415ea48"
default-features = false
features = ["serde"]

[workspace.dependencies.ibc-primitives]
git = "https://github.com/heliaxdev/cosmos-ibc-rs"
rev = "42826d74f47959a4e9a000f90ff6ae569415ea48"
default-features = false
features = ["serde"]

[workspace.dependencies.ibc-app-transfer-types]
git = "https://github.com/heliaxdev/cosmos-ibc-rs"
rev = "42826d74f47959a4e9a000f90ff6ae569415ea48"
default-features = false
features = ["serde"]

[workspace.dependencies.ibc-testkit]
git = "https://github.com/heliaxdev/cosmos-ibc-rs"
rev = "42826d74f47959a4e9a000f90ff6ae569415ea48"
default-features = false
features = ["serde"]
1 change: 0 additions & 1 deletion crates/packet-forward/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ ibc-core-router.workspace = true
ibc-primitives.workspace = true
serde.workspace = true
serde_json.workspace = true
zjson.workspace = true

[dev-dependencies]
ibc-testkit.workspace = true
63 changes: 25 additions & 38 deletions crates/packet-forward/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,18 @@ where
transfer_pkt: PacketData,
inflight_packet: InFlightPacket,
) -> Result<(), MiddlewareError> {
let next = if !transfer_pkt.memo.as_ref().is_empty() {
Some(transfer_pkt.memo.to_string())
} else {
None
let next = {
let memo = transfer_pkt.memo.as_ref();

if !memo.is_empty() {
let json_obj_memo: serde_json::Map<String, serde_json::Value> =
serde_json::from_str(memo).map_err(|err| {
MiddlewareError::Message(format!("Failed to decode next memo: {err}"))
})?;
Some(json_obj_memo)
} else {
None
}
};
let fwd_metadata = msg::ForwardMetadata {
receiver: transfer_pkt.receiver,
Expand Down Expand Up @@ -864,46 +872,25 @@ fn decode_ics20_msg(packet: &Packet) -> Result<PacketData, MiddlewareError> {
fn decode_forward_msg(
packet: &Packet,
) -> Result<(PacketData, msg::ForwardMetadata), MiddlewareError> {
use zjson::any::Any;
use zjson::document::Document;

let transfer_pkt = decode_ics20_msg(packet)?;

let mut document = Document::new(transfer_pkt.memo.as_ref());

let Some(Any::Object(mut json_obj_memo)) = document.next().map_err(|_| {
// NB: if the ICS-20 packet memo is not valid JSON, we forward
// this call to the next middleware
MiddlewareError::ForwardToNextMiddleware
})?
else {
// NB: if the ICS-20 packet memo is not a valid JSON object, we forward
// this call to the next middleware
return Err(MiddlewareError::ForwardToNextMiddleware);
};

while let Some((key, mut value)) = json_obj_memo.next().map_err(|_| {
// NB: the data is not a valid json object. kind of weird,
// since it looked like one, but oh well. alas, we forward
// the call to the next middleware
MiddlewareError::ForwardToNextMiddleware
})? {
if key == "forward" {
return serde_json::from_str(transfer_pkt.memo.as_ref()).map_or_else(
|err| Err(MiddlewareError::Message(err.to_string())),
|msg::PacketMetadata { forward }| Ok((transfer_pkt, forward)),
);
}

value.finish().map_err(|_| {
// NB: again, not json data? forward the call
let json_obj_memo: serde_json::Map<String, serde_json::Value> =
serde_json::from_str(transfer_pkt.memo.as_ref()).map_err(|_| {
// NB: if the ICS-20 packet memo is not a valid JSON object, we forward
// this call to the next middleware
MiddlewareError::ForwardToNextMiddleware
})?;

if !json_obj_memo.contains_key("forward") {
// NB: the memo was a valid json object, but it wasn't up to
// the PFM to consume it, so we forward the call to the next middleware
return Err(MiddlewareError::ForwardToNextMiddleware);
}

// NB: we could not find the `forward` key. give up and
// forward the call to the next middleware
Err(MiddlewareError::ForwardToNextMiddleware)
serde_json::from_value(json_obj_memo.into()).map_or_else(
|err| Err(MiddlewareError::Message(err.to_string())),
|msg::PacketMetadata { forward }| Ok((transfer_pkt, forward)),
)
}

fn join_module_extras(first: &mut ModuleExtras, mut second: ModuleExtras) {
Expand Down
53 changes: 37 additions & 16 deletions crates/packet-forward/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct ForwardMetadata {
/// another [`ForwardMetadata`] structure, along with
/// any additional middleware callbacks.
#[serde(skip_serializing_if = "Option::is_none")]
pub next: Option<String>,
pub next: Option<serde_json::Map<String, serde_json::Value>>,
}

fn deserialize_non_empty_signer<'de, D>(deserializer: D) -> Result<Signer, D::Error>
Expand Down Expand Up @@ -88,28 +88,28 @@ mod duration {
dur::Duration,
);

impl From<u64> for U64Dur {
fn from(dur: u64) -> Self {
impl From<f64> for F64Dur {
fn from(dur: f64) -> Self {
Self(DurrDerp(dur::Duration::from_nanos(dur as u128)))
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(from = "u64")]
struct U64Dur(DurrDerp);
#[serde(from = "f64")]
struct F64Dur(DurrDerp);

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum AllDuration {
Dur(DurrDerp),
U64(U64Dur),
F64(F64Dur),
}

impl From<AllDuration> for Duration {
fn from(dur: AllDuration) -> Self {
match dur {
AllDuration::Dur(DurrDerp(dur)) => Self(dur),
AllDuration::U64(U64Dur(DurrDerp(dur))) => Self(dur),
AllDuration::F64(F64Dur(DurrDerp(dur))) => Self(dur),
}
}
}
Expand All @@ -136,16 +136,16 @@ mod tests {
#[test]
fn duration_serde_roundtrip_parsing() {
const DUR_STR: &str = "\"1m5s\"";
const DUR_U64: &str = "1";
const DUR_F64: &str = "1.2345";

let expected_from_str = Duration(dur::Duration::from_secs(65));
let expected_from_u64 = Duration(dur::Duration::from_nanos(1));
let expected_from_f64 = Duration(dur::Duration::from_nanos(1));

let parsed: Duration = serde_json::from_str(DUR_STR).unwrap();
assert_eq!(parsed, expected_from_str);

let parsed: Duration = serde_json::from_str(DUR_U64).unwrap();
assert_eq!(parsed, expected_from_u64);
let parsed: Duration = serde_json::from_str(DUR_F64).unwrap();
assert_eq!(parsed, expected_from_f64);
}

#[test]
Expand Down Expand Up @@ -217,7 +217,13 @@ mod tests {
"channel": "channel-1180",
"port": "transfer",
"receiver": "tnam1qrx3tphxjr9qaznadzykxzt4x76c0cm8ts3pwukt",
"next": "{\"forward\":{\"receiver\":\"noble18st0wqx84av8y6xdlss9d6m2nepyqwj6nfxxuv\",\"channel\":\"channel-1181\",\"port\":\"transfer\"}}"
"next": {
"forward": {
"receiver": "noble18st0wqx84av8y6xdlss9d6m2nepyqwj6nfxxuv",
"channel": "channel-1181",
"port": "transfer"
}
}
}
}
"#,
Expand All @@ -230,10 +236,25 @@ mod tests {
channel: ChannelId::new(1180),
timeout: None,
retries: None,
next: Some(
"{\"forward\":{\"receiver\":\"noble18st0wqx84av8y6xdlss9d6m2nepyqwj6nfxxuv\",\
\"channel\":\"channel-1181\",\"port\":\"transfer\"}}".to_string(),
),
next: Some(serde_json::Map::from_iter([(
"forward".to_owned(),
serde_json::Value::Object(serde_json::Map::from_iter([
(
"receiver".to_owned(),
serde_json::Value::String(
"noble18st0wqx84av8y6xdlss9d6m2nepyqwj6nfxxuv".to_owned(),
),
),
(
"channel".to_owned(),
serde_json::Value::String("channel-1181".to_owned()),
),
(
"port".to_owned(),
serde_json::Value::String("transfer".to_owned()),
),
])),
)])),
},
}),
},
Expand Down

0 comments on commit b6cbd09

Please sign in to comment.