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(ext): add app-side library for NEAR #374

Merged
merged 43 commits into from
Jun 26, 2024

Conversation

its-saeed
Copy link
Contributor

@its-saeed its-saeed commented Jun 13, 2024

Summary

We need to develop a high-level library that wraps around the low-level fetch function to call query methods of the NEAR blockchain in a deterministic way. This library should abstract the complexities of the low-level function and provide a simple and intuitive interface for developers.

Test plan

For internal tests, I added this function to the only-peers app to call NEAR query methods:

#[app::logic]
impl OnlyPeers {
    pub fn fetch(&mut self) -> String {
        let client = Client::testnet();
        let request = RpcQueryRequest {
            block_id: calimero_sdk_near::BlockId::Height(166710735),
            request: QueryRequest::ViewAccount {
                account_id: "nearkat.testnet".parse().unwrap(),
            },
        };
        match client.call(request) {
            Ok(r) => format!("{:?}", r),
            Err(e) => format!("{:?}", e),
        }
    }

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have added a test plan that prove my fix is effective or that my feature works
  • I squashed all commits and provided a meaningful commit message
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • New and existing unit tests pass locally with my changes

Summary by CodeRabbit

  • New Features

    • Introduced a new Client struct for interacting with NEAR Protocol RPC endpoints.
    • Added new RPC query requests and response structs to handle NEAR Protocol queries.
    • Implemented JSON-RPC client functionality for NEAR Protocol communication.
  • Enhancements

    • Updated dependency in Cargo.toml to calimero-sdk-near.
    • Added a new public function foo to the OnlyPeers struct for retrieving account balance.
    • Extended serialization and deserialization traits for custom enums.
  • Bug Fixes

    • Updated peer_id field type in PeerJoinedPayload struct to libp2p_identity::PeerId.
  • Refactor

    • Changed dependency from libp2p to libp2p-identity with additional features.
    • Updated import statements and type declarations to reflect new dependencies.
  • Chores

    • Modified example runtime script to change the function argument, affecting the program's behavior.

Copy link

coderabbitai bot commented Jun 13, 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

This update introduces enhanced functionality to the NEAR protocol SDK, including new structures for handling RPC query requests and responses. It implements a Client struct with methods for NEAR protocol interactions, enhances error handling using the thiserror crate, and adjusts dependencies along with serialization/deserialization mappings. Additionally, it modifies the OnlyPeers struct to interact with the NEAR protocol client, refactors libp2p dependencies, and updates enums, traits, and modules for better interoperability and efficiency.

Changes

Files/Modules Change Summary
crates/sdk/libs/near/src/lib.rs Introduced Client struct, new traits, error handling, and RPC endpoint interactions
crates/sdk/libs/near/src/query.rs Added structures for RPC query requests/responses and trait implementations
crates/sdk/libs/near/src/types.rs Defined new type aliases, enums for block representations, structs for storage and function args
crates/sdk/libs/near/src/views.rs Introduced structs for handling query requests and various account and contract views
crates/sdk/libs/near/src/error.rs Introduced detailed error handling with the thiserror crate
crates/sdk/libs/near/src/jsonrpc.rs Added JSON-RPC request and response handling, client struct for communication
apps/only-peers/Cargo.toml Added calimero-sdk-near dependency
apps/only-peers/src/lib.rs Added new function foo to interact with NEAR protocol client
crates/primitives/Cargo.toml Refactored libp2p dependency to libp2p-identity with extra features
crates/primitives/src/common.rs Added serialization and deserialization for custom ResultAlt enum
crates/primitives/src/events.rs Updated PeerJoinedPayload struct to use libp2p_identity::PeerId
crates/primitives/src/identity.rs Changed import to use libp2p_identity::Keypair within the serde_identity module
crates/runtime/examples/fetch.rs Modified string argument in run function from "fetch" to "foo"

Poem

A rabbit hops through code so neat,
Weaving queries, quite the feat! 🐇
NEAR Protocol, answers found,
Data dances all around.
Errors handled, none too sly,
In network streams, dreams fly high. 🌌


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.

@chefsale
Copy link
Member

chefsale commented Jun 14, 2024

@miraclx shouldn't this be using the generic fetch call instead being part of the client SDK? I'm a bit confused.

@its-saeed
Copy link
Contributor Author

@miraclx shouldn't this be using the generic fetch call instead being part of the client SDK? I'm a bit confused.

I think this is because fetch is unsafe behaviorally. It can be called to query an inconsistent end-point and cause inconsistency in states. That's why we restrict it to trusted end-points through the client SDK.

@chefsale
Copy link
Member

But for the blockchain calls we should implement some logic to verify it right? And also allow developers to bake verification into the app if needed on the service level? @miraclx can you provide info here?

@miraclx
Copy link
Member

miraclx commented Jun 17, 2024

@miraclx shouldn't this be using the generic fetch call instead being part of the client SDK?

it's part of the client SDK to consolidate the available host functions and abstractions for correct use

however, it's marked unsafe and following #369 (comment), it's now also buried in a module hidden from the docs so it's undiscoverable, signifying our discouragement of direct use.

However, the client libraries will continue to use it, cautiously.

But for the blockchain calls we should implement some logic to verify it right?

That is correct, at least for transaction data, blocks are trickier since we'll have to fetch the whole block (and all it's transactions) to do this verification, but we can provide the option.

@chefsale
Copy link
Member

Yes, we should use it with caution and provide feedback to people who use fetch directly. That is why we should close this PR and implement the near fetch using the generic fetch with some verification.

@miraclx
Copy link
Member

miraclx commented Jun 17, 2024

That is why we should close this PR and implement the near fetch using the generic fetch with some verification.

we shouldn't need to close it, just expand on it

Cargo.toml Outdated Show resolved Hide resolved
crates/near-sdk/Cargo.toml Outdated Show resolved Hide resolved
crates/near-sdk/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/near-sdk/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/near-sdk/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/near-sdk/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/near-sdk/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/near-sdk/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/near-sdk/src/lib.rs Outdated Show resolved Hide resolved
crates/near-sdk/src/lib.rs Outdated Show resolved Hide resolved
crates/near-sdk/src/lib.rs Outdated Show resolved Hide resolved
Base automatically changed from 368-add-a-fetch-function-inside-the-runtime-host-environment to master June 18, 2024 06:37
@its-saeed its-saeed self-assigned this Jun 18, 2024
@its-saeed its-saeed marked this pull request as ready for review June 18, 2024 16:22
crates/runtime/src/logic.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/runtime/src/logic.rs Outdated Show resolved Hide resolved
crates/runtime/src/logic.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/lib.rs Outdated Show resolved Hide resolved
crates/primitives/Cargo.toml Outdated Show resolved Hide resolved
crates/sdk/libs/near/Cargo.toml Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
crates/sdk/libs/near/Cargo.toml Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/lib.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/lib.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/error.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/lib.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.

Actionable comments posted: 6

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6f05dfe and 8ffddab.

Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
Files selected for processing (14)
  • Cargo.toml (3 hunks)
  • apps/only-peers/Cargo.toml (1 hunks)
  • crates/primitives/Cargo.toml (1 hunks)
  • crates/primitives/src/common.rs (1 hunks)
  • crates/primitives/src/events.rs (1 hunks)
  • crates/primitives/src/identity.rs (1 hunks)
  • crates/runtime/src/logic.rs (1 hunks)
  • crates/sdk/libs/near/Cargo.toml (1 hunks)
  • crates/sdk/libs/near/src/error.rs (1 hunks)
  • crates/sdk/libs/near/src/jsonrpc.rs (1 hunks)
  • crates/sdk/libs/near/src/lib.rs (1 hunks)
  • crates/sdk/libs/near/src/query.rs (1 hunks)
  • crates/sdk/libs/near/src/types.rs (1 hunks)
  • crates/sdk/libs/near/src/views.rs (1 hunks)
Files skipped from review as they are similar to previous changes (11)
  • Cargo.toml
  • apps/only-peers/Cargo.toml
  • crates/primitives/Cargo.toml
  • crates/primitives/src/common.rs
  • crates/primitives/src/events.rs
  • crates/primitives/src/identity.rs
  • crates/runtime/src/logic.rs
  • crates/sdk/libs/near/Cargo.toml
  • crates/sdk/libs/near/src/error.rs
  • crates/sdk/libs/near/src/jsonrpc.rs
  • crates/sdk/libs/near/src/views.rs
Additional comments not posted (2)
crates/sdk/libs/near/src/types.rs (2)

1-11: Code organization and usage of serde_with for serialization.

The use of serde_with for Base64 encoding is appropriate for binary data types like StoreValue and StoreKey. This ensures efficient and correct serialization and deserialization of binary data.


45-58: Review and optimize serialization settings for FunctionArgs.

The FunctionArgs struct uses Base64 encoding, which is suitable for binary data. However, ensure that all uses of this struct require such encoding to avoid unnecessary performance overhead.

Verification successful

Base64 encoding for FunctionArgs is appropriate.

The usage of FunctionArgs in both crates/sdk/libs/near/src/views.rs and crates/application/src/lib.rs involves handling binary data, justifying the need for Base64 encoding.

  • crates/sdk/libs/near/src/views.rs: Used in CallFunction variant of QueryRequest with a field name args_base64.
  • crates/application/src/lib.rs: Used in constructing a QueryRequest::CallFunction request where JSON data is converted to bytes.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Search for all usages of `FunctionArgs` to ensure Base64 encoding is necessary.

# Test: Search for the struct usage. Expect: Only contexts where binary data is handled.
rg --type rust $'FunctionArgs'

Length of output: 486



Script:

#!/bin/bash
# Search for the context around the usage of `FunctionArgs` in `crates/sdk/libs/near/src/views.rs`.
rg -A 10 -B 10 'FunctionArgs' crates/sdk/libs/near/src/views.rs

Length of output: 1012



Script:

#!/bin/bash
# Search for the context around the usage of `FunctionArgs` in `crates/application/src/lib.rs`.
rg -A 10 -B 10 'FunctionArgs' crates/application/src/lib.rs

Length of output: 1500

crates/sdk/libs/near/src/lib.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/lib.rs Show resolved Hide resolved
crates/sdk/libs/near/src/types.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/query.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/query.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/query.rs Show resolved Hide resolved
crates/sdk/libs/near/src/error.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/query.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/jsonrpc.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/views.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/views.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/types.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/types.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/types.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/types.rs Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/query.rs Outdated Show resolved Hide resolved
@miraclx
Copy link
Member

miraclx commented Jun 24, 2024

@coderabbitai pause

Copy link

coderabbitai bot commented Jun 24, 2024

Actions performed

Reviews paused.

@xilosada xilosada changed the title feat: Add app-side library for NEAR feat(ext): add app-side library for NEAR Jun 25, 2024
@xilosada
Copy link
Member

@its-saeed Please add a test plan and a summary. Can you prepare the docs? This must have public docs @MatejVukosav

@its-saeed its-saeed requested a review from miraclx June 26, 2024 12:38
Copy link
Member

@miraclx miraclx left a comment

Choose a reason for hiding this comment

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

lgtm, now let's add an example that tests the library for basic things

like your test case in the description, but it's own application, you should receive specific variables from the inputs, and not hard coded

crates/sdk/libs/near/src/error.rs Outdated Show resolved Hide resolved
apps/only-peers/Cargo.toml Outdated Show resolved Hide resolved
crates/sdk/libs/near/src/views.rs Outdated Show resolved Hide resolved
@its-saeed
Copy link
Contributor Author

lgtm, now let's add an example that tests the library for basic things

like your test case in the description, but it's own application, you should receive specific variables from the inputs, and not hard coded

Should it be in the apps folder?

Copy link
Member

@xilosada xilosada left a comment

Choose a reason for hiding this comment

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

@miraclx let's keep current scope. If the code is ok please approve the PR. @its-saeed review the comments you are adding in the codebase.

crates/runtime/src/logic.rs Outdated Show resolved Hide resolved
Copy link
Member

@miraclx miraclx left a comment

Choose a reason for hiding this comment

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

approving to unblock, let's get this merged 🎉

@its-saeed its-saeed merged commit 0fe9ad4 into master Jun 26, 2024
12 checks passed
@its-saeed its-saeed deleted the 370-add-near-app-side-library branch June 26, 2024 17:44
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.

7 participants