Skip to content

Commit

Permalink
Move get_random_enclave_index to deposit.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ssantos21 committed Jun 17, 2024
1 parent 2fa53a2 commit 776ac9e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
37 changes: 34 additions & 3 deletions server/src/endpoints/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::str::FromStr;

use bitcoin::hashes::sha256;
use bitcoin::hashes::{sha256, Hash};
use rocket::{serde::json::Json, response::status, State, http::Status};
use secp256k1_zkp::{XOnlyPublicKey, schnorr::Signature, Message, Secp256k1, PublicKey};
use serde::{Serialize, Deserialize};
use serde_json::{Value, json};
use crate::server::StateChainEntity;
use crate::{server::StateChainEntity, server_config::Enclave};

#[get("/deposit/get_token")]
pub async fn get_token(statechain_entity: &State<StateChainEntity>) -> status::Custom<Json<Value>> {
Expand Down Expand Up @@ -75,6 +75,37 @@ pub async fn token_init(statechain_entity: &State<StateChainEntity>) -> status::
return status::Custom(Status::Ok, Json(response_body));
}

fn get_random_enclave_index(statechain_id: &str, enclaves: &Vec<Enclave>) -> Result<usize, String> {
let index_from_statechain_id = get_enclave_index_from_statechain_id(statechain_id, enclaves.len() as u32);

println!("index_from_statechain_id: {}", index_from_statechain_id);

let selected_enclave = enclaves.get(index_from_statechain_id).unwrap();
if selected_enclave.allow_deposit {
return Ok(index_from_statechain_id);
} else {
for (i, enclave) in enclaves.iter().enumerate() {
if enclave.allow_deposit {
return Ok(i);
}
}
}

Err("No valid enclave found with allow_deposit set to true".to_string())
}

fn get_enclave_index_from_statechain_id(statechain_id: &str, enclave_array_len: u32) -> usize {
let hash = sha256::Hash::hash(statechain_id.as_bytes());
let hash_bytes = hash.as_byte_array();
let mut bytes = [0u8; 16];
bytes.copy_from_slice(&hash_bytes[..16]);
let random_number = u128::from_be_bytes(bytes);

println!("statechain_id: {}", statechain_id);
println!("random_number: {}", random_number);
return (random_number % enclave_array_len as u128) as usize;
}

#[post("/deposit/init/pod", format = "json", data = "<deposit_msg1>")]
pub async fn post_deposit(statechain_entity: &State<StateChainEntity>, deposit_msg1: Json<mercurylib::deposit::DepositMsg1>) -> status::Custom<Json<Value>> {

Expand Down Expand Up @@ -136,7 +167,7 @@ pub async fn post_deposit(statechain_entity: &State<StateChainEntity>, deposit_m

let config = crate::server_config::ServerConfig::load();

let enclave_index = crate::endpoints::utils::get_random_enclave_index(&statechain_id, &config.enclaves).unwrap();
let enclave_index = get_random_enclave_index(&statechain_id, &config.enclaves).unwrap();

let lockbox_endpoint = config.enclaves.get(enclave_index).unwrap().url.clone();
let path = "get_public_key";
Expand Down
31 changes: 0 additions & 31 deletions server/src/endpoints/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,6 @@ pub async fn validate_signature(pool: &sqlx::PgPool, signed_message_hex: &str, s
secp.verify_schnorr(&signed_message, &msg, &auth_key).is_ok()
}

pub fn get_random_enclave_index(statechain_id: &str, enclaves: &Vec<Enclave>) -> Result<usize, String> {
let index_from_statechain_id = get_enclave_index_from_statechain_id(statechain_id, enclaves.len() as u32);

println!("index_from_statechain_id: {}", index_from_statechain_id);

let selected_enclave = enclaves.get(index_from_statechain_id).unwrap();
if selected_enclave.allow_deposit {
return Ok(index_from_statechain_id);
} else {
for (i, enclave) in enclaves.iter().enumerate() {
if enclave.allow_deposit {
return Ok(i);
}
}
}

Err("No valid enclave found with allow_deposit set to true".to_string())
}

fn get_enclave_index_from_statechain_id(statechain_id: &str, enclave_array_len: u32) -> usize {
let hash = sha256::Hash::hash(statechain_id.as_bytes());
let hash_bytes = hash.as_byte_array();
let mut bytes = [0u8; 16];
bytes.copy_from_slice(&hash_bytes[..16]);
let random_number = u128::from_be_bytes(bytes);

println!("statechain_id: {}", statechain_id);
println!("random_number: {}", random_number);
return (random_number % enclave_array_len as u128) as usize;
}

#[get("/info/config")]
pub async fn info_config() -> status::Custom<Json<Value>> {

Expand Down

0 comments on commit 776ac9e

Please sign in to comment.