-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Separate components into separate modules
Before everything was handled by the trading module. Now the following responsibilities have been split up. - Rollover: Moved to the coordinator component and responsible for proposing a rollover. - Async Match: Moved into a dedicated component to check if an async match needs to be executed by the app. - Notification: Responsible for the users and sending messages to them. Note 1, the websocket communication is still in one component and not separated between coordinator and orderbook. It would have been correct to split that up as well, but the effort and additional complexity was IMHO not worth it. Note 2, we have multiple commons crates, which are very much related to each ohter. I think we should combine all of those into a single one, to simplify things.
- Loading branch information
Showing
17 changed files
with
517 additions
and
446 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
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
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,84 @@ | ||
use anyhow::Result; | ||
use bitcoin::secp256k1::PublicKey; | ||
use futures::future::RemoteHandle; | ||
use futures::FutureExt; | ||
use orderbook_commons::Message; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use std::sync::RwLock; | ||
use tokio::sync::broadcast; | ||
use tokio::sync::mpsc; | ||
|
||
/// This value is arbitrarily set to 100 and defines the message accepted in the notification | ||
/// channel buffer. | ||
const NOTIFICATION_BUFFER_SIZE: usize = 100; | ||
|
||
// TODO(holzeis): This enum should be extended to allow for sending push notifications. | ||
pub enum Notification { | ||
Message { | ||
trader_id: PublicKey, | ||
message: Message, | ||
}, | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct NewUserMessage { | ||
pub new_user: PublicKey, | ||
pub sender: mpsc::Sender<Message>, | ||
} | ||
|
||
pub fn start( | ||
tx_user_feed: broadcast::Sender<NewUserMessage>, | ||
) -> (RemoteHandle<Result<()>>, mpsc::Sender<Notification>) { | ||
let (sender, mut receiver) = mpsc::channel::<Notification>(NOTIFICATION_BUFFER_SIZE); | ||
|
||
let authenticated_users = Arc::new(RwLock::new(HashMap::new())); | ||
|
||
tokio::task::spawn({ | ||
let traders = authenticated_users.clone(); | ||
async move { | ||
let mut user_feed = tx_user_feed.subscribe(); | ||
while let Ok(new_user_msg) = user_feed.recv().await { | ||
traders | ||
.write() | ||
.expect("RwLock to not be poisoned") | ||
.insert(new_user_msg.new_user, new_user_msg.sender); | ||
} | ||
} | ||
}); | ||
|
||
let (fut, remote_handle) = { | ||
async move { | ||
while let Some(notification) = receiver.recv().await { | ||
match notification { | ||
Notification::Message { trader_id, message } => { | ||
tracing::info!(%trader_id, "Sending message: {message:?}"); | ||
|
||
let trader = { | ||
let traders = authenticated_users | ||
.read() | ||
.expect("RwLock to not be poisoned"); | ||
traders.get(&trader_id).cloned() | ||
}; | ||
|
||
match trader { | ||
Some(sender) => { | ||
if let Err(e) = sender.send(message).await { | ||
tracing::warn!("Connection lost to trader {e:#}"); | ||
} | ||
} | ||
None => tracing::warn!(%trader_id, "Trader is not connected"), | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
.remote_handle() | ||
}; | ||
|
||
tokio::spawn(fut); | ||
|
||
(remote_handle, sender) | ||
} |
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,110 @@ | ||
use crate::notification::NewUserMessage; | ||
use crate::notification::Notification; | ||
use crate::orderbook::db::matches; | ||
use crate::orderbook::db::orders; | ||
use anyhow::ensure; | ||
use anyhow::Result; | ||
use bitcoin::secp256k1::PublicKey; | ||
use bitcoin::XOnlyPublicKey; | ||
use diesel::r2d2::ConnectionManager; | ||
use diesel::r2d2::Pool; | ||
use diesel::PgConnection; | ||
use futures::future::RemoteHandle; | ||
use futures::FutureExt; | ||
use orderbook_commons::FilledWith; | ||
use orderbook_commons::Match; | ||
use orderbook_commons::Matches; | ||
use orderbook_commons::Message; | ||
use orderbook_commons::OrderReason; | ||
use orderbook_commons::OrderState; | ||
use std::str::FromStr; | ||
use time::OffsetDateTime; | ||
use tokio::sync::broadcast; | ||
use tokio::sync::mpsc; | ||
|
||
pub fn monitor( | ||
pool: Pool<ConnectionManager<PgConnection>>, | ||
tx_user_feed: broadcast::Sender<NewUserMessage>, | ||
notifier: mpsc::Sender<Notification>, | ||
) -> RemoteHandle<Result<()>> { | ||
let mut user_feed = tx_user_feed.subscribe(); | ||
let (fut, remote_handle) = async move { | ||
while let Ok(new_user_msg) = user_feed.recv().await { | ||
tokio::spawn({ | ||
let mut conn = pool.get()?; | ||
let notifier = notifier.clone(); | ||
async move { | ||
tracing::debug!(trader_id=%new_user_msg.new_user, "Checking if the user needs to be notified about pending matches"); | ||
if let Err(e) = process_pending_match(&mut conn, notifier, new_user_msg.new_user).await { | ||
tracing::error!("Failed to process pending match. Error: {e:#}"); | ||
} | ||
} | ||
}); | ||
} | ||
Ok(()) | ||
}.remote_handle(); | ||
|
||
tokio::spawn(fut); | ||
|
||
remote_handle | ||
} | ||
|
||
/// Checks if there are any pending matches | ||
async fn process_pending_match( | ||
conn: &mut PgConnection, | ||
notifier: mpsc::Sender<Notification>, | ||
trader_id: PublicKey, | ||
) -> Result<()> { | ||
if let Some(order) = orders::get_by_trader_id_and_state(conn, trader_id, OrderState::Matched)? { | ||
tracing::debug!(%trader_id, order_id=%order.id, "Notifying trader about pending match"); | ||
|
||
let matches = matches::get_matches_by_order_id(conn, order.id)?; | ||
let filled_with = get_filled_with_from_matches(matches)?; | ||
|
||
let message = match order.order_reason { | ||
OrderReason::Manual => Message::Match(filled_with), | ||
OrderReason::Expired => Message::AsyncMatch { order, filled_with }, | ||
}; | ||
|
||
let msg = Notification::Message { trader_id, message }; | ||
if let Err(e) = notifier.send(msg).await { | ||
tracing::error!("Failed to send notification. Error: {e:#}"); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn get_filled_with_from_matches(matches: Vec<Matches>) -> Result<FilledWith> { | ||
ensure!( | ||
!matches.is_empty(), | ||
"Need at least one matches record to construct a FilledWith" | ||
); | ||
|
||
let order_id = matches | ||
.first() | ||
.expect("to have at least one match") | ||
.order_id; | ||
let oracle_pk = XOnlyPublicKey::from_str( | ||
"16f88cf7d21e6c0f46bcbc983a4e3b19726c6c98858cc31c83551a88fde171c0", | ||
) | ||
.expect("To be a valid pubkey"); | ||
|
||
let expiry_timestamp = coordinator_commons::calculate_next_expiry(OffsetDateTime::now_utc()); | ||
|
||
Ok(FilledWith { | ||
order_id, | ||
expiry_timestamp, | ||
oracle_pk, | ||
matches: matches | ||
.iter() | ||
.map(|m| Match { | ||
id: m.id, | ||
order_id: m.order_id, | ||
quantity: m.quantity, | ||
pubkey: m.match_trader_id, | ||
execution_price: m.execution_price, | ||
}) | ||
.collect(), | ||
}) | ||
} |
Oops, something went wrong.