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

Mdjakovic/random generator #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mdjakovic0920
Copy link

@mdjakovic0920 mdjakovic0920 commented Jul 16, 2024

ADO Purpose

With the Random Generator ADO, user can generate the random value based on blockhash, because all of the block hash are different.

Messages

Instantiation (What is specified and stored at instantiation)

pub struct InstantiateMsg {
    pub kernel_address: String,
    pub owner: Option<String>,
}

Execute Messages (What are the messages that can be executed)

#[andr_exec]
#[cw_serde]
pub enum ExecuteMsg {
    IncreaseNonce {},
}

IncreaseNonce: Nonce value is increased every time user call this.

IncreaseNonce {}

Query Messages (What are the messages that can be queried, what does each return)

  1. GetNonce: Returns the nonce value for random generator.
GetNonce {},

Returns:

#[cw_serde]
pub struct GetNonceResponse {
    pub nonce: u128,
}
  1. Generate Random: Returns MD5 hash value from block informations.
GetRandom {},

Returns:

#[cw_serde]
pub struct GetRandomResponse {
    pub random: String,
}

State

The contract maintains the following state:

pub const DEFAULT_NONCE: u128 = 0;

pub const NONCE: Item<u128> = Item::new("nonce");

Summary by CodeRabbit

  • New Features

    • Introduced a Random Generator ADO for generating random values based on blockhash uniqueness.
    • Added message structures for instantiation, execution, and querying, including nonce management and MD5 hash generation.
  • Documentation

    • Added README.md outlining the features, usage, and message structures for the Random Generator ADO.
    • Included LICENSE and NOTICE files to define usage and distribution terms.
  • Chores

    • Configured .gitignore, .editorconfig, and Cargo.toml for project setup and consistency.
    • Introduced CircleCI and GitHub Actions workflows for automated testing, linting, and release processes.

Copy link

coderabbitai bot commented Jul 16, 2024

Walkthrough

This update introduces a Rust project called andromeda-random-generator, incorporating infrastructure for building, testing, and deploying via CircleCI and GitHub Actions. The project includes a random generator contract that leverages block hash uniqueness for nonce-based random number generation. Key features include custom error handling, message structures for various operations, and unit tests. Additionally, configuration files for .cargo, .editorconfig, and .gitignore are provided to maintain code quality and manage dependencies.

Changes

File(s) Change Summary
.cargo/config Introduces build and test command aliases for WebAssembly, unit tests, and schema execution.
.circleci/config.yml CircleCI configuration for Docker image building and deployment based on branch/tag conditions.
.editorconfig Configuration for indentation, character encoding, trailing whitespace trimming, and final newline insertion.
.github/workflows/Basic.yml, .github/workflows/Release.yml GitHub Actions workflows for testing, linting, and releasing WebAssembly artifacts.
.gitignore Exclusions for build results, system files, and IDE-specific files from version control.
Cargo.toml Metadata and dependencies for the andromeda-random-generator Rust package, including build settings and features.
LICENSE, NOTICE Apache License Version 2.0 and related copyright information.
README.md Documentation detailing the Random Generator ADO and its functionalities.
src/bin/schema.rs Main function utilizing cosmwasm_schema crate for defining the write API.
src/contract.rs Implementation of the contract, including instantiation, execution, querying, and migration logic.
src/error.rs Custom error handling enum ContractError for encapsulating various error types.
src/lib.rs Module definitions and re-exports for the project, including contract, error, msg, state, and testing.
src/msg.rs Message structures for instantiation, execution, and querying, with serialization annotations.
src/state.rs Constants and item declaration for nonce handling.
src/testing/mock.rs Mock implementations for testing contract behavior without deploying to a live blockchain.
src/testing/mod.rs Publicly accessible modules tests and mock for testing functionalities.
src/testing/tests.rs Test functions for instantiation, nonce incrementation, and random number generation, ensuring proper contract behavior.

Poem

In fields of code, a rabbit weaves,
New features bloom like summer leaves.
With nonce and block, randomness flows,
In structured lines, the program grows.
Tests and configs, all aligned,
In digital realms, success defined.
🐇✨


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 Configuration 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

@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, codebase verification and nitpick comments (1)
andromeda-random-generator/.circleci/config.yml (1)

50-51: Consider including more branches in the CI pipeline.

Currently, the CI pipeline is configured to run only on the master branch and tagged commits. Including additional active development branches might help in early detection of issues.

Also applies to: 58-59

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6385276 and c33b732.

