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

Add odra attributes #273

Merged
merged 14 commits into from
Dec 5, 2023
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"odra-macros",
"odra-casper/wasm-env",
"odra-casper/test-vm",
"try-from-macro"
# needs a refactor
# "odra-casper/livenet"
]
Expand Down
3 changes: 3 additions & 0 deletions core/src/contract_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ pub trait ContractContext {
fn emit_event(&self, event: &Bytes);
fn transfer_tokens(&self, to: &Address, amount: &U512);
fn revert(&self, error: OdraError) -> !;
fn get_named_arg(&self, name: &str) -> Bytes;
fn handle_attached_value(&self);
fn clear_attached_value(&self);
}
53 changes: 46 additions & 7 deletions core/src/contract_env.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::call_def::CallDef;
use crate::prelude::*;
use crate::{Address, Bytes, CLTyped, FromBytes, OdraError, ToBytes, U512};

use crate::key_maker;
pub use crate::ContractContext;
use crate::{prelude::*, ExecutionError};
use crate::{Address, Bytes, CLTyped, FromBytes, OdraError, ToBytes, U512};

pub struct ContractEnv {
index: u32,
Expand All @@ -20,15 +19,15 @@ impl ContractEnv {
}
}

pub fn duplicate(&self) -> Self {
pub fn __duplicate(&self) -> Self {
kpob marked this conversation as resolved.
Show resolved Hide resolved
Self {
index: self.index,
mapping_data: self.mapping_data.clone(),
backend: self.backend.clone()
}
}

pub fn current_key(&self) -> Vec<u8> {
pub(crate) fn current_key(&self) -> Vec<u8> {
kpob marked this conversation as resolved.
Show resolved Hide resolved
let index_bytes = key_maker::u32_to_hex(self.index);
let mapping_data_bytes = key_maker::bytes_to_hex(&self.mapping_data);
let mut key = Vec::new();
Expand All @@ -37,11 +36,11 @@ impl ContractEnv {
key
}

pub fn add_to_mapping_data(&mut self, data: &[u8]) {
pub(crate) fn add_to_mapping_data(&mut self, data: &[u8]) {
kpob marked this conversation as resolved.
Show resolved Hide resolved
self.mapping_data.extend_from_slice(data);
}

pub fn child(&self, index: u8) -> Self {
pub(crate) fn child(&self, index: u8) -> Self {
kpob marked this conversation as resolved.
Show resolved Hide resolved
Self {
index: (self.index << 4) + index as u32,
mapping_data: self.mapping_data.clone(),
Expand Down Expand Up @@ -102,3 +101,43 @@ impl ContractEnv {
backend.emit_event(&event.to_bytes().unwrap().into())
}
}

pub struct ExecutionEnv {
kpob marked this conversation as resolved.
Show resolved Hide resolved
env: Rc<ContractEnv>
}

impl ExecutionEnv {
pub fn new(env: Rc<ContractEnv>) -> Self {
Self { env }
}

pub fn non_reentrant_before(&self) {
let status: bool = self
kpob marked this conversation as resolved.
Show resolved Hide resolved
.env
.get_value(crate::consts::REENTRANCY_GUARD.as_slice())
.unwrap_or_default();
if status {
self.env.revert(ExecutionError::ReentrantCall);
}
self.env
.set_value(crate::consts::REENTRANCY_GUARD.as_slice(), true);
}

pub fn non_reentrant_after(&self) {
self.env
.set_value(crate::consts::REENTRANCY_GUARD.as_slice(), false);
}

pub fn handle_attached_value(&self) {
self.env.backend.borrow().handle_attached_value();
}

pub fn clear_attached_value(&self) {
self.env.backend.borrow().clear_attached_value();
}

pub fn get_named_arg<T: FromBytes>(&self, name: &str) -> T {
kpob marked this conversation as resolved.
Show resolved Hide resolved
let bytes = self.env.backend.borrow().get_named_arg(name);
T::from_bytes(&bytes).unwrap().0
zie1ony marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub use address::{Address, OdraAddress};
pub use call_def::CallDef;
pub use casper_event_standard;
pub use contract_context::ContractContext;
pub use contract_env::ContractEnv;
pub use contract_env::{ContractEnv, ExecutionEnv};
pub use entry_point_callback::EntryPointsCaller;
pub use error::{AddressError, CollectionError, ExecutionError, OdraError, VmError};
pub use host_context::HostContext;
Expand Down
2 changes: 1 addition & 1 deletion core/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<K: ToBytes, V> Mapping<K, V> {

impl<K: ToBytes, V> Mapping<K, V> {
fn env_for_key(&self, key: K) -> ContractEnv {
let mut env = self.parent_env.duplicate();
let mut env = self.parent_env.__duplicate();
let key = key.to_bytes().unwrap_or_default();
env.add_to_mapping_data(&key);
env
Expand Down
1 change: 0 additions & 1 deletion examples2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition = "2021"

[dependencies]
odra = { path = "../odra" }
odra-macros = { path = "../odra-macros" }

[[bin]]
name = "contract"
Expand Down
89 changes: 2 additions & 87 deletions examples2/src/counter_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use odra::Mapping;
use odra::Module;
use odra::ModuleWrapper;

#[odra_macros::module]
#[odra::module]
pub struct CounterPack {
env: Rc<ContractEnv>,
counter0: ModuleWrapper<Counter>,
Expand All @@ -22,7 +22,7 @@ pub struct CounterPack {
counters_map: Mapping<u8, Counter>
}

#[odra_macros::module]
#[odra::module]
impl CounterPack {
pub fn get_count(&self, index_a: u8, index_b: u8) -> u32 {
match index_a {
Expand Down Expand Up @@ -62,91 +62,6 @@ impl CounterPack {
}
}

#[cfg(odra_module = "CounterPack")]
#[cfg(target_arch = "wasm32")]
mod __counter_pack_wasm_parts {
use odra::casper_event_standard::Schemas;
use odra::odra_casper_wasm_env;
use odra::odra_casper_wasm_env::casper_contract::contract_api::runtime;
use odra::odra_casper_wasm_env::casper_contract::unwrap_or_revert::UnwrapOrRevert;
use odra::odra_casper_wasm_env::WasmContractEnv;
use odra::types::casper_types::{
CLType, CLTyped, CLValue, EntryPoint, EntryPointAccess, EntryPointType, EntryPoints, Group,
Parameter, RuntimeArgs
};
use odra::types::{runtime_args, Address, U256};
use odra::{prelude::*, ContractEnv};

use super::CounterPack;

extern crate alloc;

pub fn entry_points() -> EntryPoints {
let mut entry_points = EntryPoints::new();
entry_points.add_entry_point(EntryPoint::new(
"get_count",
alloc::vec![
Parameter::new("index_a", CLType::U8),
Parameter::new("index_b", CLType::U8),
],
CLType::U32,
EntryPointAccess::Public,
EntryPointType::Contract
));
entry_points.add_entry_point(EntryPoint::new(
"increment",
alloc::vec![
Parameter::new("index_a", CLType::U8),
Parameter::new("index_b", CLType::U8),
],
CLType::Unit,
EntryPointAccess::Public,
EntryPointType::Contract
));
entry_points
}

pub fn execute_call() {
odra::odra_casper_wasm_env::host_functions::install_contract(
entry_points(),
Schemas::new(),
None
);
}

pub fn execute_get_count() {
let index_a: u8 = runtime::get_named_arg("index_a");
let index_b: u8 = runtime::get_named_arg("index_b");
let env = WasmContractEnv::new();
let contract: CounterPack = CounterPack::new(Rc::new(env));
let result = contract.get_count(index_a, index_b);
runtime::ret(CLValue::from_t(result).unwrap_or_revert());
}

pub fn execute_increment() {
let index_a: u8 = runtime::get_named_arg("index_a");
let index_b: u8 = runtime::get_named_arg("index_b");
let env = WasmContractEnv::new();
let mut contract: CounterPack = CounterPack::new(Rc::new(env));
contract.increment(index_a, index_b);
}

#[no_mangle]
fn call() {
execute_call();
}

#[no_mangle]
fn get_count() {
execute_get_count();
}

#[no_mangle]
fn increment() {
execute_increment();
}
}

#[cfg(test)]
mod tests {
pub use super::*;
Expand Down
Loading
Loading