Skip to content

Commit

Permalink
Merge pull request #290 from mojoX911/last-unwraps
Browse files Browse the repository at this point in the history
Last unwraps
  • Loading branch information
Shourya742 authored Dec 3, 2024
2 parents f010232 + 64fd3ec commit e0a772a
Show file tree
Hide file tree
Showing 20 changed files with 367 additions and 394 deletions.
21 changes: 0 additions & 21 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
use std::error::Error;

use bitcoin::Amount;

use crate::protocol::error::ContractError;

/// Includes all network-related errors.
#[derive(Debug)]
pub enum NetError {
Expand Down Expand Up @@ -40,20 +36,3 @@ impl From<serde_cbor::Error> for NetError {
Self::Cbor(value)
}
}

/// Includes all Protocol-level errors.
#[derive(Debug)]
pub enum ProtocolError {
WrongMessage { expected: String, received: String },
WrongNumOfSigs { expected: usize, received: usize },
WrongNumOfContractTxs { expected: usize, received: usize },
WrongNumOfPrivkeys { expected: usize, received: usize },
IncorrectFundingAmount { expected: Amount, found: Amount },
Contract(ContractError),
}

impl From<ContractError> for ProtocolError {
fn from(value: ContractError) -> Self {
Self::Contract(value)
}
}
2 changes: 1 addition & 1 deletion src/maker/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl Maker {

//check that the provided contract matches the scriptpubkey from the
//cache which was populated when the ReqContractSigsForSender message arrived
let contract_spk = redeemscript_to_scriptpubkey(&funding_info.contract_redeemscript);
let contract_spk = redeemscript_to_scriptpubkey(&funding_info.contract_redeemscript)?;

if !self.wallet.read()?.does_prevout_match_cached_contract(
&(OutPoint {
Expand Down
18 changes: 4 additions & 14 deletions src/maker/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use std::sync::{MutexGuard, PoisonError, RwLockReadGuard, RwLockWriteGuard};

use bitcoin::secp256k1;

use crate::{
error::{NetError, ProtocolError},
protocol::error::ContractError,
wallet::WalletError,
};
use crate::{error::NetError, protocol::error::ProtocolError, wallet::WalletError};

use super::MakerBehavior;

Expand Down Expand Up @@ -62,9 +58,9 @@ impl From<secp256k1::Error> for MakerError {
}
}

impl From<ContractError> for MakerError {
fn from(value: ContractError) -> Self {
Self::Protocol(ProtocolError::from(value))
impl From<ProtocolError> for MakerError {
fn from(value: ProtocolError) -> Self {
Self::Protocol(value)
}
}

Expand All @@ -85,9 +81,3 @@ impl From<NetError> for MakerError {
Self::Net(value)
}
}

impl From<ProtocolError> for MakerError {
fn from(value: ProtocolError) -> Self {
Self::Protocol(value)
}
}
25 changes: 14 additions & 11 deletions src/maker/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use bitcoin::{
use bitcoind::bitcoincore_rpc::RpcApi;

use crate::{
error::ProtocolError,
maker::api::recover_from_swap,
protocol::{
error::ProtocolError,
messages::{MakerHello, MultisigPrivkey, PrivKeyHandover},
Hash160,
},
Expand Down Expand Up @@ -301,7 +301,7 @@ impl Maker {
funding_output.value,
&funding_info.contract_redeemscript,
Amount::from_sat(message.next_fee_rate),
);
)?;

let (tweakable_privkey, _) = self.wallet.read()?.get_tweakable_keypair()?;
let multisig_privkey =
Expand Down Expand Up @@ -345,15 +345,18 @@ impl Maker {
}

// Calculate output amounts for the next hop
let incoming_amount = message.confirmed_funding_txes.iter().fold(0u64, |acc, fi| {
let index = find_funding_output_index(fi).unwrap();
let txout = fi
.funding_tx
.output
.get(index as usize)
.expect("output at index expected");
acc + txout.value.to_sat()
});
let incoming_amount = message
.confirmed_funding_txes
.iter()
.try_fold(0u64, |acc, fi| {
let index = find_funding_output_index(fi)?;
let txout = fi
.funding_tx
.output
.get(index as usize)
.expect("output at index expected");
Ok::<_, MakerError>(acc + txout.value.to_sat())
})?;

let calc_coinswap_fees = calculate_coinswap_fee(
self.config.absolute_fee_sats,
Expand Down
2 changes: 1 addition & 1 deletion src/maker/rpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn handle_request(maker: &Arc<Maker>, socket: &mut TcpStream) -> Result<(), Make
}
RpcMsgReq::GetTorAddress => {
let path = get_maker_dir().join("tor");
let resp = RpcMsgResp::GetTorAddressResp(get_tor_addrs(&path));
let resp = RpcMsgResp::GetTorAddressResp(get_tor_addrs(&path)?);
if let Err(e) = send_message(socket, &resp) {
log::info!("Error sending RPC response {:?}", e);
};
Expand Down
2 changes: 1 addition & 1 deletion src/market/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub fn start_directory_server(directory: Arc<DirectoryServer>) -> Result<(), Dir

log::info!("Directory tor is instantiated");

let onion_addr = get_tor_addrs(&PathBuf::from("/tmp/tor-rust-directory"));
let onion_addr = get_tor_addrs(&PathBuf::from("/tmp/tor-rust-directory"))?;

log::info!(
"Directory Server is listening at {}:{}",
Expand Down
Loading

0 comments on commit e0a772a

Please sign in to comment.