Giter VIP home page Giter VIP logo

api3-rust's Introduction

API3-Rust

This is the repo for RUST implementation of API3's Beacon Server

Common

Common package used for all the subsequent chain implementations. To run all test

cd common
cargo test

Solana

Read up on anchors https://book.anchor-lang.com/. To build the solana code, do the following in docker container (.devcontainer/Solana-Dockerfile):

cd solana/beacon-server
anchor build
solana-keygen new
anchor test

Instead of docker, you can follow installation commands from docker image.

Near

Prerequisite

Read up on Near from these links:

  • Get started: https://docs.near.org/docs/develop/contracts/overview
  • Create account: https://docs.near.org/docs/develop/basics/create-account#creating-a-testnet-account
  • Cross contract call: https://docs.near.org/docs/tutorials/contracts/xcc-receipts
  • End to end test: https://docs.near.org/docs/develop/contracts/rust/testing-rust-contracts#end-to-end-tests After reading the above, you should be able to know:
  • The basic syntax of writing a Near contract using rust
  • How to create test accounts
  • How and why we need cross contract call
  • How end to end test with javascript is written and tested

Dev

To setup the dev env, checkout the dockerfile in .devcontainer/Near-Dockerfile, build and launch the docker. Alternatively, we recommend you use vscode remote docker plugin, you can open this folder in remote docker, then you can have the same dev env. Once in the docker, you can follow the subsection accordingly.

Compile

  • go to the near contract folder: cd near/contract
  • compile: cargo build --target wasm32-unknown-unknown --release Once done you should be able to see, relative to the repo root folder, target/wasm32-unknown-unknown/release/dapi_server.wasm.

Create test accounts

You need to create 3 accounts for testing:

CONTRACT_ACCOUNT: the account for the dapiServer contract.
ADMIN_ACCOUNT: the default admin of the contract.
USER_ACCOUNT: test util account, mainly for reading data points with unlimited access for data verification.

Now go to near testnet and create the above accounts, you can choose your own names. Remember to define the above env variables with the account names, i.e. for our dev env, it's:

export CONTRACT_ACCOUNT=dapi-contract1.testnet
export ADMIN_ACCOUNT=mocha-test1.testnet
export USER_ACCOUNT=user-test1.testnet

Login on CLI

Once the acconts are created, you need to login from CLI:

near login --account-id ${CONTRACT_ACCOUNT}
near login --account-id ${ADMIN_ACCOUNT}
near login --account-id ${USER_ACCOUNT}

Deploy the contracts

In the root folder, deploy the api3-contract using:

near deploy --wasmFile ./target/wasm32-unknown-unknown/release/dapi_server.wasm --accountId=${CONTRACT_ACCOUNT}

If you get error on not enough balance. Run near dev-deploy ... and delete generated dev-xxxx account in favour of your account: near delete dev-xxxx ${CONTRACT_ACCOUNT}

Once you have deployed the contract, perform some santiy checks to ensure proper deployment, such as:

# This should pass with no problem
near call <CONTRACT_ACCOUNT> grant_role '{"role":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"who":"user-test1.testnet"}' --accountId <ADMIN_ACCOUNT>
# This should return true
near view <CONTRACT_ACCOUNT> has_role '{"role":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"who":"user-test1.testnet"}'
# This should pass with no problem
near call <CONTRACT_ACCOUNT> revoke_role '{"role":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"who":"user-test1.testnet"}' --accountId <ADMIN_ACCOUNT>
# This should return false
near view <CONTRACT_ACCOUNT> has_role '{"role":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"who":"user-test1.testnet"}'

Run tests

The tests are located in near/client-test, the main for tests is near/client-test/tests/test.spec.js. Use npm install if you have not setup before.

To run the test: cd near/client-test && yarn jest. Please note that the tests will take around 10 minutes to finish. At the same time, it is also running against a live network, sometimes there will be timeout errors, but near would retry automatically.

When the contract reverts execution, the near client would log the contract execution error with console.warn. The tests would capture the exceptions thrown and check the expected error name appears in the error message. If you want to disable the warn logs, use yarn jest --silent.

Clean up

To clean up, just delete the accounts using near delete ... .... See https://docs.near.org/docs/tools/near-cli#near-delete.

api3-rust's People

Contributors

ivanceras avatar mpetrunic avatar redoudou avatar willeslau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

api3-rust's Issues

Usage of getrandom crate in the contract will result in: Error happened during instantiation

Describe the bug

  • The getrandom provides a way to return random numbers
    • This is implemented for various targets such as
      • std
      • wasm-unknown-unknown, via js feature flag.
  • However, when used in a contract code for NEAR, though the contract code is compiled to wasm-unknown-unknown target, it is not run in the browser, therefore the random implementation through js will not be to connect to the the contract runtime when the contract ask for the random number.

Expected Behavior

Current Behavior

  • The error: "Error happened during instantiation" will occur.

Possible Solution

  • Remove any dependencies that may have used getrandom directly or indirectly.
    • Use cargo-tree to check if there is getrandom in your contract dependency tree.
  • If removing the important crates such as ethabi, put them behind a feature flag.

To Reproduce

Steps to reproduce the behavior:

  1. Make a simplistic contract, add getrandom with "js" as feature flag.
    • alternatively, you could also add a crate which has a direct/indirect dependency to getrandom
  2. compile the contract and run the test
    • all test will pass
  3. deploy the contract
  4. interact with the contract using near-cli,

Log output

Log Output
paste log output...

Specification

  • rustc version:
  • pint version:
  • commit tag:
  • commit hash:
  • operating system:
  • additional links:

Implement Solana contract call interface with anchor

Setup the contract call interface.
With JS client that invokes the methods on the original solidity contracts, returns dummy data.

Run the chain locally and query against local chain.
If possible, deploy to solana devnet for public testing.

beacon::tests::encode_packed_works fails

Issue summary

RUST_BACKTRACE=1 cargo test -- beacon::tests::encode_packed_works
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
package:   /home/adm0/src/github.com/ChainSafe/api3-rust/near/contract/Cargo.toml
workspace: /home/adm0/src/github.com/ChainSafe/api3-rust/Cargo.toml
    Finished test [unoptimized + debuginfo] target(s) in 0.24s
     Running unittests src/lib.rs (target/debug/deps/api3_common-1ccd5c015c8e75ed)

running 1 test
test beacon::tests::encode_packed_works ... FAILED

failures:

---- beacon::tests::encode_packed_works stdout ----
thread 'beacon::tests::encode_packed_works' panicked at 'called `Result::unwrap()` on an `Err` value: TempalteIdZero', common/src/beacon.rs:320:60
stack backtrace:
   0: rust_begin_unwind
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/panicking.rs:143:14
   2: core::result::unwrap_failed
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/result.rs:1785:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/result.rs:1078:23
   4: api3_common::beacon::derive_beacon_id
             at ./src/beacon.rs:320:5
   5: api3_common::beacon::tests::encode_packed_works
             at ./src/beacon.rs:418:25
   6: api3_common::beacon::tests::encode_packed_works::{{closure}}
             at ./src/beacon.rs:409:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/ops/function.rs:227:5
   8: core::ops::function::FnOnce::call_once
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.


failures:
    beacon::tests::encode_packed_works

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 24 filtered out; finished in 0.02s

error: test failed, to rerun pass '-p api3-common --lib'

Ethabi uses getrandom which doesn't work on NEAR contract

Issue summary

  • Ethabi uses fixed-hash which uses rand which uses getrandom
  • getrandom can not be used in NEAR smart contracts

Possible solutions

  • Find a way to remove getrandom out of ethabi
  • Completely remove dependence of ethabi
  • Disable fixed-hash out of ethabi
  • Fork a version of ethabi with fixed-hash removed since it uses getrandom

Other information and links

Skipped value typecast checking: min and max for U256 equivalent for int224

require(
            decodedData >= type(int224).min && decodedData <= type(int224).max,
            "Value typecasting error"
        );

We skip this assertion from the implementation. We are using U256 in rust to contain int224 value from solidity.
We might look back into this if we encounter issue in the future.

Implementation details

  • solidity int224 is mapped to ethereum_types::U256

Testing details

Acceptance Criteria

Implement DataPoint manager service

Define the common data model, DataPoint struct and common storage traits.

pub struct Manager;

We should perform aggregation operation as according to contract: contracts/dapis/DapiServer.sol L128 - L141

Implement solana contract

Implement the full contract on solana:

  • updateBeaconWithSignedData
  • updateDapiWithBeacons
  • updateDapiWithSignedData
  • setName
  • nameToDataPointId (Do in TS client)
  • readWithDataPointId
  • readWithName
  • readerCanReadDataPoint
  • dataPointIdToReaderToWhitelistStatus (Do in TS client)
  • deriveBeaconId
  • deriveDapiId

near/client-test: fix contract name in postbuild

Issue summary

  • contract WASM file name varies, being dependent on initial CONTRACT_ACCOUNT which makes postbuild (yarn test)
    broken to run near tests

Other information and links

  • This is the fix that I had to apply, for example, but it would be different for different contributors

image

Implement sort, median and quickSelect

Acces Control Registry

  • AccessControlRegistry.sol
  • AccessControlRegistryAdminned.sol
  • AccessControlRegistryAdminnedWithManager.sol
  • AccessControlRegistryUser.sol
  • RoleDeriver.sol

Interfaces

  • IAccessControlRegistry.sol
  • IAccessControlRegistryAdminned.sol
  • IAccessControlRegistryAdminnedWithManager.sol
  • IAccessControlRegistryUser.sol

Utils

  • OwnableCallForwarder.sol
  • IOwnableCallForwarder.sol

_ dapis_

  • DapiReader.sol

  • DapiServer.sol

  • Median.sol

  • QuickSelect.sol

  • Sort.sol

  • IDapiReader.sol

  • IDapiServer.sol

Implement Median, QuickSelect and Sort in the common crate with test cases. This is the equivalent implementation of the following contracts:

- [x] Median.sol
- [x] QuickSelect.sol
- [x] Sort.sol

Upgrading smart contracts doesn't deploy the new code

Describe the bug

  • Issuing near deploy for the smart contract will not overwrite the previously deployed smart contract on the same configured address

Expected Behavior

Current Behavior

Possible Solution

  • recreate sub-accounts each time you deploy a new version of the smart contract

To Reproduce

Steps to reproduce the behavior:

  1. create smart contract
  2. deploy using near deploy
  3. interact with the contract using near-cli ie: near call counter1.testnet get_num
  4. change one of the methods to return something else
  5. interact with the contract using near-cli ie: near call counter1.testnet get_num
    • it will execute the old contract code and return the original value of the method of the old code.

Log output

Log Output
paste log output...

Specification

  • rustc version:
  • pint version:
  • commit tag:
  • commit hash:
  • operating system:
  • additional links:

Implement NEAR contract

Implement the full contract on solana:

  • updateBeaconWithSignedData
  • updateDapiWithBeacons
  • updateDapiWithSignedData
  • setName
  • nameToDataPointId
  • readWithDataPointId
  • readWithName
  • readerCanReadDataPoint
  • dataPointIdToReaderToWhitelistStatus
  • deriveBeaconId
  • deriveDapiId

Can not sign transactions for account test.near on network shared-test

Describe the bug

There is an inconsistent errors when trying to delpoy and test a contract in near protocol.

 FAIL  src/test.js
   Test suite failed to run

    Can not sign transactions for account test.near on network shared-test, no matching key pair found in InMemorySigner(UnencryptedFileSystemKeyStore(/data/chainsafe/api3-rust/near/client-test/neardev))

This could be affected by many files

  • ~/.near-credentials
  • ~/.near-settings
  • ./neardev/dev-account
  • ./neardev/dev-account.env
  • local storage in the browser

Expected Behavior

  • The test should be consistent when issued with the series of command
  • yarn clean
  • yarn
  • yarn deploy
  • yarn test

Current Behavior

  • When running yarn.test, it seems that the config.js and test-setup.js is not executed by jest.
    which cause it to use different account(test.near) and different network(shared-test) other than the configured in config.js

Possible Solution

To Reproduce

Steps to reproduce the behavior:

Log output

Log Output
paste log output...

Specification

  • rustc version:
  • pint version:
  • commit tag:
  • commit hash:
  • operating system:
  • additional links:

Track common methods to implement

  • Ethereum Common Types
  • DataPoint struct and serialization/deserialization
  • DataPoint aggregator
  • Storage trait
  • Whitelist and role management

fix typo TempalteIdZero

Issue summary

  • please rename TempalteIdZero to TemplateIdZero to fix the typo

Other information and links

image

Port Whitelist solidity contract into NEAR contract

Convert this code into rust for use in NEAR contract.

  • types
  • storage
contract Whitelist {
    struct WhitelistStatus {
        uint64 expirationTimestamp;
        uint192 indefiniteWhitelistCount;
    }

    mapping(bytes32 => mapping(address => WhitelistStatus))
        internal serviceIdToUserToWhitelistStatus;

    mapping(bytes32 => mapping(address => mapping(address => bool)))
        internal serviceIdToUserToSetterToIndefiniteWhitelistStatus;
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.