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

Global db nonces in InterchainDB #2068

Merged

Conversation

ChiTimesChi
Copy link
Collaborator

@ChiTimesChi ChiTimesChi commented Feb 20, 2024

Description
A clear and concise description of the features you're adding in this pull request.

Additional context
Add any other context about the problem you're solving.

Metadata

  • Fixes #[Link to Issue]

Summary by CodeRabbit

  • New Features
    • Introduced a new LocalEntry structure with enhanced fields for better data management.
    • Implemented a new system for entry verification and request processes, improving the interchain communication efficiency.
  • Refactor
    • Renamed dbWriterNonce to dbNonce across various contracts for consistency.
    • Overhauled the InterchainDB contract, including its storage mappings and function signatures, to accommodate new data structures and logic.
    • Updated event signatures to reflect the renaming of nonce variables.
    • Modified test cases to align with the updated contract logic and structures.
  • Bug Fixes
    • Adjusted parameter orders and values in test modules to fix inconsistencies with the new contract logic.

Copy link
Contributor

coderabbitai bot commented Feb 20, 2024

Important

Auto Review Skipped

Auto reviews are disabled on base/target branches other than the default branch. Please add the base/target branch pattern to the list of additional branches to be reviewed in the settings.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.

Walkthrough

The recent updates encompass a comprehensive overhaul of the interchain communication contracts, focusing on renaming dbWriterNonce to dbNonce and refining the data structures and functionalities within. This includes adjustments to event parameters, internal struct fields, and the logic for entry management, aimed at enhancing the system's efficiency and clarity.

Changes

Files Changes Summary
.../InterchainClientV1.sol, .../events/InterchainDBEvents.sol, .../interfaces/IInterchainDB.sol, .../test/InterchainClientV1Test.t.sol, .../test/mocks/InterchainDBMock.sol, .../test/modules/SynapseModule.Destination.t.sol, .../test/modules/SynapseModule.Source.t.sol Renamed dbWriterNonce to dbNonce. Updated event parameters, struct fields, and function calls.
.../InterchainDB.sol Refactored storage mappings, updated function signatures, and revised entry management logic.
.../libs/InterchainEntry.sol, .../test/libs/InterchainEntryLib.t.sol, .../test/harnesses/InterchainEntryLibHarness.sol Added dbNonce to structs, updated function names and logic for entry management.
.../test/InterchainDB.Dst.t.sol, .../test/InterchainDB.Src.t.sol Adjusted test functions and parameters to align with updated contract logic.
.../test/harnesses/InterchainClientV1Harness.sol Minor import statement formatting change.

🐇✨
In a land of code and byte,
Changes swift, through day and night.
dbNonce now takes the stage,
Scripts evolve, turn the page.
Together, we hop to greater height!
🌟🐾

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-tests for this file.
  • 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 tests 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 from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@ChiTimesChi
Copy link
Collaborator Author

@coderabbitai review

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8930c83 and 112f18f.
Files selected for processing (14)
  • packages/contracts-communication/contracts/InterchainClientV1.sol (7 hunks)
  • packages/contracts-communication/contracts/InterchainDB.sol (6 hunks)
  • packages/contracts-communication/contracts/events/InterchainDBEvents.sol (1 hunks)
  • packages/contracts-communication/contracts/interfaces/IInterchainDB.sol (3 hunks)
  • packages/contracts-communication/contracts/libs/InterchainEntry.sol (2 hunks)
  • packages/contracts-communication/test/InterchainClientV1Test.t.sol (2 hunks)
  • packages/contracts-communication/test/InterchainDB.Dst.t.sol (8 hunks)
  • packages/contracts-communication/test/InterchainDB.Src.t.sol (6 hunks)
  • packages/contracts-communication/test/harnesses/InterchainClientV1Harness.sol (2 hunks)
  • packages/contracts-communication/test/harnesses/InterchainEntryLibHarness.sol (1 hunks)
  • packages/contracts-communication/test/libs/InterchainEntryLib.t.sol (1 hunks)
  • packages/contracts-communication/test/mocks/InterchainDBMock.sol (2 hunks)
  • packages/contracts-communication/test/modules/SynapseModule.Destination.t.sol (1 hunks)
  • packages/contracts-communication/test/modules/SynapseModule.Source.t.sol (1 hunks)
Files skipped from review due to trivial changes (1)
  • packages/contracts-communication/test/harnesses/InterchainClientV1Harness.sol
Additional comments: 66
packages/contracts-communication/contracts/events/InterchainDBEvents.sol (3)
  • 6-6: The change from writerNonce to dbNonce in the InterchainEntryWritten event aligns with the PR's objective to refactor nonce management. This modification is consistent and correctly implemented.
  • 8-8: The update in the InterchainEntryVerified event to include dbNonce instead of writerNonce is consistent with the overall goal of transitioning to a global nonce system. This change is correctly applied.
  • 11-11: The modification in the InterchainVerificationRequested event to use dbNonce instead of writerNonce is in line with the PR's objectives and is correctly implemented.
packages/contracts-communication/test/harnesses/InterchainEntryLibHarness.sol (3)
  • 8-16: The addition of dbNonce as a parameter to the constructLocalEntry function aligns with the PR's objective to refactor nonce management. This change is correctly implemented.
  • 19-21: The entryKey function correctly calculates the globally unique identifier of the entry, which is consistent with the new nonce management approach.
  • 23-24: The entryValue function correctly calculates the value of the entry, which is consistent with the new nonce management approach.
packages/contracts-communication/test/mocks/InterchainDBMock.sol (3)
  • 9-9: The update to the requestVerification function signature to include dbNonce aligns with the PR's objective to refactor nonce management. This change is correctly implemented.
  • 25-25: The modification in the getEntry function to accept dbNonce instead of writerNonce is consistent with the transition to a global nonce system. This change is correctly applied.
  • 27-27: Replacing getWriterNonce with getDBNonce to get the database nonce is in line with the PR's objectives and is correctly implemented.
packages/contracts-communication/contracts/libs/InterchainEntry.sol (4)
  • 6-16: The addition of dbNonce to the InterchainEntry struct is a key part of the PR's objective to refactor nonce management. This change is correctly implemented and aligns with the transition to a global nonce system.
  • 3-32: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [23-39]

The modification of the constructLocalEntry function to include dbNonce is consistent with the new nonce management approach. This change is correctly applied.

  • 45-47: The entryKey function correctly calculates the globally unique identifier of the entry, which is consistent with the new nonce management approach.
  • 49-51: The entryValue function correctly calculates the value of the entry, which is consistent with the new nonce management approach.
packages/contracts-communication/test/libs/InterchainEntryLib.t.sol (8)
  • 12-12: The modification to include dbNonce in the InterchainEntry struct for testing purposes aligns with the PR's objective to refactor nonce management. This change is correctly implemented.
  • 20-20: The update to the assertEq function to include a check for dbNonce is consistent with the new nonce management approach. This change is correctly applied.
  • 27-30: The update to the test_constructLocalEntry function to accept dbNonce as a parameter is in line with the PR's objectives and is correctly implemented.
  • 34-42: The parameterized test function test_constructLocalEntry correctly includes dbNonce, which is consistent with the new nonce management approach.
  • 46-48: The test_entryKey function correctly calculates the key based on srcChainId and dbNonce, aligning with the new nonce management approach.
  • 51-53: The parameterized test_entryKey function correctly calculates the key based on srcChainId and dbNonce, which is consistent with the new nonce management approach.
  • 56-58: The test_entryValue function correctly calculates the value based on srcWriter and dataHash, aligning with the new nonce management approach.
  • 61-63: The parameterized test_entryValue function correctly calculates the value based on srcWriter and dataHash, which is consistent with the new nonce management approach.
packages/contracts-communication/test/modules/SynapseModule.Source.t.sol (1)
  • 31-32: The modification to the initialization of the mockEntry struct, including the correct order and values for dbNonce and srcWriter, aligns with the PR's objective to refactor nonce management. This change is correctly implemented.
packages/contracts-communication/contracts/interfaces/IInterchainDB.sol (8)
  • 7-13: The addition of the LocalEntry struct with writer and dataHash fields is a new introduction that aligns with the PR's objective to refactor nonce management. This change is correctly implemented.
  • 17-20: Renaming dataHash to entryValue in the RemoteEntry struct is consistent with the new approach to nonce management and entry representation. This change is correctly applied.
  • 23-24: The changes to the parameters of InterchainDB__ConflictingEntries and InterchainDB__EntryDoesNotExist errors are in line with the transition to a global nonce system. These changes are correctly implemented.
  • 33-34: Updating the return type and parameter names in the writeEntry function to reflect the new nonce management approach is correctly done.
  • 42-44: The update to the requestVerification function signature to include dbNonce aligns with the PR's objective to refactor nonce management. This change is correctly implemented.
  • 50-56: Clarification in comments for writeEntryWithVerification and the update to return dbNonce are consistent with the new nonce management approach. These changes are correctly applied.
  • 79-80: Changing the parameter in getEntry from address writer, uint256 writerNonce to dbNonce is in line with the PR's objectives and is correctly implemented.
  • 82-83: Replacing getWriterNonce with getDBNonce to get the database nonce aligns with the transition to a global nonce system. This change is correctly implemented.
packages/contracts-communication/test/InterchainClientV1Test.t.sol (2)
  • 129-129: The change from writerNonce to dbNonce in the InterchainEntry struct within the test setup aligns with the PR's objective to refactor nonce management. This modification is consistent and correctly implemented.
  • 142-142: The update in the InterchainClientV1.InterchainTransaction struct to include dbNonce instead of writerNonce is consistent with the overall goal of transitioning to a unified global database nonce (dbNonce). This change is correctly applied.
packages/contracts-communication/contracts/InterchainDB.sol (10)
  • 12-13: The introduction of LocalEntry[] internal _entries; and the modification of _remoteEntries mapping to include RemoteEntry struct directly are significant changes. Ensure that all interactions with these storage variables are correctly updated throughout the contract to reflect the new nonce management system. This change should improve data organization and access patterns, potentially enhancing gas efficiency for operations involving these structures.
  • 25-25: The writeEntry function now returns a uint256 dbNonce, which is a significant change aligning with the global nonce management strategy. Ensure that all external calls to this function correctly handle the returned nonce value. This change simplifies the interface and makes it more intuitive for contract users.
  • 32-39: The requestVerification function's signature has been updated to include uint256 dbNonce as a parameter, reflecting the shift to global nonce management. This change requires careful validation to ensure that the correct nonce is passed by callers, and it enhances the function's clarity by explicitly requiring the nonce associated with the entry to be verified.
  • 52-55: The writeEntryWithVerification function combines entry writing and verification request into a single operation, which is a useful abstraction for users. The explicit return of uint256 dbNonce and its subsequent use within the function demonstrate a clear and efficient handling of the new global nonce system. This pattern should be encouraged for its simplicity and effectiveness.
  • 63-75: The verifyEntry function's logic for handling the verification of entries has been carefully updated to work with the new global nonce system. The use of InterchainEntryLib for constructing keys and values, and the conditional logic for handling new versus existing entries, are correctly implemented. However, ensure that the timestamp (block.timestamp) used for verifiedAt is the desired approach for recording verification times, considering potential issues around block time manipulation by miners.
  • 91-94: The readEntry function's implementation, which checks the entry value against the one verified by the module and returns the verification timestamp, is a critical part of the verification logic. This function's correctness directly impacts the security and reliability of the verification process. Ensure that the comparison logic (remoteEntry.entryValue == entryValue) and the conditional return value are thoroughly tested, especially in edge cases where entry values might not match due to unexpected reasons.
  • 103-107: The getEntry function correctly handles the retrieval of entries based on the global nonce, including error handling for non-existent entries. This function is a key component of the new nonce management system, and its implementation appears to be correct and efficient. However, ensure that the condition getDBNonce() <= dbNonce correctly accounts for all valid nonces, especially considering zero-based indexing versus one-based nonce values.
  • 111-112: The getDBNonce function, which returns the current global nonce value, is a straightforward and essential part of the new nonce management system. This function's implementation is simple and appears to be correct. It's important to ensure that all parts of the contract and any interacting contracts correctly interpret and use the returned nonce value, especially in the context of zero-based array indexing.
  • 118-121: The _writeEntry internal function's implementation, which updates the global nonce and stores a new LocalEntry, is crucial for the new nonce management system. The use of _entries.length to determine the new nonce and the subsequent push to the _entries array are correctly implemented. Ensure that the event InterchainEntryWritten is emitted with the correct parameters, as this is vital for external observers to track entry creation.
  • 141-141: The emission of the InterchainVerificationRequested event in the _requestVerification internal function is an important aspect of the contract's transparency and auditability. Ensure that the event parameters, especially entry.dbNonce and srcModules, accurately reflect the verification request's details. This event's correct emission is crucial for external systems monitoring verification requests.
