Skip to content

Conversation

@TrezorHannes
Copy link
Owner

PR: Optimize fee_adjuster.py with Global Channel Cache

Summary of Changes

  • Added global channel cache to eliminate redundant LNDg API calls
  • Reduces API calls from 6 (one per peer) to 1 (one per script run)
  • Enables VPS migration by removing local file dependencies

Detailed Changes

fee_adjuster.py

  • Added _CHANNEL_CACHE global variable to store channel data across peer iterations
  • Added fetch_all_channels(config) function that fetches all channels once and caches by remote_pubkey
  • Refactored get_channels_to_modify(pubkey, config) to use cached data via simple dict lookup
  • Added logging to track cache usage and channel counts

Performance Impact

Before After
6 LNDg API calls (1 per peer) 1 LNDg API call (cached)
~1-2 seconds per peer Near-instant lookups after first fetch

Testing Strategy

  • Unit Tests: Existing calculate_fee_band_adjustment tests pass (4/4)
  • Manual Testing: --debug run verified identical fee calculations
  • VPS Testing: In progress (user testing on VPS)

What's Left for Later

  • Batch Amboss API time ranges (4 calls → 1 per peer)
  • Batch forward timestamp queries in get_last_peer_forwarding_timestamp
  • Add unit tests for fetch_all_channels() and get_channels_to_modify()

Checklist

  • Code follows project conventions
  • All tests passing locally (4/4)
  • No breaking changes to existing functionality
  • Config change (write_charge_lnd_file: false) handled locally (gitignored)

- Add fetch_all_channels() that fetches all channels once and caches by pubkey
- Refactor get_channels_to_modify() to use cached data instead of per-peer API calls
- Reduces API calls from N (one per peer) to 1 (one per script run)

Tests passing: 4/4
@TrezorHannes TrezorHannes self-assigned this Jan 16, 2026
@TrezorHannes TrezorHannes added the enhancement New feature or request label Jan 16, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @TrezorHannes, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a crucial performance enhancement to the fee_adjuster.py script by implementing a global caching mechanism for LNDg channel data. This change streamlines data retrieval, significantly cutting down on external API requests and improving the script's overall efficiency and responsiveness. Furthermore, it removes a barrier for deploying the script in cloud-based environments.

Highlights

  • Performance Optimization: Implemented a global channel cache in fee_adjuster.py to drastically reduce redundant LNDg API calls.
  • API Call Reduction: Decreased LNDg API calls from 6 per peer to a single call per script execution, leading to significant speed improvements.
  • VPS Compatibility: Facilitated migration to VPS environments by eliminating dependencies on local files for channel data.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a significant performance improvement by caching LNDg channel data, which will drastically reduce API calls from one per peer to one per script run. The implementation is clean and effectively uses a global cache. I've identified a high-severity issue with how the cache is handled during API failures, which could lead to incorrect behavior for subsequent operations within the same script run. Additionally, there's a medium-severity robustness issue related to date string parsing. I've provided a single, comprehensive code suggestion that addresses both of these points to make applying the fix straightforward. Overall, this is an excellent enhancement that will improve the script's efficiency and reliability.

- Move cache initialization after API success to prevent empty cache on failure
- Reset cache to None on API failure to allow retry on next call
- Add try-except fallback for date parsing without fractional seconds

Tests passing: 4/4
@TrezorHannes TrezorHannes merged commit 85fbfbe into main Jan 16, 2026
3 checks passed
@TrezorHannes TrezorHannes deleted the perf/optimize-fee-adjuster branch January 16, 2026 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants