fix sql syntax error in fetch_and_delete_offer_from_public_offers_table()

This commit is contained in:
f321x
2024-07-10 11:52:10 +00:00
parent 3cb0c9a0a9
commit dbcd9b65ba
4 changed files with 21 additions and 18 deletions

View File

@ -2,6 +2,7 @@ use super::*;
#[cfg(test)]
use anyhow::Ok;
#[allow(dead_code)]
async fn create_coordinator() -> Result<database::CoordinatorDB, anyhow::Error> {
// Set up the in-memory database
env::set_var("DATABASE_PATH", ":memory:");
@ -370,7 +371,7 @@ async fn test_fetch_and_delete_offer_from_public_offers_table() -> Result<()> {
// Insert a test entry into active_maker_offers
let offer_id_hex = "offer_id_1";
let robohash = hex::decode("a3f1f1f0e2f3f4f5").unwrap(); // Example robohash
let is_buy_order = true;
let is_buy_order = bool_to_sql_int(true);
let amount_sat = 1000;
let bond_ratio = 50;
let offer_duration_ts = 1234567890;
@ -408,7 +409,7 @@ async fn test_fetch_and_delete_offer_from_public_offers_table() -> Result<()> {
// Verify the result
assert_eq!(result.offer_id, offer_id_hex);
assert_eq!(result.robohash_maker, robohash);
assert_eq!(result.is_buy_order, is_buy_order);
assert_eq!(bool_to_sql_int(result.is_buy_order), is_buy_order);
assert_eq!(result.amount_sat, amount_sat);
assert_eq!(result.bond_ratio, bond_ratio);
assert_eq!(result.offer_duration_ts, offer_duration_ts);

View File

@ -1,8 +1,7 @@
pub mod db_tests;
use anyhow::Context;
use axum::routing::trace;
use futures_util::{lock, StreamExt};
use futures_util::StreamExt;
use super::*;
use sqlx::{sqlite::SqlitePoolOptions, Pool, Row, Sqlite};
@ -301,16 +300,16 @@ impl CoordinatorDB {
&self,
offer_id_hex: &str,
) -> Result<AwaitingTakerOffer> {
let fetched_values = sqlx::query_as::<_, (Vec<u8>, bool, i64, i32, i64, String, i64, String, String, String, String)> (
let fetched_values = sqlx::query_as::<_, (Vec<u8>, i32, i64, i32, i64, String, i64, String, String, String, String)> (
"SELECT robohash, is_buy_order, amount_sat, bond_ratio, offer_duration_ts, bond_address, bond_amount_sat, bond_tx_hex, payout_address,
musig_pub_nonce_hex, musig_pubkey_hex FROM active_maker_offers WHERE <unique_identifier_column> = ?",
musig_pub_nonce_hex, musig_pubkey_hex FROM active_maker_offers WHERE offer_id = ?",
)
.bind(offer_id_hex)
.fetch_one(&*self.db_pool)
.await?;
// Delete the database entry.
sqlx::query("DELETE FROM active_maker_offers WHERE <unique_identifier_column> = ?")
sqlx::query("DELETE FROM active_maker_offers WHERE offer_id = ?")
.bind(offer_id_hex)
.execute(&*self.db_pool)
.await?;
@ -318,7 +317,7 @@ impl CoordinatorDB {
Ok(AwaitingTakerOffer {
offer_id: offer_id_hex.to_string(),
robohash_maker: fetched_values.0,
is_buy_order: fetched_values.1,
is_buy_order: fetched_values.1 != 0,
amount_sat: fetched_values.2,
bond_ratio: fetched_values.3,
offer_duration_ts: fetched_values.4,

View File

@ -101,7 +101,7 @@ impl<D: bdk::database::BatchDatabase> CoordinatorWallet<D> {
};
}
// check if bond output to us is big enough
let output_sum = match tx.bond_output_sum(&requirements.bond_address) {
match tx.bond_output_sum(&requirements.bond_address) {
Ok(amount) => {
if amount < requirements.locking_amount_sat {
return Err(anyhow!("Bond output sum too small"));
@ -112,11 +112,11 @@ impl<D: bdk::database::BatchDatabase> CoordinatorWallet<D> {
return Err(anyhow!(e));
}
};
if ((input_sum - output_sum) / tx.vsize() as u64) < 200 {
if ((input_sum - tx.all_output_sum()) / tx.vsize() as u64) < 200 {
return Err(anyhow!("Bond fee rate too low"));
}
debug!("validate_bond_tx_hex(): Bond validation successful.");
panic!("fix function");
Ok(())
}
@ -144,11 +144,9 @@ impl fmt::Debug for CoordinatorWallet<Tree> {
#[cfg(test)]
mod tests {
use super::*;
use bdk::bitcoin::{Address, Network};
use bdk::bitcoin::Network;
use bdk::database::MemoryDatabase;
use bdk::{blockchain::ElectrumBlockchain, Wallet};
// use tokio::test;
// use bitcoincore_rpc_json::GetRawTransactionResult;
async fn new_test_wallet(wallet_xprv: &str) -> CoordinatorWallet<MemoryDatabase> {
let backend = ElectrumBlockchain::from(Client::new("ssl://mempool.space:40002").unwrap());
@ -171,7 +169,7 @@ mod tests {
#[tokio::test]
async fn test_valid_bond_tx() {
let test_wallet = new_test_wallet("xprv9s21ZrQH143K2XqaJ5boFeHgrJTsMgfzrgrsFXdk3UBYtLLhUkCj2QKPmqYpC92zd6bv46Nh8QxXmjH2MwJWVLQzfC6Bv1Tbeoz28nXjeM2").await;
let test_wallet = new_test_wallet("tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32").await;
let bond = "020000000001010127a9d96655011fca55dc2667f30b98655e46da98d0f84df676b53d7fb380140000000000010000000250c3000000000000225120a12e5d145a4a3ab43f6cc1188435e74f253eace72bd986f1aaf780fd0c653236aa900000000000002251207dd0d1650cdc22537709e35620f3b5cc3249b305bda1209ba4e5e01bc3ad2d8c014010e19c8b915624bd4aa0ba4d094d26ca031a6f2d8f23fe51372c7ea50e05f3caf81c7e139f6fed3e9ffd20c03d79f78542acb3d8aed664898f1c4b2909c2188c00000000";
let requirements = BondRequirements {
min_input_sum_sat: 100000,
@ -186,7 +184,7 @@ mod tests {
#[tokio::test]
async fn test_invalid_bond_tx_low_input_sum() {
let test_wallet = new_test_wallet("xprv9s21ZrQH143K2XqaJ5boFeHgrJTsMgfzrgrsFXdk3UBYtLLhUkCj2QKPmqYpC92zd6bv46Nh8QxXmjH2MwJWVLQzfC6Bv1Tbeoz28nXjeM2").await;
let test_wallet = new_test_wallet("tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32").await;
let bond = "020000000001010127a9d96655011fca55dc2667f30b98655e46da98d0f84df676b53d7fb380140000000000010000000250c3000000000000225120a12e5d145a4a3ab43f6cc1188435e74f253eace72bd986f1aaf780fd0c653236aa900000000000002251207dd0d1650cdc22537709e35620f3b5cc3249b305bda1209ba4e5e01bc3ad2d8c014010e19c8b915624bd4aa0ba4d094d26ca031a6f2d8f23fe51372c7ea50e05f3caf81c7e139f6fed3e9ffd20c03d79f78542acb3d8aed664898f1c4b2909c2188c00000000";
let requirements = BondRequirements {
min_input_sum_sat: 2000000, // Set higher than the actual input sum
@ -205,7 +203,7 @@ mod tests {
#[tokio::test]
async fn test_invalid_bond_tx_low_output_sum() {
let test_wallet = new_test_wallet("xprv9s21ZrQH143K2XqaJ5boFeHgrJTsMgfzrgrsFXdk3UBYtLLhUkCj2QKPmqYpC92zd6bv46Nh8QxXmjH2MwJWVLQzfC6Bv1Tbeoz28nXjeM2").await;
let test_wallet = new_test_wallet("tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32").await;
let bond = "020000000001010127a9d96655011fca55dc2667f30b98655e46da98d0f84df676b53d7fb380140000000000010000000250c3000000000000225120a12e5d145a4a3ab43f6cc1188435e74f253eace72bd986f1aaf780fd0c653236aa900000000000002251207dd0d1650cdc22537709e35620f3b5cc3249b305bda1209ba4e5e01bc3ad2d8c014010e19c8b915624bd4aa0ba4d094d26ca031a6f2d8f23fe51372c7ea50e05f3caf81c7e139f6fed3e9ffd20c03d79f78542acb3d8aed664898f1c4b2909c2188c00000000";
let requirements = BondRequirements {
min_input_sum_sat: 100000,
@ -224,7 +222,7 @@ mod tests {
#[tokio::test]
async fn test_invalid_bond_tx_low_fee_rate() {
let test_wallet = new_test_wallet("xprv9s21ZrQH143K2XqaJ5boFeHgrJTsMgfzrgrsFXdk3UBYtLLhUkCj2QKPmqYpC92zd6bv46Nh8QxXmjH2MwJWVLQzfC6Bv1Tbeoz28nXjeM2").await;
let test_wallet = new_test_wallet("tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32").await;
let bond = "020000000001010127a9d96655011fca55dc2667f30b98655e46da98d0f84df676b53d7fb380140000000000fdffffff0259b00000000000002251207dd0d1650cdc22537709e35620f3b5cc3249b305bda1209ba4e5e01bc3ad2d8c50c3000000000000225120a12e5d145a4a3ab43f6cc1188435e74f253eace72bd986f1aaf780fd0c6532360140bee11f7f644cf09d5031683203bbe61109090b1e4be4626e13de7a889d6e5d2f154233a2bfaf9cb983f31ccf01b1be5db2cd37bb0cb9a395e2632bc50105b4583f860000";
let requirements = BondRequirements {
min_input_sum_sat: 100000,

View File

@ -8,6 +8,7 @@ use bdk::{
pub trait BondTx {
fn input_sum<D: Database, B: GetTx>(&self, blockchain: &B, db: &D) -> Result<u64>;
fn bond_output_sum(&self, bond_address: &str) -> Result<u64>;
fn all_output_sum(&self) -> u64;
}
impl BondTx for Transaction {
@ -50,4 +51,8 @@ impl BondTx for Transaction {
}
Err(anyhow!("No output to bond address in transaction"))
}
fn all_output_sum(&self) -> u64 {
self.output.iter().map(|output| output.value).sum()
}
}