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/logic vfs #678

Merged
merged 8 commits into from
Jun 20, 2024
Merged

Refactor/logic vfs #678

merged 8 commits into from
Jun 20, 2024

Conversation

ccamel
Copy link
Member

@ccamel ccamel commented Jun 17, 2024

By this PR, I’ve taken the liberty of refactoring a bit the Virtual File System of the logic module to make it more coherent and consistent, and to enhance its extensibility to other types of virtual file systems (for instance, a memory-based VFS could facilitate certain operations with Prolog streams - subject to evaluation).

So we now have the following vfs:

vfs
 ├── composite    VFS composed of multiple other VFS, which it resolves based on their URI schemes (it was previously implemented by the `router`)
 ├── filtered     VFS wrapper around another VFS, controlling access to specific paths.
 └── wasm         VFS capable of "reading" data from a WASM contract.

I'd like to say that although it might be tempting to rely on existing VFS implementations (such as C2FO/vfs,
mandelsoft/vfs or spf13/afero), this refactoring builds on what already exists and offers the simplest possible implementation, focused specifically on the needs of the logic module.

Note: the PR introduces no breaking changes; it only refines some error messages, and the tests have been updated accordingly.

Summary by CodeRabbit

  • New Features

    • Introduced a composite virtual file system with enhanced capabilities.
    • Added support for WebAssembly (Wasm) file systems.
  • Bug Fixes

    • Improved path validation and error handling in the file system.
  • Tests

    • Added new test suites for composite and filtered file systems.
    • Provided mock implementations for file system interfaces for thorough testing.
  • Chores

    • Updated import statements and refactored function calls for consistency with new file systems.
    • Generated mock interfaces for testing purposes using MockGen.

@ccamel ccamel self-assigned this Jun 17, 2024
Copy link
Contributor

coderabbitai bot commented Jun 17, 2024

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

The overall changes introduce a structured way to handle virtual file systems within the project. New functionalities include creating composite and filtered file systems, as well as specific file systems for WebAssembly environments. The modifications enhance the ability to manage and interact with these diverse file systems seamlessly, incorporating path validation, mounting logic, and improved testing utilities.

Changes

Files/Group Summary
app/app.go Updated provideFS to create and mount composite and Wasm file systems.
x/logic/fs/composite/fs.go Introduced composite filesystem interface and implementation for virtual file systems.
x/logic/fs/filtered/fs.go Added filtered filesystem interface for path-whitelisting and blacklisting functionalities.
x/logic/fs/provider.go Introduced a Provider type to derive filesystems from contexts.
x/logic/fs/wasm/file.go Defined structures and methods for virtual Wasm files and their metadata.
x/logic/fs/wasm/fs.go Implemented a filesystem to interact with data from a Wasm contract.
x/logic/keeper/features_test.go Updated tests to utilize the new composite and Wasm filesystems.
x/logic/keeper/interpreter.go and x/logic/keeper/keeper.go Modified imports, function calls, and variable types to align with new FS interfaces.
Makefile Added mock generation for FS and ReadFileFS interfaces.
x/logic/testutil/fs_mocks.go and read_file_fs_mocks.go Updated and added mock implementations for filesystem methods.
x/logic/fs/composite/fs_test.go and x/logic/fs/filtered/fs_test.go Added test cases for composite and filtered filesystems.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant Composite as Composite FS
    participant Wasm as Wasm FS

    App->>Composite: Initialize Composite FS
    Composite->>Composite: Mount Wasm FS
    App->>Wasm: Create Wasm FS instance
    Wasm-->>Composite: Wasm FS instance mounted

    App->>Composite: Open file
    Composite->>Composite: Validate and resolve path
    Composite->>Wasm: Open file in Wasm FS
    Wasm-->>Composite: File content
    Composite-->>App: Return file content

    App->>Composite: List mounts
    Composite-->>App: Return mount points
Loading

Poem

In lands of code where bytes do spawn,
A rabbit hops from dusk to dawn,
New filesystems rise, oh what a sight!
With Wasm's glow and composite might.
Filters guard, paths they weave,
In this realm no file shall deceive.
🐇✨ A coder's dream in lines of gleam.


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.

@ccamel ccamel force-pushed the refactor/logic-vfs branch from b806eae to 22e4f6b Compare June 17, 2024 13:12
Copy link

codecov bot commented Jun 17, 2024

Codecov Report

Attention: Patch coverage is 98.90110% with 2 lines in your changes missing coverage. Please review.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #678      +/-   ##
==========================================
+ Coverage   51.98%   54.35%   +2.36%     
==========================================
  Files          75       74       -1     
  Lines        2824     2896      +72     
==========================================
+ Hits         1468     1574     +106     
+ Misses       1260     1226      -34     
  Partials       96       96              
Files Coverage Δ
x/logic/fs/filtered/fs.go 100.00% <100.00%> (ø)
x/logic/fs/wasm/file.go 100.00% <100.00%> (ø)
x/logic/fs/wasm/fs.go 100.00% <100.00%> (ø)
x/logic/keeper/interpreter.go 94.04% <100.00%> (ø)
x/logic/keeper/keeper.go 60.00% <ø> (ø)
x/logic/fs/composite/fs.go 96.22% <96.22%> (ø)

@ccamel ccamel marked this pull request as ready for review June 17, 2024 13: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: 4

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d27bb31 and 22e4f6b.

Files selected for processing (11)
  • app/app.go (2 hunks)
  • x/logic/fs/composite/fs.go (1 hunks)
  • x/logic/fs/filtered/fs.go (1 hunks)
  • x/logic/fs/filtered/fs_test.go (4 hunks)
  • x/logic/fs/provider.go (1 hunks)
  • x/logic/fs/wasm/file.go (1 hunks)
  • x/logic/fs/wasm/fs.go (1 hunks)
  • x/logic/fs/wasm/fs_test.go (4 hunks)
  • x/logic/keeper/features_test.go (2 hunks)
  • x/logic/keeper/interpreter.go (2 hunks)
  • x/logic/keeper/keeper.go (3 hunks)
Additional context used
GitHub Check: codecov/patch
x/logic/fs/wasm/file.go

[warning] 37-38: x/logic/fs/wasm/file.go#L37-L38
Added lines #L37 - L38 were not covered by tests


[warning] 45-46: x/logic/fs/wasm/file.go#L45-L46
Added lines #L45 - L46 were not covered by tests


[warning] 49-50: x/logic/fs/wasm/file.go#L49-L50
Added lines #L49 - L50 were not covered by tests


[warning] 53-54: x/logic/fs/wasm/file.go#L53-L54
Added lines #L53 - L54 were not covered by tests


[warning] 57-58: x/logic/fs/wasm/file.go#L57-L58
Added lines #L57 - L58 were not covered by tests

x/logic/fs/filtered/fs.go

[warning] 34-36: x/logic/fs/filtered/fs.go#L34-L36
Added lines #L34 - L36 were not covered by tests


[warning] 39-40: x/logic/fs/filtered/fs.go#L39-L40
Added lines #L39 - L40 were not covered by tests


[warning] 43-43: x/logic/fs/filtered/fs.go#L43
Added line #L43 was not covered by tests

x/logic/fs/wasm/fs.go

[warning] 54-55: x/logic/fs/wasm/fs.go#L54-L55
Added lines #L54 - L55 were not covered by tests


[warning] 67-70: x/logic/fs/wasm/fs.go#L67-L70
Added lines #L67 - L70 were not covered by tests


[warning] 102-103: x/logic/fs/wasm/fs.go#L102-L103
Added lines #L102 - L103 were not covered by tests

Additional comments not posted (25)
x/logic/fs/provider.go (1)

1-9: The definition of Provider as a type alias for a function returning a fs.FS object is clear and well-implemented.

x/logic/fs/wasm/file.go (1)

9-12: The structure definitions for file and fileInfo are well-defined and appropriate for the intended functionality.

Also applies to: 14-18

x/logic/keeper/keeper.go (1)

Line range hint 12-26: The integration of the fs.Provider in the Keeper struct and its constructor function is correctly implemented. This change supports the PR's goal of enhancing the filesystem's extensibility.

Also applies to: 37-37

Verification successful

The integration of fs.Provider into the Keeper struct is evident, and its usage spans several files within the keeper module. To ensure correctness, we need to verify its initialization and usage across the module.

Let's proceed by checking the constructor of the Keeper struct to confirm the correct initialization of fsProvider, and then we will verify its usage in methods and tests.


The fsProvider is correctly integrated into the Keeper struct and initialized in the NewKeeper constructor. Its usage is evident in both the implementation and test files, indicating consistent application across the module.

  • x/logic/keeper/keeper.go: Initialization of fsProvider in the NewKeeper constructor and its inclusion in the Keeper struct.
  • x/logic/keeper/interpreter.go: Usage of fsProvider in the interpreter.WithFS method.
  • Various test files (msg_server_test.go, grpc_query_params_test.go, grpc_query_ask_test.go): Mocking and usage of fsProvider.

The integration appears to be thorough and aligns with the intended functionality.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify integration of fs.Provider in Keeper and its usage across the module.

# Test: Search for usage of `fsProvider` in the keeper module. Expect: Correct usage and initialization.
rg --type go $'fsProvider' 'x/logic/keeper'

Length of output: 756



Script:

#!/bin/bash
# Description: Verify the initialization of fsProvider in the Keeper constructor and its usage in methods.

# Test: Search for the Keeper constructor to check fsProvider initialization.
rg --type go 'func NewKeeper' 'x/logic/keeper'

