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

chore: pull out get light client module to helper methods #6712

Conversation

gjermundgaraba
Copy link
Contributor

@gjermundgaraba gjermundgaraba commented Jun 26, 2024

Description

Start of #6019
In order to keep the PRs for this task a little more digestible, I wanted to do this small refactor first.

And I wanted to ask if I should also use the helper method in other places where we currently do not do any if !k.GetParams(ctx).IsAllowedClient(clientType) checks.

The places I have identified that currently gets the route, but does not run IsAllowedClient:

  • func (k *Keeper) GetLatestClientConsensusState(ctx sdk.Context, clientID string) (exported.ConsensusState, bool) {
    clientModule, found := k.router.GetRoute(clientID)
    if !found {
    return nil, false
    }
    return k.GetClientConsensusState(ctx, clientID, clientModule.LatestHeight(ctx, clientID))
    }
  • func (k *Keeper) GetClientStatus(ctx sdk.Context, clientID string) exported.Status {
    clientType, _, err := types.ParseClientIdentifier(clientID)
    if err != nil {
    return exported.Unauthorized
    }
    if !k.GetParams(ctx).IsAllowedClient(clientType) {
    return exported.Unauthorized
    }
    clientModule, found := k.router.GetRoute(clientID)
    if !found {
    return exported.Unauthorized
    }
    return clientModule.Status(ctx, clientID)
    }
  • func (k *Keeper) GetClientLatestHeight(ctx sdk.Context, clientID string) types.Height {
    clientType, _, err := types.ParseClientIdentifier(clientID)
    if err != nil {
    return types.ZeroHeight()
    }
    if !k.GetParams(ctx).IsAllowedClient(clientType) {
    return types.ZeroHeight()
    }
    clientModule, found := k.router.GetRoute(clientID)
    if !found {
    return types.ZeroHeight()
    }
    var latestHeight types.Height
    latestHeight, ok := clientModule.LatestHeight(ctx, clientID).(types.Height)
    if !ok {
    panic(fmt.Errorf("cannot convert %T to %T", clientModule.LatestHeight, latestHeight))
    }
    return latestHeight
    }
  • func (k *Keeper) UpdateClient(ctx sdk.Context, clientID string, clientMsg exported.ClientMessage) error {
    if status := k.GetClientStatus(ctx, clientID); status != exported.Active {
    return errorsmod.Wrapf(types.ErrClientNotActive, "cannot update client (%s) with status %s", clientID, status)
    }
    clientType, _, err := types.ParseClientIdentifier(clientID)
    if err != nil {
    return errorsmod.Wrapf(err, "unable to parse client identifier %s", clientID)
    }
    clientModule, found := k.router.GetRoute(clientID)
    if !found {
    return errorsmod.Wrap(types.ErrRouteNotFound, clientID)
    }
    if err := clientModule.VerifyClientMessage(ctx, clientID, clientMsg); err != nil {
    return err
    }
    foundMisbehaviour := clientModule.CheckForMisbehaviour(ctx, clientID, clientMsg)
    if foundMisbehaviour {
    clientModule.UpdateStateOnMisbehaviour(ctx, clientID, clientMsg)
    k.Logger(ctx).Info("client frozen due to misbehaviour", "client-id", clientID)
    defer telemetry.IncrCounterWithLabels(
    []string{"ibc", "client", "misbehaviour"},
    1,
    []metrics.Label{
    telemetry.NewLabel(types.LabelClientType, clientType),
    telemetry.NewLabel(types.LabelClientID, clientID),
    telemetry.NewLabel(types.LabelMsgType, "update"),
    },
    )
    emitSubmitMisbehaviourEvent(ctx, clientID, clientType)
    return nil
    }
    consensusHeights := clientModule.UpdateState(ctx, clientID, clientMsg)
    k.Logger(ctx).Info("client state updated", "client-id", clientID, "heights", consensusHeights)
    defer telemetry.IncrCounterWithLabels(
    []string{"ibc", "client", "update"},
    1,
    []metrics.Label{
    telemetry.NewLabel(types.LabelClientType, clientType),
    telemetry.NewLabel(types.LabelClientID, clientID),
    telemetry.NewLabel(types.LabelUpdateType, "msg"),
    },
    )
    // emitting events in the keeper emits for both begin block and handler client updates
    emitUpdateClientEvent(ctx, clientID, clientType, consensusHeights, k.cdc, clientMsg)
    return nil
    }

Should they also do this?


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against the correct branch (see CONTRIBUTING.md).
  • Linked to GitHub issue with discussion and accepted design, OR link to spec that describes this work.
  • Code follows the module structure standards and Go style guide.
  • Wrote unit and integration tests.
  • Updated relevant documentation (docs/).
  • Added relevant godoc comments.
  • Provide a conventional commit message to follow the repository standards.
  • Include a descriptive changelog entry when appropriate. This may be left to the discretion of the PR reviewers. (e.g. chores should be omitted from changelog)
  • Re-reviewed Files changed in the GitHub PR explorer.
  • Review SonarCloud Report in the comment section below once CI passes.

