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

Feature/livenet on 0.8 #316

Merged
merged 25 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Generated by Cargo
# will have compiled files and executables
.*
*.env
!/.gitignore
kubaplas marked this conversation as resolved.
Show resolved Hide resolved
/target/
Cargo.lock
.vscode/
Expand Down
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ members = [
"odra-vm",
"core",
"odra-macros",
"odra-casper/livenet-env",
"odra-casper/casper-client",
"odra-casper/wasm-env",
"odra-casper/test-vm",
"odra-test",
"try-from-macro"
# needs a refactor
# "odra-casper/livenet"
]
exclude = [ "examples", "examples2", "modules", "odra-casper/proxy-caller" ]
resolver = "2"
Expand All @@ -25,4 +25,5 @@ repository = "https://github.com/odradev/odra"
casper-contract = { version = "3.0.0", default-features = false }
casper-types = { version = "3.0.0", default-features = false }
casper-execution-engine = "5.0.0"
casper-event-standard = "0.4.1"
casper-event-standard = "0.4.1"
blake2 = "0.10.6"
4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
casper-types = "3.0.0"
casper-event-standard = "0.4.0"
casper-types = { workspace = true }
casper-event-standard = { workspace = true }
12 changes: 9 additions & 3 deletions core/src/call_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ use casper_types::{CLTyped, RuntimeArgs, U512};
pub struct CallDef {
pub entry_point: String,
pub args: RuntimeArgs,
pub amount: U512
pub amount: U512,
is_mut: bool
kubaplas marked this conversation as resolved.
Show resolved Hide resolved
}

impl CallDef {
pub fn new(method: String, args: RuntimeArgs) -> Self {
pub fn new(method: String, is_mut: bool, args: RuntimeArgs) -> Self {
CallDef {
entry_point: method,
args,
amount: U512::zero()
amount: U512::zero(),
is_mut
}
}

Expand All @@ -38,4 +40,8 @@ impl CallDef {
pub fn attached_value(&self) -> U512 {
self.amount
}

pub fn is_mut(&self) -> bool {
self.is_mut
}
}
14 changes: 13 additions & 1 deletion odra-vm/src/vm/callstack.rs → core/src/callstack.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use odra_core::{casper_types::U512, Address, CallDef};
use super::{casper_types::U512, Address, CallDef};
use crate::prelude::*;

#[derive(Clone)]
pub enum CallstackElement {
Expand Down Expand Up @@ -31,6 +32,13 @@ impl Entrypoint {
pub struct Callstack(Vec<CallstackElement>);

impl Callstack {
pub fn first(&self) -> CallstackElement {
self.0
.first()
.expect("Not enough elements on callstack")
.clone()
}

pub fn pop(&mut self) -> Option<CallstackElement> {
self.0.pop()
}
Expand Down Expand Up @@ -66,4 +74,8 @@ impl Callstack {
pub fn len(&self) -> usize {
self.0.len()
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use odra_core::call_def::CallDef;
use odra_core::entry_point_callback::EntryPointsCaller;
use odra_core::prelude::*;
use odra_core::HostEnv;
use odra_core::{
casper_types::{NamedArg, RuntimeArgs},
Bytes, OdraError, VmError
};
use crate::prelude::*;
kubaplas marked this conversation as resolved.
Show resolved Hide resolved
use crate::{CallDef, EntryPointsCaller, OdraError, VmError};
use casper_types::bytesrepr::Bytes;
use casper_types::{NamedArg, RuntimeArgs};

#[doc(hidden)]
pub type EntrypointCall = fn(String, &RuntimeArgs) -> Vec<u8>;
Expand All @@ -14,15 +10,14 @@ pub type EntrypointArgs = Vec<String>;

#[derive(Clone)]
pub struct ContractContainer {
name: String,

_name: String,
kubaplas marked this conversation as resolved.
Show resolved Hide resolved
entry_points_caller: EntryPointsCaller
}

impl ContractContainer {
pub fn new(name: &str, entry_points_caller: EntryPointsCaller) -> Self {
Self {
name: String::from(name),
_name: String::from(name),
entry_points_caller
}
}
Expand All @@ -37,7 +32,7 @@ impl ContractContainer {
self.entry_points_caller.call(call_def)
}

fn validate_args(&self, args: &[String], input_args: &RuntimeArgs) -> Result<(), OdraError> {
fn _validate_args(&self, args: &[String], input_args: &RuntimeArgs) -> Result<(), OdraError> {
kubaplas marked this conversation as resolved.
Show resolved Hide resolved
// TODO: What's the purpose of this code? Is it needed?
let named_args = input_args
.named_args()
Expand All @@ -60,15 +55,13 @@ impl ContractContainer {

#[cfg(test)]
mod tests {
use odra_core::prelude::{collections::*, *};
use odra_core::{
use super::{ContractContainer, EntrypointArgs, EntrypointCall};
use crate::prelude::{collections::*, *};
use crate::{
casper_types::{runtime_args, RuntimeArgs},
OdraError, VmError
};
use odra_core::{EntryPointsCaller, HostEnv};
use url::Host;

use super::{ContractContainer, EntrypointArgs, EntrypointCall};
use crate::{EntryPointsCaller, HostEnv};

#[test]
fn test_call_wrong_entrypoint() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use odra_core::call_def::CallDef;
use odra_core::prelude::*;
use odra_core::HostEnv;
use odra_core::{casper_types::RuntimeArgs, Address, Bytes, OdraError, VmError};
use crate::call_def::CallDef;
use crate::prelude::*;
use crate::{Address, Bytes, OdraError, VmError};

use super::contract_container::ContractContainer;

Expand Down
1 change: 1 addition & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub enum ExecutionError {
ExceededRecursionDepth = 116,
KeyNotFound = 117,
CouldNotDeserializeSignature = 118,
TypeMismatch = 119,
MaxUserError = 32767,
/// User error too high. The code should be in range 0..32767.
UserErrorTooHigh = 32768,
Expand Down
4 changes: 3 additions & 1 deletion core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ pub enum EventError {
/// Unexpected error while deserializing.
Parsing,
/// Could not extract event name.
CouldntExtractName
CouldntExtractName,
/// Could not extract event data.
CouldntExtractEventData
}
4 changes: 3 additions & 1 deletion core/src/host_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use casper_types::PublicKey;

pub trait HostContext {
fn set_caller(&self, caller: Address);
fn set_gas(&self, gas: u64);
fn caller(&self) -> Address;
fn get_account(&self, index: usize) -> Address;
fn balance_of(&self, address: &Address) -> U512;
Expand All @@ -25,8 +26,9 @@ pub trait HostContext {
&self,
name: &str,
init_args: Option<RuntimeArgs>,
entry_points_caller: Option<EntryPointsCaller>
entry_points_caller: EntryPointsCaller
kubaplas marked this conversation as resolved.
Show resolved Hide resolved
) -> Address;
fn register_contract(&self, address: Address, entry_points_caller: EntryPointsCaller);
kubaplas marked this conversation as resolved.
Show resolved Hide resolved
fn contract_env(&self) -> ContractEnv;
fn print_gas_report(&self);
fn last_call_gas_cost(&self) -> u64;
Expand Down
19 changes: 18 additions & 1 deletion core/src/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl HostEnv {
&self,
name: &str,
init_args: Option<RuntimeArgs>,
entry_points_caller: Option<EntryPointsCaller>
entry_points_caller: EntryPointsCaller
) -> Address {
let backend = self.backend.borrow();
let deployed_contract = backend.new_contract(name, init_args, entry_points_caller);
Expand All @@ -57,6 +57,13 @@ impl HostEnv {
deployed_contract
}

pub fn register_contract(&self, address: Address, entry_points_caller: EntryPointsCaller) {
let backend = self.backend.borrow();
backend.register_contract(address, entry_points_caller);
self.deployed_contracts.borrow_mut().push(address);
self.events_count.borrow_mut().insert(address, 0);
}

pub fn call_contract<T: FromBytes + CLTyped>(
&self,
address: Address,
Expand Down Expand Up @@ -235,4 +242,14 @@ impl HostEnv {
let backend = self.backend.borrow();
backend.public_key(address)
}

pub fn caller(&self) -> Address {
let backend = self.backend.borrow();
backend.caller()
}

pub fn set_gas(&self, gas: u64) {
let backend = self.backend.borrow();
backend.set_gas(gas)
}
}
3 changes: 3 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ pub mod address;
pub mod arithmetic;
pub mod call_def;
pub mod call_result;
pub mod callstack;
pub mod consts;
pub mod contract_container;
mod contract_context;
pub mod contract_def;
mod contract_env;
pub mod contract_register;
pub mod crypto;
pub mod entry_point_callback;
pub mod error;
Expand Down
13 changes: 12 additions & 1 deletion examples/.env.sample
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
# .env file used by Livenet integration. You can use multiple .env files to manage deploys on multiple chains
# by naming them casper-test.env, casper-livenet.env, etc. and calling the deploy script with the name of the
# ennviroment provided in the "ODRA_CASPER_LIVENET_ENV" variable. For example:
# ODRA_CASPER_LIVENET_ENV=casper-test cargo run --bin livenet_tests --feature livenet
# This will load integration.env file first, and then fill the missing values with the values from casper-test.env.

# Path to the secret key of the account that will be used to deploy the contracts.
# ODRA_CASPER_LIVENET_SECRET_KEY_PATH=<path to secret_key.pem>

# RPC address of the node that will be used to deploy the contracts.
# ODRA_CASPER_LIVENET_NODE_ADDRESS=http://localhost:7777

# Chain name of the network. Known values:
# Chain name of the network. Sample values:
# - integration-test
# - casper-test
# ODRA_CASPER_LIVENET_CHAIN_NAME=<chain_name>

# Paths to the secret keys of the additional acccounts. Main secret key will be 0th account.
# ODRA_CASPER_LIVENET_KEY_1=./keys/secret_key_1.pem
# ODRA_CASPER_LIVENET_KEY_2=./keys/secret_key_2.pem
21 changes: 17 additions & 4 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ edition = "2021"
odra = { path = "../odra", default-features = false }
odra-modules = { path = "../modules", default-features = false }
sha3 = { version = "0.10.6", default-features = false }
odra-casper-livenet-env = { path = "../odra-casper/livenet-env", optional = true }

[dev-dependencies]
odra-test = { path = "../odra-test" }
hex = "0.4.3"

[features]
default = []
livenet = ["odra-casper-livenet-env"]

[[bin]]
name = "odra_examples_build_contract"
path = "bin/build_contract.rs"
Expand All @@ -22,13 +27,21 @@ name = "odra_examples_build_schema"
path = "bin/build_schema.rs"
test = false

[[bin]]
name = "erc20_on_livenet"
path = "bin/erc20_on_livenet.rs"
required-features = ["livenet"]
test = false

[[bin]]
name = "livenet_tests"
path = "bin/livenet_tests.rs"
required-features = ["livenet"]
test = false

[profile.release]
codegen-units = 1
lto = true

[profile.dev.package."*"]
opt-level = 3
#[[bin]]
#name = "erc20-on-livenet"
#path = "bin/erc20_on_livenet.rs"
#required-features = ["casper-livenet"]
4 changes: 4 additions & 0 deletions examples/Odra.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ fqn = "odra_examples::features::access_control::MockModerated"
[[contracts]]
name = "PauseableCounter"
fqn = "odra_examples::features::pauseable::PauseableCounter"

[[contracts]]
name = "LivenetContract"
fqn = "odra_examples::features::livenet::LivenetContract"
44 changes: 29 additions & 15 deletions examples/bin/erc20_on_livenet.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
use odra::{Address, HostEnv, U256};
use odra_modules::erc20::{Erc20Deployer, Erc20HostRef};
use std::str::FromStr;

fn main() {
let name = String::from("Plascoin");
let symbol = String::from("PLS");
let decimals = 10u8;
let initial_supply: U256 = U256::from(10_000);
let env = odra_casper_livenet_env::livenet_env();

let owner = client_env::caller();
let owner = env.caller();
let recipient = "hash-2c4a6ce0da5d175e9638ec0830e01dd6cf5f4b1fbb0724f7d2d9de12b1e0f840";
let recipient = Address::from_str(recipient).unwrap();

client_env::set_gas(110_000_000_000u64);
let mut token = Erc20Deployer::init(name, symbol, decimals, &Some(initial_supply));
// Deploy new contract.
let mut token = deploy_new(&env);
println!("Token address: {}", token.address().to_string());

// Uncomment to use already deployed contract.
// let address = "hash-a12760e3ece51e0f31aa6d5af39660f5ec61185ad61c7551c796cca4592b9498";
// let address = Address::from_str(address).unwrap();
// let mut token = Erc20Deployer::register(address);
// Uncomment to load existing contract.
// let mut token = load(&env);

println!("Token name: {}", token.name());

client_env::set_gas(3_000_000_000u64);
token.transfer(&recipient, &U256::from(1000));
env.set_gas(3_000_000_000u64);
token.transfer(recipient, U256::from(1000));

println!("Owner's balance: {:?}", token.balance_of(owner));
println!("Recipient's balance: {:?}", token.balance_of(recipient));
}

fn deploy_new(env: &HostEnv) -> Erc20HostRef {
let name = String::from("Plascoin");
let symbol = String::from("PLS");
let decimals = 10u8;
let initial_supply: U256 = U256::from(10_000);

env.set_gas(100_000_000_000u64);
Erc20Deployer::init(env, name, symbol, decimals, Some(initial_supply))
}

println!("Owner's balance: {:?}", token.balance_of(&owner));
println!("Recipient's balance: {:?}", token.balance_of(&recipient));
fn _load(env: &HostEnv) -> Erc20HostRef {
let address = "hash-d26fcbd2106e37be975d2045c580334a6d7b9d0a241c2358a4db970dfd516945";
let address = Address::from_str(address).unwrap();
Erc20Deployer::load(env, address)
kubaplas marked this conversation as resolved.
Show resolved Hide resolved
}
Loading