packages/contracts-communication/test/InterchainDB.Src.t.sol (14)
  • 16-18: The introduction of INITIAL_DB_NONCE as the sum of INITIAL_WRITER_F and INITIAL_WRITER_S is a clear and logical way to initialize the global nonce based on the initial values for two hypothetical writers. This change aligns with the PR's objective to centralize nonce management.
  • 27-27: The addition of the initialEntries array is a good practice for managing initial entries in a structured manner. This facilitates easier manipulation and access to these entries throughout the test suite.
  • 50-59: The initialWrites function effectively replaces the previous setupWriterNonce function, adapting to the new global nonce system. It initializes entries for two writers based on INITIAL_WRITER_F and INITIAL_WRITER_S, which is a practical approach to simulate initial conditions for testing. However, ensure that the logic correctly reflects the intended behavior in the context of the updated nonce management system.
  • 63-66: The getInitialEntry function provides a straightforward way to retrieve an entry based on its dbNonce. The use of a require statement to check if the dbNonce is within the range of initialEntries is a good practice for input validation, preventing out-of-bounds access.
  • 72-77: The getMockEntry function is well-implemented, creating mock InterchainEntry instances for testing purposes. It correctly assigns the srcChainId, dbNonce, srcWriter, and dataHash, which are essential for simulating realistic test scenarios.
  • 96-98: The writeEntry function simplifies the process of writing an entry and obtaining the resulting dbNonce. Using vm.prank to simulate the action being performed by the specified writer is a clever use of the Foundry testing framework's capabilities.
  • 104-111: The requestVerification function correctly encapsulates the logic for requesting verification of an entry, including dealing with the caller's balance and using vm.prank to simulate the action. This encapsulation improves test readability and maintainability.
  • 121-125: The writeEntryWithVerification function combines the actions of writing an entry and requesting its verification in one step. This is a useful abstraction for tests that need to simulate this combined operation, enhancing test clarity.
  • 137-138: The expectEntryDoesNotExist function is a good example of using Foundry's vm.expectRevert to anticipate specific revert conditions in tests. This is crucial for testing error handling paths in the contract logic.
  • 157-158: The test test_setup_getDBNonce correctly asserts that the initial DB nonce is set as expected. This is an important test to ensure that the initial state of the contract aligns with the expectations set by the new nonce management system.
  • 162-163: The loop in test_setup_getEntry effectively tests the retrieval of initial entries, ensuring that the contract's state matches the expected initial conditions. This thorough approach is commendable for validating the setup phase of the contract.
  • 286-296: The series of tests for requestVerification with reverts due to non-existent entries are comprehensive, covering a range of scenarios including next nonce, huge nonce values, and the maximum possible nonce. This thorough testing is crucial for ensuring robust error handling in the contract.
  • 316-334: The tests for requestVerification with reverts due to incorrect fee amounts are well-structured, covering both underpaid and overpaid scenarios for one module and two modules. These tests are essential for validating the contract's fee handling logic.
  • 339-345: The tests for requestVerification with reverts due to no modules specified and the same chain ID are important for ensuring the contract correctly handles these edge cases. This attention to detail in testing edge cases enhances the contract's reliability.
