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

Server only api #37

Closed
wants to merge 52 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
d173ad2
add validation for the 'from' an 'to' params of the 'compute_transfer…
jaensen May 17, 2023
a24e84d
fixed format strings for error messages
jaensen May 17, 2023
5144dfb
fixed format strings for error messages
jaensen May 17, 2023
b19584e
moved the rpc call dispatching to rpc_handler.rs and the function imp…
jaensen May 17, 2023
1ad37fe
added a CallContext for loggin of rpc requests and moved the rpc call…
jaensen May 18, 2023
de6324f
log startup info
jaensen May 18, 2023
2201bf9
dropped the incremental chunked results for simpler code. + log resul…
jaensen May 18, 2023
fd7268c
removed all code that's not directly required for the server use of t…
jaensen May 18, 2023
d7579f4
more logging
jaensen May 18, 2023
6668e27
trial and erroring a memory leak :/
jaensen May 18, 2023
b7992c4
more logging, more dropping
jaensen May 18, 2023
5b10d16
removed trace logging after the pathfinder ran for ~ 8h without issues.
jaensen May 19, 2023
96befac
unfinished: Allow to 'push' graph updates via http.
jaensen May 23, 2023
2aece90
get the test-data from the 'dev' branch
jaensen May 23, 2023
5320349
trying different trust_transfer_limit implementation
jaensen May 23, 2023
0626107
restored 'trust_transfer_limit' logic
jaensen May 24, 2023
cd108e0
fixed build warning
jaensen May 24, 2023
80aed20
refactored the compute_flow function to better show the data flow and…
jaensen May 24, 2023
7b89934
moved 'trust_transfer_limit' from safe to db
jaensen May 25, 2023
ca465fb
re-added fn default() -> CallContext for use in tests
jaensen May 25, 2023
e268db6
add capacity edges from users that hold accepted tokens to orgas
jaensen May 25, 2023
159a951
removed unnecessary field
jaensen May 25, 2023
5950ab8
add flow-edges from users with trusted tokens to the organizations th…
jaensen May 30, 2023
ee593af
OP-159: fix(workflow): add feature/server_only branch trigger, remove…
almereyda Jul 20, 2023
60fa1b1
Merge pull request #42 from CirclesUBI/fix/op-159-server_only
almereyda Jul 20, 2023
4baf76d
Edit compute_edges
Jul 24, 2023
9b1d4e8
update with fixed workflows
JacqueGM Aug 7, 2023
48fce9e
fix format
JacqueGM Aug 7, 2023
b37076d
Merge branch 'dev' of https://github.com/CirclesUBI/pathfinder2 into …
JacqueGM Aug 7, 2023
7f3050a
fix format
JacqueGM Aug 7, 2023
ad3398d
update lint issues
JacqueGM Aug 7, 2023
a6f2198
fix lint
JacqueGM Aug 7, 2023
a9fd2d2
fix map and lint
JacqueGM Aug 7, 2023
0d273ed
update branch
JacqueGM Aug 7, 2023
bbac07b
avoid building for server_only for now
JacqueGM Aug 10, 2023
2f07179
fix lint and add test back
JacqueGM Aug 10, 2023
2c187c0
git test fmt
JacqueGM Aug 10, 2023
9e7d27a
add default trait
JacqueGM Aug 10, 2023
329302d
adding test but with error with call_context
JacqueGM Aug 10, 2023
9b7db63
fix format
JacqueGM Aug 10, 2023
98b3c17
fix call_context error
JacqueGM Aug 10, 2023
cf3b970
remove some test to start
JacqueGM Aug 10, 2023
d3125e9
Exclude edges with equeal source and destination
Aug 24, 2023
4091056
Make compiler happy
Aug 27, 2023
a022f69
Resolve merge conflicts
Aug 27, 2023
c94dbc8
Remove repeated comments
Aug 27, 2023
bc2ac09
Fix imports
Aug 27, 2023
a0d0c57
Merge pull request #43 from CirclesUBI/compute-edges-all
JacqueGM Aug 28, 2023
e8aec86
update build work
JacqueGM Aug 28, 2023
18d2cc3
apply format changes
JacqueGM Aug 28, 2023
d515a68
update clippy failing
JacqueGM Aug 28, 2023
9c53202
fix fmt
JacqueGM Aug 28, 2023
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ json = "^0.12.4"
num-bigint = "^0.4.3"
serde = { version = "1.0.149", features = ["serde_derive"] }
serde_json = "1.0.89"
regex = "1.8.1"
69 changes: 45 additions & 24 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::sync::mpsc::TrySendError;
use std::sync::{mpsc, Arc, Mutex, RwLock};
use std::thread;
use num_bigint::BigUint;
use regex::Regex;

struct JsonRpcRequest {
id: JsonValue,
Expand All @@ -35,6 +36,37 @@ impl Display for InputValidationError {
}
}

fn validate_and_parse_ethereum_address(address: &str) -> Result<Address, Box<dyn Error>> {
let re = Regex::new(r"^0x[0-9a-fA-F]{40}$").unwrap();
if re.is_match(address) {
Ok(Address::from(address))
} else {
Err(Box::new(InputValidationError(format!(
"Invalid Ethereum address: {}",
address
))))
}
}

fn validate_and_parse_u256(value_str: &str) -> Result<U256, Box<dyn Error>> {
match BigUint::from_str(value_str) {
Ok(parsed_value) => {
if parsed_value > U256::MAX.into() {
Err(Box::new(InputValidationError(format!(
"Value {} is too large. Maximum value is {}.",
parsed_value, U256::MAX
))))
} else {
Ok(U256::from_bigint_truncating(parsed_value))
}
}
Err(e) => Err(Box::new(InputValidationError(format!(
"Invalid value: {}. Couldn't parse value: {}",
value_str, e
)))),
}
}

pub fn start_server(listen_at: &str, queue_size: usize, threads: u64) {
let edges: Arc<RwLock<Arc<EdgeDB>>> = Arc::new(RwLock::new(Arc::new(EdgeDB::default())));

Expand All @@ -46,7 +78,7 @@ pub fn start_server(listen_at: &str, queue_size: usize, threads: u64) {
thread::spawn(move || loop {
let socket = rec.lock().unwrap().recv().unwrap();
if let Err(e) = handle_connection(e.deref(), socket) {
println!("Error handling connection: {e}");
println!("Error handling connection: {}", e);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait what is this? I would have expected the opposite as that is the one recommended by cargo clippy.

}
});
}
Expand All @@ -62,7 +94,7 @@ pub fn start_server(listen_at: &str, queue_size: usize, threads: u64) {
panic!("Internal communication channel disconnected.");
}
},
Err(e) => println!("Error accepting connection: {e}"),
Err(e) => println!("Error accepting connection: {}", e),
}
}
}
Expand All @@ -77,7 +109,7 @@ fn handle_connection(
let response = match load_edges_binary(edges, &request.params["file"].to_string()) {
Ok(len) => jsonrpc_response(request.id, len),
Err(e) => {
jsonrpc_error_response(request.id, -32000, &format!("Error loading edges: {e}"))
jsonrpc_error_response(request.id, -32000, &format!("Error loading edges: {}", e))
}
};
socket.write_all(response.as_bytes())?;
Expand All @@ -86,7 +118,7 @@ fn handle_connection(
let response = match load_edges_csv(edges, &request.params["file"].to_string()) {
Ok(len) => jsonrpc_response(request.id, len),
Err(e) => {
jsonrpc_error_response(request.id, -32000, &format!("Error loading edges: {e}"))
jsonrpc_error_response(request.id, -32000, &format!("Error loading edges: {}", e))
}
};
socket.write_all(response.as_bytes())?;
Expand All @@ -95,7 +127,7 @@ fn handle_connection(
let response = match load_safes_binary(edges, &request.params["file"].to_string()) {
Ok(len) => jsonrpc_response(request.id, len),
Err(e) => {
jsonrpc_error_response(request.id, -32000, &format!("Error loading edges: {e}"))
jsonrpc_error_response(request.id, -32000, &format!("Error loading edges: {}", e))
}
};
socket.write_all(response.as_bytes())?;
Expand Down Expand Up @@ -156,37 +188,26 @@ fn compute_transfer(
socket.write_all(chunked_header().as_bytes())?;

let parsed_value_param = match request.params["value"].as_str() {
Some(value_str) => match BigUint::from_str(value_str) {
Ok(parsed_value) => parsed_value,
Err(e) => {
return Err(Box::new(InputValidationError(format!(
"Invalid value: {}. Couldn't parse value: {}",
value_str, e
))));
}
},
None => U256::MAX.into(),
Some(value_str) => validate_and_parse_u256(value_str)?,
None => U256::MAX,
};

if parsed_value_param > U256::MAX.into() {
return Err(Box::new(InputValidationError(format!(
"Value {} is too large. Maximum value is {}.",
parsed_value_param, U256::MAX
))));
}
let from_address = validate_and_parse_ethereum_address(&request.params["from"].to_string())?;
let to_address = validate_and_parse_ethereum_address(&request.params["to"].to_string())?;

let max_distances = if request.params["iterative"].as_bool().unwrap_or_default() {
vec![Some(1), Some(2), None]
} else {
vec![None]
};

let max_transfers = request.params["max_transfers"].as_u64();
for max_distance in max_distances {
let (flow, transfers) = graph::compute_flow(
&Address::from(request.params["from"].to_string().as_str()),
&Address::from(request.params["to"].to_string().as_str()),
&from_address,
&to_address,
edges,
U256::from_bigint_truncating(parsed_value_param.clone()),
parsed_value_param,
max_distance,
max_transfers,
);
Expand Down