Skip to content

Commit

Permalink
🔧 Fix FMT
Browse files Browse the repository at this point in the history
  • Loading branch information
Roaring30s committed Sep 13, 2023
1 parent 71337bc commit 9f79a0c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 89 deletions.
17 changes: 9 additions & 8 deletions crates/cli/cli/parse.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/**
*
*
* @Purpose: Parse cmd arguments so main.rs has data to execute other libs.
* Parse Command Arguments and determine whether cmd is 'Run', 'DryRun' or 'Serve'.
* Upon Match, grab the rest of the data from 'pargs' and feed it into the 'Flags' enum.
* ParseResult enum which houses the Flags enum is then returned inside Ok()
*
*
* @Note: Code to understand right away
* `subcommand()` - grabs first argument in cmd i.e. 'Run', 'DryRun' or 'Serve'
* `opt_value_from_str(key)` - sees if an optional key was provided & extracts value, ex. [--height, 42] -> 42
* `unwrap_or_else()` - extract value or do something else, ex. Option<String> becomes String
* `as_deref()` - converts an Option<String> into Option<&str> so we can borrow and not clone any values, more efficient.
*
*
*/

use crate::print_help::print_help;
use pico_args::Arguments;
use std::ops::Deref;
Expand Down Expand Up @@ -65,8 +64,8 @@ fn parse_node_limit(
}

pub fn parse() -> Result<ParseResult, pico_args::Error> {
let mut pargs = Arguments::from_env(); //Ex. -> Arguments(["arg1", "arg2", "arg3"])
let is_help = pargs.contains("--help"); //Checks if user entered help flag
let mut pargs = Arguments::from_env(); //Ex. -> Arguments(["arg1", "arg2", "arg3"])
let is_help = pargs.contains("--help"); //Checks if user entered help flag

/**
* subcommand -> Ok(Some("arg1")) grabs first argument
Expand All @@ -80,11 +79,13 @@ pub fn parse() -> Result<ParseResult, pico_args::Error> {
.unwrap_or("Unknown")
.to_string();

if is_help { //Store cmd with --help flag inside Help struct
if is_help {
//Store cmd with --help flag inside Help struct
Ok(ParseResult::Help {
cmd: String::from(cmd),
})
} else { //CHECK IF subcommand was dry-run, run or serve
} else {
//CHECK IF subcommand was dry-run, run or serve
#[allow(clippy::wildcard_in_or_patterns)]
let flags = match cmd.deref() {
"dry-run" => ParseResult::Known {
Expand Down
42 changes: 22 additions & 20 deletions crates/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
* @Purpose
* Parses cmd arguments.
* Initiates a runtime to manage concurrency and schedule tasks
*
*
*
*
*/

// Imports the moduls in the sister files next to main
mod cli;
mod core_nodes;
Expand All @@ -31,39 +30,40 @@ fn main() -> Result<(), AnyError> {
let parse_result = parse::parse()?;

/**
* @Runtime
* Will Manage Concurrent Tasks with Ease
* @Runtime
* Will Manage Concurrent Tasks with Ease
* Such as async operations, start/pause/schedule tasks
*
*
* What is a runtime?
* A layer on top of the OS.
* A layer on top of the OS.
* Executable files are placed into RAM, the processor turns the executable into machine code and runs the program.
* The lifetime execution of the program is the runtime.
*
* The lifetime execution of the program is the runtime.
*
* Why use Runtime?
* Rust has built in async/await and thread operations
* But more complex ops such as task scheduling, thread pools, or asynchronous I/O
* need an external system to manage this for us.
* Building a thread pool from scratch would require use to manually make
* need an external system to manage this for us.
* Building a thread pool from scratch would require use to manually make
* a dedicated Worker struct that saves JoinHandle types responsible for tracking a thread.
* And of course there are tons of other features we would have to build that tokio automates.
*
* And of course there are tons of other features we would have to build that tokio automates.
*
* @Note Functions to learn
* `block_on` - entry point into the runtime, WILL BLOCK main thread - need to see why this was picked.
* `block_on` - entry point into the runtime, WILL BLOCK main thread - need to see why this was picked.
* `future` - a future task to be completed
* `DryRun` - testing the programming logic without starting the program
*
*
* @Note Flags::Run is the entry point to execute an instance of the contract ready for IO-bound operations
*
*
*/
let rt = tokio::runtime::Runtime::new()?;

match parse_result {
ParseResult::Help { cmd } => {
print_help::print_help(Some(cmd.deref())); //Prints message containing list of cmds, options etc.
print_help::print_help(Some(cmd.deref())); //Prints message containing list of cmds, options etc.
}

ParseResult::Known { flag } => { // Match whether user wants Run, DryRun, Serve
ParseResult::Known { flag } => {
// Match whether user wants Run, DryRun, Serve
match flag {
Flags::Run {
port,
Expand Down Expand Up @@ -102,7 +102,8 @@ fn main() -> Result<(), AnyError> {
))?;
}
}
Flags::DryRun { // Used to test code
Flags::DryRun {
// Used to test code
host,
port,
protocol,
Expand All @@ -124,7 +125,8 @@ fn main() -> Result<(), AnyError> {
))?;
}
}
Flags::Serve { //Spins up a local testnet
Flags::Serve {
//Spins up a local testnet
server_port,
server_host,
} => {
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn run(
let state = data.state;
let validity_table = data.validity;
let result = data.result.unwrap_or(serde_json::Value::Null);
// return state and result when endpoint is reached.
// return state and result when endpoint is reached.
let value = if show_validity {
serde_json::json!({
"state": state,
Expand Down
14 changes: 8 additions & 6 deletions crates/executor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use three_em_wasm::WasmRuntime;
/*
* Questions:
* opsmartweavereadcontract - uses executecontract instead of simulate
*
*
*
*
*/

pub type ValidityTable = IndexMap<String, Value>;
Expand Down Expand Up @@ -59,7 +59,8 @@ pub type OnCached = dyn Fn() -> ExecuteResult;
pub fn get_execution_context(
maybe_context: Result<ExmContext, AnyError>,
) -> ExmContext {
if let Ok(exm_known_context) = maybe_context { //Check for existence of maybe_context, else create new hashmap
if let Ok(exm_known_context) = maybe_context {
//Check for existence of maybe_context, else create new hashmap
exm_known_context
} else {
ExmContext {
Expand All @@ -74,8 +75,8 @@ pub fn process_execution(
execute_result: ExecuteResult,
show_validity: bool,
) -> Value {
match execute_result {
ExecuteResult::V8(result) => {
match execute_result {
ExecuteResult::V8(result) => {
if show_validity {
serde_json::json!({
"state": result.state,
Expand Down Expand Up @@ -111,7 +112,8 @@ pub async fn op_smartweave_read_contract(
state: Rc<RefCell<OpState>>,
(contract_id, height, show_validity): (String, Option<usize>, Option<bool>),
_: (),
) -> Result<Value, AnyError> { //Reads the state in the contract, not write to it
) -> Result<Value, AnyError> {
//Reads the state in the contract, not write to it
let op_state = state.borrow();
let info = op_state.borrow::<three_em_smartweave::ArweaveInfo>(); //Borrow data in ArweaveInfo type
let cl = Arweave::new(
Expand Down
26 changes: 13 additions & 13 deletions crates/executor/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ use three_em_evm::Instruction;
use three_em_evm::U256;
/**
* @Purpose - Enables execution of contracts
*
*
* Questions.
* what is interactions and maybe exm context
*
*
* elaborate on the loaded contract with tokio::join
*
*
* what does raw_execute_contract call exactly? Is this the final step that will really simulate a contract
*
* how is execute contract different from simulate_contract and why does mem-core rely on simulate_contract instead.
*
*
* how is execute contract different from simulate_contract and why does mem-core rely on simulate_contract instead.
*
* Which crates matter in 3em to cover anything and everythng arweave, not EVM
*
* Biggest take home here is to how to grab this data base and test out each piece of the code.
*
*
* Biggest take home here is to how to grab this data base and test out each piece of the code.
*
*/
static LRU_CACHE: Lazy<Mutex<LruCache<String, ExecuteResult>>> =
Lazy::new(|| Mutex::new(LruCache::unbounded()));
Expand All @@ -63,7 +63,7 @@ pub async fn simulate_contract(
* else
* load_contract based on contract id
* return
* loaded contract
* loaded contract
*
*/
// There is no reason to join - you join here to run async tasks concurrently.
Expand Down Expand Up @@ -108,10 +108,10 @@ pub async fn simulate_contract(
);

/**
*
*
* maybe_exm_context_str != None, save it
*
*
*
*
*/
if loaded_contract.is_ok() {
let maybe_exm_context = {
Expand Down
85 changes: 44 additions & 41 deletions crates/js/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ mod test {
#[tokio::test]
async fn test_putKv() {
let mut exec_settings: HashMap<String, deno_core::serde_json::Value> =
HashMap::new();
HashMap::new();
exec_settings.insert(
String::from("EXM"),
deno_core::serde_json::Value::Bool(true),
Expand All @@ -458,29 +458,30 @@ mod test {
(12345, String::from("arweave.net"), String::from("http")),
never_op::decl(),
exec_settings,
None
None,
)
.await
.unwrap();
.await
.unwrap();

rt.call((), None).await.unwrap();
let calls = rt.get_exm_context::<ExmContext>().unwrap();
let state = rt.get_contract_state::<deno_core::serde_json::Value>().unwrap();

let state = rt
.get_contract_state::<deno_core::serde_json::Value>()
.unwrap();

let variable_type = format!("{:?}", state.as_str().unwrap());

println!("**Testing PUT: {}", state.as_str().unwrap());
let res = calls.kv.get("hello").unwrap();
println!("**Test PUT 2: {}", res);
assert_eq!(res, "world");

}

// GET
#[tokio::test]
async fn test_getKv() {
let mut exec_settings: HashMap<String, deno_core::serde_json::Value> =
HashMap::new();
HashMap::new();
exec_settings.insert(
String::from("EXM"),
deno_core::serde_json::Value::Bool(true),
Expand All @@ -502,31 +503,32 @@ mod test {
(12345, String::from("arweave.net"), String::from("http")),
never_op::decl(),
exec_settings,
None
None,
)
.await
.unwrap();
.await
.unwrap();

rt.call((), None).await.unwrap();

let state = rt.get_contract_state::<deno_core::serde_json::Value>().unwrap();
let state = rt
.get_contract_state::<deno_core::serde_json::Value>()
.unwrap();

println!("**Testing GET: {}", state);
//assert_eq!(state.as_str().unwrap(), "hello world");
}

}

// DELETE
#[tokio::test]
async fn test_delKv() {
let mut exec_settings: HashMap<String, deno_core::serde_json::Value> =
HashMap::new();
exec_settings.insert(
String::from("EXM"),
deno_core::serde_json::Value::Bool(true),
);
let mut rt = Runtime::new(
r#"
// DELETE
#[tokio::test]
async fn test_delKv() {
let mut exec_settings: HashMap<String, deno_core::serde_json::Value> =
HashMap::new();
exec_settings.insert(
String::from("EXM"),
deno_core::serde_json::Value::Bool(true),
);
let mut rt = Runtime::new(
r#"
export async function handle() {
try {
EXM.print("Data");
Expand All @@ -539,22 +541,23 @@ mod test {
}
}
"#,
(),
(12345, String::from("arweave.net"), String::from("http")),
never_op::decl(),
exec_settings,
None
)
.await
.unwrap();

rt.call((), None).await.unwrap();

let state = rt.get_contract_state::<deno_core::serde_json::Value>().unwrap();
println!("**Testing DEL: {}", state);
//assert_eq!(state.as_str().unwrap(), "hello world");

}
(),
(12345, String::from("arweave.net"), String::from("http")),
never_op::decl(),
exec_settings,
None,
)
.await
.unwrap();

rt.call((), None).await.unwrap();

let state = rt
.get_contract_state::<deno_core::serde_json::Value>()
.unwrap();
println!("**Testing DEL: {}", state);
//assert_eq!(state.as_str().unwrap(), "hello world");
}

#[tokio::test]
async fn test_state_empty() {
Expand Down

0 comments on commit 9f79a0c

Please sign in to comment.