@gjermundgaraba gjermundgaraba marked this pull request as ready for review June 26, 2024 13:46
@crodriguezvega
Copy link
Contributor

I was thinking that it might be worth to have feature branch for this, if it's going to span multiple PRs. We have still the benefit of reviewing smaller PRs, but at the same time we also have another opportunity to review how each piece fits together when reviewing the feature branch. What do you think?

@gjermundgaraba
Copy link
Contributor Author

gjermundgaraba commented Jun 26, 2024

I was thinking that it might be worth to have feature branch for this, if it's going to span multiple PRs. We have still the benefit of reviewing smaller PRs, but at the same time we also have another opportunity to review how each piece fits together when reviewing the feature branch. What do you think?

Yeah, happy to do that! I will create a feature branch and target that one instead :)

Edit: done! feat/refactor-client-route-to-verify

@gjermundgaraba gjermundgaraba changed the base branch from main to feat/refactor-client-route-to-verify June 26, 2024 14:24
Copy link
Contributor

@crodriguezvega crodriguezvega left a comment

Choose a reason for hiding this comment

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

I'd say that it makes sense to also do the IsAllowedClient check in the other functions...

modules/core/02-client/keeper/keeper.go Outdated Show resolved Hide resolved
@crodriguezvega crodriguezvega self-assigned this Jul 2, 2024
Copy link

sonarcloud bot commented Jul 2, 2024

Copy link
Contributor

@crodriguezvega crodriguezvega left a comment

Choose a reason for hiding this comment

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

Trying to continue the work of Gjermund while he is away...

@@ -89,7 +89,7 @@ func (ClientState) VerifyMembership(
}

// The commitment prefix (eg: "ibc") is omitted when operating on the core IBC store
bz := store.Get([]byte(merklePath.KeyPath[1]))
bz := store.Get(merklePath.KeyPath[1])
Copy link
Contributor

Choose a reason for hiding this comment

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

Linter was complaining about this.

clientModule, found := k.router.GetRoute(clientID)
if !found {
return errorsmod.Wrap(types.ErrRouteNotFound, clientID)
clientModule, err := k.getLightClientModule(ctx, clientID)
Copy link
Contributor

Choose a reason for hiding this comment

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

I had to leave the parsing of the client ID above, because the client type is used in the telemetry below. Same thing in UpdateClient. If somebody has a proposal to get rid of it (since it is also done in getLightClientModule), I'm all ears.

@crodriguezvega crodriguezvega added the priority PRs that need prompt reviews label Jul 2, 2024
Copy link
Contributor

@chatton chatton left a comment

Choose a reason for hiding this comment

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

LGTM!

I also think it makes sense to include the IsAllowed check anywhere we are fetching the client module.

@crodriguezvega crodriguezvega merged commit 92b6c86 into feat/refactor-client-route-to-verify Jul 3, 2024
82 of 87 checks passed
@crodriguezvega crodriguezvega deleted the gjermund/6019-refactor-get-route branch July 3, 2024 19:44
github-merge-queue bot pushed a commit that referenced this pull request Jul 9, 2024
…ient keeper (#6760)

* chore: pull out get light client module to helper methods (#6712)

* chore: pull out get light client module to helper methods

* another place to use getLightClientModuleRoute

* use getLightClientModule in other 02-client functions

* lint

---------

Co-authored-by: Carlos Rodriguez <[email protected]>

* add verify(non)membership functions to client keeper and removed route from interface

* delete file

* use VerifyMembershipProof function name until #6775

* Update modules/core/02-client/keeper/keeper.go

Co-authored-by: DimitrisJim <[email protected]>

---------

Co-authored-by: Gjermund Garaba <[email protected]>
Co-authored-by: DimitrisJim <[email protected]>
github-merge-queue bot pushed a commit that referenced this pull request Jul 11, 2024
* chore: pull out get light client module to helper methods (#6712)

* chore: pull out get light client module to helper methods

* another place to use getLightClientModuleRoute

* use getLightClientModule in other 02-client functions

* lint

---------

Co-authored-by: Carlos Rodriguez <[email protected]>

* add verify(non)membership functions to client keeper and removed route from interface

* delete file

* refactor: unexported grpc queryServer struct for 02-client

* refactor: use queryServer struct for 03-connection handlers

* refactor: 04-channel query server

* refactor: more query server refactoring

* refactor: rm top level query server interface

* chore: update interface type assertions

* chore: add changelog and migration docs

* chore: linter

* Update docs/docs/05-migrations/13-v8-to-v9.md

* chore: update godocs on submodule query servers

---------

Co-authored-by: Gjermund Garaba <[email protected]>
Co-authored-by: Carlos Rodriguez <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority PRs that need prompt reviews
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants