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 pending blocks handling #1568

Merged
merged 20 commits into from
Mar 6, 2024
Merged

Refactor pending blocks handling #1568

merged 20 commits into from
Mar 6, 2024

Conversation

tzdybal
Copy link
Member

@tzdybal tzdybal commented Feb 27, 2024

Overview

This PR creates a new test to check pending blocks as described in issue #1548. It mimics the behavior of a node producing blocks, stopping and restarting, then producing more blocks.

Looking at commit history you can check that test was initially failing and then was fixed.

New implementation of pendingBlocks is safer because its data is persisted in store. If node is restarted, block submission to DA will restart when it ended. The worst case scenario (very unlikely), where pendingBlocks can't save information to store, results in resubmission of blocks already submitted to DA, which is only the extra cost. The solution ensures that all the blocks are successfully submitted to DA.

This PR is bigger than I expected, and because of that I will work on #1524 and limiting the number of blocks returned by getPendingBlocks in follow-up PR.

Resolves #1548
Resolves #457

Checklist

  • New and updated code has appropriate documentation
  • New and updated code has new and/or updated testing
  • Required CI checks are passing
  • Visual proof for any user facing features like CLI or documentation updates
  • Linked issues closed with keywords

Summary by CodeRabbit

  • New Features

    • Introduced a new mock generation command for enhanced testing capabilities.
    • Added functionality for improved block management and submission to Data Availability layers.
    • Enhanced metadata management in the store for better data handling and retrieval.
    • Implemented new testing functions and improved existing ones for better coverage and reliability.
  • Bug Fixes

    • Fixed handling of empty block submissions to prevent errors during block publishing.
  • Refactor

    • Refactored block management to improve code efficiency and readability.
    • Consolidated mock application creation logic in integration tests for better maintainability.
  • Tests

    • Expanded testing suite with new tests for block submission, metadata operations, and mock DA interactions.
  • Chores

    • Updated dependencies and imports across various files to enhance functionality and testing.

Copy link
Contributor

coderabbitai bot commented Feb 27, 2024

Warning

Rate Limit Exceeded

@tzdybal has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 3 minutes and 42 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 7b52fa5 and 95c074b.

Walkthrough

This update introduces significant improvements to the handling of pending blocks and metadata management within a blockchain system. By enhancing the atomicity and persistence of block submissions and metadata storage, the system now ensures better data integrity and resilience against node restarts or crashes. The changes span across block management, testing enhancements, and store functionalities, aiming to optimize synchronization and submission processes to the Data Aggregator (DA).

Changes

Files Change Summary
Makefile Added a new mock generation command for DA.
block/manager.go, block/manager_test.go Updated NewManager to include store and logger, refined block handling logic.
block/pending_blocks.go, block/pending_blocks_test.go Enhanced block management with atomic operations, context support, and error handling.
node/full_node_integration_test.go, node/full_node_test.go Improved node testing with mock application creation and simulation of DA submission behavior.
store/store.go, store/types.go, store/store_test.go Enhanced metadata management with new functions and tests for metadata operations.
test/mocks/DA.go Introduced a mock implementation for DA type for testing purposes.

Assessment against linked issues

