mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-06 21:44:49 -04:00
Avoid code duplication and nested returns
This commit is contained in:
parent
9d6525c0ae
commit
a2fc93e582
4 changed files with 27 additions and 33 deletions
|
@ -16,11 +16,10 @@ use crate::{
|
||||||
state,
|
state,
|
||||||
state::{Alice, Swap},
|
state::{Alice, Swap},
|
||||||
storage::Database,
|
storage::Database,
|
||||||
SwapAmounts, TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE,
|
SwapAmounts,
|
||||||
};
|
};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use async_recursion::async_recursion;
|
use async_recursion::async_recursion;
|
||||||
use bitcoin_harness::bitcoind_rpc::jsonrpc_client::JsonRpcError;
|
|
||||||
use futures::{
|
use futures::{
|
||||||
future::{select, Either},
|
future::{select, Either},
|
||||||
pin_mut,
|
pin_mut,
|
||||||
|
@ -474,17 +473,7 @@ pub async fn run_until(
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
AliceState::T1Expired { state3 } => {
|
AliceState::T1Expired { state3 } => {
|
||||||
if let Err(error) = state3.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
|
state3.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;
|
||||||
if let Some(json_rpc_err) = error.downcast_ref::<JsonRpcError>() {
|
|
||||||
if json_rpc_err.code == TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE {
|
|
||||||
info!("Failed to send cancel transaction, assuming that is was already included by the other party...");
|
|
||||||
} else {
|
|
||||||
return Err(error);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let state = AliceState::BtcCancelled { state3 };
|
let state = AliceState::BtcCancelled { state3 };
|
||||||
let db_state = (&state).into();
|
let db_state = (&state).into();
|
||||||
|
|
|
@ -2,7 +2,11 @@ use anyhow::{Context, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _};
|
use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _};
|
||||||
use bitcoin::util::psbt::PartiallySignedTransaction;
|
use bitcoin::util::psbt::PartiallySignedTransaction;
|
||||||
use bitcoin_harness::{bitcoind_rpc::PsbtBase64, BitcoindRpcApi};
|
use bitcoin_harness::{
|
||||||
|
bitcoind_rpc,
|
||||||
|
bitcoind_rpc::{jsonrpc_client, jsonrpc_client::JsonRpcError, PsbtBase64},
|
||||||
|
BitcoindRpcApi,
|
||||||
|
};
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::time::interval;
|
use tokio::time::interval;
|
||||||
|
@ -18,6 +22,7 @@ pub use ::bitcoin::{Address, Transaction};
|
||||||
pub use xmr_btc::bitcoin::*;
|
pub use xmr_btc::bitcoin::*;
|
||||||
|
|
||||||
pub const TX_LOCK_MINE_TIMEOUT: u64 = 3600;
|
pub const TX_LOCK_MINE_TIMEOUT: u64 = 3600;
|
||||||
|
const TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE: i64 = -27;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Wallet {
|
pub struct Wallet {
|
||||||
|
@ -105,10 +110,24 @@ impl SignTxLock for Wallet {
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl BroadcastSignedTransaction for Wallet {
|
impl BroadcastSignedTransaction for Wallet {
|
||||||
async fn broadcast_signed_transaction(&self, transaction: Transaction) -> Result<Txid> {
|
async fn broadcast_signed_transaction(&self, transaction: Transaction) -> Result<Txid> {
|
||||||
let txid = self.inner.send_raw_transaction(transaction).await?;
|
let txid = transaction.txid();
|
||||||
|
match self.inner.send_raw_transaction(transaction).await {
|
||||||
|
Err(bitcoind_rpc::Error::JsonRpcClient(jsonrpc_client::Error::JsonRpc(
|
||||||
|
JsonRpcError {
|
||||||
|
code: TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
))) => {
|
||||||
|
tracing::info!("Cancel transaction is already confirmed.");
|
||||||
|
Ok(txid)
|
||||||
|
}
|
||||||
|
Err(err) => Err(err.into()),
|
||||||
|
Ok(txid) => {
|
||||||
tracing::debug!("Bitcoin tx broadcasted! TXID = {}", txid);
|
tracing::debug!("Bitcoin tx broadcasted! TXID = {}", txid);
|
||||||
Ok(txid)
|
Ok(txid)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: For retry, use `backoff::ExponentialBackoff` in production as opposed
|
// TODO: For retry, use `backoff::ExponentialBackoff` in production as opposed
|
||||||
|
|
|
@ -3,11 +3,10 @@ use crate::{
|
||||||
state,
|
state,
|
||||||
state::{Bob, Swap},
|
state::{Bob, Swap},
|
||||||
storage::Database,
|
storage::Database,
|
||||||
SwapAmounts, TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE,
|
SwapAmounts,
|
||||||
};
|
};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use async_recursion::async_recursion;
|
use async_recursion::async_recursion;
|
||||||
use bitcoin_harness::bitcoind_rpc::jsonrpc_client::JsonRpcError;
|
|
||||||
use libp2p::{core::Multiaddr, PeerId};
|
use libp2p::{core::Multiaddr, PeerId};
|
||||||
use rand::{CryptoRng, RngCore};
|
use rand::{CryptoRng, RngCore};
|
||||||
use std::{convert::TryFrom, fmt, sync::Arc};
|
use std::{convert::TryFrom, fmt, sync::Arc};
|
||||||
|
@ -345,18 +344,7 @@ where
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
BobState::T1Expired(state4) => {
|
BobState::T1Expired(state4) => {
|
||||||
let result = state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await;
|
state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;
|
||||||
if let Err(error) = result {
|
|
||||||
if let Some(json_rpc_err) = error.downcast_ref::<JsonRpcError>() {
|
|
||||||
if json_rpc_err.code == TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE {
|
|
||||||
info!("Failed to send cancel transaction, assuming that is was already included by the other party...");
|
|
||||||
} else {
|
|
||||||
return Err(error);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let state = BobState::Cancelled(state4);
|
let state = BobState::Cancelled(state4);
|
||||||
db.insert_latest_state(swap_id, state::Swap::Bob(state.clone().into()))
|
db.insert_latest_state(swap_id, state::Swap::Bob(state.clone().into()))
|
||||||
|
|
|
@ -15,8 +15,6 @@ pub mod trace;
|
||||||
|
|
||||||
pub type Never = std::convert::Infallible;
|
pub type Never = std::convert::Infallible;
|
||||||
|
|
||||||
const TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE: i64 = -27;
|
|
||||||
|
|
||||||
/// Commands sent from Bob to the main task.
|
/// Commands sent from Bob to the main task.
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub enum Cmd {
|
pub enum Cmd {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue