Skip to content

feat(fees): inbound discount protection and global rebalance guard - #45

Merged
TrezorHannes merged 12 commits into
mainfrom
feat/inbound-rebalance-safety
Aug 27, 2026
Merged

feat(fees): inbound discount protection and global rebalance guard#45
TrezorHannes merged 12 commits into
mainfrom
feat/inbound-rebalance-safety

Conversation

@TrezorHannes

Copy link
Copy Markdown
Owner

Summary of Changes

1. Inbound Fee Discount Protection & Rebalance Guard

  • Implemented Inbound Fee Discount Protection in Other/fee_adjuster.py and dedicated daemon/standalone runner Other/rebalance_guard.py.
  • Prevents draining discounted liquidity through costly circular rebalances:
    • When a channel receives an inbound fee discount, LNDg's ar_out_target is automatically set to 100% (disabling outbound rebalancing on that channel).
    • Records channel-specific baseline targets (fee_adjuster_baseline_ar_out_target) before locking.
    • Automatically restores custom channel baseline targets once the channel rebalances and reaches the unlock liquidity threshold.

2. Dynamic Hysteresis & Unlock Condition

  • Implemented dynamic hysteresis so channels only unlock when liquidity rises safely above the discount threshold (e.g. discount applied below 20%, unlocked above 25-30% or when no longer stuck), preventing flip-flopping.

3. Global Rebalance Guard

  • Audits and synchronizes rebalance targets across 100% of managed channels, logging audit tables and ensuring fail-closed safety.

4. Comprehensive Test Suites & CI

  • Added tests/test_rebalance_guard.py (6 tests).
  • Expanded tests/test_fee_adjuster.py (14 tests).
  • Verified against CI workflow matrix (Python 3.10, 3.11, 3.12).
  • Total tests passing: 28 / 28.

- Cap inbound fee discounts to prevent unprofitable routing against exit channels
- Implement dynamic ar_out_target locking in fee_adjuster.py when inbound discounts are active
- Add standalone rebalance_guard.py auditor to protect all 112 channels across native LNDg and fee_adjuster
- Add full unit test coverage using TDD (23 passing tests)
- Update documentation and feeConfig.json.example
- Make max_inbound_discount_ppm optional (null for unlimited discount)
- Keep lock_ar_out_target_on_discount active independently
- Add README documentation for rebalance_guard.py
- Add unit test for disabled locking
- Persist original channel ar_out_target values in data/rebalance_targets_baseline.json
- Restore each channel to its specific baseline target upon liquidity recovery
- Fall back to default_restored_ar_out_target only when baseline is unknown
- Add unit tests for custom baseline restorations (26 passing tests)
- Prevent restoring intentionally unmanaged 100% channels (like bfx-lnd0)
- Only trigger RESTORE when a channel is recorded in baseline_map with a baseline < 100%
- Add test coverage for unmanaged 100% channels
@TrezorHannes TrezorHannes self-assigned this Aug 27, 2026
@TrezorHannes TrezorHannes added the enhancement New feature or request label Aug 27, 2026
@TrezorHannes
TrezorHannes merged commit 791b06b into main Aug 27, 2026
7 checks passed
@TrezorHannes
TrezorHannes deleted the feat/inbound-rebalance-safety branch August 27, 2026 19:42
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.

1 participant