Skip to content

Commit

Permalink
Merge pull request #34 from f321x/resilientcoordinator
Browse files Browse the repository at this point in the history
Make coordinator more resilient
  • Loading branch information
rodant authored Oct 4, 2024
2 parents 96e5941 + 212ed88 commit d426ceb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
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

0 comments on commit d426ceb

Please sign in to comment.