Skip to content

Commit

Permalink
Merge pull request #497 from odradev/feature/js-client-2024
Browse files Browse the repository at this point in the history
RPC client: Replaced blocking reqwest with async version.
  • Loading branch information
kubaplas authored Jun 28, 2024
2 parents c958ea3 + 202a2ef commit f17a38c
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 213 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ serde = { version = "1.0.195", default-features = false }
serde_json = { version = "1.0.113", default-features = false }
num-traits = { version = "0.2.14", default-features = false }
mockall = { version = "0.12.1" }
tokio = "1.38"
1 change: 1 addition & 0 deletions odra-casper/livenet-env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ odra-core = { workspace = true }
odra-casper-rpc-client = { workspace = true }
blake2 = { workspace = true }
log = { workspace = true }
tokio = { workspace = true, features = ["rt-multi-thread"]}

[lints.rust]
missing_docs = "warn"
54 changes: 36 additions & 18 deletions odra-casper/livenet-env/src/livenet_contract_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,54 @@ use odra_core::{Address, OdraError};
use odra_core::{CallDef, ContractContext, ContractRegister};
use std::io::Write;
use std::sync::RwLock;
use tokio::runtime::Runtime;

/// Livenet contract environment struct.
pub struct LivenetContractEnv {
casper_client: Rc<RefCell<CasperClient>>,
callstack: Rc<RefCell<Callstack>>,
contract_register: Rc<RwLock<ContractRegister>>
contract_register: Rc<RwLock<ContractRegister>>,
runtime: Runtime
}

impl ContractContext for LivenetContractEnv {
fn get_value(&self, key: &[u8]) -> Option<Bytes> {
self.casper_client
.borrow()
.get_value(self.callstack.borrow().current().address(), key)
.ok()
let callstack = self.callstack.borrow();
let client = self.casper_client.borrow();
self.runtime.block_on(async {
client
.get_value(callstack.current().address(), key)
.await
.ok()
})
}

fn set_value(&self, _key: &[u8], _value: Bytes) {
panic!("Cannot set value in LivenetEnv without a deploy")
}

fn get_named_value(&self, name: &str) -> Option<Bytes> {
self.casper_client
.borrow()
.get_named_value(self.callstack.borrow().current().address(), name)
let client = self.casper_client.borrow();
let callstack = self.callstack.borrow();
self.runtime.block_on(async {
client
.get_named_value(callstack.current().address(), name)
.await
})
}

fn set_named_value(&self, _name: &str, _value: CLValue) {
panic!("Cannot set named value in LivenetEnv without a deploy")
}

fn get_dictionary_value(&self, dictionary_name: &str, key: &[u8]) -> Option<Bytes> {
self.casper_client.borrow().get_dictionary_value(
self.callstack.borrow().current().address(),
dictionary_name,
key
)
let callstack = self.callstack.borrow();
let client = self.casper_client.borrow();
self.runtime.block_on(async {
client
.get_dictionary_value(callstack.current().address(), dictionary_name, key)
.await
})
}

fn set_dictionary_value(&self, _dictionary_name: &str, _key: &[u8], _value: CLValue) {
Expand Down Expand Up @@ -85,17 +97,20 @@ impl ContractContext for LivenetContractEnv {
}

fn get_block_time(&self) -> u64 {
self.casper_client.borrow().get_block_time()
let client = self.casper_client.borrow();
self.runtime
.block_on(async { client.get_block_time().await })
}

fn attached_value(&self) -> U512 {
self.callstack.borrow().attached_value()
}

fn self_balance(&self) -> U512 {
self.casper_client
.borrow()
.get_balance(self.callstack.borrow().current().address())
let client = self.casper_client.borrow();
let callstack = self.callstack.borrow();
self.runtime
.block_on(async { client.get_balance(callstack.current().address()).await })
}

fn emit_event(&self, _event: &Bytes) {
Expand Down Expand Up @@ -162,7 +177,10 @@ impl LivenetContractEnv {
Rc::new(RefCell::new(Self {
casper_client,
callstack,
contract_register
contract_register,
runtime: Runtime::new().unwrap_or_else(|_| {
panic!("Couldn't create tokio runtime");
})
}))
}
}
94 changes: 58 additions & 36 deletions odra-casper/livenet-env/src/livenet_host.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Livenet implementation of HostContext for HostEnv.
use std::sync::RwLock;
use std::thread::sleep;

use crate::livenet_contract_env::LivenetContractEnv;
use odra_casper_rpc_client::casper_client::CasperClient;
use odra_casper_rpc_client::log::info;
use odra_core::callstack::{Callstack, CallstackElement};
use odra_core::casper_types::bytesrepr::ToBytes;
use odra_core::casper_types::Timestamp;
use odra_core::entry_point_callback::EntryPointsCaller;
use odra_core::{
casper_types::{bytesrepr::Bytes, PublicKey, RuntimeArgs, U512},
Expand All @@ -13,8 +13,9 @@ use odra_core::{
};
use odra_core::{prelude::*, EventError, OdraResult};
use odra_core::{ContractContainer, ContractRegister};

use crate::livenet_contract_env::LivenetContractEnv;
use std::sync::RwLock;
use std::thread::sleep;
use tokio::runtime::Runtime;

/// LivenetHost struct.
pub struct LivenetHost {
Expand Down Expand Up @@ -67,7 +68,9 @@ impl HostContext for LivenetHost {
}

fn balance_of(&self, address: &Address) -> U512 {
self.casper_client.borrow().get_balance(address)
let rt = Runtime::new().unwrap();
let client = self.casper_client.borrow();
rt.block_on(async { client.get_balance(address).await })
}

fn advance_block_time(&self, time_diff: u64) {
Expand All @@ -79,20 +82,22 @@ impl HostContext for LivenetHost {
}

fn block_time(&self) -> u64 {
self.casper_client.borrow().get_block_time()
let rt = Runtime::new().unwrap();
let client = self.casper_client.borrow();
rt.block_on(async { client.get_block_time().await })
}

fn get_event(&self, contract_address: &Address, index: u32) -> Result<Bytes, EventError> {
self.casper_client
.borrow()
.get_event(contract_address, index)
let rt = Runtime::new().unwrap();
let client = self.casper_client.borrow();
rt.block_on(async { client.get_event(contract_address, index).await })
.map_err(|_| EventError::CouldntExtractEventData)
}

fn get_events_count(&self, contract_address: &Address) -> u32 {
self.casper_client
.borrow()
.events_count(contract_address)
let rt = Runtime::new().unwrap();
let client = self.casper_client.borrow();
rt.block_on(async { client.events_count(contract_address).await })
.unwrap_or_default()
}

Expand All @@ -117,18 +122,46 @@ impl HostContext for LivenetHost {
self.callstack.borrow_mut().pop();
return result;
}
let timestamp = Timestamp::now();
let rt = Runtime::new().unwrap();
let client = self.casper_client.borrow_mut();
match use_proxy {
true => self
.casper_client
.borrow_mut()
.deploy_entrypoint_call_with_proxy(*address, call_def),
false => self
.casper_client
.borrow_mut()
.deploy_entrypoint_call(*address, call_def)
true => rt.block_on(async {
client
.deploy_entrypoint_call_with_proxy(*address, call_def, timestamp)
.await
}),
false => {
rt.block_on(async {
client
.deploy_entrypoint_call(*address, call_def, timestamp)
.await
})?;
Ok(
().to_bytes()
.expect("Couldn't serialize (). This shouldn't happen.")
.into()
)
}
}
}

fn new_contract(
&self,
name: &str,
init_args: RuntimeArgs,
entry_points_caller: EntryPointsCaller
) -> OdraResult<Address> {
let timestamp = Timestamp::now();
let address = {
let mut client = self.casper_client.borrow_mut();
let rt = Runtime::new().unwrap();
rt.block_on(async { client.deploy_wasm(name, init_args, timestamp).await })?
};
self.register_contract(address, name.to_string(), entry_points_caller);
Ok(address)
}

fn register_contract(
&self,
address: Address,
Expand All @@ -144,20 +177,6 @@ impl HostContext for LivenetHost {
.register_name(address, contract_name);
}

fn new_contract(
&self,
name: &str,
init_args: RuntimeArgs,
entry_points_caller: EntryPointsCaller
) -> OdraResult<Address> {
let address = self
.casper_client
.borrow_mut()
.deploy_wasm(name, init_args)?;
self.register_contract(address, name.to_string(), entry_points_caller);
Ok(address)
}

fn contract_env(&self) -> ContractEnv {
(*self.contract_env).clone()
}
Expand All @@ -184,6 +203,9 @@ impl HostContext for LivenetHost {
}

fn transfer(&self, to: Address, amount: U512) -> OdraResult<()> {
self.casper_client.borrow_mut().transfer(to, amount)
let rt = Runtime::new().unwrap();
let timestamp = Timestamp::now();
let client = self.casper_client.borrow_mut();
rt.block_on(async { client.transfer(to, amount, timestamp).await })
}
}
3 changes: 2 additions & 1 deletion odra-casper/rpc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ prettycli = "0.1.1"
thiserror = "1.0.40"
bytes = "1.5.0"
anyhow = "1.0.86"
humantime = "2.1.0"
humantime = "2.1.0"
tokio = { workspace = true }
Loading

0 comments on commit f17a38c

Please sign in to comment.