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

Only fetch onchain transactions from LND within start_time and end_time range #202

Merged
merged 1 commit into from
Jan 21, 2025

Conversation

nordbjorn
Copy link
Contributor

@nordbjorn nordbjorn commented Jan 15, 2025

Ensure that the NodeAudit RPC makes use of the provided start_time and end_time arguments, in the sense that Faraday will only query the LND backend for onchain transactions using a block height range which corresponds to the start_time and end_time (with a buffer).

Motivation and Context

The NodeAudit request can currently fail for old LND nodes, with a lot of historical onchain activity. Even if the caller provides a small time range to produce the report for. This is because currently all onchain transactions are fetched from the LND backend, and then filtered in memory based on the given time range.

This PR aims to alleviate most of the problem by ensuring Faraday does not need to query the entire history of onchain transactions when the caller provides a time range for the report.

Related issue

#177

Implementation Notes

I did not find any obvious way to resolve a timestamp to a block height, which needs to be provided for the queries to the LND backend. For now I chose an approach where we do a binary search on the entire blockchain to find the block height just before the provided timestamp.

This means we need to look up several block headers for each timestamp we want to resolve. I think this approach is fine for now, and in this context, considering that this RPC is a slow and heavy one anyways. Worst case is that we need to fetch ~20 block headers per timestamp we look up.

Future improvements

  1. If we want to optimize the timestamp to block height resolving more, we could make use of this public API endpoint: https://mempool.space/docs/api/rest#get-block-timestamp
    This endpoint seems to do a lookup from an internal DB (see here), which is likely more performant. In that case the current binary search mechanism could serve as only a fallback for if the mempool.space API is unavailable

  2. We can also make use of pagination when querying onchain activity from the LND backend. This seems to require changes in the lndclient repository as well, to ensure that the GetTransactions RPC can be called with a index_offset and max_transactions, like described here

Pull Request Checklist

  • Update MinLndVersion if your PR uses new RPC methods or fields of lnd.

@nordbjorn nordbjorn force-pushed the feat/onchain-pagination-temp branch from e0d6aac to 74518d2 Compare January 15, 2025 15:04
@guggero guggero self-requested a review January 15, 2025 15:35
accounting/config.go Outdated Show resolved Hide resolved
accounting/config.go Outdated Show resolved Hide resolved
Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Thanks for the improvement!
Can you please squash the commits?

accounting/config.go Outdated Show resolved Hide resolved
accounting/config.go Outdated Show resolved Hide resolved
frdrpcserver/node_audit.go Outdated Show resolved Hide resolved
@nordbjorn nordbjorn force-pushed the feat/onchain-pagination-temp branch from a01826c to 901eff5 Compare January 16, 2025 14:07
@guggero guggero self-requested a review January 16, 2025 15:07
Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

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

tACK, LGTM 🎉

Thanks a lot for the fix!

frdrpcserver/node_audit.go Outdated Show resolved Hide resolved
@guggero guggero requested a review from bhandras January 17, 2025 16:26
@nordbjorn nordbjorn force-pushed the feat/onchain-pagination-temp branch from 901eff5 to 771d1ae Compare January 20, 2025 10:05
@guggero guggero requested a review from matheusavi January 20, 2025 10:08
Copy link

@matheusavi matheusavi left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@bhandras bhandras left a comment

Choose a reason for hiding this comment

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

LGTM, thank you! 🎉

// certain range, we will apply a buffer on the time range which we use to
// find start and end block heights. This should ensure we widen the block
// height range enough to fetch all relevant transactions within a time range.
const blockTimeRangeBuffer = time.Hour * 24
Copy link
Member

Choose a reason for hiding this comment

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

While 24 hours seems like a safe bet, in practice we could probably lower this to 12 hours.

@guggero guggero merged commit 529d9eb into lightninglabs:master Jan 21, 2025
5 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.

4 participants