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

cosmrs: refactor Msg* traits #146

Merged
merged 1 commit into from
Oct 28, 2021
Merged

cosmrs: refactor Msg* traits #146

merged 1 commit into from
Oct 28, 2021

Conversation

tony-iqlusion
Copy link
Member

  • Removes the former Msg newtype for Any; uses the Any type explicitly. The Msg newtype was providing little value and obscuring access to the type_url.
  • Renames from_msg and to_msg methods to from_any and to_any.
  • Renames the MsgType trait to Msg. This trait now has an associated Proto type bounded on MsgProto. This alows for default impls of from_any/to_any/into_any methods which replace the previous blanket impl of MsgType for MsgProto.

Altogether these changes simplify the API and eliminate some redundant
boilerplate impling the former MsgType trait.

- Removes the former `Msg` newtype for `Any`; uses the `Any` type
  explicitly. The `Msg` newtype was providing little value and obscuring
  access to the `type_url`.
- Renames `from_msg` and `to_msg` methods to `from_any` and `to_any`.
- Renames the `MsgType` trait to `Msg`. This trait now has an associated
  `Proto` type bounded on `MsgProto`. This alows for default impls of
  `from_any`/`to_any`/`into_any` methods which replace the previous
  blanket impl of `MsgType` for `MsgProto`.

Altogether these changes simplify the API and eliminate some redundant
boilerplate impling the former `MsgType` trait.
@tony-iqlusion
Copy link
Member Author

There are a lot of changes in this PR that are just churn because the new trait design eliminates a lot of previous repetitive boilerplate, so perhaps I can break it down a bit and make it easier to review.

The main changes of note are all in msg.rs, namely:

The struct Msg newtype has been removed; replaced with Any

It used to look like this:

/// Transaction messages
#[derive(Clone, Debug, PartialEq)]
pub struct Msg(pub(crate) Any);

But really its usage is synonymous with Any. At the very least the pub(crate) provides no value because it doesn't maintain an invariant.

This also means that tx::Body::messages is a Vec<Any> instead of Vec<Msg>. However that's not to strange as Any is already used for tx::Body::extension_options and tx::Body::non_critical_extension_options.

MsgType is now just Msg; has an associated Proto type

This is a trait for "domain types", which each have a corresponding Proto type, and is bounded on TryFrom and Into conversions to the proto type.

It provides two default methods from_any and to_any that take care of serializing the proto and wrapping it in an Any to simplify serialization, leveraging the TryFrom and Into bounds.

The many changes you see across other files are handwritten implementations of from_msg and to_msg being replaced by the default implementations of from_any and to_any across the many message types currently defined in the crate.

Copy link

@andynog andynog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me. I think it's mostly proto changes and the ErrorReport. The new Msg methods seem fine to me.

@tony-iqlusion tony-iqlusion merged commit 9484409 into main Oct 28, 2021
@tony-iqlusion tony-iqlusion deleted the cosmrs/msg-refactor branch October 28, 2021 15:12
@tony-iqlusion tony-iqlusion mentioned this pull request Oct 28, 2021
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 this pull request may close these issues.

2 participants