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

Enhance update_add_htlc with remote peer's custom records #8660

Open
wants to merge 6 commits into
base: 0-19-staging
Choose a base branch
from

Conversation

GeorgeTsagk
Copy link
Collaborator

@GeorgeTsagk GeorgeTsagk commented Apr 17, 2024

Description

Based on #8633

This PR enhances the update_add_htlc message with a "custom records" field which may contain custom records that are meant to be transmitted to the direct peer.

This extra field is included in routerrpc.SendPaymentRequest and is also exposed in the forward interceptor

Closes #8618

@GeorgeTsagk GeorgeTsagk self-assigned this Apr 17, 2024
Copy link

coderabbitai bot commented Apr 17, 2024

Important

Auto Review Skipped

Auto reviews are disabled on this repository.

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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@@ -93,6 +93,7 @@ func (r *forwardInterceptor) onIntercept(
CustomRecords: htlc.CustomRecords,
OnionBlob: htlc.OnionBlob[:],
AutoFailHeight: htlc.AutoFailHeight,
WireCustomRecords: htlc.CustomPeerRecords,
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 this should just be htlc.ExtraData. This is everything else left over after we read all the current hard wired fields. Start on the latter commits, so this may have been something added elsewhere to manipulate the map version?

Copy link
Collaborator

Choose a reason for hiding this comment

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

also CustomPeerRecords doesn't exist yet


// WireRecordsBlob contains the TLV records blob that was included in
// the original wire message that added this HTLC.
WireRecordsBlob lnwire.CustomRecordsBlob
Copy link
Member

Choose a reason for hiding this comment

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

So I have something of this logic in: 99db910

Later commits then ensure that this is threaded all the way down to disk. This takes an approach of just accepting the blob, instead of the wire record map.

Copy link
Member

Choose a reason for hiding this comment

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

Should attempt to rebase over that. That also handles all the accounting for restart/retransmission logic not in this PR.

Copy link
Collaborator

@Crypt-iQ Crypt-iQ left a comment

Choose a reason for hiding this comment

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

first pass done, need to go over it again though

@@ -119,6 +121,24 @@ func (r *forwardInterceptor) resolveFromClient(
Action: htlcswitch.FwdActionResume,
})

case ResolveHoldForwardAction_RESUME_MODIFIED:
// Modify HTLC and resume forward.
outgoingAmtMsat := lnwire.MilliSatoshi(in.OutgoingAmountMsat)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this value always needs to be set o/w it will set the outgoing amt to 0?

if err := firstHopRecords.Validate(); err != nil {
return nil, err
}
payIntent.FirstHopCustomRecords = firstHopRecords
Copy link
Collaborator

Choose a reason for hiding this comment

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

FirstHopCustomRecords introduced in a later commit so this commit won't compile

