Skip to content

Commit

Permalink
Make configuration file local state in each REST function instead of …
Browse files Browse the repository at this point in the history
…server state
  • Loading branch information
ssantos21 committed Jun 16, 2024
1 parent e7d524e commit 171fd11
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 29 deletions.
14 changes: 10 additions & 4 deletions server/src/endpoints/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use crate::server::StateChainEntity;
#[get("/deposit/get_token")]
pub async fn get_token(statechain_entity: &State<StateChainEntity>) -> status::Custom<Json<Value>> {

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."
Expand All @@ -35,7 +37,9 @@ pub async fn get_token(statechain_entity: &State<StateChainEntity>) -> status::C
#[get("/tokens/token_init")]
pub async fn token_init(statechain_entity: &State<StateChainEntity>) -> status::Custom<Json<Value>> {

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."
Expand Down Expand Up @@ -130,11 +134,13 @@ pub async fn post_deposit(statechain_entity: &State<StateChainEntity>, 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();
Expand Down
10 changes: 5 additions & 5 deletions server/src/endpoints/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use chrono::{DateTime, Duration, Utc};
use rocket::State;

use crate::server::StateChainEntity;

pub mod deposit;
pub mod sign;
Expand All @@ -11,8 +8,11 @@ pub mod transfer_receiver;
pub mod withdraw;


fn is_batch_expired(statechain_entity: &State<StateChainEntity>, batch_time: DateTime<Utc>) -> bool {
let batch_timeout = statechain_entity.config.batch_timeout;
fn is_batch_expired(batch_time: DateTime<Utc>) -> 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);

Expand Down
12 changes: 8 additions & 4 deletions server/src/endpoints/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ pub async fn insert_new_signature_data(pool: &sqlx::PgPool, server_pubnonce: &st
#[post("/sign/first", format = "json", data = "<sign_first_request_payload>")]
pub async fn sign_first(statechain_entity: &State<StateChainEntity>, sign_first_request_payload: Json<SignFirstRequestPayload>) -> status::Custom<Json<Value>> {

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();
Expand Down Expand Up @@ -137,11 +139,13 @@ pub async fn sign_second (statechain_entity: &State<StateChainEntity>, 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();
Expand Down
13 changes: 8 additions & 5 deletions server/src/endpoints/transfer_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ pub async fn statechain_info(statechain_entity: &State<StateChainEntity>, 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();
Expand Down Expand Up @@ -147,7 +149,7 @@ pub async fn validate_batch(statechain_entity: &State<StateChainEntity>, 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 {
Expand Down Expand Up @@ -260,12 +262,13 @@ pub async fn transfer_receiver(statechain_entity: &State<StateChainEntity>, 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();
Expand Down
4 changes: 2 additions & 2 deletions server/src/endpoints/transfer_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub async fn validate_batch_transfer(statechain_entity: &State<StateChainEntity>

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.

Expand Down Expand Up @@ -59,7 +59,7 @@ pub async fn validate_batch_transfer(statechain_entity: &State<StateChainEntity>
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 {
Expand Down
9 changes: 5 additions & 4 deletions server/src/endpoints/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StateChainEntity>) -> status::Custom<Json<Value>> {
let statechain_entity = statechain_entity.inner();
pub async fn info_config() -> status::Custom<Json<Value>> {

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);
Expand Down
6 changes: 4 additions & 2 deletions server/src/endpoints/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ pub async fn withdraw_complete(statechain_entity: &State<StateChainEntity>, 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();
Expand Down
4 changes: 1 addition & 3 deletions server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ use sqlx::{Pool, Postgres, postgres::PgPoolOptions};
use crate::server_config::ServerConfig;

pub struct StateChainEntity {
pub config: ServerConfig,
pub pool: Pool<Postgres>,
}

impl StateChainEntity {
pub async fn new() -> Self {

let config = ServerConfig::load();

let pool =
PgPoolOptions::new()
.max_connections(10)
Expand All @@ -23,7 +22,6 @@ impl StateChainEntity {
.unwrap();

StateChainEntity {
config,
pool,
}
}
Expand Down

0 comments on commit 171fd11

Please sign in to comment.