From ec05e6132343713a29dc1e6f4c7d72bb77660fbf Mon Sep 17 00:00:00 2001 From: Felix <51097237+f321x@users.noreply.github.com> Date: Thu, 11 Jul 2024 17:41:40 +0000 Subject: [PATCH] refactor monitoring and bond validation, fix tests --- .../coordinator/src/coordinator/monitoring.rs | 3 +- .../coordinator/src/wallet/mod.rs | 67 +++++++++++++------ 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/taptrade-cli-demo/coordinator/src/coordinator/monitoring.rs b/taptrade-cli-demo/coordinator/src/coordinator/monitoring.rs index 10c8b91..a9d18a7 100644 --- a/taptrade-cli-demo/coordinator/src/coordinator/monitoring.rs +++ b/taptrade-cli-demo/coordinator/src/coordinator/monitoring.rs @@ -8,6 +8,7 @@ use super::*; pub enum Table { Orderbook, ActiveTrades, + Memory, } #[derive(Debug, Clone)] @@ -55,7 +56,7 @@ pub async fn monitor_bonds(coordinator: Arc) -> Result<()> { punish_trader(&coordinator, &bond).await?; } "0" => { - dbg!("Punishment disabled, ignoring bond violation: {:?}", e); + dbg!("Punishment disabled, ignoring bond violation: {:?}", error); continue; } _ => Err(anyhow!("Invalid PUNISHMENT_ENABLED env var"))?, diff --git a/taptrade-cli-demo/coordinator/src/wallet/mod.rs b/taptrade-cli-demo/coordinator/src/wallet/mod.rs index c898997..f9a9381 100644 --- a/taptrade-cli-demo/coordinator/src/wallet/mod.rs +++ b/taptrade-cli-demo/coordinator/src/wallet/mod.rs @@ -73,6 +73,26 @@ impl CoordinatorWallet { Ok(address.address.to_string()) } + pub async fn validate_bond_tx_hex( + &self, + bond_tx_hex: &str, + requirements: &BondRequirements, + ) -> Result<()> { + debug!("Validating bond in validate_bond_tx_hex()"); + let dummy_monitoring_bond = MonitoringBond { + bond_tx_hex: bond_tx_hex.to_string(), + trade_id_hex: "0".to_string(), + robot: vec![0], + requirements: requirements.clone(), + table: Table::Memory, + }; + let invalid_bond = self.validate_bonds(&vec![dummy_monitoring_bond]).await?; + if !invalid_bond.is_empty() { + return Err(anyhow!(invalid_bond[0].1.to_string())); + } + Ok(()) + } + // validate bond (check amounts, valid inputs, correct addresses, valid signature, feerate) // also check if inputs are confirmed already // bdk::blockchain::compact_filters::Mempool::iter_txs() -> Vec(Tx) to check if contained in mempool @@ -142,11 +162,21 @@ impl CoordinatorWallet { } } } + self.test_mempool_accept_bonds(bonds, &mut invalid_bonds)?; + debug!("validate_bond_tx_hex(): Bond validation done."); + Ok(invalid_bonds) + } + fn test_mempool_accept_bonds( + &self, + bonds: &Vec, + invalid_bonds: &mut Vec<(MonitoringBond, anyhow::Error)>, + ) -> Result<()> { let raw_bonds: Vec = bonds .iter() .map(|bond| bond.bond_tx_hex.clone().raw_hex()) // Assuming `raw_hex()` returns a String or &str .collect(); + let test_mempool_accept_res = self .json_rpc_client .deref() @@ -154,7 +184,7 @@ impl CoordinatorWallet { for res in test_mempool_accept_res { if !res.allowed { - let invalid_bond = + let invalid_bond: MonitoringBond = Self::search_monitoring_bond_by_txid(&bonds, &res.txid.to_string())?; invalid_bonds.push(( invalid_bond, @@ -164,23 +194,13 @@ impl CoordinatorWallet { .unwrap_or("rejected by testmempoolaccept".to_string()) ), )); - } + }; } - - debug!("validate_bond_tx_hex(): Bond validation done."); - Ok(invalid_bonds) - } - - pub fn publish_bond_tx_hex(&self, bond: &str) -> Result<()> { - warn!("publish_bond_tx_hex(): publishing cheating bond tx!"); - let blockchain = &*self.backend; - let tx: Transaction = deserialize(&hex::decode(bond)?)?; - - blockchain.broadcast(&tx)?; Ok(()) } fn search_monitoring_bond_by_txid( + // this should not happen often, so the inefficiency is acceptable monitoring_bonds: &Vec, txid: &str, ) -> Result { @@ -192,6 +212,15 @@ impl CoordinatorWallet { } Err(anyhow!("Bond not found in monitoring bonds")) } + + pub fn publish_bond_tx_hex(&self, bond: &str) -> Result<()> { + warn!("publish_bond_tx_hex(): publishing cheating bond tx!"); + let blockchain = &*self.backend; + let tx: Transaction = deserialize(&hex::decode(bond)?)?; + + blockchain.broadcast(&tx)?; + Ok(()) + } } impl fmt::Debug for CoordinatorWallet { @@ -211,19 +240,19 @@ mod tests { use bdk::bitcoin::Network; use bdk::database::MemoryDatabase; use bdk::{blockchain::RpcBlockchain, Wallet}; - async fn new_test_wallet(wallet_xprv: &str) -> CoordinatorWallet { + dotenv().ok(); let rpc_config = RpcConfig { - url: env::var("BITCOIN_RPC_ADDRESS_PORT")?.to_string(), + url: env::var("BITCOIN_RPC_ADDRESS_PORT").unwrap().to_string(), auth: Auth::Cookie { - file: env::var("BITCOIN_RPC_COOKIE_FILE_PATH")?.into(), + file: env::var("BITCOIN_RPC_COOKIE_FILE_PATH").unwrap().into(), }, network: bdk::bitcoin::Network::Testnet, - wallet_name: env::var("BITCOIN_RPC_WALLET_NAME")?, + wallet_name: env::var("BITCOIN_RPC_WALLET_NAME").unwrap(), sync_params: None, }; - let json_rpc_client = Client::new(&rpc_config.url, rpc_config.auth.clone().into())?; - let backend = RpcBlockchain::from_config(&rpc_config)?; + let json_rpc_client = Client::new(&rpc_config.url, rpc_config.auth.clone().into()).unwrap(); + let backend = RpcBlockchain::from_config(&rpc_config).unwrap(); let wallet_xprv = ExtendedPrivKey::from_str(wallet_xprv).unwrap(); let wallet = Wallet::new(