@@ -713,7 +713,7 @@ func (r *ChannelRouter) Start() error {
// be tried.
_, _, err := r.sendPayment(
0, payment.Info.PaymentIdentifier, 0,
paySession, shardTracker,
paySession, shardTracker, nil,
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we resume the payment, shouldn't we include the TLVs?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we want to include this information on resume.

I think we want to add this to channeldb.PaymentCreationInfo as a new optional TLV field. We store the bytes in its own key, so no issue w.r.t migrations or anything.

@@ -93,6 +93,7 @@ func (r *forwardInterceptor) onIntercept(
CustomRecords: htlc.CustomRecords,
OnionBlob: htlc.OnionBlob[:],
AutoFailHeight: htlc.AutoFailHeight,
WireCustomRecords: htlc.CustomPeerRecords,
Copy link
Collaborator

Choose a reason for hiding this comment

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

also CustomPeerRecords doesn't exist yet

@@ -713,7 +713,7 @@ func (r *ChannelRouter) Start() error {
// be tried.
_, _, err := r.sendPayment(
0, payment.Info.PaymentIdentifier, 0,
paySession, shardTracker,
paySession, shardTracker, nil,
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we want to include this information on resume.

I think we want to add this to channeldb.PaymentCreationInfo as a new optional TLV field. We store the bytes in its own key, so no issue w.r.t migrations or anything.

@@ -963,6 +963,9 @@ message ForwardHtlcInterceptRequest {
// The block height at which this htlc will be auto-failed to prevent the
// channel from force-closing.
int32 auto_fail_height = 10;

// The custom records of the peer's wire message.
map<uint64, bytes> wire_custom_records = 11;
Copy link
Member

Choose a reason for hiding this comment

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

To make this explicit, I think we should name the field: incoming_htlc_custom_wire_records.

@@ -486,6 +486,21 @@ func (s *InterceptableSwitch) interceptForward(packet *htlcPacket,
return false, nil
}

var customRecordsBlob []byte

htlc.CustomRecordsBlob.WhenSome(
Copy link
Member

Choose a reason for hiding this comment

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

Other comment re ExtraData also applies here. Will only note it here to avoid duplicate comments.

)

customPeerRecords := new(record.CustomSet)
customPeerRecords.Decode(bytes.NewReader(customRecordsBlob))
Copy link
Member

Choose a reason for hiding this comment

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

Should propagate the decode error. IIUC, that can fail if the incoming wire records aren't canonical for w/e reason.

customPeerRecords := new(record.CustomSet)
customPeerRecords.Decode(bytes.NewReader(customRecordsBlob))

packet.wireRecords = *customPeerRecords
Copy link
Member

Choose a reason for hiding this comment

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

incomingWireRecords?

CustomRecords: f.packet.customRecords,
OnionBlob: f.htlc.OnionBlob,
AutoFailHeight: f.autoFailHeight,
CustomPeerRecords: f.packet.wireRecords,
Copy link
Member

Choose a reason for hiding this comment

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

Should add the Incoming prefix.

Amount: fwdInfo.AmountToForward,
PaymentHash: pd.RHash,
BlindingPoint: fwdInfo.NextBlinding,
CustomRecordsBlob: pd.WireRecordsBlob,
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I don't think we want to store this here. Instead, we should add it only to the htlcPacket field we generate. This'll transport the wire TLV records of the incoming HTLC. But here we've already modified the outgoing HTLC to have these values (the interceptor might wnat them to be different, or they shouldn't propagate to this hop as well).

Amount: fwdInfo.AmountToForward,
PaymentHash: pd.RHash,
BlindingPoint: fwdInfo.NextBlinding,
CustomRecordsBlob: pd.WireRecordsBlob,
Copy link
Member

Choose a reason for hiding this comment

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

Same here re only adding it to the htlcPacket.


// WireRecordsBlob contains the TLV records blob that was included in
// the original wire message that added this HTLC.
WireRecordsBlob lnwire.CustomRecordsBlob
Copy link
Member

Choose a reason for hiding this comment

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

Should attempt to rebase over that. That also handles all the accounting for restart/retransmission logic not in this PR.

HtlcIndex: lc.remoteUpdateLog.htlcCounter,
OnionBlob: htlc.OnionBlob[:],
BlindingPoint: htlc.BlindingPoint,
WireRecordsBlob: htlc.CustomRecordsBlob,
Copy link
Member

Choose a reason for hiding this comment

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

ExtraData see: a0f19a5#r1569611486

@GeorgeTsagk GeorgeTsagk changed the base branch from master to custom-channels-integration-rpc-channel May 2, 2024 14:50
@GeorgeTsagk GeorgeTsagk force-pushed the interceptor-wire-records branch 2 times, most recently from e9bd133 to 5203776 Compare May 2, 2024 18:02
@guggero guggero changed the base branch from custom-channels-integration-rpc-channel to custom-channels-integration May 2, 2024 18:54
@guggero guggero force-pushed the custom-channels-integration branch from 15abb14 to 09b34f1 Compare May 14, 2024 07:49
@lightninglabs-deploy
Copy link

@GeorgeTsagk, remember to re-request review from reviewers when ready

@dstadulis
Copy link
Collaborator

Fixes lightninglabs/taproot-assets#897

@GeorgeTsagk GeorgeTsagk force-pushed the interceptor-wire-records branch 2 times, most recently from ac58e73 to 3163902 Compare May 15, 2024 14:53
@GeorgeTsagk GeorgeTsagk changed the base branch from custom-channels-integration to 0-19-staging May 15, 2024 14:54
@GeorgeTsagk GeorgeTsagk changed the title Add interceptor access to update_add_htlc remote peer's custom records Enhance update_add_htlc with remote peer's custom records May 15, 2024
Copy link
Collaborator

@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.

Looks pretty good! Might need a rebase on top of @ffranr's latest state of #8633 (not sure if the unit test failures come from there or from this PR).

@@ -2550,6 +2550,8 @@ type HTLC struct {
// HTLC. It is stored in the ExtraData field, which is used to store
// a TLV stream of additional information associated with the HTLC.
BlindingPoint lnwire.BlindingPointRecord

CustomRecords lnwire.CustomRecords
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: missing Godoc comment. And maybe we should mention here that this will be encoded into the ExtraData alongside the BlindingPoint when encoded (for sending on the wire or storing to the DB).

@@ -3924,7 +4014,8 @@ func (lc *LightningChannel) createCommitDiff(newCommit *commitment,

// getUnsignedAckedUpdates returns all remote log updates that we haven't
// signed for yet ourselves.
func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate {
func (lc *LightningChannel) getUnsignedAckedUpdates() ([]channeldb.LogUpdate,
error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: add newline after multi-line method definition.

auxLeaves, err := AuxLeavesFromCommit(
chanState, *remoteCommitView.toDiskCommit(false), leafStore,
chanState, *diskCommit, leafStore,
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: can all fit on a single line now (same further below).


lc.Lock()
defer lc.Unlock()

pd := lc.htlcAddDescriptor(htlc, openKey)
if err := lc.validateAddHtlc(pd, buffer); err != nil {
pd, err := lc.htlcAddDescriptor(htlc, openKey)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like we dropped the call to lc.validateAddHtlc() here. Perhaps by accident? Sounds like that's still needed.

@@ -14,6 +14,26 @@ const (
MinCustomRecordsTlvType = 65536
)

// ParseCustomRecords decodes a set of custom records from a byte slice.
func ParseCustomRecords(b tlv.Blob) (CustomRecords, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like this ended up a duplicate of NewCustomRecordsFromTlvBlob below. So can just use that.
Or maybe rename NewCustomRecordsFromTlvBlob to ParseCustomRecords which is way shorter.

Copy link
Collaborator

@carlaKC carlaKC left a comment

Choose a reason for hiding this comment

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

First pass, a few high level comments:

  1. Already noted by other reviewers, but restart flows need some more attention here (both including the custom fields and ensuring we always have this data persisted)
  2. Meta/personal preference: move itest to the end of the PR rather than having several iterations of it throughout the code
  3. One consideration that this PR introduces is that we're now going to store all the custom records that we're send with a HTLC, which could result in our storing a whole lot of junk that our peers send us - this was originally discussed when we added the update that allows us to store this data. I originally thought that a good way to approach this would be to allow users to specify the custom TLV values they want to store (or hardcode some notable ones that we want to keep by default), but I haven't fully thought that design through tbh.

dbgInfo, err := bob.RPC.LN.GetDebugInfo(
ctx, &lnrpc.GetDebugInfoRequest{},
)
require.NoError(ht, err, "failed to get Barol node debug info")
Copy link
Collaborator

Choose a reason for hiding this comment

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

:s/Barol/Carol

Comment on lines 482 to 483
// testForwardInterceptorFirstHopRecords
func testForwardInterceptorFirstHopRecords(ht *lntest.HarnessTest) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: needs godoc

65537: []byte("test"),
},
}
stream := alice.RPC.SendPayment(sendReq)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would be good to test across restarts as well to get coverage for this comment

@@ -357,6 +357,10 @@ type InterceptedPacket struct {
// OnionBlob is the onion packet for the next hop
OnionBlob [lnwire.OnionPacketSize]byte

// WireCustomRecords are user-defined records that were defined by the
// peer that forwarded this htlc to us.
WireCustomRecords record.CustomSet
Copy link
Collaborator

Choose a reason for hiding this comment

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

This name is a little ambiguous to me (since everything arrives on the wire), WDYT of IncomingAddCustomRecords - a little longer but it's clearer about where these records come from, when I read the name without seeing the godoc for the field it's unclear to me whether these are from update_add_htlc or the htlc's payload.

@@ -426,6 +426,10 @@ var allTestCases = []*lntest.TestCase{
Name: "forward interceptor modified htlc",
TestFunc: testForwardInterceptorModifiedHtlc,
},
{
Name: "forward interceptor first hop records",
Copy link
Collaborator

Choose a reason for hiding this comment

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

structural comment: seems to me that it would make sense to move this itest commit to the end of the PR so that we don't have to have two iterations of it (one here, one adding coverage using the interceptor later)

@@ -894,8 +931,12 @@ func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight,

// Ensure that we'll restore any custom records which were stored as
// extra data on disk.
if len(htlc.ExtraData) != 0 {
pd.CustomRecords = fn.Some[tlv.Blob](htlc.ExtraData)
if len(htlc.CustomRecords) != 0 {
Copy link
Collaborator

@carlaKC carlaKC May 20, 2024

Choose a reason for hiding this comment

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

While we're setting the CustomRecords values above, we're not actually persisting them using the workaround added in #7710, which means that in certain restart cases we won't restore these records (diff to test db restore here).

So as-is we'll always have htlc.CustomRecords=0 because we're not persisting them with the channeldb.HTLC that needs to be turned into a PaymentDescriptor here.

The restart flow always takes me forever to reload into my brain, tried to write it all down here if that's at all helpful.

@@ -6416,6 +6570,16 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64, err
BlindingPoint: htlc.BlindingPoint,
}

if len(htlc.CustomRecords) > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If ExtraData contains a protocol-range TLV that isn't blinding point, all of our htlc.CustomRecords will be overwritten because we'll hit ExtraData != nil?

While we're not expecting this (there aren't any other defined protocol TLVs for update_add_htlc right now), I think we should account for the possibility that other nodes in the network may relay these fields in the future because as-is it'll break our code.

return err
}

addMsg.CustomRecords = r
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want to be overloading the CustomRecords field here?

  • pd.CustomRecords are the records we received on the incoming HTLC's update_add_htlc
  • addMsg.CustomRecords are the records that we're going to pack for the outgoing HTLC's update_add_htlc

This has the impact of always relaying all of the custom TLVs that we get on the incoming update_add_htlc to the outgoing update_add_htlc - confirmed this with a quick itest hack - and I don't see anything in the original issue saying that we want to do this?

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.

routing+rpc: enable senders to set a custom first-hop TLV blob for update_add_htlc
7 participants