Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make coordinator more resilient #34

Merged
merged 5 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ ESCROW_NPUB=npub1hcsc4r3xc9ygnefp4eqyan9r46tjvd3w0dxk2hgydc9k6m5xd3jq2hkjqp

# Mint URL
MINT_URL=http://0.0.0.0:3338
#MINT_URL=https://mint.minibits.cash/Bitcoin
# MINT_URL=https://mint.minibits.cash/Bitcoin

# Nostr relays (comma separated)
NOSTR_RELAYS="ws://localhost:4736"
# NOSTR_RELAYS="wss://relay.damus.io, wss://relay.primal.net, wss://relay.nostr.band, wss://ftp.halifax.rwth-aachen.de/nostr, wss://nostr.mom, wss://relay.nostrplebs.com"
# NOSTR_RELAYS="wss://relay.damus.io, wss://relay.primal.net, wss://relay.nostr.band, wss://ftp.halifax.rwth-aachen.de/nostr, wss://nostr.mom, wss://relay.nostrplebs.com"
20 changes: 13 additions & 7 deletions coordinator/src/escrow_coordinator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use anyhow::anyhow;
use cashu_escrow_common::model::TradeContract;
use cdk::nuts::SecretKey as CDKSecretKey;
use hashes::hex::DisplayHex;
Expand Down Expand Up @@ -81,15 +80,13 @@ impl EscrowCoordinator {
}
}
} else if RelayPoolNotification::Shutdown == notification {
break Err(anyhow!(
"Got shutdown notification, breaking coordinator loop!"
));
error!("Got shutdown notification, restarting nostr client!");
self.reconnect_nostr_client().await?;
}
}
Err(RecvError::Closed) => {
break Err(anyhow!(
"Got closed error from channel, breaking coordinator loop!"
))
error!("Got closed error from channel, restarting nostr client...");
self.reconnect_nostr_client().await?;
}
Err(RecvError::Lagged(count)) => {
warn!("Lost {} events, resuming after that...", count);
Expand Down Expand Up @@ -136,4 +133,13 @@ impl EscrowCoordinator {
let trade_hash: [u8; 32] = hasher.finalize().into();
Ok((trade_hash, contract))
}

async fn reconnect_nostr_client(&self) -> anyhow::Result<()> {
self.nostr_client.client.disconnect().await?;
warn!("Reconnecting nostr client in 60 seconds...");
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
self.nostr_client.client.connect().await;
info!("Nostr client reconnected sucessfully!");
Ok(())
}
}
4 changes: 2 additions & 2 deletions coordinator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ async fn main() -> anyhow::Result<()> {
.init();

let keys = Keys::from_str(&env::var("ESCROW_NSEC")?)?;
let relays = env::var("NOSTR_RELAYS")?
let relays: Vec<String> = env::var("NOSTR_RELAYS")?
.split(',')
.map(String::from)
.collect();
let nostr_client = NostrClient::new(keys, relays).await?;
let nostr_client = NostrClient::new(keys.clone(), relays.clone()).await?;
info!(
"Coordinator npub: {}",
nostr_client.public_key().to_bech32()?
Expand Down