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 21 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
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ members = [
"odra-vm",
"core",
"odra-macros",
"odra-casper/livenet-env",
"odra-casper/casper-client",
"odra-casper/wasm-env",
"odra-casper/test-vm",
"odra-test"
# needs a refactor
# "odra-casper/livenet"
]
exclude = [ "examples", "examples2", "modules", "odra-casper/proxy-caller" ]
exclude = [ "examples", "modules", "odra-casper/proxy-caller" ]
resolver = "2"

[workspace.package]
Expand All @@ -24,4 +24,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,8 +4,8 @@ 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 }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
serde = { version = "1.0.195", default-features = false, features = ["alloc", "derive"] }
Expand Down
26 changes: 20 additions & 6 deletions core/src/call_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,35 @@ use casper_types::{CLTyped, RuntimeArgs, U512};

#[derive(Clone, Debug)]
pub struct CallDef {
pub entry_point: String,
pub args: RuntimeArgs,
pub amount: U512
entry_point: String,
args: RuntimeArgs,
amount: U512,
is_mut: bool
kubaplas marked this conversation as resolved.
Show resolved Hide resolved
}

impl CallDef {
pub fn new<T: ToString>(method: T, args: RuntimeArgs) -> Self {
pub fn new<T: ToString>(method: T, is_mut: bool, args: RuntimeArgs) -> Self {
CallDef {
entry_point: method.to_string(),
args,
amount: U512::zero()
amount: U512::zero(),
is_mut
}
}

pub fn with_amount(mut self, amount: U512) -> Self {
pub fn with_amount(mut self, amount: U512) -> CallDef {
self.amount = amount;
self
}

pub fn entry_point(&self) -> &str {
&self.entry_point
}

pub fn amount(&self) -> U512 {
self.amount
}

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

pub fn is_mut(&self) -> bool {
self.is_mut
}
}
18 changes: 15 additions & 3 deletions 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 All @@ -43,13 +51,13 @@ impl Callstack {
let ce = self.0.last().unwrap();
match ce {
CallstackElement::Account(_) => U512::zero(),
CallstackElement::Entrypoint(e) => e.call_def.amount
CallstackElement::Entrypoint(e) => e.call_def.amount()
}
}

pub fn attach_value(&mut self, amount: U512) {
if let Some(CallstackElement::Entrypoint(entrypoint)) = self.0.last_mut() {
entrypoint.call_def.amount = amount;
entrypoint.call_def = entrypoint.call_def.clone().with_amount(amount);
}
}

Expand All @@ -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,12 +1,7 @@
use odra_core::call_def::CallDef;
use odra_core::entry_point_callback::EntryPointsCaller;
use odra_core::prelude::*;
use odra_core::EntryPointArgument;
use odra_core::HostEnv;
use odra_core::{
casper_types::{NamedArg, RuntimeArgs},
Bytes, OdraError, VmError
};
use crate::prelude::*;
use crate::{CallDef, EntryPointArgument, EntryPointsCaller, OdraError, VmError};
kubaplas marked this conversation as resolved.
Show resolved Hide resolved
use casper_types::bytesrepr::Bytes;
use casper_types::{NamedArg, RuntimeArgs};

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

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

impl ContractContainer {
pub fn new(name: &str, entry_points_caller: EntryPointsCaller) -> Self {
pub fn new(entry_points_caller: EntryPointsCaller) -> Self {
Self {
name: String::from(name),
entry_points_caller
}
}
Expand All @@ -45,7 +38,7 @@ impl ContractContainer {
args: &[EntryPointArgument],
input_args: &RuntimeArgs
) -> Result<(), OdraError> {
let named_args = input_args
let _named_args = input_args
.named_args()
.map(NamedArg::name)
.collect::<Vec<_>>();
Expand Down Expand Up @@ -73,15 +66,13 @@ impl ContractContainer {

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

use super::{ContractContainer, EntrypointArgs, EntrypointCall};
use crate::{CLType, CallDef, EntryPoint, EntryPointArgument, EntryPointsCaller, HostEnv};

const TEST_ENTRYPOINT: &'static str = "ep";

Expand All @@ -91,7 +82,7 @@ mod tests {
let instance = ContractContainer::empty();

// When call some entrypoint.
let call_def = CallDef::new(TEST_ENTRYPOINT, RuntimeArgs::new());
let call_def = CallDef::new(TEST_ENTRYPOINT, false, RuntimeArgs::new());
let result = instance.call(call_def);

// Then an error occurs.
Expand All @@ -104,7 +95,7 @@ mod tests {
let instance = ContractContainer::with_entrypoint(vec![]);

// When call the registered entrypoint.
let call_def = CallDef::new(TEST_ENTRYPOINT, RuntimeArgs::new());
let call_def = CallDef::new(TEST_ENTRYPOINT, false, RuntimeArgs::new());
let result = instance.call(call_def);

// Then teh call succeeds.
Expand Down Expand Up @@ -149,7 +140,7 @@ mod tests {
let instance = ContractContainer::with_entrypoint(vec![("first", CLType::U32)]);

// When call a valid entrypoint without args.
let call_def = CallDef::new(TEST_ENTRYPOINT, RuntimeArgs::new());
let call_def = CallDef::new(TEST_ENTRYPOINT, false, RuntimeArgs::new());
let result = instance.call(call_def);

// Then MissingArg error is returned.
Expand All @@ -166,7 +157,7 @@ mod tests {
]);

// When call a valid entrypoint with a single valid args,
let call_def = CallDef::new(TEST_ENTRYPOINT, runtime_args! { "third" => 0u32 });
let call_def = CallDef::new(TEST_ENTRYPOINT, false, runtime_args! { "third" => 0u32 });
let result = instance.call(call_def);

// Then MissingArg error is returned.
Expand All @@ -182,7 +173,6 @@ mod tests {
)))
});
Self {
name: String::from("contract"),
entry_points_caller
}
}
Expand Down Expand Up @@ -210,7 +200,6 @@ mod tests {
);

Self {
name: String::from("contract"),
entry_points_caller
}
}
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 @@ -97,6 +97,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
}
6 changes: 4 additions & 2 deletions core/src/host_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ 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;
fn advance_block_time(&self, time_diff: u64);
/// Returns event bytes by contract address and index.
fn get_event(&self, contract_address: &Address, index: i32) -> Result<Bytes, EventError>;
fn get_event(&self, contract_address: &Address, index: u32) -> Result<Bytes, EventError>;
fn get_events_count(&self, contract_address: &Address) -> u32;
fn call_contract(
&self,
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
Loading
Loading