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

Feat/dataverse instantiate #422

Merged
merged 13 commits into from
Nov 16, 2023
Merged

Feat/dataverse instantiate #422

merged 13 commits into from
Nov 16, 2023

Conversation

amimart
Copy link
Member

@amimart amimart commented Nov 15, 2023

Implement the instantiate message handling of the dataverse smart contract according to #425.

Details

To allow the dataverse to manage its triple store (i.e. the cognitarium) I've added the necessary elements (i.e. code id & limitations) to its instantiation at the message level, without tainting it strongly as cognitarium as it could be another implementation.

In its implementation, the instantiation of the triple store doesn't leverage a reply message mechanism to get its address but uses instead a generated predictable address, which causes some ugly necessary stuffs to make the tests works, see: CosmWasm/cosmwasm#1648.

Finally, the instantiated triple store smart contract takes as label the dataverse name suffixed by _triplestore.

Misc

A change in the build system has been necessary to allow compiling packages multiple times with different set of features, for instance: when running cargo make wasm it should compiles okp4-cognitarium twice, once with the library feature for the okp4-dataverse contract, and a second time for the cognitarium contract itself, but this not works.

The solution here consists in the use of cargo-hack plugin to compiles each contract separately, the tradeoff is a longer wasm compilation time..

An issue can be tracked for this on cargo: rust-lang/cargo#4463

Summary by CodeRabbit

  • New Features

    • Introduced a new Dataverse structure to store dataverse information including a triplestore_address.
    • Added triplestore_config to the instantiation message for configuring triple stores.
    • Implemented new TripleStoreConfig and TripleStoreLimitsInput structures for better triple store configuration and limitation management.
  • Documentation

    • Updated documentation to include new parameters and types such as TripleStoreConfig and StoreLimitsInput.
    • Added descriptions and usage examples for new numerical types Uint128 and Uint64.
  • Bug Fixes

    • Added error handling for instantiation errors with the new Instantiate2Address error variant.
  • Tests

    • Added new tests to validate the instantiation process of the contract.
  • Chores

    • Updated .size-limit.json to include new wasm file configurations.

Copy link

coderabbitai bot commented Nov 15, 2023

Warning

Rate Limit Exceeded

@amimart has exceeded the limit for the number of files or commits that can be reviewed per hour. Please wait 13 minutes and 10 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. 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 repository.
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 c142976 and 89a55ee.

The provided content is comprehensive and well-structured. I will now generate the requested sections based on the provided instructions.

Walkthrough

The updates involve enhancing a smart contract by adding a new Dataverse struct to manage state, introducing error handling for instantiation, and configuring a triple store with limits. The contract's instantiate function now calculates and stores a triplestore_address, and the documentation has been updated to reflect new parameters and types. A test ensures the instantiation works as expected, and a size limit configuration is added for the contract's WebAssembly module.

Changes

File Path Change Summary
contracts/okp4-dataverse/src/contract.rs Updated instantiate function to calculate and store triplestore_address; added proper_instantiate test. Also updated import statements and added new imports.
contracts/okp4-dataverse/src/error.rs Added Instantiate2Address error variant for instantiation address errors.
contracts/okp4-dataverse/src/msg.rs Added TripleStoreConfig and TripleStoreLimitsInput structs; updated InstantiateMsg and ExecuteMsg.
contracts/okp4-dataverse/src/state.rs Introduced Dataverse struct with state management and a constant DATAVERSE.
docs/okp4-dataverse.md Updated documentation for new parameters, types, and usage examples for Uint128 and Uint64.
.size-limit.json Added size limit configuration for the contract's WebAssembly module.

As autumn leaves 🍂 gently fall,
A rabbit codes beneath the sprawl.
With each key tap, a new world's address,
In smart contracts, we now progress. 🌟


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

Note: For conversation with the bot, please use the review comments on code diffs or files.

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

@bot-anik
Copy link
Member

bot-anik commented Nov 15, 2023

size-limit report 📦

Path Size
target/wasm32-unknown-unknown/release/okp4_objectarium.wasm 432.31 KB (+0.47% 🔺)
target/wasm32-unknown-unknown/release/okp4_law_stone.wasm 641.23 KB (+0.25% 🔺)
target/wasm32-unknown-unknown/release/okp4_cognitarium.wasm 825.61 KB (+0.44% 🔺)
target/wasm32-unknown-unknown/release/okp4_dataverse.wasm 230.07 KB (+100% 🔺)

@amimart amimart requested a review from ccamel November 15, 2023 20:10
@amimart amimart self-assigned this Nov 15, 2023
Copy link

codecov bot commented Nov 15, 2023

Codecov Report

Attention: 17 lines in your changes are missing coverage. Please review.

Comparison is base (9e1b882) 96.94% compared to head (89a55ee) 96.84%.

Files Patch % Lines
contracts/okp4-dataverse/src/msg.rs 68.96% 9 Missing ⚠️
contracts/okp4-dataverse/src/contract.rs 91.30% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #422      +/-   ##
==========================================
- Coverage   96.94%   96.84%   -0.11%     
==========================================
  Files          48       49       +1     
  Lines        9882    10003     +121     
==========================================
+ Hits         9580     9687     +107     
- Misses        302      316      +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@amimart amimart marked this pull request as ready for review November 15, 2023 20:59
Copy link

@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 46ab1e9 and 87fd9a7.
Files ignored due to filter (4)
  • Cargo.lock
  • Cargo.toml
  • Makefile.toml
  • contracts/okp4-dataverse/Cargo.toml
Files selected for processing (5)
  • contracts/okp4-dataverse/src/contract.rs (3 hunks)
  • contracts/okp4-dataverse/src/error.rs (1 hunks)
  • contracts/okp4-dataverse/src/msg.rs (3 hunks)
  • contracts/okp4-dataverse/src/state.rs (1 hunks)
  • docs/okp4-dataverse.md (4 hunks)
Additional comments: 8
contracts/okp4-dataverse/src/error.rs (1)
  • 4-11: The addition of the Instantiate2Address error variant is a good practice for handling specific instantiation errors related to addresses. This aligns with the Rust convention of using enums for error handling and the use of thiserror for deriving the Error trait, which simplifies the implementation of error types.

Ensure that the rest of the codebase is updated to handle this new error variant where relevant, especially in the instantiation logic of the smart contract.

contracts/okp4-dataverse/src/msg.rs (2)
  • 4-12: The addition of triplestore_config to InstantiateMsg is a significant change. Ensure that all instantiations of the dataverse contract are updated to include this new configuration parameter. The documentation should also be updated to reflect this change.

  • 126-137: The TripleStoreConfig struct is well-defined and includes a default for limits using okp4_cognitarium::msg::StoreLimitsInput::default. This is a good use of defaults to simplify configuration. However, ensure that the okp4_cognitarium dependency is correctly referenced and accessible in the project.

docs/okp4-dataverse.md (5)
  • 50-55: The documentation for InstantiateMsg has been updated to include the new triplestore_config parameter. This is a required parameter that specifies the configuration for the triple store associated with the dataverse instance. The code_id and limits fields within triplestore_config are also documented, providing details on their purpose and default values. The default values for limits are quite large, which may be intended to indicate no practical limit, but it's important to ensure that these defaults are appropriate for the system's expected usage patterns and resource constraints.

  • 86-92: The documentation for ExecuteMsg::RegisterDigitalResource has been updated to reflect the new parameters for registering a digital resource. It includes detailed descriptions of each parameter and their preconditions. The documentation is clear and provides the necessary information for understanding how to use the function.

  • 205-220: The StoreLimitsInput section has been updated to include descriptions of various limitations that can be applied to the triple store. The use of [Uint128](#uint128) for byte size and triple count fields suggests that the system is designed to handle very large numbers. It's important to ensure that the system has been tested to handle these limits and that the underlying infrastructure can support such configurations.

  • 226-236: The TripleStoreConfig section provides a detailed explanation of the configuration options available for the triple store. The documentation includes default values for each limit, which are the maximum values representable by Uint128. This implies that, by default, there are no limits unless explicitly set. It's crucial to validate that these defaults align with the intended use cases and that they won't lead to potential resource exhaustion.

  • 245-270: The documentation for the Uint64 and Uint128 types has been added, including examples of how to use these types in code. This is useful for developers who need to work with large integers that exceed the safe range for JavaScript numbers. The examples provided are clear and demonstrate the usage effectively.

Copy link

@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 87fd9a7 and 4c1db02.
Files selected for processing (1)
  • .size-limit.json (1 hunks)
Files skipped from review due to trivial changes (1)
  • .size-limit.json

Copy link
Member

@ccamel ccamel left a comment

Choose a reason for hiding this comment

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

Great job! 💪 There's just a minor comment I have, which I'll leave for you to review.

contracts/okp4-dataverse/src/msg.rs Outdated Show resolved Hide resolved
Copy link

@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: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4c1db02 and 2211293.
Files selected for processing (3)
  • contracts/okp4-dataverse/src/contract.rs (3 hunks)
  • contracts/okp4-dataverse/src/msg.rs (3 hunks)
  • docs/okp4-dataverse.md (4 hunks)
Additional comments: 10
docs/okp4-dataverse.md (5)
  • 50-55: The documentation clearly explains the new parameters added to the InstantiateMsg. It is important to ensure that the code_id and limits are properly validated in the smart contract code to prevent any issues related to incorrect configuration or potential security vulnerabilities.

  • 86-92: The documentation for ExecuteMsg::RegisterDigitalResource is well-detailed, providing clear descriptions and preconditions for each parameter. It's crucial that the smart contract enforces these preconditions to maintain the integrity of the dataverse.

  • 210-222: The TripleStoreConfig and its limits are well-documented. However, it's essential to ensure that the smart contract code properly enforces these limits to prevent abuse or denial of service attacks due to resource exhaustion. For instance, the max_triple_byte_size should be checked to prevent the storage of excessively large triples.

  • 226-236: The TripleStoreLimitsInput mirrors the structure of TripleStoreConfig limits. It's important to ensure that the input provided by users is validated against the expected ranges and types to prevent any runtime errors or misconfigurations.

  • 250-270: The documentation for Uint128 and Uint64 includes examples of how to use these types within the smart contract. It's important to ensure that the smart contract code handles these types correctly, especially considering JavaScript's limitations with large integers.

contracts/okp4-dataverse/src/msg.rs (4)
  • 4-12: The InstantiateMsg struct has been updated to include a triplestore_config field. Ensure that all instantiations of the dataverse contract are updated to provide this configuration.

  • 58-76: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [14-75]

The ExecuteMsg enum has been updated to reflect the change from registering datasets to registering digital resources. This is a significant change in functionality and should be clearly communicated to users of the contract to avoid confusion.

  • 126-169: The TripleStoreConfig and TripleStoreLimitsInput structs have been added to configure the triple store. It's important to ensure that the limits set here are reasonable and that they are properly documented so that users understand the constraints of the triple store.

  • 171-198: The From trait implementation for converting TripleStoreLimitsInput to okp4_cognitarium::msg::StoreLimitsInput is straightforward. However, it's important to ensure that the okp4_cognitarium contract expects the same default values as set in the TripleStoreLimitsInput struct when None is provided.

contracts/okp4-dataverse/src/contract.rs (1)
  • 1-14: The import statements and contract version setting are standard and appear correct. The use of concat! macro for CONTRACT_NAME ensures that the contract name is correctly set at compile time.

contracts/okp4-dataverse/src/contract.rs Outdated Show resolved Hide resolved
contracts/okp4-dataverse/src/contract.rs Outdated Show resolved Hide resolved
Copy link
Member

@ccamel ccamel left a comment

Choose a reason for hiding this comment

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

All good! 👍

Copy link

@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 9e1b882 and c142976.
Files ignored due to filter (4)
  • Cargo.lock
  • Cargo.toml
  • Makefile.toml
  • contracts/okp4-dataverse/Cargo.toml
Files selected for processing (6)
  • .size-limit.json (1 hunks)
  • contracts/okp4-dataverse/src/contract.rs (3 hunks)
  • contracts/okp4-dataverse/src/error.rs (1 hunks)
  • contracts/okp4-dataverse/src/msg.rs (3 hunks)
  • contracts/okp4-dataverse/src/state.rs (1 hunks)
  • docs/okp4-dataverse.md (4 hunks)
Files skipped from review due to trivial changes (1)
  • .size-limit.json
Additional comments: 13
contracts/okp4-dataverse/src/error.rs (1)
  • 4-11: The addition of the Instantiate2Address error variant is appropriate for handling instantiation errors related to a specific address. This follows the standard pattern of error handling in Rust, using the thiserror crate for deriving the Error trait, which is a common practice in Rust for error management.

Ensure that the rest of the codebase properly uses this new error variant where relevant, especially in the instantiation logic of the smart contract.

contracts/okp4-dataverse/src/msg.rs (3)
  • 1-12: The InstantiateMsg struct has been updated to include a triplestore_config field of type TripleStoreConfig. This change allows the dataverse to be initialized with specific configuration settings for the triple store. Ensure that all parts of the code that create an InstantiateMsg are updated to include this new field.

  • 126-168: The TripleStoreConfig and TripleStoreLimitsInput structs have been introduced to specify the configuration and limitations of the triple store. It's important to ensure that the default values mentioned in the comments (e.g., Uint128::MAX for max_triple_count) are actually set as defaults in the implementation if the fields are None. If not, the code should be updated to reflect the correct defaults.

  • 171-198: The From trait implementation for converting TripleStoreLimitsInput to okp4_cognitarium::msg::StoreLimitsInput is correctly checking for None before setting the values. However, it's important to ensure that the okp4_cognitarium::msg::StoreLimitsInput struct has the same fields and expects the same types. If there are any discrepancies, they should be addressed.

contracts/okp4-dataverse/src/contract.rs (3)
  • 14-14: The CONTRACT_NAME constant is correctly using the concat! macro to include the package name from CARGO_PKG_NAME. This is a good practice for dynamically setting the contract name based on the package name.

---end hunk 0---

---start hunk 1---

  • 36-42: The conditional compilation for testing purposes is a good practice to mock behavior that depends on external factors not present in the test environment. However, ensure that the "predicted address" used in tests aligns with the actual behavior in production to avoid discrepancies.

  • 52-63: The response construction is correct and follows best practices by adding attributes and messages that will be useful for debugging and interaction with the contract.

---end hunk 1---

---start hunk 2---

docs/okp4-dataverse.md (6)
  • 50-55: The documentation clearly explains the new parameters added to the InstantiateMsg. It is important to ensure that the descriptions are accurate and that the TripleStoreConfig and its nested parameters are implemented as described. The use of Markdown formatting for required fields is consistent and enhances readability.

  • 86-92: The documentation for ExecuteMsg::RegisterDigitalResource is well-structured and provides a clear explanation of the parameters and their requirements. The preconditions for the register_digital_resource.identifier and register_digital_resource.provided_by are particularly important for ensuring data integrity within the dataverse.

  • 205-240: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [205-246]

The RdfFormat section is informative and provides external references to the specifications for each format. This is useful for developers who may need to implement or interact with these formats. The inclusion of NQuads and NTriples indicates support for standard RDF serialization formats, which is beneficial for interoperability.

  • 210-222: The TripleStoreConfig section is crucial as it defines the configuration for the triple store. It is important to ensure that the default values mentioned (e.g., [Uint128::MAX]) are correctly implemented in the code to prevent any unexpected behavior. The documentation should reflect the actual behavior of the system.

  • 226-236: The TripleStoreLimitsInput section is well-documented, providing clear descriptions and default values for each property. It is important to verify that the system correctly handles null values and defaults as described here.

  • 248-270: The Uint128 and Uint64 sections provide a brief explanation of these types. The example provided for Uint64 is helpful for understanding its usage. Ensure that the example code is tested and works as expected, as it is a part of the official documentation.

Comment on lines 4 to 10
pub const DATAVERSE: Item<'_, Dataverse> = Item::new("dataverse");

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Dataverse {
pub name: String,
pub triplestore_address: String,
}
Copy link

Choose a reason for hiding this comment

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

The implementation of the Dataverse struct and the DATAVERSE constant is consistent with the summary provided. However, as previously mentioned, it is recommended to use the Addr type for the triplestore_address field to ensure that the address is validated and to avoid potential errors from handling it as a String.

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Dataverse {
    pub name: String,
    pub triplestore_address: Addr, // Use Addr to ensure the address is valid
}

let salt = Binary::from(msg.name.as_bytes());

/// Necessary stuff for testing purposes, see: https://github.com/CosmWasm/cosmwasm/issues/1648
#[allow(unused)]
Copy link

Choose a reason for hiding this comment

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

The #[allow(unused)] attribute should be removed if triplestore_address is being used, as it seems to be in this case.

- #[allow(unused)]

assert_eq!(
res.messages,
vec![SubMsg::new(WasmMsg::Instantiate2 {
admin: Some("cosmos2contract".to_string()),
Copy link

Choose a reason for hiding this comment

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

The admin field in the Instantiate2 message should match the env.contract.address used in the actual instantiate function. If "cosmos2contract" is not the intended admin, this should be corrected to maintain consistency with the contract's logic.

- admin: Some("cosmos2contract".to_string()),
+ admin: Some(env.contract.address.to_string()),

---end hunk 2---

@amimart amimart force-pushed the feat/dataverse-instantiate branch 2 times, most recently from 54ef46a to e2cf607 Compare November 16, 2023 09:23
Copy link

@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 c142976 and e2cf607.
Files selected for processing (2)
  • contracts/okp4-dataverse/src/contract.rs (3 hunks)
  • contracts/okp4-dataverse/src/state.rs (1 hunks)
Additional comments: 5
contracts/okp4-dataverse/src/state.rs (2)
  • 5-5: The constant DATAVERSE is correctly defined using Item::new with the key "dataverse". This will create a storage item for the Dataverse struct.

  • 7-11: The Dataverse struct is well-defined with appropriate attributes and derives common traits such as Serialize, Deserialize, Clone, Debug, PartialEq, and Eq. These traits are necessary for serialization/deserialization, cloning instances, debugging, and comparing instances respectively.

contracts/okp4-dataverse/src/contract.rs (3)
  • 1-14: The use of concat! macro for CONTRACT_NAME is a good practice as it ensures the name is determined at compile time, which is beneficial for performance and error prevention. However, ensure that the CONTRACT_NAME is unique and descriptive enough to avoid conflicts with other contracts.

  • 17-64: The instantiate function has been updated to include logic for calculating the triplestore_address and saving it to the Dataverse state. This is a significant change and should be thoroughly tested to ensure that the address calculation and state saving are functioning as expected.

  • The use of instantiate2_address function to calculate the triplestore_address is correct and follows the CosmWasm documentation.
  • The conditional compilation with #[cfg(not(test))] and #[cfg(test)] is a good practice to handle test scenarios differently from production code.
  • The Response object is correctly constructed with attributes and messages, which is a good practice for debugging and transaction history.

However, there is a potential issue with error handling:

  • The ? operator is used after set_contract_version, addr_canonicalize, query_wasm_code_info, instantiate2_address, addr_humanize, and DATAVERSE.save calls. Ensure that the ContractError type can properly handle the conversion from StdError to provide meaningful error messages to the contract users.
  • 83-154: The test proper_instantiate is well-structured and covers the instantiation process of the contract. It checks the response attributes, messages, and the state after instantiation, which are key aspects to verify.
  • Mocking the querier response with update_wasm to return a CodeInfoResponse is a good practice for unit testing.
  • The use of mock_env and mock_info to simulate the environment and message info is correct.
  • The assertions are comprehensive and check for the expected values in the response and the state.

However, ensure that the predicted address used in the test matches the actual address that would be generated in a real environment to avoid discrepancies between test and production behavior.

@amimart amimart merged commit cf1aa4f into main Nov 16, 2023
15 of 18 checks passed
@amimart amimart deleted the feat/dataverse-instantiate branch November 16, 2023 09:33
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