complete bond validation function

This commit is contained in:
f321x
2024-07-03 11:25:05 +00:00
parent ef6ca16337
commit b7a3522069
4 changed files with 249 additions and 215 deletions

View File

@ -21,7 +21,7 @@ struct AwaitingBondOffer {
bond_amount_sat: u64,
}
struct AwaitinigTakerOffer {
struct AwaitingTakerOffer {
offer_id: String,
robohash_maker: Vec<u8>,
is_buy_order: bool,
@ -160,7 +160,7 @@ impl CoordinatorDB {
})
}
pub async fn fetch_and_delete_offer_from_bond_table(
async fn fetch_and_delete_offer_from_bond_table(
&self,
robohash_hex: &str,
) -> Result<AwaitingBondOffer> {
@ -271,10 +271,10 @@ impl CoordinatorDB {
})
}
pub async fn fetch_and_delete_offer_from_public_offers_table(
async fn fetch_and_delete_offer_from_public_offers_table(
&self,
offer_id_hex: &str,
) -> Result<AwaitinigTakerOffer> {
) -> Result<AwaitingTakerOffer> {
let fetched_values = sqlx::query_as::<_, (Vec<u8>, bool, 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> = ?",
@ -289,7 +289,7 @@ impl CoordinatorDB {
.execute(&*self.db_pool)
.await?;
Ok(AwaitinigTakerOffer {
Ok(AwaitingTakerOffer {
offer_id: offer_id_hex.to_string(),
robohash_maker: fetched_values.0,
is_buy_order: fetched_values.1,
@ -475,7 +475,9 @@ mod tests {
.await?;
// Fetch and delete the order request
let fetched_offer = database.fetch_maker_request(&robohash_hex.to_string()).await?;
let fetched_offer = database
.fetch_maker_request(&robohash_hex.to_string())
.await?;
// Verify the result
let expected = BondRequirementResponse {
@ -519,7 +521,9 @@ mod tests {
.await?;
// Fetch and delete the order request
let fetched_offer = database.fetch_and_delete_offer_from_bond_table(robohash_hex).await?;
let fetched_offer = database
.fetch_and_delete_offer_from_bond_table(robohash_hex)
.await?;
// Verify the fetched offer
let expected_offer = AwaitingBondOffer {
@ -548,7 +552,6 @@ mod tests {
// Create a temporary SQLite database
let database = create_coordinator().await?;
// Insert a test entry into maker_requests
let robohash_hex = "a3f1f1f0e2f3f4f5";
let order_request = (
@ -676,7 +679,6 @@ mod tests {
.await?;
}
// Create a sample OffersRequest
let offers_request = OffersRequest {
buy_offers: true,
@ -731,7 +733,9 @@ mod tests {
.await?;
// Call the fetch_taker_bond_requirements function
let result = database.fetch_taker_bond_requirements(&offer_id_hex.to_string()).await?;
let result = database
.fetch_taker_bond_requirements(&offer_id_hex.to_string())
.await?;
// Verify the result
assert_eq!(result.bond_address, taker_bond_address);
@ -778,7 +782,9 @@ mod tests {
.await?;
// Call the fetch_and_delete_offer_from_public_offers_table function
let result = database.fetch_and_delete_offer_from_public_offers_table(offer_id_hex).await?;
let result = database
.fetch_and_delete_offer_from_public_offers_table(offer_id_hex)
.await?;
// Verify the result
assert_eq!(result.offer_id, offer_id_hex);
@ -795,7 +801,8 @@ mod tests {
assert_eq!(result.musig_pubkey_hex_maker, musig_pubkey_hex);
// Verify the deletion
let remaining_offers = sqlx::query("SELECT COUNT(*) FROM active_maker_offers WHERE offer_id = ?")
let remaining_offers =
sqlx::query("SELECT COUNT(*) FROM active_maker_offers WHERE offer_id = ?")
.bind(offer_id_hex)
.fetch_one(&*database.db_pool)
.await?;
@ -805,5 +812,4 @@ mod tests {
Ok(())
}
}

View File

@ -1,2 +0,0 @@
use super::*;
use bdk::wallet::verify::verify_tx;

View File

@ -64,30 +64,51 @@ impl CoordinatorWallet {
&self,
bond: &String,
requirements: BondRequirements,
) -> Result<bool> {
) -> Result<()> {
let input_sum: u64;
let tx: Transaction = deserialize(&hex::decode(bond)?)?;
let wallet = self.wallet.lock().await;
{
let blockchain = ElectrumBlockchain::from(Client::new(
&env::var("ELECTRUM_BACKEND")
.context("Parsing ELECTRUM_BACKEND from .env failed, is it set?")?,
)?);
let wallet = self.wallet.lock().await;
// we need to test this with signed and invalid/unsigned transactions
// checks signatures and inputs
if let Err(e) = verify_tx(&tx, &*wallet.database(), &blockchain) {
dbg!(e);
return Ok(false);
return Err(anyhow!(e));
}
// check if the tx has the correct input amounts (have to be >= trading amount)
if tx.input_sum(&blockchain, &*wallet.database())? < requirements.min_input_sum_sat {
return Ok(false);
input_sum = match tx.input_sum(&blockchain, &*wallet.database()) {
Ok(amount) => {
if amount < requirements.min_input_sum_sat {
return Err(anyhow!("Bond input sum too small"));
}
amount
}
Err(e) => {
return Err(anyhow!(e));
}
};
}
// check if bond output to us is big enough
// trait bond_output_sum
let output_sum = match tx.bond_output_sum(&requirements.bond_address) {
Ok(amount) => {
if amount < requirements.locking_amount_sat {
return Err(anyhow!("Bond output sum too small"));
}
amount
}
Err(e) => {
return Err(anyhow!(e));
}
};
// let valid = tx.verify_tx();
Ok(true)
if ((input_sum - output_sum) / tx.vsize() as u64) < 200 {
return Err(anyhow!("Bond fee rate too low"));
}
Ok(())
}
}

View File

@ -1,5 +1,9 @@
use super::*;
use bdk::{blockchain::GetTx, database::Database};
use bdk::{
bitcoin::{Address, Network},
blockchain::GetTx,
database::Database,
};
pub trait BondTx {
fn input_sum<D: Database, B: GetTx>(&self, blockchain: &B, db: &D) -> Result<u64>;
@ -35,10 +39,15 @@ impl BondTx for Transaction {
}
fn bond_output_sum(&self, bond_address: &str) -> Result<u64> {
panic!("implement");
// let bond_script = ScriptBuf;
let bond_script = Address::from_str(bond_address)?
.require_network(Network::Testnet)?
.script_pubkey();
for output in self.output.iter() {}
Ok(0)
for output in self.output.iter() {
if output.script_pubkey == bond_script {
return Ok(output.value);
}
}
Err(anyhow!("No output to bond address in transaction"))
}
}