Skip to content

Commit

Permalink
Merge pull request citadel-tech#345 from mojoX911/log-and-other-fixes
Browse files Browse the repository at this point in the history
Make the DNS send ALL addresses
  • Loading branch information
Shourya742 authored Dec 27, 2024
2 parents 1bc3e5d + bcbd223 commit 67c39a9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/bin/taker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ fn main() -> Result<(), TakerError> {
}

Commands::SyncOfferBook => {
taker.sync_offerbook(args.maker_count)?;
taker.sync_offerbook()?;
}
Commands::DoCoinswap => {
taker.do_coinswap(swap_params)?;
Expand Down
24 changes: 10 additions & 14 deletions src/market/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ fn handle_client(
let dns_request: DnsRequest = serde_cbor::de::from_reader(&buf[..])?;
match dns_request {
DnsRequest::Post { metadata } => {
log::info!("Received new maker address: {}", &metadata.url);
log::info!("Received POST | From {}", &metadata.url);

let txid = metadata.proof.bond.outpoint.txid;
let transaction = rpc.get_raw_transaction(&txid, None)?;
Expand Down Expand Up @@ -422,22 +422,18 @@ fn handle_client(
}
}
}
DnsRequest::Get { makers } => {
log::info!("Taker pinged the directory server");
log::info!("Number of makers requested: {:?}", makers);
DnsRequest::Get => {
log::info!("Received GET | From {}", stream.peer_addr()?);
let addresses = directory.addresses.read()?;
let response = if addresses.len() < makers as usize {
String::new()
} else {
addresses
.iter()
.take(makers as usize)
.fold(String::new(), |acc, AddressEntry(_, addr)| {
acc + addr + "\n"
})
};
let response = addresses
.iter()
.fold(String::new(), |acc, AddressEntry(_, addr)| {
acc + addr + "\n"
});
log::debug!("Sending Addresses: {}", response);
send_message(stream, &response)?;
}
#[cfg(feature = "integration-test")]
DnsRequest::Dummy { url } => {
log::info!("Got new maker address: {}", &url);
directory.addresses.write()?.insert(AddressEntry(0, url));
Expand Down
34 changes: 22 additions & 12 deletions src/taker/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,13 @@ impl Taker {
/// If that fails too. Open an issue at [our github](https://github.com/citadel-tech/coinswap/issues)
pub fn send_coinswap(&mut self, swap_params: SwapParams) -> Result<(), TakerError> {
log::info!("Syncing Offerbook");
self.sync_offerbook(swap_params.maker_count)?;
self.sync_offerbook()?;

// Error early if hop_count > available good makers.
if swap_params.maker_count > self.offerbook.all_makers.len() {
return Err(TakerError::NotEnoughMakersInOfferBook);
}

// Generate new random preimage and initiate the first hop.
let mut preimage = [0u8; 32];
OsRng.fill_bytes(&mut preimage);
Expand Down Expand Up @@ -1920,7 +1926,7 @@ impl Taker {
}

/// Synchronizes the offer book with addresses obtained from directory servers and local configurations.
pub fn sync_offerbook(&mut self, maker_count: usize) -> Result<(), TakerError> {
pub fn sync_offerbook(&mut self) -> Result<(), TakerError> {
let directory_address = match self.config.connection_type {
ConnectionType::CLEARNET => {
let mut address = self.config.directory_server_address.clone();
Expand Down Expand Up @@ -1952,18 +1958,21 @@ impl Taker {
socks_port = Some(self.config.socks_port);
}
}
let addresses_from_dns = fetch_addresses_from_dns(
socks_port,
directory_address,
maker_count,
self.config.connection_type,
)?;
let offers = fetch_offer_from_makers(addresses_from_dns, &self.config)?;
let addresses_from_dns =
fetch_addresses_from_dns(socks_port, directory_address, self.config.connection_type)?;

let new_offers = offers
// Filter for new addresses only.
let know_addrs = self
.offerbook
.all_makers
.iter()
.map(|oa| oa.address.clone())
.collect::<Vec<_>>();
let new_addrs = addresses_from_dns
.into_iter()
.filter(|offer| !self.offerbook.bad_makers.contains(offer))
.filter(|addr| !know_addrs.contains(addr))
.collect::<Vec<_>>();
let new_offers = fetch_offer_from_makers(new_addrs, &self.config)?;

for offer in new_offers {
log::info!(
Expand All @@ -1976,10 +1985,11 @@ impl Taker {
.verify_fidelity_proof(&offer.offer.fidelity, &offer.address.to_string())
{
log::warn!(
"Fidelity Proof Verification failed with error: {:?}. Rejecting Offer from Maker : {}",
"Fidelity Proof Verification failed with error: {:?}. Adding this to bad maker list : {}",
e,
offer.address.to_string()
);
self.offerbook.add_bad_maker(&offer);
} else {
log::info!("Fideity Bond verification succes. Adding offer to our OfferBook");
self.offerbook.add_new_offer(&offer);
Expand Down
24 changes: 4 additions & 20 deletions src/taker/offers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,8 @@ pub fn fetch_offer_from_makers(
thread.thread().name().expect("thread names expected")
);
let join_result = thread.join();
if let Ok(r) = join_result {
log::info!("Thread closing result: {:?}", r)
} else if let Err(e) = join_result {
log::info!("Error in internal thread: {:?}", e);
if let Err(e) = join_result {
log::error!("Error in internal thread: {:?}", e);
}
}
Ok(result)
Expand All @@ -178,7 +176,6 @@ pub fn fetch_offer_from_makers(
pub fn fetch_addresses_from_dns(
socks_port: Option<u16>,
directory_server_address: String,
number_of_makers: usize,
connection_type: ConnectionType,
) -> Result<Vec<MakerAddress>, TakerError> {
// TODO: Make the communication in serde_encoded bytes.
Expand All @@ -199,10 +196,7 @@ pub fn fetch_addresses_from_dns(
stream.flush()?;

// Change datatype of number of makers to u32 from usize
let request = DnsRequest::Get {
makers: number_of_makers as u32,
};
if let Err(e) = send_message(&mut stream, &request) {
if let Err(e) = send_message(&mut stream, &DnsRequest::Get) {
log::warn!("Failed to send request. Retrying...{}", e);
thread::sleep(GLOBAL_PAUSE);
continue;
Expand All @@ -225,17 +219,7 @@ pub fn fetch_addresses_from_dns(
.collect::<Result<Vec<MakerAddress>, _>>()
{
Ok(addresses) => {
if addresses.len() < number_of_makers {
log::info!(
"Insufficient addresses received. Need: {}, Got: {}. Retrying...",
number_of_makers,
addresses.len()
);
thread::sleep(GLOBAL_PAUSE);
continue;
} else {
return Ok(addresses);
}
return Ok(addresses);
}
Err(e) => {
log::error!("Error decoding DNS response: {:?}. Retrying...", e);
Expand Down
11 changes: 8 additions & 3 deletions src/utill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,14 @@ pub struct DnsMetadata {
// Structured requests and responses using serde.
#[derive(Serialize, Deserialize, Debug)]
pub enum DnsRequest {
Post { metadata: Box<DnsMetadata> },
Get { makers: u32 },
Dummy { url: String },
Post {
metadata: Box<DnsMetadata>,
},
Get,
#[cfg(feature = "integration-test")]
Dummy {
url: String,
},
}

pub fn verify_fidelity_checks(
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ impl Wallet {
// Generate Master key
let master_key = {
let mnemonic = Mnemonic::generate(12)?;
log::info!("Mnemonics: \n {:?}", mnemonic);
let words = mnemonic.words().collect::<Vec<_>>();
log::info!("Backup the Wallet Mnemonics. \n {:?}", words);
let seed = mnemonic.to_entropy();
Xpriv::new_master(rpc_config.network, &seed)?
};
Expand Down

0 comments on commit 67c39a9

Please sign in to comment.