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

Refactor and fix version validation in connection and channel handshakes #626

Merged
merged 20 commits into from
Apr 24, 2023

Conversation

Farhad-Shabani
Copy link
Member

@Farhad-Shabani Farhad-Shabani commented Apr 14, 2023

Closes: #625

(As a side note, this PR streamlined also a few errors, showing that some of our error confusion comes from how certain logic is implemented)


PR author checklist:

  • Added changelog entry, using unclog.
  • Added tests.
  • 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).

@codecov
Copy link

codecov bot commented Apr 14, 2023

Codecov Report

Patch coverage: 94.04% and project coverage change: +0.09 🎉

Comparison is base (87f2f54) 72.97% compared to head (3fa02e6) 73.07%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #626      +/-   ##
==========================================
+ Coverage   72.97%   73.07%   +0.09%     
==========================================
  Files         126      126              
  Lines       15684    15678       -6     
==========================================
+ Hits        11446    11456      +10     
+ Misses       4238     4222      -16     
Impacted Files Coverage Δ
crates/ibc/src/core/ics03_connection/error.rs 7.14% <ø> (ø)
crates/ibc/src/core/ics04_channel/error.rs 0.00% <ø> (ø)
.../ibc/src/core/ics04_channel/handler/send_packet.rs 85.56% <ø> (ø)
crates/ibc/src/applications/transfer/context.rs 52.19% <72.72%> (+1.37%) ⬆️
crates/ibc/src/core/ics04_channel/version.rs 70.73% <77.77%> (+1.98%) ⬆️
crates/ibc/src/core/ics03_connection/connection.rs 36.17% <81.81%> (+1.07%) ⬆️
crates/ibc/src/core/ics03_connection/version.rs 90.54% <95.38%> (+0.86%) ⬆️
crates/ibc/src/applications/transfer/error.rs 12.00% <100.00%> (+12.00%) ⬆️
crates/ibc/src/core/context.rs 83.51% <100.00%> (+0.18%) ⬆️
crates/ibc/src/core/context/acknowledgement.rs 97.46% <100.00%> (+0.01%) ⬆️
... and 23 more

... and 4 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Farhad-Shabani Farhad-Shabani marked this pull request as ready for review April 14, 2023 20:03
Comment on lines 336 to 338
if self.versions.len() != 1 {
return Err(ConnectionError::InvalidVersionLength);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think ibc-go enforces this, right? If not, neither should we

Copy link
Member Author

Choose a reason for hiding this comment

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

  • The way a connection is created, neither us nor IBC-go will end up with a connection supporting multiple versions (Vec<Version>). Since get_compatible_versions returns a vector of size 1, and the pick_version also matches a single version.

  • Though, already the get_compatible_versions API allows hosts to introduce multiple versions. Then, storing a ConnectionEnd on host with different versions is possible (bc of here), which is not good looking at how a channel is initialized. The MsgChannelOpenTry::validate() method doesn't know which connection version should be checked for ordering.

I kept this condition as we are not supposed to see multiples upon calling versions. We had this before as InvalidVersionLengthConnection error. Perhaps, we should drop it after figuring out how to manage various versions or not allowing users for the Vec<Version> in get_compatible_versions. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

I looked into it a bit more. The problem is that versions has different semantics based on which handshake step it is in:

  • after INIT: versions holds the compatible versions
    • This was given by the relayer, or a set of default values
  • after TRY,ACK,CONFIRM: versions holds THE version (exactly one) that was chosen by the handshake

Therefore, to accomodate both cases, we currently store a Vec<Version>. My takeaways:

  1. There's probably a better way to model this using an enum which differentates between the 2 cases.
  2. For now though, we should update the check to: if !matches!(self.state, State::Init) && self.versions.len() != 1 (and update the comment because this is hard to read)

Copy link
Contributor

Choose a reason for hiding this comment

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

But then the invariant is enforced at "read time", which is not very elegant. A better way is to enforce the invariant in new(); when we create the object, we ensure that it is valid, and then we don't have to check this everytime we read (and add a ?)

Copy link
Member Author

Choose a reason for hiding this comment

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

See if this works? 63c50a3

@plafer plafer changed the title Streamline codebase around Version validation Refactor and fix Version validation in connection and channel handshakes Apr 21, 2023
@plafer plafer changed the title Refactor and fix Version validation in connection and channel handshakes Refactor and fix version validation in connection and channel handshakes Apr 21, 2023
@plafer
Copy link
Contributor

plafer commented Apr 24, 2023

Note: made minor name changes, see if you like aa91e71

@plafer
Copy link
Contributor

plafer commented Apr 24, 2023

Could you also update basecoin to run the integration tests on this branch?

Comment on lines 513 to 515
pub fn state_matches(&self, expected: &State) -> bool {
self == expected
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noticed this; this is a reimplementation of the standard PartialEq::eq. 3fa02e6

Copy link
Contributor

@plafer plafer left a comment

Choose a reason for hiding this comment

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

👌

Let's wait for the integration tests to finish and then we can merge informalsystems/basecoin-rs#95

@Farhad-Shabani Farhad-Shabani merged commit 933b5aa into main Apr 24, 2023
@Farhad-Shabani Farhad-Shabani deleted the farhad/streamline-version-validation branch April 24, 2023 17:27
Farhad-Shabani added a commit that referenced this pull request Sep 9, 2024
…kes (#626)

* Streamline codebase around Version validation

* Fix versions() check

* Add a docstring for versions() check

* Simplify version check in conn_open_ack

* Remove unnecessary for

* Resolve a minor issue

* Further pruning

* favor slices to `Vec`

* Mend feature checks

* Fix clippy

* Allow empty version for on_chan_open_init_validate

* Fix pick() test

* Modify pick_version

* Edit changelog description

* name changes

* Move version length check into the new ConnectionEnd constructor

* Revert Versions signature

* docstring

* remove redundant function

---------

Co-authored-by: Philippe Laferrière <plafer@protonmail.com>
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.

[ICS03][ICS04] Refactor and fix version validation in connection and channel handshakes
2 participants