From 19836a10515d2e75d78f503f66faf80a7692d699 Mon Sep 17 00:00:00 2001 From: rishflab Date: Thu, 9 Sep 2021 21:14:57 +1000 Subject: [PATCH 1/4] Rename variables to match struct fields --- swap/src/asb/command.rs | 78 ++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/swap/src/asb/command.rs b/swap/src/asb/command.rs index 81019535..548a4f29 100644 --- a/swap/src/asb/command.rs +++ b/swap/src/asb/command.rs @@ -18,51 +18,51 @@ where let matches = RawArguments::clap().get_matches_from_safe(raw_args)?; let args = RawArguments::from_clap(&matches); - let is_json = args.json; - let is_testnet = args.testnet; + let json = args.json; + let testnet = args.testnet; let config = args.config; let command: RawCommand = args.cmd; let arguments = match command { RawCommand::Start { resume_only } => Arguments { - testnet: is_testnet, - json: is_json, - config_path: config_path(config, is_testnet)?, - env_config: env_config(is_testnet), + testnet, + json, + config_path: config_path(config, testnet)?, + env_config: env_config(testnet), cmd: Command::Start { resume_only }, }, RawCommand::History => Arguments { - testnet: is_testnet, - json: is_json, - config_path: config_path(config, is_testnet)?, - env_config: env_config(is_testnet), + testnet, + json, + config_path: config_path(config, testnet)?, + env_config: env_config(testnet), cmd: Command::History, }, RawCommand::WithdrawBtc { amount, address } => Arguments { - testnet: is_testnet, - json: is_json, - config_path: config_path(config, is_testnet)?, - env_config: env_config(is_testnet), + testnet, + json, + config_path: config_path(config, testnet)?, + env_config: env_config(testnet), cmd: Command::WithdrawBtc { amount, - address: bitcoin_address(address, is_testnet)?, + address: bitcoin_address(address, testnet)?, }, }, RawCommand::Balance => Arguments { - testnet: is_testnet, - json: is_json, - config_path: config_path(config, is_testnet)?, - env_config: env_config(is_testnet), + testnet, + json, + config_path: config_path(config, testnet)?, + env_config: env_config(testnet), cmd: Command::Balance, }, RawCommand::ManualRecovery(ManualRecovery::Redeem { redeem_params: RecoverCommandParams { swap_id }, do_not_await_finality, }) => Arguments { - testnet: is_testnet, - json: is_json, - config_path: config_path(config, is_testnet)?, - env_config: env_config(is_testnet), + testnet, + json, + config_path: config_path(config, testnet)?, + env_config: env_config(testnet), cmd: Command::Redeem { swap_id, @@ -72,35 +72,35 @@ where RawCommand::ManualRecovery(ManualRecovery::Cancel { cancel_params: RecoverCommandParams { swap_id }, }) => Arguments { - testnet: is_testnet, - json: is_json, - config_path: config_path(config, is_testnet)?, - env_config: env_config(is_testnet), + testnet, + json, + config_path: config_path(config, testnet)?, + env_config: env_config(testnet), cmd: Command::Cancel { swap_id }, }, RawCommand::ManualRecovery(ManualRecovery::Refund { refund_params: RecoverCommandParams { swap_id }, }) => Arguments { - testnet: is_testnet, - json: is_json, - config_path: config_path(config, is_testnet)?, - env_config: env_config(is_testnet), + testnet, + json, + config_path: config_path(config, testnet)?, + env_config: env_config(testnet), cmd: Command::Refund { swap_id }, }, RawCommand::ManualRecovery(ManualRecovery::Punish { punish_params: RecoverCommandParams { swap_id }, }) => Arguments { - testnet: is_testnet, - json: is_json, - config_path: config_path(config, is_testnet)?, - env_config: env_config(is_testnet), + testnet, + json, + config_path: config_path(config, testnet)?, + env_config: env_config(testnet), cmd: Command::Punish { swap_id }, }, RawCommand::ManualRecovery(ManualRecovery::SafelyAbort { swap_id }) => Arguments { - testnet: is_testnet, - json: is_json, - config_path: config_path(config, is_testnet)?, - env_config: env_config(is_testnet), + testnet, + json, + config_path: config_path(config, testnet)?, + env_config: env_config(testnet), cmd: Command::SafelyAbort { swap_id }, }, }; From 679b155db1746bb839dda81e5c4bae3cf9f36b7f Mon Sep 17 00:00:00 2001 From: rishflab Date: Thu, 9 Sep 2021 21:35:03 +1000 Subject: [PATCH 2/4] Enable log timestamps using explicit command line flag Previously logs were only timestamped when the ASB was run in an interactive terminal or if the logs were output as JSON. JSON logs and ASB output in an interactive terminal are no longer timestamped by default. --- CHANGELOG.md | 2 ++ swap/src/asb/command.rs | 34 ++++++++++++++++++++++++++++++++++ swap/src/asb/tracing.rs | 13 ++++++------- swap/src/bin/asb.rs | 3 ++- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f205f00..471c65e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Timestamping can now be enabled using the `timestamp` command line argument to the ASB. + JSON logs and ASB output in an interactive terminal are no longer timestamped by default. - The `cancel`, `refund` and `punish` subcommands in ASB and CLI are run with the `--force` by default and the `--force` option has been removed. The force flag was used to ignore blockheight and protocol state checks. Users can still restart a swap with these checks using the `resume` subcommand. diff --git a/swap/src/asb/command.rs b/swap/src/asb/command.rs index 548a4f29..a5a0699d 100644 --- a/swap/src/asb/command.rs +++ b/swap/src/asb/command.rs @@ -19,6 +19,7 @@ where let args = RawArguments::from_clap(&matches); let json = args.json; + let timestamp = args.timestamp; let testnet = args.testnet; let config = args.config; let command: RawCommand = args.cmd; @@ -27,6 +28,7 @@ where RawCommand::Start { resume_only } => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Start { resume_only }, @@ -34,6 +36,7 @@ where RawCommand::History => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::History, @@ -41,6 +44,7 @@ where RawCommand::WithdrawBtc { amount, address } => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::WithdrawBtc { @@ -51,6 +55,7 @@ where RawCommand::Balance => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Balance, @@ -61,6 +66,7 @@ where }) => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Redeem { @@ -74,6 +80,7 @@ where }) => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Cancel { swap_id }, @@ -83,6 +90,7 @@ where }) => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Refund { swap_id }, @@ -92,6 +100,7 @@ where }) => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Punish { swap_id }, @@ -99,6 +108,7 @@ where RawCommand::ManualRecovery(ManualRecovery::SafelyAbort { swap_id }) => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::SafelyAbort { swap_id }, @@ -158,6 +168,7 @@ pub struct BitcoinAddressNetworkMismatch { pub struct Arguments { pub testnet: bool, pub json: bool, + pub timestamp: bool, pub config_path: PathBuf, pub env_config: env::Config, pub cmd: Command, @@ -210,6 +221,13 @@ pub struct RawArguments { )] pub json: bool, + #[structopt( + short, + long = "timestamp", + help = "Adds a timestamp to the log messages" + )] + pub timestamp: bool, + #[structopt( long = "config", help = "Provide a custom path to the configuration file. The configuration file must be a toml file.", @@ -326,6 +344,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::Start { resume_only: false }, @@ -337,6 +356,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::History, @@ -348,6 +368,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::Balance, @@ -364,6 +385,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::WithdrawBtc { @@ -384,6 +406,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::Cancel { @@ -403,6 +426,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::Refund { @@ -422,6 +446,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::Punish { @@ -441,6 +466,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::SafelyAbort { @@ -460,6 +486,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::Start { resume_only: false }, @@ -471,6 +498,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::History, @@ -482,6 +510,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::Balance, @@ -499,6 +528,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::WithdrawBtc { @@ -520,6 +550,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::Cancel { @@ -540,6 +571,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::Refund { @@ -560,6 +592,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::Punish { @@ -580,6 +613,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::SafelyAbort { diff --git a/swap/src/asb/tracing.rs b/swap/src/asb/tracing.rs index bced94d4..dc3f7cca 100644 --- a/swap/src/asb/tracing.rs +++ b/swap/src/asb/tracing.rs @@ -3,7 +3,7 @@ use tracing_subscriber::filter::LevelFilter; use tracing_subscriber::fmt::time::ChronoLocal; use tracing_subscriber::FmtSubscriber; -pub fn init(level: LevelFilter, json_format: bool) -> Result<()> { +pub fn init(level: LevelFilter, json_format: bool, timestamp: bool) -> Result<()> { if level == LevelFilter::OFF { return Ok(()); } @@ -17,12 +17,11 @@ pub fn init(level: LevelFilter, json_format: bool) -> Result<()> { .with_timer(ChronoLocal::with_format("%F %T".to_owned())) .with_target(false); - if json_format { - builder.json().init(); - } else if is_terminal { - builder.init(); - } else { - builder.without_time().init(); + match (json_format, timestamp) { + (true, true) => builder.json().init(), + (true, false) => builder.json().without_time().init(), + (false, true) => builder.init(), + (false, false) => builder.without_time().init(), } tracing::info!(%level, "Initialized tracing"); diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index d8583aeb..79cd71e2 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -45,6 +45,7 @@ async fn main() -> Result<()> { let Arguments { testnet, json, + timestamp, config_path, env_config, cmd, @@ -66,7 +67,7 @@ async fn main() -> Result<()> { } }; - asb::tracing::init(LevelFilter::DEBUG, json).expect("initialize tracing"); + asb::tracing::init(LevelFilter::DEBUG, json, timestamp).expect("initialize tracing"); let config = match read_config(config_path.clone())? { Ok(config) => config, From e38005261088598abae7c3705239a4c651633001 Mon Sep 17 00:00:00 2001 From: rishflab Date: Fri, 10 Sep 2021 15:12:54 +1000 Subject: [PATCH 3/4] Split monolithic unit test into separate unit tests per assertion --- swap/src/asb/command.rs | 114 ++++++++++++++++++++++++++++++++++------ 1 file changed, 98 insertions(+), 16 deletions(-) diff --git a/swap/src/asb/command.rs b/swap/src/asb/command.rs index a5a0699d..542be5e2 100644 --- a/swap/src/asb/command.rs +++ b/swap/src/asb/command.rs @@ -336,7 +336,7 @@ mod tests { const SWAP_ID: &str = "ea030832-3be9-454f-bb98-5ea9a788406b"; #[test] - fn ensure_command_mapping_for_mainnet() { + fn ensure_start_command_mapping_mainnet() { let default_mainnet_conf_path = env::Mainnet::getConfigFileDefaults().unwrap().config_path; let mainnet_env_config = env::Mainnet::get_config(); @@ -345,37 +345,54 @@ mod tests { testnet: false, json: false, timestamp: false, - config_path: default_mainnet_conf_path.clone(), + config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::Start { resume_only: false }, }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_history_command_mapping_mainnet() { + let default_mainnet_conf_path = env::Mainnet::getConfigFileDefaults().unwrap().config_path; + let mainnet_env_config = env::Mainnet::get_config(); let raw_ars = vec![BINARY_NAME, "history"]; let expected_args = Arguments { testnet: false, json: false, timestamp: false, - config_path: default_mainnet_conf_path.clone(), + config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::History, }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_balance_command_mapping_mainnet() { + let default_mainnet_conf_path = env::Mainnet::getConfigFileDefaults().unwrap().config_path; + let mainnet_env_config = env::Mainnet::get_config(); let raw_ars = vec![BINARY_NAME, "balance"]; let expected_args = Arguments { testnet: false, json: false, timestamp: false, - config_path: default_mainnet_conf_path.clone(), + config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::Balance, }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + #[test] + fn ensure_withdraw_command_mapping_mainnet() { + let default_mainnet_conf_path = env::Mainnet::getConfigFileDefaults().unwrap().config_path; + let mainnet_env_config = env::Mainnet::get_config(); let raw_ars = vec![ BINARY_NAME, "withdraw-btc", @@ -386,7 +403,7 @@ mod tests { testnet: false, json: false, timestamp: false, - config_path: default_mainnet_conf_path.clone(), + config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::WithdrawBtc { amount: None, @@ -395,6 +412,12 @@ mod tests { }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_cancel_command_mapping_mainnet() { + let default_mainnet_conf_path = env::Mainnet::getConfigFileDefaults().unwrap().config_path; + let mainnet_env_config = env::Mainnet::get_config(); let raw_ars = vec![ BINARY_NAME, @@ -407,7 +430,7 @@ mod tests { testnet: false, json: false, timestamp: false, - config_path: default_mainnet_conf_path.clone(), + config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::Cancel { swap_id: Uuid::parse_str(SWAP_ID).unwrap(), @@ -415,6 +438,12 @@ mod tests { }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_refund_command_mappin_mainnet() { + let default_mainnet_conf_path = env::Mainnet::getConfigFileDefaults().unwrap().config_path; + let mainnet_env_config = env::Mainnet::get_config(); let raw_ars = vec![ BINARY_NAME, @@ -427,7 +456,7 @@ mod tests { testnet: false, json: false, timestamp: false, - config_path: default_mainnet_conf_path.clone(), + config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::Refund { swap_id: Uuid::parse_str(SWAP_ID).unwrap(), @@ -435,6 +464,12 @@ mod tests { }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_punish_command_mapping_mainnet() { + let default_mainnet_conf_path = env::Mainnet::getConfigFileDefaults().unwrap().config_path; + let mainnet_env_config = env::Mainnet::get_config(); let raw_ars = vec![ BINARY_NAME, @@ -447,7 +482,7 @@ mod tests { testnet: false, json: false, timestamp: false, - config_path: default_mainnet_conf_path.clone(), + config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::Punish { swap_id: Uuid::parse_str(SWAP_ID).unwrap(), @@ -455,6 +490,12 @@ mod tests { }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_safely_abort_command_mapping_mainnet() { + let default_mainnet_conf_path = env::Mainnet::getConfigFileDefaults().unwrap().config_path; + let mainnet_env_config = env::Mainnet::get_config(); let raw_ars = vec![ BINARY_NAME, @@ -478,7 +519,7 @@ mod tests { } #[test] - fn ensure_command_mapping_for_testnet() { + fn ensure_start_command_mapping_for_testnet() { let default_testnet_conf_path = env::Testnet::getConfigFileDefaults().unwrap().config_path; let testnet_env_config = env::Testnet::get_config(); @@ -487,36 +528,54 @@ mod tests { testnet: true, json: false, timestamp: false, - config_path: default_testnet_conf_path.clone(), + config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::Start { resume_only: false }, }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_history_command_mapping_testnet() { + let default_testnet_conf_path = env::Testnet::getConfigFileDefaults().unwrap().config_path; + let testnet_env_config = env::Testnet::get_config(); let raw_ars = vec![BINARY_NAME, "--testnet", "history"]; let expected_args = Arguments { testnet: true, json: false, timestamp: false, - config_path: default_testnet_conf_path.clone(), + config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::History, }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_balance_command_mapping_testnet() { + let default_testnet_conf_path = env::Testnet::getConfigFileDefaults().unwrap().config_path; + let testnet_env_config = env::Testnet::get_config(); let raw_ars = vec![BINARY_NAME, "--testnet", "balance"]; let expected_args = Arguments { testnet: true, json: false, timestamp: false, - config_path: default_testnet_conf_path.clone(), + config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::Balance, }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_withdraw_command_mapping_testnet() { + let default_testnet_conf_path = env::Testnet::getConfigFileDefaults().unwrap().config_path; + let testnet_env_config = env::Testnet::get_config(); let raw_ars = vec![ BINARY_NAME, @@ -529,7 +588,7 @@ mod tests { testnet: true, json: false, timestamp: false, - config_path: default_testnet_conf_path.clone(), + config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::WithdrawBtc { amount: None, @@ -538,6 +597,11 @@ mod tests { }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + #[test] + fn ensure_cancel_command_mapping_testnet() { + let default_testnet_conf_path = env::Testnet::getConfigFileDefaults().unwrap().config_path; + let testnet_env_config = env::Testnet::get_config(); let raw_ars = vec![ BINARY_NAME, @@ -551,7 +615,7 @@ mod tests { testnet: true, json: false, timestamp: false, - config_path: default_testnet_conf_path.clone(), + config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::Cancel { swap_id: Uuid::parse_str(SWAP_ID).unwrap(), @@ -559,6 +623,12 @@ mod tests { }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_refund_command_mapping_testnet() { + let default_testnet_conf_path = env::Testnet::getConfigFileDefaults().unwrap().config_path; + let testnet_env_config = env::Testnet::get_config(); let raw_ars = vec![ BINARY_NAME, @@ -572,7 +642,7 @@ mod tests { testnet: true, json: false, timestamp: false, - config_path: default_testnet_conf_path.clone(), + config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::Refund { swap_id: Uuid::parse_str(SWAP_ID).unwrap(), @@ -580,6 +650,12 @@ mod tests { }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_punish_command_mapping_testnet() { + let default_testnet_conf_path = env::Testnet::getConfigFileDefaults().unwrap().config_path; + let testnet_env_config = env::Testnet::get_config(); let raw_ars = vec![ BINARY_NAME, @@ -593,7 +669,7 @@ mod tests { testnet: true, json: false, timestamp: false, - config_path: default_testnet_conf_path.clone(), + config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::Punish { swap_id: Uuid::parse_str(SWAP_ID).unwrap(), @@ -601,6 +677,12 @@ mod tests { }; let args = parse_args(raw_ars).unwrap(); assert_eq!(expected_args, args); + } + + #[test] + fn ensure_safely_abort_command_mapping_testnet() { + let default_testnet_conf_path = env::Testnet::getConfigFileDefaults().unwrap().config_path; + let testnet_env_config = env::Testnet::get_config(); let raw_ars = vec![ BINARY_NAME, From ee6524a75f8326b6f9af4ca611568c0996a83ea7 Mon Sep 17 00:00:00 2001 From: rishflab Date: Fri, 10 Sep 2021 15:35:59 +1000 Subject: [PATCH 4/4] Timestamp logs by default on the ASB Disable timestamps using the command line flag --- CHANGELOG.md | 7 ++-- swap/src/asb/command.rs | 78 +++++++++++++++++++++++++---------------- swap/src/bin/asb.rs | 4 +-- 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 471c65e4..fa4731ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,12 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Timestamping can now be enabled using the `timestamp` command line argument to the ASB. - JSON logs and ASB output in an interactive terminal are no longer timestamped by default. +- Timestamping is now enabled by default even when the ASB is not run inside an interactive terminal. - The `cancel`, `refund` and `punish` subcommands in ASB and CLI are run with the `--force` by default and the `--force` option has been removed. The force flag was used to ignore blockheight and protocol state checks. Users can still restart a swap with these checks using the `resume` subcommand. +### Added + +- Added a `disable-timestamp` flag to the ASB that disables timestamps from logs. + ## [0.8.3] - 2021-09-03 ### Fixed diff --git a/swap/src/asb/command.rs b/swap/src/asb/command.rs index 542be5e2..63c0594c 100644 --- a/swap/src/asb/command.rs +++ b/swap/src/asb/command.rs @@ -19,7 +19,7 @@ where let args = RawArguments::from_clap(&matches); let json = args.json; - let timestamp = args.timestamp; + let disable_timestamp = args.disable_timestamp; let testnet = args.testnet; let config = args.config; let command: RawCommand = args.cmd; @@ -28,7 +28,7 @@ where RawCommand::Start { resume_only } => Arguments { testnet, json, - timestamp, + disable_timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Start { resume_only }, @@ -36,7 +36,7 @@ where RawCommand::History => Arguments { testnet, json, - timestamp, + disable_timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::History, @@ -44,7 +44,7 @@ where RawCommand::WithdrawBtc { amount, address } => Arguments { testnet, json, - timestamp, + disable_timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::WithdrawBtc { @@ -55,7 +55,7 @@ where RawCommand::Balance => Arguments { testnet, json, - timestamp, + disable_timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Balance, @@ -66,7 +66,7 @@ where }) => Arguments { testnet, json, - timestamp, + disable_timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Redeem { @@ -80,7 +80,7 @@ where }) => Arguments { testnet, json, - timestamp, + disable_timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Cancel { swap_id }, @@ -90,7 +90,7 @@ where }) => Arguments { testnet, json, - timestamp, + disable_timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Refund { swap_id }, @@ -100,7 +100,7 @@ where }) => Arguments { testnet, json, - timestamp, + disable_timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Punish { swap_id }, @@ -108,7 +108,7 @@ where RawCommand::ManualRecovery(ManualRecovery::SafelyAbort { swap_id }) => Arguments { testnet, json, - timestamp, + disable_timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::SafelyAbort { swap_id }, @@ -168,7 +168,7 @@ pub struct BitcoinAddressNetworkMismatch { pub struct Arguments { pub testnet: bool, pub json: bool, - pub timestamp: bool, + pub disable_timestamp: bool, pub config_path: PathBuf, pub env_config: env::Config, pub cmd: Command, @@ -223,10 +223,10 @@ pub struct RawArguments { #[structopt( short, - long = "timestamp", - help = "Adds a timestamp to the log messages" + long = "disable-timestamp", + help = "Disable timestamping of log messages" )] - pub timestamp: bool, + pub disable_timestamp: bool, #[structopt( long = "config", @@ -344,7 +344,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::Start { resume_only: false }, @@ -362,7 +362,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::History, @@ -380,7 +380,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::Balance, @@ -402,7 +402,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::WithdrawBtc { @@ -429,7 +429,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::Cancel { @@ -455,7 +455,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::Refund { @@ -481,7 +481,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::Punish { @@ -507,7 +507,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::SafelyAbort { @@ -527,7 +527,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::Start { resume_only: false }, @@ -545,7 +545,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::History, @@ -563,7 +563,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::Balance, @@ -587,7 +587,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::WithdrawBtc { @@ -614,7 +614,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::Cancel { @@ -641,7 +641,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::Refund { @@ -668,7 +668,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::Punish { @@ -695,7 +695,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, - timestamp: false, + disable_timestamp: false, config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::SafelyAbort { @@ -706,6 +706,24 @@ mod tests { assert_eq!(expected_args, args); } + #[test] + fn ensure_disable_timestamp_mapping() { + let default_mainnet_conf_path = env::Mainnet::getConfigFileDefaults().unwrap().config_path; + let mainnet_env_config = env::Mainnet::get_config(); + + let raw_ars = vec![BINARY_NAME, "--disable-timestamp", "start"]; + let expected_args = Arguments { + testnet: false, + json: false, + disable_timestamp: true, + config_path: default_mainnet_conf_path, + env_config: mainnet_env_config, + cmd: Command::Start { resume_only: false }, + }; + let args = parse_args(raw_ars).unwrap(); + assert_eq!(expected_args, args); + } + #[test] fn given_user_provides_config_path_then_no_default_config_path_returned() { let cp = PathBuf::from_str("/some/config/path").unwrap(); diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index 79cd71e2..ae3ead70 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -45,7 +45,7 @@ async fn main() -> Result<()> { let Arguments { testnet, json, - timestamp, + disable_timestamp, config_path, env_config, cmd, @@ -67,7 +67,7 @@ async fn main() -> Result<()> { } }; - asb::tracing::init(LevelFilter::DEBUG, json, timestamp).expect("initialize tracing"); + asb::tracing::init(LevelFilter::DEBUG, json, !disable_timestamp).expect("initialize tracing"); let config = match read_config(config_path.clone())? { Ok(config) => config,