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

modules: Refactor reference-to-tuple method args #2603

Merged
merged 4 commits into from
Sep 7, 2022

Conversation

mzabaluev
Copy link
Contributor

@mzabaluev mzabaluev commented Aug 30, 2022

In core::ics04_channel::{ChannelReader, ChannelKeeper}, rework method arguments that force the caller to make unnecessary clones or form tuples for no good reason, breaking them up into individual arguments. Change some arguments on store_* methods from passed by reference to moved because the implementation is expected to take ownership of them. On delete_* methods, the key arguments need to be passed by reference.


PR author checklist:

  • Added changelog entry, using unclog.
  • Added tests: integration (for Hermes) or unit/mock tests (for modules).
  • Linked to GitHub issue.
  • Updated code comments and documentation (e.g., docs/).
  • Tagged one reviewer who will be the one responsible for shepherding this PR.

Reviewer checklist:

  • Reviewed Files changed in the GitHub PR explorer.
  • Manually tested (in case integration/unit/mock tests are absent).

In core::ics04_channel::{ChannelReader, ChannelKeeper},
rework method arguments that force the caller to make unnecessary
clones or form tuples for no good reason, breaking them up into
individual arguments. Change some arguments on store_* methods
from passed by reference to moved because the implementation is expected
to take ownership of them. On delete_* methods, the key arguments
need to be passed by reference.
@mzabaluev mzabaluev added O: new-feature Objective: cause to add a new feature or support modules O: code-hygiene Objective: cause to improve code hygiene labels Aug 30, 2022
@mzabaluev mzabaluev requested a review from hu55a1n1 August 30, 2022 11:39
Copy link
Member

@hu55a1n1 hu55a1n1 left a comment

Choose a reason for hiding this comment

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

Awesome stuff! 🎉 Approved with a minor nit related to mock code (feel free to ignore).


// Used by unordered channel
pub packet_receipt: BTreeMap<(PortId, ChannelId, Sequence), Receipt>,
pub packet_receipt: PortChannelIdMap<BTreeMap<Sequence, Receipt>>,
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think it would be better to use BTreeMap<ibc::core::ics24_host::path::ReceiptsPath, Receipt> in such cases or provide a PortChannelIdMap::get() that hides the double/triple look up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So far PortChannelIdMap is just a type alias, so I was hesitant to add method API around it. Maybe standalone helper fns would be the simplest way to deal with it; I don't want to invent an extension trait or do a newtype just for a convenience tradeoff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it would be better to use BTreeMap<ibc::core::ics24_host::path::ReceiptsPath, Receipt>

A non-allocating API for this would force use of ReceiptsPath in method parameters for get_packet_receipt/store_packet_receipt, and the call sites do not readily support that: the PortId, ChannelId, Sequence triplet comes from some larger structure like Packet. I suppose grouping of these packet fields as ReceiptsPath inside Packet does not make sense, because it might as well be an AcksPath?

Copy link
Member

@hu55a1n1 hu55a1n1 Sep 7, 2022

Choose a reason for hiding this comment

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

Yeah, it's tricky to design a non-allocating API, also because some paths deal with source port/channel-id (e.g. CommitmentsPath) while others need destination port/channel-id (e.g. ReceiptsPath & AcksPath) - so grouping them together in the Packet struct wouldn't work. I have been thinking if we should have references in all path structs in the future but that would be problematic for the FromStr impl ->

pub struct ReceiptsPath<'a> {
    pub port_id: &'a PortId,
    pub channel_id: &'a ChannelId,
    pub sequence: Sequence,
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I gave it some thought and tried to work with some reference-containing structs too, but quickly gave it up as impractical.

Oddly, it's one instance where the internal implementation, in conjunction with Rust associative container lookup APIs, comes at odds with API ergonomics. If anyone can suggest how to organize multi-value key containers better, let it be known.

@@ -24,7 +24,7 @@ use super::timeout::TimeoutHeight;
/// A context supplying all the necessary read-only dependencies for processing any `ChannelMsg`.
pub trait ChannelReader {
/// Returns the ChannelEnd for the given `port_id` and `chan_id`.
fn channel_end(&self, port_channel_id: &(PortId, ChannelId)) -> Result<ChannelEnd, Error>;
fn channel_end(&self, port_id: &PortId, channel_id: &ChannelId) -> Result<ChannelEnd, Error>;
Copy link
Member

Choose a reason for hiding this comment

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

💯 👌

@mzabaluev mzabaluev merged commit 4fe2f52 into master Sep 7, 2022
@mzabaluev mzabaluev deleted the mikhail/less-clone-happy-apis branch September 7, 2022 14:29
hu55a1n1 added a commit to hu55a1n1/hermes that referenced this pull request Sep 13, 2022
In core::ics04_channel::{ChannelReader, ChannelKeeper},
rework method arguments that force the caller to make unnecessary
clones or form tuples for no good reason, breaking them up into
individual arguments. Change some arguments on store_* methods
from passed by reference to moved because the implementation is expected
to take ownership of them. On delete_* methods, the key arguments
need to be passed by reference.

Co-authored-by: hu55a1n1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O: code-hygiene Objective: cause to improve code hygiene O: new-feature Objective: cause to add a new feature or support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants