Skip to content

[3/7] multi: start using the new interfaces throughout the codebase #8252

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

Draft
wants to merge 9 commits into
base: elle-g175ChanDBUpdates
Choose a base branch
from

Conversation

ellemouton
Copy link
Collaborator

@ellemouton ellemouton commented Dec 6, 2023

This is part of the Gossip 1.75 epic.

Depends on #8164 so only the last 10 commits are new

In this PR, we start threading the new interfaces throughout the code base. This threading
process has been split up into 3 PRs. This one covers the following major changes:

  1. updating routing.Validate to validate the new Channel Announcement
  2. Let the channeldb.GraphCache use the new interfaces where possible
  3. use the new ChannelEdgeInfo interface throughout
  4. update the channeldb reject cache to also take block heights (instead of just blocks)
  5. Create a new update index for ChannelEdgePolicy2
  6. update HasChannelPolicy accordingly

@ellemouton ellemouton marked this pull request as draft December 6, 2023 13:33
@ellemouton ellemouton force-pushed the g175-thread-interfaces-1 branch from 9f01c45 to 73b8186 Compare January 11, 2024 15:13
@ellemouton ellemouton changed the base branch from master to elle-g175ChanDBUpdates January 11, 2024 15:13
@ellemouton ellemouton force-pushed the elle-g175ChanDBUpdates branch from be56e6f to 2d9ef0c Compare February 12, 2024 13:22
@ellemouton ellemouton force-pushed the g175-thread-interfaces-1 branch from 73b8186 to e6ea852 Compare February 12, 2024 13:23
Copy link
Contributor

coderabbitai bot commented Feb 12, 2024

Important

Review skipped

Auto reviews are limited to specific labels.

Labels to auto review (1)
  • llm-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

In preparation for CachedEdgePolicy being used to represent
ChannelEdgePolicy1 or ChannelEdgePolicy2, we update it to have
IsDisabled and HasMaxHTLC booleans (which can be extracted from both
messages) instead of having the MessageFlags and ChannelFlags which only
applies to ChannelEdgePolicy1.
Update the graph cache to use the new ChannelEdgePolicy and
ChannelEdgeInfo interfaces where possible.
Add a new ChannelUpdate2 message which can be returned with a Failure.
Also add a block_height member to the RoutingPolicy which will be
populated when the last_update field is not.
@ellemouton ellemouton force-pushed the elle-g175ChanDBUpdates branch from 2d9ef0c to 12d2106 Compare September 2, 2024 11:08
@ellemouton ellemouton force-pushed the g175-thread-interfaces-1 branch 2 times, most recently from b1baa7d to f05fe47 Compare September 2, 2024 11:22
@ellemouton ellemouton force-pushed the g175-thread-interfaces-1 branch from f05fe47 to fea428d Compare September 3, 2024 11:14
if err != nil {
return

// TODO(elle): Add inbound fees field to ChannelEdgePolicy2 &
Copy link
Member

Choose a reason for hiding this comment

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

I think so. This came up in the spec meeting a few weeks ago, and it's the prefect location imo:

  • Doing positive fees needs all senders to update.
  • In order to use the new gossip protocol, the sender will need to have updated to be able to validate the channels.
  • Therefore, the new gossip protocol is a great place to add inbound fees "officially".

We can use a tlv.OptionaRecordT here.

@@ -4878,6 +4883,83 @@ message ChannelUpdate {
bytes extra_opaque_data = 12;
}

message ChannelUpdate2 {
Copy link
Member

Choose a reason for hiding this comment

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

Given all the data is pretty much the same, do we actually need to make an entirely new proto?

One consideration is the signature field, but in that case, we can add a new enum to indicate which channel update type was encoded.

// construct the pk script from the announcement, and so we
// instead need to fetch the pk script.
if a.BitcoinKey1.IsNone() && a.BitcoinKey2.IsNone() {
pkScript, err := d.fetchPKScript(&a.ShortChannelID.Val)
Copy link
Member

Choose a reason for hiding this comment

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

IIRC previously this was done at the router level, given that's where the on chain validation actually occurs.

@@ -41,9 +43,47 @@ func (f rejectFlags) unpack() (bool, bool) {
// including the timestamps of its latest edge policies and whether or not the
// channel exists in the graph.
type rejectCacheEntry struct {
times *updateTimes
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we should actually just split the caches into two? Given that in the future, we may have two announcements for any given channel.

return err
}
}

if err := updateIndex.Put(indexKey[:], nil); err != nil {
if err := updateIndex.Put(indexKey, nil); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should consider splitting this bucket structure.

We commonly use this index to implement an in-order to send out updates ordered by timestamp to a peer:

lnd/channeldb/graph.go

Lines 1922 to 1948 in 13a7bec

edgeUpdateIndex := edges.NestedReadBucket(edgeUpdateIndexBucket)
if edgeUpdateIndex == nil {
return ErrGraphNoEdgesFound
}
nodes := tx.ReadBucket(nodeBucket)
if nodes == nil {
return ErrGraphNodesNotFound
}
// We'll now obtain a cursor to perform a range query within
// the index to find all channels within the horizon.
updateCursor := edgeUpdateIndex.ReadCursor()
var startTimeBytes, endTimeBytes [8 + 8]byte
byteOrder.PutUint64(
startTimeBytes[:8], uint64(startTime.Unix()),
)
byteOrder.PutUint64(
endTimeBytes[:8], uint64(endTime.Unix()),
)
// With our start and end times constructed, we'll step through
// the index collecting the info and policy of each update of
// each channel that has a last update within the time range.
for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil &&
bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() {

If we overload the key in this structure, then the ordering is inherently broken, and then requires extra logic to skip over those new entries for a peer that only understands the legacy update type.

Assuming that the desired behavior for an updated peer is to send both edge types, coalescing the block based and timestamp based, then we can implement a composite cursor/iterator (using slow/fast pointers) to create a consistently ordered response.

// timestamps.
//
// maps: blockHeight || chanID -> nil
edgeUpdate2IndexBucket = []byte("edge-update-2-index")
Copy link
Member

Choose a reason for hiding this comment

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

Ah scratch my other comment, it was partitioned after all!

}, nil

case *models.ChannelEdgePolicy2:
indexKey := make([]byte, 4+8)
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need the block height and the scid value? The scid encodes a block height (unrolled)

@@ -1157,13 +1157,84 @@ func (c *ChannelGraph) addChannelEdge(tx kvdb.RwTx,
return chanIndex.Put(b.Bytes(), chanKey[:])
}

// HasChannelEdge returns true if the database knows of a channel edge with the
func (c *ChannelGraph) HasChannelEdge(chanID uint64) (bool, bool, error) {
Copy link
Member

Choose a reason for hiding this comment

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

godoc comment was lost in the update.

// We'll query the cache with the shared lock held to allow multiple
// readers to access values in the cache concurrently if they exist.
c.cacheMu.RLock()
if entry, ok := c.rejectCache.get(chanID); ok {
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this entire method is a dup.

@@ -1279,6 +1350,122 @@ func (c *ChannelGraph) HasChannelEdge(
return upd1Time, upd2Time, exists, isZombie, nil
}

func (c *ChannelGraph) HasChannelEdge2(
chanID uint64) (uint32, uint32, bool, bool, error) {
Copy link
Member

Choose a reason for hiding this comment

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

I think time for a return value struct given we have 5 return values here.

Copy link
Member

Choose a reason for hiding this comment

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

Also these two methods look pretty much identical (just height vs time stamps). Minimally, we can refactor some of the duplicate inner logic into a new function. Going a step further: can we abstract away the height vs timestamp difference? So something like a UpdateOrder?

@saubyk saubyk added this to the 0.20.0 milestone Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants