-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
275b8eb
commit 33f63eb
Showing
8 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::{ | ||
fmt, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
use axum::{ | ||
http::StatusCode, | ||
response::{IntoResponse, Response}, | ||
}; | ||
|
||
|
||
|
||
#[derive(Debug)] | ||
pub struct AppErr { | ||
error: anyhow::Error, | ||
status: Option<StatusCode>, | ||
} | ||
|
||
impl AppErr { | ||
pub fn set_status(err: impl Into<Self>, status: StatusCode) -> Self { | ||
let mut err = err.into(); | ||
err.status = Some(status); | ||
err | ||
} | ||
} | ||
|
||
impl IntoResponse for AppErr { | ||
fn into_response(self) -> Response { | ||
( | ||
self.status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR), | ||
format!("{}", self.error), | ||
) | ||
.into_response() | ||
} | ||
} | ||
|
||
impl Deref for AppErr { | ||
type Target = anyhow::Error; | ||
fn deref(&self) -> &Self::Target { | ||
&self.error | ||
} | ||
} | ||
|
||
impl DerefMut for AppErr { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.error | ||
} | ||
} | ||
|
||
impl fmt::Display for AppErr { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!( | ||
f, | ||
"{}: {}", | ||
self.status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR), | ||
self.error | ||
) | ||
} | ||
} | ||
|
||
impl std::error::Error for AppErr {} | ||
|
||
impl From<anyhow::Error> for AppErr { | ||
fn from(error: anyhow::Error) -> Self { | ||
Self { | ||
error, | ||
status: None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
pub mod errors; | ||
pub mod routes; | ||
|
||
use std::{ | ||
collections::{HashMap, HashSet}, | ||
sync::Arc, | ||
}; | ||
|
||
use axum::{routing::post, Router}; | ||
use routes::{transfer::Transfer, *}; | ||
use tokio::sync::RwLock; | ||
|
||
pub use errors::AppErr; | ||
|
||
type PublicKey = String; | ||
|
||
pub struct BatchState { | ||
pub balances: HashMap<PublicKey, u64>, | ||
pub batch_epoch: u64, | ||
/// The set of transfers that will be batched in the next epoch. | ||
pub batched_transfers: HashSet<Transfer>, | ||
} | ||
impl BatchState { | ||
pub fn new() -> Arc<RwLock<Self>> { | ||
Arc::new(RwLock::new(Self { | ||
balances: HashMap::new(), | ||
batch_epoch: 0, | ||
batched_transfers: HashSet::new(), | ||
})) | ||
} | ||
} | ||
|
||
pub fn app_router(state: Arc<RwLock<BatchState>>) -> Router { | ||
Router::new() | ||
.route("/api/v1/mock/deposit", post(deposit)) | ||
.route("/api/v1/mock/withdraw", post(withdraw)) | ||
.route("/api/v1/transfer", post(transfer)) | ||
.with_state(state) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
use std::net::SocketAddr; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
tracing_subscriber::fmt::init(); | ||
let axum_port: u16 = std::env::var("SERVER_PORT").map_or(8000, |x| { | ||
x.parse().unwrap_or_else(|e| { | ||
format!("Failed to parse SERVER_PORT: {}", e) | ||
.parse() | ||
.unwrap() | ||
}) | ||
}); | ||
|
||
let app = kairos_server::app_router(kairos_server::BatchState::new()); | ||
|
||
let axum_addr = SocketAddr::from(([127, 0, 0, 1], axum_port)); | ||
tracing::info!("starting http server"); | ||
let listener = tokio::net::TcpListener::bind(axum_addr).await.unwrap(); | ||
axum::serve(listener, app).await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::{extract::State, Json}; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio::sync::RwLock; | ||
|
||
use crate::{AppErr, BatchState, PublicKey}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct DepositRequest { | ||
pub public_key: PublicKey, | ||
pub amount: u64, | ||
} | ||
|
||
pub async fn deposit( | ||
State(pool): State<Arc<RwLock<BatchState>>>, | ||
Json(proof_request): Json<DepositRequest>, | ||
) -> Result<(), AppErr> { | ||
todo!("deposit") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pub mod deposit; | ||
pub mod withdraw; | ||
pub mod transfer; | ||
|
||
pub use deposit::deposit; | ||
pub use withdraw::withdraw; | ||
pub use transfer::transfer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::{extract::State, Json}; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio::sync::RwLock; | ||
|
||
use crate::{AppErr, BatchState, PublicKey}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct Transfer { | ||
pub from: PublicKey, | ||
pub to: PublicKey, | ||
pub amount: u64, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct TransferRequest { | ||
transfer: Transfer, | ||
signature: String, | ||
} | ||
|
||
pub async fn transfer( | ||
State(pool): State<Arc<RwLock<BatchState>>>, | ||
Json(proof_request): Json<TransferRequest>, | ||
) -> Result<(), AppErr> { | ||
todo!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::{extract::State, Json}; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio::sync::RwLock; | ||
|
||
use crate::{AppErr, BatchState, PublicKey}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct WithdrawRequest { | ||
pub public_key: PublicKey, | ||
pub amount: u64, | ||
} | ||
|
||
pub async fn withdraw( | ||
State(pool): State<Arc<RwLock<BatchState>>>, | ||
Json(proof_request): Json<WithdrawRequest>, | ||
) -> Result<(), AppErr> { | ||
todo!() | ||
} |