From cbaa665140155e7591c8668ae368d7ef58fdfda2 Mon Sep 17 00:00:00 2001 From: f321x Date: Thu, 27 Jun 2024 10:23:58 +0000 Subject: [PATCH] additional sql table for active offers --- taptrade-cli-demo/coordinator/Cargo.lock | 11 +++++++++++ taptrade-cli-demo/coordinator/Cargo.toml | 2 +- .../coordinator/src/communication/mod.rs | 5 ++++- .../coordinator/src/coordinator/monitoring.rs | 4 ++++ .../coordinator/src/database/mod.rs | 18 ++++++++++++++++++ .../coordinator/src/wallet/mod.rs | 15 ++++++++++++++- 6 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 taptrade-cli-demo/coordinator/src/coordinator/monitoring.rs diff --git a/taptrade-cli-demo/coordinator/Cargo.lock b/taptrade-cli-demo/coordinator/Cargo.lock index 33882b0..a18a9c7 100644 --- a/taptrade-cli-demo/coordinator/Cargo.lock +++ b/taptrade-cli-demo/coordinator/Cargo.lock @@ -177,6 +177,7 @@ dependencies = [ "async-trait", "bdk-macros", "bitcoin", + "bitcoinconsensus", "electrum-client", "getrandom", "js-sys", @@ -237,6 +238,16 @@ dependencies = [ "serde", ] +[[package]] +name = "bitcoinconsensus" +version = "0.19.0-3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a8aa43b5cd02f856cb126a9af819e77b8910fdd74dd1407be649f2f5fe3a1b5" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "bitflags" version = "1.3.2" diff --git a/taptrade-cli-demo/coordinator/Cargo.toml b/taptrade-cli-demo/coordinator/Cargo.toml index d873600..ffea849 100644 --- a/taptrade-cli-demo/coordinator/Cargo.toml +++ b/taptrade-cli-demo/coordinator/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [dependencies] anyhow = "1.0.86" axum = { version = "0.7.5", features = ["tokio", "json"] } -bdk = { version = "0.29.0", features = ["key-value-db"] } +bdk = { version = "0.29.0", features = ["key-value-db", "bitcoinconsensus", "verify"] } dotenv = "0.15.0" hex = "0.4.3" reqwest = { version = "0.12.4", features = ["blocking", "json"] } diff --git a/taptrade-cli-demo/coordinator/src/communication/mod.rs b/taptrade-cli-demo/coordinator/src/communication/mod.rs index b6d76b2..54411c2 100755 --- a/taptrade-cli-demo/coordinator/src/communication/mod.rs +++ b/taptrade-cli-demo/coordinator/src/communication/mod.rs @@ -51,10 +51,13 @@ async fn submit_maker_bond( // validate bond (check amounts, valid inputs, correct addresses, valid signature, feerate) wallet - .validate_bond_tx_hex(&payload.signed_bond_hex) + .validate_bond_tx_hex(&payload.signed_bond_hex, &bond_requirements) .await?; // insert bond into sql database + database + .move_offer_to_active(&payload, &bond_requirements) + .await?; // begin monitoring bond diff --git a/taptrade-cli-demo/coordinator/src/coordinator/monitoring.rs b/taptrade-cli-demo/coordinator/src/coordinator/monitoring.rs new file mode 100644 index 0000000..555d5c7 --- /dev/null +++ b/taptrade-cli-demo/coordinator/src/coordinator/monitoring.rs @@ -0,0 +1,4 @@ +// we create an async function that loops trough the sqlite db table active_maker_offers and +// continoously verifies the bond inputs (mempool and chain), maybe with some caching in a hashmap to +// prevent querying the db all the time. +// Also needs to implement punishment logic in case a fraud is detected. diff --git a/taptrade-cli-demo/coordinator/src/database/mod.rs b/taptrade-cli-demo/coordinator/src/database/mod.rs index b2b25d8..45b50bc 100644 --- a/taptrade-cli-demo/coordinator/src/database/mod.rs +++ b/taptrade-cli-demo/coordinator/src/database/mod.rs @@ -39,6 +39,22 @@ impl CoordinatorDB { ) .execute(&db_pool) .await?; + sqlx::query( + // robohash is binary hash + "CREATE TABLE IF NOT EXISTS active_maker_offers ( + trade_id BLOB PRIMARY KEY, + robohash BLOB, + is_buy_order INTEGER, + amount_sat INTEGER NOT NULL, + bond_ratio INTEGER NOT NULL, + offer_duration_ts INTEGER NOT NULL, + bond_address TEXT NOT NULL, + bond_amount_sat INTEGER NOT NULL + bond_tx_hex TEXT NOT NULL + )", + ) + .execute(&db_pool) + .await?; dbg!("Database initialized"); let shared_db_pool = Arc::new(db_pool); Ok(Self { @@ -84,4 +100,6 @@ impl CoordinatorDB { locking_amount_sat: maker_request.try_get::("bond_amount_sat")? as u64, }) } + + pub async fn move_offer_to_active(&self) -> Result<()> {} } diff --git a/taptrade-cli-demo/coordinator/src/wallet/mod.rs b/taptrade-cli-demo/coordinator/src/wallet/mod.rs index a65d466..9cfa047 100644 --- a/taptrade-cli-demo/coordinator/src/wallet/mod.rs +++ b/taptrade-cli-demo/coordinator/src/wallet/mod.rs @@ -6,6 +6,7 @@ use bdk::{ electrum_client::Client, sled::{self, Tree}, template::Bip86, + wallet::verify::*, KeychainKind, SyncOptions, Wallet, }; use std::str::FromStr; @@ -13,6 +14,7 @@ use std::str::FromStr; #[derive(Clone, Debug)] pub struct CoordinatorWallet { pub wallet: Arc>>, + // database: Arc>, } impl CoordinatorWallet { @@ -38,6 +40,7 @@ impl CoordinatorWallet { dbg!(wallet.get_balance()?); Ok(CoordinatorWallet { wallet: Arc::new(Mutex::new(wallet)), + // database: Arc::new(Mutex::new(sled_db)), }) } @@ -48,9 +51,19 @@ impl CoordinatorWallet { } // validate bond (check amounts, valid inputs, correct addresses, valid signature, feerate) - pub async fn validate_bond_tx_hex(&self, bond: &String) -> Result { + pub async fn validate_bond_tx_hex( + &self, + bond: &String, + requirements: &BondRequirementResponse, + ) -> Result { let tx: Transaction = deserialize(&hex::decode(bond)?)?; + let wallet = self.wallet.lock().await; + // we need to test this with signed and invalid/unsigned transactions + // let result = verify_tx(&tx, wallet.database(), blockchain); + + // let valid = tx.verify_tx(); + panic!("Not implemented"); Ok(true) } }