Skip to content

Commit

Permalink
🔨 KV Implementation (#234)
Browse files Browse the repository at this point in the history
* 🔨 KV Implementation

* 🔧 Fix FMT
  • Loading branch information
Roaring30s authored Sep 13, 2023
1 parent ebf412b commit 2d042cd
Show file tree
Hide file tree
Showing 10 changed files with 959 additions and 13 deletions.
26 changes: 24 additions & 2 deletions crates/cli/cli/parse.rs
Original file line number Diff line number Diff line change
@@ -1,3 +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 @@ -50,20 +64,28 @@ fn parse_node_limit(
}

pub fn parse() -> Result<ParseResult, pico_args::Error> {
let mut pargs = Arguments::from_env();
let is_help = pargs.contains("--help");
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
* as_deref -> Peels back the Ok() wrapper so its Some("arg1")
* unwrap_or -> Peels back <Some> or panics
* to_string -> Converts back to mutable string
*/
let cmd = pargs
.subcommand()?
.as_deref()
.unwrap_or("Unknown")
.to_string();

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
#[allow(clippy::wildcard_in_or_patterns)]
let flags = match cmd.deref() {
"dry-run" => ParseResult::Known {
Expand Down
41 changes: 40 additions & 1 deletion crates/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* @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;
mod dry_run;
Expand All @@ -21,13 +29,41 @@ use std::str::FromStr;
fn main() -> Result<(), AnyError> {
let parse_result = parse::parse()?;

/**
* @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.
* 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.
*
* 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
* 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.
*
* @Note Functions to learn
* `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()));
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
match flag {
Flags::Run {
port,
Expand All @@ -48,6 +84,7 @@ fn main() -> Result<(), AnyError> {
print_help::print_help(Some("run"));
println!("{}", "Option '--contract-id' is required");
} else {
//run a new Arweave object w/ cache established - blocking the thread until future task complete
rt.block_on(run::run(
port,
host,
Expand All @@ -66,6 +103,7 @@ fn main() -> Result<(), AnyError> {
}
}
Flags::DryRun {
// Used to test code
host,
port,
protocol,
Expand All @@ -88,6 +126,7 @@ fn main() -> Result<(), AnyError> {
}
}
Flags::Serve {
//Spins up a local testnet
server_port,
server_host,
} => {
Expand Down
6 changes: 4 additions & 2 deletions crates/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ pub async fn run(
no_cache: bool,
show_errors: bool,
) -> Result<(), AnyError> {
// Create a new Arweave Object with a new cache
let arweave = Arweave::new(port, host, protocol, ArweaveCache::new());
let start = std::time::Instant::now();

let execution =
//Run contract based on contract id - this is only a runtime so no input is sent here
let execution: ExecuteResult =
execute_contract(tx, height, !no_cache, show_errors, None, None, &arweave)
.await?;

Expand All @@ -39,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.
let value = if show_validity {
serde_json::json!({
"state": state,
Expand Down
Loading

0 comments on commit 2d042cd

Please sign in to comment.