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(ibc-testkit): use bon instead of typed-builder #1326

Merged
merged 13 commits into from
Sep 25, 2024

Conversation

Veetaha
Copy link
Contributor

@Veetaha Veetaha commented Aug 28, 2024

Closes: #1289

Description

Hi, I'm the maintainer of the bon crate. I found your issue #1289 and decided to work on it. While working on it I also fixed a couple of bugs in bon caused by edge cases in the handling of generics in functions return types and added a workaround to the bon's code generation for the rustc bug (rust-lang/rust#87682 (comment)). The new 2.0.1 patch release contains the necessary fixes, so now I can submit this PR with confidence that there are no more bugs in bon triggered by the usage patterns in your repo.


Note that I know almost nothing about blockchain 🐱, and this PR is basically just a mechanical migration from TypedBuilder syntax to bon syntax.


PR author checklist:

  • Added changelog entry, using unclog (this internal change doesn't need a changelog entry).
  • Added tests (no additional tests are needed).
  • 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).

@Veetaha
Copy link
Contributor Author

Veetaha commented Aug 28, 2024

@rnbguy, I think your review is required, since you are the author of the issue

pub fn init<H>(
context: &TestContext<H>,

#[builder(default, into)] //
Copy link
Contributor Author

@Veetaha Veetaha Aug 28, 2024

Choose a reason for hiding this comment

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

Note that rustfmt does a bad job of formatting attributes on function arguments due to rust-lang/rustfmt#6276. Throughout the PR I tried to avoid using them (e.g. use Option type instead of adding #[builder(default)]), but here I used a workaround with an empty line comment to nudge rustfmt into placing the attribute on a separate line before the function parameter (which reads better IMO).

I described this problem in the docs about limitations.

Copy link
Collaborator

Choose a reason for hiding this comment

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

hey. Thanks for mentioning this. We will try it out at our end and decide. 🙌

Copy link
Collaborator

Choose a reason for hiding this comment

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

I took a look at the examples from your doc and tried on our codebase. For now, I am ok with having #[builder(default = ...)] arg: T in the same line 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll remove the workarounds

@rnbguy
Copy link
Collaborator

rnbguy commented Aug 31, 2024

hey @Veetaha ! 👋 Thanks so much for this PR. 🙏

Since this touches many parts of the codebase, please allow us a few weeks to review this properly. 🙂

Comment on lines 481 to 496
#[bon]
impl LightClientBuilder {
#[builder(finish_fn = build)]
pub fn init<H>(
context: &TestContext<H>,

#[builder(default, into)] //
consensus_heights: Vec<Height>,

#[builder(default)] //
params: H::LightClientParams,
) -> LightClientState<H>
where
H: TestHost,
HostClientState<H>: ClientStateValidation<DefaultIbcStore>,
{
Copy link
Collaborator

@rnbguy rnbguy Sep 19, 2024

Choose a reason for hiding this comment

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

hey. can you explain why you still needed a struct here ? not a dummy_light_client() method ?

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, I just hesitated here a bit, since it seemed to me like it wasn't just a dummy fixture. Now that you pointed this out, I'll change it to dummy_light_client().

Copy link
Contributor Author

@Veetaha Veetaha Sep 19, 2024

Choose a reason for hiding this comment

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

Btw. I made context here to be the parameter of the starting function, so the syntax is:

dummy_light_client(context) // context is passed to the starting function
     .consensus_heights(...) // optional
     .params(...) // optional
     .build()

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah nice ! I can think of some places, we could do this. But I will take care of it. 🙌

Also, build is the default name for finish function, right ? In that case, we can remove the (finish_fn = build).

Copy link
Contributor Author

@Veetaha Veetaha Sep 20, 2024

Choose a reason for hiding this comment

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

build is the default name for finish function only if you derive the builder for a struct with derive(bon::Builder) or if you derive the builder from the associated method named new. In these contexts, it's obvious that the builder is used for "building on object".

For other functions and methods that can execute any other arbitrary logic the default finishing method name is call(). bon can't assume what the logic of the general-purpose functions/method does. In some cases like in ballistics_rs crate the functions do some pure calculations and return primitive values, in other cases people use bon on associated methods of async API clients, client.method().param().call().await. In all of these cases the build() name of the method doesn't really fit, so a more generic name was chosen.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah. fair enough. I am ok with using call instead of build 👍

cbadac8

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I guess all things are resolved, and this is ready to merge then

Copy link
Collaborator

Choose a reason for hiding this comment

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

There are some huge PRs due this week. After them, I will resolve any conflicts in your PR and merge it. Thanks, @Veetaha, for your contribution ! 🤝

@Veetaha Veetaha requested a review from rnbguy September 19, 2024 23:13
Copy link

codecov bot commented Sep 20, 2024

Codecov Report

Attention: Patch coverage is 96.46018% with 4 lines in your changes missing coverage. Please review.

Project coverage is 66.83%. Comparing base (ede380f) to head (c0945d5).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
ibc-testkit/src/fixtures/core/context.rs 93.02% 3 Missing ⚠️
ibc-testkit/src/testapp/ibc/core/types.rs 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1326      +/-   ##
==========================================
+ Coverage   66.76%   66.83%   +0.07%     
==========================================
  Files         225      225              
  Lines       22614    22633      +19     
==========================================
+ Hits        15098    15127      +29     
+ Misses       7516     7506      -10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@rnbguy rnbguy left a comment

Choose a reason for hiding this comment

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

Thanks @Veetaha ! ✨

@rnbguy rnbguy added this pull request to the merge queue Sep 25, 2024
Merged via the queue into cosmos:main with commit cfb707e Sep 25, 2024
10 checks passed
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.

use bon instead of typed-builder
2 participants