Skip to content

Commit

Permalink
Merge pull request #139 from nightly-labs/add-remaining-handlers
Browse files Browse the repository at this point in the history
Add remaining handlers
  • Loading branch information
Giems authored Mar 21, 2024
2 parents 6ed4938 + 68193d1 commit b38920c
Show file tree
Hide file tree
Showing 30 changed files with 1,013 additions and 250 deletions.
11 changes: 6 additions & 5 deletions database/src/structs/request_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ pub enum RequestFail {
TimedOut,
}

impl Into<RequestStatus> for RequestFail {
fn into(self) -> RequestStatus {
match self {
RequestFail::Rejected => RequestStatus::Rejected,
RequestFail::TimedOut => RequestStatus::TimedOut,
impl From<&Option<RequestFail>> for RequestStatus {
fn from(fail: &Option<RequestFail>) -> Self {
match fail {
Some(RequestFail::Rejected) => RequestStatus::Rejected,
Some(RequestFail::TimedOut) => RequestStatus::TimedOut,
None => RequestStatus::Completed,
}
}
}
2 changes: 1 addition & 1 deletion database/src/tables/events/change_network/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Db {
tx: &mut Transaction<'_, Postgres>,
request_id: &String,
request_status: RequestStatus,
new_network: Option<String>,
new_network: &Option<String>,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {EVENT_CHANGE_NETWORK_TABLE_NAME} SET request_status = $1, new_network = $2 WHERE request_id = $3",
Expand Down
4 changes: 2 additions & 2 deletions database/src/tables/events/change_wallet/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Db {
pub async fn create_new_event_change_wallet(
&self,
tx: &mut Transaction<'_, Postgres>,
event_id: &i64,
event_id: i64,
session_id: &String,
request_id: &String,
network_id: &String,
Expand Down Expand Up @@ -47,7 +47,7 @@ impl Db {
tx: &mut Transaction<'_, Postgres>,
request_id: &String,
request_status: RequestStatus,
new_wallet_address: Option<String>,
new_wallet_address: &Option<String>,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {EVENT_CHANGE_WALLET_TABLE_NAME} SET request_status = $1, new_wallet_address = $2 WHERE request_id = $3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Db {
tx: &mut Transaction<'_, Postgres>,
request_id: &String,
request_status: RequestStatus,
tx_hash: Option<String>,
tx_hash: &Option<String>,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {EVENT_SIGN_AND_SEND_TRANSACTION_TABLE_NAME} SET request_status = $1, tx_hash = $2 WHERE request_id = $3"
Expand Down
2 changes: 1 addition & 1 deletion database/src/tables/events/sign_transaction/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Db {
tx: &mut Transaction<'_, Postgres>,
request_id: &String,
request_status: RequestStatus,
tx_hash: Option<String>,
tx_hash: &Option<String>,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {EVENT_SIGN_TRANSACTION_TABLE_NAME} SET request_status = $1, tx_hash = $2 WHERE request_id = $3"
Expand Down
2 changes: 1 addition & 1 deletion server/bindings/ChangeNetworkEvent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface ChangeNetworkEvent { sessionId: string, requestId: string, oldNetwork: string, newNetwork: string, }
export interface ChangeNetworkEvent { sessionId: string, requestId: string, oldNetwork: string, }
2 changes: 1 addition & 1 deletion server/bindings/ChangeNetworkResolveEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { RequestFail } from "./RequestFail";

export interface ChangeNetworkResolveEvent { sessionId: string, requestId: string, failureReason?: RequestFail, }
export interface ChangeNetworkResolveEvent { sessionId: string, requestId: string, newNetwork: string | null, failureReason?: RequestFail, }
3 changes: 2 additions & 1 deletion server/bindings/ClientConnectEvent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { SessionType } from "./SessionType";

export interface ClientConnectEvent { clientId: string, publicKeys: Array<string>, sessionId: string, device: string | null, metadata: string | null, notification: string | null, }
export interface ClientConnectEvent { clientId: string, sessionId: string, walletName: string, walletType: string, sessionType: SessionType, }
2 changes: 1 addition & 1 deletion server/bindings/ClientConnectResolveEvent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface ClientConnectResolveEvent { clientId: string, sessionId: string, addresses: Array<string>, success: boolean, }
export interface ClientConnectResolveEvent { clientId: string, sessionId: string, addresses: Array<string>, walletName: string, walletType: string, success: boolean, }
2 changes: 1 addition & 1 deletion server/bindings/ClientDisconnectEvent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface ClientDisconnectEvent { disconnectedSessionId: string, }
export interface ClientDisconnectEvent { clientId: string, disconnectedSessionId: string, }
4 changes: 2 additions & 2 deletions server/bindings/EventData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { AppConnectEvent } from "./AppConnectEvent";
import type { AppDisconnectEvent } from "./AppDisconnectEvent";
import type { ChangeNetworkEvent } from "./ChangeNetworkEvent";
import type { ChangeWalletEvent } from "./ChangeWalletEvent";
import type { ClientConnectInitEvent } from "./ClientConnectInitEvent";
import type { ClientConnectEvent } from "./ClientConnectEvent";
import type { ClientConnectResolveEvent } from "./ClientConnectResolveEvent";
import type { ClientDisconnectEvent } from "./ClientDisconnectEvent";
import type { SignAndSendTransactionEvent } from "./SignAndSendTransactionEvent";
Expand All @@ -13,4 +13,4 @@ import type { SignMessageResolveEvent } from "./SignMessageResolveEvent";
import type { SignTransactionEvent } from "./SignTransactionEvent";
import type { SignTransactionResolveEvent } from "./SignTransactionResolveEvent";

export type EventData = { type: "AppConnect" } & AppConnectEvent | { type: "AppDisconnect" } & AppDisconnectEvent | { type: "ClientConnectInit" } & ClientConnectInitEvent | { type: "ClientConnectResolve" } & ClientConnectResolveEvent | { type: "ClientDisconnect" } & ClientDisconnectEvent | { type: "SignMessage" } & SignMessageEvent | { type: "SignMessageResolve" } & SignMessageResolveEvent | { type: "SignTransaction" } & SignTransactionEvent | { type: "SignTransactionResolve" } & SignTransactionResolveEvent | { type: "SignAndSendTransaction" } & SignAndSendTransactionEvent | { type: "SignAndSendTransactionResolve" } & SignAndSendTransactionResolveEvent | { type: "ChangeNetwork" } & ChangeNetworkEvent | { type: "ChangeWallet" } & ChangeWalletEvent;
export type EventData = { type: "AppConnect" } & AppConnectEvent | { type: "AppDisconnect" } & AppDisconnectEvent | { type: "ClientConnect" } & ClientConnectEvent | { type: "ClientConnectResolve" } & ClientConnectResolveEvent | { type: "ClientDisconnect" } & ClientDisconnectEvent | { type: "SignMessage" } & SignMessageEvent | { type: "SignMessageResolve" } & SignMessageResolveEvent | { type: "SignTransaction" } & SignTransactionEvent | { type: "SignTransactionResolve" } & SignTransactionResolveEvent | { type: "SignAndSendTransaction" } & SignAndSendTransactionEvent | { type: "SignAndSendTransactionResolve" } & SignAndSendTransactionResolveEvent | { type: "ChangeNetwork" } & ChangeNetworkEvent | { type: "ChangeWallet" } & ChangeWalletEvent;
47 changes: 39 additions & 8 deletions server/src/http/cloud/events/events_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ use super::{
processors::{
process_event_app_connect::process_event_app_connect,
process_event_app_disconnect::process_event_app_disconnect,
process_event_change_network::process_event_change_network,
process_event_change_wallet::process_event_change_wallet,
process_event_client_connect::process_event_client_connect_init,
process_event_client_connect_resolve::process_event_client_connect_resolve,
process_event_client_disconnect::process_event_client_disconnect,
process_event_sign_and_send_transaction::process_event_sign_and_send_transaction,
process_event_sign_and_send_transaction_resolve::process_event_sign_and_send_transaction_resolve,
process_event_sign_message::process_event_sign_message,
process_event_sign_message_resolve::process_event_sign_message_resolve,
process_event_sign_transaction::process_event_sign_transaction,
process_event_sign_transaction_resolve::process_event_sign_transaction_resolve,
},
};
use crate::{
Expand Down Expand Up @@ -54,13 +62,36 @@ pub async fn process_event(
EventData::ClientDisconnect(event) => {
process_event_client_disconnect(event, &event_payload.app_id, ip, db_connection).await;
}
EventData::SignMessage(_) => todo!(),
EventData::SignMessageResolve(_) => todo!(),
EventData::SignTransaction(_) => todo!(),
EventData::SignTransactionResolve(_) => todo!(),
EventData::SignAndSendTransaction(_) => todo!(),
EventData::SignAndSendTransactionResolve(_) => todo!(),
EventData::ChangeNetwork(_) => todo!(),
EventData::ChangeWallet(_) => todo!(),
EventData::SignMessage(event) => {
process_event_sign_message(event, &event_payload.app_id, db_connection).await;
}
EventData::SignMessageResolve(event) => {
process_event_sign_message_resolve(event, &event_payload.app_id, db_connection).await;
}
EventData::SignTransaction(event) => {
process_event_sign_transaction(event, &event_payload.app_id, db_connection).await;
}
EventData::SignTransactionResolve(event) => {
process_event_sign_transaction_resolve(event, &event_payload.app_id, db_connection)
.await;
}
EventData::SignAndSendTransaction(event) => {
process_event_sign_and_send_transaction(event, &event_payload.app_id, db_connection)
.await;
}
EventData::SignAndSendTransactionResolve(event) => {
process_event_sign_and_send_transaction_resolve(
event,
&event_payload.app_id,
db_connection,
)
.await;
}
EventData::ChangeNetwork(event) => {
process_event_change_network(event, &event_payload.app_id, db_connection).await;
}
EventData::ChangeWallet(event) => {
process_event_change_wallet(event, &event_payload.app_id, db_connection).await;
}
}
}
10 changes: 10 additions & 0 deletions server/src/http/cloud/events/processors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
pub mod process_event_app_connect;
pub mod process_event_app_disconnect;
pub mod process_event_change_network;
pub mod process_event_change_network_resolve;
pub mod process_event_change_wallet;
pub mod process_event_change_wallet_resolve;
pub mod process_event_client_connect;
pub mod process_event_client_connect_resolve;
pub mod process_event_client_disconnect;
pub mod process_event_sign_and_send_transaction;
pub mod process_event_sign_and_send_transaction_resolve;
pub mod process_event_sign_message;
pub mod process_event_sign_message_resolve;
pub mod process_event_sign_transaction;
pub mod process_event_sign_transaction_resolve;
110 changes: 59 additions & 51 deletions server/src/http/cloud/events/processors/process_event_app_connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,71 +119,79 @@ pub async fn process_event_app_connect(

async fn save_event_app_connect(db: &Arc<Db>, app_id: &String, event: &AppConnectEvent) {
// Establish a new transaction
match db.connection_pool.begin().await {
Ok(mut tx) => {
// Create a new event index in the database
match db
.create_new_event_entry(&mut tx, &app_id, &EventType::AppConnect)
.await
{
Ok(event_id) => {
// Now create a new event app connect corresponding to the event
match db
.create_new_event_app_connect(
&mut tx,
event_id,
&event.session_id,
&event
.device_metadata
.to_string()
.unwrap_or("Failed to serialize device metadata".to_string()),
&event.language,
&event.timezone,
event.new_session,
)
.await
{
Ok(_) => {
// Commit the transaction
if let Err(err) = tx.commit().await {
error!(
"Failed to commit transaction for new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
let mut tx = match db.connection_pool.begin().await {
Ok(tx) => tx,
Err(err) => {
error!(
"Failed to create new transaction to save app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
return;
}
};

return;
}
Err(err) => {
error!(
"Failed to create new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
}
Err(err) => {
error!(
"Failed to create new event index, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
// Create a new event index
let event_id = match db
.create_new_event_entry(&mut tx, &app_id, &EventType::AppConnect)
.await
{
Ok(event_id) => event_id,
Err(err) => {
error!(
"Failed to create new event index, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);

// If we have not returned yet, then we have failed to save the event
// Rollback the transaction
if let Err(err) = tx.rollback().await {
error!(
"Failed to rollback transaction for new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}

return;
}
};

// Now create a new event app connect corresponding to the event
match db
.create_new_event_app_connect(
&mut tx,
event_id,
&event.session_id,
&event
.device_metadata
.to_string()
.unwrap_or("Failed to serialize device metadata".to_string()),
&event.language,
&event.timezone,
event.new_session,
)
.await
{
Ok(_) => {
// Commit the transaction
if let Err(err) = tx.commit().await {
error!(
"Failed to commit transaction for new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
Err(err) => {
error!(
"Failed to create new transaction to save app connection event, app_id: [{}], event: [{:?}], err: [{}]",
"Failed to create new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);

// Rollback the transaction
if let Err(err) = tx.rollback().await {
error!(
"Failed to rollback transaction for new app connection event, app_id: [{}], event: [{:?}], err: [{}]",
app_id, event, err
);
}
}
}
}
Loading

0 comments on commit b38920c

Please sign in to comment.