Skip to content

Commit

Permalink
using bincode 2 with non-std support
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Jun 28, 2024
1 parent 43e0802 commit d1d7d22
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
33 changes: 29 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ smallvec = "1.11.0"
serde = { version = "1.0.195", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.111" }
serde-big-array = { version = "0.5" }
bincode = { version = "1.3.3" }
bincode = { version = "2.0.0-rc.3", features-features = false, features = ["alloc", "serde"] }
parity-scale-codec = { version = "3.6.1", default-features = false, features = ["derive"] }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
log = { version = "0.4.20", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions pallets/liquidity-pools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ std = [
"staging-xcm/std",
"scale-info/std",
"serde/std",
"bincode/std",
]
runtime-benchmarks = [
"orml-tokens/runtime-benchmarks",
Expand Down
38 changes: 22 additions & 16 deletions pallets/liquidity-pools/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use bincode::Options;
//! A message requires a custom decoding & encoding, meeting the
//! LiquidityPool Generic Message Passing Format (GMPF): Every message is
//! encoded with a u8 at head flagging the message type, followed by its field.
//! Integers are big-endian encoded and enum values (such as `[crate::Domain]`)
//! also have a custom GMPF implementation, aiming for a fixed-size encoded
//! representation for each message variant.
use cfg_traits::Seconds;
use cfg_types::domain_address::Domain;
use frame_support::pallet_prelude::RuntimeDebug;
Expand Down Expand Up @@ -58,28 +64,28 @@ impl TryInto<Domain> for SerializableDomain {
}
}

/// A message requires a custom decoding & encoding, meeting the
/// LiquidityPool Generic Message Passing Format (GMPF): Every message is
/// encoded with a u8 at head flagging the message type, followed by its field.
/// Integers are big-endian encoded and enum values (such as `[crate::Domain]`)
/// also have a custom GMPF implementation, aiming for a fixed-size encoded
/// representation for each message variant.
///
fn bincode_config() -> bincode::config::Configuration<
bincode::config::BigEndian,
bincode::config::Fixint,
bincode::config::NoLimit,
> {
Default::default()
}

/// Inverse of [`solidity_deserialization()`]
/// Note that the enum tags as treat as u32 by bincode instead of the expected
/// u8. Only use this function if you can handle that.
fn solidity_serialization<T: Serialize>(ty: &T) -> Result<Vec<u8>, DispatchError> {
bincode::DefaultOptions::new()
.with_fixint_encoding()
.with_big_endian()
.serialize(ty)
bincode::serde::encode_to_vec(ty, bincode_config())
.map_err(|_| DispatchError::Other("Type can not be serialized"))
}

/// Inverse of [`solidity_serialization()`]
/// Note that the enum tags as treat as u32 by bincode instead of the expected
/// u8. Only use this function if you can handle that.
fn solidity_deserialization<T: DeserializeOwned>(data: &[u8]) -> Result<T, DispatchError> {
bincode::DefaultOptions::new()
.with_fixint_encoding()
.with_big_endian()
.deserialize(data)
bincode::serde::decode_from_slice(data, bincode_config())
.map(|(ty, _)| ty)
.map_err(|_| DispatchError::Other("Type can not be deserialized"))
}

Expand Down

0 comments on commit d1d7d22

Please sign in to comment.