# Test: Search for methods in the keeper module that use fsProvider.
rg --type go 'fsProvider' 'x/logic/keeper'

Length of output: 844

x/logic/fs/filtered/fs.go (1)

10-14: The implementation of the vfs struct and the NewFS function is well-executed. The use of whitelists and blacklists to filter file access is a robust feature for security.

Also applies to: 21-25

x/logic/fs/composite/fs.go (7)

32-34: Constructor method NewFS correctly initializes a vfs instance.


36-47: The Open method correctly handles path validation and resolution errors.


49-76: The ReadFile method appropriately handles both ReadFileFS and regular FS types, ensuring compatibility.


78-80: The Mount method effectively assigns a filesystem to a mount point.


82-87: Method ListMounts uses efficient library functions to return sorted mount points.


89-99: The validatePath method correctly validates URLs and ensures they contain a scheme.


101-108: The resolve method accurately retrieves filesystems based on URI schemes, handling errors appropriately.

x/logic/fs/filtered/fs_test.go (2)

79-81: The DoAndReturn method correctly simulates the Open operation using a mocked filesystem.


86-90: The NewFS method correctly initializes a filtered filesystem with URL-based whitelists and blacklists.

x/logic/fs/wasm/fs.go (5)

38-42: The NewFS method initializes the WASM filesystem with appropriate context and keeper.


44-52: Method Open correctly retrieves data and creates a virtual file.


58-96: The readFile method effectively handles complex path parsing and data retrieval from a WASM contract.

Tools
GitHub Check: codecov/patch

[warning] 67-70: x/logic/fs/wasm/fs.go#L67-L70
Added lines #L67 - L70 were not covered by tests


98-153: The parsePath method thoroughly validates and decomposes the URI into components, handling various errors.

Tools
GitHub Check: codecov/patch

[warning] 102-103: x/logic/fs/wasm/fs.go#L102-L103
Added lines #L102 - L103 were not covered by tests


54-56: Ensure the readFile method is covered by tests as it is critical for reading files.

Tools
GitHub Check: codecov/patch

[warning] 54-55: x/logic/fs/wasm/fs.go#L54-L55
Added lines #L54 - L55 were not covered by tests

x/logic/keeper/interpreter.go (1)

142-142: The WithFS method correctly sets up the filesystem with whitelisted and blacklisted URLs.

x/logic/keeper/features_test.go (2)

35-36: The new imports for the composite and wasm packages reflect the changes in the virtual file system as described in the PR summary.


300-303: The implementation of the Provider function now constructs a composite file system and mounts a WASM file system, which aligns with the PR's objectives to enhance modularity and extensibility in the VFS handling.

x/logic/fs/wasm/fs_test.go (2)

Line range hint 31-121: This test case setup for the TestWasmHandler function is comprehensive, covering various scenarios including different data, URIs, and expected results. The error messages are well-formed and provide clear indications of the expected failures, which improves debugging and maintainability.


140-143: The test for the Open method in the wasm file system handler is well-structured. It effectively tests both the error and success paths, ensuring that the file system behavior aligns with the expected outcomes based on the URI provided.

app/app.go (2)

142-143: Import statements for new VFS modules are correctly added.


1165-1169: The provideFS function correctly constructs and configures the virtual file system as described in the PR. Ensure that the wasm2.NewFS method properly handles the context and WasmKeeper as these are critical for its operation.

x/logic/fs/composite/fs.go Outdated Show resolved Hide resolved
x/logic/fs/filtered/fs_test.go Show resolved Hide resolved
x/logic/fs/wasm/file.go Show resolved Hide resolved
x/logic/fs/filtered/fs.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.

Actionable comments posted: 5

Outside diff range and nitpick comments (2)
x/logic/fs/composite/fs_test.go (1)

18-23: Define fileSpec struct more clearly.

Consider adding a comment above the fileSpec struct to explain its purpose and usage within the test suite. This can improve code readability and maintainability.

Makefile (1)

Line range hint 259-259: Address potential security issue with hardcoded API key.

There is a potential security risk due to a hardcoded API key. Consider removing or encrypting this key, or fetching it from a secure environment variable.

- MNEMONIC_VALIDATOR="island position immense mom cross enemy grab little deputy tray hungry detect state helmet tomorrow trap expect admit inhale present vault reveal scene atom"
+ MNEMONIC_VALIDATOR=$(getenv "MNEMONIC_VALIDATOR")
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 22e4f6b and 4aba324.