packages/contracts-communication/test/InterchainDB.Dst.t.sol (10)
  • 4-10: The import statements have been updated to include InterchainEntryLib. This change aligns with the PR objectives to refactor the nonce management system. Ensure that InterchainEntryLib is used appropriately within the test suite for operations related to the new global dbNonce.
  • 41-54: The setUp function has been modified to use the new getMockEntry function signature, which now includes a dbNonce parameter. This change is consistent with the PR's objective to transition to a global nonce system. Ensure that all test entries are correctly initialized with the intended dbNonce values for accurate testing.
  • 63-74: The functions introduceConflictsSameWriter and introduceConflictsDiffWriter have been updated to reflect the new nonce management approach. It's important to verify that these functions accurately simulate the intended conflict scenarios under the new system, especially considering the transition to a global dbNonce.
  • 80-97: The introduction of empty entries in introduceEmptyEntries and introduceEqualEmptyEntries functions is crucial for testing edge cases. Ensure that these functions correctly utilize the new getEmptyEntry signature and that the tests cover scenarios relevant to the global nonce management system.
  • 135-168: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [117-164]

The getMockEntry, getFakeEntry, and getEmptyEntry functions have been updated to include the dbNonce parameter. This change is essential for generating test entries that align with the new nonce management system. Ensure that the data generation functions correctly create entries with the intended nonce values and data hashes.

  • 183-190: The assertCorrectVerificationTime and expectConflictingEntries helper functions are used to assert conditions and expect specific reverts. It's important to ensure that these functions are correctly implemented to accurately test the contract's behavior under various scenarios, especially with the updated nonce management system.
  • 202-218: The test cases, such as test_verifyEntry_new_emitsEvent and test_verifyEntry_existing_diffModule_emitsEvent, have been updated to reflect the new event parameters and the global nonce system. Ensure that the expected events are correctly emitted with the appropriate parameters, including the global dbNonce.
  • 278-296: The test cases that expect reverts, such as test_verifyEntry_revert_conflict_sameModule_sameWriter and test_verifyEntry_revert_sameChainId, are crucial for ensuring the contract behaves as expected under error conditions. Verify that these tests accurately reflect the contract's logic, especially in handling conflicting entries and same chain ID errors under the new nonce system.
  • 305-517: The reading entry test cases, such as test_readEntry_existingA_existingB and test_readEntry_emptyA_emptyB, have been updated to account for the new nonce management system. It's important to ensure that these tests accurately assess the contract's functionality in reading and verifying entries under various scenarios, including conflicts, empty entries, and entries verified by different modules.
  • 525-525: The test_readEntry_revert_sameChainId function tests the contract's behavior when attempting to read an entry from the same chain ID. This test is important for ensuring the contract correctly handles such cases, especially with the updated nonce management system. Verify that the expected revert is correctly triggered.

@ChiTimesChi ChiTimesChi merged commit b344693 into contracts/communication Feb 20, 2024
49 of 51 checks passed
@ChiTimesChi ChiTimesChi deleted the contracts/communication-global-db-nonces branch February 20, 2024 18:38
aureliusbtc pushed a commit that referenced this pull request Feb 20, 2024
* Use dbNonce in `InterchainEntry` struct

* Update InterchainDB: source chain logic

* Update InterchainDB: destination chain logic

* Update InterchainDB tests

* Use `dbNonce` in InterchainClient

* Update the rest of the tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants