From 5d8c702125fb345bca6fe336415fd6a1cb1c9a4e Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Thu, 2 Jan 2025 21:59:45 +0530 Subject: [PATCH] master build fix and few more docs --- src/lib.rs | 2 +- src/market/directory.rs | 12 ++++++++++++ src/market/rpc/messages.rs | 2 +- src/market/rpc/server.rs | 2 +- src/protocol/messages.rs | 17 ++++++++++++++--- src/protocol/mod.rs | 2 ++ src/utill.rs | 1 + tests/dns.rs | 4 ++-- 8 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 908363d8..492a05f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ extern crate bitcoind; pub mod error; pub mod maker; pub mod market; -pub(crate) mod protocol; +pub mod protocol; pub mod taker; #[cfg(feature = "tor")] pub mod tor; diff --git a/src/market/directory.rs b/src/market/directory.rs index d31a5267..065cf7c9 100644 --- a/src/market/directory.rs +++ b/src/market/directory.rs @@ -60,6 +60,7 @@ pub enum DirectoryServerError { /// /// This variant wraps a [`WalletError`] to capture issues arising during wallet-related operations. Wallet(WalletError), + /// Represents an error caused by a corrupted address file read. AddressFileCorrupted(String), } @@ -126,6 +127,7 @@ pub struct DirectoryServer { pub data_dir: PathBuf, /// Shutdown flag to stop the directory server pub shutdown: AtomicBool, + /// A collection of maker addresses received from the Dns Server. pub addresses: Arc>>, } @@ -329,6 +331,16 @@ pub fn read_addresses_from_file( .collect::, DirectoryServerError>>() } +/// Initializes and starts the Directory Server with the provided configuration. +/// +/// This function configures the Directory Server based on the specified `directory` and optional `rpc_config`. +/// It handles both Clearnet and Tor connections (if the `tor` feature is enabled) and performs the following tasks: +/// +/// - Sets up the Directory Server for the appropriate connection type. +/// - Spawns threads for handling RPC requests and writing address data to disk. +/// - Monitors and manages incoming TCP connections. +/// - Handles shutdown signals gracefully, ensuring all threads are terminated and resources are cleaned up. +/// pub fn start_directory_server( directory: Arc, rpc_config: Option, diff --git a/src/market/rpc/messages.rs b/src/market/rpc/messages.rs index 91c9fbc6..1227120a 100644 --- a/src/market/rpc/messages.rs +++ b/src/market/rpc/messages.rs @@ -13,5 +13,5 @@ pub enum RpcMsgReq { #[derive(Serialize, Deserialize, Debug)] pub enum RpcMsgResp { /// ListAddressesResp RPC message response variant - ListAddressesResp(BTreeSet), + ListAddressesResp(BTreeSet<(OutPoint, String)>), } diff --git a/src/market/rpc/server.rs b/src/market/rpc/server.rs index 785f224b..1bc3e11d 100644 --- a/src/market/rpc/server.rs +++ b/src/market/rpc/server.rs @@ -40,7 +40,7 @@ fn handle_request( Ok(()) } -pub(crate) fn start_rpc_server_thread( +pub fn start_rpc_server_thread( directory: Arc, ) -> Result<(), DirectoryServerError> { let rpc_port = directory.rpc_port; diff --git a/src/protocol/messages.rs b/src/protocol/messages.rs index d271dece..78af7850 100644 --- a/src/protocol/messages.rs +++ b/src/protocol/messages.rs @@ -229,7 +229,7 @@ pub(crate) struct MakerHello { /// Contains proof data related to fidelity bond. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] -pub(crate) struct FidelityProof { +pub struct FidelityProof { pub(crate) bond: FidelityBond, pub(crate) cert_hash: Hash, pub(crate) cert_sig: bitcoin::secp256k1::ecdsa::Signature, @@ -316,23 +316,34 @@ impl Display for MakerToTakerMessage { } } +/// Metadata shared by the maker with the Directory Server for verifying authenticity. #[derive(Serialize, Deserialize, Debug)] pub struct DnsMetadata { + /// The maker's URL. pub url: String, + /// Proof of the maker's fidelity bond funding. pub proof: FidelityProof, } -// Structured requests and responses using serde. +/// Enum representing DNS request message types. +/// +/// These requests and responses are structured using Serde for serialization and deserialization. #[derive(Serialize, Deserialize, Debug)] #[allow(clippy::large_enum_variant)] pub enum DnsRequest { + /// A request sent by the maker to register itself with the DNS server and authenticate. Post { + /// Metadata containing the maker's URL and fidelity proof. metadata: DnsMetadata, }, + /// A request sent by the taker to fetch all valid maker addresses from the DNS server. Get, + /// Dummy data used for integration tests. #[cfg(feature = "integration-test")] Dummy { + /// A dummy URL for testing. url: String, - vout: u32, // represents a specific vout value of the OutPoint(deadbeefcafebabefeedc0ffee123456789abcdeffedcba9876543210ffeeddcc:vout) + /// A dummy `vout` value, representing a specific output index of an OutPoint. + vout: u32, }, } diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 1edc20e5..17a84438 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -5,3 +5,5 @@ pub mod error; pub(crate) mod messages; pub(crate) use contract::Hash160; + +pub use messages::{DnsMetadata, DnsRequest}; diff --git a/src/utill.rs b/src/utill.rs index dd03bf32..15bba671 100644 --- a/src/utill.rs +++ b/src/utill.rs @@ -13,6 +13,7 @@ use log4rs::{ config::{Appender, Logger, Root}, Config, }; +use serde::{Deserialize, Serialize}; use std::{ env, fmt, io::{BufReader, BufWriter, ErrorKind, Read}, diff --git a/tests/dns.rs b/tests/dns.rs index f5666091..7cec48d0 100644 --- a/tests/dns.rs +++ b/tests/dns.rs @@ -3,7 +3,7 @@ use std::{io::Write, net::TcpStream, process::Command, thread, time::Duration}; mod test_framework; -use coinswap::{protocol::messages::DnsRequest, utill::ConnectionType}; +use coinswap::protocol::DnsRequest; use test_framework::{init_bitcoind, start_dns}; fn send_addresses(addresses: &[(&str, u32)]) { @@ -46,7 +46,7 @@ fn verify_addresses(addresses: &[(&str, u32)]) { ); assert_eq!( addresses_output - .match_indices(&format!("vout: {}", index.to_string())) + .match_indices(&format!("vout: {}", index)) .count(), 1, "OP index {} not found",