mempool bond validation done, monitoring now fully functional:)

This commit is contained in:
f321x
2024-07-16 19:55:10 +02:00
parent 78796fe851
commit ad07b393a3
6 changed files with 16 additions and 14 deletions

View File

@ -1 +1 @@
__cookie__:b3219c105fc87f4de97f8a14a17ea82da2f0e5c17ba79bbf45e641de96cd6a55 __cookie__:7b9dd407a2bf94a226776d1d35d5aeb64148b9425ddd6cf286381d6ab1d5f585

View File

@ -1,17 +1,16 @@
use super::*; use super::*;
use anyhow::Ok; use anyhow::Ok;
use bdk::bitcoin::consensus::encode::deserialize; use bdk::bitcoin::consensus::encode::deserialize;
use bdk::bitcoin::Transaction; use bdk::bitcoin::{OutPoint, Transaction};
use bdk::bitcoin::{TxIn, Txid}; use bdk::bitcoin::{TxIn, Txid};
use bdk::bitcoincore_rpc::{Client, RpcApi}; use bdk::bitcoincore_rpc::{Client, RpcApi};
use serde::Deserialize;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::ops::Deref; use std::ops::Deref;
use std::sync::RwLock; use std::sync::RwLock;
struct Mempool { struct Mempool {
transactions: Arc<RwLock<HashMap<Txid, Vec<TxIn>>>>, transactions: Arc<RwLock<HashMap<Txid, Vec<TxIn>>>>,
utxo_set: Arc<RwLock<HashSet<TxIn>>>, utxo_set: Arc<RwLock<HashSet<OutPoint>>>,
json_rpc_client: Arc<Client>, json_rpc_client: Arc<Client>,
} }
@ -75,7 +74,7 @@ fn run_mempool(mempool: Arc<Mempool>) {
utxo_set.clear(); utxo_set.clear();
for (_, inputs) in mempool_state.iter() { for (_, inputs) in mempool_state.iter() {
for input in inputs { for input in inputs {
utxo_set.insert(input.clone()); utxo_set.insert(input.previous_output);
} }
} }
} }
@ -97,16 +96,18 @@ impl MempoolHandler {
&self, &self,
bonds: &Vec<MonitoringBond>, bonds: &Vec<MonitoringBond>,
) -> Result<HashMap<Vec<u8>, (MonitoringBond, anyhow::Error)>> { ) -> Result<HashMap<Vec<u8>, (MonitoringBond, anyhow::Error)>> {
debug!("Looking up mempool inputs for bonds");
let mut bonds_to_punish: HashMap<Vec<u8>, (MonitoringBond, anyhow::Error)> = HashMap::new(); let mut bonds_to_punish: HashMap<Vec<u8>, (MonitoringBond, anyhow::Error)> = HashMap::new();
let utxo_set = self let utxo_set = self
.mempool .mempool
.utxo_set .utxo_set
.read() .read()
.expect("Error locking utxo_set read mutex"); .expect("Error locking utxo_set read mutex");
debug!("Mempool utxo_set: {:?}", utxo_set);
for bond in bonds { for bond in bonds {
let bond_tx: Transaction = deserialize(&hex::decode(&bond.bond_tx_hex)?)?; let bond_tx: Transaction = deserialize(&hex::decode(&bond.bond_tx_hex)?)?;
for input in bond_tx.input { for input in bond_tx.input {
if utxo_set.contains(&input) { if utxo_set.contains(&input.previous_output) {
bonds_to_punish.insert(bond.id()?, (bond.clone(), anyhow!("Input in mempool"))); bonds_to_punish.insert(bond.id()?, (bond.clone(), anyhow!("Input in mempool")));
break; break;
} }

View File

@ -30,8 +30,9 @@ impl MonitoringBond {
Ok(sha256(&hex::decode(&self.bond_tx_hex)?)) Ok(sha256(&hex::decode(&self.bond_tx_hex)?))
} }
async fn remove_from_db_tables(&self, db: Arc<CoordinatorDB>) -> Result<()> { async fn remove_from_db_tables(&self, db: &Arc<CoordinatorDB>) -> Result<()> {
// remove bond from db // remove bond from db
debug!("Removing violating bond from db tables");
db.remove_violating_bond(self) db.remove_violating_bond(self)
.await .await
.context("Error removing violating bond from db")?; .context("Error removing violating bond from db")?;
@ -51,7 +52,7 @@ impl MonitoringBond {
.publish_bond_tx_hex(&self.bond_tx_hex)?; // can be made async with esplora backend if we figure out the compilation error of bdk .publish_bond_tx_hex(&self.bond_tx_hex)?; // can be made async with esplora backend if we figure out the compilation error of bdk
// remove offer from db/orderbook // remove offer from db/orderbook
self.remove_from_db_tables(coordinator.coordinator_db.clone()) self.remove_from_db_tables(&coordinator.coordinator_db)
.await?; .await?;
Ok(()) Ok(())
} }
@ -81,11 +82,11 @@ pub async fn monitor_bonds(coordinator: Arc<Coordinator>) -> Result<()> {
.as_str() .as_str()
{ {
"1" => { "1" => {
dbg!("Punishing trader for bond violation: {:?}", error); warn!("Punishing trader for bond violation: {:?}", error);
bond.punish(&coordinator).await?; bond.punish(&coordinator).await?;
} }
"0" => { "0" => {
dbg!("Punishment disabled, ignoring bond violation: {:?}", error); warn!("Punishment disabled, ignoring bond violation: {:?}", error);
continue; continue;
} }
_ => Err(anyhow!("Invalid PUNISHMENT_ENABLED env var"))?, _ => Err(anyhow!("Invalid PUNISHMENT_ENABLED env var"))?,

View File

@ -4,5 +4,5 @@ ROBOHASH_HEX="26ee3dee4815655d223c3505162fd4610294a9542f89bb3d3e9748f534ac10ae"
TRADE_TYPE="buy" TRADE_TYPE="buy"
PAYOUT_ADDRESS="tb1p45daj2eaza6drcd85c3wvn0zrpqxuduk3rzcmla4eu7a02cep9kqjzkc64" PAYOUT_ADDRESS="tb1p45daj2eaza6drcd85c3wvn0zrpqxuduk3rzcmla4eu7a02cep9kqjzkc64"
BOND_RATIO=5 BOND_RATIO=5
XPRV="tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32" # wallet xprv XPRV="tprv8ZgxMBicQKsPdRP5cDng7tV2hShHRDqRGGp749EEiXgP9t7RXCPqhPyHfDUL4pG6pzYD7mX4Kmx6Y21bdTDuNAwcDaPCkTNJn2odyRdCHRU" # wallet xprv
OFFER_DURATION_HOURS=48 OFFER_DURATION_HOURS=48

View File

@ -47,13 +47,13 @@ impl Bond {
builder builder
.add_recipient(address.script_pubkey(), bond_target.locking_amount_sat) .add_recipient(address.script_pubkey(), bond_target.locking_amount_sat)
.do_not_spend_change() // reconsider if we need this? // .do_not_spend_change() // reconsider if we need this?
.fee_rate(FeeRate::from_sat_per_vb(201.0)); .fee_rate(FeeRate::from_sat_per_vb(201.0));
builder.finish()? builder.finish()?
}; };
debug!("Signing bond transaction."); debug!("Signing bond transaction.");
let finalized = wallet.sign(&mut psbt, SignOptions::default())?; // deactivated to test bond validation let finalized = wallet.sign(&mut psbt, SignOptions::default())?; // deactivate to test bond validation
if !finalized { if !finalized {
return Err(anyhow!("Transaction could not be finalized")); return Err(anyhow!("Transaction could not be finalized"));
}; };

View File

@ -4,5 +4,5 @@ ROBOHASH_HEX="169b6049cf865eba7d01e1ad26975f1d5ff29d570297ff18d40a53c8281dff5d"
TRADE_TYPE="sell" TRADE_TYPE="sell"
PAYOUT_ADDRESS="tb1pca4thykxsj4ura8h2pj3zx7v9hzlcvlw9k32u8m0vqs6mxp02c9qr9eup6" PAYOUT_ADDRESS="tb1pca4thykxsj4ura8h2pj3zx7v9hzlcvlw9k32u8m0vqs6mxp02c9qr9eup6"
BOND_RATIO=5 BOND_RATIO=5
XPRV="tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32" # wallet xprv XPRV="tprv8ZgxMBicQKsPdrVEng4ZxVWady4HcwJp34wDo5VmA34J5V2rUfPTeQbcsiTbx5YWZQKnSfCE5vLBtxcBjZafH5L1JJNHtjuVMDyBtDogfeG" # wallet xprv
OFFER_DURATION_HOURS=48 OFFER_DURATION_HOURS=48