Skip to content

forwarding ability: introduce db helpers#240

Merged
bitromortac merged 4 commits into
lightninglabs:faraday-forwarding-abilityfrom
bitromortac:2604-fwd-prep-10
May 13, 2026
Merged

forwarding ability: introduce db helpers#240
bitromortac merged 4 commits into
lightninglabs:faraday-forwarding-abilityfrom
bitromortac:2604-fwd-prep-10

Conversation

@bitromortac
Copy link
Copy Markdown
Contributor

Introduces helpers for the forwarding ability algorithm. A preview for how this is used can be seen here https://github.com/bitromortac/faraday/tree/2604-fwd-prep-11.

@bitromortac bitromortac requested a review from ViktorT-11 May 12, 2026 13:12
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, 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 several helper utilities and database access patterns to support the forwarding ability algorithm. By adding efficient data retrieval methods and a streaming iterator, the changes enable the system to process historical channel event data more effectively while maintaining memory efficiency through chunked pagination.

Highlights

  • New Quantile Utility: Added a generic Quantile function in chanevents/quantile.go to support statistical analysis of channel event data.
  • Database Enhancements: Introduced new SQL queries to fetch channel lists and the latest channel event before a specific timestamp, along with corresponding Go store methods.
  • Streaming Iterator: Implemented GetChannelEventsIter, a streaming iterator that uses chunked pagination to efficiently process large ranges of channel events.
  • Testing and Stability: Added comprehensive tests for the new quantile logic and verified the iterator's stability against concurrent data insertions.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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 the 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 counterproductive. 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.

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
Copy Markdown

@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 generic Quantile function for numeric types and expands the chanevents store with new retrieval methods, including a streaming iterator for channel events and utilities for mapping short channel IDs to peers. Review feedback identifies a compilation error in store.go due to a missing log import, highlights potential precision loss when calculating quantiles for integer types, and recommends explicitly listing columns in SQL queries instead of using SELECT * for better maintainability.

Comment thread chanevents/store.go Outdated
Comment on lines +381 to +382
log.Tracef("Iterating channel events for channel ID %d "+
"between %v and %v", channelID, startTime, endTime)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

The log.Tracef call will cause a compilation error because log is not defined or imported in this file. Please ensure the necessary logging package is imported and the log variable is initialized, similar to how it's done in other packages in this repository.

Comment thread chanevents/quantile.go Outdated
Comment thread db/sqlc/queries/chanevents.sql
Copy link
Copy Markdown
Contributor

@ViktorT-11 ViktorT-11 left a comment

Choose a reason for hiding this comment

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

Looks great, LGTM 🔥! Just leaving some suggestions below, non of which are required.

Comment thread chanevents/store.go Outdated
Comment thread chanevents/store_test.go Outdated
Comment thread chanevents/store_test.go Outdated
Comment thread chanevents/store.go

// GetLatestChannelUpdateBefore returns the latest channel event before a given
// time (exclusive). If no event is found, it returns (nil, nil).
func (s *Store) GetLatestChannelUpdateBefore(ctx context.Context,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could potentially make it optional to specify which event type this function uses (and then rename the function)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I skipped this as there's only a single caller right now.

Comment thread chanevents/store.go
Add GetLatestChannelUpdateBefore, which fetches the most recent
EventTypeUpdate strictly before a given instant. Forwarding-ability
analyses that summarise behaviour over a window must seed their state
from the channel's balance at the window's lower bound; without the
ability to look back past that bound, the first events in the window
have no baseline to compare against. The lookup tolerates missing
predecessors by returning (nil, nil), letting callers distinguish "no
prior update" from a genuine error. Coverage exercises both the present
and absent cases against the existing TestStore fixture.
Add ScidToPeerMap, which materialises a snapshot of every short channel
id paired with the pubkey of the channel's remote peer. Forwarding-data
sources index events by short channel id, but downstream analyses need
to attribute behaviour to the peer, not the channel. The map skips
channels whose short channel id is still zero (unconfirmed), so callers
see only fully advertised channels. Coverage extends TestStore with a
two-channel fixture pinning the join.
Add GetChannelByShortChanID, the inverse of AddChannel. The
forwarding-ability analyzer receives scids from lnd's forwarding history
and must map them back to the chanevents store's internal channel id to
query events.
Add Quantile, a generic linear-interpolation q-quantile over a slice of
sortable numeric values. The forwarding-ability analyzer needs to
characterise the distribution of historical forwarded amounts and uses a
configurable percentile as the headline statistic; lifting the
computation into its own helper keeps the analyzer focused on forwarding
logic and gives the quantile contract its own table-driven test that
covers the interpolation rule and the empty/out-of-bounds error paths.
@bitromortac bitromortac merged commit 65ef4ab into lightninglabs:faraday-forwarding-ability May 13, 2026
8 checks passed
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.

2 participants