-
Notifications
You must be signed in to change notification settings - Fork 114
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
base: main
Are you sure you want to change the base?
Track HTLCs in rfq policies #1186
Conversation
Pull Request Test Coverage Report for Build 12074140759Details
💛 - Coveralls |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
781102e
to
e559106
Compare
There was a problem hiding this 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.
e559106
to
e5edcfd
Compare
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 |
e5edcfd
to
1fd418e
Compare
af862cd
to
0dd58d6
Compare
0dd58d6
to
5c62e34
Compare
Rebased on #1224 to include manual rfq scid functionality in |
There was a problem hiding this 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( |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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 := |
There was a problem hiding this comment.
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.
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)