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

Feature/wasm contract env #478

Open
wants to merge 4 commits into
base: release/1.1.0
Choose a base branch
from
Open

Conversation

zie1ony
Copy link
Contributor

@zie1ony zie1ony commented Jun 7, 2024

I was wondering what would happen if I move odra-casper-wasm-env to odra-core and conditionally don't use dyn in ContractEnv for wasm32.

Summary by CodeRabbit

  • New Features

    • Introduced a Wasm environment module with support for contract interactions, cryptographic operations, event handling, and more.
    • Added constants and host functions specific to the wasm runtime.
  • Chores

    • Updated dependencies and configurations for wasm32 target architecture.
    • Added RUSTFLAGS environment variable for benchmarking.
  • Refactor

    • Conditional compilation adjustments for better wasm32 support.
    • Improved modularization of wasm-related code.

Copy link
Contributor

coderabbitai bot commented Jun 7, 2024

Walkthrough

The changes introduce support for the wasm32 target architecture in the project, adding conditional compilation to adjust functionality based on the target. This includes updating dependencies, modifying structures, and implementing new modules and functions specific to the wasm32 environment. Additionally, the build process for benchmarks is optimized by setting the RUSTFLAGS environment variable.

Changes

File(s) Change Summary
core/Cargo.toml Added dependencies for casper-contract, ink_allocator, and lazy_static for wasm32 target.
core/src/contract_env.rs Introduced conditional compilation for ContractEnv struct and new method based on target architecture.
core/src/lib.rs Added attributes and a new wasm module for wasm32 target.
core/src/wasm/consts.rs Introduced constants for the wasm runtime.
core/src/wasm/host_functions.rs Added functions for interacting with the Casper host environment.
core/src/wasm/mod.rs Introduced a Wasm environment module with submodules and a panic handler.
core/src/wasm/wasm_contract_env.rs Implemented WasmContractEnv struct with various functions for ContractContext.
odra-casper/proxy-caller/Cargo.toml Changed dependency on odra-casper-wasm-env to include disable-allocator feature.
odra/wasm-env/src/lib.rs Commented out ink_allocator import and panic handler for wasm32 target.
odra/wasm-env/src/wasm_contract_env.rs Added conditional compilation for functions creating ContractEnv instance.
odra/Cargo.toml Removed odra-casper-wasm-env dependency for wasm32 target and updated disable-allocator feature configuration.
odra/src/lib.rs Modified import statement for odra_casper_wasm_env under wasm32 target.
justfile Added RUSTFLAGS=-C passes=mergefunc to the benchmark target.

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant Cargo
    participant RustCompiler
    participant WasmModule

    Developer->>Cargo: Add wasm32 dependencies
    Cargo->>RustCompiler: Compile with wasm32 target
    RustCompiler->>WasmModule: Generate wasm32 specific code
    WasmModule-->>RustCompiler: Provide WasmContractEnv
    RustCompiler-->>Cargo: Complete build
    Cargo-->>Developer: Build successful
Loading

Poem

In the realm of code we strive,
For wasm32, we now arrive.
With ContractEnv and WasmContractEnv,
New paths and modules we extend.
Functions tailored, constants set,
Our Wasm journey's best yet.
With benchmarks swift, our flags align,
In coding realms, we brightly shine. 🌟


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

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

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

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

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

CodeRabbit Configration File (.coderabbit.yaml)

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

Documentation and Community

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

Copy link

github-actions bot commented Jun 7, 2024

Benchmark report

Action Details Gas diff
Wasm Deploy Filename: Benchmark.wasm 🟢 -29.155692959 CSPR (18.64%)
Contract Call Entry point: set_variable 🟢 -0.00098033 CSPR (0.64%)
Contract Call Entry point: get_variable 🟢 -0.00080582 CSPR (0.15%)
Contract Call Entry point: set_struct_variable 🟢 -0.00080076 CSPR (0.42%)
Contract Call Entry point: get_struct_variable 🟢 -0.00084123 CSPR (0.20%)
Contract Call Entry point: set_mapping 🟢 -0.00117924 CSPR (0.75%)
Contract Call Entry point: get_mapping 🟢 -0.00103959 CSPR (0.27%)
Contract Call Entry point: push_list 🟢 -0.00151729 CSPR (0.48%)
Contract Call Entry point: get_list 🟢 -0.00104043 CSPR (0.26%)
Contract Call Entry point: init_submodule 🟢 -0.00253506 CSPR (0.26%)
Contract Call Entry point: call_submodule 🟢 -0.00091574 CSPR (0.24%)
Contract Call Entry point: call_payable 🟢 -0.00105026 CSPR (0.02%)
Contract Call Entry point: transfer_back 🟢 -0.00089435 CSPR (0.04%)
Contract Call Entry point: emit_event 🟢 -0.00055589 CSPR (0.39%)
Contract Call Entry point: set_named_key 🟢 -0.00073971 CSPR (0.36%)
Contract Call Entry point: get_named_key 🟢 -0.00080622 CSPR (0.20%)
Contract Call Entry point: set_dictionary 🟢 -0.00098817 CSPR (0.33%)
Contract Call Entry point: get_dictionary 🟢 -0.00108967 CSPR (0.27%)

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between cdf5479 and 0492ba2.

Files selected for processing (12)
  • core/Cargo.toml (1 hunks)
  • core/src/contract_env.rs (3 hunks)
  • core/src/lib.rs (2 hunks)
  • core/src/wasm/consts.rs (1 hunks)
  • core/src/wasm/host_functions.rs (1 hunks)
  • core/src/wasm/mod.rs (1 hunks)
  • core/src/wasm/wasm_contract_env.rs (1 hunks)
  • odra-casper/proxy-caller/Cargo.toml (1 hunks)
  • odra-casper/wasm-env/src/lib.rs (1 hunks)
  • odra-casper/wasm-env/src/wasm_contract_env.rs (1 hunks)
  • odra/Cargo.toml (1 hunks)
  • odra/src/lib.rs (1 hunks)
Files skipped from review due to trivial changes (5)
  • core/src/wasm/consts.rs
  • core/src/wasm/mod.rs
  • odra-casper/proxy-caller/Cargo.toml
  • odra-casper/wasm-env/src/lib.rs
  • odra/Cargo.toml
Additional comments not posted (12)
core/Cargo.toml (1)

18-21: Dependencies for wasm32 target added correctly.

Ensure that these dependencies are compatible with the rest of the project and do not introduce version conflicts.

core/src/lib.rs (1)

4-5: Attributes and wasm module added for wasm32 target.

Also applies to: 60-61

Verification successful

Attributes #![allow(internal_features)] and #![cfg_attr(not(test), feature(core_intrinsics))] are present in both core/src/lib.rs and odra-casper/wasm-env/src/lib.rs. Let's verify if these attributes are necessary by checking for usage of core intrinsics and internal features in the codebase.


The attributes #![allow(internal_features)] and #![cfg_attr(not(test), feature(core_intrinsics))] are necessary due to the usage of core::intrinsics::abort() and the need to allow internal features for wasm-specific functionality.

  • core/src/wasm/mod.rs: Usage of core::intrinsics::abort()
  • odra-casper/wasm-env/src/lib.rs: Commented-out usage of core::intrinsics::abort()
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the necessity and impact of the new attributes on the project.

# Test: Search for usage of internal features and core intrinsics in the project.
rg --type rust $'internal_features|core_intrinsics'

Length of output: 306



Script:

#!/bin/bash
# Description: Check for usage of core intrinsics and internal features in the codebase.

# Search for core intrinsics usage
rg --type rust 'core::intrinsics'

# Search for internal features usage
rg --type rust 'internal_features'

Length of output: 292

odra/src/lib.rs (1)

63-63: Import statement updated to use the new wasm module from odra_core.

Ensure that this change is reflected wherever odra_casper_wasm_env is used in the project.

odra-casper/wasm-env/src/wasm_contract_env.rs (1)

105-115: Conditional compilation used for creating ContractEnv instances based on the target architecture.

Verification successful

The search results indicate that the new_env and new methods are indeed used in the codebase, specifically in the odra-casper/wasm-env/src/wasm_contract_env.rs file. However, to ensure the correct functionality of these conditionally compiled methods, we need to verify their usage in different contexts and ensure they are correctly invoked based on the target architecture.

To proceed, let's gather more specific information about the usage of these methods in the context of the target architecture. We will search for the conditional compilation flags (#[cfg(not(target_arch = "wasm32"))] and #[cfg(target_arch = "wasm32")]) to see how these methods are being utilized.


The search results confirm that the conditional compilation flags #[cfg(not(target_arch = "wasm32"))] and #[cfg(target_arch = "wasm32")] are used extensively throughout the codebase. Specifically, the new_env and new methods in odra-casper/wasm-env/src/wasm_contract_env.rs are correctly conditionally compiled based on the target architecture.

Given the extensive use of these conditional compilation flags and the specific context in which new_env and new are used, it appears that the implementation is consistent and correctly handles the different target architectures.

Therefore, the conditional compilation for creating ContractEnv instances based on the target architecture is correctly implemented and necessary.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct functionality of the conditionally compiled methods.

# Test: Search for usage of `new_env` and `new` methods in the project.
rg --type rust $'new_env|new'

Length of output: 92039



Script:

#!/bin/bash
# Description: Verify the correct functionality of the conditionally compiled methods by searching for conditional compilation flags.

# Search for the usage of `#[cfg(not(target_arch = "wasm32"))]` and `#[cfg(target_arch = "wasm32")]` in the project.
rg --type rust '#\[cfg\(not\(target_arch = "wasm32"\)\)\]' --context 5
rg --type rust '#\[cfg\(target_arch = "wasm32"\)\]' --context 5

Length of output: 53047

core/src/wasm/wasm_contract_env.rs (3)

15-101: Implementation of ContractContext for WasmContractEnv looks comprehensive and well-structured.


109-112: The borrow method correctly returns a reference to self, which is a typical pattern in Rust for borrowing instances.


105-107: Ensure that the new_env function correctly initializes WasmContractEnv.

core/src/contract_env.rs (2)

Line range hint 37-62: Conditional compilation for wasm32 and non-wasm32 targets is correctly implemented in ContractEnv.


Line range hint 44-62: The new method for both wasm32 and non-wasm32 targets correctly initializes ContractEnv. Ensure that the index and mapping_data are appropriately used throughout the contract logic.

Verification successful

The index and mapping_data fields in the ContractEnv struct are used appropriately within the contract logic. The index is utilized for key generation and creating child environments, while mapping_data is used for extending key data and adding to the mapping data.

  • core/src/contract_env.rs
    • index is used in key generation and child environment creation.
    • mapping_data is used for extending key data and adding to the mapping data.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `index` and `mapping_data` are used correctly in contract logic.

# Test: Search for usage of `index` and `mapping_data`. Expect: Correct usage in contract logic.
rg --type rust $'index|mapping_data'

Length of output: 14949

core/src/wasm/host_functions.rs (3)

112-116: The revert function uses inline optimization and correctly reverts the contract execution using the Casper API.


159-183: The set_value function correctly writes values to the contract's storage. Review the use of unsafe blocks to ensure safety.


51-110: The install_contract function correctly handles contract installation with checks for key overrides and upgradability. Ensure that error handling is robust and that the TODO comment is addressed.

Comment on lines +118 to +145
/// Returns given named argument passed to the host. The result is not deserialized,
/// is returned as a `Vec<u8>`.
pub fn get_named_arg(name: &str) -> Result<Vec<u8>, ApiError> {
let arg_size = get_named_arg_size(name)?;
if arg_size > 0 {
let data_non_null_ptr = contract_api::alloc_bytes(arg_size);
let ret = unsafe {
ext_ffi::casper_get_named_arg(
name.as_bytes().as_ptr(),
name.len(),
data_non_null_ptr.as_ptr(),
arg_size
)
};
if ret != 0 {
return Err(ApiError::from(ret as u32));
}
unsafe {
Ok(Vec::from_raw_parts(
data_non_null_ptr.as_ptr(),
arg_size,
arg_size
))
}
} else {
Ok(Vec::new())
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The get_named_arg function in core/src/wasm/host_functions.rs does handle errors by returning an ApiError when ext_ffi::casper_get_named_arg returns a non-zero value. However, the error messages are not detailed, which could be improved for better debugging.

  • core/src/wasm/host_functions.rs: The function handles errors but lacks detailed error messages.
Analysis chain

Ensure that get_named_arg correctly handles errors and returns the expected results. Consider adding more detailed error messages for better debugging.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify error handling in `get_named_arg`.

# Test: Search for error handling in `get_named_arg`. Expect: Detailed error messages.
rg --type rust $'get_named_arg'

Length of output: 5618

Copy link

Benchmark report

Action Details Gas diff
Wasm Deploy Filename: Benchmark.wasm 🟢 -29.154575372 CSPR (18.64%)
Contract Call Entry point: set_variable 🟢 -0.00098033 CSPR (0.64%)
Contract Call Entry point: get_variable 🟢 -0.00080582 CSPR (0.15%)
Contract Call Entry point: set_struct_variable 🟢 -0.00080076 CSPR (0.42%)
Contract Call Entry point: get_struct_variable 🟢 -0.00084123 CSPR (0.20%)
Contract Call Entry point: set_mapping 🟢 -0.00117924 CSPR (0.75%)
Contract Call Entry point: get_mapping 🟢 -0.00103959 CSPR (0.27%)
Contract Call Entry point: push_list 🟢 -0.00151729 CSPR (0.48%)
Contract Call Entry point: get_list 🟢 -0.00104043 CSPR (0.26%)
Contract Call Entry point: init_submodule 🟢 -0.00253506 CSPR (0.26%)
Contract Call Entry point: call_submodule 🟢 -0.00091574 CSPR (0.24%)
Contract Call Entry point: call_payable 🟢 -0.00105026 CSPR (0.02%)
Contract Call Entry point: transfer_back 🟢 -0.00089435 CSPR (0.04%)
Contract Call Entry point: emit_event 🟢 -0.00055589 CSPR (0.39%)
Contract Call Entry point: set_named_key 🟢 -0.00073971 CSPR (0.36%)
Contract Call Entry point: get_named_key 🟢 -0.00080622 CSPR (0.20%)
Contract Call Entry point: set_dictionary 🟢 -0.00098817 CSPR (0.33%)
Contract Call Entry point: get_dictionary 🟢 -0.00108967 CSPR (0.27%)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0492ba2 and 409fb06.

Files selected for processing (2)
  • core/Cargo.toml (1 hunks)
  • odra/Cargo.toml (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • core/Cargo.toml
  • odra/Cargo.toml

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 409fb06 and ec693ef.

Files selected for processing (1)
  • justfile (1 hunks)
Files skipped from review due to trivial changes (1)
  • justfile

Copy link

Benchmark report

Action Details Gas diff
Wasm Deploy Filename: Benchmark.wasm 🟢 -29.154575372 CSPR (18.64%)
Contract Call Entry point: set_variable 🟢 -0.00098033 CSPR (0.64%)
Contract Call Entry point: get_variable 🟢 -0.00080582 CSPR (0.15%)
Contract Call Entry point: set_struct_variable 🟢 -0.00080076 CSPR (0.42%)
Contract Call Entry point: get_struct_variable 🟢 -0.00084123 CSPR (0.20%)
Contract Call Entry point: set_mapping 🟢 -0.00117924 CSPR (0.75%)
Contract Call Entry point: get_mapping 🟢 -0.00103959 CSPR (0.27%)
Contract Call Entry point: push_list 🟢 -0.00151729 CSPR (0.48%)
Contract Call Entry point: get_list 🟢 -0.00104043 CSPR (0.26%)
Contract Call Entry point: init_submodule 🟢 -0.00253506 CSPR (0.26%)
Contract Call Entry point: call_submodule 🟢 -0.00091574 CSPR (0.24%)
Contract Call Entry point: call_payable 🟢 -0.00105026 CSPR (0.02%)
Contract Call Entry point: transfer_back 🟢 -0.00089435 CSPR (0.04%)
Contract Call Entry point: emit_event 🟢 -0.00055589 CSPR (0.39%)
Contract Call Entry point: set_named_key 🟢 -0.00073971 CSPR (0.36%)
Contract Call Entry point: get_named_key 🟢 -0.00080622 CSPR (0.20%)
Contract Call Entry point: set_dictionary 🟢 -0.00098817 CSPR (0.33%)
Contract Call Entry point: get_dictionary 🟢 -0.00108967 CSPR (0.27%)

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.

1 participant