Skip to content

Commit

Permalink
implement request_id on mostro messages (#376)
Browse files Browse the repository at this point in the history
* testing for bilthon

* request_id introduced

* fix tests

* fixed cargo.toml

* updated mostro-core

* Use request_id from parameter

* Replaced last get_inner_message_kind().request_id

* Fix test

---------

Co-authored-by: Francisco Calderón <fjcalderon@gmail.com>
  • Loading branch information
arkanoider and grunch authored Nov 7, 2024
1 parent df845f9 commit 53eb1e5
Show file tree
Hide file tree
Showing 20 changed files with 944 additions and 646 deletions.
1,144 changes: 594 additions & 550 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ uuid = { version = "1.8.0", features = [
"serde",
] }
reqwest = { version = "0.12.1", features = ["json"] }
mostro-core = { version = "0.6.8", features = ["sqlx"] }
mostro-core = { version = "0.6.9", features = ["sqlx"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
config = "0.14.0"
Expand Down
56 changes: 36 additions & 20 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,49 @@ pub async fn run(
match message {
Ok(msg) => {
if msg.get_inner_message_kind().verify() {
let request_id = msg.get_inner_message_kind().request_id;

if let Some(action) = msg.inner_action() {
match action {
Action::NewOrder => {
if let Err(e) =
order_action(msg, &event, &my_keys, &pool).await
if let Err(e) = order_action(
msg, &event, &my_keys, &pool, request_id,
)
.await
{
warning_msg(&action, e)
}
}
Action::TakeSell => {
if let Err(e) =
take_sell_action(msg, &event, &my_keys, &pool).await
if let Err(e) = take_sell_action(
msg, &event, &my_keys, &pool, request_id,
)
.await
{
warning_msg(&action, e)
}
}
Action::TakeBuy => {
if let Err(e) =
take_buy_action(msg, &event, &my_keys, &pool).await
if let Err(e) = take_buy_action(
msg, &event, &my_keys, &pool, request_id,
)
.await
{
warning_msg(&action, e)
}
}
Action::FiatSent => {
if let Err(e) =
fiat_sent_action(msg, &event, &my_keys, &pool).await
if let Err(e) = fiat_sent_action(
msg, &event, &my_keys, &pool, request_id,
)
.await
{
warning_msg(&action, e)
}
}
Action::Release => {
if let Err(e) = release_action(
msg, &event, &my_keys, &pool, ln_client,
msg, &event, &my_keys, &pool, ln_client, request_id,
)
.await
{
Expand All @@ -123,17 +133,18 @@ pub async fn run(
}
Action::Cancel => {
if let Err(e) = cancel_action(
msg, &event, &my_keys, &pool, ln_client,
msg, &event, &my_keys, &pool, ln_client, request_id,
)
.await
{
warning_msg(&action, e)
}
}
Action::AddInvoice => {
if let Err(e) =
add_invoice_action(msg, &event, &my_keys, &pool)
.await
if let Err(e) = add_invoice_action(
msg, &event, &my_keys, &pool, request_id,
)
.await
{
warning_msg(&action, e)
}
Expand All @@ -146,22 +157,25 @@ pub async fn run(
&my_keys,
&pool,
rate_list.clone(),
request_id,
)
.await
{
warning_msg(&action, e)
}
}
Action::Dispute => {
if let Err(e) =
dispute_action(msg, &event, &my_keys, &pool).await
if let Err(e) = dispute_action(
msg, &event, &my_keys, &pool, request_id,
)
.await
{
warning_msg(&action, e)
}
}
Action::AdminCancel => {
if let Err(e) = admin_cancel_action(
msg, &event, &my_keys, &pool, ln_client,
msg, &event, &my_keys, &pool, ln_client, request_id,
)
.await
{
Expand All @@ -170,7 +184,7 @@ pub async fn run(
}
Action::AdminSettle => {
if let Err(e) = admin_settle_action(
msg, &event, &my_keys, &pool, ln_client,
msg, &event, &my_keys, &pool, ln_client, request_id,
)
.await
{
Expand All @@ -179,16 +193,18 @@ pub async fn run(
}
Action::AdminAddSolver => {
if let Err(e) = admin_add_solver_action(
msg, &event, &my_keys, &pool,
msg, &event, &my_keys, &pool, request_id,
)
.await
{
warning_msg(&action, e)
}
}
Action::AdminTakeDispute => {
if let Err(e) =
admin_take_dispute_action(msg, &event, &pool).await
if let Err(e) = admin_take_dispute_action(
msg, &event, &pool, request_id,
)
.await
{
warning_msg(&action, e)
}
Expand Down
26 changes: 23 additions & 3 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub async fn add_invoice_action(
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
request_id: u64,
) -> Result<()> {
let order_msg = msg.get_inner_message_kind();
let mut order = if let Some(order_id) = order_msg.id {
Expand Down Expand Up @@ -54,7 +55,7 @@ pub async fn add_invoice_action(
};
// Only the buyer can add an invoice
if buyer_pubkey != event.sender {
send_cant_do_msg(Some(order.id), None, &event.sender).await;
send_cant_do_msg(request_id, Some(order.id), None, &event.sender).await;
return Ok(());
}

Expand All @@ -74,6 +75,7 @@ pub async fn add_invoice_action(
Ok(_) => payment_request,
Err(_) => {
send_new_order_msg(
request_id,
Some(order.id),
Action::IncorrectInvoiceAmount,
None,
Expand All @@ -96,11 +98,19 @@ pub async fn add_invoice_action(
Status::SettledHoldInvoice => {
order.payment_attempts = 0;
order.clone().update(pool).await?;
send_new_order_msg(Some(order.id), Action::InvoiceUpdated, None, &buyer_pubkey).await;
send_new_order_msg(
request_id,
Some(order.id),
Action::InvoiceUpdated,
None,
&buyer_pubkey,
)
.await;
return Ok(());
}
_ => {
send_new_order_msg(
request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Expand Down Expand Up @@ -145,6 +155,7 @@ pub async fn add_invoice_action(

// We send a confirmation message to seller
send_new_order_msg(
request_id,
Some(order.id),
Action::BuyerTookOrder,
Some(Content::Order(order_data.clone())),
Expand All @@ -153,14 +164,23 @@ pub async fn add_invoice_action(
.await;
// We send a message to buyer saying seller paid
send_new_order_msg(
request_id,
Some(order.id),
Action::HoldInvoicePaymentAccepted,
Some(Content::Order(order_data)),
&buyer_pubkey,
)
.await;
} else {
show_hold_invoice(my_keys, None, &buyer_pubkey, &seller_pubkey, order).await?;
show_hold_invoice(
my_keys,
None,
&buyer_pubkey,
&seller_pubkey,
order,
request_id,
)
.await?;
}

Ok(())
Expand Down
5 changes: 3 additions & 2 deletions src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub async fn admin_add_solver_action(
event: &UnwrappedGift,
my_keys: &Keys,
pool: &Pool<Sqlite>,
request_id: u64,
) -> Result<()> {
let inner_message = msg.get_inner_message_kind();
let content = if let Some(content) = &inner_message.content {
Expand All @@ -32,7 +33,7 @@ pub async fn admin_add_solver_action(
// Check if the pubkey is Mostro
if event.sender.to_string() != my_keys.public_key().to_string() {
// We create a Message
send_cant_do_msg(None, None, &event.sender).await;
send_cant_do_msg(request_id, None, None, &event.sender).await;
return Ok(());
}
let public_key = PublicKey::from_bech32(npubkey)?.to_hex();
Expand All @@ -43,7 +44,7 @@ pub async fn admin_add_solver_action(
Err(ee) => error!("Error creating solver: {:#?}", ee),
}
// We create a Message for admin
let message = Message::new_dispute(None, Action::AdminAddSolver, None);
let message = Message::new_dispute(request_id, None, Action::AdminAddSolver, None);
let message = message.as_json()?;
// Send the message
let sender_keys = crate::util::get_keys().unwrap();
Expand Down
12 changes: 10 additions & 2 deletions src/app/admin_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub async fn admin_cancel_action(
my_keys: &Keys,
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
request_id: u64,
) -> Result<()> {
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
Expand All @@ -31,6 +32,7 @@ pub async fn admin_cancel_action(
match is_assigned_solver(pool, &event.sender.to_string(), order_id).await {
Ok(false) => {
send_new_order_msg(
msg.get_inner_message_kind().request_id,
Some(order_id),
Action::IsNotYourDispute,
None,
Expand All @@ -56,7 +58,12 @@ pub async fn admin_cancel_action(

// Was order cooperatively cancelled?
if order.status == Status::CooperativelyCanceled.to_string() {
let message = MessageKind::new(Some(order_id), Action::CooperativeCancelAccepted, None);
let message = MessageKind::new(
request_id,
Some(order_id),
Action::CooperativeCancelAccepted,
None,
);
if let Ok(message) = message.as_json() {
let sender_keys = crate::util::get_keys().unwrap();
let _ = send_dm(&event.sender, sender_keys, message).await;
Expand All @@ -66,6 +73,7 @@ pub async fn admin_cancel_action(

if order.status != Status::Dispute.to_string() {
send_new_order_msg(
msg.get_inner_message_kind().request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Expand Down Expand Up @@ -124,7 +132,7 @@ pub async fn admin_cancel_action(
let order_updated = update_order_event(my_keys, Status::CanceledByAdmin, &order).await?;
order_updated.update(pool).await?;
// We create a Message for cancel
let message = Message::new_order(Some(order.id), Action::AdminCanceled, None);
let message = Message::new_order(request_id, Some(order.id), Action::AdminCanceled, None);
let message = message.as_json()?;
// Message to admin
let sender_keys = crate::util::get_keys().unwrap();
Expand Down
29 changes: 25 additions & 4 deletions src/app/admin_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub async fn admin_settle_action(
my_keys: &Keys,
pool: &Pool<Sqlite>,
ln_client: &mut LndConnector,
request_id: u64,
) -> Result<()> {
let order_id = if let Some(order_id) = msg.get_inner_message_kind().id {
order_id
Expand All @@ -34,6 +35,7 @@ pub async fn admin_settle_action(
match is_assigned_solver(pool, &event.sender.to_string(), order_id).await {
Ok(false) => {
send_new_order_msg(
msg.get_inner_message_kind().request_id,
Some(order_id),
Action::IsNotYourDispute,
None,
Expand All @@ -59,7 +61,12 @@ pub async fn admin_settle_action(

// Was orde cooperatively cancelled?
if order.status == Status::CooperativelyCanceled.to_string() {
let message = MessageKind::new(Some(order_id), Action::CooperativeCancelAccepted, None);
let message = MessageKind::new(
msg.get_inner_message_kind().request_id,
Some(order_id),
Action::CooperativeCancelAccepted,
None,
);
if let Ok(message) = message.as_json() {
let sender_keys = crate::util::get_keys().unwrap();
let _ = send_dm(&event.sender, sender_keys, message).await;
Expand All @@ -69,6 +76,7 @@ pub async fn admin_settle_action(

if order.status != Status::Dispute.to_string() {
send_new_order_msg(
msg.get_inner_message_kind().request_id,
Some(order.id),
Action::NotAllowedByStatus,
None,
Expand All @@ -78,7 +86,15 @@ pub async fn admin_settle_action(
return Ok(());
}

settle_seller_hold_invoice(event, ln_client, Action::AdminSettled, true, &order).await?;
settle_seller_hold_invoice(
event,
ln_client,
Action::AdminSettled,
true,
&order,
request_id,
)
.await?;

let order_updated = update_order_event(my_keys, Status::SettledHoldInvoice, &order).await?;

Expand Down Expand Up @@ -121,7 +137,12 @@ pub async fn admin_settle_action(
}
}
// We create a Message for settle
let message = Message::new_order(Some(order_updated.id), Action::AdminSettled, None);
let message = Message::new_order(
request_id,
Some(order_updated.id),
Action::AdminSettled,
None,
);
let message = message.as_json()?;
// Message to admin
let sender_keys = crate::util::get_keys().unwrap();
Expand All @@ -143,7 +164,7 @@ pub async fn admin_settle_action(
.await?;
}

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

Ok(())
}
Loading

0 comments on commit 53eb1e5

Please sign in to comment.