mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-05-20 23:50:47 -04:00
Unify logging of broadcasted transactions
We eliminate unnecessary layers of indirection for broadcasting logic and force our callers to provide us with the `kind` of transaction that we are publishing. Eventually, we can replace this string with some type-system magic we can derive the name from the actual transaction. For now, we just require the caller to duplicate this information because it is faster and good enough TM.
This commit is contained in:
parent
3a503bf95f
commit
8c9b087e39
4 changed files with 44 additions and 66 deletions
|
@ -147,14 +147,20 @@ impl Wallet {
|
||||||
self.inner.lock().await.network()
|
self.inner.lock().await.network()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn broadcast(&self, transaction: Transaction) -> Result<Txid> {
|
/// Broadcast the given transaction to the network and emit a log statement
|
||||||
|
/// if done so successfully.
|
||||||
|
pub async fn broadcast(&self, transaction: Transaction, kind: &str) -> Result<Txid> {
|
||||||
let txid = transaction.txid();
|
let txid = transaction.txid();
|
||||||
|
|
||||||
self.inner
|
self.inner
|
||||||
.lock()
|
.lock()
|
||||||
.await
|
.await
|
||||||
.broadcast(transaction)
|
.broadcast(transaction)
|
||||||
.with_context(|| format!("failed to broadcast transaction {}", txid))?;
|
.with_context(|| {
|
||||||
|
format!("failed to broadcast Bitcoin {} transaction {}", kind, txid)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
tracing::info!("Published Bitcoin {} transaction as {}", txid, kind);
|
||||||
|
|
||||||
Ok(txid)
|
Ok(txid)
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,6 @@ use libp2p::PeerId;
|
||||||
use sha2::Sha256;
|
use sha2::Sha256;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
use tracing::info;
|
|
||||||
|
|
||||||
// TODO(Franck): Use helper functions from xmr-btc instead of re-writing them
|
// TODO(Franck): Use helper functions from xmr-btc instead of re-writing them
|
||||||
// here
|
// here
|
||||||
|
@ -125,16 +124,6 @@ pub fn build_bitcoin_redeem_transaction(
|
||||||
Ok(tx)
|
Ok(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn publish_bitcoin_redeem_transaction(
|
|
||||||
redeem_tx: bitcoin::Transaction,
|
|
||||||
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
||||||
) -> Result<::bitcoin::Txid> {
|
|
||||||
info!("Attempting to publish bitcoin redeem txn");
|
|
||||||
let txid = bitcoin_wallet.broadcast(redeem_tx).await?;
|
|
||||||
|
|
||||||
Ok(txid)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn publish_cancel_transaction(
|
pub async fn publish_cancel_transaction(
|
||||||
tx_lock: TxLock,
|
tx_lock: TxLock,
|
||||||
a: bitcoin::SecretKey,
|
a: bitcoin::SecretKey,
|
||||||
|
@ -170,7 +159,7 @@ pub async fn publish_cancel_transaction(
|
||||||
.expect("sig_{a,b} to be valid signatures for tx_cancel");
|
.expect("sig_{a,b} to be valid signatures for tx_cancel");
|
||||||
|
|
||||||
// TODO(Franck): Error handling is delicate, why can't we broadcast?
|
// TODO(Franck): Error handling is delicate, why can't we broadcast?
|
||||||
bitcoin_wallet.broadcast(tx_cancel).await?;
|
bitcoin_wallet.broadcast(tx_cancel, "cancel").await?;
|
||||||
|
|
||||||
// TODO(Franck): Wait until transaction is mined and returned mined
|
// TODO(Franck): Wait until transaction is mined and returned mined
|
||||||
// block height
|
// block height
|
||||||
|
@ -248,17 +237,3 @@ pub fn build_bitcoin_punish_transaction(
|
||||||
|
|
||||||
Ok(signed_tx_punish)
|
Ok(signed_tx_punish)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn publish_bitcoin_punish_transaction(
|
|
||||||
punish_tx: bitcoin::Transaction,
|
|
||||||
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
||||||
execution_params: ExecutionParams,
|
|
||||||
) -> Result<bitcoin::Txid> {
|
|
||||||
let txid = bitcoin_wallet.broadcast(punish_tx).await?;
|
|
||||||
|
|
||||||
bitcoin_wallet
|
|
||||||
.wait_for_transaction_finality(txid, execution_params)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(txid)
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,8 +15,7 @@ use crate::{
|
||||||
event_loop::EventLoopHandle,
|
event_loop::EventLoopHandle,
|
||||||
steps::{
|
steps::{
|
||||||
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
|
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
|
||||||
extract_monero_private_key, lock_xmr, publish_bitcoin_punish_transaction,
|
extract_monero_private_key, lock_xmr, publish_cancel_transaction,
|
||||||
publish_bitcoin_redeem_transaction, publish_cancel_transaction,
|
|
||||||
wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund,
|
wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund,
|
||||||
wait_for_locked_bitcoin,
|
wait_for_locked_bitcoin,
|
||||||
},
|
},
|
||||||
|
@ -219,10 +218,7 @@ async fn run_until_internal(
|
||||||
state3.B,
|
state3.B,
|
||||||
&state3.redeem_address,
|
&state3.redeem_address,
|
||||||
) {
|
) {
|
||||||
Ok(tx) => {
|
Ok(tx) => match bitcoin_wallet.broadcast(tx, "redeem").await {
|
||||||
match publish_bitcoin_redeem_transaction(tx, bitcoin_wallet.clone())
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(txid) => {
|
Ok(txid) => {
|
||||||
let publishded_redeem_tx = bitcoin_wallet
|
let publishded_redeem_tx = bitcoin_wallet
|
||||||
.wait_for_transaction_finality(txid, execution_params)
|
.wait_for_transaction_finality(txid, execution_params)
|
||||||
|
@ -238,9 +234,7 @@ async fn run_until_internal(
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Publishing the redeem transaction failed with {}, attempting to wait for cancellation now. If you restart the application before the timelock is expired publishing the redeem transaction will be retried.", e);
|
error!("Publishing the redeem transaction failed with {}, attempting to wait for cancellation now. If you restart the application before the timelock is expired publishing the redeem transaction will be retried.", e);
|
||||||
state3
|
state3
|
||||||
.wait_for_cancel_timelock_to_expire(
|
.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref())
|
||||||
bitcoin_wallet.as_ref(),
|
|
||||||
)
|
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
AliceState::CancelTimelockExpired {
|
AliceState::CancelTimelockExpired {
|
||||||
|
@ -248,8 +242,7 @@ async fn run_until_internal(
|
||||||
monero_wallet_restore_blockheight,
|
monero_wallet_restore_blockheight,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Constructing the redeem transaction failed with {}, attempting to wait for cancellation now.", e);
|
error!("Constructing the redeem transaction failed with {}, attempting to wait for cancellation now.", e);
|
||||||
state3
|
state3
|
||||||
|
@ -427,11 +420,15 @@ async fn run_until_internal(
|
||||||
state3.B,
|
state3.B,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let punish_tx_finalised = publish_bitcoin_punish_transaction(
|
let punish_tx_finalised = async {
|
||||||
signed_tx_punish,
|
let txid = bitcoin_wallet.broadcast(signed_tx_punish, "punish").await?;
|
||||||
bitcoin_wallet.clone(),
|
|
||||||
execution_params,
|
bitcoin_wallet
|
||||||
);
|
.wait_for_transaction_finality(txid, execution_params)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Result::<_, anyhow::Error>::Ok(txid)
|
||||||
|
};
|
||||||
|
|
||||||
let refund_tx_seen = bitcoin_wallet.watch_for_raw_transaction(tx_refund.txid());
|
let refund_tx_seen = bitcoin_wallet.watch_for_raw_transaction(tx_refund.txid());
|
||||||
|
|
||||||
|
|
|
@ -271,8 +271,7 @@ impl State2 {
|
||||||
pub async fn lock_btc(self, bitcoin_wallet: &bitcoin::Wallet) -> Result<State3> {
|
pub async fn lock_btc(self, bitcoin_wallet: &bitcoin::Wallet) -> Result<State3> {
|
||||||
let signed_tx_lock = bitcoin_wallet.sign_tx_lock(self.tx_lock.clone()).await?;
|
let signed_tx_lock = bitcoin_wallet.sign_tx_lock(self.tx_lock.clone()).await?;
|
||||||
|
|
||||||
tracing::debug!("locking BTC in transaction {}", self.tx_lock.txid());
|
let _ = bitcoin_wallet.broadcast(signed_tx_lock, "lock").await?;
|
||||||
let _ = bitcoin_wallet.broadcast(signed_tx_lock).await?;
|
|
||||||
|
|
||||||
Ok(State3 {
|
Ok(State3 {
|
||||||
A: self.A,
|
A: self.A,
|
||||||
|
@ -475,7 +474,8 @@ impl State4 {
|
||||||
tx_cancel",
|
tx_cancel",
|
||||||
);
|
);
|
||||||
|
|
||||||
let tx_id = bitcoin_wallet.broadcast(tx_cancel).await?;
|
let tx_id = bitcoin_wallet.broadcast(tx_cancel, "cancel").await?;
|
||||||
|
|
||||||
Ok(tx_id)
|
Ok(tx_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -544,7 +544,7 @@ impl State4 {
|
||||||
let signed_tx_refund =
|
let signed_tx_refund =
|
||||||
tx_refund.add_signatures((self.A, sig_a), (self.b.public(), sig_b))?;
|
tx_refund.add_signatures((self.A, sig_a), (self.b.public(), sig_b))?;
|
||||||
|
|
||||||
let txid = bitcoin_wallet.broadcast(signed_tx_refund).await?;
|
let txid = bitcoin_wallet.broadcast(signed_tx_refund, "refund").await?;
|
||||||
|
|
||||||
bitcoin_wallet
|
bitcoin_wallet
|
||||||
.wait_for_transaction_finality(txid, execution_params)
|
.wait_for_transaction_finality(txid, execution_params)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue