Skip to content
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

Optional request id - to be checked and compiled #385

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,457 changes: 889 additions & 568 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ uuid = { version = "1.8.0", features = [
"serde",
] }
reqwest = { version = "0.12.1", features = ["json"] }
mostro-core = { version = "0.6.10", features = ["sqlx"] }
mostro-core = { version = "0.6.11", features = ["sqlx"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
config = "0.14.0"
clap = { version = "4.5.19", features = ["derive"] }
lnurl-rs = "0.8.0"
openssl = { version = "0.10.66", features = ["vendored"] }
once_cell = "1.20.2"
# openssl = { version = "0.10.66", features = ["vendored"] }
once_cell = "1.20.2"
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub async fn run(
match message {
Ok(msg) => {
if msg.get_inner_message_kind().verify() {
// Get the optional request id
let request_id = msg.get_inner_message_kind().request_id;

if let Some(action) = msg.inner_action() {
Expand Down
2 changes: 1 addition & 1 deletion src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub async fn add_invoice_action(
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
request_id: u64,
request_id: Option<u64>,
arkanoider marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<()> {
let order_msg = msg.get_inner_message_kind();
let mut order = if let Some(order_id) = order_msg.id {
Expand Down
2 changes: 1 addition & 1 deletion src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn admin_add_solver_action(
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
let inner_message = msg.get_inner_message_kind();
let content = if let Some(content) = &inner_message.content {
Expand Down
2 changes: 1 addition & 1 deletion src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub async fn admin_cancel_action(
my_keys: &Keys,
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
Expand Down
4 changes: 2 additions & 2 deletions src/app/admin_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn admin_settle_action(
my_keys: &Keys,
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
Expand Down Expand Up @@ -164,7 +164,7 @@ pub async fn admin_settle_action(
.await?;
}

let _ = do_payment(order_updated, Some(request_id)).await;
let _ = do_payment(order_updated, request_id).await;

Ok(())
}
2 changes: 1 addition & 1 deletion src/app/admin_take_dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn admin_take_dispute_action(
msg: Message,
event: &UnwrappedGift,
pool: &Pool<Sqlite>,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
// Find dipute id in the message
let dispute_id = if let Some(dispute_id) = msg.get_inner_message_kind().id {
Expand Down
6 changes: 3 additions & 3 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn cancel_action(
my_keys: &Keys,
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
Expand Down Expand Up @@ -178,7 +178,7 @@ pub async fn cancel_add_invoice(
event: &UnwrappedGift,
pool: &Pool<Sqlite>,
my_keys: &Keys,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
if let Some(hash) = &order.hash {
ln_client.cancel_hold_invoice(hash).await?;
Expand Down Expand Up @@ -245,7 +245,7 @@ pub async fn cancel_pay_hold_invoice(
event: &UnwrappedGift,
pool: &Pool<Sqlite>,
my_keys: &Keys,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
if order.hash.is_some() {
// We return funds to seller
Expand Down
2 changes: 1 addition & 1 deletion src/app/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn dispute_action(
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
Expand Down
2 changes: 1 addition & 1 deletion src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub async fn fiat_sent_action(
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
Expand Down
2 changes: 1 addition & 1 deletion src/app/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn order_action(
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
if let Some(order) = msg.get_inner_message_kind().get_order() {
let mostro_settings = Settings::get_mostro();
Expand Down
2 changes: 1 addition & 1 deletion src/app/rate_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub async fn update_user_reputation_action(
my_keys: &Keys,
pool: &Pool<Sqlite>,
rate_list: Arc<Mutex<Vec<Event>>>,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
Expand Down
12 changes: 6 additions & 6 deletions src/app/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub async fn check_failure_retries(order: &Order, request_id: Option<u64>) -> Re
};

send_new_order_msg(
request_id.unwrap_or(1),
request_id,
Some(order.id),
Action::PaymentFailed,
None,
Expand All @@ -64,7 +64,7 @@ pub async fn release_action(
my_keys: &Keys,
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
// Check if order id is ok
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
Expand Down Expand Up @@ -152,7 +152,7 @@ pub async fn release_action(
)
.await;

let _ = do_payment(order_updated, Some(request_id)).await;
let _ = do_payment(order_updated, request_id).await;

Ok(())
}
Expand All @@ -177,7 +177,7 @@ pub async fn do_payment(mut order: Order, request_id: Option<u64>) -> Result<()>
let payment_task = ln_client_payment.send_payment(&payment_request, amount as i64, tx);
if let Err(paymement_result) = payment_task.await {
info!("Error during ln payment : {}", paymement_result);
if let Ok(failed_payment) = check_failure_retries(&order, None).await {
if let Ok(failed_payment) = check_failure_retries(&order, request_id).await {
info!(
"Order id {} has {} failed payments retries",
failed_payment.id, failed_payment.payment_attempts
Expand Down Expand Up @@ -213,7 +213,7 @@ pub async fn do_payment(mut order: Order, request_id: Option<u64>) -> Result<()>
&buyer_pubkey,
&seller_pubkey,
&my_keys,
request_id.unwrap_or(1),
request_id,
)
.await;
}
Expand Down Expand Up @@ -248,7 +248,7 @@ async fn payment_success(
buyer_pubkey: &PublicKey,
seller_pubkey: &PublicKey,
my_keys: &Keys,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
// Purchase completed message to buyer
send_new_order_msg(
Expand Down
12 changes: 3 additions & 9 deletions src/app/take_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub async fn take_buy_action(
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
// Safe unwrap as we verified the message
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
Expand All @@ -34,14 +34,9 @@ pub async fn take_buy_action(
return Ok(());
}
};
// Maker can't take own order
if order.creator_pubkey == event.sender.to_hex() {
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
return Ok(());
}

if order.kind != Kind::Buy.to_string() {
error!("Order Id {order_id} wrong kind");
// Maker can't take own order
if order.kind != Kind::Buy.to_string() || order.creator_pubkey == event.sender.to_hex() {
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
return Ok(());
}
Expand Down Expand Up @@ -100,7 +95,6 @@ pub async fn take_buy_action(
order.fiat_amount,
&order.fiat_code,
order.premium,
request_id,
)
.await?;
// Update order with new sats value
Expand Down
3 changes: 1 addition & 2 deletions src/app/take_sell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub async fn take_sell_action(
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
// Safe unwrap as we verified the message
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
Expand Down Expand Up @@ -131,7 +131,6 @@ pub async fn take_sell_action(
order.fiat_amount,
&order.fiat_code,
order.premium,
request_id,
)
.await?;
// Update order with new sats value
Expand Down
8 changes: 4 additions & 4 deletions src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub async fn hold_invoice_paid(hash: &str, request_id: Option<u64>) -> Result<()
order_data.status = Some(status);
// We send a confirmation message to seller
send_new_order_msg(
request_id.unwrap_or(1),
request_id,
Some(order.id),
Action::BuyerTookOrder,
Some(Content::Order(order_data.clone())),
Expand All @@ -70,7 +70,7 @@ pub async fn hold_invoice_paid(hash: &str, request_id: Option<u64>) -> Result<()
.await;
// We send a message to buyer saying seller paid
send_new_order_msg(
request_id.unwrap_or(1),
request_id,
Some(order.id),
Action::HoldInvoicePaymentAccepted,
Some(Content::Order(order_data)),
Expand All @@ -86,7 +86,7 @@ pub async fn hold_invoice_paid(hash: &str, request_id: Option<u64>) -> Result<()
order_data.master_seller_pubkey = None;
// We ask to buyer for a new invoice
send_new_order_msg(
request_id.unwrap_or(1),
request_id,
Some(order.id),
Action::AddInvoice,
Some(Content::Order(order_data)),
Expand All @@ -96,7 +96,7 @@ pub async fn hold_invoice_paid(hash: &str, request_id: Option<u64>) -> Result<()

// We send a message to seller we are waiting for buyer invoice
send_new_order_msg(
request_id.unwrap_or(1),
request_id,
Some(order.id),
Action::WaitingBuyerInvoice,
None,
Expand Down
17 changes: 8 additions & 9 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub async fn publish_order(
new_order: &SmallOrder,
initiator_pubkey: &str,
ack_pubkey: PublicKey,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
let mut fee = 0;
if new_order.amount > 0 {
Expand Down Expand Up @@ -363,7 +363,7 @@ pub async fn show_hold_invoice(
buyer_pubkey: &PublicKey,
seller_pubkey: &PublicKey,
mut order: Order,
request_id: u64,
request_id: Option<u64>,
) -> anyhow::Result<()> {
let mut ln_client = lightning::LndConnector::new().await?;
// Add fee of seller to hold invoice
Expand Down Expand Up @@ -421,7 +421,7 @@ pub async fn show_hold_invoice(
)
.await;

let _ = invoice_subscribe(hash, Some(request_id)).await;
let _ = invoice_subscribe(hash, request_id).await;

Ok(())
}
Expand Down Expand Up @@ -476,7 +476,6 @@ pub async fn get_market_amount_and_fee(
fiat_amount: i64,
fiat_code: &str,
premium: i64,
_request_id: u64,
) -> Result<(i64, i64)> {
// Update amount order
let new_sats_amount = get_market_quote(&fiat_amount, fiat_code, premium).await?;
Expand All @@ -489,7 +488,7 @@ pub async fn get_market_amount_and_fee(
pub async fn set_waiting_invoice_status(
order: &mut Order,
buyer_pubkey: PublicKey,
request_id: u64,
request_id: Option<u64>,
) -> Result<i64> {
let kind = OrderKind::from_str(&order.kind).unwrap();
let status = Status::WaitingBuyerInvoice;
Expand Down Expand Up @@ -533,7 +532,7 @@ pub async fn rate_counterpart(
buyer_pubkey: &PublicKey,
seller_pubkey: &PublicKey,
order: &Order,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
// Send dm to counterparts
// to buyer
Expand All @@ -559,7 +558,7 @@ pub async fn settle_seller_hold_invoice(
action: Action,
is_admin: bool,
order: &Order,
request_id: u64,
request_id: Option<u64>,
) -> Result<()> {
// Check if the pubkey is right
if !is_admin && event.sender.to_string() != *order.seller_pubkey.as_ref().unwrap().to_string() {
Expand All @@ -586,7 +585,7 @@ pub fn bytes_to_string(bytes: &[u8]) -> String {
}

pub async fn send_cant_do_msg(
request_id: u64,
request_id: Option<u64>,
order_id: Option<Uuid>,
message: Option<String>,
destination_key: &PublicKey,
Expand All @@ -603,7 +602,7 @@ pub async fn send_cant_do_msg(
}

pub async fn send_new_order_msg(
request_id: u64,
request_id: Option<u64>,
order_id: Option<Uuid>,
action: Action,
content: Option<Content>,
Expand Down
Loading