mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-25 07:29:32 -05:00
Make sure all error messages start with an uppercase letter
These might potentially be shown to a user, let's make them all consistent.
This commit is contained in:
parent
816e8b9b96
commit
4138039ea0
@ -77,7 +77,7 @@ pub fn read_config(config_path: PathBuf) -> Result<Result<Config, ConfigNotIniti
|
|||||||
}
|
}
|
||||||
|
|
||||||
let file = Config::read(&config_path)
|
let file = Config::read(&config_path)
|
||||||
.with_context(|| format!("failed to read config file {}", config_path.display()))?;
|
.with_context(|| format!("Failed to read config file at {}", config_path.display()))?;
|
||||||
|
|
||||||
Ok(Ok(file))
|
Ok(Ok(file))
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::{bitcoin, monero};
|
use crate::{bitcoin, monero};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{Context, Result};
|
||||||
use rust_decimal::prelude::ToPrimitive;
|
use rust_decimal::prelude::ToPrimitive;
|
||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
use std::fmt::{Debug, Display, Formatter};
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
@ -30,20 +30,20 @@ impl Rate {
|
|||||||
let quote_in_sats = quote.as_sat();
|
let quote_in_sats = quote.as_sat();
|
||||||
let quote_in_btc = Decimal::from(quote_in_sats)
|
let quote_in_btc = Decimal::from(quote_in_sats)
|
||||||
.checked_div(Decimal::from(bitcoin::Amount::ONE_BTC.as_sat()))
|
.checked_div(Decimal::from(bitcoin::Amount::ONE_BTC.as_sat()))
|
||||||
.ok_or_else(|| anyhow!("division overflow"))?;
|
.context("Division overflow")?;
|
||||||
|
|
||||||
let rate_in_btc = Decimal::from(rate.as_sat())
|
let rate_in_btc = Decimal::from(rate.as_sat())
|
||||||
.checked_div(Decimal::from(bitcoin::Amount::ONE_BTC.as_sat()))
|
.checked_div(Decimal::from(bitcoin::Amount::ONE_BTC.as_sat()))
|
||||||
.ok_or_else(|| anyhow!("division overflow"))?;
|
.context("Division overflow")?;
|
||||||
|
|
||||||
let base_in_xmr = quote_in_btc
|
let base_in_xmr = quote_in_btc
|
||||||
.checked_div(rate_in_btc)
|
.checked_div(rate_in_btc)
|
||||||
.ok_or_else(|| anyhow!("division overflow"))?;
|
.context("Division overflow")?;
|
||||||
let base_in_piconero = base_in_xmr * Decimal::from(monero::Amount::ONE_XMR.as_piconero());
|
let base_in_piconero = base_in_xmr * Decimal::from(monero::Amount::ONE_XMR.as_piconero());
|
||||||
|
|
||||||
let base_in_piconero = base_in_piconero
|
let base_in_piconero = base_in_piconero
|
||||||
.to_u64()
|
.to_u64()
|
||||||
.ok_or_else(|| anyhow!("decimal cannot be represented as u64"))?;
|
.context("Failed to fit piconero amount into a u64")?;
|
||||||
|
|
||||||
Ok(monero::Amount::from_piconero(base_in_piconero))
|
Ok(monero::Amount::from_piconero(base_in_piconero))
|
||||||
}
|
}
|
||||||
|
@ -285,7 +285,7 @@ async fn init_monero_wallet(
|
|||||||
let _test_wallet_connection = monero_wallet
|
let _test_wallet_connection = monero_wallet
|
||||||
.block_height()
|
.block_height()
|
||||||
.await
|
.await
|
||||||
.context("failed to validate connection to monero-wallet-rpc")?;
|
.context("Failed to validate connection to monero-wallet-rpc")?;
|
||||||
|
|
||||||
Ok((monero_wallet, monero_wallet_rpc_process))
|
Ok((monero_wallet, monero_wallet_rpc_process))
|
||||||
}
|
}
|
||||||
@ -299,7 +299,7 @@ async fn determine_btc_to_swap(
|
|||||||
) -> Result<bitcoin::Amount> {
|
) -> Result<bitcoin::Amount> {
|
||||||
debug!("Requesting quote");
|
debug!("Requesting quote");
|
||||||
|
|
||||||
let bid_quote = request_quote.await.context("failed to request quote")?;
|
let bid_quote = request_quote.await.context("Failed to request quote")?;
|
||||||
|
|
||||||
info!("Received quote: 1 XMR ~ {}", bid_quote.price);
|
info!("Received quote: 1 XMR ~ {}", bid_quote.price);
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ pub use wallet::Wallet;
|
|||||||
use ::bitcoin::hashes::hex::ToHex;
|
use ::bitcoin::hashes::hex::ToHex;
|
||||||
use ::bitcoin::hashes::Hash;
|
use ::bitcoin::hashes::Hash;
|
||||||
use ::bitcoin::{secp256k1, SigHash};
|
use ::bitcoin::{secp256k1, SigHash};
|
||||||
use anyhow::{anyhow, bail, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use ecdsa_fun::adaptor::{Adaptor, HashTranscript};
|
use ecdsa_fun::adaptor::{Adaptor, HashTranscript};
|
||||||
use ecdsa_fun::fun::Point;
|
use ecdsa_fun::fun::Point;
|
||||||
use ecdsa_fun::nonce::Deterministic;
|
use ecdsa_fun::nonce::Deterministic;
|
||||||
@ -204,7 +204,7 @@ pub fn recover(S: PublicKey, sig: Signature, encsig: EncryptedSignature) -> Resu
|
|||||||
let s = adaptor
|
let s = adaptor
|
||||||
.recover_decryption_key(&S.0, &sig, &encsig)
|
.recover_decryption_key(&S.0, &sig, &encsig)
|
||||||
.map(SecretKey::from)
|
.map(SecretKey::from)
|
||||||
.ok_or_else(|| anyhow!("secret recovery failure"))?;
|
.context("Failed to recover secret from adaptor signature")?;
|
||||||
|
|
||||||
Ok(s)
|
Ok(s)
|
||||||
}
|
}
|
||||||
|
@ -107,7 +107,7 @@ impl TxRedeem {
|
|||||||
let sig = sigs
|
let sig = sigs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|sig| verify_sig(&B, &self.digest(), &sig).is_ok())
|
.find(|sig| verify_sig(&B, &self.digest(), &sig).is_ok())
|
||||||
.context("neither signature on witness stack verifies against B")?;
|
.context("Neither signature on witness stack verifies against B")?;
|
||||||
|
|
||||||
Ok(sig)
|
Ok(sig)
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ impl TxRefund {
|
|||||||
let sig = sigs
|
let sig = sigs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|sig| verify_sig(&B, &self.digest(), &sig).is_ok())
|
.find(|sig| verify_sig(&B, &self.digest(), &sig).is_ok())
|
||||||
.context("neither signature on witness stack verifies against B")?;
|
.context("Neither signature on witness stack verifies against B")?;
|
||||||
|
|
||||||
Ok(sig)
|
Ok(sig)
|
||||||
}
|
}
|
||||||
|
@ -74,13 +74,23 @@ impl Wallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn balance(&self) -> Result<Amount> {
|
pub async fn balance(&self) -> Result<Amount> {
|
||||||
let balance = self.inner.lock().await.get_balance().context("Failed to calculate Bitcoin balance")?;
|
let balance = self
|
||||||
|
.inner
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get_balance()
|
||||||
|
.context("Failed to calculate Bitcoin balance")?;
|
||||||
|
|
||||||
Ok(Amount::from_sat(balance))
|
Ok(Amount::from_sat(balance))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn new_address(&self) -> Result<Address> {
|
pub async fn new_address(&self) -> Result<Address> {
|
||||||
let address = self.inner.lock().await.get_new_address().context("Failed to get new Bitcoin address")?;
|
let address = self
|
||||||
|
.inner
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get_new_address()
|
||||||
|
.context("Failed to get new Bitcoin address")?;
|
||||||
|
|
||||||
Ok(address)
|
Ok(address)
|
||||||
}
|
}
|
||||||
@ -111,7 +121,7 @@ impl Wallet {
|
|||||||
.lock()
|
.lock()
|
||||||
.await
|
.await
|
||||||
.sync(noop_progress(), None)
|
.sync(noop_progress(), None)
|
||||||
.context("failed to sync balance of Bitcoin wallet")?;
|
.context("Failed to sync balance of Bitcoin wallet")?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -166,7 +176,7 @@ impl Wallet {
|
|||||||
.await
|
.await
|
||||||
.broadcast(transaction)
|
.broadcast(transaction)
|
||||||
.with_context(|| {
|
.with_context(|| {
|
||||||
format!("failed to broadcast Bitcoin {} transaction {}", kind, txid)
|
format!("Failed to broadcast Bitcoin {} transaction {}", kind, txid)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
tracing::info!("Published Bitcoin {} transaction as {}", txid, kind);
|
tracing::info!("Published Bitcoin {} transaction as {}", txid, kind);
|
||||||
@ -209,7 +219,7 @@ impl Wallet {
|
|||||||
Result::<_, backoff::Error<Error>>::Ok(tx)
|
Result::<_, backoff::Error<Error>>::Ok(tx)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.context("transient errors to be retried")?;
|
.context("Transient errors should be retried")?;
|
||||||
|
|
||||||
Ok(tx)
|
Ok(tx)
|
||||||
}
|
}
|
||||||
@ -230,7 +240,7 @@ impl Wallet {
|
|||||||
Result::<_, backoff::Error<Error>>::Ok(height)
|
Result::<_, backoff::Error<Error>>::Ok(height)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.context("transient errors to be retried")?;
|
.context("Transient errors should be retried")?;
|
||||||
|
|
||||||
Ok(BlockHeight::new(height))
|
Ok(BlockHeight::new(height))
|
||||||
}
|
}
|
||||||
@ -261,7 +271,7 @@ impl Wallet {
|
|||||||
Result::<_, backoff::Error<Error>>::Ok(block_height)
|
Result::<_, backoff::Error<Error>>::Ok(block_height)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.context("transient errors to be retried")?;
|
.context("Transient errors should be retried")?;
|
||||||
|
|
||||||
Ok(BlockHeight::new(height))
|
Ok(BlockHeight::new(height))
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ pub fn read_config(config_path: PathBuf) -> Result<Result<Config, ConfigNotIniti
|
|||||||
}
|
}
|
||||||
|
|
||||||
let file = Config::read(&config_path)
|
let file = Config::read(&config_path)
|
||||||
.with_context(|| format!("failed to read config file {}", config_path.display()))?;
|
.with_context(|| format!("Failed to read config file at {}", config_path.display()))?;
|
||||||
|
|
||||||
Ok(Ok(file))
|
Ok(Ok(file))
|
||||||
}
|
}
|
||||||
|
@ -96,15 +96,15 @@ impl Database {
|
|||||||
.map(|item| match item {
|
.map(|item| match item {
|
||||||
Ok((key, value)) => {
|
Ok((key, value)) => {
|
||||||
let swap_id = deserialize::<Uuid>(&key);
|
let swap_id = deserialize::<Uuid>(&key);
|
||||||
let swap = deserialize::<Swap>(&value).context("failed to deserialize swap");
|
let swap = deserialize::<Swap>(&value).context("Failed to deserialize swap");
|
||||||
|
|
||||||
match (swap_id, swap) {
|
match (swap_id, swap) {
|
||||||
(Ok(swap_id), Ok(swap)) => Ok((swap_id, swap)),
|
(Ok(swap_id), Ok(swap)) => Ok((swap_id, swap)),
|
||||||
(Ok(_), Err(err)) => Err(err),
|
(Ok(_), Err(err)) => Err(err),
|
||||||
_ => bail!("failed to deserialize swap"),
|
_ => bail!("Failed to deserialize swap"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => Err(err).context("failed to retrieve swap from DB"),
|
Err(err) => Err(err).context("Failed to retrieve swap from DB"),
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
@ -80,7 +80,7 @@ impl WalletRpc {
|
|||||||
|
|
||||||
let content_length = response.headers()[CONTENT_LENGTH]
|
let content_length = response.headers()[CONTENT_LENGTH]
|
||||||
.to_str()
|
.to_str()
|
||||||
.context("failed to convert content-length to string")?
|
.context("Failed to convert content-length to string")?
|
||||||
.parse::<u64>()?;
|
.parse::<u64>()?;
|
||||||
|
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
|
@ -184,7 +184,7 @@ impl Behaviour {
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
self.quote
|
self.quote
|
||||||
.send_response(channel, response)
|
.send_response(channel, response)
|
||||||
.map_err(|_| anyhow!("failed to respond with quote"))?;
|
.map_err(|_| anyhow!("Failed to respond with quote"))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -196,7 +196,7 @@ impl Behaviour {
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
self.spot_price
|
self.spot_price
|
||||||
.send_response(channel, response)
|
.send_response(channel, response)
|
||||||
.map_err(|_| anyhow!("failed to respond with spot price"))?;
|
.map_err(|_| anyhow!("Failed to respond with spot price"))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -65,31 +65,31 @@ impl Behaviour {
|
|||||||
.do_protocol_listener(bob, move |mut substream| async move {
|
.do_protocol_listener(bob, move |mut substream| async move {
|
||||||
let message0 =
|
let message0 =
|
||||||
serde_cbor::from_slice::<Message0>(&substream.read_message(BUF_SIZE).await?)
|
serde_cbor::from_slice::<Message0>(&substream.read_message(BUF_SIZE).await?)
|
||||||
.context("failed to deserialize message0")?;
|
.context("Failed to deserialize message0")?;
|
||||||
let state1 = state0.receive(message0)?;
|
let state1 = state0.receive(message0)?;
|
||||||
|
|
||||||
substream
|
substream
|
||||||
.write_message(
|
.write_message(
|
||||||
&serde_cbor::to_vec(&state1.next_message())
|
&serde_cbor::to_vec(&state1.next_message())
|
||||||
.context("failed to serialize message1")?,
|
.context("Failed to serialize message1")?,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let message2 =
|
let message2 =
|
||||||
serde_cbor::from_slice::<Message2>(&substream.read_message(BUF_SIZE).await?)
|
serde_cbor::from_slice::<Message2>(&substream.read_message(BUF_SIZE).await?)
|
||||||
.context("failed to deserialize message2")?;
|
.context("Failed to deserialize message2")?;
|
||||||
let state2 = state1.receive(message2);
|
let state2 = state1.receive(message2);
|
||||||
|
|
||||||
substream
|
substream
|
||||||
.write_message(
|
.write_message(
|
||||||
&serde_cbor::to_vec(&state2.next_message())
|
&serde_cbor::to_vec(&state2.next_message())
|
||||||
.context("failed to serialize message3")?,
|
.context("Failed to serialize message3")?,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let message4 =
|
let message4 =
|
||||||
serde_cbor::from_slice::<Message4>(&substream.read_message(BUF_SIZE).await?)
|
serde_cbor::from_slice::<Message4>(&substream.read_message(BUF_SIZE).await?)
|
||||||
.context("failed to deserialize message4")?;
|
.context("Failed to deserialize message4")?;
|
||||||
let state3 = state2.receive(message4)?;
|
let state3 = state2.receive(message4)?;
|
||||||
|
|
||||||
Ok((bob, state3))
|
Ok((bob, state3))
|
||||||
|
@ -106,7 +106,7 @@ pub fn build_bitcoin_redeem_transaction(
|
|||||||
|
|
||||||
let tx = tx_redeem
|
let tx = tx_redeem
|
||||||
.add_signatures((a.public(), sig_a), (B, sig_b))
|
.add_signatures((a.public(), sig_a), (B, sig_b))
|
||||||
.context("sig_{a,b} are invalid for tx_redeem")?;
|
.context("Failed to sign Bitcoin redeem transaction")?;
|
||||||
|
|
||||||
Ok(tx)
|
Ok(tx)
|
||||||
}
|
}
|
||||||
|
@ -236,7 +236,7 @@ impl EventLoop {
|
|||||||
let _ = self.conn_established.send(peer_id).await;
|
let _ = self.conn_established.send(peer_id).await;
|
||||||
} else {
|
} else {
|
||||||
debug!("Dialing alice at {}", peer_id);
|
debug!("Dialing alice at {}", peer_id);
|
||||||
libp2p::Swarm::dial(&mut self.swarm, &peer_id).context("failed to dial alice")?;
|
libp2p::Swarm::dial(&mut self.swarm, &peer_id).context("Failed to dial alice")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -73,31 +73,31 @@ impl Behaviour {
|
|||||||
substream
|
substream
|
||||||
.write_message(
|
.write_message(
|
||||||
&serde_cbor::to_vec(&state0.next_message())
|
&serde_cbor::to_vec(&state0.next_message())
|
||||||
.context("failed to serialize message0")?,
|
.context("Failed to serialize message0")?,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let message1 =
|
let message1 =
|
||||||
serde_cbor::from_slice::<Message1>(&substream.read_message(BUF_SIZE).await?)
|
serde_cbor::from_slice::<Message1>(&substream.read_message(BUF_SIZE).await?)
|
||||||
.context("failed to deserialize message1")?;
|
.context("Failed to deserialize message1")?;
|
||||||
let state1 = state0.receive(bitcoin_wallet.as_ref(), message1).await?;
|
let state1 = state0.receive(bitcoin_wallet.as_ref(), message1).await?;
|
||||||
|
|
||||||
substream
|
substream
|
||||||
.write_message(
|
.write_message(
|
||||||
&serde_cbor::to_vec(&state1.next_message())
|
&serde_cbor::to_vec(&state1.next_message())
|
||||||
.context("failed to serialize message2")?,
|
.context("Failed to serialize message2")?,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let message3 =
|
let message3 =
|
||||||
serde_cbor::from_slice::<Message3>(&substream.read_message(BUF_SIZE).await?)
|
serde_cbor::from_slice::<Message3>(&substream.read_message(BUF_SIZE).await?)
|
||||||
.context("failed to deserialize message3")?;
|
.context("Failed to deserialize message3")?;
|
||||||
let state2 = state1.receive(message3)?;
|
let state2 = state1.receive(message3)?;
|
||||||
|
|
||||||
substream
|
substream
|
||||||
.write_message(
|
.write_message(
|
||||||
&serde_cbor::to_vec(&state2.next_message())
|
&serde_cbor::to_vec(&state2.next_message())
|
||||||
.context("failed to serialize message4")?,
|
.context("Failed to serialize message4")?,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -266,7 +266,7 @@ impl State2 {
|
|||||||
let signed_tx = bitcoin_wallet
|
let signed_tx = bitcoin_wallet
|
||||||
.sign_and_finalize(self.tx_lock.clone().into())
|
.sign_and_finalize(self.tx_lock.clone().into())
|
||||||
.await
|
.await
|
||||||
.context("failed to sign Bitcoin lock transaction")?;
|
.context("Failed to sign Bitcoin lock transaction")?;
|
||||||
|
|
||||||
let _ = bitcoin_wallet.broadcast(signed_tx, "lock").await?;
|
let _ = bitcoin_wallet.broadcast(signed_tx, "lock").await?;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user