diff --git a/server/src/endpoints/deposit.rs b/server/src/endpoints/deposit.rs index 3cbc6001..e29afa7e 100644 --- a/server/src/endpoints/deposit.rs +++ b/server/src/endpoints/deposit.rs @@ -10,7 +10,9 @@ use crate::server::StateChainEntity; #[get("/deposit/get_token")] pub async fn get_token(statechain_entity: &State) -> status::Custom> { - if statechain_entity.config.network == "mainnet" { + let config = crate::server_config::ServerConfig::load(); + + if config.network == "mainnet" { let response_body = json!({ "error": "Internal Server Error", "message": "Token generation not supported on mainnet." @@ -35,7 +37,9 @@ pub async fn get_token(statechain_entity: &State) -> status::C #[get("/tokens/token_init")] pub async fn token_init(statechain_entity: &State) -> status::Custom> { - if statechain_entity.config.network == "mainnet" { + let config = crate::server_config::ServerConfig::load(); + + if config.network == "mainnet" { let response_body = json!({ "error": "Internal Server Error", "message": "Token generation not supported on mainnet." @@ -130,11 +134,13 @@ pub async fn post_deposit(statechain_entity: &State, deposit_m let statechain_id = uuid::Uuid::new_v4().as_simple().to_string(); - let enclave_array_len = statechain_entity.config.enclaves.len() as u32; + let config = crate::server_config::ServerConfig::load(); + + let enclave_array_len = config.enclaves.len() as u32; let enclave_index = crate::endpoints::utils::get_enclave_index_from_statechain_id(&statechain_id, enclave_array_len); - let lockbox_endpoint = statechain_entity.config.enclaves.get(enclave_index).unwrap(); + let lockbox_endpoint = config.enclaves.get(enclave_index).unwrap(); let path = "get_public_key"; let client: reqwest::Client = reqwest::Client::new(); diff --git a/server/src/endpoints/mod.rs b/server/src/endpoints/mod.rs index 201bf810..0639d25e 100644 --- a/server/src/endpoints/mod.rs +++ b/server/src/endpoints/mod.rs @@ -1,7 +1,4 @@ use chrono::{DateTime, Duration, Utc}; -use rocket::State; - -use crate::server::StateChainEntity; pub mod deposit; pub mod sign; @@ -11,8 +8,11 @@ pub mod transfer_receiver; pub mod withdraw; -fn is_batch_expired(statechain_entity: &State, batch_time: DateTime) -> bool { - let batch_timeout = statechain_entity.config.batch_timeout; +fn is_batch_expired(batch_time: DateTime) -> bool { + + let config = crate::server_config::ServerConfig::load(); + + let batch_timeout = config.batch_timeout; let expiration_time = batch_time + Duration::seconds(batch_timeout as i64); diff --git a/server/src/endpoints/sign.rs b/server/src/endpoints/sign.rs index 6f992419..b2303255 100644 --- a/server/src/endpoints/sign.rs +++ b/server/src/endpoints/sign.rs @@ -48,15 +48,17 @@ pub async fn insert_new_signature_data(pool: &sqlx::PgPool, server_pubnonce: &st #[post("/sign/first", format = "json", data = "")] pub async fn sign_first(statechain_entity: &State, sign_first_request_payload: Json) -> status::Custom> { + let config = crate::server_config::ServerConfig::load(); + let statechain_id = sign_first_request_payload.0.statechain_id.clone(); let statechain_entity = statechain_entity.inner(); - let enclave_array_len = statechain_entity.config.enclaves.len() as u32; + let enclave_array_len = config.enclaves.len() as u32; let enclave_index = crate::endpoints::utils::get_enclave_index_from_statechain_id(&statechain_id, enclave_array_len); - let lockbox_endpoint = statechain_entity.config.enclaves.get(enclave_index).unwrap(); + let lockbox_endpoint = config.enclaves.get(enclave_index).unwrap(); let path = "get_public_nonce"; let client: reqwest::Client = reqwest::Client::new(); @@ -137,11 +139,13 @@ pub async fn sign_second (statechain_entity: &State, partial_s let statechain_entity = statechain_entity.inner(); - let enclave_array_len = statechain_entity.config.enclaves.len() as u32; + let config = crate::server_config::ServerConfig::load(); + + let enclave_array_len = config.enclaves.len() as u32; let enclave_index = crate::endpoints::utils::get_enclave_index_from_statechain_id(&statechain_id, enclave_array_len); - let lockbox_endpoint = statechain_entity.config.enclaves.get(enclave_index).unwrap(); + let lockbox_endpoint = config.enclaves.get(enclave_index).unwrap(); let path = "get_partial_signature"; let client: reqwest::Client = reqwest::Client::new(); diff --git a/server/src/endpoints/transfer_receiver.rs b/server/src/endpoints/transfer_receiver.rs index 153d2843..cea5d220 100644 --- a/server/src/endpoints/transfer_receiver.rs +++ b/server/src/endpoints/transfer_receiver.rs @@ -25,11 +25,13 @@ pub async fn statechain_info(statechain_entity: &State, statec let enclave_public_key = enclave_public_key.unwrap(); - let enclave_array_len = statechain_entity.config.enclaves.len() as u32; + let config = crate::server_config::ServerConfig::load(); + + let enclave_array_len = config.enclaves.len() as u32; let enclave_index = crate::endpoints::utils::get_enclave_index_from_statechain_id(&statechain_id, enclave_array_len); - let lockbox_endpoint = statechain_entity.config.enclaves.get(enclave_index).unwrap(); + let lockbox_endpoint = config.enclaves.get(enclave_index).unwrap(); let path = "signature_count"; let client: reqwest::Client = reqwest::Client::new(); @@ -147,7 +149,7 @@ pub async fn validate_batch(statechain_entity: &State, statech let (batch_id, batch_time) = batch_info.unwrap(); - if is_batch_expired(&statechain_entity, batch_time) { + if is_batch_expired(batch_time) { // the batch time has not expired. It is possible to add a new coin to the batch. return BatchTransferReceiveValidationResult::ExpiredBatchTimeError("Batch time has expired".to_string()); } else { @@ -260,12 +262,13 @@ pub async fn transfer_receiver(statechain_entity: &State, tran x1: x1_hex, }; + let config = crate::server_config::ServerConfig::load(); - let enclave_array_len = statechain_entity.config.enclaves.len() as u32; + let enclave_array_len = config.enclaves.len() as u32; let enclave_index = crate::endpoints::utils::get_enclave_index_from_statechain_id(&statechain_id, enclave_array_len); - let lockbox_endpoint = statechain_entity.config.enclaves.get(enclave_index).unwrap(); + let lockbox_endpoint = config.enclaves.get(enclave_index).unwrap(); let path = "keyupdate"; let client: reqwest::Client = reqwest::Client::new(); diff --git a/server/src/endpoints/transfer_sender.rs b/server/src/endpoints/transfer_sender.rs index d418710f..7436798c 100644 --- a/server/src/endpoints/transfer_sender.rs +++ b/server/src/endpoints/transfer_sender.rs @@ -30,7 +30,7 @@ pub async fn validate_batch_transfer(statechain_entity: &State let (batch_id, batch_time) = batch_info.unwrap(); - if !is_batch_expired(&statechain_entity, batch_time) { + if !is_batch_expired(batch_time) { // TODO: check if the batch is complete. If complete, should return success. @@ -59,7 +59,7 @@ pub async fn validate_batch_transfer(statechain_entity: &State if batch_time.is_some() { let batch_time = batch_time.unwrap(); - if !is_batch_expired(&statechain_entity, batch_time) { + if !is_batch_expired(batch_time) { // the batch time has not expired. It is possible to add a new coin to the batch. return BatchTransferValidationResult::Success } else { diff --git a/server/src/endpoints/utils.rs b/server/src/endpoints/utils.rs index 5453a06a..9cc55617 100644 --- a/server/src/endpoints/utils.rs +++ b/server/src/endpoints/utils.rs @@ -64,12 +64,13 @@ pub fn get_enclave_index_from_statechain_id(statechain_id: &str, enclave_array_l } #[get("/info/config")] -pub async fn info_config(statechain_entity: &State) -> status::Custom> { - let statechain_entity = statechain_entity.inner(); +pub async fn info_config() -> status::Custom> { + + let config = crate::server_config::ServerConfig::load(); let server_config = mercurylib::utils::ServerConfig { - initlock: statechain_entity.config.lockheight_init, - interval: statechain_entity.config.lh_decrement, + initlock: config.lockheight_init, + interval: config.lh_decrement, }; let response_body = json!(server_config); diff --git a/server/src/endpoints/withdraw.rs b/server/src/endpoints/withdraw.rs index a6b4c6a5..2921b95b 100644 --- a/server/src/endpoints/withdraw.rs +++ b/server/src/endpoints/withdraw.rs @@ -43,11 +43,13 @@ pub async fn withdraw_complete(statechain_entity: &State, dele return status::Custom(Status::InternalServerError, Json(response_body)); } - let enclave_array_len = statechain_entity.config.enclaves.len() as u32; + let config = crate::server_config::ServerConfig::load(); + + let enclave_array_len = config.enclaves.len() as u32; let enclave_index = crate::endpoints::utils::get_enclave_index_from_statechain_id(&statechain_id, enclave_array_len); - let lockbox_endpoint = statechain_entity.config.enclaves.get(enclave_index).unwrap(); + let lockbox_endpoint = config.enclaves.get(enclave_index).unwrap(); let path = "delete_statechain"; let client: reqwest::Client = reqwest::Client::new(); diff --git a/server/src/server.rs b/server/src/server.rs index c11b1cf2..bf41794c 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -5,7 +5,6 @@ use sqlx::{Pool, Postgres, postgres::PgPoolOptions}; use crate::server_config::ServerConfig; pub struct StateChainEntity { - pub config: ServerConfig, pub pool: Pool, } @@ -13,7 +12,7 @@ impl StateChainEntity { pub async fn new() -> Self { let config = ServerConfig::load(); - + let pool = PgPoolOptions::new() .max_connections(10) @@ -23,7 +22,6 @@ impl StateChainEntity { .unwrap(); StateChainEntity { - config, pool, } }