Skip to content

Commit

Permalink
remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
jaensen committed Oct 29, 2023
1 parent 60fa1b1 commit 9ed2cde
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 241 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
name = "pathfinder2"
version = "0.1.0"
edition = "2021"
default-run = "server"

[lib]
name = "pathfinder2"
crate-type = ["cdylib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -13,3 +16,4 @@ num-bigint = "^0.4.3"
serde = { version = "1.0.149", features = ["serde_derive"] }
serde_json = "1.0.89"
regex = "1.8.1"
lazy_static = "1.4"
23 changes: 0 additions & 23 deletions src/bin/server.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/graph/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ pub fn compute_flow(
call_context.log_message(format!("Flow after limiting transfer steps to {}: {}", max_transfers.unwrap_or_default(), flow.to_decimal()).as_str());

let transfers = create_sorted_transfers(source, sink, flow, used_edges, call_context);
// call_context.log_message(format!("Transfers: {:?}", transfers).as_str());

(flow, transfers)
}
Expand Down
8 changes: 4 additions & 4 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub fn import_from_safes_binary(path: &str) -> Result<DB, io::Error> {
// trust edges
for _ in 0..read_u32(&mut f)? {
let user = read_address(&mut f, &address_index)?;
assert!(user != Address::default());
assert_ne!(user, Address::default());
let send_to = read_address(&mut f, &address_index)?;
assert!(send_to != Address::default());
assert_ne!(send_to, Address::default());
let limit_percentage = read_u8(&mut f)?;
assert!(limit_percentage <= 100);

Expand All @@ -40,9 +40,9 @@ pub fn import_from_safes_binary(path: &str) -> Result<DB, io::Error> {
// balances
for _ in 0..read_u32(&mut f)? {
let user = read_address(&mut f, &address_index)?;
assert!(user != Address::default());
assert_ne!(user, Address::default());
let token_owner = read_address(&mut f, &address_index)?;
assert!(token_owner != Address::default());
assert_ne!(token_owner, Address::default());
let balance = read_u256(&mut f)?;
if balance != U256::from(0) {
safes
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod graph;
pub mod io;
pub mod safe_db;
pub mod server;
pub mod types;
pub mod rpc;
45 changes: 4 additions & 41 deletions src/rpc/call_context.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,17 @@
use std::fmt;
use std::fmt::{Display, Formatter};
use std::sync::Arc;
use std::time::SystemTime;
use json::JsonValue;
use crate::safe_db::edge_db_dispenser::{EdgeDbDispenser, EdgeDBVersion};

pub struct CallContext {
pub client_ip: String,
pub request_id: JsonValue,
pub rpc_function: String,
pub start_time: std::time::Instant,
pub dispenser: Arc<EdgeDbDispenser>,
pub version: Option<EdgeDBVersion>,
}

impl CallContext {
pub(crate) fn default() -> CallContext {
CallContext {
client_ip: "".to_string(),
request_id: JsonValue::Null,
rpc_function: "".to_string(),
start_time: std::time::Instant::now(),
dispenser: Arc::new(EdgeDbDispenser::new()),
version: None,
}
}
}

impl CallContext {
pub fn new(client_ip: &str, request_id: &JsonValue, rpc_function: &str, dispenser: &Arc<EdgeDbDispenser>) -> Self {
pub fn new(dispenser: &Arc<EdgeDbDispenser>) -> Self {
let version = dispenser.get_latest_version();
let context = CallContext {
client_ip: client_ip.to_string(),
request_id: request_id.clone(),
rpc_function: rpc_function.to_string(),
start_time: std::time::Instant::now(),
dispenser: dispenser.clone(),
version: version,
Expand All @@ -51,18 +29,12 @@ impl CallContext {

let suffix_str = suffix.unwrap_or("");

if self.client_ip.is_empty() && self.request_id.is_null() && self.rpc_function.is_empty() {
println!("{}{}", prefix, suffix_str);
return;
}

let thread_id_string = format!("{:?}", std::thread::current().id());
let version_number = self.version.as_ref().map(|v| v.version_number).unwrap_or(0);

println!(
"{} {} [{}] [{}] [{}] [{}] [{}] {}",
prefix, timestamp, thread_id_string, self.client_ip, self.request_id, self.rpc_function,
version.unwrap_or(&version_number.to_string()), suffix_str
"{} {} [{}] [{}] {}",
prefix, timestamp, thread_id_string, version.unwrap_or(&version_number.to_string()), suffix_str
);
}

Expand All @@ -79,16 +51,7 @@ impl Drop for CallContext {
self.dispenser.try_release_version(version);
}

if self.client_ip.is_empty() && self.request_id.is_null() && self.rpc_function.is_empty() {
return;
}
let call_duration = self.start_time.elapsed().as_millis();
self.log("<-", Some(&format!(" (took {} ms)", call_duration)), Some(&version_number.to_string()));
}
}

impl Display for CallContext {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "[{}] [{}]", self.client_ip, self.request_id)
}
}
}
1 change: 0 additions & 1 deletion src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod rpc_functions;
pub mod rpc_handler;
pub mod call_context;
45 changes: 45 additions & 0 deletions src/rpc/rpc_functions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::error::Error;
use std::ffi::CString;
use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;
use json::JsonValue;
Expand All @@ -8,6 +9,16 @@ use crate::io::{import_from_safes_binary};
use crate::types::{Address, U256};
use regex::Regex;
use crate::rpc::call_context::CallContext;
use lazy_static::lazy_static;
use std::sync::{Arc, Mutex};
use crate::safe_db::edge_db_dispenser::EdgeDbDispenser;
use json::parse as json_parse;
use std::os::raw::c_char;

// Global state for edge_db_dispenser
lazy_static! {
static ref EDGE_DB_DISPENSER: Mutex<Option<Arc<EdgeDbDispenser>>> = Mutex::new(None);
}

pub struct JsonRpcRequest {
pub id: JsonValue,
Expand All @@ -31,9 +42,43 @@ impl Display for InputValidationError {
}
}

#[no_mangle]
pub extern "C" fn ffi_initialize() {
let edge_db_dispenser: Arc<EdgeDbDispenser> = Arc::new(EdgeDbDispenser::new());
*EDGE_DB_DISPENSER.lock().unwrap() = Some(edge_db_dispenser);
}


#[no_mangle]
pub extern "C" fn ffi_load_safes_binary(file: *const c_char) -> usize {
let file_str = unsafe { std::ffi::CStr::from_ptr(file).to_str().unwrap() };
let dispenser = EDGE_DB_DISPENSER.lock().unwrap().as_ref().unwrap().clone();
let call_context = CallContext::new(&dispenser); // Unwrap the parsed JSON
let result = load_safes_binary(file_str, &call_context).unwrap_or(0);

result
}

#[no_mangle]
pub extern "C" fn ffi_compute_transfer(request_json: *const c_char) -> *mut c_char {
let request_str = unsafe { std::ffi::CStr::from_ptr(request_json).to_str().unwrap() };
let parsed_json = json_parse(request_str).unwrap();
let request = JsonRpcRequest {
id: parsed_json["id"].clone(),
method: parsed_json["method"].as_str().unwrap_or_default().to_string(),
params: parsed_json["params"].clone(),
};
let dispenser = EDGE_DB_DISPENSER.lock().unwrap().as_ref().unwrap().clone();
let call_context = CallContext::new(&dispenser);
let result = compute_transfer(&request, &call_context).unwrap_or(json::object! {});
let c_string = CString::new(result.dump()).unwrap();
c_string.into_raw()
}

pub fn load_safes_binary(file: &str, call_context: &CallContext) -> Result<usize, Box<dyn Error>> {
let updated_edges = import_from_safes_binary(file)?.edges().clone();
let len = updated_edges.edge_count();

call_context.dispenser.update(updated_edges);
Ok(len)
}
Expand Down
116 changes: 0 additions & 116 deletions src/rpc/rpc_handler.rs

This file was deleted.

43 changes: 0 additions & 43 deletions src/server.rs

This file was deleted.

Loading

0 comments on commit 9ed2cde

Please sign in to comment.