Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serialization and deserialization features for codec and borsh to the host type in ics24 #259

Closed
5 tasks
DaviRain-Su opened this issue Nov 25, 2022 · 7 comments · Fixed by #273
Closed
5 tasks

Comments

@DaviRain-Su
Copy link
Contributor

Summary

## this for codec 
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.1.2", default-features = false, features = ["derive"] }
sp-runtime = {  version = "7.0.0", default-features = false }
## this for borsh
borsh = {version = "0.9.3", default-features = false }

example case ClientId:

use serde::{Deserialize, Serialize};

use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::RuntimeDebug;
use borsh::{BorshSerialize, BorshDeserialize};


#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ClientId(pub String);

#[cfg(feature = "codec")]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct ClientId(pub String);

#[cfg(feature = "borsh")]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct ClientId(pub String);

Problem Definition

Proposal

Acceptance Criteria


For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate milestone (priority) applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned
@DaviRain-Su
Copy link
Contributor Author

I encountered some problems when implementing the Encode and Decode property macros for the timestamp codec, I found that the timestamp wrapper uses the time in the tendermint and this time the crate, can't add the property macros very well, do you have any good suggestions for implementation?

@plafer
Copy link
Contributor

plafer commented Nov 30, 2022

I found that the timestamp wrapper uses the time in the tendermint and this time the crate, can't add the property macros very well

I don't really understand the issue you're describing here. Could you provide some relevant links (e.g. link to the macro you're referring to), and perhaps rephrase as well?

@kevinji
Copy link
Contributor

kevinji commented Nov 30, 2022

This might not be the best place for this discussion, but I had previously tried serializing via borsh (by forking ibc-rs to add #[derive]s) but found that borsh doesn't have an easy way to override specific fields of a struct with a macro like serde so it ended up being somewhat of a dead end as some underlying types (I think the timestamps in Tendermint) don't derive borsh. This could definitely be solved if someone put some more work into supporting borsh but at this time it isn't widely derived, so using borsh often requires things like deriving on wrapper structs.

Around this time, I realized that most of IBC already had protobuf serializations so it was a lot easier to commit to protobuf representation for serialization/deserialization. Maybe some guidance around using impl Protobuf, defining protobuf files, etc. might be better to nudge people towards that direction?

@DaviRain-Su
Copy link
Contributor Author

I found that the timestamp wrapper uses the time in the tendermint and this time the crate, can't add the property macros very well

I don't really understand the issue you're describing here. Could you provide some relevant links (e.g. link to the macro you're referring to), and perhaps rephrase as well?

Error:    --> crates/ibc/src/core/ics04_channel/packet.rs:144:5
    |
144 |     codec::Encode,
    |     ^^^^^^^^^^^^^ the trait `WrapperTypeEncode` is not implemented for `timestamp::Timestamp`
    |
    = help: the following other types implement trait `WrapperTypeEncode`:
              &T
              &mut T
              Arc<T>
              Cow<'a, T>
              Rc<T>
              alloc::boxed::Box<T>
              alloc::string::String
              alloc::vec::Vec<T>
            and 3 others
    = note: required for `timestamp::Timestamp` to implement `Encode`
    = note: this error originates in the derive macro `codec::Encode` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `timestamp::Timestamp: WrapperTypeDecode` is not satisfied
Error:    --> crates/ibc/src/core/ics04_channel/packet.rs:157:5
    |
157 |     pub timeout_timestamp: Timestamp,
    |     ^^^ the trait `WrapperTypeDecode` is not implemented for `timestamp::Timestamp`
    |
    = help: the following other types implement trait `WrapperTypeDecode`:
              Arc<T>
              Rc<T>
              alloc::boxed::Box<T>
              sp_runtime::sp_application_crypto::sp_core::Bytes
    = note: required for `timestamp::Timestamp` to implement `Decode`

error[E0277]: the trait bound `timestamp::Timestamp: TypeInfo` is not satisfied
Error:    --> crates/ibc/src/core/ics04_channel/packet.rs:157:28
    |
157 |     pub timeout_timestamp: Timestamp,
    |                            ^^^^^^^^^ the trait `TypeInfo` is not implemented for `timestamp::Timestamp`
    |
    = help: the following other types implement trait `TypeInfo`:
              &T
              &mut T
              ()
              (A, B)
              (A, B, C)
              (A, B, C, D)
              (A, B, C, D, E)
              (A, B, C, D, E, F)
            and 139 others
note: required by a bound in `FieldBuilder::<MetaForm, N>::ty`
   --> /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/scale-info-2.3.0/src/build.rs:458:13
    |
458 |         TY: TypeInfo + 'static + ?Sized,
    |             ^^^^^^^^ required by this bound in `FieldBuilder::<MetaForm, N>::ty`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `ibc` due to 3 previous errors
Error: warning: build failed, waiting for other jobs to finish...
error: could not compile `ibc` due to 3 previous errors
Error: The process '/home/runner/.cargo/bin/cargo' failed with exit code 101

yet, This error message is a good illustration of what I mean

@DaviRain-Su
Copy link
Contributor Author

This might not be the best place for this discussion, but I had previously tried serializing via borsh (by forking ibc-rs to add #[derive]s) but found that borsh doesn't have an easy way to override specific fields of a struct with a macro like serde so it ended up being somewhat of a dead end as some underlying types (I think the timestamps in Tendermint) don't derive borsh. This could definitely be solved if someone put some more work into supporting borsh but at this time it isn't widely derived, so using borsh often requires things like deriving on wrapper structs.

Around this time, I realized that most of IBC already had protobuf serializations so it was a lot easier to commit to protobuf representation for serialization/deserialization. Maybe some guidance around using impl Protobuf, defining protobuf files, etc. might be better to nudge people towards that direction?

Hey man do you have any idea, not only borsh but also codec is like this, can't serialize and deserialize types as well as serde

@plafer
Copy link
Contributor

plafer commented Dec 1, 2022

This might not be the best place for this discussion, but I had previously tried serializing via borsh (by forking ibc-rs to add #[derive]s) but found that borsh doesn't have an easy way to override specific fields of a struct with a macro like serde so it ended up being somewhat of a dead end as some underlying types (I think the timestamps in Tendermint) don't derive borsh. This could definitely be solved if someone put some more work into supporting borsh but at this time it isn't widely derived, so using borsh often requires things like deriving on wrapper structs.

Around this time, I realized that most of IBC already had protobuf serializations so it was a lot easier to commit to protobuf representation for serialization/deserialization. Maybe some guidance around using impl Protobuf, defining protobuf files, etc. might be better to nudge people towards that direction?

Very insightful. Seems to be what @DaviRain-Su is encountering as well. Supporting borsh does seem like it would take a decent commitment. While I think we want to end up supporting borsh, we don't have time to focus on that in the short term. However if this is a short-term requirement for you @DaviRain-Su then it does seem like the approach to take is to make sure that every type used by an ibc struct also derives borsh serialize/deserialize. And probably the same with parity-scale-codec.

@DaviRain-Su
Copy link
Contributor Author

Now I have implemented codec and borsh codec for the timestamp type separately

plafer pushed a commit that referenced this issue Dec 16, 2022
Farhad-Shabani pushed a commit that referenced this issue Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants