Skip to content
This repository has been archived by the owner on May 21, 2023. It is now read-only.

Commit

Permalink
Version 0.0.1 (#9)
Browse files Browse the repository at this point in the history
* Version 0.0.1

Co-authored-by: Kuba Płaskonka <[email protected]>
Co-authored-by: Krzysztof Pobiarżyn <[email protected]>
  • Loading branch information
3 people authored Aug 9, 2022
1 parent 65aa4ff commit e458bfd
Show file tree
Hide file tree
Showing 42 changed files with 824 additions and 1,340 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/odra-casper-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: odra-casper-ci

on:
push:
branches:
- master
- develop
paths-ignore:
- "**.md"

pull_request:
branches:
- master
- develop
- feature/*
paths-ignore:
- "**.md"

jobs:
build:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v1
- run: make prepare
- run: make build-test-env
- run: make check-lint
- run: make test
11 changes: 4 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# Generated by Cargo
# will have compiled files and executables
/*/target/
/*/Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
.idea
*/target/
*/Cargo.lock
test_env/getter_proxy/target
test_env/getter_proxy/Cargo.lock
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2022 odradev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
prepare:
sudo apt install wabt
rustup target add wasm32-unknown-unknown

test:
cd shared && cargo test
cd backend && cargo test

build-test-env:
cd test_env && cargo build --release

clippy:
cd backend && cargo clippy --all-targets -- -D warnings
cd shared && cargo clippy --all-targets -- -D warnings
cd test_env && cargo clippy --all-targets -- -D warnings
cd test_env/getter_proxy && cargo clippy --all-targets -- -D warnings

check-lint: clippy
cd backend && cargo fmt -- --check
cd shared && cargo fmt -- --check
cd test_env && cargo fmt -- --check
cd test_env/getter_proxy && cargo fmt -- --check

lint: clippy
cd backend && cargo fmt
cd shared && cargo fmt
cd test_env && cargo fmt
cd test_env/getter_proxy && cargo fmt

clean:
cd backend && cargo clean
cd shared && cargo clean
cd test_env && cargo clean
cd test_env/getter_proxy && cargo clean
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Odra Casper

Implementation of the Casper backend for the Odra.
9 changes: 9 additions & 0 deletions backend/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog

Changelog for `odra-casper-backend`.

## [0.0.1] - 2022-07-18
### Added
- `codegen` module that is used to generate wasm file.
- `backend` module that exports all required `#[no_mangle]` functions and interacts with the Casper Host.
- `CHANGELOG.md` and `README.md` files.
23 changes: 12 additions & 11 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
[package]
name = "casper_backend"
version = "0.1.0"
name = "odra-casper-backend"
version = "0.0.1"
edition = "2021"
authors = ["Jakub Płaskonka <[email protected]>", "Krzysztof Pobiarżyn <[email protected]>", "Maciej Zieliński <[email protected]>"]
license = "MIT"
repository = "https://github.com/odradev/odra-casper"
description = "Odra backend bindings and codegen utility for the Casper Blockchain."
keywords = ["wasm", "webassembly", "blockchain"]
categories = ["wasm", "smart contracts"]

[dependencies]
odra = { git = "https://github.com/odradev/odra.git", default-features = false, features = [ "wasm" ] }
# odra = { path = "../../../odra/core", default-features = false, features = ["wasm"] }
casper-contract = { version = "1.4.4", default-features = false, features = ["std", "test-support"] }
casper-commons = { path = "../common", features = [ "wasm" ] }
casper-contract = { version = "1.4.4", default-features = false, features = ["std"] }
casper-types = "1.5.0"
proc-macro2 = "1.0.39"
quote = "1.0.18"
syn = "1.0.96"
hex = "0.4.3"
convert_case = "0.5.0"
pretty_assertions = "1.2.1"
lazy_static = "1.4.0"

[features]
default = [ "codegen", "backend" ]
codegen = []
backend = []
odra-casper-shared = { version = "0.0.1", path = "../shared" }
odra = { version = "0.0.1", default-features = false, features = [ "wasm" ] }
4 changes: 4 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Odra Casper Backend

This crate implements `#[no_mangle]` bindings for `odra::external_api::contract_env::ContractEnv`.
It also provides codegen capabilites used to build a Casper WebAssembly file.
64 changes: 27 additions & 37 deletions backend/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
mod casper_env;
use lazy_static::lazy_static;

use std::{
collections::{hash_map::DefaultHasher, BTreeMap},
hash::{Hash, Hasher},
marker::PhantomData,
sync::Mutex,
};
pub use casper_commons::{odra_address_wrapper::OdraAddressWrapper, address::Address};
pub use casper_contract::{
self,
contract_api::{runtime, storage},
};
use odra::types::{URef, Key, Address as OdraAddress, CLValue, ContractPackageHash, RuntimeArgs, EventData, OdraError};
pub use casper_types;
use odra::types::{Address as OdraAddress, CLValue, EventData, ExecutionError, RuntimeArgs};
pub use odra_casper_shared::casper_address::CasperAddress;

use crate::casper_env;

#[no_mangle]
pub fn __get_blocktime() -> u64 {
Expand All @@ -21,60 +15,56 @@ pub fn __get_blocktime() -> u64 {

#[no_mangle]
pub fn __caller() -> OdraAddress {
casper_env::caller().into()
OdraAddress::try_from(casper_env::caller()).unwrap()
}

#[no_mangle]
pub fn __set_var(key: &[u8], value: &CLValue) {
let name = std::str::from_utf8(key).unwrap();
casper_env::set_cl_value(name, value.clone());
pub fn __self_address() -> OdraAddress {
OdraAddress::try_from(casper_env::self_address()).unwrap()
}

#[no_mangle]
fn __get_var(key: &[u8]) -> Option<CLValue> {
let name = std::str::from_utf8(key).unwrap();
casper_env::get_cl_value(name)
pub fn __set_var(key: &str, value: &CLValue) {
casper_env::set_cl_value(key, value.clone());
}

#[no_mangle]
fn __set_dict_value(dict: &[u8], key: &[u8], value: &CLValue) {
let dict = std::str::from_utf8(dict).unwrap();
casper_env::set_dict_value(dict, key, value);
fn __get_var(key: &str) -> Option<CLValue> {
casper_env::get_cl_value(key)
}

#[no_mangle]
fn __get_dict_value(dict: &[u8], key: &[u8]) -> Option<CLValue> {
let dict = std::str::from_utf8(dict).unwrap();
casper_env::get_dict_value(dict, key)
fn __set_dict_value(dict: &str, key: &[u8], value: &CLValue) {
casper_env::set_dict_value(dict, key, value);
}

#[no_mangle]
fn __revert(reason: &OdraError) -> ! {
let code = match reason {
OdraError::ExecutionError(code, _) => *code,
_ => 0
};
casper_env::revert(code);
fn __get_dict_value(dict: &str, key: &[u8]) -> Option<CLValue> {
casper_env::get_dict_value(dict, key)
}

#[no_mangle]
fn __print(message: &str) {
casper_env::print(message);
fn __revert(reason: &ExecutionError) -> ! {
casper_env::revert(reason.code());
}

// #[no_mangle]
// fn __print(message: &str) {
// casper_env::print(message);
// }

#[no_mangle]
pub fn __call_contract(address: &OdraAddress, entrypoint: &str, args: &RuntimeArgs) -> Vec<u8> {
let address: Address = OdraAddressWrapper::new(*address).into();
casper_env::call_contract(address, entrypoint, args.clone())
let casper_address = CasperAddress::try_from(*address).unwrap();
casper_env::call_contract(casper_address, entrypoint, args.clone())
}

#[no_mangle]
fn __emit_event(event: &EventData) {
casper_env::emit_event(event);
}

// @TODO: rename to
pub fn is_named_arg_exist(name: &str) -> bool {
pub fn named_arg_exists(name: &str) -> bool {
let mut arg_size: usize = 0;
let ret = unsafe {
casper_contract::ext_ffi::casper_get_named_arg_size(
Expand All @@ -83,5 +73,5 @@ pub fn is_named_arg_exist(name: &str) -> bool {
&mut arg_size as *mut usize,
)
};
odra::types::api_error::result_from(ret).is_ok()
casper_types::api_error::result_from(ret).is_ok()
}
Loading

0 comments on commit e458bfd

Please sign in to comment.