[FEAT]: port to gotham-engine all the http endpoints and make gotham-city depend on gotham engine
leontiadZen opened this issue · 0 comments
leontiadZen commented
Principles
- Public Gotham must be functional and implement our most updated MPC crypto
- Private Gotham should actually use public Gotham for MPC crypto (no code duplication)
Solution
Maybe if at trait default implementation we can parametrize to take another trait impl as input. Sth like dynamic dispatcher in c++. We would like the keygen and sign to decide at compile time which specific keygen and sign will execute. Can we?
//idea is to have all traits implement all default behavior and being generic over db and authenticator. Problem is the async nature of gotham functions which is not supported maturely enough by traits in current stable rust.
enum dbConnector {
rocksDB,
dynamoDB,
...
}
enum authenticator {
None,
jwt,
...
}
Struct PublicGotham{
dbType: dbConnector;
auth: authenticator;
}
Struct PrivateGotham{
dbType: dbConnector;
auth: authenticator;
}
pub trait Db {
type dbConnector;
fn insert(&self,db:dbConnector,item) -> Result<>;
fn delete(&self,db:dbConnector,item) -> Result<>;
fn get(&self,db:dbConnector,item) -> Result<>;
}
impl Db for PublicGotham{
fn insert(&self,db:dbConnector,item) -> Result<>{
...
}
fn delete(&self,db:dbConnector,item) -> Result<>
{
...
}
fn get(&self,db:dbConnector,item) -> Result<>
{
...
}
}
impl Db for PrivateGotham{
fn insert(&self,db:dbConnector,item) -> Result<>{
...
}
fn delete(&self,db:dbConnector,item) -> Result<>
{
...
}
fn get(&self,db:dbConnector,item) -> Result<>
{
...
}
}
//async trait might be a blocker: https://rust-lang.github.io/async-book/07_workarounds/05_async_in_traits.html
//only in nightly and not mature enough: https://blog.rust-lang.org/inside-rust/2022/11/17/async-fn-in-trait-nightly.html
//not sure if the macros of rocket framework for http endpoints can be implemented at default trait functions
//might be impossible to connect trait functions as routes to rocket: https://github.com/ZenGo-X/zengo-gotham-city/blob/77cf354d8fcfd2865e2549110d0054d52bc96292/gotham-server/src/server.rs#L77
// trait functions are callable from their struct types. but here those functions are never explicitely called they are http async endpoints. Do not know how to call them.
trait KeyGen<S: Db> {
#[post("/ecdsa/keygen/first", format = "json")]
async fn first(&self, dbConn: S){
S.insert()...// at this point trait does not know which gotham will be run time called thanks to dbConn abstraction. That is what we want. Not sure it compiles though.
two_party_ecdsa...
S.get()...
}
#[post("/ecdsa/keygen/<id>/second", format = "json", data = "<dlog_proof>")]
async fn second(&self, dbConn: S){
...
}
#[post(
"/ecdsa/keygen/<id>/third",
format = "json",
data = "<party_2_pdl_first_message>"
)]
async fn third(&self, dbConn: S){
...
}
#[post(
"/ecdsa/keygen/<id>/fourth",
format = "json",
data = "<party_two_pdl_second_message>"
)]
async fn fourth(&self, dbConn: S){
...
}
}
trait Sign<S: Db> {
async fn sign_first(&self, dbConn: S){
...
}
async fn sign_second(&self, dbConn: S){
...
}
}
impl<S: Db> KeyGen<S> for PublicGotham {}
impl<S: Db> KeyGen<S> for PrivateGotham {}
impl<S: Db> Sign<S> for PublicGotham {}
impl<S: Db> Sign<S> for PrivateGotham {}
let public = PublicGotham {
dbType: dbConnector::rocksDB,
auth: authenticator::None,
};
let private = PrivateGotham {
dbType: dbConnector::dynamoDB,
auth: authenticator::jwt,
};
//the question is can we put the trait default functions as routes to rocket:
rocket::Rocket::build()
.mount(
"/",
routes![
crate::routes::ecdsa::public::first,
crate::routes::ecdsa::public::second,
crate::routes::ecdsa::public::third,
crate::routes::ecdsa::public::fourth,
crate::routes::ecdsa::public::sign_first,
crate::routes::ecdsa::public::sign_second,
],
)
//and appropriately for PrivateGotham Struct:
rocket::Rocket::build()
.mount(
"/",
routes![
crate::routes::ecdsa::private::first,
crate::routes::ecdsa::private::second,
crate::routes::ecdsa::private::third,
crate::routes::ecdsa::private::fourth,
crate::routes::ecdsa::private::sign_first,
crate::routes::ecdsa::private::sign_second,
],
)`