Files selected for processing (9)
  • Makefile (1 hunks)
  • x/logic/fs/composite/fs.go (1 hunks)
  • x/logic/fs/composite/fs_test.go (1 hunks)
  • x/logic/fs/filtered/fs.go (1 hunks)
  • x/logic/fs/filtered/fs_test.go (4 hunks)
  • x/logic/fs/wasm/fs.go (1 hunks)
  • x/logic/fs/wasm/fs_test.go (4 hunks)
  • x/logic/testutil/fs_mocks.go (2 hunks)
  • x/logic/testutil/read_file_fs_mocks.go (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • x/logic/fs/filtered/fs.go
  • x/logic/fs/filtered/fs_test.go
  • x/logic/fs/wasm/fs_test.go
Additional context used
Learnings (1)
x/logic/fs/wasm/fs.go (1)
User: ccamel
PR: axone-protocol/axoned#678
File: x/logic/fs/wasm/file.go:37-38
Timestamp: 2024-06-17T19:59:54.055Z
Learning: The test coverage for methods `Name`, `Mode`, `ModTime`, `IsDir`, and `Sys` in the `wasm` package of the `logic` module has been significantly improved by the user ccamel.
GitHub Check: codecov/patch
x/logic/fs/composite/fs.go

[warning] 78-78: x/logic/fs/composite/fs.go#L78
Added line #L78 was not covered by tests


[warning] 123-123: x/logic/fs/composite/fs.go#L123
Added line #L123 was not covered by tests

x/logic/fs/wasm/fs.go

[warning] 67-70: x/logic/fs/wasm/fs.go#L67-L70
Added lines #L67 - L70 were not covered by tests

Gitleaks
Makefile

259-259: Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (generic-api-key)

Additional comments not posted (13)
x/logic/testutil/fs_mocks.go (2)

38-40: The implementation of the Open method in the mock looks correct, adhering to the expected behavior of returning a file and an error.


47-49: The recorder method for Open correctly sets up the expected call within the mock framework.

x/logic/testutil/read_file_fs_mocks.go (4)

38-43: The Open method's mock implementation is correctly set up to simulate the interface's behavior, returning a file and an error based on the input.


47-49: The recorder method for Open is properly configured to record the expected method call in the mock framework.


52-58: The ReadFile method in the mock is implemented correctly to return a byte array and an error, reflecting the behavior expected from the interface.


61-64: The recorder method for ReadFile correctly sets up the expected call within the mock framework.

x/logic/fs/composite/fs.go (3)

33-35: The NewFS function correctly initializes a new instance of vfs with an empty map for mounted file systems, adhering to the expected behavior of the composite file system.


37-48: The Open method properly handles path validation and resolves the file system based on the URI, returning the file object. The error handling is robust, using fs.PathError to provide detailed error information.


84-92: The Mount and ListMounts methods are correctly implemented to manage the mounting of file systems and listing them in sorted order. The use of sort.Strings ensures that the mounts are returned in a predictable order.

x/logic/fs/wasm/fs.go (3)

38-42: The NewFS constructor is correctly set up to create a new instance of the WASM file system with the provided context and keeper.


44-52: The Open method effectively reads data from a WASM contract and wraps it into a virtual file. The integration with the SDK context is well-handled.


54-56: The ReadFile method simplifies file reading by delegating to the readFile helper function, maintaining consistency in how files are read.

Makefile (1)

380-381: Ensure mock generation commands are appropriately scoped.

The addition of mock generation commands for FS and ReadFileFS interfaces is appropriate and aligns with the testing strategy. Ensure that these mocks are utilized effectively in unit tests.

x/logic/fs/composite/fs_test.go Outdated Show resolved Hide resolved
x/logic/fs/composite/fs_test.go Outdated Show resolved Hide resolved
x/logic/fs/composite/fs.go Outdated Show resolved Hide resolved
x/logic/fs/composite/fs.go Outdated Show resolved Hide resolved
x/logic/fs/wasm/fs.go Show resolved Hide resolved
@ccamel
Copy link
Member Author

ccamel commented Jun 18, 2024

@coderabbitai pause

Copy link
Contributor

coderabbitai bot commented Jun 18, 2024

Actions performed

Reviews paused.

@ccamel ccamel force-pushed the refactor/logic-vfs branch from 4aba324 to e4f9d92 Compare June 18, 2024 09:12
@ccamel ccamel force-pushed the refactor/logic-vfs branch from e4f9d92 to 2c41f3c Compare June 18, 2024 09:34
@ccamel ccamel requested review from amimart and bdeneux June 18, 2024 10:41
Copy link
Contributor

@bdeneux bdeneux left a comment

Choose a reason for hiding this comment

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

Awesome implementation 👌 seems good ! 👍

Copy link
Member

@amimart amimart left a comment

Choose a reason for hiding this comment

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

Like that! Sounds goods, tested locally everything's fine 😉

@ccamel ccamel merged commit d8b5710 into main Jun 20, 2024
20 checks passed
@ccamel ccamel deleted the refactor/logic-vfs branch June 20, 2024 06:31
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.

3 participants