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

Cache LSP information #919

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Cache LSP information #919

wants to merge 2 commits into from

Conversation

dangeross
Copy link
Collaborator

This PR caches the LspInformation and only refreshes it when a record of the opening fee params expires (hourly) or on reconnect. The aim is to help reduce the requests sent to the Breez server when we already have valid fee param promises.

@roeierez
Copy link
Member

roeierez commented Apr 8, 2024

@dangeross One of the requests to this is from invoice creation. We need that to match the invoice expiry. It seems that now we can get into a state where the expensive fee param will be chosen for the default one hour invoice?

@dangeross
Copy link
Collaborator Author

It seems that now we can get into a state where the expensive fee param will be chosen for the default one hour invoice?

No, if any of the fee param values are expired, it is deemed invalid and it requests an updated version from the server. It uses the same validate function as when validating the initial response from the server:
https://github.com/breez/breez-sdk/blob/64a928f0355d757b74c823a805b46d9f2faa07aa/libs/sdk-core/src/models.rs#L1203-L1211
and is re-validated when lsp_info() is used:
https://github.com/breez/breez-sdk/blob/14c8b3104e11c1cd7e74c9da5e9b0106fda174a7/libs/sdk-core/src/breez_services.rs#L1275-L1281

@dangeross
Copy link
Collaborator Author

@dangeross One of the requests to this is from invoice creation. We need that to match the invoice expiry. It seems that now we can get into a state where the expensive fee param will be chosen for the default one hour invoice?

Ah, I see I think, for the expiry of the invoice is needs to be valid

@dangeross
Copy link
Collaborator Author

I've added a skip_cache param, but it this case previously it was getting the latest lsp information anyway.

Comment on lines +2618 to +2633
let persisted_lsp_info =
persister.get_lsp_information()?.and_then(|lsp_info| {
match !skip_cache && lsp_info.opening_fee_params_list.is_valid() {
true => Some(lsp_info),
false => {
debug!("Ignoring LSP information: {:?} {:?}", skip_cache, lsp_info);
None
}
}
});
match persisted_lsp_info {
Some(lsp_info) => {
debug!("Using persisted LSP information");
Ok(lsp_info)
}
None => {
Copy link
Contributor

@ok300 ok300 Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let persisted_lsp_info =
persister.get_lsp_information()?.and_then(|lsp_info| {
match !skip_cache && lsp_info.opening_fee_params_list.is_valid() {
true => Some(lsp_info),
false => {
debug!("Ignoring LSP information: {:?} {:?}", skip_cache, lsp_info);
None
}
}
});
match persisted_lsp_info {
Some(lsp_info) => {
debug!("Using persisted LSP information");
Ok(lsp_info)
}
None => {
let persisted_lsp_info = persister.get_lsp_information()?;
let cached_lsp_info = persisted_lsp_info
.filter(|_| !skip_cache)
.filter(|lsp_info| lsp_info.opening_fee_params_list.is_valid());
match cached_lsp_info {
Some(lsp_info) => {
debug!("Using cached LSP info");
Ok(lsp_info)
}
None => {
debug!("Ignoring cached LSP info: skip_cache={skip_cache} lsp_info={lsp_info:?}");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a suggestion on how the first part of the method could be rewritten for clarity.

@ok300
Copy link
Contributor

ok300 commented Apr 15, 2024

IMO this looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants