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

perf: minimize logic on rechecktx for recvpacket #6280

Merged
merged 18 commits into from
May 21, 2024

Conversation

colin-axner
Copy link
Contributor

@colin-axner colin-axner commented May 8, 2024

Description

a riff on top of #6248

closes: #6232

can be backported to any release line as patch release


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.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced replay protection for relay transactions to improve security and performance during re-executions.
    • Improved IBC packet reception by separating logging and event emission from replay protection logic.
  • Refactor

    • Restructured code for packet reception to enhance clarity and maintainability.
  • Tests

    • Added comprehensive tests to validate new functionalities and ensure robustness in packet handling and relay transaction processes.

Copy link
Contributor

coderabbitai bot commented May 8, 2024

Walkthrough

Walkthrough

The changes made across various modules aim to optimize the RedundantRelayDecorator for CheckTx and RecheckTx operations in the IBC module. By streamlining the logic and reducing unnecessary processing, these modifications target performance improvements, particularly in consensus and mempool handling.

Changes

File Path Summary
.../04-channel/keeper/ante.go Introduces RecvPacketReCheckTx in the Keeper struct for replay protection in ReCheckTx.
.../04-channel/keeper/ante_test.go Adds tests for RecvPacketReCheckTx functionality within the keeper_test package.
.../04-channel/keeper/packet.go Restructures packet reception logic by moving logging and event emission to applyReplayProtection.
modules/core/ante/ante.go
modules/core/ante/ante_test.go
Implements recvPacketReCheckTx in RedundantRelayDecorator and updates related tests.
CHANGELOG.md Enhances performance by optimizing redundant proof checking in RecvPacket execution.

Assessment against linked issues

Objective Addressed Explanation
Speedup RedundantRelayDecorator for Check and RecheckTx (#6232) The changes directly address the performance concerns outlined in the linked issue.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@@ -526,6 +526,21 @@ func (k *Keeper) RecvPacket(goCtx context.Context, msg *channeltypes.MsgRecvPack
return &channeltypes.MsgRecvPacketResponse{Result: channeltypes.SUCCESS}, nil
}

func (k *Keeper) RecvPacketReCheckTx(goCtx context.Context, msg *channeltypes.MsgRecvPacket) (*channeltypes.MsgRecvPacketResponse, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

if 04-channel msg server logic was moved, we could do with only a single public api for all this logic

@colin-axner colin-axner marked this pull request as ready for review May 13, 2024 15:21
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits Files that changed from the base of the PR and between 36d1335 and d972605.
Files selected for processing (5)
  • modules/core/04-channel/keeper/ante.go (1 hunks)
  • modules/core/04-channel/keeper/ante_test.go (1 hunks)
  • modules/core/04-channel/keeper/packet.go (2 hunks)
  • modules/core/ante/ante.go (3 hunks)
  • modules/core/ante/ante_test.go (4 hunks)
Additional Context Used
GitHub Check Runs (1)
lint failure (5)

modules/core/04-channel/keeper/ante_test.go: [failure] 28-28:
string invalid-port has 3 occurrences, make it a constant (goconst)


modules/core/04-channel/keeper/ante_test.go: [failure] 5-5:
duplicated-imports: Package "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" already imported (revive)


modules/core/04-channel/keeper/ante_test.go: [failure] 4-4:
ST1019: package "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" is being imported more than once (stylecheck)

Path-based Instructions (5)
modules/core/04-channel/keeper/ante.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

modules/core/04-channel/keeper/ante_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

modules/core/ante/ante.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

modules/core/ante/ante_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

modules/core/04-channel/keeper/packet.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (6)
modules/core/04-channel/keeper/ante.go (1)

9-22: The implementation of RecvPacketReCheckTx in Keeper struct effectively handles replay protection by checking the existence of the channel and applying replay protection logic. This is a crucial enhancement for ensuring that redundant relay transactions are filtered out during the ReCheckTx phase.

modules/core/04-channel/keeper/ante_test.go (1)

9-66: The test function TestRecvPacketReCheckTx is well-structured and covers various scenarios including successful packet reception, channel not found, and redundant relay. This ensures that the new replay protection logic is robustly tested.

modules/core/ante/ante.go (1)

105-118: The function recvPacketReCheckTx within RedundantRelayDecorator is designed to handle the core IBC receiving logic during the ReCheckTx phase, skipping application logic for performance. This separation is crucial for optimizing the handling of packet receptions in different transaction contexts.

modules/core/ante/ante_test.go (2)

Line range hint 178-492: The test function TestAnteDecoratorCheckTx thoroughly tests the new logic in the AnteHandle function of RedundantRelayDecorator, covering various scenarios including success on new messages and failure on redundant messages. This comprehensive testing ensures that the new logic behaves as expected under different conditions.


493-583: The test function TestAnteDecoratorReCheckTx effectively tests the ReCheckTx logic in the AnteHandle function, ensuring that new and redundant messages are handled correctly. This is crucial for verifying the performance optimizations made in the recvPacketReCheckTx function.

modules/core/04-channel/keeper/packet.go (1)

191-209: The separation of replay protection logic into the applyReplayProtection function from RecvPacket is a significant improvement. This not only cleans up the RecvPacket function but also enhances modularity and maintainability of the code.

Copy link
Member

@damiannolan damiannolan 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! Thank you @colin-axner! LGTM 🚀

modules/core/ante/ante.go Outdated Show resolved Hide resolved
modules/core/ante/ante.go Show resolved Hide resolved
modules/core/ante/ante_test.go Outdated Show resolved Hide resolved
modules/core/04-channel/keeper/ante_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits Files that changed from the base of the PR and between d972605 and 2bd15dd.
Files selected for processing (2)
  • modules/core/04-channel/keeper/ante_test.go (1 hunks)
  • modules/core/ante/ante.go (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • modules/core/04-channel/keeper/ante_test.go
  • modules/core/ante/ante.go

modules/core/ante/ante.go Outdated Show resolved Hide resolved

// RecvPacketReCheckTx applies replay protection ensuring that when relay messages are
// re-executed in ReCheckTx, we can appropriately filter out redundant relay transactions.
func (k *Keeper) RecvPacketReCheckTx(ctx sdk.Context, packet types.Packet) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering if there would be an alternative way to implement this where we don't need to increase the public API of the channel keeper...

Also, for my understanding: why what we do in ReCheckTx is different from what we do in CheckTx?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Outside of copy pasting the code, you would need to increase the public API of the channel keeper. I do not recommend copy pasting the code due to the codes sensitivity. It'd be nice to limit the channel keeper API, but we need to do some restructuring and make use of the internal package more. I'd say we should start by addressing #19 and moving the respective msg servers to their respective keepers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ReCheckTx is rerun on the mempool after every block commit. It is redundant to recheck proofs and other logic which will not change between commits

func (rrd RedundantRelayDecorator) recvPacketReCheckTx(ctx sdk.Context, msg *channeltypes.MsgRecvPacket) (*channeltypes.MsgRecvPacketResponse, error) {
// If the packet was already received, perform a no-op
// Use a cached context to prevent accidental state changes
cacheCtx, writeFn := ctx.CacheContext()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

added for consistency with checkTx

Copy link
Contributor

@DimitrisJim DimitrisJim left a comment

Choose a reason for hiding this comment

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

wonderful job with docs and tests!

Copy link

sonarcloud bot commented May 20, 2024

Quality Gate Passed Quality Gate passed for 'ibc-go'

Issues
8 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@colin-axner colin-axner enabled auto-merge (squash) May 21, 2024 09:25
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (3)
CHANGELOG.md (3)

Line range hint 195-195: Remove trailing spaces.

- * (core/ante) [\#6302](https://github.com/cosmos/ibc-go/pull/6302) Performance: Skip app callbacks during RecvPacket execution in checkTx within the redundant relay ante handler. 
+ * (core/ante) [\#6302](https://github.com/cosmos/ibc-go/pull/6302) Performance: Skip app callbacks during RecvPacket execution in checkTx within the redundant relay ante handler.

Line range hint 245-245: Remove trailing spaces.

- * (core/ante) [\#6280](https://github.com/cosmos/ibc-go/pull/6280) Performance: Skip redundant proof checking in RecvPacket execution in reCheckTx within the redundant relay ante handler. 
+ * (core/ante) [\#6280](https://github.com/cosmos/ibc-go/pull/6280) Performance: Skip redundant proof checking in RecvPacket execution in reCheckTx within the redundant relay ante handler.

Line range hint 180-180: Use a descriptive text for the URL to improve accessibility and SEO.

- * [\#6193](https://github.com/cosmos/ibc-go/pull/6193) Bump Cosmos SDK to v0.50.6.
+ * [Bump Cosmos SDK to v0.50.6](https://github.com/cosmos/ibc-go/pull/6193).
Review Details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits Files that changed from the base of the PR and between 2bd15dd and 2a83e8d.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Additional Context Used
Path-based Instructions (1)
CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

@colin-axner colin-axner merged commit 56ae97d into main May 21, 2024
75 of 76 checks passed
@colin-axner colin-axner deleted the colin/6232-perf-rechecktx-recvpacket branch May 21, 2024 09:28
mergify bot pushed a commit that referenced this pull request May 21, 2024
* perf: minimize logic on rechecktx for recvpacket

* refactor: rework layout for recvpacket rechecktx

* test: add tests for 04-channel rechecktx func

* test: add tests for core ante handler

* chore: add comment explaining is rechecktx usage

* linter appeasement

* chore: add changelog entry

* Update modules/core/ante/ante.go

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>

* imp: use cached ctx for consistency

* refactor: change added test to use expected errors

* lint

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
(cherry picked from commit 56ae97d)

# Conflicts:
#	modules/core/04-channel/keeper/packet.go
#	modules/core/ante/ante.go
mergify bot pushed a commit that referenced this pull request May 21, 2024
* perf: minimize logic on rechecktx for recvpacket

* refactor: rework layout for recvpacket rechecktx

* test: add tests for 04-channel rechecktx func

* test: add tests for core ante handler

* chore: add comment explaining is rechecktx usage

* linter appeasement

* chore: add changelog entry

* Update modules/core/ante/ante.go

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>

* imp: use cached ctx for consistency

* refactor: change added test to use expected errors

* lint

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
(cherry picked from commit 56ae97d)

# Conflicts:
#	modules/core/04-channel/keeper/packet.go
#	modules/core/ante/ante.go
mergify bot pushed a commit that referenced this pull request May 21, 2024
* perf: minimize logic on rechecktx for recvpacket

* refactor: rework layout for recvpacket rechecktx

* test: add tests for 04-channel rechecktx func

* test: add tests for core ante handler

* chore: add comment explaining is rechecktx usage

* linter appeasement

* chore: add changelog entry

* Update modules/core/ante/ante.go

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>

* imp: use cached ctx for consistency

* refactor: change added test to use expected errors

* lint

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
(cherry picked from commit 56ae97d)

# Conflicts:
#	modules/core/ante/ante.go
colin-axner added a commit that referenced this pull request May 21, 2024
)

* perf: minimize logic on rechecktx for recvpacket (#6280)

* perf: minimize logic on rechecktx for recvpacket

* refactor: rework layout for recvpacket rechecktx

* test: add tests for 04-channel rechecktx func

* test: add tests for core ante handler

* chore: add comment explaining is rechecktx usage

* linter appeasement

* chore: add changelog entry

* Update modules/core/ante/ante.go

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>

* imp: use cached ctx for consistency

* refactor: change added test to use expected errors

* lint

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
(cherry picked from commit 56ae97d)

# Conflicts:
#	modules/core/04-channel/keeper/packet.go
#	modules/core/ante/ante.go

* fix: merge conflicts

* lint

* lint

---------

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
colin-axner added a commit that referenced this pull request May 21, 2024
)

* perf: minimize logic on rechecktx for recvpacket (#6280)

* perf: minimize logic on rechecktx for recvpacket

* refactor: rework layout for recvpacket rechecktx

* test: add tests for 04-channel rechecktx func

* test: add tests for core ante handler

* chore: add comment explaining is rechecktx usage

* linter appeasement

* chore: add changelog entry

* Update modules/core/ante/ante.go

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>

* imp: use cached ctx for consistency

* refactor: change added test to use expected errors

* lint

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
(cherry picked from commit 56ae97d)

# Conflicts:
#	modules/core/04-channel/keeper/packet.go
#	modules/core/ante/ante.go

* fix: merge conflicts

---------

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
colin-axner added a commit that referenced this pull request May 21, 2024
)

* perf: minimize logic on rechecktx for recvpacket (#6280)

* perf: minimize logic on rechecktx for recvpacket

* refactor: rework layout for recvpacket rechecktx

* test: add tests for 04-channel rechecktx func

* test: add tests for core ante handler

* chore: add comment explaining is rechecktx usage

* linter appeasement

* chore: add changelog entry

* Update modules/core/ante/ante.go

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>

* imp: use cached ctx for consistency

* refactor: change added test to use expected errors

* lint

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
(cherry picked from commit 56ae97d)

# Conflicts:
#	modules/core/ante/ante.go

* fix: merge conflicts

---------

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Speedup RedundantRelayDecorator for Check and RecheckTx
4 participants