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

Track HTLCs in rfq policies #1186

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

GeorgeTsagk
Copy link
Member

Description

This PR adds a simple in-memory tracking of accepted htlcs in the RFQ policy level. HTLCs that passed the policy check are now tracked by each policy, possibly affecting future policy check outcomes (depends on individual policy's implementation)

@GeorgeTsagk GeorgeTsagk self-assigned this Nov 11, 2024
@coveralls
Copy link

coveralls commented Nov 11, 2024

Pull Request Test Coverage Report for Build 12074140759

Details

  • 0 of 134 (0.0%) changed or added relevant lines in 5 files are covered.
  • 19 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.09%) to 40.796%

Changes Missing Coverage Covered Lines Changed/Added Lines %
rfq/manager.go 0 1 0.0%
tapcfg/server.go 0 1 0.0%
chain_bridge.go 0 4 0.0%
rpcserver.go 0 16 0.0%
rfq/order.go 0 112 0.0%
Files with Coverage Reduction New Missed Lines %
tappsbt/create.go 2 53.22%
tapchannel/aux_leaf_signer.go 3 43.43%
universe/interface.go 4 51.95%
tapgarden/caretaker.go 4 68.87%
tapdb/multiverse.go 6 68.21%
Totals Coverage Status
Change from base Build 12057455142: -0.09%
Covered Lines: 25787
Relevant Lines: 63210

💛 - Coveralls

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.

As mentioned offline, we also need to track and take into account canceled HTLCs from previous attempts.
These can be subscribed to with https://lightning.engineering/api-docs/api/lnd/router/subscribe-htlc-events.

// TrackAcceptedHtlc accounts for the newly accepted htlc. This may affect the
// acceptance of future htlcs.
func (c *AssetSalePolicy) TrackAcceptedHtlc(htlc lndclient.InterceptedHtlc) {
c.CurrentAmountMsat += htlc.AmountOutMsat
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if these need to be atomic values? Since multiple goroutines could manipulate them concurrently.

rfq/order.go Show resolved Hide resolved
Copy link
Contributor

@ffranr ffranr left a comment

Choose a reason for hiding this comment

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

I'm up-to-date with this PR. I agree with Oli's comments.

@GeorgeTsagk
Copy link
Member Author

@ffranr @guggero
I updated the PR to now include an htlc event subscription.
We now maintain a map of htlc circuit keys to rfq policy
When an htlc forward is failed we retrieve the htlc circuit key, fetch the corresponding policy and have it to stop tracking the htlc.

@GeorgeTsagk
Copy link
Member Author

Will also see how to add some coverage here

@ffranr
Copy link
Contributor

ffranr commented Nov 19, 2024

Will also see how to add some coverage here

@GeorgeTsagk should i review this before you add coverage or wait for that extra changes?

@GeorgeTsagk
Copy link
Member Author

Will also see how to add some coverage here

@GeorgeTsagk should i review this before you add coverage or wait for that extra changes?

You can review
Coverage will soon be added in the LitD itests

tapchannel/aux_leaf_signer.go Outdated Show resolved Hide resolved
tapchannel/aux_leaf_signer.go Outdated Show resolved Hide resolved
tapchannel/aux_leaf_signer.go Outdated Show resolved Hide resolved
tapchannel/aux_leaf_signer_test.go Outdated Show resolved Hide resolved
rfq/order.go Outdated Show resolved Hide resolved
rfq/order.go Show resolved Hide resolved
@GeorgeTsagk GeorgeTsagk force-pushed the rfq-track-accepted-amount branch 2 times, most recently from af862cd to 0dd58d6 Compare November 28, 2024 09:46
@GeorgeTsagk
Copy link
Member Author

Rebased on #1224 to include manual rfq scid functionality in SendPayment

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.

Looks pretty good. Left a couple of comments to simplify the code and make it more concurrency safe.

@@ -376,9 +377,17 @@ func (l *LndRouterClient) DeleteLocalAlias(ctx context.Context, alias,
return l.lnd.Router.XDeleteLocalChanAlias(ctx, alias, baseScid)
}

func (l *LndRouterClient) SubscribeHtlcEvents(
Copy link
Member

Choose a reason for hiding this comment

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

nit: requires Godoc comment (for the same method in the HtlcSubscriber interface as well).

// AskAssetRate is the quote's asking asset unit to BTC conversion rate.
AskAssetRate rfqmath.BigIntFixedPoint

// htlcToAmt maps the unique htlc identifiers to the effective amount
// that they carry.
htlcToAmt lnutils.SyncMap[string, lnwire.MilliSatoshi]
Copy link
Member

Choose a reason for hiding this comment

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

So both the htlcToAmt and CurrentAmountMsat need to be in sync. If we use an atomic value for one and a sync map for the other, then it's still possible for a read from another goroutine to get a different value while we're updating things (e.g. the map was updated but the total not yet).

So I think because we have two separate values, we need a sync.RWMutex instead. But then we can use a normal map here at least.

@@ -152,7 +168,8 @@ func (c *AssetSalePolicy) CheckHtlcCompliance(
maxAssetAmount, c.AskAssetRate,
)

if htlc.AmountOutMsat > policyMaxOutMsat {
if (c.CurrentAmountMsat + htlc.AmountOutMsat) > policyMaxOutMsat {
// if htlc.AmountOutMsat > policyMaxOutMsat {
Copy link
Member

Choose a reason for hiding this comment

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

nit: commented out code.


c.CurrentAmountMsat += htlc.AmountOutMsat

htlcIDStr := htlcIdentifierStr(
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 can probably simplify the interface of this TrackAcceptedHtlc a bit by just passing in circuitKey and amount. Then it's IMO a bit more apparent what's happening (looks more like a map add and delete) and the policy doesn't need to know how the key is calculated.

func (c *AssetSalePolicy) TrackAcceptedHtlc(htlc lndclient.InterceptedHtlc) {
c.CurrentAmountMsat += htlc.AmountOutMsat

htlcIDStr := htlcIdentifierStr(
Copy link
Member

Choose a reason for hiding this comment

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

We can use structs as map keys directly. And as long as there are no pointer values in a struct (meaning the struct can be directly compared with == without the pointers leading to side effects), things work out as expected. So we can just use the circuit key directly as the map key, no need to convert to string.

return
}

c.CurrentAmountMsat -= amt
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 definitely need a mutex here. Otherwise another goroutine might read this value while we're waiting on the LoadAndDelete above to complete.

@@ -95,9 +103,17 @@ type AssetSalePolicy struct {
// the policy.
MaxOutboundAssetAmount uint64

// CurrentAssetAmountMsat is the total amount that is held currently in
// accepted htlcs.
Copy link
Member

Choose a reason for hiding this comment

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

nit: use the same capitalization for HTLC everywhere? So s/htlcs/HTLCs here and throughout the file.

// the policy that applies to it. We need this map because for failed
// HTLCs we don't have the RFQ data available, so we need to cache this
// info.
htlcToPolicy lnutils.SyncMap[string, Policy]
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, can use the circuit key as the map key directly. Here a sync map is enough and we don't need a mutex, as it's just a single map that needs to be synced.

case linkFail != nil:
// Fetch the policy that is related to this
// htlc.
policy, found :=
Copy link
Member

Choose a reason for hiding this comment

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

nit: format as:

				policy, found := h.htlcToPolicy.LoadAndDelete(
					htlcIDStr,
				)

instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 👀 In review
Development

Successfully merging this pull request may close these issues.

5 participants