Objective Addressed Explanation
Ensure pending blocks are not lost during restarts and guarantee submission to DA (#1548)
Maintain order and implement atomic submission of blocks to DA (#1548)
Handle duplicate and out-of-order blocks during sync (#1548)
Recover from a crash post submitBlockToDA and assess system's resilience (#457)
Prevent unnecessary block resubmissions and associated costs (#457)
Address duplicate block submission in submitBlocksToDA (#1472)

Related issues

🐇🎉

In the land of code where blocks align,
A rabbit hopped, making changes fine.
With each leap and bound, the data entwined,
No block left behind, in restart or decline.

"To DA we go, with atomic grace,
No duplicate nor loss, in this digital space."
🌟🐾

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.
    • 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 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 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.

Base automatically changed from tzdybal/closing to main February 27, 2024 14:29
@tzdybal tzdybal force-pushed the tzdybal/fix_pending_blocks branch 3 times, most recently from ab5704e to a8e4179 Compare February 28, 2024 21:58
@tzdybal tzdybal marked this pull request as ready for review February 29, 2024 20:20
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: 5

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between a5c1173 and e1614d1.
Files selected for processing (11)
  • Makefile (1 hunks)
  • block/manager.go (4 hunks)
  • block/manager_test.go (3 hunks)
  • block/pending_blocks.go (1 hunks)
  • block/pending_blocks_test.go (1 hunks)
  • node/full_node_integration_test.go (2 hunks)
  • node/full_node_test.go (2 hunks)
  • store/store.go (3 hunks)
  • store/store_test.go (2 hunks)
  • store/types.go (1 hunks)
  • test/mocks/DA.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • test/mocks/DA.go
Additional comments: 13
block/pending_blocks_test.go (1)
  • 4-90: The modifications in the test file, including the introduction of context handling, the use of new store methods, and the addition of the newPendingBlocks function, significantly improve the robustness and maintainability of the tests. These changes demonstrate good testing practices and effective use of the newly introduced store functionality.

The use of context ensures that the tests are more reliable and can be managed more effectively, especially in asynchronous operations or those that might require cancellation or timeouts. The newPendingBlocks function simplifies the setup process for creating PendingBlocks instances, making the test code cleaner and easier to understand.

Overall, these changes enhance the quality and maintainability of the tests, aligning well with the PR's objectives of improving system reliability and safety.

Makefile (1)
  • 86-86: The addition of a new mock generation command for the DA interface in the Makefile is a valuable enhancement, supporting the PR's objectives related to testing enhancements. This change facilitates the generation of mocks for the DA interface, which is essential for thorough and effective testing of DA interactions.

The use of mockery for generating mocks is consistent with common practices in Go projects, and the command syntax aligns well with the established patterns in the Makefile. This addition underscores the importance of robust testing infrastructure in ensuring the reliability and safety of the system.

block/pending_blocks.go (1)
  • 4-111: The extensive changes in block/pending_blocks.go, including the introduction of new imports, the LastSubmittedHeightKey constant, updates to the PendingBlocks struct, and significant modifications to methods, significantly enhance the handling and persistence of pending blocks. These changes align well with the PR's objectives of improving system reliability and safety, especially in scenarios involving node restarts or unexpected failures.

The use of context handling and error management in methods like getPendingBlocks improves the robustness and reliability of the pending blocks handling. The introduction of the LastSubmittedHeightKey constant for persisting the last submitted height is a clear and maintainable approach. Modifications to methods such as setLastSubmittedHeight and loadFromStore demonstrate careful consideration of atomicity and error handling, which are crucial for the reliability of the system.

These changes enhance the reliability, maintainability, and robustness of the pending blocks handling, making the system more resilient to failures and restarts. Minor refinements could be made for clarity and consistency, but overall, the modifications are well-conceived and effectively implemented.

store/store_test.go (1)
  • 210-244: The addition of the TestMetadata function in store/store_test.go is a valuable enhancement, thoroughly testing the new metadata operations (SetMetadata and GetMetadata) added to the store. This test verifies the functionality of setting and retrieving arbitrary metadata using an in-memory key-value store, ensuring that the metadata operations work as expected.

The test is well-structured and covers basic scenarios, including setting and retrieving metadata and handling non-existent metadata keys. This coverage is crucial for verifying the correct behavior and error handling of the metadata operations.

The addition of this test enhances the robustness of the store's testing suite, ensuring that the new functionality is correctly implemented and behaves as expected. It might be beneficial to consider adding more test cases if there are specific edge cases or error conditions that are not covered by the current tests.

block/manager_test.go (1)
  • 190-229: > 📝 NOTE

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

The modifications in block/manager_test.go, including the addition of the getTempKVStore function and enhancements to the TestSubmitBlocksToDA function, significantly improve the testing of block submission to the DA layer. These changes address the need for temporary on-disk key-value stores in testing and demonstrate a comprehensive approach to verifying the functionality and reliability of block submission.

The getTempKVStore function is a practical solution for creating temporary on-disk stores, effectively addressing the limitations of the in-memory KV store for certain test scenarios. The modifications to the TestSubmitBlocksToDA function, including the handling of temporary on-disk stores, saving blocks, updating height, and checking metadata, enhance the test coverage and robustness.

These changes contribute to the overall quality and reliability of the system by ensuring that block submission to the DA layer is thoroughly tested. It's important to ensure that temporary on-disk stores are properly cleaned up after tests to avoid potential resource leaks.

store/store.go (2)
  • 25-25: The addition of the metaPrefix constant is a straightforward and necessary change for distinguishing metadata keys from other data types in the store. This is a good practice for namespace separation in key-value stores.
  • 256-258: The getMetaKey function correctly generates keys for metadata by combining the metaPrefix with the user-provided key. This encapsulation of key generation logic is a good practice for maintainability and consistency.
node/full_node_test.go (2)
  • 6-20: The addition of new imports, including crypto/sha256, errors, os, strconv, and others, is necessary for the new test functionality introduced in this file. These imports are correctly placed and follow Go's convention of grouping standard library imports separately from third-party packages.
  • 253-291: The modifications to the createAggregatorWithPersistence function to include additional parameters and setup logic for the TestPendingBlocks test are appropriate. This function now supports creating a node with a persistent database and a configurable DA client, which is crucial for testing the persistence and submission behavior. Ensure that the error handling within this function is robust, especially around resource creation and initialization steps, to prevent resource leaks or unclear test failures.
node/full_node_integration_test.go (2)
  • 512-520: The addition of the getMockApplication function centralizes the creation of a mock application, which is a good practice for reducing code duplication and improving readability. This change effectively consolidates the initialization logic for the mock application used in various tests, making the test setup more maintainable and easier to understand.
  • 627-627: The replacement of direct mock application creation with the getMockApplication function call in the createNode function is a positive change. It ensures consistency in how mock applications are initialized across different tests and reduces the potential for errors or inconsistencies in the mock setup. This change enhances the maintainability and readability of the test code.
block/manager.go (2)
  • 214-214: The addition of store and logger parameters to NewPendingBlocks in the NewManager function is a positive change. It ensures that the PendingBlocks instance has access to necessary resources for persistence and logging, aligning with the PR's objectives to enhance reliability and safety in block submission.
  • 854-858: Setting the last submitted height after successfully submitting blocks to the DA layer is a crucial step for maintaining the integrity of the submission process. This change ensures that the system can accurately track the progress of block submissions, which is essential for resuming operations after a restart or failure. The logic to determine the last submitted height based on the last block in the submittedBlocks slice is correct and efficiently implemented.

block/manager.go Outdated Show resolved Hide resolved
store/store.go Outdated Show resolved Hide resolved
store/store.go Outdated Show resolved Hide resolved
store/types.go 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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e1614d1 and 0147381.
Files selected for processing (1)
  • store/store.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • store/store.go

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: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between bec9ad0 and 78883cd.
Files selected for processing (11)
  • Makefile (1 hunks)
  • block/manager.go (4 hunks)
  • block/manager_test.go (4 hunks)
  • block/pending_blocks.go (1 hunks)
  • block/pending_blocks_test.go (1 hunks)
  • node/full_node_integration_test.go (2 hunks)
  • node/full_node_test.go (2 hunks)
  • store/store.go (3 hunks)
  • store/store_test.go (2 hunks)
  • store/types.go (1 hunks)
  • test/mocks/DA.go (1 hunks)
Files skipped from review as they are similar to previous changes (7)
  • Makefile
  • block/manager_test.go
  • node/full_node_integration_test.go
  • store/store.go
  • store/store_test.go
  • store/types.go
  • test/mocks/DA.go
Additional comments: 20
block/pending_blocks_test.go (7)
  • 4-11: The addition of imports for context, test, and store packages aligns with the changes made in the test functions to use context and the new store functionality. This ensures that the tests are up-to-date with the latest codebase changes.
  • 19-25: The test TestGetPendingBlocks now initializes a context and uses it in calls to SaveBlock and SetHeight. This is a good practice as it allows for better control over these operations, especially in a testing environment where you might want to simulate cancellations or timeouts.
  • 33-40: In TestRemoveSubmittedBlocks, the use of context and the newPendingBlocks helper function to create a PendingBlocks instance is consistent with the changes in other test functions. This consistency is important for maintainability and readability of the test code.
  • 46-58: The test TestRemoveSubsetOfBlocks correctly uses context and the newPendingBlocks helper. Additionally, it tests the removal of a subset of blocks, verifying the remaining blocks' heights. This test is crucial for ensuring the correct behavior of the PendingBlocks functionality when only a subset of blocks needs to be removed.
  • 64-73: TestRemoveAllBlocksAndVerifyEmpty checks the behavior of removing all blocks and verifying that the PendingBlocks instance is empty afterward. The use of context and the newPendingBlocks helper function here is appropriate and follows the pattern established in other tests.
  • 79-82: The test TestRemoveBlocksFromEmptyPendingBlocks ensures that attempting to remove blocks from an already empty PendingBlocks instance does not cause a panic. This is an important edge case to test for, ensuring the robustness of the PendingBlocks implementation.
  • 86-90: The newPendingBlocks helper function is a significant addition, simplifying the creation of a PendingBlocks instance with a store and logger for testing purposes. This function improves the readability and maintainability of the test code by abstracting the setup process.
block/pending_blocks.go (8)
  • 4-13: The addition of imports for context, errors, fmt, strconv, atomic, and various packages from the project and third-party libraries is necessary to support the updated functionality in PendingBlocks. These imports are correctly placed and used throughout the file.
  • 17-31: The introduction of the LastSubmittedHeightKey constant for persisting the last submitted height is a good practice. It avoids hardcoding the key in multiple places, making the code more maintainable and less error-prone.
  • 33-37: The update to the PendingBlocks struct to include a store, logger, and lastSubmittedHeight using the atomic.Uint64 type is crucial for thread-safe operations and better logging. This change supports the objectives of making the block submission process more reliable and safe.
  • 41-44: The NewPendingBlocks constructor now correctly initializes a PendingBlocks instance with the provided store and logger. This change is necessary for the refactored implementation that relies on these components.
  • 50-78: The getPendingBlocks method has been significantly refactored to use context and handle errors more gracefully. The use of atomic operations for reading the lastSubmittedHeight and the detailed error handling improve the method's robustness and reliability.
  • 82-82: The isEmpty method's update to compare the store's height with the lastSubmittedHeight using atomic operations ensures thread safety and correctness in determining if there are any pending blocks.
  • 85-93: The setLastSubmittedHeight method's refactoring to use atomic compare-and-swap operations and to persist the new height in the store is a significant improvement. It ensures thread safety and data persistence, aligning with the PR's objectives to enhance reliability.
  • 96-111: The loadFromStore method for loading the last submitted height from the store is a new addition that supports the persistence objectives of this PR. It handles errors appropriately and uses atomic operations, ensuring the method's reliability and safety.
node/full_node_test.go (2)
  • 6-20: The addition of imports for crypto/sha256, errors, os, strconv, and various packages from the project and third-party libraries supports the new test functionality introduced in this file. These imports are correctly placed and used throughout the file.
  • 256-294: The createAggregatorWithPersistence function has been updated to include additional parameters and setup logic for the test. This update is necessary to simulate the DA submission behavior accurately. The function is well-structured and correctly sets up the necessary components for the test environment.
block/manager.go (3)
  • 222-222: The addition of store and logger parameters to NewPendingBlocks in the NewManager function aligns with the PR's objective to enhance the reliability and safety of block submission. This change enables PendingBlocks to interact with persistent storage and logging, which is crucial for managing the state of pending blocks, especially across node restarts. This modification supports the persistence of pendingBlocks data, addressing one of the key objectives of the PR.
  • 840-846: The error handling logic in submitBlocksToDA has been updated to address a previously identified issue where errors from getPendingBlocks could potentially lead to unintended behavior. The revised logic now correctly handles scenarios where getPendingBlocks returns an empty slice of blocks to submit along with an error. By checking for an error before processing the length of blocksToSubmit, this change ensures that errors are appropriately logged and the function returns early in error scenarios, preventing further execution. This improvement aligns with the PR's objective to ensure a more robust implementation and safer block submission process.
  • 869-873: The logic for setting the last submitted height after successfully submitting blocks to the DA layer has been updated. This change is crucial for maintaining the integrity of the block submission process, ensuring that the system accurately tracks the height of the last block submitted to the DA layer. By updating the last submitted height only after successful submission, this modification helps prevent potential issues related to block re-submission or data loss, especially in scenarios where node restarts or failures occur. This aligns with the PR's objectives of enhancing reliability and prioritizing data integrity.

block/pending_blocks.go Show resolved Hide resolved
block/manager.go Outdated Show resolved Hide resolved
block/manager.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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 78883cd and aaf9b83.
Files selected for processing (1)
  • block/manager.go (4 hunks)
Additional comments: 2
block/manager.go (2)
  • 222-222: The addition of store and logger parameters to NewPendingBlocks in the NewManager function aligns with the PR's objective of enhancing the reliability and safety of block submission. This change enables the PendingBlocks structure to interact with persistent storage and logging, which is crucial for the persistence and robustness improvements mentioned.
  • 876-880: Setting the last submitted height after successful submission of blocks to the DA layer is a critical step for ensuring the system can track progress and potentially resume operations after interruptions. This change supports the PR's goal of improving the reliability of block submission by making the process more resilient to failures and restarts. It's important to ensure that the setLastSubmittedHeight function correctly handles all edge cases, especially regarding the persistence of this state.

block/manager.go Show resolved Hide resolved
Copy link
Contributor

@MSevey MSevey left a comment

Choose a reason for hiding this comment

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

Great start 👏

block/pending_blocks.go Show resolved Hide resolved
block/pending_blocks.go Outdated Show resolved Hide resolved
block/pending_blocks.go Outdated Show resolved Hide resolved
block/pending_blocks.go Show resolved Hide resolved
block/pending_blocks.go Outdated Show resolved Hide resolved
store/store_test.go Outdated Show resolved Hide resolved
block/pending_blocks_test.go Outdated Show resolved Hide resolved
block/pending_blocks.go Show resolved Hide resolved
block/pending_blocks_test.go Outdated Show resolved Hide resolved
block/pending_blocks_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.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between aaf9b83 and c91ce45.
Files selected for processing (4)
  • block/manager.go (5 hunks)
  • block/manager_test.go (4 hunks)
  • block/pending_blocks.go (1 hunks)
  • block/pending_blocks_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • block/manager.go
  • block/manager_test.go
Additional comments: 11
block/pending_blocks_test.go (6)
  • 4-11: Imports have been correctly updated to include necessary packages for the context and the new store functionality. This aligns with the PR objectives of enhancing reliability and safety in block submission by utilizing context for operations and integrating with a store for persistence.
  • 33-40: The test TestRemoveSubmittedBlocks correctly sets up a context and uses the newPendingBlocks function. It tests the removal of submitted blocks and checks if the PendingBlocks instance is empty afterward. This test aligns with the PR's objective of ensuring data integrity and robust handling of block submissions.
  • 46-58: In TestRemoveSubsetOfBlocks, the setup and execution are correct, and it properly tests the removal of a subset of blocks. The test ensures that only the expected blocks remain, which is crucial for the integrity of block handling.
  • 64-73: TestRemoveAllBlocksAndVerifyEmpty correctly tests the removal of all blocks and verifies that the PendingBlocks instance is empty afterward. This test is essential for ensuring the system can correctly handle scenarios where all pending blocks need to be removed, such as after successful submissions.
  • 79-82: TestRemoveBlocksFromEmptyPendingBlocks tests the system's behavior when attempting to remove blocks from an already empty PendingBlocks instance. This test ensures that such an operation does not cause a panic, aligning with the goal of robust and safe error handling.
  • 86-92: The newPendingBlocks function correctly sets up a new PendingBlocks instance for testing, utilizing an in-memory key-value store and a test logger. This function is a good practice for reducing boilerplate code in tests and ensuring consistent test setup.
block/pending_blocks.go (5)
  • 4-13: The updated imports correctly include necessary packages for context handling, error management, atomic operations, and logging. This aligns with the PR's objectives of enhancing the reliability and safety of block submission by utilizing context for operations and atomic operations for thread-safe updates.
  • 17-31: The documentation for the PendingBlocks struct has been updated to reflect the new design and assumptions. It clearly outlines the importance of safe storage, ordered submission, and atomicity in DA submission. The TODO comment about not pushing all pending blocks at once indicates future work for optimizing block submission, which is a good practice for maintaining focus on the current PR objectives while planning for future improvements.
  • 41-49: The NewPendingBlocks constructor has been correctly updated to accept a store and logger, aligning with the PR's objectives of utilizing a store for persistence and logging for debugging. The inclusion of the init method call ensures that the PendingBlocks instance is properly initialized with the last submitted height from the store.
  • 79-79: The isEmpty method correctly compares the store height with the last submitted height to determine if there are any pending blocks. This method is essential for ensuring the correct handling of block submission states.
  • 93-108: The init method correctly loads the last submitted height from the store, handling the case where the key is not found as a special non-error case. This method is crucial for ensuring the PendingBlocks instance is correctly initialized with persistent state information.

block/pending_blocks_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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c91ce45 and 57290db.
Files selected for processing (1)
  • store/store_test.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • store/store_test.go

TestPendingBlocks should fail to proof the existence of the bug.

This commit introduces a new test to check pending blocks as described in issue #1548. It mimics the behavior of a node producing blocks, stopping and restarting, then producing more blocks.
Pending blocks are retrieved directly from store. Next step is to save latest submitted block height to store (store interface needs to be modified).

Tests are expected to fail.
Two new methods have been added to the store interface: SetMetadata and GetMetadata. These methods allow for the storage and retrieval of arbitrary data associated with a specified key. Demonstration of usage and functionality is provided through additional tests in store_test.go.
tests are improved to reflect added assertions (block submission always in order) and code changes (in memory kv couldn't handle big values O_o)
As PendingBlocks depend on store, it is safe to remove addPendingBlock function - all tests were updated to save blocks to store instead of calling addPendingBlock.
setLastSubmittedHeight now accepts only height, and the name better reflects it's
The "TestPendingBlocks" test function was modified to provide more specific testing for block submissions. The test now verifies that blocks from the first and second runs are submitted in order and that this information is persisted in the store. Additionally, the test scenario has been detailed for clarity.
functionally they are the same, but unified into neat tabular test
@tzdybal tzdybal force-pushed the tzdybal/fix_pending_blocks branch from 0aee217 to 7b52fa5 Compare March 6, 2024 22:02
@tzdybal tzdybal requested a review from MSevey March 6, 2024 22:02
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 57290db and 0aee217.
Files selected for processing (1)
  • block/pending_blocks_test.go (1 hunks)
Additional comments: 2
block/pending_blocks_test.go (2)
  • 4-4: The addition of context, store, and test/log imports aligns well with the PR's objectives to enhance the handling of pending blocks and improve testing. These imports are necessary for the new testing logic and the handling of pending blocks, providing context management, storage interaction, and logging capabilities.

Also applies to: 11-12

  • 21-102: The modifications to the test functions and the introduction of the newPendingBlocks function are well-aligned with the PR's objectives to enhance the handling of pending blocks. Utilizing context in tests is a best practice, and the newPendingBlocks function simplifies setup, making tests cleaner and more maintainable. However, it would be beneficial to add more detailed comments explaining the purpose and expected outcomes of each test case for better maintainability and understanding.

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: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7ecbb74 and 7b52fa5.
Files selected for processing (11)
  • Makefile (1 hunks)
  • block/manager.go (5 hunks)
  • block/manager_test.go (4 hunks)
  • block/pending_blocks.go (1 hunks)
  • block/pending_blocks_test.go (1 hunks)
  • node/full_node_integration_test.go (2 hunks)
  • node/full_node_test.go (2 hunks)
  • store/store.go (3 hunks)
  • store/store_test.go (2 hunks)
  • store/types.go (1 hunks)
  • test/mocks/DA.go (1 hunks)
Files skipped from review as they are similar to previous changes (8)
  • Makefile
  • block/manager.go
  • block/manager_test.go
  • node/full_node_integration_test.go
  • store/store.go
  • store/store_test.go
  • store/types.go
  • test/mocks/DA.go
Additional comments: 15
block/pending_blocks_test.go (6)
  • 4-4: The addition of the context package is appropriate given the modifications in the test functions to include context as a parameter. This aligns with best practices for handling cancellations, timeouts, and passing request-scoped values.
  • 11-12: The addition of imports for the store and test packages is necessary for the new test setup, where a store is used for persistence and test for logging. This change supports the enhancements made to the testing of PendingBlocks.
  • 21-76: The refactoring of test cases to use context and the new newPendingBlocks helper function improves the readability and maintainability of the tests. By passing context, the tests now support cancellation and timeouts, which is a good practice. The newPendingBlocks function encapsulates the creation of PendingBlocks instances, making the test setup cleaner and more consistent.
  • 80-85: The newPendingBlocks function is a helpful addition that simplifies the creation of PendingBlocks instances for testing. It ensures that each test starts with a fresh instance, reducing the risk of tests affecting each other. This function also abstracts away the setup details, making the tests easier to read and maintain.
  • 88-102: The fillWithBlocks function correctly simulates the addition of blocks to the PendingBlocks instance. It uses the store.SaveBlock method to persist blocks and updates the store's height accordingly. This setup is crucial for testing the behavior of PendingBlocks under different conditions, such as having a varying number of pending blocks.
  • 95-102: The checkRequirements function is well-designed to verify the state of PendingBlocks after test execution. It checks if the number of pending blocks matches expectations and ensures that the blocks are sorted by height. This function is an essential part of the test suite, as it validates the core functionality of PendingBlocks.
block/pending_blocks.go (6)
  • 4-8: The addition of the context, errors, fmt, strconv, and sync/atomic packages supports the new functionality introduced in this file. These imports are necessary for handling context, error formatting, string conversion, and atomic operations, respectively.
  • 10-13: The addition of imports for the datastore, store, and log packages is necessary for the new implementation of PendingBlocks. These changes support data persistence, logging, and interaction with the underlying datastore.
  • 17-31: The introduction of the LastSubmittedHeightKey constant and the updated documentation for the PendingBlocks struct provide clarity on the purpose and behavior of the pending blocks management system. The documentation outlines important assertions and scenarios, which is beneficial for maintainability and understanding the system's design.
  • 33-37: The update to the PendingBlocks struct to include store and logger fields, along with the lastSubmittedHeight atomic variable, is a significant improvement. These changes enable better data persistence, logging, and thread-safe operations, aligning with the objectives of enhancing the handling of pending blocks.
  • 41-49: The NewPendingBlocks constructor function has been updated to accept store and logger parameters, which is a positive change. It ensures that instances of PendingBlocks are initialized with the necessary dependencies for data persistence and logging. The error handling in the initialization process is also a good practice.
  • 93-108: The init method is a crucial addition for loading the last submitted height from the store during initialization. This method ensures that the PendingBlocks instance is correctly synchronized with the persisted state, which is essential for the reliability of the system. The error handling and special case handling for ds.ErrNotFound are well-implemented.
node/full_node_test.go (3)
  • 6-7: The addition of imports for crypto/sha256 and errors is necessary for the new test TestPendingBlocks, which involves hashing and error handling. These changes support the test's objectives of simulating DA submission behavior.
  • 9-10: The addition of imports for os and strconv is required for handling file operations and string conversion in the new test setup. These changes are relevant for creating temporary directories and parsing numeric values from strings.
  • 14-20: The addition of imports for various packages, including cmconfig, proxy, goDA, and test, supports the enhanced test setup and the new TestPendingBlocks function. These changes enable configuration management, proxy creation, interaction with a mock DA, and logging within the test environment.

block/pending_blocks.go Show resolved Hide resolved
block/pending_blocks.go Outdated Show resolved Hide resolved
node/full_node_test.go Show resolved Hide resolved
Add context as parameter and add comment on error handling.
@tzdybal tzdybal added this pull request to the merge queue Mar 6, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 6, 2024
@MSevey MSevey added this pull request to the merge queue Mar 6, 2024
Merged via the queue into main with commit 9cbe7dc Mar 6, 2024
18 checks passed
@MSevey MSevey deleted the tzdybal/fix_pending_blocks branch March 6, 2024 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
3 participants