Files ignored due to path filters (1)
  • andromeda-random-generator/Cargo.lock is excluded by !**/*.lock
Files selected for processing (19)
  • andromeda-random-generator/.cargo/config (1 hunks)
  • andromeda-random-generator/.circleci/config.yml (1 hunks)
  • andromeda-random-generator/.editorconfig (1 hunks)
  • andromeda-random-generator/.github/workflows/Basic.yml (1 hunks)
  • andromeda-random-generator/.github/workflows/Release.yml (1 hunks)
  • andromeda-random-generator/.gitignore (1 hunks)
  • andromeda-random-generator/Cargo.toml (1 hunks)
  • andromeda-random-generator/LICENSE (1 hunks)
  • andromeda-random-generator/NOTICE (1 hunks)
  • andromeda-random-generator/README.md (1 hunks)
  • andromeda-random-generator/src/bin/schema.rs (1 hunks)
  • andromeda-random-generator/src/contract.rs (1 hunks)
  • andromeda-random-generator/src/error.rs (1 hunks)
  • andromeda-random-generator/src/lib.rs (1 hunks)
  • andromeda-random-generator/src/msg.rs (1 hunks)
  • andromeda-random-generator/src/state.rs (1 hunks)
  • andromeda-random-generator/src/testing/mock.rs (1 hunks)
  • andromeda-random-generator/src/testing/mod.rs (1 hunks)
  • andromeda-random-generator/src/testing/tests.rs (1 hunks)
Files skipped from review due to trivial changes (8)
  • andromeda-random-generator/.cargo/config
  • andromeda-random-generator/.editorconfig
  • andromeda-random-generator/.gitignore
  • andromeda-random-generator/Cargo.toml
  • andromeda-random-generator/LICENSE
  • andromeda-random-generator/NOTICE
  • andromeda-random-generator/src/lib.rs
  • andromeda-random-generator/src/testing/mod.rs
Additional context used
LanguageTool
andromeda-random-generator/README.md

[formatting] ~2-~2: If the ‘because’ clause is essential to the meaning, do not use a comma before the clause.
Context: ...rate the random value based on blockhash, because all of the block hash are different. #...

(COMMA_BEFORE_BECAUSE)


[style] ~2-~2: Consider removing “of” to be more concise
Context: ...andom value based on blockhash, because all of the block hash are different. ### Messages...

(ALL_OF_THE)


[uncategorized] ~2-~2: The grammatical number of this noun doesn’t look right. Consider replacing it.
Context: ... on blockhash, because all of the block hash are different. ### Messages ***Instan...

(AI_EN_LECTOR_REPLACEMENT_NOUN_NUMBER)


[uncategorized] ~24-~24: This verb does not appear to agree with the subject. Consider using a different form.
Context: ...once value is increased every time user call this. IncreaseNonce {} ***Qu...

(AI_EN_LECTOR_REPLACEMENT_VERB_AGREEMENT)


[misspelling] ~47-~47: The word ‘informations’ is a legal term. In standard English, the word ‘information’ is a non-count noun.
Context: ...om**: Returns MD5 hash value from block informations. GetRandom {}, Returns: ``...

(INFORMATIONS)

Markdownlint
andromeda-random-generator/README.md

4-4: Expected: h2; Actual: h3
Heading levels should only increment by one level at a time

(MD001, heading-increment)


7-7: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


16-16: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


26-26: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


34-34: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


39-39: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


48-48: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


53-53: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


63-63: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)

Additional comments not posted (11)
andromeda-random-generator/src/state.rs (1)

1-5: State management setup looks good.

The constants and state items are correctly defined using cw_storage_plus, which is standard for managing state in CosmWasm contracts.

andromeda-random-generator/src/bin/schema.rs (1)

1-11: Schema generation setup is correct.

Using write_api! to generate schema files for the contract's messages is a best practice in CosmWasm development, ensuring that interfaces are well-documented.

andromeda-random-generator/src/error.rs (1)

1-13: Error handling setup is appropriate.

The use of thiserror to define custom contract errors is standard and appropriate. The definitions for Std and Unauthorized cover typical error scenarios in contract execution.

andromeda-random-generator/src/msg.rs (1)

1-32: Message definitions are well-structured.

The use of andr_* attributes along with cw_serde for serialization correctly sets up the contract's interfaces for instantiation, execution, and queries. The response structures are also appropriately defined.

andromeda-random-generator/src/testing/tests.rs (3)

5-10: Test for Instantiation: Well-structured and clear.

This test checks that the nonce is initialized to zero, which is a good practice to ensure the contract starts in a known state.


12-19: Test for Nonce Increment: Correct and effective.

This test verifies that the nonce is incremented correctly. It's well-implemented and covers the basic functionality of the IncreaseNonce message.


21-32: Test for Random Number Generation: Ensure variability.

This test checks that two consecutive calls to query_generate_random after nonce increments yield different results. This is crucial for ensuring the randomness mechanism is functioning properly.

andromeda-random-generator/.github/workflows/Release.yml (1)

1-35: GitHub Actions Workflow for Release: Well-configured and comprehensive.

This workflow is set up to handle the release process smoothly by automating the optimization and upload of WASM binaries. It uses well-known actions and is triggered appropriately on release creation.

andromeda-random-generator/src/testing/mock.rs (1)

1-52: Mock Setup in Rust: Well-implemented and crucial for testing.

The mock functions provided are essential for isolating functionality and ensuring that each component of the Random Generator ADO can be tested independently. The use of mock_dependencies_custom and mock_info is appropriate and follows best practices for contract testing in Rust.

andromeda-random-generator/.github/workflows/Basic.yml (2)

1-1: Reference to external workflow example.

It's good to see a reference to an existing workflow example, which helps maintain transparency about the source of the workflow's structure.


3-75: Review of GitHub Actions Workflow.

  • General Setup: The workflow is triggered on push and pull request events, which is standard for continuous integration setups.
  • Test Job:
    • Toolchain Installation: Specifying a fixed Rust toolchain version (1.60.0) is good for consistency. However, consider updating to a newer stable version if applicable.
    • Unit Tests and WASM Compilation: The steps are well-structured, but the command: wasm in line 35 might be incorrect if it's intended to compile a WASM binary. Usually, the command should be something like cargo build --target wasm32-unknown-unknown.
  • Lint Job:
    • Formatting and Clippy Checks: These are essential for maintaining code quality. The use of --check with cargo fmt and -D warnings with clippy ensures that format issues and warnings break the CI build, which enforces code quality.
    • Schema Generation and Validation: The step to generate and check for uncommitted schema changes is a good practice for projects where the schema is expected to evolve.

Consider verifying the exact commands used, especially for WASM compilation, to ensure they align with the project's requirements.

-          command: wasm
+          command: build
+          args: --target wasm32-unknown-unknown --locked

andromeda-random-generator/README.md Show resolved Hide resolved
andromeda-random-generator/src/contract.rs Show resolved Hide resolved
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