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(storage): common inbound nonce #40

Merged
merged 6 commits into from
Jul 1, 2024
Merged

refactor(storage): common inbound nonce #40

merged 6 commits into from
Jul 1, 2024

Conversation

MaxMustermann2
Copy link
Collaborator

@MaxMustermann2 MaxMustermann2 commented Jun 27, 2024

The mapping of eid -> sender -> nonce was stored previously in all BootstrapStorage and ExocoreGatewayStorage. This change moves that mapping into the parent GatewayStorage contract, as well as its associated validation functionality.

The PR removes the TSS-related nonce that is no longer used.

Summary by CodeRabbit

  • Bug Fixes

    • Improved nonce handling across various contracts to enhance security and reliability.
  • Documentation

    • Updated comments to clarify the role and functionality of specific variables, aiding in better understanding and maintenance.

Copy link
Contributor

coderabbitai bot commented Jun 27, 2024

Walkthrough

The changes involve updating nonce handling mechanisms across various contracts, particularly by replacing the _consumeInboundNonce function with _verifyAndUpdateNonce. This is aimed at enhancing nonce verification and updating logic. Some storage structures have also been adjusted, including removal and addition of mappings.

Changes

Files/Modules Change Summary
src/core/BootstrapLzReceiver.sol Replaced _consumeInboundNonce with _verifyAndUpdateNonce, updating nonce logic.
src/core/ClientGatewayLzReceiver.sol, ExocoreGateway.sol Merged _consumeInboundNonce into _verifyAndUpdateNonce to handle nonces with updated logic.
src/storage/BootstrapStorage.sol Removed nonce-related mappings and variables to streamline message processing tracking.
src/storage/ClientChainGatewayStorage.sol Updated comment on outboundNonce to specify its unique role.
src/storage/ExocoreGatewayStorage.sol Removed inboundNonce mapping and added chainToBootstrapped mapping to alter nonce management structure.
src/storage/GatewayStorage.sol Introduced a new public inboundNonce mapping and new internal _verifyAndUpdateNonce function for nonce verification and updating.
test/mocks/ExocoreGatewayMock.sol Replaced _consumeInboundNonce with _verifyAndUpdateNonce, updating logic for nonce handling in mock contracts.

Sequence Diagram(s)

sequenceDiagram
    participant Sender
    participant GatewayStorage
    participant BootstrapLzReceiver
    participant ClientGatewayLzReceiver
    
    Sender ->> BootstrapLzReceiver: Send message with nonce
    BootstrapLzReceiver ->> GatewayStorage: Verify and update nonce
    GatewayStorage ->> GatewayStorage: Validate nonce
    GatewayStorage ->> BootstrapLzReceiver: Acknowledge update
    BootstrapLzReceiver ->> Sender: Confirmation

    Sender ->> ClientGatewayLzReceiver: Send message with nonce
    ClientGatewayLzReceiver ->> GatewayStorage: Verify and update nonce
    GatewayStorage ->> GatewayStorage: Validate nonce
    GatewayStorage ->> ClientGatewayLzReceiver: Acknowledge update
    ClientGatewayLzReceiver ->> Sender: Confirmation
Loading

Poem

Amid the codes, changes dance,
Nonces shift in a new romance.
Flows updated, storage clean,
In this realm of virtual sheen.
Each line a step, a rabbit's leap,
Our blockchain dreams, forever keep.


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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @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.

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 UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between acf8d2f and 6c13c59.

Files ignored due to path filters (2)
  • docs/architecture.svg is excluded by !**/*.svg
  • docs/lst-flow.svg is excluded by !**/*.svg
Files selected for processing (8)
  • src/core/BootstrapLzReceiver.sol (2 hunks)
  • src/core/ClientGatewayLzReceiver.sol (2 hunks)
  • src/core/ExocoreGateway.sol (2 hunks)
  • src/storage/BootstrapStorage.sol (1 hunks)
  • src/storage/ClientChainGatewayStorage.sol (1 hunks)
  • src/storage/ExocoreGatewayStorage.sol (1 hunks)
  • src/storage/GatewayStorage.sol (2 hunks)
  • test/mocks/ExocoreGatewayMock.sol (2 hunks)
Files skipped from review due to trivial changes (2)
  • src/storage/BootstrapStorage.sol
  • src/storage/ClientChainGatewayStorage.sol
Additional comments not posted (6)
src/storage/GatewayStorage.sol (2)

20-21: Introduction of public mapping inboundNonce.

This mapping tracks nonces for specific sources, which is crucial for ensuring the integrity and non-repudiation of messages across different chains. This change aligns with the PR's goal to centralize nonce management.


30-36: Implementation of _verifyAndUpdateNonce function.

