Skip to content

Commit 3143a02

Browse files
authored
fix(asb): Check for updates in background (#215)
1 parent d9d12fa commit 3143a02

File tree

4 files changed

+26
-66
lines changed

4 files changed

+26
-66
lines changed

swap/src/bin/asb.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use swap::asb::config::{
2929
};
3030
use swap::asb::{cancel, punish, redeem, refund, safely_abort, EventLoop, Finality, KrakenRate};
3131
use swap::common::tracing_util::Format;
32-
use swap::common::{self, check_latest_version, get_logs};
32+
use swap::common::{self, get_logs, warn_if_outdated};
3333
use swap::database::{open_db, AccessMode};
3434
use swap::network::rendezvous::XmrBtcNamespace;
3535
use swap::network::swarm;
@@ -63,12 +63,10 @@ pub async fn main() -> Result<()> {
6363
}
6464
};
6565

66-
// warn if we're not on the latest version
67-
if let Err(e) = check_latest_version(env!("CARGO_PKG_VERSION")).await {
68-
eprintln!("{}", e);
69-
}
66+
// Check in the background if there's a new version available
67+
tokio::spawn(async move { warn_if_outdated(env!("CARGO_PKG_VERSION")).await });
7068

71-
// read config from the specified path
69+
// Read config from the specified path
7270
let config = match read_config(config_path.clone())? {
7371
Ok(config) => config,
7472
Err(ConfigNotInitialized {}) => {
@@ -77,13 +75,13 @@ pub async fn main() -> Result<()> {
7775
}
7876
};
7977

80-
// initialize tracing
78+
// Initialize tracing
8179
let format = if json { Format::Json } else { Format::Raw };
8280
let log_dir = config.data.dir.join("logs");
8381
common::tracing_util::init(LevelFilter::DEBUG, format, log_dir, None)
8482
.expect("initialize tracing");
8583

86-
// check for conflicting env / config values
84+
// Check for conflicting env / config values
8785
if config.monero.network != env_config.monero_network {
8886
bail!(format!(
8987
"Expected monero network in config file to be {:?} but was {:?}",
@@ -118,12 +116,12 @@ pub async fn main() -> Result<()> {
118116
);
119117
}
120118

121-
// initialize monero wallet
119+
// Initialize Monero wallet
122120
let monero_wallet = init_monero_wallet(&config, env_config).await?;
123121
let monero_address = monero_wallet.get_main_address();
124122
tracing::info!(%monero_address, "Monero wallet address");
125123

126-
// check monero balance
124+
// Check Monero balance
127125
let monero = monero_wallet.get_balance().await?;
128126
match (monero.balance, monero.unlocked_balance) {
129127
(0, _) => {
@@ -146,14 +144,14 @@ pub async fn main() -> Result<()> {
146144
}
147145
}
148146

149-
// init bitcoin wallet
147+
// Initialize Bitcoin wallet
150148
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
151149
let bitcoin_balance = bitcoin_wallet.balance().await?;
152150
tracing::info!(%bitcoin_balance, "Bitcoin wallet balance");
153151

154152
let kraken_price_updates = kraken::connect(config.maker.price_ticker_ws_url.clone())?;
155153

156-
// setup Tor hidden services
154+
// Setup Tor hidden services
157155
let tor_client =
158156
tor::Client::new(config.tor.socks5_port).with_control_port(config.tor.control_port);
159157
let _ac = match tor_client.assert_tor_running().await {

swap/src/bin/swap.rs

-5
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@
1414
use anyhow::Result;
1515
use std::env;
1616
use swap::cli::command::{parse_args_and_apply_defaults, ParseResult};
17-
use swap::common::check_latest_version;
1817

1918
#[tokio::main]
2019
pub async fn main() -> Result<()> {
21-
if let Err(e) = check_latest_version(env!("CARGO_PKG_VERSION")).await {
22-
eprintln!("{}", e);
23-
}
24-
2520
match parse_args_and_apply_defaults(env::args_os()).await? {
2621
ParseResult::Success(context) => {
2722
context.tasks.wait_for_tasks().await?;

swap/src/cli/api.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,9 @@ pub mod api_test {
642642
json: bool,
643643
) -> Self {
644644
let data_dir = data::data_dir_from(data_dir, is_testnet).unwrap();
645-
646645
let seed = Seed::from_file_or_generate(data_dir.as_path()).unwrap();
647-
648646
let env_config = env_config_from(is_testnet);
647+
649648
Self {
650649
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
651650
env_config,

swap/src/common/mod.rs

+15-47
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,35 @@
11
pub mod tracing_util;
22

3-
use std::{collections::HashMap, path::PathBuf};
4-
53
use anyhow::anyhow;
4+
use std::{collections::HashMap, path::PathBuf};
65
use tokio::{
76
fs::{read_dir, File},
87
io::{AsyncBufReadExt, BufReader},
98
};
109
use uuid::Uuid;
1110

12-
const LATEST_RELEASE_URL: &str = "https://github.com/comit-network/xmr-btc-swap/releases/latest";
13-
14-
#[derive(Clone, Debug, PartialEq, Eq)]
15-
pub enum Version {
16-
Current,
17-
Available,
18-
}
11+
const LATEST_RELEASE_URL: &str = "https://github.com/UnstoppableSwap/core/releases/latest";
1912

20-
/// Check the latest release from GitHub API.
21-
pub async fn check_latest_version(current_version: &str) -> anyhow::Result<Version> {
13+
/// Check the latest release from GitHub and warn if we are not on the latest version.
14+
pub async fn warn_if_outdated(current_version: &str) -> anyhow::Result<()> {
15+
// Visit the Github releases page and check which url we are redirected to
2216
let response = reqwest::get(LATEST_RELEASE_URL).await?;
23-
let e = "Failed to get latest release.";
2417
let download_url = response.url();
25-
let segments = download_url.path_segments().ok_or_else(|| anyhow!(e))?;
26-
let latest_version = segments.last().ok_or_else(|| anyhow!(e))?;
2718

28-
let result = if is_latest_version(current_version, latest_version) {
29-
Version::Current
30-
} else {
19+
let segments = download_url
20+
.path_segments()
21+
.ok_or_else(|| anyhow!("Cannot split Github release URL into segments"))?;
22+
let latest_version = segments
23+
.last()
24+
.ok_or_else(|| anyhow!("Cannot extract latest version from Github release URL"))?;
25+
26+
if current_version != latest_version {
3127
tracing::warn!(%current_version, %latest_version, %download_url,
3228
"You are not on the latest version",
3329
);
34-
Version::Available
35-
};
36-
37-
Ok(result)
38-
}
30+
}
3931

40-
// todo: naive implementation can be improved using semver
41-
fn is_latest_version(current: &str, latest: &str) -> bool {
42-
current == latest
32+
Ok(())
4333
}
4434

4535
/// helper macro for [`redact`]... eldrich sorcery
@@ -196,25 +186,3 @@ pub fn redact_with(input: &str, replacements: &mut HashMap<String, String>) -> S
196186

197187
redacted
198188
}
199-
200-
#[cfg(test)]
201-
mod test {
202-
use super::*;
203-
204-
#[test]
205-
fn it_compares_versions() {
206-
assert!(is_latest_version("0.10.2", "0.10.2"));
207-
assert!(!is_latest_version("0.10.2", "0.10.3"));
208-
assert!(!is_latest_version("0.10.2", "0.11.0"));
209-
}
210-
211-
#[tokio::test]
212-
#[ignore = "For local testing, makes http requests to github."]
213-
async fn it_compares_with_github() {
214-
let result = check_latest_version("0.11.0").await.unwrap();
215-
assert_eq!(result, Version::Available);
216-
217-
let result = check_latest_version("0.11.1").await.unwrap();
218-
assert_eq!(result, Version::Current);
219-
}
220-
}

0 commit comments

Comments
 (0)