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

Remove the concept of a "zero Height" #2346

Merged
merged 46 commits into from
Jul 4, 2022
Merged

Conversation

plafer
Copy link
Contributor

@plafer plafer commented Jun 27, 2022

Closes: cosmos/ibc-go#1009

Description


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/).

Reviewer checklist:

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

Copy link
Contributor

@mzabaluev mzabaluev left a comment

Choose a reason for hiding this comment

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

Have you thought of making the revision_height member of Height a NonZeroU64?
And maybe making both members non-public while we are at it.
This will enforce avoidance of 0, and ensure that std::mem::size_of::<Option<Height>>() is 16.

@@ -29,11 +29,8 @@ pub struct MsgConnectionOpenAck {
impl MsgConnectionOpenAck {
/// Getter for accessing the `consensus_height` field from this message. Returns the special
/// value `Height(0)` if this field is not set.
Copy link
Contributor

Choose a reason for hiding this comment

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

The comments like this need to be updated too.

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 didn't find any other comments that referred to "Height zero"

4a9dca7

@plafer
Copy link
Contributor Author

plafer commented Jun 28, 2022

Have you thought of making the revision_height member of Height a NonZeroU64? And maybe making both members non-public while we are at it. This will enforce avoidance of 0, and ensure that std::mem::size_of::<Option<Height>>() is 16.

Yes, my original plan was to use NonZeroU64, but then creating a new Height looks like:

// e.g. when building from a tendermint header
Height::new(chain_id(), header_height_revision_number.try_into().map_err(|_| ...)?)

Basically since all the other types use u64, building a NonZeroU64 from a u64 requires a try_into().map_err(), and so I found it more elegant to have new() return a Result instead. We also can't use them internally because they currently don't have a stable API for addition, which we need.

I am also planning on making the members private.

@plafer plafer marked this pull request as ready for review June 28, 2022 21:43
@hu55a1n1
Copy link
Member

Great work @plafer! 👍 Just left some comments and questions.

Copy link
Member

@romac romac left a comment

Choose a reason for hiding this comment

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

Awesome work @plafer, thanks so much!

@romac romac merged commit 16c356a into master Jul 4, 2022
@romac romac deleted the plafer/1009-height-zero-complete branch July 4, 2022 08:09
hu55a1n1 pushed a commit to hu55a1n1/hermes that referenced this pull request Sep 13, 2022
Prior to this PR, the `Height` type used a special `Height::zero()` value to denote the absence of height value. This caused ad hoc checks of whether a height is zero in various places.

From now on, we instead use `Option<Height>` on places where the height value may be optional, and require non-zero value in the `Height` constructor.

- Optional height parameters should use `Option<Height>` instead of `Height`.
- Checking of `height == Height::zero()` is replaced with Option matching.

---

* From<Height> for String fix

* Use Option<Height> instead of Height::zero for consensus_height

* recv_packet still had Height::zero() in events

* TryFrom<RawHeight> for Height

* pre height parse changes

* Packet.timeout_height is now an Option<Height>

* rustdoc

* Remove Height::with_revision_height

* commit pre-modifying cli

* Finish Height::new() returns Result

* remove Height::is_zero()

* Remove `Height::zero()`

* Remove Height::default()

* Height fields accessors

* use revision_height accessor

* use revision_number accessor

* make Height fields private (WIP)

* compile error fixes

* FIXME in transfer.rs addressed

* Use existing constants instead of hardcoded strings

* changelog

* TimeoutHeight newtype

* Use TimeoutHeight everywhere

* Fixes after merging with master

* has_expired() fix

* doc url

* fix send_packet test

* Remove timeout_height == 0 && timestamp == 0 check

* Remove `has_timeout()`

* Change TimeoutHeight impl

* docstrings

* tests fix

* fix history manipulation test

* transfer test: no timeout

* msgtransfer test

* packet no timeout or timestamp test

* send_packet timeout height equal destination chain height

* send_packet test with timeout = height - 1

* test invalid timeout height

* fix docstring

* clippy

* Fix clippy warnings introduced in Rust 1.62

* Fix serialization of `TimeoutHeight` to match the format used by `Height`

Co-authored-by: Romain Ruetschi <romain@informal.systems>
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.

4 participants