This function correctly implements nonce verification and updates the nonce if it matches the expected value. The use of revert with a custom error message is a best practice for handling unexpected nonces, providing clear feedback on the error.

src/core/BootstrapLzReceiver.sol (1)

21-21: Integration of _verifyAndUpdateNonce in _lzReceive.

The integration of _verifyAndUpdateNonce within _lzReceive ensures that every incoming message is verified for nonce correctness before processing. This is a critical security feature that prevents replay attacks.

src/core/ClientGatewayLzReceiver.sol (1)

29-29: Integration of _verifyAndUpdateNonce in _lzReceive.

The use of _verifyAndUpdateNonce here ensures that each message processed by the client gateway has its nonce verified, which is essential for maintaining the integrity and sequence of cross-chain messages.
[APROVED]

test/mocks/ExocoreGatewayMock.sol (1)

170-170: Integration of _verifyAndUpdateNonce in _lzReceive for mock testing.

Correctly integrating _verifyAndUpdateNonce in the mock contract's _lzReceive function ensures that the nonce verification logic is consistently tested across different implementations, maintaining the integrity of the testing process.

src/core/ExocoreGateway.sol (1)

149-149: Refactored Nonce Verification and Update Approved

The replacement of _consumeInboundNonce with _verifyAndUpdateNonce aligns with the PR's objectives to centralize and improve nonce management. Ensure that the new function _verifyAndUpdateNonce is thoroughly tested and audited to prevent security vulnerabilities such as replay attacks.

Verification successful

Refactored Nonce Verification and Update Approved

The replacement of _consumeInboundNonce with _verifyAndUpdateNonce aligns with the PR's objectives to centralize and improve nonce management. The implementation of _verifyAndUpdateNonce in src/storage/GatewayStorage.sol correctly verifies and updates the nonce, ensuring it prevents replay attacks. Its usage in other files (src/core/BootstrapLzReceiver.sol, src/core/ClientGatewayLzReceiver.sol, src/core/ExocoreGateway.sol, and test/mocks/ExocoreGatewayMock.sol) is consistent and appropriate.

  • Implementation Location: src/storage/GatewayStorage.sol
  • Usage Locations:
    • src/core/BootstrapLzReceiver.sol
    • src/core/ClientGatewayLzReceiver.sol
    • src/core/ExocoreGateway.sol
    • test/mocks/ExocoreGatewayMock.sol
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the implementation and usage of `_verifyAndUpdateNonce`.

# Test: Search for the function implementation and usage. Expect: Secure and correct implementation.
rg --type solidity $'_verifyAndUpdateNonce'

Length of output: 75



Script:

#!/bin/bash
# Description: Verify the implementation and usage of `_verifyAndUpdateNonce`.

# Step 1: Find all Solidity files in the repository.
SOL_FILES=$(fd --extension sol)

# Step 2: Search for the `_verifyAndUpdateNonce` function definition and usage within these files.
for file in $SOL_FILES; do
    echo "Searching in $file"
    rg '_verifyAndUpdateNonce' "$file"
done

Length of output: 18923



Script:

#!/bin/bash
# Description: Extract the implementation of `_verifyAndUpdateNonce` and review its usage.

# Step 1: Extract the implementation of `_verifyAndUpdateNonce` from `src/storage/GatewayStorage.sol`.
echo "Implementation of _verifyAndUpdateNonce in src/storage/GatewayStorage.sol:"
rg --context 10 'function _verifyAndUpdateNonce' src/storage/GatewayStorage.sol

# Step 2: Review the usage of `_verifyAndUpdateNonce` in other files.
echo "Usage of _verifyAndUpdateNonce in other files:"
rg '_verifyAndUpdateNonce' src/core/BootstrapLzReceiver.sol src/core/ClientGatewayLzReceiver.sol src/core/ExocoreGateway.sol test/mocks/ExocoreGatewayMock.sol

Length of output: 1694

while the overrides still need to be present to inform the compiler
about the base class whose function should be called, the implementation
can still be made common in the base class.
@MaxMustermann2
Copy link
Collaborator Author

The last commit a40ff77 moves the implementation of nextNonce to the base class GatewayStorage. However, the overrides still need to be retained since the method is inherited from other classes as well.

Copy link
Collaborator

@adu-web3 adu-web3 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 add a simple comment explaining my thought about outboundNonce

src/storage/ClientChainGatewayStorage.sol Show resolved Hide resolved
src/storage/BootstrapStorage.sol 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 UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6c13c59 and 5919621.

Files selected for processing (1)
  • src/storage/ClientChainGatewayStorage.sol (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/storage/ClientChainGatewayStorage.sol

@bwhour bwhour merged commit 74cd9b6 into main Jul 1, 2024
5 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.

4 participants