Skip to content

Commit

Permalink
enhancements in test section.
Browse files Browse the repository at this point in the history
 This involves:
 - Docmentation about fee workflow for every IT.
 - Refactor the IT's as almost IT's uses same code -> thus created some api's to reduce redundancy.
 - Fix some bugs in calculating coinswap fees.
  • Loading branch information
KnowWhoami committed Dec 30, 2024
1 parent 876680b commit 4a3c042
Show file tree
Hide file tree
Showing 22 changed files with 1,342 additions and 1,777 deletions.
2 changes: 1 addition & 1 deletion src/maker/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl Maker {
)
.map_err(WalletError::Rpc)?
{
if txout.confirmations < (REQUIRED_CONFIRMS as u32) {
if txout.confirmations < REQUIRED_CONFIRMS {
return Err(MakerError::General(
"funding tx not confirmed to required depth",
));
Expand Down
2 changes: 1 addition & 1 deletion src/maker/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ mod tests {
rpc_port = 6103
required_confirms = 1
min_contract_reaction_time = 48
min_swap_amount = 10000
min_swap_amount = 100000
socks_port = 19050
"#;
let config_path = create_temp_config(contents, "valid_maker_config.toml");
Expand Down
21 changes: 7 additions & 14 deletions src/maker/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use crate::{
contract::{
calculate_coinswap_fee, create_receivers_contract_tx, find_funding_output_index,
read_hashvalue_from_contract, read_pubkeys_from_multisig_redeemscript,
FUNDING_TX_VBYTE_SIZE,
},
error::ProtocolError,
messages::{
Expand Down Expand Up @@ -363,10 +362,12 @@ impl Maker {
TIME_RELATIVE_FEE_PCT,
);

let calc_funding_tx_fees = (FUNDING_TX_VBYTE_SIZE
* message.contract_feerate
* (message.next_coinswap_info.len() as u64))
/ 1000;
// NOTE: The `contract_feerate` currently represents the hardcoded `MINER_FEE` of a transaction, not the fee rate.
// This will remain unchanged to avoid modifying the structure of the [ProofOfFunding] message.
// Once issue https://github.com/citadel-tech/coinswap/issues/309 is resolved,
//`contract_feerate` will represent the actual fee rate instead of the `MINER_FEE`.
let calc_funding_tx_fees =
message.contract_feerate * (message.next_coinswap_info.len() as u64);

// Check for overflow. If happens hard error.
// This can happen if the fee_rate for funding tx is very high and incoming_amount is very low.
Expand Down Expand Up @@ -401,11 +402,6 @@ impl Maker {
)?
};

log::info!(
"cal coinswap fee ______________ : {:?}",
calc_coinswap_fees
);

let act_coinswap_fees = incoming_amount
.checked_sub(outgoing_amount + act_funding_txs_fees.to_sat())
.expect("This should not overflow as we just above.");
Expand All @@ -420,10 +416,7 @@ impl Maker {
);

log::info!(
"[{}] Incoming Swap Amount = {} | Outgoing Swap Amount = {} | Swap Revenue = {}
/n
Refund Tx locktime (blocks) = {} | Total Funding Tx Mining Fees = {} |
",
"[{}] Incoming Swap Amount = {} | Outgoing Swap Amount = {} | Coinswap Fee = {} | Refund Tx locktime (blocks) = {} | Total Funding Tx Mining Fees = {} |",
self.config.port,
Amount::from_sat(incoming_amount),
Amount::from_sat(outgoing_amount),
Expand Down
2 changes: 1 addition & 1 deletion src/market/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
market::rpc::start_rpc_server_thread,
utill::{
get_dns_dir, parse_field, parse_toml, read_message, send_message, verify_fidelity_checks,
ConnectionType, HEART_BEAT_INTERVAL, DnsRequest,
ConnectionType, DnsRequest, HEART_BEAT_INTERVAL,
},
wallet::{RPCConfig, WalletError},
};
Expand Down
3 changes: 1 addition & 2 deletions src/market/rpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{RpcMsgReq, RpcMsgResp};
use crate::{
error::NetError,
market::directory::{AddressEntry, DirectoryServer, DirectoryServerError},
utill::{read_message, send_message},
utill::{read_message, send_message, HEART_BEAT_INTERVAL},
};
use std::{
collections::BTreeSet,
Expand All @@ -12,7 +12,6 @@ use std::{
thread::sleep,
time::Duration,
};
use crate::utill::HEART_BEAT_INTERVAL;
fn handle_request(
socket: &mut TcpStream,
address: Arc<RwLock<BTreeSet<AddressEntry>>>,
Expand Down
14 changes: 8 additions & 6 deletions src/taker/routines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
use serde::{Deserialize, Serialize};
#[cfg(feature = "tor")]
use socks::Socks5Stream;
use std::{io::ErrorKind, net::TcpStream, thread::sleep, time::Duration, u64::MIN};
use std::{io::ErrorKind, net::TcpStream, thread::sleep, time::Duration};

use crate::{
protocol::{
contract::{
calculate_coinswap_fee, create_contract_redeemscript, find_funding_output_index,
validate_contract_tx, FUNDING_TX_VBYTE_SIZE,
validate_contract_tx,
},
error::ProtocolError,
messages::{
Expand Down Expand Up @@ -346,20 +346,22 @@ pub(crate) fn send_proof_of_funding_and_init_next_hop(
tmi.this_maker.offer.time_relative_fee_pct,
);

let miner_fees_paid_by_taker =
(FUNDING_TX_VBYTE_SIZE * MINER_FEE * (npi.next_peer_multisig_pubkeys.len() as u64)) / 1000;
let miner_fees_paid_by_taker = (tmi.funding_tx_infos.len() as u64) * MINER_FEE;
let calculated_next_amount = this_amount - coinswap_fees - miner_fees_paid_by_taker;

if Amount::from_sat(calculated_next_amount) != next_amount {
return Err((ProtocolError::IncorrectFundingAmount {
expected: Amount::from_sat(calculated_next_amount),
found: next_amount,
})
.into());
}

log::info!(
"This Maker is forwarding = {} to next Maker | Next maker's fees = {} | Miner fees covered by us = {}",
"Maker Received ={} | Maker is Forwarding = {} | Coinswap Fees = {} | Miner Fees paid by us={} ",
Amount::from_sat(this_amount),
next_amount,
coinswap_fees, // These are not in Amount..
Amount::from_sat(coinswap_fees),
miner_fees_paid_by_taker,
);

Expand Down
1 change: 1 addition & 0 deletions src/utill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub fn get_tor_addrs(hs_dir: &Path) -> io::Result<String> {
let mut hostname_file = File::open(hostname_file_path).unwrap();
let mut tor_addrs: String = String::new();
hostname_file.read_to_string(&mut tor_addrs)?;
tor_addrs.pop(); // Remove `\n` at the end.

Check warning on line 97 in src/utill.rs

View check run for this annotation

Codecov / codecov/patch

src/utill.rs#L97

Added line #L97 was not covered by tests
Ok(tor_addrs)
}

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::utill::HEART_BEAT_INTERVAL;
use bitcoin::Network;
use bitcoind::bitcoincore_rpc::{Auth, Client, RpcApi};
use serde_json::{json, Value};
use std::{convert::TryFrom, thread, time::Duration};
use std::{convert::TryFrom, thread};

use crate::wallet::api::KeychainKind;

Expand Down
Loading

0 comments on commit 4a3c042

Please sign in to comment.