Skip to content

Commit

Permalink
feat: add swap persister hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse committed Nov 18, 2024
1 parent 6eddc8e commit f87a697
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 71 deletions.
13 changes: 13 additions & 0 deletions lib/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1597,3 +1597,16 @@ macro_rules! get_invoice_description {
}
};
}

#[macro_export]
macro_rules! get_updated_fields {
($($var:ident),* $(,)?) => {{
let mut options = Vec::new();
$(
if $var.is_some() {
options.push(stringify!($var).to_string());
}
)*
options
}};
}
65 changes: 39 additions & 26 deletions lib/core/src/persist/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ use std::collections::HashMap;

use anyhow::Result;
use boltz_client::swaps::boltz::{ChainSwapDetails, CreateChainResponse};
use rusqlite::{named_params, params, Connection, Row};
use rusqlite::{named_params, params, Connection, Row, TransactionBehavior};
use sdk_common::bitcoin::hashes::{hex::ToHex, sha256, Hash};
use serde::{Deserialize, Serialize};

use crate::ensure_sdk;
use crate::error::PaymentError;
use crate::model::*;
use crate::persist::{get_where_clause_state_in, Persister};
use crate::sync::model::RecordType;
use crate::{ensure_sdk, get_updated_fields};

impl Persister {
pub(crate) fn insert_chain_swap(&self, chain_swap: &ChainSwap) -> Result<()> {
let con = self.get_connection()?;
let mut con = self.get_connection()?;
let tx = con.transaction_with_behavior(TransactionBehavior::Immediate)?;

// There is a limit of 16 param elements in a single tuple in rusqlite,
// so we split up the insert into two statements.
let mut stmt = con.prepare(
let id_hash = sha256::Hash::hash(chain_swap.id.as_bytes()).to_hex();
tx.execute(
"
INSERT INTO chain_swaps (
id,
Expand All @@ -38,28 +41,27 @@ impl Persister {
state
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(
&chain_swap.id,
&id_hash,
&chain_swap.direction,
&chain_swap.claim_address,
&chain_swap.lockup_address,
&chain_swap.timeout_block_height,
&chain_swap.preimage,
&chain_swap.payer_amount_sat,
&chain_swap.receiver_amount_sat,
&chain_swap.accept_zero_conf,
&chain_swap.create_response_json,
&chain_swap.claim_private_key,
&chain_swap.refund_private_key,
&chain_swap.claim_fees_sat,
&chain_swap.created_at,
&chain_swap.state,
),
)?;
let id_hash = sha256::Hash::hash(chain_swap.id.as_bytes()).to_hex();
_ = stmt.execute((
&chain_swap.id,
&id_hash,
&chain_swap.direction,
&chain_swap.claim_address,
&chain_swap.lockup_address,
&chain_swap.timeout_block_height,
&chain_swap.preimage,
&chain_swap.payer_amount_sat,
&chain_swap.receiver_amount_sat,
&chain_swap.accept_zero_conf,
&chain_swap.create_response_json,
&chain_swap.claim_private_key,
&chain_swap.refund_private_key,
&chain_swap.claim_fees_sat,
&chain_swap.created_at,
&chain_swap.state,
))?;

con.execute(
tx.execute(
"UPDATE chain_swaps
SET
description = :description,
Expand All @@ -79,6 +81,10 @@ impl Persister {
},
)?;

Self::commit_new_outgoing(&tx, &chain_swap.id, RecordType::Chain)?;

tx.commit()?;

Ok(())
}

Expand Down Expand Up @@ -319,8 +325,10 @@ impl Persister {
refund_tx_id: Option<&str>,
) -> Result<(), PaymentError> {
// Do not overwrite server_lockup_tx_id, user_lockup_tx_id, claim_tx_id, refund_tx_id
let con: Connection = self.get_connection()?;
con.execute(
let mut con = self.get_connection()?;
let tx = con.transaction_with_behavior(TransactionBehavior::Immediate)?;

tx.execute(
"UPDATE chain_swaps
SET
server_lockup_tx_id =
Expand Down Expand Up @@ -360,6 +368,11 @@ impl Persister {
},
)?;

let updated_fields = get_updated_fields!(server_lockup_tx_id);
Self::commit_updated_fields(&tx, swap_id, RecordType::Chain, updated_fields)?;

tx.commit()?;

Ok(())
}
}
Expand Down
61 changes: 37 additions & 24 deletions lib/core/src/persist/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ use std::collections::HashMap;

use anyhow::Result;
use boltz_client::swaps::boltz::CreateReverseResponse;
use rusqlite::{named_params, params, Connection, Row};
use rusqlite::{named_params, params, Connection, Row, TransactionBehavior};
use sdk_common::bitcoin::hashes::{hex::ToHex, sha256, Hash};
use serde::{Deserialize, Serialize};

use crate::ensure_sdk;
use crate::error::PaymentError;
use crate::model::*;
use crate::persist::{get_where_clause_state_in, Persister};
use crate::sync::model::RecordType;
use crate::{ensure_sdk, get_updated_fields};

impl Persister {
pub(crate) fn insert_receive_swap(&self, receive_swap: &ReceiveSwap) -> Result<()> {
let con = self.get_connection()?;
let mut con = self.get_connection()?;
let tx = con.transaction_with_behavior(TransactionBehavior::Immediate)?;

let mut stmt = con.prepare(
let id_hash = sha256::Hash::hash(receive_swap.id.as_bytes()).to_hex();
tx.execute(
"
INSERT INTO receive_swaps (
id,
Expand All @@ -34,26 +37,25 @@ impl Persister {
state
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(
&receive_swap.id,
id_hash,
&receive_swap.preimage,
&receive_swap.create_response_json,
&receive_swap.claim_private_key,
&receive_swap.invoice,
&receive_swap.payment_hash,
&receive_swap.payer_amount_sat,
&receive_swap.receiver_amount_sat,
&receive_swap.created_at,
&receive_swap.claim_fees_sat,
&receive_swap.mrh_address,
&receive_swap.mrh_script_pubkey,
&receive_swap.state,
),
)?;
let id_hash = sha256::Hash::hash(receive_swap.id.as_bytes()).to_hex();
_ = stmt.execute((
&receive_swap.id,
id_hash,
&receive_swap.preimage,
&receive_swap.create_response_json,
&receive_swap.claim_private_key,
&receive_swap.invoice,
&receive_swap.payment_hash,
&receive_swap.payer_amount_sat,
&receive_swap.receiver_amount_sat,
&receive_swap.created_at,
&receive_swap.claim_fees_sat,
&receive_swap.mrh_address,
&receive_swap.mrh_script_pubkey,
&receive_swap.state,
))?;

con.execute(
tx.execute(
"UPDATE receive_swaps
SET
description = :description,
Expand All @@ -69,6 +71,10 @@ impl Persister {
},
)?;

Self::commit_new_outgoing(&tx, &receive_swap.id, RecordType::Receive)?;

tx.commit()?;

Ok(())
}

Expand Down Expand Up @@ -282,8 +288,10 @@ impl Persister {
mrh_amount_sat: Option<u64>,
) -> Result<(), PaymentError> {
// Do not overwrite claim_tx_id or lockup_tx_id
let con: Connection = self.get_connection()?;
con.execute(
let mut con = self.get_connection()?;
let tx = con.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;

tx.execute(
"UPDATE receive_swaps
SET
claim_tx_id =
Expand Down Expand Up @@ -316,6 +324,11 @@ impl Persister {
},
)?;

let updated_fields = get_updated_fields!(mrh_tx_id, mrh_amount_sat);
Self::commit_updated_fields(&tx, swap_id, RecordType::Receive, updated_fields)?;

tx.commit()?;

Ok(())
}
}
Expand Down
55 changes: 34 additions & 21 deletions lib/core/src/persist/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ use rusqlite::{named_params, params, Connection, Row};
use sdk_common::bitcoin::hashes::{hex::ToHex, sha256, Hash};
use serde::{Deserialize, Serialize};

use crate::ensure_sdk;
use crate::error::PaymentError;
use crate::model::*;
use crate::persist::{get_where_clause_state_in, Persister};
use crate::sync::model::RecordType;
use crate::{ensure_sdk, get_updated_fields};

impl Persister {
pub(crate) fn insert_send_swap(&self, send_swap: &SendSwap) -> Result<()> {
let con = self.get_connection()?;
let mut con = self.get_connection()?;
let tx = con.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;

let mut stmt = con.prepare(
let id_hash = sha256::Hash::hash(send_swap.id.as_bytes()).to_hex();
tx.execute(
"
INSERT INTO send_swaps (
id,
Expand All @@ -33,23 +36,26 @@ impl Persister {
state
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(
&send_swap.id,
&id_hash,
&send_swap.invoice,
&send_swap.payment_hash,
&send_swap.description,
&send_swap.payer_amount_sat,
&send_swap.receiver_amount_sat,
&send_swap.create_response_json,
&send_swap.refund_private_key,
&send_swap.lockup_tx_id,
&send_swap.refund_tx_id,
&send_swap.created_at,
&send_swap.state,
),
)?;
let id_hash = sha256::Hash::hash(send_swap.id.as_bytes()).to_hex();
_ = stmt.execute((
&send_swap.id,
&id_hash,
&send_swap.invoice,
&send_swap.payment_hash,
&send_swap.description,
&send_swap.payer_amount_sat,
&send_swap.receiver_amount_sat,
&send_swap.create_response_json,
&send_swap.refund_private_key,
&send_swap.lockup_tx_id,
&send_swap.refund_tx_id,
&send_swap.created_at,
&send_swap.state,
))?;

Self::commit_new_outgoing(&tx, &send_swap.id, RecordType::Send)?;

tx.commit()?;

Ok(())
}
Expand Down Expand Up @@ -203,8 +209,10 @@ impl Persister {
refund_tx_id: Option<&str>,
) -> Result<(), PaymentError> {
// Do not overwrite preimage, lockup_tx_id, refund_tx_id
let con: Connection = self.get_connection()?;
con.execute(
let mut con = self.get_connection()?;
let tx = con.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;

tx.execute(
"UPDATE send_swaps
SET
preimage =
Expand Down Expand Up @@ -237,6 +245,11 @@ impl Persister {
},
)?;

let updated_fields = get_updated_fields!(preimage);
Self::commit_updated_fields(&tx, swap_id, RecordType::Send, updated_fields)?;

tx.commit()?;

Ok(())
}

Expand Down

0 comments on commit f87a697

Please sign in to comment.