Skip to content

Commit

Permalink
wrap breez services with replaceable variant
Browse files Browse the repository at this point in the history
If an app hibernates, the breez sdk may still run. However, any
connections to the outside world, like grpc connections will (may) stop
functioning. This means after hibernation the sdk needs to reconnect.
Hibernation is detected by awaiting a 'sleep' in a loop. If the sleep
has taken a long time, that means the app has been in hibernation.
Because many services have references to the node api, and other
services than the greenlight client may have been affected by
hibernation, the entire breezservices instance is recreated,
reconnected. In order to allow this, an internal variant of
breezservices was made. This internal instance can be replaced at any
time.

Because the breezservices code was moved to another file, with edits, I
took the liberty of putting all functions inside breezservices in
alphabetical order.
  • Loading branch information
JssDWt committed Oct 25, 2024
1 parent 114eba1 commit 1f0ad15
Show file tree
Hide file tree
Showing 15 changed files with 3,662 additions and 3,268 deletions.
6 changes: 3 additions & 3 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ impl BlockingBreezServices {
}

pub fn node_credentials(&self) -> SdkResult<Option<NodeCredentials>> {
self.breez_services.node_credentials()
rt().block_on(self.breez_services.node_credentials())
}

pub fn node_info(&self) -> SdkResult<NodeState> {
self.breez_services.node_info()
rt().block_on(self.breez_services.node_info())
}

pub fn sign_message(&self, req: SignMessageRequest) -> SdkResult<SignMessageResponse> {
Expand All @@ -168,7 +168,7 @@ impl BlockingBreezServices {
}

pub fn backup_status(&self) -> SdkResult<BackupStatus> {
self.breez_services.backup_status()
rt().block_on(self.breez_services.backup_status())
}

pub fn backup(&self) -> SdkResult<()> {
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-core/src/backup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
breez_services::BackupFailedData,
error::SdkResult,
internal_breez_services::BackupFailedData,
persist::db::{HookEvent, SqliteStorage},
BreezEvent, Config,
};
Expand Down
21 changes: 7 additions & 14 deletions libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ pub use sdk_common::prelude::{
};
use tokio::sync::Mutex;

use crate::breez_services::{self, BreezEvent, BreezServices, EventListener};
use crate::breez_services::{self, BreezServices};
use crate::chain::RecommendedFees;
use crate::error::{
ConnectError, ReceiveOnchainError, ReceivePaymentError, RedeemOnchainError, SdkError,
SendOnchainError, SendPaymentError,
};
use crate::internal_breez_services::{BreezEvent, EventListener};
use crate::lsp::LspInformation;
use crate::models::{Config, LogEntry, NodeState, Payment, SwapInfo};
use crate::{
Expand Down Expand Up @@ -332,22 +333,14 @@ pub fn sync() -> Result<()> {

/// See [BreezServices::node_credentials]
pub fn node_credentials() -> Result<Option<NodeCredentials>> {
block_on(async {
get_breez_services()
.await?
.node_credentials()
.map_err(anyhow::Error::new::<SdkError>)
})
block_on(async { get_breez_services().await?.node_credentials().await })
.map_err(anyhow::Error::new::<SdkError>)
}

/// See [BreezServices::node_info]
pub fn node_info() -> Result<NodeState> {
block_on(async {
get_breez_services()
.await?
.node_info()
.map_err(anyhow::Error::new::<SdkError>)
})
block_on(async { get_breez_services().await?.node_info().await })
.map_err(anyhow::Error::new::<SdkError>)
}

/// See [BreezServices::configure_node]
Expand Down Expand Up @@ -497,7 +490,7 @@ pub fn backup() -> Result<()> {

/// See [BreezServices::backup_status]
pub fn backup_status() -> Result<BackupStatus> {
block_on(async { get_breez_services().await?.backup_status() })
block_on(async { get_breez_services().await?.backup_status().await })
.map_err(anyhow::Error::new::<SdkError>)
}

Expand Down
Loading

0 comments on commit 1f0ad15

Please sign in to comment.