Skip to content

Commit

Permalink
Basic refactor to introduce new maker structure.
Browse files Browse the repository at this point in the history
- configs separated.
- New Error structure for Maker defined.
- Handler functions in its own module.
- New Maker structure to manage states.
- Collateral changes in all other modules.
  • Loading branch information
rajarshimaitra committed Aug 16, 2023
1 parent 2c7cd7f commit f0befb1
Show file tree
Hide file tree
Showing 23 changed files with 1,811 additions and 503 deletions.
3 changes: 1 addition & 2 deletions src/bin/teleport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use structopt::StructOpt;

use teleport::{
error::TeleportError,
maker::server::MakerBehavior,
maker::MakerBehavior,
scripts::{
maker::run_maker,
market::download_and_display_offers,
Expand Down Expand Up @@ -180,7 +180,6 @@ fn main() -> Result<(), TeleportError> {
port.unwrap_or(6102),
Some(WalletMode::Testing),
maker_special_behavior,
None,
)?;
}
Subcommand::GetFidelityBondAddress { year_and_month } => {
Expand Down
26 changes: 25 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{error, io};

use crate::{market::directory::DirectoryServerError, wallet::WalletError};
use crate::{
maker::error::MakerError, market::directory::DirectoryServerError,
protocol::error::ContractError, wallet::WalletError,
};

// error enum for the whole project
// try to make functions return this
Expand All @@ -13,6 +16,9 @@ pub enum TeleportError {
Socks(tokio_socks::Error),
Wallet(WalletError),
Market(DirectoryServerError),
Json(serde_json::Error),
Maker(MakerError),
Contract(ContractError),
}

impl From<Box<dyn error::Error + Send>> for TeleportError {
Expand Down Expand Up @@ -50,3 +56,21 @@ impl From<DirectoryServerError> for TeleportError {
Self::Market(value)
}
}

impl From<serde_json::Error> for TeleportError {
fn from(value: serde_json::Error) -> Self {
Self::Json(value)
}
}

impl From<MakerError> for TeleportError {
fn from(value: MakerError) -> Self {
Self::Maker(value)
}
}

impl From<ContractError> for TeleportError {
fn from(value: ContractError) -> Self {
Self::Contract(value)
}
}
38 changes: 38 additions & 0 deletions src/maker/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[derive(Debug, Clone)]
pub struct MakerConfig {
pub port: u16,
pub heart_beat_interval_secs: u64,
pub rpc_ping_interval_secs: u64,
pub watchtower_ping_interval_secs: u64,
pub directory_servers_refresh_interval_secs: u64,
pub idle_connection_timeout: u64,
pub onion_addrs: String,
pub absolute_fee_sats: u64,
pub amount_relative_fee_ppb: u64,
pub time_relative_fee_ppb: u64,
pub required_confirms: u64,
// Minimum reaction time between contract transaction of two hops
pub min_contract_reaction_time: u16,
pub min_size: u64,
}

impl MakerConfig {
/// Init a default configuration with given port and address
pub fn init(port: u16, onion_addrs: String) -> Self {
Self {
port,
heart_beat_interval_secs: 3,
rpc_ping_interval_secs: 60,
watchtower_ping_interval_secs: 300,
directory_servers_refresh_interval_secs: 60 * 60 * 12, //12 Hours
idle_connection_timeout: 300,
onion_addrs,
absolute_fee_sats: 1000,
amount_relative_fee_ppb: 10_000_000,
time_relative_fee_ppb: 100_000,
required_confirms: 1,
min_contract_reaction_time: 48,
min_size: 10_000,
}
}
}
62 changes: 62 additions & 0 deletions src/maker/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::sync::{PoisonError, RwLockReadGuard, RwLockWriteGuard};

use bitcoin::secp256k1;

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

#[derive(Debug)]
pub enum MakerError {
IO(std::io::Error),
Json(serde_json::Error),
UnexpectedMessage { expected: String, got: String },
General(&'static str),
MutexWallet,
Secp(secp256k1::Error),
ContractError(ContractError),
Wallet(WalletError),
}

impl From<std::io::Error> for MakerError {
fn from(value: std::io::Error) -> Self {
Self::IO(value)
}
}

impl From<serde_json::Error> for MakerError {
fn from(value: serde_json::Error) -> Self {
Self::Json(value)
}
}

impl<'a> From<PoisonError<RwLockReadGuard<'a, Wallet>>> for MakerError {
fn from(_: PoisonError<RwLockReadGuard<'a, Wallet>>) -> Self {
Self::MutexWallet
}
}

impl<'a> From<PoisonError<RwLockWriteGuard<'a, Wallet>>> for MakerError {
fn from(_: PoisonError<RwLockWriteGuard<'a, Wallet>>) -> Self {
Self::MutexWallet
}
}

impl From<secp256k1::Error> for MakerError {
fn from(value: secp256k1::Error) -> Self {
Self::Secp(value)
}
}

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

impl From<WalletError> for MakerError {
fn from(value: WalletError) -> Self {
Self::Wallet(value)
}
}
Loading

0 comments on commit f0befb1

Please sign in to comment.