mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2025-07-18 08:43:17 +00:00
mempool bond validation done, monitoring now fully functional:)
This commit is contained in:
@ -1 +1 @@
|
||||
__cookie__:b3219c105fc87f4de97f8a14a17ea82da2f0e5c17ba79bbf45e641de96cd6a55
|
||||
__cookie__:7b9dd407a2bf94a226776d1d35d5aeb64148b9425ddd6cf286381d6ab1d5f585
|
@ -1,17 +1,16 @@
|
||||
use super::*;
|
||||
use anyhow::Ok;
|
||||
use bdk::bitcoin::consensus::encode::deserialize;
|
||||
use bdk::bitcoin::Transaction;
|
||||
use bdk::bitcoin::{OutPoint, Transaction};
|
||||
use bdk::bitcoin::{TxIn, Txid};
|
||||
use bdk::bitcoincore_rpc::{Client, RpcApi};
|
||||
use serde::Deserialize;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::ops::Deref;
|
||||
use std::sync::RwLock;
|
||||
|
||||
struct Mempool {
|
||||
transactions: Arc<RwLock<HashMap<Txid, Vec<TxIn>>>>,
|
||||
utxo_set: Arc<RwLock<HashSet<TxIn>>>,
|
||||
utxo_set: Arc<RwLock<HashSet<OutPoint>>>,
|
||||
json_rpc_client: Arc<Client>,
|
||||
}
|
||||
|
||||
@ -75,7 +74,7 @@ fn run_mempool(mempool: Arc<Mempool>) {
|
||||
utxo_set.clear();
|
||||
for (_, inputs) in mempool_state.iter() {
|
||||
for input in inputs {
|
||||
utxo_set.insert(input.clone());
|
||||
utxo_set.insert(input.previous_output);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,16 +96,18 @@ impl MempoolHandler {
|
||||
&self,
|
||||
bonds: &Vec<MonitoringBond>,
|
||||
) -> 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 utxo_set = self
|
||||
.mempool
|
||||
.utxo_set
|
||||
.read()
|
||||
.expect("Error locking utxo_set read mutex");
|
||||
debug!("Mempool utxo_set: {:?}", utxo_set);
|
||||
for bond in bonds {
|
||||
let bond_tx: Transaction = deserialize(&hex::decode(&bond.bond_tx_hex)?)?;
|
||||
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")));
|
||||
break;
|
||||
}
|
||||
|
@ -30,8 +30,9 @@ impl MonitoringBond {
|
||||
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
|
||||
debug!("Removing violating bond from db tables");
|
||||
db.remove_violating_bond(self)
|
||||
.await
|
||||
.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
|
||||
|
||||
// remove offer from db/orderbook
|
||||
self.remove_from_db_tables(coordinator.coordinator_db.clone())
|
||||
self.remove_from_db_tables(&coordinator.coordinator_db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
@ -81,11 +82,11 @@ pub async fn monitor_bonds(coordinator: Arc<Coordinator>) -> Result<()> {
|
||||
.as_str()
|
||||
{
|
||||
"1" => {
|
||||
dbg!("Punishing trader for bond violation: {:?}", error);
|
||||
warn!("Punishing trader for bond violation: {:?}", error);
|
||||
bond.punish(&coordinator).await?;
|
||||
}
|
||||
"0" => {
|
||||
dbg!("Punishment disabled, ignoring bond violation: {:?}", error);
|
||||
warn!("Punishment disabled, ignoring bond violation: {:?}", error);
|
||||
continue;
|
||||
}
|
||||
_ => Err(anyhow!("Invalid PUNISHMENT_ENABLED env var"))?,
|
||||
|
@ -4,5 +4,5 @@ ROBOHASH_HEX="26ee3dee4815655d223c3505162fd4610294a9542f89bb3d3e9748f534ac10ae"
|
||||
TRADE_TYPE="buy"
|
||||
PAYOUT_ADDRESS="tb1p45daj2eaza6drcd85c3wvn0zrpqxuduk3rzcmla4eu7a02cep9kqjzkc64"
|
||||
BOND_RATIO=5
|
||||
XPRV="tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32" # wallet xprv
|
||||
XPRV="tprv8ZgxMBicQKsPdRP5cDng7tV2hShHRDqRGGp749EEiXgP9t7RXCPqhPyHfDUL4pG6pzYD7mX4Kmx6Y21bdTDuNAwcDaPCkTNJn2odyRdCHRU" # wallet xprv
|
||||
OFFER_DURATION_HOURS=48
|
||||
|
@ -47,13 +47,13 @@ impl Bond {
|
||||
|
||||
builder
|
||||
.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));
|
||||
|
||||
builder.finish()?
|
||||
};
|
||||
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 {
|
||||
return Err(anyhow!("Transaction could not be finalized"));
|
||||
};
|
||||
|
@ -4,5 +4,5 @@ ROBOHASH_HEX="169b6049cf865eba7d01e1ad26975f1d5ff29d570297ff18d40a53c8281dff5d"
|
||||
TRADE_TYPE="sell"
|
||||
PAYOUT_ADDRESS="tb1pca4thykxsj4ura8h2pj3zx7v9hzlcvlw9k32u8m0vqs6mxp02c9qr9eup6"
|
||||
BOND_RATIO=5
|
||||
XPRV="tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32" # wallet xprv
|
||||
XPRV="tprv8ZgxMBicQKsPdrVEng4ZxVWady4HcwJp34wDo5VmA34J5V2rUfPTeQbcsiTbx5YWZQKnSfCE5vLBtxcBjZafH5L1JJNHtjuVMDyBtDogfeG" # wallet xprv
|
||||
OFFER_DURATION_HOURS=48
|
||||
|
Reference in New Issue
Block a user