-
Notifications
You must be signed in to change notification settings - Fork 117
feat(tpu-stats): send and store tpu swap status #2645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 12 commits
f2d4280
6c535b9
01f0b72
a197fe1
f944542
742fd0d
e17deb1
f3d9247
c1bd112
ccc0a5a
ca87640
fcf1c5c
ab67ad8
afa6652
dd65d46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,6 +140,7 @@ use pubkey_banning::BanReason; | |
| pub use pubkey_banning::{ban_pubkey_rpc, is_pubkey_banned, list_banned_pubkeys_rpc, unban_pubkeys_rpc}; | ||
| pub use recreate_swap_data::recreate_swap_data; | ||
| pub use saved_swap::{SavedSwap, SavedSwapError, SavedSwapIo, SavedSwapResult}; | ||
| pub use swap_v2_common::TPUSwapStatusForStats; | ||
| use swap_v2_common::{ | ||
| get_unfinished_swaps_uuids, swap_kickstart_handler_for_maker, swap_kickstart_handler_for_taker, ActiveSwapV2Info, | ||
| }; | ||
|
|
@@ -363,12 +364,22 @@ pub async fn process_swap_msg(ctx: MmArc, topic: &str, msg: &[u8]) -> P2PRequest | |
| Err(swap_msg_err) => { | ||
| #[cfg(not(target_arch = "wasm32"))] | ||
| return match json::from_slice::<SwapStatus>(msg) { | ||
| Ok(mut status) => { | ||
| status.data.fetch_and_set_usd_prices().await; | ||
| if let Err(e) = save_stats_swap(&ctx, &status.data).await { | ||
| error!("Error saving the swap {} status: {}", status.data.uuid(), e); | ||
| } | ||
| Ok(()) | ||
| Ok(SwapStatus { data, .. }) => match data { | ||
| SwapStatusData::Legacy(mut status) => { | ||
| status.fetch_and_set_usd_prices().await; | ||
| let uuid = *status.uuid(); | ||
| if let Err(e) = save_stats_swap(&ctx, *status).await { | ||
| error!("Error saving the swap {} status: {}", uuid, e); | ||
onur-ozkan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| Ok(()) | ||
| }, | ||
| SwapStatusData::Tpu(status) => { | ||
| let uuid = status.uuid; | ||
| if let Err(e) = add_v2swap_to_stats_db_index(&ctx, *status).await { | ||
| error!("Error saving the swap {} status: {}", uuid, e); | ||
onur-ozkan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| Ok(()) | ||
| }, | ||
| }, | ||
| Err(swap_status_err) => { | ||
| let error = format!( | ||
|
|
@@ -1012,16 +1023,33 @@ pub async fn insert_new_swap_to_db( | |
| } | ||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] | ||
| fn add_swap_to_db_index(ctx: &MmArc, swap: &SavedSwap) { | ||
| if let Some(conn) = ctx.sqlite_conn_opt() { | ||
| crate::database::stats_swaps::add_swap_to_index(&conn, swap) | ||
| } | ||
| pub async fn add_v2swap_to_stats_db_index(ctx: &MmArc, swap: TPUSwapStatusForStats) -> Result<(), String> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we have For TPU we have only a db, no jsons, so could we name them, like:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i was more or less following the names we have for legacy swaps. that being said, i think of index as tables/sqls.
that's why i find the |
||
| let ctx = ctx.clone(); | ||
| common::async_blocking(move || { | ||
| if let Some(conn) = ctx.sqlite_conn_opt() { | ||
| crate::database::stats_swaps::add_v2swap_to_index(&conn, &swap) | ||
|
Comment on lines
+1028
to
+1030
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not making
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
same for so having the also other db methods follow the same sync interface, so that aligns with them (except our There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think other db functions should do the same but I don't want to block this PR with other tech debts. It's fine if you don't want to do it that way. |
||
| } else { | ||
| ERR!("No SQLite connection available") | ||
| } | ||
| }) | ||
| .await | ||
| } | ||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] | ||
| async fn add_swap_to_db_index(ctx: &MmArc, swap: SavedSwap) { | ||
| let ctx = ctx.clone(); | ||
| common::async_blocking(move || { | ||
| if let Some(conn) = ctx.sqlite_conn_opt() { | ||
| crate::database::stats_swaps::add_swap_to_index(&conn, &swap) | ||
|
Comment on lines
+1041
to
+1043
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
| } | ||
| }) | ||
| .await | ||
| } | ||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] | ||
| async fn save_stats_swap(ctx: &MmArc, swap: &SavedSwap) -> Result<(), String> { | ||
| async fn save_stats_swap(ctx: &MmArc, swap: SavedSwap) -> Result<(), String> { | ||
| try_s!(swap.save_to_stats_db(ctx).await); | ||
| add_swap_to_db_index(ctx, swap); | ||
| add_swap_to_db_index(ctx, swap).await; | ||
| Ok(()) | ||
| } | ||
|
|
||
|
|
@@ -1168,10 +1196,17 @@ pub async fn stats_swap_status(ctx: MmArc, req: Json) -> Result<Response<Vec<u8> | |
| Ok(try_s!(Response::builder().body(res))) | ||
| } | ||
|
|
||
| #[derive(Debug, Deserialize, Serialize)] | ||
| #[serde(untagged)] | ||
| enum SwapStatusData { | ||
| Legacy(Box<SavedSwap>), | ||
| Tpu(Box<TPUSwapStatusForStats>), | ||
| } | ||
|
|
||
| #[derive(Debug, Deserialize, Serialize)] | ||
| struct SwapStatus { | ||
| method: String, | ||
| data: SavedSwap, | ||
| data: SwapStatusData, | ||
| } | ||
|
|
||
| /// Broadcasts `my` swap status to P2P network | ||
|
|
@@ -1182,15 +1217,50 @@ async fn broadcast_my_swap_status(ctx: &MmArc, uuid: Uuid) -> Result<(), String> | |
| }; | ||
| status.hide_secrets(); | ||
|
|
||
| let status = SwapStatus { | ||
| method: "swapstatus".into(), | ||
onur-ozkan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| data: SwapStatusData::Legacy(Box::new(status)), | ||
| }; | ||
| let msg = json::to_vec(&status).expect("Swap status ser should never fail"); | ||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] | ||
| try_s!(save_stats_swap(ctx, &status).await); | ||
| { | ||
| // Extract status back. We are avoiding cloning it since it's a big object. | ||
| let SwapStatusData::Legacy(status) = status.data else { | ||
| unreachable!("We are sure that status is Legacy variant"); | ||
| }; | ||
|
|
||
| try_s!(save_stats_swap(ctx, *status).await); | ||
| } | ||
|
|
||
| broadcast_p2p_msg(ctx, swap_topic(&uuid), msg, None); | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn broadcast_my_v2swap_status(ctx: &MmArc, status: TPUSwapStatusForStats) -> Result<(), String> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this function not be called
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm save is done only for native, maybe no need to rename then |
||
| let uuid = status.uuid; | ||
|
|
||
| // Serialize the status to prepare for broadcasting | ||
| let status = SwapStatus { | ||
| method: "swapstatus".into(), | ||
| data: status, | ||
| data: SwapStatusData::Tpu(Box::new(status)), | ||
| }; | ||
| let msg = json::to_vec(&status).expect("Swap status ser should never fail"); | ||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] | ||
| { | ||
| // Extract status back. We are avoiding cloning it since it's a big object. | ||
onur-ozkan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let SwapStatusData::Tpu(status) = status.data else { | ||
| unreachable!("We are sure that status is Tpu variant"); | ||
| }; | ||
|
|
||
| // Add the status to our own stats db index | ||
| try_s!(add_v2swap_to_stats_db_index(ctx, *status).await); | ||
| } | ||
|
|
||
| // Broadcast the status to the P2P network | ||
| broadcast_p2p_msg(ctx, swap_topic(&uuid), msg, None); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if making it
maker_typewith backing it Rust enum would be any good. 🤔 Just for the extensibility.So it would look something like this:
and can be extended to anything without changing things on the DB side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like the idea. we can have a new issue about that to harvest such data (cc/ @shamardy)
we can ofc do a migration to change the table to the appropriate format later.