Browse Source

Make `WalletInfo` in feed optional

In case the wallet sync fails, we replace the value with `None` to
indicate to the UI that we don't have an up-to-date wallet information.
In case the sync works again at some point, we will replace it with
`Some` again.

Making this optional also allows us to start up the system without
syncing the wallet, a step towards https://github.com/itchysats/itchysats/discussions/753.
feature/delete-wallet-cache-on-failure
Thomas Eizinger 3 years ago
parent
commit
4769bd9c23
No known key found for this signature in database GPG Key ID: 651AC83A6C6C8B96
  1. 3
      daemon/src/maker.rs
  2. 2
      daemon/src/routes_maker.rs
  3. 2
      daemon/src/routes_taker.rs
  4. 4
      daemon/src/taker.rs
  5. 12
      daemon/src/to_sse_event.rs
  6. 6
      daemon/src/wallet_sync.rs

3
daemon/src/maker.rs

@ -5,7 +5,6 @@ use bdk::{bitcoin, FeeRate};
use clap::{Parser, Subcommand};
use daemon::auth::{self, MAKER_USERNAME};
use daemon::model::cfd::Role;
use daemon::model::WalletInfo;
use daemon::seed::Seed;
use daemon::tokio_ext::FutureExt;
use daemon::{
@ -205,7 +204,7 @@ async fn main() -> Result<()> {
"ddd4636845a90185991826be5a494cde9f4a6947b1727217afedc6292fa4caf7",
)?;
let (wallet_feed_sender, wallet_feed_receiver) = watch::channel::<WalletInfo>(wallet_info);
let (wallet_feed_sender, wallet_feed_receiver) = watch::channel(Some(wallet_info));
let figment = rocket::Config::figment()
.merge(("address", opts.http_address.ip()))

2
daemon/src/routes_maker.rs

@ -29,7 +29,7 @@ pub type Maker = xtra::Address<
#[rocket::get("/feed")]
pub async fn maker_feed(
rx: &State<Feeds>,
rx_wallet: &State<watch::Receiver<WalletInfo>>,
rx_wallet: &State<watch::Receiver<Option<WalletInfo>>>,
_auth: Authenticated,
) -> EventStream![] {
let rx = rx.inner();

2
daemon/src/routes_taker.rs

@ -25,7 +25,7 @@ type Taker = xtra::Address<taker_cfd::Actor<oracle::Actor, monitor::Actor, walle
#[rocket::get("/feed")]
pub async fn feed(
rx: &State<Feeds>,
rx_wallet: &State<watch::Receiver<WalletInfo>>,
rx_wallet: &State<watch::Receiver<Option<WalletInfo>>>,
rx_maker_status: &State<watch::Receiver<ConnectionStatus>>,
) -> EventStream![] {
let rx = rx.inner();

4
daemon/src/taker.rs

@ -5,7 +5,7 @@ use bdk::{bitcoin, FeeRate};
use clap::{Parser, Subcommand};
use daemon::connection::connect;
use daemon::model::cfd::Role;
use daemon::model::{Identity, WalletInfo};
use daemon::model::Identity;
use daemon::seed::Seed;
use daemon::tokio_ext::FutureExt;
use daemon::{
@ -204,7 +204,7 @@ async fn main() -> Result<()> {
"ddd4636845a90185991826be5a494cde9f4a6947b1727217afedc6292fa4caf7",
)?;
let (wallet_feed_sender, wallet_feed_receiver) = watch::channel::<WalletInfo>(wallet_info);
let (wallet_feed_sender, wallet_feed_receiver) = watch::channel(Some(wallet_info));
let mut tasks = Tasks::default();

12
daemon/src/to_sse_event.rs

@ -46,13 +46,13 @@ pub struct WalletInfo {
last_updated_at: Timestamp,
}
impl ToSseEvent for model::WalletInfo {
impl ToSseEvent for Option<model::WalletInfo> {
fn to_sse_event(&self) -> Event {
let wallet_info = WalletInfo {
balance: self.balance,
address: self.address.to_string(),
last_updated_at: self.last_updated_at,
};
let wallet_info = self.as_ref().map(|wallet_info| WalletInfo {
balance: wallet_info.balance,
address: wallet_info.address.to_string(),
last_updated_at: wallet_info.last_updated_at,
});
Event::json(&wallet_info).event("wallet")
}

6
daemon/src/wallet_sync.rs

@ -5,7 +5,7 @@ use tokio::sync::watch;
use tokio::time::sleep;
use xtra::Address;
pub async fn new(wallet: Address<wallet::Actor>, sender: watch::Sender<WalletInfo>) {
pub async fn new(wallet: Address<wallet::Actor>, sender: watch::Sender<Option<WalletInfo>>) {
loop {
sleep(Duration::from_secs(10)).await;
@ -14,10 +14,10 @@ pub async fn new(wallet: Address<wallet::Actor>, sender: watch::Sender<WalletInf
.await
.expect("Wallet actor to be available")
{
Ok(info) => info,
Ok(info) => Some(info),
Err(e) => {
tracing::warn!("Failed to sync wallet: {:#}", e);
continue;
None
}
};

Loading…
Cancel
Save