Skip to content

Commit

Permalink
Check updated fields before committing outgoing sync
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Dec 23, 2024
1 parent 34bc791 commit dea34b9
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
18 changes: 16 additions & 2 deletions lib/core/src/persist/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::ensure_sdk;
use crate::error::PaymentError;
use crate::model::*;
use crate::persist::{get_where_clause_state_in, Persister};
use crate::sync::model::data::ChainSyncData;
use crate::sync::model::RecordType;

impl Persister {
Expand Down Expand Up @@ -94,13 +95,26 @@ impl Persister {
}

pub(crate) fn insert_or_update_chain_swap(&self, chain_swap: &ChainSwap) -> Result<()> {
let maybe_swap = self.fetch_chain_swap_by_id(&chain_swap.id)?;
let updated_fields = ChainSyncData::updated_fields(maybe_swap, chain_swap);

let mut con = self.get_connection()?;
let tx = con.transaction_with_behavior(TransactionBehavior::Immediate)?;

Self::insert_or_update_chain_swap_inner(&tx, chain_swap)?;
self.commit_outgoing(&tx, &chain_swap.id, RecordType::Chain, None)?;
// Trigger a sync if:
// - updated_fields is None (swap is inserted, not updated)
// - updated_fields in a non empty list of updated fields
let trigger_sync = updated_fields.as_ref().map_or(true, |u| !u.is_empty());
if trigger_sync {
self.commit_outgoing(&tx, &chain_swap.id, RecordType::Chain, updated_fields)?;
}

tx.commit()?;
self.sync_trigger.try_send(())?;

if trigger_sync {
self.sync_trigger.try_send(())?;
}

Ok(())
}
Expand Down
18 changes: 16 additions & 2 deletions lib/core/src/persist/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::ensure_sdk;
use crate::error::PaymentError;
use crate::model::*;
use crate::persist::{get_where_clause_state_in, Persister};
use crate::sync::model::data::ReceiveSyncData;
use crate::sync::model::RecordType;

impl Persister {
Expand Down Expand Up @@ -83,13 +84,26 @@ impl Persister {
}

pub(crate) fn insert_or_update_receive_swap(&self, receive_swap: &ReceiveSwap) -> Result<()> {
let maybe_swap = self.fetch_receive_swap_by_id(&receive_swap.id)?;
let updated_fields = ReceiveSyncData::updated_fields(maybe_swap, receive_swap);

let mut con = self.get_connection()?;
let tx = con.transaction_with_behavior(TransactionBehavior::Immediate)?;

Self::insert_or_update_receive_swap_inner(&tx, receive_swap)?;
self.commit_outgoing(&tx, &receive_swap.id, RecordType::Receive, None)?;
// Trigger a sync if:
// - updated_fields is None (swap is inserted, not updated)
// - updated_fields in a non empty list of updated fields
let trigger_sync = updated_fields.as_ref().map_or(true, |u| !u.is_empty());
if trigger_sync {
self.commit_outgoing(&tx, &receive_swap.id, RecordType::Receive, updated_fields)?;
}

tx.commit()?;
self.sync_trigger.try_send(())?;

if trigger_sync {
self.sync_trigger.try_send(())?;
}

Ok(())
}
Expand Down
18 changes: 16 additions & 2 deletions lib/core/src/persist/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
use crate::error::PaymentError;
use crate::model::*;
use crate::persist::{get_where_clause_state_in, Persister};
use crate::sync::model::data::SendSyncData;
use crate::sync::model::RecordType;
use crate::{ensure_sdk, get_updated_fields};

Expand Down Expand Up @@ -75,13 +76,26 @@ impl Persister {
}

pub(crate) fn insert_or_update_send_swap(&self, send_swap: &SendSwap) -> Result<()> {
let maybe_swap = self.fetch_send_swap_by_id(&send_swap.id)?;
let updated_fields = SendSyncData::updated_fields(maybe_swap, send_swap);

let mut con = self.get_connection()?;
let tx = con.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;

Self::insert_or_update_send_swap_inner(&tx, send_swap)?;
self.commit_outgoing(&tx, &send_swap.id, RecordType::Send, None)?;
// Trigger a sync if:
// - updated_fields is None (swap is inserted, not updated)
// - updated_fields in a non empty list of updated fields
let trigger_sync = updated_fields.as_ref().map_or(true, |u| !u.is_empty());
if trigger_sync {
self.commit_outgoing(&tx, &send_swap.id, RecordType::Send, updated_fields)?;
}

tx.commit()?;
self.sync_trigger.try_send(())?;

if trigger_sync {
self.sync_trigger.try_send(())?;
}

Ok(())
}
Expand Down
51 changes: 51 additions & 0 deletions lib/core/src/sync/model/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ impl ChainSyncData {
}
}
}

pub(crate) fn updated_fields(
swap: Option<ChainSwap>,
update: &ChainSwap,
) -> Option<Vec<String>> {
match swap {
Some(swap) => {
let mut updated_fields = vec![];
if update.accept_zero_conf != swap.accept_zero_conf {
updated_fields.push("accept_zero_conf".to_string());
}
if update.payer_amount_sat != swap.payer_amount_sat {
updated_fields.push("payer_amount_sat".to_string());
}
if update.receiver_amount_sat != swap.receiver_amount_sat {
updated_fields.push("receiver_amount_sat".to_string());
}
Some(updated_fields)
}
None => None,
}
}
}

impl From<ChainSwap> for ChainSyncData {
Expand Down Expand Up @@ -114,6 +136,20 @@ impl SendSyncData {
}
}
}

pub(crate) fn updated_fields(swap: Option<SendSwap>, update: &SendSwap) -> Option<Vec<String>> {
match swap {
Some(swap) => {
let mut updated_fields = vec![];
if !matches!(update.preimage.clone(), Some(u) if swap.preimage.map_or(false, |s| s == u))
{
updated_fields.push("preimage".to_string());
}
Some(updated_fields)
}
None => None,
}
}
}

impl From<SendSwap> for SendSyncData {
Expand Down Expand Up @@ -174,6 +210,21 @@ pub(crate) struct ReceiveSyncData {
pub(crate) description: Option<String>,
}

impl ReceiveSyncData {
pub(crate) fn updated_fields(
swap: Option<ReceiveSwap>,
_update: &ReceiveSwap,
) -> Option<Vec<String>> {
match swap {
Some(_swap) => {
let updated_fields = vec![];
Some(updated_fields)
}
None => None,
}
}
}

impl From<ReceiveSwap> for ReceiveSyncData {
fn from(value: ReceiveSwap) -> Self {
Self {
Expand Down

0 comments on commit dea34b9

Please sign in to comment.